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

Refactor run_update_hook to be more useful

This is a simple refactoring of run_update_hook to allow the function to be passed the name of the hook it runs and also to build the argument list from a list of struct commands, rather than just one struct command. The refactoring is to support new pre-receive and post-receive hooks that will be given the entire list of struct commands, rather than just one struct command. These new hooks will follow in another patch. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Shawn O. Pearce committed Mar 7, 2007 at 16:51 UTC c8dd277109c1cdb9422e474e6bd6e32a382291a0
1 file changed +44 -19
receive-pack.c
+44 -19
@@ -67,18 +67,45 @@ struct command {
67
68 static struct command *commands;
69
70 -static char update_hook[] = "hooks/update";
70 +static const char update_hook[] = "hooks/update";
71
72 -static int run_update_hook(const char *refname,
73 - char *old_hex, char *new_hex)
72 +static int run_hook(const char *hook_name,
73 + struct command *first_cmd,
74 + int single)
75 {
75 - int code;
76 + struct command *cmd;
77 + int argc, code;
78 + const char **argv;
79 +
80 + for (argc = 0, cmd = first_cmd; cmd; cmd = cmd->next) {
81 + if (!cmd->error_string)
82 + argc += 3;
83 + if (single)
84 + break;
85 + }
86
77 - if (access(update_hook, X_OK) < 0)
87 + if (!argc || access(hook_name, X_OK) < 0)
88 return 0;
79 - code = run_command_opt(RUN_COMMAND_NO_STDIN
80 - | RUN_COMMAND_STDOUT_TO_STDERR,
81 - update_hook, refname, old_hex, new_hex, NULL);
89 +
90 + argv = xmalloc(sizeof(*argv) * (2 + argc));
91 + argv[0] = hook_name;
92 + for (argc = 1, cmd = first_cmd; cmd; cmd = cmd->next) {
93 + if (!cmd->error_string) {
94 + argv[argc++] = xstrdup(cmd->ref_name);
95 + argv[argc++] = xstrdup(sha1_to_hex(cmd->old_sha1));
96 + argv[argc++] = xstrdup(sha1_to_hex(cmd->new_sha1));
97 + }
98 + if (single)
99 + break;
100 + }
101 + argv[argc] = NULL;
102 +
103 + code = run_command_v_opt(argv,
104 + RUN_COMMAND_NO_STDIN | RUN_COMMAND_STDOUT_TO_STDERR);
105 + while (--argc > 0)
106 + free((char*)argv[argc]);
107 + free(argv);
108 +
109 switch (code) {
110 case 0:
111 return 0;
@@ -91,11 +118,11 @@ static int run_update_hook(const char *refname,
118 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
119 return error("waitpid is confused");
120 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
94 - return error("%s died of signal", update_hook);
121 + return error("%s died of signal", hook_name);
122 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
96 - return error("%s died strangely", update_hook);
123 + return error("%s died strangely", hook_name);
124 default:
98 - error("%s exited with error code %d", update_hook, -code);
125 + error("%s exited with error code %d", hook_name, -code);
126 return -code;
127 }
128 }
@@ -105,7 +132,6 @@ static int update(struct command *cmd)
132 const char *name = cmd->ref_name;
133 unsigned char *old_sha1 = cmd->old_sha1;
134 unsigned char *new_sha1 = cmd->new_sha1;
108 - char new_hex[41], old_hex[41];
135 struct ref_lock *lock;
136
137 cmd->error_string = NULL;
@@ -115,13 +141,10 @@ static int update(struct command *cmd)
141 name);
142 }
143
118 - strcpy(new_hex, sha1_to_hex(new_sha1));
119 - strcpy(old_hex, sha1_to_hex(old_sha1));
120 -
144 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
145 cmd->error_string = "bad pack";
146 return error("unpack should have generated %s, "
124 - "but I can't find it!", new_hex);
147 + "but I can't find it!", sha1_to_hex(new_sha1));
148 }
149 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
150 !is_null_sha1(old_sha1) &&
@@ -140,7 +163,7 @@ static int update(struct command *cmd)
163 return error("denying non-fast forward;"
164 " you should pull first");
165 }
143 - if (run_update_hook(name, old_hex, new_hex)) {
166 + if (run_hook(update_hook, cmd, 1)) {
167 cmd->error_string = "hook declined";
168 return error("hook declined to update %s", name);
169 }
@@ -150,7 +173,8 @@ static int update(struct command *cmd)
173 cmd->error_string = "failed to delete";
174 return error("failed to delete %s", name);
175 }
153 - fprintf(stderr, "%s: %s -> deleted\n", name, old_hex);
176 + fprintf(stderr, "%s: %s -> deleted\n", name,
177 + sha1_to_hex(old_sha1));
178 }
179 else {
180 lock = lock_any_ref_for_update(name, old_sha1);
@@ -162,7 +186,8 @@ static int update(struct command *cmd)
186 cmd->error_string = "failed to write";
187 return -1; /* error() already called */
188 }
165 - fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
189 + fprintf(stderr, "%s: %s -> %s\n", name,
190 + sha1_to_hex(old_sha1), sha1_to_hex(new_sha1));
191 }
192 return 0;
193 }