am, rebase: teach quiet option
[git/dscho.git] / git-submodule.sh
blob58d2fd2ccb65925e81b1aab3c8e624359b286a14
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]|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 reference=
18 cached=
19 nofetch=
20 update=
22 # Resolve relative url by appending to parent's url
23 resolve_relative_url ()
25 remote=$(get_default_remote)
26 remoteurl=$(git config "remote.$remote.url") ||
27 die "remote ($remote) does not have a url defined in .git/config"
28 url="$1"
29 remoteurl=${remoteurl%/}
30 while test -n "$url"
32 case "$url" in
33 ../*)
34 url="${url#../}"
35 remoteurl="${remoteurl%/*}"
37 ./*)
38 url="${url#./}"
41 break;;
42 esac
43 done
44 echo "$remoteurl/${url%/}"
48 # Get submodule info for registered submodules
49 # $@ = path to limit submodule list
51 module_list()
53 git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
57 # Map submodule path to submodule name
59 # $1 = path
61 module_name()
63 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
64 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
65 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
66 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
67 test -z "$name" &&
68 die "No submodule mapping found in .gitmodules for path '$path'"
69 echo "$name"
73 # Clone a submodule
75 # Prior to calling, cmd_update checks that a possibly existing
76 # path is not a git repository.
77 # Likewise, cmd_add checks that path does not exist at all,
78 # since it is the location of a new submodule.
80 module_clone()
82 path=$1
83 url=$2
84 reference="$3"
86 # If there already is a directory at the submodule path,
87 # expect it to be empty (since that is the default checkout
88 # action) and try to remove it.
89 # Note: if $path is a symlink to a directory the test will
90 # succeed but the rmdir will fail. We might want to fix this.
91 if test -d "$path"
92 then
93 rmdir "$path" 2>/dev/null ||
94 die "Directory '$path' exist, but is neither empty nor a git repository"
97 test -e "$path" &&
98 die "A file already exist at path '$path'"
100 if test -n "$reference"
101 then
102 git-clone "$reference" -n "$url" "$path"
103 else
104 git-clone -n "$url" "$path"
105 fi ||
106 die "Clone of '$url' into submodule path '$path' failed"
110 # Add a new submodule to the working tree, .gitmodules and the index
112 # $@ = repo path
114 # optional branch is stored in global branch variable
116 cmd_add()
118 # parse $args after "submodule ... add".
119 while test $# -ne 0
121 case "$1" in
122 -b | --branch)
123 case "$2" in '') usage ;; esac
124 branch=$2
125 shift
127 -q|--quiet)
128 GIT_QUIET=1
130 --reference)
131 case "$2" in '') usage ;; esac
132 reference="--reference=$2"
133 shift
135 --reference=*)
136 reference="$1"
137 shift
140 shift
141 break
144 usage
147 break
149 esac
150 shift
151 done
153 repo=$1
154 path=$2
156 if test -z "$repo" -o -z "$path"; then
157 usage
160 # assure repo is absolute or relative to parent
161 case "$repo" in
162 ./*|../*)
163 # dereference source url relative to parent's url
164 realrepo=$(resolve_relative_url "$repo") || exit
166 *:*|/*)
167 # absolute url
168 realrepo=$repo
171 die "repo URL: '$repo' must be absolute or begin with ./|../"
173 esac
175 # normalize path:
176 # multiple //; leading ./; /./; /../; trailing /
177 path=$(printf '%s/\n' "$path" |
178 sed -e '
179 s|//*|/|g
180 s|^\(\./\)*||
181 s|/\./|/|g
182 :start
183 s|\([^/]*\)/\.\./||
184 tstart
185 s|/*$||
187 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
188 die "'$path' already exists in the index"
190 # perhaps the path exists and is already a git repo, else clone it
191 if test -e "$path"
192 then
193 if test -d "$path"/.git -o -f "$path"/.git
194 then
195 echo "Adding existing repo at '$path' to the index"
196 else
197 die "'$path' already exists and is not a valid git repo"
200 case "$repo" in
201 ./*|../*)
202 url=$(resolve_relative_url "$repo") || exit
205 url="$repo"
207 esac
208 git config submodule."$path".url "$url"
209 else
211 module_clone "$path" "$realrepo" "$reference" || exit
213 unset GIT_DIR
214 cd "$path" &&
215 # ash fails to wordsplit ${branch:+-b "$branch"...}
216 case "$branch" in
217 '') git checkout -f -q ;;
218 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
219 esac
220 ) || die "Unable to checkout submodule '$path'"
223 git add "$path" ||
224 die "Failed to add submodule '$path'"
226 git config -f .gitmodules submodule."$path".path "$path" &&
227 git config -f .gitmodules submodule."$path".url "$repo" &&
228 git add .gitmodules ||
229 die "Failed to register submodule '$path'"
233 # Execute an arbitrary command sequence in each checked out
234 # submodule
236 # $@ = command to execute
238 cmd_foreach()
240 module_list |
241 while read mode sha1 stage path
243 if test -e "$path"/.git
244 then
245 say "Entering '$path'"
246 (cd "$path" && eval "$@") ||
247 die "Stopping at '$path'; script returned non-zero status."
249 done
253 # Register submodules in .git/config
255 # $@ = requested paths (default to all)
257 cmd_init()
259 # parse $args after "submodule ... init".
260 while test $# -ne 0
262 case "$1" in
263 -q|--quiet)
264 GIT_QUIET=1
267 shift
268 break
271 usage
274 break
276 esac
277 shift
278 done
280 module_list "$@" |
281 while read mode sha1 stage path
283 # Skip already registered paths
284 name=$(module_name "$path") || exit
285 url=$(git config submodule."$name".url)
286 test -z "$url" || continue
288 url=$(git config -f .gitmodules submodule."$name".url)
289 test -z "$url" &&
290 die "No url found for submodule path '$path' in .gitmodules"
292 # Possibly a url relative to parent
293 case "$url" in
294 ./*|../*)
295 url=$(resolve_relative_url "$url") || exit
297 esac
299 git config submodule."$name".url "$url" ||
300 die "Failed to register url for submodule path '$path'"
302 upd="$(git config -f .gitmodules submodule."$name".update)"
303 test -z "$upd" ||
304 git config submodule."$name".update "$upd" ||
305 die "Failed to register update mode for submodule path '$path'"
307 say "Submodule '$name' ($url) registered for path '$path'"
308 done
312 # Update each submodule path to correct revision, using clone and checkout as needed
314 # $@ = requested paths (default to all)
316 cmd_update()
318 # parse $args after "submodule ... update".
319 while test $# -ne 0
321 case "$1" in
322 -q|--quiet)
323 shift
324 GIT_QUIET=1
326 -i|--init)
327 init=1
328 shift
330 -N|--no-fetch)
331 shift
332 nofetch=1
334 -r|--rebase)
335 shift
336 update="rebase"
338 --reference)
339 case "$2" in '') usage ;; esac
340 reference="--reference=$2"
341 shift 2
343 --reference=*)
344 reference="$1"
345 shift
348 shift
349 break
352 usage
355 break
357 esac
358 done
360 if test -n "$init"
361 then
362 cmd_init "--" "$@" || return
365 module_list "$@" |
366 while read mode sha1 stage path
368 name=$(module_name "$path") || exit
369 url=$(git config submodule."$name".url)
370 update_module=$(git config submodule."$name".update)
371 if test -z "$url"
372 then
373 # Only mention uninitialized submodules when its
374 # path have been specified
375 test "$#" != "0" &&
376 say "Submodule path '$path' not initialized" &&
377 say "Maybe you want to use 'update --init'?"
378 continue
381 if ! test -d "$path"/.git -o -f "$path"/.git
382 then
383 module_clone "$path" "$url" "$reference"|| exit
384 subsha1=
385 else
386 subsha1=$(unset GIT_DIR; cd "$path" &&
387 git rev-parse --verify HEAD) ||
388 die "Unable to find current revision in submodule path '$path'"
391 if ! test -z "$update"
392 then
393 update_module=$update
396 if test "$subsha1" != "$sha1"
397 then
398 force=
399 if test -z "$subsha1"
400 then
401 force="-f"
404 if test -z "$nofetch"
405 then
406 (unset GIT_DIR; cd "$path" &&
407 git-fetch) ||
408 die "Unable to fetch in submodule path '$path'"
411 case "$update_module" in
412 rebase)
413 command="git rebase"
414 action="rebase"
415 msg="rebased onto"
418 command="git checkout $force -q"
419 action="checkout"
420 msg="checked out"
422 esac
424 (unset GIT_DIR; cd "$path" && $command "$sha1") ||
425 die "Unable to $action '$sha1' in submodule path '$path'"
426 say "Submodule path '$path': $msg '$sha1'"
428 done
431 set_name_rev () {
432 revname=$( (
433 unset GIT_DIR
434 cd "$1" && {
435 git describe "$2" 2>/dev/null ||
436 git describe --tags "$2" 2>/dev/null ||
437 git describe --contains "$2" 2>/dev/null ||
438 git describe --all --always "$2"
441 test -z "$revname" || revname=" ($revname)"
444 # Show commit summary for submodules in index or working tree
446 # If '--cached' is given, show summary between index and given commit,
447 # or between working tree and given commit
449 # $@ = [commit (default 'HEAD'),] requested paths (default all)
451 cmd_summary() {
452 summary_limit=-1
453 for_status=
455 # parse $args after "submodule ... summary".
456 while test $# -ne 0
458 case "$1" in
459 --cached)
460 cached="$1"
462 --for-status)
463 for_status="$1"
465 -n|--summary-limit)
466 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
467 then
469 else
470 usage
472 shift
475 shift
476 break
479 usage
482 break
484 esac
485 shift
486 done
488 test $summary_limit = 0 && return
490 if rev=$(git rev-parse -q --verify "$1^0")
491 then
492 head=$rev
493 shift
494 else
495 head=HEAD
498 cd_to_toplevel
499 # Get modified modules cared by user
500 modules=$(git diff-index $cached --raw $head -- "$@" |
501 egrep '^:([0-7]* )?160000' |
502 while read mod_src mod_dst sha1_src sha1_dst status name
504 # Always show modules deleted or type-changed (blob<->module)
505 test $status = D -o $status = T && echo "$name" && continue
506 # Also show added or modified modules which are checked out
507 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
508 echo "$name"
509 done
512 test -z "$modules" && return
514 git diff-index $cached --raw $head -- $modules |
515 egrep '^:([0-7]* )?160000' |
516 cut -c2- |
517 while read mod_src mod_dst sha1_src sha1_dst status name
519 if test -z "$cached" &&
520 test $sha1_dst = 0000000000000000000000000000000000000000
521 then
522 case "$mod_dst" in
523 160000)
524 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
526 100644 | 100755 | 120000)
527 sha1_dst=$(git hash-object $name)
529 000000)
530 ;; # removed
532 # unexpected type
533 echo >&2 "unexpected mode $mod_dst"
534 continue ;;
535 esac
537 missing_src=
538 missing_dst=
540 test $mod_src = 160000 &&
541 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
542 missing_src=t
544 test $mod_dst = 160000 &&
545 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
546 missing_dst=t
548 total_commits=
549 case "$missing_src,$missing_dst" in
551 errmsg=" Warn: $name doesn't contain commit $sha1_src"
554 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
556 t,t)
557 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
560 errmsg=
561 total_commits=$(
562 if test $mod_src = 160000 -a $mod_dst = 160000
563 then
564 range="$sha1_src...$sha1_dst"
565 elif test $mod_src = 160000
566 then
567 range=$sha1_src
568 else
569 range=$sha1_dst
571 GIT_DIR="$name/.git" \
572 git log --pretty=oneline --first-parent $range | wc -l
574 total_commits=" ($(($total_commits + 0)))"
576 esac
578 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
579 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
580 if test $status = T
581 then
582 if test $mod_dst = 160000
583 then
584 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
585 else
586 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
588 else
589 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
591 if test -n "$errmsg"
592 then
593 # Don't give error msg for modification whose dst is not submodule
594 # i.e. deleted or changed to blob
595 test $mod_dst = 160000 && echo "$errmsg"
596 else
597 if test $mod_src = 160000 -a $mod_dst = 160000
598 then
599 limit=
600 test $summary_limit -gt 0 && limit="-$summary_limit"
601 GIT_DIR="$name/.git" \
602 git log $limit --pretty='format: %m %s' \
603 --first-parent $sha1_src...$sha1_dst
604 elif test $mod_dst = 160000
605 then
606 GIT_DIR="$name/.git" \
607 git log --pretty='format: > %s' -1 $sha1_dst
608 else
609 GIT_DIR="$name/.git" \
610 git log --pretty='format: < %s' -1 $sha1_src
612 echo
614 echo
615 done |
616 if test -n "$for_status"; then
617 echo "# Modified submodules:"
618 echo "#"
619 sed -e 's|^|# |' -e 's|^# $|#|'
620 else
625 # List all submodules, prefixed with:
626 # - submodule not initialized
627 # + different revision checked out
629 # If --cached was specified the revision in the index will be printed
630 # instead of the currently checked out revision.
632 # $@ = requested paths (default to all)
634 cmd_status()
636 # parse $args after "submodule ... status".
637 while test $# -ne 0
639 case "$1" in
640 -q|--quiet)
641 GIT_QUIET=1
643 --cached)
644 cached=1
647 shift
648 break
651 usage
654 break
656 esac
657 shift
658 done
660 module_list "$@" |
661 while read mode sha1 stage path
663 name=$(module_name "$path") || exit
664 url=$(git config submodule."$name".url)
665 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
666 then
667 say "-$sha1 $path"
668 continue;
670 set_name_rev "$path" "$sha1"
671 if git diff-files --quiet -- "$path"
672 then
673 say " $sha1 $path$revname"
674 else
675 if test -z "$cached"
676 then
677 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
678 set_name_rev "$path" "$sha1"
680 say "+$sha1 $path$revname"
682 done
685 # Sync remote urls for submodules
686 # This makes the value for remote.$remote.url match the value
687 # specified in .gitmodules.
689 cmd_sync()
691 while test $# -ne 0
693 case "$1" in
694 -q|--quiet)
695 GIT_QUIET=1
696 shift
699 shift
700 break
703 usage
706 break
708 esac
709 done
710 cd_to_toplevel
711 module_list "$@" |
712 while read mode sha1 stage path
714 name=$(module_name "$path")
715 url=$(git config -f .gitmodules --get submodule."$name".url)
717 # Possibly a url relative to parent
718 case "$url" in
719 ./*|../*)
720 url=$(resolve_relative_url "$url") || exit
722 esac
724 if test -e "$path"/.git
725 then
727 unset GIT_DIR
728 cd "$path"
729 remote=$(get_default_remote)
730 say "Synchronizing submodule url for '$name'"
731 git config remote."$remote".url "$url"
734 done
737 # This loop parses the command line arguments to find the
738 # subcommand name to dispatch. Parsing of the subcommand specific
739 # options are primarily done by the subcommand implementations.
740 # Subcommand specific options such as --branch and --cached are
741 # parsed here as well, for backward compatibility.
743 while test $# != 0 && test -z "$command"
745 case "$1" in
746 add | foreach | init | update | status | summary | sync)
747 command=$1
749 -q|--quiet)
750 GIT_QUIET=1
752 -b|--branch)
753 case "$2" in
755 usage
757 esac
758 branch="$2"; shift
760 --cached)
761 cached="$1"
764 break
767 usage
770 break
772 esac
773 shift
774 done
776 # No command word defaults to "status"
777 test -n "$command" || command=status
779 # "-b branch" is accepted only by "add"
780 if test -n "$branch" && test "$command" != add
781 then
782 usage
785 # "--cached" is accepted only by "status" and "summary"
786 if test -n "$cached" && test "$command" != status -a "$command" != summary
787 then
788 usage
791 "cmd_$command" "$@"