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