Skip to content

fix(git): fix empty output when branch name contains '/' in git diff#1437

Merged
pszymkowiak merged 1 commit intortk-ai:developfrom
binsee:fix/git-diff-branch-slash
Apr 22, 2026
Merged

fix(git): fix empty output when branch name contains '/' in git diff#1437
pszymkowiak merged 1 commit intortk-ai:developfrom
binsee:fix/git-diff-branch-slash

Conversation

@binsee
Copy link
Copy Markdown
Contributor

@binsee binsee commented Apr 21, 2026

Summary

  • rtk git diff feature/user-auth produced silent empty output (exit 0) because
    normalize_diff_args treated any arg containing / as a file path and injected
    -- before it, causing git to interpret the branch name as a pathspec with no match.
  • Replace the string heuristic looks_like_path() with a three-tier detection strategy:
    1. Explicit path prefixes (., ~) → always a path
    2. Contains path separator (/, \) → filesystem existence check to distinguish
      branch names (feature/auth, not on disk) from real paths (src/main.rs)
    3. Bare word with no separator → never inject -- (avoids misfiring when a file
      shares a name with a branch/ref, e.g. a file named main)
  • Extract normalize_diff_args_impl with injectable path-checker for testability;
    update all existing tests to use mock existence checks; add three regression tests.

Test plan


Investigated, implemented, and tested with the assistance of Claude Code (claude-sonnet-4-6).

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 21, 2026

CLA assistant check
All committers have signed the CLA.

Branch names containing '/' (e.g. feature/user-auth, main...feature/auth)
were incorrectly treated as file paths by the string heuristic in
normalize_diff_args, causing '--' to be injected before them and making
git treat them as pathspecs — resulting in silent empty output (exit 0).

Replace looks_like_path() with a three-tier detection strategy:
- Explicit path prefixes (. ~) → always a path, no filesystem check needed
- Contains path separator (/ \) → use filesystem existence to distinguish
  branch names (feature/auth, not on disk) from real paths (src/main.rs)
- Bare word with no separator → never inject '--', regardless of filesystem
  state (avoids misfire when a file shares a name with a branch/ref)

Introduce normalize_diff_args_impl with injectable path-checker for
testability. Update all existing tests to use mock existence checks.
Add three regression tests: branch-with-slash, range-with-slash, and
bare-word-that-exists-on-disk.

Fixes: rtk-ai#1431

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@binsee binsee force-pushed the fix/git-diff-branch-slash branch from b92ed32 to 13188a8 Compare April 21, 2026 16:25
@pszymkowiak
Copy link
Copy Markdown
Collaborator

Real-binary test results ✅

Tested with binary built from this branch ():

# Test 1: branch name with / — was empty before fix
$ rtk git diff feature/user-auth
file.txt | 1 -
 1 file changed, 1 deletion(-)
--- Changes ---
file.txt
  @@ -1,2 +1 @@
  -auth feature change

# Test 2: real file path still works
$ rtk git diff file.txt
file.txt | 1 +
 1 file changed, 1 insertion(+)
--- Changes ---

# Test 3: 3-dot branch syntax feature/user-auth
$ rtk git diff HEAD...feature/user-auth  → works correctly

All cases pass. The filesystem-existence check correctly distinguishes feature/user-auth (not on disk → treat as ref) from file.txt (on disk → inject --).

Copy link
Copy Markdown
Collaborator

@pszymkowiak pszymkowiak left a comment

Choose a reason for hiding this comment

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

Tested with real binary built from this branch. All cases confirmed working:

  • rtk git diff feature/user-auth now shows diff (was silent empty before)
  • rtk git diff file.txt still works (real path detection intact)
  • rtk git diff HEAD...feature/user-auth (3-dot syntax) works
  • 1609 tests pass, no regressions

@aeppling
Copy link
Copy Markdown
Contributor

Hey, checking if path exist to differentiate branch / path is correct
Thanks for contributing

@pszymkowiak pszymkowiak merged commit e070226 into rtk-ai:develop Apr 22, 2026
10 checks passed
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.

4 participants