Skip to content

Commit

Permalink
🚸 feat(Invoke-GitCheckoutNewBranch): sort the branch options when cho…
Browse files Browse the repository at this point in the history
…osing a remote branch to track
  • Loading branch information
capdiem committed Jul 28, 2022
1 parent ce30d97 commit f795c48
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
2 changes: 1 addition & 1 deletion PSGitUtils/PSGitUtils.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'PSGitUtils.psm1'

# Version number of this module.
ModuleVersion = '1.13.2'
ModuleVersion = '1.13.3'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down
22 changes: 17 additions & 5 deletions PSGitUtils/PSGitUtils.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ function Invoke-GitCheckoutNewBranch {
[string[]]$originBranches = Get-GitOriginBranches -onlyName
[char[]]$existChars = @('n')

$charBranchDict = @() # char:branch key:value

for ($i = 0; $i -lt $originBranches.Count; $i++) {
$originBranch = $originBranches[$i]

Expand All @@ -411,17 +413,27 @@ function Invoke-GitCheckoutNewBranch {

if ($originBranchCharIndex -ne -1) {
$originBranch = $originBranch.Insert(($originBranchCharIndex + $typeLength), '&')
$charBranchDict += @{k = $existChars[-1]; v = 'origin/' + $originBranch }
} else {
$item = 'origin/' + $originBranch
$originBranchOptions += $item
}

$item = 'origin/' + $originBranch
$originBranchOptions += $item
}

$originBranchOptions = ($charBranchDict | Sort-Object -Property k | Select-Object -Property v).v + $originBranchOptions

$originBranchOptions += "&notrack";

$originBranchIndex = (Get-Host).UI.PromptForChoice("", "${step}. Please choose a remote branch to track", $originBranchOptions, $originBranchOptions.Count - 1)

if ($originBranchIndex -lt $originBranches.Count) {
$startPoint = "origin/$($originBranches[$originBranchIndex])"
if ($originBranchIndex -eq $originBranchOptions.Count - 1) {
git checkout -b $branch
return
}

$startPoint = $originBranchOptions[$originBranchIndex].Label
if ($startPoint.Contains('&')) {
$startPoint = $startPoint.Replace('&', '')
}

git checkout -b $branch $startPoint
Expand Down

0 comments on commit f795c48

Please sign in to comment.