From 5569e7e3331ddc01a00b6da1d7f15fac31c72dfb Mon Sep 17 00:00:00 2001 From: Kentaro Wada Date: Sat, 22 Aug 2015 11:59:58 +0900 Subject: [PATCH] Fetch and abort if unpublishing branch not found closes: https://github.com/kennethreitz/legit/issues/113 --- legit/cli.py | 10 +++++++--- legit/scm.py | 19 +++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/legit/cli.py b/legit/cli.py index dc458b7..25aa844 100644 --- a/legit/cli.py +++ b/legit/cli.py @@ -542,10 +542,14 @@ def display_version(): )) -def handle_abort(aborted): - print(colored.red('Error:'), aborted.message) +def handle_abort(aborted, type=None): + print('{0} {1}'.format(colored.red('Error:'), aborted.message)) print(black(str(aborted.log))) - print('Unfortunately, there was a merge conflict. It has to be merged manually.') + if type == 'merge': + print('Unfortunately, there was a merge conflict.' + ' It has to be merged manually.') + elif type == 'unpublish': + print('It seems that the remote branch has been already deleted.') sys.exit(1) diff --git a/legit/scm.py b/legit/scm.py index 158f072..b3b80b8 100644 --- a/legit/scm.py +++ b/legit/scm.py @@ -33,13 +33,13 @@ def __init__(self): self.log = None -def abort(message, log=None): +def abort(message, log=None, type=None): a = Aborted() a.message = message a.log = log - settings.abort_handler(a) + settings.abort_handler(a, type=type) def repo_check(require_remote=False): if repo is None: @@ -138,7 +138,8 @@ def smart_merge(branch, allow_rebase=True): return repo.git.execute([git, verb, branch]) except GitCommandError as why: log = repo.git.execute([git, verb, '--abort']) - abort('Merge failed. Reverting.', log='{0}\n{1}'.format(why, log)) + abort('Merge failed. Reverting.', + log='{0}\n{1}'.format(why, log), type='merge') @@ -180,7 +181,8 @@ def graft_branch(branch): log.append(msg) except GitCommandError as why: log = repo.git.execute([git,'merge', '--abort']) - abort('Merge failed. Reverting.', log='{0}\n{1}'.format(why, log)) + abort('Merge failed. Reverting.', + log='{0}\n{1}'.format(why, log), type='merge') out = repo.git.execute([git, 'branch', '-D', branch]) @@ -193,8 +195,13 @@ def unpublish_branch(branch): repo_check() - return repo.git.execute([git, - 'push', remote.name, ':{0}'.format(branch)]) + try: + return repo.git.execute([git, + 'push', remote.name, ':{0}'.format(branch)]) + except GitCommandError: + _, _, log = repo.git.execute([git, 'fetch', remote.name, '--prune'], + with_extended_output=True) + abort('Unpublish failed. Fetching.', log=log, type='unpublish') def publish_branch(branch):