1 #include "cache.h"
2 #include "pack.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "sideband.h"
6 #include "run-command.h"
7 #include "exec_cmd.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "remote.h"
11 #include "transport.h"
13 static const char receive_pack_usage[] = "git receive-pack <git-dir>";
15 enum deny_action {
16 DENY_UNCONFIGURED,
17 DENY_IGNORE,
18 DENY_WARN,
19 DENY_REFUSE,
20 };
22 static int deny_deletes;
23 static int deny_non_fast_forwards;
24 static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
25 static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
26 static int receive_fsck_objects;
27 static int receive_unpack_limit = -1;
28 static int transfer_unpack_limit = -1;
29 static int unpack_limit = 100;
30 static int report_status;
31 static int use_sideband;
32 static int prefer_ofs_delta = 1;
33 static int auto_update_server_info;
34 static int auto_gc = 1;
35 static const char *head_name;
36 static int sent_capabilities;
38 static enum deny_action parse_deny_action(const char *var, const char *value)
39 {
40 if (value) {
41 if (!strcasecmp(value, "ignore"))
42 return DENY_IGNORE;
43 if (!strcasecmp(value, "warn"))
44 return DENY_WARN;
45 if (!strcasecmp(value, "refuse"))
46 return DENY_REFUSE;
47 }
48 if (git_config_bool(var, value))
49 return DENY_REFUSE;
50 return DENY_IGNORE;
51 }
53 static int receive_pack_config(const char *var, const char *value, void *cb)
54 {
55 if (strcmp(var, "receive.denydeletes") == 0) {
56 deny_deletes = git_config_bool(var, value);
57 return 0;
58 }
60 if (strcmp(var, "receive.denynonfastforwards") == 0) {
61 deny_non_fast_forwards = git_config_bool(var, value);
62 return 0;
63 }
65 if (strcmp(var, "receive.unpacklimit") == 0) {
66 receive_unpack_limit = git_config_int(var, value);
67 return 0;
68 }
70 if (strcmp(var, "transfer.unpacklimit") == 0) {
71 transfer_unpack_limit = git_config_int(var, value);
72 return 0;
73 }
75 if (strcmp(var, "receive.fsckobjects") == 0) {
76 receive_fsck_objects = git_config_bool(var, value);
77 return 0;
78 }
80 if (!strcmp(var, "receive.denycurrentbranch")) {
81 deny_current_branch = parse_deny_action(var, value);
82 return 0;
83 }
85 if (strcmp(var, "receive.denydeletecurrent") == 0) {
86 deny_delete_current = parse_deny_action(var, value);
87 return 0;
88 }
90 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
91 prefer_ofs_delta = git_config_bool(var, value);
92 return 0;
93 }
95 if (strcmp(var, "receive.updateserverinfo") == 0) {
96 auto_update_server_info = git_config_bool(var, value);
97 return 0;
98 }
100 if (strcmp(var, "receive.autogc") == 0) {
101 auto_gc = git_config_bool(var, value);
102 return 0;
103 }
105 return git_default_config(var, value, cb);
106 }
108 static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
109 {
110 if (sent_capabilities)
111 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
112 else
113 packet_write(1, "%s %s%c%s%s\n",
114 sha1_to_hex(sha1), path, 0,
115 " report-status delete-refs side-band-64k",
116 prefer_ofs_delta ? " ofs-delta" : "");
117 sent_capabilities = 1;
118 return 0;
119 }
121 static void write_head_info(void)
122 {
123 for_each_ref(show_ref, NULL);
124 if (!sent_capabilities)
125 show_ref("capabilities^{}", null_sha1, 0, NULL);
127 }
129 struct command {
130 struct command *next;
131 const char *error_string;
132 unsigned char old_sha1[20];
133 unsigned char new_sha1[20];
134 char ref_name[FLEX_ARRAY]; /* more */
135 };
137 static struct command *commands;
139 static const char pre_receive_hook[] = "hooks/pre-receive";
140 static const char post_receive_hook[] = "hooks/post-receive";
142 static int copy_to_sideband(int in, int out, void *arg)
143 {
144 char data[128];
145 while (1) {
146 ssize_t sz = xread(in, data, sizeof(data));
147 if (sz <= 0)
148 break;
149 send_sideband(1, 2, data, sz, use_sideband);
150 }
151 close(in);
152 return 0;
153 }
155 static int run_receive_hook(const char *hook_name)
156 {
157 static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
158 struct command *cmd;
159 struct child_process proc;
160 struct async muxer;
161 const char *argv[2];
162 int have_input = 0, code;
164 for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
165 if (!cmd->error_string)
166 have_input = 1;
167 }
169 if (!have_input || access(hook_name, X_OK) < 0)
170 return 0;
172 argv[0] = hook_name;
173 argv[1] = NULL;
175 memset(&proc, 0, sizeof(proc));
176 proc.argv = argv;
177 proc.in = -1;
178 proc.stdout_to_stderr = 1;
180 if (use_sideband) {
181 memset(&muxer, 0, sizeof(muxer));
182 muxer.proc = copy_to_sideband;
183 muxer.in = -1;
184 code = start_async(&muxer);
185 if (code)
186 return code;
187 proc.err = muxer.in;
188 }
190 code = start_command(&proc);
191 if (code) {
192 if (use_sideband)
193 finish_async(&muxer);
194 return code;
195 }
197 for (cmd = commands; cmd; cmd = cmd->next) {
198 if (!cmd->error_string) {
199 size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n",
200 sha1_to_hex(cmd->old_sha1),
201 sha1_to_hex(cmd->new_sha1),
202 cmd->ref_name);
203 if (write_in_full(proc.in, buf, n) != n)
204 break;
205 }
206 }
207 close(proc.in);
208 if (use_sideband)
209 finish_async(&muxer);
210 return finish_command(&proc);
211 }
213 static int run_update_hook(struct command *cmd)
214 {
215 static const char update_hook[] = "hooks/update";
216 const char *argv[5];
217 struct child_process proc;
218 int code;
220 if (access(update_hook, X_OK) < 0)
221 return 0;
223 argv[0] = update_hook;
224 argv[1] = cmd->ref_name;
225 argv[2] = sha1_to_hex(cmd->old_sha1);
226 argv[3] = sha1_to_hex(cmd->new_sha1);
227 argv[4] = NULL;
229 memset(&proc, 0, sizeof(proc));
230 proc.no_stdin = 1;
231 proc.stdout_to_stderr = 1;
232 proc.err = use_sideband ? -1 : 0;
233 proc.argv = argv;
235 code = start_command(&proc);
236 if (code)
237 return code;
238 if (use_sideband)
239 copy_to_sideband(proc.err, -1, NULL);
240 return finish_command(&proc);
241 }
243 static int is_ref_checked_out(const char *ref)
244 {
245 if (is_bare_repository())
246 return 0;
248 if (!head_name)
249 return 0;
250 return !strcmp(head_name, ref);
251 }
253 static char *refuse_unconfigured_deny_msg[] = {
254 "By default, updating the current branch in a non-bare repository",
255 "is denied, because it will make the index and work tree inconsistent",
256 "with what you pushed, and will require 'git reset --hard' to match",
257 "the work tree to HEAD.",
258 "",
259 "You can set 'receive.denyCurrentBranch' configuration variable to",
260 "'ignore' or 'warn' in the remote repository to allow pushing into",
261 "its current branch; however, this is not recommended unless you",
262 "arranged to update its work tree to match what you pushed in some",
263 "other way.",
264 "",
265 "To squelch this message and still keep the default behaviour, set",
266 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
267 };
269 static void refuse_unconfigured_deny(void)
270 {
271 int i;
272 for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
273 error("%s", refuse_unconfigured_deny_msg[i]);
274 }
276 static char *refuse_unconfigured_deny_delete_current_msg[] = {
277 "By default, deleting the current branch is denied, because the next",
278 "'git clone' won't result in any file checked out, causing confusion.",
279 "",
280 "You can set 'receive.denyDeleteCurrent' configuration variable to",
281 "'warn' or 'ignore' in the remote repository to allow deleting the",
282 "current branch, with or without a warning message.",
283 "",
284 "To squelch this message, you can set it to 'refuse'."
285 };
287 static void refuse_unconfigured_deny_delete_current(void)
288 {
289 int i;
290 for (i = 0;
291 i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
292 i++)
293 error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
294 }
296 static const char *update(struct command *cmd)
297 {
298 const char *name = cmd->ref_name;
299 unsigned char *old_sha1 = cmd->old_sha1;
300 unsigned char *new_sha1 = cmd->new_sha1;
301 struct ref_lock *lock;
303 /* only refs/... are allowed */
304 if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) {
305 error("refusing to create funny ref '%s' remotely", name);
306 return "funny refname";
307 }
309 if (is_ref_checked_out(name)) {
310 switch (deny_current_branch) {
311 case DENY_IGNORE:
312 break;
313 case DENY_WARN:
314 warning("updating the current branch");
315 break;
316 case DENY_REFUSE:
317 case DENY_UNCONFIGURED:
318 error("refusing to update checked out branch: %s", name);
319 if (deny_current_branch == DENY_UNCONFIGURED)
320 refuse_unconfigured_deny();
321 return "branch is currently checked out";
322 }
323 }
325 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
326 error("unpack should have generated %s, "
327 "but I can't find it!", sha1_to_hex(new_sha1));
328 return "bad pack";
329 }
331 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
332 if (deny_deletes && !prefixcmp(name, "refs/heads/")) {
333 error("denying ref deletion for %s", name);
334 return "deletion prohibited";
335 }
337 if (!strcmp(name, head_name)) {
338 switch (deny_delete_current) {
339 case DENY_IGNORE:
340 break;
341 case DENY_WARN:
342 warning("deleting the current branch");
343 break;
344 case DENY_REFUSE:
345 case DENY_UNCONFIGURED:
346 if (deny_delete_current == DENY_UNCONFIGURED)
347 refuse_unconfigured_deny_delete_current();
348 error("refusing to delete the current branch: %s", name);
349 return "deletion of the current branch prohibited";
350 }
351 }
352 }
354 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
355 !is_null_sha1(old_sha1) &&
356 !prefixcmp(name, "refs/heads/")) {
357 struct object *old_object, *new_object;
358 struct commit *old_commit, *new_commit;
359 struct commit_list *bases, *ent;
361 old_object = parse_object(old_sha1);
362 new_object = parse_object(new_sha1);
364 if (!old_object || !new_object ||
365 old_object->type != OBJ_COMMIT ||
366 new_object->type != OBJ_COMMIT) {
367 error("bad sha1 objects for %s", name);
368 return "bad ref";
369 }
370 old_commit = (struct commit *)old_object;
371 new_commit = (struct commit *)new_object;
372 bases = get_merge_bases(old_commit, new_commit, 1);
373 for (ent = bases; ent; ent = ent->next)
374 if (!hashcmp(old_sha1, ent->item->object.sha1))
375 break;
376 free_commit_list(bases);
377 if (!ent) {
378 error("denying non-fast-forward %s"
379 " (you should pull first)", name);
380 return "non-fast-forward";
381 }
382 }
383 if (run_update_hook(cmd)) {
384 error("hook declined to update %s", name);
385 return "hook declined";
386 }
388 if (is_null_sha1(new_sha1)) {
389 if (!parse_object(old_sha1)) {
390 warning ("Allowing deletion of corrupt ref.");
391 old_sha1 = NULL;
392 }
393 if (delete_ref(name, old_sha1, 0)) {
394 error("failed to delete %s", name);
395 return "failed to delete";
396 }
397 return NULL; /* good */
398 }
399 else {
400 lock = lock_any_ref_for_update(name, old_sha1, 0);
401 if (!lock) {
402 error("failed to lock %s", name);
403 return "failed to lock";
404 }
405 if (write_ref_sha1(lock, new_sha1, "push")) {
406 return "failed to write"; /* error() already called */
407 }
408 return NULL; /* good */
409 }
410 }
412 static char update_post_hook[] = "hooks/post-update";
414 static void run_update_post_hook(struct command *cmd)
415 {
416 struct command *cmd_p;
417 int argc;
418 const char **argv;
419 struct child_process proc;
421 for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
422 if (cmd_p->error_string)
423 continue;
424 argc++;
425 }
426 if (!argc || access(update_post_hook, X_OK) < 0)
427 return;
428 argv = xmalloc(sizeof(*argv) * (2 + argc));
429 argv[0] = update_post_hook;
431 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
432 char *p;
433 if (cmd_p->error_string)
434 continue;
435 p = xmalloc(strlen(cmd_p->ref_name) + 1);
436 strcpy(p, cmd_p->ref_name);
437 argv[argc] = p;
438 argc++;
439 }
440 argv[argc] = NULL;
442 memset(&proc, 0, sizeof(proc));
443 proc.no_stdin = 1;
444 proc.stdout_to_stderr = 1;
445 proc.err = use_sideband ? -1 : 0;
446 proc.argv = argv;
448 if (!start_command(&proc)) {
449 if (use_sideband)
450 copy_to_sideband(proc.err, -1, NULL);
451 finish_command(&proc);
452 }
453 }
455 static void execute_commands(const char *unpacker_error)
456 {
457 struct command *cmd = commands;
458 unsigned char sha1[20];
460 if (unpacker_error) {
461 while (cmd) {
462 cmd->error_string = "n/a (unpacker error)";
463 cmd = cmd->next;
464 }
465 return;
466 }
468 if (run_receive_hook(pre_receive_hook)) {
469 while (cmd) {
470 cmd->error_string = "pre-receive hook declined";
471 cmd = cmd->next;
472 }
473 return;
474 }
476 head_name = resolve_ref("HEAD", sha1, 0, NULL);
478 while (cmd) {
479 cmd->error_string = update(cmd);
480 cmd = cmd->next;
481 }
482 }
484 static void read_head_info(void)
485 {
486 struct command **p = &commands;
487 for (;;) {
488 static char line[1000];
489 unsigned char old_sha1[20], new_sha1[20];
490 struct command *cmd;
491 char *refname;
492 int len, reflen;
494 len = packet_read_line(0, line, sizeof(line));
495 if (!len)
496 break;
497 if (line[len-1] == '\n')
498 line[--len] = 0;
499 if (len < 83 ||
500 line[40] != ' ' ||
501 line[81] != ' ' ||
502 get_sha1_hex(line, old_sha1) ||
503 get_sha1_hex(line + 41, new_sha1))
504 die("protocol error: expected old/new/ref, got '%s'",
505 line);
507 refname = line + 82;
508 reflen = strlen(refname);
509 if (reflen + 82 < len) {
510 if (strstr(refname + reflen + 1, "report-status"))
511 report_status = 1;
512 if (strstr(refname + reflen + 1, "side-band-64k"))
513 use_sideband = LARGE_PACKET_MAX;
514 }
515 cmd = xmalloc(sizeof(struct command) + len - 80);
516 hashcpy(cmd->old_sha1, old_sha1);
517 hashcpy(cmd->new_sha1, new_sha1);
518 memcpy(cmd->ref_name, line + 82, len - 81);
519 cmd->error_string = NULL;
520 cmd->next = NULL;
521 *p = cmd;
522 p = &cmd->next;
523 }
524 }
526 static const char *parse_pack_header(struct pack_header *hdr)
527 {
528 switch (read_pack_header(0, hdr)) {
529 case PH_ERROR_EOF:
530 return "eof before pack header was fully read";
532 case PH_ERROR_PACK_SIGNATURE:
533 return "protocol error (pack signature mismatch detected)";
535 case PH_ERROR_PROTOCOL:
536 return "protocol error (pack version unsupported)";
538 default:
539 return "unknown error in parse_pack_header";
541 case 0:
542 return NULL;
543 }
544 }
546 static const char *pack_lockfile;
548 static const char *unpack(void)
549 {
550 struct pack_header hdr;
551 const char *hdr_err;
552 char hdr_arg[38];
554 hdr_err = parse_pack_header(&hdr);
555 if (hdr_err)
556 return hdr_err;
557 snprintf(hdr_arg, sizeof(hdr_arg),
558 "--pack_header=%"PRIu32",%"PRIu32,
559 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
561 if (ntohl(hdr.hdr_entries) < unpack_limit) {
562 int code, i = 0;
563 const char *unpacker[4];
564 unpacker[i++] = "unpack-objects";
565 if (receive_fsck_objects)
566 unpacker[i++] = "--strict";
567 unpacker[i++] = hdr_arg;
568 unpacker[i++] = NULL;
569 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
570 if (!code)
571 return NULL;
572 return "unpack-objects abnormal exit";
573 } else {
574 const char *keeper[7];
575 int s, status, i = 0;
576 char keep_arg[256];
577 struct child_process ip;
579 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
580 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
581 strcpy(keep_arg + s, "localhost");
583 keeper[i++] = "index-pack";
584 keeper[i++] = "--stdin";
585 if (receive_fsck_objects)
586 keeper[i++] = "--strict";
587 keeper[i++] = "--fix-thin";
588 keeper[i++] = hdr_arg;
589 keeper[i++] = keep_arg;
590 keeper[i++] = NULL;
591 memset(&ip, 0, sizeof(ip));
592 ip.argv = keeper;
593 ip.out = -1;
594 ip.git_cmd = 1;
595 status = start_command(&ip);
596 if (status) {
597 return "index-pack fork failed";
598 }
599 pack_lockfile = index_pack_lockfile(ip.out);
600 close(ip.out);
601 status = finish_command(&ip);
602 if (!status) {
603 reprepare_packed_git();
604 return NULL;
605 }
606 return "index-pack abnormal exit";
607 }
608 }
610 static void report(const char *unpack_status)
611 {
612 struct command *cmd;
613 struct strbuf buf = STRBUF_INIT;
615 packet_buf_write(&buf, "unpack %s\n",
616 unpack_status ? unpack_status : "ok");
617 for (cmd = commands; cmd; cmd = cmd->next) {
618 if (!cmd->error_string)
619 packet_buf_write(&buf, "ok %s\n",
620 cmd->ref_name);
621 else
622 packet_buf_write(&buf, "ng %s %s\n",
623 cmd->ref_name, cmd->error_string);
624 }
625 packet_buf_flush(&buf);
627 if (use_sideband)
628 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
629 else
630 safe_write(1, buf.buf, buf.len);
631 strbuf_release(&buf);
632 }
634 static int delete_only(struct command *cmd)
635 {
636 while (cmd) {
637 if (!is_null_sha1(cmd->new_sha1))
638 return 0;
639 cmd = cmd->next;
640 }
641 return 1;
642 }
644 static int add_refs_from_alternate(struct alternate_object_database *e, void *unused)
645 {
646 char *other;
647 size_t len;
648 struct remote *remote;
649 struct transport *transport;
650 const struct ref *extra;
652 e->name[-1] = '\0';
653 other = xstrdup(make_absolute_path(e->base));
654 e->name[-1] = '/';
655 len = strlen(other);
657 while (other[len-1] == '/')
658 other[--len] = '\0';
659 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
660 return 0;
661 /* Is this a git repository with refs? */
662 memcpy(other + len - 8, "/refs", 6);
663 if (!is_directory(other))
664 return 0;
665 other[len - 8] = '\0';
666 remote = remote_get(other);
667 transport = transport_get(remote, other);
668 for (extra = transport_get_remote_refs(transport);
669 extra;
670 extra = extra->next) {
671 add_extra_ref(".have", extra->old_sha1, 0);
672 }
673 transport_disconnect(transport);
674 free(other);
675 return 0;
676 }
678 static void add_alternate_refs(void)
679 {
680 foreach_alt_odb(add_refs_from_alternate, NULL);
681 }
683 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
684 {
685 int advertise_refs = 0;
686 int stateless_rpc = 0;
687 int i;
688 char *dir = NULL;
690 argv++;
691 for (i = 1; i < argc; i++) {
692 const char *arg = *argv++;
694 if (*arg == '-') {
695 if (!strcmp(arg, "--advertise-refs")) {
696 advertise_refs = 1;
697 continue;
698 }
699 if (!strcmp(arg, "--stateless-rpc")) {
700 stateless_rpc = 1;
701 continue;
702 }
704 usage(receive_pack_usage);
705 }
706 if (dir)
707 usage(receive_pack_usage);
708 dir = xstrdup(arg);
709 }
710 if (!dir)
711 usage(receive_pack_usage);
713 setup_path();
715 if (!enter_repo(dir, 0))
716 die("'%s' does not appear to be a git repository", dir);
718 if (is_repository_shallow())
719 die("attempt to push into a shallow repository");
721 git_config(receive_pack_config, NULL);
723 if (0 <= transfer_unpack_limit)
724 unpack_limit = transfer_unpack_limit;
725 else if (0 <= receive_unpack_limit)
726 unpack_limit = receive_unpack_limit;
728 if (advertise_refs || !stateless_rpc) {
729 add_alternate_refs();
730 write_head_info();
731 clear_extra_refs();
733 /* EOF */
734 packet_flush(1);
735 }
736 if (advertise_refs)
737 return 0;
739 read_head_info();
740 if (commands) {
741 const char *unpack_status = NULL;
743 if (!delete_only(commands))
744 unpack_status = unpack();
745 execute_commands(unpack_status);
746 if (pack_lockfile)
747 unlink_or_warn(pack_lockfile);
748 if (report_status)
749 report(unpack_status);
750 run_receive_hook(post_receive_hook);
751 run_update_post_hook(commands);
752 if (auto_gc) {
753 const char *argv_gc_auto[] = {
754 "gc", "--auto", "--quiet", NULL,
755 };
756 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
757 }
758 if (auto_update_server_info)
759 update_server_info(0);
760 }
761 if (use_sideband)
762 packet_flush(1);
763 return 0;
764 }