Skip to content

Commit

Permalink
Always call Get-GitBranch, but use existing if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlbyk committed Mar 27, 2022
1 parent 8e73aa2 commit 66efbc4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
37 changes: 21 additions & 16 deletions src/GitUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function Get-GitDirectory {
}
}

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

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

if ($step -and $total) {
$r += " $step/$total"
}

$b = Invoke-NullCoalescing `
$b `
$branch `
{ dbg 'Trying symbolic-ref' $sw; git --no-optional-locks symbolic-ref HEAD -q 2>$null } `
{ '({0})' -f (Invoke-NullCoalescing `
{
Expand Down Expand Up @@ -152,23 +158,21 @@ function Get-GitBranch($gitDir = $(Get-GitDirectory), [Diagnostics.Stopwatch]$sw
) }
}

dbg 'Inside git directory?' $sw
$revParseOut = git --no-optional-locks rev-parse --is-inside-git-dir 2>$null
if ('true' -eq $revParseOut) {
dbg 'Inside git directory' $sw
$revParseOut = git --no-optional-locks rev-parse --is-bare-repository 2>$null
if ($isDotGitOrBare -or !$b) {
dbg 'Inside git directory?' $sw
$revParseOut = git --no-optional-locks rev-parse --is-inside-git-dir 2>$null
if ('true' -eq $revParseOut) {
$c = 'BARE:'
}
else {
$b = 'GIT_DIR!'
dbg 'Inside git directory' $sw
$revParseOut = git --no-optional-locks rev-parse --is-bare-repository 2>$null
if ('true' -eq $revParseOut) {
$c = 'BARE:'
}
else {
$b = 'GIT_DIR!'
}
}
}

if ($step -and $total) {
$r += " $step/$total"
}

"$c$($b -replace 'refs/heads/','')$r"
}
}
Expand Down Expand Up @@ -266,7 +270,8 @@ function Get-GitStatus {
$stashCount = 0

$fileStatusEnabled = $Force -or $settings.EnableFileStatus
if ($fileStatusEnabled -and !$(InDotGitOrBareRepoDir $GitDir) -and !$(InDisabledRepository)) {
# Optimization: short-circuit to avoid InDotGitOrBareRepoDir and InDisabledRepository if !$fileStatusEnabled
if ($fileStatusEnabled -and !$($isDotGitOrBare = InDotGitOrBareRepoDir $GitDir) -and !$(InDisabledRepository)) {
if ($null -eq $settings.EnableFileStatusFromCache) {
$settings.EnableFileStatusFromCache = $null -ne (Get-Module GitStatusCachePoshClient)
}
Expand Down Expand Up @@ -384,7 +389,7 @@ function Get-GitStatus {
}
}

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

dbg 'Building status object' $sw

Expand Down
6 changes: 4 additions & 2 deletions test/Get-GitBranch.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ Describe 'Get-GitBranch Tests' {
$repoRoot = (Resolve-Path $PSScriptRoot\..).Path
Set-Location $repoRoot\.git -ErrorAction Stop
InModuleScope posh-git {
Get-GitBranch | Should -BeExactly 'GIT_DIR!'
InDotGitOrBareRepoDir (Get-Location) | Should -Be $true
Get-GitBranch -IsDotGitOrBare | Should -BeExactly 'GIT_DIR!'
}
}
It 'Returns correct path when in a child folder of the .git dir of the repo' {
$repoRoot = (Resolve-Path $PSScriptRoot\..).Path
Set-Location $repoRoot\.git\hooks -ErrorAction Stop
InModuleScope posh-git {
Get-GitBranch | Should -BeExactly 'GIT_DIR!'
InDotGitOrBareRepoDir (Get-Location) | Should -Be $true
Get-GitBranch -IsDotGitOrBare | Should -BeExactly 'GIT_DIR!'
}
}
}
Expand Down

0 comments on commit 66efbc4

Please sign in to comment.