From 23400008d2f9d2143178c13f4f3144ab6a54c08f Mon Sep 17 00:00:00 2001 From: Dave Wilding Date: Thu, 29 Oct 2026 06:51:15 -0500 Subject: [PATCH] repair rebase mode The alias's mode check used over-escaped quotes ('\\"$1\\"'), which survived git's config round-trip as literal backslash-quote tokens. The shell therefore compared '\\rebase' to 'rebase' (never equal), so 'git update-branch rebase' always fell back to merge. Use plain quotes, which git and the shell both handle correctly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/gimmegit/_cli.py | 3 +- tests/unit/test_alias.py | 91 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 tests/unit/test_alias.py diff --git a/src/gimmegit/_cli.py b/src/gimmegit/_cli.py index c2fc3ed..2c22a72 100644 --- a/src/gimmegit/_cli.py +++ b/src/gimmegit/_cli.py @@ -305,8 +305,7 @@ def create_local_branch( fetch_opts_str = shlex.join(fetch_opts) update_branch = "!" + " && ".join( [ - 'mode=$([ \\"$1\\" = rebase ] && echo rebase || echo merge)', - # ^ Git drops unescaped quotes when reading the alias. + 'mode=$([ "$1" = rebase ] && echo rebase || echo merge)', "set --", # Clear positional args, to avoid leaking into the final Git command. "branch=$(git config --get gimmegit.branch)", "base_remote=$(git config --get gimmegit.baseRemote)", diff --git a/tests/unit/test_alias.py b/tests/unit/test_alias.py new file mode 100644 index 0000000..89aee7d --- /dev/null +++ b/tests/unit/test_alias.py @@ -0,0 +1,91 @@ +import subprocess +from pathlib import Path + +import git +import pytest + +from gimmegit import _cli + + +def sh(args: list[str], cwd: Path) -> None: + subprocess.run( + args, + cwd=cwd, + stdout=subprocess.DEVNULL, + stderr=subprocess.DEVNULL, + check=True, + ) + + +@pytest.fixture +def test_dir(tmp_path_factory): + yield tmp_path_factory.mktemp("unit_test_alias") + + +@pytest.fixture +def clone_dir(test_dir): + """Create a base repo, a clone of it, and a `feat` branch on the clone.""" + base = test_dir / "base" + clone = test_dir / "clone" + sh(["git", "init", "-q", "-b", "main", str(base)], test_dir) + sh(["git", "config", "user.email", "t@t"], base) + sh(["git", "config", "user.name", "t"], base) + sh(["git", "commit", "-q", "--allow-empty", "-m", "one"], base) + sh(["git", "commit", "-q", "--allow-empty", "-m", "two"], base) + sh(["git", "clone", "-q", str(base), str(clone)], test_dir) + sh(["git", "config", "user.email", "t@t"], clone) + sh(["git", "config", "user.name", "t"], clone) + context = _cli.Context( + base_branch="main", + branch="feat", + clone_url=str(base), + clone_dir=clone, + create_branch=True, + owner="me", + project="base", + upstream_owner=None, + upstream_url=None, + ) + _cli.create_local_branch( + git.Repo(clone), None, context, ["--no-tags"] + ) # Defines the 'update-branch' alias. + sh(["git", "commit", "-q", "--allow-empty", "-m", "feat"], clone) + yield clone + + +def run_alias(clone_dir: Path, args: list[str]) -> str: + result = subprocess.run( + ["git", "update-branch", *args], + cwd=clone_dir, + capture_output=True, + text=True, + check=True, + ) + return result.stderr + + +@pytest.mark.parametrize( + "args, command", + [ + ([], "merge"), + (["rebase"], "rebase"), + ], + ids=["default", "rebase"], +) +def test_update_branch_alias(clone_dir: Path, args: list[str], command: str): + stderr = run_alias(clone_dir, args) + assert f"+ git {command} origin/main" in stderr, stderr + + +def test_update_branch_rebases_not_merges(clone_dir: Path, test_dir: Path): + sh(["git", "commit", "-q", "--allow-empty", "-m", "three"], test_dir / "base") + run_alias(clone_dir, ["rebase"]) + subjects = subprocess.run( + ["git", "log", "--format=%s"], + cwd=clone_dir, + capture_output=True, + text=True, + check=True, + ).stdout.splitlines() + # Exactly these commits, in this order: no merge commit was created. + assert subjects == ["feat", "three", "two", "one"], subjects