Skip to content

Instantly share code, notes, and snippets.

@ranveeraggarwal
Created August 21, 2017 09:23
Show Gist options
  • Save ranveeraggarwal/d21319240b45471668e36531fced8ad1 to your computer and use it in GitHub Desktop.
Save ranveeraggarwal/d21319240b45471668e36531fced8ad1 to your computer and use it in GitHub Desktop.
Powershell script to find LoC differences 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.
You can basically find the lines of code difference and also ignore some files from the count if required. This includes binaries and system generated files. You can use a wildcard to do so.
.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