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