]> asedeno.scripts.mit.edu Git - git.git/commitdiff
Merge branch 'tr/receive-pack-aliased-update-fix' into maint
authorJunio C Hamano <gitster@pobox.com>
Wed, 7 Jul 2010 17:25:15 +0000 (10:25 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 7 Jul 2010 17:25:15 +0000 (10:25 -0700)
* tr/receive-pack-aliased-update-fix:
  check_aliased_update: strcpy() instead of strcat() to copy
  receive-pack: detect aliased updates which can occur with symrefs
  receive-pack: switch global variable 'commands' to a parameter

Conflicts:
t/t5516-fetch-push.sh

1  2 
builtin/receive-pack.c
t/t5516-fetch-push.sh

diff --combined builtin/receive-pack.c
index 0559fcc871894548050e99c8c258561a0dcad5c9,7e4129ddc85e1408300947ff9c041524a1c10f48..7e4129ddc85e1408300947ff9c041524a1c10f48
@@@ -9,6 -9,7 +9,7 @@@
  #include "object.h"
  #include "remote.h"
  #include "transport.h"
+ #include "string-list.h"
  
  static const char receive_pack_usage[] = "git receive-pack <git-dir>";
  
@@@ -129,13 -130,12 +130,12 @@@ static void write_head_info(void
  struct command {
        struct command *next;
        const char *error_string;
+       unsigned int skip_update;
        unsigned char old_sha1[20];
        unsigned char new_sha1[20];
        char ref_name[FLEX_ARRAY]; /* more */
  };
  
- static struct command *commands;
  static const char pre_receive_hook[] = "hooks/pre-receive";
  static const char post_receive_hook[] = "hooks/post-receive";
  
@@@ -188,7 -188,7 +188,7 @@@ static int copy_to_sideband(int in, in
        return 0;
  }
  
- static int run_receive_hook(const char *hook_name)
+ static int run_receive_hook(struct command *commands, const char *hook_name)
  {
        static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
        struct command *cmd;
@@@ -447,15 -447,15 +447,15 @@@ static const char *update(struct comman
  
  static char update_post_hook[] = "hooks/post-update";
  
- static void run_update_post_hook(struct command *cmd)
+ static void run_update_post_hook(struct command *commands)
  {
-       struct command *cmd_p;
+       struct command *cmd;
        int argc;
        const char **argv;
        struct child_process proc;
  
-       for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
-               if (cmd_p->error_string)
+       for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
+               if (cmd->error_string)
                        continue;
                argc++;
        }
        argv = xmalloc(sizeof(*argv) * (2 + argc));
        argv[0] = update_post_hook;
  
-       for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
+       for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
                char *p;
-               if (cmd_p->error_string)
+               if (cmd->error_string)
                        continue;
-               p = xmalloc(strlen(cmd_p->ref_name) + 1);
-               strcpy(p, cmd_p->ref_name);
+               p = xmalloc(strlen(cmd->ref_name) + 1);
+               strcpy(p, cmd->ref_name);
                argv[argc] = p;
                argc++;
        }
        }
  }
  
- static void execute_commands(const char *unpacker_error)
+ static void check_aliased_update(struct command *cmd, struct string_list *list)
+ {
+       struct string_list_item *item;
+       struct command *dst_cmd;
+       unsigned char sha1[20];
+       char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
+       int flag;
+       const char *dst_name = resolve_ref(cmd->ref_name, sha1, 0, &flag);
+       if (!(flag & REF_ISSYMREF))
+               return;
+       if ((item = string_list_lookup(dst_name, list)) == NULL)
+               return;
+       cmd->skip_update = 1;
+       dst_cmd = (struct command *) item->util;
+       if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
+           !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
+               return;
+       dst_cmd->skip_update = 1;
+       strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
+       strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
+       strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
+       strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
+       rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
+                " its target '%s' (%s..%s)",
+                cmd->ref_name, cmd_oldh, cmd_newh,
+                dst_cmd->ref_name, dst_oldh, dst_newh);
+       cmd->error_string = dst_cmd->error_string =
+               "inconsistent aliased update";
+ }
+ static void check_aliased_updates(struct command *commands)
+ {
+       struct command *cmd;
+       struct string_list ref_list = { NULL, 0, 0, 0 };
+       for (cmd = commands; cmd; cmd = cmd->next) {
+               struct string_list_item *item =
+                       string_list_append(cmd->ref_name, &ref_list);
+               item->util = (void *)cmd;
+       }
+       sort_string_list(&ref_list);
+       for (cmd = commands; cmd; cmd = cmd->next)
+               check_aliased_update(cmd, &ref_list);
+       string_list_clear(&ref_list, 0);
+ }
+ static void execute_commands(struct command *commands, const char *unpacker_error)
  {
-       struct command *cmd = commands;
+       struct command *cmd;
        unsigned char sha1[20];
  
        if (unpacker_error) {
-               while (cmd) {
+               for (cmd = commands; cmd; cmd = cmd->next)
                        cmd->error_string = "n/a (unpacker error)";
-                       cmd = cmd->next;
-               }
                return;
        }
  
-       if (run_receive_hook(pre_receive_hook)) {
-               while (cmd) {
+       if (run_receive_hook(commands, pre_receive_hook)) {
+               for (cmd = commands; cmd; cmd = cmd->next)
                        cmd->error_string = "pre-receive hook declined";
-                       cmd = cmd->next;
-               }
                return;
        }
  
+       check_aliased_updates(commands);
        head_name = resolve_ref("HEAD", sha1, 0, NULL);
  
-       while (cmd) {
-               cmd->error_string = update(cmd);
-               cmd = cmd->next;
-       }
+       for (cmd = commands; cmd; cmd = cmd->next)
+               if (!cmd->skip_update)
+                       cmd->error_string = update(cmd);
  }
  
- static void read_head_info(void)
+ static struct command *read_head_info(void)
  {
+       struct command *commands = NULL;
        struct command **p = &commands;
        for (;;) {
                static char line[1000];
                        if (strstr(refname + reflen + 1, "side-band-64k"))
                                use_sideband = LARGE_PACKET_MAX;
                }
-               cmd = xmalloc(sizeof(struct command) + len - 80);
+               cmd = xcalloc(1, sizeof(struct command) + len - 80);
                hashcpy(cmd->old_sha1, old_sha1);
                hashcpy(cmd->new_sha1, new_sha1);
                memcpy(cmd->ref_name, line + 82, len - 81);
-               cmd->error_string = NULL;
-               cmd->next = NULL;
                *p = cmd;
                p = &cmd->next;
        }
+       return commands;
  }
  
  static const char *parse_pack_header(struct pack_header *hdr)
