All of lore.kernel.org
 help / color / mirror / Atom feed
From: "W. Trevor King" <wking@tremily.us>
To: Git <git@vger.kernel.org>
Cc: Jeff King <peff@peff.net>, Phil Hord <phil.hord@gmail.com>,
	Shawn Pearce <spearce@spearce.org>,
	Jens Lehmann <Jens.Lehmann@web.de>,
	Nahor <nahor.j+gmane@gmail.com>,
	"W. Trevor King" <wking@tremily.us>
Subject: [PATCH v3 2/3] git-submodule foreach: export .gitmodules settings as variables
Date: Thu, 08 Nov 2012 22:35:13 -0500	[thread overview]
Message-ID: <2121ce36cf4eb02385255cbd5b0bbd1dcc803113.1352431675.git.wking@tremily.us> (raw)
In-Reply-To: <cover.1352431674.git.wking@tremily.us>
In-Reply-To: <cover.1352431674.git.wking@tremily.us>

From: "W. Trevor King" <wking@tremily.us>

This makes it easy to access per-submodule variables.  For example,

  git submodule foreach 'git checkout $(git config --file $toplevel/.gitmodules submodule.$name.branch) && git pull'

can now be reduced to

  git submodule foreach 'git checkout $submodule_branch && git pull'

Every submodule.<name>.<opt> setting from .gitmodules is available as
a $submodule_<sanitized-opt> variable.  These variables are not
propagated recursively into nested submodules.

Signed-off-by: W. Trevor King <wking@tremily.us>
Based-on-patch-by: Phil Hord <phil.hord@gmail.com>
---
 Documentation/git-submodule.txt |  3 +++
 git-sh-setup.sh                 | 20 ++++++++++++++++++++
 git-submodule.sh                | 16 ++++++++++++++++
 t/t7407-submodule-foreach.sh    | 29 +++++++++++++++++++++++++++++
 4 files changed, 68 insertions(+)
 mode change 100644 => 100755 git-sh-setup.sh

diff --git a/Documentation/git-submodule.txt b/Documentation/git-submodule.txt
index cbec363..9a99826 100644
--- a/Documentation/git-submodule.txt
+++ b/Documentation/git-submodule.txt
@@ -175,6 +175,9 @@ foreach::
 	$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.
