MCPcopy Index your code
hub / github.com/sourcebot-dev/sourcebot / fetchRepository

Function fetchRepository

packages/backend/src/git.ts:122–175  ·  view source on GitHub ↗
(
    {
        cloneUrl,
        authHeader,
        path,
        onProgress,
        signal,
    }: {
        cloneUrl: string,
        authHeader?: string,
        path: string,
        onProgress?: onProgressFn,
        signal?: AbortSignal
    }
)

Source from the content-addressed store, hash-verified

120};
121
122export const fetchRepository = async (
123 {
124 cloneUrl,
125 authHeader,
126 path,
127 onProgress,
128 signal,
129 }: {
130 cloneUrl: string,
131 authHeader?: string,
132 path: string,
133 onProgress?: onProgressFn,
134 signal?: AbortSignal
135 }
136) => {
137 const git = createGitClientForPath(path, onProgress, signal);
138 try {
139 if (authHeader) {
140 await git.addConfig("http.extraHeader", authHeader);
141 }
142
143 await git.fetch([
144 cloneUrl,
145 "+refs/heads/*:refs/heads/*",
146 "--prune",
147 "--progress",
148 ]);
149
150 // Update HEAD to match the remote's default branch. This handles the case where the remote's
151 // default branch changes.
152 const remoteDefaultBranch = await getRemoteDefaultBranch({
153 path,
154 cloneUrl,
155 });
156
157 if (remoteDefaultBranch) {
158 await git.raw(['symbolic-ref', 'HEAD', `refs/heads/${remoteDefaultBranch}`]);
159 }
160 } catch (error: unknown) {
161 const baseLog = `Failed to fetch repository: ${path}`;
162 if (env.SOURCEBOT_LOG_LEVEL !== "debug") {
163 // Avoid printing the remote URL (that may contain credentials) to logs by default.
164 throw new Error(`${baseLog}. Set environment variable SOURCEBOT_LOG_LEVEL=debug to see the full error message.`);
165 } else if (error instanceof Error) {
166 throw new Error(`${baseLog}. Reason: ${error.message}`);
167 } else {
168 throw new Error(`${baseLog}. Error: ${error}`);
169 }
170 } finally {
171 if (authHeader) {
172 await git.raw(["config", "--unset", "http.extraHeader", authHeader]);
173 }
174 }
175}
176
177/**
178 * Applies the gitConfig to the repo at the given path. Note that this will

Callers 1

indexRepositoryMethod · 0.85

Calls 2

createGitClientForPathFunction · 0.85
getRemoteDefaultBranchFunction · 0.85

Tested by

no test coverage detected