diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 05cfa398..52c25914 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -82,7 +82,6 @@ jobs: - name: Verify Changed files uses: tj-actions/verify-changed-files@v16 id: changed_files - if: github.event_name == 'pull_request' with: files: | src diff --git a/action.yml b/action.yml index 5b377f8f..20baebb2 100644 --- a/action.yml +++ b/action.yml @@ -136,6 +136,10 @@ inputs: description: "Skip the initial fetch to improve performance for shallow repositories. **NOTE**: This could lead to errors with missing history and the intended use is limited to when you've fetched the history necessary to perform the diff." required: false default: "false" + fetch_additional_submodule_history: + description: "Fetch additional history for submodules." + required: false + default: "false" since_last_remote_commit: description: "Use the last commit on the remote branch as the `base_sha`. Defaults to the last non-merge commit on the target branch for pull request events and the previous remote commit of the current branch for push events." required: false diff --git a/dist/index.js b/dist/index.js index dce749cb..074cfce2 100644 Binary files a/dist/index.js and b/dist/index.js differ diff --git a/dist/index.js.map b/dist/index.js.map index 553948dd..425ad4f7 100644 Binary files a/dist/index.js.map and b/dist/index.js.map differ diff --git a/src/changedFiles.ts b/src/changedFiles.ts index 99313e65..33c702b3 100644 --- a/src/changedFiles.ts +++ b/src/changedFiles.ts @@ -65,6 +65,9 @@ export const getRenamedFiles = async ({ diff })) ) { + core.warning( + `Set 'fetch_additional_submodule_history: true' to fetch additional submodule history for: ${submodulePath}, Note you can control the fetch depth using 'fetch_depth' input` + ) diff = '..' } @@ -157,6 +160,9 @@ export const getAllDiffFiles = async ({ diff })) ) { + core.warning( + `Set 'fetch_additional_submodule_history: true' to fetch additional submodule history for: ${submodulePath}, Note you can control the fetch depth using 'fetch_depth' input` + ) diff = '..' } diff --git a/src/commitSha.ts b/src/commitSha.ts index 6605b9d4..eedacd57 100644 --- a/src/commitSha.ts +++ b/src/commitSha.ts @@ -93,56 +93,70 @@ export const getSHAForNonPullRequestEvent = async ( const currentBranch = targetBranch let initialCommit = false - if (isShallow && !inputs.skipInitialFetch) { - core.info('Repository is shallow, fetching more history...') + if (!inputs.skipInitialFetch) { + if (isShallow) { + core.info('Repository is shallow, fetching more history...') - if (isTag) { - let sourceBranch = '' + if (isTag) { + let sourceBranch = '' - if (github.context.payload.base_ref) { - sourceBranch = github.context.payload.base_ref.replace( - 'refs/heads/', - '' - ) - } else if (github.context.payload.release?.target_commitish) { - sourceBranch = github.context.payload.release?.target_commitish + if (github.context.payload.base_ref) { + sourceBranch = github.context.payload.base_ref.replace( + 'refs/heads/', + '' + ) + } else if (github.context.payload.release?.target_commitish) { + sourceBranch = github.context.payload.release?.target_commitish + } + + await gitFetch({ + cwd: workingDirectory, + args: [ + ...gitFetchExtraArgs, + '-u', + '--progress', + `--deepen=${inputs.fetchDepth}`, + 'origin', + `+refs/heads/${sourceBranch}:refs/remotes/origin/${sourceBranch}` + ] + }) + } else { + await gitFetch({ + cwd: workingDirectory, + args: [ + ...gitFetchExtraArgs, + '-u', + '--progress', + `--deepen=${inputs.fetchDepth}`, + 'origin', + `+refs/heads/${targetBranch}:refs/remotes/origin/${targetBranch}` + ] + }) } - await gitFetch({ - cwd: workingDirectory, - args: [ - ...gitFetchExtraArgs, - '-u', - '--progress', - `--deepen=${inputs.fetchDepth}`, - 'origin', - `+refs/heads/${sourceBranch}:refs/remotes/origin/${sourceBranch}` - ] - }) + if (hasSubmodule) { + await gitFetchSubmodules({ + cwd: workingDirectory, + args: [ + ...gitFetchExtraArgs, + '-u', + '--progress', + `--deepen=${inputs.fetchDepth}` + ] + }) + } } else { - await gitFetch({ - cwd: workingDirectory, - args: [ - ...gitFetchExtraArgs, - '-u', - '--progress', - `--deepen=${inputs.fetchDepth}`, - 'origin', - `+refs/heads/${targetBranch}:refs/remotes/origin/${targetBranch}` - ] - }) - } - - if (hasSubmodule) { - await gitFetchSubmodules({ - cwd: workingDirectory, - args: [ - ...gitFetchExtraArgs, - '-u', - '--progress', - `--deepen=${inputs.fetchDepth}` - ] - }) + if (hasSubmodule && inputs.fetchSubmoduleHistory) { + await gitFetchSubmodules({ + cwd: workingDirectory, + args: [ + ...gitFetchExtraArgs, + '-u', + '--progress', + `--deepen=${inputs.fetchDepth}` + ] + }) + } } } @@ -288,55 +302,68 @@ export const getSHAForPullRequestEvent = async ( targetBranch = currentBranch } - if (isShallow && !inputs.skipInitialFetch) { + if (!inputs.skipInitialFetch) { core.info('Repository is shallow, fetching more history...') - - let prFetchExitCode = await gitFetch({ - cwd: workingDirectory, - args: [ - ...gitFetchExtraArgs, - '-u', - '--progress', - 'origin', - `pull/${github.context.payload.pull_request?.number}/head:${currentBranch}` - ] - }) - - if (prFetchExitCode !== 0) { - prFetchExitCode = await gitFetch({ + if (isShallow) { + let prFetchExitCode = await gitFetch({ cwd: workingDirectory, args: [ ...gitFetchExtraArgs, '-u', '--progress', - `--deepen=${inputs.fetchDepth}`, 'origin', - `+refs/heads/${currentBranch}*:refs/remotes/origin/${currentBranch}*` - ] - }) - } - - if (prFetchExitCode !== 0) { - throw new Error( - 'Failed to fetch pull request branch. Please ensure "persist-credentials" is set to "true" when checking out the repository. See: https://github.com/actions/checkout#usage' - ) - } - - if (!inputs.sinceLastRemoteCommit) { - core.debug('Fetching target branch...') - await gitFetch({ - cwd: workingDirectory, - args: [ - ...gitFetchExtraArgs, - '-u', - '--progress', - `--deepen=${inputs.fetchDepth}`, - 'origin', - `+refs/heads/${targetBranch}:refs/remotes/origin/${targetBranch}` + `pull/${github.context.payload.pull_request?.number}/head:${currentBranch}` ] }) - if (hasSubmodule) { + if (prFetchExitCode !== 0) { + prFetchExitCode = await gitFetch({ + cwd: workingDirectory, + args: [ + ...gitFetchExtraArgs, + '-u', + '--progress', + `--deepen=${inputs.fetchDepth}`, + 'origin', + `+refs/heads/${currentBranch}*:refs/remotes/origin/${currentBranch}*` + ] + }) + } + + if (prFetchExitCode !== 0) { + throw new Error( + 'Failed to fetch pull request branch. Please ensure "persist-credentials" is set to "true" when checking out the repository. See: https://github.com/actions/checkout#usage' + ) + } + + if (!inputs.sinceLastRemoteCommit) { + core.debug('Fetching target branch...') + await gitFetch({ + cwd: workingDirectory, + args: [ + ...gitFetchExtraArgs, + '-u', + '--progress', + `--deepen=${inputs.fetchDepth}`, + 'origin', + `+refs/heads/${targetBranch}:refs/remotes/origin/${targetBranch}` + ] + }) + + if (hasSubmodule) { + await gitFetchSubmodules({ + cwd: workingDirectory, + args: [ + ...gitFetchExtraArgs, + '-u', + '--progress', + `--deepen=${inputs.fetchDepth}` + ] + }) + } + } + } else { + if (hasSubmodule && inputs.fetchSubmoduleHistory) { await gitFetchSubmodules({ cwd: workingDirectory, args: [ diff --git a/src/inputs.ts b/src/inputs.ts index 2b88afc3..a92041db 100644 --- a/src/inputs.ts +++ b/src/inputs.ts @@ -34,6 +34,7 @@ export type Inputs = { json: boolean escapeJson: boolean fetchDepth?: number + fetchSubmoduleHistory?: boolean sinceLastRemoteCommit: boolean writeOutputFiles: boolean outputDir: string @@ -189,6 +190,12 @@ export const getInputs = (): Inputs => { const skipInitialFetch = core.getBooleanInput('skip_initial_fetch', { required: false }) + const fetchSubmoduleHistory = core.getBooleanInput( + 'fetch_additional_submodule_history', + { + required: false + } + ) const inputs: Inputs = { files, @@ -225,6 +232,7 @@ export const getInputs = (): Inputs => { oldNewSeparator, oldNewFilesSeparator, skipInitialFetch, + fetchSubmoduleHistory, // End Not Supported via REST API dirNames, dirNamesExcludeCurrentDir, diff --git a/src/main.ts b/src/main.ts index 43fcd1e0..2f8024a6 100644 --- a/src/main.ts +++ b/src/main.ts @@ -317,7 +317,8 @@ export async function run(): Promise { 'recoverFiles', 'recoverFilesIgnore', 'includeAllOldNewRenamedFiles', - 'skipInitialFetch' + 'skipInitialFetch', + 'fetchSubmoduleHistory' ] for (const input of unsupportedInputs) {