From 138368ff4f3d92d53cbfa56919e920c2d9711b60 Mon Sep 17 00:00:00 2001 From: Michal Dorner Date: Tue, 9 Mar 2021 21:56:18 +0100 Subject: [PATCH] Use ref instead of HEAD --- dist/index.js | 18 +++++++++--------- src/git.ts | 14 +++++++------- src/main.ts | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/dist/index.js b/dist/index.js index 44274fb..e79004f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -3865,21 +3865,21 @@ async function getChangesOnHead() { return parseGitDiffOutput(output); } exports.getChangesOnHead = getChangesOnHead; -async function getChangesSinceMergeBase(base, initialFetchDepth) { +async function getChangesSinceMergeBase(base, ref, initialFetchDepth) { const baseRef = `remotes/origin/${base}`; async function hasMergeBase() { - return (await exec_1.default('git', ['merge-base', baseRef, exports.HEAD], { ignoreReturnCode: true })).code === 0; + return (await exec_1.default('git', ['merge-base', baseRef, ref], { ignoreReturnCode: true })).code === 0; } let noMergeBase = false; - core.startGroup(`Searching for merge-base ${baseRef}...${exports.HEAD}`); + core.startGroup(`Searching for merge-base ${baseRef}...${ref}`); try { if (!(await hasMergeBase())) { - await exec_1.default('git', ['fetch', `--depth=${initialFetchDepth}`, 'origin', base, exports.HEAD]); + await exec_1.default('git', ['fetch', `--depth=${initialFetchDepth}`, 'origin', base, ref]); 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]); + await exec_1.default('git', ['fetch', `--deepen=${depth}`, 'origin', base, ref]); const commitCount = await getCommitCount(); if (commitCount === lastCommitCount) { core.info('No more commits were fetched'); @@ -3902,11 +3902,11 @@ async function getChangesSinceMergeBase(base, initialFetchDepth) { return await listAllFilesAsAdded(); } // Get changes introduced on HEAD compared to ref - core.startGroup(`Change detection ${baseRef}...${exports.HEAD}`); + core.startGroup(`Change detection ${baseRef}...${ref}`); let output = ''; try { // Three dots '...' change detection - finds merge-base and compares against it - output = (await exec_1.default('git', ['diff', '--no-renames', '--name-status', '-z', `${baseRef}...${exports.HEAD}`])).stdout; + output = (await exec_1.default('git', ['diff', '--no-renames', '--name-status', '-z', `${baseRef}...${ref}`])).stdout; } finally { fixStdOutNullTermination(); @@ -4732,7 +4732,7 @@ async function getChangedFilesFromGit(base, initialFetchDepth) { if (baseSha === git.NULL_SHA) { if (defaultRef && baseRef !== defaultRef) { core.info(`First push of a branch detected - changes will be detected against the default branch ${defaultRef}`); - return await git.getChangesSinceMergeBase(defaultRef, initialFetchDepth); + return await git.getChangesSinceMergeBase(defaultRef, ref, initialFetchDepth); } else { core.info('Initial push detected - all files will be listed as added'); @@ -4744,7 +4744,7 @@ async function getChangedFilesFromGit(base, initialFetchDepth) { } // Changes introduced by current branch against the base branch core.info(`Changes will be detected against the branch ${baseRef}`); - return await git.getChangesSinceMergeBase(baseRef, initialFetchDepth); + return await git.getChangesSinceMergeBase(baseRef, ref, initialFetchDepth); } // Uses github REST api to get list of files changed in PR async function getChangedFilesFromApi(token, pullRequest) { diff --git a/src/git.ts b/src/git.ts index d81b1f5..531c301 100644 --- a/src/git.ts +++ b/src/git.ts @@ -54,23 +54,23 @@ export async function getChangesOnHead(): Promise { return parseGitDiffOutput(output) } -export async function getChangesSinceMergeBase(base: string, initialFetchDepth: number): Promise { +export async function getChangesSinceMergeBase(base: string, ref: string, initialFetchDepth: number): Promise { const baseRef = `remotes/origin/${base}` async function hasMergeBase(): Promise { - return (await exec('git', ['merge-base', baseRef, HEAD], {ignoreReturnCode: true})).code === 0 + return (await exec('git', ['merge-base', baseRef, ref], {ignoreReturnCode: true})).code === 0 } let noMergeBase = false - core.startGroup(`Searching for merge-base ${baseRef}...${HEAD}`) + core.startGroup(`Searching for merge-base ${baseRef}...${ref}`) try { if (!(await hasMergeBase())) { - await exec('git', ['fetch', `--depth=${initialFetchDepth}`, 'origin', base, HEAD]) + await exec('git', ['fetch', `--depth=${initialFetchDepth}`, 'origin', base, ref]) 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]) + await exec('git', ['fetch', `--deepen=${depth}`, 'origin', base, ref]) const commitCount = await getCommitCount() if (commitCount === lastCommitCount) { core.info('No more commits were fetched') @@ -94,11 +94,11 @@ export async function getChangesSinceMergeBase(base: string, initialFetchDepth: } // Get changes introduced on HEAD compared to ref - core.startGroup(`Change detection ${baseRef}...${HEAD}`) + core.startGroup(`Change detection ${baseRef}...${ref}`) let output = '' try { // Three dots '...' change detection - finds merge-base and compares against it - output = (await exec('git', ['diff', '--no-renames', '--name-status', '-z', `${baseRef}...${HEAD}`])).stdout + output = (await exec('git', ['diff', '--no-renames', '--name-status', '-z', `${baseRef}...${ref}`])).stdout } finally { fixStdOutNullTermination() core.endGroup() diff --git a/src/main.ts b/src/main.ts index d907796..e3d6ebc 100644 --- a/src/main.ts +++ b/src/main.ts @@ -109,7 +109,7 @@ async function getChangedFilesFromGit(base: string, initialFetchDepth: number): if (baseSha === git.NULL_SHA) { if (defaultRef && baseRef !== defaultRef) { core.info(`First push of a branch detected - changes will be detected against the default branch ${defaultRef}`) - return await git.getChangesSinceMergeBase(defaultRef, initialFetchDepth) + return await git.getChangesSinceMergeBase(defaultRef, ref, initialFetchDepth) } else { core.info('Initial push detected - all files will be listed as added') return await git.listAllFilesAsAdded() @@ -122,7 +122,7 @@ async function getChangedFilesFromGit(base: string, initialFetchDepth: number): // Changes introduced by current branch against the base branch core.info(`Changes will be detected against the branch ${baseRef}`) - return await git.getChangesSinceMergeBase(baseRef, initialFetchDepth) + return await git.getChangesSinceMergeBase(baseRef, ref, initialFetchDepth) } // Uses github REST api to get list of files changed in PR