fix: error detecting initial commits (#1181)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
@@ -63,6 +63,7 @@ export interface DiffResult {
|
||||
currentBranch: string
|
||||
targetBranch: string
|
||||
diff: string
|
||||
initialCommit?: boolean
|
||||
}
|
||||
|
||||
export const getSHAForPushEvent = async (
|
||||
@@ -201,20 +202,15 @@ export const getSHAForPushEvent = async (
|
||||
})
|
||||
}
|
||||
|
||||
if (previousSha === currentSha) {
|
||||
if (!(await getParentSha({cwd: workingDirectory}))) {
|
||||
if (!previousSha || previousSha === currentSha) {
|
||||
previousSha = await getParentSha({
|
||||
cwd: workingDirectory
|
||||
})
|
||||
|
||||
if (!previousSha) {
|
||||
core.warning('Initial commit detected no previous commit found.')
|
||||
initialCommit = true
|
||||
previousSha = currentSha
|
||||
} else {
|
||||
previousSha = await getParentSha({
|
||||
cwd: workingDirectory
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if (!previousSha) {
|
||||
core.error('Unable to locate a previous commit.')
|
||||
throw new Error('Unable to locate a previous commit.')
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -241,7 +237,8 @@ export const getSHAForPushEvent = async (
|
||||
currentSha,
|
||||
currentBranch,
|
||||
targetBranch,
|
||||
diff
|
||||
diff,
|
||||
initialCommit
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,12 @@ export async function run(): Promise<void> {
|
||||
)
|
||||
}
|
||||
|
||||
if (diffResult.initialCommit) {
|
||||
core.info('This is the first commit for this repository; exiting...')
|
||||
core.endGroup()
|
||||
return
|
||||
}
|
||||
|
||||
core.info(
|
||||
`Retrieving changes between ${diffResult.previousSha} (${diffResult.targetBranch}) → ${diffResult.currentSha} (${diffResult.currentBranch})`
|
||||
)
|
||||
|
||||
@@ -526,15 +526,20 @@ export const gitLsRemote = async ({
|
||||
}
|
||||
|
||||
export const getParentSha = async ({cwd}: {cwd: string}): Promise<string> => {
|
||||
const {stdout} = await exec.getExecOutput(
|
||||
const {stdout, exitCode} = await exec.getExecOutput(
|
||||
'git',
|
||||
['rev-list', '-n', '1', 'HEAD^'],
|
||||
{
|
||||
cwd,
|
||||
ignoreReturnCode: true,
|
||||
silent: process.env.RUNNER_DEBUG !== '1'
|
||||
}
|
||||
)
|
||||
|
||||
if (exitCode !== 0) {
|
||||
return ''
|
||||
}
|
||||
|
||||
return stdout.trim()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user