Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify util/git state #6670

Merged
merged 1 commit into from Jul 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
39 changes: 16 additions & 23 deletions lib/util/git/index.ts
Expand Up @@ -105,8 +105,6 @@ let config: LocalConfig = {} as any;

let git: Git.SimpleGit | undefined;

let cwd: string | undefined;

let privateKeySet = false;

async function resetToBranch(branchName: string): Promise<void> {
Expand Down Expand Up @@ -162,25 +160,20 @@ export async function initRepo(args: StorageConfig): Promise<void> {
cleanRepo();

config = { ...args } as any;
const newConfig: LocalConfig = config;

cwd = newConfig.localDir;
const newCwd = cwd;

newConfig.branchExists = {};
logger.debug('Initializing git repository into ' + newCwd);
const gitHead = join(newCwd, '.git/HEAD');
config.branchExists = {};
logger.debug('Initializing git repository into ' + config.localDir);
const gitHead = join(config.localDir, '.git/HEAD');
let clone = true;

if (await fs.exists(gitHead)) {
try {
git = Git(newCwd).silent(true);
await git.raw(['remote', 'set-url', 'origin', newConfig.url]);
git = Git(config.localDir).silent(true);
await git.raw(['remote', 'set-url', 'origin', config.url]);
const fetchStart = Date.now();
await git.fetch(['--depth=10']);
newConfig.baseBranch =
newConfig.baseBranch || (await getDefaultBranch(git));
await resetToBranch(newConfig.baseBranch);
config.baseBranch = config.baseBranch || (await getDefaultBranch(git));
await resetToBranch(config.baseBranch);
await cleanLocalBranches();
await git.raw(['remote', 'prune', 'origin']);
const durationMs = Math.round(Date.now() - fetchStart);
Expand All @@ -191,18 +184,18 @@ export async function initRepo(args: StorageConfig): Promise<void> {
}
}
if (clone) {
await fs.emptyDir(newCwd);
git = Git(newCwd).silent(true);
await fs.emptyDir(config.localDir);
git = Git(config.localDir).silent(true);
const cloneStart = Date.now();
try {
// clone only the default branch
let opts = ['--depth=2'];
if (newConfig.extraCloneOpts) {
if (config.extraCloneOpts) {
opts = opts.concat(
Object.entries(newConfig.extraCloneOpts).map((e) => `${e[0]}=${e[1]}`)
Object.entries(config.extraCloneOpts).map((e) => `${e[0]}=${e[1]}`)
);
}
await git.clone(newConfig.url, '.', opts);
await git.clone(config.url, '.', opts);
} catch (err) /* istanbul ignore next */ {
logger.debug({ err }, 'git clone error');
if (err.message?.includes('write error: No space left on device')) {
Expand Down Expand Up @@ -248,7 +241,7 @@ export async function initRepo(args: StorageConfig): Promise<void> {
throw new Error(REPOSITORY_TEMPORARY_ERROR);
}

newConfig.baseBranch = newConfig.baseBranch || (await getDefaultBranch(git));
config.baseBranch = config.baseBranch || (await getDefaultBranch(git));
}

// istanbul ignore next
Expand Down Expand Up @@ -501,7 +494,7 @@ export async function commitFiles({
}: CommitFilesConfig): Promise<string | null> {
logger.debug(`Committing files to branch ${branchName}`);
if (!privateKeySet) {
await writePrivateKey(cwd);
await writePrivateKey(config.localDir);
privateKeySet = true;
}
try {
Expand All @@ -514,7 +507,7 @@ export async function commitFiles({
// istanbul ignore if
if (file.name === '|delete|') {
deleted.push(file.contents);
} else if (await isDirectory(join(cwd, file.name))) {
} else if (await isDirectory(join(config.localDir, file.name))) {
fileNames.push(file.name);
await git.add(file.name);
} else {
Expand All @@ -526,7 +519,7 @@ export async function commitFiles({
} else {
contents = file.contents;
}
await fs.outputFile(join(cwd, file.name), contents);
await fs.outputFile(join(config.localDir, file.name), contents);
}
}
// istanbul ignore if
Expand Down