Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(step): diff against the target's upstream when the local target is stale (#4281)

Maximilian Roosdacc21a39106
diff --git a/src/commands/step/diff.rs b/src/commands/step/diff.rs
index 52ac36d96..b45001e9f 100644
--- a/src/commands/step/diff.rs
+++ b/src/commands/step/diff.rs
@@ -11,6 +11,12 @@ use worktrunk::git::Repository;
/// without mutating the real index — git's stat cache stays warm and tracked
/// files aren't re-hashed.
///
+/// The diff base is what `wt merge` would squash from: the merge base with the
+/// target, or with the target's upstream when the branch was built on an
+/// upstream the local target lags ([`Repository::span_upstream`]). Without that,
+/// a stale local `main` would count every upstream commit the branch picked up
+/// as the branch's own.
+///
/// `branch` selects which worktree to diff: when `Some`, the repo is rooted at
/// that branch's worktree so the diff (and its target resolution) operate there
/// rather than on the current directory. The branch must have a checked-out
@@ -27,8 +33,11 @@ pub fn step_diff(
let wt = repo.current_worktree();
let integration_target = repo.require_target_ref(target)?;
+ let span_target = repo
+ .span_upstream(&integration_target)?
+ .unwrap_or(integration_target);
let merge_base = repo
- .merge_base("HEAD", &integration_target)?
+ .merge_base("HEAD", &span_target)?
.context("No common ancestor with target branch")?;
// Stream diff to stdout — git handles pager and coloring.
diff --git a/src/git/repository/integration.rs b/src/git/repository/integration.rs
index b86fd4ba4..f2ca3d2e2 100644
--- a/src/git/repository/integration.rs
+++ b/src/git/repository/integration.rs
@@ -781,7 +781,8 @@ impl Repository {
/// topology.
///
/// The rewrite steps (`wt step squash`, `wt step rebase`, and `wt merge`
- /// through them) measure "the branch's own commits" as
+ /// through them), and `wt step diff` previewing them, measure "the
+ /// branch's own commits" as
/// `merge-base(target, HEAD)..HEAD`. When the local target ref lags its
/// upstream (e.g. the primary checkout's `main` left behind `origin/main`)
/// and the branch was built on the newer upstream tip, that span sweeps in
diff --git a/tests/integration_tests/merge.rs b/tests/integration_tests/merge.rs
index c095b64c4..ab1df877d 100644
--- a/tests/integration_tests/merge.rs
+++ b/tests/integration_tests/merge.rs
@@ -5671,6 +5671,35 @@ fn test_step_squash_measures_against_upstream_when_local_main_stale(
);
}
+/// #3519 (standalone step): `wt step diff` shows what `wt merge` would squash,
+/// so with a stale local main it diffs from the upstream fork point — the
+/// branch's own files, not the upstream commits it was built on.
+#[rstest]
+fn test_step_diff_measures_against_upstream_when_local_main_stale(
+ #[from(repo_with_remote)] repo: TestRepo,
+) {
+ setup_stale_local_main(&repo);
+ let feature_wt =
+ add_feature_worktree(&repo, "origin/main", &[("feature.txt", "feature commit")]);
+ fs::write(feature_wt.join("untracked.txt"), "untracked").unwrap();
+
+ let output = repo
+ .wt_command()
+ .args(["step", "diff", "--", "--name-only"])
+ .current_dir(&feature_wt)
+ .output()
+ .unwrap();
+ assert!(
+ output.status.success(),
+ "step diff failed\nstderr:\n{}",
+ String::from_utf8_lossy(&output.stderr),
+ );
+ assert_snapshot!(String::from_utf8_lossy(&output.stdout), @"
+ feature.txt
+ untracked.txt
+ ");
+}
+
/// #3519 (standalone step): `wt step rebase` with a stale local target rebases
/// onto the target's *upstream*, replaying only the branch's own commits.
/// Rebasing onto the stale local ref would replay the upstream commits too,