From a301a0ad83560422b6c956dc419bb6e2afa7b72f Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Tue, 9 Mar 2021 21:44:15 +0100 Subject: [PATCH] Refactor getChangesSinceMergeBase() code --- dist/index.js | 36 ++++++++++++++++-------------------- src/git.ts | 35 ++++++++++++++++------------------- 2 files changed, 32 insertions(+), 39 deletions(-) diff --git a/dist/index.js b/dist/index.js index b9fb240..44274fb 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3873,29 +3873,25 @@ async function getChangesSinceMergeBase(base, initialFetchDepth) { let noMergeBase = false; core.startGroup(`Searching for merge-base ${baseRef}...${exports.HEAD}`); try { - let init = true; - let lastCommitCount = await getCommitCount(); - let depth = Math.max(lastCommitCount * 2, initialFetchDepth); - while (!(await hasMergeBase())) { - if (init) { - await exec_1.default('git', ['fetch', `--depth=${depth}`, 'origin', base, exports.HEAD]); - init = false; - } - else { + if (!(await hasMergeBase())) { + await exec_1.default('git', ['fetch', `--depth=${initialFetchDepth}`, 'origin', base, exports.HEAD]); + let depth = initialFetchDepth; + let lastCommitCount = await getCommitCount(); + while (!(await hasMergeBase())) { + depth = Math.min(depth * 2, Number.MAX_SAFE_INTEGER); await exec_1.default('git', ['fetch', `--deepen=${depth}`, 'origin', base, exports.HEAD]); - } - const commitCount = await getCommitCount(); - if (commitCount === lastCommitCount) { - core.info('No more commits were fetched'); - core.info('Last attempt will be to fetch full history'); - await exec_1.default('git', ['fetch']); - if (!(await hasMergeBase())) { - noMergeBase = true; + const commitCount = await getCommitCount(); + if (commitCount === lastCommitCount) { + core.info('No more commits were fetched'); + core.info('Last attempt will be to fetch full history'); + await exec_1.default('git', ['fetch']); + if (!(await hasMergeBase())) { + noMergeBase = true; + } + break; } - break; + lastCommitCount = commitCount; } - depth = Math.min(depth * 2, Number.MAX_SAFE_INTEGER); - lastCommitCount = commitCount; } } finally { diff --git a/src/git.ts b/src/git.ts index b31dedf..d81b1f5 100644 --- a/src/git.ts +++ b/src/git.ts @@ -64,28 +64,25 @@ export async function getChangesSinceMergeBase(base: string, initialFetchDepth: let noMergeBase = false core.startGroup(`Searching for merge-base ${baseRef}...${HEAD}`) try { - let init = true - let lastCommitCount = await getCommitCount() - let depth = Math.max(lastCommitCount * 2, initialFetchDepth) - while (!(await hasMergeBase())) { - if (init) { - await exec('git', ['fetch', `--depth=${depth}`, 'origin', base, HEAD]) - init = false - } else { + if (!(await hasMergeBase())) { + await exec('git', ['fetch', `--depth=${initialFetchDepth}`, 'origin', base, HEAD]) + let depth = initialFetchDepth + let lastCommitCount = await getCommitCount() + while (!(await hasMergeBase())) { + depth = Math.min(depth * 2, Number.MAX_SAFE_INTEGER) await exec('git', ['fetch', `--deepen=${depth}`, 'origin', base, HEAD]) - } - const commitCount = await getCommitCount() - if (commitCount === lastCommitCount) { - core.info('No more commits were fetched') - core.info('Last attempt will be to fetch full history') - await exec('git', ['fetch']) - if (!(await hasMergeBase())) { - noMergeBase = true + const commitCount = await getCommitCount() + if (commitCount === lastCommitCount) { + core.info('No more commits were fetched') + core.info('Last attempt will be to fetch full history') + await exec('git', ['fetch']) + if (!(await hasMergeBase())) { + noMergeBase = true + } + break } - break + lastCommitCount = commitCount } - depth = Math.min(depth * 2, Number.MAX_SAFE_INTEGER) - lastCommitCount = commitCount } } finally { core.endGroup()