refactor: Replace exec with spawn (#8510)

This commit is contained in:
Gal Zahavi
2025-09-16 12:03:17 -07:00
committed by GitHub
parent a015ea203f
commit 986b9fe7e9
11 changed files with 330 additions and 295 deletions
+22 -30
View File
@@ -5,7 +5,7 @@
*/
import { useState, useEffect, useCallback } from 'react';
import { exec } from 'node:child_process';
import { spawnAsync } from '@google/gemini-cli-core';
import fs from 'node:fs';
import fsPromises from 'node:fs/promises';
import path from 'node:path';
@@ -13,36 +13,28 @@ import path from 'node:path';
export function useGitBranchName(cwd: string): string | undefined {
const [branchName, setBranchName] = useState<string | undefined>(undefined);
const fetchBranchName = useCallback(
() =>
exec(
'git rev-parse --abbrev-ref HEAD',
const fetchBranchName = useCallback(async () => {
try {
const { stdout } = await spawnAsync(
'git',
['rev-parse', '--abbrev-ref', 'HEAD'],
{ cwd },
(error, stdout, _stderr) => {
if (error) {
setBranchName(undefined);
return;
}
const branch = stdout.toString().trim();
if (branch && branch !== 'HEAD') {
setBranchName(branch);
} else {
exec(
'git rev-parse --short HEAD',
{ cwd },
(error, stdout, _stderr) => {
if (error) {
setBranchName(undefined);
return;
}
setBranchName(stdout.toString().trim());
},
);
}
},
),
[cwd, setBranchName],
);
);
const branch = stdout.toString().trim();
if (branch && branch !== 'HEAD') {
setBranchName(branch);
} else {
const { stdout: hashStdout } = await spawnAsync(
'git',
['rev-parse', '--short', 'HEAD'],
{ cwd },
);
setBranchName(hashStdout.toString().trim());
}
} catch (_error) {
setBranchName(undefined);
}
}, [cwd, setBranchName]);
useEffect(() => {
fetchBranchName(); // Initial fetch