git-submodule: add support for --merge.
[git/git-svn.git] / git-submodule.sh
blob3aff37970ad22c2498e1b2755a214e2fdb183f21
1 #!/bin/sh
3 # git-submodules.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 USAGE="[--quiet] [--cached] \
8 [add [-b branch] <repo> <path>]|[status|init|update [-i|--init] [-N|--no-fetch] [--rebase|--merge]|summary [-n|--summary-limit <n>] [<commit>]] \
9 [--] [<path>...]|[foreach <command>]|[sync [--] [<path>...]]"
10 OPTIONS_SPEC=
11 . git-sh-setup
12 . git-parse-remote
13 require_work_tree
15 command=
16 branch=
17 quiet=
18 cached=
19 nofetch=
20 update=
23 # print stuff on stdout unless -q was specified
25 say()
27 if test -z "$quiet"
28 then
29 echo "$@"
33 # Resolve relative url by appending to parent's url
34 resolve_relative_url ()
36 remote=$(get_default_remote)
37 remoteurl=$(git config "remote.$remote.url") ||
38 die "remote ($remote) does not have a url defined in .git/config"
39 url="$1"
40 remoteurl=${remoteurl%/}
41 while test -n "$url"
43 case "$url" in
44 ../*)
45 url="${url#../}"
46 remoteurl="${remoteurl%/*}"
48 ./*)
49 url="${url#./}"
52 break;;
53 esac
54 done
55 echo "$remoteurl/${url%/}"
59 # Get submodule info for registered submodules
60 # $@ = path to limit submodule list
62 module_list()
64 git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
68 # Map submodule path to submodule name
70 # $1 = path
72 module_name()
74 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
75 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
76 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
77 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
78 test -z "$name" &&
79 die "No submodule mapping found in .gitmodules for path '$path'"
80 echo "$name"
84 # Clone a submodule
86 # Prior to calling, cmd_update checks that a possibly existing
87 # path is not a git repository.
88 # Likewise, cmd_add checks that path does not exist at all,
89 # since it is the location of a new submodule.
91 module_clone()
93 path=$1
94 url=$2
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 ||
104 die "Directory '$path' exist, but is neither empty nor a git repository"
107 test -e "$path" &&
108 die "A file already exist at path '$path'"
110 git-clone -n "$url" "$path" ||
111 die "Clone of '$url' into submodule path '$path' failed"
115 # Add a new submodule to the working tree, .gitmodules and the index
117 # $@ = repo path
119 # optional branch is stored in global branch variable
121 cmd_add()
123 # parse $args after "submodule ... add".
124 while test $# -ne 0
126 case "$1" in
127 -b | --branch)
128 case "$2" in '') usage ;; esac
129 branch=$2
130 shift
132 -q|--quiet)
133 quiet=1
136 shift
137 break
140 usage
143 break
145 esac
146 shift
147 done
149 repo=$1
150 path=$2
152 if test -z "$repo" -o -z "$path"; then
153 usage
156 # assure repo is absolute or relative to parent
157 case "$repo" in
158 ./*|../*)
159 # dereference source url relative to parent's url
160 realrepo=$(resolve_relative_url "$repo") || exit
162 *:*|/*)
163 # absolute url
164 realrepo=$repo
167 die "repo URL: '$repo' must be absolute or begin with ./|../"
169 esac
171 # normalize path:
172 # multiple //; leading ./; /./; /../; trailing /
173 path=$(printf '%s/\n' "$path" |
174 sed -e '
175 s|//*|/|g
176 s|^\(\./\)*||
177 s|/\./|/|g
178 :start
179 s|\([^/]*\)/\.\./||
180 tstart
181 s|/*$||
183 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
184 die "'$path' already exists in the index"
186 # perhaps the path exists and is already a git repo, else clone it
187 if test -e "$path"
188 then
189 if test -d "$path"/.git -o -f "$path"/.git
190 then
191 echo "Adding existing repo at '$path' to the index"
192 else
193 die "'$path' already exists and is not a valid git repo"
196 case "$repo" in
197 ./*|../*)
198 url=$(resolve_relative_url "$repo") || exit
201 url="$repo"
203 esac
204 git config submodule."$path".url "$url"
205 else
207 module_clone "$path" "$realrepo" || exit
209 unset GIT_DIR
210 cd "$path" &&
211 # ash fails to wordsplit ${branch:+-b "$branch"...}
212 case "$branch" in
213 '') git checkout -f -q ;;
214 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
215 esac
216 ) || die "Unable to checkout submodule '$path'"
219 git add "$path" ||
220 die "Failed to add submodule '$path'"
222 git config -f .gitmodules submodule."$path".path "$path" &&
223 git config -f .gitmodules submodule."$path".url "$repo" &&
224 git add .gitmodules ||
225 die "Failed to register submodule '$path'"
229 # Execute an arbitrary command sequence in each checked out
230 # submodule
232 # $@ = command to execute
234 cmd_foreach()
236 module_list |
237 while read mode sha1 stage path
239 if test -e "$path"/.git
240 then
241 say "Entering '$path'"
242 (cd "$path" && eval "$@") ||
243 die "Stopping at '$path'; script returned non-zero status."
245 done
249 # Register submodules in .git/config
251 # $@ = requested paths (default to all)
253 cmd_init()
255 # parse $args after "submodule ... init".
256 while test $# -ne 0
258 case "$1" in
259 -q|--quiet)
260 quiet=1
263 shift
264 break
267 usage
270 break
272 esac
273 shift
274 done
276 module_list "$@" |
277 while read mode sha1 stage path
279 # Skip already registered paths
280 name=$(module_name "$path") || exit
281 url=$(git config submodule."$name".url)
282 test -z "$url" || continue
284 url=$(git config -f .gitmodules submodule."$name".url)
285 test -z "$url" &&
286 die "No url found for submodule path '$path' in .gitmodules"
288 # Possibly a url relative to parent
289 case "$url" in
290 ./*|../*)
291 url=$(resolve_relative_url "$url") || exit
293 esac
295 git config submodule."$name".url "$url" ||
296 die "Failed to register url for submodule path '$path'"
298 upd="$(git config -f .gitmodules submodule."$name".update)"
299 test -z "$upd" ||
300 git config submodule."$name".update "$upd" ||
301 die "Failed to register update mode for submodule path '$path'"
303 say "Submodule '$name' ($url) registered for path '$path'"
304 done
308 # Update each submodule path to correct revision, using clone and checkout as needed
310 # $@ = requested paths (default to all)
312 cmd_update()
314 # parse $args after "submodule ... update".
315 while test $# -ne 0
317 case "$1" in
318 -q|--quiet)
319 shift
320 quiet=1
322 -i|--init)
323 shift
324 cmd_init "$@" || return
326 -N|--no-fetch)
327 shift
328 nofetch=1
330 -r|--rebase)
331 shift
332 update="rebase"
334 -m|--merge)
335 shift
336 update="merge"
339 shift
340 break
343 usage
346 break
348 esac
349 done
351 module_list "$@" |
352 while read mode sha1 stage path
354 name=$(module_name "$path") || exit
355 url=$(git config submodule."$name".url)
356 update_module=$(git config submodule."$name".update)
357 if test -z "$url"
358 then
359 # Only mention uninitialized submodules when its
360 # path have been specified
361 test "$#" != "0" &&
362 say "Submodule path '$path' not initialized" &&
363 say "Maybe you want to use 'update --init'?"
364 continue
367 if ! test -d "$path"/.git -o -f "$path"/.git
368 then
369 module_clone "$path" "$url" || exit
370 subsha1=
371 else
372 subsha1=$(unset GIT_DIR; cd "$path" &&
373 git rev-parse --verify HEAD) ||
374 die "Unable to find current revision in submodule path '$path'"
377 if ! test -z "$update"
378 then
379 update_module=$update
382 if test "$subsha1" != "$sha1"
383 then
384 force=
385 if test -z "$subsha1"
386 then
387 force="-f"
390 if test -z "$nofetch"
391 then
392 (unset GIT_DIR; cd "$path" &&
393 git-fetch) ||
394 die "Unable to fetch in submodule path '$path'"
397 case "$update_module" in
398 rebase)
399 command="git rebase"
400 action="rebase"
401 msg="rebased onto"
403 merge)
404 command="git merge"
405 action="merge"
406 msg="merged in"
409 command="git checkout $force -q"
410 action="checkout"
411 msg="checked out"
413 esac
415 (unset GIT_DIR; cd "$path" && $command "$sha1") ||
416 die "Unable to $action '$sha1' in submodule path '$path'"
417 say "Submodule path '$path': $msg '$sha1'"
419 done
422 set_name_rev () {
423 revname=$( (
424 unset GIT_DIR
425 cd "$1" && {
426 git describe "$2" 2>/dev/null ||
427 git describe --tags "$2" 2>/dev/null ||
428 git describe --contains "$2" 2>/dev/null ||
429 git describe --all --always "$2"
432 test -z "$revname" || revname=" ($revname)"
435 # Show commit summary for submodules in index or working tree
437 # If '--cached' is given, show summary between index and given commit,
438 # or between working tree and given commit
440 # $@ = [commit (default 'HEAD'),] requested paths (default all)
442 cmd_summary() {
443 summary_limit=-1
444 for_status=
446 # parse $args after "submodule ... summary".
447 while test $# -ne 0
449 case "$1" in
450 --cached)
451 cached="$1"
453 --for-status)
454 for_status="$1"
456 -n|--summary-limit)
457 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
458 then
460 else
461 usage
463 shift
466 shift
467 break
470 usage
473 break
475 esac
476 shift
477 done
479 test $summary_limit = 0 && return
481 if rev=$(git rev-parse -q --verify "$1^0")
482 then
483 head=$rev
484 shift
485 else
486 head=HEAD
489 cd_to_toplevel
490 # Get modified modules cared by user
491 modules=$(git diff-index $cached --raw $head -- "$@" |
492 egrep '^:([0-7]* )?160000' |
493 while read mod_src mod_dst sha1_src sha1_dst status name
495 # Always show modules deleted or type-changed (blob<->module)
496 test $status = D -o $status = T && echo "$name" && continue
497 # Also show added or modified modules which are checked out
498 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
499 echo "$name"
500 done
503 test -z "$modules" && return
505 git diff-index $cached --raw $head -- $modules |
506 egrep '^:([0-7]* )?160000' |
507 cut -c2- |
508 while read mod_src mod_dst sha1_src sha1_dst status name
510 if test -z "$cached" &&
511 test $sha1_dst = 0000000000000000000000000000000000000000
512 then
513 case "$mod_dst" in
514 160000)
515 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
517 100644 | 100755 | 120000)
518 sha1_dst=$(git hash-object $name)
520 000000)
521 ;; # removed
523 # unexpected type
524 echo >&2 "unexpected mode $mod_dst"
525 continue ;;
526 esac
528 missing_src=
529 missing_dst=
531 test $mod_src = 160000 &&
532 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
533 missing_src=t
535 test $mod_dst = 160000 &&
536 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
537 missing_dst=t
539 total_commits=
540 case "$missing_src,$missing_dst" in
542 errmsg=" Warn: $name doesn't contain commit $sha1_src"
545 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
547 t,t)
548 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
551 errmsg=
552 total_commits=$(
553 if test $mod_src = 160000 -a $mod_dst = 160000
554 then
555 range="$sha1_src...$sha1_dst"
556 elif test $mod_src = 160000
557 then
558 range=$sha1_src
559 else
560 range=$sha1_dst
562 GIT_DIR="$name/.git" \
563 git log --pretty=oneline --first-parent $range | wc -l
565 total_commits=" ($(($total_commits + 0)))"
567 esac
569 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
570 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
571 if test $status = T
572 then
573 if test $mod_dst = 160000
574 then
575 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
576 else
577 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
579 else
580 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
582 if test -n "$errmsg"
583 then
584 # Don't give error msg for modification whose dst is not submodule
585 # i.e. deleted or changed to blob
586 test $mod_dst = 160000 && echo "$errmsg"
587 else
588 if test $mod_src = 160000 -a $mod_dst = 160000
589 then
590 limit=
591 test $summary_limit -gt 0 && limit="-$summary_limit"
592 GIT_DIR="$name/.git" \
593 git log $limit --pretty='format: %m %s' \
594 --first-parent $sha1_src...$sha1_dst
595 elif test $mod_dst = 160000
596 then
597 GIT_DIR="$name/.git" \
598 git log --pretty='format: > %s' -1 $sha1_dst
599 else
600 GIT_DIR="$name/.git" \
601 git log --pretty='format: < %s' -1 $sha1_src
603 echo
605 echo
606 done |
607 if test -n "$for_status"; then
608 echo "# Modified submodules:"
609 echo "#"
610 sed -e 's|^|# |' -e 's|^# $|#|'
611 else
616 # List all submodules, prefixed with:
617 # - submodule not initialized
618 # + different revision checked out
620 # If --cached was specified the revision in the index will be printed
621 # instead of the currently checked out revision.
623 # $@ = requested paths (default to all)
625 cmd_status()
627 # parse $args after "submodule ... status".
628 while test $# -ne 0
630 case "$1" in
631 -q|--quiet)
632 quiet=1
634 --cached)
635 cached=1
638 shift
639 break
642 usage
645 break
647 esac
648 shift
649 done
651 module_list "$@" |
652 while read mode sha1 stage path
654 name=$(module_name "$path") || exit
655 url=$(git config submodule."$name".url)
656 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
657 then
658 say "-$sha1 $path"
659 continue;
661 set_name_rev "$path" "$sha1"
662 if git diff-files --quiet -- "$path"
663 then
664 say " $sha1 $path$revname"
665 else
666 if test -z "$cached"
667 then
668 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
669 set_name_rev "$path" "$sha1"
671 say "+$sha1 $path$revname"
673 done
676 # Sync remote urls for submodules
677 # This makes the value for remote.$remote.url match the value
678 # specified in .gitmodules.
680 cmd_sync()
682 while test $# -ne 0
684 case "$1" in
685 -q|--quiet)
686 quiet=1
687 shift
690 shift
691 break
694 usage
697 break
699 esac
700 done
701 cd_to_toplevel
702 module_list "$@" |
703 while read mode sha1 stage path
705 name=$(module_name "$path")
706 url=$(git config -f .gitmodules --get submodule."$name".url)
708 # Possibly a url relative to parent
709 case "$url" in
710 ./*|../*)
711 url=$(resolve_relative_url "$url") || exit
713 esac
715 if test -e "$path"/.git
716 then
718 unset GIT_DIR
719 cd "$path"
720 remote=$(get_default_remote)
721 say "Synchronizing submodule url for '$name'"
722 git config remote."$remote".url "$url"
725 done
728 # This loop parses the command line arguments to find the
729 # subcommand name to dispatch. Parsing of the subcommand specific
730 # options are primarily done by the subcommand implementations.
731 # Subcommand specific options such as --branch and --cached are
732 # parsed here as well, for backward compatibility.
734 while test $# != 0 && test -z "$command"
736 case "$1" in
737 add | foreach | init | update | status | summary | sync)
738 command=$1
740 -q|--quiet)
741 quiet=1
743 -b|--branch)
744 case "$2" in
746 usage
748 esac
749 branch="$2"; shift
751 --cached)
752 cached="$1"
755 break
758 usage
761 break
763 esac
764 shift
765 done
767 # No command word defaults to "status"
768 test -n "$command" || command=status
770 # "-b branch" is accepted only by "add"
771 if test -n "$branch" && test "$command" != add
772 then
773 usage
776 # "--cached" is accepted only by "status" and "summary"
777 if test -n "$cached" && test "$command" != status -a "$command" != summary
778 then
779 usage
782 "cmd_$command" "$@"