bisect: fix missing "exit"
[git.git] / builtin-receive-pack.c
blob7f9f134806766244bf1eb3de2de3a823a62b180a
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"
9 #include "remote.h"
10 #include "transport.h"
12 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
14 static int deny_deletes = 0;
15 static int deny_non_fast_forwards = 0;
16 static int receive_fsck_objects;
17 static int receive_unpack_limit = -1;
18 static int transfer_unpack_limit = -1;
19 static int unpack_limit = 100;
20 static int report_status;
22 static char capabilities[] = " report-status delete-refs ";
23 static int capabilities_sent;
25 static int receive_pack_config(const char *var, const char *value, void *cb)
27 if (strcmp(var, "receive.denydeletes") == 0) {
28 deny_deletes = git_config_bool(var, value);
29 return 0;
32 if (strcmp(var, "receive.denynonfastforwards") == 0) {
33 deny_non_fast_forwards = git_config_bool(var, value);
34 return 0;
37 if (strcmp(var, "receive.unpacklimit") == 0) {
38 receive_unpack_limit = git_config_int(var, value);
39 return 0;
42 if (strcmp(var, "transfer.unpacklimit") == 0) {
43 transfer_unpack_limit = git_config_int(var, value);
44 return 0;
47 if (strcmp(var, "receive.fsckobjects") == 0) {
48 receive_fsck_objects = git_config_bool(var, value);
49 return 0;
52 return git_default_config(var, value, cb);
55 static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
57 if (capabilities_sent)
58 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
59 else
60 packet_write(1, "%s %s%c%s\n",
61 sha1_to_hex(sha1), path, 0, capabilities);
62 capabilities_sent = 1;
63 return 0;
66 static void write_head_info(void)
68 for_each_ref(show_ref, NULL);
69 if (!capabilities_sent)
70 show_ref("capabilities^{}", null_sha1, 0, NULL);
74 struct command {
75 struct command *next;
76 const char *error_string;
77 unsigned char old_sha1[20];
78 unsigned char new_sha1[20];
79 char ref_name[FLEX_ARRAY]; /* more */
82 static struct command *commands;
84 static const char pre_receive_hook[] = "hooks/pre-receive";
85 static const char post_receive_hook[] = "hooks/post-receive";
87 static int hook_status(int code, const char *hook_name)
89 switch (code) {
90 case 0:
91 return 0;
92 case -ERR_RUN_COMMAND_FORK:
93 return error("hook fork failed");
94 case -ERR_RUN_COMMAND_EXEC:
95 return error("hook execute failed");
96 case -ERR_RUN_COMMAND_PIPE:
97 return error("hook pipe failed");
98 case -ERR_RUN_COMMAND_WAITPID:
99 return error("waitpid failed");
100 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
101 return error("waitpid is confused");
102 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
103 return error("%s died of signal", hook_name);
104 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
105 return error("%s died strangely", hook_name);
106 default:
107 error("%s exited with error code %d", hook_name, -code);
108 return -code;
112 static int run_hook(const char *hook_name)
114 static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
115 struct command *cmd;
116 struct child_process proc;
117 const char *argv[2];
118 int have_input = 0, code;
120 for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
121 if (!cmd->error_string)
122 have_input = 1;
125 if (!have_input || access(hook_name, X_OK) < 0)
126 return 0;
128 argv[0] = hook_name;
129 argv[1] = NULL;
131 memset(&proc, 0, sizeof(proc));
132 proc.argv = argv;
133 proc.in = -1;
134 proc.stdout_to_stderr = 1;
136 code = start_command(&proc);
137 if (code)
138 return hook_status(code, hook_name);
139 for (cmd = commands; cmd; cmd = cmd->next) {
140 if (!cmd->error_string) {
141 size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n",
142 sha1_to_hex(cmd->old_sha1),
143 sha1_to_hex(cmd->new_sha1),
144 cmd->ref_name);
145 if (write_in_full(proc.in, buf, n) != n)
146 break;
149 close(proc.in);
150 return hook_status(finish_command(&proc), hook_name);
153 static int run_update_hook(struct command *cmd)
155 static const char update_hook[] = "hooks/update";
156 struct child_process proc;
157 const char *argv[5];
159 if (access(update_hook, X_OK) < 0)
160 return 0;
162 argv[0] = update_hook;
163 argv[1] = cmd->ref_name;
164 argv[2] = sha1_to_hex(cmd->old_sha1);
165 argv[3] = sha1_to_hex(cmd->new_sha1);
166 argv[4] = NULL;
168 memset(&proc, 0, sizeof(proc));
169 proc.argv = argv;
170 proc.no_stdin = 1;
171 proc.stdout_to_stderr = 1;
173 return hook_status(run_command(&proc), update_hook);
176 static const char *update(struct command *cmd)
178 const char *name = cmd->ref_name;
179 unsigned char *old_sha1 = cmd->old_sha1;
180 unsigned char *new_sha1 = cmd->new_sha1;
181 struct ref_lock *lock;
183 /* only refs/... are allowed */
184 if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) {
185 error("refusing to create funny ref '%s' remotely", name);
186 return "funny refname";
189 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
190 error("unpack should have generated %s, "
191 "but I can't find it!", sha1_to_hex(new_sha1));
192 return "bad pack";
194 if (deny_deletes && is_null_sha1(new_sha1) &&
195 !is_null_sha1(old_sha1) &&
196 !prefixcmp(name, "refs/heads/")) {
197 error("denying ref deletion for %s", name);
198 return "deletion prohibited";
200 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
201 !is_null_sha1(old_sha1) &&
202 !prefixcmp(name, "refs/heads/")) {
203 struct object *old_object, *new_object;
204 struct commit *old_commit, *new_commit;
205 struct commit_list *bases, *ent;
207 old_object = parse_object(old_sha1);
208 new_object = parse_object(new_sha1);
210 if (!old_object || !new_object ||
211 old_object->type != OBJ_COMMIT ||
212 new_object->type != OBJ_COMMIT) {
213 error("bad sha1 objects for %s", name);
214 return "bad ref";
216 old_commit = (struct commit *)old_object;
217 new_commit = (struct commit *)new_object;
218 bases = get_merge_bases(old_commit, new_commit, 1);
219 for (ent = bases; ent; ent = ent->next)
220 if (!hashcmp(old_sha1, ent->item->object.sha1))
221 break;
222 free_commit_list(bases);
223 if (!ent) {
224 error("denying non-fast forward %s"
225 " (you should pull first)", name);
226 return "non-fast forward";
229 if (run_update_hook(cmd)) {
230 error("hook declined to update %s", name);
231 return "hook declined";
234 if (is_null_sha1(new_sha1)) {
235 if (!parse_object(old_sha1)) {
236 warning ("Allowing deletion of corrupt ref.");
237 old_sha1 = NULL;
239 if (delete_ref(name, old_sha1, 0)) {
240 error("failed to delete %s", name);
241 return "failed to delete";
243 return NULL; /* good */
245 else {
246 lock = lock_any_ref_for_update(name, old_sha1, 0);
247 if (!lock) {
248 error("failed to lock %s", name);
249 return "failed to lock";
251 if (write_ref_sha1(lock, new_sha1, "push")) {
252 return "failed to write"; /* error() already called */
254 return NULL; /* good */
258 static char update_post_hook[] = "hooks/post-update";
260 static void run_update_post_hook(struct command *cmd)
262 struct command *cmd_p;
263 int argc;
264 const char **argv;
266 for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
267 if (cmd_p->error_string)
268 continue;
269 argc++;
271 if (!argc || access(update_post_hook, X_OK) < 0)
272 return;
273 argv = xmalloc(sizeof(*argv) * (2 + argc));
274 argv[0] = update_post_hook;
276 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
277 char *p;
278 if (cmd_p->error_string)
279 continue;
280 p = xmalloc(strlen(cmd_p->ref_name) + 1);
281 strcpy(p, cmd_p->ref_name);
282 argv[argc] = p;
283 argc++;
285 argv[argc] = NULL;
286 run_command_v_opt(argv, RUN_COMMAND_NO_STDIN
287 | RUN_COMMAND_STDOUT_TO_STDERR);
290 static void execute_commands(const char *unpacker_error)
292 struct command *cmd = commands;
294 if (unpacker_error) {
295 while (cmd) {
296 cmd->error_string = "n/a (unpacker error)";
297 cmd = cmd->next;
299 return;
302 if (run_hook(pre_receive_hook)) {
303 while (cmd) {
304 cmd->error_string = "pre-receive hook declined";
305 cmd = cmd->next;
307 return;
310 while (cmd) {
311 cmd->error_string = update(cmd);
312 cmd = cmd->next;
316 static void read_head_info(void)
318 struct command **p = &commands;
319 for (;;) {
320 static char line[1000];
321 unsigned char old_sha1[20], new_sha1[20];
322 struct command *cmd;
323 char *refname;
324 int len, reflen;
326 len = packet_read_line(0, line, sizeof(line));
327 if (!len)
328 break;
329 if (line[len-1] == '\n')
330 line[--len] = 0;
331 if (len < 83 ||
332 line[40] != ' ' ||
333 line[81] != ' ' ||
334 get_sha1_hex(line, old_sha1) ||
335 get_sha1_hex(line + 41, new_sha1))
336 die("protocol error: expected old/new/ref, got '%s'",
337 line);
339 refname = line + 82;
340 reflen = strlen(refname);
341 if (reflen + 82 < len) {
342 if (strstr(refname + reflen + 1, "report-status"))
343 report_status = 1;
345 cmd = xmalloc(sizeof(struct command) + len - 80);
346 hashcpy(cmd->old_sha1, old_sha1);
347 hashcpy(cmd->new_sha1, new_sha1);
348 memcpy(cmd->ref_name, line + 82, len - 81);
349 cmd->error_string = NULL;
350 cmd->next = NULL;
351 *p = cmd;
352 p = &cmd->next;
356 static const char *parse_pack_header(struct pack_header *hdr)
358 switch (read_pack_header(0, hdr)) {
359 case PH_ERROR_EOF:
360 return "eof before pack header was fully read";
362 case PH_ERROR_PACK_SIGNATURE:
363 return "protocol error (pack signature mismatch detected)";
365 case PH_ERROR_PROTOCOL:
366 return "protocol error (pack version unsupported)";
368 default:
369 return "unknown error in parse_pack_header";
371 case 0:
372 return NULL;
376 static const char *pack_lockfile;
378 static const char *unpack(void)
380 struct pack_header hdr;
381 const char *hdr_err;
382 char hdr_arg[38];
384 hdr_err = parse_pack_header(&hdr);
385 if (hdr_err)
386 return hdr_err;
387 snprintf(hdr_arg, sizeof(hdr_arg),
388 "--pack_header=%"PRIu32",%"PRIu32,
389 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
391 if (ntohl(hdr.hdr_entries) < unpack_limit) {
392 int code, i = 0;
393 const char *unpacker[4];
394 unpacker[i++] = "unpack-objects";
395 if (receive_fsck_objects)
396 unpacker[i++] = "--strict";
397 unpacker[i++] = hdr_arg;
398 unpacker[i++] = NULL;
399 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
400 switch (code) {
401 case 0:
402 return NULL;
403 case -ERR_RUN_COMMAND_FORK:
404 return "unpack fork failed";
405 case -ERR_RUN_COMMAND_EXEC:
406 return "unpack execute failed";
407 case -ERR_RUN_COMMAND_WAITPID:
408 return "waitpid failed";
409 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
410 return "waitpid is confused";
411 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
412 return "unpacker died of signal";
413 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
414 return "unpacker died strangely";
415 default:
416 return "unpacker exited with error code";
418 } else {
419 const char *keeper[7];
420 int s, status, i = 0;
421 char keep_arg[256];
422 struct child_process ip;
424 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
425 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
426 strcpy(keep_arg + s, "localhost");
428 keeper[i++] = "index-pack";
429 keeper[i++] = "--stdin";
430 if (receive_fsck_objects)
431 keeper[i++] = "--strict";
432 keeper[i++] = "--fix-thin";
433 keeper[i++] = hdr_arg;
434 keeper[i++] = keep_arg;
435 keeper[i++] = NULL;
436 memset(&ip, 0, sizeof(ip));
437 ip.argv = keeper;
438 ip.out = -1;
439 ip.git_cmd = 1;
440 if (start_command(&ip))
441 return "index-pack fork failed";
442 pack_lockfile = index_pack_lockfile(ip.out);
443 close(ip.out);
444 status = finish_command(&ip);
445 if (!status) {
446 reprepare_packed_git();
447 return NULL;
449 return "index-pack abnormal exit";
453 static void report(const char *unpack_status)
455 struct command *cmd;
456 packet_write(1, "unpack %s\n",
457 unpack_status ? unpack_status : "ok");
458 for (cmd = commands; cmd; cmd = cmd->next) {
459 if (!cmd->error_string)
460 packet_write(1, "ok %s\n",
461 cmd->ref_name);
462 else
463 packet_write(1, "ng %s %s\n",
464 cmd->ref_name, cmd->error_string);
466 packet_flush(1);
469 static int delete_only(struct command *cmd)
471 while (cmd) {
472 if (!is_null_sha1(cmd->new_sha1))
473 return 0;
474 cmd = cmd->next;
476 return 1;
479 static int add_refs_from_alternate(struct alternate_object_database *e, void *unused)
481 char *other;
482 size_t len;
483 struct remote *remote;
484 struct transport *transport;
485 const struct ref *extra;
487 e->name[-1] = '\0';
488 other = xstrdup(make_absolute_path(e->base));
489 e->name[-1] = '/';
490 len = strlen(other);
492 while (other[len-1] == '/')
493 other[--len] = '\0';
494 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
495 return 0;
496 /* Is this a git repository with refs? */
497 memcpy(other + len - 8, "/refs", 6);
498 if (!is_directory(other))
499 return 0;
500 other[len - 8] = '\0';
501 remote = remote_get(other);
502 transport = transport_get(remote, other);
503 for (extra = transport_get_remote_refs(transport);
504 extra;
505 extra = extra->next) {
506 add_extra_ref(".have", extra->old_sha1, 0);
508 transport_disconnect(transport);
509 free(other);
510 return 0;
513 static void add_alternate_refs(void)
515 foreach_alt_odb(add_refs_from_alternate, NULL);
518 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
520 int i;
521 char *dir = NULL;
523 argv++;
524 for (i = 1; i < argc; i++) {
525 const char *arg = *argv++;
527 if (*arg == '-') {
528 /* Do flag handling here */
529 usage(receive_pack_usage);
531 if (dir)
532 usage(receive_pack_usage);
533 dir = xstrdup(arg);
535 if (!dir)
536 usage(receive_pack_usage);
538 setup_path();
540 if (!enter_repo(dir, 0))
541 die("'%s': unable to chdir or not a git archive", dir);
543 if (is_repository_shallow())
544 die("attempt to push into a shallow repository");
546 git_config(receive_pack_config, NULL);
548 if (0 <= transfer_unpack_limit)
549 unpack_limit = transfer_unpack_limit;
550 else if (0 <= receive_unpack_limit)
551 unpack_limit = receive_unpack_limit;
553 add_alternate_refs();
554 write_head_info();
555 clear_extra_refs();
557 /* EOF */
558 packet_flush(1);
560 read_head_info();
561 if (commands) {
562 const char *unpack_status = NULL;
564 if (!delete_only(commands))
565 unpack_status = unpack();
566 execute_commands(unpack_status);
567 if (pack_lockfile)
568 unlink(pack_lockfile);
569 if (report_status)
570 report(unpack_status);
571 run_hook(post_receive_hook);
572 run_update_post_hook(commands);
574 return 0;