Skip to content

Commit

Permalink
Merge pull request dahlbyk#855 from dahlbyk/rkeithhill/tab-complete-a…
Browse files Browse the repository at this point in the history
…lias-to-app-with-extension

Fix dahlbyk#854 - PowerShell aliases against app with .exe ext doesn't work
  • Loading branch information
dahlbyk authored Apr 14, 2021
2 parents 30a558f + 76642c9 commit b79c2dc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ $Global:GitTabSettings = New-Object PSObject -Property @{
}
EnableLogging = $false
LogPath = Join-Path ([System.IO.Path]::GetTempPath()) posh-git_tabexp.log
RegisteredCommands = ""
}

$subcommands = @{
Expand Down Expand Up @@ -301,6 +302,7 @@ function script:expandParamValues($cmd, $param, $filter) {
}

function Expand-GitCommand($Command) {
# Parse all Git output as UTF8, including tab completion output - https://github.com/dahlbyk/posh-git/pull/359
$res = Invoke-Utf8ConsoleCommand { GitTabExpansionInternal $Command $Global:GitStatus }
$res
}
Expand Down Expand Up @@ -537,10 +539,21 @@ function WriteTabExpLog([string] $Message) {

if (!$UseLegacyTabExpansion -and ($PSVersionTable.PSVersion.Major -ge 6)) {
$cmdNames = "git","tgit","gitk"

# Create regex pattern from $cmdNames: ^(git|git\.exe|tgit|tgit\.exe|gitk|gitk\.exe)$
$cmdNamesPattern = "^($($cmdNames -join '|'))(\.exe)?$"
$cmdNames += Get-Alias | Where-Object { $_.Definition -match $cmdNamesPattern } | Foreach-Object Name

if ($EnableProxyFunctionExpansion) {
$cmdNames += Get-ChildItem -Path Function:\ | Where-Object { $_.Definition -match $script:GitProxyFunctionRegex } | Select-Object -ExpandProperty 'Name'
$funcNames += Get-ChildItem -Path Function:\ | Where-Object { $_.Definition -match $script:GitProxyFunctionRegex } | Foreach-Object Name
$cmdNames += $funcNames

# Create regex pattern from $funcNames e.g.: ^(Git-Checkout|Git-Switch)$
$funcNamesPattern = "^($($funcNames -join '|'))$"
$cmdNames += Get-Alias | Where-Object { $_.Definition -match $funcNamesPattern } | Foreach-Object Name
}
$cmdNames += Get-Alias -Definition $cmdNames -ErrorAction Ignore | ForEach-Object Name

$global:GitTabSettings.RegisteredCommands = $cmdNames -join ", "

Microsoft.PowerShell.Core\Register-ArgumentCompleter -CommandName $cmdNames -Native -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)
Expand Down
2 changes: 1 addition & 1 deletion src/GitUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ function InDotGitOrBareRepoDir([string][ValidateNotNullOrEmpty()]$GitDir) {
}

function Get-AliasPattern($cmd) {
$aliases = @($cmd) + @(Get-Alias | Where-Object { $_.Definition -eq $cmd } | Select-Object -Exp Name)
$aliases = @($cmd) + @(Get-Alias | Where-Object { $_.Definition -match "^$cmd(\.exe)?$" } | Foreach-Object Name)
"($($aliases -join '|'))"
}

Expand Down
33 changes: 32 additions & 1 deletion test/TabExpansion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ Describe 'TabExpansion Tests' {
}
}

Context 'Alias TabExpansion Tests' {
Context 'Git Config Alias TabExpansion Tests' {
BeforeAll {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoPath = NewGitTempRepo -MakeInitialCommit
Expand Down Expand Up @@ -283,6 +283,37 @@ Describe 'TabExpansion Tests' {
}
}

Context 'PowerShell Alias TabExpansion Tests' {
BeforeAll {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
$repoPath = NewGitTempRepo -MakeInitialCommit
New-Alias g git -Scope Global
New-Alias ge git.exe -Scope Global
}
AfterAll {
Remove-Alias g, ge
RemoveGitTempRepo $repoPath
}
It 'Tab completes PowerShell alias specifying git (with no extension)' {
$result = & $module GitTabExpansionInternal "g check"
$result | Should -BeExactly 'checkout'

$result = & $module GitTabExpansionInternal "g checkout ma"
$result | Should -BeExactly 'master'
}
It 'Tab completes PowerShell alias specifying git.exe' {
$result = & $module GitTabExpansionInternal "ge check"
$result | Should -BeExactly 'checkout'

$result = & $module GitTabExpansionInternal "ge checkout ma"
$result | Should -BeExactly 'master'
}
It 'Get-AliasPattern finds the aliases for the given command' {
$result = & $module Get-AliasPattern git
$result | Should -BeExactly '(git|g|ge)'
}
}

Context 'PowerShell Special Chars Tests' {
BeforeAll {
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssigments', '')]
Expand Down

0 comments on commit b79c2dc

Please sign in to comment.