Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Get-GitBranch invoke git only once #472

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 47 additions & 44 deletions src/GitUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@ function Get-GitBranch($gitDir = $(Get-GitDirectory), [Diagnostics.Stopwatch]$sw
Invoke-Utf8ConsoleCommand {
dbg 'Finding branch' $sw
$r = ''; $b = ''; $c = ''
dbg 'Running git rev-parse' $sw;
$GitOutput = $(git rev-parse --symbolic-full-name --abbrev-ref --is-inside-git-dir --is-bare-repository HEAD 2>$null)
$IsInsideGitDir = 'true' -eq $GitOutput[0]
$IsBareRepository = 'true' -eq $GitOutput[1]
$GitRefName = $GitOutput[2]
if (Test-Path $gitDir\rebase-merge\interactive) {
dbg 'Found rebase-merge\interactive' $sw
$r = '|REBASE-i'
$b = "$(Get-Content $gitDir\rebase-merge\head-name)"
$b = "$(Get-Content $gitDir\rebase-merge\head-name)" -replace 'refs/heads/', ''
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't notice this before, but it's more correct to only replace that as a prefix: ^refs/heads/.

}
elseif (Test-Path $gitDir\rebase-merge) {
dbg 'Found rebase-merge' $sw
$r = '|REBASE-m'
$b = "$(Get-Content $gitDir\rebase-merge\head-name)"
$b = "$(Get-Content $gitDir\rebase-merge\head-name)" -replace 'refs/heads/', ''
}
else {
if (Test-Path $gitDir\rebase-apply) {
Expand Down Expand Up @@ -104,56 +109,54 @@ function Get-GitBranch($gitDir = $(Get-GitDirectory), [Diagnostics.Stopwatch]$sw
$r = '|BISECTING'
}

$b = Invoke-NullCoalescing `
{ dbg 'Trying symbolic-ref' $sw; git symbolic-ref HEAD -q 2>$null } `
{ '({0})' -f (Invoke-NullCoalescing `
{
dbg 'Trying describe' $sw
switch ($Global:GitPromptSettings.DescribeStyle) {
'contains' { git describe --contains HEAD 2>$null }
'branch' { git describe --contains --all HEAD 2>$null }
'describe' { git describe HEAD 2>$null }
default { git tag --points-at HEAD 2>$null }
}
} `
{
dbg 'Falling back on parsing HEAD' $sw
$ref = $null

if (Test-Path $gitDir\HEAD) {
dbg 'Reading from .git\HEAD' $sw
$ref = Get-Content $gitDir\HEAD 2>$null
}
else {
dbg 'Trying rev-parse' $sw
$ref = git rev-parse HEAD 2>$null
}

if ($ref -match 'ref: (?<ref>.+)') {
return $Matches['ref']
}
elseif ($ref -and $ref.Length -ge 7) {
return $ref.Substring(0,7)+'...'
}
else {
return 'unknown'
}
}
) }
if ('HEAD' -eq $GitRefName) {
$b = '({0})' -f (Invoke-NullCoalescing `
{
dbg 'Trying describe' $sw
switch ($Global:GitPromptSettings.DescribeStyle) {
'contains' { git describe --contains HEAD 2>$null }
'branch' { git describe --contains --all HEAD 2>$null }
'describe' { git describe HEAD 2>$null }
default { git tag --points-at HEAD 2>$null }
}
} `
{
dbg 'Falling back on parsing HEAD' $sw
$ref = $null

if (Test-Path $gitDir\HEAD) {
dbg 'Reading from .git\HEAD' $sw
$ref = Get-Content $gitDir\HEAD 2>$null
if ($ref -match 'ref: (?<ref>.+)') {
return $Matches['ref'] -replace 'refs/heads/', ''
}
elseif ($ref -and $ref.Length -ge 7) {
return $ref.Substring(0,7)+'...'
}
else {
return 'unknown'
}
}
else {
return 'unknown'
}
}
)
}
else {
$b = $GitRefName
}
}

dbg 'Inside git directory?' $sw
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you get rid of this dbg call and the next one (line 147)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find it (or the other) useful anymore since it was no longer marking a part of the code that takes any time, but I'm happy to put it back if you prefer.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to put it back; this dbg is essentially replaced by the one on L68.

if ('true' -eq $(git rev-parse --is-inside-git-dir 2>$null)) {
dbg 'Inside git directory' $sw
if ('true' -eq $(git rev-parse --is-bare-repository 2>$null)) {
if ($IsInsideGitDir) {
if ($IsBareRepository) {
$c = 'BARE:'
}
else {
$b = 'GIT_DIR!'
}
}

"$c$($b -replace 'refs/heads/','')$r"
"$c$b$r"
}
}

Expand Down