]>
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 | ||
1d5bec8b | 7 | dashless=$(basename "$0" | sed -e 's/-/ /') |
d27b876b | 8 | USAGE="[--quiet] add [-b branch] [-f|--force] [--reference <repository>] [--] <repository> [<path>] |
64b19ffe | 9 | or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...] |
1d5bec8b | 10 | or: $dashless [--quiet] init [--] [<path>...] |
b13fd5c1 | 11 | or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...] |
adc54235 | 12 | or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...] |
15fc56a8 | 13 | or: $dashless [--quiet] foreach [--recursive] <command> |
1d5bec8b | 14 | or: $dashless [--quiet] sync [--] [<path>...]" |
8f321a39 | 15 | OPTIONS_SPEC= |
70c7ac22 | 16 | . git-sh-setup |
98fcf840 | 17 | . git-parse-remote |
70c7ac22 LH |
18 | require_work_tree |
19 | ||
5c08dbbd | 20 | command= |
ecda0723 | 21 | branch= |
d27b876b | 22 | force= |
d92a3959 | 23 | reference= |
70c7ac22 | 24 | cached= |
48bb3033 GP |
25 | recursive= |
26 | init= | |
1c244f6e | 27 | files= |
31ca3ac3 | 28 | nofetch= |
32948425 | 29 | update= |
15fc56a8 | 30 | prefix= |
70c7ac22 | 31 | |
f31a522a ML |
32 | # Resolve relative url by appending to parent's url |
33 | resolve_relative_url () | |
34 | { | |
98fcf840 | 35 | remote=$(get_default_remote) |
8e7e6f39 ML |
36 | remoteurl=$(git config "remote.$remote.url") || |
37 | die "remote ($remote) does not have a url defined in .git/config" | |
f31a522a | 38 | url="$1" |
99b120af | 39 | remoteurl=${remoteurl%/} |
f31a522a ML |
40 | while test -n "$url" |
41 | do | |
42 | case "$url" in | |
43 | ../*) | |
44 | url="${url#../}" | |
45 | remoteurl="${remoteurl%/*}" | |
46 | ;; | |
47 | ./*) | |
48 | url="${url#./}" | |
49 | ;; | |
50 | *) | |
51 | break;; | |
52 | esac | |
53 | done | |
99b120af | 54 | echo "$remoteurl/${url%/}" |
f31a522a ML |
55 | } |
56 | ||
a7b3269c DA |
57 | # |
58 | # Get submodule info for registered submodules | |
59 | # $@ = path to limit submodule list | |
60 | # | |
61 | module_list() | |
62 | { | |
e1622bfc | 63 | git ls-files --error-unmatch --stage -- "$@" | sane_grep '^160000 ' |
a7b3269c DA |
64 | } |
65 | ||
941987a5 LH |
66 | # |
67 | # Map submodule path to submodule name | |
68 | # | |
69 | # $1 = path | |
70 | # | |
71 | module_name() | |
72 | { | |
537601ac | 73 | # Do we have "submodule.<something>.path = $1" defined in .gitmodules file? |
fe22e542 | 74 | re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g') |
a5099bb4 | 75 | name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' | |
537601ac | 76 | sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' ) |
941987a5 LH |
77 | test -z "$name" && |
78 | die "No submodule mapping found in .gitmodules for path '$path'" | |
79 | echo "$name" | |
80 | } | |
33aa6fff LH |
81 | |
82 | # | |
83 | # Clone a submodule | |
84 | # | |
23a485e3 | 85 | # Prior to calling, cmd_update checks that a possibly existing |
ecda0723 | 86 | # path is not a git repository. |
23a485e3 | 87 | # Likewise, cmd_add checks that path does not exist at all, |
ecda0723 SV |
88 | # since it is the location of a new submodule. |
89 | # | |
33aa6fff LH |
90 | module_clone() |
91 | { | |
92 | path=$1 | |
93 | url=$2 | |
d92a3959 | 94 | reference="$3" |
33aa6fff LH |
95 | |
96 | # If there already is a directory at the submodule path, | |
97 | # expect it to be empty (since that is the default checkout | |
98 | # action) and try to remove it. | |
99 | # Note: if $path is a symlink to a directory the test will | |
100 | # succeed but the rmdir will fail. We might want to fix this. | |
101 | if test -d "$path" | |
102 | then | |
103 | rmdir "$path" 2>/dev/null || | |
0484682e | 104 | die "Directory '$path' exists, but is neither empty nor a git repository" |
33aa6fff LH |
105 | fi |
106 | ||
107 | test -e "$path" && | |
108 | die "A file already exist at path '$path'" | |
109 | ||
d92a3959 MT |
110 | if test -n "$reference" |
111 | then | |
112 | git-clone "$reference" -n "$url" "$path" | |
113 | else | |
114 | git-clone -n "$url" "$path" | |
115 | fi || | |
941987a5 | 116 | die "Clone of '$url' into submodule path '$path' failed" |
33aa6fff LH |
117 | } |
118 | ||
ecda0723 SV |
119 | # |
120 | # Add a new submodule to the working tree, .gitmodules and the index | |
121 | # | |
ec05df35 | 122 | # $@ = repo path |
ecda0723 SV |
123 | # |
124 | # optional branch is stored in global branch variable | |
125 | # | |
23a485e3 | 126 | cmd_add() |
ecda0723 | 127 | { |
5c08dbbd JH |
128 | # parse $args after "submodule ... add". |
129 | while test $# -ne 0 | |
130 | do | |
131 | case "$1" in | |
132 | -b | --branch) | |
133 | case "$2" in '') usage ;; esac | |
134 | branch=$2 | |
135 | shift | |
136 | ;; | |
d27b876b JL |
137 | -f | --force) |
138 | force=$1 | |
139 | ;; | |
5c08dbbd | 140 | -q|--quiet) |
2e6a30ef | 141 | GIT_QUIET=1 |
5c08dbbd | 142 | ;; |
d92a3959 MT |
143 | --reference) |
144 | case "$2" in '') usage ;; esac | |
145 | reference="--reference=$2" | |
146 | shift | |
147 | ;; | |
148 | --reference=*) | |
149 | reference="$1" | |
150 | shift | |
151 | ;; | |
5c08dbbd JH |
152 | --) |
153 | shift | |
154 | break | |
155 | ;; | |
156 | -*) | |
157 | usage | |
158 | ;; | |
159 | *) | |
160 | break | |
161 | ;; | |
162 | esac | |
163 | shift | |
164 | done | |
165 | ||
ecda0723 SV |
166 | repo=$1 |
167 | path=$2 | |
168 | ||
1414e578 JL |
169 | if test -z "$path"; then |
170 | path=$(echo "$repo" | | |
171 | sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g') | |
172 | fi | |
173 | ||
ec05df35 | 174 | if test -z "$repo" -o -z "$path"; then |
ecda0723 SV |
175 | usage |
176 | fi | |
177 | ||
ec05df35 ML |
178 | # assure repo is absolute or relative to parent |
179 | case "$repo" in | |
180 | ./*|../*) | |
181 | # dereference source url relative to parent's url | |
182 | realrepo=$(resolve_relative_url "$repo") || exit | |
183 | ;; | |
184 | *:*|/*) | |
185 | # absolute url | |
186 | realrepo=$repo | |
187 | ;; | |
188 | *) | |
189 | die "repo URL: '$repo' must be absolute or begin with ./|../" | |
190 | ;; | |
191 | esac | |
192 | ||
db75ada5 MG |
193 | # normalize path: |
194 | # multiple //; leading ./; /./; /../; trailing / | |
195 | path=$(printf '%s/\n' "$path" | | |
196 | sed -e ' | |
197 | s|//*|/|g | |
198 | s|^\(\./\)*|| | |
199 | s|/\./|/|g | |
200 | :start | |
201 | s|\([^/]*\)/\.\./|| | |
202 | tstart | |
203 | s|/*$|| | |
204 | ') | |
5be60078 | 205 | git ls-files --error-unmatch "$path" > /dev/null 2>&1 && |
ecda0723 SV |
206 | die "'$path' already exists in the index" |
207 | ||
d27b876b JL |
208 | if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1 |
209 | then | |
210 | echo >&2 "The following path is ignored by one of your .gitignore files:" && | |
211 | echo >&2 $path && | |
212 | echo >&2 "Use -f if you really want to add it." | |
213 | exit 1 | |
214 | fi | |
215 | ||
d4264ca3 ML |
216 | # perhaps the path exists and is already a git repo, else clone it |
217 | if test -e "$path" | |
218 | then | |
e9656473 | 219 | if test -d "$path"/.git -o -f "$path"/.git |
d4264ca3 ML |
220 | then |
221 | echo "Adding existing repo at '$path' to the index" | |
222 | else | |
223 | die "'$path' already exists and is not a valid git repo" | |
224 | fi | |
c2f93917 ML |
225 | |
226 | case "$repo" in | |
227 | ./*|../*) | |
228 | url=$(resolve_relative_url "$repo") || exit | |
229 | ;; | |
230 | *) | |
231 | url="$repo" | |
232 | ;; | |
233 | esac | |
234 | git config submodule."$path".url "$url" | |
d4264ca3 | 235 | else |
d4264ca3 | 236 | |
d92a3959 | 237 | module_clone "$path" "$realrepo" "$reference" || exit |
ea10b60c | 238 | ( |
74ae1419 | 239 | clear_local_git_env |
ea10b60c BJ |
240 | cd "$path" && |
241 | # ash fails to wordsplit ${branch:+-b "$branch"...} | |
242 | case "$branch" in | |
243 | '') git checkout -f -q ;; | |
244 | ?*) git checkout -f -q -b "$branch" "origin/$branch" ;; | |
245 | esac | |
246 | ) || die "Unable to checkout submodule '$path'" | |
d4264ca3 ML |
247 | fi |
248 | ||
d27b876b | 249 | git add $force "$path" || |
ecda0723 SV |
250 | die "Failed to add submodule '$path'" |
251 | ||
a5099bb4 IY |
252 | git config -f .gitmodules submodule."$path".path "$path" && |
253 | git config -f .gitmodules submodule."$path".url "$repo" && | |
31991b02 | 254 | git add --force .gitmodules || |
ecda0723 SV |
255 | die "Failed to register submodule '$path'" |
256 | } | |
257 | ||
19a31f9c ML |
258 | # |
259 | # Execute an arbitrary command sequence in each checked out | |
260 | # submodule | |
261 | # | |
262 | # $@ = command to execute | |
263 | # | |
264 | cmd_foreach() | |
265 | { | |
1d5bec8b JH |
266 | # parse $args after "submodule ... foreach". |
267 | while test $# -ne 0 | |
268 | do | |
269 | case "$1" in | |
270 | -q|--quiet) | |
271 | GIT_QUIET=1 | |
272 | ;; | |
15fc56a8 JH |
273 | --recursive) |
274 | recursive=1 | |
275 | ;; | |
1d5bec8b JH |
276 | -*) |
277 | usage | |
278 | ;; | |
279 | *) | |
280 | break | |
281 | ;; | |
282 | esac | |
283 | shift | |
284 | done | |
285 | ||
f030c96d ÆAB |
286 | toplevel=$(pwd) |
287 | ||
a7b3269c | 288 | module_list | |
19a31f9c ML |
289 | while read mode sha1 stage path |
290 | do | |
291 | if test -e "$path"/.git | |
292 | then | |
15fc56a8 | 293 | say "Entering '$prefix$path'" |
1e7f2aad | 294 | name=$(module_name "$path") |
15fc56a8 JH |
295 | ( |
296 | prefix="$prefix$path/" | |
74ae1419 | 297 | clear_local_git_env |
15fc56a8 JH |
298 | cd "$path" && |
299 | eval "$@" && | |
300 | if test -n "$recursive" | |
301 | then | |
302 | cmd_foreach "--recursive" "$@" | |
303 | fi | |
304 | ) || | |
19a31f9c ML |
305 | die "Stopping at '$path'; script returned non-zero status." |
306 | fi | |
307 | done | |
308 | } | |
309 | ||
70c7ac22 | 310 | # |
211b7f19 | 311 | # Register submodules in .git/config |
70c7ac22 LH |
312 | # |
313 | # $@ = requested paths (default to all) | |
314 | # | |
23a485e3 | 315 | cmd_init() |
70c7ac22 | 316 | { |
5c08dbbd JH |
317 | # parse $args after "submodule ... init". |
318 | while test $# -ne 0 | |
319 | do | |
320 | case "$1" in | |
321 | -q|--quiet) | |
2e6a30ef | 322 | GIT_QUIET=1 |
5c08dbbd JH |
323 | ;; |
324 | --) | |
325 | shift | |
326 | break | |
327 | ;; | |
328 | -*) | |
329 | usage | |
330 | ;; | |
331 | *) | |
332 | break | |
333 | ;; | |
334 | esac | |
335 | shift | |
336 | done | |
337 | ||
a7b3269c | 338 | module_list "$@" | |
70c7ac22 LH |
339 | while read mode sha1 stage path |
340 | do | |
211b7f19 | 341 | # Skip already registered paths |
941987a5 | 342 | name=$(module_name "$path") || exit |
5be60078 | 343 | url=$(git config submodule."$name".url) |
211b7f19 | 344 | test -z "$url" || continue |
70c7ac22 | 345 | |
a5099bb4 | 346 | url=$(git config -f .gitmodules submodule."$name".url) |
70c7ac22 | 347 | test -z "$url" && |
941987a5 | 348 | die "No url found for submodule path '$path' in .gitmodules" |
70c7ac22 | 349 | |
f31a522a ML |
350 | # Possibly a url relative to parent |
351 | case "$url" in | |
352 | ./*|../*) | |
8e7e6f39 | 353 | url=$(resolve_relative_url "$url") || exit |
f31a522a ML |
354 | ;; |
355 | esac | |
356 | ||
5be60078 | 357 | git config submodule."$name".url "$url" || |
941987a5 | 358 | die "Failed to register url for submodule path '$path'" |
70c7ac22 | 359 | |
32948425 JH |
360 | upd="$(git config -f .gitmodules submodule."$name".update)" |
361 | test -z "$upd" || | |
362 | git config submodule."$name".update "$upd" || | |
363 | die "Failed to register update mode for submodule path '$path'" | |
ca2cedba | 364 | |
941987a5 | 365 | say "Submodule '$name' ($url) registered for path '$path'" |
70c7ac22 LH |
366 | done |
367 | } | |
368 | ||
369 | # | |
211b7f19 | 370 | # Update each submodule path to correct revision, using clone and checkout as needed |
70c7ac22 LH |
371 | # |
372 | # $@ = requested paths (default to all) | |
373 | # | |
23a485e3 | 374 | cmd_update() |
70c7ac22 | 375 | { |
5c08dbbd | 376 | # parse $args after "submodule ... update". |
98dbe63d | 377 | orig_flags= |
5c08dbbd JH |
378 | while test $# -ne 0 |
379 | do | |
380 | case "$1" in | |
381 | -q|--quiet) | |
2e6a30ef | 382 | GIT_QUIET=1 |
5c08dbbd | 383 | ;; |
be4d2c83 | 384 | -i|--init) |
d92a3959 | 385 | init=1 |
be4d2c83 | 386 | ;; |
31ca3ac3 | 387 | -N|--no-fetch) |
31ca3ac3 FF |
388 | nofetch=1 |
389 | ;; | |
ca2cedba | 390 | -r|--rebase) |
32948425 | 391 | update="rebase" |
ca2cedba | 392 | ;; |
d92a3959 MT |
393 | --reference) |
394 | case "$2" in '') usage ;; esac | |
395 | reference="--reference=$2" | |
98dbe63d KB |
396 | orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")" |
397 | shift | |
d92a3959 MT |
398 | ;; |
399 | --reference=*) | |
400 | reference="$1" | |
d92a3959 | 401 | ;; |
42b49178 | 402 | -m|--merge) |
42b49178 JH |
403 | update="merge" |
404 | ;; | |
b13fd5c1 | 405 | --recursive) |
b13fd5c1 JH |
406 | recursive=1 |
407 | ;; | |
5c08dbbd JH |
408 | --) |
409 | shift | |
410 | break | |
411 | ;; | |
412 | -*) | |
413 | usage | |
414 | ;; | |
415 | *) | |
416 | break | |
417 | ;; | |
418 | esac | |
98dbe63d KB |
419 | orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")" |
420 | shift | |
5c08dbbd JH |
421 | done |
422 | ||
d92a3959 MT |
423 | if test -n "$init" |
424 | then | |
425 | cmd_init "--" "$@" || return | |
426 | fi | |
427 | ||
a7b3269c | 428 | module_list "$@" | |
70c7ac22 LH |
429 | while read mode sha1 stage path |
430 | do | |
941987a5 | 431 | name=$(module_name "$path") || exit |
5be60078 | 432 | url=$(git config submodule."$name".url) |
32948425 | 433 | update_module=$(git config submodule."$name".update) |
211b7f19 | 434 | if test -z "$url" |
70c7ac22 LH |
435 | then |
436 | # Only mention uninitialized submodules when its | |
437 | # path have been specified | |
438 | test "$#" != "0" && | |
989206f5 | 439 | say "Submodule path '$path' not initialized" && |
be4d2c83 | 440 | say "Maybe you want to use 'update --init'?" |
211b7f19 LH |
441 | continue |
442 | fi | |
443 | ||
ba88a1fe | 444 | if ! test -d "$path"/.git -o -f "$path"/.git |
211b7f19 | 445 | then |
d92a3959 | 446 | module_clone "$path" "$url" "$reference"|| exit |
bf2d8246 LH |
447 | subsha1= |
448 | else | |
74ae1419 | 449 | subsha1=$(clear_local_git_env; cd "$path" && |
5be60078 | 450 | git rev-parse --verify HEAD) || |
941987a5 | 451 | die "Unable to find current revision in submodule path '$path'" |
70c7ac22 | 452 | fi |
211b7f19 | 453 | |
32948425 | 454 | if ! test -z "$update" |
ca2cedba | 455 | then |
32948425 | 456 | update_module=$update |
ca2cedba PH |
457 | fi |
458 | ||
70c7ac22 LH |
459 | if test "$subsha1" != "$sha1" |
460 | then | |
b9b378a0 PY |
461 | force= |
462 | if test -z "$subsha1" | |
463 | then | |
464 | force="-f" | |
465 | fi | |
31ca3ac3 FF |
466 | |
467 | if test -z "$nofetch" | |
468 | then | |
74ae1419 | 469 | (clear_local_git_env; cd "$path" && |
31ca3ac3 FF |
470 | git-fetch) || |
471 | die "Unable to fetch in submodule path '$path'" | |
472 | fi | |
473 | ||
32948425 JH |
474 | case "$update_module" in |
475 | rebase) | |
476 | command="git rebase" | |
ca2cedba PH |
477 | action="rebase" |
478 | msg="rebased onto" | |
32948425 | 479 | ;; |
42b49178 JH |
480 | merge) |
481 | command="git merge" | |
482 | action="merge" | |
483 | msg="merged in" | |
484 | ;; | |
32948425 JH |
485 | *) |
486 | command="git checkout $force -q" | |
ca2cedba PH |
487 | action="checkout" |
488 | msg="checked out" | |
32948425 JH |
489 | ;; |
490 | esac | |
70c7ac22 | 491 | |
74ae1419 | 492 | (clear_local_git_env; cd "$path" && $command "$sha1") || |
ca2cedba PH |
493 | die "Unable to $action '$sha1' in submodule path '$path'" |
494 | say "Submodule path '$path': $msg '$sha1'" | |
70c7ac22 | 495 | fi |
b13fd5c1 JH |
496 | |
497 | if test -n "$recursive" | |
498 | then | |
98dbe63d | 499 | (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags") || |
b13fd5c1 JH |
500 | die "Failed to recurse into submodule path '$path'" |
501 | fi | |
70c7ac22 LH |
502 | done |
503 | } | |
504 | ||
bffe71f4 EM |
505 | set_name_rev () { |
506 | revname=$( ( | |
74ae1419 | 507 | clear_local_git_env |
bffe71f4 | 508 | cd "$1" && { |
5be60078 JH |
509 | git describe "$2" 2>/dev/null || |
510 | git describe --tags "$2" 2>/dev/null || | |
f669ac0b ML |
511 | git describe --contains "$2" 2>/dev/null || |
512 | git describe --all --always "$2" | |
bffe71f4 EM |
513 | } |
514 | ) ) | |
515 | test -z "$revname" || revname=" ($revname)" | |
516 | } | |
28f9af5d PY |
517 | # |
518 | # Show commit summary for submodules in index or working tree | |
519 | # | |
520 | # If '--cached' is given, show summary between index and given commit, | |
521 | # or between working tree and given commit | |
522 | # | |
523 | # $@ = [commit (default 'HEAD'),] requested paths (default all) | |
524 | # | |
525 | cmd_summary() { | |
f2dc06a3 | 526 | summary_limit=-1 |
d0f64dd4 | 527 | for_status= |
1c244f6e | 528 | diff_cmd=diff-index |
f2dc06a3 | 529 | |
28f9af5d PY |
530 | # parse $args after "submodule ... summary". |
531 | while test $# -ne 0 | |
532 | do | |
533 | case "$1" in | |
534 | --cached) | |
535 | cached="$1" | |
536 | ;; | |
1c244f6e JL |
537 | --files) |
538 | files="$1" | |
539 | ;; | |
d0f64dd4 PY |
540 | --for-status) |
541 | for_status="$1" | |
542 | ;; | |
f2dc06a3 PY |
543 | -n|--summary-limit) |
544 | if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2" | |
545 | then | |
546 | : | |
547 | else | |
548 | usage | |
549 | fi | |
550 | shift | |
551 | ;; | |
28f9af5d PY |
552 | --) |
553 | shift | |
554 | break | |
555 | ;; | |
556 | -*) | |
557 | usage | |
558 | ;; | |
559 | *) | |
560 | break | |
561 | ;; | |
562 | esac | |
563 | shift | |
564 | done | |
bffe71f4 | 565 | |
f2dc06a3 PY |
566 | test $summary_limit = 0 && return |
567 | ||
3deea89c | 568 | if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"}) |
28f9af5d PY |
569 | then |
570 | head=$rev | |
caa9c3ca | 571 | test $# = 0 || shift |
3deea89c JH |
572 | elif test -z "$1" -o "$1" = "HEAD" |
573 | then | |
14e940d7 JH |
574 | # before the first commit: compare with an empty tree |
575 | head=$(git hash-object -w -t tree --stdin </dev/null) | |
2ea6c2c9 | 576 | test -z "$1" || shift |
28f9af5d | 577 | else |
3deea89c | 578 | head="HEAD" |
28f9af5d PY |
579 | fi |
580 | ||
1c244f6e JL |
581 | if [ -n "$files" ] |
582 | then | |
583 | test -n "$cached" && | |
584 | die "--cached cannot be used with --files" | |
585 | diff_cmd=diff-files | |
586 | head= | |
587 | fi | |
588 | ||
28f9af5d PY |
589 | cd_to_toplevel |
590 | # Get modified modules cared by user | |
18076502 | 591 | modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" | |
e1622bfc | 592 | sane_egrep '^:([0-7]* )?160000' | |
28f9af5d PY |
593 | while read mod_src mod_dst sha1_src sha1_dst status name |
594 | do | |
595 | # Always show modules deleted or type-changed (blob<->module) | |
596 | test $status = D -o $status = T && echo "$name" && continue | |
597 | # Also show added or modified modules which are checked out | |
598 | GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 && | |
599 | echo "$name" | |
600 | done | |
601 | ) | |
1cb639e6 | 602 | |
d0f64dd4 PY |
603 | test -z "$modules" && return |
604 | ||
18076502 | 605 | git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules | |
e1622bfc | 606 | sane_egrep '^:([0-7]* )?160000' | |
1cb639e6 PY |
607 | cut -c2- | |
608 | while read mod_src mod_dst sha1_src sha1_dst status name | |
609 | do | |
610 | if test -z "$cached" && | |
611 | test $sha1_dst = 0000000000000000000000000000000000000000 | |
612 | then | |
613 | case "$mod_dst" in | |
614 | 160000) | |
615 | sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD) | |
616 | ;; | |
617 | 100644 | 100755 | 120000) | |
618 | sha1_dst=$(git hash-object $name) | |
619 | ;; | |
620 | 000000) | |
621 | ;; # removed | |
622 | *) | |
623 | # unexpected type | |
624 | echo >&2 "unexpected mode $mod_dst" | |
625 | continue ;; | |
626 | esac | |
627 | fi | |
628 | missing_src= | |
629 | missing_dst= | |
630 | ||
631 | test $mod_src = 160000 && | |
30353a40 | 632 | ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null && |
1cb639e6 PY |
633 | missing_src=t |
634 | ||
635 | test $mod_dst = 160000 && | |
30353a40 | 636 | ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null && |
1cb639e6 | 637 | missing_dst=t |
bffe71f4 | 638 | |
1cb639e6 PY |
639 | total_commits= |
640 | case "$missing_src,$missing_dst" in | |
641 | t,) | |
642 | errmsg=" Warn: $name doesn't contain commit $sha1_src" | |
643 | ;; | |
644 | ,t) | |
645 | errmsg=" Warn: $name doesn't contain commit $sha1_dst" | |
646 | ;; | |
647 | t,t) | |
648 | errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst" | |
649 | ;; | |
650 | *) | |
651 | errmsg= | |
652 | total_commits=$( | |
653 | if test $mod_src = 160000 -a $mod_dst = 160000 | |
654 | then | |
655 | range="$sha1_src...$sha1_dst" | |
656 | elif test $mod_src = 160000 | |
657 | then | |
658 | range=$sha1_src | |
659 | else | |
660 | range=$sha1_dst | |
661 | fi | |
662 | GIT_DIR="$name/.git" \ | |
b0e621ad | 663 | git rev-list --first-parent $range -- | wc -l |
1cb639e6 | 664 | ) |
eed35595 | 665 | total_commits=" ($(($total_commits + 0)))" |
1cb639e6 PY |
666 | ;; |
667 | esac | |
668 | ||
669 | sha1_abbr_src=$(echo $sha1_src | cut -c1-7) | |
670 | sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7) | |
671 | if test $status = T | |
672 | then | |
673 | if test $mod_dst = 160000 | |
674 | then | |
675 | echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:" | |
676 | else | |
677 | echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:" | |
678 | fi | |
679 | else | |
680 | echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:" | |
681 | fi | |
682 | if test -n "$errmsg" | |
683 | then | |
684 | # Don't give error msg for modification whose dst is not submodule | |
685 | # i.e. deleted or changed to blob | |
686 | test $mod_dst = 160000 && echo "$errmsg" | |
687 | else | |
688 | if test $mod_src = 160000 -a $mod_dst = 160000 | |
689 | then | |
f2dc06a3 PY |
690 | limit= |
691 | test $summary_limit -gt 0 && limit="-$summary_limit" | |
1cb639e6 | 692 | GIT_DIR="$name/.git" \ |
f2dc06a3 | 693 | git log $limit --pretty='format: %m %s' \ |
1cb639e6 PY |
694 | --first-parent $sha1_src...$sha1_dst |
695 | elif test $mod_dst = 160000 | |
696 | then | |
697 | GIT_DIR="$name/.git" \ | |
698 | git log --pretty='format: > %s' -1 $sha1_dst | |
699 | else | |
700 | GIT_DIR="$name/.git" \ | |
701 | git log --pretty='format: < %s' -1 $sha1_src | |
702 | fi | |
703 | echo | |
704 | fi | |
705 | echo | |
d0f64dd4 PY |
706 | done | |
707 | if test -n "$for_status"; then | |
f17a5d34 JL |
708 | if [ -n "$files" ]; then |
709 | echo "# Submodules changed but not updated:" | |
710 | else | |
711 | echo "# Submodule changes to be committed:" | |
712 | fi | |
d0f64dd4 PY |
713 | echo "#" |
714 | sed -e 's|^|# |' -e 's|^# $|#|' | |
715 | else | |
716 | cat | |
717 | fi | |
28f9af5d | 718 | } |
70c7ac22 | 719 | # |
941987a5 | 720 | # List all submodules, prefixed with: |
70c7ac22 LH |
721 | # - submodule not initialized |
722 | # + different revision checked out | |
723 | # | |
724 | # If --cached was specified the revision in the index will be printed | |
725 | # instead of the currently checked out revision. | |
726 | # | |
727 | # $@ = requested paths (default to all) | |
728 | # | |
23a485e3 | 729 | cmd_status() |
70c7ac22 | 730 | { |
5c08dbbd | 731 | # parse $args after "submodule ... status". |
98dbe63d | 732 | orig_flags= |
5c08dbbd JH |
733 | while test $# -ne 0 |
734 | do | |
735 | case "$1" in | |
736 | -q|--quiet) | |
2e6a30ef | 737 | GIT_QUIET=1 |
5c08dbbd JH |
738 | ;; |
739 | --cached) | |
740 | cached=1 | |
741 | ;; | |
64b19ffe JH |
742 | --recursive) |
743 | recursive=1 | |
744 | ;; | |
5c08dbbd JH |
745 | --) |
746 | shift | |
747 | break | |
748 | ;; | |
749 | -*) | |
750 | usage | |
751 | ;; | |
752 | *) | |
753 | break | |
754 | ;; | |
755 | esac | |
98dbe63d | 756 | orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")" |
5c08dbbd JH |
757 | shift |
758 | done | |
759 | ||
a7b3269c | 760 | module_list "$@" | |
70c7ac22 LH |
761 | while read mode sha1 stage path |
762 | do | |
941987a5 | 763 | name=$(module_name "$path") || exit |
5be60078 | 764 | url=$(git config submodule."$name".url) |
64b19ffe | 765 | displaypath="$prefix$path" |
ba88a1fe | 766 | if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git |
70c7ac22 | 767 | then |
64b19ffe | 768 | say "-$sha1 $displaypath" |
70c7ac22 LH |
769 | continue; |
770 | fi | |
41c7c1bd | 771 | set_name_rev "$path" "$sha1" |
18076502 | 772 | if git diff-files --ignore-submodules=dirty --quiet -- "$path" |
70c7ac22 | 773 | then |
64b19ffe | 774 | say " $sha1 $displaypath$revname" |
70c7ac22 LH |
775 | else |
776 | if test -z "$cached" | |
777 | then | |
74ae1419 | 778 | sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD) |
41c7c1bd | 779 | set_name_rev "$path" "$sha1" |
70c7ac22 | 780 | fi |
64b19ffe JH |
781 | say "+$sha1 $displaypath$revname" |
782 | fi | |
783 | ||
784 | if test -n "$recursive" | |
785 | then | |
786 | ( | |
787 | prefix="$displaypath/" | |
74ae1419 | 788 | clear_local_git_env |
64b19ffe | 789 | cd "$path" && |
a7eff1a8 | 790 | eval cmd_status "$orig_args" |
64b19ffe JH |
791 | ) || |
792 | die "Failed to recurse into submodule path '$path'" | |
70c7ac22 LH |
793 | fi |
794 | done | |
795 | } | |
2327f61e DA |
796 | # |
797 | # Sync remote urls for submodules | |
798 | # This makes the value for remote.$remote.url match the value | |
799 | # specified in .gitmodules. | |
800 | # | |
801 | cmd_sync() | |
802 | { | |
803 | while test $# -ne 0 | |
804 | do | |
805 | case "$1" in | |
806 | -q|--quiet) | |
2e6a30ef | 807 | GIT_QUIET=1 |
2327f61e DA |
808 | shift |
809 | ;; | |
810 | --) | |
811 | shift | |
812 | break | |
813 | ;; | |
814 | -*) | |
815 | usage | |
816 | ;; | |
817 | *) | |
818 | break | |
819 | ;; | |
820 | esac | |
821 | done | |
822 | cd_to_toplevel | |
823 | module_list "$@" | | |
824 | while read mode sha1 stage path | |
825 | do | |
826 | name=$(module_name "$path") | |
827 | url=$(git config -f .gitmodules --get submodule."$name".url) | |
baede9f8 JH |
828 | |
829 | # Possibly a url relative to parent | |
830 | case "$url" in | |
831 | ./*|../*) | |
832 | url=$(resolve_relative_url "$url") || exit | |
833 | ;; | |
834 | esac | |
835 | ||
2327f61e DA |
836 | if test -e "$path"/.git |
837 | then | |
838 | ( | |
0b9dca43 DA |
839 | say "Synchronizing submodule url for '$name'" |
840 | git config submodule."$name".url "$url" | |
74ae1419 | 841 | clear_local_git_env |
2327f61e DA |
842 | cd "$path" |
843 | remote=$(get_default_remote) | |
2327f61e DA |
844 | git config remote."$remote".url "$url" |
845 | ) | |
846 | fi | |
847 | done | |
848 | } | |
70c7ac22 | 849 | |
5c08dbbd JH |
850 | # This loop parses the command line arguments to find the |
851 | # subcommand name to dispatch. Parsing of the subcommand specific | |
852 | # options are primarily done by the subcommand implementations. | |
853 | # Subcommand specific options such as --branch and --cached are | |
854 | # parsed here as well, for backward compatibility. | |
855 | ||
856 | while test $# != 0 && test -z "$command" | |
70c7ac22 LH |
857 | do |
858 | case "$1" in | |
2327f61e | 859 | add | foreach | init | update | status | summary | sync) |
5c08dbbd | 860 | command=$1 |
70c7ac22 LH |
861 | ;; |
862 | -q|--quiet) | |
2e6a30ef | 863 | GIT_QUIET=1 |
70c7ac22 | 864 | ;; |
ecda0723 SV |
865 | -b|--branch) |
866 | case "$2" in | |
867 | '') | |
868 | usage | |
869 | ;; | |
870 | esac | |
871 | branch="$2"; shift | |
872 | ;; | |
70c7ac22 | 873 | --cached) |
28f9af5d | 874 | cached="$1" |
70c7ac22 LH |
875 | ;; |
876 | --) | |
877 | break | |
878 | ;; | |
879 | -*) | |
880 | usage | |
881 | ;; | |
882 | *) | |
883 | break | |
884 | ;; | |
885 | esac | |
886 | shift | |
887 | done | |
888 | ||
5c08dbbd JH |
889 | # No command word defaults to "status" |
890 | test -n "$command" || command=status | |
891 | ||
892 | # "-b branch" is accepted only by "add" | |
893 | if test -n "$branch" && test "$command" != add | |
894 | then | |
ecda0723 | 895 | usage |
5c08dbbd JH |
896 | fi |
897 | ||
28f9af5d PY |
898 | # "--cached" is accepted only by "status" and "summary" |
899 | if test -n "$cached" && test "$command" != status -a "$command" != summary | |
5c08dbbd | 900 | then |
70c7ac22 | 901 | usage |
5c08dbbd JH |
902 | fi |
903 | ||
904 | "cmd_$command" "$@" |