diff --git a/Tests/Parse-GitIndexes.Tests.ps1 b/Tests/Parse-GitIndexes.Tests.ps1 index 77e2890..b53c38c 100644 --- a/Tests/Parse-GitIndexes.Tests.ps1 +++ b/Tests/Parse-GitIndexes.Tests.ps1 @@ -29,6 +29,32 @@ Describe 'Parse-GitIndexes' { Pop-Location } + It 'Parses the last argument as commit message if it could not otherwise be parsed' { + $fileInfos = Parse-GitIndexes @(0,1,"commit message") + + $fileInfos.Length | Should -Be 3 + $fileInfos[0].file | Should -Be 'file0' + $fileInfos[1].file | Should -Be 'file1' + $fileInfos[2] | Should -Be "commit message" + } + + It 'Parses the last argument as commit message with a single index' { + $fileInfos = Parse-GitIndexes @(0,"commit message") + + $fileInfos.Length | Should -Be 2 + $fileInfos[0].file | Should -Be 'file0' + $fileInfos[1] | Should -Be "commit message" + } + + It 'Parses the last argument as commit message with multiple conactenated indexes' { + $fileInfos = Parse-GitIndexes @("03","commit message") + + $fileInfos.Length | Should -Be 3 + $fileInfos[0].file | Should -Be 'file0' + $fileInfos[1].file | Should -Be 'file3' + $fileInfos[2] | Should -Be "commit message" + } + It 'Parses a single int argument' { $fileInfos = Parse-GitIndexes @(3) $fileInfos.Length | Should -Be 1 diff --git a/lib/Parse-GitIndexes.ps1 b/lib/Parse-GitIndexes.ps1 index e63654e..ba61e03 100644 --- a/lib/Parse-GitIndexes.ps1 +++ b/lib/Parse-GitIndexes.ps1 @@ -11,19 +11,19 @@ function Parse-GitIndexes($argIndexes, $lookIn = "workingDir") { $allFiles = @($allFiles) } - - - if ([string]$argIndexes -match '^[0-9]+$' ` - -and ([string]$argIndexes).Length -gt 1 ` - -and ($allFiles.length -lt 11 -or [int][string]$argIndexes -ge $allFiles.length) + if ([string]$argIndexes -match '^([0-9]+)(?: (\D+))?$' ` + -and ($Matches[0].Length -gt 1) ` + -and ($allFiles.length -lt 11 -or [int]$Matches[0] -ge $allFiles.length) ) { # Add by many 1 digit indexes (ex: 035 == 0, 3 and 5) - $argIndexes = ([string]$argIndexes).ToCharArray() + $argIndexes = $Matches[1].ToCharArray() + $commitMsg = $Matches[2] } $indexes = @() - foreach ($arg in $argIndexes) { - $index = $null; + for ($counter = 0; $counter -lt $argIndexes.Length; $counter++) { + $arg = $argIndexes[$counter] + $index = $null; # Initialization for [ref] usage below (CI complains otherwise) if ($arg -match '^\d+-\d+$') { # Add by range (ex: 3-5) @@ -49,18 +49,32 @@ function Parse-GitIndexes($argIndexes, $lookIn = "workingDir") { # Add by index (ex: 3, 15) $indexes += $index + } elseif ($argIndexes.Length -gt 1 -and $argIndexes.Length -eq $counter + 1) { + # Last argument: commit message + $commitMsg = $arg + } else { Write-Host "Unparseable argument '$arg'" -ForegroundColor DarkMagenta } } - return $indexes | % {$_} | ? { + $return = $indexes | ? { if ($_ -ge $allFiles.length) { Write-Host "$_ is outside of the boundaries of Git-NumberedStatus (Length: $($allFiles.length))" -ForegroundColor DarkMagenta return $false } return $true } | % { $allFiles[$_] } + + if ($commitMsg) { + if ($return -is [array]) { + $return += $commitMsg + } else { + $return = $return,$commitMsg + } + } + + return $return } diff --git a/lib/actions/Git-NumberedAdd.ps1 b/lib/actions/Git-NumberedAdd.ps1 index 21f07cc..0460db2 100644 --- a/lib/actions/Git-NumberedAdd.ps1 +++ b/lib/actions/Git-NumberedAdd.ps1 @@ -8,9 +8,20 @@ function Git-NumberedAdd { return } - $files = $fileInfos | % {$_.fullPath} + $files = $fileInfos | % { + if ($_ -is [string]) { + $commitMsg = $_ + } else { + $_.fullPath + } + } # write-host "git add -v $files" git add -v $files + + + if ($commitMsg) { + git commit -m $commitMsg + } }