fix(git): inject -- for bare-word-with-extension paths in git diff#1679
fix(git): inject -- for bare-word-with-extension paths in git diff#1679truffle-dev wants to merge 1 commit into
-- for bare-word-with-extension paths in git diff#1679Conversation
clap's `trailing_var_arg = true` drops `--` when it's the first positional. `normalize_diff_args` re-inserts it, but the bare-word rule (deliberately conservative since rtk-ai#1431) skipped over args like `package.json` and `playwright.config.ts`. When mixed with an explicit-path arg (`.harness/...`), `--` landed at the explicit path's index, leaving the bare-word-with-extension args before it to be misinterpreted as revisions, producing `fatal: bad revision 'package.json'`. This adds a rule between the existing slash-path rule and the plain-bare-word rule: a bare word with a mid-string `.` (looks like a filename with extension) uses `path_exists` to distinguish real files (`package.json`) from refs/tags (`v1.2.3`). Plain bare words like `main` continue to be treated as refs even when a same-named file exists, preserving rtk-ai#1431's behavior. Closes rtk-ai#1669
|
Hi, thanks for your PR, I'm willing to wait to see if clap allows us to keep escapes |
|
Sounds good. Subscribed to clap-rs/clap#5055. Happy to keep this rebased as a fallback, or close whenever you prefer. |
|
@truffle-dev After thinking more about this and seeing that clap's issue has not seen any updates since the spring of 2024, I pushed a more generic solution than stacking more heuristic on already existing ones, in #1823. If you want to give it a try, it would help, thanks |
|
Tried it. Built #1823, ran the args_utils tests (15/15 pass), and walked through the original cases:
Closing this in favor of #1823. The preserve-what-the-user-typed shape is cleaner than the path-existence heuristic; users who want a bare-word-with-extension treated as a path can still spell |
|
Superseded by #1823. |
Closes #1669.
Reporter ran:
and got
fatal: bad revision 'package.json'while rawgit diff -- ...worked.What was happening
clap's
trailing_var_arg = truedrops--when it's the first positional argument (existing comment atsrc/cmds/git/git.rs:74-83).normalize_diff_args(added for issue #1215) re-inserts it, walking the args until it finds the first one that looks path-like. Three rules existed before this PR:.or~→ always a path./or\→ usepath_exists.rtk git diffproduces empty output when branch name contains/#1431 to keeprtk git diff mainresolving as a branch).For the reporter's command, the first path-looking arg by rule 1 is
.harness/e2e_pipeline.yamlat index 2.--lands at index 2, leavingpackage.json(index 0) andplaywright.config.ts(index 1) before the separator, where git treats them as revisions.The fix
A new rule between the slash-path rule and the plain-bare-word rule: a bare word with a mid-string
.(looks like a filename with extension) usespath_existsto distinguish real files from refs/tags.package.jsonandplaywright.config.tsresolve as paths because the filesystem has them, so--lands at index 0 instead of index 2. Plain bare words likemaincontinue to be treated as refs even when a same-named file exists (issue #1431's deliberate behavior is preserved by the existing rule sitting after this one). A version-tag-shape arg likev1.2.3that doesn't exist as a file remains a ref candidate.Verification
Stash-bisect on a five-file repro that mirrors the reporter's exact command:
Before the patch:
After the patch:
Tests
Four new unit tests next to the existing ones in
mod tests:bare_word_with_extension_treated_as_path— singlepackage.jsonthat exists.bare_word_with_extension_no_injection_when_not_file—v1.2.3tag pattern that doesn't exist on disk stays a ref.mixed_bare_extension_and_explicit_path— the reporter's exact five-file case.ref_before_bare_word_extension—["HEAD", "Cargo.toml"]→["HEAD", "--", "Cargo.toml"].cargo test --release --bin rtk normalize_diff_argsruns all 14 (10 prior + 4 new) green.Scope notes
run_logandrun_show(src/cmds/git/git.rs:447,:240) do not callnormalize_diff_args, sortk git log -- file.jsonandrtk git show -- file.jsonlikely have the same shape. Kept this PR narrow to the diff path the reporter hit; happy to extend the normalization to log/show in a follow-up if you prefer a sweep.