Skip to content

Commit 0bc993a

Browse files
only21milmaria-rcksjuliusmarminge
authored
fix(server): Git status finds the default branch when the only remote isn't origin (pingdotgg#28815)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 451e375 commit 0bc993a

2 files changed

Lines changed: 72 additions & 16 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4480,6 +4480,56 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
44804480
}),
44814481
);
44824482

4483+
it.effect.each(["checkout", "bare-backed worktree"] as const)(
4484+
"uses the only remote's HEAD for default-branch detection without origin in a %s",
4485+
(layout) =>
4486+
Effect.gen(function* () {
4487+
const driver = yield* GitVcsDriver.GitVcsDriver;
4488+
const path = yield* Path.Path;
4489+
const source = yield* makeTmpDir();
4490+
const remote = yield* makeTmpDir("git-vcs-driver-github-");
4491+
yield* initRepoWithCommit(source);
4492+
yield* git(remote, ["init", "--bare"]);
4493+
yield* git(source, ["branch", "-M", "trunk"]);
4494+
yield* git(source, ["remote", "add", "github", remote]);
4495+
yield* git(source, ["push", "-u", "github", "trunk"]);
4496+
let cwd = source;
4497+
if (layout === "bare-backed worktree") {
4498+
const container = yield* makeTmpDir("git-vcs-driver-bare-");
4499+
const gitDirArgs = ["--git-dir", path.join(container, "repo.git")];
4500+
cwd = path.join(container, "trunk");
4501+
yield* git(container, ["init", "--bare", "repo.git"]);
4502+
yield* git(container, [...gitDirArgs, "config", "user.email", "[email protected]"]);
4503+
yield* git(container, [...gitDirArgs, "config", "user.name", "Test"]);
4504+
yield* git(container, [...gitDirArgs, "remote", "add", "github", remote]);
4505+
yield* git(container, [...gitDirArgs, "fetch", "github"]);
4506+
yield* git(container, [
4507+
...gitDirArgs,
4508+
"worktree",
4509+
"add",
4510+
"--track",
4511+
"-b",
4512+
"trunk",
4513+
cwd,
4514+
"github/trunk",
4515+
]);
4516+
}
4517+
yield* git(cwd, [
4518+
"symbolic-ref",
4519+
"refs/remotes/github/HEAD",
4520+
"refs/remotes/github/trunk",
4521+
]);
4522+
4523+
assert.isTrue((yield* driver.statusDetailsLocal(cwd)).isDefaultBranch);
4524+
const remoteStatus = yield* driver.statusDetailsRemote(cwd);
4525+
assert.equal(remoteStatus.defaultBranch, "trunk");
4526+
assert.isTrue(remoteStatus.isDefaultBranch);
4527+
4528+
yield* git(cwd, ["remote", "add", "origin", remote]);
4529+
assert.isNull((yield* driver.statusDetailsRemote(cwd)).defaultBranch);
4530+
}),
4531+
);
4532+
44834533
it.effect("makes background upstream status fetches non-interactive", () =>
44844534
Effect.gen(function* () {
44854535
const cwd = yield* makeTmpDir();

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,17 +1580,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
15801580
const path = yield* Path.Path;
15811581
const fetchCwd =
15821582
path.basename(gitCommonDir) === ".git" ? path.dirname(gitCommonDir) : gitCommonDir;
1583-
return yield* executeGit(
1584-
"GitVcsDriver.statusDetails.defaultBranch",
1585-
fetchCwd,
1586-
["--git-dir", gitCommonDir, "symbolic-ref", "refs/remotes/origin/HEAD"],
1587-
{ allowNonZeroExit: true },
1588-
).pipe(
1589-
Effect.map((result) => {
1590-
if (result.exitCode !== 0) return null;
1591-
return parseDefaultBranchFromRemoteHeadRef(result.stdout, "origin");
1592-
}),
1593-
);
1583+
return yield* resolvePrimaryDefaultBranchName(fetchCwd, ["--git-dir", gitCommonDir]);
15941584
}),
15951585
{
15961586
capacity: 2_048,
@@ -1726,11 +1716,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
17261716
const resolveDefaultBranchName = (
17271717
cwd: string,
17281718
remoteName: string,
1719+
gitDirArgs: ReadonlyArray<string> = [],
17291720
): Effect.Effect<string | null, GitCommandError> =>
17301721
executeGit(
17311722
"GitVcsDriver.resolveDefaultBranchName",
17321723
cwd,
1733-
["symbolic-ref", `refs/remotes/${remoteName}/HEAD`],
1724+
[...gitDirArgs, "symbolic-ref", `refs/remotes/${remoteName}/HEAD`],
17341725
{ allowNonZeroExit: true },
17351726
).pipe(
17361727
Effect.map((result) => {
@@ -1764,8 +1755,11 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
17641755
const originRemoteExists = (cwd: string): Effect.Effect<boolean, GitCommandError> =>
17651756
remoteExists({ cwd, remoteName: "origin" });
17661757

1767-
const listRemoteNames = (cwd: string): Effect.Effect<ReadonlyArray<string>, GitCommandError> =>
1768-
runGitStdout("GitVcsDriver.listRemoteNames", cwd, ["remote"]).pipe(
1758+
const listRemoteNames = (
1759+
cwd: string,
1760+
gitDirArgs: ReadonlyArray<string> = [],
1761+
): Effect.Effect<ReadonlyArray<string>, GitCommandError> =>
1762+
runGitStdout("GitVcsDriver.listRemoteNames", cwd, [...gitDirArgs, "remote"]).pipe(
17691763
Effect.map(parseRemoteNamesInGitOrder),
17701764
);
17711765

@@ -1788,6 +1782,18 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
17881782
return firstRemote ?? null;
17891783
});
17901784

1785+
const resolvePrimaryDefaultBranchName = Effect.fn("GitVcsDriver.resolvePrimaryDefaultBranchName")(
1786+
function* (cwd: string, gitDirArgs: ReadonlyArray<string> = []) {
1787+
const originDefault = yield* resolveDefaultBranchName(cwd, "origin", gitDirArgs);
1788+
if (originDefault !== null) return originDefault;
1789+
const remoteNames = yield* listRemoteNames(cwd, gitDirArgs);
1790+
const [firstRemote] = remoteNames;
1791+
return firstRemote === undefined || remoteNames.includes("origin")
1792+
? null
1793+
: yield* resolveDefaultBranchName(cwd, firstRemote, gitDirArgs);
1794+
},
1795+
);
1796+
17911797
const resolvePrimaryRemoteName = (cwd: string) =>
17921798
findPrimaryRemoteName(cwd).pipe(
17931799
Effect.filterOrFail(
@@ -2123,7 +2129,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
21232129
);
21242130
}
21252131

2126-
const defaultBranch = yield* resolveDefaultBranchName(cwd, "origin");
2132+
const defaultBranch = yield* resolvePrimaryDefaultBranchName(cwd);
21272133
const isDefaultBranch =
21282134
branch !== null &&
21292135
(branch === defaultBranch ||
@@ -2269,7 +2275,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
22692275
),
22702276
statusCacheKey
22712277
? Cache.get(defaultBranchCache, statusCacheKey).pipe(Effect.orElseSucceed(() => null))
2272-
: resolveDefaultBranchName(cwd, "origin").pipe(Effect.orElseSucceed(() => null)),
2278+
: resolvePrimaryDefaultBranchName(cwd).pipe(Effect.orElseSucceed(() => null)),
22732279
statusCacheKey
22742280
? Cache.get(primaryRemoteExistsCache, statusCacheKey).pipe(
22752281
Effect.orElseSucceed(() => false),

0 commit comments

Comments
 (0)