@matheusbsbs / matheus-bagliano / commits / 20e8b465a5
Fork 0

refactor ref status logic for pushing

Move the logic that detects up-to-date and non-fast-forward refs to a new function in remote.[ch], set_ref_status_for_push(). Make transport_push() invoke set_ref_status_for_push() before invoking the push_refs() implementation. (As a side-effect, the push_refs() implementation in transport-helper.c now knows of non-fast-forward pushes.) Removed logic for detecting up-to-date refs from the push_refs() implementation in transport-helper.c, as transport_push() has already done so for it. Make cmd_send_pack() invoke set_ref_status_for_push() before invoking send_pack(), as transport_push() can't do it for send_pack() here. Mark the test on the return status of non-fast-forward push to fail. Git now exits with success, as transport.c::transport_push() does not check for refs with status REF_STATUS_REJECT_NONFASTFORWARD nor does it indicate rejected pushes with its return value. Mark the test for ref status to succeed. As mentioned earlier, refs might be marked as non-fast-forwards, triggering the push status printing mechanism in transport.c. Signed-off-by: Tay Ray Chuan <rctay89@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Tay Ray Chuan committed Jan 8, 2010 at 10:12 UTC 20e8b465a53e651cc3f50bd60f39d577ecdb7722
6 files changed +77 -48
builtin-send-pack.c
+12 -39
@@ -406,50 +406,20 @@ int send_pack(struct send_pack_args *args,
406 */
407 new_refs = 0;
408 for (ref = remote_refs; ref; ref = ref->next) {
409 -
410 - if (ref->peer_ref)
411 - hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
412 - else if (!args->send_mirror)
409 + if (!ref->peer_ref && !args->send_mirror)
410 continue;
411
415 - ref->deletion = is_null_sha1(ref->new_sha1);
416 - if (ref->deletion && !allow_deleting_refs) {
417 - ref->status = REF_STATUS_REJECT_NODELETE;
418 - continue;
419 - }
420 - if (!ref->deletion &&
421 - !hashcmp(ref->old_sha1, ref->new_sha1)) {
422 - ref->status = REF_STATUS_UPTODATE;
412 + /* Check for statuses set by set_ref_status_for_push() */
413 + switch (ref->status) {
414 + case REF_STATUS_REJECT_NONFASTFORWARD:
415 + case REF_STATUS_UPTODATE:
416 continue;
417 + default:
418 + ; /* do nothing */
419 }
420
426 - /* This part determines what can overwrite what.
427 - * The rules are:
428 - *
429 - * (0) you can always use --force or +A:B notation to
430 - * selectively force individual ref pairs.
431 - *
432 - * (1) if the old thing does not exist, it is OK.
433 - *
434 - * (2) if you do not have the old thing, you are not allowed
435 - * to overwrite it; you would not know what you are losing
436 - * otherwise.
437 - *
438 - * (3) if both new and old are commit-ish, and new is a
439 - * descendant of old, it is OK.
440 - *
441 - * (4) regardless of all of the above, removing :B is
442 - * always allowed.
443 - */
444 -
445 - ref->nonfastforward =
446 - !ref->deletion &&
447 - !is_null_sha1(ref->old_sha1) &&
448 - (!has_sha1_file(ref->old_sha1)
449 - || !ref_newer(ref->new_sha1, ref->old_sha1));
450 -
451 - if (ref->nonfastforward && !ref->force && !args->force_update) {
452 - ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
421 + if (ref->deletion && !allow_deleting_refs) {
422 + ref->status = REF_STATUS_REJECT_NODELETE;
423 continue;
424 }
425
@@ -673,6 +643,9 @@ int cmd_send_pack(int argc, const char **argv, const char *prefix)
643 if (match_refs(local_refs, &remote_refs, nr_refspecs, refspecs, flags))
644 return -1;
645
646 + set_ref_status_for_push(remote_refs, args.send_mirror,
647 + args.force_update);
648 +
649 ret = send_pack(&args, fd, conn, remote_refs, &extra_have);
650
651 if (helper_status)
remote.c
+50
@@ -1247,6 +1247,56 @@ int match_refs(struct ref *src, struct ref **dst,
1247 return 0;
1248 }
1249
1250 +void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
1251 + int force_update)
1252 +{
1253 + struct ref *ref;
1254 +
1255 + for (ref = remote_refs; ref; ref = ref->next) {
1256 + if (ref->peer_ref)
1257 + hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
1258 + else if (!send_mirror)
1259 + continue;
1260 +
1261 + ref->deletion = is_null_sha1(ref->new_sha1);
1262 + if (!ref->deletion &&
1263 + !hashcmp(ref->old_sha1, ref->new_sha1)) {
1264 + ref->status = REF_STATUS_UPTODATE;
1265 + continue;
1266 + }
1267 +
1268 + /* This part determines what can overwrite what.
1269 + * The rules are:
1270 + *
1271 + * (0) you can always use --force or +A:B notation to
1272 + * selectively force individual ref pairs.
1273 + *
1274 + * (1) if the old thing does not exist, it is OK.
1275 + *
1276 + * (2) if you do not have the old thing, you are not allowed
1277 + * to overwrite it; you would not know what you are losing
1278 + * otherwise.
1279 + *
1280 + * (3) if both new and old are commit-ish, and new is a
1281 + * descendant of old, it is OK.
1282 + *
1283 + * (4) regardless of all of the above, removing :B is
1284 + * always allowed.
1285 + */
1286 +
1287 + ref->nonfastforward =
1288 + !ref->deletion &&
1289 + !is_null_sha1(ref->old_sha1) &&
1290 + (!has_sha1_file(ref->old_sha1)
1291 + || !ref_newer(ref->new_sha1, ref->old_sha1));
1292 +
1293 + if (ref->nonfastforward && !ref->force && !force_update) {
1294 + ref->status = REF_STATUS_REJECT_NONFASTFORWARD;
1295 + continue;
1296 + }
1297 + }
1298 +}
1299 +
1300 struct branch *branch_get(const char *name)
1301 {
1302 struct branch *ret;
remote.h
+2
@@ -98,6 +98,8 @@ char *apply_refspecs(struct refspec *refspecs, int nr_refspec,
98
99 int match_refs(struct ref *src, struct ref **dst,
100 int nr_refspec, const char **refspec, int all);
101 +void set_ref_status_for_push(struct ref *remote_refs, int send_mirror,
102 + int force_update);
103
104 /*
105 * Given a list of the remote refs and the specification of things to
t/t5541-http-push.sh
+2 -2
@@ -88,7 +88,7 @@ test_expect_success 'used receive-pack service' '
88 test_cmp exp act
89 '
90
91 -test_expect_success 'non-fast-forward push fails' '
91 +test_expect_failure 'non-fast-forward push fails' '
92 cd "$ROOT_PATH"/test_repo_clone &&
93 git checkout master &&
94 echo "changed" > path2 &&
@@ -100,7 +100,7 @@ test_expect_success 'non-fast-forward push fails' '
100 test $HEAD != $(git rev-parse --verify HEAD))
101 '
102
103 -test_expect_failure 'non-fast-forward push show ref status' '
103 +test_expect_success 'non-fast-forward push show ref status' '
104 grep "^ ! \[rejected\][ ]*master -> master (non-fast-forward)$" output
105 '
106
transport-helper.c
+7 -7
@@ -329,16 +329,16 @@ static int push_refs(struct transport *transport,
329 return 1;
330
331 for (ref = remote_refs; ref; ref = ref->next) {
332 - if (ref->peer_ref)
333 - hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
334 - else if (!mirror)
332 + if (!ref->peer_ref && !mirror)
333 continue;
334
337 - ref->deletion = is_null_sha1(ref->new_sha1);
338 - if (!ref->deletion &&
339 - !hashcmp(ref->old_sha1, ref->new_sha1)) {
340 - ref->status = REF_STATUS_UPTODATE;
335 + /* Check for statuses set by set_ref_status_for_push() */
336 + switch (ref->status) {
337 + case REF_STATUS_REJECT_NONFASTFORWARD:
338 + case REF_STATUS_UPTODATE:
339 continue;
340 + default:
341 + ; /* do nothing */
342 }
343
344 if (force_all)
transport.c
+4
@@ -887,6 +887,10 @@ int transport_push(struct transport *transport,
887 return -1;
888 }
889
890 + set_ref_status_for_push(remote_refs,
891 + flags & TRANSPORT_PUSH_MIRROR,
892 + flags & TRANSPORT_PUSH_FORCE);
893 +
894 ret = transport->push_refs(transport, remote_refs, flags);
895
896 if (!quiet || push_had_errors(remote_refs))