Skip to content

refactor: route every short-SHA display through git's abbreviation logic - #2576

Merged
max-sixty merged 2 commits into
mainfrom
push-merge-sha-abbrev
May 4, 2026
Merged

max-sixty merged 2 commits into
mainfrom
push-merge-sha-abbrev

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Every site that abbreviated a commit SHA was either slicing &sha[..7] or running its own ad-hoc git rev-parse --short call. 7-char prefixes regularly collide in repos with many commits, and none of the slicing sites honored core.abbrev. Cut over to a single canonical helper.

Single helper, every display site

Repository::short_sha(&str) -> Result<String> wraps git rev-parse --short. Routes display through git's own abbreviation logic so core.abbrev is honored and prefixes auto-extend on collision. Used by:

  • step commit / step squash success lines
  • step push --no-ff Merged to @ <hash> line — the original bug. Flagged on feat: add --format=json to seven step + hook commands #2560 as a pre-existing third instance of the same pattern fixed there in commit.rs and step_commands.rs.
  • {{ short_commit }} template var in hook contexts (command_executor.rs, template_vars.rs)
  • post-remove hook context for the removed worktree
  • safety-backup ref display (create_safety_backup)
  • (detached <sha>) label in the orphan-check loop

All seven sites previously sliced commit[..7] or called their own rev-parse. Now they route through one helper.

Batched form for wt list --format=json

The JSON list path emits one short_sha per worktree row. Looping short_sha would fork a subprocess per row, so the short SHA is folded into the existing commit_details_many batch instead — %h is added to the git log --no-walk --format=... call that already fetches timestamp and subject. One subprocess for the whole list, same core.abbrev behavior as every other site.

CommitDetails gains a short_sha: String field with the same provenance as the timestamp and subject. The JSON schema is unchanged (commit.short_sha was already a field) — only its length now varies by core.abbrev instead of being hard-coded to 7.

API change

TemplateVars::with_active_commit(commit, short_commit) now takes both forms. Previously it sliced commit.get(..7) internally. The sole caller (worktree/finish.rs) resolves the short form via Repository::short_sha and passes both.

Docs

{{ short_commit }} is no longer documented as "(7 chars)" — src/cli/mod.rs and src/config/project.rs now describe core.abbrev behavior. Doc-sync regenerated docs/content/hook.md and the skill mirror.

Tests

Full suite passes (3463/3463). commit_details_many tests updated for the new tuple shape; CommitDetails fixtures in layout.rs get a short_sha field; template_vars tests pass both forms explicitly. Two integration tests previously asserting short_commit was exactly 7 chars (post_start_commands.rs, user_hooks.rs) still pass — fresh test repos default to core.abbrev = 7.

This was written by Claude Code on behalf of @max-sixty

Every site that abbreviated a commit SHA was either slicing `&sha[..7]`
or running its own ad-hoc `git rev-parse --short` call. 7-char prefixes
regularly collide in repos with many commits, and none of the slicing
sites honored `core.abbrev`. Cut over to a single canonical helper.

## Single helper, every display site

`Repository::short_sha(&str) -> Result<String>` wraps
`git rev-parse --short`. Used by:

- `step commit` / `step squash` success lines
- `step push --no-ff` "Merged to @ <hash>" line (the original bug —
  flagged on #2560 as a pre-existing third instance of the same pattern
  fixed in `commit.rs` / `step_commands.rs` there)
- `{{ short_commit }}` template var in hook contexts
- post-remove hook context for the removed worktree
- safety-backup ref display
- `(detached <sha>)` label in the orphan-check loop

All seven sites previously sliced `commit[..7]` or called their own
`rev-parse`. Now they route through one helper.

## Batched form for `wt list --format=json`

The JSON list path emits one `short_sha` per worktree row. Looping
`Repository::short_sha` would fork a subprocess per row, so the short
SHA is folded into the existing `commit_details_many` batch instead:
`%h` is added to the `git log --no-walk --format=...` call that already
fetches timestamp and subject. One subprocess for the whole list,
honoring `core.abbrev` like every other site.

`CommitDetails` gains a `short_sha: String` field — same provenance as
the timestamp and subject, populated from the same batch. The JSON
schema is unchanged (`commit.short_sha` was already a field; only its
length now varies by `core.abbrev`).

## API change

`TemplateVars::with_active_commit(commit, short_commit)` now takes both
forms. Previously it sliced `commit.get(..7)` internally. Caller in
`worktree/finish.rs` resolves the short form via `Repository::short_sha`
and passes both. Test updated to pass both pre-resolved.

## Docs

`{{ short_commit }}` is no longer documented as "(7 chars)". Docs in
`src/cli/mod.rs` and `src/config/project.rs` describe `core.abbrev`
behavior. Doc-sync regenerated `docs/content/hook.md` and the skill
mirror.

## Tests

Full suite passes (3463/3463). `commit_details_many` tests updated for
the new tuple shape; `CommitDetails` fixtures in `layout.rs` get a
`short_sha` field; `template_vars` tests pass both forms explicitly.
Two integration tests previously asserting `short_commit` was exactly
7 chars (`post_start_commands.rs`, `user_hooks.rs`) still pass — fresh
test repos default to `core.abbrev = 7`.

> _This was written by Claude Code on behalf of @max-sixty_

Co-Authored-By: Claude <[email protected]>

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A few inconsistencies with the stated "every site routes through one helper / core.abbrev everywhere" goal:

