Fix: Update git branch watcher to use .git/logs/HEAD (#643)
This commit is contained in:
parent
1468047081
commit
bda7ec94df
|
@ -37,14 +37,17 @@ export function useGitBranchName(cwd: string): string | undefined {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
fetchBranchName(); // Initial fetch
|
fetchBranchName(); // Initial fetch
|
||||||
|
|
||||||
const gitHeadPath = path.join(cwd, '.git', 'HEAD');
|
const gitLogsHeadPath = path.join(cwd, '.git', 'logs', 'HEAD');
|
||||||
let watcher: fs.FSWatcher | undefined;
|
let watcher: fs.FSWatcher | undefined;
|
||||||
|
|
||||||
const setupWatcher = async () => {
|
const setupWatcher = async () => {
|
||||||
try {
|
try {
|
||||||
await fsPromises.access(gitHeadPath, fs.constants.F_OK);
|
// Check if .git/logs/HEAD exists, as it might not in a new repo or orphaned head
|
||||||
watcher = fs.watch(gitHeadPath, (eventType) => {
|
await fsPromises.access(gitLogsHeadPath, fs.constants.F_OK);
|
||||||
if (eventType === 'change') {
|
watcher = fs.watch(gitLogsHeadPath, (eventType: string) => {
|
||||||
|
// Changes to .git/logs/HEAD (appends) indicate HEAD has likely changed
|
||||||
|
if (eventType === 'change' || eventType === 'rename') {
|
||||||
|
// Handle rename just in case
|
||||||
fetchBranchName();
|
fetchBranchName();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue