|
1 | 1 | import { CheckRepoActions, GitConfigScope, simpleGit, SimpleGitProgressEvent } from 'simple-git'; |
| 2 | +import { mkdir } from 'node:fs/promises'; |
| 3 | +import { env } from './env.js'; |
2 | 4 |
|
3 | 5 | type onProgressFn = (event: SimpleGitProgressEvent) => void; |
4 | 6 |
|
5 | | -export const cloneRepository = async (cloneURL: string, path: string, onProgress?: onProgressFn) => { |
6 | | - const git = simpleGit({ |
7 | | - progress: onProgress, |
8 | | - }); |
| 7 | +export const cloneRepository = async ( |
| 8 | + remoteUrl: URL, |
| 9 | + path: string, |
| 10 | + onProgress?: onProgressFn |
| 11 | +) => { |
9 | 12 | try { |
10 | | - await git.clone( |
11 | | - cloneURL, |
12 | | - path, |
13 | | - [ |
14 | | - "--bare", |
15 | | - ] |
16 | | - ); |
| 13 | + await mkdir(path, { recursive: true }); |
17 | 14 |
|
18 | | - await git.cwd({ |
| 15 | + const git = simpleGit({ |
| 16 | + progress: onProgress, |
| 17 | + }).cwd({ |
19 | 18 | path, |
20 | | - }).addConfig("remote.origin.fetch", "+refs/heads/*:refs/heads/*"); |
| 19 | + }) |
| 20 | + |
| 21 | + await git.init(/*bare = */ true); |
| 22 | + |
| 23 | + await git.fetch([ |
| 24 | + remoteUrl.toString(), |
| 25 | + // See https://git-scm.com/book/en/v2/Git-Internals-The-Refspec |
| 26 | + "+refs/heads/*:refs/heads/*", |
| 27 | + "--progress", |
| 28 | + ]); |
21 | 29 | } catch (error: unknown) { |
22 | | - if (error instanceof Error) { |
23 | | - throw new Error(`Failed to clone repository: ${error.message}`); |
| 30 | + const baseLog = `Failed to clone repository: ${path}`; |
| 31 | + |
| 32 | + if (env.SOURCEBOT_LOG_LEVEL !== "debug") { |
| 33 | + // Avoid printing the remote URL (that may contain credentials) to logs by default. |
| 34 | + throw new Error(`${baseLog}. Set environment variable SOURCEBOT_LOG_LEVEL=debug to see the full error message.`); |
| 35 | + } else if (error instanceof Error) { |
| 36 | + throw new Error(`${baseLog}. Reason: ${error.message}`); |
24 | 37 | } else { |
25 | | - throw new Error(`Failed to clone repository: ${error}`); |
| 38 | + throw new Error(`${baseLog}. Error: ${error}`); |
26 | 39 | } |
27 | 40 | } |
28 | | -} |
29 | | - |
30 | | - |
31 | | -export const fetchRepository = async (path: string, onProgress?: onProgressFn) => { |
32 | | - const git = simpleGit({ |
33 | | - progress: onProgress, |
34 | | - }); |
| 41 | +}; |
35 | 42 |
|
| 43 | +export const fetchRepository = async ( |
| 44 | + remoteUrl: URL, |
| 45 | + path: string, |
| 46 | + onProgress?: onProgressFn |
| 47 | +) => { |
36 | 48 | try { |
37 | | - await git.cwd({ |
| 49 | + const git = simpleGit({ |
| 50 | + progress: onProgress, |
| 51 | + }).cwd({ |
38 | 52 | path: path, |
39 | | - }).fetch( |
40 | | - "origin", |
41 | | - [ |
42 | | - "--prune", |
43 | | - "--progress" |
44 | | - ] |
45 | | - ); |
| 53 | + }) |
| 54 | + |
| 55 | + await git.fetch([ |
| 56 | + remoteUrl.toString(), |
| 57 | + "+refs/heads/*:refs/heads/*", |
| 58 | + "--prune", |
| 59 | + "--progress" |
| 60 | + ]); |
46 | 61 | } catch (error: unknown) { |
47 | | - if (error instanceof Error) { |
48 | | - throw new Error(`Failed to fetch repository ${path}: ${error.message}`); |
| 62 | + const baseLog = `Failed to fetch repository: ${path}`; |
| 63 | + if (env.SOURCEBOT_LOG_LEVEL !== "debug") { |
| 64 | + // Avoid printing the remote URL (that may contain credentials) to logs by default. |
| 65 | + throw new Error(`${baseLog}. Set environment variable SOURCEBOT_LOG_LEVEL=debug to see the full error message.`); |
| 66 | + } else if (error instanceof Error) { |
| 67 | + throw new Error(`${baseLog}. Reason: ${error.message}`); |
49 | 68 | } else { |
50 | | - throw new Error(`Failed to fetch repository ${path}: ${error}`); |
| 69 | + throw new Error(`${baseLog}. Error: ${error}`); |
51 | 70 | } |
52 | 71 | } |
53 | 72 | } |
@@ -76,6 +95,33 @@ export const upsertGitConfig = async (path: string, gitConfig: Record<string, st |
76 | 95 | } |
77 | 96 | } |
78 | 97 |
|
| 98 | +/** |
| 99 | + * Unsets the specified keys in the git config for the repo at the given path. |
| 100 | + * If a key is not set, this is a no-op. |
| 101 | + */ |
| 102 | +export const unsetGitConfig = async (path: string, keys: string[], onProgress?: onProgressFn) => { |
| 103 | + const git = simpleGit({ |
| 104 | + progress: onProgress, |
| 105 | + }).cwd(path); |
| 106 | + |
| 107 | + try { |
| 108 | + const configList = await git.listConfig(); |
| 109 | + const setKeys = Object.keys(configList.all); |
| 110 | + |
| 111 | + for (const key of keys) { |
| 112 | + if (setKeys.includes(key)) { |
| 113 | + await git.raw(['config', '--unset', key]); |
| 114 | + } |
| 115 | + } |
| 116 | + } catch (error: unknown) { |
| 117 | + if (error instanceof Error) { |
| 118 | + throw new Error(`Failed to unset git config ${path}: ${error.message}`); |
| 119 | + } else { |
| 120 | + throw new Error(`Failed to unset git config ${path}: ${error}`); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
79 | 125 | /** |
80 | 126 | * Returns true if `path` is the _root_ of a git repository. |
81 | 127 | */ |
|
0 commit comments