Merge branch 'master' into js/shallow
[git] / git-fetch.sh
1 #!/bin/sh
2 #
3
4 USAGE='<fetch-options> <repository> <refspec>...'
5 SUBDIRECTORY_OK=Yes
6 . git-sh-setup
7 TOP=$(git-rev-parse --show-cdup)
8 if test ! -z "$TOP"
9 then
10         cd "$TOP"
11 fi
12 . git-parse-remote
13 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
14 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
15
16 LF='
17 '
18 IFS="$LF"
19
20 rloga=fetch
21 no_tags=
22 tags=
23 append=
24 force=
25 verbose=
26 update_head_ok=
27 exec=
28 upload_pack=
29 keep=
30 shallow_depth=
31 while case "$#" in 0) break ;; esac
32 do
33         case "$1" in
34         -a|--a|--ap|--app|--appe|--appen|--append)
35                 append=t
36                 ;;
37         --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
38         --upload-pa|--upload-pac|--upload-pack)
39                 shift
40                 exec="--exec=$1" 
41                 upload_pack="-u $1"
42                 ;;
43         -f|--f|--fo|--for|--forc|--force)
44                 force=t
45                 ;;
46         -t|--t|--ta|--tag|--tags)
47                 tags=t
48                 ;;
49         -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
50                 no_tags=t
51                 ;;
52         -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
53         --update-he|--update-hea|--update-head|--update-head-|\
54         --update-head-o|--update-head-ok)
55                 update_head_ok=t
56                 ;;
57         -v|--verbose)
58                 verbose=Yes
59                 ;;
60         -k|--k|--ke|--kee|--keep)
61                 keep='-k -k'
62                 ;;
63         --reflog-action=*)
64                 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`
65                 ;;
66         --depth=*)
67                 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
68                 ;;
69         --depth)
70                 shift
71                 shallow_depth="--depth=$1"
72                 ;;
73         -*)
74                 usage
75                 ;;
76         *)
77                 break
78                 ;;
79         esac
80         shift
81 done
82
83 case "$#" in
84 0)
85         origin=$(get_default_remote)
86         test -n "$(get_remote_url ${origin})" ||
87                 die "Where do you want to fetch from today?"
88         set x $origin ; shift ;;
89 esac
90
91 remote_nick="$1"
92 remote=$(get_remote_url "$@")
93 refs=
94 rref=
95 rsync_slurped_objects=
96
97 rloga="$rloga $remote_nick"
98 test "$remote_nick" = "$remote" || rloga="$rloga $remote"
99
100 if test "" = "$append"
101 then
102         : >"$GIT_DIR/FETCH_HEAD"
103 fi
104
105 # Global that is reused later
106 ls_remote_result=$(git ls-remote $upload_pack "$remote") ||
107         die "Cannot get the repository state from $remote"
108
109 append_fetch_head () {
110     head_="$1"
111     remote_="$2"
112     remote_name_="$3"
113     remote_nick_="$4"
114     local_name_="$5"
115     case "$6" in
116     t) not_for_merge_='not-for-merge' ;;
117     '') not_for_merge_= ;;
118     esac
119
120     # remote-nick is the URL given on the command line (or a shorthand)
121     # remote-name is the $GIT_DIR relative refs/ path we computed
122     # for this refspec.
123
124     # the $note_ variable will be fed to git-fmt-merge-msg for further
125     # processing.
126     case "$remote_name_" in
127     HEAD)
128         note_= ;;
129     refs/heads/*)
130         note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
131         note_="branch '$note_' of " ;;
132     refs/tags/*)
133         note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
134         note_="tag '$note_' of " ;;
135     refs/remotes/*)
136         note_="$(expr "$remote_name_" : 'refs/remotes/\(.*\)')"
137         note_="remote branch '$note_' of " ;;
138     *)
139         note_="$remote_name of " ;;
140     esac
141     remote_1_=$(expr "z$remote_" : 'z\(.*\)\.git/*$') &&
142         remote_="$remote_1_"
143     note_="$note_$remote_"
144
145     # 2.6.11-tree tag would not be happy to be fed to resolve.
146     if git-cat-file commit "$head_" >/dev/null 2>&1
147     then
148         headc_=$(git-rev-parse --verify "$head_^0") || exit
149         echo "$headc_   $not_for_merge_ $note_" >>"$GIT_DIR/FETCH_HEAD"
150     else
151         echo "$head_    not-for-merge   $note_" >>"$GIT_DIR/FETCH_HEAD"
152     fi
153
154     update_local_ref "$local_name_" "$head_" "$note_"
155 }
156
157 update_local_ref () {
158     # If we are storing the head locally make sure that it is
159     # a fast forward (aka "reverse push").
160
161     label_=$(git-cat-file -t $2)
162     newshort_=$(git-rev-parse --short $2)
163     if test -z "$1" ; then
164         [ "$verbose" ] && echo >&2 "* fetched $3"
165         [ "$verbose" ] && echo >&2 "  $label_: $newshort_"
166         return 0
167     fi
168     oldshort_=$(git show-ref --hash --abbrev "$1" 2>/dev/null)
169
170     case "$1" in
171     refs/tags/*)
172         # Tags need not be pointing at commits so there
173         # is no way to guarantee "fast-forward" anyway.
174         if test -n "$oldshort_"
175         then
176                 if now_=$(git show-ref --hash "$1") && test "$now_" = "$2"
177                 then
178                         [ "$verbose" ] && echo >&2 "* $1: same as $3"
179                         [ "$verbose" ] && echo >&2 "  $label_: $newshort_" ||:
180                 else
181                         echo >&2 "* $1: updating with $3"
182                         echo >&2 "  $label_: $newshort_"
183                         git-update-ref -m "$rloga: updating tag" "$1" "$2"
184                 fi
185         else
186                 echo >&2 "* $1: storing $3"
187                 echo >&2 "  $label_: $newshort_"
188                 git-update-ref -m "$rloga: storing tag" "$1" "$2"
189         fi
190         ;;
191
192     refs/heads/* | refs/remotes/*)
193         # $1 is the ref being updated.
194         # $2 is the new value for the ref.
195         local=$(git-rev-parse --verify "$1^0" 2>/dev/null)
196         if test "$local"
197         then
198             # Require fast-forward.
199             mb=$(git-merge-base "$local" "$2") &&
200             case "$2,$mb" in
201             $local,*)
202                 if test -n "$verbose"
203                 then
204                         echo >&2 "* $1: same as $3"
205                         echo >&2 "  $label_: $newshort_"
206                 fi
207                 ;;
208             *,$local)
209                 echo >&2 "* $1: fast forward to $3"
210                 echo >&2 "  old..new: $oldshort_..$newshort_"
211                 git-update-ref -m "$rloga: fast-forward" "$1" "$2" "$local"
212                 ;;
213             *)
214                 false
215                 ;;
216             esac || {
217                 case ",$force,$single_force," in
218                 *,t,*)
219                         echo >&2 "* $1: forcing update to non-fast forward $3"
220                         echo >&2 "  old...new: $oldshort_...$newshort_"
221                         git-update-ref -m "$rloga: forced-update" "$1" "$2" "$local"
222                         ;;
223                 *)
224                         echo >&2 "* $1: not updating to non-fast forward $3"
225                         echo >&2 "  old...new: $oldshort_...$newshort_"
226                         exit 1
227                         ;;
228                 esac
229             }
230         else
231             echo >&2 "* $1: storing $3"
232             echo >&2 "  $label_: $newshort_"
233             git-update-ref -m "$rloga: storing head" "$1" "$2"
234         fi
235         ;;
236     esac
237 }
238
239 case "$update_head_ok" in
240 '')
241         orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
242         ;;
243 esac
244
245 # If --tags (and later --heads or --all) is specified, then we are
246 # not talking about defaults stored in Pull: line of remotes or
247 # branches file, and just fetch those and refspecs explicitly given.
248 # Otherwise we do what we always did.
249
250 reflist=$(get_remote_refs_for_fetch "$@")
251 if test "$tags"
252 then
253         taglist=`IFS='  ' &&
254                   echo "$ls_remote_result" |
255                   while read sha1 name
256                   do
257                         case "$sha1" in
258                         fail)
259                                 exit 1
260                         esac
261                         case "$name" in
262                         *^*) continue ;;
263                         refs/tags/*) ;;
264                         *) continue ;;
265                         esac
266                         if git-check-ref-format "$name"
267                         then
268                             echo ".${name}:${name}"
269                         else
270                             echo >&2 "warning: tag ${name} ignored"
271                         fi
272                   done` || exit
273         if test "$#" -gt 1
274         then
275                 # remote URL plus explicit refspecs; we need to merge them.
276                 reflist="$reflist$LF$taglist"
277         else
278                 # No explicit refspecs; fetch tags only.
279                 reflist=$taglist
280         fi
281 fi
282
283 fetch_main () {
284   reflist="$1"
285   refs=
286   rref=
287
288   for ref in $reflist
289   do
290       refs="$refs$LF$ref"
291
292       # These are relative path from $GIT_DIR, typically starting at refs/
293       # but may be HEAD
294       if expr "z$ref" : 'z\.' >/dev/null
295       then
296           not_for_merge=t
297           ref=$(expr "z$ref" : 'z\.\(.*\)')
298       else
299           not_for_merge=
300       fi
301       if expr "z$ref" : 'z+' >/dev/null
302       then
303           single_force=t
304           ref=$(expr "z$ref" : 'z+\(.*\)')
305       else
306           single_force=
307       fi
308       remote_name=$(expr "z$ref" : 'z\([^:]*\):')
309       local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
310
311       rref="$rref$LF$remote_name"
312
313       # There are transports that can fetch only one head at a time...
314       case "$remote" in
315       http://* | https://* | ftp://*)
316           test -n "$shallow_depth" &&
317                 die "shallow clone with http not supported"
318           proto=`expr "$remote" : '\([^:]*\):'`
319           if [ -n "$GIT_SSL_NO_VERIFY" ]; then
320               curl_extra_args="-k"
321           fi
322           if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
323                 "`git-repo-config --bool http.noEPSV`" = true ]; then
324               noepsv_opt="--disable-epsv"
325           fi
326
327           # Find $remote_name from ls-remote output.
328           head=$(
329                 IFS='   '
330                 echo "$ls_remote_result" |
331                 while read sha1 name
332                 do
333                         test "z$name" = "z$remote_name" || continue
334                         echo "$sha1"
335                         break
336                 done
337           )
338           expr "z$head" : "z$_x40\$" >/dev/null ||
339                 die "No such ref $remote_name at $remote"
340           echo >&2 "Fetching $remote_name from $remote using $proto"
341           git-http-fetch -v -a "$head" "$remote/" || exit
342           ;;
343       rsync://*)
344           test -n "$shallow_depth" &&
345                 die "shallow clone with rsync not supported"
346           TMP_HEAD="$GIT_DIR/TMP_HEAD"
347           rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
348           head=$(git-rev-parse --verify TMP_HEAD)
349           rm -f "$TMP_HEAD"
350           test "$rsync_slurped_objects" || {
351               rsync -av --ignore-existing --exclude info \
352                   "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
353
354               # Look at objects/info/alternates for rsync -- http will
355               # support it natively and git native ones will do it on
356               # the remote end.  Not having that file is not a crime.
357               rsync -q "$remote/objects/info/alternates" \
358                   "$GIT_DIR/TMP_ALT" 2>/dev/null ||
359                   rm -f "$GIT_DIR/TMP_ALT"
360               if test -f "$GIT_DIR/TMP_ALT"
361               then
362                   resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
363                   while read alt
364                   do
365                       case "$alt" in 'bad alternate: '*) die "$alt";; esac
366                       echo >&2 "Getting alternate: $alt"
367                       rsync -av --ignore-existing --exclude info \
368                       "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
369                   done
370                   rm -f "$GIT_DIR/TMP_ALT"
371               fi
372               rsync_slurped_objects=t
373           }
374           ;;
375       *)
376           # We will do git native transport with just one call later.
377           continue ;;
378       esac
379
380       append_fetch_head "$head" "$remote" \
381           "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
382
383   done
384
385   case "$remote" in
386   http://* | https://* | ftp://* | rsync://* )
387       ;; # we are already done.
388   *)
389     ( : subshell because we muck with IFS
390       pack_lockfile=
391       IFS="     $LF"
392       (
393           git-fetch-pack --thin $exec $keep $shallow_depth "$remote" $rref || echo failed "$remote"
394       ) |
395       while read sha1 remote_name
396       do
397           case "$sha1" in
398           failed)
399                   echo >&2 "Fetch failure: $remote"
400                   exit 1 ;;
401           # special line coming from index-pack with the pack name
402           pack)
403                   continue ;;
404           keep)
405                   pack_lockfile="$GIT_OBJECT_DIRECTORY/pack/pack-$remote_name.keep"
406                   continue ;;
407           esac
408           found=
409           single_force=
410           for ref in $refs
411           do
412               case "$ref" in
413               +$remote_name:*)
414                   single_force=t
415                   not_for_merge=
416                   found="$ref"
417                   break ;;
418               .+$remote_name:*)
419                   single_force=t
420                   not_for_merge=t
421                   found="$ref"
422                   break ;;
423               .$remote_name:*)
424                   not_for_merge=t
425                   found="$ref"
426                   break ;;
427               $remote_name:*)
428                   not_for_merge=
429                   found="$ref"
430                   break ;;
431               esac
432           done
433           local_name=$(expr "z$found" : 'z[^:]*:\(.*\)')
434           append_fetch_head "$sha1" "$remote" \
435                   "$remote_name" "$remote_nick" "$local_name" \
436                   "$not_for_merge" || exit
437       done &&
438       if [ "$pack_lockfile" ]; then rm -f "$pack_lockfile"; fi
439     ) || exit ;;
440   esac
441
442 }
443
444 fetch_main "$reflist" || exit
445
446 # automated tag following
447 case "$no_tags$tags" in
448 '')
449         case "$reflist" in
450         *:refs/*)
451                 # effective only when we are following remote branch
452                 # using local tracking branch.
453                 taglist=$(IFS=' ' &&
454                 echo "$ls_remote_result" |
455                 git-show-ref --exclude-existing=refs/tags/ |
456                 while read sha1 name
457                 do
458                         git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
459                         echo >&2 "Auto-following $name"
460                         echo ".${name}:${name}"
461                 done)
462         esac
463         case "$taglist" in
464         '') ;;
465         ?*)
466                 # do not deepen a shallow tree when following tags
467                 shallow_depth=
468                 fetch_main "$taglist" || exit ;;
469         esac
470 esac
471
472 # If the original head was empty (i.e. no "master" yet), or
473 # if we were told not to worry, we do not have to check.
474 case "$orig_head" in
475 '')
476         ;;
477 ?*)
478         curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
479         if test "$curr_head" != "$orig_head"
480         then
481             git-update-ref \
482                         -m "$rloga: Undoing incorrectly fetched HEAD." \
483                         HEAD "$orig_head"
484                 die "Cannot fetch into the current branch."
485         fi
486         ;;
487 esac