Reputation: 3151
How to recurse into all submodules and save the info to an array? that array should be accessible from outside of git submodule foreach
, in the below example, I am trying to save all the paths which has foo
in it.
$ declare -a paths
$ git submodule foreach --recursive '[[ "$name" = *"foo"* ]] && \
( echo $path; paths+=($path) ) || true'
Entering 'bar-1'
Entering 'foo-1'
foo-1
Entering 'foo-2'
foo-2
Entering 'foo-8'
foo-8
Entering 'foo'
foo
Entering 'baz'
$
$ echo ${paths[@]}
$
Upvotes: 1
Views: 1996
Reputation: 487755
git submodule foreach
runs in a sub-shell. This means there is no direct way to affect the parent shell, and that, in turn, means you need to affect the parent shell indirectly.
There are any number of ways to do this, but a simple one is to write to a file, then use source
or .
to read the file. Given your syntax above, you are presumably using bash, so:
git submodule foreach --recursive '[[ "$name" = *"foo"* ]] && \
( echo $path; echo "paths+=($path)" >> /tmp/paths ) || true'
source /tmp/paths
rm /tmp/paths
echo ${paths[@]}
Another way to do this is to eval
the output of the foreach, but this is trickier since you then have to be careful with all output. There's a handy trick with exec
for this, to redirect various file descriptors:
exec 1>&3
eval $(command)
where command
expands (via alias or shell function, or script, or whatever) to:
command() {
exec 4>&1 1>&3 3>&-
echo now we can print normally
echo var=value 1>&4 # this is a directive for the "eval"
}
The outer 3>&1
makes a copy of stdout for the inner command
, which then moves its fd 1 to fd 4, moves 3 to 1, and closes 3. Now the inner command's stdout is the same as the outer stdout, while fd 4 is where the items to be eval
-ed go.
Upvotes: 3
Reputation: 60245
Write the values as assignment statements to a temp file. Source the temp file.
Upvotes: 0