Skip to content

Commit ca4d640

Browse files
wip
1 parent eace574 commit ca4d640

File tree

5 files changed

+26
-13
lines changed

5 files changed

+26
-13
lines changed

packages/backend/src/git.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,21 @@ export const fetchRepository = async (
120120
await git.fetch([
121121
cloneUrl,
122122
"+refs/heads/*:refs/heads/*",
123-
"+HEAD:HEAD",
124123
"--prune",
125124
"--progress"
126125
]);
126+
127+
// Update HEAD to match the remote's default branch. This handles the case where the remote's
128+
// default branch changes.
129+
const remoteHead = await git.raw(['ls-remote', '--symref', cloneUrl, 'HEAD']);
130+
const match = remoteHead.match(/^ref: refs\/heads\/(\S+)\s+HEAD/m);
131+
if (match) {
132+
const defaultBranch = match[1];
133+
await git.raw(['symbolic-ref', 'HEAD', `refs/heads/${defaultBranch}`]);
134+
return defaultBranch;
135+
}
136+
137+
return undefined;
127138
} catch (error: unknown) {
128139
const baseLog = `Failed to fetch repository: ${path}`;
129140
if (env.SOURCEBOT_LOG_LEVEL !== "debug") {
@@ -298,4 +309,4 @@ export const getCommitHashForRefName = async ({
298309
logger.debug(error);
299310
return undefined;
300311
}
301-
}
312+
}

packages/backend/src/repoIndexManager.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ export class RepoIndexManager {
337337
}
338338
}
339339

340+
let defaultBranch: string | undefined = undefined;
341+
340342
if (existsSync(repoPath) && !isReadOnly) {
341343
// @NOTE: in #483, we changed the cloning method s.t., we _no longer_
342344
// write the clone URL (which could contain a auth token) to the
@@ -351,7 +353,7 @@ export class RepoIndexManager {
351353
});
352354

353355
logger.info(`Fetching ${repo.name} (id: ${repo.id})...`);
354-
const { durationMs } = await measure(() => fetchRepository({
356+
const { durationMs, data: remoteDefaultBranch } = await measure(() => fetchRepository({
355357
cloneUrl: cloneUrlMaybeWithToken,
356358
authHeader,
357359
path: repoPath,
@@ -365,6 +367,7 @@ export class RepoIndexManager {
365367
process.stdout.write('\n');
366368
logger.info(`Fetched ${repo.name} (id: ${repo.id}) in ${fetchDuration_s}s`);
367369

370+
defaultBranch = remoteDefaultBranch;
368371
} else if (!isReadOnly) {
369372
logger.info(`Cloning ${repo.name} (id: ${repo.id})...`);
370373

@@ -394,9 +397,7 @@ export class RepoIndexManager {
394397
});
395398
}
396399

397-
let revisions = [
398-
'HEAD'
399-
];
400+
let revisions = defaultBranch ? [defaultBranch] : ['HEAD'];
400401

401402
if (metadata.branches) {
402403
const branchGlobs = metadata.branches

packages/web/src/app/[domain]/components/pathHeader.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ interface FileHeaderProps {
3131
displayName?: string;
3232
webUrl?: string;
3333
},
34+
isBranchDisplayNameVisible?: boolean;
3435
branchDisplayName?: string;
3536
branchDisplayTitle?: string;
3637
isCodeHostIconVisible?: boolean;
@@ -53,6 +54,7 @@ export const PathHeader = ({
5354
path,
5455
pathHighlightRange,
5556
branchDisplayName,
57+
isBranchDisplayNameVisible = !!branchDisplayName,
5658
branchDisplayTitle,
5759
pathType = 'blob',
5860
isCodeHostIconVisible = true,
@@ -224,7 +226,7 @@ export const PathHeader = ({
224226
>
225227
{info?.displayName}
226228
</Link>
227-
{branchDisplayName && (
229+
{(isBranchDisplayNameVisible && branchDisplayName) && (
228230
<p
229231
className="text-xs font-semibold text-gray-500 dark:text-gray-400 mt-[3px] flex items-center gap-0.5"
230232
title={branchDisplayTitle}

packages/web/src/app/[domain]/search/components/searchResultsPage.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,8 @@ export const SearchResultsPage = ({
104104

105105
// Look for any files that are not on the default branch.
106106
const isBranchFilteringEnabled = useMemo(() => {
107-
return files.some((file) => {
108-
return file.branches?.some((branch) => branch !== 'HEAD') ?? false;
109-
});
110-
}, [files]);
107+
return searchQuery.includes('rev:');
108+
}, [searchQuery]);
111109

112110
useEffect(() => {
113111
if (isStreaming || !stats) {

packages/web/src/app/[domain]/search/components/searchResultsPanel/fileMatchContainer.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ export const FileMatchContainer = ({
7070
}, [file.branches]);
7171

7272
const branchDisplayName = useMemo(() => {
73-
if (!isBranchFilteringEnabled || branches.length === 0) {
73+
if (branches.length === 0) {
7474
return undefined;
7575
}
7676

7777
return `${branches[0]}${branches.length > 1 ? ` +${branches.length - 1}` : ''}`;
78-
}, [branches, isBranchFilteringEnabled]);
78+
}, [branches]);
7979

8080
const repo = useMemo(() => {
8181
return repoInfo[file.repositoryId];
@@ -99,6 +99,7 @@ export const FileMatchContainer = ({
9999
}}
100100
path={file.fileName.text}
101101
pathHighlightRange={fileNameRange}
102+
isBranchDisplayNameVisible={isBranchFilteringEnabled}
102103
branchDisplayName={branchDisplayName}
103104
branchDisplayTitle={branches.join(", ")}
104105
/>

0 commit comments

Comments
 (0)