(
gitPaths: string[],
{ cwd, short = false }: { cwd: string; short?: boolean }
)
| 62 | * @param options - `cwd` and `short` |
| 63 | */ |
| 64 | export async function getCommitsThatAddFiles( |
| 65 | gitPaths: string[], |
| 66 | { cwd, short = false }: { cwd: string; short?: boolean } |
| 67 | ): Promise<(string | undefined)[]> { |
| 68 | // Maps gitPath to commit SHA |
| 69 | const map = new Map<string, string>(); |
| 70 | |
| 71 | // Paths we haven't completed processing on yet |
| 72 | let remaining = gitPaths; |
| 73 | |
| 74 | do { |
| 75 | // Fetch commit information for all paths we don't have yet |
| 76 | const commitInfos = await Promise.all( |
| 77 | remaining.map(async (gitPath: string) => { |
| 78 | const [commitSha, parentSha] = ( |
| 79 | await spawn( |
| 80 | "git", |
| 81 | [ |
| 82 | "log", |
| 83 | "--diff-filter=A", |
| 84 | "--max-count=1", |
| 85 | short ? "--pretty=format:%h:%p" : "--pretty=format:%H:%p", |
| 86 | gitPath, |
| 87 | ], |
| 88 | { cwd } |
| 89 | ) |
| 90 | ).stdout |
| 91 | .toString() |
| 92 | .split(":"); |
| 93 | return { path: gitPath, commitSha, parentSha }; |
| 94 | }) |
| 95 | ); |
| 96 | |
| 97 | // To collect commits without parents (usually because they're absent from |
| 98 | // a shallow clone). |
| 99 | let commitsWithMissingParents = []; |
| 100 | |
| 101 | for (const info of commitInfos) { |
| 102 | if (info.commitSha) { |
| 103 | if (info.parentSha) { |
| 104 | // We have found the parent of the commit that added the file. |
| 105 | // Therefore we know that the commit is legitimate and isn't simply the boundary of a shallow clone. |
| 106 | map.set(info.path, info.commitSha); |
| 107 | } else { |
| 108 | commitsWithMissingParents.push(info); |
| 109 | } |
| 110 | } else { |
| 111 | // No commit for this file, which indicates it doesn't exist. |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | if (commitsWithMissingParents.length === 0) { |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | // The commits we've found may be the real commits or they may be the boundary of |
| 120 | // a shallow clone. |
| 121 |
no test coverage detected