Skip to content

fix(git): scrub inherited Git context from worktree diffs - #4879

Merged
max-sixty merged 1 commit into
mainfrom
nightly/clean-44993595642
Nov 22, 2026
Merged

max-sixty merged 1 commit into
mainfrom
nightly/clean-44993595642

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

WorkingTree::prepare_diff, prepare_staged_diff and prepare_commit_diff run diff-index/diff-tree in a worktree Worktrunk picked, but they kept inherited GIT_DIR, GIT_WORK_TREE and GIT_INDEX_FILE. When wt runs from a Git hook or a !wt alias in a different worktree, those diffs read the invoking worktree's index and HEAD. So wt 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_command and TempIndex::command). This PR applies scrub_git_discovery_env() to the three WorkingTree diff constructors. Repository::prepare_diff is repo-level and keeps the inherited context, as Repository::run_command does. with_object_store_env still runs after the scrub, so a redirected repository's own GIT_OBJECT_DIRECTORY survives.

The new test_step_commit_show_prompt_branch_ignores_inherited_git_context stages a file in each of two worktrees. It runs wt step commit --show-prompt --branch feature with the primary worktree's GIT_DIR/GIT_WORK_TREE/GIT_INDEX_FILE. Before the fix, the preview diffed caller_only.txt, the invoking worktree's file. Now it diffs feature_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, TempIndex and the ignored-file listing.

`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.
@worktrunk-bot worktrunk-bot added the nightly-cleanup Issues found by nightly code quality sweep label Nov 22, 2026
@max-sixty
max-sixty merged commit 7a72ec2 into main Nov 22, 2026
38 of 47 checks passed
@max-sixty
max-sixty deleted the nightly/clean-44993595642 branch November 22, 2026 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

nightly-cleanup Issues found by nightly code quality sweep

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants