def cmd_push(self, flags, remote, branch): cur_remote, cur_branch = self.get_cur_remote_branch() remote, branch = remote or cur_remote, branch or cur_branch #self._assert_remote_branch(remote, branch) available_branch = self.get_available_remote_branches(remote) if 'force' in flags: args = self._get_args_list(flags) + [remote, branch] self.git.push(args) output = Color.green('Push --force completed') elif branch not in available_branch: output = Color.green( self.git.push(remote, branch) or 'Push completed (New Branch)') elif not self._is_ahead_commit(remote, branch): output = Color.yellow('Nothing to commit') else: if self._is_behind_commit(remote, branch): if 'rebase' not in flags: raise ValueError( 'Merge is not allowed. You need to use --rebase to push' ) self.cmd_rebase(remote, branch) output = Color.green( self.git.push(remote, branch) or 'Push completed') return output
def run(self): """ :param str git_cmd: :rtype list(str) """ if len(self.packages) == 0: print(Color.red('There is not packages selected')) exit(-1) global pool_packages print(Color.yellow('Following command "git %s" is about to run on:\n' % self.git_cmd)) for package in self.packages: print("\t" + package.get_name()) # raw_input(Color.green('\n\nPress Enter to continue...')) for package in self.packages: pool_packages.add(package.get_name()) if self.pool: self.pool.apply_async(execute_package, [package, self.git_cmd, self.git_args], callback=lambda output, package=package: competed_package(package, output)) else: output = execute_package(package, self.git_cmd, self.git_args) competed_package(package, output) if not self.pool: return try: while len(pool_packages): print "Waiting packages: %s" % str(pool_packages) time.sleep(1) except KeyboardInterrupt: terminate = True; print "Interrupt!!!" else: terminate = False; if terminate: self.pool.terminate() print "Waiting threads to complete"; self.pool.close() print "Waiting threads to wrap-up"; self.pool.join()
def cmd_commit(self, flags, message): if not message: output = Color.red('Commit message cannot be empty') elif not self._has_local_changes(): output = Color.yellow('There is not local changes') else: flags['-m'] = message args = self._get_args_list(flags) output = Color.green(self.git.commit(args)) return output
def cmd_bash(self, bash_cmd): try: return subprocess.check_output('cd %s; %s ;cd -' % (self.location, bash_cmd), shell=True) except OSError as e: return Color.red(e.strerror)
def execute_package(package, git_cmd, git_args): """ :param Package package: :param str git_cmd: :param list git_args: :rtype str: """ try: output = Workspace.run_cmd(package, git_cmd, git_args) except GitCommandError as e: output = Color.red(e.stderr or e.stdout) except ValueError as e: output = Color.red(e.message) except: output = Color.red(traceback.format_exc()) return output
def cmd_diff(self, flags, remote, branch): cur_remote, cur_branch = self.get_cur_remote_branch() branch = branch or cur_branch self._assert_remote_branch(remote, branch) list_args = self._get_args_list(flags) if remote: list_args += ['%s/%s' % (remote, branch)] else: list_args += [branch] output = self.git.diff(*list_args) if not output: output = Color.yellow("There is not changes") return output
def cmd_checkout(self, flags, branch_name, from_branch=None): available_branches = self.get_available_local_branches() if not branch_name: raise ValueError('Branch name missing') if '-b' in flags: if branch_name in available_branches: raise ValueError( 'Failing creating branch "%s". Already exists' % branch_name) if from_branch: self.git.checkout(['-b', flags['-b'], from_branch]) else: self.git.checkout(['-b', flags['-b']]) output = Color.green(('New branch %s created' % flags['-b']) + (' from %s' % from_branch if from_branch else '')) else: if from_branch: raise ValueError('Invalid syntax') if branch_name not in available_branches: raise ValueError('Branch "%s" does not exists.' % branch_name) self.git.checkout(branch_name) output = Color.green('Checkout %s branch' % branch_name) return output
def cmd_reset(self, flags, remote, branch): self._assert_remote_branch(remote, branch) try: if 'stash' in flags and self._has_local_changes(): stashed = self.git.stash(u=True) if 'soft' in flags: args = ['--soft'] + ([branch] if remote is None else [remote + '/' + branch]) self.git.reset(args) output = Color.green( 'Reset soft was executed on %s' % (branch if remote is None else remote + '/' + branch)) elif 'hard' in flags: args = ['--hard'] + ([branch] if remote is None else [remote + '/' + branch]) output = Color.green(self.git.reset(args)) else: raise ValueError('Invalid mode') finally: if 'stashed' in locals() and stashed: self.git.stash('pop') return output
def cmd_clean(self, remote, branch): self._assert_remote_branch(remote, branch) cur_remote, cur_branch = self.get_cur_remote_branch() if cur_branch == branch: raise ValueError('Cannot remove branch in use "%s"' % branch) self.git.branch(['-D', branch]) output = Color.green('Branch "%s" was removed' % branch) if not remote: return output available_remotes = self.get_available_remotes() if remote not in available_remotes: raise ValueError('Remote "%s" was not found' % remote) available_remote_branches = self.get_available_remote_branches(remote) if branch in available_remote_branches: self.git.push([remote, ':' + branch]) output += "\n" + Color.green('Remove branch "%s/%s" removed' % (remote, branch)) else: output += "\n" + Color.yellow('Remote branch "%s/%s" not found' % (remote, branch)) return output
def cmd_rebase(self, remote, branch): cur_remote, cur_branch = self.get_cur_remote_branch() self._assert_remote_branch(remote, branch) try: if self._has_local_changes(): stashed = self.git.stash(u=True) != u'No local changes to save' if remote: output = self.git.rebase('%s/%s' % (remote, branch)) else: output = self.git.rebase(branch) except GitCommandError as e: self.git.rebase(abort=True) self.git.checkout(cur_branch) print(Color.red("ERR: Rebasing")) raise e finally: if 'stashed' in locals() and stashed: self.git.stash('pop') return output
def get_packages(src, all_packages, only_local_changes, only_no_prod, package_names): """ :param string src: Source path :param bool only_local_changes: :param book only_no_prod: :param list(string) packages: :return: """ folders = [path.join(src, f) for f in listdir(path.join(src)) if not path.isfile(path.join(src, f))] packages = [Package(pf) for pf in folders if path.isdir(path.join(pf, '.git'))] if len(packages) == 0: print(Color.red('Empty workspace. None found `./*/.git` folders')) exit(-1) return [package for package in packages if all_packages or (only_local_changes and package._has_local_changes()) or (packages and package.get_name() in package_names) or (only_no_prod and package.get_cur_remote_branch(True) != environ['prod_branch'])]