Skip to content

Conversation

@cjsoft
Copy link

@cjsoft cjsoft commented Oct 13, 2025

Problem

When a branch name matches a filename in the working directory, Git commands like git checkout and git reset become ambiguous about whether arguments are refs or paths. This causes git-branchless commands (like git branchless amend) to fail when operating on branches that have the same name as files in the current directory.

Example scenario:

$ git checkout -b foo
$ touch foo
$ git branchless amend
# Error: pathspec 'foo' did not match any files

Solution

This PR adds the standard Git -- separator to all checkout and reset operations in the check_out module to explicitly separate refs from paths, preventing such ambiguity.

Changes

Core fix (check_out.rs): Added -- separator to 5 git command invocations:

  • Line 150: git reset <target> --
  • Line 159: git checkout <target> --
  • Line 261: git reset --hard HEAD --
  • Line 274: git checkout <oid> --
  • Line 286: git reset <oid> --

Tests:

  • Updated 220+ test snapshots across 15 test files to match the new command output format with -- separator
  • Added new test test_amend_with_branch_name_matching_file() in test_amend.rs to verify the fix works when branch names match filenames

The -- separator is a standard Git convention that tells Git to treat everything before it as refs/options and everything after it as paths. This ensures unambiguous command parsing even when branch names and filenames collide.

Test Results

All affected test suites passing:

  • ✅ test_amend (18 tests)
  • ✅ test_navigation (24 tests)
  • ✅ test_move (70 tests)
  • ✅ test_snapshot (6 tests)
  • ✅ test_split (20 tests)
  • ✅ test_record (16 tests)
  • ✅ test_sync, test_restack, test_reword, test_branchless
  • ✅ test_github_forge, test_phabricator_forge

@cjsoft cjsoft force-pushed the explicit_ref branch 3 times, most recently from c0bf7ca to a63777d Compare October 13, 2025 11:38
@cjsoft cjsoft marked this pull request as draft October 13, 2025 11:40
@cjsoft cjsoft changed the title fix: add '--' separator to Git commands to disambiguate refs from paths draft: fix: add '--' separator to Git commands to disambiguate refs from paths Oct 13, 2025
When a branch name matches a filename in the working directory, Git
commands like `git checkout` and `git reset` become ambiguous about
whether arguments are refs or paths. This causes git-branchless commands
(like `git branchless amend`) to fail when operating on branches that
have the same name as files in the current directory.

This commit adds the standard Git '--' separator to all checkout and
reset operations in the check_out module to explicitly separate refs
from paths, preventing such ambiguity.

Changes:
- check_out.rs: Added '--' separator to 5 git command invocations:
  * git reset <target> (line 150)
  * git checkout <target> (line 159)
  * git reset --hard HEAD (line 261)
  * git checkout <oid> (line 274)
  * git reset <oid> (line 286)

- Updated 220+ test snapshots across 15 test files to match the new
  command output format with '--' separator

- test_amend.rs: Added new test test_amend_with_branch_name_matching_file()
  to verify the fix works when branch names match filenames

The '--' separator is a standard Git convention that tells Git to treat
everything before it as refs/options and everything after it as paths.
This ensures unambiguous command parsing even when branch names and
filenames collide.

Example scenario this fixes:
  $ git checkout -b foo
  $ touch foo
  $ git branchless amend  # Previously failed, now works
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Core changes are in this file. Changes in other files are mainly simple replacement of test ground truth string.

@cjsoft cjsoft changed the title draft: fix: add '--' separator to Git commands to disambiguate refs from paths fix: add '--' separator to Git commands to disambiguate refs from paths Oct 13, 2025
@cjsoft cjsoft marked this pull request as ready for review October 13, 2025 12:41
Copy link
Collaborator

@claytonrcarter claytonrcarter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this! It looks like a simple enough change, and the test is self explanatory.

I'm going to approve it and then plan to merge it in a week or so, so other maintainers can weigh in if they choose to.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants