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] [-f|--force] [--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] [-f|--force] [--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>...]"
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 remoteurl=$(pwd) # the repository is its own authoritative upstream
40 remoteurl=${remoteurl%/}
49 remoteurl="${remoteurl%/*}"
52 remoteurl="${remoteurl%:*}"
56 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
67 echo "$remoteurl$sep${url%/}"
71 # Get submodule info for registered submodules
72 # $@ = path to limit submodule list
76 git ls-files --error-unmatch --stage -- "$@" |
79 my ($null_sha1) = ("0" x 40);
82 my ($mode, $sha1, $stage, $path) =
83 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
84 next unless $mode eq "160000";
86 if (!$unmerged{$path}++) {
87 print "$mode $null_sha1 U\t$path\n";
97 # Map submodule path to submodule name
103 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
104 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
105 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
106 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
108 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$path'")"
115 # Prior to calling, cmd_update checks that a possibly existing
116 # path is not a git repository.
117 # Likewise, cmd_add checks that path does not exist at all,
118 # since it is the location of a new submodule.
127 name=$(module_name "$path")
128 base_path=$(dirname "$path")
130 gitdir=$(git rev-parse --git-dir)
131 gitdir_base="$gitdir/modules/$base_path"
132 gitdir="$gitdir/modules/$path"
136 a="$(cd_to_toplevel && pwd)/"
138 while [ "$b" ] && [ "${a%%/*}" = "${b%%/*}" ]
144 rel=`echo $rel | sed -e 's|[^/]*|..|g'`
148 rel=`echo $name | sed -e 's|[^/]*|..|g'`
149 rel_gitdir="$rel/$gitdir"
156 echo "gitdir: $rel_gitdir" >"$path/.git"
157 rm -f "$gitdir/index"
159 mkdir -p "$gitdir_base"
160 if test -n "$reference"
162 git-clone "$reference" -n "$url" "$path" --separate-git-dir "$gitdir"
164 git-clone -n "$url" "$path" --separate-git-dir "$gitdir"
166 die "$(eval_gettext "Clone of '\$url' into submodule path '\$path' failed")"
171 # Add a new submodule to the working tree, .gitmodules and the index
175 # optional branch is stored in global branch variable
179 # parse $args after "submodule ... add".
184 case "$2" in '') usage ;; esac
195 case "$2" in '') usage ;; esac
196 reference="--reference=$2"
220 if test -z "$path"; then
221 path=$(echo "$repo" |
222 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
225 if test -z "$repo" -o -z "$path"; then
229 # assure repo is absolute or relative to parent
232 # dereference source url relative to parent's url
233 realrepo=$(resolve_relative_url "$repo") || exit
240 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
245 # multiple //; leading ./; /./; /../; trailing /
246 path=$(printf '%s/\n' "$path" |
256 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
257 die "$(eval_gettext "'\$path' already exists in the index")"
259 if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
262 eval_gettext "The following path is ignored by one of your .gitignore files:
264 Use -f if you really want to add it." &&
270 # perhaps the path exists and is already a git repo, else clone it
273 if test -d "$path"/.git -o -f "$path"/.git
275 eval_gettext "Adding existing repo at '\$path' to the index"; echo
277 die "$(eval_gettext "'\$path' already exists and is not a valid git repo")"
282 module_clone "$path" "$realrepo" "$reference" || exit
286 # ash fails to wordsplit ${branch:+-b "$branch"...}
288 '') git checkout -f -q ;;
289 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
291 ) || die "$(eval_gettext "Unable to checkout submodule '\$path'")"
293 git config submodule."$path".url "$realrepo"
295 git add $force "$path" ||
296 die "$(eval_gettext "Failed to add submodule '\$path'")"
298 git config -f .gitmodules submodule."$path".path "$path" &&
299 git config -f .gitmodules submodule."$path".url "$repo" &&
300 git add --force .gitmodules ||
301 die "$(eval_gettext "Failed to register submodule '\$path'")"
305 # Execute an arbitrary command sequence in each checked out
308 # $@ = command to execute
312 # parse $args after "submodule ... foreach".
334 # dup stdin so that it can be restored when running the external
335 # command in the subshell (and a recursive call to this function)
339 while read mode sha1 stage path
341 if test -e "$path"/.git
343 say "$(eval_gettext "Entering '\$prefix\$path'")"
344 name=$(module_name "$path")
346 prefix="$prefix$path/"
350 if test -n "$recursive"
352 cmd_foreach "--recursive" "$@"
355 die "$(eval_gettext "Stopping at '\$path'; script returned non-zero status.")"
361 # Register submodules in .git/config
363 # $@ = requested paths (default to all)
367 # parse $args after "submodule ... init".
389 while read mode sha1 stage path
391 # Skip already registered paths
392 name=$(module_name "$path") || exit
393 if test -z "$(git config "submodule.$name.url")"
395 url=$(git config -f .gitmodules submodule."$name".url)
397 die "$(eval_gettext "No url found for submodule path '\$path' in .gitmodules")"
399 # Possibly a url relative to parent
402 url=$(resolve_relative_url "$url") || exit
405 git config submodule."$name".url "$url" ||
406 die "$(eval_gettext "Failed to register url for submodule path '\$path'")"
409 # Copy "update" setting when it is not set yet
410 upd="$(git config -f .gitmodules submodule."$name".update)"
412 test -n "$(git config submodule."$name".update)" ||
413 git config submodule."$name".update "$upd" ||
414 die "$(eval_gettext "Failed to register update mode for submodule path '\$path'")"
416 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$path'")"
421 # Update each submodule path to correct revision, using clone and checkout as needed
423 # $@ = requested paths (default to all)
427 # parse $args after "submodule ... update".
448 case "$2" in '') usage ;; esac
449 reference="--reference=$2"
450 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
473 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
479 cmd_init "--" "$@" || return
485 while read mode sha1 stage path
489 echo >&2 "Skipping unmerged submodule $path"
492 name=$(module_name "$path") || exit
493 url=$(git config submodule."$name".url)
494 update_module=$(git config submodule."$name".update)
497 # Only mention uninitialized submodules when its
498 # path have been specified
500 say "$(eval_gettext "Submodule path '\$path' not initialized
501 Maybe you want to use 'update --init'?")"
505 if ! test -d "$path"/.git -o -f "$path"/.git
507 module_clone "$path" "$url" "$reference"|| exit
508 cloned_modules="$cloned_modules;$name"
511 subsha1=$(clear_local_git_env; cd "$path" &&
512 git rev-parse --verify HEAD) ||
513 die "$(eval_gettext "Unable to find current revision in submodule path '\$path'")"
516 if ! test -z "$update"
518 update_module=$update
521 if test "$subsha1" != "$sha1"
524 # If we don't already have a -f flag and the submodule has never been checked out
525 if test -z "$subsha1" -a -z "$force"
530 if test -z "$nofetch"
532 # Run fetch only if $sha1 isn't present or it
533 # is not reachable from a ref.
534 (clear_local_git_env; cd "$path" &&
535 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
536 test -z "$rev") || git-fetch)) ||
537 die "$(eval_gettext "Unable to fetch in submodule path '\$path'")"
540 # Is this something we just cloned?
541 case ";$cloned_modules;" in
543 # then there is no local change to integrate
548 case "$update_module" in
551 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$path'")"
552 say_msg="$(eval_gettext "Submodule path '\$path': rebased into '\$sha1'")"
553 must_die_on_failure=yes
557 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$path'")"
558 say_msg="$(eval_gettext "Submodule path '\$path': merged in '\$sha1'")"
559 must_die_on_failure=yes
562 command="git checkout $subforce -q"
563 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$path'")"
564 say_msg="$(eval_gettext "Submodule path '\$path': checked out '\$sha1'")"
568 if (clear_local_git_env; cd "$path" && $command "$sha1")
571 elif test -n "$must_die_on_failure"
573 die_with_status 2 "$die_msg"
575 err="${err};$die_msg"
580 if test -n "$recursive"
582 (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags")
586 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$path'")"
589 err="${err};$die_msg"
592 die_with_status $res "$die_msg"
619 git describe "$2" 2>/dev/null ||
620 git describe --tags "$2" 2>/dev/null ||
621 git describe --contains "$2" 2>/dev/null ||
622 git describe --all --always "$2"
625 test -z "$revname" || revname=" ($revname)"
628 # Show commit summary for submodules in index or working tree
630 # If '--cached' is given, show summary between index and given commit,
631 # or between working tree and given commit
633 # $@ = [commit (default 'HEAD'),] requested paths (default all)
640 # parse $args after "submodule ... summary".
654 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
676 test $summary_limit = 0 && return
678 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
682 elif test -z "$1" -o "$1" = "HEAD"
684 # before the first commit: compare with an empty tree
685 head=$(git hash-object -w -t tree --stdin </dev/null)
686 test -z "$1" || shift
694 die "$(gettext -- "--cached cannot be used with --files")"
700 # Get modified modules cared by user
701 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
702 sane_egrep '^:([0-7]* )?160000' |
703 while read mod_src mod_dst sha1_src sha1_dst status name
705 # Always show modules deleted or type-changed (blob<->module)
706 test $status = D -o $status = T && echo "$name" && continue
707 # Also show added or modified modules which are checked out
708 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
713 test -z "$modules" && return
715 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
716 sane_egrep '^:([0-7]* )?160000' |
718 while read mod_src mod_dst sha1_src sha1_dst status name
720 if test -z "$cached" &&
721 test $sha1_dst = 0000000000000000000000000000000000000000
725 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
727 100644 | 100755 | 120000)
728 sha1_dst=$(git hash-object $name)
735 eval_gettext "unexpected mode \$mod_dst" &&
744 test $mod_src = 160000 &&
745 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
748 test $mod_dst = 160000 &&
749 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
753 case "$missing_src,$missing_dst" in
755 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
758 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
761 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
766 if test $mod_src = 160000 -a $mod_dst = 160000
768 range="$sha1_src...$sha1_dst"
769 elif test $mod_src = 160000
775 GIT_DIR="$name/.git" \
776 git rev-list --first-parent $range -- | wc -l
778 total_commits=" ($(($total_commits + 0)))"
782 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
783 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
786 blob="$(gettext "blob")"
787 submodule="$(gettext "submodule")"
788 if test $mod_dst = 160000
790 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
792 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
795 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
799 # Don't give error msg for modification whose dst is not submodule
800 # i.e. deleted or changed to blob
801 test $mod_dst = 160000 && echo "$errmsg"
803 if test $mod_src = 160000 -a $mod_dst = 160000
806 test $summary_limit -gt 0 && limit="-$summary_limit"
807 GIT_DIR="$name/.git" \
808 git log $limit --pretty='format: %m %s' \
809 --first-parent $sha1_src...$sha1_dst
810 elif test $mod_dst = 160000
812 GIT_DIR="$name/.git" \
813 git log --pretty='format: > %s' -1 $sha1_dst
815 GIT_DIR="$name/.git" \
816 git log --pretty='format: < %s' -1 $sha1_src
822 if test -n "$for_status"; then
823 if [ -n "$files" ]; then
824 gettext "# Submodules changed but not updated:"; echo
826 gettext "# Submodule changes to be committed:"; echo
829 sed -e 's|^|# |' -e 's|^# $|#|'
835 # List all submodules, prefixed with:
836 # - submodule not initialized
837 # + different revision checked out
839 # If --cached was specified the revision in the index will be printed
840 # instead of the currently checked out revision.
842 # $@ = requested paths (default to all)
846 # parse $args after "submodule ... status".
871 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
876 while read mode sha1 stage path
878 name=$(module_name "$path") || exit
879 url=$(git config submodule."$name".url)
880 displaypath="$prefix$path"
883 say "U$sha1 $displaypath"
886 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
888 say "-$sha1 $displaypath"
891 set_name_rev "$path" "$sha1"
892 if git diff-files --ignore-submodules=dirty --quiet -- "$path"
894 say " $sha1 $displaypath$revname"
898 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
899 set_name_rev "$path" "$sha1"
901 say "+$sha1 $displaypath$revname"
904 if test -n "$recursive"
907 prefix="$displaypath/"
910 eval cmd_status "$orig_args"
912 die "$(eval_gettext "Failed to recurse into submodule path '\$path'")"
917 # Sync remote urls for submodules
918 # This makes the value for remote.$remote.url match the value
919 # specified in .gitmodules.
944 while read mode sha1 stage path
946 name=$(module_name "$path")
947 url=$(git config -f .gitmodules --get submodule."$name".url)
949 # Possibly a url relative to parent
952 url=$(resolve_relative_url "$url") || exit
956 if git config "submodule.$name.url" >/dev/null 2>/dev/null
958 say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
959 git config submodule."$name".url "$url"
961 if test -e "$path"/.git
966 remote=$(get_default_remote)
967 git config remote."$remote".url "$url"
974 # This loop parses the command line arguments to find the
975 # subcommand name to dispatch. Parsing of the subcommand specific
976 # options are primarily done by the subcommand implementations.
977 # Subcommand specific options such as --branch and --cached are
978 # parsed here as well, for backward compatibility.
980 while test $# != 0 && test -z "$command"
983 add | foreach | init | update | status | summary | sync)
1013 # No command word defaults to "status"
1014 test -n "$command" || command=status
1016 # "-b branch" is accepted only by "add"
1017 if test -n "$branch" && test "$command" != add
1022 # "--cached" is accepted only by "status" and "summary"
1023 if test -n "$cached" && test "$command" != status -a "$command" != summary