fetch-pack: brown paper bag fix
[git] / git-submodule.sh
1 #!/bin/sh
2 #
3 # git-submodules.sh: add, init, update or list git submodules
4 #
5 # Copyright (c) 2007 Lars Hjemli
6
7 USAGE="[--quiet] [--cached] \
8 [add <repo> [-b branch]|status|init|update|summary [-n|--summary-limit <n>] [<commit>]] \
9 [--] [<path>...]"
10 OPTIONS_SPEC=
11 . git-sh-setup
12 require_work_tree
13
14 command=
15 branch=
16 quiet=
17 cached=
18
19 #
20 # print stuff on stdout unless -q was specified
21 #
22 say()
23 {
24         if test -z "$quiet"
25         then
26                 echo "$@"
27         fi
28 }
29
30 # NEEDSWORK: identical function exists in get_repo_base in clone.sh
31 get_repo_base() {
32         (
33                 cd "`/bin/pwd`" &&
34                 cd "$1" || cd "$1.git" &&
35                 {
36                         cd .git
37                         pwd
38                 }
39         ) 2>/dev/null
40 }
41
42 # Resolve relative url by appending to parent's url
43 resolve_relative_url ()
44 {
45         branch="$(git symbolic-ref HEAD 2>/dev/null)"
46         remote="$(git config branch.${branch#refs/heads/}.remote)"
47         remote="${remote:-origin}"
48         remoteurl="$(git config remote.$remote.url)" ||
49                 die "remote ($remote) does not have a url in .git/config"
50         url="$1"
51         while test -n "$url"
52         do
53                 case "$url" in
54                 ../*)
55                         url="${url#../}"
56                         remoteurl="${remoteurl%/*}"
57                         ;;
58                 ./*)
59                         url="${url#./}"
60                         ;;
61                 *)
62                         break;;
63                 esac
64         done
65         echo "$remoteurl/$url"
66 }
67
68 #
69 # Map submodule path to submodule name
70 #
71 # $1 = path
72 #
73 module_name()
74 {
75         # Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
76         re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
77         name=$( GIT_CONFIG=.gitmodules \
78                 git config --get-regexp '^submodule\..*\.path$' |
79                 sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
80        test -z "$name" &&
81        die "No submodule mapping found in .gitmodules for path '$path'"
82        echo "$name"
83 }
84
85 #
86 # Clone a submodule
87 #
88 # Prior to calling, cmd_update checks that a possibly existing
89 # path is not a git repository.
90 # Likewise, cmd_add checks that path does not exist at all,
91 # since it is the location of a new submodule.
92 #
93 module_clone()
94 {
95         path=$1
96         url=$2
97
98         # If there already is a directory at the submodule path,
99         # expect it to be empty (since that is the default checkout
100         # action) and try to remove it.
101         # Note: if $path is a symlink to a directory the test will
102         # succeed but the rmdir will fail. We might want to fix this.
103         if test -d "$path"
104         then
105                 rmdir "$path" 2>/dev/null ||
106                 die "Directory '$path' exist, but is neither empty nor a git repository"
107         fi
108
109         test -e "$path" &&
110         die "A file already exist at path '$path'"
111
112         git-clone -n "$url" "$path" ||
113         die "Clone of '$url' into submodule path '$path' failed"
114 }
115
116 #
117 # Add a new submodule to the working tree, .gitmodules and the index
118 #
119 # $@ = repo [path]
120 #
121 # optional branch is stored in global branch variable
122 #
123 cmd_add()
124 {
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)
135                         quiet=1
136                         ;;
137                 --)
138                         shift
139                         break
140                         ;;
141                 -*)
142                         usage
143                         ;;
144                 *)
145                         break
146                         ;;
147                 esac
148                 shift
149         done
150
151         repo=$1
152         path=$2
153
154         if test -z "$repo"; then
155                 usage
156         fi
157
158         # Guess path from repo if not specified or strip trailing slashes
159         if test -z "$path"; then
160                 path=$(echo "$repo" | sed -e 's|/*$||' -e 's|:*/*\.git$||' -e 's|.*[/:]||g')
161         else
162                 path=$(echo "$path" | sed -e 's|/*$||')
163         fi
164
165         git ls-files --error-unmatch "$path" > /dev/null 2>&1 &&
166         die "'$path' already exists in the index"
167
168         # perhaps the path exists and is already a git repo, else clone it
169         if test -e "$path"
170         then
171                 if test -d "$path/.git" &&
172                 test "$(unset GIT_DIR; cd $path; git rev-parse --git-dir)" = ".git"
173                 then
174                         echo "Adding existing repo at '$path' to the index"
175                 else
176                         die "'$path' already exists and is not a valid git repo"
177                 fi
178         else
179                 case "$repo" in
180                 ./*|../*)
181                         # dereference source url relative to parent's url
182                         realrepo="$(resolve_relative_url $repo)" ;;
183                 *)
184                         # Turn the source into an absolute path if
185                         # it is local
186                         if base=$(get_repo_base "$repo"); then
187                                 repo="$base"
188                         fi
189                         realrepo=$repo
190                         ;;
191                 esac
192
193                 module_clone "$path" "$realrepo" || exit
194                 (unset GIT_DIR; cd "$path" && git checkout -q ${branch:+-b "$branch" "origin/$branch"}) ||
195                 die "Unable to checkout submodule '$path'"
196         fi
197
198         git add "$path" ||
199         die "Failed to add submodule '$path'"
200
201         GIT_CONFIG=.gitmodules git config submodule."$path".path "$path" &&
202         GIT_CONFIG=.gitmodules git config submodule."$path".url "$repo" &&
203         git add .gitmodules ||
204         die "Failed to register submodule '$path'"
205 }
206
207 #
208 # Register submodules in .git/config
209 #
210 # $@ = requested paths (default to all)
211 #
212 cmd_init()
213 {
214         # parse $args after "submodule ... init".
215         while test $# -ne 0
216         do
217                 case "$1" in
218                 -q|--quiet)
219                         quiet=1
220                         ;;
221                 --)
222                         shift
223                         break
224                         ;;
225                 -*)
226                         usage
227                         ;;
228                 *)
229                         break
230                         ;;
231                 esac
232                 shift
233         done
234
235         git ls-files --stage -- "$@" | grep '^160000 ' |
236         while read mode sha1 stage path
237         do
238                 # Skip already registered paths
239                 name=$(module_name "$path") || exit
240                 url=$(git config submodule."$name".url)
241                 test -z "$url" || continue
242
243                 url=$(GIT_CONFIG=.gitmodules git config submodule."$name".url)
244                 test -z "$url" &&
245                 die "No url found for submodule path '$path' in .gitmodules"
246
247                 # Possibly a url relative to parent
248                 case "$url" in
249                 ./*|../*)
250                         url="$(resolve_relative_url "$url")"
251                         ;;
252                 esac
253
254                 git config submodule."$name".url "$url" ||
255                 die "Failed to register url for submodule path '$path'"
256
257                 say "Submodule '$name' ($url) registered for path '$path'"
258         done
259 }
260
261 #
262 # Update each submodule path to correct revision, using clone and checkout as needed
263 #
264 # $@ = requested paths (default to all)
265 #
266 cmd_update()
267 {
268         # parse $args after "submodule ... update".
269         while test $# -ne 0
270         do
271                 case "$1" in
272                 -q|--quiet)
273                         quiet=1
274                         ;;
275                 --)
276                         shift
277                         break
278                         ;;
279                 -*)
280                         usage
281                         ;;
282                 *)
283                         break
284                         ;;
285                 esac
286                 shift
287         done
288
289         git ls-files --stage -- "$@" | grep '^160000 ' |
290         while read mode sha1 stage path
291         do
292                 name=$(module_name "$path") || exit
293                 url=$(git config submodule."$name".url)
294                 if test -z "$url"
295                 then
296                         # Only mention uninitialized submodules when its
297                         # path have been specified
298                         test "$#" != "0" &&
299                         say "Submodule path '$path' not initialized"
300                         continue
301                 fi
302
303                 if ! test -d "$path"/.git
304                 then
305                         module_clone "$path" "$url" || exit
306                         subsha1=
307                 else
308                         subsha1=$(unset GIT_DIR; cd "$path" &&
309                                 git rev-parse --verify HEAD) ||
310                         die "Unable to find current revision in submodule path '$path'"
311                 fi
312
313                 if test "$subsha1" != "$sha1"
314                 then
315                         (unset GIT_DIR; cd "$path" && git-fetch &&
316                                 git-checkout -q "$sha1") ||
317                         die "Unable to checkout '$sha1' in submodule path '$path'"
318
319                         say "Submodule path '$path': checked out '$sha1'"
320                 fi
321         done
322 }
323
324 set_name_rev () {
325         revname=$( (
326                 unset GIT_DIR
327                 cd "$1" && {
328                         git describe "$2" 2>/dev/null ||
329                         git describe --tags "$2" 2>/dev/null ||
330                         git describe --contains "$2" 2>/dev/null ||
331                         git describe --all --always "$2"
332                 }
333         ) )
334         test -z "$revname" || revname=" ($revname)"
335 }
336 #
337 # Show commit summary for submodules in index or working tree
338 #
339 # If '--cached' is given, show summary between index and given commit,
340 # or between working tree and given commit
341 #
342 # $@ = [commit (default 'HEAD'),] requested paths (default all)
343 #
344 cmd_summary() {
345         summary_limit=-1
346
347         # parse $args after "submodule ... summary".
348         while test $# -ne 0
349         do
350                 case "$1" in
351                 --cached)
352                         cached="$1"
353                         ;;
354                 -n|--summary-limit)
355                         if summary_limit=$(($2 + 0)) 2>/dev/null && test "$summary_limit" = "$2"
356                         then
357                                 :
358                         else
359                                 usage
360                         fi
361                         shift
362                         ;;
363                 --)
364                         shift
365                         break
366                         ;;
367                 -*)
368                         usage
369                         ;;
370                 *)
371                         break
372                         ;;
373                 esac
374                 shift
375         done
376
377         test $summary_limit = 0 && return
378
379         if rev=$(git rev-parse --verify "$1^0" 2>/dev/null)
380         then
381                 head=$rev
382                 shift
383         else
384                 head=HEAD
385         fi
386
387         cd_to_toplevel
388         # Get modified modules cared by user
389         modules=$(git diff-index $cached --raw $head -- "$@" |
390                 grep -e '^:160000' -e '^:[0-7]* 160000' |
391                 while read mod_src mod_dst sha1_src sha1_dst status name
392                 do
393                         # Always show modules deleted or type-changed (blob<->module)
394                         test $status = D -o $status = T && echo "$name" && continue
395                         # Also show added or modified modules which are checked out
396                         GIT_DIR="$name/.git" git-rev-parse --git-dir >/dev/null 2>&1 &&
397                         echo "$name"
398                 done
399         )
400
401         test -n "$modules" &&
402         git diff-index $cached --raw $head -- $modules |
403         grep -e '^:160000' -e '^:[0-7]* 160000' |
404         cut -c2- |
405         while read mod_src mod_dst sha1_src sha1_dst status name
406         do
407                 if test -z "$cached" &&
408                         test $sha1_dst = 0000000000000000000000000000000000000000
409                 then
410                         case "$mod_dst" in
411                         160000)
412                                 sha1_dst=$(GIT_DIR="$name/.git" git rev-parse HEAD)
413                                 ;;
414                         100644 | 100755 | 120000)
415                                 sha1_dst=$(git hash-object $name)
416                                 ;;
417                         000000)
418                                 ;; # removed
419                         *)
420                                 # unexpected type
421                                 echo >&2 "unexpected mode $mod_dst"
422                                 continue ;;
423                         esac
424                 fi
425                 missing_src=
426                 missing_dst=
427
428                 test $mod_src = 160000 &&
429                 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_src^0 >/dev/null 2>&1 &&
430                 missing_src=t
431
432                 test $mod_dst = 160000 &&
433                 ! GIT_DIR="$name/.git" git-rev-parse --verify $sha1_dst^0 >/dev/null 2>&1 &&
434                 missing_dst=t
435
436                 total_commits=
437                 case "$missing_src,$missing_dst" in
438                 t,)
439                         errmsg="  Warn: $name doesn't contain commit $sha1_src"
440                         ;;
441                 ,t)
442                         errmsg="  Warn: $name doesn't contain commit $sha1_dst"
443                         ;;
444                 t,t)
445                         errmsg="  Warn: $name doesn't contain commits $sha1_src and $sha1_dst"
446                         ;;
447                 *)
448                         errmsg=
449                         total_commits=$(
450                         if test $mod_src = 160000 -a $mod_dst = 160000
451                         then
452                                 range="$sha1_src...$sha1_dst"
453                         elif test $mod_src = 160000
454                         then
455                                 range=$sha1_src
456                         else
457                                 range=$sha1_dst
458                         fi
459                         GIT_DIR="$name/.git" \
460                         git log --pretty=oneline --first-parent $range | wc -l
461                         )
462                         total_commits=" ($(($total_commits + 0)))"
463                         ;;
464                 esac
465
466                 sha1_abbr_src=$(echo $sha1_src | cut -c1-7)
467                 sha1_abbr_dst=$(echo $sha1_dst | cut -c1-7)
468                 if test $status = T
469                 then
470                         if test $mod_dst = 160000
471                         then
472                                 echo "* $name $sha1_abbr_src(blob)->$sha1_abbr_dst(submodule)$total_commits:"
473                         else
474                                 echo "* $name $sha1_abbr_src(submodule)->$sha1_abbr_dst(blob)$total_commits:"
475                         fi
476                 else
477                         echo "* $name $sha1_abbr_src...$sha1_abbr_dst$total_commits:"
478                 fi
479                 if test -n "$errmsg"
480                 then
481                         # Don't give error msg for modification whose dst is not submodule
482                         # i.e. deleted or changed to blob
483                         test $mod_dst = 160000 && echo "$errmsg"
484                 else
485                         if test $mod_src = 160000 -a $mod_dst = 160000
486                         then
487                                 limit=
488                                 test $summary_limit -gt 0 && limit="-$summary_limit"
489                                 GIT_DIR="$name/.git" \
490                                 git log $limit --pretty='format:  %m %s' \
491                                 --first-parent $sha1_src...$sha1_dst
492                         elif test $mod_dst = 160000
493                         then
494                                 GIT_DIR="$name/.git" \
495                                 git log --pretty='format:  > %s' -1 $sha1_dst
496                         else
497                                 GIT_DIR="$name/.git" \
498                                 git log --pretty='format:  < %s' -1 $sha1_src
499                         fi
500                         echo
501                 fi
502                 echo
503         done
504 }
505 #
506 # List all submodules, prefixed with:
507 #  - submodule not initialized
508 #  + different revision checked out
509 #
510 # If --cached was specified the revision in the index will be printed
511 # instead of the currently checked out revision.
512 #
513 # $@ = requested paths (default to all)
514 #
515 cmd_status()
516 {
517         # parse $args after "submodule ... status".
518         while test $# -ne 0
519         do
520                 case "$1" in
521                 -q|--quiet)
522                         quiet=1
523                         ;;
524                 --cached)
525                         cached=1
526                         ;;
527                 --)
528                         shift
529                         break
530                         ;;
531                 -*)
532                         usage
533                         ;;
534                 *)
535                         break
536                         ;;
537                 esac
538                 shift
539         done
540
541         git ls-files --stage -- "$@" | grep '^160000 ' |
542         while read mode sha1 stage path
543         do
544                 name=$(module_name "$path") || exit
545                 url=$(git config submodule."$name".url)
546                 if test -z "$url" || ! test -d "$path"/.git
547                 then
548                         say "-$sha1 $path"
549                         continue;
550                 fi
551                 set_name_rev "$path" "$sha1"
552                 if git diff-files --quiet -- "$path"
553                 then
554                         say " $sha1 $path$revname"
555                 else
556                         if test -z "$cached"
557                         then
558                                 sha1=$(unset GIT_DIR; cd "$path" && git rev-parse --verify HEAD)
559                                 set_name_rev "$path" "$sha1"
560                         fi
561                         say "+$sha1 $path$revname"
562                 fi
563         done
564 }
565
566 # This loop parses the command line arguments to find the
567 # subcommand name to dispatch.  Parsing of the subcommand specific
568 # options are primarily done by the subcommand implementations.
569 # Subcommand specific options such as --branch and --cached are
570 # parsed here as well, for backward compatibility.
571
572 while test $# != 0 && test -z "$command"
573 do
574         case "$1" in
575         add | init | update | status | summary)
576                 command=$1
577                 ;;
578         -q|--quiet)
579                 quiet=1
580                 ;;
581         -b|--branch)
582                 case "$2" in
583                 '')
584                         usage
585                         ;;
586                 esac
587                 branch="$2"; shift
588                 ;;
589         --cached)
590                 cached="$1"
591                 ;;
592         --)
593                 break
594                 ;;
595         -*)
596                 usage
597                 ;;
598         *)
599                 break
600                 ;;
601         esac
602         shift
603 done
604
605 # No command word defaults to "status"
606 test -n "$command" || command=status
607
608 # "-b branch" is accepted only by "add"
609 if test -n "$branch" && test "$command" != add
610 then
611         usage
612 fi
613
614 # "--cached" is accepted only by "status" and "summary"
615 if test -n "$cached" && test "$command" != status -a "$command" != summary
616 then
617         usage
618 fi
619
620 "cmd_$command" "$@"