@matheusbsbs / matheus-bagliano / commits / 211b7f19c7
Fork 0

git-submodule: clone during update, not during init

This teaches 'git-submodule init' to register submodule paths and urls in .git/config instead of actually cloning them. The cloning is now handled as part of 'git-submodule update'. With this change it is possible to specify preferred/alternate urls for the submodules in .git/config before the submodules are cloned. Signed-off-by: Lars Hjemli <hjemli@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lars Hjemli committed Jun 6, 2007 at 11:13 UTC 211b7f19c7b046a6cadd36d54c549e4f335f0519
3 files changed +51 -44
Documentation/git-submodule.txt
+8 -8
@@ -23,15 +23,15 @@ status::
23 repository. This command is the default command for git-submodule.
24
25 init::
26 - Initialize the submodules, i.e. clone the git repositories specified
27 - in the .gitmodules file and checkout the submodule commits specified
28 - in the index of the containing repository. This will make the
29 - submodules HEAD be detached.
26 + Initialize the submodules, i.e. register in .git/config each submodule
27 + path and url found in .gitmodules. The key used in git/config is
28 + `submodule.$path.url`. This command does not alter existing information
29 + in .git/config.
30
31 update::
32 - Update the initialized submodules, i.e. checkout the submodule commits
33 - specified in the index of the containing repository. This will make
34 - the submodules HEAD be detached.
32 + Update the registered submodules, i.e. clone missing submodules and
33 + checkout the commit specified in the index of the containing repository.
34 + This will make the submodules HEAD be detached.
35
36
37 OPTIONS
@@ -50,7 +50,7 @@ OPTIONS
50
51 FILES
52 -----
53 -When cloning submodules, a .gitmodules file in the top-level directory
53 +When initializing submodules, a .gitmodules file in the top-level directory
54 of the containing repository is used to find the url of each submodule.
55 This file should be formatted in the same way as $GIR_DIR/config. The key
56 to each submodule url is "module.$path.url".
git-submodule.sh
+17 -24
@@ -53,7 +53,7 @@ module_clone()
53 }
54
55 #
56 -# Run clone + checkout on missing submodules
56 +# Register submodules in .git/config
57 #
58 # $@ = requested paths (default to all)
59 #
@@ -62,37 +62,23 @@ modules_init()
62 git ls-files --stage -- "$@" | grep -e '^160000 ' |
63 while read mode sha1 stage path
64 do
65 - # Skip submodule paths that already contain a .git directory.
66 - # This will also trigger if $path is a symlink to a git
67 - # repository
68 - test -d "$path"/.git && continue
65 + # Skip already registered paths
66 + url=$(git-config submodule."$path".url)
67 + test -z "$url" || continue
68
69 url=$(GIT_CONFIG=.gitmodules git-config module."$path".url)
70 test -z "$url" &&
71 die "No url found for submodule '$path' in .gitmodules"
72
74 - # MAYBE FIXME: this would be the place to check GIT_CONFIG
75 - # for a preferred url for this submodule, possibly like this:
76 - #
77 - # modname=$(GIT_CONFIG=.gitmodules git-config module."$path".name)
78 - # alturl=$(git-config module."$modname".url)
79 - #
80 - # This would let the versioned .gitmodules file use the submodule
81 - # path as key, while the unversioned GIT_CONFIG would use the
82 - # logical modulename (if present) as key. But this would need
83 - # another fallback mechanism if the module wasn't named.
73 + git-config submodule."$path".url "$url" ||
74 + die "Failed to register url for submodule '$path'"
75
85 - module_clone "$path" "$url" || exit
86 -
87 - (unset GIT_DIR && cd "$path" && git-checkout -q "$sha1") ||
88 - die "Checkout of submodule '$path' failed"
89 -
90 - say "Submodule '$path' initialized"
76 + say "Submodule '$path' registered with url '$url'"
77 done
78 }
79
80 #
95 -# Checkout correct revision of each initialized submodule
81 +# Update each submodule path to correct revision, using clone and checkout as needed
82 #
83 # $@ = requested paths (default to all)
84 #
@@ -101,14 +87,21 @@ modules_update()
87 git ls-files --stage -- "$@" | grep -e '^160000 ' |
88 while read mode sha1 stage path
89 do
104 - if ! test -d "$path"/.git
90 + url=$(git-config submodule."$path".url)
91 + if test -z "$url"
92 then
93 # Only mention uninitialized submodules when its
94 # path have been specified
95 test "$#" != "0" &&
96 say "Submodule '$path' not initialized"
110 - continue;
97 + continue
98 fi
99 +
100 + if ! test -d "$path"/.git
101 + then
102 + module_clone "$path" "$url" || exit
103 + fi
104 +
105 subsha1=$(unset GIT_DIR && cd "$path" &&
106 git-rev-parse --verify HEAD) ||
107 die "Unable to find current revision of submodule '$path'"
t/t7400-submodule-basic.sh
+26 -12
@@ -40,7 +40,7 @@ test_expect_success 'Prepare submodule testing' '
40 git-add a lib z &&
41 git-commit -m "super commit 1" &&
42 mv lib .subrepo &&
43 - GIT_CONFIG=.gitmodules git-config module.lib.url ./.subrepo
43 + GIT_CONFIG=.gitmodules git-config module.lib.url git://example.com/lib.git
44 '
45
46 test_expect_success 'status should only print one line' '
@@ -52,41 +52,55 @@ test_expect_success 'status should initially be "missing"' '
52 git-submodule status | grep "^-$rev1"
53 '
54
55 -test_expect_success 'init should fail when path is used by a file' '
55 +test_expect_success 'init should register submodule url in .git/config' '
56 + git-submodule init &&
57 + url=$(git-config submodule.lib.url) &&
58 + if test "$url" != "git://example.com/lib.git"
59 + then
60 + echo "[OOPS] init succeeded but submodule url is wrong"
61 + false
62 + elif ! git-config submodule.lib.url ./.subrepo
63 + then
64 + echo "[OOPS] init succeeded but update of url failed"
65 + false
66 + fi
67 +'
68 +
69 +test_expect_success 'update should fail when path is used by a file' '
70 echo "hello" >lib &&
57 - if git-submodule init
71 + if git-submodule update
72 then
59 - echo "[OOPS] init should have failed"
73 + echo "[OOPS] update should have failed"
74 false
75 elif test -f lib && test "$(cat lib)" != "hello"
76 then
63 - echo "[OOPS] init failed but lib file was molested"
77 + echo "[OOPS] update failed but lib file was molested"
78 false
79 else
80 rm lib
81 fi
82 '
83
70 -test_expect_success 'init should fail when path is used by a nonempty directory' '
84 +test_expect_success 'update should fail when path is used by a nonempty directory' '
85 mkdir lib &&
86 echo "hello" >lib/a &&
73 - if git-submodule init
87 + if git-submodule update
88 then
75 - echo "[OOPS] init should have failed"
89 + echo "[OOPS] update should have failed"
90 false
91 elif test "$(cat lib/a)" != "hello"
92 then
79 - echo "[OOPS] init failed but lib/a was molested"
93 + echo "[OOPS] update failed but lib/a was molested"
94 false
95 else
96 rm lib/a
97 fi
98 '
99
86 -test_expect_success 'init should work when path is an empty dir' '
100 +test_expect_success 'update should work when path is an empty dir' '
101 rm -rf lib &&
102 mkdir lib &&
89 - git-submodule init &&
103 + git-submodule update &&
104 head=$(cd lib && git-rev-parse HEAD) &&
105 if test -z "$head"
106 then
@@ -99,7 +113,7 @@ test_expect_success 'init should work when path is an empty dir' '
113 fi
114 '
115
102 -test_expect_success 'status should be "up-to-date" after init' '
116 +test_expect_success 'status should be "up-to-date" after update' '
117 git-submodule status | grep "^ $rev1"
118 '
119