3 # git-submodule.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
77 git ls-files --error-unmatch --stage -- "$@" ||
78 echo "unmatched pathspec exists"
82 my ($null_sha1) = ("0" x 40);
86 if (/^unmatched pathspec/) {
91 my ($mode, $sha1, $stage, $path) =
92 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
93 next unless $mode eq "160000";
95 if (!$unmerged{$path}++) {
96 push @out, "$mode $null_sha1 U\t$path\n";
103 print "#unmatched\n";
112 if test "$1" = "#unmatched"
119 # Map submodule path to submodule name
125 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
127 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
128 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
129 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
131 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$sm_path'")"
138 # Prior to calling, cmd_update checks that a possibly existing
139 # path is not a git repository.
140 # Likewise, cmd_add checks that path does not exist at all,
141 # since it is the location of a new submodule.
149 if test -n "$GIT_QUIET"
156 name=$(module_name "$sm_path" 2>/dev/null)
157 test -n "$name" || name="$sm_path"
158 base_name=$(dirname "$name")
160 gitdir=$(git rev-parse --git-dir)
161 gitdir_base="$gitdir/modules/$base_name"
162 gitdir="$gitdir/modules/$name"
167 rm -f "$gitdir/index"
169 mkdir -p "$gitdir_base"
170 git clone $quiet -n ${reference:+"$reference"} \
171 --separate-git-dir "$gitdir" "$url" "$sm_path" ||
172 die "$(eval_gettext "Clone of '\$url' into submodule path '\$sm_path' failed")"
175 # We already are at the root of the work tree but cd_to_toplevel will
176 # resolve any symlinks that might be present in $PWD
177 a=$(cd_to_toplevel && cd "$gitdir" && pwd)/
178 b=$(cd_to_toplevel && cd "$sm_path" && pwd)/
179 # normalize Windows-style absolute paths to POSIX-style absolute paths
180 case $a in [a-zA-Z]:/*) a=/${a%%:*}${a#*:} ;; esac
181 case $b in [a-zA-Z]:/*) b=/${b%%:*}${b#*:} ;; esac
182 # Remove all common leading directories after a sanity check
183 if test "${a#$b}" != "$a" || test "${b#$a}" != "$b"; then
184 die "$(eval_gettext "Gitdir '\$a' is part of the submodule path '\$b' or vice versa")"
186 while test "${a%%/*}" = "${b%%/*}"
191 # Now chop off the trailing '/'s that were added in the beginning
195 # Turn each leading "*/" component into "../"
196 rel=$(echo $b | sed -e 's|[^/][^/]*|..|g')
197 echo "gitdir: $rel/$a" >"$sm_path/.git"
199 rel=$(echo $a | sed -e 's|[^/][^/]*|..|g')
200 (clear_local_git_env; cd "$sm_path" && GIT_WORK_TREE=. git config core.worktree "$rel/$b")
204 # Add a new submodule to the working tree, .gitmodules and the index
208 # optional branch is stored in global branch variable
212 # parse $args after "submodule ... add".
217 case "$2" in '') usage ;; esac
228 case "$2" in '') usage ;; esac
229 reference="--reference=$2"
253 if test -z "$sm_path"; then
254 sm_path=$(echo "$repo" |
255 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
258 if test -z "$repo" -o -z "$sm_path"; then
262 # assure repo is absolute or relative to parent
265 # dereference source url relative to parent's url
266 realrepo=$(resolve_relative_url "$repo") || exit
273 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
278 # multiple //; leading ./; /./; /../; trailing /
279 sm_path=$(printf '%s/\n' "$sm_path" |
289 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
290 die "$(eval_gettext "'\$sm_path' already exists in the index")"
292 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
294 eval_gettextln "The following path is ignored by one of your .gitignore files:
296 Use -f if you really want to add it." >&2
300 # perhaps the path exists and is already a git repo, else clone it
301 if test -e "$sm_path"
303 if test -d "$sm_path"/.git -o -f "$sm_path"/.git
305 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
307 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
312 module_clone "$sm_path" "$realrepo" "$reference" || exit
316 # ash fails to wordsplit ${branch:+-b "$branch"...}
318 '') git checkout -f -q ;;
319 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
321 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
323 git config submodule."$sm_path".url "$realrepo"
325 git add $force "$sm_path" ||
326 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
328 git config -f .gitmodules submodule."$sm_path".path "$sm_path" &&
329 git config -f .gitmodules submodule."$sm_path".url "$repo" &&
330 git add --force .gitmodules ||
331 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
335 # Execute an arbitrary command sequence in each checked out
338 # $@ = command to execute
342 # parse $args after "submodule ... foreach".
364 # dup stdin so that it can be restored when running the external
365 # command in the subshell (and a recursive call to this function)
369 while read mode sha1 stage sm_path
371 die_if_unmatched "$mode"
372 if test -e "$sm_path"/.git
374 say "$(eval_gettext "Entering '\$prefix\$sm_path'")"
375 name=$(module_name "$sm_path")
377 prefix="$prefix$sm_path/"
379 # we make $path available to scripts ...
383 if test -n "$recursive"
385 cmd_foreach "--recursive" "$@"
388 die "$(eval_gettext "Stopping at '\$sm_path'; script returned non-zero status.")"
394 # Register submodules in .git/config
396 # $@ = requested paths (default to all)
400 # parse $args after "submodule ... init".
422 while read mode sha1 stage sm_path
424 die_if_unmatched "$mode"
425 name=$(module_name "$sm_path") || exit
427 # Copy url setting when it is not set yet
428 if test -z "$(git config "submodule.$name.url")"
430 url=$(git config -f .gitmodules submodule."$name".url)
432 die "$(eval_gettext "No url found for submodule path '\$sm_path' in .gitmodules")"
434 # Possibly a url relative to parent
437 url=$(resolve_relative_url "$url") || exit
440 git config submodule."$name".url "$url" ||
441 die "$(eval_gettext "Failed to register url for submodule path '\$sm_path'")"
443 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$sm_path'")"
446 # Copy "update" setting when it is not set yet
447 upd="$(git config -f .gitmodules submodule."$name".update)"
449 test -n "$(git config submodule."$name".update)" ||
450 git config submodule."$name".update "$upd" ||
451 die "$(eval_gettext "Failed to register update mode for submodule path '\$sm_path'")"
456 # Update each submodule path to correct revision, using clone and checkout as needed
458 # $@ = requested paths (default to all)
462 # parse $args after "submodule ... update".
483 case "$2" in '') usage ;; esac
484 reference="--reference=$2"
485 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
511 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
517 cmd_init "--" "$@" || return
523 while read mode sha1 stage sm_path
525 die_if_unmatched "$mode"
528 echo >&2 "Skipping unmerged submodule $sm_path"
531 name=$(module_name "$sm_path") || exit
532 url=$(git config submodule."$name".url)
533 if ! test -z "$update"
535 update_module=$update
537 update_module=$(git config submodule."$name".update)
540 if test "$update_module" = "none"
542 echo "Skipping submodule '$sm_path'"
548 # Only mention uninitialized submodules when its
549 # path have been specified
551 say "$(eval_gettext "Submodule path '\$sm_path' not initialized
552 Maybe you want to use 'update --init'?")"
556 if ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
558 module_clone "$sm_path" "$url" "$reference"|| exit
559 cloned_modules="$cloned_modules;$name"
562 subsha1=$(clear_local_git_env; cd "$sm_path" &&
563 git rev-parse --verify HEAD) ||
564 die "$(eval_gettext "Unable to find current revision in submodule path '\$sm_path'")"
567 if test "$subsha1" != "$sha1" -o -n "$force"
570 # If we don't already have a -f flag and the submodule has never been checked out
571 if test -z "$subsha1" -a -z "$force"
576 if test -z "$nofetch"
578 # Run fetch only if $sha1 isn't present or it
579 # is not reachable from a ref.
580 (clear_local_git_env; cd "$sm_path" &&
581 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
582 test -z "$rev") || git-fetch)) ||
583 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
586 # Is this something we just cloned?
587 case ";$cloned_modules;" in
589 # then there is no local change to integrate
594 case "$update_module" in
597 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$sm_path'")"
598 say_msg="$(eval_gettext "Submodule path '\$sm_path': rebased into '\$sha1'")"
599 must_die_on_failure=yes
603 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$sm_path'")"
604 say_msg="$(eval_gettext "Submodule path '\$sm_path': merged in '\$sha1'")"
605 must_die_on_failure=yes
608 command="git checkout $subforce -q"
609 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$sm_path'")"
610 say_msg="$(eval_gettext "Submodule path '\$sm_path': checked out '\$sha1'")"
614 if (clear_local_git_env; cd "$sm_path" && $command "$sha1")
617 elif test -n "$must_die_on_failure"
619 die_with_status 2 "$die_msg"
621 err="${err};$die_msg"
626 if test -n "$recursive"
628 (clear_local_git_env; cd "$sm_path" && eval cmd_update "$orig_flags")
632 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
635 err="${err};$die_msg"
638 die_with_status $res "$die_msg"
665 git describe "$2" 2>/dev/null ||
666 git describe --tags "$2" 2>/dev/null ||
667 git describe --contains "$2" 2>/dev/null ||
668 git describe --all --always "$2"
671 test -z "$revname" || revname=" ($revname)"
674 # Show commit summary for submodules in index or working tree
676 # If '--cached' is given, show summary between index and given commit,
677 # or between working tree and given commit
679 # $@ = [commit (default 'HEAD'),] requested paths (default all)
686 # parse $args after "submodule ... summary".
700 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
722 test $summary_limit = 0 && return
724 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
728 elif test -z "$1" -o "$1" = "HEAD"
730 # before the first commit: compare with an empty tree
731 head=$(git hash-object -w -t tree --stdin </dev/null)
732 test -z "$1" || shift
740 die "$(gettext -- "--cached cannot be used with --files")"
746 # Get modified modules cared by user
747 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
748 sane_egrep '^:([0-7]* )?160000' |
749 while read mod_src mod_dst sha1_src sha1_dst status name
751 # Always show modules deleted or type-changed (blob<->module)
752 test $status = D -o $status = T && echo "$name" && continue
753 # Also show added or modified modules which are checked out
754 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
759 test -z "$modules" && return
761 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
762 sane_egrep '^:([0-7]* )?160000' |
764 while read mod_src mod_dst sha1_src sha1_dst status name
766 if test -z "$cached" &&
767 test $sha1_dst = 0000000000000000000000000000000000000000
771 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
773 100644 | 100755 | 120000)
774 sha1_dst=$(git hash-object $name)
780 eval_gettextln "unexpected mode \$mod_dst" >&2
787 test $mod_src = 160000 &&
788 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
791 test $mod_dst = 160000 &&
792 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
796 case "$missing_src,$missing_dst" in
798 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
801 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
804 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
809 if test $mod_src = 160000 -a $mod_dst = 160000
811 range="$sha1_src...$sha1_dst"
812 elif test $mod_src = 160000
818 GIT_DIR="$name/.git" \
819 git rev-list --first-parent $range -- | wc -l
821 total_commits=" ($(($total_commits + 0)))"
825 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
826 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
829 blob="$(gettext "blob")"
830 submodule="$(gettext "submodule")"
831 if test $mod_dst = 160000
833 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
835 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
838 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
842 # Don't give error msg for modification whose dst is not submodule
843 # i.e. deleted or changed to blob
844 test $mod_dst = 160000 && echo "$errmsg"
846 if test $mod_src = 160000 -a $mod_dst = 160000
849 test $summary_limit -gt 0 && limit="-$summary_limit"
850 GIT_DIR="$name/.git" \
851 git log $limit --pretty='format: %m %s' \
852 --first-parent $sha1_src...$sha1_dst
853 elif test $mod_dst = 160000
855 GIT_DIR="$name/.git" \
856 git log --pretty='format: > %s' -1 $sha1_dst
858 GIT_DIR="$name/.git" \
859 git log --pretty='format: < %s' -1 $sha1_src
865 if test -n "$for_status"; then
866 if [ -n "$files" ]; then
867 gettextln "# Submodules changed but not updated:"
869 gettextln "# Submodule changes to be committed:"
872 sed -e 's|^|# |' -e 's|^# $|#|'
878 # List all submodules, prefixed with:
879 # - submodule not initialized
880 # + different revision checked out
882 # If --cached was specified the revision in the index will be printed
883 # instead of the currently checked out revision.
885 # $@ = requested paths (default to all)
889 # parse $args after "submodule ... status".
914 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
919 while read mode sha1 stage sm_path
921 die_if_unmatched "$mode"
922 name=$(module_name "$sm_path") || exit
923 url=$(git config submodule."$name".url)
924 displaypath="$prefix$sm_path"
927 say "U$sha1 $displaypath"
930 if test -z "$url" || ! test -d "$sm_path"/.git -o -f "$sm_path"/.git
932 say "-$sha1 $displaypath"
935 set_name_rev "$sm_path" "$sha1"
936 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
938 say " $sha1 $displaypath$revname"
942 sha1=$(clear_local_git_env; cd "$sm_path" && git rev-parse --verify HEAD)
943 set_name_rev "$sm_path" "$sha1"
945 say "+$sha1 $displaypath$revname"
948 if test -n "$recursive"
951 prefix="$displaypath/"
954 eval cmd_status "$orig_args"
956 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
961 # Sync remote urls for submodules
962 # This makes the value for remote.$remote.url match the value
963 # specified in .gitmodules.
988 while read mode sha1 stage sm_path
990 die_if_unmatched "$mode"
991 name=$(module_name "$sm_path")
992 url=$(git config -f .gitmodules --get submodule."$name".url)
994 # Possibly a url relative to parent
997 url=$(resolve_relative_url "$url") || exit
1001 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1003 say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
1004 git config submodule."$name".url "$url"
1006 if test -e "$sm_path"/.git
1011 remote=$(get_default_remote)
1012 git config remote."$remote".url "$url"
1019 # This loop parses the command line arguments to find the
1020 # subcommand name to dispatch. Parsing of the subcommand specific
1021 # options are primarily done by the subcommand implementations.
1022 # Subcommand specific options such as --branch and --cached are
1023 # parsed here as well, for backward compatibility.
1025 while test $# != 0 && test -z "$command"
1028 add | foreach | init | update | status | summary | sync)
1058 # No command word defaults to "status"
1059 test -n "$command" || command=status
1061 # "-b branch" is accepted only by "add"
1062 if test -n "$branch" && test "$command" != add
1067 # "--cached" is accepted only by "status" and "summary"
1068 if test -n "$cached" && test "$command" != status -a "$command" != summary