]>
Commit | Line | Data |
---|---|---|
70c7ac22 LH |
1 | #!/bin/sh |
2 | # | |
ecda0723 | 3 | # git-submodules.sh: add, init, update or list git submodules |
70c7ac22 LH |
4 | # |
5 | # Copyright (c) 2007 Lars Hjemli | |
6 | ||
ecda0723 | 7 | USAGE='[--quiet] [--cached] [add <repo> [-b branch]|status|init|update] [--] [<path>...]' |
70c7ac22 LH |
8 | . git-sh-setup |
9 | require_work_tree | |
10 | ||
ecda0723 SV |
11 | add= |
12 | branch= | |
70c7ac22 LH |
13 | init= |
14 | update= | |
15 | status= | |
16 | quiet= | |
17 | cached= | |
18 | ||
19 | # | |
20 | # print stuff on stdout unless -q was specified | |
21 | # | |
22 | say() | |
23 | { | |
24 | if test -z "$quiet" | |
25 | then | |
26 | echo "$@" | |
27 | fi | |
28 | } | |
29 | ||
ecda0723 SV |
30 | # NEEDSWORK: identical function exists in get_repo_base in clone.sh |
31 | get_repo_base() { | |
32 | ( | |
33 | cd "`/bin/pwd`" && | |
34 | cd "$1" || cd "$1.git" && | |
35 | { | |
36 | cd .git | |
37 | pwd | |
38 | } | |
39 | ) 2>/dev/null | |
40 | } | |
41 | ||
f31a522a ML |
42 | # Resolve relative url by appending to parent's url |
43 | resolve_relative_url () | |
44 | { | |
45 | branch="$(git symbolic-ref HEAD 2>/dev/null)" | |
46 | remote="$(git config branch.${branch#refs/heads/}.remote)" | |
47 | remote="${remote:-origin}" | |
48 | remoteurl="$(git config remote.$remote.url)" || | |
49 | die "remote ($remote) does not have a url in .git/config" | |
50 | url="$1" | |
51 | while test -n "$url" | |
52 | do | |
53 | case "$url" in | |
54 | ../*) | |
55 | url="${url#../}" | |
56 | remoteurl="${remoteurl%/*}" | |
57 | ;; | |
58 | ./*) | |
59 | url="${url#./}" | |
60 | ;; | |
61 | *) | |
62 | break;; | |
63 | esac | |
64 | done | |
65 | echo "$remoteurl/$url" | |
66 | } | |
67 | ||
941987a5 LH |
68 | # |
69 | # Map submodule path to submodule name | |
70 | # | |
71 | # $1 = path | |
72 | # | |
73 | module_name() | |
74 | { | |
537601ac JH |
75 | # Do we have "submodule.<something>.path = $1" defined in .gitmodules file? |
76 | re=$(printf '%s' "$1" | sed -e 's/\([^a-zA-Z0-9_]\)/\\\1/g') | |
77 | name=$( GIT_CONFIG=.gitmodules \ | |
78 | git config --get-regexp '^submodule\..*\.path$' | | |
79 | sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' ) | |
941987a5 LH |
80 | test -z "$name" && |
81 | die "No submodule mapping found in .gitmodules for path '$path'" | |
82 | echo "$name" | |
83 | } | |
33aa6fff LH |
84 | |
85 | # | |
86 | # Clone a submodule | |
87 | # | |
ecda0723 SV |
88 | # Prior to calling, modules_update checks that a possibly existing |
89 | # path is not a git repository. | |
90 | # Likewise, module_add checks that path does not exist at all, | |
91 | # since it is the location of a new submodule. | |
92 | # | |
33aa6fff LH |
93 | module_clone() |
94 | { | |
95 | path=$1 | |
96 | url=$2 | |
97 | ||
98 | # If there already is a directory at the submodule path, | |
99 | # expect it to be empty (since that is the default checkout | |
100 | # action) and try to remove it. | |
101 | # Note: if $path is a symlink to a directory the test will | |
102 | # succeed but the rmdir will fail. We might want to fix this. | |
103 | if test -d "$path" | |
104 | then | |
105 | rmdir "$path" 2>/dev/null || | |
106 | die "Directory '$path' exist, but is neither empty nor a git repository" | |
107 | fi | |
108 | ||
109 | test -e "$path" && | |
110 | die "A file already exist at path '$path'" | |
111 | ||
112 | git-clone -n "$url" "$path" || | |
941987a5 | 113 | die "Clone of '$url' into submodule path '$path' failed" |
33aa6fff LH |
114 | } |
115 | ||
ecda0723 SV |
116 | # |
117 | # Add a new submodule to the working tree, .gitmodules and the index | |
118 | # | |
119 | # $@ = repo [path] | |
120 | # | |
121 | # optional branch is stored in global branch variable | |
122 | # | |
123 | module_add() | |
124 | { | |
125 | repo=$1 | |
126 | path=$2 | |
127 | ||
128 | if test -z "$repo"; then | |
129 | usage | |
130 | fi | |
131 | ||
f31a522a ML |
132 | case "$repo" in |
133 | ./*|../*) | |
134 | # dereference source url relative to parent's url | |
135 | realrepo="$(resolve_relative_url $repo)" ;; | |
136 | *) | |
137 | # Turn the source into an absolute path if | |
138 | # it is local | |
139 | if base=$(get_repo_base "$repo"); then | |
140 | repo="$base" | |
f31a522a | 141 | fi |
fe50eef5 | 142 | realrepo=$repo |
f31a522a ML |
143 | ;; |
144 | esac | |
ecda0723 SV |
145 | |
146 | # Guess path from repo if not specified or strip trailing slashes | |
147 | if test -z "$path"; then | |
148 | path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g') | |
149 | else | |
150 | path=$(echo "$path" | sed -e 's|/*$||') | |
151 | fi | |
152 | ||
153 | test -e "$path" && | |
154 | die "'$path' already exists" | |
155 | ||
5be60078 | 156 | git ls-files --error-unmatch "$path" > /dev/null 2>&1 && |
ecda0723 SV |
157 | die "'$path' already exists in the index" |
158 | ||
f31a522a | 159 | module_clone "$path" "$realrepo" || exit |
ecda0723 SV |
160 | (unset GIT_DIR && cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) || |
161 | die "Unable to checkout submodule '$path'" | |
162 | git add "$path" || | |
163 | die "Failed to add submodule '$path'" | |
164 | ||
165 | GIT_CONFIG=.gitmodules git config submodule."$path".path "$path" && | |
166 | GIT_CONFIG=.gitmodules git config submodule."$path".url "$repo" && | |
167 | git add .gitmodules || | |
168 | die "Failed to register submodule '$path'" | |
169 | } | |
170 | ||
70c7ac22 | 171 | # |
211b7f19 | 172 | # Register submodules in .git/config |
70c7ac22 LH |
173 | # |
174 | # $@ = requested paths (default to all) | |
175 | # | |
176 | modules_init() | |
177 | { | |
178 | git ls-files --stage -- "$@" | grep -e '^160000 ' | | |
179 | while read mode sha1 stage path | |
180 | do | |
211b7f19 | 181 | # Skip already registered paths |
941987a5 | 182 | name=$(module_name "$path") || exit |
5be60078 | 183 | url=$(git config submodule."$name".url) |
211b7f19 | 184 | test -z "$url" || continue |
70c7ac22 | 185 | |
5be60078 | 186 | url=$(GIT_CONFIG=.gitmodules git config submodule."$name".url) |
70c7ac22 | 187 | test -z "$url" && |
941987a5 | 188 | die "No url found for submodule path '$path' in .gitmodules" |
70c7ac22 | 189 | |
f31a522a ML |
190 | # Possibly a url relative to parent |
191 | case "$url" in | |
192 | ./*|../*) | |
193 | url="$(resolve_relative_url "$url")" | |
194 | ;; | |
195 | esac | |
196 | ||
5be60078 | 197 | git config submodule."$name".url "$url" || |
941987a5 | 198 | die "Failed to register url for submodule path '$path'" |
70c7ac22 | 199 | |
941987a5 | 200 | say "Submodule '$name' ($url) registered for path '$path'" |
70c7ac22 LH |
201 | done |
202 | } | |
203 | ||
204 | # | |
211b7f19 | 205 | # Update each submodule path to correct revision, using clone and checkout as needed |
70c7ac22 LH |
206 | # |
207 | # $@ = requested paths (default to all) | |
208 | # | |
209 | modules_update() | |
210 | { | |
211 | git ls-files --stage -- "$@" | grep -e '^160000 ' | | |
212 | while read mode sha1 stage path | |
213 | do | |
941987a5 | 214 | name=$(module_name "$path") || exit |
5be60078 | 215 | url=$(git config submodule."$name".url) |
211b7f19 | 216 | if test -z "$url" |
70c7ac22 LH |
217 | then |
218 | # Only mention uninitialized submodules when its | |
219 | # path have been specified | |
220 | test "$#" != "0" && | |
941987a5 | 221 | say "Submodule path '$path' not initialized" |
211b7f19 LH |
222 | continue |
223 | fi | |
224 | ||
225 | if ! test -d "$path"/.git | |
226 | then | |
227 | module_clone "$path" "$url" || exit | |
bf2d8246 LH |
228 | subsha1= |
229 | else | |
230 | subsha1=$(unset GIT_DIR && cd "$path" && | |
5be60078 | 231 | git rev-parse --verify HEAD) || |
941987a5 | 232 | die "Unable to find current revision in submodule path '$path'" |
70c7ac22 | 233 | fi |
211b7f19 | 234 | |
70c7ac22 LH |
235 | if test "$subsha1" != "$sha1" |
236 | then | |
237 | (unset GIT_DIR && cd "$path" && git-fetch && | |
238 | git-checkout -q "$sha1") || | |
941987a5 | 239 | die "Unable to checkout '$sha1' in submodule path '$path'" |
70c7ac22 | 240 | |
941987a5 | 241 | say "Submodule path '$path': checked out '$sha1'" |
70c7ac22 LH |
242 | fi |
243 | done | |
244 | } | |
245 | ||
bffe71f4 EM |
246 | set_name_rev () { |
247 | revname=$( ( | |
248 | unset GIT_DIR && | |
249 | cd "$1" && { | |
5be60078 JH |
250 | git describe "$2" 2>/dev/null || |
251 | git describe --tags "$2" 2>/dev/null || | |
252 | git describe --contains --tags "$2" | |
bffe71f4 EM |
253 | } |
254 | ) ) | |
255 | test -z "$revname" || revname=" ($revname)" | |
256 | } | |
257 | ||
70c7ac22 | 258 | # |
941987a5 | 259 | # List all submodules, prefixed with: |
70c7ac22 LH |
260 | # - submodule not initialized |
261 | # + different revision checked out | |
262 | # | |
263 | # If --cached was specified the revision in the index will be printed | |
264 | # instead of the currently checked out revision. | |
265 | # | |
266 | # $@ = requested paths (default to all) | |
267 | # | |
268 | modules_list() | |
269 | { | |
270 | git ls-files --stage -- "$@" | grep -e '^160000 ' | | |
271 | while read mode sha1 stage path | |
272 | do | |
941987a5 | 273 | name=$(module_name "$path") || exit |
5be60078 | 274 | url=$(git config submodule."$name".url) |
941987a5 | 275 | if test -z "url" || ! test -d "$path"/.git |
70c7ac22 LH |
276 | then |
277 | say "-$sha1 $path" | |
278 | continue; | |
279 | fi | |
41c7c1bd | 280 | set_name_rev "$path" "$sha1" |
70c7ac22 LH |
281 | if git diff-files --quiet -- "$path" |
282 | then | |
bffe71f4 | 283 | say " $sha1 $path$revname" |
70c7ac22 LH |
284 | else |
285 | if test -z "$cached" | |
286 | then | |
5be60078 | 287 | sha1=$(unset GIT_DIR && cd "$path" && git rev-parse --verify HEAD) |
41c7c1bd | 288 | set_name_rev "$path" "$sha1" |
70c7ac22 | 289 | fi |
bffe71f4 | 290 | say "+$sha1 $path$revname" |
70c7ac22 LH |
291 | fi |
292 | done | |
293 | } | |
294 | ||
822f7c73 | 295 | while test $# != 0 |
70c7ac22 LH |
296 | do |
297 | case "$1" in | |
ecda0723 SV |
298 | add) |
299 | add=1 | |
300 | ;; | |
70c7ac22 LH |
301 | init) |
302 | init=1 | |
303 | ;; | |
304 | update) | |
305 | update=1 | |
306 | ;; | |
307 | status) | |
308 | status=1 | |
309 | ;; | |
310 | -q|--quiet) | |
311 | quiet=1 | |
312 | ;; | |
ecda0723 SV |
313 | -b|--branch) |
314 | case "$2" in | |
315 | '') | |
316 | usage | |
317 | ;; | |
318 | esac | |
319 | branch="$2"; shift | |
320 | ;; | |
70c7ac22 LH |
321 | --cached) |
322 | cached=1 | |
323 | ;; | |
324 | --) | |
325 | break | |
326 | ;; | |
327 | -*) | |
328 | usage | |
329 | ;; | |
330 | *) | |
331 | break | |
332 | ;; | |
333 | esac | |
334 | shift | |
335 | done | |
336 | ||
ecda0723 SV |
337 | case "$add,$branch" in |
338 | 1,*) | |
339 | ;; | |
340 | ,) | |
341 | ;; | |
342 | ,*) | |
343 | usage | |
344 | ;; | |
345 | esac | |
346 | ||
347 | case "$add,$init,$update,$status,$cached" in | |
348 | 1,,,,) | |
349 | module_add "$@" | |
350 | ;; | |
351 | ,1,,,) | |
70c7ac22 LH |
352 | modules_init "$@" |
353 | ;; | |
ecda0723 | 354 | ,,1,,) |
70c7ac22 LH |
355 | modules_update "$@" |
356 | ;; | |
97a5d8cc | 357 | ,,,*,*) |
70c7ac22 LH |
358 | modules_list "$@" |
359 | ;; | |
360 | *) | |
361 | usage | |
362 | ;; | |
363 | esac |