blob: b581dc4cd6fa7fe6406cf3b1e1e142118a52b00b [file] [log] [blame]
Linus Torvalds7ef76922005-05-22 11:03:24 -07001#!/bin/sh
2#
Linus Torvaldsb33e9662005-07-08 10:57:21 -07003. git-sh-setup-script || die "Not a git archive"
Junio C Hamano853a3692005-08-20 02:54:34 -07004. git-parse-remote-script
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"
Linus Torvaldsb33e9662005-07-08 10:57:21 -07007
Junio C Hamano853a3692005-08-20 02:54:34 -07008append=
Junio C Hamanoae2da402005-08-22 21:28:33 -07009force=
10while case "$#" in 0) break ;; esac
11do
12 case "$1" in
13 -a|--a|--ap|--app|--appe|--appen|--append)
14 append=t
15 shift
16 ;;
17 -f|--f|--fo|--for|--forc|--force)
18 force=t
19 shift
20 ;;
21 *)
22 break
23 ;;
24 esac
25done
26
Junio C Hamano853a3692005-08-20 02:54:34 -070027case "$#" in
280)
Junio C Hamano92c533e2005-08-20 03:00:03 -070029 test -f "$GIT_DIR/branches/origin" ||
30 test -f "$GIT_DIR/remotes/origin" ||
31 die "Where do you want to fetch from?"
32 set origin ;;
Junio C Hamano853a3692005-08-20 02:54:34 -070033esac
Junio C Hamanoae2da402005-08-22 21:28:33 -070034
Junio C Hamano853a3692005-08-20 02:54:34 -070035remote_nick="$1"
36remote=$(get_remote_url "$@")
37refs=
38rref=
39rsync_slurped_objects=
Linus Torvalds7ef76922005-05-22 11:03:24 -070040
Junio C Hamano853a3692005-08-20 02:54:34 -070041if test "" = "$append"
42then
43 : >$GIT_DIR/FETCH_HEAD
44fi
45
46append_fetch_head () {
47 head_="$1"
48 remote_="$2"
49 remote_name_="$3"
50 remote_nick_="$4"
51 local_name_="$5"
52
53 # 2.6.11-tree tag would not be happy to be fed to resolve.
54 if git-cat-file commit "$head_" >/dev/null 2>&1
55 then
Junio C Hamano8572aa82005-08-24 22:46:07 -070056 headc_=$(git-rev-parse --verify "$head_^0") || exit
57 note_="$headc_ $remote_name_ from $remote_nick_"
Junio C Hamano853a3692005-08-20 02:54:34 -070058 echo "$note_" >>$GIT_DIR/FETCH_HEAD
59 echo >&2 "* committish: $note_"
60 else
61 echo >&2 "* non-commit: $note_"
62 fi
63 if test "$local_name_" != ""
64 then
65 # We are storing the head locally. Make sure that it is
66 # a fast forward (aka "reverse push").
67 fast_forward_local "$local_name_" "$head_" "$remote_" "$remote_name_"
68 fi
69}
70
71fast_forward_local () {
72 case "$1" in
73 refs/tags/*)
74 # Tags need not be pointing at commits so there
75 # is no way to guarantee "fast-forward" anyway.
Junio C Hamanoae2da402005-08-22 21:28:33 -070076 if test -f "$GIT_DIR/$1"
77 then
78 echo >&2 "* $1: updating with $4"
79 echo >&2 " from $3."
80 else
81 echo >&2 "* $1: storing $4"
82 echo >&2 " from $3."
83 fi
Junio C Hamano853a3692005-08-20 02:54:34 -070084 echo "$2" >"$GIT_DIR/$1" ;;
Junio C Hamanoae2da402005-08-22 21:28:33 -070085
Junio C Hamano853a3692005-08-20 02:54:34 -070086 refs/heads/*)
87 # NEEDSWORK: use the same cmpxchg protocol here.
88 echo "$2" >"$GIT_DIR/$1.lock"
89 if test -f "$GIT_DIR/$1"
90 then
91 local=$(git-rev-parse --verify "$1^0") &&
92 mb=$(git-merge-base "$local" "$2") &&
93 case "$2,$mb" in
94 $local,*)
95 echo >&2 "* $1: same as $4"
96 echo >&2 " from $3"
97 ;;
98 *,$local)
99 echo >&2 "* $1: fast forward to $4"
100 echo >&2 " from $3"
101 ;;
102 *)
103 false
104 ;;
105 esac || {
Junio C Hamano853a3692005-08-20 02:54:34 -0700106 echo >&2 "* $1: does not fast forward to $4"
Junio C Hamanoefe9bf02005-08-22 22:52:43 -0700107 case "$force,$single_force" in
108 t,* | *,t)
Junio C Hamanoae2da402005-08-22 21:28:33 -0700109 echo >&2 " from $3; forcing update."
110 ;;
111 *)
112 mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1.remote"
113 echo >&2 " from $3; leaving it in '$1.remote'"
114 ;;
115 esac
Junio C Hamano853a3692005-08-20 02:54:34 -0700116 }
117 else
118 echo >&2 "* $1: storing $4"
119 echo >&2 " from $3."
120 fi
121 test -f "$GIT_DIR/$1.lock" &&
122 mv "$GIT_DIR/$1.lock" "$GIT_DIR/$1"
Linus Torvalds0a623e72005-07-05 11:38:37 -0700123 ;;
Junio C Hamano853a3692005-08-20 02:54:34 -0700124 esac
125}
126
127for ref in $(get_remote_refs_for_fetch "$@")
128do
129 refs="$refs $ref"
130
131 # These are relative path from $GIT_DIR, typically starting at refs/
132 # but may be HEAD
Junio C Hamanoefe9bf02005-08-22 22:52:43 -0700133 if expr "$ref" : '\+' >/dev/null
134 then
135 single_force=t
136 ref=$(expr "$ref" : '\+\(.*\)')
137 else
138 single_force=
139 fi
Junio C Hamano853a3692005-08-20 02:54:34 -0700140 remote_name=$(expr "$ref" : '\([^:]*\):')
141 local_name=$(expr "$ref" : '[^:]*:\(.*\)')
142
143 rref="$rref $remote_name"
144
145 # There are transports that can fetch only one head at a time...
146 case "$remote" in
147 http://* | https://*)
148 if [ -n "$GIT_SSL_NO_VERIFY" ]; then
149 curl_extra_args="-k"
150 fi
151 head=$(curl -nsf $curl_extra_args "$remote/$remote_name") &&
152 expr "$head" : "$_x40\$" >/dev/null ||
153 die "Failed to fetch $remote_name from $remote"
154 echo Fetching "$remote_name from $remote" using http
155 git-http-pull -v -a "$head" "$remote/" || exit
156 ;;
157 rsync://*)
158 TMP_HEAD="$GIT_DIR/TMP_HEAD"
159 rsync -L "$remote/$remote_name" "$TMP_HEAD" || exit 1
Linus Torvalds0a623e72005-07-05 11:38:37 -0700160 head=$(git-rev-parse TMP_HEAD)
161 rm -f "$TMP_HEAD"
Junio C Hamano853a3692005-08-20 02:54:34 -0700162 test "$rsync_slurped_objects" || {
163 rsync -avz --ignore-existing "$remote/objects/" \
164 "$GIT_OBJECT_DIRECTORY/" || exit
165 rsync_slurped_objects=t
166 }
Linus Torvalds0a623e72005-07-05 11:38:37 -0700167 ;;
Junio C Hamano853a3692005-08-20 02:54:34 -0700168 *)
169 # We will do git native transport with just one call later.
170 continue ;;
171 esac
172
173 append_fetch_head "$head" "$remote" "$remote_name" "$remote_nick" "$local_name"
174
175done
176
177case "$remote" in
178http://* | https://* | rsync://* )
179 ;; # we are already done.
Linus Torvalds0a623e72005-07-05 11:38:37 -0700180*)
Junio C Hamano853a3692005-08-20 02:54:34 -0700181 git-fetch-pack "$remote" $rref |
182 while read sha1 remote_name
183 do
184 found=
Junio C Hamanoefe9bf02005-08-22 22:52:43 -0700185 single_force=
Junio C Hamano853a3692005-08-20 02:54:34 -0700186 for ref in $refs
187 do
188 case "$ref" in
Junio C Hamanoefe9bf02005-08-22 22:52:43 -0700189 +$remote_name:*)
190 single_force=t
191 found="$ref"
192 break ;;
Junio C Hamano853a3692005-08-20 02:54:34 -0700193 $remote_name:*)
194 found="$ref"
195 break ;;
196 esac
197 done
Junio C Hamanof170e4b2005-07-16 00:16:24 -0700198
Junio C Hamano853a3692005-08-20 02:54:34 -0700199 local_name=$(expr "$found" : '[^:]*:\(.*\)')
Junio C Hamanoefe9bf02005-08-22 22:52:43 -0700200 append_fetch_head "$sha1" "$remote" "$remote_name" "$remote_nick" "$local_name"
Junio C Hamano853a3692005-08-20 02:54:34 -0700201 done
202 ;;
203esac