git submodule: Cleanup usage string and add option parsing to cmd_foreach()
[git.git] / git-submodule.sh
blobf48f682ab6d54f8c1f016acaea8d80a9ef33a6c4
1 #!/bin/sh
3 # git-submodules.sh: add, init, update or list git submodules
5 # Copyright (c) 2007 Lars Hjemli
7 dashless=$(basename "$0" | sed -e 's/-/ /')
8 USAGE="[--quiet] add [-b branch] [--reference <repository>] [--] <repository> <path>
9 or: $dashless [--quiet] status [--cached] [--] [<path>...]
10 or: $dashless [--quiet] init [--] [<path>...]
11 or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--] [<path>...]
12 or: $dashless [--quiet] summary [--cached] [--summary-limit <n>] [commit] [--] [<path>...]
13 or: $dashless [--quiet] foreach <command>
14 or: $dashless [--quiet] sync [--] [<path>...]"
15 OPTIONS_SPEC=
16 . git-sh-setup
17 . git-parse-remote
18 require_work_tree
20 command=
21 branch=
22 reference=
23 cached=
24 nofetch=
25 update=
27 # Resolve relative url by appending to parent's url
28 resolve_relative_url ()
30 remote=$(get_default_remote)
31 remoteurl=$(git config "remote.$remote.url") ||
32 die "remote ($remote) does not have a url defined in .git/config"
33 url="$1"
34 remoteurl=${remoteurl%/}
35 while test -n "$url"
37 case "$url" in
38 ../*)
39 url="${url#../}"
40 remoteurl="${remoteurl%/*}"
42 ./*)
43 url="${url#./}"
46 break;;
47 esac
48 done
49 echo "$remoteurl/${url%/}"
53 # Get submodule info for registered submodules
54 # $@ = path to limit submodule list
56 module_list()
58 git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
62 # Map submodule path to submodule name
64 # $1 = path
66 module_name()
68 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
69 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
70 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
71 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
72 test -z "$name" &&
73 die "No submodule mapping found in .gitmodules for path '$path'"
74 echo "$name"
78 # Clone a submodule
80 # Prior to calling, cmd_update checks that a possibly existing
81 # path is not a git repository.
82 # Likewise, cmd_add checks that path does not exist at all,
83 # since it is the location of a new submodule.
85 module_clone()
87 path=$1
88 url=$2
89 reference="$3"
91 # If there already is a directory at the submodule path,
92 # expect it to be empty (since that is the default checkout
93 # action) and try to remove it.
94 # Note: if $path is a symlink to a directory the test will
95 # succeed but the rmdir will fail. We might want to fix this.
96 if test -d "$path"
97 then
98 rmdir "$path" 2>/dev/null ||
99 die "Directory '$path' exist, but is neither empty nor a git repository"
102 test -e "$path" &&
103 die "A file already exist at path '$path'"
105 if test -n "$reference"
106 then
107 git-clone "$reference" -n "$url" "$path"
108 else
109 git-clone -n "$url" "$path"
110 fi ||
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 GIT_QUIET=1
135 --reference)
136 case "$2" in '') usage ;; esac
137 reference="--reference=$2"
138 shift
140 --reference=*)
141 reference="$1"
142 shift
145 shift
146 break
149 usage
152 break
154 esac
155 shift
156 done
158 repo=$1
159 path=$2
161 if test -z "$repo" -o -z "$path"; then
162 usage
165 # assure repo is absolute or relative to parent
166 case "$repo" in
167 ./*|../*)
168 # dereference source url relative to parent's url
169 realrepo=$(resolve_relative_url "$repo") || exit
171 *:*|/*)
172 # absolute url
173 realrepo=$repo
176 die "repo URL: '$repo' must be absolute or begin with ./|../"
178 esac
180 # normalize path:
181 # multiple //; leading ./; /./; /../; trailing /
182 path=$(printf '%s/\n' "$path" |
183 sed -e '
184 s|//*|/|g
185 s|^\(\./\)*||
186 s|/\./|/|g
187 :start
188 s|\([^/]*\)/\.\./||
189 tstart
190 s|/*$||
192 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
193 die "'$path' already exists in the index"
195 # perhaps the path exists and is already a git repo, else clone it
196 if test -e "$path"
197 then
198 if test -d "$path"/.git -o -f "$path"/.git
199 then
200 echo "Adding existing repo at '$path' to the index"
201 else
202 die "'$path' already exists and is not a valid git repo"
205 case "$repo" in
206 ./*|../*)
207 url=$(resolve_relative_url "$repo") || exit
210 url="$repo"
212 esac
213 git config submodule."$path".url "$url"
214 else
216 module_clone "$path" "$realrepo" "$reference" || exit
218 unset GIT_DIR
219 cd "$path" &&
220 # ash fails to wordsplit ${branch:+-b "$branch"...}
221 case "$branch" in
222 '') git checkout -f -q ;;
223 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
224 esac
225 ) || die "Unable to checkout submodule '$path'"
228 git add "$path" ||
229 die "Failed to add submodule '$path'"
231 git config -f .gitmodules submodule."$path".path "$path" &&
232 git config -f .gitmodules submodule."$path".url "$repo" &&
233 git add .gitmodules ||
234 die "Failed to register submodule '$path'"
238 # Execute an arbitrary command sequence in each checked out
239 # submodule
241 # $@ = command to execute
243 cmd_foreach()
245 # parse $args after "submodule ... foreach".
246 while test $# -ne 0
248 case "$1" in
249 -q|--quiet)
250 GIT_QUIET=1
253 usage
256 break
258 esac
259 shift
260 done
262 module_list |
263 while read mode sha1 stage path
265 if test -e "$path"/.git
266 then
267 say "Entering '$path'"
268 name=$(module_name "$path")
269 (cd "$path" && eval "$@") ||
270 die "Stopping at '$path'; script returned non-zero status."
272 done
276 # Register submodules in .git/config
278 # $@ = requested paths (default to all)
280 cmd_init()
282 # parse $args after "submodule ... init".
283 while test $# -ne 0
285 case "$1" in
286 -q|--quiet)
287 GIT_QUIET=1
290 shift
291 break
294 usage
297 break
299 esac
300 shift
301 done
303 module_list "$@" |
304 while read mode sha1 stage path
306 # Skip already registered paths
307 name=$(module_name "$path") || exit
308 url=$(git config submodule."$name".url)
309 test -z "$url" || continue
311 url=$(git config -f .gitmodules submodule."$name".url)
312 test -z "$url" &&
313 die "No url found for submodule path '$path' in .gitmodules"
315 # Possibly a url relative to parent
316 case "$url" in
317 ./*|../*)
318 url=$(resolve_relative_url "$url") || exit
320 esac
322 git config submodule."$name".url "$url" ||
323 die "Failed to register url for submodule path '$path'"
325 upd="$(git config -f .gitmodules submodule."$name".update)"
326 test -z "$upd" ||
327 git config submodule."$name".update "$upd" ||
328 die "Failed to register update mode for submodule path '$path'"
330 say "Submodule '$name' ($url) registered for path '$path'"
331 done
335 # Update each submodule path to correct revision, using clone and checkout as needed
337 # $@ = requested paths (default to all)
339 cmd_update()
341 # parse $args after "submodule ... update".
342 while test $# -ne 0
344 case "$1" in
345 -q|--quiet)
346 shift
347 GIT_QUIET=1
349 -i|--init)
350 init=1
351 shift
353 -N|--no-fetch)
354 shift
355 nofetch=1
357 -r|--rebase)
358 shift
359 update="rebase"
361 --reference)
362 case "$2" in '') usage ;; esac
363 reference="--reference=$2"
364 shift 2
366 --reference=*)
367 reference="$1"
368 shift
370 -m|--merge)
371 shift
372 update="merge"
375 shift
376 break
379 usage
382 break
384 esac
385 done
387 if test -n "$init"
388 then
389 cmd_init "--" "$@" || return
392 module_list "$@" |
393 while read mode sha1 stage path
395 name=$(module_name "$path") || exit
396 url=$(git config submodule."$name".url)
397 update_module=$(git config submodule."$name".update)
398 if test -z "$url"
399 then
400 # Only mention uninitialized submodules when its
401 # path have been specified
402 test "$#" != "0" &&
403 say "Submodule path '$path' not initialized" &&
404 say "Maybe you want to use 'update --init'?"
405 continue
408 if ! test -d "$path"/.git -o -f "$path"/.git
409 then
410 module_clone "$path" "$url" "$reference"|| exit
411 subsha1=
412 else
413 subsha1=$(unset GIT_DIR; cd "$path" &&
414 git rev-parse --verify HEAD) ||
415 die "Unable to find current revision in submodule path '$path'"
418 if ! test -z "$update"
419 then
420 update_module=$update
423 if test "$subsha1" != "$sha1"
424 then
425 force=
426 if test -z "$subsha1"
427 then
428 force="-f"
431 if test -z "$nofetch"
432 then
433 (unset GIT_DIR; cd "$path" &&
434 git-fetch) ||
435 die "Unable to fetch in submodule path '$path'"
438 case "$update_module" in
439 rebase)
440 command="git rebase"
441 action="rebase"
442 msg="rebased onto"
444 merge)
445 command="git merge"
446 action="merge"
447 msg="merged in"
450 command="git checkout $force -q"
451 action="checkout"
452 msg="checked out"
454 esac
456 (unset GIT_DIR; cd "$path" && $command "$sha1") ||
457 die "Unable to $action '$sha1' in submodule path '$path'"
458 say "Submodule path '$path': $msg '$sha1'"
460 done
463 set_name_rev () {
464 revname=$( (
465 unset GIT_DIR
466 cd "$1" && {
467 git describe "$2" 2>/dev/null ||
468 git describe --tags "$2" 2>/dev/null ||
469 git describe --contains "$2" 2>/dev/null ||
470 git describe --all --always "$2"
473 test -z "$revname" || revname=" ($revname)"
476 # Show commit summary for submodules in index or working tree
478 # If '--cached' is given, show summary between index and given commit,
479 # or between working tree and given commit
481 # $@ = [commit (default 'HEAD'),] requested paths (default all)
483 cmd_summary() {
484 summary_limit=-1
485 for_status=
487 # parse $args after "submodule ... summary".
488 while test $# -ne 0
490 case "$1" in
491 --cached)
492 cached="$1"
494 --for-status)
495 for_status="$1"
497 -n|--summary-limit)
498 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
499 then
501 else
502 usage
504 shift
507 shift
508 break
511 usage
514 break
516 esac
517 shift
518 done
520 test $summary_limit = 0 && return
522 if rev=$(git rev-parse -q --verify "$1^0")
523 then
524 head=$rev
525 shift
526 else
527 head=HEAD
530 cd_to_toplevel
531 # Get modified modules cared by user
532 modules=$(git diff-index $cached --raw $head -- "$@" |
533 egrep '^:([0-7]* )?160000' |
534 while read mod_src mod_dst sha1_src sha1_dst status name
536 # Always show modules deleted or type-changed (blob<->module)
537 test $status = D -o $status = T && echo "$name" && continue
538 # Also show added or modified modules which are checked out
539 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
540 echo "$name"
541 done
544 test -z "$modules" && return
546 git diff-index $cached --raw $head -- $modules |
547 egrep '^:([0-7]* )?160000' |
548 cut -c2- |
549 while read mod_src mod_dst sha1_src sha1_dst status name
551 if test -z "$cached" &&
552 test $sha1_dst = 0000000000000000000000000000000000000000
553 then
554 case "$mod_dst" in
555 160000)
556 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
558 100644 | 100755 | 120000)
559 sha1_dst=$(git hash-object $name)
561 000000)
562 ;; # removed
564 # unexpected type
565 echo >&2 "unexpected mode $mod_dst"
566 continue ;;
567 esac
569 missing_src=
570 missing_dst=
572 test $mod_src = 160000 &&
573 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
574 missing_src=t
576 test $mod_dst = 160000 &&
577 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
578 missing_dst=t
580 total_commits=
581 case "$missing_src,$missing_dst" in
583 errmsg=" Warn: $name doesn't contain commit $sha1_src"
586 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
588 t,t)
589 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
592 errmsg=
593 total_commits=$(
594 if test $mod_src = 160000 -a $mod_dst = 160000
595 then
596 range="$sha1_src...$sha1_dst"
597 elif test $mod_src = 160000
598 then
599 range=$sha1_src
600 else
601 range=$sha1_dst
603 GIT_DIR="$name/.git" \
604 git log --pretty=oneline --first-parent $range | wc -l
606 total_commits=" ($(($total_commits + 0)))"
608 esac
610 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
611 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
612 if test $status = T
613 then
614 if test $mod_dst = 160000
615 then
616 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
617 else
618 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
620 else
621 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
623 if test -n "$errmsg"
624 then
625 # Don't give error msg for modification whose dst is not submodule
626 # i.e. deleted or changed to blob
627 test $mod_dst = 160000 && echo "$errmsg"
628 else
629 if test $mod_src = 160000 -a $mod_dst = 160000
630 then
631 limit=
632 test $summary_limit -gt 0 && limit="-$summary_limit"
633 GIT_DIR="$name/.git" \
634 git log $limit --pretty='format: %m %s' \
635 --first-parent $sha1_src...$sha1_dst
636 elif test $mod_dst = 160000
637 then
638 GIT_DIR="$name/.git" \
639 git log --pretty='format: > %s' -1 $sha1_dst
640 else
641 GIT_DIR="$name/.git" \
642 git log --pretty='format: < %s' -1 $sha1_src
644 echo
646 echo
647 done |
648 if test -n "$for_status"; then
649 echo "# Modified submodules:"
650 echo "#"
651 sed -e 's|^|# |' -e 's|^# $|#|'
652 else
657 # List all submodules, prefixed with:
658 # - submodule not initialized
659 # + different revision checked out
661 # If --cached was specified the revision in the index will be printed
662 # instead of the currently checked out revision.
664 # $@ = requested paths (default to all)
666 cmd_status()
668 # parse $args after "submodule ... status".
669 while test $# -ne 0
671 case "$1" in
672 -q|--quiet)
673 GIT_QUIET=1
675 --cached)
676 cached=1
679 shift
680 break
683 usage
686 break
688 esac
689 shift
690 done
692 module_list "$@" |
693 while read mode sha1 stage path
695 name=$(module_name "$path") || exit
696 url=$(git config submodule."$name".url)
697 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
698 then
699 say "-$sha1 $path"
700 continue;
702 set_name_rev "$path" "$sha1"
703 if git diff-files --quiet -- "$path"
704 then
705 say " $sha1 $path$revname"
706 else
707 if test -z "$cached"
708 then
709 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
710 set_name_rev "$path" "$sha1"
712 say "+$sha1 $path$revname"
714 done
717 # Sync remote urls for submodules
718 # This makes the value for remote.$remote.url match the value
719 # specified in .gitmodules.
721 cmd_sync()
723 while test $# -ne 0
725 case "$1" in
726 -q|--quiet)
727 GIT_QUIET=1
728 shift
731 shift
732 break
735 usage
738 break
740 esac
741 done
742 cd_to_toplevel
743 module_list "$@" |
744 while read mode sha1 stage path
746 name=$(module_name "$path")
747 url=$(git config -f .gitmodules --get submodule."$name".url)
749 # Possibly a url relative to parent
750 case "$url" in
751 ./*|../*)
752 url=$(resolve_relative_url "$url") || exit
754 esac
756 if test -e "$path"/.git
757 then
759 unset GIT_DIR
760 cd "$path"
761 remote=$(get_default_remote)
762 say "Synchronizing submodule url for '$name'"
763 git config remote."$remote".url "$url"
766 done
769 # This loop parses the command line arguments to find the
770 # subcommand name to dispatch. Parsing of the subcommand specific
771 # options are primarily done by the subcommand implementations.
772 # Subcommand specific options such as --branch and --cached are
773 # parsed here as well, for backward compatibility.
775 while test $# != 0 && test -z "$command"
777 case "$1" in
778 add | foreach | init | update | status | summary | sync)
779 command=$1
781 -q|--quiet)
782 GIT_QUIET=1
784 -b|--branch)
785 case "$2" in
787 usage
789 esac
790 branch="$2"; shift
792 --cached)
793 cached="$1"
796 break
799 usage
802 break
804 esac
805 shift
806 done
808 # No command word defaults to "status"
809 test -n "$command" || command=status
811 # "-b branch" is accepted only by "add"
812 if test -n "$branch" && test "$command" != add
813 then
814 usage
817 # "--cached" is accepted only by "status" and "summary"
818 if test -n "$cached" && test "$command" != status -a "$command" != summary
819 then
820 usage
823 "cmd_$command" "$@"