mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-19 21:00:30 +00:00
Fix status bar git branch detection in worktrees and submodules
This commit is contained in:
parent
9063a71fe6
commit
6cb3135695
2 changed files with 33 additions and 7 deletions
|
|
@ -2,6 +2,10 @@
|
||||||
|
|
||||||
## [Unreleased]
|
## [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
|
## [0.37.2] - 2026-01-05
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
||||||
import { type Component, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
|
import { type Component, truncateToWidth, visibleWidth } from "@mariozechner/pi-tui";
|
||||||
import { existsSync, type FSWatcher, readFileSync, watch } from "fs";
|
import { existsSync, type FSWatcher, readFileSync, statSync, watch } from "fs";
|
||||||
import { dirname, join } from "path";
|
import { dirname, join, resolve } from "path";
|
||||||
import type { AgentSession } from "../../../core/agent-session.js";
|
import type { AgentSession } from "../../../core/agent-session.js";
|
||||||
import { theme } from "../theme/theme.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.
|
* Find the git HEAD path by walking up from cwd.
|
||||||
* Returns the path to .git/HEAD if found, null otherwise.
|
* 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 {
|
function findGitHeadPath(): string | null {
|
||||||
let dir = process.cwd();
|
let dir = process.cwd();
|
||||||
while (true) {
|
while (true) {
|
||||||
const gitHeadPath = join(dir, ".git", "HEAD");
|
const gitPath = join(dir, ".git");
|
||||||
if (existsSync(gitHeadPath)) {
|
if (existsSync(gitPath)) {
|
||||||
return gitHeadPath;
|
try {
|
||||||
|
const stat = statSync(gitPath);
|
||||||
|
if (stat.isFile()) {
|
||||||
|
// Worktree: .git is a file containing "gitdir: <path>"
|
||||||
|
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);
|
const parent = dirname(dir);
|
||||||
if (parent === dir) {
|
if (parent === dir) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue