[PATCH] fetch.c: Clean up object flag definitions
[alt-git.git] / git-fetch.sh
blobcbfa27de90baa14b268785a72e1f9c1a91a47f54
1 #!/bin/sh
3 . git-sh-setup || die "Not a git archive"
4 . git-parse-remote
5 _x40='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
6 _x40="$_x40$_x40$_x40$_x40$_x40$_x40$_x40$_x40"
8 append=
9 force=
10 update_head_ok=
11 while case "$#" in 0) break ;; esac
13 case "$1" in
14 -a|--a|--ap|--app|--appe|--appen|--append)
15 append=t
17 -f|--f|--fo|--for|--forc|--force)
18 force=t
20 -u|--u|--up|--upd|--upda|--updat|--update|--update-|--update-h|\
21 --update-he|--update-hea|--update-head|--update-head-|\
22 --update-head-o|--update-head-ok)
23 update_head_ok=t
26 break
28 esac
29 shift
30 done
32 case "$#" in
34 test -f "$GIT_DIR/branches/origin" ||
35 test -f "$GIT_DIR/remotes/origin" ||
36 die "Where do you want to fetch from today?"
37 set origin ;;
38 esac
40 remote_nick="$1"
41 remote=$(get_remote_url "$@")
42 refs=
43 rref=
44 rsync_slurped_objects=
46 if test "" = "$append"
47 then
48 : >$GIT_DIR/FETCH_HEAD
51 append_fetch_head () {
52 head_="$1"
53 remote_="$2"
54 remote_name_="$3"
55 remote_nick_="$4"
56 local_name_="$5"
58 # remote-nick is the URL given on the command line (or a shorthand)
59 # remote-name is the $GIT_DIR relative refs/ path we computed
60 # for this refspec.
61 remote_1_=$(expr "$remote_" : '\(.*\)\.git/*$') &&
62 remote_="$remote_1_"
63 case "$remote_" in
64 . | ./) where_= ;;
65 *) where_=" of $remote_" ;;
66 esac
67 case "$remote_name_" in
68 HEAD)
69 note_= ;;
70 refs/heads/*)
71 note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
72 note_="branch '$note_'" ;;
73 refs/tags/*)
74 note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
75 note_="tag '$note_'" ;;
77 note_="$remote_name" ;;
78 esac
79 note_="$note_$where_"
81 # 2.6.11-tree tag would not be happy to be fed to resolve.
82 if git-cat-file commit "$head_" >/dev/null 2>&1
83 then
84 headc_=$(git-rev-parse --verify "$head_^0") || exit
85 echo "$headc_ $note_" >>$GIT_DIR/FETCH_HEAD
86 echo >&2 "* committish: $head_"
87 echo >&2 " $note_"
88 else
89 echo >&2 "* non-commit: $head_"
90 echo >&2 " $note_"
92 if test "$local_name_" != ""
93 then
94 # We are storing the head locally. Make sure that it is
95 # a fast forward (aka "reverse push").
96 fast_forward_local "$local_name_" "$head_" "$note_"
100 fast_forward_local () {
101 mkdir -p "$(dirname "$GIT_DIR/$1")"
102 case "$1" in
103 refs/tags/*)
104 # Tags need not be pointing at commits so there
105 # is no way to guarantee "fast-forward" anyway.
106 if test -f "$GIT_DIR/$1"
107 then
108 echo >&2 "* $1: updating with $3"
109 else
110 echo >&2 "* $1: storing $3"
112 echo "$2" >"$GIT_DIR/$1" ;;
114 refs/heads/*)
115 # NEEDSWORK: use the same cmpxchg protocol here.
116 echo "$2" >"$GIT_DIR/$1.lock"
117 if test -f "$GIT_DIR/$1"
118 then
119 local=$(git-rev-parse --verify "$1^0") &&
120 mb=$(git-merge-base "$local" "$2") &&
121 case "$2,$mb" in
122 $local,*)
123 echo >&2 "* $1: same as $3"
125 *,$local)
126 echo >&2 "* $1: fast forward to $3"
129 false
131 esac || {
132 echo >&2 "* $1: does not fast forward to $3;"
133 case "$force,$single_force" in
134 t,* | *,t)
135 echo >&2 " forcing update."
138 mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1.remote"
139 echo >&2 " leaving it in '$1.remote'"
141 esac
143 else
144 echo >&2 "* $1: storing $3"
146 test -f "$GIT_DIR/$1.lock" &&
147 mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1"
149 esac
152 case "$update_head_ok" in
154 orig_head=$(cat "$GIT_DIR/HEAD" 2>/dev/null)
156 esac
158 for ref in $(get_remote_refs_for_fetch "$@")
160 refs="$refs $ref"
162 # These are relative path from $GIT_DIR, typically starting at refs/
163 # but may be HEAD
164 if expr "$ref" : '\+' >/dev/null
165 then
166 single_force=t
167 ref=$(expr "$ref" : '\+\(.*\)')
168 else
169 single_force=
171 remote_name=$(expr "$ref" : '\([^:]*\):')
172 local_name=$(expr "$ref" : '[^:]*:\(.*\)')
174 rref="$rref $remote_name"
176 # There are transports that can fetch only one head at a time...
177 case "$remote" in
178 http://* | https://*)
179 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
180 curl_extra_args="-k"
182 head=$(curl -nsf $curl_extra_args "$remote/$remote_name") &&
183 expr "$head" : "$_x40\$" >/dev/null ||
184 die "Failed to fetch $remote_name from $remote"
185 echo Fetching "$remote_name from $remote" using http
186 git-http-fetch -v -a "$head" "$remote/" || exit
188 rsync://*)
189 TMP_HEAD="$GIT_DIR/TMP_HEAD"
190 rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
191 head=$(git-rev-parse TMP_HEAD)
192 rm -f "$TMP_HEAD"
193 test "$rsync_slurped_objects" || {
194 rsync -av --ignore-existing --exclude info \
195 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
197 # Look at objects/info/alternates for rsync -- http will
198 # support it natively and git native ones will do it on the remote
199 # end. Not having that file is not a crime.
200 rsync -q "$remote/objects/info/alternates" \
201 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
202 rm -f "$GIT_DIR/TMP_ALT"
203 if test -f "$GIT_DIR/TMP_ALT"
204 then
205 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
206 while read alt
208 case "$alt" in 'bad alternate: '*) die "$alt";; esac
209 echo >&2 "Getting alternate: $alt"
210 rsync -av --ignore-existing --exclude info \
211 "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
212 done
213 rm -f "$GIT_DIR/TMP_ALT"
215 rsync_slurped_objects=t
219 # We will do git native transport with just one call later.
220 continue ;;
221 esac
223 append_fetch_head "$head" "$remote" "$remote_name" "$remote_nick" "$local_name"
225 done
227 case "$remote" in
228 http://* | https://* | rsync://* )
229 ;; # we are already done.
232 git-fetch-pack "$remote" $rref || echo failed "$remote"
234 while read sha1 remote_name
236 case "$sha1" in
237 failed)
238 echo >&2 "Fetch failure: $remote"
239 exit 1 ;;
240 esac
241 found=
242 single_force=
243 for ref in $refs
245 case "$ref" in
246 +$remote_name:*)
247 single_force=t
248 found="$ref"
249 break ;;
250 $remote_name:*)
251 found="$ref"
252 break ;;
253 esac
254 done
256 local_name=$(expr "$found" : '[^:]*:\(.*\)')
257 append_fetch_head "$sha1" "$remote" "$remote_name" "$remote_nick" "$local_name"
258 done || exit
260 esac
262 # If the original head was empty (i.e. no "master" yet), or
263 # if we were told not to worry, we do not have to check.
264 case ",$update_head_ok,$orig_head," in
265 *,, | t,* )
268 curr_head=$(cat "$GIT_DIR/HEAD" 2>/dev/null)
269 if test "$curr_head" != "$orig_head"
270 then
271 echo "$orig_head" >$GIT_DIR/HEAD
272 die "Cannot fetch into the current branch."
275 esac