diff --git a/lib/git/status_shortcuts.sh b/lib/git/status_shortcuts.sh index c740bb0..8c9f60f 100644 --- a/lib/git/status_shortcuts.sh +++ b/lib/git/status_shortcuts.sh @@ -76,6 +76,7 @@ git_add_shortcuts() { git_silent_add_shortcuts() { if [ -n "$1" ]; then # Expand args and process resulting set of files. + IFS=$'\t' for file in $(git_expand_args "$@"); do # Use 'git rm' if file doesn't exist and 'ga_auto_remove' is enabled. if [[ $ga_auto_remove == "yes" ]] && ! [ -e "$file" ]; then @@ -86,6 +87,7 @@ git_silent_add_shortcuts() { echo -e "# add '$file'" fi done + IFS=$' \t\n' echo "#" fi } @@ -110,7 +112,7 @@ git_add_patch_shortcuts() { git_silent_add_patch_shortcuts() { if [ -n "$1" ]; then # Expand args and process resulting set of files. - IFS=$'\n' + IFS=$'\t' for file in $(git_expand_args "$@"); do git add -p "$file" echo -e "# add '$file'" @@ -142,22 +144,24 @@ git_expand_args() { first=1 for arg in "$@"; do if [[ "$arg" =~ ^[0-9]+$ ]] ; then # Substitute $e{*} variables for any integers - if [ "$first" -eq 1 ]; then first=0; else echo -n " "; fi - eval printf '%s' "\$$git_env_char$arg" + if [ "$first" -eq 1 ]; then first=0; else printf '\t'; fi + eval printf '%s' "\"\$$git_env_char$arg\"" 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 - if [ "$first" -eq 1 ]; then first=0; else echo -n " "; fi - eval printf '%s' "\$$git_env_char$i" + if [ "$first" -eq 1 ]; then first=0; else printf '\t'; fi + eval printf '%s' "\"\$$git_env_char$i\"" done + IFS="$OLDIFS" else # Otherwise, treat $arg as a normal string. - if [ "$first" -eq 1 ]; then first=0; else echo -n " "; fi - printf '%q' "$arg" + if [ "$first" -eq 1 ]; then first=0; else printf '\t'; fi + printf '%s' "$arg" fi done } # 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) -exec_git_expand_args() { $(git_expand_args "$@"); } +exec_git_expand_args() { eval "$(git_expand_args "$@" | sed -e "s/ /\\\ /g")"; } # Clear numbered env variables git_clear_vars() { diff --git a/test/lib/git/status_shortcuts_test.sh b/test/lib/git/status_shortcuts_test.sh index 7765d5e..7017c47 100755 --- a/test/lib/git/status_shortcuts_test.sh +++ b/test/lib/git/status_shortcuts_test.sh @@ -50,13 +50,13 @@ setupTestRepo() { 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 error="Args not expanded correctly" - assertEquals "$error" "one three seven" "$(git_expand_args 1 3 7)" - assertEquals "$error" "one two three six" "$(git_expand_args 1-3 6)" - assertEquals "$error" "seven two three four five one" "$(git_expand_args seven 2-5 1)" + assertEquals "$error" "$(printf 'one\tthree\tseven')" "$(git_expand_args 1 3 7)" + assertEquals "$error" "$(printf 'one\ttwo\tthree\tsix')" "$(git_expand_args 1-3 6)" + assertEquals "$error" "$(printf 'seven\ttwo\tthree\tfour\tfive\tone')" "$(git_expand_args seven 2-5 1)" # 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" "-ma Test\ Commit\ Message Unquoted"\ + assertEquals "$error" "$(printf -- '-m\tTest Commit Message\tone')" "$(git_expand_args -m "Test Commit Message" 1)" + assertEquals "$error" "$(printf -- '-ma\tTest Commit Message\tUnquoted')"\ "$(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\"" } +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