diff --git a/README.md b/README.md index 06988df..bc9b215 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,8 @@ options: -h, --help show this help message and exit -V, --version show program's version number and exit --dry-run Prints out the commands, but not executed - --pr-remote REMOTE Git remote to use for PR branches + --pr-remote REMOTE Git remote to use for PR branches (default: + remote.pushDefault or origin) --upstream-remote REMOTE Git remote to use for upstream branches --abort Abort current cherry-pick and clean up branch @@ -89,9 +90,11 @@ $ git remote add upstream https://github.com/python/cpython.git ``` By default, the PR branches used to submit pull requests back to the main -repository are pushed to `origin`. If this is incorrect, then the correct -remote will need be specified using the `--pr-remote` option (e.g. -`--pr-remote pr` to use a remote named `pr`). +repository are pushed to Git's `remote.pushDefault`, or `origin` if that is +not set. To set it for a group of checkouts, use an +[`includeIf`](https://git-scm.com/docs/git-config#_conditional_includes) +in your global Git config. To use a remote named `pr`, either pass +`--pr-remote pr` or set `remote.pushDefault` to `pr`. ## Cherry-picking 🐍🍒⛏️ @@ -122,7 +125,8 @@ Use the full commit hash for `cherry_picker.py`. ``` --dry-run Dry Run Mode. Prints out the commands, but not executed. ---pr-remote REMOTE Specify the git remote to push into. Default is 'origin'. +--pr-remote REMOTE Specify the git remote to push into. Default is + remote.pushDefault or 'origin'. --upstream-remote REMOTE Specify the git remote to use for upstream branches. Default is 'upstream' or 'origin' if the former doesn't exist. --status Do `git status` in cpython directory. diff --git a/cherry_picker/cherry_picker.py b/cherry_picker/cherry_picker.py index a71a1ac..71d71c3 100755 --- a/cherry_picker/cherry_picker.py +++ b/cherry_picker/cherry_picker.py @@ -139,7 +139,9 @@ def __init__( if dry_run: print("Dry run requested, listing expected command sequence") - self.pr_remote = pr_remote + self.pr_remote = ( + pr_remote or get_git_config_val("remote.pushDefault") or "origin" + ) self.upstream_remote = upstream_remote self.commit_sha1 = commit_sha1 self.branches = branches @@ -751,8 +753,11 @@ def get_parser(): parser.add_argument( "--pr-remote", metavar="REMOTE", - default="origin", - help="Git remote to use for PR branches", + default=None, + help=( + "Git remote to use for PR branches " + "(default: remote.pushDefault or origin)" + ), ) parser.add_argument( "--upstream-remote", @@ -1095,7 +1100,13 @@ def wipe_cfg_vals_from_git_cfg(*cfg_opts): def load_val_from_git_cfg(cfg_key_suffix): """Retrieve one option from Git config.""" cfg_key = f'cherry-picker.{cfg_key_suffix.replace("_", "-")}' - cmd = "git", "config", "--local", "--get", cfg_key + return get_git_config_val(cfg_key, local=True) + + +def get_git_config_val(key, *, local=False): + """Retrieve one option from Git config, or None if unset.""" + scope = ("--local",) if local else () + cmd = "git", "config", *scope, "--get", key try: return ( subprocess.check_output(cmd, stderr=subprocess.DEVNULL) diff --git a/cherry_picker/test_cherry_picker.py b/cherry_picker/test_cherry_picker.py index 35e8e14..da762c8 100644 --- a/cherry_picker/test_cherry_picker.py +++ b/cherry_picker/test_cherry_picker.py @@ -25,6 +25,7 @@ get_commits_from_backport_branch, get_current_branch, get_full_sha_from_short, + get_git_config_val, get_sha1_from, get_state, load_config, @@ -317,6 +318,13 @@ def test_upstream_name( assert cp.upstream == remote_name +@pytest.mark.parametrize("value,expected", ((None, None), ("", ""), ("set", "set"))) +def test_get_git_config_val(value, expected, tmp_git_repo_dir, git_config): + if value is not None: + git_config("--local", "cherry-picker.test-key", value) + assert get_git_config_val("cherry-picker.test-key") == expected + + @pytest.mark.parametrize( "remote_to_add,remote_name,upstream_remote", (