Skip to content

Commit 1a61369

Browse files
pipopotamasusimonrenoult
authored andcommitted
add maxBuffer option
1 parent 4e2ff68 commit 1a61369

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010

1111
Quoting Michael Feathers (source [here][michael-feathers-source]):
1212

13-
*Often when we refactor, we look at local areas of code. If we take a wider
13+
_Often when we refactor, we look at local areas of code. If we take a wider
1414
view, using information from our version control systems, we can get a better
15-
sense of the effects of our refactoring efforts.*
16-
15+
sense of the effects of our refactoring efforts._
1716

1817
Note: `code-complexity` currently measures complexity using either:
18+
1919
- lines of code count (all languages)
2020
- cyclomatic complexity (JavaScript/TypeScript)
2121
- halstead complexity (JavaScript/TypeScript)
@@ -43,6 +43,7 @@ $ npx code-complexity <path-to-git-directory or URL> [options]
4343
-u, --until [until] limit analysis to commits older in age than date
4444
-s, --sort [sort] sort results (allowed valued: score, churn, complexity or file)
4545
-d, --directories display values for directories instead of files
46+
-mb, --max-buffer [maxBuffer] set the max buffer size for git log (in bytes)
4647
-h, --help display help for command
4748
4849
Examples:
@@ -53,10 +54,11 @@ $ npx code-complexity <path-to-git-directory or URL> [options]
5354
$ code-complexity ../foo --sort score
5455
$ code-complexity /foo/bar --filter 'src/**,!src/front/**'
5556
$ code-complexity . --limit 10 --sort score
56-
$ code-complexity . --limit 10 --directories
57+
$ code-complexity . --limit 10 --directories
5758
$ code-complexity . --limit 10 --sort score -cs halstead
5859
$ code-complexity . --since=2021-06-01 --limit 100
5960
$ code-complexity . --since=2021-04-01 --until=2021-07-01
61+
$ code-complexity . --max-buffer 64000000
6062
```
6163

6264
## Output
@@ -82,8 +84,8 @@ A special thanks to a few contributors that helped me make `code-complexity` bet
8284
- Alexander Dormann (alexdo) for fixing the `ENOBUFS` (and apologies for stealing your code).
8385
- Scott Brooks (scottamplitude) for initiating the work on complexity strategies
8486

85-
[michael-feathers-source]:https://www.stickyminds.com/article/getting-empirical-about-refactoring
86-
[travis-image]:https://img.shields.io/travis/simonrenoult/code-complexity/master.svg?style=flat-square
87+
[michael-feathers-source]: https://www.stickyminds.com/article/getting-empirical-about-refactoring
88+
[travis-image]: https://img.shields.io/travis/simonrenoult/code-complexity/master.svg?style=flat-square
8789
[travis-url]: https://travis-ci.org/simonrenoult/code-complexity
8890
[style-image]: https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square
8991
[style-url]: https://prettier.io/

src/io/cli.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ function getRawCli(
7979
"-d, --directories",
8080
"display values for directories instead of files"
8181
)
82+
.option(
83+
"-mb, --max-buffer [maxBuffer]",
84+
"set the max buffer size for git log (in bytes)",
85+
parseInt
86+
)
8287
.on("--help", () => {
8388
console.log();
8489
console.log("Examples:");
@@ -94,6 +99,7 @@ function getRawCli(
9499
"$ code-complexity . --limit 10 --sort score -cs halstead",
95100
"$ code-complexity . --since=2021-06-01 --limit 100",
96101
"$ code-complexity . --since=2021-04-01 --until=2021-07-01",
102+
"$ code-complexity . --max-buffer 64000000",
97103
].forEach((example) => console.log(example.padStart(2)));
98104
});
99105
}
@@ -113,6 +119,7 @@ function buildOptions(args: string[], options: any): Options {
113119
complexityStrategy: options.complexityStrategy
114120
? (String(options.complexityStrategy) as ComplexityStrategy)
115121
: "sloc",
122+
maxBuffer: options.maxBuffer ? Number(options.maxBuffer) : undefined,
116123
};
117124

118125
// FIXME: I'm not a fan of pulling the code here but it's good enough.

src/lib/githistory/githistory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export default class GitHistory {
6868
}
6969

7070
private executeGitLogCommand(gitLogCommand: string): string {
71-
return execSync(gitLogCommand, { encoding: "utf8", maxBuffer: 32_000_000 });
71+
const maxBuffer = this.options.maxBuffer ?? 32_000_000;
72+
return execSync(gitLogCommand, { encoding: "utf8", maxBuffer });
7273
}
7374

7475
private listFiles(): string[] {

src/lib/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ export type Options = {
1515
complexityStrategy?: ComplexityStrategy;
1616
filter?: string[];
1717
format?: Format;
18+
maxBuffer?: number;
1819
};

test/lib/statistics.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,27 @@ describe("Statistics", () => {
442442
});
443443
});
444444

445+
context("options.maxBuffer", () => {
446+
it("returns the appropriate elements", async () => {
447+
// Given
448+
const options: Options = { ...defaultOptions, maxBuffer: 64_000_000 };
449+
new TestRepositoryFixture().addFile({ name: "a.js" }).writeOnDisk();
450+
451+
// When
452+
const result = (await Statistics.compute(options)).list();
453+
454+
// Then
455+
expect(result).to.deep.equal([
456+
{
457+
churn: 1,
458+
complexity: 1,
459+
path: "a.js",
460+
score: 1,
461+
},
462+
]);
463+
});
464+
});
465+
445466
context("when file no longer exists", () => {
446467
it("it is ignored", async () => {
447468
// Given

0 commit comments

Comments
 (0)