wt list --format=json schema docs still say "7 chars" in three places (the table at src/cli/mod.rs:850, the JsonCommit struct doc at src/commands/list/json_output.rs:118, and their synced mirrors in docs/content/list.md / skills/worktrunk/reference/list.md). With this PR the JSON short_sha field now also varies by core.abbrev, so these should match the hook table's wording. Editing src/cli/mod.rs:850 and the struct doc and re-running the doc-sync test would update the mirrors.

-| `short_sha` | string | Short commit SHA (7 chars) |
+| `short_sha` | string | Short commit SHA, abbreviated per `core.abbrev` (auto-extends for ambiguous prefixes) |

handle_pushing_message in src/commands/worktree/push.rs:163 still calls rev-parse --short HEAD ad-hoc rather than going through Repository::short_sha. It already honors core.abbrev so this is correctness-neutral, but it's the one straggler if the goal is "every display site routes through one helper". Either migrate it to repo.short_sha(&full_sha) or trim the claim from the PR description.

Inline suggestion below for the JsonItem::from_list_item fallback — that one's a real schema-vs-description disagreement (full 40-char SHA in the short_sha field for prunable worktrees and rows whose commit_details_many errored).

Comment thread src/commands/list/json_output.rs Resolved
Three follow-ups flagged on PR #2576:

- `JsonItem::from_list_item` fallback for prunable worktrees and
  batch-failure rows now slices `sha[..7.min(sha.len())]` instead of
  cloning the full 40-char SHA. Restores the pre-PR JSON shape; the
  primary path still routes through git's `%h`. Comment explains the
  fallback's purpose.
- `handle_pushing_message` in `worktree/push.rs:163` migrates from an
  ad-hoc `rev-parse --short HEAD` to `Repository::short_sha`. The
  cutover claim is now true for every display site.
- `wt list --format=json` schema docs updated: the `short_sha` row in
  `cli/mod.rs:850` and the `JsonCommit` struct doc in `json_output.rs`
  now describe `core.abbrev` behavior, matching the hook table. Doc-sync
  regenerated `docs/content/list.md` and the skill mirror; `wt list`
  help snapshots refreshed via `cargo insta test --accept`.

> _This was written by Claude Code on behalf of @max-sixty_

Co-Authored-By: Claude <[email protected]>
@max-sixty
max-sixty merged commit 3593606 into main May 4, 2026
27 checks passed
@max-sixty
max-sixty deleted the push-merge-sha-abbrev branch May 4, 2026 06:17
max-sixty added a commit that referenced this pull request May 4, 2026
)

Follow-up to #2576. The parent PR routed every short-SHA display through
`git`'s abbreviation logic, but the JSON path kept a `sha[..7]` slice
fallback for prunable worktrees and rows whose batch failed — those rows
have no `CommitDetails` populated, so the field had nowhere to read
from.

The pre-skeleton `commit_details_many` batch already returns `%h` for
every row in the list, including prunable worktrees (the batch runs
against the repo, not the worktree dir). Hoisting `short_sha` out of
`CommitDetails` and onto `ListItem` directly lets the populate loops set
it on every row before the prunable skip; `JsonItem::from_list_item`
then reads `item.short_sha` without a fallback. `CommitDetails` keeps
the prunable skip for the timestamp/message bundle, which is what
actually preserves the old task-queue UX.

Net effect: every short-SHA emission in worktrunk now routes through
`git`'s `%h`/`rev-parse --short` machinery — no manual slicing left in
the user-facing path.

> _This was written by Claude Code on behalf of @max-sixty_

Co-authored-by: Claude <[email protected]>
max-sixty added a commit that referenced this pull request May 4, 2026
The last manual `&self.head[..8]` slice in the list path. Routes through
the canonical `short_sha` instead, completing the cutover started in
#2576 (every short-SHA emission through git's `%h`/`rev-parse --short`
machinery) and #2577 (hoisting `short_sha` onto `ListItem`).

When `short_sha` is empty the function returns `""`. The unborn-branch
case is caught by the branch arm. The only path where this falls through
to an empty string is "detached HEAD where the `commit_details_many`
batch failed" — already a degraded state with placeholders in every
other cell, so an empty diagnostic label is acceptable.

> _This was written by Claude Code on behalf of @max-sixty_

Co-authored-by: Claude <[email protected]>
@max-sixty max-sixty mentioned this pull request May 5, 2026
2 of 3 tasks
max-sixty added a commit that referenced this pull request May 5, 2026
## Summary
Release v0.48.0. See
[CHANGELOG.md](https://github.com/max-sixty/worktrunk/blob/release/CHANGELOG.md)
for the full notes.

Highlights:
- `--format=json` extends to seven step + hook commands (#2560)
- `wt step commit` / `wt step squash` gain `--dry-run` (#2557)
- New `dirname` / `basename` template filters (#2592, #2605)
- New `[remove] delete-branch` config option (#2589)
- `wt-perf timeline` subcommand (#2558)
- Faster `wt list` on dirty worktrees (#2602) and faster alias dispatch
(#2556, #2573)
- Short-SHA display honors `core.abbrev` (#2576)
- Cleaner `wt config show` shell-integration section for new users

## Test plan
- [x] `cargo run -- hook pre-merge --yes` (3497 tests, lints clean)
- [x] `cargo semver-checks check-release -p worktrunk` consulted; minor
bump confirmed
- [ ] CI green
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.

2 participants