checkout: report where the new HEAD is upon detaching HEAD
[git/jnareb-git/bp-gitweb.git] / git-fetch.sh
blob93349330d45ad43c5c7e146a942d76b78c0e2ee9
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 no_progress=
28 test -t 1 || no_progress=--no-progress
29 while case "$#" in 0) break ;; esac
31 case "$1" in
32 -a|--a|--ap|--app|--appe|--appen|--append)
33 append=t
35 --upl|--uplo|--uploa|--upload|--upload-|--upload-p|\
36 --upload-pa|--upload-pac|--upload-pack)
37 shift
38 exec="--upload-pack=$1"
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
45 -f|--f|--fo|--for|--forc|--force)
46 force=t
48 -t|--t|--ta|--tag|--tags)
49 tags=t
51 -n|--n|--no|--no-|--no-t|--no-ta|--no-tag|--no-tags)
52 no_tags=t
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
59 -v|--verbose)
60 verbose=Yes
62 -k|--k|--ke|--kee|--keep)
63 keep='-k -k'
65 --depth=*)
66 shallow_depth="--depth=`expr "z$1" : 'z-[^=]*=\(.*\)'`"
68 --depth)
69 shift
70 shallow_depth="--depth=$1"
72 -*)
73 usage
76 break
78 esac
79 shift
80 done
82 case "$#" in
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
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)"
96 remote_nick="$1"
97 remote=$(get_remote_url "$@")
98 refs=
99 rref=
100 rsync_slurped_objects=
102 if test "" = "$append"
103 then
104 : >"$GIT_DIR/FETCH_HEAD"
107 # Global that is reused later
108 ls_remote_result=$(git ls-remote $exec "$remote") ||
109 die "Cannot get the repository state from $remote"
111 append_fetch_head () {
112 flags=
113 test -n "$verbose" && flags="$flags$LF-v"
114 test -n "$force$single_force" && flags="$flags$LF-f"
115 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
116 git-fetch--tool $flags append-fetch-head "$@"
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)
126 # Allow --notags from remote.$1.tagopt
127 case "$tags$no_tags" in
129 case "$(git-config --get "remote.$1.tagopt")" in
130 --no-tags)
131 no_tags=t ;;
132 esac
133 esac
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.
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
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
160 fetch_all_at_once () {
162 eval=$(echo "$1" | git-fetch--tool parse-reflist "-")
163 eval "$eval"
165 ( : subshell because we muck with IFS
166 IFS=" $LF"
168 if test "$remote" = . ; then
169 git-show-ref $rref || echo failed "$remote"
170 elif test -f "$remote" ; then
171 test -n "$shallow_depth" &&
172 die "shallow clone with bundle is not supported"
173 git-bundle unbundle "$remote" $rref ||
174 echo failed "$remote"
175 else
176 git-fetch-pack --thin $exec $keep $shallow_depth $no_progress \
177 "$remote" $rref ||
178 echo failed "$remote"
182 flags=
183 test -n "$verbose" && flags="$flags -v"
184 test -n "$force" && flags="$flags -f"
185 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION" \
186 git-fetch--tool $flags native-store \
187 "$remote" "$remote_nick" "$refs"
189 ) || exit
193 fetch_per_ref () {
194 reflist="$1"
195 refs=
196 rref=
198 for ref in $reflist
200 refs="$refs$LF$ref"
202 # These are relative path from $GIT_DIR, typically starting at refs/
203 # but may be HEAD
204 if expr "z$ref" : 'z\.' >/dev/null
205 then
206 not_for_merge=t
207 ref=$(expr "z$ref" : 'z\.\(.*\)')
208 else
209 not_for_merge=
211 if expr "z$ref" : 'z+' >/dev/null
212 then
213 single_force=t
214 ref=$(expr "z$ref" : 'z+\(.*\)')
215 else
216 single_force=
218 remote_name=$(expr "z$ref" : 'z\([^:]*\):')
219 local_name=$(expr "z$ref" : 'z[^:]*:\(.*\)')
221 rref="$rref$LF$remote_name"
223 # There are transports that can fetch only one head at a time...
224 case "$remote" in
225 http://* | https://* | ftp://*)
226 test -n "$shallow_depth" &&
227 die "shallow clone with http not supported"
228 proto=`expr "$remote" : '\([^:]*\):'`
229 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
230 curl_extra_args="-k"
232 if [ -n "$GIT_CURL_FTP_NO_EPSV" -o \
233 "`git-config --bool http.noEPSV`" = true ]; then
234 noepsv_opt="--disable-epsv"
237 # Find $remote_name from ls-remote output.
238 head=$(
239 IFS=' '
240 echo "$ls_remote_result" |
241 while read sha1 name
243 test "z$name" = "z$remote_name" || continue
244 echo "$sha1"
245 break
246 done
248 expr "z$head" : "z$_x40\$" >/dev/null ||
249 die "No such ref $remote_name at $remote"
250 echo >&2 "Fetching $remote_name from $remote using $proto"
251 git-http-fetch -v -a "$head" "$remote/" || exit
253 rsync://*)
254 test -n "$shallow_depth" &&
255 die "shallow clone with rsync not supported"
256 TMP_HEAD="$GIT_DIR/TMP_HEAD"
257 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
258 head=$(git-rev-parse --verify TMP_HEAD)
259 rm -f "$TMP_HEAD"
260 test "$rsync_slurped_objects" || {
261 rsync -av --ignore-existing --exclude info \
262 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
264 # Look at objects/info/alternates for rsync -- http will
265 # support it natively and git native ones will do it on
266 # the remote end. Not having that file is not a crime.
267 rsync -q "$remote/objects/info/alternates" \
268 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
269 rm -f "$GIT_DIR/TMP_ALT"
270 if test -f "$GIT_DIR/TMP_ALT"
271 then
272 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
273 while read alt
275 case "$alt" in 'bad alternate: '*) die "$alt";; esac
276 echo >&2 "Getting alternate: $alt"
277 rsync -av --ignore-existing --exclude info \
278 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
279 done
280 rm -f "$GIT_DIR/TMP_ALT"
282 rsync_slurped_objects=t
285 esac
287 append_fetch_head "$head" "$remote" \
288 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge" || exit
290 done
294 fetch_main () {
295 case "$remote" in
296 http://* | https://* | ftp://* | rsync://* )
297 fetch_per_ref "$@"
300 fetch_all_at_once "$@"
302 esac
305 fetch_main "$reflist" || exit
307 # automated tag following
308 case "$no_tags$tags" in
310 case "$reflist" in
311 *:refs/*)
312 # effective only when we are following remote branch
313 # using local tracking branch.
314 taglist=$(IFS=' ' &&
315 echo "$ls_remote_result" |
316 git-show-ref --exclude-existing=refs/tags/ |
317 while read sha1 name
319 git-cat-file -t "$sha1" >/dev/null 2>&1 || continue
320 echo >&2 "Auto-following $name"
321 echo ".${name}:${name}"
322 done)
323 esac
324 case "$taglist" in
325 '') ;;
327 # do not deepen a shallow tree when following tags
328 shallow_depth=
329 fetch_main "$taglist" || exit ;;
330 esac
331 esac
333 # If the original head was empty (i.e. no "master" yet), or
334 # if we were told not to worry, we do not have to check.
335 case "$orig_head" in
339 curr_head=$(git-rev-parse --verify HEAD 2>/dev/null)
340 if test "$curr_head" != "$orig_head"
341 then
342 git-update-ref \
343 -m "$GIT_REFLOG_ACTION: Undoing incorrectly fetched HEAD." \
344 HEAD "$orig_head"
345 die "Cannot fetch into the current branch."
348 esac