@matheusbsbs / matheus-bagliano / commits / da3efdb17b
Fork 0

receive-pack: detect aliased updates which can occur with symrefs

When pushing to a remote repo the sending side filters out aliased updates (e.g., foo:baz bar:baz). However, it is not possible for the sender to know if two refs are aliased on the receiving side via symrefs. Here is one such scenario: $ git init origin $ (cd origin && touch file && git add file && git commit -a -m intial) $ git clone --bare origin origin.git $ rm -rf origin $ git clone origin.git client $ git clone --mirror client backup.git && $ (cd backup.git && git remote set-head origin --auto) $ (cd client && git remote add --mirror backup ../backup.git && echo change1 > file && git commit -a -m change1 && git push origin && git push backup ) The push to backup fails with: Counting objects: 5, done. Writing objects: 100% (3/3), 244 bytes, done. Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done. error: Ref refs/remotes/origin/master is at ef3... but expected 262... remote: error: failed to lock refs/remotes/origin/master To ../backup.git 262cd57..ef307ff master -> master 262cd57..ef307ff origin/HEAD -> origin/HEAD ! [remote rejected] origin/master -> origin/master (failed to lock) error: failed to push some refs to '../backup.git' The reason is that refs/remotes/origin/HEAD is a symref to refs/remotes/origin/master, but it is not possible for the sending side to unambiguously know this. This commit fixes the issue by having receive-pack ignore any update to a symref whose target is being identically updated. If a symref and its target are being updated inconsistently, then the update for both fails with an error message ("refusing inconsistent update...") to help diagnose the situation. Signed-off-by: Jay Soffian <jaysoffian@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jay Soffian committed Apr 19, 2010 at 18:19 UTC da3efdb17bef25dedc753131462ee784d822132e
2 files changed +113 -4
builtin-receive-pack.c
+64 -4
@@ -9,6 +9,7 @@
9 #include "object.h"
10 #include "remote.h"
11 #include "transport.h"
12 +#include "string-list.h"
13
14 static const char receive_pack_usage[] = "git receive-pack <git-dir>";
15
@@ -129,6 +130,7 @@ static void write_head_info(void)
130 struct command {
131 struct command *next;
132 const char *error_string;
133 + unsigned int skip_update;
134 unsigned char old_sha1[20];
135 unsigned char new_sha1[20];
136 char ref_name[FLEX_ARRAY]; /* more */
@@ -486,6 +488,63 @@ static void run_update_post_hook(struct command *commands)
488 }
489 }
490
491 +static void check_aliased_update(struct command *cmd, struct string_list *list)
492 +{
493 + struct string_list_item *item;
494 + struct command *dst_cmd;
495 + unsigned char sha1[20];
496 + char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
497 + int flag;
498 +
499 + const char *dst_name = resolve_ref(cmd->ref_name, sha1, 0, &flag);
500 +
501 + if (!(flag & REF_ISSYMREF))
502 + return;
503 +
504 + if ((item = string_list_lookup(dst_name, list)) == NULL)
505 + return;
506 +
507 + cmd->skip_update = 1;
508 +
509 + dst_cmd = (struct command *) item->util;
510 +
511 + if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
512 + !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
513 + return;
514 +
515 + dst_cmd->skip_update = 1;
516 +
517 + strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
518 + strcat(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
519 + strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
520 + strcat(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
521 + rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
522 + " its target '%s' (%s..%s)",
523 + cmd->ref_name, cmd_oldh, cmd_newh,
524 + dst_cmd->ref_name, dst_oldh, dst_newh);
525 +
526 + cmd->error_string = dst_cmd->error_string =
527 + "inconsistent aliased update";
528 +}
529 +
530 +static void check_aliased_updates(struct command *commands)
531 +{
532 + struct command *cmd;
533 + struct string_list ref_list = { NULL, 0, 0, 0 };
534 +
535 + for (cmd = commands; cmd; cmd = cmd->next) {
536 + struct string_list_item *item =
537 + string_list_append(cmd->ref_name, &ref_list);
538 + item->util = (void *)cmd;
539 + }
540 + sort_string_list(&ref_list);
541 +
542 + for (cmd = commands; cmd; cmd = cmd->next)
543 + check_aliased_update(cmd, &ref_list);
544 +
545 + string_list_clear(&ref_list, 0);
546 +}
547 +
548 static void execute_commands(struct command *commands, const char *unpacker_error)
549 {
550 struct command *cmd;
@@ -503,10 +562,13 @@ static void execute_commands(struct command *commands, const char *unpacker_erro
562 return;
563 }
564
565 + check_aliased_updates(commands);
566 +
567 head_name = resolve_ref("HEAD", sha1, 0, NULL);
568
569 for (cmd = commands; cmd; cmd = cmd->next)
509 - cmd->error_string = update(cmd);
570 + if (!cmd->skip_update)
571 + cmd->error_string = update(cmd);
572 }
573
574 static struct command *read_head_info(void)
@@ -541,12 +603,10 @@ static struct command *read_head_info(void)
603 if (strstr(refname + reflen + 1, "side-band-64k"))
604 use_sideband = LARGE_PACKET_MAX;
605 }
544 - cmd = xmalloc(sizeof(struct command) + len - 80);
606 + cmd = xcalloc(1, sizeof(struct command) + len - 80);
607 hashcpy(cmd->old_sha1, old_sha1);
608 hashcpy(cmd->new_sha1, new_sha1);
609 memcpy(cmd->ref_name, line + 82, len - 81);
548 - cmd->error_string = NULL;
549 - cmd->next = NULL;
610 *p = cmd;
611 p = &cmd->next;
612 }
t/t5516-fetch-push.sh
+49
@@ -660,4 +660,53 @@ test_expect_success 'push with branches containing #' '
660 git checkout master
661 '
662
663 +test_expect_success 'push into aliased refs (consistent)' '
664 + mk_test heads/master &&
665 + mk_child child1 &&
666 + mk_child child2 &&
667 + (
668 + cd child1 &&
669 + git branch foo &&
670 + git symbolic-ref refs/heads/bar refs/heads/foo
671 + git config receive.denyCurrentBranch false
672 + ) &&
673 + (
674 + cd child2 &&
675 + >path2 &&
676 + git add path2 &&
677 + test_tick &&
678 + git commit -a -m child2 &&
679 + git branch foo &&
680 + git branch bar &&
681 + git push ../child1 foo bar
682 + )
683 +'
684 +
685 +test_expect_success 'push into aliased refs (inconsistent)' '
686 + mk_test heads/master &&
687 + mk_child child1 &&
688 + mk_child child2 &&
689 + (
690 + cd child1 &&
691 + git branch foo &&
692 + git symbolic-ref refs/heads/bar refs/heads/foo
693 + git config receive.denyCurrentBranch false
694 + ) &&
695 + (
696 + cd child2 &&
697 + >path2 &&
698 + git add path2 &&
699 + test_tick &&
700 + git commit -a -m child2 &&
701 + git branch foo &&
702 + >path3 &&
703 + git add path3 &&
704 + test_tick &&
705 + git commit -a -m child2 &&
706 + git branch bar &&
707 + test_must_fail git push ../child1 foo bar 2>stderr &&
708 + grep "refusing inconsistent update" stderr
709 + )
710 +'
711 +
712 test_done