Skip to content

Instantly share code, notes, and snippets.

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 GrahamTheCoder/76b9f0e50464f198397f997c7a55868f to your computer and use it in GitHub Desktop.
Save GrahamTheCoder/76b9f0e50464f198397f997c7a55868f to your computer and use it in GitHub Desktop.
Gets a list of line ranges (index and length if more than 1) for files changed since diverging from origin master
function Get-LinesChanged() {
$diffFromCommonBase = git diff origin/master...HEAD --unified=0 --dst-prefix=""
$linesByFile = @{}
$currentLines = New-Object System.Collections.ArrayList
foreach ($diffLine in $diffFromCommonBase ) {
if ($diffLine.StartsWith("+++")) {
#e.g. "+++ Path/to/file.cs" -> "Path/to/file.cs"
$currentFile = $diffLine.Substring(4)
$currentLines = $linesByFile[$currentFile] = New-Object System.Collections.ArrayList
}
if ($diffLine.StartsWith("@@")) {
# e.g. "@@ -38 +39,16 @@..." -> "+39,16", the index and length in the new file
$newIndexAndLength = $diffLine.Split(" ")[2].Substring(1)
$null = $currentLines.Add($newIndexAndLength)
}
}
return $linesByFile
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment