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