diff --git a/git_redmine.py b/git_redmine.py index 8d793d8..dcbb5e7 100644 --- a/git_redmine.py +++ b/git_redmine.py @@ -407,6 +407,60 @@ def merge_and_push(target_branch): raise +@redmine.command(name='rebase') +@click.argument('target_branch', default='master') +def rebase(target_branch): + repo = get_repo() + origin = repo.remote() + + if repo.head.is_detached: + raise click.UsageError('Your cannot rebase from a detached HEAD.') + + if repo.is_dirty(): + raise click.UsageError('Your cannot rebase, your repo is dirty.') + + current_head = repo.head.ref.name + + if current_head == target_branch: + raise click.UsageError(u'Your cannot rebase on « %s » as your are already on it.' % target_branch) + + try: + repo.branches[target_branch] + except IndexError: + raise click.UsageError('%r is not a local branch.' % target_branch) + + try: + click.echo(u'Checking-out branch « %s » ... ' % target_branch, nl=False) + repo.branches[target_branch].checkout() + click.echo(click.style('Done.', fg='green')) + click.echo(u'Pull-rebasing from remote « %s » onto branch « %s » ... ' % (origin.name, target_branch), nl=False) + failure = False + for pi in origin.pull(rebase=True): + if pi.flags & pi.ERROR: + failure = True + click.echo(click.style(u'Pull-rebase from « %s » failed: %s.' % ( + pi.ref.name, pi.note), fg='red')) + click.echo(click.style('Done.', fg='green')) + if failure: + raise click.ClickException('Pull rebase failed.') + finally: + click.echo(u'Checking-out branch « %s »... ' % current_head, nl=False) + repo.branches[current_head].checkout() + click.echo(click.style('Done.', fg='green')) + + try: + click.echo(u'Rebasing branch « %s » onto branch « %s » ... ' % (current_head, target_branch), nl=False) + repo.git.rebase(target_branch) + except git.GitCommandError as e: + click.echo(click.style('command %r failed, aborting.' % e.command, fg='red')) + try: + repo.git.rebase(abort=True) + except git.GitCommandError as e: + click.echo(click.style('rebase abort failed, %s\n%s.' % (e.stdout, e.stderr), fg='red')) + raise click.Abort() + click.echo(click.style('Done.', fg='green')) + + @issue.command(name='open') @click.option('--issue', default=None, type=int) def _open(issue):