From 6cb313569516be2f30ca82b3514c2ddd29aa179b Mon Sep 17 00:00:00 2001 From: Kevin Date: Mon, 5 Jan 2026 21:20:56 -0600 Subject: [PATCH] Fix status bar git branch detection in worktrees and submodules --- packages/coding-agent/CHANGELOG.md | 4 +++ .../modes/interactive/components/footer.ts | 36 +++++++++++++++---- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md index 4ed345af..a285733b 100644 --- a/packages/coding-agent/CHANGELOG.md +++ b/packages/coding-agent/CHANGELOG.md @@ -2,6 +2,10 @@ ## [Unreleased] +### Fixed + +- Status bar now shows correct git branch when running in a git worktree ([#490](https://github.com/badlogic/pi-mono/pull/490) by [@kcosr](https://github.com/kcosr)) + ## [0.37.2] - 2026-01-05 ### Fixed diff --git a/packages/coding-agent/src/modes/interactive/components/footer.ts b/packages/coding-agent/src/modes/interactive/components/footer.ts index 2063e28f..cb31b62c 100644 --- a/packages/coding-agent/src/modes/interactive/components/footer.ts +++ b/packages/coding-agent/src/modes/interactive/components/footer.ts @@ -1,7 +1,7 @@ import type { AssistantMessage } from "@mariozechner/pi-ai"; import { type Component, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui"; -import { existsSync, type FSWatcher, readFileSync, watch } from "fs"; -import { dirname, join } from "path"; +import { existsSync, type FSWatcher, readFileSync, statSync, watch } from "fs"; +import { dirname, join, resolve } from "path"; import type { AgentSession } from "../../../core/agent-session.js"; import { theme } from "../theme/theme.js"; @@ -18,15 +18,37 @@ function sanitizeStatusText(text: string): string { } /** - * Find the git root directory by walking up from cwd. - * Returns the path to .git/HEAD if found, null otherwise. + * Find the git HEAD path by walking up from cwd. + * Handles both regular git repos (.git is a directory) and worktrees (.git is a file). + * Returns the path to the HEAD file if found, null otherwise. */ function findGitHeadPath(): string | null { let dir = process.cwd(); while (true) { - const gitHeadPath = join(dir, ".git", "HEAD"); - if (existsSync(gitHeadPath)) { - return gitHeadPath; + const gitPath = join(dir, ".git"); + if (existsSync(gitPath)) { + try { + const stat = statSync(gitPath); + if (stat.isFile()) { + // Worktree: .git is a file containing "gitdir: " + const content = readFileSync(gitPath, "utf8").trim(); + if (content.startsWith("gitdir: ")) { + const gitDir = content.slice(8); + const headPath = resolve(dir, gitDir, "HEAD"); + if (existsSync(headPath)) { + return headPath; + } + } + } else if (stat.isDirectory()) { + // Regular repo: .git is a directory + const headPath = join(gitPath, "HEAD"); + if (existsSync(headPath)) { + return headPath; + } + } + } catch { + return null; + } } const parent = dirname(dir); if (parent === dir) {