@@ -70,6 +70,76 @@ function Convert-ToPowerShellBranchCompletionText {
7070 return $BranchName
7171}
7272
73+ function Get-GitBranchCompletionPrefix {
74+ param (
75+ [AllowEmptyString ()]
76+ [string ]$WordToComplete
77+ )
78+
79+ $prefix = $WordToComplete.Trim (" '" , ' "' )
80+ if ($prefix.StartsWith (' `' )) {
81+ $prefix = $prefix.Substring (1 )
82+ }
83+
84+ return $prefix
85+ }
86+
87+ function Get-GitBranchNames {
88+ if (-not (Test-InGitRepo )) {
89+ return @ ()
90+ }
91+
92+ try {
93+ return @ (
94+ git branch - a -- format= ' %(refname:short)' |
95+ ForEach-Object { $_.Trim () } |
96+ Where-Object { -not [string ]::IsNullOrWhiteSpace($_ ) } |
97+ ForEach-Object { $_ -replace ' ^remotes/origin/' , ' ' } |
98+ Where-Object { $_ -ne ' HEAD' } |
99+ Sort-Object - Unique
100+ )
101+ } catch {
102+ return @ ()
103+ }
104+ }
105+
106+ function Select-GitBranchesForCompletion {
107+ param (
108+ [AllowEmptyCollection ()]
109+ [string []]$Branches ,
110+ [AllowEmptyString ()]
111+ [string ]$Prefix
112+ )
113+
114+ if (-not $Branches -or $Branches.Count -eq 0 ) {
115+ return @ ()
116+ }
117+
118+ if ([string ]::IsNullOrWhiteSpace($Prefix )) {
119+ return @ ($Branches )
120+ }
121+
122+ $startsWithMatches = @ ()
123+ $containsMatches = @ ()
124+ foreach ($entry in $Branches ) {
125+ $branchName = [string ]$entry
126+ if ([string ]::IsNullOrWhiteSpace($branchName )) {
127+ continue
128+ }
129+
130+ if ($branchName.StartsWith ($Prefix , [System.StringComparison ]::OrdinalIgnoreCase)) {
131+ $startsWithMatches += $branchName
132+ continue
133+ }
134+
135+ if ($branchName.IndexOf ($Prefix , [System.StringComparison ]::OrdinalIgnoreCase) -ge 0 ) {
136+ $containsMatches += $branchName
137+ }
138+ }
139+
140+ return @ ($startsWithMatches + $containsMatches )
141+ }
142+
73143function Get-GitLongOptionCompletions {
74144 param (
75145 [Parameter (Mandatory = $true )]
@@ -272,28 +342,11 @@ function Get-GitBranchCompletions {
272342 [string ]$WordToComplete
273343 )
274344
275- if (-not (Test-InGitRepo )) {
276- return @ ()
277- }
278-
279- $prefix = $WordToComplete.Trim (" '" , ' "' )
280- if ($prefix.StartsWith (' `' )) {
281- $prefix = $prefix.Substring (1 )
282- }
283-
284345 try {
285- $branches = @ (
286- git branch - a -- format= ' %(refname:short)' |
287- ForEach-Object { $_.Trim () } |
288- Where-Object { -not [string ]::IsNullOrWhiteSpace($_ ) } |
289- ForEach-Object { $_ -replace ' ^remotes/origin/' , ' ' } |
290- Where-Object { $_ -ne ' HEAD' } |
291- Sort-Object - Unique
292- )
293-
294- if (-not [string ]::IsNullOrWhiteSpace($prefix )) {
295- $branches = @ ($branches | Where-Object { $_ -like " $prefix *" })
296- }
346+ $prefix = Get-GitBranchCompletionPrefix - WordToComplete $WordToComplete
347+ $branches = Select-GitBranchesForCompletion `
348+ - Branches (Get-GitBranchNames ) `
349+ - Prefix $prefix
297350
298351 if (-not $branches -or $branches.Count -eq 0 ) {
299352 return @ ()
@@ -1091,6 +1144,14 @@ function Register-GitAliasCompletion {
10911144 }
10921145 }
10931146
1147+ if ($primarySubCommand -in @ (' checkout' , ' switch' , ' merge' , ' rebase' , ' branch' , ' reset' , ' revert' ) -and
1148+ $wordToComplete -notlike ' -*' ) {
1149+ $branchCompletions = Get-GitBranchCompletions - WordToComplete $wordToComplete
1150+ if ($branchCompletions -and $branchCompletions.Count -gt 0 ) {
1151+ return $branchCompletions
1152+ }
1153+ }
1154+
10941155 if ($wordToComplete -like ' -*' ) {
10951156 $optionCompletions = Get-GitLongOptionCompletions - SubCommandLine $subCommandLine - WordToComplete $wordToComplete
10961157 if ($optionCompletions -and $optionCompletions.Count -gt 0 ) {
@@ -1134,16 +1195,9 @@ function Register-GitAliasCompletion {
11341195 # Final fallback when delegated completion is unavailable
11351196 if ($primarySubCommand -in @ (' checkout' , ' switch' , ' merge' , ' rebase' , ' branch' , ' reset' , ' revert' )) {
11361197 try {
1137- $branches = git branch - a -- format= ' %(refname:short)' |
1138- ForEach-Object { $_ -replace ' ^remotes/origin/' , ' ' } |
1139- Sort-Object - Unique |
1140- Where-Object { $_ -like " $wordToComplete *" }
1141- if ($branches ) {
1142- return $branches | ForEach-Object {
1143- $branchName = $_
1144- $safeText = Convert-ToPowerShellBranchCompletionText - BranchName $branchName
1145- [System.Management.Automation.CompletionResult ]::new($safeText , $branchName , ' ParameterValue' , $branchName )
1146- }
1198+ $branchCompletions = Get-GitBranchCompletions - WordToComplete $wordToComplete
1199+ if ($branchCompletions -and $branchCompletions.Count -gt 0 ) {
1200+ return $branchCompletions
11471201 }
11481202 } catch {
11491203 return @ ()
0 commit comments