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] [--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 die "remote ($remote) does not have a url defined in .git/config"
39 remoteurl=${remoteurl%/}
45 remoteurl="${remoteurl%/*}"
54 echo "$remoteurl/${url%/}"
58 # Get submodule info for registered submodules
59 # $@ = path to limit submodule list
63 git ls-files --error-unmatch --stage -- "$@" | sane_grep '^160000 '
67 # Map submodule path to submodule name
73 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
74 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
75 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
76 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
78 die "No submodule mapping found in .gitmodules for path '$path'"
85 # Prior to calling, cmd_update checks that a possibly existing
86 # path is not a git repository.
87 # Likewise, cmd_add checks that path does not exist at all,
88 # since it is the location of a new submodule.
96 # If there already is a directory at the submodule path,
97 # expect it to be empty (since that is the default checkout
98 # action) and try to remove it.
99 # Note: if $path is a symlink to a directory the test will
100 # succeed but the rmdir will fail. We might want to fix this.
103 rmdir "$path" 2>/dev/null ||
104 die "Directory '$path' exists, but is neither empty nor a git repository"
108 die "A file already exist at path '$path'"
110 if test -n "$reference"
112 git-clone "$reference" -n "$url" "$path"
114 git-clone -n "$url" "$path"
116 die "Clone of '$url' into submodule path '$path' failed"
120 # Add a new submodule to the working tree, .gitmodules and the index
124 # optional branch is stored in global branch variable
128 # parse $args after "submodule ... add".
133 case "$2" in '') usage ;; esac
144 case "$2" in '') usage ;; esac
145 reference="--reference=$2"
169 if test -z "$path"; then
170 path=$(echo "$repo" |
171 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
174 if test -z "$repo" -o -z "$path"; then
178 # assure repo is absolute or relative to parent
181 # dereference source url relative to parent's url
182 realrepo=$(resolve_relative_url "$repo") || exit
189 die "repo URL: '$repo' must be absolute or begin with ./|../"
194 # multiple //; leading ./; /./; /../; trailing /
195 path=$(printf '%s/\n' "$path" |
205 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
206 die "'$path' already exists in the index"
208 if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
210 echo >&2 "The following path is ignored by one of your .gitignore files:" &&
212 echo >&2 "Use -f if you really want to add it."
216 # perhaps the path exists and is already a git repo, else clone it
219 if test -d "$path"/.git -o -f "$path"/.git
221 echo "Adding existing repo at '$path' to the index"
223 die "'$path' already exists and is not a valid git repo"
228 url=$(resolve_relative_url "$repo") || exit
236 module_clone "$path" "$realrepo" "$reference" || exit
240 # ash fails to wordsplit ${branch:+-b "$branch"...}
242 '') git checkout -f -q ;;
243 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
245 ) || die "Unable to checkout submodule '$path'"
247 git config submodule."$path".url "$url"
249 git add $force "$path" ||
250 die "Failed to add submodule '$path'"
252 git config -f .gitmodules submodule."$path".path "$path" &&
253 git config -f .gitmodules submodule."$path".url "$repo" &&
254 git add --force .gitmodules ||
255 die "Failed to register submodule '$path'"
259 # Execute an arbitrary command sequence in each checked out
262 # $@ = command to execute
266 # parse $args after "submodule ... foreach".
289 while read mode sha1 stage path
291 if test -e "$path"/.git
293 say "Entering '$prefix$path'"
294 name=$(module_name "$path")
296 prefix="$prefix$path/"
300 if test -n "$recursive"
302 cmd_foreach "--recursive" "$@"
305 die "Stopping at '$path'; script returned non-zero status."
311 # Register submodules in .git/config
313 # $@ = requested paths (default to all)
317 # parse $args after "submodule ... init".
339 while read mode sha1 stage path
341 # Skip already registered paths
342 name=$(module_name "$path") || exit
343 if test -z "$(git config "submodule.$name.url")"
345 url=$(git config -f .gitmodules submodule."$name".url)
347 die "No url found for submodule path '$path' in .gitmodules"
349 # Possibly a url relative to parent
352 url=$(resolve_relative_url "$url") || exit
355 git config submodule."$name".url "$url" ||
356 die "Failed to register url for submodule path '$path'"
359 # Copy "update" setting when it is not set yet
360 upd="$(git config -f .gitmodules submodule."$name".update)"
362 test -n "$(git config submodule."$name".update)" ||
363 git config submodule."$name".update "$upd" ||
364 die "Failed to register update mode for submodule path '$path'"
366 say "Submodule '$name' ($url) registered for path '$path'"
371 # Update each submodule path to correct revision, using clone and checkout as needed
373 # $@ = requested paths (default to all)
377 # parse $args after "submodule ... update".
399 case "$2" in '') usage ;; esac
400 reference="--reference=$2"
430 cmd_init "--" "$@" || return
434 while read mode sha1 stage path
436 name=$(module_name "$path") || exit
437 url=$(git config submodule."$name".url)
438 update_module=$(git config submodule."$name".update)
441 # Only mention uninitialized submodules when its
442 # path have been specified
444 say "Submodule path '$path' not initialized" &&
445 say "Maybe you want to use 'update --init'?"
449 if ! test -d "$path"/.git -o -f "$path"/.git
451 module_clone "$path" "$url" "$reference"|| exit
454 subsha1=$(clear_local_git_env; cd "$path" &&
455 git rev-parse --verify HEAD) ||
456 die "Unable to find current revision in submodule path '$path'"
459 if ! test -z "$update"
461 update_module=$update
464 if test "$subsha1" != "$sha1"
467 if test -z "$subsha1"
472 if test -z "$nofetch"
474 (clear_local_git_env; cd "$path" &&
476 die "Unable to fetch in submodule path '$path'"
479 case "$update_module" in
491 command="git checkout $force -q"
497 (clear_local_git_env; cd "$path" && $command "$sha1") ||
498 die "Unable to $action '$sha1' in submodule path '$path'"
499 say "Submodule path '$path': $msg '$sha1'"
502 if test -n "$recursive"
504 (clear_local_git_env; cd "$path" && cmd_update $orig_args) ||
505 die "Failed to recurse into submodule path '$path'"
514 git describe "$2" 2>/dev/null ||
515 git describe --tags "$2" 2>/dev/null ||
516 git describe --contains "$2" 2>/dev/null ||
517 git describe --all --always "$2"
520 test -z "$revname" || revname=" ($revname)"
523 # Show commit summary for submodules in index or working tree
525 # If '--cached' is given, show summary between index and given commit,
526 # or between working tree and given commit
528 # $@ = [commit (default 'HEAD'),] requested paths (default all)
535 # parse $args after "submodule ... summary".
549 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
571 test $summary_limit = 0 && return
573 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
577 elif test -z "$1" -o "$1" = "HEAD"
579 # before the first commit: compare with an empty tree
580 head=$(git hash-object -w -t tree --stdin </dev/null)
581 test -z "$1" || shift
589 die "--cached cannot be used with --files"
595 # Get modified modules cared by user
596 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
597 sane_egrep '^:([0-7]* )?160000' |
598 while read mod_src mod_dst sha1_src sha1_dst status name
600 # Always show modules deleted or type-changed (blob<->module)
601 test $status = D -o $status = T && echo "$name" && continue
602 # Also show added or modified modules which are checked out
603 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
608 test -z "$modules" && return
610 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
611 sane_egrep '^:([0-7]* )?160000' |
613 while read mod_src mod_dst sha1_src sha1_dst status name
615 if test -z "$cached" &&
616 test $sha1_dst = 0000000000000000000000000000000000000000
620 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
622 100644 | 100755 | 120000)
623 sha1_dst=$(git hash-object $name)
629 echo >&2 "unexpected mode $mod_dst"
636 test $mod_src = 160000 &&
637 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
640 test $mod_dst = 160000 &&
641 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
645 case "$missing_src,$missing_dst" in
647 errmsg=" Warn: $name doesn't contain commit $sha1_src"
650 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
653 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
658 if test $mod_src = 160000 -a $mod_dst = 160000
660 range="$sha1_src...$sha1_dst"
661 elif test $mod_src = 160000
667 GIT_DIR="$name/.git" \
668 git rev-list --first-parent $range -- | wc -l
670 total_commits=" ($(($total_commits + 0)))"
674 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
675 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
678 if test $mod_dst = 160000
680 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
682 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
685 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
689 # Don't give error msg for modification whose dst is not submodule
690 # i.e. deleted or changed to blob
691 test $mod_dst = 160000 && echo "$errmsg"
693 if test $mod_src = 160000 -a $mod_dst = 160000
696 test $summary_limit -gt 0 && limit="-$summary_limit"
697 GIT_DIR="$name/.git" \
698 git log $limit --pretty='format: %m %s' \
699 --first-parent $sha1_src...$sha1_dst
700 elif test $mod_dst = 160000
702 GIT_DIR="$name/.git" \
703 git log --pretty='format: > %s' -1 $sha1_dst
705 GIT_DIR="$name/.git" \
706 git log --pretty='format: < %s' -1 $sha1_src
712 if test -n "$for_status"; then
713 if [ -n "$files" ]; then
714 echo "# Submodules changed but not updated:"
716 echo "# Submodule changes to be committed:"
719 sed -e 's|^|# |' -e 's|^# $|#|'
725 # List all submodules, prefixed with:
726 # - submodule not initialized
727 # + different revision checked out
729 # If --cached was specified the revision in the index will be printed
730 # instead of the currently checked out revision.
732 # $@ = requested paths (default to all)
736 # parse $args after "submodule ... status".
765 while read mode sha1 stage path
767 name=$(module_name "$path") || exit
768 url=$(git config submodule."$name".url)
769 displaypath="$prefix$path"
770 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
772 say "-$sha1 $displaypath"
775 set_name_rev "$path" "$sha1"
776 if git diff-files --ignore-submodules=dirty --quiet -- "$path"
778 say " $sha1 $displaypath$revname"
782 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
783 set_name_rev "$path" "$sha1"
785 say "+$sha1 $displaypath$revname"
788 if test -n "$recursive"
791 prefix="$displaypath/"
794 cmd_status $orig_args
796 die "Failed to recurse into submodule path '$path'"
801 # Sync remote urls for submodules
802 # This makes the value for remote.$remote.url match the value
803 # specified in .gitmodules.
828 while read mode sha1 stage path
830 name=$(module_name "$path")
831 url=$(git config -f .gitmodules --get submodule."$name".url)
833 # Possibly a url relative to parent
836 url=$(resolve_relative_url "$url") || exit
840 if git config "submodule.$name.url" >/dev/null 2>/dev/null
842 say "Synchronizing submodule url for '$name'"
843 git config submodule."$name".url "$url"
845 if test -e "$path"/.git
850 remote=$(get_default_remote)
851 git config remote."$remote".url "$url"
858 # This loop parses the command line arguments to find the
859 # subcommand name to dispatch. Parsing of the subcommand specific
860 # options are primarily done by the subcommand implementations.
861 # Subcommand specific options such as --branch and --cached are
862 # parsed here as well, for backward compatibility.
864 while test $# != 0 && test -z "$command"
867 add | foreach | init | update | status | summary | sync)
897 # No command word defaults to "status"
898 test -n "$command" || command=status
900 # "-b branch" is accepted only by "add"
901 if test -n "$branch" && test "$command" != add
906 # "--cached" is accepted only by "status" and "summary"
907 if test -n "$cached" && test "$command" != status -a "$command" != summary