diff --git a/apps/server/src/git/Layers/GitCore.test.ts b/apps/server/src/git/Layers/GitCore.test.ts index 9eab8ff8c1..f5e5585e67 100644 --- a/apps/server/src/git/Layers/GitCore.test.ts +++ b/apps/server/src/git/Layers/GitCore.test.ts @@ -992,6 +992,32 @@ it.layer(TestLayer)("git integration", (it) => { }), ); + it.effect( + "computes ahead count against origin/default when local default branch is missing", + () => + Effect.gen(function* () { + const remote = yield* makeTmpDir(); + const source = yield* makeTmpDir(); + yield* git(remote, ["init", "--bare"]); + + yield* initRepoWithCommit(source); + yield* git(source, ["remote", "add", "origin", remote]); + yield* git(source, ["push", "-u", "origin", "main"]); + yield* git(source, ["checkout", "-b", "feature/remote-base-only"]); + yield* writeTextFile(path.join(source, "feature.txt"), "ahead of origin/main\n"); + yield* git(source, ["add", "feature.txt"]); + yield* git(source, ["commit", "-m", "feature commit"]); + yield* git(source, ["branch", "-D", "main"]); + + const core = yield* GitCore; + const details = yield* core.statusDetails(source); + expect(details.branch).toBe("feature/remote-base-only"); + expect(details.hasUpstream).toBe(false); + expect(details.aheadCount).toBe(1); + expect(details.behindCount).toBe(0); + }), + ); + it.effect("skips push when no upstream is configured and branch is not ahead of base", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); diff --git a/apps/server/src/git/Layers/GitCore.ts b/apps/server/src/git/Layers/GitCore.ts index 30c2f99041..b7f2a0c9f0 100644 --- a/apps/server/src/git/Layers/GitCore.ts +++ b/apps/server/src/git/Layers/GitCore.ts @@ -287,6 +287,19 @@ const makeGitCore = Effect.gen(function* () { allowNonZeroExit: true, }).pipe(Effect.map((result) => result.code === 0)); + const remoteBranchExists = ( + cwd: string, + branch: string, + ): Effect.Effect => + executeGit( + "GitCore.remoteBranchExists", + cwd, + ["show-ref", "--verify", "--quiet", `refs/remotes/origin/${branch}`], + { + allowNonZeroExit: true, + }, + ).pipe(Effect.map((result) => result.code === 0)); + const resolveBaseBranchForNoUpstream = ( cwd: string, branch: string, @@ -307,12 +320,23 @@ const makeGitCore = Effect.gen(function* () { ]; for (const candidate of candidates) { - if (!candidate || candidate === branch) { + if (!candidate) { continue; } - if (yield* branchExists(cwd, candidate)) { - return candidate; + const normalizedCandidate = candidate.startsWith("origin/") + ? candidate.slice("origin/".length) + : candidate; + if (normalizedCandidate.length === 0 || normalizedCandidate === branch) { + continue; + } + + if (yield* branchExists(cwd, normalizedCandidate)) { + return normalizedCandidate; + } + + if (yield* remoteBranchExists(cwd, normalizedCandidate)) { + return `origin/${normalizedCandidate}`; } }