Skip to content

Commit 32fdf6a

Browse files
committed
Git-NumberedAdd: accept commit message as last argument
1 parent f703ba3 commit 32fdf6a

File tree

3 files changed

+61
-10
lines changed

3 files changed

+61
-10
lines changed

Tests/Parse-GitIndexes.Tests.ps1

+26
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,32 @@ Describe 'Parse-GitIndexes' {
2929
Pop-Location
3030
}
3131

32+
It 'Parses the last argument as commit message if it could not otherwise be parsed' {
33+
$fileInfos = Parse-GitIndexes @(0,1,"commit message")
34+
35+
$fileInfos.Length | Should -Be 3
36+
$fileInfos[0].file | Should -Be 'file0'
37+
$fileInfos[1].file | Should -Be 'file1'
38+
$fileInfos[2] | Should -Be "commit message"
39+
}
40+
41+
It 'Parses the last argument as commit message with a single index' {
42+
$fileInfos = Parse-GitIndexes @(0,"commit message")
43+
44+
$fileInfos.Length | Should -Be 2
45+
$fileInfos[0].file | Should -Be 'file0'
46+
$fileInfos[1] | Should -Be "commit message"
47+
}
48+
49+
It 'Parses the last argument as commit message with multiple conactenated indexes' {
50+
$fileInfos = Parse-GitIndexes @("03","commit message")
51+
52+
$fileInfos.Length | Should -Be 3
53+
$fileInfos[0].file | Should -Be 'file0'
54+
$fileInfos[1].file | Should -Be 'file3'
55+
$fileInfos[2] | Should -Be "commit message"
56+
}
57+
3258
It 'Parses a single int argument' {
3359
$fileInfos = Parse-GitIndexes @(3)
3460
$fileInfos.Length | Should -Be 1

lib/Parse-GitIndexes.ps1

+23-9
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ function Parse-GitIndexes($argIndexes, $lookIn = "workingDir") {
1111
$allFiles = @($allFiles)
1212
}
1313

14-
15-
16-
if ([string]$argIndexes -match '^[0-9]+$' `
17-
-and ([string]$argIndexes).Length -gt 1 `
18-
-and ($allFiles.length -lt 11 -or [int][string]$argIndexes -ge $allFiles.length)
14+
if ([string]$argIndexes -match '^([0-9]+)(?: (\D+))?$' `
15+
-and ($Matches[0].Length -gt 1) `
16+
-and ($allFiles.length -lt 11 -or [int]$Matches[0] -ge $allFiles.length)
1917
) {
2018
# Add by many 1 digit indexes (ex: 035 == 0, 3 and 5)
21-
$argIndexes = ([string]$argIndexes).ToCharArray()
19+
$argIndexes = $Matches[1].ToCharArray()
20+
$commitMsg = $Matches[2]
2221
}
2322

2423
$indexes = @()
25-
foreach ($arg in $argIndexes) {
26-
$index = $null;
24+
for ($counter = 0; $counter -lt $argIndexes.Length; $counter++) {
25+
$arg = $argIndexes[$counter]
26+
$index = $null; # Initialization for [ref] usage below (CI complains otherwise)
2727

2828
if ($arg -match '^\d+-\d+$') {
2929
# Add by range (ex: 3-5)
@@ -49,18 +49,32 @@ function Parse-GitIndexes($argIndexes, $lookIn = "workingDir") {
4949
# Add by index (ex: 3, 15)
5050
$indexes += $index
5151

52+
} elseif ($argIndexes.Length -gt 1 -and $argIndexes.Length -eq $counter + 1) {
53+
# Last argument: commit message
54+
$commitMsg = $arg
55+
5256
} else {
5357
Write-Host "Unparseable argument '$arg'" -ForegroundColor DarkMagenta
5458
}
5559
}
5660

57-
return $indexes | % {$_} | ? {
61+
$return = $indexes | ? {
5862
if ($_ -ge $allFiles.length) {
5963
Write-Host "$_ is outside of the boundaries of Git-NumberedStatus (Length: $($allFiles.length))" -ForegroundColor DarkMagenta
6064
return $false
6165
}
6266
return $true
6367
} | % { $allFiles[$_] }
68+
69+
if ($commitMsg) {
70+
if ($return -is [array]) {
71+
$return += $commitMsg
72+
} else {
73+
$return = $return,$commitMsg
74+
}
75+
}
76+
77+
return $return
6478
}
6579

6680

lib/actions/Git-NumberedAdd.ps1

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,20 @@ function Git-NumberedAdd {
88
return
99
}
1010

11-
$files = $fileInfos | % {$_.fullPath}
11+
$files = $fileInfos | % {
12+
if ($_ -is [string]) {
13+
$commitMsg = $_
14+
} else {
15+
$_.fullPath
16+
}
17+
}
1218
# write-host "git add -v $files"
1319
git add -v $files
20+
21+
22+
if ($commitMsg) {
23+
git commit -m $commitMsg
24+
}
1425
}
1526

1627

0 commit comments

Comments
 (0)