Documentation: multi-head fetch.
[git.git] / git-fetch-script
1 #!/bin/sh
2 #
3 . git-sh-setup-script || die "Not a git archive"
4 . 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"
7
8 append=
9 force=
10 while case "$#" in 0) break ;; esac
11 do
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
25 done
26
27 case "$#" in
28 0)
29         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 ;;
33 esac
34
35 remote_nick="$1"
36 remote=$(get_remote_url "$@")
37 refs=
38 rref=
39 rsync_slurped_objects=
40
41 if test "" = "$append"
42 then
43         : >$GIT_DIR/FETCH_HEAD
44 fi
45
46 append_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
56         head_=$(git-rev-parse --verify "$head_^0") || exit
57         note_="$head_   $remote_name_ from $remote_nick_"
58         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
71 fast_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.
76         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
84         echo "$2" >"$GIT_DIR/$1" ;;
85
86     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 || {
106                 echo >&2 "* $1: does not fast forward to $4"
107                 case "$force,$single_force" in
108                 t,* | *,t)
109                         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
116             }
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"
123         ;;
124     esac
125 }
126
127 for ref in $(get_remote_refs_for_fetch "$@")
128 do
129     refs="$refs $ref"
130
131     # These are relative path from $GIT_DIR, typically starting at refs/
132     # but may be HEAD
133     if expr "$ref" : '\+' >/dev/null
134     then
135         single_force=t
136         ref=$(expr "$ref" : '\+\(.*\)')
137     else
138         single_force=
139     fi
140     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
160         head=$(git-rev-parse TMP_HEAD)
161         rm -f "$TMP_HEAD"
162         test "$rsync_slurped_objects" || {
163             rsync -avz --ignore-existing "$remote/objects/" \
164                 "$GIT_OBJECT_DIRECTORY/" || exit
165             rsync_slurped_objects=t
166         }
167         ;;
168     *)
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
175 done
176
177 case "$remote" in
178 http://* | https://* | rsync://* )
179     ;; # we are already done.
180 *)
181     git-fetch-pack "$remote" $rref |
182     while read sha1 remote_name
183     do
184         found=
185         single_force=
186         for ref in $refs
187         do
188             case "$ref" in
189             +$remote_name:*)
190                 single_force=t
191                 found="$ref"
192                 break ;;
193             $remote_name:*)
194                 found="$ref"
195                 break ;;
196             esac
197         done
198
199         local_name=$(expr "$found" : '[^:]*:\(.*\)')
200         append_fetch_head "$sha1" "$remote" "$remote_name" "$remote_nick" "$local_name"
201     done
202     ;;
203 esac