blob: 514ede259660d387fd392b950facc706f7c4d5e7 [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/-/ /')
Denton Liu68cabbf2019-02-15 01:26:41 -08008USAGE="[--quiet] [--cached]
9 or: $dashless [--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
Johan Herland64b19ff2009-08-19 03:45:24 +020010 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
Johan Herland1d5bec82009-08-19 03:45:19 +020011 or: $dashless [--quiet] init [--] [<path>...]
Stefan Bellerf6a52792016-05-05 12:52:32 -070012 or: $dashless [--quiet] deinit [-f|--force] (--all| [--] <path>...)
Stefan Bellerabed0002016-05-26 14:59:43 -070013 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 -070014 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
Johan Herland15fc56a2009-08-19 03:45:22 +020015 or: $dashless [--quiet] foreach [--recursive] <command>
Stefan Bellerc32eaa82017-01-11 12:59:17 -080016 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]
17 or: $dashless [--quiet] absorbgitdirs [--] [<path>...]"
Junio C Hamano8f321a32007-11-06 01:50:02 -080018OPTIONS_SPEC=
John Keeping091a6eb2013-06-16 15:18:18 +010019SUBDIRECTORY_OK=Yes
Lars Hjemli70c7ac22007-05-26 15:56:40 +020020. git-sh-setup
Mark Levedahl98fcf842008-08-24 14:46:10 -040021. git-parse-remote
Lars Hjemli70c7ac22007-05-26 15:56:40 +020022require_work_tree
John Keeping091a6eb2013-06-16 15:18:18 +010023wt_prefix=$(git rev-parse --show-prefix)
24cd_to_toplevel
Lars Hjemli70c7ac22007-05-26 15:56:40 +020025
Brandon Williamsf1762d72016-12-14 14:39:52 -080026# Tell the rest of git that any URLs we get don't come
27# directly from the user, so it can apply policy as appropriate.
28GIT_PROTOCOL_FROM_USER=0
29export GIT_PROTOCOL_FROM_USER
Jeff King33cfccb2015-09-16 13:13:12 -040030
Junio C Hamano5c08dbb2008-01-15 02:48:45 -080031command=
Sven Verdoolaegeecda0722007-06-24 23:06:07 +020032branch=
Jens Lehmannd27b8762010-07-17 17:11:43 +020033force=
Michael S. Tsirkind92a3952009-05-04 22:30:01 +030034reference=
Lars Hjemli70c7ac22007-05-26 15:56:40 +020035cached=
Gerrit Pape48bb3032010-04-26 11:50:39 +020036recursive=
37init=
Jens Lehmann1c244f62009-08-13 21:32:50 +020038files=
W. Trevor King06b1abb2012-12-19 11:03:32 -050039remote=
Fabian Franz31ca3ac2009-02-05 20:18:32 -020040nofetch=
Johan Herland32948422009-06-03 08:27:06 +020041update=
Johan Herland15fc56a2009-08-19 03:45:22 +020042prefix=
Jens Lehmann73b08982012-09-30 01:05:58 +020043custom_name=
Fredrik Gustafsson275cd182013-07-02 23:42:56 +020044depth=
Jeff King72c5f882016-09-22 01:24:46 -040045progress=
Casey Fitzpatricka0ef2932018-05-03 06:53:46 -040046dissociate=
Lars Hjemli70c7ac22007-05-26 15:56:40 +020047
Heiko Voigtbe9d0a32012-08-14 22:35:27 +020048die_if_unmatched ()
49{
50 if test "$1" = "#unmatched"
51 then
Johannes Sixtc4c02bf2016-07-22 21:14:38 +020052 exit ${2:-1}
Heiko Voigtbe9d0a32012-08-14 22:35:27 +020053 fi
54}
55
David Aguilara7b32692008-08-22 00:30:50 -070056#
W. Trevor King88ce00c2012-12-11 13:58:15 -050057# Print a submodule configuration setting
58#
59# $1 = submodule name
60# $2 = option name
61# $3 = default value
62#
63# Checks in the usual git-config places first (for overrides),
64# otherwise it falls back on .gitmodules. This allows you to
65# distribute project-wide defaults in .gitmodules, while still
66# customizing individual repositories if necessary. If the option is
67# not in .gitmodules either, print a default value.
68#
69get_submodule_config () {
70 name="$1"
71 option="$2"
72 default="$3"
73 value=$(git config submodule."$name"."$option")
74 if test -z "$value"
75 then
Antonio Ospiteb2faad42018-10-05 15:05:57 +020076 value=$(git submodule--helper config submodule."$name"."$option")
W. Trevor King88ce00c2012-12-11 13:58:15 -050077 fi
78 printf '%s' "${value:-$default}"
79}
80
René Scharfe862ae6c2013-04-01 15:06:27 +020081isnumber()
82{
83 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
84}
85
brian m. carlsondda63462018-10-15 00:02:01 +000086# Given a full hex object ID, is this the zero OID?
87is_zero_oid () {
88 echo "$1" | sane_egrep '^0+$' >/dev/null 2>&1
89}
90
Jacob Keller14111fc2016-02-29 14:58:35 -080091# Sanitize the local git environment for use within a submodule. We
92# can't simply use clear_local_git_env since we want to preserve some
93# of the settings from GIT_CONFIG_PARAMETERS.
94sanitize_submodule_env()
95{
Jeff King89044ba2016-05-04 21:22:19 -040096 save_config=$GIT_CONFIG_PARAMETERS
Jacob Keller14111fc2016-02-29 14:58:35 -080097 clear_local_git_env
Jeff King89044ba2016-05-04 21:22:19 -040098 GIT_CONFIG_PARAMETERS=$save_config
Jeff King860cba62016-04-28 09:37:44 -040099 export GIT_CONFIG_PARAMETERS
Jacob Keller14111fc2016-02-29 14:58:35 -0800100}
101
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200102#
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200103# Add a new submodule to the working tree, .gitmodules and the index
104#
Mark Levedahlec05df32008-07-09 21:05:40 -0400105# $@ = repo path
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200106#
107# optional branch is stored in global branch variable
108#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800109cmd_add()
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200110{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800111 # parse $args after "submodule ... add".
John Keeping091a6eb2013-06-16 15:18:18 +0100112 reference_path=
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800113 while test $# -ne 0
114 do
115 case "$1" in
116 -b | --branch)
117 case "$2" in '') usage ;; esac
118 branch=$2
119 shift
120 ;;
Jens Lehmannd27b8762010-07-17 17:11:43 +0200121 -f | --force)
122 force=$1
123 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800124 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700125 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800126 ;;
Casey Fitzpatrick6d33e1c2018-05-03 06:53:45 -0400127 --progress)
128 progress=1
129 ;;
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300130 --reference)
131 case "$2" in '') usage ;; esac
John Keeping091a6eb2013-06-16 15:18:18 +0100132 reference_path=$2
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300133 shift
134 ;;
135 --reference=*)
John Keeping091a6eb2013-06-16 15:18:18 +0100136 reference_path="${1#--reference=}"
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300137 ;;
Casey Fitzpatricka0ef2932018-05-03 06:53:46 -0400138 --dissociate)
139 dissociate=1
140 ;;
Jens Lehmann73b08982012-09-30 01:05:58 +0200141 --name)
142 case "$2" in '') usage ;; esac
143 custom_name=$2
144 shift
145 ;;
Fredrik Gustafsson275cd182013-07-02 23:42:56 +0200146 --depth)
147 case "$2" in '') usage ;; esac
148 depth="--depth=$2"
149 shift
150 ;;
151 --depth=*)
152 depth=$1
153 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800154 --)
155 shift
156 break
157 ;;
158 -*)
159 usage
160 ;;
161 *)
162 break
163 ;;
164 esac
165 shift
166 done
167
Antonio Ospite76e9bdc2018-10-25 18:18:12 +0200168 if ! git submodule--helper config --check-writeable >/dev/null 2>&1
169 then
170 die "$(eval_gettext "please make sure that the .gitmodules file is in the working tree")"
171 fi
172
John Keeping091a6eb2013-06-16 15:18:18 +0100173 if test -n "$reference_path"
174 then
175 is_absolute_path "$reference_path" ||
176 reference_path="$wt_prefix$reference_path"
177
178 reference="--reference=$reference_path"
179 fi
180
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200181 repo=$1
Ramsay Jones64394e32012-04-17 19:00:58 +0100182 sm_path=$2
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200183
Ramsay Jones64394e32012-04-17 19:00:58 +0100184 if test -z "$sm_path"; then
Junio C Hamano6a066232014-06-10 08:33:39 -0700185 sm_path=$(printf '%s\n' "$repo" |
Jens Lehmann1414e572009-09-22 17:10:12 +0200186 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
187 fi
188
Elia Pinto496eeeb2014-06-10 05:28:33 -0700189 if test -z "$repo" || test -z "$sm_path"; then
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200190 usage
191 fi
192
John Keeping091a6eb2013-06-16 15:18:18 +0100193 is_absolute_path "$sm_path" || sm_path="$wt_prefix$sm_path"
194
Mark Levedahlec05df32008-07-09 21:05:40 -0400195 # assure repo is absolute or relative to parent
196 case "$repo" in
197 ./*|../*)
John Keeping091a6eb2013-06-16 15:18:18 +0100198 test -z "$wt_prefix" ||
199 die "$(gettext "Relative path can only be used from the toplevel of the working tree")"
200
Mark Levedahlec05df32008-07-09 21:05:40 -0400201 # dereference source url relative to parent's url
Stefan Beller63e95be2016-04-15 17:50:12 -0700202 realrepo=$(git submodule--helper resolve-relative-url "$repo") || exit
Mark Levedahlec05df32008-07-09 21:05:40 -0400203 ;;
204 *:*|/*)
205 # absolute url
206 realrepo=$repo
207 ;;
208 *)
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000209 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
Mark Levedahlec05df32008-07-09 21:05:40 -0400210 ;;
211 esac
212
Michael J Gruberdb75ada2009-03-03 16:08:21 +0100213 # normalize path:
214 # multiple //; leading ./; /./; /../; trailing /
Ramsay Jones64394e32012-04-17 19:00:58 +0100215 sm_path=$(printf '%s/\n' "$sm_path" |
Michael J Gruberdb75ada2009-03-03 16:08:21 +0100216 sed -e '
217 s|//*|/|g
218 s|^\(\./\)*||
Patrick Steinhardt8196e722015-01-30 16:14:03 +0100219 s|/\(\./\)*|/|g
Michael J Gruberdb75ada2009-03-03 16:08:21 +0100220 :start
221 s|\([^/]*\)/\.\./||
222 tstart
223 s|/*$||
224 ')
Stefan Beller619acfc2016-10-06 12:37:24 -0700225 if test -z "$force"
226 then
227 git ls-files --error-unmatch "$sm_path" > /dev/null 2>&1 &&
228 die "$(eval_gettext "'\$sm_path' already exists in the index")"
229 else
230 git ls-files -s "$sm_path" | sane_grep -v "^160000" > /dev/null 2>&1 &&
231 die "$(eval_gettext "'\$sm_path' already exists in the index and is not a submodule")"
232 fi
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200233
Jeff King53213992017-06-14 06:58:22 -0400234 if test -z "$force" &&
235 ! git add --dry-run --ignore-missing --no-warn-embedded-repo "$sm_path" > /dev/null 2>&1
Jens Lehmannd27b8762010-07-17 17:11:43 +0200236 then
Jon Seymour6ff875c2011-08-07 21:58:17 +1000237 eval_gettextln "The following path is ignored by one of your .gitignore files:
Ramsay Jones64394e32012-04-17 19:00:58 +0100238\$sm_path
Jon Seymour6ff875c2011-08-07 21:58:17 +1000239Use -f if you really want to add it." >&2
Jens Lehmannd27b8762010-07-17 17:11:43 +0200240 exit 1
241 fi
242
Jens Lehmann73b08982012-09-30 01:05:58 +0200243 if test -n "$custom_name"
244 then
245 sm_name="$custom_name"
246 else
247 sm_name="$sm_path"
248 fi
249
Jeff King0383bbb2018-04-30 03:25:25 -0400250 if ! git submodule--helper check-name "$sm_name"
251 then
252 die "$(eval_gettext "'$sm_name' is not a valid submodule name")"
253 fi
254
Mark Levedahld4264ca2008-03-04 20:15:02 -0500255 # perhaps the path exists and is already a git repo, else clone it
Ramsay Jones64394e32012-04-17 19:00:58 +0100256 if test -e "$sm_path"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500257 then
Elia Pinto496eeeb2014-06-10 05:28:33 -0700258 if test -d "$sm_path"/.git || test -f "$sm_path"/.git
Mark Levedahld4264ca2008-03-04 20:15:02 -0500259 then
Ramsay Jones64394e32012-04-17 19:00:58 +0100260 eval_gettextln "Adding existing repo at '\$sm_path' to the index"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500261 else
Ramsay Jones64394e32012-04-17 19:00:58 +0100262 die "$(eval_gettext "'\$sm_path' already exists and is not a valid git repo")"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500263 fi
Mark Levedahlc2f93912008-07-09 21:05:41 -0400264
Mark Levedahld4264ca2008-03-04 20:15:02 -0500265 else
Jens Lehmann4b7c2862012-09-30 23:01:29 +0200266 if test -d ".git/modules/$sm_name"
267 then
268 if test -z "$force"
269 then
Vasco Almeida0d71dbf2016-06-17 21:54:12 +0000270 eval_gettextln >&2 "A git directory for '\$sm_name' is found locally with remote(s):"
Jens Lehmann4b7c2862012-09-30 23:01:29 +0200271 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 +0000272 die "$(eval_gettextln "\
273If you want to reuse this local git directory instead of cloning again from
274 \$realrepo
275use the '--force' option. If the local git directory is not the correct repo
276or you are unsure what this means choose another name with the '--name' option.")"
Jens Lehmann4b7c2862012-09-30 23:01:29 +0200277 else
Vasco Almeida0d71dbf2016-06-17 21:54:12 +0000278 eval_gettextln "Reactivating local git directory for submodule '\$sm_name'."
Jens Lehmann4b7c2862012-09-30 23:01:29 +0200279 fi
280 fi
Casey Fitzpatricka0ef2932018-05-03 06:53:46 -0400281 git submodule--helper clone ${GIT_QUIET:+--quiet} ${progress:+"--progress"} --prefix "$wt_prefix" --path "$sm_path" --name "$sm_name" --url "$realrepo" ${reference:+"$reference"} ${dissociate:+"--dissociate"} ${depth:+"$depth"} || exit
Junio C Hamanod851ffb2014-04-02 14:15:36 -0700282 (
Jacob Keller14111fc2016-02-29 14:58:35 -0800283 sanitize_submodule_env
Junio C Hamanod851ffb2014-04-02 14:15:36 -0700284 cd "$sm_path" &&
285 # ash fails to wordsplit ${branch:+-b "$branch"...}
286 case "$branch" in
287 '') git checkout -f -q ;;
288 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
289 esac
290 ) || die "$(eval_gettext "Unable to checkout submodule '\$sm_path'")"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500291 fi
Jens Lehmann73b08982012-09-30 01:05:58 +0200292 git config submodule."$sm_name".url "$realrepo"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500293
Jeff King53213992017-06-14 06:58:22 -0400294 git add --no-warn-embedded-repo $force "$sm_path" ||
Ramsay Jones64394e32012-04-17 19:00:58 +0100295 die "$(eval_gettext "Failed to add submodule '\$sm_path'")"
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200296
Antonio Ospiteb2faad42018-10-05 15:05:57 +0200297 git submodule--helper config submodule."$sm_name".path "$sm_path" &&
298 git submodule--helper config submodule."$sm_name".url "$repo" &&
W. Trevor Kingb9289222012-12-19 11:03:33 -0500299 if test -n "$branch"
300 then
Antonio Ospiteb2faad42018-10-05 15:05:57 +0200301 git submodule--helper config submodule."$sm_name".branch "$branch"
W. Trevor Kingb9289222012-12-19 11:03:33 -0500302 fi &&
Ævar Arnfjörð Bjarmason31991b02010-07-05 17:33:03 +0000303 git add --force .gitmodules ||
Ramsay Jones64394e32012-04-17 19:00:58 +0100304 die "$(eval_gettext "Failed to register submodule '\$sm_path'")"
Brandon Williams1b614c02017-03-17 15:38:05 -0700305
306 # NEEDSWORK: In a multi-working-tree world, this needs to be
307 # set in the per-worktree config.
308 if git config --get submodule.active >/dev/null
309 then
310 # If the submodule being adding isn't already covered by the
311 # current configured pathspec, set the submodule's active flag
312 if ! git submodule--helper is-active "$sm_path"
313 then
314 git config submodule."$sm_name".active "true"
315 fi
316 else
317 git config submodule."$sm_name".active "true"
318 fi
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200319}
320
321#
Mark Levedahl19a31f92008-08-10 19:10:04 -0400322# Execute an arbitrary command sequence in each checked out
323# submodule
324#
325# $@ = command to execute
326#
327cmd_foreach()
328{
Johan Herland1d5bec82009-08-19 03:45:19 +0200329 # parse $args after "submodule ... foreach".
330 while test $# -ne 0
331 do
332 case "$1" in
333 -q|--quiet)
334 GIT_QUIET=1
335 ;;
Johan Herland15fc56a2009-08-19 03:45:22 +0200336 --recursive)
337 recursive=1
338 ;;
Johan Herland1d5bec82009-08-19 03:45:19 +0200339 -*)
340 usage
341 ;;
342 *)
343 break
344 ;;
345 esac
346 shift
347 done
348
Prathamesh Chavanfc1b9242018-05-10 14:25:01 -0700349 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper foreach ${GIT_QUIET:+--quiet} ${recursive:+--recursive} "$@"
Mark Levedahl19a31f92008-08-10 19:10:04 -0400350}
351
352#
Lars Hjemli211b7f12007-06-06 11:13:02 +0200353# Register submodules in .git/config
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200354#
355# $@ = requested paths (default to all)
356#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800357cmd_init()
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200358{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800359 # parse $args after "submodule ... init".
360 while test $# -ne 0
361 do
362 case "$1" in
363 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700364 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800365 ;;
366 --)
367 shift
368 break
369 ;;
370 -*)
371 usage
372 ;;
373 *)
374 break
375 ;;
376 esac
377 shift
378 done
379
Stefan Beller6e7c14e2017-01-06 16:19:53 -0800380 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet} "$@"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200381}
382
383#
Jens Lehmanncf419822013-03-04 22:20:24 +0100384# Unregister submodules from .git/config and remove their work tree
385#
Jens Lehmanncf419822013-03-04 22:20:24 +0100386cmd_deinit()
387{
388 # parse $args after "submodule ... deinit".
Stefan Bellerf6a52792016-05-05 12:52:32 -0700389 deinit_all=
Jens Lehmanncf419822013-03-04 22:20:24 +0100390 while test $# -ne 0
391 do
392 case "$1" in
393 -f|--force)
394 force=$1
395 ;;
396 -q|--quiet)
397 GIT_QUIET=1
398 ;;
Stefan Bellerf6a52792016-05-05 12:52:32 -0700399 --all)
400 deinit_all=t
401 ;;
Jens Lehmanncf419822013-03-04 22:20:24 +0100402 --)
403 shift
404 break
405 ;;
406 -*)
407 usage
408 ;;
409 *)
410 break
411 ;;
412 esac
413 shift
414 done
415
Prathamesh Chavan2e612732018-01-15 02:45:29 +0530416 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${prefix:+--prefix "$prefix"} ${force:+--force} ${deinit_all:+--all} "$@"
Jens Lehmanncf419822013-03-04 22:20:24 +0100417}
418
Stefan Bellerfb43e312016-02-23 19:32:13 -0800419is_tip_reachable () (
Junio C Hamano01e1d542016-04-06 11:39:12 -0700420 sanitize_submodule_env &&
Stefan Bellerfb43e312016-02-23 19:32:13 -0800421 cd "$1" &&
422 rev=$(git rev-list -n 1 "$2" --not --all 2>/dev/null) &&
423 test -z "$rev"
424)
425
426fetch_in_submodule () (
Junio C Hamano01e1d542016-04-06 11:39:12 -0700427 sanitize_submodule_env &&
Stefan Bellerfb43e312016-02-23 19:32:13 -0800428 cd "$1" &&
429 case "$2" in
430 '')
431 git fetch ;;
432 *)
Stefan Beller6cbf4542016-07-28 17:44:04 -0700433 shift
434 git fetch $(get_default_remote) "$@" ;;
Stefan Bellerfb43e312016-02-23 19:32:13 -0800435 esac
436)
437
Jens Lehmanncf419822013-03-04 22:20:24 +0100438#
Lars Hjemli211b7f12007-06-06 11:13:02 +0200439# Update each submodule path to correct revision, using clone and checkout as needed
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200440#
441# $@ = requested paths (default to all)
442#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800443cmd_update()
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200444{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800445 # parse $args after "submodule ... update".
446 while test $# -ne 0
447 do
448 case "$1" in
449 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700450 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800451 ;;
Stefan Bellere84c3cf2018-08-14 11:22:02 -0700452 -v)
453 GIT_QUIET=0
454 ;;
Jeff King72c5f882016-09-22 01:24:46 -0400455 --progress)
Casey Fitzpatrickc7199e32018-05-03 06:53:44 -0400456 progress=1
Jeff King72c5f882016-09-22 01:24:46 -0400457 ;;
Johannes Schindelinbe4d2c82008-05-16 11:23:03 +0100458 -i|--init)
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300459 init=1
Johannes Schindelinbe4d2c82008-05-16 11:23:03 +0100460 ;;
W. Trevor King06b1abb2012-12-19 11:03:32 -0500461 --remote)
462 remote=1
463 ;;
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200464 -N|--no-fetch)
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200465 nofetch=1
466 ;;
Nicolas Morey-Chaisemartin9db31bd2011-04-01 11:42:03 +0200467 -f|--force)
468 force=$1
469 ;;
Peter Huttererca2cedb2009-04-24 09:06:38 +1000470 -r|--rebase)
Johan Herland32948422009-06-03 08:27:06 +0200471 update="rebase"
Peter Huttererca2cedb2009-04-24 09:06:38 +1000472 ;;
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300473 --reference)
474 case "$2" in '') usage ;; esac
475 reference="--reference=$2"
Kevin Ballard98dbe632010-11-02 23:26:25 -0700476 shift
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300477 ;;
478 --reference=*)
479 reference="$1"
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300480 ;;
Casey Fitzpatricka0ef2932018-05-03 06:53:46 -0400481 --dissociate)
482 dissociate=1
483 ;;
Johan Herland42b49172009-06-03 00:59:12 +0200484 -m|--merge)
Johan Herland42b49172009-06-03 00:59:12 +0200485 update="merge"
486 ;;
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200487 --recursive)
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200488 recursive=1
489 ;;
Junio C Hamanoefc5fb62011-10-10 15:56:16 -0700490 --checkout)
491 update="checkout"
492 ;;
Stefan Bellerabed0002016-05-26 14:59:43 -0700493 --recommend-shallow)
494 recommend_shallow="--recommend-shallow"
495 ;;
496 --no-recommend-shallow)
497 recommend_shallow="--no-recommend-shallow"
498 ;;
Fredrik Gustafsson275cd182013-07-02 23:42:56 +0200499 --depth)
500 case "$2" in '') usage ;; esac
501 depth="--depth=$2"
502 shift
503 ;;
504 --depth=*)
505 depth=$1
506 ;;
Stefan Beller2335b872016-02-29 18:07:19 -0800507 -j|--jobs)
508 case "$2" in '') usage ;; esac
509 jobs="--jobs=$2"
510 shift
511 ;;
512 --jobs=*)
513 jobs=$1
514 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800515 --)
516 shift
517 break
518 ;;
519 -*)
520 usage
521 ;;
522 *)
523 break
524 ;;
525 esac
Kevin Ballard98dbe632010-11-02 23:26:25 -0700526 shift
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800527 done
528
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300529 if test -n "$init"
530 then
531 cmd_init "--" "$@" || return
532 fi
533
Stefan Beller48308682016-02-29 18:07:17 -0800534 {
535 git submodule--helper update-clone ${GIT_QUIET:+--quiet} \
Casey Fitzpatrickc7199e32018-05-03 06:53:44 -0400536 ${progress:+"--progress"} \
Stefan Beller48308682016-02-29 18:07:17 -0800537 ${wt_prefix:+--prefix "$wt_prefix"} \
538 ${prefix:+--recursive-prefix "$prefix"} \
539 ${update:+--update "$update"} \
Stefan Beller5f50f332016-08-11 16:14:01 -0700540 ${reference:+"$reference"} \
Casey Fitzpatricka0ef2932018-05-03 06:53:46 -0400541 ${dissociate:+"--dissociate"} \
Stefan Beller48308682016-02-29 18:07:17 -0800542 ${depth:+--depth "$depth"} \
Casey Fitzpatrickc7199e32018-05-03 06:53:44 -0400543 $recommend_shallow \
544 $jobs \
Johannes Sixtc4c02bf2016-07-22 21:14:38 +0200545 "$@" || echo "#unmatched" $?
Stefan Beller48308682016-02-29 18:07:17 -0800546 } | {
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200547 err=
Stefan Beller9eca7012018-08-03 15:23:17 -0700548 while read -r quickabort sha1 just_cloned sm_path
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200549 do
Stefan Beller9eca7012018-08-03 15:23:17 -0700550 die_if_unmatched "$quickabort" "$sha1"
Stefan Beller48308682016-02-29 18:07:17 -0800551
Stefan Beller5d124f42019-01-18 13:55:19 -0800552 git submodule--helper ensure-core-worktree "$sm_path" || exit 1
Stefan Beller74d47312018-08-13 15:42:34 -0700553
Stefan Belleree69b2a2018-08-13 15:42:35 -0700554 update_module=$(git submodule--helper update-module-mode $just_cloned "$sm_path" $update)
Junio C Hamanoefc5fb62011-10-10 15:56:16 -0700555
Stefan Beller44431df2016-05-31 17:27:59 -0700556 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
John Keeping091a6eb2013-06-16 15:18:18 +0100557
Stefan Beller48308682016-02-29 18:07:17 -0800558 if test $just_cloned -eq 1
Junio C Hamanod851ffb2014-04-02 14:15:36 -0700559 then
Lars Hjemlibf2d8242007-06-11 21:12:22 +0200560 subsha1=
561 else
Jacob Keller14111fc2016-02-29 14:58:35 -0800562 subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700563 git rev-parse --verify HEAD) ||
John Keeping091a6eb2013-06-16 15:18:18 +0100564 die "$(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
Lars Hjemli211b7f12007-06-06 11:13:02 +0200565 fi
566
W. Trevor King06b1abb2012-12-19 11:03:32 -0500567 if test -n "$remote"
568 then
Stefan Beller92bbe7c2016-08-03 13:44:03 -0700569 branch=$(git submodule--helper remote-branch "$sm_path")
W. Trevor King06b1abb2012-12-19 11:03:32 -0500570 if test -z "$nofetch"
571 then
572 # Fetch remote before determining tracking $sha1
Stefan Beller6cbf4542016-07-28 17:44:04 -0700573 fetch_in_submodule "$sm_path" $depth ||
W. Trevor King06b1abb2012-12-19 11:03:32 -0500574 die "$(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
575 fi
Jacob Keller14111fc2016-02-29 14:58:35 -0800576 remote_name=$(sanitize_submodule_env; cd "$sm_path" && get_default_remote)
577 sha1=$(sanitize_submodule_env; cd "$sm_path" &&
W. Trevor King06b1abb2012-12-19 11:03:32 -0500578 git rev-parse --verify "${remote_name}/${branch}") ||
Vasco Almeidac87302b2016-06-17 21:54:13 +0000579 die "$(eval_gettext "Unable to find current \${remote_name}/\${branch} revision in submodule path '\$sm_path'")"
W. Trevor King06b1abb2012-12-19 11:03:32 -0500580 fi
581
Elia Pinto496eeeb2014-06-10 05:28:33 -0700582 if test "$subsha1" != "$sha1" || test -n "$force"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200583 then
Nicolas Morey-Chaisemartin9db31bd2011-04-01 11:42:03 +0200584 subforce=$force
585 # If we don't already have a -f flag and the submodule has never been checked out
Elia Pinto496eeeb2014-06-10 05:28:33 -0700586 if test -z "$subsha1" && test -z "$force"
Ping Yinb9b378a2008-09-26 23:33:23 +0800587 then
Nicolas Morey-Chaisemartin9db31bd2011-04-01 11:42:03 +0200588 subforce="-f"
Ping Yinb9b378a2008-09-26 23:33:23 +0800589 fi
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200590
591 if test -z "$nofetch"
592 then
Jens Lehmanne5f522d2011-03-06 23:13:36 +0100593 # Run fetch only if $sha1 isn't present or it
594 # is not reachable from a ref.
Stefan Bellerfb43e312016-02-23 19:32:13 -0800595 is_tip_reachable "$sm_path" "$sha1" ||
Stefan Beller6cbf4542016-07-28 17:44:04 -0700596 fetch_in_submodule "$sm_path" $depth ||
Stefan Bellere30d8332018-05-15 12:40:54 -0700597 say "$(eval_gettext "Unable to fetch in submodule path '\$displaypath'")"
Stefan Bellerfb43e312016-02-23 19:32:13 -0800598
599 # Now we tried the usual fetch, but $sha1 may
600 # not be reachable from any of the refs
601 is_tip_reachable "$sm_path" "$sha1" ||
Stefan Beller6cbf4542016-07-28 17:44:04 -0700602 fetch_in_submodule "$sm_path" $depth "$sha1" ||
Vasco Almeidac87302b2016-06-17 21:54:13 +0000603 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 -0200604 fi
605
Junio C Hamano877449c2011-06-13 12:17:52 -0700606 must_die_on_failure=
Johan Herland32948422009-06-03 08:27:06 +0200607 case "$update_module" in
W. Trevor Kinga2aed082014-01-15 20:10:22 -0800608 checkout)
609 command="git checkout $subforce -q"
610 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$displaypath'")"
611 say_msg="$(eval_gettext "Submodule path '\$displaypath': checked out '\$sha1'")"
612 ;;
Johan Herland32948422009-06-03 08:27:06 +0200613 rebase)
614 command="git rebase"
John Keeping091a6eb2013-06-16 15:18:18 +0100615 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$displaypath'")"
616 say_msg="$(eval_gettext "Submodule path '\$displaypath': rebased into '\$sha1'")"
Junio C Hamano877449c2011-06-13 12:17:52 -0700617 must_die_on_failure=yes
Johan Herland32948422009-06-03 08:27:06 +0200618 ;;
Johan Herland42b49172009-06-03 00:59:12 +0200619 merge)
620 command="git merge"
John Keeping091a6eb2013-06-16 15:18:18 +0100621 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$displaypath'")"
622 say_msg="$(eval_gettext "Submodule path '\$displaypath': merged in '\$sha1'")"
Junio C Hamano877449c2011-06-13 12:17:52 -0700623 must_die_on_failure=yes
Johan Herland42b49172009-06-03 00:59:12 +0200624 ;;
Chris Packham6cb57282013-07-03 21:02:02 +1200625 !*)
626 command="${update_module#!}"
Stefan Bellerb08238a2016-03-29 18:27:44 -0700627 die_msg="$(eval_gettext "Execution of '\$command \$sha1' failed in submodule path '\$displaypath'")"
628 say_msg="$(eval_gettext "Submodule path '\$displaypath': '\$command \$sha1'")"
Chris Packham6cb57282013-07-03 21:02:02 +1200629 must_die_on_failure=yes
630 ;;
Johan Herland32948422009-06-03 08:27:06 +0200631 *)
Stefan Bellerff03d932018-08-03 15:23:16 -0700632 die "$(eval_gettext "Invalid update mode '$update_module' for submodule path '$path'")"
Johan Herland32948422009-06-03 08:27:06 +0200633 esac
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200634
Jacob Keller14111fc2016-02-29 14:58:35 -0800635 if (sanitize_submodule_env; cd "$sm_path" && $command "$sha1")
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200636 then
Junio C Hamanoff968f02011-07-13 14:31:35 -0700637 say "$say_msg"
Junio C Hamano877449c2011-06-13 12:17:52 -0700638 elif test -n "$must_die_on_failure"
639 then
Junio C Hamanoff968f02011-07-13 14:31:35 -0700640 die_with_status 2 "$die_msg"
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200641 else
Junio C Hamanoff968f02011-07-13 14:31:35 -0700642 err="${err};$die_msg"
Junio C Hamano877449c2011-06-13 12:17:52 -0700643 continue
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200644 fi
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200645 fi
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200646
647 if test -n "$recursive"
648 then
William Entriken75bf5e62013-03-02 14:44:59 -0500649 (
Stefan Beller44431df2016-05-31 17:27:59 -0700650 prefix=$(git submodule--helper relative-path "$prefix$sm_path/" "$wt_prefix")
Stefan Beller36042422016-04-15 17:50:13 -0700651 wt_prefix=
Jacob Keller14111fc2016-02-29 14:58:35 -0800652 sanitize_submodule_env
William Entriken75bf5e62013-03-02 14:44:59 -0500653 cd "$sm_path" &&
Jens Lehmann36141282013-11-11 21:55:52 +0100654 eval cmd_update
William Entriken75bf5e62013-03-02 14:44:59 -0500655 )
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200656 res=$?
657 if test $res -gt 0
658 then
John Keeping091a6eb2013-06-16 15:18:18 +0100659 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
Stefan Bellerbb9d91b2016-06-09 12:06:37 -0700660 if test $res -ne 2
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200661 then
Junio C Hamanoff968f02011-07-13 14:31:35 -0700662 err="${err};$die_msg"
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200663 continue
664 else
Junio C Hamanoff968f02011-07-13 14:31:35 -0700665 die_with_status $res "$die_msg"
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200666 fi
667 fi
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200668 fi
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200669 done
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200670
671 if test -n "$err"
672 then
673 OIFS=$IFS
674 IFS=';'
675 for e in $err
676 do
677 if test -n "$e"
678 then
679 echo >&2 "$e"
680 fi
681 done
682 IFS=$OIFS
683 exit 1
684 fi
685 }
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200686}
687
Ping Yin28f9af52008-03-11 21:52:15 +0800688#
689# Show commit summary for submodules in index or working tree
690#
691# If '--cached' is given, show summary between index and given commit,
692# or between working tree and given commit
693#
694# $@ = [commit (default 'HEAD'),] requested paths (default all)
695#
696cmd_summary() {
Ping Yinf2dc06a2008-03-11 21:52:17 +0800697 summary_limit=-1
Ping Yind0f64dd2008-04-12 23:05:31 +0800698 for_status=
Jens Lehmann1c244f62009-08-13 21:32:50 +0200699 diff_cmd=diff-index
Emil Medvebffe71f2007-06-26 18:40:58 -0500700
Ping Yin28f9af52008-03-11 21:52:15 +0800701 # parse $args after "submodule ... summary".
702 while test $# -ne 0
703 do
704 case "$1" in
705 --cached)
706 cached="$1"
707 ;;
Jens Lehmann1c244f62009-08-13 21:32:50 +0200708 --files)
709 files="$1"
710 ;;
Ping Yind0f64dd2008-04-12 23:05:31 +0800711 --for-status)
712 for_status="$1"
713 ;;
Ping Yinf2dc06a2008-03-11 21:52:17 +0800714 -n|--summary-limit)
René Scharfe862ae6c2013-04-01 15:06:27 +0200715 summary_limit="$2"
716 isnumber "$summary_limit" || usage
Ping Yinf2dc06a2008-03-11 21:52:17 +0800717 shift
718 ;;
René Scharfe862ae6c2013-04-01 15:06:27 +0200719 --summary-limit=*)
720 summary_limit="${1#--summary-limit=}"
721 isnumber "$summary_limit" || usage
722 ;;
Ping Yin28f9af52008-03-11 21:52:15 +0800723 --)
724 shift
725 break
726 ;;
727 -*)
728 usage
729 ;;
730 *)
731 break
732 ;;
733 esac
734 shift
735 done
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200736
Ping Yinf2dc06a2008-03-11 21:52:17 +0800737 test $summary_limit = 0 && return
738
Johan Herland3deea892010-02-16 11:21:14 +0100739 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
Ping Yin28f9af52008-03-11 21:52:15 +0800740 then
741 head=$rev
Jeff Kingcaa9c3c2010-03-03 14:19:09 -0800742 test $# = 0 || shift
Elia Pinto496eeeb2014-06-10 05:28:33 -0700743 elif test -z "$1" || test "$1" = "HEAD"
Johan Herland3deea892010-02-16 11:21:14 +0100744 then
Junio C Hamano14e940d2010-03-03 14:19:10 -0800745 # before the first commit: compare with an empty tree
746 head=$(git hash-object -w -t tree --stdin </dev/null)
Jens Lehmann2ea6c2c2010-03-09 15:55:32 +0100747 test -z "$1" || shift
Ping Yin28f9af52008-03-11 21:52:15 +0800748 else
Johan Herland3deea892010-02-16 11:21:14 +0100749 head="HEAD"
Ping Yin28f9af52008-03-11 21:52:15 +0800750 fi
751
Jens Lehmann1c244f62009-08-13 21:32:50 +0200752 if [ -n "$files" ]
753 then
754 test -n "$cached" &&
Jiang Xin465d6a02012-07-25 22:53:09 +0800755 die "$(gettext "The --cached option cannot be used with the --files option")"
Jens Lehmann1c244f62009-08-13 21:32:50 +0200756 diff_cmd=diff-files
757 head=
758 fi
759
Ping Yin28f9af52008-03-11 21:52:15 +0800760 cd_to_toplevel
John Keeping091a6eb2013-06-16 15:18:18 +0100761 eval "set $(git rev-parse --sq --prefix "$wt_prefix" -- "$@")"
Ping Yin28f9af52008-03-11 21:52:15 +0800762 # Get modified modules cared by user
Jens Lehmann18076502010-06-25 16:56:02 +0200763 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
Junio C Hamanoe1622bf2009-11-23 15:56:32 -0800764 sane_egrep '^:([0-7]* )?160000' |
Brandon Williamscf9e55f2017-04-07 10:23:06 -0700765 while read -r mod_src mod_dst sha1_src sha1_dst status sm_path
Ping Yin28f9af52008-03-11 21:52:15 +0800766 do
767 # Always show modules deleted or type-changed (blob<->module)
Elia Pinto496eeeb2014-06-10 05:28:33 -0700768 if test "$status" = D || test "$status" = T
769 then
Junio C Hamano6a066232014-06-10 08:33:39 -0700770 printf '%s\n' "$sm_path"
Elia Pinto496eeeb2014-06-10 05:28:33 -0700771 continue
772 fi
Brian M. Carlson927b26f2013-09-01 20:06:49 +0000773 # Respect the ignore setting for --for-status.
774 if test -n "$for_status"
775 then
Stefan Beller0ea306e2015-09-02 14:42:25 -0700776 name=$(git submodule--helper name "$sm_path")
Brian M. Carlson927b26f2013-09-01 20:06:49 +0000777 ignore_config=$(get_submodule_config "$name" ignore none)
Elia Pinto496eeeb2014-06-10 05:28:33 -0700778 test $status != A && test $ignore_config = all && continue
Brian M. Carlson927b26f2013-09-01 20:06:49 +0000779 fi
Ping Yin28f9af52008-03-11 21:52:15 +0800780 # Also show added or modified modules which are checked out
Michael Forney974ce802017-08-04 23:49:05 -0700781 GIT_DIR="$sm_path/.git" git rev-parse --git-dir >/dev/null 2>&1 &&
Junio C Hamano6a066232014-06-10 08:33:39 -0700782 printf '%s\n' "$sm_path"
Ping Yin28f9af52008-03-11 21:52:15 +0800783 done
784 )
Ping Yin1cb639e2008-03-11 21:52:16 +0800785
Ping Yind0f64dd2008-04-12 23:05:31 +0800786 test -z "$modules" && return
787
Jens Lehmann18076502010-06-25 16:56:02 +0200788 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
Junio C Hamanoe1622bf2009-11-23 15:56:32 -0800789 sane_egrep '^:([0-7]* )?160000' |
Ping Yin1cb639e2008-03-11 21:52:16 +0800790 cut -c2- |
Brandon Williamscf9e55f2017-04-07 10:23:06 -0700791 while read -r mod_src mod_dst sha1_src sha1_dst status name
Ping Yin1cb639e2008-03-11 21:52:16 +0800792 do
793 if test -z "$cached" &&
brian m. carlsondda63462018-10-15 00:02:01 +0000794 is_zero_oid $sha1_dst
Ping Yin1cb639e2008-03-11 21:52:16 +0800795 then
796 case "$mod_dst" in
797 160000)
798 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
799 ;;
800 100644 | 100755 | 120000)
801 sha1_dst=$(git hash-object $name)
802 ;;
803 000000)
804 ;; # removed
805 *)
806 # unexpected type
Jon Seymour6ff875c2011-08-07 21:58:17 +1000807 eval_gettextln "unexpected mode \$mod_dst" >&2
Ping Yin1cb639e2008-03-11 21:52:16 +0800808 continue ;;
809 esac
810 fi
811 missing_src=
812 missing_dst=
813
814 test $mod_src = 160000 &&
Michael Forney974ce802017-08-04 23:49:05 -0700815 ! GIT_DIR="$name/.git" git rev-parse -q --verify $sha1_src^0 >/dev/null &&
Ping Yin1cb639e2008-03-11 21:52:16 +0800816 missing_src=t
817
818 test $mod_dst = 160000 &&
Michael Forney974ce802017-08-04 23:49:05 -0700819 ! GIT_DIR="$name/.git" git rev-parse -q --verify $sha1_dst^0 >/dev/null &&
Ping Yin1cb639e2008-03-11 21:52:16 +0800820 missing_dst=t
821
Stefan Beller44431df2016-05-31 17:27:59 -0700822 display_name=$(git submodule--helper relative-path "$name" "$wt_prefix")
John Keeping091a6eb2013-06-16 15:18:18 +0100823
Ping Yin1cb639e2008-03-11 21:52:16 +0800824 total_commits=
825 case "$missing_src,$missing_dst" in
826 t,)
John Keeping091a6eb2013-06-16 15:18:18 +0100827 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_src")"
Ping Yin1cb639e2008-03-11 21:52:16 +0800828 ;;
829 ,t)
John Keeping091a6eb2013-06-16 15:18:18 +0100830 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commit \$sha1_dst")"
Ping Yin1cb639e2008-03-11 21:52:16 +0800831 ;;
832 t,t)
John Keeping091a6eb2013-06-16 15:18:18 +0100833 errmsg="$(eval_gettext " Warn: \$display_name doesn't contain commits \$sha1_src and \$sha1_dst")"
Ping Yin1cb639e2008-03-11 21:52:16 +0800834 ;;
835 *)
836 errmsg=
837 total_commits=$(
Elia Pinto496eeeb2014-06-10 05:28:33 -0700838 if test $mod_src = 160000 && test $mod_dst = 160000
Ping Yin1cb639e2008-03-11 21:52:16 +0800839 then
840 range="$sha1_src...$sha1_dst"
841 elif test $mod_src = 160000
842 then
843 range=$sha1_src
844 else
845 range=$sha1_dst
846 fi
847 GIT_DIR="$name/.git" \
Jeff Kingb0e621a2010-04-08 15:42:37 -0400848 git rev-list --first-parent $range -- | wc -l
Ping Yin1cb639e2008-03-11 21:52:16 +0800849 )
Johannes Sixteed35592008-03-12 09:30:01 +0100850 total_commits=" ($(($total_commits + 0)))"
Ping Yin1cb639e2008-03-11 21:52:16 +0800851 ;;
852 esac
853
Sven van Haastregt0586a432019-02-03 21:00:27 +0000854 sha1_abbr_src=$(GIT_DIR="$name/.git" git rev-parse --short $sha1_src 2>/dev/null ||
855 echo $sha1_src | cut -c1-7)
856 sha1_abbr_dst=$(GIT_DIR="$name/.git" git rev-parse --short $sha1_dst 2>/dev/null ||
857 echo $sha1_dst | cut -c1-7)
858
Ping Yin1cb639e2008-03-11 21:52:16 +0800859 if test $status = T
860 then
Ævar Arnfjörð Bjarmasonb3e73442011-05-21 18:44:09 +0000861 blob="$(gettext "blob")"
862 submodule="$(gettext "submodule")"
Ping Yin1cb639e2008-03-11 21:52:16 +0800863 if test $mod_dst = 160000
864 then
John Keeping091a6eb2013-06-16 15:18:18 +0100865 echo "* $display_name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
Ping Yin1cb639e2008-03-11 21:52:16 +0800866 else
John Keeping091a6eb2013-06-16 15:18:18 +0100867 echo "* $display_name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
Ping Yin1cb639e2008-03-11 21:52:16 +0800868 fi
869 else
John Keeping091a6eb2013-06-16 15:18:18 +0100870 echo "* $display_name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
Ping Yin1cb639e2008-03-11 21:52:16 +0800871 fi
872 if test -n "$errmsg"
873 then
874 # Don't give error msg for modification whose dst is not submodule
875 # i.e. deleted or changed to blob
876 test $mod_dst = 160000 && echo "$errmsg"
877 else
Elia Pinto496eeeb2014-06-10 05:28:33 -0700878 if test $mod_src = 160000 && test $mod_dst = 160000
Ping Yin1cb639e2008-03-11 21:52:16 +0800879 then
Ping Yinf2dc06a2008-03-11 21:52:17 +0800880 limit=
881 test $summary_limit -gt 0 && limit="-$summary_limit"
Ping Yin1cb639e2008-03-11 21:52:16 +0800882 GIT_DIR="$name/.git" \
Ping Yinf2dc06a2008-03-11 21:52:17 +0800883 git log $limit --pretty='format: %m %s' \
Ping Yin1cb639e2008-03-11 21:52:16 +0800884 --first-parent $sha1_src...$sha1_dst
885 elif test $mod_dst = 160000
886 then
887 GIT_DIR="$name/.git" \
888 git log --pretty='format: > %s' -1 $sha1_dst
889 else
890 GIT_DIR="$name/.git" \
891 git log --pretty='format: < %s' -1 $sha1_src
892 fi
893 echo
894 fi
895 echo
Matthieu Moy3ba74072013-09-06 19:43:06 +0200896 done
Ping Yin28f9af52008-03-11 21:52:15 +0800897}
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200898#
Lars Hjemli941987a2007-06-11 21:12:24 +0200899# List all submodules, prefixed with:
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200900# - submodule not initialized
901# + different revision checked out
902#
903# If --cached was specified the revision in the index will be printed
904# instead of the currently checked out revision.
905#
906# $@ = requested paths (default to all)
907#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800908cmd_status()
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200909{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800910 # parse $args after "submodule ... status".
911 while test $# -ne 0
912 do
913 case "$1" in
914 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700915 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800916 ;;
917 --cached)
918 cached=1
919 ;;
Johan Herland64b19ff2009-08-19 03:45:24 +0200920 --recursive)
921 recursive=1
922 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800923 --)
924 shift
925 break
926 ;;
927 -*)
928 usage
929 ;;
930 *)
931 break
932 ;;
933 esac
934 shift
935 done
936
Prathamesh Chavana9f8a372017-10-06 18:54:15 +0530937 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} "$@"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200938}
David Aguilar2327f612008-08-24 12:43:37 -0700939#
940# Sync remote urls for submodules
941# This makes the value for remote.$remote.url match the value
942# specified in .gitmodules.
943#
944cmd_sync()
945{
946 while test $# -ne 0
947 do
948 case "$1" in
949 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700950 GIT_QUIET=1
David Aguilar2327f612008-08-24 12:43:37 -0700951 shift
952 ;;
Phil Hord82f49f22012-10-26 15:44:42 -0400953 --recursive)
954 recursive=1
955 shift
956 ;;
David Aguilar2327f612008-08-24 12:43:37 -0700957 --)
958 shift
959 break
960 ;;
961 -*)
962 usage
963 ;;
964 *)
965 break
966 ;;
967 esac
968 done
Brandon Williamse7849a92017-03-16 15:29:45 -0700969
Prathamesh Chavan13424762018-01-15 02:45:28 +0530970 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} "$@"
David Aguilar2327f612008-08-24 12:43:37 -0700971}
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200972
Stefan Bellerf6f85862016-12-12 11:04:35 -0800973cmd_absorbgitdirs()
974{
975 git submodule--helper absorb-git-dirs --prefix "$wt_prefix" "$@"
976}
977
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800978# This loop parses the command line arguments to find the
979# subcommand name to dispatch. Parsing of the subcommand specific
980# options are primarily done by the subcommand implementations.
981# Subcommand specific options such as --branch and --cached are
982# parsed here as well, for backward compatibility.
983
984while test $# != 0 && test -z "$command"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200985do
986 case "$1" in
Stefan Bellerf6f85862016-12-12 11:04:35 -0800987 add | foreach | init | deinit | update | status | summary | sync | absorbgitdirs)
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800988 command=$1
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200989 ;;
990 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700991 GIT_QUIET=1
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200992 ;;
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200993 -b|--branch)
994 case "$2" in
995 '')
996 usage
997 ;;
998 esac
999 branch="$2"; shift
1000 ;;
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001001 --cached)
Ping Yin28f9af52008-03-11 21:52:15 +08001002 cached="$1"
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001003 ;;
1004 --)
1005 break
1006 ;;
1007 -*)
1008 usage
1009 ;;
1010 *)
1011 break
1012 ;;
1013 esac
1014 shift
1015done
1016
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001017# No command word defaults to "status"
Ramkumar Ramachandraaf9c9f92012-09-22 16:57:59 +05301018if test -z "$command"
1019then
1020 if test $# = 0
1021 then
1022 command=status
1023 else
1024 usage
1025 fi
1026fi
Sven Verdoolaegeecda0722007-06-24 23:06:07 +02001027
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001028# "-b branch" is accepted only by "add"
1029if test -n "$branch" && test "$command" != add
1030then
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001031 usage
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001032fi
1033
Ping Yin28f9af52008-03-11 21:52:15 +08001034# "--cached" is accepted only by "status" and "summary"
Elia Pinto496eeeb2014-06-10 05:28:33 -07001035if test -n "$cached" && test "$command" != status && test "$command" != summary
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001036then
1037 usage
1038fi
1039
1040"cmd_$command" "$@"