Skip to content

Commit

Permalink
feat: improve checking local branch history (#1436)
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 committed Aug 5, 2023
1 parent 1e9cd5f commit d4e6e22
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 11 deletions.
40 changes: 35 additions & 5 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.

58 changes: 53 additions & 5 deletions src/utils.ts
Expand Up @@ -786,9 +786,58 @@ export const canDiffCommits = async ({
sha2: string
diff: string
}): Promise<boolean> => {
const {exitCode, stderr} = await exec.getExecOutput(
if (diff === '...') {
const mergeBase = await getMergeBase(cwd, sha1, sha2)

if (!mergeBase) {
core.warning(`Unable to find merge base between ${sha1} and ${sha2}`)
return false
}

const {exitCode, stderr} = await exec.getExecOutput(
'git',
['log', '--format=%H', `${mergeBase}..${sha2}`],
{
cwd,
ignoreReturnCode: true,
silent: !core.isDebug()
}
)

if (exitCode !== 0) {
core.warning(stderr || `Error checking commit history`)
return false
}

return true
} else {
const {exitCode, stderr} = await exec.getExecOutput(
'git',
['diff', '--quiet', sha1, sha2],
{
cwd,
ignoreReturnCode: true,
silent: !core.isDebug()
}
)

if (exitCode !== 0) {
core.warning(stderr || `Error checking commit history`)
return false
}

return true
}
}

const getMergeBase = async (
cwd: string,
sha1: string,
sha2: string
): Promise<string | null> => {
const {exitCode, stdout} = await exec.getExecOutput(
'git',
['diff', '--name-only', '--ignore-submodules=all', `${sha1}${diff}${sha2}`],
['merge-base', sha1, sha2],
{
cwd,
ignoreReturnCode: true,
Expand All @@ -797,11 +846,10 @@ export const canDiffCommits = async ({
)

if (exitCode !== 0) {
core.warning(stderr || `Unable find merge base between ${sha1} and ${sha2}`)
return false
return null
}

return true
return stdout.trim()
}

export const getDirnameMaxDepth = ({
Expand Down

0 comments on commit d4e6e22

Please sign in to comment.