rtk-ai/rtk

fix(git): detached HEAD status loses commit SHA

Open

#1 854 ouverte le 12 mai 2026

Voir sur GitHub
 (2 commentaires) (0 réactions) (0 assignés)Rust (2 914 forks)batch import
area:clibughelp wantedpriority:medium

Métriques du dépôt

Stars
 (48 085 stars)
Métriques de merge PR
 (Merge moyen 11j 1h) (45 PRs mergées en 30 j)

Description

: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

Guide contributeur