25
25
fi
26
26
}
27
27
28
+
28
29
#
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
30
57
#
31
58
# $@ = requested paths (default to all)
32
59
#
@@ -35,52 +62,23 @@ modules_init()
35
62
git ls-files --stage -- " $@ " | grep -e ' ^160000 ' |
36
63
while read mode sha1 stage path
37
64
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
56
68
57
69
url=$( GIT_CONFIG=.gitmodules git-config module." $path " .url)
58
70
test -z " $url " &&
59
71
die " No url found for submodule '$path ' in .gitmodules"
60
72
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 '"
74
75
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 '"
79
77
done
80
78
}
81
79
82
80
#
83
- # Checkout correct revision of each initialized submodule
81
+ # Update each submodule path to correct revision, using clone and checkout as needed
84
82
#
85
83
# $@ = requested paths (default to all)
86
84
#
@@ -89,14 +87,21 @@ modules_update()
89
87
git ls-files --stage -- " $@ " | grep -e ' ^160000 ' |
90
88
while read mode sha1 stage path
91
89
do
92
- if ! test -d " $path " /.git
90
+ url=$( git-config submodule." $path " .url)
91
+ if test -z " $url "
93
92
then
94
93
# Only mention uninitialized submodules when its
95
94
# path have been specified
96
95
test " $# " ! = " 0" &&
97
96
say " Submodule '$path ' not initialized"
98
- continue ;
97
+ continue
99
98
fi
99
+
100
+ if ! test -d " $path " /.git
101
+ then
102
+ module_clone " $path " " $url " || exit
103
+ fi
104
+
100
105
subsha1=$( unset GIT_DIR && cd " $path " &&
101
106
git-rev-parse --verify HEAD) ||
102
107
die " Unable to find current revision of submodule '$path '"
0 commit comments