From e830c38fa3a328aff080c5d0f93733d95038fd59 Mon Sep 17 00:00:00 2001 From: Rick van Hattem Date: Tue, 10 Apr 2018 05:12:09 +0200 Subject: [PATCH] Added auto-push support --- PyGitUp/git_wrapper.py | 34 ++++++++++++++++++++++++++++++ PyGitUp/gitup.py | 47 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 3 deletions(-) diff --git a/PyGitUp/git_wrapper.py b/PyGitUp/git_wrapper.py index 489473b..33b8e20 100644 --- a/PyGitUp/git_wrapper.py +++ b/PyGitUp/git_wrapper.py @@ -210,6 +210,40 @@ def fetch(self, *args, **kwargs): return stdout.strip() + def push(self, *args, **kwargs): + ''' Push commits to remote ''' + stdout = six.b('') + + # Execute command + cmd = self.git.push(as_process=True, *args, **kwargs) + + # Capture output + while True: + output = cmd.stdout.read(1) + + sys.stdout.write(output.decode('utf-8')) + sys.stdout.flush() + + stdout += output + + # Check for EOF + if output == six.b(""): + break + + # Wait for the process to quit + try: + cmd.wait() + except GitCommandError as error: + # Add more meta-information to errors + message = "'{0}' returned exit status {1}".format( + ' '.join(str(c) for c in error.command), + error.status + ) + + raise GitError(message, stderr=error.stderr, stdout=stdout) + + return stdout.strip() + def config(self, key): """ Return `git config key` output or None. """ try: diff --git a/PyGitUp/gitup.py b/PyGitUp/gitup.py index cf988f8..b63650d 100644 --- a/PyGitUp/gitup.py +++ b/PyGitUp/gitup.py @@ -99,7 +99,10 @@ class GitUp(object): 'rebase.arguments': None, 'rebase.auto': True, 'rebase.log-hook': None, - 'updates.check': True + 'updates.check': True, + 'push.auto': False, + 'push.tags': False, + 'push.all': False, } def __init__(self, testing=False, sparse=False): @@ -199,6 +202,9 @@ def run(self): if self.with_bundler(): self.check_bundler() + if self.settings['push.auto']: + self.push() + except GitError as error: self.print_error(error) @@ -314,6 +320,36 @@ def fetch(self): error.message = "`git fetch` failed" raise error + def push(self): + ''' + Push the changes back to the remote(s) after fetching + ''' + print('pushing...') + push_kwargs = {} + push_args = [] + + if self.settings['push.tags']: + push_kwargs['push'] = True + + if self.settings['push.all']: + push_kwargs['all'] = True + else: + if '.' in self.remotes: + self.remotes.remove('.') + + if not self.remotes: + # Only local target branches, + # `git push` will fail + return + + push_args.append(self.remotes) + + try: + self.git.push(*push_args, **push_kwargs) + except GitError as error: + error.message = "`git push` failed" + raise error + def log(self, branch, remote): """ Call a log-command, if set by git-up.fetch.all. """ log_hook = self.settings['rebase.log-hook'] @@ -586,11 +622,12 @@ def print_error(self, error): help='Be quiet, only print error messages.') @click.option('--no-fetch', '--no-f', is_flag=True, help='Don\'t try to fetch from origin.') -def run(version, quiet, no_f): # pragma: no cover +@click.option('-p', '--push/--no-push', default=None, + help='Push the changes after pulling successfully.') +def run(version, quiet, no_f, push, **kwargs): # pragma: no cover """ A nicer `git pull`. """ - if version: if NO_DISTRIBUTE: print(colored('Please install \'git-up\' via pip in order to ' @@ -605,9 +642,13 @@ def run(version, quiet, no_f): # pragma: no cover try: gitup = GitUp() + if push is not None: + gitup.settings['push.auto'] = push + # if arguments['--no-fetch'] or arguments['--no-f']: if no_f: gitup.should_fetch = False + except GitError: sys.exit(1) # Error in constructor else: