Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions apps/server/src/vcs/GitVcsDriverCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3431,7 +3431,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
}),
);

it.effect("reports remote status on unborn HEAD without failing", () =>
it.effect("reports status on unborn HEAD without failing or dropping its upstream", () =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
const driver = yield* GitVcsDriver.GitVcsDriver;
Expand All @@ -3445,6 +3445,17 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
assert.equal(status.hasUpstream, false);
assert.equal(status.aheadCount, 0);
assert.equal(status.behindCount, 0);

const seed = yield* makeTmpDir();
const remote = yield* makeTmpDir("git-remote-");
yield* initRepoWithCommit(seed);
yield* git(remote, ["init", "--bare"]);
yield* git(seed, ["push", remote, `HEAD:refs/heads/${initialBranch}`]);
yield* git(cwd, ["remote", "add", "origin", remote]);
yield* git(cwd, ["fetch", "origin"]);
yield* git(cwd, ["config", `branch.${initialBranch}.remote`, "origin"]);
yield* git(cwd, ["config", `branch.${initialBranch}.merge`, `refs/heads/${initialBranch}`]);
assert.equal((yield* driver.statusDetailsLocal(cwd)).hasUpstream, true);
}),
);

Expand Down Expand Up @@ -5668,7 +5679,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
}),
);

it.effect("pushes with upstream setup and skips when already up to date", () =>
it.effect("pushes, skips when up to date, and republishes a deleted upstream", () =>
Effect.gen(function* () {
const cwd = yield* makeTmpDir();
const remote = yield* makeTmpDir("git-remote-");
Expand Down Expand Up @@ -5703,6 +5714,17 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
status: "skipped_up_to_date",
branch: "feature/push",
});

yield* git(cwd, ["push", "origin", "--delete", "feature/push"]);
yield* writeTextFile(cwd, "followup.txt", "followup\n");
yield* git(cwd, ["add", "-A"]);
yield* (yield* GitVcsDriver.GitVcsDriver).commit(cwd, "Add followup", "");
const republished = yield* (yield* GitVcsDriver.GitVcsDriver).pushCurrentBranch(cwd, null);
assert.deepInclude(republished, { status: "pushed", branch: "feature/push" });
assert.equal(
yield* git(remote, ["--git-dir=.", "rev-parse", "refs/heads/feature/push"]),
yield* git(cwd, ["rev-parse", "HEAD"]),
);
}),
);

Expand Down
22 changes: 17 additions & 5 deletions apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2232,6 +2232,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*

let refName: string | null = null;
let upstreamRef: string | null = null;
// Git omits branch.ab when the upstream ref is gone, e.g. deleted after its PR merged,
// and also for an unborn HEAD, which can still track a fetched upstream.
let upstreamRefExists = false;
let isUnbornHead = false;
let aheadCount = 0;
let behindCount = 0;
let aheadOfDefaultCount = 0;
Expand All @@ -2242,6 +2246,10 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
const statusRecords = statusStdout.split("\0");
for (let index = 0; index < statusRecords.length; index++) {
const line = statusRecords[index]!;
if (line === "# branch.oid (initial)") {
isUnbornHead = true;
continue;
}
if (line.startsWith("# branch.head ")) {
const value = line.slice("# branch.head ".length).trim();
refName = value.startsWith("(") ? null : value;
Expand All @@ -2252,11 +2260,13 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
upstreamRef = value.length > 0 ? value : null;
continue;
}
if (includeDivergence && line.startsWith("# branch.ab ")) {
const value = line.slice("# branch.ab ".length).trim();
const parsed = parseBranchAb(value);
aheadCount = parsed.ahead;
behindCount = parsed.behind;
if (line.startsWith("# branch.ab ")) {
upstreamRefExists = true;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
if (includeDivergence) {
const parsed = parseBranchAb(line.slice("# branch.ab ".length).trim());
aheadCount = parsed.ahead;
behindCount = parsed.behind;
}
continue;
}
if (line.length > 0 && !line.startsWith("#")) {
Expand All @@ -2275,6 +2285,8 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
}
}

if (!upstreamRefExists && !isUnbornHead) upstreamRef = null;
Comment thread
saphid marked this conversation as resolved.
Resolved

const isDefaultBranch =
refName !== null &&
(refName === defaultBranch ||
Expand Down
Loading