Skip to content

Commit

Permalink
fix: error detecting initial commits (#1181)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
jackton1 and actions-user authored May 26, 2023
1 parent 88fb02b commit 9ad1a5b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
32 changes: 17 additions & 15 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

21 changes: 9 additions & 12 deletions src/commitSha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export interface DiffResult {
currentBranch: string
targetBranch: string
diff: string
initialCommit?: boolean
}

export const getSHAForPushEvent = async (
Expand Down Expand Up @@ -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.')
}
}
}
Expand All @@ -241,7 +237,8 @@ export const getSHAForPushEvent = async (
currentSha,
currentBranch,
targetBranch,
diff
diff,
initialCommit
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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})`
)
Expand Down
7 changes: 6 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit 9ad1a5b

Please sign in to comment.