blob: c0d0e9a4c63495d4522666b6edf984c01c0cfa23 [file] [log] [blame]
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001#!/bin/sh
2#
Michał Górny4c8a9db2012-06-25 12:56:59 +02003# git-submodule.sh: add, init, update or list git submodules
Lars Hjemli70c7ac22007-05-26 15:56:40 +02004#
5# Copyright (c) 2007 Lars Hjemli
6
Johan Herland1d5bec82009-08-19 03:45:19 +02007dashless=$(basename "$0" | sed -e 's/-/ /')
Junio C Hamano681b0362012-11-21 13:25:42 -08008USAGE="[--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
Johan Herland64b19ff2009-08-19 03:45:24 +02009 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
Johan Herland1d5bec82009-08-19 03:45:19 +020010 or: $dashless [--quiet] init [--] [<path>...]
Stefan Bellerf6a52792016-05-05 12:52:32 -070011 or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)
Stefan Bellerabed0002016-05-26 14:59:43 -070012 or: $dashless [--quiet] update [--init] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--] [<path>...]
Junio C Hamanoadc54232009-08-27 16:59:25 -070013 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
Johan Herland15fc56a2009-08-19 03:45:22 +020014 or: $dashless [--quiet] foreach [--recursive] <command>
Stefan Bellerc32eaa82017-01-11 12:59:17 -080015 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]
16 or: $dashless [--quiet] absorbgitdirs [--] [<path>...]"
Junio C Hamano8f321a32007-11-06 01:50:02 -080017OPTIONS_SPEC=
John Keeping091a6eb2013-06-16 15:18:18 +010018SUBDIRECTORY_OK=Yes
Lars Hjemli70c7ac22007-05-26 15:56:40 +020019. git-sh-setup
Mark Levedahl98fcf842008-08-24 14:46:10 -040020. git-parse-remote
Lars Hjemli70c7ac22007-05-26 15:56:40 +020021require_work_tree
John Keeping091a6eb2013-06-16 15:18:18 +010022wt_prefix=$(git rev-parse --show-prefix)
23cd_to_toplevel
Lars Hjemli70c7ac22007-05-26 15:56:40 +020024
Brandon Williamsf1762d72016-12-14 14:39:52 -080025# Tell the rest of git that any URLs we get don't come
26# directly from the user, so it can apply policy as appropriate.
27GIT_PROTOCOL_FROM_USER=0
28export GIT_PROTOCOL_FROM_USER
Jeff King33cfccb2015-09-16 13:13:12 -040029
Junio C Hamano5c08dbb2008-01-15 02:48:45 -080030command=
Sven Verdoolaegeecda0722007-06-24 23:06:07 +020031branch=
Jens Lehmannd27b8762010-07-17 17:11:43 +020032force=
Michael S. Tsirkind92a3952009-05-04 22:30:01 +030033reference=
Lars Hjemli70c7ac22007-05-26 15:56:40 +020034cached=
Gerrit Pape48bb3032010-04-26 11:50:39 +020035recursive=
36init=
Jens Lehmann1c244f62009-08-13 21:32:50 +020037files=
W. Trevor King06b1abb2012-12-19 11:03:32 -050038remote=
Fabian Franz31ca3ac2009-02-05 20:18:32 -020039nofetch=
Johan Herland32948422009-06-03 08:27:06 +020040update=
Johan Herland15fc56a2009-08-19 03:45:22 +020041prefix=
Jens Lehmann73b08982012-09-30 01:05:58 +020042custom_name=
Fredrik Gustafsson275cd182013-07-02 23:42:56 +020043depth=
Jeff King72c5f882016-09-22 01:24:46 -040044progress=
Lars Hjemli70c7ac22007-05-26 15:56:40 +020045
Heiko Voigtbe9d0a32012-08-14 22:35:27 +020046die_if_unmatched ()
47{
48 if test "$1" = "#unmatched"
49 then
Johannes Sixtc4c02bf2016-07-22 21:14:38 +020050 exit ${2:-1}
Heiko Voigtbe9d0a32012-08-14 22:35:27 +020051 fi
52}
53
David Aguilara7b32692008-08-22 00:30:50 -070054#
W. Trevor King88ce00c2012-12-11 13:58:15 -050055# Print a submodule configuration setting
56#
57# $1 = submodule name
58# $2 = option name
59# $3 = default value
60#
61# Checks in the usual git-config places first (for overrides),
62# otherwise it falls back on .gitmodules. This allows you to
63# distribute project-wide defaults in .gitmodules, while still
64# customizing individual repositories if necessary. If the option is
65# not in .gitmodules either, print a default value.
66#
67get_submodule_config () {
68 name="$1"
69 option="$2"
70 default="$3"
71 value=$(git config submodule."$name"."$option")
72 if test -z "$value"
73 then
74 value=$(git config -f .gitmodules submodule."$name"."$option")
75 fi
76 printf '%s' "${value:-$default}"
77}
78
René Scharfe862ae6c2013-04-01 15:06:27 +020079isnumber()
80{
81 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
82}
83
Jacob Keller14111fc2016-02-29 14:58:35 -080084# Sanitize the local git environment for use within a submodule. We
85# can't simply use clear_local_git_env since we want to preserve some
86# of the settings from GIT_CONFIG_PARAMETERS.
87sanitize_submodule_env()
88{
Jeff King89044ba2016-05-04 21:22:19 -040089 save_config=$GIT_CONFIG_PARAMETERS
Jacob Keller14111fc2016-02-29 14:58:35 -080090 clear_local_git_env
Jeff King89044ba2016-05-04 21:22:19 -040091 GIT_CONFIG_PARAMETERS=$save_config
Jeff King860cba62016-04-28 09:37:44 -040092 export GIT_CONFIG_PARAMETERS
Jacob Keller14111fc2016-02-29 14:58:35 -080093}
94
Lars Hjemli70c7ac22007-05-26 15:56:40 +020095#
Sven Verdoolaegeecda0722007-06-24 23:06:07 +020096# Add a new submodule to the working tree, .gitmodules and the index
97#
Mark Levedahlec05df32008-07-09 21:05:40 -040098# $@ = repo path
Sven Verdoolaegeecda0722007-06-24 23:06:07 +020099#
100# optional branch is stored in global branch variable
101#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800102cmd_add()
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200103{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800104 # parse $args after "submodule ... add".
John Keeping091a6eb2013-06-16 15:18:18 +0100105 reference_path=
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800106 while test $# -ne 0
107 do
108 case "$1" in
109 -b | --branch)
110 case "$2" in '') usage ;; esac
111 branch=$2
112 shift
113 ;;
Jens Lehmannd27b8762010-07-17 17:11:43 +0200114 -f | --force)
115 force=$1
116 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800117 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700118 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800119 ;;
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300120 --reference)
121 case "$2" in '') usage ;; esac
John Keeping091a6eb2013-06-16 15:18:18 +0100122 reference_path=$2
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300123 shift
124 ;;
125 --reference=*)
John Keeping091a6eb2013-06-16 15:18:18 +0100126 reference_path="${1#--reference=}"
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300127 ;;
Jens Lehmann73b08982012-09-30 01:05:58 +0200128 --name)
129 case "$2" in '') usage ;; esac
130 custom_name=$2
131 shift
132 ;;
Fredrik Gustafsson275cd182013-07-02 23:42:56 +0200133 --depth)
134 case "$2" in '') usage ;; esac
135 depth="--depth=$2"
136 shift
137 ;;
138 --depth=*)
139 depth=$1
140 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800141 --)
142 shift
143 break
144 ;;
145 -*)
146 usage
147 ;;
148 *)
149 break
150 ;;
151 esac
152 shift
153 done
154
John Keeping091a6eb2013-06-16 15:18:18 +0100155 if test -n "$reference_path"
156 then
157 is_absolute_path "$reference_path" ||
158 reference_path="$wt_prefix$reference_path"
159
160 reference="--reference=$reference_path"
161 fi
162
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200163 repo=$1
Ramsay Jones64394e32012-04-17 19:00:58 +0100164 sm_path=$2
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200165
Ramsay Jones64394e32012-04-17 19:00:58 +0100166 if test -z "$sm_path"; then
Junio C Hamano6a066232014-06-10 08:33:39 -0700167 sm_path=$(printf '%s\n' "$repo" |
Jens Lehmann1414e572009-09-22 17:10:12 +0200168 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
169 fi
170
Elia Pinto496eeeb2014-06-10 05:28:33 -0700171 if test -z "$repo" || test -z "$sm_path"; then
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200172 usage
173 fi
174
John Keeping091a6eb2013-06-16 15:18:18 +0100175 is_absolute_path "$sm_path" || sm_path="$wt_prefix$sm_path"
176
Mark Levedahlec05df32008-07-09 21:05:40 -0400177 # assure repo is absolute or relative to parent
178 case "$repo" in
179 ./*|../*)
John Keeping091a6eb2013-06-16 15:18:18 +0100180 test -z "$wt_prefix" ||
181 die "$(gettext "Relative path can only be used from the toplevel of the working tree")"
182
Mark Levedahlec05df32008-07-09 21:05:40 -0400183 # dereference source url relative to parent's url
Stefan Beller63e95be2016-04-15 17:50:12 -0700184 realrepo=$(git submodule--helper resolve-relative-url "$repo") || exit
Mark Levedahlec05df32008-07-09 21:05:40 -0400185 ;;
186 *:*|/*)
187 # absolute url
188 realrepo=$repo
189 ;;
190 *)
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000191 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
Mark Levedahlec05df32008-07-09 21:05:40 -0400192 ;;
193 esac
194
Michael J Gruberdb75ada2009-03-03 16:08:21 +0100195 # normalize path:
196 # multiple //; leading ./; /./; /../; trailing /
Ramsay Jones64394e32012-04-17 19:00:58 +0100197 sm_path=$(printf '%s/\n' "$sm_path" |
Michael J Gruberdb75ada2009-03-03 16:08:21 +0100198 sed -e '
199 s|//*|/|g
200 s|^\(\./\)*||
Patrick Steinhardt8196e722015-01-30 16:14:03 +0100201 s|/\(\./\)*|/|g
Michael J Gruberdb75ada2009-03-03 16:08:21 +0100202 :start
203 s|\([^/]*\)/\.\./||
204 tstart
205 s|/*$||
206 ')
Stefan Beller619acfc2016-10-06 12:37:24 -0700207 if test -z "$force"
208 then
209 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
210 die "$(eval_gettext "'\$sm_path' already exists in the index")"
211 else
212 git ls-files -s "$sm_path" | sane_grep -v "^160000" > /dev/null 2>&1 &&
213 die "$(eval_gettext "'\$sm_path' already exists in the index and is not a submodule")"
214 fi
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200215
Ramsay Jones64394e32012-04-17 19:00:58 +0100216 if test -z "$force" && ! git add --dry-run --ignore-missing "$sm_path" > /dev/null 2>&1
Jens Lehmannd27b8762010-07-17 17:11:43 +0200217 then
Jon Seymour6ff875c2011-08-07 21:58:17 +1000218 eval_gettextln "The following path is ignored by one of your .gitignore files:
Ramsay Jones64394e32012-04-17 19:00:58 +0100219\$sm_path
Jon Seymour6ff875c2011-08-07 21:58:17 +1000220Use -f if you really want to add it." >&2
Jens Lehmannd27b8762010-07-17 17:11:43 +0200221 exit 1
222 fi
223
Jens Lehmann73b08982012-09-30 01:05:58 +0200224 if test -n "$custom_name"
225 then
226 sm_name="$custom_name"
227 else
228 sm_name="$sm_path"
229 fi
230
Mark Levedahld4264ca2008-03-04 20:15:02 -0500231 # perhaps the path exists and is already a git repo, else clone it
Ramsay Jones64394e32012-04-17 19:00:58 +0100232 if test -e "$sm_path"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500233 then
Elia Pinto496eeeb2014-06-10 05:28:33 -0700234 if test -d "$sm_path"/.git || test -f "$sm_path"/.git
Mark Levedahld4264ca2008-03-04 20:15:02 -0500235 then
Ramsay Jones64394e32012-04-17 19:00:58 +0100236 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500237 else
Ramsay Jones64394e32012-04-17 19:00:58 +0100238 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500239 fi
Mark Levedahlc2f93912008-07-09 21:05:41 -0400240
Mark Levedahld4264ca2008-03-04 20:15:02 -0500241 else
Jens Lehmann4b7c2862012-09-30 23:01:29 +0200242 if test -d ".git/modules/$sm_name"
243 then
244 if test -z "$force"
245 then
Vasco Almeida0d71dbf2016-06-17 21:54:12 +0000246 eval_gettextln >&2 "A git directory for '\$sm_name' is found locally with remote(s):"
Jens Lehmann4b7c2862012-09-30 23:01:29 +0200247 GIT_DIR=".git/modules/$sm_name" GIT_WORK_TREE=. git remote -v | grep '(fetch)' | sed -e s,^," ", -e s,' (fetch)',, >&2
Vasco Almeida0d71dbf2016-06-17 21:54:12 +0000248 die "$(eval_gettextln "\
249If you want to reuse this local git directory instead of cloning again from
250 \$realrepo
251use the '--force' option. If the local git directory is not the correct repo
252or you are unsure what this means choose another name with the '--name' option.")"
Jens Lehmann4b7c2862012-09-30 23:01:29 +0200253 else
Vasco Almeida0d71dbf2016-06-17 21:54:12 +0000254 eval_gettextln "Reactivating local git directory for submodule '\$sm_name'."
Jens Lehmann4b7c2862012-09-30 23:01:29 +0200255 fi
256 fi
Jacob Kellerd10e3b42016-02-29 14:58:30 -0800257 git submodule--helper clone ${GIT_QUIET:+--quiet} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" ${reference:+"$reference"} ${depth:+"$depth"} || exit
Junio C Hamanod851ffb2014-04-02 14:15:36 -0700258 (
Jacob Keller14111fc2016-02-29 14:58:35 -0800259 sanitize_submodule_env
Junio C Hamanod851ffb2014-04-02 14:15:36 -0700260 cd "$sm_path" &&
261 # ash fails to wordsplit ${branch:+-b "$branch"...}
262 case "$branch" in
263 '') git checkout -f -q ;;
264 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
265 esac
266 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500267 fi
Jens Lehmann73b08982012-09-30 01:05:58 +0200268 git config submodule."$sm_name".url "$realrepo"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500269
Ramsay Jones64394e32012-04-17 19:00:58 +0100270 git add $force "$sm_path" ||
271 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200272
Jens Lehmann73b08982012-09-30 01:05:58 +0200273 git config -f .gitmodules submodule."$sm_name".path "$sm_path" &&
274 git config -f .gitmodules submodule."$sm_name".url "$repo" &&
W. Trevor Kingb9289222012-12-19 11:03:33 -0500275 if test -n "$branch"
276 then
277 git config -f .gitmodules submodule."$sm_name".branch "$branch"
278 fi &&
Ævar Arnfjörð Bjarmason31991b02010-07-05 17:33:03 +0000279 git add --force .gitmodules ||
Ramsay Jones64394e32012-04-17 19:00:58 +0100280 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
Brandon Williams1b614c02017-03-17 15:38:05 -0700281
282 # NEEDSWORK: In a multi-working-tree world, this needs to be
283 # set in the per-worktree config.
284 if git config --get submodule.active >/dev/null
285 then
286 # If the submodule being adding isn't already covered by the
287 # current configured pathspec, set the submodule's active flag
288 if ! git submodule--helper is-active "$sm_path"
289 then
290 git config submodule."$sm_name".active "true"
291 fi
292 else
293 git config submodule."$sm_name".active "true"
294 fi
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200295}
296
297#
Mark Levedahl19a31f92008-08-10 19:10:04 -0400298# Execute an arbitrary command sequence in each checked out
299# submodule
300#
301# $@ = command to execute
302#
303cmd_foreach()
304{
Johan Herland1d5bec82009-08-19 03:45:19 +0200305 # parse $args after "submodule ... foreach".
306 while test $# -ne 0
307 do
308 case "$1" in
309 -q|--quiet)
310 GIT_QUIET=1
311 ;;
Johan Herland15fc56a2009-08-19 03:45:22 +0200312 --recursive)
313 recursive=1
314 ;;
Johan Herland1d5bec82009-08-19 03:45:19 +0200315 -*)
316 usage
317 ;;
318 *)
319 break
320 ;;
321 esac
322 shift
323 done
324
Ævar Arnfjörð Bjarmasonf030c962010-05-21 16:10:10 +0000325 toplevel=$(pwd)
326
Brandon Casey4dca1aa2011-06-29 19:34:58 -0500327 # dup stdin so that it can be restored when running the external
328 # command in the subshell (and a recursive call to this function)
329 exec 3<&0
330
Stefan Bellerb0f4b402016-05-31 16:59:33 -0700331 {
332 git submodule--helper list --prefix "$wt_prefix" ||
Johannes Sixtc4c02bf2016-07-22 21:14:38 +0200333 echo "#unmatched" $?
Stefan Bellerb0f4b402016-05-31 16:59:33 -0700334 } |
Brandon Williamscf9e55f2017-04-07 10:23:06 -0700335 while read -r mode sha1 stage sm_path
Mark Levedahl19a31f92008-08-10 19:10:04 -0400336 do
Johannes Sixtc4c02bf2016-07-22 21:14:38 +0200337 die_if_unmatched "$mode" "$sha1"
Ramsay Jones64394e32012-04-17 19:00:58 +0100338 if test -e "$sm_path"/.git
Mark Levedahl19a31f92008-08-10 19:10:04 -0400339 then
Stefan Beller44431df2016-05-31 17:27:59 -0700340 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
Stefan Bellerea2fa102016-03-29 18:27:41 -0700341 say "$(eval_gettext "Entering '\$displaypath'")"
Stefan Beller0ea306e2015-09-02 14:42:25 -0700342 name=$(git submodule--helper name "$sm_path")
Johan Herland15fc56a2009-08-19 03:45:22 +0200343 (
Ramsay Jones64394e32012-04-17 19:00:58 +0100344 prefix="$prefix$sm_path/"
Jacob Keller14111fc2016-02-29 14:58:35 -0800345 sanitize_submodule_env
Ramsay Jones64394e32012-04-17 19:00:58 +0100346 cd "$sm_path" &&
Stefan Beller44431df2016-05-31 17:27:59 -0700347 sm_path=$(git submodule--helper relative-path "$sm_path" "$wt_prefix") &&
John Keeping091a6eb2013-06-16 15:18:18 +0100348 # we make $path available to scripts ...
349 path=$sm_path &&
Anders Kaseorg1c4fb132013-09-27 06:23:55 -0400350 if test $# -eq 1
351 then
352 eval "$1"
353 else
354 "$@"
355 fi &&
Johan Herland15fc56a2009-08-19 03:45:22 +0200356 if test -n "$recursive"
357 then
358 cmd_foreach "--recursive" "$@"
359 fi
Brandon Casey4dca1aa2011-06-29 19:34:58 -0500360 ) <&3 3<&- ||
Stefan Bellerea2fa102016-03-29 18:27:41 -0700361 die "$(eval_gettext "Stopping at '\$displaypath'; script returned non-zero status.")"
Mark Levedahl19a31f92008-08-10 19:10:04 -0400362 fi
363 done
364}
365
366#
Lars Hjemli211b7f12007-06-06 11:13:02 +0200367# Register submodules in .git/config
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200368#
369# $@ = requested paths (default to all)
370#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800371cmd_init()
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200372{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800373 # parse $args after "submodule ... init".
374 while test $# -ne 0
375 do
376 case "$1" in
377 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700378 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800379 ;;
380 --)
381 shift
382 break
383 ;;
384 -*)
385 usage
386 ;;
387 *)
388 break
389 ;;
390 esac
391 shift
392 done
393
Stefan Beller6e7c14e2017-01-06 16:19:53 -0800394 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet} "$@"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200395}
396
397#
Jens Lehmanncf419822013-03-04 22:20:24 +0100398# Unregister submodules from .git/config and remove their work tree
399#
Jens Lehmanncf419822013-03-04 22:20:24 +0100400cmd_deinit()
401{
402 # parse $args after "submodule ... deinit".
Stefan Bellerf6a52792016-05-05 12:52:32 -0700403 deinit_all=
Jens Lehmanncf419822013-03-04 22:20:24 +0100404 while test $# -ne 0
405 do
406 case "$1" in
407 -f|--force)
408 force=$1
409 ;;
410 -q|--quiet)
411 GIT_QUIET=1
412 ;;
Stefan Bellerf6a52792016-05-05 12:52:32 -0700413 --all)
414 deinit_all=t
415 ;;
Jens Lehmanncf419822013-03-04 22:20:24 +0100416 --)
417 shift
418 break
419 ;;
420 -*)
421 usage
422 ;;
423 *)
424 break
425 ;;
426 esac
427 shift
428 done
429
Stefan Bellerf6a52792016-05-05 12:52:32 -0700430 if test -n "$deinit_all" && test "$#" -ne 0
Jens Lehmanncf419822013-03-04 22:20:24 +0100431 then
Stefan Bellerf6a52792016-05-05 12:52:32 -0700432 echo >&2 "$(eval_gettext "pathspec and --all are incompatible")"
433 usage
434 fi
435 if test $# = 0 && test -z "$deinit_all"
436 then
437 die "$(eval_gettext "Use '--all' if you really want to deinitialize all submodules")"
Jens Lehmanncf419822013-03-04 22:20:24 +0100438 fi
439
Stefan Bellerb0f4b402016-05-31 16:59:33 -0700440 {
441 git submodule--helper list --prefix "$wt_prefix" "$@" ||
Johannes Sixtc4c02bf2016-07-22 21:14:38 +0200442 echo "#unmatched" $?
Stefan Bellerb0f4b402016-05-31 16:59:33 -0700443 } |
Brandon Williamscf9e55f2017-04-07 10:23:06 -0700444 while read -r mode sha1 stage sm_path
Jens Lehmanncf419822013-03-04 22:20:24 +0100445 do
Johannes Sixtc4c02bf2016-07-22 21:14:38 +0200446 die_if_unmatched "$mode" "$sha1"
Stefan Beller0ea306e2015-09-02 14:42:25 -0700447 name=$(git submodule--helper name "$sm_path") || exit
Jens Lehmanncf419822013-03-04 22:20:24 +0100448
Stefan Beller44431df2016-05-31 17:27:59 -0700449 displaypath=$(git submodule--helper relative-path "$sm_path" "$wt_prefix")
John Keeping091a6eb2013-06-16 15:18:18 +0100450
Jens Lehmanncf419822013-03-04 22:20:24 +0100451 # Remove the submodule work tree (unless the user already did it)
452 if test -d "$sm_path"
453 then
454 # Protect submodules containing a .git directory
455 if test -d "$sm_path/.git"
456 then
Vasco Almeida0d71dbf2016-06-17 21:54:12 +0000457 die "$(eval_gettext "\
458Submodule work tree '\$displaypath' contains a .git directory
459(use 'rm -rf' if you really want to remove it including all of its history)")"
Jens Lehmanncf419822013-03-04 22:20:24 +0100460 fi
461
462 if test -z "$force"
463 then
Jens Lehmann7b294bf2013-04-01 21:02:00 +0200464 git rm -qn "$sm_path" ||
John Keeping091a6eb2013-06-16 15:18:18 +0100465 die "$(eval_gettext "Submodule work tree '\$displaypath' contains local modifications; use '-f' to discard them")"
Jens Lehmanncf419822013-03-04 22:20:24 +0100466 fi
Jens Lehmann7b294bf2013-04-01 21:02:00 +0200467 rm -rf "$sm_path" &&
John Keeping091a6eb2013-06-16 15:18:18 +0100468 say "$(eval_gettext "Cleared directory '\$displaypath'")" ||
469 say "$(eval_gettext "Could not remove submodule work tree '\$displaypath'")"
Jens Lehmanncf419822013-03-04 22:20:24 +0100470 fi
471
John Keeping091a6eb2013-06-16 15:18:18 +0100472 mkdir "$sm_path" || say "$(eval_gettext "Could not create empty submodule directory '\$displaypath'")"
Jens Lehmanncf419822013-03-04 22:20:24 +0100473
474 # Remove the .git/config entries (unless the user already did it)
475 if test -n "$(git config --get-regexp submodule."$name\.")"
476 then
477 # Remove the whole section so we have a clean state when
478 # the user later decides to init this submodule again
479 url=$(git config submodule."$name".url)
480 git config --remove-section submodule."$name" 2>/dev/null &&
John Keeping091a6eb2013-06-16 15:18:18 +0100481 say "$(eval_gettext "Submodule '\$name' (\$url) unregistered for path '\$displaypath'")"
Jens Lehmanncf419822013-03-04 22:20:24 +0100482 fi
483 done
484}
485
Stefan Bellerfb43e312016-02-23 19:32:13 -0800486is_tip_reachable () (
Junio C Hamano01e1d542016-04-06 11:39:12 -0700487 sanitize_submodule_env &&
Stefan Bellerfb43e312016-02-23 19:32:13 -0800488 cd "$1" &&
489 rev=$(git rev-list -n 1 "$2" --not --all 2>/dev/null) &&
490 test -z "$rev"
491)
492
493fetch_in_submodule () (
Junio C Hamano01e1d542016-04-06 11:39:12 -0700494 sanitize_submodule_env &&
Stefan Bellerfb43e312016-02-23 19:32:13 -0800495 cd "$1" &&
496 case "$2" in
497 '')
498 git fetch ;;
499 *)
Stefan Beller6cbf4542016-07-28 17:44:04 -0700500 shift
501 git fetch $(get_default_remote) "$@" ;;
Stefan Bellerfb43e312016-02-23 19:32:13 -0800502 esac
503)
504
Jens Lehmanncf419822013-03-04 22:20:24 +0100505#
Lars Hjemli211b7f12007-06-06 11:13:02 +0200506# Update each submodule path to correct revision, using clone and checkout as needed
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200507#
508# $@ = requested paths (default to all)
509#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800510cmd_update()
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200511{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800512 # parse $args after "submodule ... update".
513 while test $# -ne 0
514 do
515 case "$1" in
516 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700517 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800518 ;;
Jeff King72c5f882016-09-22 01:24:46 -0400519 --progress)
520 progress="--progress"
521 ;;
Johannes Schindelinbe4d2c82008-05-16 11:23:03 +0100522 -i|--init)
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300523 init=1
Johannes Schindelinbe4d2c82008-05-16 11:23:03 +0100524 ;;
W. Trevor King06b1abb2012-12-19 11:03:32 -0500525 --remote)
526 remote=1
527 ;;
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200528 -N|--no-fetch)
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200529 nofetch=1
530 ;;
Nicolas Morey-Chaisemartin9db31bd2011-04-01 11:42:03 +0200531 -f|--force)
532 force=$1
533 ;;
Peter Huttererca2cedb2009-04-24 09:06:38 +1000534 -r|--rebase)
Johan Herland32948422009-06-03 08:27:06 +0200535 update="rebase"
Peter Huttererca2cedb2009-04-24 09:06:38 +1000536 ;;
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300537 --reference)
538 case "$2" in '') usage ;; esac
539 reference="--reference=$2"
Kevin Ballard98dbe632010-11-02 23:26:25 -0700540 shift
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300541 ;;
542 --reference=*)
543 reference="$1"
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300544 ;;
Johan Herland42b49172009-06-03 00:59:12 +0200545 -m|--merge)
Johan Herland42b49172009-06-03 00:59:12 +0200546 update="merge"
547 ;;
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200548 --recursive)
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200549 recursive=1
550 ;;
Junio C Hamanoefc5fb62011-10-10 15:56:16 -0700551 --checkout)
552 update="checkout"
553 ;;
Stefan Bellerabed0002016-05-26 14:59:43 -0700554 --recommend-shallow)
555 recommend_shallow="--recommend-shallow"
556 ;;
557 --no-recommend-shallow)
558 recommend_shallow="--no-recommend-shallow"
559 ;;
Fredrik Gustafsson275cd182013-07-02 23:42:56 +0200560 --depth)
561 case "$2" in '') usage ;; esac
562 depth="--depth=$2"
563 shift
564 ;;
565 --depth=*)
566 depth=$1
567 ;;
Stefan Beller2335b872016-02-29 18:07:19 -0800568 -j|--jobs)
569 case "$2" in '') usage ;; esac
570 jobs="--jobs=$2"
571 shift
572 ;;
573 --jobs=*)
574 jobs=$1
575 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800576 --)
577 shift
578 break
579 ;;
580 -*)
581 usage
582 ;;
583 *)
584 break
585 ;;
586 esac
Kevin Ballard98dbe632010-11-02 23:26:25 -0700587 shift
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800588 done
589
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300590 if test -n "$init"
591 then
592 cmd_init "--" "$@" || return
593 fi
594
Stefan Beller48308682016-02-29 18:07:17 -0800595 {
596 git submodule--helper update-clone ${GIT_QUIET:+--quiet} \
Jeff King72c5f882016-09-22 01:24:46 -0400597 ${progress:+"$progress"} \
Stefan Beller48308682016-02-29 18:07:17 -0800598 ${wt_prefix:+--prefix "$wt_prefix"} \
599 ${prefix:+--recursive-prefix "$prefix"} \
600 ${update:+--update "$update"} \
Stefan Beller5f50f332016-08-11 16:14:01 -0700601 ${reference:+"$reference"} \
Stefan Beller48308682016-02-29 18:07:17 -0800602 ${depth:+--depth "$depth"} \
Stefan Bellerabed0002016-05-26 14:59:43 -0700603 ${recommend_shallow:+"$recommend_shallow"} \
Stefan Beller2335b872016-02-29 18:07:19 -0800604 ${jobs:+$jobs} \
Johannes Sixtc4c02bf2016-07-22 21:14:38 +0200605 "$@" || echo "#unmatched" $?
Stefan Beller48308682016-02-29 18:07:17 -0800606 } | {
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200607 err=
Brandon Williamscf9e55f2017-04-07 10:23:06 -0700608 while read -r mode sha1 stage just_cloned sm_path
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200609 do
Johannes Sixtc4c02bf2016-07-22 21:14:38 +0200610 die_if_unmatched "$mode" "$sha1"
Stefan Beller48308682016-02-29 18:07:17 -0800611
Stefan Beller0ea306e2015-09-02 14:42:25 -0700612 name=$(git submodule--helper name "$sm_path") || exit
Junio C Hamano5be60072007-07-02 22:52:14 -0700613 url=$(git config submodule."$name".url)
Junio C Hamanoefc5fb62011-10-10 15:56:16 -0700614 if ! test -z "$update"
615 then
616 update_module=$update
617 else
618 update_module=$(git config submodule."$name".update)
W. Trevor Kinga2aed082014-01-15 20:10:22 -0800619 if test -z "$update_module"
620 then
621 update_module="checkout"
622 fi
Junio C Hamanoefc5fb62011-10-10 15:56:16 -0700623 fi
624
Stefan Beller44431df2016-05-31 17:27:59 -0700625 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
John Keeping091a6eb2013-06-16 15:18:18 +0100626
Stefan Beller48308682016-02-29 18:07:17 -0800627 if test $just_cloned -eq 1
Junio C Hamanod851ffb2014-04-02 14:15:36 -0700628 then
Lars Hjemlibf2d8242007-06-11 21:12:22 +0200629 subsha1=
Stefan Bellere7b37ca2017-01-25 15:48:51 -0800630 case "$update_module" in
631 merge | rebase | none)
632 update_module=checkout ;;
633 esac
Lars Hjemlibf2d8242007-06-11 21:12:22 +0200634 else
Jacob Keller14111fc2016-02-29 14:58:35 -0800635 subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700636 git rev-parse --verify HEAD) ||
John Keeping091a6eb2013-06-16 15:18:18 +0100637 die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
Lars Hjemli211b7f12007-06-06 11:13:02 +0200638 fi
639
W. Trevor King06b1abb2012-12-19 11:03:32 -0500640 if test -n "$remote"
641 then
Stefan Beller92bbe7c2016-08-03 13:44:03 -0700642 branch=$(git submodule--helper remote-branch "$sm_path")
W. Trevor King06b1abb2012-12-19 11:03:32 -0500643 if test -z "$nofetch"
644 then
645 # Fetch remote before determining tracking $sha1
Stefan Beller6cbf4542016-07-28 17:44:04 -0700646 fetch_in_submodule "$sm_path" $depth ||
W. Trevor King06b1abb2012-12-19 11:03:32 -0500647 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
648 fi
Jacob Keller14111fc2016-02-29 14:58:35 -0800649 remote_name=$(sanitize_submodule_env; cd "$sm_path" && get_default_remote)
650 sha1=$(sanitize_submodule_env; cd "$sm_path" &&
W. Trevor King06b1abb2012-12-19 11:03:32 -0500651 git rev-parse --verify "${remote_name}/${branch}") ||
Vasco Almeidac87302b2016-06-17 21:54:13 +0000652 die "$(eval_gettext "Unable to find current \${remote_name}/\${branch} revision in submodule path '\$sm_path'")"
W. Trevor King06b1abb2012-12-19 11:03:32 -0500653 fi
654
Elia Pinto496eeeb2014-06-10 05:28:33 -0700655 if test "$subsha1" != "$sha1" || test -n "$force"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200656 then
Nicolas Morey-Chaisemartin9db31bd2011-04-01 11:42:03 +0200657 subforce=$force
658 # If we don't already have a -f flag and the submodule has never been checked out
Elia Pinto496eeeb2014-06-10 05:28:33 -0700659 if test -z "$subsha1" && test -z "$force"
Ping Yinb9b378a2008-09-26 23:33:23 +0800660 then
Nicolas Morey-Chaisemartin9db31bd2011-04-01 11:42:03 +0200661 subforce="-f"
Ping Yinb9b378a2008-09-26 23:33:23 +0800662 fi
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200663
664 if test -z "$nofetch"
665 then
Jens Lehmanne5f522d2011-03-06 23:13:36 +0100666 # Run fetch only if $sha1 isn't present or it
667 # is not reachable from a ref.
Stefan Bellerfb43e312016-02-23 19:32:13 -0800668 is_tip_reachable "$sm_path" "$sha1" ||
Stefan Beller6cbf4542016-07-28 17:44:04 -0700669 fetch_in_submodule "$sm_path" $depth ||
John Keeping091a6eb2013-06-16 15:18:18 +0100670 die "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
Stefan Bellerfb43e312016-02-23 19:32:13 -0800671
672 # Now we tried the usual fetch, but $sha1 may
673 # not be reachable from any of the refs
674 is_tip_reachable "$sm_path" "$sha1" ||
Stefan Beller6cbf4542016-07-28 17:44:04 -0700675 fetch_in_submodule "$sm_path" $depth "$sha1" ||
Vasco Almeidac87302b2016-06-17 21:54:13 +0000676 die "$(eval_gettext "Fetched in submodule path '\$displaypath', but it did not contain \$sha1. Direct fetching of that commit failed.")"
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200677 fi
678
Junio C Hamano877449c2011-06-13 12:17:52 -0700679 must_die_on_failure=
Johan Herland32948422009-06-03 08:27:06 +0200680 case "$update_module" in
W. Trevor Kinga2aed082014-01-15 20:10:22 -0800681 checkout)
682 command="git checkout $subforce -q"
683 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
684 say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
685 ;;
Johan Herland32948422009-06-03 08:27:06 +0200686 rebase)
687 command="git rebase"
John Keeping091a6eb2013-06-16 15:18:18 +0100688 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$displaypath'")"
689 say_msg="$(eval_gettext "Submodule path '\$displaypath': rebased into '\$sha1'")"
Junio C Hamano877449c2011-06-13 12:17:52 -0700690 must_die_on_failure=yes
Johan Herland32948422009-06-03 08:27:06 +0200691 ;;
Johan Herland42b49172009-06-03 00:59:12 +0200692 merge)
693 command="git merge"
John Keeping091a6eb2013-06-16 15:18:18 +0100694 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$displaypath'")"
695 say_msg="$(eval_gettext "Submodule path '\$displaypath': merged in '\$sha1'")"
Junio C Hamano877449c2011-06-13 12:17:52 -0700696 must_die_on_failure=yes
Johan Herland42b49172009-06-03 00:59:12 +0200697 ;;
Chris Packham6cb57282013-07-03 21:02:02 +1200698 !*)
699 command="${update_module#!}"
Stefan Bellerb08238a2016-03-29 18:27:44 -0700700 die_msg="$(eval_gettext "Execution of '\$command \$sha1' failed in submodule path '\$displaypath'")"
701 say_msg="$(eval_gettext "Submodule path '\$displaypath': '\$command \$sha1'")"
Chris Packham6cb57282013-07-03 21:02:02 +1200702 must_die_on_failure=yes
703 ;;
Johan Herland32948422009-06-03 08:27:06 +0200704 *)
W. Trevor Kinga2aed082014-01-15 20:10:22 -0800705 die "$(eval_gettext "Invalid update mode '$update_module' for submodule '$name'")"
Johan Herland32948422009-06-03 08:27:06 +0200706 esac
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200707
Jacob Keller14111fc2016-02-29 14:58:35 -0800708 if (sanitize_submodule_env; cd "$sm_path" && $command "$sha1")
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200709 then
Junio C Hamanoff968f02011-07-13 14:31:35 -0700710 say "$say_msg"
Junio C Hamano877449c2011-06-13 12:17:52 -0700711 elif test -n "$must_die_on_failure"
712 then
Junio C Hamanoff968f02011-07-13 14:31:35 -0700713 die_with_status 2 "$die_msg"
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200714 else
Junio C Hamanoff968f02011-07-13 14:31:35 -0700715 err="${err};$die_msg"
Junio C Hamano877449c2011-06-13 12:17:52 -0700716 continue
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200717 fi
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200718 fi
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200719
720 if test -n "$recursive"
721 then
William Entriken75bf5e62013-03-02 14:44:59 -0500722 (
Stefan Beller44431df2016-05-31 17:27:59 -0700723 prefix=$(git submodule--helper relative-path "$prefix$sm_path/" "$wt_prefix")
Stefan Beller36042422016-04-15 17:50:13 -0700724 wt_prefix=
Jacob Keller14111fc2016-02-29 14:58:35 -0800725 sanitize_submodule_env
William Entriken75bf5e62013-03-02 14:44:59 -0500726 cd "$sm_path" &&
Jens Lehmann36141282013-11-11 21:55:52 +0100727 eval cmd_update
William Entriken75bf5e62013-03-02 14:44:59 -0500728 )
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200729 res=$?
730 if test $res -gt 0
731 then
John Keeping091a6eb2013-06-16 15:18:18 +0100732 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
Stefan Bellerbb9d91b2016-06-09 12:06:37 -0700733 if test $res -ne 2
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200734 then
Junio C Hamanoff968f02011-07-13 14:31:35 -0700735 err="${err};$die_msg"
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200736 continue
737 else
Junio C Hamanoff968f02011-07-13 14:31:35 -0700738 die_with_status $res "$die_msg"
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200739 fi
740 fi
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200741 fi
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200742 done
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200743
744 if test -n "$err"
745 then
746 OIFS=$IFS
747 IFS=';'
748 for e in $err
749 do
750 if test -n "$e"
751 then
752 echo >&2 "$e"
753 fi
754 done
755 IFS=$OIFS
756 exit 1
757 fi
758 }
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200759}
760
Emil Medvebffe71f2007-06-26 18:40:58 -0500761set_name_rev () {
762 revname=$( (
Jacob Keller14111fc2016-02-29 14:58:35 -0800763 sanitize_submodule_env
Emil Medvebffe71f2007-06-26 18:40:58 -0500764 cd "$1" && {
Junio C Hamano5be60072007-07-02 22:52:14 -0700765 git describe "$2" 2>/dev/null ||
766 git describe --tags "$2" 2>/dev/null ||
Mark Levedahlf669ac02008-04-14 22:48:06 -0400767 git describe --contains "$2" 2>/dev/null ||
768 git describe --all --always "$2"
Emil Medvebffe71f2007-06-26 18:40:58 -0500769 }
770 ) )
771 test -z "$revname" || revname=" ($revname)"
772}
Ping Yin28f9af52008-03-11 21:52:15 +0800773#
774# Show commit summary for submodules in index or working tree
775#
776# If '--cached' is given, show summary between index and given commit,
777# or between working tree and given commit
778#
779# $@ = [commit (default 'HEAD'),] requested paths (default all)
780#
781cmd_summary() {
Ping Yinf2dc06a2008-03-11 21:52:17 +0800782 summary_limit=-1
Ping Yind0f64dd2008-04-12 23:05:31 +0800783 for_status=
Jens Lehmann1c244f62009-08-13 21:32:50 +0200784 diff_cmd=diff-index
Emil Medvebffe71f2007-06-26 18:40:58 -0500785
Ping Yin28f9af52008-03-11 21:52:15 +0800786 # parse $args after "submodule ... summary".
787 while test $# -ne 0
788 do
789 case "$1" in
790 --cached)
791 cached="$1"
792 ;;
Jens Lehmann1c244f62009-08-13 21:32:50 +0200793 --files)
794 files="$1"
795 ;;
Ping Yind0f64dd2008-04-12 23:05:31 +0800796 --for-status)
797 for_status="$1"
798 ;;
Ping Yinf2dc06a2008-03-11 21:52:17 +0800799 -n|--summary-limit)
René Scharfe862ae6c2013-04-01 15:06:27 +0200800 summary_limit="$2"
801 isnumber "$summary_limit" || usage
Ping Yinf2dc06a2008-03-11 21:52:17 +0800802 shift
803 ;;
René Scharfe862ae6c2013-04-01 15:06:27 +0200804 --summary-limit=*)
805 summary_limit="${1#--summary-limit=}"
806 isnumber "$summary_limit" || usage
807 ;;
Ping Yin28f9af52008-03-11 21:52:15 +0800808 --)
809 shift
810 break
811 ;;
812 -*)
813 usage
814 ;;
815 *)
816 break
817 ;;
818 esac
819 shift
820 done
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200821
Ping Yinf2dc06a2008-03-11 21:52:17 +0800822 test $summary_limit = 0 && return
823
Johan Herland3deea892010-02-16 11:21:14 +0100824 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
Ping Yin28f9af52008-03-11 21:52:15 +0800825 then
826 head=$rev
Jeff Kingcaa9c3c2010-03-03 14:19:09 -0800827 test $# = 0 || shift
Elia Pinto496eeeb2014-06-10 05:28:33 -0700828 elif test -z "$1" || test "$1" = "HEAD"
Johan Herland3deea892010-02-16 11:21:14 +0100829 then
Junio C Hamano14e940d2010-03-03 14:19:10 -0800830 # before the first commit: compare with an empty tree
831 head=$(git hash-object -w -t tree --stdin </dev/null)
Jens Lehmann2ea6c2c2010-03-09 15:55:32 +0100832 test -z "$1" || shift
Ping Yin28f9af52008-03-11 21:52:15 +0800833 else
Johan Herland3deea892010-02-16 11:21:14 +0100834 head="HEAD"
Ping Yin28f9af52008-03-11 21:52:15 +0800835 fi
836
Jens Lehmann1c244f62009-08-13 21:32:50 +0200837 if [ -n "$files" ]
838 then
839 test -n "$cached" &&
Jiang Xin465d6a02012-07-25 22:53:09 +0800840 die "$(gettext "The --cached option cannot be used with the --files option")"
Jens Lehmann1c244f62009-08-13 21:32:50 +0200841 diff_cmd=diff-files
842 head=
843 fi
844
Ping Yin28f9af52008-03-11 21:52:15 +0800845 cd_to_toplevel
John Keeping091a6eb2013-06-16 15:18:18 +0100846 eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
Ping Yin28f9af52008-03-11 21:52:15 +0800847 # Get modified modules cared by user
Jens Lehmann18076502010-06-25 16:56:02 +0200848 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
Junio C Hamanoe1622bf2009-11-23 15:56:32 -0800849 sane_egrep '^:([0-7]* )?160000' |
Brandon Williamscf9e55f2017-04-07 10:23:06 -0700850 while read -r mod_src mod_dst sha1_src sha1_dst status sm_path
Ping Yin28f9af52008-03-11 21:52:15 +0800851 do
852 # Always show modules deleted or type-changed (blob<->module)
Elia Pinto496eeeb2014-06-10 05:28:33 -0700853 if test "$status" = D || test "$status" = T
854 then
Junio C Hamano6a066232014-06-10 08:33:39 -0700855 printf '%s\n' "$sm_path"
Elia Pinto496eeeb2014-06-10 05:28:33 -0700856 continue
857 fi
Brian M. Carlson927b26f2013-09-01 20:06:49 +0000858 # Respect the ignore setting for --for-status.
859 if test -n "$for_status"
860 then
Stefan Beller0ea306e2015-09-02 14:42:25 -0700861 name=$(git submodule--helper name "$sm_path")
Brian M. Carlson927b26f2013-09-01 20:06:49 +0000862 ignore_config=$(get_submodule_config "$name" ignore none)
Elia Pinto496eeeb2014-06-10 05:28:33 -0700863 test $status != A && test $ignore_config = all && continue
Brian M. Carlson927b26f2013-09-01 20:06:49 +0000864 fi
Ping Yin28f9af52008-03-11 21:52:15 +0800865 # Also show added or modified modules which are checked out
Brian M. Carlson2be94502013-08-17 17:25:42 +0000866 GIT_DIR="$sm_path/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
Junio C Hamano6a066232014-06-10 08:33:39 -0700867 printf '%s\n' "$sm_path"
Ping Yin28f9af52008-03-11 21:52:15 +0800868 done
869 )
Ping Yin1cb639e2008-03-11 21:52:16 +0800870
Ping Yind0f64dd2008-04-12 23:05:31 +0800871 test -z "$modules" && return
872
Jens Lehmann18076502010-06-25 16:56:02 +0200873 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
Junio C Hamanoe1622bf2009-11-23 15:56:32 -0800874 sane_egrep '^:([0-7]* )?160000' |
Ping Yin1cb639e2008-03-11 21:52:16 +0800875 cut -c2- |
Brandon Williamscf9e55f2017-04-07 10:23:06 -0700876 while read -r mod_src mod_dst sha1_src sha1_dst status name
Ping Yin1cb639e2008-03-11 21:52:16 +0800877 do
878 if test -z "$cached" &&
879 test $sha1_dst = 0000000000000000000000000000000000000000
880 then
881 case "$mod_dst" in
882 160000)
883 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
884 ;;
885 100644 | 100755 | 120000)
886 sha1_dst=$(git hash-object $name)
887 ;;
888 000000)
889 ;; # removed
890 *)
891 # unexpected type
Jon Seymour6ff875c2011-08-07 21:58:17 +1000892 eval_gettextln "unexpected mode \$mod_dst" >&2
Ping Yin1cb639e2008-03-11 21:52:16 +0800893 continue ;;
894 esac
895 fi
896 missing_src=
897 missing_dst=
898
899 test $mod_src = 160000 &&
Miklos Vajna30353a42008-12-03 14:26:52 +0100900 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
Ping Yin1cb639e2008-03-11 21:52:16 +0800901 missing_src=t
902
903 test $mod_dst = 160000 &&
Miklos Vajna30353a42008-12-03 14:26:52 +0100904 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
Ping Yin1cb639e2008-03-11 21:52:16 +0800905 missing_dst=t
906
Stefan Beller44431df2016-05-31 17:27:59 -0700907 display_name=$(git submodule--helper relative-path "$name" "$wt_prefix")
John Keeping091a6eb2013-06-16 15:18:18 +0100908
Ping Yin1cb639e2008-03-11 21:52:16 +0800909 total_commits=
910 case "$missing_src,$missing_dst" in
911 t,)
John Keeping091a6eb2013-06-16 15:18:18 +0100912 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_src")"
Ping Yin1cb639e2008-03-11 21:52:16 +0800913 ;;
914 ,t)
John Keeping091a6eb2013-06-16 15:18:18 +0100915 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_dst")"
Ping Yin1cb639e2008-03-11 21:52:16 +0800916 ;;
917 t,t)
John Keeping091a6eb2013-06-16 15:18:18 +0100918 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commits \$sha1_src and \$sha1_dst")"
Ping Yin1cb639e2008-03-11 21:52:16 +0800919 ;;
920 *)
921 errmsg=
922 total_commits=$(
Elia Pinto496eeeb2014-06-10 05:28:33 -0700923 if test $mod_src = 160000 && test $mod_dst = 160000
Ping Yin1cb639e2008-03-11 21:52:16 +0800924 then
925 range="$sha1_src...$sha1_dst"
926 elif test $mod_src = 160000
927 then
928 range=$sha1_src
929 else
930 range=$sha1_dst
931 fi
932 GIT_DIR="$name/.git" \
Jeff Kingb0e621a2010-04-08 15:42:37 -0400933 git rev-list --first-parent $range -- | wc -l
Ping Yin1cb639e2008-03-11 21:52:16 +0800934 )
Johannes Sixteed35592008-03-12 09:30:01 +0100935 total_commits=" ($(($total_commits + 0)))"
Ping Yin1cb639e2008-03-11 21:52:16 +0800936 ;;
937 esac
938
939 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
940 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
941 if test $status = T
942 then
Ævar Arnfjörð Bjarmasonb3e73442011-05-21 18:44:09 +0000943 blob="$(gettext "blob")"
944 submodule="$(gettext "submodule")"
Ping Yin1cb639e2008-03-11 21:52:16 +0800945 if test $mod_dst = 160000
946 then
John Keeping091a6eb2013-06-16 15:18:18 +0100947 echo "* $display_name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
Ping Yin1cb639e2008-03-11 21:52:16 +0800948 else
John Keeping091a6eb2013-06-16 15:18:18 +0100949 echo "* $display_name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
Ping Yin1cb639e2008-03-11 21:52:16 +0800950 fi
951 else
John Keeping091a6eb2013-06-16 15:18:18 +0100952 echo "* $display_name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
Ping Yin1cb639e2008-03-11 21:52:16 +0800953 fi
954 if test -n "$errmsg"
955 then
956 # Don't give error msg for modification whose dst is not submodule
957 # i.e. deleted or changed to blob
958 test $mod_dst = 160000 && echo "$errmsg"
959 else
Elia Pinto496eeeb2014-06-10 05:28:33 -0700960 if test $mod_src = 160000 && test $mod_dst = 160000
Ping Yin1cb639e2008-03-11 21:52:16 +0800961 then
Ping Yinf2dc06a2008-03-11 21:52:17 +0800962 limit=
963 test $summary_limit -gt 0 && limit="-$summary_limit"
Ping Yin1cb639e2008-03-11 21:52:16 +0800964 GIT_DIR="$name/.git" \
Ping Yinf2dc06a2008-03-11 21:52:17 +0800965 git log $limit --pretty='format: %m %s' \
Ping Yin1cb639e2008-03-11 21:52:16 +0800966 --first-parent $sha1_src...$sha1_dst
967 elif test $mod_dst = 160000
968 then
969 GIT_DIR="$name/.git" \
970 git log --pretty='format: > %s' -1 $sha1_dst
971 else
972 GIT_DIR="$name/.git" \
973 git log --pretty='format: < %s' -1 $sha1_src
974 fi
975 echo
976 fi
977 echo
Matthieu Moy3ba74072013-09-06 19:43:06 +0200978 done
Ping Yin28f9af52008-03-11 21:52:15 +0800979}
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200980#
Lars Hjemli941987a2007-06-11 21:12:24 +0200981# List all submodules, prefixed with:
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200982# - submodule not initialized
983# + different revision checked out
984#
985# If --cached was specified the revision in the index will be printed
986# instead of the currently checked out revision.
987#
988# $@ = requested paths (default to all)
989#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800990cmd_status()
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200991{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800992 # parse $args after "submodule ... status".
993 while test $# -ne 0
994 do
995 case "$1" in
996 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700997 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800998 ;;
999 --cached)
1000 cached=1
1001 ;;
Johan Herland64b19ff2009-08-19 03:45:24 +02001002 --recursive)
1003 recursive=1
1004 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001005 --)
1006 shift
1007 break
1008 ;;
1009 -*)
1010 usage
1011 ;;
1012 *)
1013 break
1014 ;;
1015 esac
1016 shift
1017 done
1018
Stefan Bellerb0f4b402016-05-31 16:59:33 -07001019 {
1020 git submodule--helper list --prefix "$wt_prefix" "$@" ||
Johannes Sixtc4c02bf2016-07-22 21:14:38 +02001021 echo "#unmatched" $?
Stefan Bellerb0f4b402016-05-31 16:59:33 -07001022 } |
Brandon Williamscf9e55f2017-04-07 10:23:06 -07001023 while read -r mode sha1 stage sm_path
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001024 do
Johannes Sixtc4c02bf2016-07-22 21:14:38 +02001025 die_if_unmatched "$mode" "$sha1"
Stefan Beller0ea306e2015-09-02 14:42:25 -07001026 name=$(git submodule--helper name "$sm_path") || exit
Stefan Beller44431df2016-05-31 17:27:59 -07001027 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
Nicolas Morey-Chaisemartin313ee0d2011-03-30 07:20:02 +02001028 if test "$stage" = U
1029 then
1030 say "U$sha1 $displaypath"
1031 continue
1032 fi
Brandon Williams6dc9f012017-03-16 15:29:44 -07001033 if ! git submodule--helper is-active "$sm_path" ||
Elia Pinto496eeeb2014-06-10 05:28:33 -07001034 {
1035 ! test -d "$sm_path"/.git &&
1036 ! test -f "$sm_path"/.git
1037 }
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001038 then
Johan Herland64b19ff2009-08-19 03:45:24 +02001039 say "-$sha1 $displaypath"
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001040 continue;
1041 fi
Ramsay Jones64394e32012-04-17 19:00:58 +01001042 if git diff-files --ignore-submodules=dirty --quiet -- "$sm_path"
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001043 then
Fredrik Gustafssonb545cd12013-06-17 11:55:36 +02001044 set_name_rev "$sm_path" "$sha1"
Johan Herland64b19ff2009-08-19 03:45:24 +02001045 say " $sha1 $displaypath$revname"
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001046 else
1047 if test -z "$cached"
1048 then
Jacob Keller14111fc2016-02-29 14:58:35 -08001049 sha1=$(sanitize_submodule_env; cd "$sm_path" && git rev-parse --verify HEAD)
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001050 fi
Fredrik Gustafssonb545cd12013-06-17 11:55:36 +02001051 set_name_rev "$sm_path" "$sha1"
Johan Herland64b19ff2009-08-19 03:45:24 +02001052 say "+$sha1 $displaypath$revname"
1053 fi
1054
1055 if test -n "$recursive"
1056 then
1057 (
1058 prefix="$displaypath/"
Jacob Keller14111fc2016-02-29 14:58:35 -08001059 sanitize_submodule_env
Stefan Beller10450cf2016-03-29 18:27:43 -07001060 wt_prefix=
Ramsay Jones64394e32012-04-17 19:00:58 +01001061 cd "$sm_path" &&
Jens Lehmanne15bec02012-10-28 22:37:16 +01001062 eval cmd_status
Johan Herland64b19ff2009-08-19 03:45:24 +02001063 ) ||
Ramsay Jones64394e32012-04-17 19:00:58 +01001064 die "$(eval_gettext "Failed to recurse into submodule path '\$sm_path'")"
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001065 fi
1066 done
1067}
David Aguilar2327f612008-08-24 12:43:37 -07001068#
1069# Sync remote urls for submodules
1070# This makes the value for remote.$remote.url match the value
1071# specified in .gitmodules.
1072#
1073cmd_sync()
1074{
1075 while test $# -ne 0
1076 do
1077 case "$1" in
1078 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -07001079 GIT_QUIET=1
David Aguilar2327f612008-08-24 12:43:37 -07001080 shift
1081 ;;
Phil Hord82f49f22012-10-26 15:44:42 -04001082 --recursive)
1083 recursive=1
1084 shift
1085 ;;
David Aguilar2327f612008-08-24 12:43:37 -07001086 --)
1087 shift
1088 break
1089 ;;
1090 -*)
1091 usage
1092 ;;
1093 *)
1094 break
1095 ;;
1096 esac
1097 done
1098 cd_to_toplevel
Stefan Bellerb0f4b402016-05-31 16:59:33 -07001099 {
1100 git submodule--helper list --prefix "$wt_prefix" "$@" ||
Johannes Sixtc4c02bf2016-07-22 21:14:38 +02001101 echo "#unmatched" $?
Stefan Bellerb0f4b402016-05-31 16:59:33 -07001102 } |
Brandon Williamscf9e55f2017-04-07 10:23:06 -07001103 while read -r mode sha1 stage sm_path
David Aguilar2327f612008-08-24 12:43:37 -07001104 do
Johannes Sixtc4c02bf2016-07-22 21:14:38 +02001105 die_if_unmatched "$mode" "$sha1"
Brandon Williamse7849a92017-03-16 15:29:45 -07001106
1107 # skip inactive submodules
Brandon Williams25b31f12017-03-16 15:29:46 -07001108 if ! git submodule--helper is-active "$sm_path"
Brandon Williamse7849a92017-03-16 15:29:45 -07001109 then
1110 continue
1111 fi
1112
Brandon Williams25b31f12017-03-16 15:29:46 -07001113 name=$(git submodule--helper name "$sm_path")
David Aguilar2327f612008-08-24 12:43:37 -07001114 url=$(git config -f .gitmodules --get submodule."$name".url)
Johan Herlandbaede9f2008-09-22 18:08:31 +02001115
1116 # Possibly a url relative to parent
1117 case "$url" in
1118 ./*|../*)
Jon Seymour967b2c62012-06-06 21:57:29 +10001119 # rewrite foo/bar as ../.. to find path from
1120 # submodule work tree to superproject work tree
Junio C Hamano6a066232014-06-10 08:33:39 -07001121 up_path="$(printf '%s\n' "$sm_path" | sed "s/[^/][^/]*/../g")" &&
Jon Seymour967b2c62012-06-06 21:57:29 +10001122 # guarantee a trailing /
1123 up_path=${up_path%/}/ &&
1124 # path from submodule work tree to submodule origin repo
Stefan Beller63e95be2016-04-15 17:50:12 -07001125 sub_origin_url=$(git submodule--helper resolve-relative-url "$url" "$up_path") &&
Jon Seymour967b2c62012-06-06 21:57:29 +10001126 # path from superproject work tree to submodule origin repo
Stefan Beller63e95be2016-04-15 17:50:12 -07001127 super_config_url=$(git submodule--helper resolve-relative-url "$url") || exit
Jon Seymour967b2c62012-06-06 21:57:29 +10001128 ;;
1129 *)
1130 sub_origin_url="$url"
1131 super_config_url="$url"
Johan Herlandbaede9f2008-09-22 18:08:31 +02001132 ;;
1133 esac
1134
Brandon Williamse7849a92017-03-16 15:29:45 -07001135 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
1136 say "$(eval_gettext "Synchronizing submodule url for '\$displaypath'")"
1137 git config submodule."$name".url "$super_config_url"
1138
1139 if test -e "$sm_path"/.git
David Aguilar2327f612008-08-24 12:43:37 -07001140 then
Brandon Williamse7849a92017-03-16 15:29:45 -07001141 (
1142 sanitize_submodule_env
1143 cd "$sm_path"
1144 remote=$(get_default_remote)
1145 git config remote."$remote".url "$sub_origin_url"
Junio C Hamanoccee6082011-06-25 22:41:25 +02001146
Brandon Williamse7849a92017-03-16 15:29:45 -07001147 if test -n "$recursive"
Junio C Hamanoccee6082011-06-25 22:41:25 +02001148 then
Brandon Williamse7849a92017-03-16 15:29:45 -07001149 prefix="$prefix$sm_path/"
1150 eval cmd_sync
Junio C Hamanoccee6082011-06-25 22:41:25 +02001151 fi
Brandon Williamse7849a92017-03-16 15:29:45 -07001152 )
David Aguilar2327f612008-08-24 12:43:37 -07001153 fi
1154 done
1155}
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001156
Stefan Bellerf6f85862016-12-12 11:04:35 -08001157cmd_absorbgitdirs()
1158{
1159 git submodule--helper absorb-git-dirs --prefix "$wt_prefix" "$@"
1160}
1161
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001162# This loop parses the command line arguments to find the
1163# subcommand name to dispatch. Parsing of the subcommand specific
1164# options are primarily done by the subcommand implementations.
1165# Subcommand specific options such as --branch and --cached are
1166# parsed here as well, for backward compatibility.
1167
1168while test $# != 0 && test -z "$command"
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001169do
1170 case "$1" in
Stefan Bellerf6f85862016-12-12 11:04:35 -08001171 add | foreach | init | deinit | update | status | summary | sync | absorbgitdirs)
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001172 command=$1
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001173 ;;
1174 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -07001175 GIT_QUIET=1
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001176 ;;
Sven Verdoolaegeecda0722007-06-24 23:06:07 +02001177 -b|--branch)
1178 case "$2" in
1179 '')
1180 usage
1181 ;;
1182 esac
1183 branch="$2"; shift
1184 ;;
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001185 --cached)
Ping Yin28f9af52008-03-11 21:52:15 +08001186 cached="$1"
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001187 ;;
1188 --)
1189 break
1190 ;;
1191 -*)
1192 usage
1193 ;;
1194 *)
1195 break
1196 ;;
1197 esac
1198 shift
1199done
1200
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001201# No command word defaults to "status"
Ramkumar Ramachandraaf9c9f92012-09-22 16:57:59 +05301202if test -z "$command"
1203then
1204 if test $# = 0
1205 then
1206 command=status
1207 else
1208 usage
1209 fi
1210fi
Sven Verdoolaegeecda0722007-06-24 23:06:07 +02001211
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001212# "-b branch" is accepted only by "add"
1213if test -n "$branch" && test "$command" != add
1214then
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001215 usage
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001216fi
1217
Ping Yin28f9af52008-03-11 21:52:15 +08001218# "--cached" is accepted only by "status" and "summary"
Elia Pinto496eeeb2014-06-10 05:28:33 -07001219if test -n "$cached" && test "$command" != status && test "$command" != summary
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001220then
1221 usage
1222fi
1223
1224"cmd_$command" "$@"