mirror of
https://github.com/tj-actions/changed-files
synced 2025-01-30 09:35:15 +00:00
feat: add support for outputting renamed files as deleted and added (#1260)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
c648759d89
commit
90ef0b1b22
9 changed files with 75 additions and 16 deletions
35
.github/workflows/test.yml
vendored
35
.github/workflows/test.yml
vendored
|
@ -869,6 +869,41 @@ jobs:
|
||||||
exit 1
|
exit 1
|
||||||
shell:
|
shell:
|
||||||
bash
|
bash
|
||||||
|
- name: Run changed-files for old new filenames test rename 2 output as deleted and added
|
||||||
|
id: changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added
|
||||||
|
uses: ./
|
||||||
|
with:
|
||||||
|
base_sha: 4d04215
|
||||||
|
sha: fe238e6
|
||||||
|
fetch_depth: 60000
|
||||||
|
include_all_old_new_renamed_files: true
|
||||||
|
output_renamed_files_as_deleted_and_added: true
|
||||||
|
- name: Show output
|
||||||
|
run: |
|
||||||
|
echo '${{ toJSON(steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs) }}'
|
||||||
|
shell:
|
||||||
|
bash
|
||||||
|
- name: Check all_old_new_renamed_files output
|
||||||
|
if: "!contains(steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.all_old_new_renamed_files, 'test/test rename 2.txt,test/test rename-2.txt')"
|
||||||
|
run: |
|
||||||
|
echo "Invalid output: Expected to include (test/test rename 2.txt test/test rename-2.txt) got (${{ steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.all_old_new_renamed_files }})"
|
||||||
|
exit 1
|
||||||
|
shell:
|
||||||
|
bash
|
||||||
|
- name: Check deleted_files output
|
||||||
|
if: "!contains(steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.deleted_files, 'test/test rename 2.txt')"
|
||||||
|
run: |
|
||||||
|
echo "Invalid output: Expected to include (test/test rename 2.txt) got (${{ steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.deleted_files }})"
|
||||||
|
exit 1
|
||||||
|
shell:
|
||||||
|
bash
|
||||||
|
- name: Check added_files output
|
||||||
|
if: "!contains(steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.added_files, 'test/test rename-2.txt')"
|
||||||
|
run: |
|
||||||
|
echo "Invalid output: Expected to include (test/test rename-2.txt) got (${{ steps.changed-files-all-old-new-renamed-files-2-output-as-deleted-and-added.outputs.added_files }})"
|
||||||
|
exit 1
|
||||||
|
shell:
|
||||||
|
bash
|
||||||
- name: Run changed-files with specific files
|
- name: Run changed-files with specific files
|
||||||
id: changed-files-specific
|
id: changed-files-specific
|
||||||
uses: ./
|
uses: ./
|
||||||
|
|
|
@ -117,6 +117,10 @@ inputs:
|
||||||
description: "Directory to store output files."
|
description: "Directory to store output files."
|
||||||
required: false
|
required: false
|
||||||
default: ".github/outputs"
|
default: ".github/outputs"
|
||||||
|
output_renamed_files_as_deleted_and_added:
|
||||||
|
description: "Output renamed files as deleted and added files."
|
||||||
|
required: false
|
||||||
|
default: "false"
|
||||||
|
|
||||||
outputs:
|
outputs:
|
||||||
added_files:
|
added_files:
|
||||||
|
|
BIN
dist/index.js
generated
vendored
BIN
dist/index.js
generated
vendored
Binary file not shown.
BIN
dist/index.js.map
generated
vendored
BIN
dist/index.js.map
generated
vendored
Binary file not shown.
|
@ -88,18 +88,21 @@ export const getAllDiffFiles = async ({
|
||||||
workingDirectory,
|
workingDirectory,
|
||||||
hasSubmodule,
|
hasSubmodule,
|
||||||
diffResult,
|
diffResult,
|
||||||
submodulePaths
|
submodulePaths,
|
||||||
|
outputRenamedFilesAsDeletedAndAdded
|
||||||
}: {
|
}: {
|
||||||
workingDirectory: string
|
workingDirectory: string
|
||||||
hasSubmodule: boolean
|
hasSubmodule: boolean
|
||||||
diffResult: DiffResult
|
diffResult: DiffResult
|
||||||
submodulePaths: string[]
|
submodulePaths: string[]
|
||||||
|
outputRenamedFilesAsDeletedAndAdded: boolean
|
||||||
}): Promise<ChangedFiles> => {
|
}): Promise<ChangedFiles> => {
|
||||||
const files = await getAllChangedFiles({
|
const files = await getAllChangedFiles({
|
||||||
cwd: workingDirectory,
|
cwd: workingDirectory,
|
||||||
sha1: diffResult.previousSha,
|
sha1: diffResult.previousSha,
|
||||||
sha2: diffResult.currentSha,
|
sha2: diffResult.currentSha,
|
||||||
diff: diffResult.diff
|
diff: diffResult.diff,
|
||||||
|
outputRenamedFilesAsDeletedAndAdded
|
||||||
})
|
})
|
||||||
|
|
||||||
if (hasSubmodule) {
|
if (hasSubmodule) {
|
||||||
|
@ -124,7 +127,8 @@ export const getAllDiffFiles = async ({
|
||||||
sha2: submoduleShaResult.currentSha,
|
sha2: submoduleShaResult.currentSha,
|
||||||
diff: diffResult.diff,
|
diff: diffResult.diff,
|
||||||
isSubmodule: true,
|
isSubmodule: true,
|
||||||
parentDir: submodulePath
|
parentDir: submodulePath,
|
||||||
|
outputRenamedFilesAsDeletedAndAdded
|
||||||
})
|
})
|
||||||
|
|
||||||
for (const changeType of Object.keys(
|
for (const changeType of Object.keys(
|
||||||
|
|
|
@ -370,12 +370,7 @@ export const getSHAForPullRequestEvent = async (
|
||||||
|
|
||||||
if (!previousSha) {
|
if (!previousSha) {
|
||||||
if (inputs.sinceLastRemoteCommit) {
|
if (inputs.sinceLastRemoteCommit) {
|
||||||
previousSha =
|
previousSha = env.GITHUB_EVENT_BEFORE
|
||||||
env.GITHUB_EVENT_BEFORE ||
|
|
||||||
(await getRemoteBranchHeadSha({
|
|
||||||
cwd: workingDirectory,
|
|
||||||
branch: currentBranch
|
|
||||||
}))
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!previousSha ||
|
!previousSha ||
|
||||||
|
|
|
@ -30,6 +30,7 @@ export type Inputs = {
|
||||||
sinceLastRemoteCommit: boolean
|
sinceLastRemoteCommit: boolean
|
||||||
writeOutputFiles: boolean
|
writeOutputFiles: boolean
|
||||||
outputDir: string
|
outputDir: string
|
||||||
|
outputRenamedFilesAsDeletedAndAdded: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getInputs = (): Inputs => {
|
export const getInputs = (): Inputs => {
|
||||||
|
@ -111,6 +112,10 @@ export const getInputs = (): Inputs => {
|
||||||
required: false
|
required: false
|
||||||
})
|
})
|
||||||
const outputDir = core.getInput('output_dir', {required: false})
|
const outputDir = core.getInput('output_dir', {required: false})
|
||||||
|
const outputRenamedFilesAsDeletedAndAdded = core.getBooleanInput(
|
||||||
|
'output_renamed_files_as_deleted_and_added',
|
||||||
|
{required: false}
|
||||||
|
)
|
||||||
|
|
||||||
const inputs: Inputs = {
|
const inputs: Inputs = {
|
||||||
files,
|
files,
|
||||||
|
@ -139,7 +144,8 @@ export const getInputs = (): Inputs => {
|
||||||
escapeJson,
|
escapeJson,
|
||||||
sinceLastRemoteCommit,
|
sinceLastRemoteCommit,
|
||||||
writeOutputFiles,
|
writeOutputFiles,
|
||||||
outputDir
|
outputDir,
|
||||||
|
outputRenamedFilesAsDeletedAndAdded
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fetchDepth) {
|
if (fetchDepth) {
|
||||||
|
|
11
src/main.ts
11
src/main.ts
|
@ -61,6 +61,8 @@ export async function run(): Promise<void> {
|
||||||
const hasSubmodule = await submoduleExists({cwd: workingDirectory})
|
const hasSubmodule = await submoduleExists({cwd: workingDirectory})
|
||||||
let gitFetchExtraArgs = ['--no-tags', '--prune', '--recurse-submodules']
|
let gitFetchExtraArgs = ['--no-tags', '--prune', '--recurse-submodules']
|
||||||
const isTag = env.GITHUB_REF?.startsWith('refs/tags/')
|
const isTag = env.GITHUB_REF?.startsWith('refs/tags/')
|
||||||
|
const outputRenamedFilesAsDeletedAndAdded =
|
||||||
|
inputs.outputRenamedFilesAsDeletedAndAdded
|
||||||
let submodulePaths: string[] = []
|
let submodulePaths: string[] = []
|
||||||
|
|
||||||
if (hasSubmodule) {
|
if (hasSubmodule) {
|
||||||
|
@ -118,7 +120,8 @@ export async function run(): Promise<void> {
|
||||||
workingDirectory,
|
workingDirectory,
|
||||||
hasSubmodule,
|
hasSubmodule,
|
||||||
diffResult,
|
diffResult,
|
||||||
submodulePaths
|
submodulePaths,
|
||||||
|
outputRenamedFilesAsDeletedAndAdded
|
||||||
})
|
})
|
||||||
core.debug(`All diff files: ${JSON.stringify(allDiffFiles)}`)
|
core.debug(`All diff files: ${JSON.stringify(allDiffFiles)}`)
|
||||||
|
|
||||||
|
@ -261,7 +264,8 @@ export async function run(): Promise<void> {
|
||||||
const otherChangedFiles = allOtherChangedFiles
|
const otherChangedFiles = allOtherChangedFiles
|
||||||
.split(inputs.separator)
|
.split(inputs.separator)
|
||||||
.filter(
|
.filter(
|
||||||
filePath => !allChangedFiles.split(inputs.separator).includes(filePath)
|
(filePath: string) =>
|
||||||
|
!allChangedFiles.split(inputs.separator).includes(filePath)
|
||||||
)
|
)
|
||||||
|
|
||||||
const onlyChanged =
|
const onlyChanged =
|
||||||
|
@ -320,7 +324,8 @@ export async function run(): Promise<void> {
|
||||||
const otherModifiedFiles = allOtherModifiedFiles
|
const otherModifiedFiles = allOtherModifiedFiles
|
||||||
.split(inputs.separator)
|
.split(inputs.separator)
|
||||||
.filter(
|
.filter(
|
||||||
filePath => !allModifiedFiles.split(inputs.separator).includes(filePath)
|
(filePath: string) =>
|
||||||
|
!allModifiedFiles.split(inputs.separator).includes(filePath)
|
||||||
)
|
)
|
||||||
|
|
||||||
const onlyModified =
|
const onlyModified =
|
||||||
|
|
16
src/utils.ts
16
src/utils.ts
|
@ -410,7 +410,8 @@ export const getAllChangedFiles = async ({
|
||||||
sha2,
|
sha2,
|
||||||
diff,
|
diff,
|
||||||
isSubmodule = false,
|
isSubmodule = false,
|
||||||
parentDir = ''
|
parentDir = '',
|
||||||
|
outputRenamedFilesAsDeletedAndAdded = false
|
||||||
}: {
|
}: {
|
||||||
cwd: string
|
cwd: string
|
||||||
sha1: string
|
sha1: string
|
||||||
|
@ -418,6 +419,7 @@ export const getAllChangedFiles = async ({
|
||||||
diff: string
|
diff: string
|
||||||
isSubmodule?: boolean
|
isSubmodule?: boolean
|
||||||
parentDir?: string
|
parentDir?: string
|
||||||
|
outputRenamedFilesAsDeletedAndAdded?: boolean
|
||||||
}): Promise<ChangedFiles> => {
|
}): Promise<ChangedFiles> => {
|
||||||
const {exitCode, stdout, stderr} = await exec.getExecOutput(
|
const {exitCode, stdout, stderr} = await exec.getExecOutput(
|
||||||
'git',
|
'git',
|
||||||
|
@ -466,13 +468,21 @@ export const getAllChangedFiles = async ({
|
||||||
const lines = stdout.split('\n').filter(Boolean)
|
const lines = stdout.split('\n').filter(Boolean)
|
||||||
|
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
const [changeType, filePath] = line.split('\t')
|
const [changeType, filePath, newPath = ''] = line.split('\t')
|
||||||
const normalizedFilePath = isSubmodule
|
const normalizedFilePath = isSubmodule
|
||||||
? normalizePath(path.join(parentDir, filePath))
|
? normalizePath(path.join(parentDir, filePath))
|
||||||
: normalizePath(filePath)
|
: normalizePath(filePath)
|
||||||
|
const normalizedNewPath = isSubmodule
|
||||||
|
? normalizePath(path.join(parentDir, newPath))
|
||||||
|
: normalizePath(newPath)
|
||||||
|
|
||||||
if (changeType.startsWith('R')) {
|
if (changeType.startsWith('R')) {
|
||||||
changedFiles[ChangeTypeEnum.Renamed].push(normalizedFilePath)
|
if (outputRenamedFilesAsDeletedAndAdded) {
|
||||||
|
changedFiles[ChangeTypeEnum.Deleted].push(normalizedFilePath)
|
||||||
|
changedFiles[ChangeTypeEnum.Added].push(normalizedNewPath)
|
||||||
|
} else {
|
||||||
|
changedFiles[ChangeTypeEnum.Renamed].push(normalizedFilePath)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
changedFiles[changeType as ChangeTypeEnum].push(normalizedFilePath)
|
changedFiles[changeType as ChangeTypeEnum].push(normalizedFilePath)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue