Commit 7cacc3cc authored by Dustin Howett's avatar Dustin Howett

Add Get-ChangedFiles, a powershell 'git status'

parent 4eb3b21f
[CmdletBinding()]
Param(
[Parameter()]
[ValidateSet("Index", "Worktree", "All")]
[string]$Source = "All",
[Parameter()]
[string]$Filter,
[Parameter()]
[string[]]$Include,
[Parameter()]
[string[]]$Exclude
)
enum GitFileStatus {
None;
Added;
Modified;
Deleted;
Renamed;
Copied;
}
Function Convert-GitStatusCharacter {
Param( [char]$Character )
Switch ($Character) {
'A' { [GitFileStatus]::Added }
'C' { [GitFileStatus]::Copied }
'D' { [GitFileStatus]::Deleted }
'M' { [GitFileStatus]::Modified }
'R' { [GitFileStatus]::Renamed }
default { [GitFileStatus]::None }
}
}
Function Coalesce-Status {
Param( [GitFileStatus[]]$Statuses )
ForEach ($s in $Statuses) {
If ($s -Ne [GitFileStatus]::None) {
Return $s
}
}
Return [GitFileStatus]::None
}
& git status --porcelain=v2 | % {
If ([String]::IsNullOrEmpty($_)) { Return }
$line = $_
$filename, $renamedOrCopiedFrom = $Null, $Null
Switch ($line[0]) {
"1" { $filename = $line.Split(" ", 9)[8] }
"2" {
$filename, $renamedOrCopiedFrom = $line.Split(" ", 10)[9].Split("`t", 2)
}
"?" { Return }
default {
Write-Warning "Unexpected git status type $($line[0])."
Return
}
}
If (![String]::IsNullOrEmpty($Filter) -And $filename -NotLike $Filter) {
Return
}
ForEach ($i in $Exclude) {
If ($filename -Like $i) {
Return
}
}
ForEach ($i in $Include) {
If ($filename -NotLike $i) {
Return
}
}
$inidx = Convert-GitStatusCharacter([char]($line[2]))
$workt = Convert-GitStatusCharacter([char]($line[3]))
If (($inidx -Ne [GitFileStatus]::None -And $Source -Ne "Worktree") -Or
($workt -Ne [GitFileStatus]::None -And $Source -Ne "Index")) {
Get-Item $filename | Add-Member -PassThru -NotePropertyMembers @{
Status = (Coalesce-Status $workt, $inidx);
OriginalFile = $renamedOrCopiedFrom;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment