2023-05-25 18:22:24 +00:00
|
|
|
import * as path from 'path'
|
|
|
|
|
|
|
|
import {DiffResult} from './commitSha'
|
|
|
|
import {Inputs} from './inputs'
|
|
|
|
import {
|
|
|
|
getDirnameMaxDepth,
|
|
|
|
gitRenamedFiles,
|
|
|
|
gitSubmoduleDiffSHA,
|
2023-06-14 18:45:32 +00:00
|
|
|
jsonOutput,
|
|
|
|
getAllChangedFiles
|
2023-05-25 18:22:24 +00:00
|
|
|
} from './utils'
|
2023-06-14 18:45:32 +00:00
|
|
|
import flatten from 'lodash/flatten'
|
2023-05-25 18:22:24 +00:00
|
|
|
|
|
|
|
export const getRenamedFiles = async ({
|
|
|
|
inputs,
|
|
|
|
workingDirectory,
|
|
|
|
hasSubmodule,
|
|
|
|
diffResult,
|
|
|
|
submodulePaths
|
|
|
|
}: {
|
|
|
|
inputs: Inputs
|
|
|
|
workingDirectory: string
|
|
|
|
hasSubmodule: boolean
|
|
|
|
diffResult: DiffResult
|
|
|
|
submodulePaths: string[]
|
2023-06-17 03:13:40 +00:00
|
|
|
}): Promise<{paths: string; count: string}> => {
|
2023-05-25 18:22:24 +00:00
|
|
|
const renamedFiles = await gitRenamedFiles({
|
|
|
|
cwd: workingDirectory,
|
|
|
|
sha1: diffResult.previousSha,
|
|
|
|
sha2: diffResult.currentSha,
|
|
|
|
diff: diffResult.diff,
|
|
|
|
oldNewSeparator: inputs.oldNewSeparator
|
|
|
|
})
|
|
|
|
|
|
|
|
if (hasSubmodule) {
|
|
|
|
for (const submodulePath of submodulePaths) {
|
|
|
|
const submoduleShaResult = await gitSubmoduleDiffSHA({
|
|
|
|
cwd: workingDirectory,
|
|
|
|
parentSha1: diffResult.previousSha,
|
|
|
|
parentSha2: diffResult.currentSha,
|
|
|
|
submodulePath,
|
|
|
|
diff: diffResult.diff
|
|
|
|
})
|
|
|
|
|
|
|
|
const submoduleWorkingDirectory = path.join(
|
|
|
|
workingDirectory,
|
|
|
|
submodulePath
|
|
|
|
)
|
|
|
|
|
|
|
|
if (submoduleShaResult.currentSha && submoduleShaResult.previousSha) {
|
|
|
|
const submoduleRenamedFiles = await gitRenamedFiles({
|
|
|
|
cwd: submoduleWorkingDirectory,
|
|
|
|
sha1: submoduleShaResult.previousSha,
|
|
|
|
sha2: submoduleShaResult.currentSha,
|
|
|
|
diff: diffResult.diff,
|
|
|
|
oldNewSeparator: inputs.oldNewSeparator,
|
|
|
|
isSubmodule: true,
|
|
|
|
parentDir: submodulePath
|
|
|
|
})
|
|
|
|
renamedFiles.push(...submoduleRenamedFiles)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputs.json) {
|
2023-06-17 03:13:40 +00:00
|
|
|
return {
|
|
|
|
paths: jsonOutput({value: renamedFiles, shouldEscape: inputs.escapeJson}),
|
|
|
|
count: renamedFiles.length.toString()
|
|
|
|
}
|
2023-05-25 18:22:24 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 03:13:40 +00:00
|
|
|
return {
|
|
|
|
paths: renamedFiles.join(inputs.oldNewFilesSeparator),
|
|
|
|
count: renamedFiles.length.toString()
|
|
|
|
}
|
2023-05-25 18:22:24 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 18:45:32 +00:00
|
|
|
export enum ChangeTypeEnum {
|
|
|
|
Added = 'A',
|
|
|
|
Copied = 'C',
|
|
|
|
Deleted = 'D',
|
|
|
|
Modified = 'M',
|
|
|
|
Renamed = 'R',
|
|
|
|
TypeChanged = 'T',
|
|
|
|
Unmerged = 'U',
|
|
|
|
Unknown = 'X'
|
|
|
|
}
|
|
|
|
|
|
|
|
export type ChangedFiles = {
|
|
|
|
[key in ChangeTypeEnum]: string[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getAllDiffFiles = async ({
|
2023-05-25 18:22:24 +00:00
|
|
|
workingDirectory,
|
|
|
|
hasSubmodule,
|
|
|
|
diffResult,
|
2023-06-14 19:59:31 +00:00
|
|
|
submodulePaths,
|
|
|
|
outputRenamedFilesAsDeletedAndAdded
|
2023-05-25 18:22:24 +00:00
|
|
|
}: {
|
|
|
|
workingDirectory: string
|
|
|
|
hasSubmodule: boolean
|
|
|
|
diffResult: DiffResult
|
|
|
|
submodulePaths: string[]
|
2023-06-14 19:59:31 +00:00
|
|
|
outputRenamedFilesAsDeletedAndAdded: boolean
|
2023-06-14 18:45:32 +00:00
|
|
|
}): Promise<ChangedFiles> => {
|
|
|
|
const files = await getAllChangedFiles({
|
2023-05-25 18:22:24 +00:00
|
|
|
cwd: workingDirectory,
|
|
|
|
sha1: diffResult.previousSha,
|
|
|
|
sha2: diffResult.currentSha,
|
2023-06-14 19:59:31 +00:00
|
|
|
diff: diffResult.diff,
|
|
|
|
outputRenamedFilesAsDeletedAndAdded
|
2023-05-25 18:22:24 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
if (hasSubmodule) {
|
|
|
|
for (const submodulePath of submodulePaths) {
|
|
|
|
const submoduleShaResult = await gitSubmoduleDiffSHA({
|
|
|
|
cwd: workingDirectory,
|
|
|
|
parentSha1: diffResult.previousSha,
|
|
|
|
parentSha2: diffResult.currentSha,
|
|
|
|
submodulePath,
|
|
|
|
diff: diffResult.diff
|
|
|
|
})
|
|
|
|
|
|
|
|
const submoduleWorkingDirectory = path.join(
|
|
|
|
workingDirectory,
|
|
|
|
submodulePath
|
|
|
|
)
|
|
|
|
|
|
|
|
if (submoduleShaResult.currentSha && submoduleShaResult.previousSha) {
|
2023-06-14 18:45:32 +00:00
|
|
|
const submoduleFiles = await getAllChangedFiles({
|
2023-05-25 18:22:24 +00:00
|
|
|
cwd: submoduleWorkingDirectory,
|
|
|
|
sha1: submoduleShaResult.previousSha,
|
|
|
|
sha2: submoduleShaResult.currentSha,
|
|
|
|
diff: diffResult.diff,
|
|
|
|
isSubmodule: true,
|
2023-06-14 19:59:31 +00:00
|
|
|
parentDir: submodulePath,
|
|
|
|
outputRenamedFilesAsDeletedAndAdded
|
2023-05-25 18:22:24 +00:00
|
|
|
})
|
2023-06-14 18:45:32 +00:00
|
|
|
|
|
|
|
for (const changeType of Object.keys(
|
|
|
|
submoduleFiles
|
|
|
|
) as ChangeTypeEnum[]) {
|
|
|
|
if (!files[changeType]) {
|
|
|
|
files[changeType] = []
|
|
|
|
}
|
|
|
|
files[changeType].push(...submoduleFiles[changeType])
|
|
|
|
}
|
2023-05-25 18:22:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 18:45:32 +00:00
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
|
|
|
function* getChangeTypeFilesGenerator({
|
|
|
|
inputs,
|
|
|
|
changedFiles,
|
|
|
|
changeTypes
|
|
|
|
}: {
|
|
|
|
inputs: Inputs
|
|
|
|
changedFiles: ChangedFiles
|
|
|
|
changeTypes: ChangeTypeEnum[]
|
|
|
|
}): Generator<string> {
|
|
|
|
for (const changeType of changeTypes) {
|
|
|
|
const files = changedFiles[changeType] || []
|
|
|
|
for (const file of files) {
|
|
|
|
if (inputs.dirNames) {
|
|
|
|
yield getDirnameMaxDepth({
|
|
|
|
pathStr: file,
|
|
|
|
dirNamesMaxDepth: inputs.dirNamesMaxDepth,
|
|
|
|
excludeCurrentDir:
|
|
|
|
inputs.dirNamesExcludeRoot || inputs.dirNamesExcludeCurrentDir
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
yield file
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const getChangeTypeFiles = async ({
|
|
|
|
inputs,
|
|
|
|
changedFiles,
|
|
|
|
changeTypes
|
|
|
|
}: {
|
|
|
|
inputs: Inputs
|
|
|
|
changedFiles: ChangedFiles
|
|
|
|
changeTypes: ChangeTypeEnum[]
|
2023-06-17 03:13:40 +00:00
|
|
|
}): Promise<{paths: string; count: string}> => {
|
2023-06-14 18:45:32 +00:00
|
|
|
const files = [
|
|
|
|
...new Set(getChangeTypeFilesGenerator({inputs, changedFiles, changeTypes}))
|
|
|
|
]
|
|
|
|
|
|
|
|
if (inputs.json) {
|
2023-06-17 03:13:40 +00:00
|
|
|
return {
|
|
|
|
paths: jsonOutput({value: files, shouldEscape: inputs.escapeJson}),
|
|
|
|
count: files.length.toString()
|
|
|
|
}
|
2023-06-14 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 03:13:40 +00:00
|
|
|
return {
|
|
|
|
paths: files.join(inputs.separator),
|
|
|
|
count: files.length.toString()
|
|
|
|
}
|
2023-06-14 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function* getAllChangeTypeFilesGenerator({
|
|
|
|
inputs,
|
|
|
|
changedFiles
|
|
|
|
}: {
|
|
|
|
inputs: Inputs
|
|
|
|
changedFiles: ChangedFiles
|
|
|
|
}): Generator<string> {
|
|
|
|
for (const file of flatten(Object.values(changedFiles))) {
|
|
|
|
if (inputs.dirNames) {
|
|
|
|
yield getDirnameMaxDepth({
|
2023-05-25 18:22:24 +00:00
|
|
|
pathStr: file,
|
|
|
|
dirNamesMaxDepth: inputs.dirNamesMaxDepth,
|
2023-06-06 12:00:56 +00:00
|
|
|
excludeCurrentDir:
|
|
|
|
inputs.dirNamesExcludeRoot || inputs.dirNamesExcludeCurrentDir
|
2023-05-25 18:22:24 +00:00
|
|
|
})
|
2023-06-14 18:45:32 +00:00
|
|
|
} else {
|
|
|
|
yield file
|
|
|
|
}
|
2023-05-25 18:22:24 +00:00
|
|
|
}
|
2023-06-14 18:45:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const getAllChangeTypeFiles = async ({
|
|
|
|
inputs,
|
|
|
|
changedFiles
|
|
|
|
}: {
|
|
|
|
inputs: Inputs
|
|
|
|
changedFiles: ChangedFiles
|
2023-06-17 03:13:40 +00:00
|
|
|
}): Promise<{paths: string; count: string}> => {
|
2023-06-14 18:45:32 +00:00
|
|
|
const files = [
|
|
|
|
...new Set(getAllChangeTypeFilesGenerator({inputs, changedFiles}))
|
|
|
|
]
|
2023-05-25 18:22:24 +00:00
|
|
|
|
|
|
|
if (inputs.json) {
|
2023-06-17 03:13:40 +00:00
|
|
|
return {
|
|
|
|
paths: jsonOutput({value: files, shouldEscape: inputs.escapeJson}),
|
|
|
|
count: files.length.toString()
|
|
|
|
}
|
2023-05-25 18:22:24 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 03:13:40 +00:00
|
|
|
return {
|
|
|
|
paths: files.join(inputs.separator),
|
|
|
|
count: files.length.toString()
|
|
|
|
}
|
2023-05-25 18:22:24 +00:00
|
|
|
}
|