Re: [PATCH] git-submodule add: Record branch name in .gitmodules
- From
Phil Hord <phil.hord@gmail.com>
- Date
- Oct 24, 2012, 18:12 UTC
- Message-ID
- <CABURp0rpPqeTJMYXmgMG2CmU=SzzvCMeX46rnMVB0EeB_LXPiA@mail.gmail.com>
- In-Reply-To
- <20121022225546.GO23101@odin.tremily.us>
On Mon, Oct 22, 2012 at 6:55 PM, W. Trevor King <wking@tremily.us> wrote:
Show 13 quoted lines
> On a tangentially related note, it would be nice to set environment > variables for each of the settings in submodule.$name during a foreach > call. Then you could use > > git submodule foreach 'git checkout $branch && git pull' > > Perhaps you'd want to blacklist/whitelist or downcase settings names > to avoid things like > > [submodule "foo"] > PATH = /my/rootkit/ > > but the update line is much cleaner.
This is ugly as can be, but it works in my meagre testing. It defines "submodule_path=/my/rootkit/" for the above bit of .gitmodules. That is, it adds definitions for 'submodule_<var-name>' for each <var-name> in .gitmodules in submodule.$name.<var-name>, but <var-name> is lowercased and defanged (everything that's not already [a-z0-9] is replaced with underscore). For example,
git submodule foreach 'echo $submodule_url'
--- >8 ---
diff --git c/git-submodule.sh i/git-submodule.sh index 6dd2338..79b3467 100755 --- c/git-submodule.sh +++ i/git-submodule.sh @@ -416,7 +416,15 @@ cmd_foreach() prefix="$prefix$sm_path/" 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 | sed -e 's/^[^a-z]/_/' -e 's/[^a-z0-9]/_/g') + printf 'submodule_%s=%s;\n' "$VAR_NAME" "'$VAR_VALUE'" + done) cd "$sm_path" && eval "$@" && if test -n "$recursive"