Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 890 lines (828 sloc) 17.625 kb
70c7ac2 @hjemli Add git-submodule command
hjemli authored
1 #!/bin/sh
2 #
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
3 # git-submodules.sh: add, init, update or list git submodules
70c7ac2 @hjemli Add git-submodule command
hjemli authored
4 #
5 # Copyright (c) 2007 Lars Hjemli
6
1d5bec8 @jherland git submodule: Cleanup usage string and add option parsing to cmd_for…
jherland authored
7 dashless=$(basename "$0" | sed -e 's/-/ /')
1414e57 @jlehmann git submodule add: make the <path> parameter optional
jlehmann authored
8 USAGE="[--quiet] add [-b branch] [--reference <repository>] [--] <repository> [<path>]
64b19ff @jherland git submodule status: Add --recursive to recurse into nested submodules
jherland authored
9 or: $dashless [--quiet] status [--cached] [--recursive] [--] [<path>...]
1d5bec8 @jherland git submodule: Cleanup usage string and add option parsing to cmd_for…
jherland authored
10 or: $dashless [--quiet] init [--] [<path>...]
b13fd5c @jherland git submodule update: Introduce --recursive to update nested submodules
jherland authored
11 or: $dashless [--quiet] update [--init] [-N|--no-fetch] [--rebase] [--reference <repository>] [--merge] [--recursive] [--] [<path>...]
adc5423 @gitster Merge branch 'jh/submodule-foreach'
gitster authored
12 or: $dashless [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
15fc56a @jherland git submodule foreach: Add --recursive to recurse into nested submodules
jherland authored
13 or: $dashless [--quiet] foreach [--recursive] <command>
1d5bec8 @jherland git submodule: Cleanup usage string and add option parsing to cmd_for…
jherland authored
14 or: $dashless [--quiet] sync [--] [<path>...]"
8f321a3 @gitster scripts: Add placeholders for OPTIONS_SPEC
gitster authored
15 OPTIONS_SPEC=
70c7ac2 @hjemli Add git-submodule command
hjemli authored
16 . git-sh-setup
98fcf84 @mark987 git-submodule - Use "get_default_remote" from git-parse-remote
mark987 authored
17 . git-parse-remote
70c7ac2 @hjemli Add git-submodule command
hjemli authored
18 require_work_tree
19
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
20 command=
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
21 branch=
d92a395 @mstsirkin Add --reference option to git submodule.
mstsirkin authored
22 reference=
70c7ac2 @hjemli Add git-submodule command
hjemli authored
23 cached=
1c244f6 @jlehmann git submodule summary: add --files option
jlehmann authored
24 files=
31ca3ac @Fabianx submodule: add --no-fetch parameter to update command
Fabianx authored
25 nofetch=
3294842 @jherland Rename submodule.<name>.rebase to submodule.<name>.update
jherland authored
26 update=
15fc56a @jherland git submodule foreach: Add --recursive to recurse into nested submodules
jherland authored
27 prefix=
70c7ac2 @hjemli Add git-submodule command
hjemli authored
28
f31a522 @mark987 git-submodule - allow a relative path as the subproject url
mark987 authored
29 # Resolve relative url by appending to parent's url
30 resolve_relative_url ()
31 {
98fcf84 @mark987 git-submodule - Use "get_default_remote" from git-parse-remote
mark987 authored
32 remote=$(get_default_remote)
8e7e6f3 @mark987 git-submodule - Fix errors regarding resolve_relative_url
mark987 authored
33 remoteurl=$(git config "remote.$remote.url") ||
34 die "remote ($remote) does not have a url defined in .git/config"
f31a522 @mark987 git-submodule - allow a relative path as the subproject url
mark987 authored
35 url="$1"
99b120a @mark987 git-submodule.sh - Remove trailing / from URL if found
mark987 authored
36 remoteurl=${remoteurl%/}
f31a522 @mark987 git-submodule - allow a relative path as the subproject url
mark987 authored
37 while test -n "$url"
38 do
39 case "$url" in
40 ../*)
41 url="${url#../}"
42 remoteurl="${remoteurl%/*}"
43 ;;
44 ./*)
45 url="${url#./}"
46 ;;
47 *)
48 break;;
49 esac
50 done
99b120a @mark987 git-submodule.sh - Remove trailing / from URL if found
mark987 authored
51 echo "$remoteurl/${url%/}"
f31a522 @mark987 git-submodule - allow a relative path as the subproject url
mark987 authored
52 }
53
941987a @hjemli git-submodule: give submodules proper names
hjemli authored
54 #
a7b3269 @davvid git-submodule: replace duplicated code with a module_list function
davvid authored
55 # Get submodule info for registered submodules
56 # $@ = path to limit submodule list
57 #
58 module_list()
59 {
e1622bf @gitster Protect scripted Porcelains from GREP_OPTIONS insanity
gitster authored
60 git ls-files --error-unmatch --stage -- "$@" | sane_grep '^160000 '
a7b3269 @davvid git-submodule: replace duplicated code with a module_list function
davvid authored
61 }
62
63 #
941987a @hjemli git-submodule: give submodules proper names
hjemli authored
64 # Map submodule path to submodule name
65 #
66 # $1 = path
67 #
68 module_name()
69 {
537601a @gitster git-submodule module_name: avoid using unwieldy "value_regexp" feature.
gitster authored
70 # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
fe22e54 Improve sed portability
Chris Ridd authored
71 re=$(printf '%s\n' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
a5099bb @imyousuf Use '-f' option to point to the .gitmodules file
imyousuf authored
72 name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
537601a @gitster git-submodule module_name: avoid using unwieldy "value_regexp" feature.
gitster authored
73 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
941987a @hjemli git-submodule: give submodules proper names
hjemli authored
74 test -z "$name" &&
75 die "No submodule mapping found in .gitmodules for path '$path'"
76 echo "$name"
77 }
33aa6ff @hjemli git-submodule: move cloning into a separate function
hjemli authored
78
79 #
80 # Clone a submodule
81 #
23a485e @gitster git-submodule: rename shell functions for consistency
gitster authored
82 # Prior to calling, cmd_update checks that a possibly existing
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
83 # path is not a git repository.
23a485e @gitster git-submodule: rename shell functions for consistency
gitster authored
84 # Likewise, cmd_add checks that path does not exist at all,
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
85 # since it is the location of a new submodule.
86 #
33aa6ff @hjemli git-submodule: move cloning into a separate function
hjemli authored
87 module_clone()
88 {
89 path=$1
90 url=$2
d92a395 @mstsirkin Add --reference option to git submodule.
mstsirkin authored
91 reference="$3"
33aa6ff @hjemli git-submodule: move cloning into a separate function
hjemli authored
92
93 # If there already is a directory at the submodule path,
94 # expect it to be empty (since that is the default checkout
95 # action) and try to remove it.
96 # Note: if $path is a symlink to a directory the test will
97 # succeed but the rmdir will fail. We might want to fix this.
98 if test -d "$path"
99 then
100 rmdir "$path" 2>/dev/null ||
0484682 typo fix: Directory `...' exist, ...: s/exist/exists/
Jim Meyering authored
101 die "Directory '$path' exists, but is neither empty nor a git repository"
33aa6ff @hjemli git-submodule: move cloning into a separate function
hjemli authored
102 fi
103
104 test -e "$path" &&
105 die "A file already exist at path '$path'"
106
d92a395 @mstsirkin Add --reference option to git submodule.
mstsirkin authored
107 if test -n "$reference"
108 then
109 git-clone "$reference" -n "$url" "$path"
110 else
111 git-clone -n "$url" "$path"
112 fi ||
941987a @hjemli git-submodule: give submodules proper names
hjemli authored
113 die "Clone of '$url' into submodule path '$path' failed"
33aa6ff @hjemli git-submodule: move cloning into a separate function
hjemli authored
114 }
115
70c7ac2 @hjemli Add git-submodule command
hjemli authored
116 #
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
117 # Add a new submodule to the working tree, .gitmodules and the index
118 #
ec05df3 @mark987 git-submodule - make "submodule add" more strict, and document it
mark987 authored
119 # $@ = repo path
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
120 #
121 # optional branch is stored in global branch variable
122 #
23a485e @gitster git-submodule: rename shell functions for consistency
gitster authored
123 cmd_add()
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
124 {
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
125 # parse $args after "submodule ... add".
126 while test $# -ne 0
127 do
128 case "$1" in
129 -b | --branch)
130 case "$2" in '') usage ;; esac
131 branch=$2
132 shift
133 ;;
134 -q|--quiet)
2e6a30e @bebarino submodule, repack: migrate to git-sh-setup's say()
bebarino authored
135 GIT_QUIET=1
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
136 ;;
d92a395 @mstsirkin Add --reference option to git submodule.
mstsirkin authored
137 --reference)
138 case "$2" in '') usage ;; esac
139 reference="--reference=$2"
140 shift
141 ;;
142 --reference=*)
143 reference="$1"
144 shift
145 ;;
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
146 --)
147 shift
148 break
149 ;;
150 -*)
151 usage
152 ;;
153 *)
154 break
155 ;;
156 esac
157 shift
158 done
159
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
160 repo=$1
161 path=$2
162
1414e57 @jlehmann git submodule add: make the <path> parameter optional
jlehmann authored
163 if test -z "$path"; then
164 path=$(echo "$repo" |
165 sed -e 's|/$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
166 fi
167
ec05df3 @mark987 git-submodule - make "submodule add" more strict, and document it
mark987 authored
168 if test -z "$repo" -o -z "$path"; then
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
169 usage
170 fi
171
ec05df3 @mark987 git-submodule - make "submodule add" more strict, and document it
mark987 authored
172 # assure repo is absolute or relative to parent
173 case "$repo" in
174 ./*|../*)
175 # dereference source url relative to parent's url
176 realrepo=$(resolve_relative_url "$repo") || exit
177 ;;
178 *:*|/*)
179 # absolute url
180 realrepo=$repo
181 ;;
182 *)
183 die "repo URL: '$repo' must be absolute or begin with ./|../"
184 ;;
185 esac
186
db75ada @mjg git submodule: Fix adding of submodules at paths with ./, .. and //
mjg authored
187 # normalize path:
188 # multiple //; leading ./; /./; /../; trailing /
189 path=$(printf '%s/\n' "$path" |
190 sed -e '
191 s|//*|/|g
192 s|^\(\./\)*||
193 s|/\./|/|g
194 :start
195 s|\([^/]*\)/\.\./||
196 tstart
197 s|/*$||
198 ')
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
gitster authored
199 git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
200 die "'$path' already exists in the index"
201
d4264ca @mark987 git-submodule - Allow adding a submodule in-place
mark987 authored
202 # perhaps the path exists and is already a git repo, else clone it
203 if test -e "$path"
204 then
e965647 @mark987 git-submodule - Fix bugs in adding an existing repo as a module
mark987 authored
205 if test -d "$path"/.git -o -f "$path"/.git
d4264ca @mark987 git-submodule - Allow adding a submodule in-place
mark987 authored
206 then
207 echo "Adding existing repo at '$path' to the index"
208 else
209 die "'$path' already exists and is not a valid git repo"
210 fi
c2f9391 @mark987 git-submodule - register submodule URL if adding in place
mark987 authored
211
212 case "$repo" in
213 ./*|../*)
214 url=$(resolve_relative_url "$repo") || exit
215 ;;
216 *)
217 url="$repo"
218 ;;
219 esac
220 git config submodule."$path".url "$url"
d4264ca @mark987 git-submodule - Allow adding a submodule in-place
mark987 authored
221 else
222
d92a395 @mstsirkin Add --reference option to git submodule.
mstsirkin authored
223 module_clone "$path" "$realrepo" "$reference" || exit
ea10b60 @bjj Work around ash "alternate value" expansion bug
bjj authored
224 (
225 unset GIT_DIR
226 cd "$path" &&
227 # ash fails to wordsplit ${branch:+-b "$branch"...}
228 case "$branch" in
229 '') git checkout -f -q ;;
230 ?*) git checkout -f -q -b "$branch" "origin/$branch" ;;
231 esac
232 ) || die "Unable to checkout submodule '$path'"
d4264ca @mark987 git-submodule - Allow adding a submodule in-place
mark987 authored
233 fi
234
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
235 git add "$path" ||
236 die "Failed to add submodule '$path'"
237
a5099bb @imyousuf Use '-f' option to point to the .gitmodules file
imyousuf authored
238 git config -f .gitmodules submodule."$path".path "$path" &&
239 git config -f .gitmodules submodule."$path".url "$repo" &&
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
240 git add .gitmodules ||
241 die "Failed to register submodule '$path'"
242 }
243
244 #
19a31f9 @mark987 git-submodule - Add 'foreach' subcommand
mark987 authored
245 # Execute an arbitrary command sequence in each checked out
246 # submodule
247 #
248 # $@ = command to execute
249 #
250 cmd_foreach()
251 {
1d5bec8 @jherland git submodule: Cleanup usage string and add option parsing to cmd_for…
jherland authored
252 # parse $args after "submodule ... foreach".
253 while test $# -ne 0
254 do
255 case "$1" in
256 -q|--quiet)
257 GIT_QUIET=1
258 ;;
15fc56a @jherland git submodule foreach: Add --recursive to recurse into nested submodules
jherland authored
259 --recursive)
260 recursive=1
261 ;;
1d5bec8 @jherland git submodule: Cleanup usage string and add option parsing to cmd_for…
jherland authored
262 -*)
263 usage
264 ;;
265 *)
266 break
267 ;;
268 esac
269 shift
270 done
271
a7b3269 @davvid git-submodule: replace duplicated code with a module_list function
davvid authored
272 module_list |
19a31f9 @mark987 git-submodule - Add 'foreach' subcommand
mark987 authored
273 while read mode sha1 stage path
274 do
275 if test -e "$path"/.git
276 then
15fc56a @jherland git submodule foreach: Add --recursive to recurse into nested submodules
jherland authored
277 say "Entering '$prefix$path'"
1e7f2aa @jherland git submodule foreach: Provide access to submodule name, as '$name'
jherland authored
278 name=$(module_name "$path")
15fc56a @jherland git submodule foreach: Add --recursive to recurse into nested submodules
jherland authored
279 (
280 prefix="$prefix$path/"
281 unset GIT_DIR
282 cd "$path" &&
283 eval "$@" &&
284 if test -n "$recursive"
285 then
286 cmd_foreach "--recursive" "$@"
287 fi
288 ) ||
19a31f9 @mark987 git-submodule - Add 'foreach' subcommand
mark987 authored
289 die "Stopping at '$path'; script returned non-zero status."
290 fi
291 done
292 }
293
294 #
211b7f1 @hjemli git-submodule: clone during update, not during init
hjemli authored
295 # Register submodules in .git/config
70c7ac2 @hjemli Add git-submodule command
hjemli authored
296 #
297 # $@ = requested paths (default to all)
298 #
23a485e @gitster git-submodule: rename shell functions for consistency
gitster authored
299 cmd_init()
70c7ac2 @hjemli Add git-submodule command
hjemli authored
300 {
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
301 # parse $args after "submodule ... init".
302 while test $# -ne 0
303 do
304 case "$1" in
305 -q|--quiet)
2e6a30e @bebarino submodule, repack: migrate to git-sh-setup's say()
bebarino authored
306 GIT_QUIET=1
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
307 ;;
308 --)
309 shift
310 break
311 ;;
312 -*)
313 usage
314 ;;
315 *)
316 break
317 ;;
318 esac
319 shift
320 done
321
a7b3269 @davvid git-submodule: replace duplicated code with a module_list function
davvid authored
322 module_list "$@" |
70c7ac2 @hjemli Add git-submodule command
hjemli authored
323 while read mode sha1 stage path
324 do
211b7f1 @hjemli git-submodule: clone during update, not during init
hjemli authored
325 # Skip already registered paths
941987a @hjemli git-submodule: give submodules proper names
hjemli authored
326 name=$(module_name "$path") || exit
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
gitster authored
327 url=$(git config submodule."$name".url)
211b7f1 @hjemli git-submodule: clone during update, not during init
hjemli authored
328 test -z "$url" || continue
70c7ac2 @hjemli Add git-submodule command
hjemli authored
329
a5099bb @imyousuf Use '-f' option to point to the .gitmodules file
imyousuf authored
330 url=$(git config -f .gitmodules submodule."$name".url)
70c7ac2 @hjemli Add git-submodule command
hjemli authored
331 test -z "$url" &&
941987a @hjemli git-submodule: give submodules proper names
hjemli authored
332 die "No url found for submodule path '$path' in .gitmodules"
70c7ac2 @hjemli Add git-submodule command
hjemli authored
333
f31a522 @mark987 git-submodule - allow a relative path as the subproject url
mark987 authored
334 # Possibly a url relative to parent
335 case "$url" in
336 ./*|../*)
8e7e6f3 @mark987 git-submodule - Fix errors regarding resolve_relative_url
mark987 authored
337 url=$(resolve_relative_url "$url") || exit
f31a522 @mark987 git-submodule - allow a relative path as the subproject url
mark987 authored
338 ;;
339 esac
340
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
gitster authored
341 git config submodule."$name".url "$url" ||
941987a @hjemli git-submodule: give submodules proper names
hjemli authored
342 die "Failed to register url for submodule path '$path'"
70c7ac2 @hjemli Add git-submodule command
hjemli authored
343
3294842 @jherland Rename submodule.<name>.rebase to submodule.<name>.update
jherland authored
344 upd="$(git config -f .gitmodules submodule."$name".update)"
345 test -z "$upd" ||
346 git config submodule."$name".update "$upd" ||
347 die "Failed to register update mode for submodule path '$path'"
ca2cedb @whot git-submodule: add support for --rebase.
whot authored
348
941987a @hjemli git-submodule: give submodules proper names
hjemli authored
349 say "Submodule '$name' ($url) registered for path '$path'"
70c7ac2 @hjemli Add git-submodule command
hjemli authored
350 done
351 }
352
353 #
211b7f1 @hjemli git-submodule: clone during update, not during init
hjemli authored
354 # Update each submodule path to correct revision, using clone and checkout as needed
70c7ac2 @hjemli Add git-submodule command
hjemli authored
355 #
356 # $@ = requested paths (default to all)
357 #
23a485e @gitster git-submodule: rename shell functions for consistency
gitster authored
358 cmd_update()
70c7ac2 @hjemli Add git-submodule command
hjemli authored
359 {
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
360 # parse $args after "submodule ... update".
b13fd5c @jherland git submodule update: Introduce --recursive to update nested submodules
jherland authored
361 orig_args="$@"
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
362 while test $# -ne 0
363 do
364 case "$1" in
365 -q|--quiet)
d1f63a3 @MadCoder git-submodule: move ill placed shift.
MadCoder authored
366 shift
2e6a30e @bebarino submodule, repack: migrate to git-sh-setup's say()
bebarino authored
367 GIT_QUIET=1
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
368 ;;
be4d2c8 @dscho submodule update: add convenience option --init
dscho authored
369 -i|--init)
d92a395 @mstsirkin Add --reference option to git submodule.
mstsirkin authored
370 init=1
be4d2c8 @dscho submodule update: add convenience option --init
dscho authored
371 shift
372 ;;
31ca3ac @Fabianx submodule: add --no-fetch parameter to update command
Fabianx authored
373 -N|--no-fetch)
374 shift
375 nofetch=1
376 ;;
ca2cedb @whot git-submodule: add support for --rebase.
whot authored
377 -r|--rebase)
378 shift
3294842 @jherland Rename submodule.<name>.rebase to submodule.<name>.update
jherland authored
379 update="rebase"
ca2cedb @whot git-submodule: add support for --rebase.
whot authored
380 ;;
d92a395 @mstsirkin Add --reference option to git submodule.
mstsirkin authored
381 --reference)
382 case "$2" in '') usage ;; esac
383 reference="--reference=$2"
384 shift 2
385 ;;
386 --reference=*)
387 reference="$1"
388 shift
389 ;;
42b4917 @jherland git-submodule: add support for --merge.
jherland authored
390 -m|--merge)
391 shift
392 update="merge"
393 ;;
b13fd5c @jherland git submodule update: Introduce --recursive to update nested submodules
jherland authored
394 --recursive)
395 shift
396 recursive=1
397 ;;
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
398 --)
399 shift
400 break
401 ;;
402 -*)
403 usage
404 ;;
405 *)
406 break
407 ;;
408 esac
409 done
410
d92a395 @mstsirkin Add --reference option to git submodule.
mstsirkin authored
411 if test -n "$init"
412 then
413 cmd_init "--" "$@" || return
414 fi
415
a7b3269 @davvid git-submodule: replace duplicated code with a module_list function
davvid authored
416 module_list "$@" |
70c7ac2 @hjemli Add git-submodule command
hjemli authored
417 while read mode sha1 stage path
418 do
941987a @hjemli git-submodule: give submodules proper names
hjemli authored
419 name=$(module_name "$path") || exit
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
gitster authored
420 url=$(git config submodule."$name".url)
3294842 @jherland Rename submodule.<name>.rebase to submodule.<name>.update
jherland authored
421 update_module=$(git config submodule."$name".update)
211b7f1 @hjemli git-submodule: clone during update, not during init
hjemli authored
422 if test -z "$url"
70c7ac2 @hjemli Add git-submodule command
hjemli authored
423 then
424 # Only mention uninitialized submodules when its
425 # path have been specified
426 test "$#" != "0" &&
989206f @julliard git-submodule: Avoid printing a spurious message.
julliard authored
427 say "Submodule path '$path' not initialized" &&
be4d2c8 @dscho submodule update: add convenience option --init
dscho authored
428 say "Maybe you want to use 'update --init'?"
211b7f1 @hjemli git-submodule: clone during update, not during init
hjemli authored
429 continue
430 fi
431
ba88a1f @hjemli Teach git-submodule.sh about the .git file
hjemli authored
432 if ! test -d "$path"/.git -o -f "$path"/.git
211b7f1 @hjemli git-submodule: clone during update, not during init
hjemli authored
433 then
d92a395 @mstsirkin Add --reference option to git submodule.
mstsirkin authored
434 module_clone "$path" "$url" "$reference"|| exit
bf2d824 @hjemli git-submodule: remember to checkout after clone
hjemli authored
435 subsha1=
436 else
5188408 @Tux Do not rely on the exit status of "unset" for unset variables
Tux authored
437 subsha1=$(unset GIT_DIR; cd "$path" &&
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
gitster authored
438 git rev-parse --verify HEAD) ||
941987a @hjemli git-submodule: give submodules proper names
hjemli authored
439 die "Unable to find current revision in submodule path '$path'"
70c7ac2 @hjemli Add git-submodule command
hjemli authored
440 fi
211b7f1 @hjemli git-submodule: clone during update, not during init
hjemli authored
441
3294842 @jherland Rename submodule.<name>.rebase to submodule.<name>.update
jherland authored
442 if ! test -z "$update"
ca2cedb @whot git-submodule: add support for --rebase.
whot authored
443 then
3294842 @jherland Rename submodule.<name>.rebase to submodule.<name>.update
jherland authored
444 update_module=$update
ca2cedb @whot git-submodule: add support for --rebase.
whot authored
445 fi
446
70c7ac2 @hjemli Add git-submodule command
hjemli authored
447 if test "$subsha1" != "$sha1"
448 then
b9b378a @pkufranky git-submodule: Fix "Unable to checkout" for the initial 'update'
pkufranky authored
449 force=
450 if test -z "$subsha1"
451 then
452 force="-f"
453 fi
31ca3ac @Fabianx submodule: add --no-fetch parameter to update command
Fabianx authored
454
455 if test -z "$nofetch"
456 then
457 (unset GIT_DIR; cd "$path" &&
458 git-fetch) ||
459 die "Unable to fetch in submodule path '$path'"
460 fi
461
3294842 @jherland Rename submodule.<name>.rebase to submodule.<name>.update
jherland authored
462 case "$update_module" in
463 rebase)
464 command="git rebase"
ca2cedb @whot git-submodule: add support for --rebase.
whot authored
465 action="rebase"
466 msg="rebased onto"
3294842 @jherland Rename submodule.<name>.rebase to submodule.<name>.update
jherland authored
467 ;;
42b4917 @jherland git-submodule: add support for --merge.
jherland authored
468 merge)
469 command="git merge"
470 action="merge"
471 msg="merged in"
472 ;;
3294842 @jherland Rename submodule.<name>.rebase to submodule.<name>.update
jherland authored
473 *)
474 command="git checkout $force -q"
ca2cedb @whot git-submodule: add support for --rebase.
whot authored
475 action="checkout"
476 msg="checked out"
3294842 @jherland Rename submodule.<name>.rebase to submodule.<name>.update
jherland authored
477 ;;
478 esac
70c7ac2 @hjemli Add git-submodule command
hjemli authored
479
ca2cedb @whot git-submodule: add support for --rebase.
whot authored
480 (unset GIT_DIR; cd "$path" && $command "$sha1") ||
481 die "Unable to $action '$sha1' in submodule path '$path'"
482 say "Submodule path '$path': $msg '$sha1'"
70c7ac2 @hjemli Add git-submodule command
hjemli authored
483 fi
b13fd5c @jherland git submodule update: Introduce --recursive to update nested submodules
jherland authored
484
485 if test -n "$recursive"
486 then
487 (unset GIT_DIR; cd "$path" && cmd_update $orig_args) ||
488 die "Failed to recurse into submodule path '$path'"
489 fi
70c7ac2 @hjemli Add git-submodule command
hjemli authored
490 done
491 }
492
bffe71f git-submodule: Instead of using only annotated tags, use any tags.
Emil Medve authored
493 set_name_rev () {
494 revname=$( (
5188408 @Tux Do not rely on the exit status of "unset" for unset variables
Tux authored
495 unset GIT_DIR
bffe71f git-submodule: Instead of using only annotated tags, use any tags.
Emil Medve authored
496 cd "$1" && {
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
gitster authored
497 git describe "$2" 2>/dev/null ||
498 git describe --tags "$2" 2>/dev/null ||
f669ac0 @mark987 git-submodule - possibly use branch name to describe a module
mark987 authored
499 git describe --contains "$2" 2>/dev/null ||
500 git describe --all --always "$2"
bffe71f git-submodule: Instead of using only annotated tags, use any tags.
Emil Medve authored
501 }
502 ) )
503 test -z "$revname" || revname=" ($revname)"
504 }
28f9af5 @pkufranky git-submodule summary: code framework
pkufranky authored
505 #
506 # Show commit summary for submodules in index or working tree
507 #
508 # If '--cached' is given, show summary between index and given commit,
509 # or between working tree and given commit
510 #
511 # $@ = [commit (default 'HEAD'),] requested paths (default all)
512 #
513 cmd_summary() {
f2dc06a @pkufranky git-submodule summary: limit summary size
pkufranky authored
514 summary_limit=-1
d0f64dd @pkufranky git-submodule summary: --for-status option
pkufranky authored
515 for_status=
1c244f6 @jlehmann git submodule summary: add --files option
jlehmann authored
516 diff_cmd=diff-index
f2dc06a @pkufranky git-submodule summary: limit summary size
pkufranky authored
517
28f9af5 @pkufranky git-submodule summary: code framework
pkufranky authored
518 # parse $args after "submodule ... summary".
519 while test $# -ne 0
520 do
521 case "$1" in
522 --cached)
523 cached="$1"
524 ;;
1c244f6 @jlehmann git submodule summary: add --files option
jlehmann authored
525 --files)
526 files="$1"
527 ;;
d0f64dd @pkufranky git-submodule summary: --for-status option
pkufranky authored
528 --for-status)
529 for_status="$1"
530 ;;
f2dc06a @pkufranky git-submodule summary: limit summary size
pkufranky authored
531 -n|--summary-limit)
532 if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
533 then
534 :
535 else
536 usage
537 fi
538 shift
539 ;;
28f9af5 @pkufranky git-submodule summary: code framework
pkufranky authored
540 --)
541 shift
542 break
543 ;;
544 -*)
545 usage
546 ;;
547 *)
548 break
549 ;;
550 esac
551 shift
552 done
bffe71f git-submodule: Instead of using only annotated tags, use any tags.
Emil Medve authored
553
f2dc06a @pkufranky git-submodule summary: limit summary size
pkufranky authored
554 test $summary_limit = 0 && return
555
3deea89 @jherland submodule summary: Don't barf when invoked in an empty repo
jherland authored
556 if rev=$(git rev-parse -q --verify --default HEAD ${1+"$1"})
28f9af5 @pkufranky git-submodule summary: code framework
pkufranky authored
557 then
558 head=$rev
caa9c3c @peff submodule summary: do not shift a non-existent positional variable
peff authored
559 test $# = 0 || shift
3deea89 @jherland submodule summary: Don't barf when invoked in an empty repo
jherland authored
560 elif test -z "$1" -o "$1" = "HEAD"
561 then
14e940d @gitster submodule summary: do not fail before the first commit
gitster authored
562 # before the first commit: compare with an empty tree
563 head=$(git hash-object -w -t tree --stdin </dev/null)
28f9af5 @pkufranky git-submodule summary: code framework
pkufranky authored
564 else
3deea89 @jherland submodule summary: Don't barf when invoked in an empty repo
jherland authored
565 head="HEAD"
28f9af5 @pkufranky git-submodule summary: code framework
pkufranky authored
566 fi
567
1c244f6 @jlehmann git submodule summary: add --files option
jlehmann authored
568 if [ -n "$files" ]
569 then
570 test -n "$cached" &&
571 die "--cached cannot be used with --files"
572 diff_cmd=diff-files
573 head=
574 fi
575
28f9af5 @pkufranky git-submodule summary: code framework
pkufranky authored
576 cd_to_toplevel
577 # Get modified modules cared by user
1c244f6 @jlehmann git submodule summary: add --files option
jlehmann authored
578 modules=$(git $diff_cmd $cached --raw $head -- "$@" |
e1622bf @gitster Protect scripted Porcelains from GREP_OPTIONS insanity
gitster authored
579 sane_egrep '^:([0-7]* )?160000' |
28f9af5 @pkufranky git-submodule summary: code framework
pkufranky authored
580 while read mod_src mod_dst sha1_src sha1_dst status name
581 do
582 # Always show modules deleted or type-changed (blob<->module)
583 test $status = D -o $status = T && echo "$name" && continue
584 # Also show added or modified modules which are checked out
585 GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
586 echo "$name"
587 done
588 )
1cb639e @pkufranky git-submodule summary: show commit summary
pkufranky authored
589
d0f64dd @pkufranky git-submodule summary: --for-status option
pkufranky authored
590 test -z "$modules" && return
591
1c244f6 @jlehmann git submodule summary: add --files option
jlehmann authored
592 git $diff_cmd $cached --raw $head -- $modules |
e1622bf @gitster Protect scripted Porcelains from GREP_OPTIONS insanity
gitster authored
593 sane_egrep '^:([0-7]* )?160000' |
1cb639e @pkufranky git-submodule summary: show commit summary
pkufranky authored
594 cut -c2- |
595 while read mod_src mod_dst sha1_src sha1_dst status name
596 do
597 if test -z "$cached" &&
598 test $sha1_dst = 0000000000000000000000000000000000000000
599 then
600 case "$mod_dst" in
601 160000)
602 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
603 ;;
604 100644 | 100755 | 120000)
605 sha1_dst=$(git hash-object $name)
606 ;;
607 000000)
608 ;; # removed
609 *)
610 # unexpected type
611 echo >&2 "unexpected mode $mod_dst"
612 continue ;;
613 esac
614 fi
615 missing_src=
616 missing_dst=
617
618 test $mod_src = 160000 &&
30353a4 submodule: use git rev-parse -q
Miklos Vajna authored
619 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_src^0 >/dev/null &&
1cb639e @pkufranky git-submodule summary: show commit summary
pkufranky authored
620 missing_src=t
621
622 test $mod_dst = 160000 &&
30353a4 submodule: use git rev-parse -q
Miklos Vajna authored
623 ! GIT_DIR="$name/.git" git-rev-parse -q --verify $sha1_dst^0 >/dev/null &&
1cb639e @pkufranky git-submodule summary: show commit summary
pkufranky authored
624 missing_dst=t
bffe71f git-submodule: Instead of using only annotated tags, use any tags.
Emil Medve authored
625
1cb639e @pkufranky git-submodule summary: show commit summary
pkufranky authored
626 total_commits=
627 case "$missing_src,$missing_dst" in
628 t,)
629 errmsg=" Warn: $name doesn't contain commit $sha1_src"
630 ;;
631 ,t)
632 errmsg=" Warn: $name doesn't contain commit $sha1_dst"
633 ;;
634 t,t)
635 errmsg=" Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
636 ;;
637 *)
638 errmsg=
639 total_commits=$(
640 if test $mod_src = 160000 -a $mod_dst = 160000
641 then
642 range="$sha1_src...$sha1_dst"
643 elif test $mod_src = 160000
644 then
645 range=$sha1_src
646 else
647 range=$sha1_dst
648 fi
649 GIT_DIR="$name/.git" \
650 git log --pretty=oneline --first-parent $range | wc -l
651 )
eed3559 git-submodule summary: fix that some "wc" flavors produce leading spaces
Johannes Sixt authored
652 total_commits=" ($(($total_commits + 0)))"
1cb639e @pkufranky git-submodule summary: show commit summary
pkufranky authored
653 ;;
654 esac
655
656 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
657 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
658 if test $status = T
659 then
660 if test $mod_dst = 160000
661 then
662 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
663 else
664 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
665 fi
666 else
667 echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
668 fi
669 if test -n "$errmsg"
670 then
671 # Don't give error msg for modification whose dst is not submodule
672 # i.e. deleted or changed to blob
673 test $mod_dst = 160000 && echo "$errmsg"
674 else
675 if test $mod_src = 160000 -a $mod_dst = 160000
676 then
f2dc06a @pkufranky git-submodule summary: limit summary size
pkufranky authored
677 limit=
678 test $summary_limit -gt 0 && limit="-$summary_limit"
1cb639e @pkufranky git-submodule summary: show commit summary
pkufranky authored
679 GIT_DIR="$name/.git" \
f2dc06a @pkufranky git-submodule summary: limit summary size
pkufranky authored
680 git log $limit --pretty='format: %m %s' \
1cb639e @pkufranky git-submodule summary: show commit summary
pkufranky authored
681 --first-parent $sha1_src...$sha1_dst
682 elif test $mod_dst = 160000
683 then
684 GIT_DIR="$name/.git" \
685 git log --pretty='format: > %s' -1 $sha1_dst
686 else
687 GIT_DIR="$name/.git" \
688 git log --pretty='format: < %s' -1 $sha1_src
689 fi
690 echo
691 fi
692 echo
d0f64dd @pkufranky git-submodule summary: --for-status option
pkufranky authored
693 done |
694 if test -n "$for_status"; then
f17a5d3 @jlehmann git status: Show uncommitted submodule changes too when enabled
jlehmann authored
695 if [ -n "$files" ]; then
696 echo "# Submodules changed but not updated:"
697 else
698 echo "# Submodule changes to be committed:"
699 fi
d0f64dd @pkufranky git-submodule summary: --for-status option
pkufranky authored
700 echo "#"
701 sed -e 's|^|# |' -e 's|^# $|#|'
702 else
703 cat
704 fi
28f9af5 @pkufranky git-submodule summary: code framework
pkufranky authored
705 }
70c7ac2 @hjemli Add git-submodule command
hjemli authored
706 #
941987a @hjemli git-submodule: give submodules proper names
hjemli authored
707 # List all submodules, prefixed with:
70c7ac2 @hjemli Add git-submodule command
hjemli authored
708 # - submodule not initialized
709 # + different revision checked out
710 #
711 # If --cached was specified the revision in the index will be printed
712 # instead of the currently checked out revision.
713 #
714 # $@ = requested paths (default to all)
715 #
23a485e @gitster git-submodule: rename shell functions for consistency
gitster authored
716 cmd_status()
70c7ac2 @hjemli Add git-submodule command
hjemli authored
717 {
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
718 # parse $args after "submodule ... status".
64b19ff @jherland git submodule status: Add --recursive to recurse into nested submodules
jherland authored
719 orig_args="$@"
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
720 while test $# -ne 0
721 do
722 case "$1" in
723 -q|--quiet)
2e6a30e @bebarino submodule, repack: migrate to git-sh-setup's say()
bebarino authored
724 GIT_QUIET=1
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
725 ;;
726 --cached)
727 cached=1
728 ;;
64b19ff @jherland git submodule status: Add --recursive to recurse into nested submodules
jherland authored
729 --recursive)
730 recursive=1
731 ;;
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
732 --)
733 shift
734 break
735 ;;
736 -*)
737 usage
738 ;;
739 *)
740 break
741 ;;
742 esac
743 shift
744 done
745
a7b3269 @davvid git-submodule: replace duplicated code with a module_list function
davvid authored
746 module_list "$@" |
70c7ac2 @hjemli Add git-submodule command
hjemli authored
747 while read mode sha1 stage path
748 do
941987a @hjemli git-submodule: give submodules proper names
hjemli authored
749 name=$(module_name "$path") || exit
5be6007 @gitster Rewrite "git-frotz" to "git frotz"
gitster authored
750 url=$(git config submodule."$name".url)
64b19ff @jherland git submodule status: Add --recursive to recurse into nested submodules
jherland authored
751 displaypath="$prefix$path"
ba88a1f @hjemli Teach git-submodule.sh about the .git file
hjemli authored
752 if test -z "$url" || ! test -d "$path"/.git -o -f "$path"/.git
70c7ac2 @hjemli Add git-submodule command
hjemli authored
753 then
64b19ff @jherland git submodule status: Add --recursive to recurse into nested submodules
jherland authored
754 say "-$sha1 $displaypath"
70c7ac2 @hjemli Add git-submodule command
hjemli authored
755 continue;
756 fi
41c7c1b @neurocyte git-submodule: Fix two instances of the same typo
neurocyte authored
757 set_name_rev "$path" "$sha1"
70c7ac2 @hjemli Add git-submodule command
hjemli authored
758 if git diff-files --quiet -- "$path"
759 then
64b19ff @jherland git submodule status: Add --recursive to recurse into nested submodules
jherland authored
760 say " $sha1 $displaypath$revname"
70c7ac2 @hjemli Add git-submodule command
hjemli authored
761 else
762 if test -z "$cached"
763 then
5188408 @Tux Do not rely on the exit status of "unset" for unset variables
Tux authored
764 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
41c7c1b @neurocyte git-submodule: Fix two instances of the same typo
neurocyte authored
765 set_name_rev "$path" "$sha1"
70c7ac2 @hjemli Add git-submodule command
hjemli authored
766 fi
64b19ff @jherland git submodule status: Add --recursive to recurse into nested submodules
jherland authored
767 say "+$sha1 $displaypath$revname"
768 fi
769
770 if test -n "$recursive"
771 then
772 (
773 prefix="$displaypath/"
774 unset GIT_DIR
775 cd "$path" &&
776 cmd_status $orig_args
777 ) ||
778 die "Failed to recurse into submodule path '$path'"
70c7ac2 @hjemli Add git-submodule command
hjemli authored
779 fi
780 done
781 }
2327f61 @davvid git-submodule: add "sync" command
davvid authored
782 #
783 # Sync remote urls for submodules
784 # This makes the value for remote.$remote.url match the value
785 # specified in .gitmodules.
786 #
787 cmd_sync()
788 {
789 while test $# -ne 0
790 do
791 case "$1" in
792 -q|--quiet)
2e6a30e @bebarino submodule, repack: migrate to git-sh-setup's say()
bebarino authored
793 GIT_QUIET=1
2327f61 @davvid git-submodule: add "sync" command
davvid authored
794 shift
795 ;;
796 --)
797 shift
798 break
799 ;;
800 -*)
801 usage
802 ;;
803 *)
804 break
805 ;;
806 esac
807 done
808 cd_to_toplevel
809 module_list "$@" |
810 while read mode sha1 stage path
811 do
812 name=$(module_name "$path")
813 url=$(git config -f .gitmodules --get submodule."$name".url)
baede9f @jherland Fix submodule sync with relative submodule URLs
jherland authored
814
815 # Possibly a url relative to parent
816 case "$url" in
817 ./*|../*)
818 url=$(resolve_relative_url "$url") || exit
819 ;;
820 esac
821
2327f61 @davvid git-submodule: add "sync" command
davvid authored
822 if test -e "$path"/.git
823 then
824 (
825 unset GIT_DIR
826 cd "$path"
827 remote=$(get_default_remote)
828 say "Synchronizing submodule url for '$name'"
829 git config remote."$remote".url "$url"
830 )
831 fi
832 done
833 }
70c7ac2 @hjemli Add git-submodule command
hjemli authored
834
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
835 # This loop parses the command line arguments to find the
836 # subcommand name to dispatch. Parsing of the subcommand specific
837 # options are primarily done by the subcommand implementations.
838 # Subcommand specific options such as --branch and --cached are
839 # parsed here as well, for backward compatibility.
840
841 while test $# != 0 && test -z "$command"
70c7ac2 @hjemli Add git-submodule command
hjemli authored
842 do
843 case "$1" in
2327f61 @davvid git-submodule: add "sync" command
davvid authored
844 add | foreach | init | update | status | summary | sync)
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
845 command=$1
70c7ac2 @hjemli Add git-submodule command
hjemli authored
846 ;;
847 -q|--quiet)
2e6a30e @bebarino submodule, repack: migrate to git-sh-setup's say()
bebarino authored
848 GIT_QUIET=1
70c7ac2 @hjemli Add git-submodule command
hjemli authored
849 ;;
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
850 -b|--branch)
851 case "$2" in
852 '')
853 usage
854 ;;
855 esac
856 branch="$2"; shift
857 ;;
70c7ac2 @hjemli Add git-submodule command
hjemli authored
858 --cached)
28f9af5 @pkufranky git-submodule summary: code framework
pkufranky authored
859 cached="$1"
70c7ac2 @hjemli Add git-submodule command
hjemli authored
860 ;;
861 --)
862 break
863 ;;
864 -*)
865 usage
866 ;;
867 *)
868 break
869 ;;
870 esac
871 shift
872 done
873
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
874 # No command word defaults to "status"
875 test -n "$command" || command=status
876
877 # "-b branch" is accepted only by "add"
878 if test -n "$branch" && test "$command" != add
879 then
ecda072 git-submodule: provide easy way of adding new submodules
Sven Verdoolaege authored
880 usage
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
881 fi
882
28f9af5 @pkufranky git-submodule summary: code framework
pkufranky authored
883 # "--cached" is accepted only by "status" and "summary"
884 if test -n "$cached" && test "$command" != status -a "$command" != summary
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
885 then
70c7ac2 @hjemli Add git-submodule command
hjemli authored
886 usage
5c08dbb @gitster git-submodule: fix subcommand parser
gitster authored
887 fi
888
889 "cmd_$command" "$@"
Something went wrong with that request. Please try again.