Skip to content

Commit

Permalink
fix: bug recovering deleted files for submodules (#1784)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 10, 2023
1 parent 7611ff3 commit 9454999
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 17 deletions.
32 changes: 31 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ on:
- auto_merge_enabled
- auto_merge_disabled
branches:
- main
- "**"

jobs:
build:
Expand Down Expand Up @@ -824,6 +824,36 @@ jobs:
cat "deleted_files/test/test deleted.txt"
fi
- name: Run changed-files with recover_deleted_files for an expected git submodule file
id: changed-files-recover-deleted-files-within-submodule
uses: ./
with:
base_sha: "3be651e99d3d4eae395694f6c6f3b9d18457f6c8"
sha: "d90c240f2ad4ec04d8f0f48e5ac290ad96ebe850"
recover_deleted_files: true
fetch_depth: 60000

- name: Show output
run: |
echo "${{ toJSON(steps.changed-files-recover-deleted-files-within-submodule.outputs) }}"
shell:
bash

- name: Verify deleted files
if: steps.changed-files-recover-deleted-files-within-submodule.outputs.deleted_files != 'test/demo/.github/FUNDING.yml'
run: |
echo "Expected: (test/demo/.github/FUNDING.yml) got ${{ steps.changed-files-recover-deleted-files-within-submodule.outputs.deleted_files }}"
exit 1
- name: Verify that test/demo/.github/FUNDING.yml is restored
run: |
if [ ! -f "test/demo/.github/FUNDING.yml" ]; then
echo "Expected: (test/demo/.github/FUNDING.yml) to exist"
exit 1
else
cat "test/demo/.github/FUNDING.yml"
rm "test/demo/.github/FUNDING.yml"
fi
test-dir-names-deleted-files-include-only-deleted-dirs-single-file:
name: Test dir names deleted files include only deleted dirs single file
runs-on: ubuntu-latest
Expand Down
47 changes: 40 additions & 7 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.

4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ const getChangedFilesFromLocalGitHistory = async ({
workingDirectory,
deletedFiles: allDiffFiles[ChangeTypeEnum.Deleted],
recoverPatterns,
sha: diffResult.previousSha
diffResult,
hasSubmodule,
submodulePaths
})
}

Expand Down
58 changes: 51 additions & 7 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as path from 'path'
import {createInterface} from 'readline'
import {parseDocument} from 'yaml'
import {ChangedFiles, ChangeTypeEnum} from './changedFiles'
import {DiffResult} from './commitSha'
import {Inputs} from './inputs'

const MINIMUM_GIT_VERSION = '2.18.0'
Expand Down Expand Up @@ -1395,13 +1396,17 @@ export const recoverDeletedFiles = async ({
workingDirectory,
deletedFiles,
recoverPatterns,
sha
diffResult,
hasSubmodule,
submodulePaths
}: {
inputs: Inputs
workingDirectory: string
deletedFiles: string[]
recoverPatterns: string[]
sha: string
diffResult: DiffResult
hasSubmodule: boolean
submodulePaths: string[]
}): Promise<void> => {
let recoverableDeletedFiles = deletedFiles
core.debug(`recoverable deleted files: ${recoverableDeletedFiles}`)
Expand All @@ -1426,16 +1431,55 @@ export const recoverDeletedFiles = async ({
)
}

const deletedFileContents = await getDeletedFileContents({
cwd: workingDirectory,
filePath: deletedFile,
sha
})
let deletedFileContents: string

const submodulePath = submodulePaths.find(p => deletedFile.startsWith(p))

if (hasSubmodule && submodulePath) {
const submoduleShaResult = await gitSubmoduleDiffSHA({
cwd: workingDirectory,
parentSha1: diffResult.previousSha,
parentSha2: diffResult.currentSha,
submodulePath,
diff: diffResult.diff
})

if (submoduleShaResult.previousSha) {
core.debug(
`recovering deleted file "${deletedFile}" from submodule ${submodulePath} from ${submoduleShaResult.previousSha}`
)
deletedFileContents = await getDeletedFileContents({
cwd: path.join(workingDirectory, submodulePath),
// E.g. submodulePath = test/demo and deletedFile = test/demo/.github/README.md => filePath => .github/README.md
filePath: deletedFile.replace(submodulePath, '').substring(1),
sha: submoduleShaResult.previousSha
})
} else {
core.warning(
`Unable to recover deleted file "${deletedFile}" from submodule ${submodulePath} from ${submoduleShaResult.previousSha}`
)
continue
}
} else {
core.debug(
`recovering deleted file "${deletedFile}" from ${diffResult.previousSha}`
)
deletedFileContents = await getDeletedFileContents({
cwd: workingDirectory,
filePath: deletedFile,
sha: diffResult.previousSha
})
}

core.debug(`recovered deleted file "${deletedFile}"`)

if (!(await exists(path.dirname(target)))) {
core.debug(`creating directory "${path.dirname(target)}"`)
await fs.mkdir(path.dirname(target), {recursive: true})
}
core.debug(`writing file "${target}"`)
await fs.writeFile(target, deletedFileContents)
core.debug(`wrote file "${target}"`)
}
}

Expand Down

0 comments on commit 9454999

Please sign in to comment.