|
1 | | -import { execSync } from "node:child_process"; |
2 | | -import { existsSync } from "node:fs"; |
3 | | -import { resolve } from "node:path"; |
| 1 | +import { Path } from "../types"; |
4 | 2 |
|
5 | | -import * as micromatch from "micromatch"; |
| 3 | +export default class Churn { |
| 4 | + private path: Path; |
| 5 | + private changes: number; |
6 | 6 |
|
7 | | -import { buildDebugger, withDuration } from "../../utils"; |
8 | | -import { Options, Path } from "../types"; |
9 | | - |
10 | | -const internal = { debug: buildDebugger("churn") }; |
11 | | -const PER_LINE = "\n"; |
12 | | - |
13 | | -export default { |
14 | | - compute: (...args: any[]): Promise<Map<Path, number>> => |
15 | | - withDuration(compute, args, internal.debug), |
16 | | -}; |
17 | | - |
18 | | -async function compute(options: Options): Promise<Map<Path, number>> { |
19 | | - const gitLogCommand = buildGitLogCommand(options); |
20 | | - const singleStringWithAllChurns = executeGitLogCommand(gitLogCommand); |
21 | | - return computeChurnsPerFiles( |
22 | | - singleStringWithAllChurns, |
23 | | - options.directory, |
24 | | - options.filter |
25 | | - ); |
26 | | -} |
27 | | - |
28 | | -function executeGitLogCommand(gitLogCommand: string): string { |
29 | | - return execSync(gitLogCommand, { encoding: "utf8", maxBuffer: 32_000_000 }); |
30 | | -} |
31 | | - |
32 | | -function buildGitLogCommand(options: Options): string { |
33 | | - const isWindows = process.platform === "win32"; |
34 | | - |
35 | | - return [ |
36 | | - "git", |
37 | | - `-C ${options.directory}`, |
38 | | - `log`, |
39 | | - `--follow`, |
40 | | - |
41 | | - // Windows CMD handle quotes differently than linux, this is why we should put empty string as said in: |
42 | | - // https://github.com/git-for-windows/git/issues/3131 |
43 | | - `--format=${isWindows ? "" : "''"}`, |
44 | | - `--name-only`, |
45 | | - options.since ? `--since="${options.since}"` : "", |
46 | | - options.until ? `--until="${options.until}"` : "", |
47 | | - |
48 | | - // Windows CMD handle quotes differently |
49 | | - isWindows ? "*" : "'*'", |
50 | | - ] |
51 | | - .filter((s) => s.length > 0) |
52 | | - .join(" "); |
53 | | -} |
54 | | - |
55 | | -function computeChurnsPerFiles( |
56 | | - gitLogOutput: string, |
57 | | - directory: string, |
58 | | - filters: string[] | undefined |
59 | | -): Map<Path, number> { |
60 | | - const changedFiles = gitLogOutput |
61 | | - .split(PER_LINE) |
62 | | - .filter((line) => line !== "") |
63 | | - .sort(); |
64 | | - |
65 | | - return changedFiles.reduce((map: Map<Path, number>, path) => { |
66 | | - applyFiltersAndExcludeObsoletePath(path, map); |
67 | | - return map; |
68 | | - }, new Map()); |
69 | | - |
70 | | - function applyFiltersAndExcludeObsoletePath( |
71 | | - path: string, |
72 | | - map: Map<Path, number> |
73 | | - ) { |
74 | | - if (!filters || !filters.length) { |
75 | | - if (pathStillExists(path)) { |
76 | | - addOrIncrement(map, path); |
77 | | - } |
78 | | - } else { |
79 | | - const pathHasAMatch = filters.every((f) => micromatch.isMatch(path, f)); |
80 | | - if (pathHasAMatch) { |
81 | | - if (pathStillExists(path)) { |
82 | | - addOrIncrement(map, path); |
83 | | - } |
84 | | - } |
85 | | - } |
| 7 | + constructor(path: Path) { |
| 8 | + this.path = path; |
| 9 | + this.changes = 0; |
86 | 10 | } |
87 | 11 |
|
88 | | - function addOrIncrement(map: Map<Path, number>, path: string) { |
89 | | - map.set(path, (map.get(path) ?? 0) + 1); |
| 12 | + public increment(): this { |
| 13 | + this.changes += 1; |
| 14 | + return this; |
90 | 15 | } |
91 | 16 |
|
92 | | - function pathStillExists(fileName: string) { |
93 | | - return existsSync(resolve(directory, fileName)); |
| 17 | + public getValue(): number { |
| 18 | + return this.changes; |
94 | 19 | } |
95 | 20 | } |
0 commit comments