blob: 87772ac89174fa7d2d47262f164c2dbc9f854ba7 [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>...)
Josh Steadmonf05da2b2022-02-04 21:00:49 -080013 or: $dashless [--quiet] update [--init [--filter=<filter-spec>]] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--[no-]single-branch] [--] [<path>...]
Denton Liub57e8112019-02-08 03:21:34 -080014 or: $dashless [--quiet] set-branch (--default|--branch <branch>) [--] <path>
Denton Liu26b06102019-10-29 10:01:52 -070015 or: $dashless [--quiet] set-url [--] <path> <newurl>
Junio C Hamanoadc54232009-08-27 16:59:25 -070016 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
Johan Herland15fc56a2009-08-19 03:45:22 +020017 or: $dashless [--quiet] foreach [--recursive] <command>
Stefan Bellerc32eaa82017-01-11 12:59:17 -080018 or: $dashless [--quiet] sync [--recursive] [--] [<path>...]
19 or: $dashless [--quiet] absorbgitdirs [--] [<path>...]"
Junio C Hamano8f321a32007-11-06 01:50:02 -080020OPTIONS_SPEC=
John Keeping091a6eb2013-06-16 15:18:18 +010021SUBDIRECTORY_OK=Yes
Lars Hjemli70c7ac22007-05-26 15:56:40 +020022. git-sh-setup
23require_work_tree
John Keeping091a6eb2013-06-16 15:18:18 +010024wt_prefix=$(git rev-parse --show-prefix)
25cd_to_toplevel
Lars Hjemli70c7ac22007-05-26 15:56:40 +020026
Brandon Williamsf1762d72016-12-14 14:39:52 -080027# Tell the rest of git that any URLs we get don't come
28# directly from the user, so it can apply policy as appropriate.
29GIT_PROTOCOL_FROM_USER=0
30export GIT_PROTOCOL_FROM_USER
Jeff King33cfccb2015-09-16 13:13:12 -040031
Junio C Hamano5c08dbb2008-01-15 02:48:45 -080032command=
Sven Verdoolaegeecda0722007-06-24 23:06:07 +020033branch=
Jens Lehmannd27b8762010-07-17 17:11:43 +020034force=
Michael S. Tsirkind92a3952009-05-04 22:30:01 +030035reference=
Lars Hjemli70c7ac22007-05-26 15:56:40 +020036cached=
Gerrit Pape48bb3032010-04-26 11:50:39 +020037recursive=
38init=
Johannes Schindelin0060fd12019-09-12 14:20:39 +020039require_init=
Jens Lehmann1c244f62009-08-13 21:32:50 +020040files=
W. Trevor King06b1abb2012-12-19 11:03:32 -050041remote=
Fabian Franz31ca3ac2009-02-05 20:18:32 -020042nofetch=
Johan Herland32948422009-06-03 08:27:06 +020043update=
Johan Herland15fc56a2009-08-19 03:45:22 +020044prefix=
Jens Lehmann73b08982012-09-30 01:05:58 +020045custom_name=
Fredrik Gustafsson275cd182013-07-02 23:42:56 +020046depth=
Jeff King72c5f882016-09-22 01:24:46 -040047progress=
Casey Fitzpatricka0ef2932018-05-03 06:53:46 -040048dissociate=
Emily Shaffer132f6002020-02-20 19:10:27 -080049single_branch=
Li Xuejiang65d100c2020-04-02 16:42:51 +080050jobs=
51recommend_shallow=
Josh Steadmonf05da2b2022-02-04 21:00:49 -080052filter=
Lars Hjemli70c7ac22007-05-26 15:56:40 +020053
Heiko Voigtbe9d0a32012-08-14 22:35:27 +020054die_if_unmatched ()
55{
56 if test "$1" = "#unmatched"
57 then
Johannes Sixtc4c02bf2016-07-22 21:14:38 +020058 exit ${2:-1}
Heiko Voigtbe9d0a32012-08-14 22:35:27 +020059 fi
60}
61
René Scharfe862ae6c2013-04-01 15:06:27 +020062isnumber()
63{
64 n=$(($1 + 0)) 2>/dev/null && test "$n" = "$1"
65}
66
Jacob Keller14111fc2016-02-29 14:58:35 -080067# Sanitize the local git environment for use within a submodule. We
68# can't simply use clear_local_git_env since we want to preserve some
69# of the settings from GIT_CONFIG_PARAMETERS.
70sanitize_submodule_env()
71{
Jeff King89044ba2016-05-04 21:22:19 -040072 save_config=$GIT_CONFIG_PARAMETERS
Jacob Keller14111fc2016-02-29 14:58:35 -080073 clear_local_git_env
Jeff King89044ba2016-05-04 21:22:19 -040074 GIT_CONFIG_PARAMETERS=$save_config
Jeff King860cba62016-04-28 09:37:44 -040075 export GIT_CONFIG_PARAMETERS
Jacob Keller14111fc2016-02-29 14:58:35 -080076}
77
Lars Hjemli70c7ac22007-05-26 15:56:40 +020078#
Sven Verdoolaegeecda0722007-06-24 23:06:07 +020079# Add a new submodule to the working tree, .gitmodules and the index
80#
Mark Levedahlec05df32008-07-09 21:05:40 -040081# $@ = repo path
Sven Verdoolaegeecda0722007-06-24 23:06:07 +020082#
83# optional branch is stored in global branch variable
84#
Junio C Hamano23a485e2008-01-15 02:35:49 -080085cmd_add()
Sven Verdoolaegeecda0722007-06-24 23:06:07 +020086{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -080087 # parse $args after "submodule ... add".
John Keeping091a6eb2013-06-16 15:18:18 +010088 reference_path=
Junio C Hamano5c08dbb2008-01-15 02:48:45 -080089 while test $# -ne 0
90 do
91 case "$1" in
92 -b | --branch)
93 case "$2" in '') usage ;; esac
94 branch=$2
95 shift
96 ;;
Jens Lehmannd27b8762010-07-17 17:11:43 +020097 -f | --force)
98 force=$1
99 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800100 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700101 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800102 ;;
Casey Fitzpatrick6d33e1c2018-05-03 06:53:45 -0400103 --progress)
104 progress=1
105 ;;
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300106 --reference)
107 case "$2" in '') usage ;; esac
John Keeping091a6eb2013-06-16 15:18:18 +0100108 reference_path=$2
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300109 shift
110 ;;
111 --reference=*)
John Keeping091a6eb2013-06-16 15:18:18 +0100112 reference_path="${1#--reference=}"
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300113 ;;
Casey Fitzpatricka0ef2932018-05-03 06:53:46 -0400114 --dissociate)
115 dissociate=1
116 ;;
Jens Lehmann73b08982012-09-30 01:05:58 +0200117 --name)
118 case "$2" in '') usage ;; esac
119 custom_name=$2
120 shift
121 ;;
Fredrik Gustafsson275cd182013-07-02 23:42:56 +0200122 --depth)
123 case "$2" in '') usage ;; esac
124 depth="--depth=$2"
125 shift
126 ;;
127 --depth=*)
128 depth=$1
129 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800130 --)
131 shift
132 break
133 ;;
134 -*)
135 usage
136 ;;
137 *)
138 break
139 ;;
140 esac
141 shift
142 done
143
Atharva Raykara6226fd2021-08-10 17:16:37 +0530144 if test -z "$1"
Antonio Ospite76e9bdc2018-10-25 18:18:12 +0200145 then
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200146 usage
147 fi
148
Atharva Raykara6226fd2021-08-10 17:16:37 +0530149 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper add ${GIT_QUIET:+--quiet} ${force:+--force} ${progress:+"--progress"} ${branch:+--branch "$branch"} ${reference_path:+--reference "$reference_path"} ${dissociate:+--dissociate} ${custom_name:+--name "$custom_name"} ${depth:+"$depth"} -- "$@"
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200150}
151
152#
Mark Levedahl19a31f92008-08-10 19:10:04 -0400153# Execute an arbitrary command sequence in each checked out
154# submodule
155#
156# $@ = command to execute
157#
158cmd_foreach()
159{
Johan Herland1d5bec82009-08-19 03:45:19 +0200160 # parse $args after "submodule ... foreach".
161 while test $# -ne 0
162 do
163 case "$1" in
164 -q|--quiet)
165 GIT_QUIET=1
166 ;;
Johan Herland15fc56a2009-08-19 03:45:22 +0200167 --recursive)
168 recursive=1
169 ;;
Johan Herland1d5bec82009-08-19 03:45:19 +0200170 -*)
171 usage
172 ;;
173 *)
174 break
175 ;;
176 esac
177 shift
178 done
179
Kaartic Sivaraam1cf823d2021-06-22 23:44:52 +0530180 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper foreach ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
Mark Levedahl19a31f92008-08-10 19:10:04 -0400181}
182
183#
Lars Hjemli211b7f12007-06-06 11:13:02 +0200184# Register submodules in .git/config
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200185#
186# $@ = requested paths (default to all)
187#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800188cmd_init()
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200189{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800190 # parse $args after "submodule ... init".
191 while test $# -ne 0
192 do
193 case "$1" in
194 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700195 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800196 ;;
197 --)
198 shift
199 break
200 ;;
201 -*)
202 usage
203 ;;
204 *)
205 break
206 ;;
207 esac
208 shift
209 done
210
Nguyễn Thái Ngọc Duya282f5a2019-04-12 17:08:19 +0700211 git ${wt_prefix:+-C "$wt_prefix"} ${prefix:+--super-prefix "$prefix"} submodule--helper init ${GIT_QUIET:+--quiet} -- "$@"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200212}
213
214#
Jens Lehmanncf419822013-03-04 22:20:24 +0100215# Unregister submodules from .git/config and remove their work tree
216#
Jens Lehmanncf419822013-03-04 22:20:24 +0100217cmd_deinit()
218{
219 # parse $args after "submodule ... deinit".
Stefan Bellerf6a52792016-05-05 12:52:32 -0700220 deinit_all=
Jens Lehmanncf419822013-03-04 22:20:24 +0100221 while test $# -ne 0
222 do
223 case "$1" in
224 -f|--force)
225 force=$1
226 ;;
227 -q|--quiet)
228 GIT_QUIET=1
229 ;;
Stefan Bellerf6a52792016-05-05 12:52:32 -0700230 --all)
231 deinit_all=t
232 ;;
Jens Lehmanncf419822013-03-04 22:20:24 +0100233 --)
234 shift
235 break
236 ;;
237 -*)
238 usage
239 ;;
240 *)
241 break
242 ;;
243 esac
244 shift
245 done
246
Kaartic Sivaraam1cf823d2021-06-22 23:44:52 +0530247 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper deinit ${GIT_QUIET:+--quiet} ${force:+--force} ${deinit_all:+--all} -- "$@"
Jens Lehmanncf419822013-03-04 22:20:24 +0100248}
249
Jeff King66d36b92020-11-24 04:06:05 -0500250# usage: fetch_in_submodule <module_path> [<depth>] [<sha1>]
251# Because arguments are positional, use an empty string to omit <depth>
252# but include <sha1>.
Stefan Bellerfb43e312016-02-23 19:32:13 -0800253fetch_in_submodule () (
Junio C Hamano01e1d542016-04-06 11:39:12 -0700254 sanitize_submodule_env &&
Stefan Bellerfb43e312016-02-23 19:32:13 -0800255 cd "$1" &&
Ævar Arnfjörð Bjarmason1c151802020-11-14 13:21:30 +0100256 if test $# -eq 3
257 then
Nicholas Clark62af4bd2021-04-30 09:59:06 +0000258 echo "$3" | git fetch ${GIT_QUIET:+--quiet} --stdin ${2:+"$2"}
Ævar Arnfjörð Bjarmason1c151802020-11-14 13:21:30 +0100259 else
Nicholas Clark62af4bd2021-04-30 09:59:06 +0000260 git fetch ${GIT_QUIET:+--quiet} ${2:+"$2"}
Ævar Arnfjörð Bjarmason1c151802020-11-14 13:21:30 +0100261 fi
Stefan Bellerfb43e312016-02-23 19:32:13 -0800262)
263
Jens Lehmanncf419822013-03-04 22:20:24 +0100264#
Lars Hjemli211b7f12007-06-06 11:13:02 +0200265# Update each submodule path to correct revision, using clone and checkout as needed
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200266#
267# $@ = requested paths (default to all)
268#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800269cmd_update()
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200270{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800271 # parse $args after "submodule ... update".
272 while test $# -ne 0
273 do
274 case "$1" in
275 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700276 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800277 ;;
Stefan Bellere84c3cf2018-08-14 11:22:02 -0700278 -v)
Theodore Dubois3ad04012020-09-30 12:50:53 -0700279 unset GIT_QUIET
Stefan Bellere84c3cf2018-08-14 11:22:02 -0700280 ;;
Jeff King72c5f882016-09-22 01:24:46 -0400281 --progress)
Casey Fitzpatrickc7199e32018-05-03 06:53:44 -0400282 progress=1
Jeff King72c5f882016-09-22 01:24:46 -0400283 ;;
Johannes Schindelinbe4d2c82008-05-16 11:23:03 +0100284 -i|--init)
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300285 init=1
Johannes Schindelinbe4d2c82008-05-16 11:23:03 +0100286 ;;
Johannes Schindelin0060fd12019-09-12 14:20:39 +0200287 --require-init)
288 init=1
289 require_init=1
290 ;;
W. Trevor King06b1abb2012-12-19 11:03:32 -0500291 --remote)
292 remote=1
293 ;;
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200294 -N|--no-fetch)
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200295 nofetch=1
296 ;;
Nicolas Morey-Chaisemartin9db31bd2011-04-01 11:42:03 +0200297 -f|--force)
298 force=$1
299 ;;
Peter Huttererca2cedb2009-04-24 09:06:38 +1000300 -r|--rebase)
Johan Herland32948422009-06-03 08:27:06 +0200301 update="rebase"
Peter Huttererca2cedb2009-04-24 09:06:38 +1000302 ;;
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300303 --reference)
304 case "$2" in '') usage ;; esac
305 reference="--reference=$2"
Kevin Ballard98dbe632010-11-02 23:26:25 -0700306 shift
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300307 ;;
308 --reference=*)
309 reference="$1"
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300310 ;;
Casey Fitzpatricka0ef2932018-05-03 06:53:46 -0400311 --dissociate)
312 dissociate=1
313 ;;
Johan Herland42b49172009-06-03 00:59:12 +0200314 -m|--merge)
Johan Herland42b49172009-06-03 00:59:12 +0200315 update="merge"
316 ;;
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200317 --recursive)
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200318 recursive=1
319 ;;
Junio C Hamanoefc5fb62011-10-10 15:56:16 -0700320 --checkout)
321 update="checkout"
322 ;;
Stefan Bellerabed0002016-05-26 14:59:43 -0700323 --recommend-shallow)
324 recommend_shallow="--recommend-shallow"
325 ;;
326 --no-recommend-shallow)
327 recommend_shallow="--no-recommend-shallow"
328 ;;
Fredrik Gustafsson275cd182013-07-02 23:42:56 +0200329 --depth)
330 case "$2" in '') usage ;; esac
331 depth="--depth=$2"
332 shift
333 ;;
334 --depth=*)
335 depth=$1
336 ;;
Stefan Beller2335b872016-02-29 18:07:19 -0800337 -j|--jobs)
338 case "$2" in '') usage ;; esac
339 jobs="--jobs=$2"
340 shift
341 ;;
342 --jobs=*)
343 jobs=$1
344 ;;
Emily Shaffer132f6002020-02-20 19:10:27 -0800345 --single-branch)
346 single_branch="--single-branch"
347 ;;
348 --no-single-branch)
349 single_branch="--no-single-branch"
350 ;;
Josh Steadmonf05da2b2022-02-04 21:00:49 -0800351 --filter)
352 case "$2" in '') usage ;; esac
353 filter="--filter=$2"
354 shift
355 ;;
356 --filter=*)
357 filter="$1"
358 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800359 --)
360 shift
361 break
362 ;;
363 -*)
364 usage
365 ;;
366 *)
367 break
368 ;;
369 esac
Kevin Ballard98dbe632010-11-02 23:26:25 -0700370 shift
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800371 done
372
Josh Steadmonf05da2b2022-02-04 21:00:49 -0800373 if test -n "$filter" && test "$init" != "1"
374 then
375 usage
376 fi
377
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300378 if test -n "$init"
379 then
380 cmd_init "--" "$@" || return
381 fi
382
Stefan Beller48308682016-02-29 18:07:17 -0800383 {
384 git submodule--helper update-clone ${GIT_QUIET:+--quiet} \
Casey Fitzpatrickc7199e32018-05-03 06:53:44 -0400385 ${progress:+"--progress"} \
Stefan Beller48308682016-02-29 18:07:17 -0800386 ${wt_prefix:+--prefix "$wt_prefix"} \
387 ${prefix:+--recursive-prefix "$prefix"} \
388 ${update:+--update "$update"} \
Stefan Beller5f50f332016-08-11 16:14:01 -0700389 ${reference:+"$reference"} \
Casey Fitzpatricka0ef2932018-05-03 06:53:46 -0400390 ${dissociate:+"--dissociate"} \
Stefan Beller48308682016-02-29 18:07:17 -0800391 ${depth:+--depth "$depth"} \
Johannes Schindelin0060fd12019-09-12 14:20:39 +0200392 ${require_init:+--require-init} \
Emily Shaffer132f6002020-02-20 19:10:27 -0800393 $single_branch \
Casey Fitzpatrickc7199e32018-05-03 06:53:44 -0400394 $recommend_shallow \
395 $jobs \
Josh Steadmonf05da2b2022-02-04 21:00:49 -0800396 $filter \
Nguyễn Thái Ngọc Duya282f5a2019-04-12 17:08:19 +0700397 -- \
Johannes Sixtc4c02bf2016-07-22 21:14:38 +0200398 "$@" || echo "#unmatched" $?
Stefan Beller48308682016-02-29 18:07:17 -0800399 } | {
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200400 err=
Stefan Beller9eca7012018-08-03 15:23:17 -0700401 while read -r quickabort sha1 just_cloned sm_path
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200402 do
Stefan Beller9eca7012018-08-03 15:23:17 -0700403 die_if_unmatched "$quickabort" "$sha1"
Stefan Beller48308682016-02-29 18:07:17 -0800404
Stefan Beller5d124f42019-01-18 13:55:19 -0800405 git submodule--helper ensure-core-worktree "$sm_path" || exit 1
Stefan Beller74d47312018-08-13 15:42:34 -0700406
Stefan Beller44431df2016-05-31 17:27:59 -0700407 displaypath=$(git submodule--helper relative-path "$prefix$sm_path" "$wt_prefix")
John Keeping091a6eb2013-06-16 15:18:18 +0100408
Stefan Beller48308682016-02-29 18:07:17 -0800409 if test $just_cloned -eq 1
Junio C Hamanod851ffb2014-04-02 14:15:36 -0700410 then
Lars Hjemlibf2d8242007-06-11 21:12:22 +0200411 subsha1=
412 else
Atharva Raykarc51f8f92021-08-24 19:36:09 +0530413 just_cloned=
Jacob Keller14111fc2016-02-29 14:58:35 -0800414 subsha1=$(sanitize_submodule_env; cd "$sm_path" &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700415 git rev-parse --verify HEAD) ||
Atharva Raykar0008d122021-07-10 13:17:59 +0530416 die "fatal: $(eval_gettext "Unable to find current revision in submodule path '\$displaypath'")"
Lars Hjemli211b7f12007-06-06 11:13:02 +0200417 fi
418
W. Trevor King06b1abb2012-12-19 11:03:32 -0500419 if test -n "$remote"
420 then
Stefan Beller92bbe7c2016-08-03 13:44:03 -0700421 branch=$(git submodule--helper remote-branch "$sm_path")
W. Trevor King06b1abb2012-12-19 11:03:32 -0500422 if test -z "$nofetch"
423 then
424 # Fetch remote before determining tracking $sha1
Stefan Beller6cbf4542016-07-28 17:44:04 -0700425 fetch_in_submodule "$sm_path" $depth ||
Atharva Raykar0008d122021-07-10 13:17:59 +0530426 die "fatal: $(eval_gettext "Unable to fetch in submodule path '\$sm_path'")"
W. Trevor King06b1abb2012-12-19 11:03:32 -0500427 fi
Ævar Arnfjörð Bjarmasone63f7b02020-11-14 13:21:31 +0100428 remote_name=$(sanitize_submodule_env; cd "$sm_path" && git submodule--helper print-default-remote)
Jacob Keller14111fc2016-02-29 14:58:35 -0800429 sha1=$(sanitize_submodule_env; cd "$sm_path" &&
W. Trevor King06b1abb2012-12-19 11:03:32 -0500430 git rev-parse --verify "${remote_name}/${branch}") ||
Atharva Raykar0008d122021-07-10 13:17:59 +0530431 die "fatal: $(eval_gettext "Unable to find current \${remote_name}/\${branch} revision in submodule path '\$sm_path'")"
W. Trevor King06b1abb2012-12-19 11:03:32 -0500432 fi
433
Atharva Raykarc51f8f92021-08-24 19:36:09 +0530434 out=$(git submodule--helper run-update-procedure \
435 ${wt_prefix:+--prefix "$wt_prefix"} \
436 ${GIT_QUIET:+--quiet} \
437 ${force:+--force} \
438 ${just_cloned:+--just-cloned} \
439 ${nofetch:+--no-fetch} \
440 ${depth:+"$depth"} \
441 ${update:+--update "$update"} \
442 ${prefix:+--recursive-prefix "$prefix"} \
443 ${sha1:+--oid "$sha1"} \
444 ${subsha1:+--suboid "$subsha1"} \
445 "--" \
446 "$sm_path")
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200447
Atharva Raykarc51f8f92021-08-24 19:36:09 +0530448 # exit codes for run-update-procedure:
449 # 0: update was successful, say command output
450 # 1: update procedure failed, but should not die
451 # 2 or 128: subcommand died during execution
452 # 3: no update procedure was run
453 res="$?"
454 case $res in
455 0)
456 say "$out"
457 ;;
458 1)
459 err="${err};fatal: $out"
460 continue
461 ;;
462 2|128)
463 die_with_status $res "fatal: $out"
464 ;;
465 esac
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200466
467 if test -n "$recursive"
468 then
William Entriken75bf5e62013-03-02 14:44:59 -0500469 (
Stefan Beller44431df2016-05-31 17:27:59 -0700470 prefix=$(git submodule--helper relative-path "$prefix$sm_path/" "$wt_prefix")
Stefan Beller36042422016-04-15 17:50:13 -0700471 wt_prefix=
Jacob Keller14111fc2016-02-29 14:58:35 -0800472 sanitize_submodule_env
William Entriken75bf5e62013-03-02 14:44:59 -0500473 cd "$sm_path" &&
Jens Lehmann36141282013-11-11 21:55:52 +0100474 eval cmd_update
William Entriken75bf5e62013-03-02 14:44:59 -0500475 )
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200476 res=$?
477 if test $res -gt 0
478 then
Atharva Raykar0008d122021-07-10 13:17:59 +0530479 die_msg="fatal: $(eval_gettext "Failed to recurse into submodule path '\$displaypath'")"
Stefan Bellerbb9d91b2016-06-09 12:06:37 -0700480 if test $res -ne 2
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200481 then
Junio C Hamanoff968f02011-07-13 14:31:35 -0700482 err="${err};$die_msg"
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200483 continue
484 else
Junio C Hamanoff968f02011-07-13 14:31:35 -0700485 die_with_status $res "$die_msg"
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200486 fi
487 fi
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200488 fi
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200489 done
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200490
491 if test -n "$err"
492 then
493 OIFS=$IFS
494 IFS=';'
495 for e in $err
496 do
497 if test -n "$e"
498 then
499 echo >&2 "$e"
500 fi
501 done
502 IFS=$OIFS
503 exit 1
504 fi
505 }
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200506}
507
Ping Yin28f9af52008-03-11 21:52:15 +0800508#
Denton Liub57e8112019-02-08 03:21:34 -0800509# Configures a submodule's default branch
510#
511# $@ = requested path
512#
513cmd_set_branch() {
Shourya Shukla2964d6e2020-06-02 22:05:23 +0530514 default=
Denton Liub57e8112019-02-08 03:21:34 -0800515 branch=
516
517 while test $# -ne 0
518 do
519 case "$1" in
520 -q|--quiet)
521 # we don't do anything with this but we need to accept it
522 ;;
523 -d|--default)
Shourya Shukla2964d6e2020-06-02 22:05:23 +0530524 default=1
Denton Liub57e8112019-02-08 03:21:34 -0800525 ;;
526 -b|--branch)
527 case "$2" in '') usage ;; esac
528 branch=$2
529 shift
530 ;;
531 --)
532 shift
533 break
534 ;;
535 -*)
536 usage
537 ;;
538 *)
539 break
540 ;;
541 esac
542 shift
543 done
544
Kaartic Sivaraam1cf823d2021-06-22 23:44:52 +0530545 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-branch ${GIT_QUIET:+--quiet} ${branch:+--branch "$branch"} ${default:+--default} -- "$@"
Denton Liub57e8112019-02-08 03:21:34 -0800546}
547
548#
Denton Liu26b06102019-10-29 10:01:52 -0700549# Configures a submodule's remote url
550#
551# $@ = requested path, requested url
552#
553cmd_set_url() {
554 while test $# -ne 0
555 do
556 case "$1" in
557 -q|--quiet)
558 GIT_QUIET=1
559 ;;
560 --)
561 shift
562 break
563 ;;
564 -*)
565 usage
566 ;;
567 *)
568 break
569 ;;
570 esac
571 shift
572 done
573
Kaartic Sivaraam1cf823d2021-06-22 23:44:52 +0530574 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper set-url ${GIT_QUIET:+--quiet} -- "$@"
Denton Liu26b06102019-10-29 10:01:52 -0700575}
576
577#
Ping Yin28f9af52008-03-11 21:52:15 +0800578# Show commit summary for submodules in index or working tree
579#
580# If '--cached' is given, show summary between index and given commit,
581# or between working tree and given commit
582#
583# $@ = [commit (default 'HEAD'),] requested paths (default all)
584#
585cmd_summary() {
Ping Yinf2dc06a2008-03-11 21:52:17 +0800586 summary_limit=-1
Ping Yind0f64dd2008-04-12 23:05:31 +0800587 for_status=
Jens Lehmann1c244f62009-08-13 21:32:50 +0200588 diff_cmd=diff-index
Emil Medvebffe71f2007-06-26 18:40:58 -0500589
Ping Yin28f9af52008-03-11 21:52:15 +0800590 # parse $args after "submodule ... summary".
591 while test $# -ne 0
592 do
593 case "$1" in
594 --cached)
595 cached="$1"
596 ;;
Jens Lehmann1c244f62009-08-13 21:32:50 +0200597 --files)
598 files="$1"
599 ;;
Ping Yind0f64dd2008-04-12 23:05:31 +0800600 --for-status)
601 for_status="$1"
602 ;;
Ping Yinf2dc06a2008-03-11 21:52:17 +0800603 -n|--summary-limit)
René Scharfe862ae6c2013-04-01 15:06:27 +0200604 summary_limit="$2"
605 isnumber "$summary_limit" || usage
Ping Yinf2dc06a2008-03-11 21:52:17 +0800606 shift
607 ;;
René Scharfe862ae6c2013-04-01 15:06:27 +0200608 --summary-limit=*)
609 summary_limit="${1#--summary-limit=}"
610 isnumber "$summary_limit" || usage
611 ;;
Ping Yin28f9af52008-03-11 21:52:15 +0800612 --)
613 shift
614 break
615 ;;
616 -*)
617 usage
618 ;;
619 *)
620 break
621 ;;
622 esac
623 shift
624 done
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200625
Kaartic Sivaraam1cf823d2021-06-22 23:44:52 +0530626 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper summary ${files:+--files} ${cached:+--cached} ${for_status:+--for-status} ${summary_limit:+-n $summary_limit} -- "$@"
Ping Yin28f9af52008-03-11 21:52:15 +0800627}
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200628#
Lars Hjemli941987a2007-06-11 21:12:24 +0200629# List all submodules, prefixed with:
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200630# - submodule not initialized
631# + different revision checked out
632#
633# If --cached was specified the revision in the index will be printed
634# instead of the currently checked out revision.
635#
636# $@ = requested paths (default to all)
637#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800638cmd_status()
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200639{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800640 # parse $args after "submodule ... status".
641 while test $# -ne 0
642 do
643 case "$1" in
644 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700645 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800646 ;;
647 --cached)
648 cached=1
649 ;;
Johan Herland64b19ff2009-08-19 03:45:24 +0200650 --recursive)
651 recursive=1
652 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800653 --)
654 shift
655 break
656 ;;
657 -*)
658 usage
659 ;;
660 *)
661 break
662 ;;
663 esac
664 shift
665 done
666
Kaartic Sivaraam1cf823d2021-06-22 23:44:52 +0530667 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper status ${GIT_QUIET:+--quiet} ${cached:+--cached} ${recursive:+--recursive} -- "$@"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200668}
David Aguilar2327f612008-08-24 12:43:37 -0700669#
670# Sync remote urls for submodules
671# This makes the value for remote.$remote.url match the value
672# specified in .gitmodules.
673#
674cmd_sync()
675{
676 while test $# -ne 0
677 do
678 case "$1" in
679 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700680 GIT_QUIET=1
David Aguilar2327f612008-08-24 12:43:37 -0700681 shift
682 ;;
Phil Hord82f49f22012-10-26 15:44:42 -0400683 --recursive)
684 recursive=1
685 shift
686 ;;
David Aguilar2327f612008-08-24 12:43:37 -0700687 --)
688 shift
689 break
690 ;;
691 -*)
692 usage
693 ;;
694 *)
695 break
696 ;;
697 esac
698 done
Brandon Williamse7849a92017-03-16 15:29:45 -0700699
Kaartic Sivaraam1cf823d2021-06-22 23:44:52 +0530700 git ${wt_prefix:+-C "$wt_prefix"} submodule--helper sync ${GIT_QUIET:+--quiet} ${recursive:+--recursive} -- "$@"
David Aguilar2327f612008-08-24 12:43:37 -0700701}
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200702
Stefan Bellerf6f85862016-12-12 11:04:35 -0800703cmd_absorbgitdirs()
704{
705 git submodule--helper absorb-git-dirs --prefix "$wt_prefix" "$@"
706}
707
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800708# This loop parses the command line arguments to find the
709# subcommand name to dispatch. Parsing of the subcommand specific
710# options are primarily done by the subcommand implementations.
711# Subcommand specific options such as --branch and --cached are
712# parsed here as well, for backward compatibility.
713
714while test $# != 0 && test -z "$command"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200715do
716 case "$1" in
Denton Liu26b06102019-10-29 10:01:52 -0700717 add | foreach | init | deinit | update | set-branch | set-url | status | summary | sync | absorbgitdirs)
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800718 command=$1
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200719 ;;
720 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700721 GIT_QUIET=1
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200722 ;;
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200723 -b|--branch)
724 case "$2" in
725 '')
726 usage
727 ;;
728 esac
729 branch="$2"; shift
730 ;;
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200731 --cached)
Ping Yin28f9af52008-03-11 21:52:15 +0800732 cached="$1"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200733 ;;
734 --)
735 break
736 ;;
737 -*)
738 usage
739 ;;
740 *)
741 break
742 ;;
743 esac
744 shift
745done
746
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800747# No command word defaults to "status"
Ramkumar Ramachandraaf9c9f92012-09-22 16:57:59 +0530748if test -z "$command"
749then
750 if test $# = 0
751 then
752 command=status
753 else
754 usage
755 fi
756fi
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200757
Denton Liub57e8112019-02-08 03:21:34 -0800758# "-b branch" is accepted only by "add" and "set-branch"
759if test -n "$branch" && (test "$command" != add || test "$command" != set-branch)
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800760then
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200761 usage
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800762fi
763
Ping Yin28f9af52008-03-11 21:52:15 +0800764# "--cached" is accepted only by "status" and "summary"
Elia Pinto496eeeb2014-06-10 05:28:33 -0700765if test -n "$cached" && test "$command" != status && test "$command" != summary
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800766then
767 usage
768fi
769
Denton Liub57e8112019-02-08 03:21:34 -0800770"cmd_$(echo $command | sed -e s/-/_/g)" "$@"