Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added auto-push support #74

Merged
merged 2 commits into from
Apr 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions PyGitUp/git_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
47 changes: 44 additions & 3 deletions PyGitUp/gitup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -586,12 +622,13 @@ 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.')
@click.option('-p', '--push/--no-push', default=None,
help='Push the changes after pulling successfully.')
@click.help_option('-h', '--help')
def run(version, quiet, no_f): # pragma: no cover
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 '
Expand All @@ -606,9 +643,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:
Expand Down