3
0
Fork 0
mirror of https://github.com/tj-actions/changed-files synced 2025-02-06 09:41:22 +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:
Tonye Jack 2023-06-14 13:59:31 -06:00 committed by GitHub
parent c648759d89
commit 90ef0b1b22
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 102 additions and 33 deletions

View file

@ -869,6 +869,41 @@ jobs:
exit 1
shell:
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
id: changed-files-specific
uses: ./

View file

@ -117,6 +117,10 @@ inputs:
description: "Directory to store output files."
required: false
default: ".github/outputs"
output_renamed_files_as_deleted_and_added:
description: "Output renamed files as deleted and added files."
required: false
default: "false"
outputs:
added_files:

40
dist/index.js generated vendored
View file

@ -95,12 +95,13 @@ var ChangeTypeEnum;
ChangeTypeEnum["Unmerged"] = "U";
ChangeTypeEnum["Unknown"] = "X";
})(ChangeTypeEnum || (exports.ChangeTypeEnum = ChangeTypeEnum = {}));
const getAllDiffFiles = ({ workingDirectory, hasSubmodule, diffResult, submodulePaths }) => __awaiter(void 0, void 0, void 0, function* () {
const getAllDiffFiles = ({ workingDirectory, hasSubmodule, diffResult, submodulePaths, outputRenamedFilesAsDeletedAndAdded }) => __awaiter(void 0, void 0, void 0, function* () {
const files = yield (0, utils_1.getAllChangedFiles)({
cwd: workingDirectory,
sha1: diffResult.previousSha,
sha2: diffResult.currentSha,
diff: diffResult.diff
diff: diffResult.diff,
outputRenamedFilesAsDeletedAndAdded
});
if (hasSubmodule) {
for (const submodulePath of submodulePaths) {
@ -119,7 +120,8 @@ const getAllDiffFiles = ({ workingDirectory, hasSubmodule, diffResult, submodule
sha2: submoduleShaResult.currentSha,
diff: diffResult.diff,
isSubmodule: true,
parentDir: submodulePath
parentDir: submodulePath,
outputRenamedFilesAsDeletedAndAdded
});
for (const changeType of Object.keys(submoduleFiles)) {
if (!files[changeType]) {
@ -500,12 +502,7 @@ const getSHAForPullRequestEvent = (inputs, env, workingDirectory, isShallow, has
}
if (!previousSha) {
if (inputs.sinceLastRemoteCommit) {
previousSha =
env.GITHUB_EVENT_BEFORE ||
(yield (0, utils_1.getRemoteBranchHeadSha)({
cwd: workingDirectory,
branch: currentBranch
}));
previousSha = env.GITHUB_EVENT_BEFORE;
if (!previousSha ||
(previousSha &&
(yield (0, utils_1.verifyCommitSha)({ sha: previousSha, cwd: workingDirectory })) !==
@ -776,6 +773,7 @@ const getInputs = () => {
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 = {
files,
filesSeparator,
@ -803,7 +801,8 @@ const getInputs = () => {
escapeJson,
sinceLastRemoteCommit,
writeOutputFiles,
outputDir
outputDir,
outputRenamedFilesAsDeletedAndAdded
};
if (fetchDepth) {
inputs.fetchDepth = Math.max(parseInt(fetchDepth, 10), 2);
@ -895,6 +894,7 @@ function run() {
const hasSubmodule = yield (0, utils_1.submoduleExists)({ cwd: workingDirectory });
let gitFetchExtraArgs = ['--no-tags', '--prune', '--recurse-submodules'];
const isTag = (_a = env.GITHUB_REF) === null || _a === void 0 ? void 0 : _a.startsWith('refs/tags/');
const outputRenamedFilesAsDeletedAndAdded = inputs.outputRenamedFilesAsDeletedAndAdded;
let submodulePaths = [];
if (hasSubmodule) {
submodulePaths = yield (0, utils_1.getSubmodulePath)({ cwd: workingDirectory });
@ -926,7 +926,8 @@ function run() {
workingDirectory,
hasSubmodule,
diffResult,
submodulePaths
submodulePaths,
outputRenamedFilesAsDeletedAndAdded
});
core.debug(`All diff files: ${JSON.stringify(allDiffFiles)}`);
const allFilteredDiffFiles = yield (0, utils_1.getFilteredChangedFiles)({
@ -1055,7 +1056,7 @@ function run() {
core.debug(`All other changed files: ${allOtherChangedFiles}`);
const otherChangedFiles = allOtherChangedFiles
.split(inputs.separator)
.filter(filePath => !allChangedFiles.split(inputs.separator).includes(filePath));
.filter((filePath) => !allChangedFiles.split(inputs.separator).includes(filePath));
const onlyChanged = otherChangedFiles.length === 0 &&
allChangedFiles.length > 0 &&
filePatterns.length > 0;
@ -1104,7 +1105,7 @@ function run() {
});
const otherModifiedFiles = allOtherModifiedFiles
.split(inputs.separator)
.filter(filePath => !allModifiedFiles.split(inputs.separator).includes(filePath));
.filter((filePath) => !allModifiedFiles.split(inputs.separator).includes(filePath));
const onlyModified = otherModifiedFiles.length === 0 &&
allModifiedFiles.length > 0 &&
filePatterns.length > 0;
@ -1530,7 +1531,7 @@ const gitRenamedFiles = ({ cwd, sha1, sha2, diff, oldNewSeparator, isSubmodule =
});
});
exports.gitRenamedFiles = gitRenamedFiles;
const getAllChangedFiles = ({ cwd, sha1, sha2, diff, isSubmodule = false, parentDir = '' }) => __awaiter(void 0, void 0, void 0, function* () {
const getAllChangedFiles = ({ cwd, sha1, sha2, diff, isSubmodule = false, parentDir = '', outputRenamedFilesAsDeletedAndAdded = false }) => __awaiter(void 0, void 0, void 0, function* () {
const { exitCode, stdout, stderr } = yield exec.getExecOutput('git', [
'diff',
'--name-status',
@ -1565,13 +1566,22 @@ const getAllChangedFiles = ({ cwd, sha1, sha2, diff, isSubmodule = false, parent
}
const lines = stdout.split('\n').filter(Boolean);
for (const line of lines) {
const [changeType, filePath] = line.split('\t');
const [changeType, filePath, newPath = ''] = line.split('\t');
const normalizedFilePath = isSubmodule
? normalizePath(path.join(parentDir, filePath))
: normalizePath(filePath);
const normalizedNewPath = isSubmodule
? normalizePath(path.join(parentDir, newPath))
: normalizePath(newPath);
if (changeType.startsWith('R')) {
if (outputRenamedFilesAsDeletedAndAdded) {
changedFiles[changedFiles_1.ChangeTypeEnum.Deleted].push(normalizedFilePath);
changedFiles[changedFiles_1.ChangeTypeEnum.Added].push(normalizedNewPath);
}
else {
changedFiles[changedFiles_1.ChangeTypeEnum.Renamed].push(normalizedFilePath);
}
}
else {
changedFiles[changeType].push(normalizedFilePath);
}

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View file

@ -88,18 +88,21 @@ export const getAllDiffFiles = async ({
workingDirectory,
hasSubmodule,
diffResult,
submodulePaths
submodulePaths,
outputRenamedFilesAsDeletedAndAdded
}: {
workingDirectory: string
hasSubmodule: boolean
diffResult: DiffResult
submodulePaths: string[]
outputRenamedFilesAsDeletedAndAdded: boolean
}): Promise<ChangedFiles> => {
const files = await getAllChangedFiles({
cwd: workingDirectory,
sha1: diffResult.previousSha,
sha2: diffResult.currentSha,
diff: diffResult.diff
diff: diffResult.diff,
outputRenamedFilesAsDeletedAndAdded
})
if (hasSubmodule) {
@ -124,7 +127,8 @@ export const getAllDiffFiles = async ({
sha2: submoduleShaResult.currentSha,
diff: diffResult.diff,
isSubmodule: true,
parentDir: submodulePath
parentDir: submodulePath,
outputRenamedFilesAsDeletedAndAdded
})
for (const changeType of Object.keys(

View file

@ -370,12 +370,7 @@ export const getSHAForPullRequestEvent = async (
if (!previousSha) {
if (inputs.sinceLastRemoteCommit) {
previousSha =
env.GITHUB_EVENT_BEFORE ||
(await getRemoteBranchHeadSha({
cwd: workingDirectory,
branch: currentBranch
}))
previousSha = env.GITHUB_EVENT_BEFORE
if (
!previousSha ||

View file

@ -30,6 +30,7 @@ export type Inputs = {
sinceLastRemoteCommit: boolean
writeOutputFiles: boolean
outputDir: string
outputRenamedFilesAsDeletedAndAdded: boolean
}
export const getInputs = (): Inputs => {
@ -111,6 +112,10 @@ export const getInputs = (): Inputs => {
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 = {
files,
@ -139,7 +144,8 @@ export const getInputs = (): Inputs => {
escapeJson,
sinceLastRemoteCommit,
writeOutputFiles,
outputDir
outputDir,
outputRenamedFilesAsDeletedAndAdded
}
if (fetchDepth) {

View file

@ -61,6 +61,8 @@ export async function run(): Promise<void> {
const hasSubmodule = await submoduleExists({cwd: workingDirectory})
let gitFetchExtraArgs = ['--no-tags', '--prune', '--recurse-submodules']
const isTag = env.GITHUB_REF?.startsWith('refs/tags/')
const outputRenamedFilesAsDeletedAndAdded =
inputs.outputRenamedFilesAsDeletedAndAdded
let submodulePaths: string[] = []
if (hasSubmodule) {
@ -118,7 +120,8 @@ export async function run(): Promise<void> {
workingDirectory,
hasSubmodule,
diffResult,
submodulePaths
submodulePaths,
outputRenamedFilesAsDeletedAndAdded
})
core.debug(`All diff files: ${JSON.stringify(allDiffFiles)}`)
@ -261,7 +264,8 @@ export async function run(): Promise<void> {
const otherChangedFiles = allOtherChangedFiles
.split(inputs.separator)
.filter(
filePath => !allChangedFiles.split(inputs.separator).includes(filePath)
(filePath: string) =>
!allChangedFiles.split(inputs.separator).includes(filePath)
)
const onlyChanged =
@ -320,7 +324,8 @@ export async function run(): Promise<void> {
const otherModifiedFiles = allOtherModifiedFiles
.split(inputs.separator)
.filter(
filePath => !allModifiedFiles.split(inputs.separator).includes(filePath)
(filePath: string) =>
!allModifiedFiles.split(inputs.separator).includes(filePath)
)
const onlyModified =

View file

@ -410,7 +410,8 @@ export const getAllChangedFiles = async ({
sha2,
diff,
isSubmodule = false,
parentDir = ''
parentDir = '',
outputRenamedFilesAsDeletedAndAdded = false
}: {
cwd: string
sha1: string
@ -418,6 +419,7 @@ export const getAllChangedFiles = async ({
diff: string
isSubmodule?: boolean
parentDir?: string
outputRenamedFilesAsDeletedAndAdded?: boolean
}): Promise<ChangedFiles> => {
const {exitCode, stdout, stderr} = await exec.getExecOutput(
'git',
@ -466,13 +468,21 @@ export const getAllChangedFiles = async ({
const lines = stdout.split('\n').filter(Boolean)
for (const line of lines) {
const [changeType, filePath] = line.split('\t')
const [changeType, filePath, newPath = ''] = line.split('\t')
const normalizedFilePath = isSubmodule
? normalizePath(path.join(parentDir, filePath))
: normalizePath(filePath)
const normalizedNewPath = isSubmodule
? normalizePath(path.join(parentDir, newPath))
: normalizePath(newPath)
if (changeType.startsWith('R')) {
if (outputRenamedFilesAsDeletedAndAdded) {
changedFiles[ChangeTypeEnum.Deleted].push(normalizedFilePath)
changedFiles[ChangeTypeEnum.Added].push(normalizedNewPath)
} else {
changedFiles[ChangeTypeEnum.Renamed].push(normalizedFilePath)
}
} else {
changedFiles[changeType as ChangeTypeEnum].push(normalizedFilePath)
}