Skip to content

Commit decc121

Browse files
unset git remote url
1 parent a43779c commit decc121

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

packages/backend/src/git.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@ export const cloneRepository = async (
99
onProgress?: onProgressFn
1010
) => {
1111
try {
12-
const git = simpleGit({
13-
progress: onProgress,
14-
});
15-
1612
await mkdir(path, { recursive: true });
1713

18-
await git.cwd({
14+
const git = simpleGit({
15+
progress: onProgress,
16+
}).cwd({
1917
path,
20-
}).init(/*bare = */ true);
18+
})
19+
20+
await git.init(/*bare = */ true);
2121

22-
await git.cwd({
23-
path
24-
}).fetch([
22+
await git.fetch([
2523
remoteUrl.toString(),
2624
// See https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
2725
"+refs/heads/*:refs/heads/*",
@@ -41,14 +39,14 @@ export const fetchRepository = async (
4139
path: string,
4240
onProgress?: onProgressFn
4341
) => {
44-
const git = simpleGit({
45-
progress: onProgress,
46-
});
47-
4842
try {
49-
await git.cwd({
43+
const git = simpleGit({
44+
progress: onProgress,
45+
}).cwd({
5046
path: path,
51-
}).fetch([
47+
})
48+
49+
await git.fetch([
5250
remoteUrl.toString(),
5351
"+refs/heads/*:refs/heads/*",
5452
"--prune",
@@ -87,6 +85,28 @@ export const upsertGitConfig = async (path: string, gitConfig: Record<string, st
8785
}
8886
}
8987

88+
/**
89+
* Unsets the specified keys in the git config for the repo at the given path.
90+
* If a key is not set, this is a no-op.
91+
*/
92+
export const unsetGitConfig = async (path: string, keys: string[], onProgress?: onProgressFn) => {
93+
const git = simpleGit({
94+
progress: onProgress,
95+
}).cwd(path);
96+
97+
try {
98+
for (const key of keys) {
99+
await git.raw(['config', '--unset', key]);
100+
}
101+
} catch (error: unknown) {
102+
if (error instanceof Error) {
103+
throw new Error(`Failed to unset git config ${path}: ${error.message}`);
104+
} else {
105+
throw new Error(`Failed to unset git config ${path}: ${error}`);
106+
}
107+
}
108+
}
109+
90110
/**
91111
* Returns true if `path` is the _root_ of a git repository.
92112
*/

packages/backend/src/repoManager.ts

Lines changed: 9 additions & 3 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, BitbucketConnectionConfig } from '@sourcebot/schemas/v3/connection.type';
66
import { AppContext, Settings, repoMetadataSchema } from "./types.js";
77
import { getRepoPath, getTokenFromConfig, measure, getShardPrefix } from "./utils.js";
8-
import { cloneRepository, fetchRepository, upsertGitConfig } from "./git.js";
8+
import { cloneRepository, fetchRepository, unsetGitConfig, upsertGitConfig } from "./git.js";
99
import { existsSync, readdirSync, promises } from 'fs';
1010
import { indexGitRepository } from "./zoekt.js";
1111
import { PromClient } from './promClient.js';
@@ -254,8 +254,15 @@ export class RepoManager implements IRepoManager {
254254
}
255255

256256
if (existsSync(repoPath) && !isReadOnly) {
257-
logger.info(`Fetching ${repo.displayName}...`);
257+
// @NOTE: in #483, we changed the cloning method s.t., we _no longer_
258+
// write the clone URL (which could contain a auth token) to the
259+
// `remote.origin.url` entry. For the upgrade scenario, we want
260+
// to unset this key since it is no longer needed, hence this line.
261+
// This will no-op if the key is already unset.
262+
// @see: https://github.com/sourcebot-dev/sourcebot/pull/483
263+
await unsetGitConfig(repoPath, ["remote.origin.url"]);
258264

265+
logger.info(`Fetching ${repo.displayName}...`);
259266
const { durationMs } = await measure(() => fetchRepository(
260267
remoteUrl,
261268
repoPath,
@@ -271,7 +278,6 @@ export class RepoManager implements IRepoManager {
271278
} else if (!isReadOnly) {
272279
logger.info(`Cloning ${repo.displayName}...`);
273280

274-
// Use the new secure cloning method that doesn't store credentials in .git/config
275281
const { durationMs } = await measure(() => cloneRepository(
276282
remoteUrl,
277283
repoPath,

0 commit comments

Comments
 (0)