Skip to content

Commit

Permalink
Git-NumberedAdd: accept commit message as last argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Laoujin committed Jan 13, 2020
1 parent f703ba3 commit 32fdf6a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
26 changes: 26 additions & 0 deletions Tests/Parse-GitIndexes.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 23 additions & 9 deletions lib/Parse-GitIndexes.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}


Expand Down
13 changes: 12 additions & 1 deletion lib/actions/Git-NumberedAdd.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}


Expand Down

0 comments on commit 32fdf6a

Please sign in to comment.