[PATCH] "git fetch --force".
[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" in
108                 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     remote_name=$(expr "$ref" : '\([^:]*\):')
134     local_name=$(expr "$ref" : '[^:]*:\(.*\)')
135
136     rref="$rref $remote_name"
137
138     # There are transports that can fetch only one head at a time...
139     case "$remote" in
140     http://* | https://*)
141         if [ -n "$GIT_SSL_NO_VERIFY" ]; then
142             curl_extra_args="-k"
143         fi
144         head=$(curl -nsf $curl_extra_args "$remote/$remote_name") &&
145         expr "$head" : "$_x40\$" >/dev/null ||
146             die "Failed to fetch $remote_name from $remote"
147         echo Fetching "$remote_name from $remote" using http
148         git-http-pull -v -a "$head" "$remote/" || exit
149         ;;
150     rsync://*)
151         TMP_HEAD="$GIT_DIR/TMP_HEAD"
152         rsync -L "$remote/$remote_name" "$TMP_HEAD" || exit 1
153         head=$(git-rev-parse TMP_HEAD)
154         rm -f "$TMP_HEAD"
155         test "$rsync_slurped_objects" || {
156             rsync -avz --ignore-existing "$remote/objects/" \
157                 "$GIT_OBJECT_DIRECTORY/" || exit
158             rsync_slurped_objects=t
159         }
160         ;;
161     *)
162         # We will do git native transport with just one call later.
163         continue ;;
164     esac
165
166     append_fetch_head "$head" "$remote" "$remote_name" "$remote_nick" "$local_name"
167
168 done
169
170 case "$remote" in
171 http://* | https://* | rsync://* )
172     ;; # we are already done.
173 *)
174     git-fetch-pack "$remote" $rref |
175     while read sha1 remote_name
176     do
177         found=
178         for ref in $refs
179         do
180             case "$ref" in
181             $remote_name:*)
182                 found="$ref"
183                 break ;;
184             esac
185         done
186
187         local_name=$(expr "$found" : '[^:]*:\(.*\)')
188         append_fetch_head "$sha1" "$remote" "$remote_name" "$remote_nick" "$local_name"
189     done
190     ;;
191 esac