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

Add last checkouts to tabcompletion #641

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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*" } |
Expand Down Expand Up @@ -387,6 +396,7 @@ function GitTabExpansionInternal($lastBlock, $GitStatus = $null) {
# Handles git checkout <ref>
"^(?:checkout).* (?<ref>\S*)$" {
& {
lastCheckouts $matches['ref']
gitBranches $matches['ref'] $true
gitRemoteUniqueBranches $matches['ref']
gitTags $matches['ref']
Expand Down
13 changes: 12 additions & 1 deletion test/TabExpansion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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' {
Expand Down