rtk-ai/rtk

fix(git): detached HEAD status loses commit SHA

Open

#1.854 geöffnet am 12. Mai 2026

Auf GitHub ansehen
 (2 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)Rust (2.914 Forks)batch import
area:clibughelp wantedpriority:medium

Repository-Metriken

Stars
 (48.085 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 11T 1h) (45 gemergte PRs in 30 T)

Beschreibung

:robot: This was written by an AI agent on behalf of @paolomainardi.

Description

When HEAD is detached, rtk git status shows * HEAD (no branch) instead of the actual commit SHA. Raw git status shows HEAD detached at 77a04d3.

The SHA is critical for AI agents — without it, the agent cannot identify which commit it is on in detached HEAD state (common during rebase, bisect, CI pipelines, and checkout of specific commits).

Root Cause

run_status() uses git status --porcelain -b, which reports detached HEAD as:

## HEAD (no branch)

Porcelain v1 does not include the SHA for detached HEAD. format_status_output() passes this through as-is, losing the SHA.

The raw status output (git status without --porcelain) — which is already captured in run_status() for tracking — contains the full info:

HEAD detached at 77a04d3

Reproduction

git checkout HEAD~2

# Raw: shows the SHA
git status | head -1
# Output: HEAD detached at 77a04d3

# RTK: SHA is lost
rtk git status | head -1
# Output: * HEAD (no branch)

Fix

After formatting the porcelain output, check for HEAD (no branch) and replace it with the HEAD detached at <sha> line from the raw output (already available in run_status()). This follows the same pattern used by extract_state_header() to recover rebase/merge state from raw output.

Impact

  • Severity: Medium — agents cannot identify their position in detached HEAD state
  • Token cost: +8 bytes — negligible
  • Version: Verified on v0.39.0

Contributor Guide