Repository navigation
fix(git): scrub inherited Git context from worktree diffs - #4879
Merged
Merged
Conversation
`WorkingTree::prepare_diff`, `prepare_staged_diff`, and `prepare_commit_diff` ran `diff-index`/`diff-tree` in the worktree wt resolved but kept inherited `GIT_DIR`, `GIT_WORK_TREE`, and `GIT_INDEX_FILE`. Run from a Git hook or a `!wt` alias in another worktree, `wt step commit --branch <b>` then built its stats and LLM prompt from the invoking worktree's index and HEAD, as did the picker's diff preview and the summary diff. Every other worktree-local plumbing site (`WorkingTree::run_command`, `TempIndex::command`) already scrubs; repo-level `Repository::prepare_diff` keeps the inherited context, as repo-level commands do.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WorkingTree::prepare_diff,prepare_staged_diffandprepare_commit_diffrundiff-index/diff-treein a worktree Worktrunk picked, but they kept inheritedGIT_DIR,GIT_WORK_TREEandGIT_INDEX_FILE. Whenwtruns from a Git hook or a!wtalias in a different worktree, those diffs read the invoking worktree's index and HEAD. Sowt step commit --branch <b>built its stats line and LLM prompt from the wrong worktree's staged changes. The picker's diff preview and the summary diff had the same problem.AGENTS.md requires this scrub for worktree-local plumbing, and the sibling paths already apply it (
WorkingTree::run_commandandTempIndex::command). This PR appliesscrub_git_discovery_env()to the threeWorkingTreediff constructors.Repository::prepare_diffis repo-level and keeps the inherited context, asRepository::run_commanddoes.with_object_store_envstill runs after the scrub, so a redirected repository's ownGIT_OBJECT_DIRECTORYsurvives.The new
test_step_commit_show_prompt_branch_ignores_inherited_git_contextstages a file in each of two worktrees. It runswt step commit --show-prompt --branch featurewith the primary worktree'sGIT_DIR/GIT_WORK_TREE/GIT_INDEX_FILE. Before the fix, the preview diffedcaller_only.txt, the invoking worktree's file. Now it diffsfeature_only.txt.The closed #4075 included this change in a much broader diff. It was dropped there for scope, not on approach. The narrower #4082 that landed instead covered only
WorkingTree::run_command,TempIndexand the ignored-file listing.