Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

refuse to sweep when a .git file names no gitdir

[._.]/ Adam Eivy2e29a20f283e
diff --git a/server/lib/gitStaleLock.js b/server/lib/gitStaleLock.js
index 2f14014809..2ab3413639 100644
--- a/server/lib/gitStaleLock.js
+++ b/server/lib/gitStaleLock.js
@@ -173,9 +173,15 @@ export function clearStaleGitLocksIn(gitDir) {
*/
function resolveCommonGitDir(gitPath) {
if (typeof gitPath !== 'string' || !existsSync(gitPath)) return null;
- const pointer = statSync(gitPath).isDirectory()
- ? gitPath
- : resolve(dirname(gitPath), readFileSync(gitPath, 'utf8').match(/^gitdir:\s*(.+)$/m)?.[1].trim() ?? '');
+ let pointer = gitPath;
+ if (!statSync(gitPath).isDirectory()) {
+ // A `.git` file that names no gitdir is not a repository. Returning the
+ // file's own directory here would resolve to the WORKING TREE and walk the
+ // whole checkout looking for locks.
+ const target = readFileSync(gitPath, 'utf8').match(/^gitdir:\s*(\S.*)$/m)?.[1].trim();
+ if (!target) return null;
+ pointer = resolve(dirname(gitPath), target);
+ }
if (!existsSync(pointer) || !statSync(pointer).isDirectory()) return null;
const commondir = join(pointer, 'commondir');
return existsSync(commondir)
diff --git a/server/lib/gitStaleLock.test.js b/server/lib/gitStaleLock.test.js
index da631ea8fc..8e084c62b2 100644
--- a/server/lib/gitStaleLock.test.js
+++ b/server/lib/gitStaleLock.test.js
@@ -141,4 +141,16 @@ describe('clearStaleGitLocksIn', () => {
expect(clearStaleGitLocksIn(pointerFile)).toEqual([shared]);
expect(existsSync(shared)).toBe(false);
});
+
+ it('treats a .git file naming no gitdir as no repository at all', () => {
+ // Resolving this to the file's own directory would point the walk at the
+ // WORKING TREE and sweep the whole checkout.
+ const stray = writeLock('tree/.git-is-broken/x.lock', ABANDONED);
+ mkdirSync(join(dir, 'tree'), { recursive: true });
+ const pointerFile = join(dir, 'tree/.git');
+ writeFileSync(pointerFile, 'not a gitdir line\n');
+
+ expect(clearStaleGitLocksIn(pointerFile)).toEqual([]);
+ expect(existsSync(stray)).toBe(true);
+ });
});