Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error detecting initial commits #1181

Merged
merged 4 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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
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
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