{"thread":{"id":"7143","subject":"[PATCH 3/5] Refactor run_update_hook to be more useful","startedAt":"2007-03-07T21:51:09Z","lastAt":"2007-03-07T21:51:09Z","messageCount":1,"participants":["Shawn O. Pearce"],"isPatch":true,"patchVersion":1,"patchTotal":5},"messages":[{"id":"36572","messageId":"20070307215109.GC28649@spearce.org","threadId":"7143","inReplyTo":null,"subject":"[PATCH 3/5] Refactor run_update_hook to be more useful","fromName":"Shawn O. Pearce","fromEmail":"spearce@spearce.org","sentAt":"2007-03-07T21:51:09Z","receivedAt":"2007-03-07T21:51:09Z","isPatch":true,"sender":{"key":"spearce@spearce.org","avatar":"https://avatars.githubusercontent.com/u/34844?v=4"},"body":"This is a simple refactoring of run_update_hook to allow the function\nto be passed the name of the hook it runs and also to build the\nargument list from a list of struct commands, rather than just one\nstruct command.\n\nThe refactoring is to support new pre-receive and post-receive\nhooks that will be given the entire list of struct commands,\nrather than just one struct command.  These new hooks will follow\nin another patch.\n\nSigned-off-by: Shawn O. Pearce <spearce@spearce.org>\n---\n receive-pack.c |   63 +++++++++++++++++++++++++++++++++++++++----------------\n 1 files changed, 44 insertions(+), 19 deletions(-)\n\ndiff --git a/receive-pack.c b/receive-pack.c\nindex e32e301..c55d905 100644\n--- a/receive-pack.c\n+++ b/receive-pack.c\n@@ -67,18 +67,45 @@ struct command {\n \n static struct command *commands;\n \n-static char update_hook[] = \"hooks/update\";\n+static const char update_hook[] = \"hooks/update\";\n \n-static int run_update_hook(const char *refname,\n-\t\t\t   char *old_hex, char *new_hex)\n+static int run_hook(const char *hook_name,\n+\tstruct command *first_cmd,\n+\tint single)\n {\n-\tint code;\n+\tstruct command *cmd;\n+\tint argc, code;\n+\tconst char **argv;\n+\n+\tfor (argc = 0, cmd = first_cmd; cmd; cmd = cmd->next) {\n+\t\tif (!cmd->error_string)\n+\t\t\targc += 3;\n+\t\tif (single)\n+\t\t\tbreak;\n+\t}\n \n-\tif (access(update_hook, X_OK) < 0)\n+\tif (!argc || access(hook_name, X_OK) < 0)\n \t\treturn 0;\n-\tcode = run_command_opt(RUN_COMMAND_NO_STDIN\n-\t\t| RUN_COMMAND_STDOUT_TO_STDERR,\n-\t\tupdate_hook, refname, old_hex, new_hex, NULL);\n+\n+\targv = xmalloc(sizeof(*argv) * (2 + argc));\n+\targv[0] = hook_name;\n+\tfor (argc = 1, cmd = first_cmd; cmd; cmd = cmd->next) {\n+\t\tif (!cmd->error_string) {\n+\t\t\targv[argc++] = xstrdup(cmd->ref_name);\n+\t\t\targv[argc++] = xstrdup(sha1_to_hex(cmd->old_sha1));\n+\t\t\targv[argc++] = xstrdup(sha1_to_hex(cmd->new_sha1));\n+\t\t}\n+\t\tif (single)\n+\t\t\tbreak;\n+\t}\n+\targv[argc] = NULL;\n+\n+\tcode = run_command_v_opt(argv,\n+\t\tRUN_COMMAND_NO_STDIN | RUN_COMMAND_STDOUT_TO_STDERR);\n+\twhile (--argc > 0)\n+\t\tfree((char*)argv[argc]);\n+\tfree(argv);\n+\n \tswitch (code) {\n \tcase 0:\n \t\treturn 0;\n@@ -91,11 +118,11 @@ static int run_update_hook(const char *refname,\n \tcase -ERR_RUN_COMMAND_WAITPID_WRONG_PID:\n \t\treturn error(\"waitpid is confused\");\n \tcase -ERR_RUN_COMMAND_WAITPID_SIGNAL:\n-\t\treturn error(\"%s died of signal\", update_hook);\n+\t\treturn error(\"%s died of signal\", hook_name);\n \tcase -ERR_RUN_COMMAND_WAITPID_NOEXIT:\n-\t\treturn error(\"%s died strangely\", update_hook);\n+\t\treturn error(\"%s died strangely\", hook_name);\n \tdefault:\n-\t\terror(\"%s exited with error code %d\", update_hook, -code);\n+\t\terror(\"%s exited with error code %d\", hook_name, -code);\n \t\treturn -code;\n \t}\n }\n@@ -105,7 +132,6 @@ static int update(struct command *cmd)\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[41], old_hex[41];\n \tstruct ref_lock *lock;\n \n \tcmd->error_string = NULL;\n@@ -115,13 +141,10 @@ static int update(struct command *cmd)\n \t\t\t     name);\n \t}\n \n-\tstrcpy(new_hex, sha1_to_hex(new_sha1));\n-\tstrcpy(old_hex, sha1_to_hex(old_sha1));\n-\n \tif (!is_null_sha1(new_sha1) && !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+\t\t\t     \"but I can't find it!\", sha1_to_hex(new_sha1));\n \t}\n \tif (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&\n \t    !is_null_sha1(old_sha1) &&\n@@ -140,7 +163,7 @@ static int update(struct command *cmd)\n \t\t\treturn error(\"denying non-fast forward;\"\n \t\t\t\t     \" you should pull first\");\n \t}\n-\tif (run_update_hook(name, old_hex, new_hex)) {\n+\tif (run_hook(update_hook, cmd, 1)) {\n \t\tcmd->error_string = \"hook declined\";\n \t\treturn error(\"hook declined to update %s\", name);\n \t}\n@@ -150,7 +173,8 @@ static int update(struct command *cmd)\n \t\t\tcmd->error_string = \"failed to delete\";\n \t\t\treturn error(\"failed to delete %s\", name);\n \t\t}\n-\t\tfprintf(stderr, \"%s: %s -> deleted\\n\", name, old_hex);\n+\t\tfprintf(stderr, \"%s: %s -> deleted\\n\", name,\n+\t\t\tsha1_to_hex(old_sha1));\n \t}\n \telse {\n \t\tlock = lock_any_ref_for_update(name, old_sha1);\n@@ -162,7 +186,8 @@ static int update(struct command *cmd)\n \t\t\tcmd->error_string = \"failed to write\";\n \t\t\treturn -1; /* error() already called */\n \t\t}\n-\t\tfprintf(stderr, \"%s: %s -> %s\\n\", name, old_hex, new_hex);\n+\t\tfprintf(stderr, \"%s: %s -> %s\\n\", name,\n+\t\t\tsha1_to_hex(old_sha1), sha1_to_hex(new_sha1));\n \t}\n \treturn 0;\n }\n-- \n1.5.0.3.895.g09890\n"}]}