Skip to content

Commit db6bded

Browse files
(fix) Always upsert metadata.gitConfig into the repo s.t., it is synced
1 parent d55bf83 commit db6bded

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

packages/backend/src/git.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
import { simpleGit, SimpleGitProgressEvent } from 'simple-git';
22

3-
export const cloneRepository = async (cloneURL: string, path: string, gitConfig?: Record<string, string>, onProgress?: (event: SimpleGitProgressEvent) => void) => {
3+
export const cloneRepository = async (cloneURL: string, path: string, onProgress?: (event: SimpleGitProgressEvent) => void) => {
44
const git = simpleGit({
55
progress: onProgress,
66
});
7-
8-
const configParams = Object.entries(gitConfig ?? {}).flatMap(
9-
([key, value]) => ['--config', `${key}=${value}`]
10-
);
11-
127
try {
138
await git.clone(
149
cloneURL,
1510
path,
1611
[
1712
"--bare",
18-
...configParams
1913
]
2014
);
2115

@@ -48,6 +42,26 @@ export const fetchRepository = async (path: string, onProgress?: (event: SimpleG
4842
}
4943
}
5044

45+
/**
46+
* Applies the gitConfig to the repo at the given path. Note that this will
47+
* override the values for any existing keys, and append new values for keys
48+
* that do not exist yet. It will _not_ remove any existing keys that are not
49+
* present in gitConfig.
50+
*/
51+
export const upsertGitConfig = async (path: string, gitConfig: Record<string, string>, onProgress?: (event: SimpleGitProgressEvent) => void) => {
52+
const git = simpleGit({
53+
progress: onProgress,
54+
}).cwd(path);
55+
56+
try {
57+
for (const [key, value] of Object.entries(gitConfig)) {
58+
await git.addConfig(key, value);
59+
}
60+
} catch (error) {
61+
throw new Error(`Failed to set git config ${path}`);
62+
}
63+
}
64+
5165
export const getBranches = async (path: string) => {
5266
const git = simpleGit();
5367
const branches = await git.cwd({

packages/backend/src/repoManager.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Connection, PrismaClient, Repo, RepoToConnection, RepoIndexingStatus, S
55
import { GithubConnectionConfig, GitlabConnectionConfig, GiteaConnectionConfig } from '@sourcebot/schemas/v3/connection.type';
66
import { AppContext, Settings, RepoMetadata } from "./types.js";
77
import { getRepoPath, getTokenFromConfig, measure, getShardPrefix } from "./utils.js";
8-
import { cloneRepository, fetchRepository } from "./git.js";
8+
import { cloneRepository, fetchRepository, upsertGitConfig } from "./git.js";
99
import { existsSync, readdirSync, promises } from 'fs';
1010
import { indexGitRepository } from "./zoekt.js";
1111
import { PromClient } from './promClient.js';
@@ -240,7 +240,7 @@ export class RepoManager implements IRepoManager {
240240
}
241241
}
242242

243-
const { durationMs } = await measure(() => cloneRepository(cloneUrl.toString(), repoPath, metadata.gitConfig, ({ method, stage, progress }) => {
243+
const { durationMs } = await measure(() => cloneRepository(cloneUrl.toString(), repoPath, ({ method, stage, progress }) => {
244244
this.logger.debug(`git.${method} ${stage} stage ${progress}% complete for ${repo.id}`)
245245
}));
246246
cloneDuration_s = durationMs / 1000;
@@ -249,6 +249,13 @@ export class RepoManager implements IRepoManager {
249249
this.logger.info(`Cloned ${repo.id} in ${cloneDuration_s}s`);
250250
}
251251

252+
// Regardless of clone or fetch, always upsert the git config for the repo.
253+
// This ensures that the git config is always up to date for whatever we
254+
// have in the DB.
255+
if (metadata.gitConfig) {
256+
await upsertGitConfig(repoPath, metadata.gitConfig);
257+
}
258+
252259
this.logger.info(`Indexing ${repo.id}...`);
253260
const { durationMs } = await measure(() => indexGitRepository(repo, this.settings, this.ctx));
254261
const indexDuration_s = durationMs / 1000;

packages/backend/src/zoekt.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,17 @@ export const indexGitRepository = async (repo: Repo, settings: Settings, ctx: Ap
5757
revisions = revisions.slice(0, 64);
5858
}
5959

60-
const command = `zoekt-git-index -allow_missing_branches -index ${ctx.indexPath} -max_trigram_count ${settings.maxTrigramCount} -file_limit ${settings.maxFileSize} -branches ${revisions.join(',')} -tenant_id ${repo.orgId} -shard_prefix ${shardPrefix} ${repoPath}`;
60+
const command = [
61+
'zoekt-git-index',
62+
'-allow_missing_branches',
63+
`-index ${ctx.indexPath}`,
64+
`-max_trigram_count ${settings.maxTrigramCount}`,
65+
`-file_limit ${settings.maxFileSize}`,
66+
`-branches ${revisions.join(',')}`,
67+
`-tenant_id ${repo.orgId}`,
68+
`-shard_prefix ${shardPrefix}`,
69+
repoPath
70+
].join(' ');
6171

6272
return new Promise<{ stdout: string, stderr: string }>((resolve, reject) => {
6373
exec(command, (error, stdout, stderr) => {

0 commit comments

Comments
 (0)