Skip to content

Commit 0bf19d5

Browse files
fix(server): the only remote's default branch is recognised when it isn't named origin
Git status read the default branch only from origin/HEAD, so in a repository whose only remote has another name the default branch looked like a feature branch. Resolve it from the primary remote (origin when present, else the first remote), the rule the driver already uses elsewhere. Fixes #35064 AI-assisted; verified with the tests and tools listed above.
1 parent 8910c57 commit 0bf19d5

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,23 +1565,37 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
15651565
return Cache.get(refresh ? repositoryPathsRefreshCache : repositoryPathsCache, cacheKey);
15661566
};
15671567

1568+
// Same rule as resolvePrimaryRemoteName: origin when present, else the first remote.
1569+
const resolvePrimaryRemoteDefaultBranch = Effect.fn("resolvePrimaryRemoteDefaultBranch")(
1570+
function* (cwd: string, gitDirArgs: ReadonlyArray<string> = []) {
1571+
const remotesResult = yield* executeGit(
1572+
"GitVcsDriver.resolvePrimaryRemoteDefaultBranch.remotes",
1573+
cwd,
1574+
[...gitDirArgs, "remote"],
1575+
{ allowNonZeroExit: true },
1576+
);
1577+
if (remotesResult.exitCode !== 0) return null;
1578+
const remoteNames = parseRemoteNamesInGitOrder(remotesResult.stdout);
1579+
const remoteName = remoteNames.includes("origin") ? "origin" : remoteNames[0];
1580+
if (remoteName === undefined) return null;
1581+
const headResult = yield* executeGit(
1582+
"GitVcsDriver.resolvePrimaryRemoteDefaultBranch.head",
1583+
cwd,
1584+
[...gitDirArgs, "symbolic-ref", `refs/remotes/${remoteName}/HEAD`],
1585+
{ allowNonZeroExit: true },
1586+
);
1587+
if (headResult.exitCode !== 0) return null;
1588+
return parseDefaultBranchFromRemoteHeadRef(headResult.stdout, remoteName);
1589+
},
1590+
);
1591+
15681592
const defaultBranchCache = yield* Cache.makeWith(
15691593
(gitCommonDir: string) =>
15701594
Effect.gen(function* () {
15711595
const path = yield* Path.Path;
15721596
const fetchCwd =
15731597
path.basename(gitCommonDir) === ".git" ? path.dirname(gitCommonDir) : gitCommonDir;
1574-
return yield* executeGit(
1575-
"GitVcsDriver.statusDetails.defaultBranch",
1576-
fetchCwd,
1577-
["--git-dir", gitCommonDir, "symbolic-ref", "refs/remotes/origin/HEAD"],
1578-
{ allowNonZeroExit: true },
1579-
).pipe(
1580-
Effect.map((result) => {
1581-
if (result.exitCode !== 0) return null;
1582-
return parseDefaultBranchFromRemoteHeadRef(result.stdout, "origin");
1583-
}),
1584-
);
1598+
return yield* resolvePrimaryRemoteDefaultBranch(fetchCwd, ["--git-dir", gitCommonDir]);
15851599
}),
15861600
{
15871601
capacity: 2_048,
@@ -2114,7 +2128,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
21142128
);
21152129
}
21162130

2117-
const defaultBranch = yield* resolveDefaultBranchName(cwd, "origin");
2131+
const defaultBranch = yield* resolvePrimaryRemoteDefaultBranch(cwd);
21182132
const isDefaultBranch =
21192133
branch !== null &&
21202134
(branch === defaultBranch ||
@@ -2260,7 +2274,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
22602274
),
22612275
statusCacheKey
22622276
? Cache.get(defaultBranchCache, statusCacheKey).pipe(Effect.orElseSucceed(() => null))
2263-
: resolveDefaultBranchName(cwd, "origin").pipe(Effect.orElseSucceed(() => null)),
2277+
: resolvePrimaryRemoteDefaultBranch(cwd).pipe(Effect.orElseSucceed(() => null)),
22642278
statusCacheKey
22652279
? Cache.get(primaryRemoteExistsCache, statusCacheKey).pipe(
22662280
Effect.orElseSucceed(() => false),

0 commit comments

Comments
 (0)