Skip to content

Commit 5943667

Browse files
author
Simon Renoult
committed
refactor: make churn calculations steps clearer
1 parent bcd03d4 commit 5943667

File tree

1 file changed

+47
-38
lines changed

1 file changed

+47
-38
lines changed

src/lib/churn.ts

Lines changed: 47 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,36 @@ async function compute(options: Options): Promise<Map<Path, number>> {
1919
assertGitIsInstalled();
2020
assertIsGitRootDirectory(options.directory);
2121

22-
const isWindows = process.platform === "win32";
23-
const gitLogCommand = buildGitLogCommand(options, isWindows);
24-
25-
internal.debug(`command to measure churns: ${gitLogCommand}`);
22+
const gitLogCommand = buildGitLogCommand(options);
23+
const rawStringOfAllChurns = executeGitLogCommand(gitLogCommand);
24+
const arrayOfAllChurns = computeNumberOfTimesFilesChanged(
25+
rawStringOfAllChurns,
26+
options.directory
27+
);
28+
const mapOfAllChurns = createMapOfChurnsPerFile(arrayOfAllChurns);
29+
return applyUserFilters(mapOfAllChurns, options.filter);
30+
}
2631

27-
const gitLogStdout = execSync(gitLogCommand, { encoding: "utf8" });
32+
function applyUserFilters(
33+
allChurns: Map<Path, number>,
34+
filter: string[] | undefined
35+
): Map<Path, number> {
36+
const filteredChurns: Map<Path, number> = new Map();
37+
allChurns.forEach((churn: number, path: Path) => {
38+
const patchIsAMatch =
39+
filter && filter.length > 0
40+
? filter.every((f) => micromatch.isMatch(path, f))
41+
: true;
2842

29-
const parsedLines: ParsedLine[] = computeNumberOfTimesFilesChanged(
30-
gitLogStdout
31-
).filter((parsedLine: ParsedLine) => {
32-
return existsSync(resolve(options.directory, parsedLine.relativePath));
43+
if (patchIsAMatch) filteredChurns.set(path, churn);
3344
});
45+
return filteredChurns;
46+
}
3447

35-
const allChurns = parsedLines.reduce(
48+
function createMapOfChurnsPerFile(
49+
numberOfTimesFilesChanged: ParsedLine[]
50+
): Map<Path, number> {
51+
return numberOfTimesFilesChanged.reduce(
3652
(map: Map<Path, number>, { relativePath, commitCount }: ParsedLine) => {
3753
const path: Path = relativePath;
3854
const churn = parseInt(commitCount, 10);
@@ -41,26 +57,10 @@ async function compute(options: Options): Promise<Map<Path, number>> {
4157
},
4258
new Map()
4359
);
60+
}
4461

45-
internal.debug(
46-
`${allChurns.size} files to compute churn on (before filters)`
47-
);
48-
49-
const filteredChurns: Map<Path, number> = new Map();
50-
allChurns.forEach((churn: number, path: Path) => {
51-
const patchIsAMatch =
52-
options.filter && options.filter.length > 0
53-
? options.filter.every((f) => micromatch.isMatch(path, f))
54-
: true;
55-
56-
if (patchIsAMatch) filteredChurns.set(path, churn);
57-
});
58-
59-
internal.debug(
60-
`${filteredChurns.size} files to compute churn on (after filters)`
61-
);
62-
63-
return filteredChurns;
62+
function executeGitLogCommand(gitLogCommand: string): string {
63+
return execSync(gitLogCommand, { encoding: "utf8" });
6464
}
6565

6666
function assertGitIsInstalled(): void {
@@ -77,7 +77,9 @@ function assertIsGitRootDirectory(directory: string): void {
7777
}
7878
}
7979

80-
function buildGitLogCommand(options: Options, isWindows: boolean): string {
80+
function buildGitLogCommand(options: Options): string {
81+
const isWindows = process.platform === "win32";
82+
8183
return [
8284
"git",
8385
`-C ${options.directory}`,
@@ -98,7 +100,10 @@ function buildGitLogCommand(options: Options, isWindows: boolean): string {
98100
.join(" ");
99101
}
100102

101-
function computeNumberOfTimesFilesChanged(gitLogOutput: string): ParsedLine[] {
103+
function computeNumberOfTimesFilesChanged(
104+
gitLogOutput: string,
105+
directory: string
106+
): ParsedLine[] {
102107
const changedFiles = gitLogOutput
103108
.split(PER_LINE)
104109
.filter((line) => line !== "")
@@ -112,11 +117,15 @@ function computeNumberOfTimesFilesChanged(gitLogOutput: string): ParsedLine[] {
112117
{}
113118
);
114119

115-
return Object.keys(changedFilesCount).map(
116-
(changedFileName) =>
117-
({
118-
relativePath: changedFileName,
119-
commitCount: changedFilesCount[changedFileName].toString(),
120-
} as ParsedLine)
121-
);
120+
return Object.keys(changedFilesCount)
121+
.map(
122+
(changedFileName) =>
123+
({
124+
relativePath: changedFileName,
125+
commitCount: changedFilesCount[changedFileName].toString(),
126+
} as ParsedLine)
127+
)
128+
.filter((parsedLine: ParsedLine) => {
129+
return existsSync(resolve(directory, parsedLine.relativePath));
130+
});
122131
}

0 commit comments

Comments
 (0)