t5551-http-fetch: Work around some libcurl versions
[git/jnareb-git/bp-gitweb.git] / git-submodule.sh
blob0462e529d934750fb63a5a897f135d60ac674b5e
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] [--recursive] [--] [<path>...]
10 or: $dashless [--quiet] init [--] [<path>...]
11 or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
12 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
13 or: $dashless [--quiet] foreach [--recursive] <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 files=
25 nofetch=
26 update=
27 prefix=
29 # Resolve relative url by appending to parent's url
30 resolve_relative_url ()
32 remote=$(get_default_remote)
33 remoteurl=$(git config "remote.$remote.url") ||
34 die "remote ($remote) does not have a url defined in .git/config"
35 url="$1"
36 remoteurl=${remoteurl%/}
37 while test -n "$url"
39 case "$url" in
40 ../*)
41 url="${url#../}"
42 remoteurl="${remoteurl%/*}"
44 ./*)
45 url="${url#./}"
48 break;;
49 esac
50 done
51 echo "$remoteurl/${url%/}"
55 # Get submodule info for registered submodules
56 # $@ = path to limit submodule list
58 module_list()
60 git ls-files --error-unmatch --stage -- "$@" | grep '^160000 '
64 # Map submodule path to submodule name
66 # $1 = path
68 module_name()
70 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
71 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
72 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
73 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
74 test -z "$name" &&
75 die "No submodule mapping found in .gitmodules for path '$path'"
76 echo "$name"
80 # Clone a submodule
82 # Prior to calling, cmd_update checks that a possibly existing
83 # path is not a git repository.
84 # Likewise, cmd_add checks that path does not exist at all,
85 # since it is the location of a new submodule.
87 module_clone()
89 path=$1
90 url=$2
91 reference="$3"
93 # If there already is a directory at the submodule path,
94 # expect it to be empty (since that is the default checkout
95 # action) and try to remove it.
96 # Note: if $path is a symlink to a directory the test will
97 # succeed but the rmdir will fail. We might want to fix this.
98 if test -d "$path"
99 then
100 rmdir "$path" 2>/dev/null ||
101 die "Directory '$path' exists, but is neither empty nor a git repository"
104 test -e "$path" &&
105 die "A file already exist at path '$path'"
107 if test -n "$reference"
108 then
109 git-clone "$reference" -n "$url" "$path"
110 else
111 git-clone -n "$url" "$path"
112 fi ||
113 die "Clone of '$url' into submodule path '$path' failed"
117 # Add a new submodule to the working tree, .gitmodules and the index
119 # $@ = repo path
121 # optional branch is stored in global branch variable
123 cmd_add()
125 # parse $args after "submodule ... add".
126 while test $# -ne 0
128 case "$1" in
129 -b | --branch)
130 case "$2" in '') usage ;; esac
131 branch=$2
132 shift
134 -q|--quiet)
135 GIT_QUIET=1
137 --reference)
138 case "$2" in '') usage ;; esac
139 reference="--reference=$2"
140 shift
142 --reference=*)
143 reference="$1"
144 shift
147 shift
148 break
151 usage
154 break
156 esac
157 shift
158 done
160 repo=$1
161 path=$2
163 if test -z "$repo" -o -z "$path"; then
164 usage
167 # assure repo is absolute or relative to parent
168 case "$repo" in
169 ./*|../*)
170 # dereference source url relative to parent's url
171 realrepo=$(resolve_relative_url "$repo") || exit
173 *:*|/*)
174 # absolute url
175 realrepo=$repo
178 die "repo URL: '$repo' must be absolute or begin with ./|../"
180 esac
182 # normalize path:
183 # multiple //; leading ./; /./; /../; trailing /
184 path=$(printf '%s/\n' "$path" |
185 sed -e '
186 s|//*|/|g
187 s|^\(\./\)*||
188 s|/\./|/|g
189 :start
190 s|\([^/]*\)/\.\./||
191 tstart
192 s|/*$||
194 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
195 die "'$path' already exists in the index"
197 # perhaps the path exists and is already a git repo, else clone it
198 if test -e "$path"
199 then
200 if test -d "$path"/.git -o -f "$path"/.git
201 then
202 echo "Adding existing repo at '$path' to the index"
203 else
204 die "'$path' already exists and is not a valid git repo"
207 case "$repo" in
208 ./*|../*)
209 url=$(resolve_relative_url "$repo") || exit
212 url="$repo"
214 esac
215 git config submodule."$path".url "$url"
216 else
218 module_clone "$path" "$realrepo" "$reference" || exit
220 unset GIT_DIR
221 cd "$path" &&
222 # ash fails to wordsplit ${branch:+-b "$branch"...}
223 case "$branch" in
224 '') git checkout -f -q ;;
225 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
226 esac
227 ) || die "Unable to checkout submodule '$path'"
230 git add "$path" ||
231 die "Failed to add submodule '$path'"
233 git config -f .gitmodules submodule."$path".path "$path" &&
234 git config -f .gitmodules submodule."$path".url "$repo" &&
235 git add .gitmodules ||
236 die "Failed to register submodule '$path'"
240 # Execute an arbitrary command sequence in each checked out
241 # submodule
243 # $@ = command to execute
245 cmd_foreach()
247 # parse $args after "submodule ... foreach".
248 while test $# -ne 0
250 case "$1" in
251 -q|--quiet)
252 GIT_QUIET=1
254 --recursive)
255 recursive=1
258 usage
261 break
263 esac
264 shift
265 done
267 module_list |
268 while read mode sha1 stage path
270 if test -e "$path"/.git
271 then
272 say "Entering '$prefix$path'"
273 name=$(module_name "$path")
275 prefix="$prefix$path/"
276 unset GIT_DIR
277 cd "$path" &&
278 eval "$@" &&
279 if test -n "$recursive"
280 then
281 cmd_foreach "--recursive" "$@"
283 ) ||
284 die "Stopping at '$path'; script returned non-zero status."
286 done
290 # Register submodules in .git/config
292 # $@ = requested paths (default to all)
294 cmd_init()
296 # parse $args after "submodule ... init".
297 while test $# -ne 0
299 case "$1" in
300 -q|--quiet)
301 GIT_QUIET=1
304 shift
305 break
308 usage
311 break
313 esac
314 shift
315 done
317 module_list "$@" |
318 while read mode sha1 stage path
320 # Skip already registered paths
321 name=$(module_name "$path") || exit
322 url=$(git config submodule."$name".url)
323 test -z "$url" || continue
325 url=$(git config -f .gitmodules submodule."$name".url)
326 test -z "$url" &&
327 die "No url found for submodule path '$path' in .gitmodules"
329 # Possibly a url relative to parent
330 case "$url" in
331 ./*|../*)
332 url=$(resolve_relative_url "$url") || exit
334 esac
336 git config submodule."$name".url "$url" ||
337 die "Failed to register url for submodule path '$path'"
339 upd="$(git config -f .gitmodules submodule."$name".update)"
340 test -z "$upd" ||
341 git config submodule."$name".update "$upd" ||
342 die "Failed to register update mode for submodule path '$path'"
344 say "Submodule '$name' ($url) registered for path '$path'"
345 done
349 # Update each submodule path to correct revision, using clone and checkout as needed
351 # $@ = requested paths (default to all)
353 cmd_update()
355 # parse $args after "submodule ... update".
356 orig_args="$@"
357 while test $# -ne 0
359 case "$1" in
360 -q|--quiet)
361 shift
362 GIT_QUIET=1
364 -i|--init)
365 init=1
366 shift
368 -N|--no-fetch)
369 shift
370 nofetch=1
372 -r|--rebase)
373 shift
374 update="rebase"
376 --reference)
377 case "$2" in '') usage ;; esac
378 reference="--reference=$2"
379 shift 2
381 --reference=*)
382 reference="$1"
383 shift
385 -m|--merge)
386 shift
387 update="merge"
389 --recursive)
390 shift
391 recursive=1
394 shift
395 break
398 usage
401 break
403 esac
404 done
406 if test -n "$init"
407 then
408 cmd_init "--" "$@" || return
411 module_list "$@" |
412 while read mode sha1 stage path
414 name=$(module_name "$path") || exit
415 url=$(git config submodule."$name".url)
416 update_module=$(git config submodule."$name".update)
417 if test -z "$url"
418 then
419 # Only mention uninitialized submodules when its
420 # path have been specified
421 test "$#" != "0" &&
422 say "Submodule path '$path' not initialized" &&
423 say "Maybe you want to use 'update --init'?"
424 continue
427 if ! test -d "$path"/.git -o -f "$path"/.git
428 then
429 module_clone "$path" "$url" "$reference"|| exit
430 subsha1=
431 else
432 subsha1=$(unset GIT_DIR; cd "$path" &&
433 git rev-parse --verify HEAD) ||
434 die "Unable to find current revision in submodule path '$path'"
437 if ! test -z "$update"
438 then
439 update_module=$update
442 if test "$subsha1" != "$sha1"
443 then
444 force=
445 if test -z "$subsha1"
446 then
447 force="-f"
450 if test -z "$nofetch"
451 then
452 (unset GIT_DIR; cd "$path" &&
453 git-fetch) ||
454 die "Unable to fetch in submodule path '$path'"
457 case "$update_module" in
458 rebase)
459 command="git rebase"
460 action="rebase"
461 msg="rebased onto"
463 merge)
464 command="git merge"
465 action="merge"
466 msg="merged in"
469 command="git checkout $force -q"
470 action="checkout"
471 msg="checked out"
473 esac
475 (unset GIT_DIR; cd "$path" && $command "$sha1") ||
476 die "Unable to $action '$sha1' in submodule path '$path'"
477 say "Submodule path '$path': $msg '$sha1'"
480 if test -n "$recursive"
481 then
482 (unset GIT_DIR; cd "$path" && cmd_update $orig_args) ||
483 die "Failed to recurse into submodule path '$path'"
485 done
488 set_name_rev () {
489 revname=$( (
490 unset GIT_DIR
491 cd "$1" && {
492 git describe "$2" 2>/dev/null ||
493 git describe --tags "$2" 2>/dev/null ||
494 git describe --contains "$2" 2>/dev/null ||
495 git describe --all --always "$2"
498 test -z "$revname" || revname=" ($revname)"
501 # Show commit summary for submodules in index or working tree
503 # If '--cached' is given, show summary between index and given commit,
504 # or between working tree and given commit
506 # $@ = [commit (default 'HEAD'),] requested paths (default all)
508 cmd_summary() {
509 summary_limit=-1
510 for_status=
511 diff_cmd=diff-index
513 # parse $args after "submodule ... summary".
514 while test $# -ne 0
516 case "$1" in
517 --cached)
518 cached="$1"
520 --files)
521 files="$1"
523 --for-status)
524 for_status="$1"
526 -n|--summary-limit)
527 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
528 then
530 else
531 usage
533 shift
536 shift
537 break
540 usage
543 break
545 esac
546 shift
547 done
549 test $summary_limit = 0 && return
551 if rev=$(git rev-parse -q --verify "$1^0")
552 then
553 head=$rev
554 shift
555 else
556 head=HEAD
559 if [ -n "$files" ]
560 then
561 test -n "$cached" &&
562 die "--cached cannot be used with --files"
563 diff_cmd=diff-files
564 head=
567 cd_to_toplevel
568 # Get modified modules cared by user
569 modules=$(git $diff_cmd $cached --raw $head -- "$@" |
570 egrep '^:([0-7]* )?160000' |
571 while read mod_src mod_dst sha1_src sha1_dst status name
573 # Always show modules deleted or type-changed (blob<->module)
574 test $status = D -o $status = T && echo "$name" && continue
575 # Also show added or modified modules which are checked out
576 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
577 echo "$name"
578 done
581 test -z "$modules" && return
583 git $diff_cmd $cached --raw $head -- $modules |
584 egrep '^:([0-7]* )?160000' |
585 cut -c2- |
586 while read mod_src mod_dst sha1_src sha1_dst status name
588 if test -z "$cached" &&
589 test $sha1_dst = 0000000000000000000000000000000000000000
590 then
591 case "$mod_dst" in
592 160000)
593 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
595 100644 | 100755 | 120000)
596 sha1_dst=$(git hash-object $name)
598 000000)
599 ;; # removed
601 # unexpected type
602 echo >&2 "unexpected mode $mod_dst"
603 continue ;;
604 esac
606 missing_src=
607 missing_dst=
609 test $mod_src = 160000 &&
610 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
611 missing_src=t
613 test $mod_dst = 160000 &&
614 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
615 missing_dst=t
617 total_commits=
618 case "$missing_src,$missing_dst" in
620 errmsg=" Warn: $name doesn't contain commit $sha1_src"
623 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
625 t,t)
626 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
629 errmsg=
630 total_commits=$(
631 if test $mod_src = 160000 -a $mod_dst = 160000
632 then
633 range="$sha1_src...$sha1_dst"
634 elif test $mod_src = 160000
635 then
636 range=$sha1_src
637 else
638 range=$sha1_dst
640 GIT_DIR="$name/.git" \
641 git log --pretty=oneline --first-parent $range | wc -l
643 total_commits=" ($(($total_commits + 0)))"
645 esac
647 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
648 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
649 if test $status = T
650 then
651 if test $mod_dst = 160000
652 then
653 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
654 else
655 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
657 else
658 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
660 if test -n "$errmsg"
661 then
662 # Don't give error msg for modification whose dst is not submodule
663 # i.e. deleted or changed to blob
664 test $mod_dst = 160000 && echo "$errmsg"
665 else
666 if test $mod_src = 160000 -a $mod_dst = 160000
667 then
668 limit=
669 test $summary_limit -gt 0 && limit="-$summary_limit"
670 GIT_DIR="$name/.git" \
671 git log $limit --pretty='format: %m %s' \
672 --first-parent $sha1_src...$sha1_dst
673 elif test $mod_dst = 160000
674 then
675 GIT_DIR="$name/.git" \
676 git log --pretty='format: > %s' -1 $sha1_dst
677 else
678 GIT_DIR="$name/.git" \
679 git log --pretty='format: < %s' -1 $sha1_src
681 echo
683 echo
684 done |
685 if test -n "$for_status"; then
686 echo "# Modified submodules:"
687 echo "#"
688 sed -e 's|^|# |' -e 's|^# $|#|'
689 else
694 # List all submodules, prefixed with:
695 # - submodule not initialized
696 # + different revision checked out
698 # If --cached was specified the revision in the index will be printed
699 # instead of the currently checked out revision.
701 # $@ = requested paths (default to all)
703 cmd_status()
705 # parse $args after "submodule ... status".
706 orig_args="$@"
707 while test $# -ne 0
709 case "$1" in
710 -q|--quiet)
711 GIT_QUIET=1
713 --cached)
714 cached=1
716 --recursive)
717 recursive=1
720 shift
721 break
724 usage
727 break
729 esac
730 shift
731 done
733 module_list "$@" |
734 while read mode sha1 stage path
736 name=$(module_name "$path") || exit
737 url=$(git config submodule."$name".url)
738 displaypath="$prefix$path"
739 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
740 then
741 say "-$sha1 $displaypath"
742 continue;
744 set_name_rev "$path" "$sha1"
745 if git diff-files --quiet -- "$path"
746 then
747 say " $sha1 $displaypath$revname"
748 else
749 if test -z "$cached"
750 then
751 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
752 set_name_rev "$path" "$sha1"
754 say "+$sha1 $displaypath$revname"
757 if test -n "$recursive"
758 then
760 prefix="$displaypath/"
761 unset GIT_DIR
762 cd "$path" &&
763 cmd_status $orig_args
764 ) ||
765 die "Failed to recurse into submodule path '$path'"
767 done
770 # Sync remote urls for submodules
771 # This makes the value for remote.$remote.url match the value
772 # specified in .gitmodules.
774 cmd_sync()
776 while test $# -ne 0
778 case "$1" in
779 -q|--quiet)
780 GIT_QUIET=1
781 shift
784 shift
785 break
788 usage
791 break
793 esac
794 done
795 cd_to_toplevel
796 module_list "$@" |
797 while read mode sha1 stage path
799 name=$(module_name "$path")
800 url=$(git config -f .gitmodules --get submodule."$name".url)
802 # Possibly a url relative to parent
803 case "$url" in
804 ./*|../*)
805 url=$(resolve_relative_url "$url") || exit
807 esac
809 if test -e "$path"/.git
810 then
812 unset GIT_DIR
813 cd "$path"
814 remote=$(get_default_remote)
815 say "Synchronizing submodule url for '$name'"
816 git config remote."$remote".url "$url"
819 done
822 # This loop parses the command line arguments to find the
823 # subcommand name to dispatch. Parsing of the subcommand specific
824 # options are primarily done by the subcommand implementations.
825 # Subcommand specific options such as --branch and --cached are
826 # parsed here as well, for backward compatibility.
828 while test $# != 0 && test -z "$command"
830 case "$1" in
831 add | foreach | init | update | status | summary | sync)
832 command=$1
834 -q|--quiet)
835 GIT_QUIET=1
837 -b|--branch)
838 case "$2" in
840 usage
842 esac
843 branch="$2"; shift
845 --cached)
846 cached="$1"
849 break
852 usage
855 break
857 esac
858 shift
859 done
861 # No command word defaults to "status"
862 test -n "$command" || command=status
864 # "-b branch" is accepted only by "add"
865 if test -n "$branch" && test "$command" != add
866 then
867 usage
870 # "--cached" is accepted only by "status" and "summary"
871 if test -n "$cached" && test "$command" != status -a "$command" != summary
872 then
873 usage
876 "cmd_$command" "$@"