Refactor run_update_hook to be more useful
[git/dscho.git] / receive-pack.c
blobc55d905d5c9511c073824c8ae759704cf2da897c
1 #include "cache.h"
2 #include "pack.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "run-command.h"
6 #include "exec_cmd.h"
7 #include "commit.h"
8 #include "object.h"
10 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
12 static int deny_non_fast_forwards = 0;
13 static int receive_unpack_limit = -1;
14 static int transfer_unpack_limit = -1;
15 static int unpack_limit = 100;
16 static int report_status;
18 static char capabilities[] = " report-status delete-refs ";
19 static int capabilities_sent;
21 static int receive_pack_config(const char *var, const char *value)
23 if (strcmp(var, "receive.denynonfastforwards") == 0) {
24 deny_non_fast_forwards = git_config_bool(var, value);
25 return 0;
28 if (strcmp(var, "receive.unpacklimit") == 0) {
29 receive_unpack_limit = git_config_int(var, value);
30 return 0;
33 if (strcmp(var, "transfer.unpacklimit") == 0) {
34 transfer_unpack_limit = git_config_int(var, value);
35 return 0;
38 return git_default_config(var, value);
41 static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
43 if (capabilities_sent)
44 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
45 else
46 packet_write(1, "%s %s%c%s\n",
47 sha1_to_hex(sha1), path, 0, capabilities);
48 capabilities_sent = 1;
49 return 0;
52 static void write_head_info(void)
54 for_each_ref(show_ref, NULL);
55 if (!capabilities_sent)
56 show_ref("capabilities^{}", null_sha1, 0, NULL);
60 struct command {
61 struct command *next;
62 const char *error_string;
63 unsigned char old_sha1[20];
64 unsigned char new_sha1[20];
65 char ref_name[FLEX_ARRAY]; /* more */
68 static struct command *commands;
70 static const char update_hook[] = "hooks/update";
72 static int run_hook(const char *hook_name,
73 struct command *first_cmd,
74 int single)
76 struct command *cmd;
77 int argc, code;
78 const char **argv;
80 for (argc = 0, cmd = first_cmd; cmd; cmd = cmd->next) {
81 if (!cmd->error_string)
82 argc += 3;
83 if (single)
84 break;
87 if (!argc || access(hook_name, X_OK) < 0)
88 return 0;
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));
98 if (single)
99 break;
101 argv[argc] = NULL;
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);
109 switch (code) {
110 case 0:
111 return 0;
112 case -ERR_RUN_COMMAND_FORK:
113 return error("hook fork failed");
114 case -ERR_RUN_COMMAND_EXEC:
115 return error("hook execute failed");
116 case -ERR_RUN_COMMAND_WAITPID:
117 return error("waitpid failed");
118 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
119 return error("waitpid is confused");
120 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
121 return error("%s died of signal", hook_name);
122 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
123 return error("%s died strangely", hook_name);
124 default:
125 error("%s exited with error code %d", hook_name, -code);
126 return -code;
130 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;
135 struct ref_lock *lock;
137 cmd->error_string = NULL;
138 if (!prefixcmp(name, "refs/") && check_ref_format(name + 5)) {
139 cmd->error_string = "funny refname";
140 return error("refusing to create funny ref '%s' locally",
141 name);
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, "
147 "but I can't find it!", sha1_to_hex(new_sha1));
149 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
150 !is_null_sha1(old_sha1) &&
151 !prefixcmp(name, "refs/heads/")) {
152 struct commit *old_commit, *new_commit;
153 struct commit_list *bases, *ent;
155 old_commit = (struct commit *)parse_object(old_sha1);
156 new_commit = (struct commit *)parse_object(new_sha1);
157 bases = get_merge_bases(old_commit, new_commit, 1);
158 for (ent = bases; ent; ent = ent->next)
159 if (!hashcmp(old_sha1, ent->item->object.sha1))
160 break;
161 free_commit_list(bases);
162 if (!ent)
163 return error("denying non-fast forward;"
164 " you should pull first");
166 if (run_hook(update_hook, cmd, 1)) {
167 cmd->error_string = "hook declined";
168 return error("hook declined to update %s", name);
171 if (is_null_sha1(new_sha1)) {
172 if (delete_ref(name, old_sha1)) {
173 cmd->error_string = "failed to delete";
174 return error("failed to delete %s", name);
176 fprintf(stderr, "%s: %s -> deleted\n", name,
177 sha1_to_hex(old_sha1));
179 else {
180 lock = lock_any_ref_for_update(name, old_sha1);
181 if (!lock) {
182 cmd->error_string = "failed to lock";
183 return error("failed to lock %s", name);
185 if (write_ref_sha1(lock, new_sha1, "push")) {
186 cmd->error_string = "failed to write";
187 return -1; /* error() already called */
189 fprintf(stderr, "%s: %s -> %s\n", name,
190 sha1_to_hex(old_sha1), sha1_to_hex(new_sha1));
192 return 0;
195 static char update_post_hook[] = "hooks/post-update";
197 static void run_update_post_hook(struct command *cmd)
199 struct command *cmd_p;
200 int argc;
201 const char **argv;
203 for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
204 if (cmd_p->error_string)
205 continue;
206 argc++;
208 if (!argc || access(update_post_hook, X_OK) < 0)
209 return;
210 argv = xmalloc(sizeof(*argv) * (2 + argc));
211 argv[0] = update_post_hook;
213 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
214 char *p;
215 if (cmd_p->error_string)
216 continue;
217 p = xmalloc(strlen(cmd_p->ref_name) + 1);
218 strcpy(p, cmd_p->ref_name);
219 argv[argc] = p;
220 argc++;
222 argv[argc] = NULL;
223 run_command_v_opt(argv, RUN_COMMAND_NO_STDIN
224 | RUN_COMMAND_STDOUT_TO_STDERR);
228 * This gets called after(if) we've successfully
229 * unpacked the data payload.
231 static void execute_commands(void)
233 struct command *cmd = commands;
234 while (cmd) {
235 update(cmd);
236 cmd = cmd->next;
240 static void read_head_info(void)
242 struct command **p = &commands;
243 for (;;) {
244 static char line[1000];
245 unsigned char old_sha1[20], new_sha1[20];
246 struct command *cmd;
247 char *refname;
248 int len, reflen;
250 len = packet_read_line(0, line, sizeof(line));
251 if (!len)
252 break;
253 if (line[len-1] == '\n')
254 line[--len] = 0;
255 if (len < 83 ||
256 line[40] != ' ' ||
257 line[81] != ' ' ||
258 get_sha1_hex(line, old_sha1) ||
259 get_sha1_hex(line + 41, new_sha1))
260 die("protocol error: expected old/new/ref, got '%s'",
261 line);
263 refname = line + 82;
264 reflen = strlen(refname);
265 if (reflen + 82 < len) {
266 if (strstr(refname + reflen + 1, "report-status"))
267 report_status = 1;
269 cmd = xmalloc(sizeof(struct command) + len - 80);
270 hashcpy(cmd->old_sha1, old_sha1);
271 hashcpy(cmd->new_sha1, new_sha1);
272 memcpy(cmd->ref_name, line + 82, len - 81);
273 cmd->error_string = "n/a (unpacker error)";
274 cmd->next = NULL;
275 *p = cmd;
276 p = &cmd->next;
280 static const char *parse_pack_header(struct pack_header *hdr)
282 switch (read_pack_header(0, hdr)) {
283 case PH_ERROR_EOF:
284 return "eof before pack header was fully read";
286 case PH_ERROR_PACK_SIGNATURE:
287 return "protocol error (pack signature mismatch detected)";
289 case PH_ERROR_PROTOCOL:
290 return "protocol error (pack version unsupported)";
292 default:
293 return "unknown error in parse_pack_header";
295 case 0:
296 return NULL;
300 static const char *pack_lockfile;
302 static const char *unpack(void)
304 struct pack_header hdr;
305 const char *hdr_err;
306 char hdr_arg[38];
308 hdr_err = parse_pack_header(&hdr);
309 if (hdr_err)
310 return hdr_err;
311 snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%u,%u",
312 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
314 if (ntohl(hdr.hdr_entries) < unpack_limit) {
315 int code;
316 const char *unpacker[3];
317 unpacker[0] = "unpack-objects";
318 unpacker[1] = hdr_arg;
319 unpacker[2] = NULL;
320 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
321 switch (code) {
322 case 0:
323 return NULL;
324 case -ERR_RUN_COMMAND_FORK:
325 return "unpack fork failed";
326 case -ERR_RUN_COMMAND_EXEC:
327 return "unpack execute failed";
328 case -ERR_RUN_COMMAND_WAITPID:
329 return "waitpid failed";
330 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
331 return "waitpid is confused";
332 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
333 return "unpacker died of signal";
334 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
335 return "unpacker died strangely";
336 default:
337 return "unpacker exited with error code";
339 } else {
340 const char *keeper[6];
341 int fd[2], s, len, status;
342 pid_t pid;
343 char keep_arg[256];
344 char packname[46];
346 s = sprintf(keep_arg, "--keep=receive-pack %i on ", getpid());
347 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
348 strcpy(keep_arg + s, "localhost");
350 keeper[0] = "index-pack";
351 keeper[1] = "--stdin";
352 keeper[2] = "--fix-thin";
353 keeper[3] = hdr_arg;
354 keeper[4] = keep_arg;
355 keeper[5] = NULL;
357 if (pipe(fd) < 0)
358 return "index-pack pipe failed";
359 pid = fork();
360 if (pid < 0)
361 return "index-pack fork failed";
362 if (!pid) {
363 dup2(fd[1], 1);
364 close(fd[1]);
365 close(fd[0]);
366 execv_git_cmd(keeper);
367 die("execv of index-pack failed");
369 close(fd[1]);
372 * The first thing we expects from index-pack's output
373 * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where
374 * %40s is the newly created pack SHA1 name. In the "keep"
375 * case, we need it to remove the corresponding .keep file
376 * later on. If we don't get that then tough luck with it.
378 for (len = 0;
379 len < 46 && (s = xread(fd[0], packname+len, 46-len)) > 0;
380 len += s);
381 close(fd[0]);
382 if (len == 46 && packname[45] == '\n' &&
383 memcmp(packname, "keep\t", 5) == 0) {
384 char path[PATH_MAX];
385 packname[45] = 0;
386 snprintf(path, sizeof(path), "%s/pack/pack-%s.keep",
387 get_object_directory(), packname + 5);
388 pack_lockfile = xstrdup(path);
391 /* Then wrap our index-pack process. */
392 while (waitpid(pid, &status, 0) < 0)
393 if (errno != EINTR)
394 return "waitpid failed";
395 if (WIFEXITED(status)) {
396 int code = WEXITSTATUS(status);
397 if (code)
398 return "index-pack exited with error code";
399 reprepare_packed_git();
400 return NULL;
402 return "index-pack abnormal exit";
406 static void report(const char *unpack_status)
408 struct command *cmd;
409 packet_write(1, "unpack %s\n",
410 unpack_status ? unpack_status : "ok");
411 for (cmd = commands; cmd; cmd = cmd->next) {
412 if (!cmd->error_string)
413 packet_write(1, "ok %s\n",
414 cmd->ref_name);
415 else
416 packet_write(1, "ng %s %s\n",
417 cmd->ref_name, cmd->error_string);
419 packet_flush(1);
422 static int delete_only(struct command *cmd)
424 while (cmd) {
425 if (!is_null_sha1(cmd->new_sha1))
426 return 0;
427 cmd = cmd->next;
429 return 1;
432 int main(int argc, char **argv)
434 int i;
435 char *dir = NULL;
437 argv++;
438 for (i = 1; i < argc; i++) {
439 char *arg = *argv++;
441 if (*arg == '-') {
442 /* Do flag handling here */
443 usage(receive_pack_usage);
445 if (dir)
446 usage(receive_pack_usage);
447 dir = arg;
449 if (!dir)
450 usage(receive_pack_usage);
452 if (!enter_repo(dir, 0))
453 die("'%s': unable to chdir or not a git archive", dir);
455 if (is_repository_shallow())
456 die("attempt to push into a shallow repository");
458 git_config(receive_pack_config);
460 if (0 <= transfer_unpack_limit)
461 unpack_limit = transfer_unpack_limit;
462 else if (0 <= receive_unpack_limit)
463 unpack_limit = receive_unpack_limit;
465 write_head_info();
467 /* EOF */
468 packet_flush(1);
470 read_head_info();
471 if (commands) {
472 const char *unpack_status = NULL;
474 if (!delete_only(commands))
475 unpack_status = unpack();
476 if (!unpack_status)
477 execute_commands();
478 if (pack_lockfile)
479 unlink(pack_lockfile);
480 if (report_status)
481 report(unpack_status);
482 run_update_post_hook(commands);
484 return 0;