Fix git branch indicator in subdirectories
Walk up directory hierarchy to find .git/HEAD instead of only checking cwd. fixes #156
This commit is contained in:
@@ -2,10 +2,30 @@ import type { AgentState } from "@mariozechner/pi-agent-core";
|
||||
import type { AssistantMessage } from "@mariozechner/pi-ai";
|
||||
import { type Component, visibleWidth } from "@mariozechner/pi-tui";
|
||||
import { existsSync, type FSWatcher, readFileSync, watch } from "fs";
|
||||
import { join } from "path";
|
||||
import { dirname, join } from "path";
|
||||
import { isModelUsingOAuth } from "../../../core/model-config.js";
|
||||
import { theme } from "../theme/theme.js";
|
||||
|
||||
/**
|
||||
* Find the git root directory by walking up from cwd.
|
||||
* Returns the path to .git/HEAD 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 parent = dirname(dir);
|
||||
if (parent === dir) {
|
||||
// Reached filesystem root
|
||||
return null;
|
||||
}
|
||||
dir = parent;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Footer component that shows pwd, token stats, and context usage
|
||||
*/
|
||||
@@ -40,8 +60,8 @@ export class FooterComponent implements Component {
|
||||
this.gitWatcher = null;
|
||||
}
|
||||
|
||||
const gitHeadPath = join(process.cwd(), ".git", "HEAD");
|
||||
if (!existsSync(gitHeadPath)) {
|
||||
const gitHeadPath = findGitHeadPath();
|
||||
if (!gitHeadPath) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -87,7 +107,11 @@ export class FooterComponent implements Component {
|
||||
}
|
||||
|
||||
try {
|
||||
const gitHeadPath = join(process.cwd(), ".git", "HEAD");
|
||||
const gitHeadPath = findGitHeadPath();
|
||||
if (!gitHeadPath) {
|
||||
this.cachedBranch = null;
|
||||
return null;
|
||||
}
|
||||
const content = readFileSync(gitHeadPath, "utf8").trim();
|
||||
|
||||
if (content.startsWith("ref: refs/heads/")) {
|
||||
|
||||
Reference in New Issue
Block a user