Skip to content

Commit c8de159

Browse files
committed
fix(scp): work around incomplete triple backslashes for remote paths
1 parent 95c1077 commit c8de159

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

completions/ssh

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,21 @@ _comp_xfunc_scp_compgen_remote_files()
544544
local REPLY=$cur
545545
if [[ ! $_less_escaping ]]; then
546546
# unescape (3 backslashes to 1 for chars we escaped)
547-
REPLY=$(command sed -e 's/\\\\\\\('"$_comp_cmd_scp__path_esc"'\)/\\\1/g' <<<"$REPLY")
547+
#
548+
# Note: We want to do the following, but POSIX BRE does not support \|:
549+
#
550+
# REPLY=$(command sed -e 's/\\\\\\\('"$_comp_cmd_scp__path_esc"'\|$\)/\\\1/g' <<<"$REPLY")
551+
#
552+
# Note: We need to store \\\\\\ in a variable to work around "shopt -s
553+
# compat31".
554+
local _tail=$REPLY _regex_triple_backslashes='\\\\\\('$_comp_cmd_scp__path_esc'|$)(.*)$'
555+
REPLY=
556+
while [[ $_tail && $_tail =~ $_regex_triple_backslashes ]]; do
557+
# shellcheck disable=SC1003
558+
REPLY=${_tail::${#_tail}-${#BASH_REMATCH}}'\'${BASH_REMATCH[1]}
559+
_tail=${BASH_REMATCH[2]}
560+
done
561+
REPLY+=$_tail
548562
fi
549563
_comp_dequote_incomplete "$REPLY"
550564
local cur_val=${REPLY-}

test/t/test_scp.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ def test_remote_path_with_backslash(self, bash):
140140
[r"abc\ def.txt", r"abc\\\ def.txt"]
141141
) or completion == sorted([r"abc\\\ def.txt", r"abc\\\\\\\ def.txt"])
142142

143+
def test_remote_path_with_backslash_2(self, bash):
144+
assert_bash_exec(
145+
bash, r"ssh() { [[ $1 == abc ]]; printf '%s\n' 'abc OK'; }"
146+
)
147+
completion = assert_complete(bash, "scp remote_host:abc\\\\\\")
148+
assert_bash_exec(bash, "unset -f ssh")
149+
150+
assert completion == "OK"
151+
143152
def test_xfunc_remote_files(self, bash):
144153
with bash_env_saved(bash) as bash_env:
145154
bash_env.save_variable("COMPREPLY")

0 commit comments

Comments
 (0)