.gitignore: add git-fetch--tool
[git.git] / git-fetch.sh
blob4a8d8d6ef73921ebd99ef68ffa097cd325453763
1 #!/bin/sh
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...
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"
14 LF='
16 IFS="$LF"
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
29 case "$1" in
30 -a|--a|--ap|--app|--appe|--appen|--append)
31 append=t
33 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
34 --upload-pa|--upload-pac|--upload-pack)
35 shift
36 exec="--upload-pack=$1"
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
43 -f|--f|--fo|--for|--forc|--force)
44 force=t
46 -t|--t|--ta|--tag|--tags)
47 tags=t
49 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
50 no_tags=t
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
57 -v|--verbose)
58 verbose=Yes
60 -k|--k|--ke|--kee|--keep)
61 keep='-k -k'
63 --depth=*)
64 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
66 --depth)
67 shift
68 shallow_depth="--depth=$1"
70 -*)
71 usage
74 break
76 esac
77 shift
78 done
80 case "$#" in
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
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)"
94 remote_nick="$1"
95 remote=$(get_remote_url "$@")
96 refs=
97 rref=
98 rsync_slurped_objects=
100 if test "" = "$append"
101 then
102 : >"$GIT_DIR/FETCH_HEAD"
105 # Global that is reused later
106 ls_remote_result=$(git ls-remote $exec "$remote") ||
107 die "Cannot get the repository state from $remote"
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 $flags append-fetch-head "$@"
117 # updating the current HEAD with git-fetch in a bare
118 # repository is always fine.
119 if test -z "$update_head_ok" && test $(is_bare_repository) = false
120 then
121 orig_head=$(git-rev-parse --verify HEAD 2>/dev/null)
124 # Allow --notags from remote.$1.tagopt
125 case "$tags$no_tags" in
127 case "$(git-config --get "remote.$1.tagopt")" in
128 --no-tags)
129 no_tags=t ;;
130 esac
131 esac
133 # If --tags (and later --heads or --all) is specified, then we are
134 # not talking about defaults stored in Pull: line of remotes or
135 # branches file, and just fetch those and refspecs explicitly given.
136 # Otherwise we do what we always did.
138 reflist=$(get_remote_refs_for_fetch "$@")
139 if test "$tags"
140 then
141 taglist=`IFS=' ' &&
142 echo "$ls_remote_result" |
143 git-show-ref --exclude-existing=refs/tags/ |
144 while read sha1 name
146 echo ".${name}:${name}"
147 done` || exit
148 if test "$#" -gt 1
149 then
150 # remote URL plus explicit refspecs; we need to merge them.
151 reflist="$reflist$LF$taglist"
152 else
153 # No explicit refspecs; fetch tags only.
154 reflist=$taglist
158 fetch_native () {
160 eval=$(echo "$1" | git-fetch--tool parse-reflist "-")
161 eval "$eval"
163 ( : subshell because we muck with IFS
164 IFS=" $LF"
166 git-fetch-pack --thin $exec $keep $shallow_depth "$remote" $rref ||
167 echo failed "$remote"
170 flags=
171 test -n "$verbose" && flags="$flags -v"
172 test -n "$force" && flags="$flags -f"
173 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
174 git-fetch--tool $flags native-store \
175 "$remote" "$remote_nick" "$refs"
177 ) || exit
181 fetch_dumb () {
182 reflist="$1"
183 refs=
184 rref=
186 for ref in $reflist
188 refs="$refs$LF$ref"
190 # These are relative path from $GIT_DIR, typically starting at refs/
191 # but may be HEAD
192 if expr "z$ref" : 'z\.' >/dev/null
193 then
194 not_for_merge=t
195 ref=$(expr "z$ref" : 'z\.\(.*\)')
196 else
197 not_for_merge=
199 if expr "z$ref" : 'z+' >/dev/null
200 then
201 single_force=t
202 ref=$(expr "z$ref" : 'z+\(.*\)')
203 else
204 single_force=
206 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
207 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
209 rref="$rref$LF$remote_name"
211 # There are transports that can fetch only one head at a time...
212 case "$remote" in
213 http://* | https://* | ftp://*)
214 test -n "$shallow_depth" &&
215 die "shallow clone with http not supported"
216 proto=`expr "$remote" : '\([^:]*\):'`
217 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
218 curl_extra_args="-k"
220 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
221 "`git-config --bool http.noEPSV`" = true ]; then
222 noepsv_opt="--disable-epsv"
225 # Find $remote_name from ls-remote output.
226 head=$(
227 IFS=' '
228 echo "$ls_remote_result" |
229 while read sha1 name
231 test "z$name" = "z$remote_name" || continue
232 echo "$sha1"
233 break
234 done
236 expr "z$head" : "z$_x40\$" >/dev/null ||
237 die "No such ref $remote_name at $remote"
238 echo >&2 "Fetching $remote_name from $remote using $proto"
239 git-http-fetch -v -a "$head" "$remote/" || exit
241 rsync://*)
242 test -n "$shallow_depth" &&
243 die "shallow clone with rsync not supported"
244 TMP_HEAD="$GIT_DIR/TMP_HEAD"
245 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
246 head=$(git-rev-parse --verify TMP_HEAD)
247 rm -f "$TMP_HEAD"
248 test "$rsync_slurped_objects" || {
249 rsync -av --ignore-existing --exclude info \
250 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
252 # Look at objects/info/alternates for rsync -- http will
253 # support it natively and git native ones will do it on
254 # the remote end. Not having that file is not a crime.
255 rsync -q "$remote/objects/info/alternates" \
256 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
257 rm -f "$GIT_DIR/TMP_ALT"
258 if test -f "$GIT_DIR/TMP_ALT"
259 then
260 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
261 while read alt
263 case "$alt" in 'bad alternate: '*) die "$alt";; esac
264 echo >&2 "Getting alternate: $alt"
265 rsync -av --ignore-existing --exclude info \
266 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
267 done
268 rm -f "$GIT_DIR/TMP_ALT"
270 rsync_slurped_objects=t
273 esac
275 append_fetch_head "$head" "$remote" \
276 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
278 done
282 fetch_main () {
283 case "$remote" in
284 http://* | https://* | ftp://* | rsync://* )
285 fetch_dumb "$@"
288 fetch_native "$@"
290 esac
293 fetch_main "$reflist" || exit
295 # automated tag following
296 case "$no_tags$tags" in
298 case "$reflist" in
299 *:refs/*)
300 # effective only when we are following remote branch
301 # using local tracking branch.
302 taglist=$(IFS=' ' &&
303 echo "$ls_remote_result" |
304 git-show-ref --exclude-existing=refs/tags/ |
305 while read sha1 name
307 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
308 echo >&2 "Auto-following $name"
309 echo ".${name}:${name}"
310 done)
311 esac
312 case "$taglist" in
313 '') ;;
315 # do not deepen a shallow tree when following tags
316 shallow_depth=
317 fetch_main "$taglist" || exit ;;
318 esac
319 esac
321 # If the original head was empty (i.e. no "master" yet), or
322 # if we were told not to worry, we do not have to check.
323 case "$orig_head" in
327 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
328 if test "$curr_head" != "$orig_head"
329 then
330 git-update-ref \
331 -m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
332 HEAD "$orig_head"
333 die "Cannot fetch into the current branch."
336 esac