-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Print commit stats #5596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+1,633
−51
Merged
Print commit stats #5596
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2778a54
Add printCommitStats
compulim 72c2af1
New doc category
compulim 2cdf55f
Better name
compulim 7cdef97
Fix filename and run npm ci
compulim f9056c8
Add dependencies
compulim dfde7f4
Better categorization
compulim 1f3f0c8
Fetch main
compulim 9b32c96
Diff from main
compulim 8b853f7
Use origin/main
compulim a9e0d50
Better output
compulim 79df5f6
Add disclaimer
compulim 54300b0
Add a broader permissions.content: read
compulim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| /* eslint-disable no-console */ | ||
| /* eslint-disable no-magic-numbers */ | ||
| /* eslint-env node */ | ||
|
|
||
| import chalk from 'chalk'; | ||
| import { exec } from 'child_process'; | ||
| import { parsePatch } from 'diff'; | ||
| import { promisify } from 'util'; | ||
|
|
||
| function getCategory( | ||
| /** @type { string } */ | ||
| path | ||
| ) { | ||
| return path.includes('/.github/') | ||
| ? 'others' | ||
| : path.endsWith('.md') | ||
| ? 'doc' | ||
| : path.includes('/__tests__/') | ||
| ? 'test' | ||
| : path.includes('package-lock.json') | ||
| ? 'generated' | ||
| : 'production'; | ||
| } | ||
|
|
||
| function toIntegerOrFixed( | ||
| /** @type { number } */ | ||
| value, | ||
| /** @type { number } */ | ||
| fractionDigits = 2 | ||
| ) { | ||
| return value % 1 === 0 ? value : value.toFixed(fractionDigits); | ||
| } | ||
|
|
||
| function toRatio( | ||
| /** @type { number } */ | ||
| x, | ||
| /** @type { number } */ | ||
| y | ||
| ) { | ||
| if (x > y) { | ||
| if (y === 0) { | ||
| return [x, 0]; | ||
| } | ||
|
|
||
| return [x / y, 1]; | ||
| } | ||
|
|
||
| if (x === 0) { | ||
| return [0, y]; | ||
| } | ||
|
|
||
| return [1, y / x]; | ||
| } | ||
|
|
||
| function toRatioString( | ||
| /** @type { number } */ | ||
| x, | ||
| /** @type { number } */ | ||
| y, | ||
| positiveIsGood = false | ||
| ) { | ||
| const chalks = x === y ? [chalk.yellow, chalk.yellow] : [chalk.green, chalk.red]; | ||
| const [ratio1, ratio2] = toRatio(x, y); | ||
|
|
||
| positiveIsGood || chalks.reverse(); | ||
|
|
||
| return chalks[x > y ? 0 : 1](`${toIntegerOrFixed(ratio1)}:${toIntegerOrFixed(ratio2)}`); | ||
| } | ||
|
|
||
| (async () => { | ||
| const { stdout: diffContent } = await promisify(exec)('git diff main..HEAD', { encoding: 'utf-8' }); | ||
|
|
||
| const patches = parsePatch(diffContent); | ||
|
|
||
| /** @type { Map<'doc' | 'generated' | 'others' | 'production' | 'test', { numFile: number; numLineAdded: number; numLineRemoved: number; }> } */ | ||
| const stats = new Map(); | ||
|
|
||
| stats.set('doc', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 }); | ||
| stats.set('generated', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 }); | ||
| stats.set('others', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 }); | ||
| stats.set('production', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 }); | ||
| stats.set('test', { numFile: 0, numLineAdded: 0, numLineRemoved: 0 }); | ||
|
|
||
| patches.forEach(patch => { | ||
| if (patch.newFileName || patch.oldFileName) { | ||
| const stat = stats.get(getCategory(patch.newFileName || patch.oldFileName)); | ||
|
|
||
| stat.numFile++; | ||
|
|
||
| for (const hunk of patch.hunks) { | ||
| for (const line of hunk.lines) { | ||
| if (line.startsWith('+') && !line.startsWith('+++') && line.substring(1).trim()) { | ||
| stat.numLineAdded++; | ||
| } | ||
|
|
||
| if (line.startsWith('-') && !line.startsWith('---') && line.substring(1).trim()) { | ||
| stat.numLineRemoved++; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| console.log('| | Files | Lines added | Lines added and removed | Lines ratio |'); | ||
| console.log('| - | - | - | - | - |'); | ||
|
|
||
| for (const [name, type] of [ | ||
| ['Production code', 'production'], | ||
| ['Test code', 'test'], | ||
| ['Documentation', 'doc'], | ||
| ['Generated code', 'generated'], | ||
| ['Others', 'others'] | ||
| ]) { | ||
| console.log( | ||
| `| ${[ | ||
| name, | ||
| `${stats.get(type).numFile} files`, | ||
| `${stats.get(type).numLineAdded} lines added`, | ||
| `${stats.get(type).numLineAdded + stats.get(type).numLineRemoved} lines total`, | ||
| `${toRatioString(stats.get(type).numLineAdded, stats.get(type).numLineRemoved)} lines added vs. removed` | ||
| ].join(' | ')} |` | ||
| ); | ||
| } | ||
|
|
||
| console.log(); | ||
|
|
||
| const [prodRatio, testRatio] = toRatio(stats.get('production').numLineAdded, stats.get('test').numLineAdded); | ||
|
|
||
| console.log( | ||
| `${testRatio > prodRatio ? '😇' : '🚨'} There are ${chalk.magenta(toIntegerOrFixed(testRatio))} lines of test code added for every ${chalk.magenta(toIntegerOrFixed(prodRatio))} lines of production code added.` | ||
| ); | ||
| })(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.