-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use a git cache to speed up clones (#275)
* use a git cache to speed up clones * drop unused git config
- Loading branch information
Showing
3 changed files
with
58 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,54 +2,68 @@ import { parse } from 'yaml'; | |
import * as fs from 'fs-extra'; | ||
import * as os from 'os'; | ||
import * as path from 'path'; | ||
import simpleGit from 'simple-git'; | ||
import simpleGit, { CheckRepoActions } from 'simple-git'; | ||
import { InitRepoOptions } from '../interfaces'; | ||
import { LogLevel } from '../enums'; | ||
import { log } from '../utils/log-util'; | ||
import { ResetMode } from 'simple-git'; | ||
import { Mutex } from 'async-mutex'; | ||
|
||
const baseDir = path.resolve(os.tmpdir(), 'trop-working'); | ||
const baseDir = | ||
process.env.WORKING_DIR ?? path.resolve(os.tmpdir(), 'trop-working'); | ||
|
||
function githubUrl({ slug, accessToken }: InitRepoOptions): string { | ||
return `https://x-access-token:${accessToken}@github.com/${slug}.git`; | ||
} | ||
|
||
const repoMutex = new Map<string, Mutex>(); | ||
function mutexForRepoCache(slug: string) { | ||
if (!repoMutex.has(slug)) repoMutex.set(slug, new Mutex()); | ||
return repoMutex.get(slug)!; | ||
} | ||
|
||
async function updateRepoCache({ slug, accessToken }: InitRepoOptions) { | ||
const cacheDir = path.resolve(baseDir, slug, 'git-cache'); | ||
|
||
await fs.mkdirp(cacheDir); | ||
const git = simpleGit(cacheDir); | ||
if (!(await git.checkIsRepo(CheckRepoActions.BARE))) { | ||
// The repo might be missing, or otherwise somehow corrupt. Re-clone it. | ||
log( | ||
'updateRepoCache', | ||
LogLevel.INFO, | ||
`${cacheDir} was not a git repo, cloning...`, | ||
); | ||
await fs.remove(cacheDir); | ||
await fs.mkdirp(cacheDir); | ||
await git.clone(githubUrl({ slug, accessToken }), '.', ['--bare']); | ||
} | ||
await git.fetch(); | ||
|
||
return cacheDir; | ||
} | ||
|
||
/** | ||
* Initializes the cloned repo trop will use to run backports. | ||
* | ||
* @param {InitRepoOptions} options - repo and payload for repo initialization | ||
* @returns {Object} - an object containing the repo initialization directory | ||
* @returns {{dir: string}} - an object containing the repo initialization directory | ||
*/ | ||
export const initRepo = async ({ slug, accessToken }: InitRepoOptions) => { | ||
export const initRepo = async ({ | ||
slug, | ||
accessToken, | ||
}: InitRepoOptions): Promise<{ dir: string }> => { | ||
log('initRepo', LogLevel.INFO, 'Setting up local repository'); | ||
|
||
await fs.mkdirp(path.resolve(baseDir, slug)); | ||
const prefix = path.resolve(baseDir, slug, 'job-'); | ||
const dir = await fs.mkdtemp(prefix); | ||
|
||
// Ensure that this directory is empty. | ||
await fs.mkdirp(dir); | ||
await fs.remove(dir); | ||
await fs.mkdirp(dir); | ||
|
||
const git = simpleGit(dir); | ||
|
||
await git.clone( | ||
`https://x-access-token:${accessToken}@github.com/${slug}.git`, | ||
'.', | ||
); | ||
|
||
// Clean up just in case. | ||
await git.reset(ResetMode.HARD); | ||
const status = await git.status(); | ||
|
||
for (const file of status.not_added) { | ||
await fs.remove(path.resolve(dir, file)); | ||
} | ||
|
||
await git.pull(); | ||
|
||
const config = fs.readFileSync('./config.yml', 'utf8'); | ||
const { tropEmail, tropName } = parse(config); | ||
await git.addConfig('user.email', tropEmail || '[email protected]'); | ||
await git.addConfig('user.name', tropName || 'Trop Bot'); | ||
// Concurrent access to the repo cache has the potential to mess things up. | ||
await mutexForRepoCache(slug).runExclusive(async () => { | ||
const cacheDir = await updateRepoCache({ slug, accessToken }); | ||
await git.clone(cacheDir, '.'); | ||
}); | ||
|
||
await git.addConfig('commit.gpgsign', 'false'); | ||
return { dir }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters