Merge d4f86e9d1689a5d2ddb94e173669f31cc3497351 into 0621d936c0d15c7a5a116268ea1f7c362b76c50b

This commit is contained in:
Tonye Jack 2023-06-16 17:53:39 -06:00 committed by GitHub
commit c706c70eed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 94 additions and 2 deletions

View File

@ -145,6 +145,14 @@ inputs:
description: "Output renamed files as deleted and added files." description: "Output renamed files as deleted and added files."
required: false required: false
default: "false" default: "false"
recover_deleted_files_to_original_location:
description: "Recover deleted files to their original location."
required: false
default: "false"
recover_deleted_files_to_destination:
description: "Recover deleted files to the destination directory."
required: false
default: ""
outputs: outputs:
added_files: added_files:

View File

@ -37,6 +37,8 @@ export type Inputs = {
writeOutputFiles: boolean writeOutputFiles: boolean
outputDir: string outputDir: string
outputRenamedFilesAsDeletedAndAdded: boolean outputRenamedFilesAsDeletedAndAdded: boolean
recoverDeletedFiles: boolean
recoverDeletedFilesToDestination: string
} }
export const getInputs = (): Inputs => { export const getInputs = (): Inputs => {
@ -145,6 +147,13 @@ export const getInputs = (): Inputs => {
'output_renamed_files_as_deleted_and_added', 'output_renamed_files_as_deleted_and_added',
{required: false} {required: false}
) )
const recoverDeletedFiles = core.getBooleanInput('recover_deleted_files', {
required: false
})
const recoverDeletedFilesToDestination = core.getInput(
'recover_deleted_files_to_destination',
{required: false}
)
const inputs: Inputs = { const inputs: Inputs = {
files, files,
@ -180,7 +189,9 @@ export const getInputs = (): Inputs => {
sinceLastRemoteCommit, sinceLastRemoteCommit,
writeOutputFiles, writeOutputFiles,
outputDir, outputDir,
outputRenamedFilesAsDeletedAndAdded outputRenamedFilesAsDeletedAndAdded,
recoverDeletedFiles,
recoverDeletedFilesToDestination
} }
if (fetchDepth) { if (fetchDepth) {

View File

@ -1,6 +1,6 @@
import * as core from '@actions/core' import * as core from '@actions/core'
import path from 'path' import path from 'path'
import {getAllDiffFiles, getRenamedFiles} from './changedFiles' import {ChangeTypeEnum, getAllDiffFiles, getRenamedFiles} from './changedFiles'
import {setChangedFilesOutput} from './changedFilesOutput' import {setChangedFilesOutput} from './changedFilesOutput'
import { import {
DiffResult, DiffResult,
@ -14,6 +14,7 @@ import {
getSubmodulePath, getSubmodulePath,
getYamlFilePatterns, getYamlFilePatterns,
isRepoShallow, isRepoShallow,
recoverDeletedFiles,
setOutput, setOutput,
submoduleExists, submoduleExists,
updateGitGlobalConfig, updateGitGlobalConfig,
@ -118,6 +119,13 @@ export async function run(): Promise<void> {
core.info('All Done!') core.info('All Done!')
core.endGroup() core.endGroup()
recoverDeletedFiles({
inputs,
workingDirectory,
deletedFiles: allDiffFiles[ChangeTypeEnum.Deleted],
sha: diffResult.currentSha
})
const filePatterns = await getFilePatterns({ const filePatterns = await getFilePatterns({
inputs, inputs,
workingDirectory workingDirectory

View File

@ -1032,3 +1032,68 @@ export const setOutput = async ({
await fs.writeFile(outputFilePath, cleanedValue.replace(/\\"/g, '"')) await fs.writeFile(outputFilePath, cleanedValue.replace(/\\"/g, '"'))
} }
} }
const getDeletedFileContents = async ({
cwd,
filePath,
sha
}: {
cwd: string
filePath: string
sha: string
}): Promise<string> => {
const {stdout, exitCode, stderr} = await exec.getExecOutput(
'git',
['show', `${sha}:${filePath}`],
{
cwd,
silent: process.env.RUNNER_DEBUG !== '1',
ignoreReturnCode: true
}
)
if (exitCode !== 0) {
throw new Error(
`Error getting file content from git history "${filePath}": ${stderr}`
)
}
return stdout
}
export const recoverDeletedFiles = async ({
inputs,
workingDirectory,
deletedFiles,
sha
}: {
inputs: Inputs
workingDirectory: string
deletedFiles: string[]
sha: string
}): Promise<void> => {
if (inputs.recoverDeletedFiles) {
for (const deletedFile of deletedFiles) {
let target = path.join(workingDirectory, deletedFile)
if (inputs.recoverDeletedFilesToDestination) {
target = path.join(
workingDirectory,
inputs.recoverDeletedFilesToDestination,
deletedFile
)
}
const deletedFileContents = await getDeletedFileContents({
cwd: workingDirectory,
filePath: deletedFile,
sha
})
if (!(await exists(path.dirname(target)))) {
await fs.mkdir(path.dirname(target), {recursive: true})
}
await fs.writeFile(target, deletedFileContents)
}
}
}