git submodule status: Add --recursive to recurse into nested submodules
In very large and hierarchically structured projects, one may encounter nested submodules. In these situations, it is valuable to not only show status for all the submodules in the current repo (which is what is currently done by 'git submodule status'), but also to show status for all submodules at all levels (i.e. recursing into nested submodules as well). This patch teaches the new --recursive option to the 'git submodule status' command. The patch also includes documentation and selftests. Signed-off-by: Johan Herland <johan@herland.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Johan Herland committed
Aug 19, 2009 at 03:45 UTC
64b19ffeddda499e7380b38b43a3dee579734905
3 files changed
+51
-6
Documentation/git-submodule.txt
+5
-2
@@ -11,7 +11,7 @@ SYNOPSIS
11
[verse]
12
'git submodule' [--quiet] add [-b branch]
13
[--reference <repository>] [--] <repository> <path>
14
-'git submodule' [--quiet] status [--cached] [--] [<path>...]
14
+'git submodule' [--quiet] status [--cached] [--recursive] [--] [<path>...]
15
'git submodule' [--quiet] init [--] [<path>...]
16
'git submodule' [--quiet] update [--init] [-N|--no-fetch] [--rebase]
17
[--reference <repository>] [--merge] [--recursive] [--] [<path>...]
@@ -100,6 +100,9 @@ status::
100
initialized and `+` if the currently checked out submodule commit
101
does not match the SHA-1 found in the index of the containing
102
repository. This command is the default command for 'git-submodule'.
103
++
104
+If '--recursive' is specified, this command will recurse into nested
105
+submodules, and show their status as well.
106
107
init::
108
Initialize the submodules, i.e. register each submodule name
@@ -216,7 +219,7 @@ OPTIONS
219
for linkgit:git-clone[1]'s --reference and --shared options carefully.
220
221
--recursive::
219
- This option is only valid for foreach and update commands.
222
+ This option is only valid for foreach, update and status commands.
223
Traverse submodules recursively. The operation is performed not
224
only in the submodules of the current repo, but also
225
in any nested submodules inside those submodules (and so on).
git-submodule.sh
+20
-4
@@ -6,7 +6,7 @@
6
7
dashless=$(basename "$0" | sed -e 's/-/ /')
8
USAGE="[--quiet] add [-b branch] [--reference <repository>] [--] <repository> <path>
9
- or: $dashless [--quiet] status [--cached] [--] [<path>...]
9
+ or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
10
or: $dashless [--quiet] init [--] [<path>...]
11
or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
12
or: $dashless [--quiet] summary [--cached] [--summary-limit <n>] [commit] [--] [<path>...]
@@ -690,6 +690,7 @@ cmd_summary() {
690
cmd_status()
691
{
692
# parse $args after "submodule ... status".
693
+ orig_args="$@"
694
while test $# -ne 0
695
do
696
case "$1" in
@@ -699,6 +700,9 @@ cmd_status()
700
--cached)
701
cached=1
702
;;
703
+ --recursive)
704
+ recursive=1
705
+ ;;
706
--)
707
shift
708
break
@@ -718,22 +722,34 @@ cmd_status()
722
do
723
name=$(module_name "$path") || exit
724
url=$(git config submodule."$name".url)
725
+ displaypath="$prefix$path"
726
if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
727
then
723
- say "-$sha1 $path"
728
+ say "-$sha1 $displaypath"
729
continue;
730
fi
731
set_name_rev "$path" "$sha1"
732
if git diff-files --quiet -- "$path"
733
then
729
- say " $sha1 $path$revname"
734
+ say " $sha1 $displaypath$revname"
735
else
736
if test -z "$cached"
737
then
738
sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
739
set_name_rev "$path" "$sha1"
740
fi
736
- say "+$sha1 $path$revname"
741
+ say "+$sha1 $displaypath$revname"
742
+ fi
743
+
744
+ if test -n "$recursive"
745
+ then
746
+ (
747
+ prefix="$displaypath/"
748
+ unset GIT_DIR
749
+ cd "$path" &&
750
+ cmd_status $orig_args
751
+ ) ||
752
+ die "Failed to recurse into submodule path '$path'"
753
fi
754
done
755
}
t/t7407-submodule-foreach.sh
+26
@@ -194,4 +194,30 @@ test_expect_success 'use "update --recursive" to checkout all submodules' '
194
)
195
'
196
197
+nested1sha1=$(cd clone3/nested1 && git rev-parse HEAD)
198
+nested2sha1=$(cd clone3/nested1/nested2 && git rev-parse HEAD)
199
+nested3sha1=$(cd clone3/nested1/nested2/nested3 && git rev-parse HEAD)
200
+submodulesha1=$(cd clone3/nested1/nested2/nested3/submodule && git rev-parse HEAD)
201
+sub1sha1=$(cd clone3/sub1 && git rev-parse HEAD)
202
+sub2sha1=$(cd clone3/sub2 && git rev-parse HEAD)
203
+sub3sha1=$(cd clone3/sub3 && git rev-parse HEAD)
204
+
205
+cat > expect <<EOF
206
+ $nested1sha1 nested1 (heads/master)
207
+ $nested2sha1 nested1/nested2 (heads/master)
208
+ $nested3sha1 nested1/nested2/nested3 (heads/master)
209
+ $submodulesha1 nested1/nested2/nested3/submodule (heads/master)
210
+ $sub1sha1 sub1 (${sub1sha1:0:7})
211
+ $sub2sha1 sub2 (${sub1sha1:0:7})
212
+ $sub3sha1 sub3 (heads/master)
213
+EOF
214
+
215
+test_expect_success 'test "status --recursive"' '
216
+ (
217
+ cd clone3 &&
218
+ git submodule status --recursive > ../actual
219
+ ) &&
220
+ test_cmp expect actual
221
+'
222
+
223
test_done