b9de2e8ff6292e1d38f6b145c087c75a9e2b8907
1#include "builtin.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 "connect.h"
12#include "transport.h"
13#include "string-list.h"
14#include "sha1-array.h"
15#include "connected.h"
16#include "argv-array.h"
17#include "version.h"
18
19static const char receive_pack_usage[] = "git receive-pack <git-dir>";
20
21enum deny_action {
22 DENY_UNCONFIGURED,
23 DENY_IGNORE,
24 DENY_WARN,
25 DENY_REFUSE
26};
27
28static int deny_deletes;
29static int deny_non_fast_forwards;
30static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
31static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
32static int receive_fsck_objects = -1;
33static int transfer_fsck_objects = -1;
34static int receive_unpack_limit = -1;
35static int transfer_unpack_limit = -1;
36static int unpack_limit = 100;
37static int report_status;
38static int use_sideband;
39static int quiet;
40static int prefer_ofs_delta = 1;
41static int auto_update_server_info;
42static int auto_gc = 1;
43static int fix_thin = 1;
44static const char *head_name;
45static void *head_name_to_free;
46static int sent_capabilities;
47static const char *alt_shallow_file;
48
49static enum deny_action parse_deny_action(const char *var, const char *value)
50{
51 if (value) {
52 if (!strcasecmp(value, "ignore"))
53 return DENY_IGNORE;
54 if (!strcasecmp(value, "warn"))
55 return DENY_WARN;
56 if (!strcasecmp(value, "refuse"))
57 return DENY_REFUSE;
58 }
59 if (git_config_bool(var, value))
60 return DENY_REFUSE;
61 return DENY_IGNORE;
62}
63
64static int receive_pack_config(const char *var, const char *value, void *cb)
65{
66 int status = parse_hide_refs_config(var, value, "receive");
67
68 if (status)
69 return status;
70
71 if (strcmp(var, "receive.denydeletes") == 0) {
72 deny_deletes = git_config_bool(var, value);
73 return 0;
74 }
75
76 if (strcmp(var, "receive.denynonfastforwards") == 0) {
77 deny_non_fast_forwards = git_config_bool(var, value);
78 return 0;
79 }
80
81 if (strcmp(var, "receive.unpacklimit") == 0) {
82 receive_unpack_limit = git_config_int(var, value);
83 return 0;
84 }
85
86 if (strcmp(var, "transfer.unpacklimit") == 0) {
87 transfer_unpack_limit = git_config_int(var, value);
88 return 0;
89 }
90
91 if (strcmp(var, "receive.fsckobjects") == 0) {
92 receive_fsck_objects = git_config_bool(var, value);
93 return 0;
94 }
95
96 if (strcmp(var, "transfer.fsckobjects") == 0) {
97 transfer_fsck_objects = git_config_bool(var, value);
98 return 0;
99 }
100
101 if (!strcmp(var, "receive.denycurrentbranch")) {
102 deny_current_branch = parse_deny_action(var, value);
103 return 0;
104 }
105
106 if (strcmp(var, "receive.denydeletecurrent") == 0) {
107 deny_delete_current = parse_deny_action(var, value);
108 return 0;
109 }
110
111 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
112 prefer_ofs_delta = git_config_bool(var, value);
113 return 0;
114 }
115
116 if (strcmp(var, "receive.updateserverinfo") == 0) {
117 auto_update_server_info = git_config_bool(var, value);
118 return 0;
119 }
120
121 if (strcmp(var, "receive.autogc") == 0) {
122 auto_gc = git_config_bool(var, value);
123 return 0;
124 }
125
126 return git_default_config(var, value, cb);
127}
128
129static void show_ref(const char *path, const unsigned char *sha1)
130{
131 if (ref_is_hidden(path))
132 return;
133
134 if (sent_capabilities)
135 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
136 else
137 packet_write(1, "%s %s%c%s%s agent=%s\n",
138 sha1_to_hex(sha1), path, 0,
139 " report-status delete-refs side-band-64k quiet",
140 prefer_ofs_delta ? " ofs-delta" : "",
141 git_user_agent_sanitized());
142 sent_capabilities = 1;
143}
144
145static int show_ref_cb(const char *path, const unsigned char *sha1, int flag, void *unused)
146{
147 path = strip_namespace(path);
148 /*
149 * Advertise refs outside our current namespace as ".have"
150 * refs, so that the client can use them to minimize data
151 * transfer but will otherwise ignore them. This happens to
152 * cover ".have" that are thrown in by add_one_alternate_ref()
153 * to mark histories that are complete in our alternates as
154 * well.
155 */
156 if (!path)
157 path = ".have";
158 show_ref(path, sha1);
159 return 0;
160}
161
162static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
163{
164 show_ref(".have", sha1);
165}
166
167static void collect_one_alternate_ref(const struct ref *ref, void *data)
168{
169 struct sha1_array *sa = data;
170 sha1_array_append(sa, ref->old_sha1);
171}
172
173static void write_head_info(void)
174{
175 struct sha1_array sa = SHA1_ARRAY_INIT;
176 for_each_alternate_ref(collect_one_alternate_ref, &sa);
177 sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL);
178 sha1_array_clear(&sa);
179 for_each_ref(show_ref_cb, NULL);
180 if (!sent_capabilities)
181 show_ref("capabilities^{}", null_sha1);
182
183 advertise_shallow_grafts(1);
184
185 /* EOF */
186 packet_flush(1);
187}
188
189struct command {
190 struct command *next;
191 const char *error_string;
192 unsigned int skip_update:1,
193 did_not_exist:1;
194 int index;
195 unsigned char old_sha1[20];
196 unsigned char new_sha1[20];
197 char ref_name[FLEX_ARRAY]; /* more */
198};
199
200static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
201static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
202
203static void report_message(const char *prefix, const char *err, va_list params)
204{
205 int sz = strlen(prefix);
206 char msg[4096];
207
208 strncpy(msg, prefix, sz);
209 sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
210 if (sz > (sizeof(msg) - 1))
211 sz = sizeof(msg) - 1;
212 msg[sz++] = '\n';
213
214 if (use_sideband)
215 send_sideband(1, 2, msg, sz, use_sideband);
216 else
217 xwrite(2, msg, sz);
218}
219
220static void rp_warning(const char *err, ...)
221{
222 va_list params;
223 va_start(params, err);
224 report_message("warning: ", err, params);
225 va_end(params);
226}
227
228static void rp_error(const char *err, ...)
229{
230 va_list params;
231 va_start(params, err);
232 report_message("error: ", err, params);
233 va_end(params);
234}
235
236static int copy_to_sideband(int in, int out, void *arg)
237{
238 char data[128];
239 while (1) {
240 ssize_t sz = xread(in, data, sizeof(data));
241 if (sz <= 0)
242 break;
243 send_sideband(1, 2, data, sz, use_sideband);
244 }
245 close(in);
246 return 0;
247}
248
249typedef int (*feed_fn)(void *, const char **, size_t *);
250static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
251{
252 struct child_process proc;
253 struct async muxer;
254 const char *argv[2];
255 int code;
256
257 argv[0] = find_hook(hook_name);
258 if (!argv[0])
259 return 0;
260
261 argv[1] = NULL;
262
263 memset(&proc, 0, sizeof(proc));
264 proc.argv = argv;
265 proc.in = -1;
266 proc.stdout_to_stderr = 1;
267
268 if (use_sideband) {
269 memset(&muxer, 0, sizeof(muxer));
270 muxer.proc = copy_to_sideband;
271 muxer.in = -1;
272 code = start_async(&muxer);
273 if (code)
274 return code;
275 proc.err = muxer.in;
276 }
277
278 code = start_command(&proc);
279 if (code) {
280 if (use_sideband)
281 finish_async(&muxer);
282 return code;
283 }
284
285 while (1) {
286 const char *buf;
287 size_t n;
288 if (feed(feed_state, &buf, &n))
289 break;
290 if (write_in_full(proc.in, buf, n) != n)
291 break;
292 }
293 close(proc.in);
294 if (use_sideband)
295 finish_async(&muxer);
296 return finish_command(&proc);
297}
298
299struct receive_hook_feed_state {
300 struct command *cmd;
301 int skip_broken;
302 struct strbuf buf;
303};
304
305static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
306{
307 struct receive_hook_feed_state *state = state_;
308 struct command *cmd = state->cmd;
309
310 while (cmd &&
311 state->skip_broken && (cmd->error_string || cmd->did_not_exist))
312 cmd = cmd->next;
313 if (!cmd)
314 return -1; /* EOF */
315 strbuf_reset(&state->buf);
316 strbuf_addf(&state->buf, "%s %s %s\n",
317 sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1),
318 cmd->ref_name);
319 state->cmd = cmd->next;
320 if (bufp) {
321 *bufp = state->buf.buf;
322 *sizep = state->buf.len;
323 }
324 return 0;
325}
326
327static int run_receive_hook(struct command *commands, const char *hook_name,
328 int skip_broken)
329{
330 struct receive_hook_feed_state state;
331 int status;
332
333 strbuf_init(&state.buf, 0);
334 state.cmd = commands;
335 state.skip_broken = skip_broken;
336 if (feed_receive_hook(&state, NULL, NULL))
337 return 0;
338 state.cmd = commands;
339 status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
340 strbuf_release(&state.buf);
341 return status;
342}
343
344static int run_update_hook(struct command *cmd)
345{
346 const char *argv[5];
347 struct child_process proc;
348 int code;
349
350 argv[0] = find_hook("update");
351 if (!argv[0])
352 return 0;
353
354 argv[1] = cmd->ref_name;
355 argv[2] = sha1_to_hex(cmd->old_sha1);
356 argv[3] = sha1_to_hex(cmd->new_sha1);
357 argv[4] = NULL;
358
359 memset(&proc, 0, sizeof(proc));
360 proc.no_stdin = 1;
361 proc.stdout_to_stderr = 1;
362 proc.err = use_sideband ? -1 : 0;
363 proc.argv = argv;
364
365 code = start_command(&proc);
366 if (code)
367 return code;
368 if (use_sideband)
369 copy_to_sideband(proc.err, -1, NULL);
370 return finish_command(&proc);
371}
372
373static int is_ref_checked_out(const char *ref)
374{
375 if (is_bare_repository())
376 return 0;
377
378 if (!head_name)
379 return 0;
380 return !strcmp(head_name, ref);
381}
382
383static char *refuse_unconfigured_deny_msg[] = {
384 "By default, updating the current branch in a non-bare repository",
385 "is denied, because it will make the index and work tree inconsistent",
386 "with what you pushed, and will require 'git reset --hard' to match",
387 "the work tree to HEAD.",
388 "",
389 "You can set 'receive.denyCurrentBranch' configuration variable to",
390 "'ignore' or 'warn' in the remote repository to allow pushing into",
391 "its current branch; however, this is not recommended unless you",
392 "arranged to update its work tree to match what you pushed in some",
393 "other way.",
394 "",
395 "To squelch this message and still keep the default behaviour, set",
396 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
397};
398
399static void refuse_unconfigured_deny(void)
400{
401 int i;
402 for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
403 rp_error("%s", refuse_unconfigured_deny_msg[i]);
404}
405
406static char *refuse_unconfigured_deny_delete_current_msg[] = {
407 "By default, deleting the current branch is denied, because the next",
408 "'git clone' won't result in any file checked out, causing confusion.",
409 "",
410 "You can set 'receive.denyDeleteCurrent' configuration variable to",
411 "'warn' or 'ignore' in the remote repository to allow deleting the",
412 "current branch, with or without a warning message.",
413 "",
414 "To squelch this message, you can set it to 'refuse'."
415};
416
417static void refuse_unconfigured_deny_delete_current(void)
418{
419 int i;
420 for (i = 0;
421 i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
422 i++)
423 rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
424}
425
426static const char *update(struct command *cmd)
427{
428 const char *name = cmd->ref_name;
429 struct strbuf namespaced_name_buf = STRBUF_INIT;
430 const char *namespaced_name;
431 unsigned char *old_sha1 = cmd->old_sha1;
432 unsigned char *new_sha1 = cmd->new_sha1;
433 struct ref_lock *lock;
434
435 /* only refs/... are allowed */
436 if (prefixcmp(name, "refs/") || check_refname_format(name + 5, 0)) {
437 rp_error("refusing to create funny ref '%s' remotely", name);
438 return "funny refname";
439 }
440
441 strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
442 namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
443
444 if (is_ref_checked_out(namespaced_name)) {
445 switch (deny_current_branch) {
446 case DENY_IGNORE:
447 break;
448 case DENY_WARN:
449 rp_warning("updating the current branch");
450 break;
451 case DENY_REFUSE:
452 case DENY_UNCONFIGURED:
453 rp_error("refusing to update checked out branch: %s", name);
454 if (deny_current_branch == DENY_UNCONFIGURED)
455 refuse_unconfigured_deny();
456 return "branch is currently checked out";
457 }
458 }
459
460 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
461 error("unpack should have generated %s, "
462 "but I can't find it!", sha1_to_hex(new_sha1));
463 return "bad pack";
464 }
465
466 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
467 if (deny_deletes && !prefixcmp(name, "refs/heads/")) {
468 rp_error("denying ref deletion for %s", name);
469 return "deletion prohibited";
470 }
471
472 if (!strcmp(namespaced_name, head_name)) {
473 switch (deny_delete_current) {
474 case DENY_IGNORE:
475 break;
476 case DENY_WARN:
477 rp_warning("deleting the current branch");
478 break;
479 case DENY_REFUSE:
480 case DENY_UNCONFIGURED:
481 if (deny_delete_current == DENY_UNCONFIGURED)
482 refuse_unconfigured_deny_delete_current();
483 rp_error("refusing to delete the current branch: %s", name);
484 return "deletion of the current branch prohibited";
485 }
486 }
487 }
488
489 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
490 !is_null_sha1(old_sha1) &&
491 !prefixcmp(name, "refs/heads/")) {
492 struct object *old_object, *new_object;
493 struct commit *old_commit, *new_commit;
494
495 old_object = parse_object(old_sha1);
496 new_object = parse_object(new_sha1);
497
498 if (!old_object || !new_object ||
499 old_object->type != OBJ_COMMIT ||
500 new_object->type != OBJ_COMMIT) {
501 error("bad sha1 objects for %s", name);
502 return "bad ref";
503 }
504 old_commit = (struct commit *)old_object;
505 new_commit = (struct commit *)new_object;
506 if (!in_merge_bases(old_commit, new_commit)) {
507 rp_error("denying non-fast-forward %s"
508 " (you should pull first)", name);
509 return "non-fast-forward";
510 }
511 }
512 if (run_update_hook(cmd)) {
513 rp_error("hook declined to update %s", name);
514 return "hook declined";
515 }
516
517 if (is_null_sha1(new_sha1)) {
518 if (!parse_object(old_sha1)) {
519 old_sha1 = NULL;
520 if (ref_exists(name)) {
521 rp_warning("Allowing deletion of corrupt ref.");
522 } else {
523 rp_warning("Deleting a non-existent ref.");
524 cmd->did_not_exist = 1;
525 }
526 }
527 if (delete_ref(namespaced_name, old_sha1, 0)) {
528 rp_error("failed to delete %s", name);
529 return "failed to delete";
530 }
531 return NULL; /* good */
532 }
533 else {
534 lock = lock_any_ref_for_update(namespaced_name, old_sha1,
535 0, NULL);
536 if (!lock) {
537 rp_error("failed to lock %s", name);
538 return "failed to lock";
539 }
540 if (write_ref_sha1(lock, new_sha1, "push")) {
541 return "failed to write"; /* error() already called */
542 }
543 return NULL; /* good */
544 }
545}
546
547static void run_update_post_hook(struct command *commands)
548{
549 struct command *cmd;
550 int argc;
551 const char **argv;
552 struct child_process proc;
553 char *hook;
554
555 hook = find_hook("post-update");
556 for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
557 if (cmd->error_string || cmd->did_not_exist)
558 continue;
559 argc++;
560 }
561 if (!argc || !hook)
562 return;
563
564 argv = xmalloc(sizeof(*argv) * (2 + argc));
565 argv[0] = hook;
566
567 for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
568 char *p;
569 if (cmd->error_string || cmd->did_not_exist)
570 continue;
571 p = xmalloc(strlen(cmd->ref_name) + 1);
572 strcpy(p, cmd->ref_name);
573 argv[argc] = p;
574 argc++;
575 }
576 argv[argc] = NULL;
577
578 memset(&proc, 0, sizeof(proc));
579 proc.no_stdin = 1;
580 proc.stdout_to_stderr = 1;
581 proc.err = use_sideband ? -1 : 0;
582 proc.argv = argv;
583
584 if (!start_command(&proc)) {
585 if (use_sideband)
586 copy_to_sideband(proc.err, -1, NULL);
587 finish_command(&proc);
588 }
589}
590
591static void check_aliased_update(struct command *cmd, struct string_list *list)
592{
593 struct strbuf buf = STRBUF_INIT;
594 const char *dst_name;
595 struct string_list_item *item;
596 struct command *dst_cmd;
597 unsigned char sha1[20];
598 char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
599 int flag;
600
601 strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
602 dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag);
603 strbuf_release(&buf);
604
605 if (!(flag & REF_ISSYMREF))
606 return;
607
608 dst_name = strip_namespace(dst_name);
609 if (!dst_name) {
610 rp_error("refusing update to broken symref '%s'", cmd->ref_name);
611 cmd->skip_update = 1;
612 cmd->error_string = "broken symref";
613 return;
614 }
615
616 if ((item = string_list_lookup(list, dst_name)) == NULL)
617 return;
618
619 cmd->skip_update = 1;
620
621 dst_cmd = (struct command *) item->util;
622
623 if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
624 !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
625 return;
626
627 dst_cmd->skip_update = 1;
628
629 strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
630 strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
631 strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
632 strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
633 rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
634 " its target '%s' (%s..%s)",
635 cmd->ref_name, cmd_oldh, cmd_newh,
636 dst_cmd->ref_name, dst_oldh, dst_newh);
637
638 cmd->error_string = dst_cmd->error_string =
639 "inconsistent aliased update";
640}
641
642static void check_aliased_updates(struct command *commands)
643{
644 struct command *cmd;
645 struct string_list ref_list = STRING_LIST_INIT_NODUP;
646
647 for (cmd = commands; cmd; cmd = cmd->next) {
648 struct string_list_item *item =
649 string_list_append(&ref_list, cmd->ref_name);
650 item->util = (void *)cmd;
651 }
652 sort_string_list(&ref_list);
653
654 for (cmd = commands; cmd; cmd = cmd->next) {
655 if (!cmd->error_string)
656 check_aliased_update(cmd, &ref_list);
657 }
658
659 string_list_clear(&ref_list, 0);
660}
661
662static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
663{
664 struct command **cmd_list = cb_data;
665 struct command *cmd = *cmd_list;
666
667 if (!cmd || is_null_sha1(cmd->new_sha1))
668 return -1; /* end of list */
669 *cmd_list = NULL; /* this returns only one */
670 hashcpy(sha1, cmd->new_sha1);
671 return 0;
672}
673
674static void set_connectivity_errors(struct command *commands)
675{
676 struct command *cmd;
677
678 for (cmd = commands; cmd; cmd = cmd->next) {
679 struct command *singleton = cmd;
680 if (!check_everything_connected(command_singleton_iterator,
681 0, &singleton))
682 continue;
683 cmd->error_string = "missing necessary objects";
684 }
685}
686
687static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
688{
689 struct command **cmd_list = cb_data;
690 struct command *cmd = *cmd_list;
691
692 while (cmd) {
693 if (!is_null_sha1(cmd->new_sha1) && !cmd->skip_update) {
694 hashcpy(sha1, cmd->new_sha1);
695 *cmd_list = cmd->next;
696 return 0;
697 }
698 cmd = cmd->next;
699 }
700 *cmd_list = NULL;
701 return -1; /* end of list */
702}
703
704static void reject_updates_to_hidden(struct command *commands)
705{
706 struct command *cmd;
707
708 for (cmd = commands; cmd; cmd = cmd->next) {
709 if (cmd->error_string || !ref_is_hidden(cmd->ref_name))
710 continue;
711 if (is_null_sha1(cmd->new_sha1))
712 cmd->error_string = "deny deleting a hidden ref";
713 else
714 cmd->error_string = "deny updating a hidden ref";
715 }
716}
717
718static void execute_commands(struct command *commands, const char *unpacker_error)
719{
720 struct command *cmd;
721 unsigned char sha1[20];
722
723 if (unpacker_error) {
724 for (cmd = commands; cmd; cmd = cmd->next)
725 cmd->error_string = "unpacker error";
726 return;
727 }
728
729 cmd = commands;
730 if (check_everything_connected(iterate_receive_command_list,
731 0, &cmd))
732 set_connectivity_errors(commands);
733
734 reject_updates_to_hidden(commands);
735
736 if (run_receive_hook(commands, "pre-receive", 0)) {
737 for (cmd = commands; cmd; cmd = cmd->next) {
738 if (!cmd->error_string)
739 cmd->error_string = "pre-receive hook declined";
740 }
741 return;
742 }
743
744 check_aliased_updates(commands);
745
746 free(head_name_to_free);
747 head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL);
748
749 for (cmd = commands; cmd; cmd = cmd->next) {
750 if (cmd->error_string)
751 continue;
752
753 if (cmd->skip_update)
754 continue;
755
756 cmd->error_string = update(cmd);
757 }
758}
759
760static struct command *read_head_info(struct sha1_array *shallow)
761{
762 struct command *commands = NULL;
763 struct command **p = &commands;
764 for (;;) {
765 char *line;
766 unsigned char old_sha1[20], new_sha1[20];
767 struct command *cmd;
768 char *refname;
769 int len, reflen;
770
771 line = packet_read_line(0, &len);
772 if (!line)
773 break;
774
775 if (len == 48 && !prefixcmp(line, "shallow ")) {
776 if (get_sha1_hex(line + 8, old_sha1))
777 die("protocol error: expected shallow sha, got '%s'", line + 8);
778 sha1_array_append(shallow, old_sha1);
779 continue;
780 }
781
782 if (len < 83 ||
783 line[40] != ' ' ||
784 line[81] != ' ' ||
785 get_sha1_hex(line, old_sha1) ||
786 get_sha1_hex(line + 41, new_sha1))
787 die("protocol error: expected old/new/ref, got '%s'",
788 line);
789
790 refname = line + 82;
791 reflen = strlen(refname);
792 if (reflen + 82 < len) {
793 const char *feature_list = refname + reflen + 1;
794 if (parse_feature_request(feature_list, "report-status"))
795 report_status = 1;
796 if (parse_feature_request(feature_list, "side-band-64k"))
797 use_sideband = LARGE_PACKET_MAX;
798 if (parse_feature_request(feature_list, "quiet"))
799 quiet = 1;
800 }
801 cmd = xcalloc(1, sizeof(struct command) + len - 80);
802 hashcpy(cmd->old_sha1, old_sha1);
803 hashcpy(cmd->new_sha1, new_sha1);
804 memcpy(cmd->ref_name, line + 82, len - 81);
805 *p = cmd;
806 p = &cmd->next;
807 }
808 return commands;
809}
810
811static const char *parse_pack_header(struct pack_header *hdr)
812{
813 switch (read_pack_header(0, hdr)) {
814 case PH_ERROR_EOF:
815 return "eof before pack header was fully read";
816
817 case PH_ERROR_PACK_SIGNATURE:
818 return "protocol error (pack signature mismatch detected)";
819
820 case PH_ERROR_PROTOCOL:
821 return "protocol error (pack version unsupported)";
822
823 default:
824 return "unknown error in parse_pack_header";
825
826 case 0:
827 return NULL;
828 }
829}
830
831static const char *pack_lockfile;
832
833static const char *unpack(int err_fd, struct shallow_info *si)
834{
835 struct pack_header hdr;
836 struct argv_array av = ARGV_ARRAY_INIT;
837 const char *hdr_err;
838 int status;
839 char hdr_arg[38];
840 struct child_process child;
841 int fsck_objects = (receive_fsck_objects >= 0
842 ? receive_fsck_objects
843 : transfer_fsck_objects >= 0
844 ? transfer_fsck_objects
845 : 0);
846
847 hdr_err = parse_pack_header(&hdr);
848 if (hdr_err) {
849 if (err_fd > 0)
850 close(err_fd);
851 return hdr_err;
852 }
853 snprintf(hdr_arg, sizeof(hdr_arg),
854 "--pack_header=%"PRIu32",%"PRIu32,
855 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
856
857 if (si->nr_ours || si->nr_theirs) {
858 alt_shallow_file = setup_temporary_shallow(si->shallow);
859 argv_array_pushl(&av, "--shallow-file", alt_shallow_file, NULL);
860 }
861
862 memset(&child, 0, sizeof(child));
863 if (ntohl(hdr.hdr_entries) < unpack_limit) {
864 argv_array_pushl(&av, "unpack-objects", hdr_arg, NULL);
865 if (quiet)
866 argv_array_push(&av, "-q");
867 if (fsck_objects)
868 argv_array_push(&av, "--strict");
869 child.argv = av.argv;
870 child.no_stdout = 1;
871 child.err = err_fd;
872 child.git_cmd = 1;
873 status = run_command(&child);
874 if (status)
875 return "unpack-objects abnormal exit";
876 } else {
877 int s;
878 char keep_arg[256];
879
880 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
881 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
882 strcpy(keep_arg + s, "localhost");
883
884 argv_array_pushl(&av, "index-pack",
885 "--stdin", hdr_arg, keep_arg, NULL);
886 if (fsck_objects)
887 argv_array_push(&av, "--strict");
888 if (fix_thin)
889 argv_array_push(&av, "--fix-thin");
890 child.argv = av.argv;
891 child.out = -1;
892 child.err = err_fd;
893 child.git_cmd = 1;
894 status = start_command(&child);
895 if (status)
896 return "index-pack fork failed";
897 pack_lockfile = index_pack_lockfile(child.out);
898 close(child.out);
899 status = finish_command(&child);
900 if (status)
901 return "index-pack abnormal exit";
902 reprepare_packed_git();
903 }
904 return NULL;
905}
906
907static const char *unpack_with_sideband(struct shallow_info *si)
908{
909 struct async muxer;
910 const char *ret;
911
912 if (!use_sideband)
913 return unpack(0, si);
914
915 memset(&muxer, 0, sizeof(muxer));
916 muxer.proc = copy_to_sideband;
917 muxer.in = -1;
918 if (start_async(&muxer))
919 return NULL;
920
921 ret = unpack(muxer.in, si);
922
923 finish_async(&muxer);
924 return ret;
925}
926
927static void update_shallow_info(struct command *commands,
928 struct shallow_info *si,
929 struct sha1_array *ref)
930{
931 struct command *cmd;
932 int *ref_status;
933 remove_nonexistent_theirs_shallow(si);
934 /* XXX remove_nonexistent_ours_in_pack() */
935 if (!si->nr_ours && !si->nr_theirs)
936 return;
937
938 for (cmd = commands; cmd; cmd = cmd->next) {
939 if (is_null_sha1(cmd->new_sha1))
940 continue;
941 sha1_array_append(ref, cmd->new_sha1);
942 cmd->index = ref->nr - 1;
943 }
944 si->ref = ref;
945
946 ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
947 assign_shallow_commits_to_refs(si, NULL, ref_status);
948 for (cmd = commands; cmd; cmd = cmd->next) {
949 if (is_null_sha1(cmd->new_sha1))
950 continue;
951 if (ref_status[cmd->index]) {
952 cmd->error_string = "shallow update not allowed";
953 cmd->skip_update = 1;
954 }
955 }
956 if (alt_shallow_file && *alt_shallow_file) {
957 unlink(alt_shallow_file);
958 alt_shallow_file = NULL;
959 }
960 free(ref_status);
961}
962
963static void report(struct command *commands, const char *unpack_status)
964{
965 struct command *cmd;
966 struct strbuf buf = STRBUF_INIT;
967
968 packet_buf_write(&buf, "unpack %s\n",
969 unpack_status ? unpack_status : "ok");
970 for (cmd = commands; cmd; cmd = cmd->next) {
971 if (!cmd->error_string)
972 packet_buf_write(&buf, "ok %s\n",
973 cmd->ref_name);
974 else
975 packet_buf_write(&buf, "ng %s %s\n",
976 cmd->ref_name, cmd->error_string);
977 }
978 packet_buf_flush(&buf);
979
980 if (use_sideband)
981 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
982 else
983 write_or_die(1, buf.buf, buf.len);
984 strbuf_release(&buf);
985}
986
987static int delete_only(struct command *commands)
988{
989 struct command *cmd;
990 for (cmd = commands; cmd; cmd = cmd->next) {
991 if (!is_null_sha1(cmd->new_sha1))
992 return 0;
993 }
994 return 1;
995}
996
997int cmd_receive_pack(int argc, const char **argv, const char *prefix)
998{
999 int advertise_refs = 0;
1000 int stateless_rpc = 0;
1001 int i;
1002 char *dir = NULL;
1003 struct command *commands;
1004 struct sha1_array shallow = SHA1_ARRAY_INIT;
1005 struct sha1_array ref = SHA1_ARRAY_INIT;
1006 struct shallow_info si;
1007
1008 packet_trace_identity("receive-pack");
1009
1010 argv++;
1011 for (i = 1; i < argc; i++) {
1012 const char *arg = *argv++;
1013
1014 if (*arg == '-') {
1015 if (!strcmp(arg, "--quiet")) {
1016 quiet = 1;
1017 continue;
1018 }
1019
1020 if (!strcmp(arg, "--advertise-refs")) {
1021 advertise_refs = 1;
1022 continue;
1023 }
1024 if (!strcmp(arg, "--stateless-rpc")) {
1025 stateless_rpc = 1;
1026 continue;
1027 }
1028 if (!strcmp(arg, "--reject-thin-pack-for-testing")) {
1029 fix_thin = 0;
1030 continue;
1031 }
1032
1033 usage(receive_pack_usage);
1034 }
1035 if (dir)
1036 usage(receive_pack_usage);
1037 dir = xstrdup(arg);
1038 }
1039 if (!dir)
1040 usage(receive_pack_usage);
1041
1042 setup_path();
1043
1044 if (!enter_repo(dir, 0))
1045 die("'%s' does not appear to be a git repository", dir);
1046
1047 if (is_repository_shallow() && stateless_rpc)
1048 die("attempt to push into a shallow repository");
1049
1050 git_config(receive_pack_config, NULL);
1051
1052 if (0 <= transfer_unpack_limit)
1053 unpack_limit = transfer_unpack_limit;
1054 else if (0 <= receive_unpack_limit)
1055 unpack_limit = receive_unpack_limit;
1056
1057 if (advertise_refs || !stateless_rpc) {
1058 write_head_info();
1059 }
1060 if (advertise_refs)
1061 return 0;
1062
1063 if ((commands = read_head_info(&shallow)) != NULL) {
1064 const char *unpack_status = NULL;
1065
1066 prepare_shallow_info(&si, &shallow);
1067 if (!delete_only(commands)) {
1068 unpack_status = unpack_with_sideband(&si);
1069 update_shallow_info(commands, &si, &ref);
1070 }
1071 execute_commands(commands, unpack_status);
1072 if (pack_lockfile)
1073 unlink_or_warn(pack_lockfile);
1074 if (report_status)
1075 report(commands, unpack_status);
1076 run_receive_hook(commands, "post-receive", 1);
1077 run_update_post_hook(commands);
1078 if (auto_gc) {
1079 const char *argv_gc_auto[] = {
1080 "gc", "--auto", "--quiet", NULL,
1081 };
1082 int opt = RUN_GIT_CMD | RUN_COMMAND_STDOUT_TO_STDERR;
1083 run_command_v_opt(argv_gc_auto, opt);
1084 }
1085 if (auto_update_server_info)
1086 update_server_info(0);
1087 clear_shallow_info(&si);
1088 }
1089 if (use_sideband)
1090 packet_flush(1);
1091 sha1_array_clear(&shallow);
1092 sha1_array_clear(&ref);
1093 return 0;
1094}