-
Notifications
You must be signed in to change notification settings - Fork 391
[waiting for #1371] fix(java,rsync,scp): handle quoted space in filepaths properly #1357
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
Draft
akinomyoga
wants to merge
9
commits into
scop:main
Choose a base branch
from
akinomyoga:_comp_xfunc_scp_compgen_local_files
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+356
−72
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
07ac056
[waiting for PR1371]
akinomyoga 6bc12ac
refactor(scp): factorize common codes
akinomyoga cf59417
fix(java,rsync,scp): handle quoted space in filepaths properly
akinomyoga 6ddd018
test(scp): leave comments for a failing test case
akinomyoga 11e318e
fix(_comp_dequote): set the literal value to REPLY as a fallback
akinomyoga 4eddf94
fix(java,rsync,ssh): complete syntactically incomplete cur
akinomyoga c448883
refactor(ssh): accept dironly flag as an option
akinomyoga 95c1077
test(dequote{,_incomplete}): use raw stringliteral r""
akinomyoga c8de159
fix(scp): work around incomplete triple backslashes for remote paths
akinomyoga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -188,11 +188,11 @@ _comp_dequote__initialize() | |
_comp_dequote__initialize | ||
|
||
# This function expands a word using `eval` in a safe way. This function can | ||
# be typically used to get the expanded value of `${word[i]}` as | ||
# `_comp_dequote "${word[i]}"`. When the word contains unquoted shell special | ||
# characters, command substitutions, and other unsafe strings, the function | ||
# call fails before applying `eval`. Otherwise, `eval` is applied to the | ||
# string to generate the result. | ||
# be typically used to get the expanded value of `${word[i]}` as `_comp_dequote | ||
# "${word[i]}"`. When the word contains unquoted shell special characters, | ||
# command substitutions, and other unsafe strings, the function call fails | ||
# before applying `eval` and REPLY is set to be the literal string. Otherwise, | ||
# `eval` is applied to the string to generate the result. | ||
# | ||
# @param $1 String to be expanded. A safe word consists of the following | ||
# sequence of substrings: | ||
|
@@ -207,7 +207,12 @@ _comp_dequote__initialize | |
# quotations, parameter expansions are allowed. | ||
# | ||
# @var[out] REPLY Array that contains the expanded results. Multiple words or | ||
# no words may be generated through pathname expansions. | ||
# no words may be generated through pathname expansions. If | ||
# $1 is not a safe word, REPLY contains the literal value of | ||
# $1. | ||
# | ||
# @return 0 if $1 is a safe word and the expansion result contains one word at | ||
# least, or 1 otherwise. | ||
# | ||
# Note: This function allows parameter expansions as safe strings, which might | ||
# cause unexpected results: | ||
|
@@ -235,8 +240,37 @@ _comp_dequote__initialize | |
_comp_dequote() | ||
{ | ||
REPLY=() # fallback value for unsafe word and failglob | ||
[[ $1 =~ $_comp_dequote__regex_safe_word ]] || return 1 | ||
eval "REPLY=($1)" 2>/dev/null # may produce failglob | ||
if [[ ${1-} =~ $_comp_dequote__regex_safe_word ]]; then | ||
eval "REPLY=($1)" 2>/dev/null # may produce failglob | ||
((${#REPLY[@]} > 0)) | ||
return "$?" | ||
else | ||
# shellcheck disable=SC2178 | ||
REPLY=${1-} | ||
return 1 | ||
fi | ||
} | ||
|
||
# Try to reconstruct an incomplete word and apply _comp_dequote. | ||
# @param $1 String to be expanded. The same as _comp_dequote, but | ||
# incomplete backslash, single quotation, and double quotation | ||
# are allowed. | ||
# @var[out] REPLY Result. The same as _comp_dequote. | ||
# @since 2.17 | ||
_comp_dequote_incomplete() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a unit test for this will be nice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the suggestion! I added tests in dcf662b. |
||
{ | ||
local _word=${1-} | ||
if ! [[ $_word =~ $_comp_dequote__regex_safe_word ]]; then | ||
# shellcheck disable=SC1003 | ||
if [[ ${_word%'\'} =~ $_comp_dequote__regex_safe_word ]]; then | ||
_word=${_word%'\'} | ||
elif [[ $_word\' =~ $_comp_dequote__regex_safe_word ]]; then | ||
_word=$_word\' | ||
elif [[ $_word\" =~ $_comp_dequote__regex_safe_word ]]; then | ||
_word=$_word\" | ||
fi | ||
fi | ||
_comp_dequote "$_word" | ||
} | ||
|
||
# Unset the given variables across a scope boundary. Useful for unshadowing | ||
|
@@ -1559,7 +1593,7 @@ _comp_compgen_help__get_help_lines() | |
--) shift 1 ;& | ||
*) | ||
local REPLY | ||
_comp_dequote "${comp_args[0]-}" || REPLY=${comp_args[0]-} | ||
_comp_dequote "${comp_args[0]-}" | ||
help_cmd=("${REPLY:-false}" "$@") | ||
;; | ||
esac | ||
|
@@ -2855,8 +2889,8 @@ _comp_command_offset() | |
if ((COMP_CWORD == 0)); then | ||
_comp_compgen_commands | ||
else | ||
_comp_dequote "${COMP_WORDS[0]}" || REPLY=${COMP_WORDS[0]} | ||
local cmd=$REPLY compcmd=$REPLY | ||
_comp_dequote "${COMP_WORDS[0]}" | ||
local cmd=${REPLY-} compcmd=${REPLY-} | ||
local cspec=$(complete -p -- "$cmd" 2>/dev/null) | ||
|
||
# If we have no completion for $cmd yet, see if we have for basename | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change seems reasonable to me, do you think this might cause breakage in external uses of the function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that is a good point.
After this change, the cases where it failed will continue to fail. Some of the cases where it succeeded and produced an empty result [e.g.,
REPLY=()
for_comp_dequote '$empty_variable'
] will start to fail after this change. Since the caller should have considered failing cases even with the currentmain
branch, I expect that increasing another case of failing wouldn't cause a problem.Rather, an empty
REPLY=()
would have caused a problem when the caller uses$REPLY
after_comp_dequote
succeeds.Another change is in the value of
REPLY
when_comp_dequote
failed. SinceREPLY
was not supposed to be used when_comp_dequote
failed (REPLY
was actually empty,REPLY=()
), I expect the change inREPLY
in the failing case wouldn't cause a problem, though I admit that there might be a case that the user expects the emptyREPLY
in principle. I think this change would rather improve the compatibility in the case where the caller isn't careful enough to check the exit status: