* Get git diff stats between main and g3, for all files and filtered to only g3 affecting * files.
(
git: AuthenticatedGitClient,
g3Ref: string,
mainRef: string,
syncMatchFns: {ngMatchFn: SyncFileMatchFn; separateMatchFn: SyncFileMatchFn},
)
| 46 | * files. |
| 47 | */ |
| 48 | static getDiffStats( |
| 49 | git: AuthenticatedGitClient, |
| 50 | g3Ref: string, |
| 51 | mainRef: string, |
| 52 | syncMatchFns: {ngMatchFn: SyncFileMatchFn; separateMatchFn: SyncFileMatchFn}, |
| 53 | ): G3StatsData { |
| 54 | /** The diff stats to be returned. */ |
| 55 | const stats = { |
| 56 | insertions: 0, |
| 57 | deletions: 0, |
| 58 | files: 0, |
| 59 | separateFiles: 0, |
| 60 | commits: 0, |
| 61 | }; |
| 62 | |
| 63 | // Determine the number of commits between main and g3 refs. */ |
| 64 | stats.commits = parseInt(git.run(['rev-list', '--count', `${g3Ref}..${mainRef}`]).stdout, 10); |
| 65 | |
| 66 | // Get the numstat information between main and g3 |
| 67 | const numStatDiff = git |
| 68 | .run(['diff', `${g3Ref}...${mainRef}`, '--numstat']) |
| 69 | .stdout // Remove the extra space after git's output. |
| 70 | .trim(); |
| 71 | |
| 72 | // If there is no diff, we can return early. |
| 73 | if (numStatDiff === '') { |
| 74 | return stats; |
| 75 | } |
| 76 | |
| 77 | // Split each line of git output into array |
| 78 | numStatDiff |
| 79 | .split('\n') |
| 80 | // Split each line from the git output into components parts: insertions, |
| 81 | // deletions and file name respectively |
| 82 | .map((line) => line.trim().split('\t')) |
| 83 | // Parse number value from the insertions and deletions values |
| 84 | // Example raw line input: |
| 85 | // 10\t5\tsrc/file/name.ts |
| 86 | .map((line) => [Number(line[0]), Number(line[1]), line[2]] as [number, number, string]) |
| 87 | // Add each line's value to the diff stats, and conditionally to the g3 |
| 88 | // stats as well if the file name is included in the files synced to g3. |
| 89 | .forEach(([insertions, deletions, fileName]) => { |
| 90 | if (syncMatchFns.ngMatchFn(fileName)) { |
| 91 | stats.insertions += insertions; |
| 92 | stats.deletions += deletions; |
| 93 | stats.files += 1; |
| 94 | } else if (syncMatchFns.separateMatchFn(fileName)) { |
| 95 | stats.insertions += insertions; |
| 96 | stats.deletions += deletions; |
| 97 | stats.separateFiles += 1; |
| 98 | } |
| 99 | }); |
| 100 | |
| 101 | return stats; |
| 102 | } |
| 103 | |
| 104 | /** Fetch and retrieve the latest sha for a specific branch. */ |
| 105 | static getShaForBranchLatest(git: AuthenticatedGitClient, branch: string) { |