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] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
10 or: $dashless [--quiet] init [--] [<path>...]
11 or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)
12 or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--] [<path>...]
13 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
14 or: $dashless [--quiet] foreach [--recursive] <command>
15 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]"
21 wt_prefix=$(git rev-parse --show-prefix)
24 # Tell the rest of git that any URLs we get don't come
25 # directly from the user, so it can apply policy as appropriate.
26 GIT_PROTOCOL_FROM_USER=0
27 export GIT_PROTOCOL_FROM_USER
47 if test "$1" = "#unmatched"
54 # Print a submodule configuration setting
60 # Checks in the usual git-config places first (for overrides),
61 # otherwise it falls back on .gitmodules. This allows you to
62 # distribute project-wide defaults in .gitmodules, while still
63 # customizing individual repositories if necessary. If the option is
64 # not in .gitmodules either, print a default value.
66 get_submodule_config () {
70 value=$(git config submodule."$name"."$option")
73 value=$(git config -f .gitmodules submodule."$name"."$option")
75 printf '%s' "${value:-$default}"
80 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
83 # Sanitize the local git environment for use within a submodule. We
84 # can't simply use clear_local_git_env since we want to preserve some
85 # of the settings from GIT_CONFIG_PARAMETERS.
86 sanitize_submodule_env()
88 save_config=$GIT_CONFIG_PARAMETERS
90 GIT_CONFIG_PARAMETERS=$save_config
91 export GIT_CONFIG_PARAMETERS
95 # Add a new submodule to the working tree, .gitmodules and the index
99 # optional branch is stored in global branch variable
103 # parse $args after "submodule ... add".
109 case "$2" in '') usage ;; esac
120 case "$2" in '') usage ;; esac
125 reference_path="${1#--reference=}"
128 case "$2" in '') usage ;; esac
133 case "$2" in '') usage ;; esac
154 if test -n "$reference_path"
156 is_absolute_path "$reference_path" ||
157 reference_path="$wt_prefix$reference_path"
159 reference="--reference=$reference_path"
165 if test -z "$sm_path"; then
166 sm_path=$(printf '%s\n' "$repo" |
167 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
170 if test -z "$repo" || test -z "$sm_path"; then
174 is_absolute_path "$sm_path" || sm_path="$wt_prefix$sm_path"
176 # assure repo is absolute or relative to parent
179 test -z "$wt_prefix" ||
180 die "$(gettext "Relative path can only be used from the toplevel of the working tree")"
182 # dereference source url relative to parent's url
183 realrepo=$(git submodule--helper resolve-relative-url "$repo") || exit
190 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
195 # multiple //; leading ./; /./; /../; trailing /
196 sm_path=$(printf '%s/\n' "$sm_path" |
208 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
209 die "$(eval_gettext "'\$sm_path' already exists in the index")"
211 git ls-files -s "$sm_path" | sane_grep -v "^160000" > /dev/null 2>&1 &&
212 die "$(eval_gettext "'\$sm_path' already exists in the index and is not a submodule")"
215 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
217 eval_gettextln "The following path is ignored by one of your .gitignore files:
219 Use -f if you really want to add it." >&2
223 if test -n "$custom_name"
225 sm_name="$custom_name"
230 # perhaps the path exists and is already a git repo, else clone it
231 if test -e "$sm_path"
233 if test -d "$sm_path"/.git || test -f "$sm_path"/.git
235 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
237 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
241 if test -d ".git/modules/$sm_name"
245 eval_gettextln >&2 "A git directory for '\$sm_name' is found locally with remote(s):"
246 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
247 die "$(eval_gettextln "\
248 If you want to reuse this local git directory instead of cloning again from
250 use the '--force' option. If the local git directory is not the correct repo
251 or you are unsure what this means choose another name with the '--name' option.")"
253 eval_gettextln "Reactivating local git directory for submodule '\$sm_name'."
256 git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" ${reference:+"$reference"} ${depth:+"$depth"} || exit
258 sanitize_submodule_env
260 # ash fails to wordsplit ${branch:+-b "$branch"...}
262 '') git checkout -f -q ;;
263 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
265 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
267 git config submodule."$sm_name".url "$realrepo"
269 git add $force "$sm_path" ||
270 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
272 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
273 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
276 git config -f .gitmodules submodule."$sm_name".branch "$branch"
278 git add --force .gitmodules ||
279 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
283 # Execute an arbitrary command sequence in each checked out
286 # $@ = command to execute
290 # parse $args after "submodule ... foreach".
312 # dup stdin so that it can be restored when running the external
313 # command in the subshell (and a recursive call to this function)
317 git submodule--helper list --prefix "$wt_prefix" ||
320 while read mode sha1 stage sm_path
322 die_if_unmatched "$mode" "$sha1"
323 if test -e "$sm_path"/.git
325 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
326 say "$(eval_gettext "Entering '\$displaypath'")"
327 name=$(git submodule--helper name "$sm_path")
329 prefix="$prefix$sm_path/"
330 sanitize_submodule_env
332 sm_path=$(git submodule--helper relative-path "$sm_path" "$wt_prefix") &&
333 # we make $path available to scripts ...
341 if test -n "$recursive"
343 cmd_foreach "--recursive" "$@"
346 die "$(eval_gettext "Stopping at '\$displaypath'; script returned non-zero status.")"
352 # Register submodules in .git/config
354 # $@ = requested paths (default to all)
358 # parse $args after "submodule ... init".
379 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper init ${GIT_QUIET:+--quiet} ${prefix:+--prefix "$prefix"} "$@"
383 # Unregister submodules from .git/config and remove their work tree
387 # parse $args after "submodule ... deinit".
415 if test -n "$deinit_all" && test "$#" -ne 0
417 echo >&2 "$(eval_gettext "pathspec and --all are incompatible")"
420 if test $# = 0 && test -z "$deinit_all"
422 die "$(eval_gettext "Use '--all' if you really want to deinitialize all submodules")"
426 git submodule--helper list --prefix "$wt_prefix" "$@" ||
429 while read mode sha1 stage sm_path
431 die_if_unmatched "$mode" "$sha1"
432 name=$(git submodule--helper name "$sm_path") || exit
434 displaypath=$(git submodule--helper relative-path "$sm_path" "$wt_prefix")
436 # Remove the submodule work tree (unless the user already did it)
437 if test -d "$sm_path"
439 # Protect submodules containing a .git directory
440 if test -d "$sm_path/.git"
442 die "$(eval_gettext "\
443 Submodule work tree '\$displaypath' contains a .git directory
444 (use 'rm -rf' if you really want to remove it including all of its history)")"
449 git rm -qn "$sm_path" ||
450 die "$(eval_gettext "Submodule work tree '\$displaypath' contains local modifications; use '-f' to discard them")"
453 say "$(eval_gettext "Cleared directory '\$displaypath'")" ||
454 say "$(eval_gettext "Could not remove submodule work tree '\$displaypath'")"
457 mkdir "$sm_path" || say "$(eval_gettext "Could not create empty submodule directory '\$displaypath'")"
459 # Remove the .git/config entries (unless the user already did it)
460 if test -n "$(git config --get-regexp submodule."$name\.")"
462 # Remove the whole section so we have a clean state when
463 # the user later decides to init this submodule again
464 url=$(git config submodule."$name".url)
465 git config --remove-section submodule."$name" 2>/dev/null &&
466 say "$(eval_gettext "Submodule '\$name' (\$url) unregistered for path '\$displaypath'")"
471 is_tip_reachable () (
472 sanitize_submodule_env &&
474 rev=$(git rev-list -n 1 "$2" --not --all 2>/dev/null) &&
478 fetch_in_submodule () (
479 sanitize_submodule_env &&
486 git fetch $(get_default_remote) "$@" ;;
491 # Update each submodule path to correct revision, using clone and checkout as needed
493 # $@ = requested paths (default to all)
497 # parse $args after "submodule ... update".
505 progress="--progress"
523 case "$2" in '') usage ;; esac
524 reference="--reference=$2"
540 recommend_shallow="--recommend-shallow"
542 --no-recommend-shallow)
543 recommend_shallow="--no-recommend-shallow"
546 case "$2" in '') usage ;; esac
554 case "$2" in '') usage ;; esac
577 cmd_init "--" "$@" || return
581 git submodule--helper update-clone ${GIT_QUIET:+--quiet} \
582 ${progress:+"$progress"} \
583 ${wt_prefix:+--prefix "$wt_prefix"} \
584 ${prefix:+--recursive-prefix "$prefix"} \
585 ${update:+--update "$update"} \
586 ${reference:+"$reference"} \
587 ${depth:+--depth "$depth"} \
588 ${recommend_shallow:+"$recommend_shallow"} \
590 "$@" || echo "#unmatched" $?
593 while read mode sha1 stage just_cloned sm_path
595 die_if_unmatched "$mode" "$sha1"
597 name=$(git submodule--helper name "$sm_path") || exit
598 url=$(git config submodule."$name".url)
599 if ! test -z "$update"
601 update_module=$update
603 update_module=$(git config submodule."$name".update)
604 if test -z "$update_module"
606 update_module="checkout"
610 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
612 if test $just_cloned -eq 1
615 update_module=checkout
617 subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
618 git rev-parse --verify HEAD) ||
619 die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
624 branch=$(git submodule--helper remote-branch "$sm_path")
625 if test -z "$nofetch"
627 # Fetch remote before determining tracking $sha1
628 fetch_in_submodule "$sm_path" $depth ||
629 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
631 remote_name=$(sanitize_submodule_env; cd "$sm_path" && get_default_remote)
632 sha1=$(sanitize_submodule_env; cd "$sm_path" &&
633 git rev-parse --verify "${remote_name}/${branch}") ||
634 die "$(eval_gettext "Unable to find current \${remote_name}/\${branch} revision in submodule path '\$sm_path'")"
637 if test "$subsha1" != "$sha1" || test -n "$force"
640 # If we don't already have a -f flag and the submodule has never been checked out
641 if test -z "$subsha1" && test -z "$force"
646 if test -z "$nofetch"
648 # Run fetch only if $sha1 isn't present or it
649 # is not reachable from a ref.
650 is_tip_reachable "$sm_path" "$sha1" ||
651 fetch_in_submodule "$sm_path" $depth ||
652 die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
654 # Now we tried the usual fetch, but $sha1 may
655 # not be reachable from any of the refs
656 is_tip_reachable "$sm_path" "$sha1" ||
657 fetch_in_submodule "$sm_path" $depth "$sha1" ||
658 die "$(eval_gettext "Fetched in submodule path '\$displaypath', but it did not contain \$sha1. Direct fetching of that commit failed.")"
662 case "$update_module" in
664 command="git checkout $subforce -q"
665 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
666 say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
670 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$displaypath'")"
671 say_msg="$(eval_gettext "Submodule path '\$displaypath': rebased into '\$sha1'")"
672 must_die_on_failure=yes
676 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$displaypath'")"
677 say_msg="$(eval_gettext "Submodule path '\$displaypath': merged in '\$sha1'")"
678 must_die_on_failure=yes
681 command="${update_module#!}"
682 die_msg="$(eval_gettext "Execution of '\$command \$sha1' failed in submodule path '\$displaypath'")"
683 say_msg="$(eval_gettext "Submodule path '\$displaypath': '\$command \$sha1'")"
684 must_die_on_failure=yes
687 die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
690 if (sanitize_submodule_env; cd "$sm_path" && $command "$sha1")
693 elif test -n "$must_die_on_failure"
695 die_with_status 2 "$die_msg"
697 err="${err};$die_msg"
702 if test -n "$recursive"
705 prefix=$(git submodule--helper relative-path "$prefix$sm_path/" "$wt_prefix")
707 sanitize_submodule_env
714 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
717 err="${err};$die_msg"
720 die_with_status $res "$die_msg"
745 sanitize_submodule_env
747 git describe "$2" 2>/dev/null ||
748 git describe --tags "$2" 2>/dev/null ||
749 git describe --contains "$2" 2>/dev/null ||
750 git describe --all --always "$2"
753 test -z "$revname" || revname=" ($revname)"
756 # Show commit summary for submodules in index or working tree
758 # If '--cached' is given, show summary between index and given commit,
759 # or between working tree and given commit
761 # $@ = [commit (default 'HEAD'),] requested paths (default all)
768 # parse $args after "submodule ... summary".
783 isnumber "$summary_limit" || usage
787 summary_limit="${1#--summary-limit=}"
788 isnumber "$summary_limit" || usage
804 test $summary_limit = 0 && return
806 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
810 elif test -z "$1" || test "$1" = "HEAD"
812 # before the first commit: compare with an empty tree
813 head=$(git hash-object -w -t tree --stdin </dev/null)
814 test -z "$1" || shift
822 die "$(gettext "The --cached option cannot be used with the --files option")"
828 eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
829 # Get modified modules cared by user
830 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
831 sane_egrep '^:([0-7]* )?160000' |
832 while read mod_src mod_dst sha1_src sha1_dst status sm_path
834 # Always show modules deleted or type-changed (blob<->module)
835 if test "$status" = D || test "$status" = T
837 printf '%s\n' "$sm_path"
840 # Respect the ignore setting for --for-status.
841 if test -n "$for_status"
843 name=$(git submodule--helper name "$sm_path")
844 ignore_config=$(get_submodule_config "$name" ignore none)
845 test $status != A && test $ignore_config = all && continue
847 # Also show added or modified modules which are checked out
848 GIT_DIR="$sm_path/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
849 printf '%s\n' "$sm_path"
853 test -z "$modules" && return
855 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
856 sane_egrep '^:([0-7]* )?160000' |
858 while read mod_src mod_dst sha1_src sha1_dst status name
860 if test -z "$cached" &&
861 test $sha1_dst = 0000000000000000000000000000000000000000
865 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
867 100644 | 100755 | 120000)
868 sha1_dst=$(git hash-object $name)
874 eval_gettextln "unexpected mode \$mod_dst" >&2
881 test $mod_src = 160000 &&
882 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
885 test $mod_dst = 160000 &&
886 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
889 display_name=$(git submodule--helper relative-path "$name" "$wt_prefix")
892 case "$missing_src,$missing_dst" in
894 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_src")"
897 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_dst")"
900 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commits \$sha1_src and \$sha1_dst")"
905 if test $mod_src = 160000 && test $mod_dst = 160000
907 range="$sha1_src...$sha1_dst"
908 elif test $mod_src = 160000
914 GIT_DIR="$name/.git" \
915 git rev-list --first-parent $range -- | wc -l
917 total_commits=" ($(($total_commits + 0)))"
921 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
922 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
925 blob="$(gettext "blob")"
926 submodule="$(gettext "submodule")"
927 if test $mod_dst = 160000
929 echo "* $display_name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
931 echo "* $display_name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
934 echo "* $display_name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
938 # Don't give error msg for modification whose dst is not submodule
939 # i.e. deleted or changed to blob
940 test $mod_dst = 160000 && echo "$errmsg"
942 if test $mod_src = 160000 && test $mod_dst = 160000
945 test $summary_limit -gt 0 && limit="-$summary_limit"
946 GIT_DIR="$name/.git" \
947 git log $limit --pretty='format: %m %s' \
948 --first-parent $sha1_src...$sha1_dst
949 elif test $mod_dst = 160000
951 GIT_DIR="$name/.git" \
952 git log --pretty='format: > %s' -1 $sha1_dst
954 GIT_DIR="$name/.git" \
955 git log --pretty='format: < %s' -1 $sha1_src
963 # List all submodules, prefixed with:
964 # - submodule not initialized
965 # + different revision checked out
967 # If --cached was specified the revision in the index will be printed
968 # instead of the currently checked out revision.
970 # $@ = requested paths (default to all)
974 # parse $args after "submodule ... status".
1002 git submodule--helper list --prefix "$wt_prefix" "$@" ||
1003 echo "#unmatched" $?
1005 while read mode sha1 stage sm_path
1007 die_if_unmatched "$mode" "$sha1"
1008 name=$(git submodule--helper name "$sm_path") || exit
1009 url=$(git config submodule."$name".url)
1010 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
1011 if test "$stage" = U
1013 say "U$sha1 $displaypath"
1016 if test -z "$url" ||
1018 ! test -d "$sm_path"/.git &&
1019 ! test -f "$sm_path"/.git
1022 say "-$sha1 $displaypath"
1025 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
1027 set_name_rev "$sm_path" "$sha1"
1028 say " $sha1 $displaypath$revname"
1030 if test -z "$cached"
1032 sha1=$(sanitize_submodule_env; cd "$sm_path" && git rev-parse --verify HEAD)
1034 set_name_rev "$sm_path" "$sha1"
1035 say "+$sha1 $displaypath$revname"
1038 if test -n "$recursive"
1041 prefix="$displaypath/"
1042 sanitize_submodule_env
1047 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
1052 # Sync remote urls for submodules
1053 # This makes the value for remote.$remote.url match the value
1054 # specified in .gitmodules.
1083 git submodule--helper list --prefix "$wt_prefix" "$@" ||
1084 echo "#unmatched" $?
1086 while read mode sha1 stage sm_path
1088 die_if_unmatched "$mode" "$sha1"
1089 name=$(git submodule--helper name "$sm_path")
1090 url=$(git config -f .gitmodules --get submodule."$name".url)
1092 # Possibly a url relative to parent
1095 # rewrite foo/bar as ../.. to find path from
1096 # submodule work tree to superproject work tree
1097 up_path="$(printf '%s\n' "$sm_path" | sed "s/[^/][^/]*/../g")" &&
1098 # guarantee a trailing /
1099 up_path=${up_path%/}/ &&
1100 # path from submodule work tree to submodule origin repo
1101 sub_origin_url=$(git submodule--helper resolve-relative-url "$url" "$up_path") &&
1102 # path from superproject work tree to submodule origin repo
1103 super_config_url=$(git submodule--helper resolve-relative-url "$url") || exit
1106 sub_origin_url="$url"
1107 super_config_url="$url"
1111 if git config "submodule.$name.url" >/dev/null 2>/dev/null
1113 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
1114 say "$(eval_gettext "Synchronizing submodule url for '\$displaypath'")"
1115 git config submodule."$name".url "$super_config_url"
1117 if test -e "$sm_path"/.git
1120 sanitize_submodule_env
1122 remote=$(get_default_remote)
1123 git config remote."$remote".url "$sub_origin_url"
1125 if test -n "$recursive"
1127 prefix="$prefix$sm_path/"
1138 git submodule--helper absorb-git-dirs --prefix "$wt_prefix" "$@"
1141 # This loop parses the command line arguments to find the
1142 # subcommand name to dispatch. Parsing of the subcommand specific
1143 # options are primarily done by the subcommand implementations.
1144 # Subcommand specific options such as --branch and --cached are
1145 # parsed here as well, for backward compatibility.
1147 while test $# != 0 && test -z "$command"
1150 add | foreach | init | deinit | update | status | summary | sync | absorbgitdirs)
1180 # No command word defaults to "status"
1181 if test -z "$command"
1191 # "-b branch" is accepted only by "add"
1192 if test -n "$branch" && test "$command" != add
1197 # "--cached" is accepted only by "status" and "summary"
1198 if test -n "$cached" && test "$command" != status && test "$command" != summary