Use tabs as delimiter so that files with spaces are properly handled. Escape spaces only in exec_git_expand_args, use tabs to split everything else.

This commit is contained in:
Nathan Broadbent
2012-08-13 00:22:16 +12:00
parent 9fe0f45185
commit aaf4f5fac5
2 changed files with 31 additions and 13 deletions

View File

@@ -76,6 +76,7 @@ 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'
for file in $(git_expand_args "$@"); do for file in $(git_expand_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
@@ -86,6 +87,7 @@ git_silent_add_shortcuts() {
echo -e "# add '$file'" echo -e "# add '$file'"
fi fi
done done
IFS=$' \t\n'
echo "#" echo "#"
fi fi
} }
@@ -110,7 +112,7 @@ git_add_patch_shortcuts() {
git_silent_add_patch_shortcuts() { git_silent_add_patch_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=$'\n' IFS=$'\t'
for file in $(git_expand_args "$@"); do for file in $(git_expand_args "$@"); do
git add -p "$file" git add -p "$file"
echo -e "# add '$file'" echo -e "# add '$file'"
@@ -142,22 +144,24 @@ git_expand_args() {
first=1 first=1
for arg in "$@"; do for arg in "$@"; do
if [[ "$arg" =~ ^[0-9]+$ ]] ; then # Substitute $e{*} variables for any integers if [[ "$arg" =~ ^[0-9]+$ ]] ; then # Substitute $e{*} variables for any integers
if [ "$first" -eq 1 ]; then first=0; else echo -n " "; fi if [ "$first" -eq 1 ]; then first=0; else printf '\t'; fi
eval printf '%s' "\$$git_env_char$arg" eval printf '%s' "\"\$$git_env_char$arg\""
elif [[ "$arg" =~ ^[0-9]+-[0-9]+$ ]]; then # Expand ranges into $e{*} variables elif [[ "$arg" =~ ^[0-9]+-[0-9]+$ ]]; then # Expand ranges into $e{*} variables
OLDIFS="$IFS"; IFS=" " # We need to split on spaces to loop over expanded range
for i in $(eval echo {${arg/-/..}}); do for i in $(eval echo {${arg/-/..}}); do
if [ "$first" -eq 1 ]; then first=0; else echo -n " "; fi if [ "$first" -eq 1 ]; then first=0; else printf '\t'; fi
eval printf '%s' "\$$git_env_char$i" eval printf '%s' "\"\$$git_env_char$i\""
done done
IFS="$OLDIFS"
else # Otherwise, treat $arg as a normal string. else # Otherwise, treat $arg as a normal string.
if [ "$first" -eq 1 ]; then first=0; else echo -n " "; fi if [ "$first" -eq 1 ]; then first=0; else printf '\t'; fi
printf '%q' "$arg" printf '%s' "$arg"
fi fi
done done
} }
# 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_git_expand_args() { $(git_expand_args "$@"); } exec_git_expand_args() { eval "$(git_expand_args "$@" | sed -e "s/ /\\\ /g")"; }
# Clear numbered env variables # Clear numbered env variables
git_clear_vars() { git_clear_vars() {

View File

@@ -50,13 +50,13 @@ setupTestRepo() {
test_git_expand_args() { test_git_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"; local e2="two"; local e3="three"; local e4="four"; local e5="five"; local e6="six"; local e7="seven"
local error="Args not expanded correctly" local error="Args not expanded correctly"
assertEquals "$error" "one three seven" "$(git_expand_args 1 3 7)" assertEquals "$error" "$(printf 'one\tthree\tseven')" "$(git_expand_args 1 3 7)"
assertEquals "$error" "one two three six" "$(git_expand_args 1-3 6)" assertEquals "$error" "$(printf 'one\ttwo\tthree\tsix')" "$(git_expand_args 1-3 6)"
assertEquals "$error" "seven two three four five one" "$(git_expand_args seven 2-5 1)" assertEquals "$error" "$(printf 'seven\ttwo\tthree\tfour\tfive\tone')" "$(git_expand_args seven 2-5 1)"
# Test that any args with spaces remain quoted # Test that any args with spaces remain quoted
assertEquals "$error" "-m Test\ Commit\ Message one" "$(git_expand_args -m "Test Commit Message" 1)" assertEquals "$error" "$(printf -- '-m\tTest Commit Message\tone')" "$(git_expand_args -m "Test Commit Message" 1)"
assertEquals "$error" "-ma Test\ Commit\ Message Unquoted"\ assertEquals "$error" "$(printf -- '-ma\tTest Commit Message\tUnquoted')"\
"$(git_expand_args -ma "Test Commit Message" "Unquoted")" "$(git_expand_args -ma "Test Commit Message" "Unquoted")"
} }
@@ -253,6 +253,20 @@ test_git_commit_prompt() {
assertIncludes "$test_history" "git commit -m \"$dbl_escaped_msg\"" assertIncludes "$test_history" "git commit -m \"$dbl_escaped_msg\""
} }
test_adding_files_with_spaces() {
setupTestRepo
test_file="file with spaces.txt"
touch "$test_file"
e1="$testRepo/$test_file"
git_add_shortcuts 1 > /dev/null
# Test that file is added by looking at git status
git_status=$(git_status_shortcuts | strip_colors)
assertIncludes "$git_status" "new file: \[1\] \"$test_file"
}
# load and run shUnit2 # load and run shUnit2