diff --git a/src/utils/git.ts b/src/utils/git.ts index 18b0b0b..4aa77d4 100644 --- a/src/utils/git.ts +++ b/src/utils/git.ts @@ -44,7 +44,7 @@ export const getFilesDiffs = async (): Promise => { const filesDiffsMap: FilesDiffsMap = {} for (const file of diffSummary.files) { - const fileDiffs = await git.diff(['-U0', targetShaOrRef, file.file]) + const fileDiffs = await getFileDiffs(targetShaOrRef, file.file) const changedLines: GitDiffs[] = fileDiffs .split('\n') @@ -75,6 +75,23 @@ export const getFilesDiffs = async (): Promise => { return filesDiffsMap } +const getFileDiffs = async ( + targetShaOrRef: string, + filePath: string +): Promise => { + try { + const diffs = await git.diff(['-U0', targetShaOrRef, filePath]) + return diffs + } catch (error) { + if ((error as Error).message?.includes('path not in the working tree')) { + // file was removed, no diffs to be parsed + return '' + } else { + throw error + } + } +} + export const isSourceInChangedScope = ( filesDiffs: FilesDiffsMap, sourceFilePath: string,