Skip to content

Commit 193757f

Browse files
szedergitster
authored andcommitted
completion: improve handling quoted paths in 'git ls-files's output
If any pathname contains backslash, double quote, tab, newline, or any control characters, 'git ls-files' and 'git diff-index' will enclose that pathname in double quotes and escape those special characters using C-style one-character escape sequences or \nnn octal values. This prevents those files from being listed during git-aware path completion, because due to the quoting they will never match the current word to be completed. Extend __git_index_files()'s 'awk' script to remove all that quoting and escaping from unique path components, so even paths containing (almost all) such special characters can be completed. Paths containing newline characters are still an issue, though. We use newlines as separator character when filling the COMPREPLY array, so a path with one or more newline will end up split to two or more elements in COMPREPLY, basically breaking completion. There is nothing we can do about it without a significant performance hit, so let's just ignore such paths for now. As far as paths with newlines are concerned, this isn't any different from the previous behavior, because those paths were always omitted, though in the past they were omitted because due to the quoting they didn't match the current word to be completed. Anyway, Bash's own filename completion (Meta-/) can complete even those paths, if need be. Note: - We don't dequote path components right away as they are coming in, because then we would have to dequote each directory name repeatedly, as many times as it appears in the input, i.e. as many times as the number of listed paths it contains. Instead, we dequote them at the end, as we print unique path components. - Even when a directory name itself does not contain any special characters, it will still be quoted if any of its trailing path components do. If a directory contains paths both with and without special characters, then the name of that directory will appear both quoted and unquoted in the output of 'git ls-files' and 'git diff-index'. Consequently, we will add such a directory name to the deduplicating associative array twice: once quoted and once unquoted. This means that we have to be careful after dequoting a directory name, and only print it if we haven't seen the same directory name unquoted. - It would be wonderful if we could just pass '-z' to those git commands to output \0-separated unquoted paths, and use \0 as record separator in the 'awk' script processing their output... this patch would be so much simpler, almost trivial even. Unfortunately, however, POSIX and most 'awk' implementations don't support \0 as record separator (GNU awk does support it). - This patch makes the earlier change to list paths with 'core.quotePath=false' basically redundant, because this could decode any \nnn-escaped non-ASCII character just fine, as well. However, I suspect that 'git ls-files' can deal with those non-ASCII characters faster than this updated 'awk' script; just in case someone is burdened with tons of pathnames containing non-ASCII characters. Signed-off-by: SZEDER Gábor <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c1bc0a0 commit 193757f

File tree

2 files changed

+79
-4
lines changed

2 files changed

+79
-4
lines changed

contrib/completion/git-completion.bash

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,70 @@ __git_index_files ()
461461
paths[$1] = 1
462462
}
463463
END {
464-
for (p in paths)
465-
print p
464+
for (p in paths) {
465+
if (substr(p, 1, 1) != "\"") {
466+
# No special characters, easy!
467+
print p
468+
continue
469+
}
470+
471+
# The path is quoted.
472+
p = dequote(p)
473+
if (p == "")
474+
continue
475+
476+
# Even when a directory name itself does not contain
477+
# any special characters, it will still be quoted if
478+
# any of its (stripped) trailing path components do.
479+
# Because of this we may have seen the same direcory
480+
# both quoted and unquoted.
481+
if (p in paths)
482+
# We have seen the same directory unquoted,
483+
# skip it.
484+
continue
485+
else
486+
print p
487+
}
488+
}
489+
function dequote(p, bs_idx, out, esc, esc_idx, dec) {
490+
# Skip opening double quote.
491+
p = substr(p, 2)
492+
493+
# Interpret backslash escape sequences.
494+
while ((bs_idx = index(p, "\\")) != 0) {
495+
out = out substr(p, 1, bs_idx - 1)
496+
esc = substr(p, bs_idx + 1, 1)
497+
p = substr(p, bs_idx + 2)
498+
499+
if ((esc_idx = index("abtvfr\"\\", esc)) != 0) {
500+
# C-style one-character escape sequence.
501+
out = out substr("\a\b\t\v\f\r\"\\",
502+
esc_idx, 1)
503+
} else if (esc == "n") {
504+
# Uh-oh, a newline character.
505+
# We cant reliably put a pathname
506+
# containing a newline into COMPREPLY,
507+
# and the newline would create a mess.
508+
# Skip this path.
509+
return ""
510+
} else {
511+
# Must be a \nnn octal value, then.
512+
dec = esc * 64 + \
513+
substr(p, 1, 1) * 8 + \
514+
substr(p, 2, 1)
515+
out = out sprintf("%c", dec)
516+
p = substr(p, 3)
517+
}
518+
}
519+
# Drop closing double quote, if there is one.
520+
# (There isnt any if this is a directory, as it was
521+
# already stripped with the trailing path components.)
522+
if (substr(p, length(p), 1) == "\"")
523+
out = out substr(p, 1, length(p) - 1)
524+
else
525+
out = out p
526+
527+
return out
466528
}'
467529
}
468530

t/t9902-completion.sh

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ else
15271527
say "Your filesystem does not allow \\ and \" in filenames."
15281528
rm -rf 'New\Dir'
15291529
fi
1530-
test_expect_failure FUNNYNAMES_BS_DQ \
1530+
test_expect_success FUNNYNAMES_BS_DQ \
15311531
'complete files - C-style escapes in ls-files output' '
15321532
test_when_finished "rm -r \"New\\\\Dir\"" &&
15331533
@@ -1548,7 +1548,7 @@ else
15481548
say 'Your filesystem does not allow special separator characters (FS, GS, RS, US) in filenames.'
15491549
rm -rf $'New\034Special\035Dir'
15501550
fi
1551-
test_expect_failure FUNNYNAMES_SEPARATORS \
1551+
test_expect_success FUNNYNAMES_SEPARATORS \
15521552
'complete files - \nnn-escaped control characters in ls-files output' '
15531553
test_when_finished "rm -r '$'New\034Special\035Dir''" &&
15541554
@@ -1562,6 +1562,19 @@ test_expect_failure FUNNYNAMES_SEPARATORS \
15621562
"'$'New\034Special\035Dir/New\036Special\037File''"
15631563
'
15641564

1565+
test_expect_success FUNNYNAMES_BS_DQ \
1566+
'complete files - removing repeated quoted path components' '
1567+
test_when_finished rm -rf NewDir &&
1568+
mkdir NewDir && # A dirname which in itself would not be quoted ...
1569+
>NewDir/0-file &&
1570+
>NewDir/1\"file && # ... but here the file makes the dirname quoted ...
1571+
>NewDir/2-file &&
1572+
>NewDir/3\"file && # ... and here, too.
1573+
1574+
# Still, we should only list it once.
1575+
test_completion "git test-path-comp New" "NewDir"
1576+
'
1577+
15651578

15661579
test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
15671580
test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&

0 commit comments

Comments
 (0)