82d897231a488d91d5408c7710dd481cf862171d
[git.git] / git-fetch.sh
1 #!/bin/sh
2 #
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"
7
8 append=
9 force=
10 update_head_ok=
11 while case "$#" in 0) break ;; esac
12 do
13         case "$1" in
14         -a|--a|--ap|--app|--appe|--appen|--append)
15                 append=t
16                 ;;
17         -f|--f|--fo|--for|--forc|--force)
18                 force=t
19                 ;;
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
24                 ;;
25         *)
26                 break
27                 ;;
28         esac
29         shift
30 done
31
32 case "$#" in
33 0)
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
39
40 remote_nick="$1"
41 remote=$(get_remote_url "$@")
42 refs=
43 rref=
44 rsync_slurped_objects=
45
46 if test "" = "$append"
47 then
48         : >$GIT_DIR/FETCH_HEAD
49 fi
50
51 append_fetch_head () {
52     head_="$1"
53     remote_="$2"
54     remote_name_="$3"
55     remote_nick_="$4"
56     local_name_="$5"
57     case "$6" in
58     t) not_for_merge_='not-for-merge' ;;
59     '') not_for_merge_= ;;
60     esac
61
62     # remote-nick is the URL given on the command line (or a shorthand)
63     # remote-name is the $GIT_DIR relative refs/ path we computed
64     # for this refspec.
65     case "$remote_name_" in
66     HEAD)
67         note_= ;;
68     refs/heads/*)
69         note_="$(expr "$remote_name_" : 'refs/heads/\(.*\)')"
70         note_="branch '$note_' of " ;;
71     refs/tags/*)
72         note_="$(expr "$remote_name_" : 'refs/tags/\(.*\)')"
73         note_="tag '$note_' of " ;;
74     *)
75         note_="$remote_name of " ;;
76     esac
77     remote_1_=$(expr "$remote_" : '\(.*\)\.git/*$') &&
78         remote_="$remote_1_"
79     note_="$note_$remote_"
80
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_   $not_for_merge_ $note_" >>$GIT_DIR/FETCH_HEAD
86         echo >&2 "* committish: $head_"
87         echo >&2 "  $note_"
88     else
89         echo "$head_    not-for-merge   $note_" >>$GIT_DIR/FETCH_HEAD
90         echo >&2 "* non-commit: $head_"
91         echo >&2 "  $note_"
92     fi
93     if test "$local_name_" != ""
94     then
95         # We are storing the head locally.  Make sure that it is
96         # a fast forward (aka "reverse push").
97         fast_forward_local "$local_name_" "$head_" "$note_"
98     fi
99 }
100
101 fast_forward_local () {
102     mkdir -p "$(dirname "$GIT_DIR/$1")"
103     case "$1" in
104     refs/tags/*)
105         # Tags need not be pointing at commits so there
106         # is no way to guarantee "fast-forward" anyway.
107         if test -f "$GIT_DIR/$1"
108         then
109                 echo >&2 "* $1: updating with $3"
110         else
111                 echo >&2 "* $1: storing $3"
112         fi
113         echo "$2" >"$GIT_DIR/$1" ;;
114
115     refs/heads/*)
116         # NEEDSWORK: use the same cmpxchg protocol here.
117         echo "$2" >"$GIT_DIR/$1.lock"
118         if test -f "$GIT_DIR/$1"
119         then
120             local=$(git-rev-parse --verify "$1^0") &&
121             mb=$(git-merge-base "$local" "$2") &&
122             case "$2,$mb" in
123             $local,*)
124                 echo >&2 "* $1: same as $3"
125                 ;;
126             *,$local)
127                 echo >&2 "* $1: fast forward to $3"
128                 ;;
129             *)
130                 false
131                 ;;
132             esac || {
133                 echo >&2 "* $1: does not fast forward to $3;"
134                 case "$force,$single_force" in
135                 t,* | *,t)
136                         echo >&2 "  forcing update."
137                         ;;
138                 *)
139                         mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1.remote"
140                         echo >&2 "  leaving it in '$1.remote'"
141                         ;;
142                 esac
143             }
144         else
145                 echo >&2 "* $1: storing $3"
146         fi
147         test -f "$GIT_DIR/$1.lock" &&
148             mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1"
149         ;;
150     esac
151 }
152
153 case "$update_head_ok" in
154 '')
155         orig_head=$(cat "$GIT_DIR/HEAD" 2>/dev/null)
156         ;;
157 esac
158
159 for ref in $(get_remote_refs_for_fetch "$@")
160 do
161     refs="$refs $ref"
162
163     # These are relative path from $GIT_DIR, typically starting at refs/
164     # but may be HEAD
165     if expr "$ref" : '\.' >/dev/null
166     then
167         not_for_merge=t
168         ref=$(expr "$ref" : '\.\(.*\)')
169     else
170         not_for_merge=
171     fi
172     if expr "$ref" : '\+' >/dev/null
173     then
174         single_force=t
175         ref=$(expr "$ref" : '\+\(.*\)')
176     else
177         single_force=
178     fi
179     remote_name=$(expr "$ref" : '\([^:]*\):')
180     local_name=$(expr "$ref" : '[^:]*:\(.*\)')
181
182     rref="$rref $remote_name"
183
184     # There are transports that can fetch only one head at a time...
185     case "$remote" in
186     http://* | https://*)
187         if [ -n "$GIT_SSL_NO_VERIFY" ]; then
188             curl_extra_args="-k"
189         fi
190         head=$(curl -nsf $curl_extra_args "$remote/$remote_name") &&
191         expr "$head" : "$_x40\$" >/dev/null ||
192                 die "Failed to fetch $remote_name from $remote"
193         echo >&2 Fetching "$remote_name from $remote" using http
194         git-http-fetch -v -a "$head" "$remote/" || exit
195         ;;
196     rsync://*)
197         TMP_HEAD="$GIT_DIR/TMP_HEAD"
198         rsync -L -q "$remote/$remote_name" "$TMP_HEAD" || exit 1
199         head=$(git-rev-parse TMP_HEAD)
200         rm -f "$TMP_HEAD"
201         test "$rsync_slurped_objects" || {
202             rsync -av --ignore-existing --exclude info \
203                 "$remote/objects/" "$GIT_OBJECT_DIRECTORY/" || exit
204
205             # Look at objects/info/alternates for rsync -- http will
206             # support it natively and git native ones will do it on the remote
207             # end.  Not having that file is not a crime.
208             rsync -q "$remote/objects/info/alternates" \
209                 "$GIT_DIR/TMP_ALT" 2>/dev/null ||
210                 rm -f "$GIT_DIR/TMP_ALT"
211             if test -f "$GIT_DIR/TMP_ALT"
212             then
213                 resolve_alternates "$remote" <"$GIT_DIR/TMP_ALT" |
214                 while read alt
215                 do
216                     case "$alt" in 'bad alternate: '*) die "$alt";; esac
217                     echo >&2 "Getting alternate: $alt"
218                     rsync -av --ignore-existing --exclude info \
219                     "$alt" "$GIT_OBJECT_DIRECTORY/" || exit
220                 done
221                 rm -f "$GIT_DIR/TMP_ALT"
222             fi
223             rsync_slurped_objects=t
224         }
225         ;;
226     *)
227         # We will do git native transport with just one call later.
228         continue ;;
229     esac
230
231     append_fetch_head "$head" "$remote" \
232         "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
233
234 done
235
236 case "$remote" in
237 http://* | https://* | rsync://* )
238     ;; # we are already done.
239 *)
240     (
241         git-fetch-pack "$remote" $rref || echo failed "$remote"
242     ) |
243     while read sha1 remote_name
244     do
245         case "$sha1" in
246         failed)
247                 echo >&2 "Fetch failure: $remote"
248                 exit 1 ;;
249         esac
250         found=
251         single_force=
252         for ref in $refs
253         do
254             case "$ref" in
255             +$remote_name:*)
256                 single_force=t
257                 not_for_merge=
258                 found="$ref"
259                 break ;;
260             .+$remote_name:*)
261                 single_force=t
262                 not_for_merge=t
263                 found="$ref"
264                 break ;;
265             .$remote_name:*)
266                 not_for_merge=t
267                 found="$ref"
268                 break ;;
269             $remote_name:*)
270                 not_for_merge=
271                 found="$ref"
272                 break ;;
273             esac
274         done
275         local_name=$(expr "$found" : '[^:]*:\(.*\)')
276         append_fetch_head "$sha1" "$remote" \
277                 "$remote_name" "$remote_nick" "$local_name" "$not_for_merge"
278     done || exit
279     ;;
280 esac
281
282 # If the original head was empty (i.e. no "master" yet), or
283 # if we were told not to worry, we do not have to check.
284 case ",$update_head_ok,$orig_head," in
285 *,, | t,* )
286         ;;
287 *)
288         curr_head=$(cat "$GIT_DIR/HEAD" 2>/dev/null)
289         if test "$curr_head" != "$orig_head"
290         then
291                 echo "$orig_head" >$GIT_DIR/HEAD
292                 die "Cannot fetch into the current branch."
293         fi
294         ;;
295 esac