blob: 2a93c611ea64e4c1f4f6cfa0d2dfc21d96d1a421 [file] [log] [blame]
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001#!/bin/sh
2#
Sven Verdoolaegeecda0722007-06-24 23:06:07 +02003# git-submodules.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/-/ /')
Jens Lehmannd27b8762010-07-17 17:11:43 +02008USAGE="[--quiet] add [-b branch] [-f|--force] [--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>...]
Nicolas Morey-Chaisemartin9db31bd2011-04-01 11:42:03 +020011 or: $dashless [--quiet] update [--init] [-N|--no-fetch] [-f|--force] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
Junio C Hamanoadc54232009-08-27 16:59:25 -070012 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
Johan Herland15fc56a2009-08-19 03:45:22 +020013 or: $dashless [--quiet] foreach [--recursive] <command>
Johan Herland1d5bec82009-08-19 03:45:19 +020014 or: $dashless [--quiet] sync [--] [<path>...]"
Junio C Hamano8f321a32007-11-06 01:50:02 -080015OPTIONS_SPEC=
Lars Hjemli70c7ac22007-05-26 15:56:40 +020016. git-sh-setup
Ævar Arnfjörð Bjarmasond0ad8252011-05-21 18:43:58 +000017. git-sh-i18n
Mark Levedahl98fcf842008-08-24 14:46:10 -040018. git-parse-remote
Lars Hjemli70c7ac22007-05-26 15:56:40 +020019require_work_tree
20
Junio C Hamano5c08dbb2008-01-15 02:48:45 -080021command=
Sven Verdoolaegeecda0722007-06-24 23:06:07 +020022branch=
Jens Lehmannd27b8762010-07-17 17:11:43 +020023force=
Michael S. Tsirkind92a3952009-05-04 22:30:01 +030024reference=
Lars Hjemli70c7ac22007-05-26 15:56:40 +020025cached=
Gerrit Pape48bb3032010-04-26 11:50:39 +020026recursive=
27init=
Jens Lehmann1c244f62009-08-13 21:32:50 +020028files=
Fabian Franz31ca3ac2009-02-05 20:18:32 -020029nofetch=
Johan Herland32948422009-06-03 08:27:06 +020030update=
Johan Herland15fc56a2009-08-19 03:45:22 +020031prefix=
Lars Hjemli70c7ac22007-05-26 15:56:40 +020032
Mark Levedahlf31a5222007-09-23 22:19:42 -040033# Resolve relative url by appending to parent's url
34resolve_relative_url ()
35{
Mark Levedahl98fcf842008-08-24 14:46:10 -040036 remote=$(get_default_remote)
Mark Levedahl8e7e6f32008-06-14 13:09:41 -040037 remoteurl=$(git config "remote.$remote.url") ||
Jens Lehmann4d689322011-06-06 21:58:04 +020038 remoteurl=$(pwd) # the repository is its own authoritative upstream
Mark Levedahlf31a5222007-09-23 22:19:42 -040039 url="$1"
Mark Levedahl99b120a2008-08-21 19:54:01 -040040 remoteurl=${remoteurl%/}
Thomas Rastea640cc2011-01-10 11:37:26 +010041 sep=/
Mark Levedahlf31a5222007-09-23 22:19:42 -040042 while test -n "$url"
43 do
44 case "$url" in
45 ../*)
46 url="${url#../}"
Thomas Rastea640cc2011-01-10 11:37:26 +010047 case "$remoteurl" in
48 */*)
49 remoteurl="${remoteurl%/*}"
50 ;;
51 *:*)
52 remoteurl="${remoteurl%:*}"
53 sep=:
54 ;;
55 *)
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +000056 die "$(eval_gettext "cannot strip one component off url '\$remoteurl'")"
Thomas Rastea640cc2011-01-10 11:37:26 +010057 ;;
58 esac
Mark Levedahlf31a5222007-09-23 22:19:42 -040059 ;;
60 ./*)
61 url="${url#./}"
62 ;;
63 *)
64 break;;
65 esac
66 done
Thomas Rastea640cc2011-01-10 11:37:26 +010067 echo "$remoteurl$sep${url%/}"
Mark Levedahlf31a5222007-09-23 22:19:42 -040068}
69
Lars Hjemli941987a2007-06-11 21:12:24 +020070#
David Aguilara7b32692008-08-22 00:30:50 -070071# Get submodule info for registered submodules
72# $@ = path to limit submodule list
73#
74module_list()
75{
Nicolas Morey-Chaisemartin313ee0d2011-03-30 07:20:02 +020076 git ls-files --error-unmatch --stage -- "$@" |
77 perl -e '
78 my %unmerged = ();
79 my ($null_sha1) = ("0" x 40);
80 while (<STDIN>) {
81 chomp;
82 my ($mode, $sha1, $stage, $path) =
83 /^([0-7]+) ([0-9a-f]{40}) ([0-3])\t(.*)$/;
84 next unless $mode eq "160000";
85 if ($stage ne "0") {
86 if (!$unmerged{$path}++) {
87 print "$mode $null_sha1 U\t$path\n";
88 }
89 next;
90 }
91 print "$_\n";
92 }
93 '
David Aguilara7b32692008-08-22 00:30:50 -070094}
95
96#
Lars Hjemli941987a2007-06-11 21:12:24 +020097# Map submodule path to submodule name
98#
99# $1 = path
100#
101module_name()
102{
Junio C Hamano537601a2007-07-25 15:51:26 -0700103 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
Chris Riddfe22e542008-06-11 14:09:19 +0100104 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
Imran M Yousufa5099bb2008-05-15 13:42:58 +0600105 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
Junio C Hamano537601a2007-07-25 15:51:26 -0700106 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
Tay Ray Chuan1e422582011-10-21 21:49:35 +0800107 test -z "$name" &&
108 die "$(eval_gettext "No submodule mapping found in .gitmodules for path '\$path'")"
109 echo "$name"
Lars Hjemli941987a2007-06-11 21:12:24 +0200110}
Lars Hjemli33aa6ff2007-06-06 11:13:01 +0200111
112#
113# Clone a submodule
114#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800115# Prior to calling, cmd_update checks that a possibly existing
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200116# path is not a git repository.
Junio C Hamano23a485e2008-01-15 02:35:49 -0800117# Likewise, cmd_add checks that path does not exist at all,
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200118# since it is the location of a new submodule.
119#
Lars Hjemli33aa6ff2007-06-06 11:13:01 +0200120module_clone()
121{
122 path=$1
123 url=$2
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300124 reference="$3"
Jens Lehmann7e604072011-07-26 23:39:03 +0200125 quiet=
126 if test -n "$GIT_QUIET"
127 then
128 quiet=-q
129 fi
Lars Hjemli33aa6ff2007-06-06 11:13:01 +0200130
Fredrik Gustafsson501770e2011-08-15 23:17:47 +0200131 gitdir=
132 gitdir_base=
Tay Ray Chuan9e76d4a2011-10-21 21:49:36 +0800133 name=$(module_name "$path" 2>/dev/null)
Jens Lehmann1017c1a2012-01-24 22:49:56 +0100134 test -n "$name" || name="$path"
Fredrik Gustafsson501770e2011-08-15 23:17:47 +0200135 base_path=$(dirname "$path")
Lars Hjemli33aa6ff2007-06-06 11:13:01 +0200136
Fredrik Gustafsson501770e2011-08-15 23:17:47 +0200137 gitdir=$(git rev-parse --git-dir)
138 gitdir_base="$gitdir/modules/$base_path"
139 gitdir="$gitdir/modules/$path"
140
141 case $gitdir in
142 /*)
143 a="$(cd_to_toplevel && pwd)/"
144 b=$gitdir
145 while [ "$b" ] && [ "${a%%/*}" = "${b%%/*}" ]
146 do
147 a=${a#*/} b=${b#*/};
148 done
149
150 rel="$a$name"
151 rel=`echo $rel | sed -e 's|[^/]*|..|g'`
152 rel_gitdir="$rel/$b"
153 ;;
154 *)
155 rel=`echo $name | sed -e 's|[^/]*|..|g'`
156 rel_gitdir="$rel/$gitdir"
157 ;;
158 esac
159
160 if test -d "$gitdir"
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300161 then
Fredrik Gustafsson501770e2011-08-15 23:17:47 +0200162 mkdir -p "$path"
Fredrik Gustafsson501770e2011-08-15 23:17:47 +0200163 rm -f "$gitdir/index"
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300164 else
Fredrik Gustafsson501770e2011-08-15 23:17:47 +0200165 mkdir -p "$gitdir_base"
Jens Lehmannea115a02012-03-04 22:14:30 +0100166 git clone $quiet -n ${reference:+"$reference"} \
167 --separate-git-dir "$gitdir" "$url" "$path" ||
Fredrik Gustafsson501770e2011-08-15 23:17:47 +0200168 die "$(eval_gettext "Clone of '\$url' into submodule path '\$path' failed")"
169 fi
Jens Lehmannea115a02012-03-04 22:14:30 +0100170
171 echo "gitdir: $rel_gitdir" >"$path/.git"
Lars Hjemli33aa6ff2007-06-06 11:13:01 +0200172}
173
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200174#
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200175# Add a new submodule to the working tree, .gitmodules and the index
176#
Mark Levedahlec05df32008-07-09 21:05:40 -0400177# $@ = repo path
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200178#
179# optional branch is stored in global branch variable
180#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800181cmd_add()
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200182{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800183 # parse $args after "submodule ... add".
184 while test $# -ne 0
185 do
186 case "$1" in
187 -b | --branch)
188 case "$2" in '') usage ;; esac
189 branch=$2
190 shift
191 ;;
Jens Lehmannd27b8762010-07-17 17:11:43 +0200192 -f | --force)
193 force=$1
194 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800195 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700196 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800197 ;;
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300198 --reference)
199 case "$2" in '') usage ;; esac
200 reference="--reference=$2"
201 shift
202 ;;
203 --reference=*)
204 reference="$1"
205 shift
206 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800207 --)
208 shift
209 break
210 ;;
211 -*)
212 usage
213 ;;
214 *)
215 break
216 ;;
217 esac
218 shift
219 done
220
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200221 repo=$1
222 path=$2
223
Jens Lehmann1414e572009-09-22 17:10:12 +0200224 if test -z "$path"; then
225 path=$(echo "$repo" |
226 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
227 fi
228
Mark Levedahlec05df32008-07-09 21:05:40 -0400229 if test -z "$repo" -o -z "$path"; then
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200230 usage
231 fi
232
Mark Levedahlec05df32008-07-09 21:05:40 -0400233 # assure repo is absolute or relative to parent
234 case "$repo" in
235 ./*|../*)
236 # dereference source url relative to parent's url
237 realrepo=$(resolve_relative_url "$repo") || exit
238 ;;
239 *:*|/*)
240 # absolute url
241 realrepo=$repo
242 ;;
243 *)
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000244 die "$(eval_gettext "repo URL: '\$repo' must be absolute or begin with ./|../")"
Mark Levedahlec05df32008-07-09 21:05:40 -0400245 ;;
246 esac
247
Michael J Gruberdb75ada2009-03-03 16:08:21 +0100248 # normalize path:
249 # multiple //; leading ./; /./; /../; trailing /
250 path=$(printf '%s/\n' "$path" |
251 sed -e '
252 s|//*|/|g
253 s|^\(\./\)*||
254 s|/\./|/|g
255 :start
256 s|\([^/]*\)/\.\./||
257 tstart
258 s|/*$||
259 ')
Junio C Hamano5be60072007-07-02 22:52:14 -0700260 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000261 die "$(eval_gettext "'\$path' already exists in the index")"
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200262
Jens Lehmannd27b8762010-07-17 17:11:43 +0200263 if test -z "$force" && ! git add --dry-run --ignore-missing "$path" > /dev/null 2>&1
264 then
Jon Seymour6ff875c2011-08-07 21:58:17 +1000265 eval_gettextln "The following path is ignored by one of your .gitignore files:
Ævar Arnfjörð Bjarmason3a4c3ed2011-05-21 18:44:07 +0000266\$path
Jon Seymour6ff875c2011-08-07 21:58:17 +1000267Use -f if you really want to add it." >&2
Jens Lehmannd27b8762010-07-17 17:11:43 +0200268 exit 1
269 fi
270
Mark Levedahld4264ca2008-03-04 20:15:02 -0500271 # perhaps the path exists and is already a git repo, else clone it
272 if test -e "$path"
273 then
Mark Levedahle9656472008-07-07 22:36:40 -0400274 if test -d "$path"/.git -o -f "$path"/.git
Mark Levedahld4264ca2008-03-04 20:15:02 -0500275 then
Jon Seymour6ff875c2011-08-07 21:58:17 +1000276 eval_gettextln "Adding existing repo at '\$path' to the index"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500277 else
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000278 die "$(eval_gettext "'\$path' already exists and is not a valid git repo")"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500279 fi
Mark Levedahlc2f93912008-07-09 21:05:41 -0400280
Mark Levedahld4264ca2008-03-04 20:15:02 -0500281 else
Mark Levedahld4264ca2008-03-04 20:15:02 -0500282
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300283 module_clone "$path" "$realrepo" "$reference" || exit
Ben Jacksonea10b602009-04-18 20:42:07 -0700284 (
Giuseppe Bilotta74ae1412010-02-25 00:34:17 +0100285 clear_local_git_env
Ben Jacksonea10b602009-04-18 20:42:07 -0700286 cd "$path" &&
287 # ash fails to wordsplit ${branch:+-b "$branch"...}
288 case "$branch" in
289 '') git checkout -f -q ;;
Jonathan Nieder502dc5b2010-12-01 12:50:46 -0600290 ?*) git checkout -f -q -B "$branch" "origin/$branch" ;;
Ben Jacksonea10b602009-04-18 20:42:07 -0700291 esac
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000292 ) || die "$(eval_gettext "Unable to checkout submodule '\$path'")"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500293 fi
Junio C Hamanoc56dce32011-07-22 14:24:35 -0700294 git config submodule."$path".url "$realrepo"
Mark Levedahld4264ca2008-03-04 20:15:02 -0500295
Jens Lehmannd27b8762010-07-17 17:11:43 +0200296 git add $force "$path" ||
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000297 die "$(eval_gettext "Failed to add submodule '\$path'")"
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200298
Imran M Yousufa5099bb2008-05-15 13:42:58 +0600299 git config -f .gitmodules submodule."$path".path "$path" &&
300 git config -f .gitmodules submodule."$path".url "$repo" &&
Ævar Arnfjörð Bjarmason31991b02010-07-05 17:33:03 +0000301 git add --force .gitmodules ||
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000302 die "$(eval_gettext "Failed to register submodule '\$path'")"
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200303}
304
305#
Mark Levedahl19a31f92008-08-10 19:10:04 -0400306# Execute an arbitrary command sequence in each checked out
307# submodule
308#
309# $@ = command to execute
310#
311cmd_foreach()
312{
Johan Herland1d5bec82009-08-19 03:45:19 +0200313 # parse $args after "submodule ... foreach".
314 while test $# -ne 0
315 do
316 case "$1" in
317 -q|--quiet)
318 GIT_QUIET=1
319 ;;
Johan Herland15fc56a2009-08-19 03:45:22 +0200320 --recursive)
321 recursive=1
322 ;;
Johan Herland1d5bec82009-08-19 03:45:19 +0200323 -*)
324 usage
325 ;;
326 *)
327 break
328 ;;
329 esac
330 shift
331 done
332
Ævar Arnfjörð Bjarmasonf030c962010-05-21 16:10:10 +0000333 toplevel=$(pwd)
334
Brandon Casey4dca1aa2011-06-29 19:34:58 -0500335 # dup stdin so that it can be restored when running the external
336 # command in the subshell (and a recursive call to this function)
337 exec 3<&0
338
David Aguilara7b32692008-08-22 00:30:50 -0700339 module_list |
Mark Levedahl19a31f92008-08-10 19:10:04 -0400340 while read mode sha1 stage path
341 do
342 if test -e "$path"/.git
343 then
Ævar Arnfjörð Bjarmason490b6d52011-05-21 18:44:06 +0000344 say "$(eval_gettext "Entering '\$prefix\$path'")"
Johan Herland1e7f2aa2009-08-16 03:10:08 +0200345 name=$(module_name "$path")
Johan Herland15fc56a2009-08-19 03:45:22 +0200346 (
347 prefix="$prefix$path/"
Giuseppe Bilotta74ae1412010-02-25 00:34:17 +0100348 clear_local_git_env
Johan Herland15fc56a2009-08-19 03:45:22 +0200349 cd "$path" &&
350 eval "$@" &&
351 if test -n "$recursive"
352 then
353 cmd_foreach "--recursive" "$@"
354 fi
Brandon Casey4dca1aa2011-06-29 19:34:58 -0500355 ) <&3 3<&- ||
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000356 die "$(eval_gettext "Stopping at '\$path'; script returned non-zero status.")"
Mark Levedahl19a31f92008-08-10 19:10:04 -0400357 fi
358 done
359}
360
361#
Lars Hjemli211b7f12007-06-06 11:13:02 +0200362# Register submodules in .git/config
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200363#
364# $@ = requested paths (default to all)
365#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800366cmd_init()
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200367{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800368 # parse $args after "submodule ... init".
369 while test $# -ne 0
370 do
371 case "$1" in
372 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700373 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800374 ;;
375 --)
376 shift
377 break
378 ;;
379 -*)
380 usage
381 ;;
382 *)
383 break
384 ;;
385 esac
386 shift
387 done
388
David Aguilara7b32692008-08-22 00:30:50 -0700389 module_list "$@" |
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200390 while read mode sha1 stage path
391 do
Lars Hjemli211b7f12007-06-06 11:13:02 +0200392 # Skip already registered paths
Lars Hjemli941987a2007-06-11 21:12:24 +0200393 name=$(module_name "$path") || exit
Jens Lehmann2cd9de32011-06-26 01:26:02 +0200394 if test -z "$(git config "submodule.$name.url")"
395 then
396 url=$(git config -f .gitmodules submodule."$name".url)
397 test -z "$url" &&
Junio C Hamano0591c0a2011-07-19 09:45:37 -0700398 die "$(eval_gettext "No url found for submodule path '\$path' in .gitmodules")"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200399
Jens Lehmann2cd9de32011-06-26 01:26:02 +0200400 # Possibly a url relative to parent
401 case "$url" in
402 ./*|../*)
403 url=$(resolve_relative_url "$url") || exit
404 ;;
405 esac
406 git config submodule."$name".url "$url" ||
Junio C Hamano0591c0a2011-07-19 09:45:37 -0700407 die "$(eval_gettext "Failed to register url for submodule path '\$path'")"
Jens Lehmann2cd9de32011-06-26 01:26:02 +0200408 fi
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200409
Jens Lehmann2cd9de32011-06-26 01:26:02 +0200410 # Copy "update" setting when it is not set yet
Johan Herland32948422009-06-03 08:27:06 +0200411 upd="$(git config -f .gitmodules submodule."$name".update)"
412 test -z "$upd" ||
Jens Lehmann2cd9de32011-06-26 01:26:02 +0200413 test -n "$(git config submodule."$name".update)" ||
Johan Herland32948422009-06-03 08:27:06 +0200414 git config submodule."$name".update "$upd" ||
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000415 die "$(eval_gettext "Failed to register update mode for submodule path '\$path'")"
Peter Huttererca2cedb2009-04-24 09:06:38 +1000416
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000417 say "$(eval_gettext "Submodule '\$name' (\$url) registered for path '\$path'")"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200418 done
419}
420
421#
Lars Hjemli211b7f12007-06-06 11:13:02 +0200422# Update each submodule path to correct revision, using clone and checkout as needed
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200423#
424# $@ = requested paths (default to all)
425#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800426cmd_update()
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200427{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800428 # parse $args after "submodule ... update".
Kevin Ballard98dbe632010-11-02 23:26:25 -0700429 orig_flags=
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800430 while test $# -ne 0
431 do
432 case "$1" in
433 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700434 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800435 ;;
Johannes Schindelinbe4d2c82008-05-16 11:23:03 +0100436 -i|--init)
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300437 init=1
Johannes Schindelinbe4d2c82008-05-16 11:23:03 +0100438 ;;
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200439 -N|--no-fetch)
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200440 nofetch=1
441 ;;
Nicolas Morey-Chaisemartin9db31bd2011-04-01 11:42:03 +0200442 -f|--force)
443 force=$1
444 ;;
Peter Huttererca2cedb2009-04-24 09:06:38 +1000445 -r|--rebase)
Johan Herland32948422009-06-03 08:27:06 +0200446 update="rebase"
Peter Huttererca2cedb2009-04-24 09:06:38 +1000447 ;;
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300448 --reference)
449 case "$2" in '') usage ;; esac
450 reference="--reference=$2"
Kevin Ballard98dbe632010-11-02 23:26:25 -0700451 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
452 shift
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300453 ;;
454 --reference=*)
455 reference="$1"
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300456 ;;
Johan Herland42b49172009-06-03 00:59:12 +0200457 -m|--merge)
Johan Herland42b49172009-06-03 00:59:12 +0200458 update="merge"
459 ;;
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200460 --recursive)
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200461 recursive=1
462 ;;
Junio C Hamanoefc5fb62011-10-10 15:56:16 -0700463 --checkout)
464 update="checkout"
465 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800466 --)
467 shift
468 break
469 ;;
470 -*)
471 usage
472 ;;
473 *)
474 break
475 ;;
476 esac
Kevin Ballard98dbe632010-11-02 23:26:25 -0700477 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
478 shift
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800479 done
480
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300481 if test -n "$init"
482 then
483 cmd_init "--" "$@" || return
484 fi
485
Spencer E. Olson1b4735d2011-02-17 09:18:45 -0700486 cloned_modules=
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200487 module_list "$@" | {
488 err=
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200489 while read mode sha1 stage path
490 do
Nicolas Morey-Chaisemartin313ee0d2011-03-30 07:20:02 +0200491 if test "$stage" = U
492 then
493 echo >&2 "Skipping unmerged submodule $path"
494 continue
495 fi
Lars Hjemli941987a2007-06-11 21:12:24 +0200496 name=$(module_name "$path") || exit
Junio C Hamano5be60072007-07-02 22:52:14 -0700497 url=$(git config submodule."$name".url)
Junio C Hamanoefc5fb62011-10-10 15:56:16 -0700498 if ! test -z "$update"
499 then
500 update_module=$update
501 else
502 update_module=$(git config submodule."$name".update)
503 fi
504
505 if test "$update_module" = "none"
506 then
507 echo "Skipping submodule '$path'"
508 continue
509 fi
510
Lars Hjemli211b7f12007-06-06 11:13:02 +0200511 if test -z "$url"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200512 then
513 # Only mention uninitialized submodules when its
514 # path have been specified
515 test "$#" != "0" &&
Ævar Arnfjörð Bjarmason1c2ef662011-05-21 18:44:08 +0000516 say "$(eval_gettext "Submodule path '\$path' not initialized
517Maybe you want to use 'update --init'?")"
Lars Hjemli211b7f12007-06-06 11:13:02 +0200518 continue
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200519 fi
Lars Hjemli211b7f12007-06-06 11:13:02 +0200520
Lars Hjemliba88a1f2008-02-20 23:13:15 +0100521 if ! test -d "$path"/.git -o -f "$path"/.git
Lars Hjemli211b7f12007-06-06 11:13:02 +0200522 then
Michael S. Tsirkind92a3952009-05-04 22:30:01 +0300523 module_clone "$path" "$url" "$reference"|| exit
Spencer E. Olson1b4735d2011-02-17 09:18:45 -0700524 cloned_modules="$cloned_modules;$name"
Lars Hjemlibf2d8242007-06-11 21:12:22 +0200525 subsha1=
526 else
Giuseppe Bilotta74ae1412010-02-25 00:34:17 +0100527 subsha1=$(clear_local_git_env; cd "$path" &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700528 git rev-parse --verify HEAD) ||
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000529 die "$(eval_gettext "Unable to find current revision in submodule path '\$path'")"
Lars Hjemli211b7f12007-06-06 11:13:02 +0200530 fi
531
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200532 if test "$subsha1" != "$sha1"
533 then
Nicolas Morey-Chaisemartin9db31bd2011-04-01 11:42:03 +0200534 subforce=$force
535 # If we don't already have a -f flag and the submodule has never been checked out
536 if test -z "$subsha1" -a -z "$force"
Ping Yinb9b378a2008-09-26 23:33:23 +0800537 then
Nicolas Morey-Chaisemartin9db31bd2011-04-01 11:42:03 +0200538 subforce="-f"
Ping Yinb9b378a2008-09-26 23:33:23 +0800539 fi
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200540
541 if test -z "$nofetch"
542 then
Jens Lehmanne5f522d2011-03-06 23:13:36 +0100543 # Run fetch only if $sha1 isn't present or it
544 # is not reachable from a ref.
Giuseppe Bilotta74ae1412010-02-25 00:34:17 +0100545 (clear_local_git_env; cd "$path" &&
Brandon Caseyf5799e02011-05-26 13:52:04 -0700546 ( (rev=$(git rev-list -n 1 $sha1 --not --all 2>/dev/null) &&
Jens Lehmanne5f522d2011-03-06 23:13:36 +0100547 test -z "$rev") || git-fetch)) ||
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000548 die "$(eval_gettext "Unable to fetch in submodule path '\$path'")"
Fabian Franz31ca3ac2009-02-05 20:18:32 -0200549 fi
550
Spencer E. Olson1b4735d2011-02-17 09:18:45 -0700551 # Is this something we just cloned?
552 case ";$cloned_modules;" in
553 *";$name;"*)
554 # then there is no local change to integrate
555 update_module= ;;
556 esac
557
Junio C Hamano877449c2011-06-13 12:17:52 -0700558 must_die_on_failure=
Johan Herland32948422009-06-03 08:27:06 +0200559 case "$update_module" in
560 rebase)
561 command="git rebase"
Ævar Arnfjörð Bjarmasonee653c82011-05-21 18:44:02 +0000562 die_msg="$(eval_gettext "Unable to rebase '\$sha1' in submodule path '\$path'")"
563 say_msg="$(eval_gettext "Submodule path '\$path': rebased into '\$sha1'")"
Junio C Hamano877449c2011-06-13 12:17:52 -0700564 must_die_on_failure=yes
Johan Herland32948422009-06-03 08:27:06 +0200565 ;;
Johan Herland42b49172009-06-03 00:59:12 +0200566 merge)
567 command="git merge"
Ævar Arnfjörð Bjarmasonee653c82011-05-21 18:44:02 +0000568 die_msg="$(eval_gettext "Unable to merge '\$sha1' in submodule path '\$path'")"
569 say_msg="$(eval_gettext "Submodule path '\$path': merged in '\$sha1'")"
Junio C Hamano877449c2011-06-13 12:17:52 -0700570 must_die_on_failure=yes
Johan Herland42b49172009-06-03 00:59:12 +0200571 ;;
Johan Herland32948422009-06-03 08:27:06 +0200572 *)
Nicolas Morey-Chaisemartin9db31bd2011-04-01 11:42:03 +0200573 command="git checkout $subforce -q"
Ævar Arnfjörð Bjarmasonee653c82011-05-21 18:44:02 +0000574 die_msg="$(eval_gettext "Unable to checkout '\$sha1' in submodule path '\$path'")"
575 say_msg="$(eval_gettext "Submodule path '\$path': checked out '\$sha1'")"
Johan Herland32948422009-06-03 08:27:06 +0200576 ;;
577 esac
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200578
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200579 if (clear_local_git_env; cd "$path" && $command "$sha1")
580 then
Junio C Hamanoff968f02011-07-13 14:31:35 -0700581 say "$say_msg"
Junio C Hamano877449c2011-06-13 12:17:52 -0700582 elif test -n "$must_die_on_failure"
583 then
Junio C Hamanoff968f02011-07-13 14:31:35 -0700584 die_with_status 2 "$die_msg"
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200585 else
Junio C Hamanoff968f02011-07-13 14:31:35 -0700586 err="${err};$die_msg"
Junio C Hamano877449c2011-06-13 12:17:52 -0700587 continue
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200588 fi
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200589 fi
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200590
591 if test -n "$recursive"
592 then
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200593 (clear_local_git_env; cd "$path" && eval cmd_update "$orig_flags")
594 res=$?
595 if test $res -gt 0
596 then
Junio C Hamanoff968f02011-07-13 14:31:35 -0700597 die_msg="$(eval_gettext "Failed to recurse into submodule path '\$path'")"
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200598 if test $res -eq 1
599 then
Junio C Hamanoff968f02011-07-13 14:31:35 -0700600 err="${err};$die_msg"
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200601 continue
602 else
Junio C Hamanoff968f02011-07-13 14:31:35 -0700603 die_with_status $res "$die_msg"
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200604 fi
605 fi
Johan Herlandb13fd5c2009-08-19 03:45:23 +0200606 fi
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200607 done
Fredrik Gustafsson15ffb7c2011-06-13 19:15:26 +0200608
609 if test -n "$err"
610 then
611 OIFS=$IFS
612 IFS=';'
613 for e in $err
614 do
615 if test -n "$e"
616 then
617 echo >&2 "$e"
618 fi
619 done
620 IFS=$OIFS
621 exit 1
622 fi
623 }
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200624}
625
Emil Medvebffe71f2007-06-26 18:40:58 -0500626set_name_rev () {
627 revname=$( (
Giuseppe Bilotta74ae1412010-02-25 00:34:17 +0100628 clear_local_git_env
Emil Medvebffe71f2007-06-26 18:40:58 -0500629 cd "$1" && {
Junio C Hamano5be60072007-07-02 22:52:14 -0700630 git describe "$2" 2>/dev/null ||
631 git describe --tags "$2" 2>/dev/null ||
Mark Levedahlf669ac02008-04-14 22:48:06 -0400632 git describe --contains "$2" 2>/dev/null ||
633 git describe --all --always "$2"
Emil Medvebffe71f2007-06-26 18:40:58 -0500634 }
635 ) )
636 test -z "$revname" || revname=" ($revname)"
637}
Ping Yin28f9af52008-03-11 21:52:15 +0800638#
639# Show commit summary for submodules in index or working tree
640#
641# If '--cached' is given, show summary between index and given commit,
642# or between working tree and given commit
643#
644# $@ = [commit (default 'HEAD'),] requested paths (default all)
645#
646cmd_summary() {
Ping Yinf2dc06a2008-03-11 21:52:17 +0800647 summary_limit=-1
Ping Yind0f64dd2008-04-12 23:05:31 +0800648 for_status=
Jens Lehmann1c244f62009-08-13 21:32:50 +0200649 diff_cmd=diff-index
Emil Medvebffe71f2007-06-26 18:40:58 -0500650
Ping Yin28f9af52008-03-11 21:52:15 +0800651 # parse $args after "submodule ... summary".
652 while test $# -ne 0
653 do
654 case "$1" in
655 --cached)
656 cached="$1"
657 ;;
Jens Lehmann1c244f62009-08-13 21:32:50 +0200658 --files)
659 files="$1"
660 ;;
Ping Yind0f64dd2008-04-12 23:05:31 +0800661 --for-status)
662 for_status="$1"
663 ;;
Ping Yinf2dc06a2008-03-11 21:52:17 +0800664 -n|--summary-limit)
665 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
666 then
667 :
668 else
669 usage
670 fi
671 shift
672 ;;
Ping Yin28f9af52008-03-11 21:52:15 +0800673 --)
674 shift
675 break
676 ;;
677 -*)
678 usage
679 ;;
680 *)
681 break
682 ;;
683 esac
684 shift
685 done
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200686
Ping Yinf2dc06a2008-03-11 21:52:17 +0800687 test $summary_limit = 0 && return
688
Johan Herland3deea892010-02-16 11:21:14 +0100689 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
Ping Yin28f9af52008-03-11 21:52:15 +0800690 then
691 head=$rev
Jeff Kingcaa9c3c2010-03-03 14:19:09 -0800692 test $# = 0 || shift
Johan Herland3deea892010-02-16 11:21:14 +0100693 elif test -z "$1" -o "$1" = "HEAD"
694 then
Junio C Hamano14e940d2010-03-03 14:19:10 -0800695 # before the first commit: compare with an empty tree
696 head=$(git hash-object -w -t tree --stdin </dev/null)
Jens Lehmann2ea6c2c2010-03-09 15:55:32 +0100697 test -z "$1" || shift
Ping Yin28f9af52008-03-11 21:52:15 +0800698 else
Johan Herland3deea892010-02-16 11:21:14 +0100699 head="HEAD"
Ping Yin28f9af52008-03-11 21:52:15 +0800700 fi
701
Jens Lehmann1c244f62009-08-13 21:32:50 +0200702 if [ -n "$files" ]
703 then
704 test -n "$cached" &&
Ævar Arnfjörð Bjarmasonb9b9c222011-05-21 18:44:03 +0000705 die "$(gettext -- "--cached cannot be used with --files")"
Jens Lehmann1c244f62009-08-13 21:32:50 +0200706 diff_cmd=diff-files
707 head=
708 fi
709
Ping Yin28f9af52008-03-11 21:52:15 +0800710 cd_to_toplevel
711 # Get modified modules cared by user
Jens Lehmann18076502010-06-25 16:56:02 +0200712 modules=$(git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- "$@" |
Junio C Hamanoe1622bf2009-11-23 15:56:32 -0800713 sane_egrep '^:([0-7]* )?160000' |
Ping Yin28f9af52008-03-11 21:52:15 +0800714 while read mod_src mod_dst sha1_src sha1_dst status name
715 do
716 # Always show modules deleted or type-changed (blob<->module)
717 test $status = D -o $status = T && echo "$name" && continue
718 # Also show added or modified modules which are checked out
719 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
720 echo "$name"
721 done
722 )
Ping Yin1cb639e2008-03-11 21:52:16 +0800723
Ping Yind0f64dd2008-04-12 23:05:31 +0800724 test -z "$modules" && return
725
Jens Lehmann18076502010-06-25 16:56:02 +0200726 git $diff_cmd $cached --ignore-submodules=dirty --raw $head -- $modules |
Junio C Hamanoe1622bf2009-11-23 15:56:32 -0800727 sane_egrep '^:([0-7]* )?160000' |
Ping Yin1cb639e2008-03-11 21:52:16 +0800728 cut -c2- |
729 while read mod_src mod_dst sha1_src sha1_dst status name
730 do
731 if test -z "$cached" &&
732 test $sha1_dst = 0000000000000000000000000000000000000000
733 then
734 case "$mod_dst" in
735 160000)
736 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
737 ;;
738 100644 | 100755 | 120000)
739 sha1_dst=$(git hash-object $name)
740 ;;
741 000000)
742 ;; # removed
743 *)
744 # unexpected type
Jon Seymour6ff875c2011-08-07 21:58:17 +1000745 eval_gettextln "unexpected mode \$mod_dst" >&2
Ping Yin1cb639e2008-03-11 21:52:16 +0800746 continue ;;
747 esac
748 fi
749 missing_src=
750 missing_dst=
751
752 test $mod_src = 160000 &&
Miklos Vajna30353a42008-12-03 14:26:52 +0100753 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
Ping Yin1cb639e2008-03-11 21:52:16 +0800754 missing_src=t
755
756 test $mod_dst = 160000 &&
Miklos Vajna30353a42008-12-03 14:26:52 +0100757 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
Ping Yin1cb639e2008-03-11 21:52:16 +0800758 missing_dst=t
759
760 total_commits=
761 case "$missing_src,$missing_dst" in
762 t,)
Ævar Arnfjörð Bjarmasonf62f8212011-05-21 18:44:05 +0000763 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_src")"
Ping Yin1cb639e2008-03-11 21:52:16 +0800764 ;;
765 ,t)
Ævar Arnfjörð Bjarmasonf62f8212011-05-21 18:44:05 +0000766 errmsg="$(eval_gettext " Warn: \$name doesn't contain commit \$sha1_dst")"
Ping Yin1cb639e2008-03-11 21:52:16 +0800767 ;;
768 t,t)
Ævar Arnfjörð Bjarmasonf62f8212011-05-21 18:44:05 +0000769 errmsg="$(eval_gettext " Warn: \$name doesn't contain commits \$sha1_src and \$sha1_dst")"
Ping Yin1cb639e2008-03-11 21:52:16 +0800770 ;;
771 *)
772 errmsg=
773 total_commits=$(
774 if test $mod_src = 160000 -a $mod_dst = 160000
775 then
776 range="$sha1_src...$sha1_dst"
777 elif test $mod_src = 160000
778 then
779 range=$sha1_src
780 else
781 range=$sha1_dst
782 fi
783 GIT_DIR="$name/.git" \
Jeff Kingb0e621a2010-04-08 15:42:37 -0400784 git rev-list --first-parent $range -- | wc -l
Ping Yin1cb639e2008-03-11 21:52:16 +0800785 )
Johannes Sixteed35592008-03-12 09:30:01 +0100786 total_commits=" ($(($total_commits + 0)))"
Ping Yin1cb639e2008-03-11 21:52:16 +0800787 ;;
788 esac
789
790 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
791 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
792 if test $status = T
793 then
Ævar Arnfjörð Bjarmasonb3e73442011-05-21 18:44:09 +0000794 blob="$(gettext "blob")"
795 submodule="$(gettext "submodule")"
Ping Yin1cb639e2008-03-11 21:52:16 +0800796 if test $mod_dst = 160000
797 then
Ævar Arnfjörð Bjarmasonb3e73442011-05-21 18:44:09 +0000798 echo "* $name $sha1_abbr_src($blob)->$sha1_abbr_dst($submodule)$total_commits:"
Ping Yin1cb639e2008-03-11 21:52:16 +0800799 else
Ævar Arnfjörð Bjarmasonb3e73442011-05-21 18:44:09 +0000800 echo "* $name $sha1_abbr_src($submodule)->$sha1_abbr_dst($blob)$total_commits:"
Ping Yin1cb639e2008-03-11 21:52:16 +0800801 fi
802 else
803 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
804 fi
805 if test -n "$errmsg"
806 then
807 # Don't give error msg for modification whose dst is not submodule
808 # i.e. deleted or changed to blob
809 test $mod_dst = 160000 && echo "$errmsg"
810 else
811 if test $mod_src = 160000 -a $mod_dst = 160000
812 then
Ping Yinf2dc06a2008-03-11 21:52:17 +0800813 limit=
814 test $summary_limit -gt 0 && limit="-$summary_limit"
Ping Yin1cb639e2008-03-11 21:52:16 +0800815 GIT_DIR="$name/.git" \
Ping Yinf2dc06a2008-03-11 21:52:17 +0800816 git log $limit --pretty='format: %m %s' \
Ping Yin1cb639e2008-03-11 21:52:16 +0800817 --first-parent $sha1_src...$sha1_dst
818 elif test $mod_dst = 160000
819 then
820 GIT_DIR="$name/.git" \
821 git log --pretty='format: > %s' -1 $sha1_dst
822 else
823 GIT_DIR="$name/.git" \
824 git log --pretty='format: < %s' -1 $sha1_src
825 fi
826 echo
827 fi
828 echo
Ping Yind0f64dd2008-04-12 23:05:31 +0800829 done |
830 if test -n "$for_status"; then
Jens Lehmannf17a5d32010-01-17 20:42:31 +0100831 if [ -n "$files" ]; then
Jon Seymour6ff875c2011-08-07 21:58:17 +1000832 gettextln "# Submodules changed but not updated:"
Jens Lehmannf17a5d32010-01-17 20:42:31 +0100833 else
Jon Seymour6ff875c2011-08-07 21:58:17 +1000834 gettextln "# Submodule changes to be committed:"
Jens Lehmannf17a5d32010-01-17 20:42:31 +0100835 fi
Ping Yind0f64dd2008-04-12 23:05:31 +0800836 echo "#"
837 sed -e 's|^|# |' -e 's|^# $|#|'
838 else
839 cat
840 fi
Ping Yin28f9af52008-03-11 21:52:15 +0800841}
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200842#
Lars Hjemli941987a2007-06-11 21:12:24 +0200843# List all submodules, prefixed with:
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200844# - submodule not initialized
845# + different revision checked out
846#
847# If --cached was specified the revision in the index will be printed
848# instead of the currently checked out revision.
849#
850# $@ = requested paths (default to all)
851#
Junio C Hamano23a485e2008-01-15 02:35:49 -0800852cmd_status()
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200853{
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800854 # parse $args after "submodule ... status".
Kevin Ballard98dbe632010-11-02 23:26:25 -0700855 orig_flags=
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800856 while test $# -ne 0
857 do
858 case "$1" in
859 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700860 GIT_QUIET=1
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800861 ;;
862 --cached)
863 cached=1
864 ;;
Johan Herland64b19ff2009-08-19 03:45:24 +0200865 --recursive)
866 recursive=1
867 ;;
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800868 --)
869 shift
870 break
871 ;;
872 -*)
873 usage
874 ;;
875 *)
876 break
877 ;;
878 esac
Kevin Ballard98dbe632010-11-02 23:26:25 -0700879 orig_flags="$orig_flags $(git rev-parse --sq-quote "$1")"
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800880 shift
881 done
882
David Aguilara7b32692008-08-22 00:30:50 -0700883 module_list "$@" |
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200884 while read mode sha1 stage path
885 do
Lars Hjemli941987a2007-06-11 21:12:24 +0200886 name=$(module_name "$path") || exit
Junio C Hamano5be60072007-07-02 22:52:14 -0700887 url=$(git config submodule."$name".url)
Johan Herland64b19ff2009-08-19 03:45:24 +0200888 displaypath="$prefix$path"
Nicolas Morey-Chaisemartin313ee0d2011-03-30 07:20:02 +0200889 if test "$stage" = U
890 then
891 say "U$sha1 $displaypath"
892 continue
893 fi
Lars Hjemliba88a1f2008-02-20 23:13:15 +0100894 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200895 then
Johan Herland64b19ff2009-08-19 03:45:24 +0200896 say "-$sha1 $displaypath"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200897 continue;
898 fi
CJ van den Berg41c7c1b2007-07-04 18:22:14 +0200899 set_name_rev "$path" "$sha1"
Jens Lehmann18076502010-06-25 16:56:02 +0200900 if git diff-files --ignore-submodules=dirty --quiet -- "$path"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200901 then
Johan Herland64b19ff2009-08-19 03:45:24 +0200902 say " $sha1 $displaypath$revname"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200903 else
904 if test -z "$cached"
905 then
Giuseppe Bilotta74ae1412010-02-25 00:34:17 +0100906 sha1=$(clear_local_git_env; cd "$path" && git rev-parse --verify HEAD)
CJ van den Berg41c7c1b2007-07-04 18:22:14 +0200907 set_name_rev "$path" "$sha1"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200908 fi
Johan Herland64b19ff2009-08-19 03:45:24 +0200909 say "+$sha1 $displaypath$revname"
910 fi
911
912 if test -n "$recursive"
913 then
914 (
915 prefix="$displaypath/"
Giuseppe Bilotta74ae1412010-02-25 00:34:17 +0100916 clear_local_git_env
Johan Herland64b19ff2009-08-19 03:45:24 +0200917 cd "$path" &&
Kevin Ballarda7eff1a2010-11-02 23:26:24 -0700918 eval cmd_status "$orig_args"
Johan Herland64b19ff2009-08-19 03:45:24 +0200919 ) ||
Ævar Arnfjörð Bjarmason497ee872011-05-21 18:44:01 +0000920 die "$(eval_gettext "Failed to recurse into submodule path '\$path'")"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200921 fi
922 done
923}
David Aguilar2327f612008-08-24 12:43:37 -0700924#
925# Sync remote urls for submodules
926# This makes the value for remote.$remote.url match the value
927# specified in .gitmodules.
928#
929cmd_sync()
930{
931 while test $# -ne 0
932 do
933 case "$1" in
934 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700935 GIT_QUIET=1
David Aguilar2327f612008-08-24 12:43:37 -0700936 shift
937 ;;
938 --)
939 shift
940 break
941 ;;
942 -*)
943 usage
944 ;;
945 *)
946 break
947 ;;
948 esac
949 done
950 cd_to_toplevel
951 module_list "$@" |
952 while read mode sha1 stage path
953 do
954 name=$(module_name "$path")
955 url=$(git config -f .gitmodules --get submodule."$name".url)
Johan Herlandbaede9f2008-09-22 18:08:31 +0200956
957 # Possibly a url relative to parent
958 case "$url" in
959 ./*|../*)
960 url=$(resolve_relative_url "$url") || exit
961 ;;
962 esac
963
Junio C Hamanoccee6082011-06-25 22:41:25 +0200964 if git config "submodule.$name.url" >/dev/null 2>/dev/null
David Aguilar2327f612008-08-24 12:43:37 -0700965 then
Junio C Hamano0591c0a2011-07-19 09:45:37 -0700966 say "$(eval_gettext "Synchronizing submodule url for '\$name'")"
Junio C Hamanoccee6082011-06-25 22:41:25 +0200967 git config submodule."$name".url "$url"
968
969 if test -e "$path"/.git
970 then
971 (
972 clear_local_git_env
973 cd "$path"
974 remote=$(get_default_remote)
975 git config remote."$remote".url "$url"
976 )
977 fi
David Aguilar2327f612008-08-24 12:43:37 -0700978 fi
979 done
980}
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200981
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800982# This loop parses the command line arguments to find the
983# subcommand name to dispatch. Parsing of the subcommand specific
984# options are primarily done by the subcommand implementations.
985# Subcommand specific options such as --branch and --cached are
986# parsed here as well, for backward compatibility.
987
988while test $# != 0 && test -z "$command"
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200989do
990 case "$1" in
David Aguilar2327f612008-08-24 12:43:37 -0700991 add | foreach | init | update | status | summary | sync)
Junio C Hamano5c08dbb2008-01-15 02:48:45 -0800992 command=$1
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200993 ;;
994 -q|--quiet)
Stephen Boyd2e6a30e2009-06-16 15:33:00 -0700995 GIT_QUIET=1
Lars Hjemli70c7ac22007-05-26 15:56:40 +0200996 ;;
Sven Verdoolaegeecda0722007-06-24 23:06:07 +0200997 -b|--branch)
998 case "$2" in
999 '')
1000 usage
1001 ;;
1002 esac
1003 branch="$2"; shift
1004 ;;
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001005 --cached)
Ping Yin28f9af52008-03-11 21:52:15 +08001006 cached="$1"
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001007 ;;
1008 --)
1009 break
1010 ;;
1011 -*)
1012 usage
1013 ;;
1014 *)
1015 break
1016 ;;
1017 esac
1018 shift
1019done
1020
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001021# No command word defaults to "status"
1022test -n "$command" || command=status
Sven Verdoolaegeecda0722007-06-24 23:06:07 +02001023
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001024# "-b branch" is accepted only by "add"
1025if test -n "$branch" && test "$command" != add
1026then
Lars Hjemli70c7ac22007-05-26 15:56:40 +02001027 usage
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001028fi
1029
Ping Yin28f9af52008-03-11 21:52:15 +08001030# "--cached" is accepted only by "status" and "summary"
1031if test -n "$cached" && test "$command" != status -a "$command" != summary
Junio C Hamano5c08dbb2008-01-15 02:48:45 -08001032then
1033 usage
1034fi
1035
1036"cmd_$command" "$@"