Skip to content

Commit 30ba380

Browse files
committed
Merge branch 'lh/submodule'
* lh/submodule: git-submodule: clone during update, not during init git-submodule: move cloning into a separate function
2 parents f26cacf + 211b7f1 commit 30ba380

File tree

3 files changed

+78
-59
lines changed

3 files changed

+78
-59
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: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,35 @@ say()
2525
fi
2626
}
2727

28+
2829
#
29-
# Run clone + checkout on missing submodules
30+
# Clone a submodule
31+
#
32+
module_clone()
33+
{
34+
path=$1
35+
url=$2
36+
37+
# If there already is a directory at the submodule path,
38+
# expect it to be empty (since that is the default checkout
39+
# action) and try to remove it.
40+
# Note: if $path is a symlink to a directory the test will
41+
# succeed but the rmdir will fail. We might want to fix this.
42+
if test -d "$path"
43+
then
44+
rmdir "$path" 2>/dev/null ||
45+
die "Directory '$path' exist, but is neither empty nor a git repository"
46+
fi
47+
48+
test -e "$path" &&
49+
die "A file already exist at path '$path'"
50+
51+
git-clone -n "$url" "$path" ||
52+
die "Clone of submodule '$path' failed"
53+
}
54+
55+
#
56+
# Register submodules in .git/config
3057
#
3158
# $@ = requested paths (default to all)
3259
#
@@ -35,52 +62,23 @@ modules_init()
3562
git ls-files --stage -- "$@" | grep -e '^160000 ' |
3663
while read mode sha1 stage path
3764
do
38-
# Skip submodule paths that already contain a .git directory.
39-
# This will also trigger if $path is a symlink to a git
40-
# repository
41-
test -d "$path"/.git && continue
42-
43-
# If there already is a directory at the submodule path,
44-
# expect it to be empty (since that is the default checkout
45-
# action) and try to remove it.
46-
# Note: if $path is a symlink to a directory the test will
47-
# succeed but the rmdir will fail. We might want to fix this.
48-
if test -d "$path"
49-
then
50-
rmdir "$path" 2>/dev/null ||
51-
die "Directory '$path' exist, but is neither empty nor a git repository"
52-
fi
53-
54-
test -e "$path" &&
55-
die "A file already exist at path '$path'"
65+
# Skip already registered paths
66+
url=$(git-config submodule."$path".url)
67+
test -z "$url" || continue
5668

5769
url=$(GIT_CONFIG=.gitmodules git-config module."$path".url)
5870
test -z "$url" &&
5971
die "No url found for submodule '$path' in .gitmodules"
6072

61-
# MAYBE FIXME: this would be the place to check GIT_CONFIG
62-
# for a preferred url for this submodule, possibly like this:
63-
#
64-
# modname=$(GIT_CONFIG=.gitmodules git-config module."$path".name)
65-
# alturl=$(git-config module."$modname".url)
66-
#
67-
# This would let the versioned .gitmodules file use the submodule
68-
# path as key, while the unversioned GIT_CONFIG would use the
69-
# logical modulename (if present) as key. But this would need
70-
# another fallback mechanism if the module wasn't named.
71-
72-
git-clone -n "$url" "$path" ||
73-
die "Clone of submodule '$path' failed"
73+
git-config submodule."$path".url "$url" ||
74+
die "Failed to register url for submodule '$path'"
7475

75-
(unset GIT_DIR && cd "$path" && git-checkout -q "$sha1") ||
76-
die "Checkout of submodule '$path' failed"
77-
78-
say "Submodule '$path' initialized"
76+
say "Submodule '$path' registered with url '$url'"
7977
done
8078
}
8179

8280
#
83-
# Checkout correct revision of each initialized submodule
81+
# Update each submodule path to correct revision, using clone and checkout as needed
8482
#
8583
# $@ = requested paths (default to all)
8684
#
@@ -89,14 +87,21 @@ modules_update()
8987
git ls-files --stage -- "$@" | grep -e '^160000 ' |
9088
while read mode sha1 stage path
9189
do
92-
if ! test -d "$path"/.git
90+
url=$(git-config submodule."$path".url)
91+
if test -z "$url"
9392
then
9493
# Only mention uninitialized submodules when its
9594
# path have been specified
9695
test "$#" != "0" &&
9796
say "Submodule '$path' not initialized"
98-
continue;
97+
continue
9998
fi
99+
100+
if ! test -d "$path"/.git
101+
then
102+
module_clone "$path" "$url" || exit
103+
fi
104+
100105
subsha1=$(unset GIT_DIR && cd "$path" &&
101106
git-rev-parse --verify HEAD) ||
102107
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)