$path is the name of the submodule directory relative to the
superproject, $sha1 is the commit as recorded in the superproject,
and $toplevel is the absolute path to the top-level of the superproject.
+ In addition, every submodule.<name>.<opt> setting from .gitmodules
+ is available as the variable $submodule_<sanitized_opt>. These
+ variables are not propagated recursively into nested submodules.
Any submodules defined in the superproject but not checked out are
ignored by this command. Unless given `--quiet`, foreach prints the name
of each submodule before evaluating the command.
unset $(git rev-parse --local-env-vars)
}
+# Remove any suspect characters from a user-generated variable name.
+sanitize_variable_name() {
+ VAR_NAME="$1"
+ printf '%s' "$VAR_NAME" |
+ sed -e 's/^[^a-zA-Z]/_/' -e 's/[^a-zA-Z0-9]/_/g'
+}
+
+# Return a command for setting a new variable.
+# Neither the variable name nor the variable value passed to this
+# function need to be sanitized. You need to eval the returned
+# string, because new variables set by the function itself don't
+# effect the calling process.
+set_user_variable() {
+ VAR_NAME="$1"
+ VAR_VALUE="$2"
+ VAR_NAME=$(sanitize_variable_name "$VAR_NAME")
+ VAR_VALUE=$(printf '%s' "$VAR_VALUE" |
+ sed -e 's/\\/\\\\/g' -e 's/"/\\"/g')
+ printf '%s=%s;\n' "$VAR_NAME" "\"$VAR_VALUE\""
+}
# Platform specific tweaks to work around some commands
case $(uname -s) in
clear_local_git_env
# we make $path available to scripts ...
path=$sm_path
+
+ # make all submodule variables available to scripts
+ eval $(
+ git config -f .gitmodules --get-regexp "^submodule\.${name}\..*" |
+ sed -e "s|^submodule\.${name}\.||" |
+ while read VAR_NAME VAR_VALUE ; do
+ VAR_NAME=$(printf '%s' "$VAR_NAME" | tr A-Z a-z)
+ set_user_variable "submodule_${VAR_NAME}" "$VAR_VALUE"
+ done)
+ UNSET_CMD=$(set |
+ sed -n -e 's|^\(submodule_[a-z_]*\)=.*$|\1|p' |
+ while read VAR_NAME ; do
+ printf 'unset %s;\n' "$VAR_NAME"
+ done)
+
cd "$sm_path" &&
eval "$@" &&
+ eval "$UNSET_CMD" &&
if test -n "$recursive"
then
cmd_foreach "--recursive" "$@"
test_cmp expected actual
'
+cat > expect <<EOF
+Entering 'nested1'
+nested1 nested1 wonky"value
+Entering 'nested1/nested2'
+nested2 nested2 another wonky"value
+Entering 'nested1/nested2/nested3'
+nested3 nested3
+Entering 'nested1/nested2/nested3/submodule'
+submodule submodule
+Entering 'sub1'
+sub1 sub1
+Entering 'sub2'
+sub2 sub2
+Entering 'sub3'
+sub3 sub3
+EOF
+
+test_expect_success 'test foreach environment variables' '
+ (
+ cd clone2 &&
+ git config -f .gitmodules submodule.nested1.wonky-var "wonky\"value" &&
+ git config -f nested1/.gitmodules submodule.nested2.wonky-var "another wonky\"value" &&
+ git submodule foreach --recursive "echo \$path \$submodule_path \$submodule_wonky_var" > ../actual
+ ) &&
+ test_i18ncmp expect actual
+'
+#
+#"echo \$toplevel-\$name-\$submodule_path-\$submodule_url"
+
test_done