diff --git a/jupyter_releaser/lib.py b/jupyter_releaser/lib.py index 0f718124..15b07c88 100644 --- a/jupyter_releaser/lib.py +++ b/jupyter_releaser/lib.py @@ -514,19 +514,24 @@ def prep_git(ref, branch, repo, auth, username, url): if not checkout_exists: util.run(f"git remote add origin {url}") - branch = branch or util.get_default_branch() - ref = ref or "" - # Make sure we have *all* tags util.run(f"{util.GIT_FETCH_CMD} --tags --force") # Handle the ref + ref = ref or "" if ref.startswith("refs/pull/"): pull = ref[len("refs/pull/") :] ref_alias = f"refs/pull/{pull}" + elif ref.startswith("refs/heads/") and not branch: + branch = ref[len("refs/heads/") :] + util.run(f"git fetch origin {branch}") + ref = None else: ref = None + # Handle the branch. + branch = branch or util.get_default_branch() + # Reuse existing branch if possible if ref: util.run(f"{util.GIT_FETCH_CMD} +{ref}:{ref_alias}") diff --git a/tests/test_cli.py b/tests/test_cli.py index 7d2ccf0f..b180cf49 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -60,6 +60,22 @@ def test_prep_git_tag(py_package, runner): assert util.get_branch() == tag, util.get_branch() +def test_prep_git_ref_branch(py_package, runner): + branch = "ref-branch" + util.run(f"git checkout -b {branch} origin/foo") + result = runner( + ["prep-git", "--git-url", py_package], + env=dict(GITHUB_ACTIONS="", RH_REF=f"refs/heads/{branch}"), + ) + + log = get_log() + assert "before-prep-git" not in log + assert "after-prep-git" in log + + os.chdir(util.CHECKOUT_NAME) + assert util.get_branch() == branch, util.get_branch() + + def test_prep_git_slashes(py_package, runner): branch = "a/b/c" util.run(f"git checkout -b {branch} foo")