Skip to content

Commit 211b7f1

Browse files
hjemligitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 33aa6ff commit 211b7f1

File tree

3 files changed

+51
-44
lines changed

3 files changed

+51
-44
lines changed

Documentation/git-submodule.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ status::
2323
repository. This command is the default command for git-submodule.
2424

2525
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.
3030

3131
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.
3535

3636

3737
OPTIONS
@@ -50,7 +50,7 @@ OPTIONS
5050

5151
FILES
5252
-----
53-
When cloning submodules, a .gitmodules file in the top-level directory
53+
When initializing submodules, a .gitmodules file in the top-level directory
5454
of the containing repository is used to find the url of each submodule.
5555
This file should be formatted in the same way as $GIR_DIR/config. The key
5656
to each submodule url is "module.$path.url".

git-submodule.sh

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ module_clone()
5353
}
5454

5555
#
56-
# Run clone + checkout on missing submodules
56+
# Register submodules in .git/config
5757
#
5858
# $@ = requested paths (default to all)
5959
#
@@ -62,37 +62,23 @@ modules_init()
6262
git ls-files --stage -- "$@" | grep -e '^160000 ' |
6363
while read mode sha1 stage path
6464
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
6968

7069
url=$(GIT_CONFIG=.gitmodules git-config module."$path".url)
7170
test -z "$url" &&
7271
die "No url found for submodule '$path' in .gitmodules"
7372

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'"
8475

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'"
9177
done
9278
}
9379

9480
#
95-
# Checkout correct revision of each initialized submodule
81+
# Update each submodule path to correct revision, using clone and checkout as needed
9682
#
9783
# $@ = requested paths (default to all)
9884
#
@@ -101,14 +87,21 @@ modules_update()
10187
git ls-files --stage -- "$@" | grep -e '^160000 ' |
10288
while read mode sha1 stage path
10389
do
104-
if ! test -d "$path"/.git
90+
url=$(git-config submodule."$path".url)
91+
if test -z "$url"
10592
then
10693
# Only mention uninitialized submodules when its
10794
# path have been specified
10895
test "$#" != "0" &&
10996
say "Submodule '$path' not initialized"
110-
continue;
97+
continue
11198
fi
99+
100+
if ! test -d "$path"/.git
101+
then
102+
module_clone "$path" "$url" || exit
103+
fi
104+
112105
subsha1=$(unset GIT_DIR && cd "$path" &&
113106
git-rev-parse --verify HEAD) ||
114107
die "Unable to find current revision of submodule '$path'"

t/t7400-submodule-basic.sh

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test_expect_success 'Prepare submodule testing' '
4040
git-add a lib z &&
4141
git-commit -m "super commit 1" &&
4242
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
4444
'
4545

4646
test_expect_success 'status should only print one line' '
@@ -52,41 +52,55 @@ test_expect_success 'status should initially be "missing"' '
5252
git-submodule status | grep "^-$rev1"
5353
'
5454

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' '
5670
echo "hello" >lib &&
57-
if git-submodule init
71+
if git-submodule update
5872
then
59-
echo "[OOPS] init should have failed"
73+
echo "[OOPS] update should have failed"
6074
false
6175
elif test -f lib && test "$(cat lib)" != "hello"
6276
then
63-
echo "[OOPS] init failed but lib file was molested"
77+
echo "[OOPS] update failed but lib file was molested"
6478
false
6579
else
6680
rm lib
6781
fi
6882
'
6983

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' '
7185
mkdir lib &&
7286
echo "hello" >lib/a &&
73-
if git-submodule init
87+
if git-submodule update
7488
then
75-
echo "[OOPS] init should have failed"
89+
echo "[OOPS] update should have failed"
7690
false
7791
elif test "$(cat lib/a)" != "hello"
7892
then
79-
echo "[OOPS] init failed but lib/a was molested"
93+
echo "[OOPS] update failed but lib/a was molested"
8094
false
8195
else
8296
rm lib/a
8397
fi
8498
'
8599

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' '
87101
rm -rf lib &&
88102
mkdir lib &&
89-
git-submodule init &&
103+
git-submodule update &&
90104
head=$(cd lib && git-rev-parse HEAD) &&
91105
if test -z "$head"
92106
then
@@ -99,7 +113,7 @@ test_expect_success 'init should work when path is an empty dir' '
99113
fi
100114
'
101115

102-
test_expect_success 'status should be "up-to-date" after init' '
116+
test_expect_success 'status should be "up-to-date" after update' '
103117
git-submodule status | grep "^ $rev1"
104118
'
105119

0 commit comments

Comments
 (0)