Skip to content

[Fix] worktree: use per-worktree upstream for diff stat base - #79

Merged
samzong merged 1 commit into
mainfrom
fix/worktree-diff-stats-fork-base
Jun 3, 2026
Merged

[Fix] worktree: use per-worktree upstream for diff stat base#79
samzong merged 1 commit into
mainfrom
fix/worktree-diff-stats-fork-base

Conversation

@samzong

@samzong samzong commented Jun 3, 2026

Copy link
Copy Markdown
Owner

What's changed?

  • Add ResolveDiffBaseForWorktree(path, override) that resolves the diff base per worktree.
  • New helper branchUpstream(path) reads branch@{upstream} — the per-branch truth git itself records.
  • Fallback order: branch upstream → upstream/<base>origin/<base> → local short name.
  • loadWorktreeDiffStats now resolves the base inside the per-worktree loop instead of once for the whole repo.
  • Two new unit tests cover the upstream-tracked and no-upstream paths.

Why

gmc wt list (PR #77) was calling resolveSyncBaseBranch once and feeding the resulting short name (e.g. main) to every worktree. In fork setups with both origin and upstream, this short name resolved to a stale local refs/heads/main, so the diff stat was computed against an ancient ancestor and produced wildly inflated numbers — e.g. a 17-file real diff on ~/git/openclaw was reported as 3223 files / +116k / -41k.

The previous function resolveSyncBaseBranch is unchanged and still backs gmc wt sync, where the upstream/remote selection is done in a separate step.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c2d9aa59bb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/worktree/diff_stat.go
Resolve the diff base per worktree instead of using a single repo-wide
value. The previous approach called `resolveSyncBaseBranch` once and
fed the result to every worktree, which on fork setups (origin + upstream)
collapsed to the local short name and produced wildly inflated stats
(e.g. 3223 files when the real diff was 17).

The new resolver:
1. Uses the branch's own `@{upstream}` when set — git's recorded
   truth for where this branch was forked from.
2. Falls back to `upstream/<base>` then `origin/<base>`, preferring
   the upstream remote.
3. Falls back to the local short name as a last resort.

`gmc wt sync` is unaffected — its base resolution path is unchanged.

Tests: two new cases cover the upstream-tracked and no-upstream paths.

## Considered and deferred

- internal/worktree/diff_stat.go:45 [BOT-NIT]: `seen` map is built but never read; harmless dead code, removing would be a NIT-only edit.
- internal/worktree/worktree_test.go [BOT-NIT]: `FallsBackToRemoteRef` test only passes because the test setup does not create `refs/remotes/upstream/main`; with the new `upstream`-first ordering, the test could be tightened, but current behavior is correct.

Signed-off-by: samzong <samzong.lu@gmail.com>
@samzong
samzong force-pushed the fix/worktree-diff-stats-fork-base branch from c2d9aa5 to e31141c Compare June 3, 2026 04:20
@samzong

samzong commented Jun 3, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e31141c4cd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +35 to +36
if up := c.branchUpstream(path); up != "" {
return up, nil

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid using tracking branch as the diff base

When a feature worktree has been pushed with git push -u origin feature/foo, branchUpstream returns origin/feature/foo here, so WorktreeDiffStat compares the branch against its own remote-tracking copy; after a push the merge base is often HEAD, and gmc wt list reports no changed files even though the worktree differs from main/upstream/main. The --diff-base help describes a base branch/ref for diff stats, so the default resolver should not treat the branch's push/pull upstream as the PR/base branch unconditionally.

Useful? React with 👍 / 👎.

@samzong
samzong merged commit 1c76275 into main Jun 3, 2026
1 check passed
@samzong
samzong deleted the fix/worktree-diff-stats-fork-base branch July 1, 2026 13:57
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.

1 participant