diff --git a/git-sh-setup.sh b/git-sh-setup.sh
old mode 100644
new mode 100755
index ee0e0bc..179a920
--- a/git-sh-setup.sh
+++ b/git-sh-setup.sh
@@ -222,6 +222,26 @@ clear_local_git_env() {
 	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
diff --git a/git-submodule.sh b/git-submodule.sh
index bc33112..e4d26f9 100755
--- a/git-submodule.sh
+++ b/git-submodule.sh
@@ -434,8 +434,24 @@ cmd_foreach()
 				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" "$@"
diff --git a/t/t7407-submodule-foreach.sh b/t/t7407-submodule-foreach.sh
index 9b69fe2..46ac746 100755
--- a/t/t7407-submodule-foreach.sh
+++ b/t/t7407-submodule-foreach.sh
@@ -313,4 +313,33 @@ test_expect_success 'command passed to foreach --recursive retains notion of std
 	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
-- 
1.8.0.3.gc2eb43a

  parent reply	other threads:[~2012-11-09  4:43 UTC|newest]

Thread overview: 156+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-22 16:34 [PATCH] git-submodule add: Record branch name in .gitmodules W. Trevor King
     [not found] ` <CABURp0pqg7XC6makK2OcundMabV9AtcBNGNK6Q0TMZfJbt3anw@mail.gmail.com>
2012-10-22 22:55   ` W. Trevor King
2012-10-24 18:12     ` Phil Hord
2012-10-25  1:12       ` W. Trevor King
2012-10-26 13:37         ` Jeff King
2012-10-23 19:16 ` Nahor
2012-10-23 19:44   ` W. Trevor King
2012-10-23 20:44     ` W. Trevor King
2012-10-23 21:57       ` [PATCH v2] git-submodule add: Add -r/--record option W. Trevor King
2012-10-24 19:15         ` Jens Lehmann
2012-10-25  0:53           ` W. Trevor King
2012-10-28 20:48             ` Jens Lehmann
2012-10-28 21:16               ` W. Trevor King
2012-10-28 21:59               ` Shawn Pearce
2012-10-28 22:34                 ` W. Trevor King
2012-10-29  5:34                   ` Jeff King
2012-10-29 10:45                     ` W. Trevor King
2012-10-29 10:58                       ` Jeff King
2012-10-29 11:29                         ` W. Trevor King
2012-10-29 11:43                           ` Jeff King
2012-10-29 17:38                             ` Phil Hord
2012-10-29 21:36                               ` Jeff King
2012-10-29 22:21                                 ` Phil Hord
2012-10-29 22:27                                   ` Jeff King
2012-11-09  3:35                                     ` [PATCH v3 0/3] " W. Trevor King
2012-11-09  3:35                                     ` [PATCH v3 1/3] " W. Trevor King
2012-11-09  7:34                                       ` Junio C Hamano
2012-11-09 16:29                                         ` Heiko Voigt
2012-11-10 19:02                                           ` W. Trevor King
2012-11-17 15:04                                             ` Heiko Voigt
2012-11-17 19:20                                               ` W. Trevor King
2012-11-17 21:31                                                 ` Heiko Voigt
2012-11-17 22:00                                                   ` W. Trevor King
2012-11-20  0:49                                                 ` Junio C Hamano
2012-11-20  1:16                                                   ` W. Trevor King
2012-11-20  5:39                                                     ` Junio C Hamano
2012-11-20 12:19                                                       ` W. Trevor King
2012-11-20 19:52                                                         ` Junio C Hamano
2012-11-23 15:55                                                           ` Heiko Voigt
2012-11-23 16:23                                                             ` W. Trevor King
2012-11-23 16:30                                                               ` W. Trevor King
2012-11-23 17:54                                                               ` W. Trevor King
2012-11-26 21:00                                                                 ` [PATCH v4 0/4] git-submodule add: Add --local-branch option W. Trevor King
2012-11-26 21:00                                                                   ` [PATCH v4 1/4] " W. Trevor King
2012-11-26 21:00                                                                   ` [PATCH v4 2/4] git-submodule init: Record submodule.<name>.branch in repository config W. Trevor King
2012-11-27 23:19                                                                     ` Jens Lehmann
2012-11-28  0:40                                                                       ` W. Trevor King
2012-11-30 17:53                                                                       ` [RFC] remove/deprecate 'submodule init' and 'sync' W. Trevor King
2012-11-30 18:17                                                                         ` W. Trevor King
2012-11-30 23:52                                                                         ` Phil Hord
2012-12-01 12:48                                                                           ` W. Trevor King
2012-12-01 15:42                                                                             ` Jens Lehmann
2012-12-01 15:56                                                                           ` Jens Lehmann
2012-12-01 16:37                                                                             ` W. Trevor King
2012-12-01 16:45                                                                             ` [PATCH] submodule: add 'deinit' command Jens Lehmann
2012-12-02  2:00                                                                               ` Junio C Hamano
2012-12-02 19:55                                                                                 ` Jens Lehmann
2012-12-03  7:58                                                                                   ` Junio C Hamano
2012-12-04 21:48                                                                                     ` [PATCH v2] " Jens Lehmann
2012-12-04 23:06                                                                                       ` Junio C Hamano
2012-12-12 15:08                                                                                       ` Michael J Gruber
2012-12-12 17:22                                                                                         ` Jens Lehmann
2012-12-12 19:32                                                                                           ` Junio C Hamano
2012-12-12 22:25                                                                                             ` Jens Lehmann
2012-12-12 22:34                                                                                               ` Junio C Hamano
2012-12-12 23:09                                                                                                 ` W. Trevor King
2012-12-12 23:35                                                                                                   ` Junio C Hamano
2012-12-13  0:28                                                                                                     ` W. Trevor King
2012-12-13 15:47                                                                                               ` Marc Branchaud
2012-12-01 15:38                                                                         ` [RFC] remove/deprecate 'submodule init' and 'sync' Jens Lehmann
2012-12-01 16:30                                                                           ` W. Trevor King
2012-12-01 17:25                                                                             ` Jens Lehmann
2012-12-01 17:49                                                                               ` W. Trevor King
2012-12-01 18:04                                                                                 ` Jens Lehmann
2012-12-01 18:16                                                                                   ` W. Trevor King
2012-12-02 19:09                                                                                     ` W. Trevor King
2012-12-02 20:29                                                                                       ` Jens Lehmann
2012-12-02 21:11                                                                                         ` W. Trevor King
2012-12-01 16:54                                                                         ` W. Trevor King
2012-12-03 15:38                                                                           ` W. Trevor King
2012-11-26 21:00                                                                   ` [PATCH v4 3/4] git-submodule update: Add --branch option W. Trevor King
2012-11-27 18:51                                                                     ` Heiko Voigt
2012-11-27 20:21                                                                       ` W. Trevor King
2012-11-26 21:00                                                                   ` [PATCH v4 4/4] Hack fix for 'submodule update does not fetch already present commits' W. Trevor King
2012-11-27 18:31                                                                   ` [PATCH v4 0/4] git-submodule add: Add --local-branch option Heiko Voigt
2012-11-27 19:04                                                                     ` W. Trevor King
2012-11-27 19:16                                                                     ` Heiko Voigt
2012-11-27 19:01                                                                 ` W. Trevor King
2012-11-27 21:18                                                                   ` [PATCH v4 4/4] Hack fix for 'submodule update does not fetch already present commits' W. Trevor King
2012-11-27 23:28                                                                   ` Re: [PATCH v4 0/4] git-submodule add: Add --local-branch option Heiko Voigt
2012-11-28  2:42                                                                     ` W. Trevor King
2012-11-29 18:51                                                                       ` Phil Hord
2012-11-23 17:24                                                             ` [PATCH v3 1/3] git-submodule add: Add -r/--record option Sascha Cunz
2012-11-23 16:03                                                         ` Heiko Voigt
2012-11-28 13:09                                           ` [PATCH v4 0/4] git-submodule add: Add --local-branch option (summary) W. Trevor King
2012-11-28 16:53                                             ` W. Trevor King
2012-11-28 19:30                                               ` [PATCH v5 0/2] submodule update: add --remote for submodule's upstream changes W. Trevor King
2012-11-28 19:30                                                 ` [PATCH v5 1/2] " W. Trevor King
2012-11-28 19:30                                                 ` [PATCH v5 2/2] submodule add: If --branch is given, record it in .gitmodules W. Trevor King
2012-11-29 16:12                                                 ` [RFC] git-submodule update: Add --commit option W. Trevor King
2012-11-29 16:21                                                   ` W. Trevor King
2012-11-29 16:27                                                   ` W. Trevor King
2012-11-29 19:13                                                 ` [PATCH v5 0/2] submodule update: add --remote for submodule's upstream changes W. Trevor King
2012-11-30  1:11                                                   ` Phil Hord
2012-11-30  3:27                                                     ` W. Trevor King
2012-12-02  3:17                                                       ` [PATCH v6 0/4] " W. Trevor King
2012-12-02  3:17                                                         ` [PATCH v6 1/4] submodule: add get_submodule_config helper funtion W. Trevor King
2012-12-03 19:30                                                           ` Junio C Hamano
2012-12-04  0:17                                                             ` W. Trevor King
2012-12-11 18:58                                                               ` [PATCH v7 0/3] submodule update: add --remote for submodule's upstream changes W. Trevor King
2012-12-11 18:58                                                                 ` [PATCH v7 1/3] submodule: add get_submodule_config helper funtion W. Trevor King
2012-12-11 18:58                                                                 ` [PATCH v7 2/3] submodule update: add --remote for submodule's upstream changes W. Trevor King
2012-12-12 17:43                                                                   ` Phil Hord
2012-12-12 19:54                                                                     ` Junio C Hamano
2012-12-12 23:02                                                                     ` W. Trevor King
2012-12-19 16:03                                                                       ` [PATCH v8 0/3] " wking
2012-12-19 16:03                                                                         ` [PATCH v8 1/3] submodule: add get_submodule_config helper funtion wking
2012-12-21  8:20                                                                           ` Heiko Voigt
2012-12-21 11:04                                                                             ` W. Trevor King
2012-12-19 16:03                                                                         ` [PATCH v8 2/3] submodule update: add --remote for submodule's upstream changes wking
2012-12-19 16:03                                                                         ` [PATCH v8 3/3] submodule add: If --branch is given, record it in .gitmodules wking
2012-12-19 17:43                                                                           ` Junio C Hamano
2012-12-21  8:18                                                                         ` [PATCH v8 0/3] submodule update: add --remote for submodule's upstream changes Heiko Voigt
2012-12-11 18:58                                                                 ` [PATCH v7 3/3] submodule add: If --branch is given, record it in .gitmodules W. Trevor King
2012-12-12  5:42                                                                 ` [PATCH v7 0/3] submodule update: add --remote for submodule's upstream changes Junio C Hamano
2012-12-12 15:24                                                                   ` W. Trevor King
2012-12-12 18:19                                                                     ` Junio C Hamano
2012-12-12 22:44                                                                       ` W. Trevor King
2012-12-02  3:17                                                         ` [PATCH v6 2/4] " W. Trevor King
2012-12-03 16:46                                                           ` Junio C Hamano
2012-12-03 18:15                                                             ` W. Trevor King
2012-12-03 18:38                                                               ` W. Trevor King
2012-12-03 20:29                                                                 ` Junio C Hamano
2012-12-02  3:17                                                         ` [PATCH v6 3/4] submodule add: If --branch is given, record it in .gitmodules W. Trevor King
2012-12-02  3:17                                                         ` [PATCH v6 4/4] submodule update: add submodule.<name>.remote config option W. Trevor King
2012-12-02 19:32                                                       ` [PATCH v5 0/2] submodule update: add --remote for submodule's upstream changes Jens Lehmann
2012-11-10 18:44                                         ` [PATCH v3 1/3] git-submodule add: Add -r/--record option W. Trevor King
2012-11-11 10:33                                           ` Junio C Hamano
2012-11-11 15:00                                             ` W. Trevor King
2012-11-17 15:30                                               ` Heiko Voigt
2012-11-28 19:42                                             ` W. Trevor King
2012-11-28 20:08                                               ` Junio C Hamano
     [not found]                                       ` <20121109104607.GC4406@ftbfs.org>
2012-11-10 19:11                                         ` W. Trevor King
2012-11-09  3:35                                     ` W. Trevor King [this message]
2012-11-09 16:45                                       ` [PATCH v3 2/3] git-submodule foreach: export .gitmodules settings as variables Heiko Voigt
2012-11-10 19:21                                         ` W. Trevor King
2012-11-09  3:35                                     ` [PATCH v3 3/3] git-submodule: Motivate --record with an example use case W. Trevor King
2012-10-25 22:14         ` [PATCH v2] git-submodule add: Add -r/--record option W. Trevor King
2012-10-26 14:00           ` Jeff King
2012-10-23 21:45     ` [PATCH] git-submodule add: Record branch name in .gitmodules Nahor
2012-10-23 20:36   ` Jens Lehmann
2012-10-23 20:55     ` W. Trevor King
2012-10-23 22:02     ` Nahor
2012-10-24 19:10       ` Jens Lehmann
2012-11-28 18:28 [PATCH] submodule update: document exisiting -r form for --rebase W. Trevor King
2012-11-28 19:02 ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2121ce36cf4eb02385255cbd5b0bbd1dcc803113.1352431675.git.wking@tremily.us \
    --to=wking@tremily.us \
    --cc=Jens.Lehmann@web.de \
    --cc=git@vger.kernel.org \
    --cc=nahor.j+gmane@gmail.com \
    --cc=peff@peff.net \
    --cc=phil.hord@gmail.com \
    --cc=spearce@spearce.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.