Elffuss BraidHub

kikocis/continue public

Merge pull request #7532 from continuedev/dallin/cli-git-branch

feat: show git branch if available
Changesetf64709e50f50c3de4b32a510a586b51382a046f537b813621edcc2b1bd6098db
AuthorNate Sesti <33237525+sestinj@users.noreply.github.com> · 2025-09-08 21:21 -07:00
CommitterGitHub <noreply@github.com> · 2025-09-08 21:21 -07:00
Parents58c9397d0f2d, 4a0adf8304dd

Browse the files at this changeset

11 files changed

added extensions/cli/spec/tui.md +15 −0
@@ -0,0 +1,15 @@
1+# Continue CLI Terminal UI spec
2+
3+This spec is incomplete.
4+
5+## Stack
6+
7+The Continue CLI uses Ink as a react TUI library.
8+
9+## cwd/git display
10+
11+The lower left corner of the TUI should display
12+
13+- the current git branch if in a git repo
14+- also the current owner/repo if enough columns are available to display and it's a gh repo
15+- if not in git, the cwd
added extensions/cli/src/e2e/git-branch-display.test.ts +258 −0
@@ -0,0 +1,258 @@
1+import { execSync } from "child_process";
2+import fs from "fs/promises";
3+import path from "path";
4+
5+import { afterEach, beforeEach, describe, expect, it } from "vitest";
6+
7+import {
8+ cleanupTestContext,
9+ createTestConfig,
10+ createTestContext,
11+ runCLI,
12+ type CLITestContext,
13+} from "../test-helpers/cli-helpers.js";
14+
15+describe("E2E: Git Branch Display", () => {
16+ let context: CLITestContext;
17+ let originalCwd: string;
18+
19+ const testConfig = `name: Test Assistant
20+version: 1.0.0
21+schema: v1
22+models:
23+ - model: gpt-4
24+ provider: openai
25+ apiKey: test-key
26+ roles:
27+ - chat`;
28+
29+ beforeEach(async () => {
30+ context = await createTestContext();
31+ originalCwd = process.cwd();
32+ });
33+
34+ afterEach(async () => {
35+ await cleanupTestContext(context);
36+ process.chdir(originalCwd);
37+ });
38+
39+ describe("git branch display in status bar", () => {
40+ it("should show branch name when in a git repository", async () => {
41+ // Initialize a git repo in the test directory
42+ process.chdir(context.testDir);
43+
44+ try {
45+ // Initialize git repo
46+ execSync("git init", { stdio: "ignore" });
47+ execSync("git config user.email 'test@example.com'", {
48+ stdio: "ignore",
49+ });
50+ execSync("git config user.name 'Test User'", { stdio: "ignore" });
51+
52+ // Create a test file and initial commit
53+ await fs.writeFile(
54+ path.join(context.testDir, "test.txt"),
55+ "test content",
56+ );
57+ execSync("git add .", { stdio: "ignore" });
58+ execSync("git commit -m 'Initial commit'", { stdio: "ignore" });
59+
60+ // Create and checkout a test branch
61+ const testBranchName = "feature/test-branch";
62+ execSync(`git checkout -b ${testBranchName}`, { stdio: "ignore" });
63+
64+ // Create config file
65+ await createTestConfig(context, testConfig);
66+
67+ // Run CLI in TUI mode - it should fail quickly but show the branch in output
68+ const result = await runCLI(context, {
69+ args: ["--config", context.configPath!, "--help"],
70+ timeout: 3000,
71+ });
72+
73+ // The help command should succeed
74+ expect(result.exitCode).toBe(0);
75+ expect(result.stdout).toContain("Continue CLI");
76+
77+ // Now let's test that our git utils work in this context
78+ // We can't easily test the full TUI display, but we can verify the git functions work
79+ const { getGitBranch } = await import("../util/git.js");
80+ const branch = getGitBranch();
81+ expect(branch).toBe(testBranchName);
82+ } catch (error) {
83+ // Skip test if git is not available or fails
84+ console.warn("Skipping git branch test - git not available:", error);
85+ return;
86+ }
87+ });
88+
89+ it("should handle non-git directory gracefully", async () => {
90+ // Don't initialize git in test directory
91+ process.chdir(context.testDir);
92+
93+ await createTestConfig(context, testConfig);
94+
95+ // Test that git functions return null in non-git directory
96+ const { getGitBranch, isGitRepo } = await import("../util/git.js");
97+
98+ expect(isGitRepo()).toBe(false);
99+ expect(getGitBranch()).toBeNull();
100+
101+ // CLI should still work without git
102+ const result = await runCLI(context, {
103+ args: ["--help"],
104+ timeout: 3000,
105+ });
106+
107+ expect(result.exitCode).toBe(0);
108+ expect(result.stdout).toContain("Continue CLI");
109+ });
110+
111+ it("should handle detached HEAD state", async () => {
112+ process.chdir(context.testDir);
113+
114+ try {
115+ // Initialize git repo
116+ execSync("git init", { stdio: "ignore" });
117+ execSync("git config user.email 'test@example.com'", {
118+ stdio: "ignore",
119+ });
120+ execSync("git config user.name 'Test User'", { stdio: "ignore" });
121+
122+ // Create initial commit
123+ await fs.writeFile(
124+ path.join(context.testDir, "test.txt"),
125+ "test content",
126+ );
127+ execSync("git add .", { stdio: "ignore" });
128+ execSync("git commit -m 'Initial commit'", { stdio: "ignore" });
129+
130+ // Create another commit
131+ await fs.writeFile(
132+ path.join(context.testDir, "test2.txt"),
133+ "test content 2",
134+ );
135+ execSync("git add .", { stdio: "ignore" });
136+ execSync("git commit -m 'Second commit'", { stdio: "ignore" });
137+
138+ // Get first commit hash and checkout to detached HEAD
139+ const firstCommit = execSync(
140+ "git rev-list --max-count=1 --reverse HEAD",
141+ {
142+ encoding: "utf-8",
143+ },
144+ ).trim();
145+ execSync(`git checkout ${firstCommit}`, { stdio: "ignore" });
146+
147+ // Test git functions in detached HEAD state
148+ const { getGitBranch, isGitRepo } = await import("../util/git.js");
149+
150+ expect(isGitRepo()).toBe(true);
151+ // In detached HEAD state, git branch --show-current returns empty string
152+ // which our function converts to null
153+ expect(getGitBranch()).toBeNull();
154+ } catch (error) {
155+ // Skip test if git operations fail
156+ console.warn(
157+ "Skipping detached HEAD test - git operations failed:",
158+ error,
159+ );
160+ return;
161+ }
162+ });
163+
164+ it("should handle branch names with special characters", async () => {
165+ process.chdir(context.testDir);
166+
167+ try {
168+ // Initialize git repo
169+ execSync("git init", { stdio: "ignore" });
170+ execSync("git config user.email 'test@example.com'", {
171+ stdio: "ignore",
172+ });
173+ execSync("git config user.name 'Test User'", { stdio: "ignore" });
174+
175+ // Create initial commit
176+ await fs.writeFile(
177+ path.join(context.testDir, "test.txt"),
178+ "test content",
179+ );
180+ execSync("git add .", { stdio: "ignore" });
181+ execSync("git commit -m 'Initial commit'", { stdio: "ignore" });
182+
183+ // Create branch with special characters
184+ const specialBranchName = "feature/fix-123_with-dashes.and.dots";
185+ execSync(`git checkout -b "${specialBranchName}"`, { stdio: "ignore" });
186+
187+ // Test git functions
188+ const { getGitBranch } = await import("../util/git.js");
189+ const branch = getGitBranch();
190+ expect(branch).toBe(specialBranchName);
191+ } catch (error) {
192+ // Skip test if git operations fail
193+ console.warn(
194+ "Skipping special characters test - git operations failed:",
195+ error,
196+ );
197+ return;
198+ }
199+ });
200+ });
201+
202+ describe("getRepoUrlText function with branch", () => {
203+ it("should format repo URL with branch correctly", async () => {
204+ process.chdir(context.testDir);
205+
206+ try {
207+ // Initialize git repo with remote
208+ execSync("git init", { stdio: "ignore" });
209+ execSync("git config user.email 'test@example.com'", {
210+ stdio: "ignore",
211+ });
212+ execSync("git config user.name 'Test User'", { stdio: "ignore" });
213+
214+ // Add a fake remote
215+ execSync(
216+ "git remote add origin https://github.com/testuser/testrepo.git",
217+ { stdio: "ignore" },
218+ );
219+
220+ // Create initial commit and branch
221+ await fs.writeFile(
222+ path.join(context.testDir, "test.txt"),
223+ "test content",
224+ );
225+ execSync("git add .", { stdio: "ignore" });
226+ execSync("git commit -m 'Initial commit'", { stdio: "ignore" });
227+ execSync("git checkout -b test-branch", { stdio: "ignore" });
228+
229+ // Test the formatting function
230+ const { getResponsiveRepoText } = await import(
231+ "../ui/hooks/useTUIChatHooks.js"
232+ );
233+ const repoText = getResponsiveRepoText();
234+
235+ // Should include both repo and branch
236+ expect(repoText).toEqual("");
237+ } catch (error) {
238+ // Skip test if git operations fail
239+ console.warn(
240+ "Skipping repo URL formatting test - git operations failed:",
241+ error,
242+ );
243+ return;
244+ }
245+ });
246+
247+ it("should handle repo URL without branch when not in git repo", async () => {
248+ process.chdir(context.testDir);
249+
250+ // Test without git repo
251+ const { getRepoUrlText } = await import("../ui/hooks/useTUIChatHooks.js");
252+ const repoText = getRepoUrlText();
253+
254+ // Should not contain branch separator
255+ expect(repoText).toEqual("");
256+ });
257+ });
258+});
modified extensions/cli/src/ui/TUIChat.tsx +1 −4
@@ -26,7 +26,6 @@
2626 import { useContextPercentage } from "./hooks/useContextPercentage.js";
2727 import { useMessageRenderer } from "./hooks/useMessageRenderer.js";
2828 import {
29- getRepoUrlText,
3029 useCurrentMode,
3130 useIntroMessage,
3231 useLoginHandlers,
@@ -147,8 +146,6 @@
147146 const { services, allServicesReady, isRemoteMode } =
148147 useTUIChatServices(remoteUrl);
149148
150- const repoURlText = useMemo(() => getRepoUrlText(remoteUrl), [remoteUrl]);
151-
152149 // Use navigation context
153150 const {
154151 state: navState,
@@ -338,7 +335,7 @@
338335 {/* Free trial status and Continue CLI info - always show */}
339336 <BottomStatusBar
340337 currentMode={currentMode}
341- repoURLText={repoURlText}
338+ remoteUrl={remoteUrl}
342339 isRemoteMode={isRemoteMode}
343340 services={services}
344341 navState={navState}
modified extensions/cli/src/ui/UpdateNotification.tsx +5 −1
@@ -66,7 +66,11 @@
6666 if (isRemoteMode) {
6767 return <Text color="cyan">◉ Remote Mode</Text>;
6868 }
69- return <Text color="gray">● Continue CLI</Text>;
69+ return (
70+ <Text wrap="truncate-end" color="gray">
71+ ● Continue CLI
72+ </Text>
73+ );
7074 }
7175
7276 return <Text color="dim">{text}</Text>;
modified extensions/cli/src/ui/__tests__/BottomStatusBarCtrlC.test.tsx +7 −4
@@ -1,8 +1,11 @@
11 import { render } from "ink-testing-library";
22 import React from "react";
3-import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
3+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
44
5-import { BottomStatusBar } from "../components/BottomStatusBar.js";
5+import {
6+ BottomStatusBar,
7+ BottomStatusBarProps,
8+} from "../components/BottomStatusBar.js";
69
710 // Mock the index module functions
811 vi.mock("../../index.js", () => ({
@@ -11,9 +14,9 @@
1114 }));
1215
1316 describe("BottomStatusBar Ctrl+C message", () => {
14- const defaultProps = {
17+ const defaultProps: BottomStatusBarProps = {
1518 currentMode: "normal" as const,
16- repoURLText: "github.com/user/repo",
19+ remoteUrl: "github.com/user/repo",
1720 isRemoteMode: false,
1821 services: { model: { model: null } },
1922 navState: { currentScreen: "chat" },
modified extensions/cli/src/ui/components/BottomStatusBar.tsx +8 −5
@@ -9,10 +9,11 @@
99
1010 import { ContextPercentageDisplay } from "./ContextPercentageDisplay.js";
1111 import { ModeIndicator } from "./ModeIndicator.js";
12+import { ResponsiveRepoDisplay } from "./ResponsiveRepoDisplay.js";
1213
13-interface BottomStatusBarProps {
14+export interface BottomStatusBarProps {
1415 currentMode: PermissionMode;
15- repoURLText: string;
16+ remoteUrl?: string;
1617 isRemoteMode: boolean;
1718 services: any;
1819 navState: any;
@@ -24,7 +25,7 @@
2425
2526 export const BottomStatusBar: React.FC<BottomStatusBarProps> = ({
2627 currentMode,
27- repoURLText,
28+ remoteUrl,
2829 isRemoteMode,
2930 services,
3031 navState,
@@ -52,10 +53,12 @@
5253 <Text key="image-paste-hint" color="cyan" wrap="truncate-start">
5354 Press Ctrl+V to paste image
5455 </Text>
55- ) : (
56+ ) : showingExitMessage ? (
5657 <Text key="repo-url" color="dim" wrap="truncate-start">
57- {showingExitMessage ? "ctrl+c to exit" : repoURLText}
58+ {"ctrl+c to exit"}
5859 </Text>
60+ ) : (
61+ <ResponsiveRepoDisplay remoteUrl={remoteUrl} />
5962 )}
6063 <Text key="repo-separator"> </Text>
6164 </React.Fragment>
added extensions/cli/src/ui/components/ResponsiveRepoDisplay.tsx +35 −0
@@ -0,0 +1,35 @@
1+import { Text } from "ink";
2+import React, { useMemo } from "react";
3+
4+import { useTerminalSize } from "../hooks/useTerminalSize.js";
5+import { getResponsiveRepoText } from "../hooks/useTUIChatHooks.js";
6+
7+interface ResponsiveRepoDisplayProps {
8+ remoteUrl?: string;
9+ reservedWidth?: number; // Width reserved for other elements in the same row
10+}
11+
12+export const ResponsiveRepoDisplay: React.FC<ResponsiveRepoDisplayProps> = ({
13+ remoteUrl,
14+}) => {
15+ const { columns } = useTerminalSize();
16+
17+ const repoText = useMemo(() => {
18+ // Calculate available width for repo display
19+ // Reserve space for margins, mode indicator, context percentage, and other UI elements
20+ const availableWidth = Math.floor(columns / 2);
21+
22+ return getResponsiveRepoText(remoteUrl, availableWidth);
23+ }, [remoteUrl, columns]);
24+
25+ // Don't render if no text to show
26+ if (!repoText) {
27+ return null;
28+ }
29+
30+ return (
31+ <Text color="dim" wrap="truncate-start">
32+ {repoText}
33+ </Text>
34+ );
35+};
added extensions/cli/src/ui/hooks/useTUIChatHooks.test.ts +118 −0
@@ -0,0 +1,118 @@
1+import { describe, expect, it, vi, beforeEach } from "vitest";
2+
3+import { getResponsiveRepoText, getRepoInfo } from "./useTUIChatHooks.js";
4+
5+// Mock the git utilities
6+vi.mock("../../util/git.js", () => ({
7+ isGitRepo: vi.fn(() => true),
8+ getGitRemoteUrl: vi.fn(() => "https://github.com/testuser/testrepo.git"),
9+ getGitBranch: vi.fn(() => "feature/test-branch"),
10+}));
11+
12+describe("Responsive repo text functionality", () => {
13+ beforeEach(async () => {
14+ // Reset mocks before each test
15+ const gitModule = await import("../../util/git.js");
16+ vi.mocked(gitModule.isGitRepo).mockReturnValue(true);
17+ vi.mocked(gitModule.getGitRemoteUrl).mockReturnValue(
18+ "https://github.com/testuser/testrepo.git",
19+ );
20+ vi.mocked(gitModule.getGitBranch).mockReturnValue("feature/test-branch");
21+ });
22+ describe("getRepoInfo", () => {
23+ it("should extract repo info correctly", () => {
24+ const info = getRepoInfo();
25+
26+ expect(info.repoName).toBe("testuser/testrepo");
27+ expect(info.branchName).toBe("feature/test-branch");
28+ expect(info.isGitRepo).toBe(true);
29+ });
30+
31+ it("should handle remote URL override", () => {
32+ const info = getRepoInfo("https://github.com/override/repo.git");
33+
34+ expect(info.repoName).toBe("override/repo");
35+ expect(info.branchName).toBe("feature/test-branch");
36+ expect(info.isGitRepo).toBe(true);
37+ });
38+ });
39+
40+ describe("getResponsiveRepoText", () => {
41+ it("should return empty string when no width provided", () => {
42+ const result = getResponsiveRepoText();
43+ expect(result).toBe("");
44+ });
45+
46+ it("should return full text when width allows", () => {
47+ const result = getResponsiveRepoText(undefined, 100);
48+ expect(result).toBe("testuser/testrepo ⊦feature/test-branch");
49+ });
50+
51+ it("should prefer branch when both fit individually", () => {
52+ // Full text: "testuser/testrepo ⊦ feature/test-branch" (38 chars)
53+ // Repo only: "testuser/testrepo" (17 chars)
54+ // Branch only: "feature/test-branch" (18 chars)
55+ // With width 20, both repo and branch fit, but we prefer branch
56+ const result = getResponsiveRepoText(undefined, 20);
57+ expect(result).toBe("feature/test-branch");
58+ });
59+
60+ it("should fallback to repo when branch doesn't fit but repo does", async () => {
61+ // Use a long branch name
62+ const gitModule = await import("../../util/git.js");
63+ vi.mocked(gitModule.getGitBranch).mockReturnValue(
64+ "very-long-feature-branch-name-that-wont-fit",
65+ );
66+
67+ const result = getResponsiveRepoText(undefined, 20);
68+ // Branch is too long, repo should fit
69+ expect(result).toBe("testuser/testrepo");
70+ });
71+
72+ it("should return branch only when repo doesn't fit but branch does", async () => {
73+ // When space is very limited, prefer shorter option
74+ const gitModule = await import("../../util/git.js");
75+ vi.mocked(gitModule.getGitRemoteUrl).mockReturnValue(
76+ "https://github.com/very-long-repo-name/testrepo.git",
77+ );
78+ vi.mocked(gitModule.getGitBranch).mockReturnValue("main");
79+
80+ const result = getResponsiveRepoText(undefined, 10);
81+ // "main" (4 chars) should fit, "very-long-repo-name/testrepo" won't
82+ expect(result).toBe("main");
83+ });
84+
85+ it("should return empty string when nothing fits", () => {
86+ const result = getResponsiveRepoText(undefined, 5);
87+ expect(result).toBe("");
88+ });
89+
90+ it("should return empty string when width is too small", () => {
91+ const result = getResponsiveRepoText(undefined, 2);
92+ expect(result).toBe("");
93+ });
94+
95+ it("should handle non-git repos", async () => {
96+ const gitModule = await import("../../util/git.js");
97+ vi.mocked(gitModule.isGitRepo).mockReturnValue(false);
98+ vi.mocked(gitModule.getGitBranch).mockReturnValue(null);
99+
100+ const result = getResponsiveRepoText(
101+ "https://github.com/user/repo.git",
102+ 50,
103+ );
104+ expect(result).toBe("user/repo");
105+ });
106+
107+ it("should return empty string when repo name is too long in non-git scenario", async () => {
108+ const gitModule = await import("../../util/git.js");
109+ vi.mocked(gitModule.isGitRepo).mockReturnValue(false);
110+
111+ const result = getResponsiveRepoText(
112+ "https://github.com/very-long-user/repo.git",
113+ 8,
114+ );
115+ expect(result).toBe("");
116+ });
117+ });
118+});
modified extensions/cli/src/ui/hooks/useTUIChatHooks.ts +89 −13
@@ -2,31 +2,107 @@
22
33 import type { PermissionMode } from "../../permissions/types.js";
44 import { modeService } from "../../services/ModeService.js";
5-import { getGitRemoteUrl, isGitRepo } from "../../util/git.js";
5+import { getGitBranch, getGitRemoteUrl, isGitRepo } from "../../util/git.js";
66 import type { ConfigOption, ModelOption } from "../types/selectorTypes.js";
77
88 import { useConfigSelector } from "./useConfigSelector.js";
99 import { useModelSelector } from "./useModelSelector.js";
1010
11-// Helper function to get repo URL text
12-export function getRepoUrlText(remoteUrl?: string): string {
11+// Helper function to get repo info for responsive display
12+export function getRepoInfo(remoteUrl?: string): {
13+ repoName: string;
14+ branchName: string | null;
15+ fullPath: string;
16+ isGitRepo: boolean;
17+} {
1318 let url = remoteUrl ?? "";
14- if (!url) {
15- const isGit = isGitRepo();
16- if (isGit) {
17- const gitUrl = getGitRemoteUrl();
18- if (gitUrl) {
19- url = gitUrl;
20- }
19+ const isGit = isGitRepo();
20+ let branchName: string | null = null;
21+
22+ if (!url && isGit) {
23+ const gitUrl = getGitRemoteUrl();
24+ if (gitUrl) {
25+ url = gitUrl;
2126 }
2227 }
28+
2329 if (!url) {
2430 url = process.cwd();
2531 }
2632
27- url = url.replace(/\.git$/, "");
28- url = url.replace(/^(https|http):\/\/.*?\//, "");
29- return url;
33+ // Get branch info if we're in a git repo
34+ if (isGit) {
35+ branchName = getGitBranch();
36+ }
37+
38+ // Clean up the URL
39+ const cleanUrl = url
40+ .replace(/\.git$/, "")
41+ .replace(/^(https|http):\/\/.*?\//, "");
42+
43+ return {
44+ repoName: cleanUrl,
45+ branchName,
46+ fullPath: url,
47+ isGitRepo: isGit,
48+ };
49+}
50+
51+// Helper function to get responsive repo URL text based on available width
52+export function getResponsiveRepoText(
53+ remoteUrl?: string,
54+ availableWidth: number = 0,
55+): string {
56+ const repoInfo = getRepoInfo(remoteUrl);
57+
58+ // If no available width provided, show nothing
59+ if (availableWidth <= 0) {
60+ return "";
61+ }
62+
63+ // Calculate the minimum space needed for different display options
64+ const branchSeparator = " ⊦";
65+ const branchText = repoInfo.branchName || "";
66+ const repoText = repoInfo.repoName;
67+
68+ // Priority order for display:
69+ // 1. repo + branch (if everything fits)
70+ // 2. branch only (preferred when both fit individually)
71+ // 3. repo only (fallback when branch doesn't fit)
72+ // 4. nothing if neither fit
73+
74+ if (repoInfo.isGitRepo && repoInfo.branchName) {
75+ const fullText = `${repoText}${branchSeparator}${branchText}`;
76+ const branchOnlyText = branchText;
77+ const repoOnlyText = repoText;
78+
79+ // If full text fits, use it
80+ if (fullText.length <= availableWidth) {
81+ return fullText;
82+ }
83+
84+ // Prefer branch if it fits (more immediately useful)
85+ if (branchOnlyText.length <= availableWidth) {
86+ return branchOnlyText;
87+ }
88+
89+ // If branch doesn't fit but repo does, use repo
90+ if (repoOnlyText.length <= availableWidth) {
91+ return repoOnlyText;
92+ }
93+ }
94+
95+ // No branch or not in git repo
96+ if (repoText.length <= availableWidth) {
97+ return repoText;
98+ }
99+
100+ return "";
101+}
102+
103+// Helper function to get repo URL text (legacy compatibility)
104+export function getRepoUrlText(remoteUrl?: string): string {
105+ return getResponsiveRepoText(remoteUrl);
30106 }
31107
32108 // Custom hook for intro message
modified extensions/cli/src/util/git.test.ts +14 −1
@@ -1,6 +1,10 @@
11 import { beforeEach, describe, expect, it } from "vitest";
22
3-import { getGitHubActionsRepoUrl, isGitHubActions } from "./git.js";
3+import {
4+ getGitBranch,
5+ getGitHubActionsRepoUrl,
6+ isGitHubActions,
7+} from "./git.js";
48
59 describe("git utilities - GitHub Actions detection", () => {
610 beforeEach(() => {
@@ -74,4 +78,13 @@
7478 expect(result).toBe("https://git.company.com/enterprise/repo");
7579 });
7680 });
81+
82+ describe("getGitBranch", () => {
83+ it("should return null when not in a git repo or when git command fails", () => {
84+ // Since we can't easily mock git commands in this test environment,
85+ // we just test that the function returns either a string or null
86+ const result = getGitBranch();
87+ expect(typeof result === "string" || result === null).toBe(true);
88+ });
89+ });
7790 });
modified extensions/cli/src/util/git.ts +17 −0
@@ -18,6 +18,23 @@
1818 }
1919 }
2020
21+/**
22+ * Get the current git branch name
23+ * @returns The current branch name or null if not found
24+ */
25+export function getGitBranch(): string | null {
26+ try {
27+ const result = execSync("git branch --show-current", {
28+ encoding: "utf-8",
29+ cwd: process.cwd(),
30+ stdio: "pipe",
31+ });
32+ return result.trim() || null;
33+ } catch {
34+ return null;
35+ }
36+}
37+
2138 /**
2239 * Check if current directory is a git repository
2340 */