( installMetadata: ExtensionInstallMetadata, destination: string, )
| 29 | * @param destination The destination path to clone the repository to. |
| 30 | */ |
| 31 | export async function cloneFromGit( |
| 32 | installMetadata: ExtensionInstallMetadata, |
| 33 | destination: string, |
| 34 | ): Promise<void> { |
| 35 | try { |
| 36 | const git = simpleGit(destination); |
| 37 | let sourceUrl = installMetadata.source; |
| 38 | const token = getGitHubToken(); |
| 39 | if (token) { |
| 40 | try { |
| 41 | const parsedUrl = new URL(sourceUrl); |
| 42 | if ( |
| 43 | parsedUrl.protocol === 'https:' && |
| 44 | parsedUrl.hostname === 'github.com' |
| 45 | ) { |
| 46 | if (!parsedUrl.username) { |
| 47 | parsedUrl.username = token; |
| 48 | } |
| 49 | sourceUrl = parsedUrl.toString(); |
| 50 | } |
| 51 | } catch { |
| 52 | // If source is not a valid URL, we don't inject the token. |
| 53 | // We let git handle the source as is. |
| 54 | } |
| 55 | } |
| 56 | await git.clone(sourceUrl, './', ['--depth', '1']); |
| 57 | |
| 58 | const remotes = await git.getRemotes(true); |
| 59 | if (remotes.length === 0) { |
| 60 | throw new Error( |
| 61 | `Unable to find any remotes for repo ${installMetadata.source}`, |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | const refToFetch = installMetadata.ref || 'HEAD'; |
| 66 | |
| 67 | await git.fetch(remotes[0].name, refToFetch); |
| 68 | |
| 69 | // After fetching, checkout FETCH_HEAD to get the content of the fetched ref. |
| 70 | // This results in a detached HEAD state, which is fine for this purpose. |
| 71 | await git.checkout('FETCH_HEAD'); |
| 72 | } catch (error) { |
| 73 | throw new Error( |
| 74 | `Failed to clone Git repository from ${installMetadata.source} ${getErrorMessage(error)}`, |
| 75 | { |
| 76 | cause: error, |
| 77 | }, |
| 78 | ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | export interface GithubRepoInfo { |
| 83 | owner: string; |
no test coverage detected