(
refs: {
base: string;
head: string;
},
git = simpleGit(),
)
| 37 | } |
| 38 | |
| 39 | export async function listChangedFiles( |
| 40 | refs: { |
| 41 | base: string; |
| 42 | head: string; |
| 43 | }, |
| 44 | git = simpleGit(), |
| 45 | ): Promise<ChangedFiles> { |
| 46 | const statuses: DiffNameStatus[] = [ |
| 47 | DiffNameStatus.ADDED, |
| 48 | DiffNameStatus.COPIED, |
| 49 | DiffNameStatus.MODIFIED, |
| 50 | DiffNameStatus.RENAMED, |
| 51 | ]; |
| 52 | const { files } = await git.diffSummary([ |
| 53 | refs.base, |
| 54 | refs.head, |
| 55 | `--diff-filter=${statuses.join('')}`, |
| 56 | '--find-renames', |
| 57 | '--find-copies', |
| 58 | ]); |
| 59 | |
| 60 | const entries = await Promise.all( |
| 61 | files |
| 62 | .filter(({ binary }) => !binary) |
| 63 | .map(({ file }) => { |
| 64 | const rename = parseFileRename(file); |
| 65 | if (rename) { |
| 66 | return { file: rename.curr, originalFile: rename.prev }; |
| 67 | } |
| 68 | return { file }; |
| 69 | }) |
| 70 | .map(async ({ file, originalFile }) => { |
| 71 | const diff = await git.diff([ |
| 72 | '--unified=0', |
| 73 | refs.base, |
| 74 | refs.head, |
| 75 | '--', |
| 76 | file, |
| 77 | ...(originalFile ? [originalFile] : []), |
| 78 | ]); |
| 79 | const lineChanges = parseDiff(diff); |
| 80 | return [ |
| 81 | file, |
| 82 | { ...(originalFile && { originalFile }), lineChanges }, |
| 83 | ] as const; |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | return Object.fromEntries(entries); |
| 88 | } |
| 89 | |
| 90 | function parseFileRename(file: string): { prev: string; curr: string } | null { |
| 91 | const partialRenameMatch = file.match(/^(.*){(.*) => (.*)}(.*)$/); |
no test coverage detected