diff --git a/fish/includes/git.fish b/fish/includes/git.fish index 6228362..2d9175b 100644 --- a/fish/includes/git.fish +++ b/fish/includes/git.fish @@ -61,6 +61,11 @@ function git-require-clean --description 'Require a clean git worktree' end end +function git-ref-exists --description 'Return success if a git ref exists' + test (count $argv) -eq 1; or return 1 + command git rev-parse --verify --quiet "$argv[1]" >/dev/null 2>&1 +end + function git-main-branch --description 'Print the repository main branch name' set -l remote_head (command git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null) if test -n "$remote_head" @@ -69,11 +74,11 @@ function git-main-branch --description 'Print the repository main branch name' end for branch in main master trunk develop - if command git show-ref --verify --quiet refs/heads/$branch + if git-ref-exists refs/heads/$branch printf '%s\n' "$branch" return 0 end - if command git show-ref --verify --quiet refs/remotes/origin/$branch + if git-ref-exists refs/remotes/origin/$branch printf '%s\n' "$branch" return 0 end @@ -102,7 +107,7 @@ function br --description 'Switch to an existing branch, or create one from upda set -l branch "$argv[1]" git-require-clean; or return 1 - if command git show-ref --verify --quiet "refs/heads/$branch"; or command git show-ref --verify --quiet "refs/remotes/origin/$branch" + if git-ref-exists "refs/heads/$branch"; or git-ref-exists "refs/remotes/origin/$branch" command git checkout "$branch" return $status end diff --git a/powershell.ps1 b/powershell.ps1 index fc69de5..d929e30 100644 --- a/powershell.ps1 +++ b/powershell.ps1 @@ -525,6 +525,16 @@ function git-require-clean { if (-not (Test-GitCleanWorktree)) { return 1 } } +function Test-GitRef { + param([Parameter(Mandatory = $true)][string]$Ref) + + $gitExe = Get-ExternalCommandPath git + if (-not $gitExe) { return $false } + + & $gitExe rev-parse --verify --quiet $Ref *> $null + return $LASTEXITCODE -eq 0 +} + function git-main-branch { $remoteHead = Invoke-RawGit symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>$null if ($LASTEXITCODE -eq 0 -and $remoteHead) { @@ -532,10 +542,8 @@ function git-main-branch { } foreach ($branch in @('main', 'master', 'trunk', 'develop')) { - Invoke-RawGit show-ref --verify --quiet "refs/heads/$branch" *> $null - if ($LASTEXITCODE -eq 0) { return $branch } - Invoke-RawGit show-ref --verify --quiet "refs/remotes/origin/$branch" *> $null - if ($LASTEXITCODE -eq 0) { return $branch } + if (Test-GitRef "refs/heads/$branch") { return $branch } + if (Test-GitRef "refs/remotes/origin/$branch") { return $branch } } Write-Error 'Could not determine main branch.' @@ -556,11 +564,7 @@ function br { param([Parameter(Mandatory = $true)][string]$Branch) if (-not (Test-GitCleanWorktree)) { return 1 } - Invoke-RawGit show-ref --verify --quiet "refs/heads/$Branch" *> $null - $hasLocal = $LASTEXITCODE -eq 0 - Invoke-RawGit show-ref --verify --quiet "refs/remotes/origin/$Branch" *> $null - $hasRemote = $LASTEXITCODE -eq 0 - if ($hasLocal -or $hasRemote) { + if ((Test-GitRef "refs/heads/$Branch") -or (Test-GitRef "refs/remotes/origin/$Branch")) { Invoke-RawGit checkout $Branch return $LASTEXITCODE } diff --git a/scripts/includes/later/git.zsh b/scripts/includes/later/git.zsh index 8eb1453..252713b 100644 --- a/scripts/includes/later/git.zsh +++ b/scripts/includes/later/git.zsh @@ -58,6 +58,11 @@ git-require-clean() { fi } +git-ref-exists() { + [[ $# -eq 1 ]] || return 1 + command git rev-parse --verify --quiet "$1" >/dev/null 2>&1 +} + git-main-branch() { local remote_head remote_head=$(command git symbolic-ref --quiet --short refs/remotes/origin/HEAD 2>/dev/null) @@ -68,11 +73,11 @@ git-main-branch() { local branch for branch in main master trunk develop; do - if command git show-ref --verify --quiet "refs/heads/$branch"; then + if git-ref-exists "refs/heads/$branch"; then echo "$branch" return 0 fi - if command git show-ref --verify --quiet "refs/remotes/origin/$branch"; then + if git-ref-exists "refs/remotes/origin/$branch"; then echo "$branch" return 0 fi @@ -101,7 +106,7 @@ br() { local branch="$1" git-require-clean || return 1 - if command git show-ref --verify --quiet "refs/heads/$branch" || command git show-ref --verify --quiet "refs/remotes/origin/$branch"; then + if git-ref-exists "refs/heads/$branch" || git-ref-exists "refs/remotes/origin/$branch"; then command git checkout "$branch" return $? fi