git-submodule: add "sync" command
When a submodule's URL changes upstream, existing submodules will be out of sync since their remote."$origin".url will still be set to the old value. This adds a "git submodule sync" command that reads submodules' URLs from .gitmodules and updates them accordingly. Signed-off-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
David Aguilar committed
Aug 24, 2008 at 12:43 UTC
2327f61ecc4e9fbb6dd9fffdec0b043aeaca908f
2 files changed
+55
-2
Documentation/git-submodule.txt
+9
@@ -15,6 +15,7 @@ SYNOPSIS
15
'git submodule' [--quiet] update [--init] [--] [<path>...]
16
'git submodule' [--quiet] summary [--summary-limit <n>] [commit] [--] [<path>...]
17
'git submodule' [--quiet] foreach <command>
18
+'git submodule' [--quiet] sync [--] [<path>...]
19
20
21
DESCRIPTION
@@ -139,6 +140,14 @@ foreach::
140
As an example, "git submodule foreach 'echo $path `git rev-parse HEAD`' will
141
show the path and currently checked out commit for each submodule.
142
143
+sync::
144
+ Synchronizes submodules' remote URL configuration setting
145
+ to the value specified in .gitmodules. This is useful when
146
+ submodule URLs change upstream and you need to update your local
147
+ repositories accordingly.
148
++
149
+"git submodule sync" synchronizes all submodules while
150
+"git submodule sync -- A" synchronizes submodule "A" only.
151
152
OPTIONS
153
-------
git-submodule.sh
+46
-2
@@ -6,7 +6,7 @@
6
7
USAGE="[--quiet] [--cached] \
8
[add <repo> [-b branch] <path>]|[status|init|update [-i|--init]|summary [-n|--summary-limit <n>] [<commit>]] \
9
-[--] [<path>...]|[foreach <command>]"
9
+[--] [<path>...]|[foreach <command>]|[sync [--] [<path>...]]"
10
OPTIONS_SPEC=
11
. git-sh-setup
12
. git-parse-remote
@@ -601,6 +601,50 @@ cmd_status()
601
fi
602
done
603
}
604
+#
605
+# Sync remote urls for submodules
606
+# This makes the value for remote.$remote.url match the value
607
+# specified in .gitmodules.
608
+#
609
+cmd_sync()
610
+{
611
+ while test $# -ne 0
612
+ do
613
+ case "$1" in
614
+ -q|--quiet)
615
+ quiet=1
616
+ shift
617
+ ;;
618
+ --)
619
+ shift
620
+ break
621
+ ;;
622
+ -*)
623
+ usage
624
+ ;;
625
+ *)
626
+ break
627
+ ;;
628
+ esac
629
+ done
630
+ cd_to_toplevel
631
+ module_list "$@" |
632
+ while read mode sha1 stage path
633
+ do
634
+ name=$(module_name "$path")
635
+ url=$(git config -f .gitmodules --get submodule."$name".url)
636
+ if test -e "$path"/.git
637
+ then
638
+ (
639
+ unset GIT_DIR
640
+ cd "$path"
641
+ remote=$(get_default_remote)
642
+ say "Synchronizing submodule url for '$name'"
643
+ git config remote."$remote".url "$url"
644
+ )
645
+ fi
646
+ done
647
+}
648
649
# This loop parses the command line arguments to find the
650
# subcommand name to dispatch. Parsing of the subcommand specific
@@ -611,7 +655,7 @@ cmd_status()
655
while test $# != 0 && test -z "$command"
656
do
657
case "$1" in
614
- add | foreach | init | update | status | summary)
658
+ add | foreach | init | update | status | summary | sync)
659
command=$1
660
;;
661
-q|--quiet)