scmb_expand_args: return an array to fix quoting issues
This commit is contained in:
@@ -79,8 +79,8 @@ git_add_shortcuts() {
|
|||||||
git_silent_add_shortcuts() {
|
git_silent_add_shortcuts() {
|
||||||
if [ -n "$1" ]; then
|
if [ -n "$1" ]; then
|
||||||
# Expand args and process resulting set of files.
|
# Expand args and process resulting set of files.
|
||||||
IFS=$'\t'
|
eval "args=$(scmb_expand_args "$@")" # create $args array
|
||||||
for file in $(scmb_expand_args "$@"); do
|
for file in "${args[@]}"; do
|
||||||
# Use 'git rm' if file doesn't exist and 'ga_auto_remove' is enabled.
|
# Use 'git rm' if file doesn't exist and 'ga_auto_remove' is enabled.
|
||||||
if [[ $ga_auto_remove == "yes" ]] && ! [ -e "$file" ]; then
|
if [[ $ga_auto_remove == "yes" ]] && ! [ -e "$file" ]; then
|
||||||
echo -n "# "
|
echo -n "# "
|
||||||
@@ -90,7 +90,6 @@ git_silent_add_shortcuts() {
|
|||||||
echo -e "# Added '$file'"
|
echo -e "# Added '$file'"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
unset IFS
|
|
||||||
echo "#"
|
echo "#"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
@@ -121,32 +120,29 @@ scmb_expand_args() {
|
|||||||
shift
|
shift
|
||||||
fi
|
fi
|
||||||
|
|
||||||
first=1
|
local -a args=() # initially empty array
|
||||||
OLDIFS="$IFS"; IFS=" " # We need to split on spaces to loop over expanded range
|
|
||||||
for arg in "$@"; do
|
for arg in "$@"; do
|
||||||
if [[ "$arg" =~ ^[0-9]{0,4}$ ]] ; then # Substitute $e{*} variables for any integers
|
if [[ "$arg" =~ ^[0-9]{0,4}$ ]] ; then # Substitute $e{*} variables for any integers
|
||||||
if [ "$first" -eq 1 ]; then first=0; else printf '\t'; fi
|
|
||||||
if [ -e "$arg" ]; then
|
if [ -e "$arg" ]; then
|
||||||
# Don't expand files or directories with numeric names
|
# Don't expand files or directories with numeric names
|
||||||
printf '%s' "$arg"
|
args+=("$arg")
|
||||||
else
|
else
|
||||||
_print_path "$relative" "$git_env_char$arg"
|
args+=("$(_print_path "$relative" "$git_env_char$arg")")
|
||||||
fi
|
fi
|
||||||
elif [[ "$arg" =~ ^[0-9]+-[0-9]+$ ]]; then # Expand ranges into $e{*} variables
|
elif [[ "$arg" =~ ^[0-9]+-[0-9]+$ ]]; then # Expand ranges into $e{*} variables
|
||||||
|
|
||||||
for i in $(eval echo {${arg/-/..}}); do
|
for i in $(eval echo {${arg/-/..}}); do
|
||||||
if [ "$first" -eq 1 ]; then first=0; else printf '\t'; fi
|
args+=("$(_print_path "$relative" "$git_env_char$i")")
|
||||||
_print_path "$relative" "$git_env_char$i"
|
|
||||||
done
|
done
|
||||||
else # Otherwise, treat $arg as a normal string.
|
else # Otherwise, treat $arg as a normal string.
|
||||||
if [ "$first" -eq 1 ]; then first=0; else printf '\t'; fi
|
args+=("$arg")
|
||||||
printf '%s' "$arg"
|
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
IFS="$OLDIFS"
|
args=$(declare -p args) # Get $args array as a string which can be `eval`-ed to recreate itself
|
||||||
|
args=${args#*=} # Remove `typeset -a args=` from beginning of string to allow caller to name variable
|
||||||
|
echo "$args"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Expand a variable into a (possibly relative) pathname
|
# Expand a variable (named by $2) into a (possibly relative) pathname
|
||||||
_print_path() {
|
_print_path() {
|
||||||
local pathname=$(eval printf '%s' "\"\${$2}\"")
|
local pathname=$(eval printf '%s' "\"\${$2}\"")
|
||||||
if [ "$1" = 1 ]; then # print relative
|
if [ "$1" = 1 ]; then # print relative
|
||||||
@@ -158,7 +154,8 @@ _print_path() {
|
|||||||
# Execute a command with expanded args, e.g. Delete files 6 to 12: $ ge rm 6-12
|
# Execute a command with expanded args, e.g. Delete files 6 to 12: $ ge rm 6-12
|
||||||
# Fails if command is a number or range (probably not worth fixing)
|
# Fails if command is a number or range (probably not worth fixing)
|
||||||
exec_scmb_expand_args() {
|
exec_scmb_expand_args() {
|
||||||
eval "$(scmb_expand_args "$@" | sed -e "s/\([][|;()<>^ \"'&]\)/"'\\\1/g')"
|
eval "args=$(scmb_expand_args "$@")" # create $args array
|
||||||
|
_safe_eval "${args[@]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
# Clear numbered env variables
|
# Clear numbered env variables
|
||||||
@@ -180,13 +177,12 @@ git_clear_vars() {
|
|||||||
_git_resolve_merge_conflict() {
|
_git_resolve_merge_conflict() {
|
||||||
if [ -n "$2" ]; then
|
if [ -n "$2" ]; then
|
||||||
# Expand args and process resulting set of files.
|
# Expand args and process resulting set of files.
|
||||||
IFS=$'\t'
|
eval "args=$(scmb_expand_args "$@")" # create $args array
|
||||||
for file in $(scmb_expand_args "${@:2}"); do
|
for file in "${args[@]:2}"; do
|
||||||
git checkout "--$1""s" "$file" # "--$1""s" is expanded to --ours or --theirs
|
git checkout "--$1""s" "$file" # "--$1""s" is expanded to --ours or --theirs
|
||||||
git add "$file"
|
git add "$file"
|
||||||
echo -e "# Added $1 version of '$file'"
|
echo -e "# Added $1 version of '$file'"
|
||||||
done
|
done
|
||||||
unset IFS
|
|
||||||
echo -e "# -- If you have finished resolving conflicts, commit the resolutions with 'git commit'"
|
echo -e "# -- If you have finished resolving conflicts, commit the resolutions with 'git commit'"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,16 +49,22 @@ setupTestRepo() {
|
|||||||
#-----------------------------------------------------------------------------
|
#-----------------------------------------------------------------------------
|
||||||
|
|
||||||
test_scmb_expand_args() {
|
test_scmb_expand_args() {
|
||||||
local e1="one"; local e2="two"; local e3="three"; local e4="four"; local e5="five"; local e6="six"; local e7="seven"
|
local e1="one" e2="two" e3="three" e4="four" e5="five" e6="six" e7='$dollar' e8='two words'
|
||||||
local error="Args not expanded correctly"
|
local error="Args not expanded correctly"
|
||||||
assertEquals "$error" "$(printf 'one\tthree\tseven')" "$(scmb_expand_args 1 3 7)"
|
assertEquals "$error" "'one' 'three' 'six'" \
|
||||||
assertEquals "$error" "$(printf 'one\ttwo\tthree\tsix')" "$(scmb_expand_args 1-3 6)"
|
"$(eval a=$(scmb_expand_args 1 3 6); token_quote "${a[@]}")"
|
||||||
assertEquals "$error" "$(printf 'seven\ttwo\tthree\tfour\tfive\tone')" "$(scmb_expand_args seven 2-5 1)"
|
assertEquals "$error" "'one' 'two' 'three' 'five'" \
|
||||||
|
"$(eval a=$(scmb_expand_args 1-3 5); token_quote "${a[@]}")"
|
||||||
|
assertEquals "$error" "'\$dollar' 'two' 'three' 'four' 'one'" \
|
||||||
|
"$(eval a=$(scmb_expand_args 7 2-4 1); token_quote "${a[@]}")"
|
||||||
|
|
||||||
# Test that any args with spaces remain quoted
|
# Test that any args with spaces remain quoted
|
||||||
assertEquals "$error" "$(printf -- '-m\tTest Commit Message\tone')" "$(scmb_expand_args -m "Test Commit Message" 1)"
|
assertEquals "$error" "'-m' 'Test Commit Message' 'one'" \
|
||||||
assertEquals "$error" "$(printf -- '-ma\tTest Commit Message\tUnquoted')"\
|
"$(eval a=$(scmb_expand_args -m "Test Commit Message" 1); token_quote "${a[@]}")"
|
||||||
"$(scmb_expand_args -ma "Test Commit Message" "Unquoted")"
|
assertEquals "$error" "'-ma' 'Test Commit Message' 'Unquoted'"\
|
||||||
|
"$(eval a=$(scmb_expand_args -ma "Test Commit Message" "Unquoted"); token_quote "${a[@]}")"
|
||||||
|
assertEquals "$error" "'\$dollar' 'one' 'two words'" \
|
||||||
|
"$(eval a=$(scmb_expand_args 7 1-1 8); token_quote "${a[@]}")"
|
||||||
}
|
}
|
||||||
|
|
||||||
test_command_wrapping_escapes_special_characters() {
|
test_command_wrapping_escapes_special_characters() {
|
||||||
|
|||||||
Reference in New Issue
Block a user