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>...]"
32 # Resolve relative url by appending to parent's url
33 resolve_relative_url ()
35 remote=$(get_default_remote)
36 remoteurl=$(git config "remote.$remote.url") ||
37 remoteurl=$(pwd) # the repository is its own authoritative upstream
39 remoteurl=${remoteurl%/}
48 remoteurl="${remoteurl%/*}"
51 remoteurl="${remoteurl%:*}"
55 die "cannot strip one component off url '$remoteurl'"
66 echo "$remoteurl$sep${url%/}"
70 # Get submodule info for registered submodules
71 # $@ = path to limit submodule list
75 git ls-files --error-unmatch --stage -- "$@" |
78 my ($null_sha1) = ("0" x 40);
81 my ($mode, $sha1, $stage, $path) =
82 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
83 next unless $mode eq "160000";
85 if (!$unmerged{$path}++) {
86 print "$mode $null_sha1 U\t$path\n";
96 # Map submodule path to submodule name
102 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
103 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
104 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
105 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
107 die "No submodule mapping found in .gitmodules for path '$path'"
114 # Prior to calling, cmd_update checks that a possibly existing
115 # path is not a git repository.
116 # Likewise, cmd_add checks that path does not exist at all,
117 # since it is the location of a new submodule.
125 if test -n "$GIT_QUIET"
130 if test -n "$reference"
132 git-clone $quiet "$reference" -n "$url" "$path"
134 git-clone $quiet -n "$url" "$path"
136 die "Clone of '$url' into submodule path '$path' failed"
140 # Add a new submodule to the working tree, .gitmodules and the index
144 # optional branch is stored in global branch variable
148 # parse $args after "submodule ... add".
153 case "$2" in '') usage ;; esac
164 case "$2" in '') usage ;; esac
165 reference="--reference=$2"
189 if test -z "$path"; then
190 path=$(echo "$repo" |
191 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
194 if test -z "$repo" -o -z "$path"; then
198 # assure repo is absolute or relative to parent
201 # dereference source url relative to parent's url
202 realrepo=$(resolve_relative_url "$repo") || exit
209 die "repo URL: '$repo' must be absolute or begin with ./|../"
214 # multiple //; leading ./; /./; /../; trailing /
215 path=$(printf '%s/\n' "$path" |
225 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
226 die "'$path' already exists in the index"
228 if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
230 echo >&2 "The following path is ignored by one of your .gitignore files:" &&
232 echo >&2 "Use -f if you really want to add it."
236 # perhaps the path exists and is already a git repo, else clone it
239 if test -d "$path"/.git -o -f "$path"/.git
241 echo "Adding existing repo at '$path' to the index"
243 die "'$path' already exists and is not a valid git repo"
248 module_clone "$path" "$realrepo" "$reference" || exit
252 # ash fails to wordsplit ${branch:+-b "$branch"...}
254 '') git checkout -f -q ;;
255 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
257 ) || die "Unable to checkout submodule '$path'"
259 git config submodule."$path".url "$realrepo"
261 git add $force "$path" ||
262 die "Failed to add submodule '$path'"
264 git config -f .gitmodules submodule."$path".path "$path" &&
265 git config -f .gitmodules submodule."$path".url "$repo" &&
266 git add --force .gitmodules ||
267 die "Failed to register submodule '$path'"
271 # Execute an arbitrary command sequence in each checked out
274 # $@ = command to execute
278 # parse $args after "submodule ... foreach".
300 # dup stdin so that it can be restored when running the external
301 # command in the subshell (and a recursive call to this function)
305 while read mode sha1 stage path
307 if test -e "$path"/.git
309 say "Entering '$prefix$path'"
310 name=$(module_name "$path")
312 prefix="$prefix$path/"
316 if test -n "$recursive"
318 cmd_foreach "--recursive" "$@"
321 die "Stopping at '$path'; script returned non-zero status."
327 # Register submodules in .git/config
329 # $@ = requested paths (default to all)
333 # parse $args after "submodule ... init".
355 while read mode sha1 stage path
357 # Skip already registered paths
358 name=$(module_name "$path") || exit
359 if test -z "$(git config "submodule.$name.url")"
361 url=$(git config -f .gitmodules submodule."$name".url)
363 die "No url found for submodule path '$path' in .gitmodules"
365 # Possibly a url relative to parent
368 url=$(resolve_relative_url "$url") || exit
371 git config submodule."$name".url "$url" ||
372 die "Failed to register url for submodule path '$path'"
375 # Copy "update" setting when it is not set yet
376 upd="$(git config -f .gitmodules submodule."$name".update)"
378 test -n "$(git config submodule."$name".update)" ||
379 git config submodule."$name".update "$upd" ||
380 die "Failed to register update mode for submodule path '$path'"
382 say "Submodule '$name' ($url) registered for path '$path'"
387 # Update each submodule path to correct revision, using clone and checkout as needed
389 # $@ = requested paths (default to all)
393 # parse $args after "submodule ... update".
414 case "$2" in '') usage ;; esac
415 reference="--reference=$2"
416 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
439 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
445 cmd_init "--" "$@" || return
450 while read mode sha1 stage path
454 echo >&2 "Skipping unmerged submodule $path"
457 name=$(module_name "$path") || exit
458 url=$(git config submodule."$name".url)
459 update_module=$(git config submodule."$name".update)
462 # Only mention uninitialized submodules when its
463 # path have been specified
465 say "Submodule path '$path' not initialized" &&
466 say "Maybe you want to use 'update --init'?"
470 if ! test -d "$path"/.git -o -f "$path"/.git
472 module_clone "$path" "$url" "$reference"|| exit
473 cloned_modules="$cloned_modules;$name"
476 subsha1=$(clear_local_git_env; cd "$path" &&
477 git rev-parse --verify HEAD) ||
478 die "Unable to find current revision in submodule path '$path'"
481 if ! test -z "$update"
483 update_module=$update
486 if test "$subsha1" != "$sha1"
489 # If we don't already have a -f flag and the submodule has never been checked out
490 if test -z "$subsha1" -a -z "$force"
495 if test -z "$nofetch"
497 # Run fetch only if $sha1 isn't present or it
498 # is not reachable from a ref.
499 (clear_local_git_env; cd "$path" &&
500 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
501 test -z "$rev") || git-fetch)) ||
502 die "Unable to fetch in submodule path '$path'"
505 # Is this something we just cloned?
506 case ";$cloned_modules;" in
508 # then there is no local change to integrate
512 case "$update_module" in
524 command="git checkout $subforce -q"
530 (clear_local_git_env; cd "$path" && $command "$sha1") ||
531 die "Unable to $action '$sha1' in submodule path '$path'"
532 say "Submodule path '$path': $msg '$sha1'"
535 if test -n "$recursive"
537 (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags") ||
538 die "Failed to recurse into submodule path '$path'"
547 git describe "$2" 2>/dev/null ||
548 git describe --tags "$2" 2>/dev/null ||
549 git describe --contains "$2" 2>/dev/null ||
550 git describe --all --always "$2"
553 test -z "$revname" || revname=" ($revname)"
556 # Show commit summary for submodules in index or working tree
558 # If '--cached' is given, show summary between index and given commit,
559 # or between working tree and given commit
561 # $@ = [commit (default 'HEAD'),] requested paths (default all)
568 # parse $args after "submodule ... summary".
582 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
604 test $summary_limit = 0 && return
606 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
610 elif test -z "$1" -o "$1" = "HEAD"
612 # before the first commit: compare with an empty tree
613 head=$(git hash-object -w -t tree --stdin </dev/null)
614 test -z "$1" || shift
622 die "--cached cannot be used with --files"
628 # Get modified modules cared by user
629 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
630 sane_egrep '^:([0-7]* )?160000' |
631 while read mod_src mod_dst sha1_src sha1_dst status name
633 # Always show modules deleted or type-changed (blob<->module)
634 test $status = D -o $status = T && echo "$name" && continue
635 # Also show added or modified modules which are checked out
636 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
641 test -z "$modules" && return
643 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
644 sane_egrep '^:([0-7]* )?160000' |
646 while read mod_src mod_dst sha1_src sha1_dst status name
648 if test -z "$cached" &&
649 test $sha1_dst = 0000000000000000000000000000000000000000
653 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
655 100644 | 100755 | 120000)
656 sha1_dst=$(git hash-object $name)
662 echo >&2 "unexpected mode $mod_dst"
669 test $mod_src = 160000 &&
670 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
673 test $mod_dst = 160000 &&
674 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
678 case "$missing_src,$missing_dst" in
680 errmsg=" Warn: $name doesn't contain commit $sha1_src"
683 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
686 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
691 if test $mod_src = 160000 -a $mod_dst = 160000
693 range="$sha1_src...$sha1_dst"
694 elif test $mod_src = 160000
700 GIT_DIR="$name/.git" \
701 git rev-list --first-parent $range -- | wc -l
703 total_commits=" ($(($total_commits + 0)))"
707 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
708 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
711 if test $mod_dst = 160000
713 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
715 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
718 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
722 # Don't give error msg for modification whose dst is not submodule
723 # i.e. deleted or changed to blob
724 test $mod_dst = 160000 && echo "$errmsg"
726 if test $mod_src = 160000 -a $mod_dst = 160000
729 test $summary_limit -gt 0 && limit="-$summary_limit"
730 GIT_DIR="$name/.git" \
731 git log $limit --pretty='format: %m %s' \
732 --first-parent $sha1_src...$sha1_dst
733 elif test $mod_dst = 160000
735 GIT_DIR="$name/.git" \
736 git log --pretty='format: > %s' -1 $sha1_dst
738 GIT_DIR="$name/.git" \
739 git log --pretty='format: < %s' -1 $sha1_src
745 if test -n "$for_status"; then
746 if [ -n "$files" ]; then
747 echo "# Submodules changed but not updated:"
749 echo "# Submodule changes to be committed:"
752 sed -e 's|^|# |' -e 's|^# $|#|'
758 # List all submodules, prefixed with:
759 # - submodule not initialized
760 # + different revision checked out
762 # If --cached was specified the revision in the index will be printed
763 # instead of the currently checked out revision.
765 # $@ = requested paths (default to all)
769 # parse $args after "submodule ... status".
794 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
799 while read mode sha1 stage path
801 name=$(module_name "$path") || exit
802 url=$(git config submodule."$name".url)
803 displaypath="$prefix$path"
806 say "U$sha1 $displaypath"
809 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
811 say "-$sha1 $displaypath"
814 set_name_rev "$path" "$sha1"
815 if git diff-files --ignore-submodules=dirty --quiet -- "$path"
817 say " $sha1 $displaypath$revname"
821 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
822 set_name_rev "$path" "$sha1"
824 say "+$sha1 $displaypath$revname"
827 if test -n "$recursive"
830 prefix="$displaypath/"
833 eval cmd_status "$orig_args"
835 die "Failed to recurse into submodule path '$path'"
840 # Sync remote urls for submodules
841 # This makes the value for remote.$remote.url match the value
842 # specified in .gitmodules.
867 while read mode sha1 stage path
869 name=$(module_name "$path")
870 url=$(git config -f .gitmodules --get submodule."$name".url)
872 # Possibly a url relative to parent
875 url=$(resolve_relative_url "$url") || exit
879 if git config "submodule.$name.url" >/dev/null 2>/dev/null
881 say "Synchronizing submodule url for '$name'"
882 git config submodule."$name".url "$url"
884 if test -e "$path"/.git
889 remote=$(get_default_remote)
890 git config remote."$remote".url "$url"
897 # This loop parses the command line arguments to find the
898 # subcommand name to dispatch. Parsing of the subcommand specific
899 # options are primarily done by the subcommand implementations.
900 # Subcommand specific options such as --branch and --cached are
901 # parsed here as well, for backward compatibility.
903 while test $# != 0 && test -z "$command"
906 add | foreach | init | update | status | summary | sync)
936 # No command word defaults to "status"
937 test -n "$command" || command=status
939 # "-b branch" is accepted only by "add"
940 if test -n "$branch" && test "$command" != add
945 # "--cached" is accepted only by "status" and "summary"
946 if test -n "$cached" && test "$command" != status -a "$command" != summary