Refactor getChangesSinceMergeBase() code

This commit is contained in:
Michal Dorner 2021-03-09 21:44:15 +01:00
parent 0c0d1a854a
commit a301a0ad83
No known key found for this signature in database
GPG key ID: 9EEE04B48DA36786
2 changed files with 32 additions and 39 deletions

View file

@ -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()