Skip to content

fix: detect renames when the new file is untracked - #246

Merged
umputun merged 4 commits into
masterfrom
fix/untracked-renames
Jun 30, 2026
Merged

umputun merged 4 commits into
masterfrom
fix/untracked-renames

Conversation

@umputun

@umputun umputun commented Jun 30, 2026

Copy link
Copy Markdown
Owner

Renames done in the working tree without staging (mv old new, no git mv) showed up as a deletion plus an all-+ new file instead of one rename entry. Staging the rename or using --staged already worked; only the unstaged-plus-untracked path was broken.

Untracked files are diffed independently against /dev/null, so the new path never shares a git diff invocation with the deleted origin and -M has nothing to pair. The fix recovers the pairing off a throwaway index: copy .git/index to a temp file, mark the untracked paths intent-to-add (git add -N) against the copy via GIT_INDEX_FILE, then git diff -M reports the rename. The real index and working tree are never touched. The same trick renders the per-file rename diff. Git-only (hg and jj have no untracked-rename concept), gated to unstaged working-tree mode, and applied across all three paths: file tree, file diff, and --annotations preload.

Credit to @LandonSchropp for the report and the GIT_INDEX_FILE approach.

Edge cases covered: pathspec-magic filenames like :(top)x are treated literally via GIT_LITERAL_PATHSPECS=1; a fresh repo with no commits (no index yet) returns no renames instead of erroring; a relative TMPDIR no longer mismatches the temp-index path against cleanup.

Also in this branch, unrelated to the rename fix: the launcher exit-code test was non-hermetic on agterm hosts. cleanOverlayEnv did not clear AGTERM_SESSION_ID / AGTERM_SOCKET, so inside an agterm session the launcher took the real agtermctl overlay path instead of the fake backend under test. Cleared those vars.

Fixes #243

umputun added 4 commits June 29, 2026 23:40
a plain `mv old new` (no `git mv`/staging) left `old` as an unstaged
deletion and `new` untracked, so `git diff -M` never paired them - the
tree showed a deletion plus an all-+ add instead of one rename.

`UntrackedRenames` recovers the pairing off a throwaway index: copy
`.git/index`, `git add -N` the untracked paths against the copy via
`GIT_INDEX_FILE`, then `git diff -M` reports the rename. The real index
and working tree are never touched. `FileDiff` renders these the same
way via `untrackedRenameDiff`. Git-only (hg/jj have no untracked-rename
concept), gated to unstaged working-tree mode.

Related to #243
`cleanOverlayEnv` cleared every overlay-detection var except the agterm
ones. On an agterm host `AGTERM_SESSION_ID` leaked from the real
environment, so the launcher took its agterm branch (real `agtermctl
session overlay open`) instead of the fake backend under test. That path
doesn't pipe the fake revdiff's stdout/exit back, so every non-clean
case returned exit 0 / empty output, while the "clean" case passed by
coincidence. CI stayed green because CI isn't an agterm session.

now clear `AGTERM_SESSION_ID` and `AGTERM_SOCKET` too. The agterm overlay
backend (#238) added the launcher branch but left this helper out of sync.
- [major] [annotations_load.go] --annotations preload now runs the untracked-rename detector (foldUntrackedRenames) so (-)/context annotations on an untracked rename round-trip instead of being dropped as all-added
- [major] [diff.go] untracked-rename git calls use GIT_LITERAL_PATHSPECS=1 (renameIndexEnv) so a filename like ":(top)x" is treated literally, not as pathspec magic
- [minor] [diff.go] temp index path made absolute (filepath.Abs) so GIT_INDEX_FILE and cleanup agree when TMPDIR is relative
- [minor] [diff.go] UntrackedRenames returns nil on a missing .git/index (fresh repo, no commits) instead of a spurious WARN
- [over-engineering] [loaders.go] dropped the redundant renameNew map in mergeUntrackedEntries (entrySet already covers rename new-sides)
- [test-gap] [diff_test.go] added pure-rename, path-with-spaces, pathspec-magic-name, and no-commit-repo tests
- [test-gap] [annotations_load_test.go] added untracked-rename preload round-trip test
- [doc-gap] [site/docs.html] rename-detection paragraph now covers the working-tree --untracked case

CLAUDE.md gotcha updated to match.
- [minor] [diff.go] scope the no-index no-op to the missing-.git/index case via an errNoIndex sentinel; previously errors.Is(err, fs.ErrNotExist) wrapped the whole tempIndexWithIntentToAdd call, so an ENOENT from os.CreateTemp (missing TMPDIR) or a corrupt index was swallowed and silently disabled rename detection. other ENOENT now propagates with a warning.
- [test] [diff_test.go] added TestGit_UntrackedRenames_RealErrorNotSwallowed
Copilot AI review requested due to automatic review settings June 30, 2026 05:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@umputun
umputun merged commit 231fe04 into master Jun 30, 2026
5 checks passed
@umputun
umputun deleted the fix/untracked-renames branch June 30, 2026 05:25
LandonSchropp added a commit to LandonSchropp/agent-toolkit that referenced this pull request Jul 22, 2026
revdiff v1.11.1 (umputun/revdiff#246) now detects renames when the new
file is untracked natively, making the throwaway-index workaround
unnecessary.
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.

Renames aren't detected when the new file is untracked

2 participants