@@@ -643,7 -697,7 +697,7 @@@ static const char *unpack(void
        }
  }
  
- static void report(const char *unpack_status)
+ static void report(struct command *commands, const char *unpack_status)
  {
        struct command *cmd;
        struct strbuf buf = STRBUF_INIT;
        strbuf_release(&buf);
  }
  
- static int delete_only(struct command *cmd)
+ static int delete_only(struct command *commands)
  {
-       while (cmd) {
+       struct command *cmd;
+       for (cmd = commands; cmd; cmd = cmd->next) {
                if (!is_null_sha1(cmd->new_sha1))
                        return 0;
-               cmd = cmd->next;
        }
        return 1;
  }
@@@ -722,6 -776,7 +776,7 @@@ int cmd_receive_pack(int argc, const ch
        int stateless_rpc = 0;
        int i;
        char *dir = NULL;
+       struct command *commands;
  
        argv++;
        for (i = 1; i < argc; i++) {
        if (advertise_refs)
                return 0;
  
-       read_head_info();
-       if (commands) {
+       if ((commands = read_head_info()) != NULL) {
                const char *unpack_status = NULL;
  
                if (!delete_only(commands))
                        unpack_status = unpack();
-               execute_commands(unpack_status);
+               execute_commands(commands, unpack_status);
                if (pack_lockfile)
                        unlink_or_warn(pack_lockfile);
                if (report_status)
-                       report(unpack_status);
-               run_receive_hook(post_receive_hook);
+                       report(commands, unpack_status);
+               run_receive_hook(commands, post_receive_hook);
                run_update_post_hook(commands);
                if (auto_gc) {
                        const char *argv_gc_auto[] = {
diff --combined t/t5516-fetch-push.sh
index 6a37a4d993df3fa4958a719fdfdb82f0dd2de623,0f98332d5e5cea3a54ce53981e038cc2e91de910..b8f64e13865a0f5fe24d90e6864b58962e8d384c
@@@ -528,7 -528,7 +528,7 @@@ test_expect_success 'push does not upda
        mk_test heads/master &&
        mk_child child &&
        mkdir testrepo/.git/hooks &&
 -      echo exit 1 >testrepo/.git/hooks/pre-receive &&
 +      echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
        chmod +x testrepo/.git/hooks/pre-receive &&
        (cd child &&
                git pull .. master
@@@ -660,54 -660,53 +660,103 @@@ test_expect_success 'push with branche
        git checkout master
  '
  
+ test_expect_success 'push into aliased refs (consistent)' '
+       mk_test heads/master &&
+       mk_child child1 &&
+       mk_child child2 &&
+       (
+               cd child1 &&
+               git branch foo &&
+               git symbolic-ref refs/heads/bar refs/heads/foo
+               git config receive.denyCurrentBranch false
+       ) &&
+       (
+               cd child2 &&
+               >path2 &&
+               git add path2 &&
+               test_tick &&
+               git commit -a -m child2 &&
+               git branch foo &&
+               git branch bar &&
+               git push ../child1 foo bar
+       )
+ '
+ test_expect_success 'push into aliased refs (inconsistent)' '
+       mk_test heads/master &&
+       mk_child child1 &&
+       mk_child child2 &&
+       (
+               cd child1 &&
+               git branch foo &&
+               git symbolic-ref refs/heads/bar refs/heads/foo
+               git config receive.denyCurrentBranch false
+       ) &&
+       (
+               cd child2 &&
+               >path2 &&
+               git add path2 &&
+               test_tick &&
+               git commit -a -m child2 &&
+               git branch foo &&
+               >path3 &&
+               git add path3 &&
+               test_tick &&
+               git commit -a -m child2 &&
+               git branch bar &&
+               test_must_fail git push ../child1 foo bar 2>stderr &&
+               grep "refusing inconsistent update" stderr
+       )
+ '
 +test_expect_success 'push --porcelain' '
 +      mk_empty &&
 +      echo >.git/foo  "To testrepo" &&
 +      echo >>.git/foo "*      refs/heads/master:refs/remotes/origin/master    [new branch]"  &&
 +      echo >>.git/foo "Done" &&
 +      git push >.git/bar --porcelain  testrepo refs/heads/master:refs/remotes/origin/master &&
 +      (
 +              cd testrepo &&
 +              r=$(git show-ref -s --verify refs/remotes/origin/master) &&
 +              test "z$r" = "z$the_commit" &&
 +              test 1 = $(git for-each-ref refs/remotes/origin | wc -l)
 +      ) &&
 +      test_cmp .git/foo .git/bar
 +'
 +
 +test_expect_success 'push --porcelain bad url' '
 +      mk_empty &&
 +      test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/master:refs/remotes/origin/master &&
 +      test_must_fail grep -q Done .git/bar
 +'
 +
 +test_expect_success 'push --porcelain rejected' '
 +      mk_empty &&
 +      git push testrepo refs/heads/master:refs/remotes/origin/master &&
 +      (cd testrepo &&
 +              git reset --hard origin/master^
 +              git config receive.denyCurrentBranch true) &&
 +
 +      echo >.git/foo  "To testrepo"  &&
 +      echo >>.git/foo "!      refs/heads/master:refs/heads/master     [remote rejected] (branch is currently checked out)" &&
 +
 +      test_must_fail git push >.git/bar --porcelain  testrepo refs/heads/master:refs/heads/master &&
 +      test_cmp .git/foo .git/bar
 +'
 +
 +test_expect_success 'push --porcelain --dry-run rejected' '
 +      mk_empty &&
 +      git push testrepo refs/heads/master:refs/remotes/origin/master &&
 +      (cd testrepo &&
 +              git reset --hard origin/master
 +              git config receive.denyCurrentBranch true) &&
 +
 +      echo >.git/foo  "To testrepo"  &&
 +      echo >>.git/foo "!      refs/heads/master^:refs/heads/master    [rejected] (non-fast-forward)" &&
 +      echo >>.git/foo "Done" &&
 +
 +      test_must_fail git push >.git/bar --porcelain  --dry-run testrepo refs/heads/master^:refs/heads/master &&
 +      test_cmp .git/foo .git/bar
 +'
 +
  test_done