Skip to content

Commit dc2b636

Browse files
authored
Merge pull request #894 from dahlbyk/NihilityT/master
Fix missing CHERRY-PICKING, MERGING, REVERTING, etc
2 parents 5c654bf + 66efbc4 commit dc2b636

File tree

4 files changed

+137
-56
lines changed

4 files changed

+137
-56
lines changed

.git-blame-ignore-revs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Normalize line endings
2+
4256c20d7fa3514651df10f4dbc38bcd8fa012e3

src/GitUtils.ps1

Lines changed: 60 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function Get-GitDirectory {
5959
}
6060
}
6161

62-
function Get-GitBranch($gitDir = $(Get-GitDirectory), [Diagnostics.Stopwatch]$sw) {
62+
function Get-GitBranch($branch = $null, $gitDir = $(Get-GitDirectory), [switch]$isDotGitOrBare, [Diagnostics.Stopwatch]$sw) {
6363
if (!$gitDir) { return }
6464

6565
Invoke-Utf8ConsoleCommand {
@@ -114,7 +114,13 @@ function Get-GitBranch($gitDir = $(Get-GitDirectory), [Diagnostics.Stopwatch]$sw
114114
$r = '|BISECTING'
115115
}
116116

117+
if ($step -and $total) {
118+
$r += " $step/$total"
119+
}
120+
117121
$b = Invoke-NullCoalescing `
122+
$b `
123+
$branch `
118124
{ dbg 'Trying symbolic-ref' $sw; git --no-optional-locks symbolic-ref HEAD -q 2>$null } `
119125
{ '({0})' -f (Invoke-NullCoalescing `
120126
{
@@ -143,7 +149,7 @@ function Get-GitBranch($gitDir = $(Get-GitDirectory), [Diagnostics.Stopwatch]$sw
143149
return $Matches['ref']
144150
}
145151
elseif ($ref -and $ref.Length -ge 7) {
146-
return $ref.Substring(0,7)+'...'
152+
return $ref.Substring(0, 7) + '...'
147153
}
148154
else {
149155
return 'unknown'
@@ -152,23 +158,21 @@ function Get-GitBranch($gitDir = $(Get-GitDirectory), [Diagnostics.Stopwatch]$sw
152158
) }
153159
}
154160

155-
dbg 'Inside git directory?' $sw
156-
$revParseOut = git --no-optional-locks rev-parse --is-inside-git-dir 2>$null
157-
if ('true' -eq $revParseOut) {
158-
dbg 'Inside git directory' $sw
159-
$revParseOut = git --no-optional-locks rev-parse --is-bare-repository 2>$null
161+
if ($isDotGitOrBare -or !$b) {
162+
dbg 'Inside git directory?' $sw
163+
$revParseOut = git --no-optional-locks rev-parse --is-inside-git-dir 2>$null
160164
if ('true' -eq $revParseOut) {
161-
$c = 'BARE:'
162-
}
163-
else {
164-
$b = 'GIT_DIR!'
165+
dbg 'Inside git directory' $sw
166+
$revParseOut = git --no-optional-locks rev-parse --is-bare-repository 2>$null
167+
if ('true' -eq $revParseOut) {
168+
$c = 'BARE:'
169+
}
170+
else {
171+
$b = 'GIT_DIR!'
172+
}
165173
}
166174
}
167175

168-
if ($step -and $total) {
169-
$r += " $step/$total"
170-
}
171-
172176
"$c$($b -replace 'refs/heads/','')$r"
173177
}
174178
}
@@ -228,7 +232,7 @@ function Get-GitStatus {
228232
param(
229233
# The path of a directory within a Git repository that you want to get
230234
# the Git status.
231-
[Parameter(Position=0)]
235+
[Parameter(Position = 0)]
232236
$GitDir = (Get-GitDirectory),
233237

234238
# If specified, overrides $GitPromptSettings.EnableFileStatus and
@@ -266,7 +270,8 @@ function Get-GitStatus {
266270
$stashCount = 0
267271

268272
$fileStatusEnabled = $Force -or $settings.EnableFileStatus
269-
if ($fileStatusEnabled -and !$(InDotGitOrBareRepoDir $GitDir) -and !$(InDisabledRepository)) {
273+
# Optimization: short-circuit to avoid InDotGitOrBareRepoDir and InDisabledRepository if !$fileStatusEnabled
274+
if ($fileStatusEnabled -and !$($isDotGitOrBare = InDotGitOrBareRepoDir $GitDir) -and !$(InDisabledRepository)) {
270275
if ($null -eq $settings.EnableFileStatusFromCache) {
271276
$settings.EnableFileStatusFromCache = $null -ne (Get-Module GitStatusCachePoshClient)
272277
}
@@ -285,21 +290,21 @@ function Get-GitStatus {
285290
else {
286291
dbg 'Parsing status' $sw
287292

288-
$indexAdded.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.IndexAdded))))
289-
$indexModified.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.IndexModified))))
293+
$indexAdded.AddRange($castStringSeq.Invoke($null, (, @($cacheResponse.IndexAdded))))
294+
$indexModified.AddRange($castStringSeq.Invoke($null, (, @($cacheResponse.IndexModified))))
290295
foreach ($indexRenamed in $cacheResponse.IndexRenamed) {
291296
$indexModified.Add($indexRenamed.Old)
292297
}
293-
$indexDeleted.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.IndexDeleted))))
294-
$indexUnmerged.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.Conflicted))))
298+
$indexDeleted.AddRange($castStringSeq.Invoke($null, (, @($cacheResponse.IndexDeleted))))
299+
$indexUnmerged.AddRange($castStringSeq.Invoke($null, (, @($cacheResponse.Conflicted))))
295300

296-
$filesAdded.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.WorkingAdded))))
297-
$filesModified.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.WorkingModified))))
301+
$filesAdded.AddRange($castStringSeq.Invoke($null, (, @($cacheResponse.WorkingAdded))))
302+
$filesModified.AddRange($castStringSeq.Invoke($null, (, @($cacheResponse.WorkingModified))))
298303
foreach ($workingRenamed in $cacheResponse.WorkingRenamed) {
299304
$filesModified.Add($workingRenamed.Old)
300305
}
301-
$filesDeleted.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.WorkingDeleted))))
302-
$filesUnmerged.AddRange($castStringSeq.Invoke($null, (,@($cacheResponse.Conflicted))))
306+
$filesDeleted.AddRange($castStringSeq.Invoke($null, (, @($cacheResponse.WorkingDeleted))))
307+
$filesUnmerged.AddRange($castStringSeq.Invoke($null, (, @($cacheResponse.Conflicted))))
303308

304309
$branch = $cacheResponse.Branch
305310
$upstream = $cacheResponse.Upstream
@@ -319,9 +324,9 @@ function Get-GitStatus {
319324
else {
320325
dbg 'Getting status' $sw
321326
switch ($settings.UntrackedFilesMode) {
322-
"No" { $untrackedFilesOption = "-uno" }
323-
"All" { $untrackedFilesOption = "-uall" }
324-
default { $untrackedFilesOption = "-unormal" }
327+
"No" { $untrackedFilesOption = "-uno" }
328+
"All" { $untrackedFilesOption = "-uall" }
329+
default { $untrackedFilesOption = "-unormal" }
325330
}
326331
$status = Invoke-Utf8ConsoleCommand { git --no-optional-locks -c core.quotepath=false -c color.status=false status $untrackedFilesOption --short --branch 2>$null }
327332
if ($settings.EnableStashStatus) {
@@ -384,41 +389,41 @@ function Get-GitStatus {
384389
}
385390
}
386391

387-
if (!$branch) { $branch = Get-GitBranch $GitDir $sw }
392+
$branch = Get-GitBranch -Branch $branch -GitDir $GitDir -IsDotGitOrBare:$isDotGitOrBare -sw $sw
388393

389394
dbg 'Building status object' $sw
390395

391396
# This collection is used twice, so create the array just once
392397
$filesAdded = $filesAdded.ToArray()
393398

394-
$indexPaths = @(GetUniquePaths $indexAdded,$indexModified,$indexDeleted,$indexUnmerged)
395-
$workingPaths = @(GetUniquePaths $filesAdded,$filesModified,$filesDeleted,$filesUnmerged)
396-
$index = (,$indexPaths) |
399+
$indexPaths = @(GetUniquePaths $indexAdded, $indexModified, $indexDeleted, $indexUnmerged)
400+
$workingPaths = @(GetUniquePaths $filesAdded, $filesModified, $filesDeleted, $filesUnmerged)
401+
$index = (, $indexPaths) |
397402
Add-Member -Force -PassThru NoteProperty Added $indexAdded.ToArray() |
398403
Add-Member -Force -PassThru NoteProperty Modified $indexModified.ToArray() |
399404
Add-Member -Force -PassThru NoteProperty Deleted $indexDeleted.ToArray() |
400405
Add-Member -Force -PassThru NoteProperty Unmerged $indexUnmerged.ToArray()
401406

402-
$working = (,$workingPaths) |
407+
$working = (, $workingPaths) |
403408
Add-Member -Force -PassThru NoteProperty Added $filesAdded |
404409
Add-Member -Force -PassThru NoteProperty Modified $filesModified.ToArray() |
405410
Add-Member -Force -PassThru NoteProperty Deleted $filesDeleted.ToArray() |
406411
Add-Member -Force -PassThru NoteProperty Unmerged $filesUnmerged.ToArray()
407412

408413
$result = New-Object PSObject -Property @{
409-
GitDir = $GitDir
410-
RepoName = Split-Path (Split-Path $GitDir -Parent) -Leaf
411-
Branch = $branch
412-
AheadBy = $aheadBy
413-
BehindBy = $behindBy
414-
UpstreamGone = $gone
415-
Upstream = $upstream
416-
HasIndex = [bool]$index
417-
Index = $index
418-
HasWorking = [bool]$working
419-
Working = $working
420-
HasUntracked = [bool]$filesAdded
421-
StashCount = $stashCount
414+
GitDir = $GitDir
415+
RepoName = Split-Path (Split-Path $GitDir -Parent) -Leaf
416+
Branch = $branch
417+
AheadBy = $aheadBy
418+
BehindBy = $behindBy
419+
UpstreamGone = $gone
420+
Upstream = $upstream
421+
HasIndex = [bool]$index
422+
Index = $index
423+
HasWorking = [bool]$working
424+
Working = $working
425+
HasUntracked = [bool]$filesAdded
426+
StashCount = $stashCount
422427
}
423428

424429
dbg 'Finished' $sw
@@ -453,7 +458,7 @@ function InDotGitOrBareRepoDir([string][ValidateNotNullOrEmpty()]$GitDir) {
453458

454459
function Get-AliasPattern($cmd) {
455460
$aliases = @($cmd) + @(Get-Alias | Where-Object { $_.Definition -match "^$cmd(\.exe)?$" } | Foreach-Object Name)
456-
"($($aliases -join '|'))"
461+
"($($aliases -join '|'))"
457462
}
458463

459464
<#
@@ -531,16 +536,16 @@ function Get-AliasPattern($cmd) {
531536
Shows the branches, both merged and unmerged, that match the specified wildcard that would be deleted without actually deleting them. Once you've verified the list of branches looks correct, remove the WhatIf parameter to actually delete the branches.
532537
#>
533538
function Remove-GitBranch {
534-
[CmdletBinding(DefaultParameterSetName="Wildcard", SupportsShouldProcess, ConfirmImpact="Medium")]
539+
[CmdletBinding(DefaultParameterSetName = "Wildcard", SupportsShouldProcess, ConfirmImpact = "Medium")]
535540
param(
536541
# Specifies a regular expression pattern for the branches that will be deleted. Certain branches are always excluded from deletion e.g. the current branch as well as the develop and master branches. See the ExcludePattern parameter to modify that pattern.
537-
[Parameter(Position=0, Mandatory, ParameterSetName="Wildcard")]
542+
[Parameter(Position = 0, Mandatory, ParameterSetName = "Wildcard")]
538543
[ValidateNotNullOrEmpty()]
539544
[string]
540545
$Name,
541546

542547
# Specifies a regular expression for the branches that will be deleted. Certain branches are always excluded from deletion e.g. the current branch as well as the develop and master branches. See the ExcludePattern parameter to modify that pattern.
543-
[Parameter(Position=0, Mandatory, ParameterSetName="Pattern")]
548+
[Parameter(Position = 0, Mandatory, ParameterSetName = "Pattern")]
544549
[ValidateNotNull()]
545550
[string]
546551
$Pattern,
@@ -579,7 +584,7 @@ function Remove-GitBranch {
579584
$branches = git branch --merged $Commit
580585
}
581586

582-
$filteredBranches = $branches | Where-Object {$_ -notmatch $ExcludePattern }
587+
$filteredBranches = $branches | Where-Object { $_ -notmatch $ExcludePattern }
583588

584589
if ($PSCmdlet.ParameterSetName -eq "Wildcard") {
585590
$branchesToDelete = $filteredBranches | Where-Object { $_.Trim() -like $Name }
@@ -588,15 +593,16 @@ function Remove-GitBranch {
588593
$branchesToDelete = $filteredBranches | Where-Object { $_ -match $Pattern }
589594
}
590595

591-
$action = if ($DeleteForce) { "delete with force"} else { "delete" }
596+
$action = if ($DeleteForce) { "delete with force" } else { "delete" }
592597
$yesToAll = $noToAll = $false
593598

594599
foreach ($branch in $branchesToDelete) {
595600
$targetBranch = $branch.Trim()
596601
if ($PSCmdlet.ShouldProcess($targetBranch, $action)) {
597602
if ($Force -or $yesToAll -or
598-
$PSCmdlet.ShouldContinue("Are you REALLY sure you want to $action `"$targetBranch`"?",
599-
"Confirm branch deletion", [ref]$yesToAll, [ref]$noToAll)) {
603+
$PSCmdlet.ShouldContinue(
604+
"Are you REALLY sure you want to $action `"$targetBranch`"?",
605+
"Confirm branch deletion", [ref]$yesToAll, [ref]$noToAll)) {
600606

601607
if ($noToAll) { return }
602608

test/Get-GitBranch.Tests.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ Describe 'Get-GitBranch Tests' {
88
$repoRoot = (Resolve-Path $PSScriptRoot\..).Path
99
Set-Location $repoRoot\.git -ErrorAction Stop
1010
InModuleScope posh-git {
11-
Get-GitBranch | Should -BeExactly 'GIT_DIR!'
11+
InDotGitOrBareRepoDir (Get-Location) | Should -Be $true
12+
Get-GitBranch -IsDotGitOrBare | Should -BeExactly 'GIT_DIR!'
1213
}
1314
}
1415
It 'Returns correct path when in a child folder of the .git dir of the repo' {
1516
$repoRoot = (Resolve-Path $PSScriptRoot\..).Path
1617
Set-Location $repoRoot\.git\hooks -ErrorAction Stop
1718
InModuleScope posh-git {
18-
Get-GitBranch | Should -BeExactly 'GIT_DIR!'
19+
InDotGitOrBareRepoDir (Get-Location) | Should -Be $true
20+
Get-GitBranch -IsDotGitOrBare | Should -BeExactly 'GIT_DIR!'
1921
}
2022
}
2123
}

test/Get-GitStatus.Tests.ps1

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,77 @@ U test/Unmerged.Tests.ps1
440440
}
441441
}
442442

443+
Context 'Branch progress suffix' {
444+
BeforeEach {
445+
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
446+
$repoPath = NewGitTempRepo -MakeInitialCommit
447+
}
448+
AfterEach {
449+
Set-Location $PSScriptRoot
450+
RemoveGitTempRepo $repoPath
451+
}
452+
453+
It('Shows CHERRY-PICKING') {
454+
git checkout -qb test
455+
Write-Output 1 > test.txt
456+
git add test.txt
457+
git commit -qam 'first' 2> $null
458+
459+
git checkout -qb conflict
460+
Write-Output 2 > test.txt
461+
git commit -qam 'second' 2> $null
462+
463+
$status = Get-GitStatus
464+
$status.Branch | Should -Be conflict
465+
466+
git cherry-pick test
467+
468+
$status = Get-GitStatus
469+
$status.Branch | Should -Be 'conflict|CHERRY-PICKING'
470+
}
471+
472+
It('Shows MERGING') {
473+
git checkout -qb test
474+
Write-Output 1 > test.txt
475+
git add test.txt
476+
git commit -qam 'first' 2> $null
477+
478+
Write-Output 2 > test.txt
479+
git commit -qam 'second' 2> $null
480+
481+
git checkout HEAD~ -qb conflict
482+
Write-Output 3 > test.txt
483+
git commit -qam 'third' 2> $null
484+
485+
$status = Get-GitStatus
486+
$status.Branch | Should -Be conflict
487+
488+
git merge test
489+
490+
$status = Get-GitStatus
491+
$status.Branch | Should -Be 'conflict|MERGING'
492+
}
493+
494+
It('Shows REVERTING') {
495+
git checkout -qb test
496+
Write-Output 1 > test.txt
497+
git add test.txt
498+
git commit -qam 'first' 2> $null
499+
500+
git checkout -qb conflict
501+
Write-Output 2 > test.txt
502+
git commit -qam 'second' 2> $null
503+
504+
$status = Get-GitStatus
505+
$status.Branch | Should -Be conflict
506+
507+
git revert test
508+
509+
$status = Get-GitStatus
510+
$status.Branch | Should -Be 'conflict|REVERTING'
511+
}
512+
}
513+
443514
Context 'In .git' {
444515
BeforeEach {
445516
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]

0 commit comments

Comments
 (0)