{"thread":{"id":"2928","subject":"[PATCH] send-pack/receive-pack: allow errors to be reported back to pusher.","startedAt":"2005-12-26T07:25:05Z","lastAt":"2005-12-26T07:25:05Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"14039","messageId":"7v7j9sl3qm.fsf@assigned-by-dhcp.cox.net","threadId":"2928","inReplyTo":null,"subject":"[PATCH] send-pack/receive-pack: allow errors to be reported back to pusher.","fromName":"Junio C Hamano","fromEmail":"junkio@cox.net","sentAt":"2005-12-26T07:25:05Z","receivedAt":"2005-12-26T07:25:05Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"This updates the protocol between git-send-pack/git-receive-pack\nin a backward compatible way to allow failures at the receiving\nend to be propagated back to the sender.  Most notably, versions\nof git-push before this could not notice if the update hook on\nthe receiving end refused to update the ref for its own policy\nreasons.\n\nSigned-off-by: Junio C Hamano <junkio@cox.net>\n\n---\n\n * This is something I've been meaning to do since I saw Pasky's\n   patch to allow Cogito to notice the failure from\n   git-send-pack.\n\n   I am reasonably sure that the protocol is backward compatible\n   and you should be able run older git-receive-pack with this\n   git-send-pack (the older one does not identify itself of\n   capable to report status, and the sender does not ask for\n   it), and this git-receive-pack can be driven with older\n   git-send-pack (the older one will ignore the funny ref that\n   hides the capability string).  Tests and comments are very\n   much appreciated.\n\n connect.c      |    8 ++---\n receive-pack.c |   98 ++++++++++++++++++++++++++++++++++++++++++--------------\n send-pack.c    |   59 ++++++++++++++++++++++++++++++++--\n 3 files changed, 134 insertions(+), 31 deletions(-)\n\nbe754924ffb9824453e8c8c6567e80789c552999\ndiff --git a/connect.c b/connect.c\nindex 6b6d613..d6f4e4c 100644\n--- a/connect.c\n+++ b/connect.c\n@@ -34,10 +34,6 @@ struct ref **get_remote_heads(int in, st\n \t\t\tdie(\"protocol error: expected sha/ref, got '%s'\", buffer);\n \t\tname = buffer + 41;\n \n-\t\tif (ignore_funny && 45 < len && !memcmp(name, \"refs/\", 5) &&\n-\t\t    check_ref_format(name + 5))\n-\t\t\tcontinue;\n-\n \t\tname_len = strlen(name);\n \t\tif (len != name_len + 41) {\n \t\t\tif (server_capabilities)\n@@ -45,6 +41,10 @@ struct ref **get_remote_heads(int in, st\n \t\t\tserver_capabilities = strdup(name + name_len + 1);\n \t\t}\n \n+\t\tif (ignore_funny && 45 < len && !memcmp(name, \"refs/\", 5) &&\n+\t\t    check_ref_format(name + 5))\n+\t\t\tcontinue;\n+\n \t\tif (nr_match && !path_match(name, nr_match, match))\n \t\t\tcontinue;\n \t\tref = xcalloc(1, sizeof(*ref) + len - 40);\ndiff --git a/receive-pack.c b/receive-pack.c\nindex 92878ec..6963545 100644\n--- a/receive-pack.c\n+++ b/receive-pack.c\n@@ -8,20 +8,26 @@ static const char receive_pack_usage[] =\n \n static const char unpacker[] = \"git-unpack-objects\";\n \n+static int report_status = 0;\n+\n static int show_ref(const char *path, const unsigned char *sha1)\n {\n \tpacket_write(1, \"%s %s\\n\", sha1_to_hex(sha1), path);\n \treturn 0;\n }\n \n+static char capabilities[] = \"report-status\";\n+\n static void write_head_info(void)\n {\n+\tpacket_write(1, \"%s capabilities^{}%c%s\\n\",\n+\t\t     sha1_to_hex(null_sha1), 0, capabilities);\n \tfor_each_ref(show_ref);\n }\n \n struct command {\n \tstruct command *next;\n-\tunsigned char updated;\n+\tconst char *error_string;\n \tunsigned char old_sha1[20];\n \tunsigned char new_sha1[20];\n \tchar ref_name[0];\n@@ -89,15 +95,20 @@ static int run_update_hook(const char *r\n \t}\n }\n \n-static int update(const char *name,\n-\t\t  unsigned char *old_sha1, unsigned char *new_sha1)\n+static int update(struct command *cmd)\n {\n+\tconst char *name = cmd->ref_name;\n+\tunsigned char *old_sha1 = cmd->old_sha1;\n+\tunsigned char *new_sha1 = cmd->new_sha1;\n \tchar new_hex[60], *old_hex, *lock_name;\n \tint newfd, namelen, written;\n \n-\tif (!strncmp(name, \"refs/\", 5) && check_ref_format(name + 5))\n+\tcmd->error_string = NULL;\n+\tif (!strncmp(name, \"refs/\", 5) && check_ref_format(name + 5)) {\n+\t\tcmd->error_string = \"funny refname\";\n \t\treturn error(\"refusing to create funny ref '%s' locally\",\n \t\t\t     name);\n+\t}\n \n \tnamelen = strlen(name);\n \tlock_name = xmalloc(namelen + 10);\n@@ -106,16 +117,19 @@ static int update(const char *name,\n \n \tstrcpy(new_hex, sha1_to_hex(new_sha1));\n \told_hex = sha1_to_hex(old_sha1);\n-\tif (!has_sha1_file(new_sha1))\n+\tif (!has_sha1_file(new_sha1)) {\n+\t\tcmd->error_string = \"bad pack\";\n \t\treturn error(\"unpack should have generated %s, \"\n \t\t\t     \"but I can't find it!\", new_hex);\n-\n+\t}\n \tsafe_create_leading_directories(lock_name);\n \n \tnewfd = open(lock_name, O_CREAT | O_EXCL | O_WRONLY, 0666);\n-\tif (newfd < 0)\n+\tif (newfd < 0) {\n+\t\tcmd->error_string = \"can't lock\";\n \t\treturn error(\"unable to create %s (%s)\",\n \t\t\t     lock_name, strerror(errno));\n+\t}\n \n \t/* Write the ref with an ending '\\n' */\n \tnew_hex[40] = '\\n';\n@@ -127,18 +141,22 @@ static int update(const char *name,\n \tclose(newfd);\n \tif (written != 41) {\n \t\tunlink(lock_name);\n+\t\tcmd->error_string = \"can't write\";\n \t\treturn error(\"unable to write %s\", lock_name);\n \t}\n \tif (verify_old_ref(name, old_hex) < 0) {\n \t\tunlink(lock_name);\n+\t\tcmd->error_string = \"raced\";\n \t\treturn error(\"%s changed during push\", name);\n \t}\n \tif (run_update_hook(name, old_hex, new_hex)) {\n \t\tunlink(lock_name);\n+\t\tcmd->error_string = \"hook declined\";\n \t\treturn error(\"hook declined to update %s\\n\", name);\n \t}\n \telse if (rename(lock_name, name) < 0) {\n \t\tunlink(lock_name);\n+\t\tcmd->error_string = \"can't rename\";\n \t\treturn error(\"unable to replace %s\", name);\n \t}\n \telse {\n@@ -158,7 +176,7 @@ static void run_update_post_hook(struct \n \tif (access(update_post_hook, X_OK) < 0)\n \t\treturn;\n \tfor (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {\n-\t\tif (!cmd_p->updated)\n+\t\tif (cmd_p->error_string)\n \t\t\tcontinue;\n \t\targc++;\n \t}\n@@ -166,7 +184,7 @@ static void run_update_post_hook(struct \n \targv[0] = update_post_hook;\n \n \tfor (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {\n-\t\tif (!cmd_p->updated)\n+\t\tif (cmd_p->error_string)\n \t\t\tcontinue;\n \t\targv[argc] = xmalloc(strlen(cmd_p->ref_name) + 1);\n \t\tstrcpy(argv[argc], cmd_p->ref_name);\n@@ -185,8 +203,7 @@ static void execute_commands(void)\n \tstruct command *cmd = commands;\n \n \twhile (cmd) {\n-\t\tcmd->updated = !update(cmd->ref_name,\n-\t\t\t\t       cmd->old_sha1, cmd->new_sha1);\n+\t\tupdate(cmd);\n \t\tcmd = cmd->next;\n \t}\n \trun_update_post_hook(commands);\n@@ -199,7 +216,8 @@ static void read_head_info(void)\n \t\tstatic char line[1000];\n \t\tunsigned char old_sha1[20], new_sha1[20];\n \t\tstruct command *cmd;\n-\t\tint len;\n+\t\tchar *refname;\n+\t\tint len, reflen;\n \n \t\tlen = packet_read_line(0, line, sizeof(line));\n \t\tif (!len)\n@@ -211,38 +229,66 @@ static void read_head_info(void)\n \t\t    line[81] != ' ' ||\n \t\t    get_sha1_hex(line, old_sha1) ||\n \t\t    get_sha1_hex(line + 41, new_sha1))\n-\t\t\tdie(\"protocol error: expected old/new/ref, got '%s'\", line);\n+\t\t\tdie(\"protocol error: expected old/new/ref, got '%s'\",\n+\t\t\t    line);\n+\n+\t\trefname = line + 82;\n+\t\treflen = strlen(refname);\n+\t\tif (reflen + 82 < len) {\n+\t\t\tif (strstr(refname + reflen + 1, \"report-status\"))\n+\t\t\t\treport_status = 1;\n+\t\t}\n \t\tcmd = xmalloc(sizeof(struct command) + len - 80);\n \t\tmemcpy(cmd->old_sha1, old_sha1, 20);\n \t\tmemcpy(cmd->new_sha1, new_sha1, 20);\n \t\tmemcpy(cmd->ref_name, line + 82, len - 81);\n+\t\tcmd->error_string = \"n/a (unpacker error)\";\n \t\tcmd->next = NULL;\n \t\t*p = cmd;\n \t\tp = &cmd->next;\n \t}\n }\n \n-static void unpack(void)\n+static const char *unpack(int *error_code)\n {\n \tint code = run_command(unpacker, NULL);\n+\n+\t*error_code = 0;\n \tswitch (code) {\n \tcase 0:\n-\t\treturn;\n+\t\treturn NULL;\n \tcase -ERR_RUN_COMMAND_FORK:\n-\t\tdie(\"unpack fork failed\");\n+\t\treturn \"unpack fork failed\";\n \tcase -ERR_RUN_COMMAND_EXEC:\n-\t\tdie(\"unpack execute failed\");\n+\t\treturn \"unpack execute failed\";\n \tcase -ERR_RUN_COMMAND_WAITPID:\n-\t\tdie(\"waitpid failed\");\n+\t\treturn \"waitpid failed\";\n \tcase -ERR_RUN_COMMAND_WAITPID_WRONG_PID:\n-\t\tdie(\"waitpid is confused\");\n+\t\treturn \"waitpid is confused\";\n \tcase -ERR_RUN_COMMAND_WAITPID_SIGNAL:\n-\t\tdie(\"%s died of signal\", unpacker);\n+\t\treturn \"unpacker died of signal\";\n \tcase -ERR_RUN_COMMAND_WAITPID_NOEXIT:\n-\t\tdie(\"%s died strangely\", unpacker);\n+\t\treturn \"unpacker died strangely\";\n \tdefault:\n-\t\tdie(\"%s exited with error code %d\", unpacker, -code);\n+\t\t*error_code = -code;\n+\t\treturn \"unpacker exited with error code\";\n+\t}\n+}\n+\n+static void report(const char *unpack_status)\n+{\n+\tstruct command *cmd;\n+\tpacket_write(1, \"unpack %s\\n\",\n+\t\t     unpack_status ? unpack_status : \"ok\");\n+\tfor (cmd = commands; cmd; cmd = cmd->next) {\n+\t\tif (!cmd->error_string)\n+\t\t\tpacket_write(1, \"ok %s\\n\",\n+\t\t\t\t     cmd->ref_name);\n+\t\telse\n+\t\t\tpacket_write(1, \"ng %s %s\\n\",\n+\t\t\t\t     cmd->ref_name, cmd->error_string);\n \t}\n+\tpacket_flush(1);\n }\n \n int main(int argc, char **argv)\n@@ -275,8 +321,12 @@ int main(int argc, char **argv)\n \n \tread_head_info();\n \tif (commands) {\n-\t\tunpack();\n-\t\texecute_commands();\n+\t\tint code;\n+\t\tconst char *unpack_status = unpack(&code);\n+\t\tif (!unpack_status)\n+\t\t\texecute_commands();\n+\t\tif (report_status)\n+\t\t\treport(unpack_status);\n \t}\n \treturn 0;\n }\ndiff --git a/send-pack.c b/send-pack.c\nindex 0d41f9a..cd36193 100644\n--- a/send-pack.c\n+++ b/send-pack.c\n@@ -176,16 +176,53 @@ static void get_local_heads(void)\n \tfor_each_ref(one_local_ref);\n }\n \n+static int receive_status(int in)\n+{\n+\tchar line[1000];\n+\tint ret = 0;\n+\tint len = packet_read_line(in, line, sizeof(line));\n+\tif (len < 10 || memcmp(line, \"unpack \", 7)) {\n+\t\tfprintf(stderr, \"did not receive status back\\n\");\n+\t\treturn -1;\n+\t}\n+\tif (memcmp(line, \"unpack ok\\n\", 10)) {\n+\t\tfputs(line, stderr);\n+\t\tret = -1;\n+\t}\n+\twhile (1) {\n+\t\tlen = packet_read_line(in, line, sizeof(line));\n+\t\tif (!len)\n+\t\t\tbreak;\n+\t\tif (len < 3 ||\n+\t\t    (memcmp(line, \"ok\", 2) && memcmp(line, \"ng\", 2))) {\n+\t\t\tfprintf(stderr, \"protocol error: %s\\n\", line);\n+\t\t\tret = -1;\n+\t\t\tbreak;\n+\t\t}\n+\t\tif (!memcmp(line, \"ok\", 2))\n+\t\t\tcontinue;\n+\t\tfputs(line, stderr);\n+\t\tret = -1;\n+\t}\n+\treturn ret;\n+}\n+\n static int send_pack(int in, int out, int nr_refspec, char **refspec)\n {\n \tstruct ref *ref;\n \tint new_refs;\n \tint ret = 0;\n+\tint ask_for_status_report = 0;\n+\tint expect_status_report = 0;\n \n \t/* No funny business with the matcher */\n \tremote_tail = get_remote_heads(in, &remote_refs, 0, NULL, 1);\n \tget_local_heads();\n \n+\t/* Does the other end support the reporting? */\n+\tif (server_supports(\"report-status\"))\n+\t\task_for_status_report = 1;\n+\n \t/* match them up */\n \tif (!remote_tail)\n \t\tremote_tail = &remote_refs;\n@@ -260,7 +297,17 @@ static int send_pack(int in, int out, in\n \t\tnew_refs++;\n \t\tstrcpy(old_hex, sha1_to_hex(ref->old_sha1));\n \t\tnew_hex = sha1_to_hex(ref->new_sha1);\n-\t\tpacket_write(out, \"%s %s %s\", old_hex, new_hex, ref->name);\n+\n+\t\tif (ask_for_status_report) {\n+\t\t\tpacket_write(out, \"%s %s %s%c%s\",\n+\t\t\t\t     old_hex, new_hex, ref->name, 0,\n+\t\t\t\t     \"report-status\");\n+\t\t\task_for_status_report = 0;\n+\t\t\texpect_status_report = 1;\n+\t\t}\n+\t\telse\n+\t\t\tpacket_write(out, \"%s %s %s\",\n+\t\t\t\t     old_hex, new_hex, ref->name);\n \t\tfprintf(stderr, \"updating '%s'\", ref->name);\n \t\tif (strcmp(ref->name, ref->peer_ref->name))\n \t\t\tfprintf(stderr, \" using '%s'\", ref->peer_ref->name);\n@@ -270,9 +317,15 @@ static int send_pack(int in, int out, in\n \tpacket_flush(out);\n \tif (new_refs)\n \t\tpack_objects(out, remote_refs);\n-\telse if (ret == 0)\n-\t\tfprintf(stderr, \"Everything up-to-date\\n\");\n \tclose(out);\n+\n+\tif (expect_status_report) {\n+\t\tif (receive_status(in))\n+\t\t\tret = -4;\n+\t}\n+\n+\tif (!new_refs && ret == 0)\n+\t\tfprintf(stderr, \"Everything up-to-date\\n\");\n \treturn ret;\n }\n \n-- \n1.0.GIT\n"}]}