Skip to content

Commit 7f047b8

Browse files
use default branch instead of head for git show operation
1 parent 04c9c3d commit 7f047b8

7 files changed

Lines changed: 34 additions & 3 deletions

File tree

packages/backend/src/github.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export type OctokitRepository = {
2828
stargazers_count?: number,
2929
watchers_count?: number,
3030
subscribers_count?: number,
31+
default_branch?: string,
3132
forks_count?: number,
3233
archived?: boolean,
3334
topics?: string[],

packages/backend/src/repoCompileUtils.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ vi.mock('./git.js', () => ({
66
isPathAValidGitRepoRoot: vi.fn(),
77
getOriginUrl: vi.fn(),
88
isUrlAValidGitRepo: vi.fn(),
9+
getLocalDefaultBranch: vi.fn(),
910
}));
1011

1112
// Mock the glob module

packages/backend/src/repoCompileUtils.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { BitbucketConnectionConfig, GerritConnectionConfig, GiteaConnectionConfi
1515
import { ProjectVisibility } from "azure-devops-node-api/interfaces/CoreInterfaces.js";
1616
import path from 'path';
1717
import { glob } from 'glob';
18-
import { getOriginUrl, isPathAValidGitRepoRoot, isUrlAValidGitRepo } from './git.js';
18+
import { getLocalDefaultBranch, getOriginUrl, isPathAValidGitRepoRoot, isUrlAValidGitRepo } from './git.js';
1919
import assert from 'assert';
2020
import GitUrlParse from 'git-url-parse';
2121
import { RepoMetadata } from '@sourcebot/shared';
@@ -118,6 +118,7 @@ export const createGitHubRepoRecord = ({
118118
cloneUrl: cloneUrl.toString(),
119119
webUrl: repo.html_url,
120120
name: repoName,
121+
defaultBranch: repo.default_branch,
121122
displayName: repoDisplayName,
122123
imageUrl: repo.owner.avatar_url,
123124
isFork: repo.fork,
@@ -185,6 +186,7 @@ export const compileGitlabConfig = async (
185186
cloneUrl: cloneUrl.toString(),
186187
webUrl: projectUrl,
187188
name: repoName,
189+
defaultBranch: project.default_branch,
188190
displayName: repoDisplayName,
189191
imageUrl: avatarUrl,
190192
isFork: isFork,
@@ -257,6 +259,7 @@ export const compileGiteaConfig = async (
257259
webUrl: repo.html_url,
258260
name: repoName,
259261
displayName: repoDisplayName,
262+
defaultBranch: repo.default_branch,
260263
imageUrl: repo.owner?.avatar_url,
261264
isFork: repo.fork!,
262265
isPublic: isPublic,
@@ -339,6 +342,10 @@ export const compileGerritConfig = async (
339342
webUrl: webUrl,
340343
name: repoName,
341344
displayName: repoDisplayName,
345+
// @note: the gerrit api doesn't return the default branch (without a seperate query).
346+
// Instead, the default branch will be set once the repo is cloned.
347+
// @see: repoIndexManager.ts
348+
defaultBranch: undefined,
342349
isFork: false,
343350
isArchived: false,
344351
org: {
@@ -444,6 +451,7 @@ export const compileBitbucketConfig = async (
444451
const repoName = path.join(repoNameRoot, displayName);
445452
const cloneUrl = getCloneUrl(repo);
446453
const webUrl = getWebUrl(repo);
454+
const defaultBranch = isServer ? (repo as BitbucketServerRepository).defaultBranch : (repo as BitbucketCloudRepository).mainbranch?.name;
447455

448456
const record: RepoData = {
449457
external_id: externalId,
@@ -453,6 +461,7 @@ export const compileBitbucketConfig = async (
453461
webUrl: webUrl,
454462
name: repoName,
455463
displayName: displayName,
464+
defaultBranch,
456465
isFork: isFork,
457466
isPublic: isPublic,
458467
isArchived: isArchived,
@@ -557,6 +566,8 @@ export const compileGenericGitHostConfig_file = async (
557566

558567
const remoteUrl = GitUrlParse(origin);
559568

569+
const defaultBranch = await getLocalDefaultBranch({ path: repoPath });
570+
560571
// @note: matches the naming here:
561572
// https://github.com/sourcebot-dev/zoekt/blob/main/gitindex/index.go#L293
562573
// Go's url.URL.Host includes the port if present (even default ports like 443),
@@ -573,6 +584,7 @@ export const compileGenericGitHostConfig_file = async (
573584
cloneUrl: `file://${repoPath}`,
574585
name: repoName,
575586
displayName: repoName,
587+
defaultBranch,
576588
isFork: false,
577589
isArchived: false,
578590
org: {
@@ -612,7 +624,6 @@ export const compileGenericGitHostConfig_file = async (
612624
}
613625
}
614626

615-
616627
export const compileGenericGitHostConfig_url = async (
617628
config: GenericGitHostConnectionConfig,
618629
connectionId: number,
@@ -645,6 +656,10 @@ export const compileGenericGitHostConfig_url = async (
645656
cloneUrl: remoteUrl.toString(),
646657
name: repoName,
647658
displayName: repoName,
659+
// @note: we can't determine the default branch from the remote url.
660+
// Instead, the default branch will be set once the repo is cloned.
661+
// @see: repoIndexManager.ts
662+
defaultBranch: undefined,
648663
isFork: false,
649664
isArchived: false,
650665
org: {
@@ -719,6 +734,7 @@ export const compileAzureDevOpsConfig = async (
719734
webUrl: webUrl,
720735
name: repoName,
721736
displayName: repoDisplayName,
737+
defaultBranch: repo.defaultBranch,
722738
imageUrl: null,
723739
isFork: !!repo.isFork,
724740
isArchived: false,

packages/backend/src/repoIndexManager.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ export class RepoIndexManager {
498498
});
499499

500500
const pushedAt = await getLatestCommitTimestamp({ path: repoPath });
501+
const defaultBranch = await getLocalDefaultBranch({ path: repoPath });
501502

502503
const jobMetadata = repoIndexingJobMetadataSchema.parse(jobData.metadata);
503504

@@ -511,6 +512,13 @@ export class RepoIndexManager {
511512
...(jobData.repo.metadata as RepoMetadata),
512513
indexedRevisions: jobMetadata.indexedRevisions,
513514
} satisfies RepoMetadata,
515+
// @note: always update the default branch. While this field can be set
516+
// during connection syncing, by setting it here we ensure that a) the
517+
// default branch is as up to date as possible (since repo indexing happens
518+
// more frequently than connection syncing) and b) for hosts where it is
519+
// impossible to determine the default branch from the host's API
520+
// (e.g., generic git url), we still set the default branch here.
521+
defaultBranch: defaultBranch,
514522
}
515523
});
516524

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "Repo" ADD COLUMN "defaultBranch" TEXT;

packages/db/prisma/schema.prisma

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ model Repo {
5959
webUrl String?
6060
connections RepoToConnection[]
6161
imageUrl String?
62+
defaultBranch String?
6263
6364
permittedAccounts AccountToRepoPermission[]
6465
permissionSyncJobs RepoPermissionSyncJob[]

packages/web/src/features/search/fileSourceApi.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ export const getFileSource = async ({ path: filePath, repo: repoName, ref }: Fil
2222
const { path: repoPath } = getRepoPath(repo);
2323
const git = simpleGit().cwd(repoPath);
2424

25-
const gitRef = ref ?? 'HEAD';
25+
const gitRef = ref ??
26+
repo.defaultBranch ??
27+
'HEAD';
2628

2729
let source: string;
2830
try {

0 commit comments

Comments
 (0)