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