Merge branch 'ak/submodule-foreach-quoting'
authorJunio C Hamano <gitster@pobox.com>
Thu, 5 Dec 2013 20:53:17 +0000 (12:53 -0800)
committerJunio C Hamano <gitster@pobox.com>
Thu, 5 Dec 2013 20:53:17 +0000 (12:53 -0800)
A behavior change, but a worthwhile one: "git submodule foreach"
was treating its arguments as part of a single command to be
concatenated and passed to a shell, making writing buggy
scripts too easy.

This patch preserves the old "just pass it to the shell" behavior
when a single argument is passed to 'git submodule foreach' and
moves to a new "skip the shell and use the arguments passed
unmolested" behavior when more than one argument is passed.

The old behavior (always concatenating and passing to the shell)
was similar to the 'ssh' command, while the new behavior (switching
on the number of arguments) is what 'xterm -e' does.

May need more thought to make sure this change is advertised well
so that scripts that used multiple arguments but added their own
extra layer of quoting are not broken.

* ak/submodule-foreach-quoting:
submodule foreach: skip eval for more than one argument

1  2 
git-submodule.sh
t/t7407-submodule-foreach.sh
diff --combined git-submodule.sh
index 66f5f752c5fb6745ab409199deb775c4704ab3e6,7b2a83d70f7a9a178f3f0526ea0334891d12b10e..c878d9579d5cdd66759ab14867f9e47ee75baf00
@@@ -545,7 -545,12 +545,12 @@@ cmd_foreach(
                                sm_path=$(relative_path "$sm_path") &&
                                # we make $path available to scripts ...
                                path=$sm_path &&
-                               eval "$@" &&
+                               if test $# -eq 1
+                               then
+                                       eval "$1"
+                               else
+                                       "$@"
+                               fi &&
                                if test -n "$recursive"
                                then
                                        cmd_foreach "--recursive" "$@"
@@@ -612,21 -617,11 +617,21 @@@ cmd_init(
                fi
  
                # Copy "update" setting when it is not set yet
 -              upd="$(git config -f .gitmodules submodule."$name".update)"
 -              test -z "$upd" ||
 -              test -n "$(git config submodule."$name".update)" ||
 -              git config submodule."$name".update "$upd" ||
 -              die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")"
 +              if upd="$(git config -f .gitmodules submodule."$name".update)" &&
 +                 test -n "$upd" &&
 +                 test -z "$(git config submodule."$name".update)"
 +              then
 +                      case "$upd" in
 +                      rebase | merge | none)
 +                              ;; # known modes of updating
 +                      *)
 +                              echo >&2 "warning: unknown update mode '$upd' suggested for submodule '$name'"
 +                              upd=none
 +                              ;;
 +                      esac
 +                      git config submodule."$name".update "$upd" ||
 +                      die "$(eval_gettext "Failed to register update mode for submodule path '\$displaypath'")"
 +              fi
        done
  }
  
@@@ -1042,20 -1037,13 +1047,20 @@@ cmd_summary() 
        # Get modified modules cared by user
        modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
                sane_egrep '^:([0-7]* )?160000' |
 -              while read mod_src mod_dst sha1_src sha1_dst status name
 +              while read mod_src mod_dst sha1_src sha1_dst status sm_path
                do
                        # Always show modules deleted or type-changed (blob<->module)
 -                      test $status = D -o $status = T && echo "$name" && continue
 +                      test $status = D -o $status = T && echo "$sm_path" && continue
 +                      # Respect the ignore setting for --for-status.
 +                      if test -n "$for_status"
 +                      then
 +                              name=$(module_name "$sm_path")
 +                              ignore_config=$(get_submodule_config "$name" ignore none)
 +                              test $status != A -a $ignore_config = all && continue
 +                      fi
                        # Also show added or modified modules which are checked out
 -                      GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
 -                      echo "$name"
 +                      GIT_DIR="$sm_path/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
 +                      echo "$sm_path"
                done
        )
  
                        echo
                fi
                echo
 -      done |
 -      if test -n "$for_status"; then
 -              if [ -n "$files" ]; then
 -                      gettextln "Submodules changed but not updated:" | git stripspace -c
 -              else
 -                      gettextln "Submodule changes to be committed:" | git stripspace -c
 -              fi
 -              printf "\n" | git stripspace -c
 -              git stripspace -c
 -      else
 -              cat
 -      fi
 +      done
  }
  #
  # List all submodules, prefixed with:
index b64c9ed8e73644b52aff38c7b4dca248ab690c73,6b2fd39aaa4890c45ef38f147ef20cf907c99da6..7ca10b8606372051ff1a4093e320722f7784fa6b
@@@ -254,6 -254,10 +254,6 @@@ test_expect_success 'ensure "status --c
                ) &&
                git submodule status --cached --recursive -- nested1 > ../actual
        ) &&
 -      if test_have_prereq MINGW
 -      then
 -              dos2unix actual
 -      fi &&
        test_cmp expect actual
  '
  
@@@ -325,4 -329,13 +325,13 @@@ test_expect_success 'command passed to 
        test_cmp expected actual
  '
  
+ test_expect_success 'multi-argument command passed to foreach is not shell-evaluated twice' '
+       (
+               cd super &&
+               git submodule foreach "echo \\\"quoted\\\"" > ../expected &&
+               git submodule foreach echo \"quoted\" > ../actual
+       ) &&
+       test_cmp expected actual
+ '
  test_done