Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions gateway/git_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1819,14 +1819,15 @@ def is_branch_switch(operation: str, args: list[str]) -> bool:
Returns True when the command would switch the active branch (e.g.
``git checkout other-branch``, ``git checkout -b new``). Returns False
for file-level operations (``git checkout -- file.txt``,
``git checkout HEAD -- file``).
``git checkout HEAD -- file``, ``git checkout main -- file``).

Only meaningful for ``checkout`` and ``switch`` operations; returns
False for everything else.

The heuristic:
* If ``--`` separator is present, everything after it is a pathspec
→ NOT a branch switch if there are no positional args before ``--``.
* If ``--`` separator is present, everything after it is a pathspec and
anything before it is a commit-ish source for the file restore
(``HEAD``, ``HEAD~1``, a sha, a branch name) — NOT a branch switch.
* ``switch`` always operates on branches, so any invocation of
``switch`` is considered a branch switch.
* ``checkout`` with ``-b``/``-B`` or ``--orphan`` is a branch switch.
Expand Down Expand Up @@ -1857,18 +1858,17 @@ def is_branch_switch(operation: str, args: list[str]) -> bool:
continue
positional_before_dd.append(arg)

# If there's a positional arg before -- it's treated as a branch ref.
# Known false positive: ``checkout HEAD -- file.txt`` classifies HEAD as
# a branch ref. In practice, pipeline agents use ``checkout -- file.txt``
# instead, and the branch lock only fires for pipeline sessions.
if positional_before_dd and not has_double_dash:
return True
# ``--`` separates a commit-ish source from pathspecs; the form
# ``checkout [<tree-ish>] -- <pathspec>`` restores files without
# switching branches, regardless of whether a tree-ish is present.
if has_double_dash:
return False

# If there's a positional arg before -- AND after, the first is branch
if positional_before_dd and has_double_dash:
# No ``--``: any positional arg is a branch/ref to switch to.
if positional_before_dd:
return True

# No positional args before -- → file checkout (e.g., checkout -- file.txt)
# No positional args, no ``--`` → bare checkout, no-op.
return False


Expand Down
16 changes: 12 additions & 4 deletions gateway/tests/test_branch_switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ def test_checkout_remote_branch(self):
assert is_branch_switch("checkout", ["origin/main"]) is True

def test_checkout_branch_with_double_dash_and_files(self):
"""checkout branch -- file is a branch switch (positional before --)."""
assert is_branch_switch("checkout", ["main", "--", "file.txt"]) is True
"""checkout <tree-ish> -- file restores from <tree-ish>; not a branch switch."""
assert is_branch_switch("checkout", ["main", "--", "file.txt"]) is False


class TestIsBranchSwitchCheckoutFiles:
Expand Down Expand Up @@ -105,8 +105,16 @@ class TestIsBranchSwitchEdgeCases:
"""Edge cases for the branch switch detection heuristic."""

def test_checkout_head_double_dash_file(self):
"""checkout HEAD -- file.txt: HEAD is a positional before -- so it's a branch switch."""
assert is_branch_switch("checkout", ["HEAD", "--", "file.txt"]) is True
"""checkout HEAD -- file.txt restores from HEAD; not a branch switch."""
assert is_branch_switch("checkout", ["HEAD", "--", "file.txt"]) is False

def test_checkout_head_relative_double_dash_file(self):
"""checkout HEAD~1 -- file.txt restores from a commit-ish; not a branch switch."""
assert is_branch_switch("checkout", ["HEAD~1", "--", "file.txt"]) is False

def test_checkout_sha_double_dash_file(self):
"""checkout <sha> -- file.txt restores from a commit-ish; not a branch switch."""
assert is_branch_switch("checkout", ["abc1234", "--", "file.txt"]) is False

def test_checkout_unknown_flags_ignored(self):
"""Unknown flags are skipped, positional still detected."""
Expand Down
Loading