git-fetch: move more code into C.
[git] / git-fetch.sh
1 #!/bin/sh
2 #
3
4 USAGE='<fetch-options> <repository> <refspec>...'
5 SUBDIRECTORY_OK=Yes
6 . git-sh-setup
7 set_reflog_action "fetch $*"
8 cd_to_toplevel ;# probably unnecessary...
9
10 . git-parse-remote
11 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
12 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
13
14 LF='
15 '
16 IFS="$LF"
17
18 no_tags=
19 tags=
20 append=
21 force=
22 verbose=
23 update_head_ok=
24 exec=
25 keep=
26 shallow_depth=
27 while case "$#" in 0) break ;; esac
28 do
29         case "$1" in
30         -a|--a|--ap|--app|--appe|--appen|--append)
31                 append=t
32                 ;;
33         --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
34         --upload-pa|--upload-pac|--upload-pack)
35                 shift
36                 exec="--upload-pack=$1"
37                 ;;
38         --upl=*|--uplo=*|--uploa=*|--upload=*|\
39         --upload-=*|--upload-p=*|--upload-pa=*|--upload-pac=*|--upload-pack=*)
40                 exec=--upload-pack=$(expr "z$1" : 'z-[^=]*=\(.*\)')
41                 shift
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         --depth=*)
64                 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
65                 ;;
66         --depth)
67                 shift
68                 shallow_depth="--depth=$1"
69                 ;;
70         -*)
71                 usage
72                 ;;
73         *)
74                 break
75                 ;;
76         esac
77         shift
78 done
79
80 case "$#" in
81 0)
82         origin=$(get_default_remote)
83         test -n "$(get_remote_url ${origin})" ||
84                 die "Where do you want to fetch from today?"
85         set x $origin ; shift ;;
86 esac
87
88 if test -z "$exec"
89 then
90         # No command line override and we have configuration for the remote.
91         exec="--upload-pack=$(get_uploadpack $1)"
92 fi
93
94 remote_nick="$1"
95 remote=$(get_remote_url "$@")
96 refs=
97 rref=
98 rsync_slurped_objects=
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 $exec "$remote") ||
107         die "Cannot get the repository state from $remote"
108
109 append_fetch_head () {
110         flags=
111         test -n "$verbose" && flags="$flags -v"
112         test -n "$force" && flags="$flags -f"
113         GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
114                 git-fetch--tool append-fetch-head $flags "$@"
115 }
116
117 update_local_ref () {
118         flags=
119         test -n "$verbose" && flags="$flags -v"
120         test -n "$force" && flags="$flags -f"
121         GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
122                 git-fetch--tool update-local-ref $flags "$@"
123 }
124
125 # updating the current HEAD with git-fetch in a bare
126 # repository is always fine.
127 if test -z "$update_head_ok" && test $(is_bare_repository) = false
128 then
129         orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
130 fi
131
132 # If --tags (and later --heads or --all) is specified, then we are
133 # not talking about defaults stored in Pull: line of remotes or
134 # branches file, and just fetch those and refspecs explicitly given.
135 # Otherwise we do what we always did.
136
137 reflist=$(get_remote_refs_for_fetch "$@")
138 if test "$tags"
139 then
140         taglist=`IFS='  ' &&
141                   echo "$ls_remote_result" |
142                   git-show-ref --exclude-existing=refs/tags/ |
143                   while read sha1 name
144                   do
145                         echo ".${name}:${name}"
146                   done` || exit
147         if test "$#" -gt 1
148         then
149                 # remote URL plus explicit refspecs; we need to merge them.
150                 reflist="$reflist$LF$taglist"
151         else
152                 # No explicit refspecs; fetch tags only.
153                 reflist=$taglist
154         fi
155 fi
156
157 fetch_native () {
158   reflist="$1"
159   refs=
160   rref=
161
162   for ref in $reflist
163   do
164       refs="$refs$LF$ref"
165
166       # These are relative path from $GIT_DIR, typically starting at refs/
167       # but may be HEAD
168       if expr "z$ref" : 'z\.' >/dev/null
169       then
170           not_for_merge=t
171           ref=$(expr "z$ref" : 'z\.\(.*\)')
172       else
173           not_for_merge=
174       fi
175       if expr "z$ref" : 'z+' >/dev/null
176       then
177           single_force=t
178           ref=$(expr "z$ref" : 'z+\(.*\)')
179       else
180           single_force=
181       fi
182       remote_name=$(expr "z$ref" : 'z\([^:]*\):')
183       local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
184
185       rref="$rref$LF$remote_name"
186   done
187
188     ( : subshell because we muck with IFS
189       IFS="     $LF"
190       (
191           git-fetch-pack --thin $exec $keep $shallow_depth "$remote" $rref ||
192           echo failed "$remote"
193       ) |
194       (
195         flags=
196         test -n "$verbose" && flags="$flags -v"
197         test -n "$force" && flags="$flags -f"
198         GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
199                 git-fetch--tool native-store \
200                         $flags "$remote" "$remote_nick" "$refs"
201       )
202     ) || exit
203
204 }
205
206 fetch_dumb () {
207   reflist="$1"
208   refs=
209   rref=
210
211   for ref in $reflist
212   do
213       refs="$refs$LF$ref"
214
215       # These are relative path from $GIT_DIR, typically starting at refs/
216       # but may be HEAD
217       if expr "z$ref" : 'z\.' >/dev/null
218       then
219           not_for_merge=t
220           ref=$(expr "z$ref" : 'z\.\(.*\)')
221       else
222           not_for_merge=
223       fi
224       if expr "z$ref" : 'z+' >/dev/null
225       then
226           single_force=t
227           ref=$(expr "z$ref" : 'z+\(.*\)')
228       else
229           single_force=
230       fi
231       remote_name=$(expr "z$ref" : 'z\([^:]*\):')
232       local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
233
234       rref="$rref$LF$remote_name"
235
236       # There are transports that can fetch only one head at a time...
237       case "$remote" in
238       http://* | https://* | ftp://*)
239           test -n "$shallow_depth" &&
240                 die "shallow clone with http not supported"
241           proto=`expr "$remote" : '\([^:]*\):'`
242           if [ -n "$GIT_SSL_NO_VERIFY" ]; then
243               curl_extra_args="-k"
244           fi
245           if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
246                 "`git-config --bool http.noEPSV`" = true ]; then
247               noepsv_opt="--disable-epsv"
248           fi
249
250           # Find $remote_name from ls-remote output.
251           head=$(
252                 IFS='   '
253                 echo "$ls_remote_result" |
254                 while read sha1 name
255                 do
256                         test "z$name" = "z$remote_name" || continue
257                         echo "$sha1"
258                         break
259                 done
260           )
261           expr "z$head" : "z$_x40\$" >/dev/null ||
262                 die "No such ref $remote_name at $remote"
263           echo >&2 "Fetching $remote_name from $remote using $proto"
264           git-http-fetch -v -a "$head" "$remote/" || exit
265           ;;
266       rsync://*)
267           test -n "$shallow_depth" &&
268                 die "shallow clone with rsync not supported"
269           TMP_HEAD="$GIT_DIR/TMP_HEAD"
270           rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
271           head=$(git-rev-parse --verify TMP_HEAD)
272           rm -f "$TMP_HEAD"
273           test "$rsync_slurped_objects" || {
274               rsync -av --ignore-existing --exclude info \
275                   "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
276
277               # Look at objects/info/alternates for rsync -- http will
278               # support it natively and git native ones will do it on
279               # the remote end.  Not having that file is not a crime.
280               rsync -q "$remote/objects/info/alternates" \
281                   "$GIT_DIR/TMP_ALT" 2>/dev/null ||
282                   rm -f "$GIT_DIR/TMP_ALT"
283               if test -f "$GIT_DIR/TMP_ALT"
284               then
285                   resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
286                   while read alt
287                   do
288                       case "$alt" in 'bad alternate: '*) die "$alt";; esac
289                       echo >&2 "Getting alternate: $alt"
290                       rsync -av --ignore-existing --exclude info \
291                       "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
292                   done
293                   rm -f "$GIT_DIR/TMP_ALT"
294               fi
295               rsync_slurped_objects=t
296           }
297           ;;
298       esac
299
300       append_fetch_head "$head" "$remote" \
301           "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
302
303   done
304
305 }
306
307 fetch_main () {
308         case "$remote" in
309         http://* | https://* | ftp://* | rsync://* )
310                 fetch_dumb "$@"
311                 ;;
312         *)
313                 fetch_native "$@"
314                 ;;
315         esac
316 }
317
318 fetch_main "$reflist" || exit
319
320 # automated tag following
321 case "$no_tags$tags" in
322 '')
323         case "$reflist" in
324         *:refs/*)
325                 # effective only when we are following remote branch
326                 # using local tracking branch.
327                 taglist=$(IFS=' ' &&
328                 echo "$ls_remote_result" |
329                 git-show-ref --exclude-existing=refs/tags/ |
330                 while read sha1 name
331                 do
332                         git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
333                         echo >&2 "Auto-following $name"
334                         echo ".${name}:${name}"
335                 done)
336         esac
337         case "$taglist" in
338         '') ;;
339         ?*)
340                 # do not deepen a shallow tree when following tags
341                 shallow_depth=
342                 fetch_main "$taglist" || exit ;;
343         esac
344 esac
345
346 # If the original head was empty (i.e. no "master" yet), or
347 # if we were told not to worry, we do not have to check.
348 case "$orig_head" in
349 '')
350         ;;
351 ?*)
352         curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
353         if test "$curr_head" != "$orig_head"
354         then
355             git-update-ref \
356                         -m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
357                         HEAD "$orig_head"
358                 die "Cannot fetch into the current branch."
359         fi
360         ;;
361 esac