def get_git_username(git: Git) -> str: """Get username from settings or from user input. Args: git: (git.cmd.Git) Manages communication with the Git binary. Returns: username string. """ try: # Required mount: $HOME/.gitconfig:/home/cluster.dev/.gitconfig:ro user = git.config('--get', 'user.name') except GitCommandError: user = ask_user( name='git_user_name', type='input', message='Please enter your git username', validate=validate.UserName, ) else: logger.info(f'Username: {user}') return user
def createRelease(self, name, parent=None): super(PuppetInstallMethod, self).createRelease(name, parent) with puppet_lock: # Move to concrete directory name orig_name = name name = name.replace("/", "@") # Clone repository cmd = Git(self.__work_path) if parent: if isinstance(parent, StringTypes): parent = parent.replace("/", "@") else: parent = parent.name.replace("/", "@") self.log.debug("cloning new git branch '%s' from '%s'" % (name, parent)) cmd.clone(self.__repo_path, name, b=parent) else: self.log.debug("creating new git branch '%s'" % name) cmd.clone(self.__repo_path, name) # Switch branch, add information cmd = Git(os.path.join(self.__work_path, name)) host = self.env.id cmd.config("--global", "user.name", "GOsa management agent on %s" % host) self.log.debug("switching to newly created branch") cmd.checkout(b=name) # Remove refs if there's no parent current_dir = os.path.join(self.__work_path, name) if not parent: self.log.debug("no parent set - removing refs") cmd.symbolic_ref("HEAD", "refs/heads/newbranch") os.remove(os.path.join(current_dir, ".git", "index")) files = os.listdir(current_dir) # Remove all but .git for f in files: if f== ".git": continue if os.path.isdir(f): shutil.rmtree(os.path.join(current_dir, f)) else: os.unlink(os.path.join(current_dir, f)) # Create release info file self.log.debug("writing release info file in %s" % current_dir) with open(os.path.join(current_dir, "release.info"), "w") as f: now = datetime.now() f.write("Release: %s\n" % orig_name) f.write("Date: %s\n" % now.strftime("%Y-%m-%d %H:%M:%S")) self.log.debug("comitting new release") cmd.add("release.info") cmd.commit(m="Created release information") # Push to origin self.log.debug("pushing change to central repository") cmd.push("origin", name) return True