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