fix: resolve no-upstream base from origin tracking branches

Co-authored-by: codex <[email protected]>
This commit is contained in:
Julius Marmingeandcodex authored and Julius Marminge committed 2026-03-01 20:06:01 -08:00
1 parent 882684a80c
commit d0ab35931e
2 files changed
+53 -3

No files matched your search

@@ -1161,6 +1161,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();
+27 -3
View File
@@ -368,6 +368,19 @@ const makeGitCore = Effect.gen(function* () {
allowNonZeroExit: true,
}).pipe(Effect.map((result) => result.code === 0));
const remoteBranchExists = (
cwd: string,
branch: string,
): Effect.Effect<boolean, GitCommandError> =>
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,
@@ -388,12 +401,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}`;
}
}