Skip to content

Commit

Permalink
Merge pull request #403 from dahlbyk/quotepath
Browse files Browse the repository at this point in the history
Fix tab expansion for non-ASCII file paths
  • Loading branch information
dahlbyk authored Feb 7, 2017
2 parents 28574ad + 5778065 commit 2f92ba6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
28 changes: 14 additions & 14 deletions src/GitTabExpansion.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,19 @@ function script:gitFiles($filter, $files) {
ForEach-Object { if ($_ -like '* *') { "'$_'" } else { $_ } }
}

function script:gitIndex($filter) {
function script:gitIndex($GitStatus, $filter) {
gitFiles $filter $GitStatus.Index
}

function script:gitAddFiles($filter) {
function script:gitAddFiles($GitStatus, $filter) {
gitFiles $filter (@($GitStatus.Working.Unmerged) + @($GitStatus.Working.Modified) + @($GitStatus.Working.Added))
}

function script:gitCheckoutFiles($filter) {
function script:gitCheckoutFiles($GitStatus, $filter) {
gitFiles $filter (@($GitStatus.Working.Unmerged) + @($GitStatus.Working.Modified) + @($GitStatus.Working.Deleted))
}

function script:gitDiffFiles($filter, $staged) {
function script:gitDiffFiles($GitStatus, $filter, $staged) {
if ($staged) {
gitFiles $filter $GitStatus.Index.Modified
}
Expand All @@ -157,11 +157,11 @@ function script:gitDiffFiles($filter, $staged) {
}
}

function script:gitMergeFiles($filter) {
function script:gitMergeFiles($GitStatus, $filter) {
gitFiles $filter $GitStatus.Working.Unmerged
}

function script:gitDeleted($filter) {
function script:gitDeleted($GitStatus, $filter) {
gitFiles $filter $GitStatus.Working.Deleted
}

Expand Down Expand Up @@ -207,11 +207,11 @@ function script:expandParamValues($cmd, $param, $filter) {
}

function GitTabExpansion($lastBlock) {
$res = Invoke-Utf8ConsoleCommand { GitTabExpansionInternal $lastBlock }
$res = Invoke-Utf8ConsoleCommand { GitTabExpansionInternal $lastBlock $Global:GitStatus }
$res
}

function GitTabExpansionInternal($lastBlock) {
function GitTabExpansionInternal($lastBlock, $GitStatus = $null) {
$ignoreGitParams = '(?<params>\s+-(?:[aA-zZ0-9]+|-[aA-zZ0-9][aA-zZ0-9-]*)(?:=\S+)?)*'

if ($lastBlock -match "^$(Get-AliasPattern git) (?<cmd>\S+)(?<args> .*)$") {
Expand Down Expand Up @@ -306,7 +306,7 @@ function GitTabExpansionInternal($lastBlock) {
# Handles git reset HEAD <path>
# Handles git reset HEAD -- <path>
"^reset.* HEAD(?:\s+--)? (?<path>\S*)$" {
gitIndex $matches['path']
gitIndex $GitStatus $matches['path']
}

# Handles git <cmd> <ref>
Expand All @@ -316,27 +316,27 @@ function GitTabExpansionInternal($lastBlock) {

# Handles git add <path>
"^add.* (?<files>\S*)$" {
gitAddFiles $matches['files']
gitAddFiles $GitStatus $matches['files']
}

# Handles git checkout -- <path>
"^checkout.* -- (?<files>\S*)$" {
gitCheckoutFiles $matches['files']
gitCheckoutFiles $GitStatus $matches['files']
}

# Handles git rm <path>
"^rm.* (?<index>\S*)$" {
gitDeleted $matches['index']
gitDeleted $GitStatus $matches['index']
}

# Handles git diff/difftool <path>
"^(?:diff|difftool)(?:.* (?<staged>(?:--cached|--staged))|.*) (?<files>\S*)$" {
gitDiffFiles $matches['files'] $matches['staged']
gitDiffFiles $GitStatus $matches['files'] $matches['staged']
}

# Handles git merge/mergetool <path>
"^(?:merge|mergetool).* (?<files>\S*)$" {
gitMergeFiles $matches['files']
gitMergeFiles $GitStatus $matches['files']
}

# Handles git checkout <ref>
Expand Down
2 changes: 1 addition & 1 deletion src/GitUtils.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ function Get-GitStatus($gitDir = (Get-GitDirectory)) {
if ($cacheResponse.State) { $branch += "|" + $cacheResponse.State }
} else {
dbg 'Getting status' $sw
$status = Invoke-Utf8ConsoleCommand { git -c color.status=false status --short --branch 2>$null }
$status = Invoke-Utf8ConsoleCommand { git -c core.quotepath=false -c color.status=false status --short --branch 2>$null }
if($settings.EnableStashStatus) {
dbg 'Getting stash count' $sw
$stashCount = $null | git stash list 2>$null | measure-object | Select-Object -expand Count
Expand Down
28 changes: 28 additions & 0 deletions test/TabExpansion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,32 @@ Describe 'TabExpansion Tests' {
}
}
}
Context 'Add/Reset/Checkout TabExpansion Tests' {
BeforeEach {
$origPath = Get-Location
$temp = [System.IO.Path]::GetTempPath()
$repoPath = Join-Path $temp ([IO.Path]::GetRandomFileName())

git init $repoPath
Set-Location $repoPath
}
AfterEach {
Set-Location $origPath
if (Test-Path $repoPath) {
Remove-Item $repoPath -Recurse -Force
}
}
It 'Tab completes non-ASCII file name' {
git config core.quotepath true # Problematic (default) config

$fileName = "posh$([char]8226)git.txt"
New-Item $fileName

$GitStatus = & $module Get-GitStatus

$result = & $module GitTabExpansionInternal 'git add ' $GitStatus

$result | Should BeExactly $fileName
}
}
}

0 comments on commit 2f92ba6

Please sign in to comment.