From 5e1c71fd1488a33680c313f287b88d9f7a7d3e45 Mon Sep 17 00:00:00 2001 From: Jay Soffian Date: Mon, 19 Apr 2010 18:08:30 -0400 Subject: receive-pack: switch global variable 'commands' to a parameter Receive-pack is inconsistent in its usage of the 'commands' variable; though it is setup as a global and accessed that way by execute_commands(), report(), and run_receive_hook(), it is also passed as a parameter to delete_only() and run_update_post_hook(). For consistency, make it local to cmd_receive_pack and pass it as a parameter. As long as we're cleaning up, also make our use of the names 'commands' and 'cmd' consistent. Signed-off-by: Jay Soffian Signed-off-by: Junio C Hamano diff --git a/builtin-receive-pack.c b/builtin-receive-pack.c index 0559fcc..fffb6ea 100644 --- a/builtin-receive-pack.c +++ b/builtin-receive-pack.c @@ -134,8 +134,6 @@ struct command { 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 +186,7 @@ static int copy_to_sideband(int in, int out, void *arg) 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 +445,15 @@ static const char *update(struct command *cmd) 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++; } @@ -464,12 +462,12 @@ static void run_update_post_hook(struct command *cmd) 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++; } @@ -488,37 +486,32 @@ static void run_update_post_hook(struct command *cmd) } } -static void execute_commands(const char *unpacker_error) +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; } head_name = resolve_ref("HEAD", sha1, 0, NULL); - while (cmd) { + for (cmd = commands; cmd; cmd = cmd->next) cmd->error_string = update(cmd); - cmd = cmd->next; - } } -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]; @@ -557,6 +550,7 @@ static void read_head_info(void) *p = cmd; p = &cmd->next; } + return commands; } static const char *parse_pack_header(struct pack_header *hdr) @@ -643,7 +637,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; @@ -667,12 +661,12 @@ static void report(const char *unpack_status) 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 +716,7 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) int stateless_rpc = 0; int i; char *dir = NULL; + struct command *commands; argv++; for (i = 1; i < argc; i++) { @@ -772,18 +767,17 @@ int cmd_receive_pack(int argc, const char **argv, const char *prefix) 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[] = { -- cgit v0.10.2-6-g49f6 From da3efdb17bef25dedc753131462ee784d822132e Mon Sep 17 00:00:00 2001 From: Jay Soffian Date: Mon, 19 Apr 2010 18:19:18 -0400 Subject: 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 Signed-off-by: Junio C Hamano diff --git a/builtin-receive-pack.c b/builtin-receive-pack.c index fffb6ea..bb34757 100644 --- a/builtin-receive-pack.c +++ b/builtin-receive-pack.c @@ -9,6 +9,7 @@ #include "object.h" #include "remote.h" #include "transport.h" +#include "string-list.h" static const char receive_pack_usage[] = "git receive-pack "; @@ -129,6 +130,7 @@ 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 */ @@ -486,6 +488,63 @@ static void run_update_post_hook(struct command *commands) } } +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)); + strcat(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV)); + strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV)); + strcat(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; @@ -503,10 +562,13 @@ static void execute_commands(struct command *commands, const char *unpacker_erro return; } + check_aliased_updates(commands); + head_name = resolve_ref("HEAD", sha1, 0, NULL); for (cmd = commands; cmd; cmd = cmd->next) - cmd->error_string = update(cmd); + if (!cmd->skip_update) + cmd->error_string = update(cmd); } static struct command *read_head_info(void) @@ -541,12 +603,10 @@ static struct command *read_head_info(void) 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; } diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh index 0f04b2e..0f98332 100755 --- a/t/t5516-fetch-push.sh +++ b/t/t5516-fetch-push.sh @@ -660,4 +660,53 @@ test_expect_success 'push with branches containing #' ' 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_done -- cgit v0.10.2-6-g49f6 From d4785cd11dd0a55e30a1185a96910f0e57b6049a Mon Sep 17 00:00:00 2001 From: Jay Soffian Date: Mon, 19 Apr 2010 18:08:31 -0400 Subject: t5516-fetch-push.sh: style cleanup Cleanup t5516-fetch-push.sh to use prevailing test script style Signed-off-by: Jay Soffian Signed-off-by: Junio C Hamano diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh index 0f98332..f0813e0 100755 --- a/t/t5516-fetch-push.sh +++ b/t/t5516-fetch-push.sh @@ -64,13 +64,13 @@ check_push_result () { test_expect_success setup ' - : >path1 && + >path1 && git add path1 && test_tick && git commit -a -m repo && the_first_commit=$(git show-ref -s --verify refs/heads/master) && - : >path2 && + >path2 && git add path2 && test_tick && git commit -a -m second && @@ -483,8 +483,10 @@ git config --remove-section remote.there test_expect_success 'push with dry-run' ' mk_test heads/master && - (cd testrepo && - old_commit=$(git show-ref -s --verify refs/heads/master)) && + ( + cd testrepo && + old_commit=$(git show-ref -s --verify refs/heads/master) + ) && git push --dry-run testrepo && check_push_result $old_commit heads/master ' @@ -493,10 +495,13 @@ test_expect_success 'push updates local refs' ' mk_test heads/master && mk_child child && - (cd child && + ( + cd child && git pull .. master && git push && - test $(git rev-parse master) = $(git rev-parse remotes/origin/master)) + test $(git rev-parse master) = \ + $(git rev-parse remotes/origin/master) + ) ' @@ -506,10 +511,13 @@ test_expect_success 'push updates up-to-date local refs' ' mk_child child1 && mk_child child2 && (cd child1 && git pull .. master && git push) && - (cd child2 && + ( + cd child2 && git pull ../child1 master && git push && - test $(git rev-parse master) = $(git rev-parse remotes/origin/master)) + test $(git rev-parse master) = \ + $(git rev-parse remotes/origin/master) + ) ' @@ -517,9 +525,11 @@ test_expect_success 'push preserves up-to-date packed refs' ' mk_test heads/master && mk_child child && - (cd child && + ( + cd child && git push && - ! test -f .git/refs/remotes/origin/master) + ! test -f .git/refs/remotes/origin/master + ) ' @@ -530,11 +540,13 @@ test_expect_success 'push does not update local refs on failure' ' mkdir testrepo/.git/hooks && echo exit 1 >testrepo/.git/hooks/pre-receive && chmod +x testrepo/.git/hooks/pre-receive && - (cd child && + ( + cd child && git pull .. master test_must_fail git push && test $(git rev-parse master) != \ - $(git rev-parse remotes/origin/master)) + $(git rev-parse remotes/origin/master) + ) ' @@ -575,34 +587,41 @@ test_expect_success 'push --delete refuses src:dest refspecs' ' test_expect_success 'warn on push to HEAD of non-bare repository' ' mk_test heads/master - (cd testrepo && + ( + cd testrepo && git checkout master && - git config receive.denyCurrentBranch warn) && + git config receive.denyCurrentBranch warn + ) && git push testrepo master 2>stderr && grep "warning: updating the current branch" stderr ' test_expect_success 'deny push to HEAD of non-bare repository' ' mk_test heads/master - (cd testrepo && + ( + cd testrepo && git checkout master && - git config receive.denyCurrentBranch true) && + git config receive.denyCurrentBranch true + ) && test_must_fail git push testrepo master ' test_expect_success 'allow push to HEAD of bare repository (bare)' ' mk_test heads/master - (cd testrepo && + ( + cd testrepo && git checkout master && git config receive.denyCurrentBranch true && - git config core.bare true) && + git config core.bare true + ) && git push testrepo master 2>stderr && ! grep "warning: updating the current branch" stderr ' test_expect_success 'allow push to HEAD of non-bare repository (config)' ' mk_test heads/master - (cd testrepo && + ( + cd testrepo && git checkout master && git config receive.denyCurrentBranch false ) && @@ -615,7 +634,8 @@ test_expect_success 'fetch with branches' ' git branch second $the_first_commit && git checkout second && echo ".." > testrepo/.git/branches/branch1 && - (cd testrepo && + ( + cd testrepo && git fetch branch1 && r=$(git show-ref -s --verify refs/heads/branch1) && test "z$r" = "z$the_commit" && @@ -627,7 +647,8 @@ test_expect_success 'fetch with branches' ' test_expect_success 'fetch with branches containing #' ' mk_empty && echo "..#second" > testrepo/.git/branches/branch2 && - (cd testrepo && + ( + cd testrepo && git fetch branch2 && r=$(git show-ref -s --verify refs/heads/branch2) && test "z$r" = "z$the_first_commit" && @@ -641,7 +662,8 @@ test_expect_success 'push with branches' ' git checkout second && echo "testrepo" > .git/branches/branch1 && git push branch1 && - (cd testrepo && + ( + cd testrepo && r=$(git show-ref -s --verify refs/heads/master) && test "z$r" = "z$the_first_commit" && test 1 = $(git for-each-ref refs/heads | wc -l) @@ -652,7 +674,8 @@ test_expect_success 'push with branches containing #' ' mk_empty && echo "testrepo#branch3" > .git/branches/branch2 && git push branch2 && - (cd testrepo && + ( + cd testrepo && r=$(git show-ref -s --verify refs/heads/branch3) && test "z$r" = "z$the_first_commit" && test 1 = $(git for-each-ref refs/heads | wc -l) -- cgit v0.10.2-6-g49f6