Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@JamesRandall
Last active March 13, 2018 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesRandall/05f04f05ef4c7ef734c147db5072ec67 to your computer and use it in GitHub Desktop.
Save JamesRandall/05f04f05ef4c7ef734c147db5072ec67 to your computer and use it in GitHub Desktop.
Script that runs through the git commit log and gets the number of additions and deletions made by date. Run the script in the folder of a git repository. See sampleoutput.csv for, errr, sample output
# Not the best written script in the world but it does a job.
Param(
[string]$path="stats.csv"
)
$commitHashes = git log --pretty=format:"%h"
$commitDates = git log --pretty=format:"%ad" --date=short
$deletions = @()
$additions = @()
$changesOverTime = @()
for ($i=0; $i -lt $commitHashes.Length-1; $i++)
{
$changes = (git diff --numstat $commitHashes[$i] $commitHashes[$i+1]) | Out-String
$changes = $changes.Replace("`r","").Split("`n")
$commitDeletions = 0
$commitAdditions = 0
for ($ic=0; $ic -lt $changes.Length-1; $ic++)
{
$changeLine = $changes[$ic]
$components = $changeLine.Split("`t",[System.StringSplitOptions]::RemoveEmptyEntries)
if ($components[0] -ne "-")
{
$commitDeletions = $commitDeletions + $components[0]
}
if ($components[1] -ne "-")
{
$commitAdditions = $commitAdditions + $components[1]
}
}
$summary = @{
date = $commitDates[$i]
additions = $commitAdditions
deletions = $commitDeletions
}
$changesOverTime += $summary
}
# Sure you could use the PS group command if you were a bit more competent with PS than I!
$groupedByDate = @{}
for($i = 0; $i -lt $changesOverTime.Length; $i++)
{
$dt = $changesOverTime[$i].date
if ($groupedByDate.ContainsKey($dt))
{
$groupedByDate[$dt].additions += $changesOverTime[$i].additions
$groupedByDate[$dt].deletions += $changesOverTime[$i].deletions
}
else
{
$groupedByDate[$dt] = New-Object PSObject -Property @{
date = $dt
additions = $changesOverTime[$i].additions
deletions = $changesOverTime[$i].deletions
}
}
}
$groupedByDate.Values | Sort-Object date | export-csv -Path $path -NoTypeInformation
date additions deletions
2017-06-23 625 1799
2017-06-24 1052 980
2017-06-26 1058 557
2017-06-27 1285 36
2017-06-28 41 3
2017-07-01 158 106
2017-07-15 589 139
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment