Skip to content

Instantly share code, notes, and snippets.

@ranveeraggarwal
Last active March 13, 2017 08:26
Show Gist options
  • Save ranveeraggarwal/b191f98f5271b45a205b4957aeec988c to your computer and use it in GitHub Desktop.
Save ranveeraggarwal/b191f98f5271b45a205b4957aeec988c to your computer and use it in GitHub Desktop.
Count lines of code between commits with wildcard ignores
*.txt
a/b/*.txt
*.xml
*.csproj
<#
.NOTES
Name: loc.ps1
Author: Ranveer Aggarwal
.SYNOPSIS
A script to count the lines of code change between two commits with wildcard ignores.
.DESCRIPTION
Run like ./loc.ps1 -ignoreFile path/to/ignore_file.txt -repository path/to/repository -commit1 commit_1_sha -commit2 commit_2_sha
#>
Param (
[String]$ignoreFile = "",
[String]$repository = ".",
[String]$commit1 = "",
[String]$commit2 = ""
)
# Get ignored wildcards
if ($ignoreFile.Equals("")){}
else{
$ignores = (Get-Content $ignoreFile | Out-String).Split("`n")
}
# Change to repository's location
Set-Location -Path $repository
# Get the changed files
$outputs = git diff --numstat $commit1 $commit2 | Out-String
$outputs = $outputs.Split("`n")
# Get list of changed filenames and changed lines
$fileLines = New-Object System.Collections.Generic.List[System.Object]
foreach($changedFile in $outputs) {
if ($changedFile.Equals("")){}
else {
$filename = $changedFile.Split("`t")[2].TrimEnd()
$addedLines = $changedFile.Split("`t")[0]
if ($addedLines.Equals("-")) {
$addedLines = 0
}
$removedLines = $changedFile.Split("`t")[1]
if ($removedLines.Equals("-")) {
$removedLines = 0
}
$fileLines.Add(($filename, $addedLines, $removedLines))
}
}
$ignoredLines = 0
foreach ($fileLine in $fileLines.ToArray()) {
foreach ($ignore in $ignores) {
if ($fileLine[0] -like $ignore.Trim()) {
$ignoredLines += $fileLine[1]
$ignoredLines += $fileLine[2]
}
}
}
# Count total lines
$totalLines = 0
foreach ($fileLine in $fileLines.ToArray()) {
$totalLines += $fileLine[1]
$totalLines += $fileLine[2]
}
# Print Final Output
Write-Host ($totalLines-$ignoredLines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment