@matheusbsbs / matheus-bagliano / commits / 334f4831e5
Fork 0

send-pack: don't update tracking refs on error

Previously, we updated the tracking refs (which match refs we are pushing) while generating the list of refs to send. However, at that point we don't know whether the refs were accepted. Instead, we now wait until we get a response code from the server. If an error was indicated, we don't update any local tracking refs. Technically some refs could have been updated on the remote, but since the local ref update is just an optimization to avoid an extra fetch, we are better off erring on the side of correctness. The user-visible message is now generated much later in the program, and has been tweaked to make more sense. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Jeff King committed Oct 18, 2007 at 02:19 UTC 334f4831e5a779d42e521b770a26eae1ecb27e86
1 file changed +34 -16
send-pack.c
+34 -16
@@ -178,6 +178,35 @@ static int receive_status(int in)
178 return ret;
179 }
180
181 +static void update_tracking_ref(struct remote *remote, struct ref *ref)
182 +{
183 + struct refspec rs;
184 + int will_delete_ref;
185 +
186 + rs.src = ref->name;
187 + rs.dst = NULL;
188 +
189 + if (!ref->peer_ref)
190 + return;
191 +
192 + will_delete_ref = is_null_sha1(ref->peer_ref->new_sha1);
193 +
194 + if (!will_delete_ref &&
195 + !hashcmp(ref->old_sha1, ref->peer_ref->new_sha1))
196 + return;
197 +
198 + if (!remote_find_tracking(remote, &rs)) {
199 + fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst);
200 + if (is_null_sha1(ref->peer_ref->new_sha1)) {
201 + if (delete_ref(rs.dst, NULL))
202 + error("Failed to delete");
203 + } else
204 + update_ref("update by push", rs.dst,
205 + ref->new_sha1, NULL, 0, 0);
206 + free(rs.dst);
207 + }
208 +}
209 +
210 static int send_pack(int in, int out, struct remote *remote, int nr_refspec, char **refspec)
211 {
212 struct ref *ref;
@@ -306,22 +335,6 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
335 fprintf(stderr, "\n from %s\n to %s\n",
336 old_hex, new_hex);
337 }
309 - if (remote && !dry_run) {
310 - struct refspec rs;
311 - rs.src = ref->name;
312 - rs.dst = NULL;
313 - if (!remote_find_tracking(remote, &rs)) {
314 - fprintf(stderr, " Also local %s\n", rs.dst);
315 - if (will_delete_ref) {
316 - if (delete_ref(rs.dst, NULL)) {
317 - error("Failed to delete");
318 - }
319 - } else
320 - update_ref("update by push", rs.dst,
321 - ref->new_sha1, NULL, 0, 0);
322 - free(rs.dst);
323 - }
324 - }
338 }
339
340 packet_flush(out);
@@ -334,6 +347,11 @@ static int send_pack(int in, int out, struct remote *remote, int nr_refspec, cha
347 ret = -4;
348 }
349
350 + if (!dry_run && remote && ret == 0) {
351 + for (ref = remote_refs; ref; ref = ref->next)
352 + update_tracking_ref(remote, ref);
353 + }
354 +
355 if (!new_refs && ret == 0)
356 fprintf(stderr, "Everything up-to-date\n");
357 return ret;