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

Return null, not an empty string, for a commitless repo's previous HEAD

[._.]/ Adam Eivyce7471c2b059
diff --git a/server/services/git.js b/server/services/git.js
index 5cc884f55a..633f206594 100644
--- a/server/services/git.js
+++ b/server/services/git.js
@@ -1262,7 +1262,13 @@ export async function resetToDefaultBranch(dir) {
execGitSafe(['rev-parse', 'HEAD', '--abbrev-ref', 'HEAD'], dir),
getStatusPorcelain(dir).catch(() => '')
]);
- const [previousHead = null, previousBranch = null] = headBefore.stdout.trim().split('\n').map(line => line.trim());
+ // `|| null` rather than a destructuring default: on a repo with no commits
+ // rev-parse prints nothing, `''.split('\n')` is `['']`, and a default only
+ // fires on `undefined` — so the caller would get `''` where the contract (and
+ // the client's `if (result.previousHead)` recovery hint) says null.
+ const [head, branchName] = headBefore.stdout.trim().split('\n').map(line => line.trim());
+ const previousHead = head || null;
+ const previousBranch = branchName || null;
// `checkout --force -B` is the whole operation: `-B` repoints the local branch
// at the remote ref and `--force` resets index AND working tree to it, so a
diff --git a/server/services/git.resetToDefault.test.js b/server/services/git.resetToDefault.test.js
index b79dc9a70c..42cc6d78de 100644
--- a/server/services/git.resetToDefault.test.js
+++ b/server/services/git.resetToDefault.test.js
@@ -98,6 +98,18 @@ describe('resetToDefaultBranch', () => {
expect(result.discardedFiles).toBe(2);
});
+ it('reports null, not an empty string, when a repo has no commits to name', async () => {
+ // `''.split('\n')` is `['']`, and a destructuring default only fires on
+ // undefined — so the empty case has to be normalized explicitly or the
+ // contract (and the client's `if (result.previousHead)` hint) gets `''`.
+ routeGit({ ...HAPPY_PATH, 'rev-parse HEAD --abbrev-ref HEAD': ok('') });
+
+ const result = await resetToDefaultBranch('/repo');
+
+ expect(result.previousHead).toBeNull();
+ expect(result.previousBranch).toBeNull();
+ });
+
it('never runs git clean — untracked files are not recoverable from a reflog', async () => {
routeGit(HAPPY_PATH);