Code

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