add cloning ability of remote branch to "issue take" command

This commit is contained in:
Benjamin Dauvergne 2022-01-29 08:47:50 +01:00
parent 8e16d6c988
commit 1e40d313ee
2 changed files with 32 additions and 13 deletions

5
README
View File

@ -53,8 +53,9 @@ subject and description of the issue.::
issue take
..........
Handle the given issue by creating or switching to a branch named
`wip/<issue-number>-<slugified-issue-subject>` forking from `master`.::
Handle the given issue by cloning an existing remote branch for the issue,
creating or switching to a branch named `wip/<issue-number>-<issue-subject>`
forking from `master`.::
git redmine issue take 123

View File

@ -229,17 +229,35 @@ def take(issue_number, reference):
break
else:
new = True
cloned = False
default_branch_name = 'wip/%s-%s' % (issue_number, slugify(issue.subject)[:32])
click.confirm(
'Do you want to create a branch tracking %s ?'
% (reference or 'origin/%s' % get_main_branch_name()),
default=True,
abort=True,
)
branch_name = click.prompt('Branch name', default=default_branch_name)
branch = repo.create_head(branch_name, commit=reference or 'origin/%s' % get_main_branch_name())
set_branch_option(repo, branch, 'merge', 'refs/heads/%s' % get_main_branch_name())
set_branch_option(repo, branch, 'remote', '.')
repo.remotes.origin.fetch()
for ref in repo.remotes.origin.refs:
if 'wip/%s-' % issue_number not in ref.name:
continue
if not click.confirm(
'Do you want to clone remote branch "%s" ?' % ref.name,
default=False,
):
continue
cloned = True
branch_name = click.prompt('Branch name', default=default_branch_name)
branch = repo.create_head(branch_name, ref)
set_branch_option(repo, branch, 'merge', 'refs/heads/%s' % get_main_branch_name())
set_branch_option(repo, branch, 'remote', '.')
break
else:
click.confirm(
'Do you want to create a branch tracking %s ?'
% (reference or 'origin/%s' % get_main_branch_name()),
default=True,
abort=True,
)
branch_name = click.prompt('Branch name', default=default_branch_name)
branch = repo.create_head(branch_name, commit=reference or 'origin/%s' % get_main_branch_name())
set_branch_option(repo, branch, 'merge', 'refs/heads/%s' % get_main_branch_name())
set_branch_option(repo, branch, 'remote', '.')
if repo.head.reference == branch:
click.echo('Already on branch %s' % branch_name)
else:
@ -252,7 +270,7 @@ def take(issue_number, reference):
):
issue.assigned_to_id = current_user.id
issue.save()
if new:
if new and not cloned:
apply_attachments(repo, issue)