submodule sync: do not auto-vivify uninteresting submodule

Earlier 33f072f (submodule sync: Update "submodule.<name>.url" for empty directories, 2010-10-08) attempted to fix a bug where "git submodule sync" command does not update the URL if the current superproject does not have a checkout of the submodule. However, it did so by unconditionally registering submodule.$name.url to every submodule in the project, even the ones that the user has never showed interest in at all by running 'git submodule init' command. This caused subsequent 'git submodule update' to start cloning/updating submodules that are not interesting to the user at all. Update the code so that the URL is updated from the .gitmodules file only for submodules that already have submodule.$name.url entries, i.e. the ones the user has showed interested in having a checkout. Acked-by: Jens Lehmann <Jens.Lehmann@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Jun 25, 2011 at 22:41 UTC ccee60862bbba6aa65fafbdb1ce357d2b31a795b
3 files changed +29 -13
Documentation/git-submodule.txt
+3 -1
@@ -166,7 +166,9 @@ commit for each submodule.
166
167 sync::
168 Synchronizes submodules' remote URL configuration setting
169 - to the value specified in .gitmodules. This is useful when
169 + to the value specified in .gitmodules. It will only affect those
170 + submodules which already have an url entry in .git/config (that is the
171 + case when they are initialized or freshly added). This is useful when
172 submodule URLs change upstream and you need to update your local
173 repositories accordingly.
174 +
git-submodule.sh
+13 -10
@@ -836,17 +836,20 @@ cmd_sync()
836 ;;
837 esac
838
839 - say "Synchronizing submodule url for '$name'"
840 - git config submodule."$name".url "$url"
841 -
842 - if test -e "$path"/.git
839 + if git config "submodule.$name.url" >/dev/null 2>/dev/null
840 then
844 - (
845 - clear_local_git_env
846 - cd "$path"
847 - remote=$(get_default_remote)
848 - git config remote."$remote".url "$url"
849 - )
841 + say "Synchronizing submodule url for '$name'"
842 + git config submodule."$name".url "$url"
843 +
844 + if test -e "$path"/.git
845 + then
846 + (
847 + clear_local_git_env
848 + cd "$path"
849 + remote=$(get_default_remote)
850 + git config remote."$remote".url "$url"
851 + )
852 + fi
853 fi
854 done
855 }
t/t7403-submodule-sync.sh
+13 -2
@@ -25,7 +25,8 @@ test_expect_success setup '
25 git clone super super-clone &&
26 (cd super-clone && git submodule update --init) &&
27 git clone super empty-clone &&
28 - (cd empty-clone && git submodule init)
28 + (cd empty-clone && git submodule init) &&
29 + git clone super top-only-clone
30 '
31
32 test_expect_success 'change submodule' '
@@ -66,7 +67,7 @@ test_expect_success '"git submodule sync" should update submodule URLs' '
67 )
68 '
69
69 -test_expect_success '"git submodule sync" should update submodule URLs if not yet cloned' '
70 +test_expect_success '"git submodule sync" should update known submodule URLs' '
71 (cd empty-clone &&
72 git pull &&
73 git submodule sync &&
@@ -74,4 +75,14 @@ test_expect_success '"git submodule sync" should update submodule URLs if not ye
75 )
76 '
77
78 +test_expect_success '"git submodule sync" should not vivify uninteresting submodule' '
79 + (cd top-only-clone &&
80 + git pull &&
81 + git submodule sync &&
82 + test -z "$(git config submodule.submodule.url)" &&
83 + git submodule sync submodule &&
84 + test -z "$(git config submodule.submodule.url)"
85 + )
86 +'
87 +
88 test_done