diff --git a/src/GitTabExpansion.ps1 b/src/GitTabExpansion.ps1 index afa2bc028..40cb9a441 100644 --- a/src/GitTabExpansion.ps1 +++ b/src/GitTabExpansion.ps1 @@ -101,6 +101,15 @@ function script:gitCommands($filter, $includeAliases) { $cmdList | Sort-Object } +function script:lastCheckouts($filter){ + # for performance reasons we limit the number of returned entries from reflog to avoid scanning + # all reflog entries which can be huge in some cases. Number 10 is chose as "good enough" for most cases. + git log --walk-reflogs --max-count=10 '--grep-reflog="checkout: moving from "' --fixed-strings --format=%gs | + Where-Object { $_ -match 'checkout: moving from (.*) to (.*)' } | + ForEach-Object { $Matches[1] } | + Where-Object { $_ -like "$filter*" } +} + function script:gitRemotes($filter) { git remote | Where-Object { $_ -like "$filter*" } | @@ -387,6 +396,7 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) { # Handles git checkout "^(?:checkout).* (?\S*)$" { & { + lastCheckouts $matches['ref'] gitBranches $matches['ref'] $true gitRemoteUniqueBranches $matches['ref'] gitTags $matches['ref'] diff --git a/test/TabExpansion.Tests.ps1 b/test/TabExpansion.Tests.ps1 index 99ce4656c..d718cd10b 100644 --- a/test/TabExpansion.Tests.ps1 +++ b/test/TabExpansion.Tests.ps1 @@ -187,7 +187,7 @@ Describe 'TabExpansion Tests' { Context 'Add/Reset/Checkout TabExpansion Tests' { BeforeEach { [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')] - $repoPath = NewGitTempRepo + $repoPath = NewGitTempRepo -MakeInitialCommit } AfterEach { RemoveGitTempRepo $repoPath @@ -203,6 +203,17 @@ Describe 'TabExpansion Tests' { $result = & $module GitTabExpansionInternal 'git add ' $gitStatus $result | Should BeExactly $fileName } + It 'Tab completes last checkouts first' { + &$gitbin checkout -q -b 'test-branch-b' 2>$null + &$gitbin checkout -q -b 'test-branch-c' 2>$null + &$gitbin checkout -q -b 'test-branch-a' 2>$null + &$gitbin checkout -q 'master' 2>$null + + $result = & $module GitTabExpansionInternal 'git checkout ' + $firstThreeResults = $result[0..2] + + $firstThreeResults | Should -BeExactly @('test-branch-a', 'test-branch-c', 'test-branch-b') + } } Context 'Alias TabExpansion Tests' {