Skip to content
This repository was archived by the owner on Nov 28, 2024. It is now read-only.

Commit 96939d8

Browse files
authored
Merge pull request #73 from sileht/branch-prefix
Allow to configure branch prefix
2 parents 75aaac7 + 83c48e7 commit 96939d8

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

git_pull_request/__init__.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,12 @@ def git_remote_url(remote="origin"):
104104

105105

106106
def git_get_config(option, default):
107-
out = _run_shell_command(
108-
["git", "config", "--get", "git-pull-request." + option],
109-
output=True, raise_on_error=False)
110-
return out or default
107+
try:
108+
return _run_shell_command(
109+
["git", "config", "--get", "git-pull-request." + option],
110+
output=True)
111+
except RuntimeError:
112+
return default
111113

112114

113115
def git_config_add_argument(parser, option, *args, **kwargs):
@@ -116,10 +118,9 @@ def git_config_add_argument(parser, option, *args, **kwargs):
116118
if isboolean and default is None:
117119
default = False
118120
default = git_get_config(option[2:], default)
119-
if default:
120-
if isboolean:
121-
default = distutils.util.strtobool(default)
122-
kwargs["default"] = default
121+
if isboolean and isinstance(default, str):
122+
default = distutils.util.strtobool(default)
123+
kwargs["default"] = default
123124
return parser.add_argument(option, *args, **kwargs)
124125

125126

@@ -250,7 +251,8 @@ def git_pull_request(target_remote=None, target_branch=None,
250251
download=None,
251252
tag_previous_revision=False,
252253
fork=True,
253-
setup_only=False):
254+
setup_only=False,
255+
branch_prefix=None):
254256
branch = git_get_branch_name()
255257
if not branch:
256258
LOG.critical("Unable to find current branch")
@@ -321,7 +323,7 @@ def git_pull_request(target_remote=None, target_branch=None,
321323
target_branch, branch, user, title, message,
322324
comment,
323325
force_editor, tag_previous_revision,
324-
fork, setup_only)
326+
fork, setup_only, branch_prefix)
325327
approve_login_password(host=hostname, user=user, password=password)
326328

327329

@@ -408,7 +410,7 @@ def fork_and_push_pull_request(g, hosttype, repo_to_fork, rebase,
408410
target_remote, target_branch, branch, user,
409411
title, message, comment,
410412
force_editor, tag_previous_revision, fork,
411-
setup_only):
413+
setup_only, branch_prefix):
412414

413415
g_user = g.get_user()
414416

@@ -430,6 +432,14 @@ def fork_and_push_pull_request(g, hosttype, repo_to_fork, rebase,
430432
forked = True
431433
LOG.info("Forked repository: %s", repo_forked.html_url)
432434

435+
if branch_prefix is None and not forked:
436+
branch_prefix = g_user.login
437+
438+
if branch_prefix:
439+
remote_branch = "{}/{}".format(branch_prefix, branch)
440+
else:
441+
remote_branch = branch
442+
433443
if forked:
434444
remote_to_push = git_remote_matching_url(repo_forked.clone_url)
435445

@@ -443,9 +453,7 @@ def fork_and_push_pull_request(g, hosttype, repo_to_fork, rebase,
443453
remote_to_push, repo_forked.clone_url])
444454
LOG.info("Added forked repository as remote `%s'", remote_to_push)
445455
head = "{}:{}".format(user, branch)
446-
remote_branch = branch
447456
else:
448-
remote_branch = "{}/{}".format(g_user.login, branch)
449457
remote_to_push = target_remote
450458
head = "{}:{}".format(repo_to_fork.owner.login, remote_branch)
451459

@@ -579,6 +587,9 @@ def build_parser():
579587
help="Title of the pull request.")
580588
parser.add_argument("--message", "-m",
581589
help="Message of the pull request.")
590+
git_config_add_argument(parser,
591+
"--branch-prefix",
592+
help="Prefix remote branch")
582593
git_config_add_argument(parser, "--no-rebase", "-R",
583594
action="store_true",
584595
help="Don't rebase branch before pushing.")
@@ -618,7 +629,6 @@ def build_parser():
618629
const="never",
619630
help="Don't fork to create the pull-request"
620631
)
621-
622632
git_config_add_argument(
623633
parser,
624634
"--setup-only",
@@ -653,7 +663,8 @@ def main():
653663
download=args.download,
654664
tag_previous_revision=args.tag_previous_revision,
655665
fork=args.fork,
656-
setup_only=args.setup_only
666+
setup_only=args.setup_only,
667+
branch_prefix=args.branch_prefix
657668
)
658669
except Exception:
659670
LOG.error("Unable to send pull request", exc_info=True)

0 commit comments

Comments
 (0)