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