]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/receive-pack.c
signed push: add "pushee" header to push certificate
[thirdparty/git.git] / builtin / receive-pack.c
CommitLineData
1e4cd68c 1#include "builtin.h"
fc04c412 2#include "pack.h"
8a65ff76 3#include "refs.h"
f3a3214e 4#include "pkt-line.h"
38a81b4e 5#include "sideband.h"
b1bf95bb 6#include "run-command.h"
576162a4 7#include "exec_cmd.h"
11031d7e
JS
8#include "commit.h"
9#include "object.h"
d79796bc 10#include "remote.h"
47a59185 11#include "connect.h"
d79796bc 12#include "transport.h"
da3efdb1 13#include "string-list.h"
cff38a5e 14#include "sha1-array.h"
52fed6e1 15#include "connected.h"
31c42bff 16#include "argv-array.h"
ff5effdf 17#include "version.h"
d05b9618
JH
18#include "tag.h"
19#include "gpg-interface.h"
575f4974 20
34263de0 21static const char receive_pack_usage[] = "git receive-pack <git-dir>";
575f4974 22
986e8239 23enum deny_action {
3d95d92b 24 DENY_UNCONFIGURED,
986e8239
JK
25 DENY_IGNORE,
26 DENY_WARN,
4b05548f 27 DENY_REFUSE
986e8239
JK
28};
29
1b53a076
JH
30static int deny_deletes;
31static int deny_non_fast_forwards;
3d95d92b 32static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
747ca245 33static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
dab76d3a
JH
34static int receive_fsck_objects = -1;
35static int transfer_fsck_objects = -1;
e28714c5
JH
36static int receive_unpack_limit = -1;
37static int transfer_unpack_limit = -1;
46732fae 38static int unpack_limit = 100;
96f1e58f 39static int report_status;
38a81b4e 40static int use_sideband;
c207e34f 41static int quiet;
b74fce16 42static int prefer_ofs_delta = 1;
77e3efbf
JH
43static int auto_update_server_info;
44static int auto_gc = 1;
f7c815c3 45static int fix_thin = 1;
747ca245 46static const char *head_name;
96ec7b1e 47static void *head_name_to_free;
185c04e0 48static int sent_capabilities;
0a1bc12b 49static int shallow_update;
5dbd7676 50static const char *alt_shallow_file;
a85b377d
JH
51static int accept_push_cert = 1;
52static struct strbuf push_cert = STRBUF_INIT;
53static unsigned char push_cert_sha1[20];
d05b9618 54static struct signature_check sigcheck;
cfee10a7 55
986e8239
JK
56static 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
ef90d6d4 71static int receive_pack_config(const char *var, const char *value, void *cb)
6fb75bed 72{
daebaa78
JH
73 int status = parse_hide_refs_config(var, value, "receive");
74
75 if (status)
76 return status;
77
a240de11
JK
78 if (strcmp(var, "receive.denydeletes") == 0) {
79 deny_deletes = git_config_bool(var, value);
80 return 0;
81 }
82
e28714c5 83 if (strcmp(var, "receive.denynonfastforwards") == 0) {
6fb75bed
SP
84 deny_non_fast_forwards = git_config_bool(var, value);
85 return 0;
86 }
87
e28714c5
JH
88 if (strcmp(var, "receive.unpacklimit") == 0) {
89 receive_unpack_limit = git_config_int(var, value);
fc04c412
SP
90 return 0;
91 }
92
e28714c5
JH
93 if (strcmp(var, "transfer.unpacklimit") == 0) {
94 transfer_unpack_limit = git_config_int(var, value);
95 return 0;
96 }
97
20dc0016
MK
98 if (strcmp(var, "receive.fsckobjects") == 0) {
99 receive_fsck_objects = git_config_bool(var, value);
100 return 0;
101 }
102
dab76d3a
JH
103 if (strcmp(var, "transfer.fsckobjects") == 0) {
104 transfer_fsck_objects = git_config_bool(var, value);
105 return 0;
106 }
107
986e8239
JK
108 if (!strcmp(var, "receive.denycurrentbranch")) {
109 deny_current_branch = parse_deny_action(var, value);
110 return 0;
111 }
112
747ca245
JH
113 if (strcmp(var, "receive.denydeletecurrent") == 0) {
114 deny_delete_current = parse_deny_action(var, value);
115 return 0;
116 }
117
b74fce16
NP
118 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
119 prefer_ofs_delta = git_config_bool(var, value);
120 return 0;
121 }
122
77e3efbf
JH
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
0a1bc12b
NTND
133 if (strcmp(var, "receive.shallowupdate") == 0) {
134 shallow_update = git_config_bool(var, value);
135 return 0;
136 }
137
a85b377d
JH
138 if (strcmp(var, "receive.acceptpushcert") == 0) {
139 accept_push_cert = git_config_bool(var, value);
140 return 0;
141 }
142
ef90d6d4 143 return git_default_config(var, value, cb);
6fb75bed
SP
144}
145
bc98201d 146static void show_ref(const char *path, const unsigned char *sha1)
575f4974 147{
daebaa78
JH
148 if (ref_is_hidden(path))
149 return;
150
52d2ae58 151 if (sent_capabilities) {
cfee10a7 152 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
52d2ae58
JH
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");
a85b377d
JH
160 if (accept_push_cert)
161 strbuf_addstr(&cap, " push-cert");
52d2ae58
JH
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 }
575f4974
LT
168}
169
bc98201d 170static int show_ref_cb(const char *path, const unsigned char *sha1, int flag, void *unused)
6b01ecfe
JT
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";
bc98201d
MH
183 show_ref(path, sha1);
184 return 0;
6b01ecfe
JT
185}
186
85f25104 187static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
b7a025d9 188{
85f25104 189 show_ref(".have", sha1);
b7a025d9
MH
190}
191
192static 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);
6b01ecfe
JT
196}
197
8a65ff76 198static void write_head_info(void)
575f4974 199{
b7a025d9
MH
200 struct sha1_array sa = SHA1_ARRAY_INIT;
201 for_each_alternate_ref(collect_one_alternate_ref, &sa);
85f25104 202 sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL);
b7a025d9 203 sha1_array_clear(&sa);
6b01ecfe 204 for_each_ref(show_ref_cb, NULL);
185c04e0 205 if (!sent_capabilities)
bc98201d 206 show_ref("capabilities^{}", null_sha1);
cfee10a7 207
ad491366
NTND
208 advertise_shallow_grafts(1);
209
b7a025d9
MH
210 /* EOF */
211 packet_flush(1);
575f4974
LT
212}
213
eb1af2df
LT
214struct command {
215 struct command *next;
cfee10a7 216 const char *error_string;
160b81ed
PYH
217 unsigned int skip_update:1,
218 did_not_exist:1;
5dbd7676 219 int index;
eb1af2df
LT
220 unsigned char old_sha1[20];
221 unsigned char new_sha1[20];
8f1d2e6f 222 char ref_name[FLEX_ARRAY]; /* more */
575f4974
LT
223};
224
466dbc42
SP
225static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
226static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
227
228static 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
245static 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
253static 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
6d525d38
SP
261static 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
a85b377d
JH
274static 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) {
d05b9618
JH
283 struct strbuf gpg_output = STRBUF_INIT;
284 struct strbuf gpg_status = STRBUF_INIT;
285 int bogs /* beginning_of_gpg_sig */;
286
a85b377d
JH
287 already_done = 1;
288 if (write_sha1_file(push_cert.buf, push_cert.len, "blob", push_cert_sha1))
289 hashclr(push_cert_sha1);
d05b9618
JH
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);
a85b377d
JH
308 }
309 if (!is_null_sha1(push_cert_sha1)) {
310 argv_array_pushf(&env, "GIT_PUSH_CERT=%s", sha1_to_hex(push_cert_sha1));
d05b9618
JH
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
a85b377d
JH
317 proc->env = env.argv;
318 }
319}
320
9684e44a
JH
321typedef int (*feed_fn)(void *, const char **, size_t *);
322static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
b1bf95bb 323{
f43cd49f 324 struct child_process proc;
6d525d38 325 struct async muxer;
f43cd49f 326 const char *argv[2];
9684e44a 327 int code;
b1bf95bb 328
5a7da2dc
AS
329 argv[0] = find_hook(hook_name);
330 if (!argv[0])
b1bf95bb 331 return 0;
c8dd2771 332
f43cd49f
SP
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
a85b377d
JH
340 prepare_push_cert_sha1(&proc);
341
6d525d38
SP
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
f43cd49f 352 code = start_command(&proc);
6d525d38
SP
353 if (code) {
354 if (use_sideband)
355 finish_async(&muxer);
90e41a89 356 return code;
6d525d38
SP
357 }
358
9684e44a
JH
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;
c8dd2771 366 }
e72ae288 367 close(proc.in);
6d525d38
SP
368 if (use_sideband)
369 finish_async(&muxer);
90e41a89 370 return finish_command(&proc);
b1bf95bb
JW
371}
372
9684e44a
JH
373struct receive_hook_feed_state {
374 struct command *cmd;
cdc2b2f3 375 int skip_broken;
9684e44a
JH
376 struct strbuf buf;
377};
378
379static 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
cdc2b2f3
JH
384 while (cmd &&
385 state->skip_broken && (cmd->error_string || cmd->did_not_exist))
9684e44a
JH
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
cdc2b2f3
JH
401static int run_receive_hook(struct command *commands, const char *hook_name,
402 int skip_broken)
9684e44a
JH
403{
404 struct receive_hook_feed_state state;
405 int status;
406
407 strbuf_init(&state.buf, 0);
408 state.cmd = commands;
cdc2b2f3 409 state.skip_broken = skip_broken;
9684e44a
JH
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
1d9e8b56
SP
418static int run_update_hook(struct command *cmd)
419{
1d9e8b56 420 const char *argv[5];
6d525d38
SP
421 struct child_process proc;
422 int code;
1d9e8b56 423
5a7da2dc
AS
424 argv[0] = find_hook("update");
425 if (!argv[0])
1d9e8b56
SP
426 return 0;
427
1d9e8b56
SP
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
6d525d38
SP
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);
1d9e8b56
SP
445}
446
986e8239
JK
447static int is_ref_checked_out(const char *ref)
448{
986e8239
JK
449 if (is_bare_repository())
450 return 0;
451
747ca245 452 if (!head_name)
986e8239 453 return 0;
747ca245 454 return !strcmp(head_name, ref);
986e8239
JK
455}
456
acd2a45b
JH
457static 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.",
3d95d92b
JH
462 "",
463 "You can set 'receive.denyCurrentBranch' configuration variable to",
acd2a45b
JH
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.",
3d95d92b 468 "",
acd2a45b
JH
469 "To squelch this message and still keep the default behaviour, set",
470 "'receive.denyCurrentBranch' configuration variable to 'refuse'."
3d95d92b
JH
471};
472
acd2a45b 473static void refuse_unconfigured_deny(void)
3d95d92b
JH
474{
475 int i;
acd2a45b 476 for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
a886ba28 477 rp_error("%s", refuse_unconfigured_deny_msg[i]);
3d95d92b
JH
478}
479
375881fa
JH
480static 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.",
747ca245
JH
483 "",
484 "You can set 'receive.denyDeleteCurrent' configuration variable to",
375881fa
JH
485 "'warn' or 'ignore' in the remote repository to allow deleting the",
486 "current branch, with or without a warning message.",
747ca245 487 "",
375881fa 488 "To squelch this message, you can set it to 'refuse'."
747ca245
JH
489};
490
375881fa 491static void refuse_unconfigured_deny_delete_current(void)
747ca245
JH
492{
493 int i;
494 for (i = 0;
375881fa 495 i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
747ca245 496 i++)
a886ba28 497 rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
747ca245
JH
498}
499
0a1bc12b
NTND
500static int command_singleton_iterator(void *cb_data, unsigned char sha1[20]);
501static 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
6aa30857 509 trace_printf_key(&trace_shallow,
0a1bc12b
NTND
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
539static const char *update(struct command *cmd, struct shallow_info *si)
2eca23da 540{
cfee10a7 541 const char *name = cmd->ref_name;
6b01ecfe
JT
542 struct strbuf namespaced_name_buf = STRBUF_INIT;
543 const char *namespaced_name;
cfee10a7
JH
544 unsigned char *old_sha1 = cmd->old_sha1;
545 unsigned char *new_sha1 = cmd->new_sha1;
3159c8dc 546 struct ref_lock *lock;
2eca23da 547
061d6b9a 548 /* only refs/... are allowed */
59556548 549 if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
466dbc42 550 rp_error("refusing to create funny ref '%s' remotely", name);
8aaf7d64 551 return "funny refname";
cfee10a7 552 }
d8a1deec 553
6b01ecfe
JT
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)) {
3d95d92b
JH
558 switch (deny_current_branch) {
559 case DENY_IGNORE:
986e8239 560 break;
3d95d92b 561 case DENY_WARN:
466dbc42 562 rp_warning("updating the current branch");
986e8239 563 break;
3d95d92b 564 case DENY_REFUSE:
acd2a45b 565 case DENY_UNCONFIGURED:
466dbc42 566 rp_error("refusing to update checked out branch: %s", name);
acd2a45b
JH
567 if (deny_current_branch == DENY_UNCONFIGURED)
568 refuse_unconfigured_deny();
3d95d92b
JH
569 return "branch is currently checked out";
570 }
986e8239
JK
571 }
572
d4f694ba 573 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
8aaf7d64
SP
574 error("unpack should have generated %s, "
575 "but I can't find it!", sha1_to_hex(new_sha1));
576 return "bad pack";
cfee10a7 577 }
747ca245
JH
578
579 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
59556548 580 if (deny_deletes && starts_with(name, "refs/heads/")) {
466dbc42 581 rp_error("denying ref deletion for %s", name);
747ca245
JH
582 return "deletion prohibited";
583 }
584
6b01ecfe 585 if (!strcmp(namespaced_name, head_name)) {
747ca245
JH
586 switch (deny_delete_current) {
587 case DENY_IGNORE:
588 break;
589 case DENY_WARN:
466dbc42 590 rp_warning("deleting the current branch");
747ca245
JH
591 break;
592 case DENY_REFUSE:
375881fa
JH
593 case DENY_UNCONFIGURED:
594 if (deny_delete_current == DENY_UNCONFIGURED)
595 refuse_unconfigured_deny_delete_current();
466dbc42 596 rp_error("refusing to delete the current branch: %s", name);
747ca245
JH
597 return "deletion of the current branch prohibited";
598 }
599 }
a240de11 600 }
747ca245 601
d4f694ba 602 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
ba988a83 603 !is_null_sha1(old_sha1) &&
59556548 604 starts_with(name, "refs/heads/")) {
eab82707 605 struct object *old_object, *new_object;
11031d7e 606 struct commit *old_commit, *new_commit;
11031d7e 607
eab82707
MK
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;
5d55915c 619 if (!in_merge_bases(old_commit, new_commit)) {
466dbc42
SP
620 rp_error("denying non-fast-forward %s"
621 " (you should pull first)", name);
a75d7b54 622 return "non-fast-forward";
8aaf7d64 623 }
11031d7e 624 }
1d9e8b56 625 if (run_update_hook(cmd)) {
466dbc42 626 rp_error("hook declined to update %s", name);
8aaf7d64 627 return "hook declined";
b1bf95bb 628 }
3159c8dc 629
d4f694ba 630 if (is_null_sha1(new_sha1)) {
28391a80 631 if (!parse_object(old_sha1)) {
28391a80 632 old_sha1 = NULL;
160b81ed
PYH
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 }
28391a80 639 }
6b01ecfe 640 if (delete_ref(namespaced_name, old_sha1, 0)) {
466dbc42 641 rp_error("failed to delete %s", name);
8aaf7d64 642 return "failed to delete";
d4f694ba 643 }
8aaf7d64 644 return NULL; /* good */
d4f694ba
JH
645 }
646 else {
0a1bc12b
NTND
647 if (shallow_update && si->shallow_ref[cmd->index] &&
648 update_shallow_ref(cmd, si))
649 return "shallow error";
650
9bbb0fa1
BK
651 lock = lock_any_ref_for_update(namespaced_name, old_sha1,
652 0, NULL);
d4f694ba 653 if (!lock) {
466dbc42 654 rp_error("failed to lock %s", name);
8aaf7d64 655 return "failed to lock";
d4f694ba 656 }
ef203f08 657 if (write_ref_sha1(lock, new_sha1, "push")) {
8aaf7d64 658 return "failed to write"; /* error() already called */
ef203f08 659 }
8aaf7d64 660 return NULL; /* good */
19614330 661 }
2eca23da
LT
662}
663
5e1c71fd 664static void run_update_post_hook(struct command *commands)
19614330 665{
5e1c71fd 666 struct command *cmd;
6d525d38 667 int argc;
9201c707 668 const char **argv;
6d525d38 669 struct child_process proc;
5a7da2dc 670 char *hook;
19614330 671
5a7da2dc 672 hook = find_hook("post-update");
5e1c71fd 673 for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
160b81ed 674 if (cmd->error_string || cmd->did_not_exist)
19614330
JH
675 continue;
676 argc++;
677 }
5a7da2dc 678 if (!argc || !hook)
3e6e152c 679 return;
5a7da2dc 680
3e6e152c 681 argv = xmalloc(sizeof(*argv) * (2 + argc));
5a7da2dc 682 argv[0] = hook;
19614330 683
5e1c71fd 684 for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
160b81ed 685 if (cmd->error_string || cmd->did_not_exist)
19614330 686 continue;
95244ae3 687 argv[argc] = xstrdup(cmd->ref_name);
19614330
JH
688 argc++;
689 }
690 argv[argc] = NULL;
6d525d38
SP
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 }
19614330 703}
2eca23da 704
da3efdb1
JS
705static void check_aliased_update(struct command *cmd, struct string_list *list)
706{
6b01ecfe
JT
707 struct strbuf buf = STRBUF_INIT;
708 const char *dst_name;
da3efdb1
JS
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
6b01ecfe 715 strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
8cad4744 716 dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag);
6b01ecfe 717 strbuf_release(&buf);
da3efdb1
JS
718
719 if (!(flag & REF_ISSYMREF))
720 return;
721
6b01ecfe
JT
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
e8c8b713 730 if ((item = string_list_lookup(list, dst_name)) == NULL)
da3efdb1
JS
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));
0e71bc30 744 strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
da3efdb1 745 strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
0e71bc30 746 strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
da3efdb1
JS
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
756static void check_aliased_updates(struct command *commands)
757{
758 struct command *cmd;
183113a5 759 struct string_list ref_list = STRING_LIST_INIT_NODUP;
da3efdb1
JS
760
761 for (cmd = commands; cmd; cmd = cmd->next) {
762 struct string_list_item *item =
1d2f80fa 763 string_list_append(&ref_list, cmd->ref_name);
da3efdb1
JS
764 item->util = (void *)cmd;
765 }
766 sort_string_list(&ref_list);
767
ef7e93d9
CB
768 for (cmd = commands; cmd; cmd = cmd->next) {
769 if (!cmd->error_string)
770 check_aliased_update(cmd, &ref_list);
771 }
da3efdb1
JS
772
773 string_list_clear(&ref_list, 0);
774}
775
52fed6e1
JH
776static 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
ee6dfb2d 781 if (!cmd || is_null_sha1(cmd->new_sha1))
52fed6e1
JH
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
0a1bc12b
NTND
788static void set_connectivity_errors(struct command *commands,
789 struct shallow_info *si)
52fed6e1
JH
790{
791 struct command *cmd;
792
793 for (cmd = commands; cmd; cmd = cmd->next) {
794 struct command *singleton = cmd;
0a1bc12b
NTND
795 if (shallow_update && si->shallow_ref[cmd->index])
796 /* to be checked in update_shallow_ref() */
797 continue;
52fed6e1
JH
798 if (!check_everything_connected(command_singleton_iterator,
799 0, &singleton))
800 continue;
801 cmd->error_string = "missing necessary objects";
802 }
803}
804
0a1bc12b
NTND
805struct iterate_data {
806 struct command *cmds;
807 struct shallow_info *si;
808};
809
52fed6e1
JH
810static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
811{
0a1bc12b
NTND
812 struct iterate_data *data = cb_data;
813 struct command **cmd_list = &data->cmds;
52fed6e1
JH
814 struct command *cmd = *cmd_list;
815
0a1bc12b
NTND
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;
5dbd7676 820 if (!is_null_sha1(cmd->new_sha1) && !cmd->skip_update) {
ee6dfb2d
JH
821 hashcpy(sha1, cmd->new_sha1);
822 *cmd_list = cmd->next;
823 return 0;
824 }
ee6dfb2d
JH
825 }
826 *cmd_list = NULL;
827 return -1; /* end of list */
52fed6e1
JH
828}
829
daebaa78
JH
830static 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
0a1bc12b
NTND
844static void execute_commands(struct command *commands,
845 const char *unpacker_error,
846 struct shallow_info *si)
575f4974 847{
0a1bc12b 848 int checked_connectivity;
5e1c71fd 849 struct command *cmd;
747ca245 850 unsigned char sha1[20];
0a1bc12b 851 struct iterate_data data;
8aaf7d64
SP
852
853 if (unpacker_error) {
5e1c71fd 854 for (cmd = commands; cmd; cmd = cmd->next)
74eb32d3 855 cmd->error_string = "unpacker error";
8aaf7d64
SP
856 return;
857 }
858
0a1bc12b
NTND
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);
52fed6e1 863
daebaa78
JH
864 reject_updates_to_hidden(commands);
865
5a7da2dc 866 if (run_receive_hook(commands, "pre-receive", 0)) {
ef7e93d9
CB
867 for (cmd = commands; cmd; cmd = cmd->next) {
868 if (!cmd->error_string)
869 cmd->error_string = "pre-receive hook declined";
870 }
05ef58ec
SP
871 return;
872 }
873
da3efdb1
JS
874 check_aliased_updates(commands);
875
96ec7b1e
NTND
876 free(head_name_to_free);
877 head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL);
747ca245 878
0a1bc12b 879 checked_connectivity = 1;
ef7e93d9
CB
880 for (cmd = commands; cmd; cmd = cmd->next) {
881 if (cmd->error_string)
882 continue;
883
884 if (cmd->skip_update)
885 continue;
886
0a1bc12b
NTND
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
0179c945
JK
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");
575f4974
LT
900}
901
39895c74
JH
902static 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
4adf569d
JH
929static void queue_commands_from_cert(struct command **tail,
930 struct strbuf *push_cert)
931{
932 const char *boc, *eoc;
933
934 if (*tail)
935 die("protocol error: got both push certificate and unsigned commands");
936
937 boc = strstr(push_cert->buf, "\n\n");
938 if (!boc)
939 die("malformed push certificate %.*s", 100, push_cert->buf);
940 else
941 boc += 2;
942 eoc = push_cert->buf + parse_signature(push_cert->buf, push_cert->len);
943
944 while (boc < eoc) {
945 const char *eol = memchr(boc, '\n', eoc - boc);
946 tail = queue_command(tail, boc, eol ? eol - boc : eoc - eol);
947 boc = eol ? eol + 1 : eoc;
948 }
949}
950
5dbd7676 951static struct command *read_head_info(struct sha1_array *shallow)
575f4974 952{
5e1c71fd 953 struct command *commands = NULL;
eb1af2df 954 struct command **p = &commands;
575f4974 955 for (;;) {
74543a04 956 char *line;
39895c74 957 int len, linelen;
eb1af2df 958
74543a04
JK
959 line = packet_read_line(0, &len);
960 if (!line)
575f4974 961 break;
5dbd7676 962
92251b1b 963 if (len == 48 && starts_with(line, "shallow ")) {
c09b71cc
JH
964 unsigned char sha1[20];
965 if (get_sha1_hex(line + 8, sha1))
966 die("protocol error: expected shallow sha, got '%s'",
967 line + 8);
968 sha1_array_append(shallow, sha1);
5dbd7676
NTND
969 continue;
970 }
971
0e3c339b
JH
972 linelen = strlen(line);
973 if (linelen < len) {
974 const char *feature_list = line + linelen + 1;
975 if (parse_feature_request(feature_list, "report-status"))
976 report_status = 1;
977 if (parse_feature_request(feature_list, "side-band-64k"))
978 use_sideband = LARGE_PACKET_MAX;
979 if (parse_feature_request(feature_list, "quiet"))
980 quiet = 1;
981 }
982
a85b377d
JH
983 if (!strcmp(line, "push-cert")) {
984 int true_flush = 0;
985 char certbuf[1024];
986
987 for (;;) {
988 len = packet_read(0, NULL, NULL,
989 certbuf, sizeof(certbuf), 0);
990 if (!len) {
991 true_flush = 1;
992 break;
993 }
994 if (!strcmp(certbuf, "push-cert-end\n"))
995 break; /* end of cert */
996 strbuf_addstr(&push_cert, certbuf);
997 }
998
999 if (true_flush)
1000 break;
1001 continue;
1002 }
1003
39895c74 1004 p = queue_command(p, line, linelen);
575f4974 1005 }
4adf569d
JH
1006
1007 if (push_cert.len)
1008 queue_commands_from_cert(p, &push_cert);
1009
5e1c71fd 1010 return commands;
575f4974
LT
1011}
1012
fc04c412
SP
1013static const char *parse_pack_header(struct pack_header *hdr)
1014{
a69e5429
JH
1015 switch (read_pack_header(0, hdr)) {
1016 case PH_ERROR_EOF:
1017 return "eof before pack header was fully read";
1018
1019 case PH_ERROR_PACK_SIGNATURE:
fc04c412 1020 return "protocol error (pack signature mismatch detected)";
a69e5429
JH
1021
1022 case PH_ERROR_PROTOCOL:
fc04c412 1023 return "protocol error (pack version unsupported)";
a69e5429
JH
1024
1025 default:
1026 return "unknown error in parse_pack_header";
1027
1028 case 0:
1029 return NULL;
1030 }
fc04c412
SP
1031}
1032
576162a4
NP
1033static const char *pack_lockfile;
1034
5dbd7676 1035static const char *unpack(int err_fd, struct shallow_info *si)
575f4974 1036{
fc04c412 1037 struct pack_header hdr;
31c42bff 1038 struct argv_array av = ARGV_ARRAY_INIT;
fc04c412 1039 const char *hdr_err;
31c42bff 1040 int status;
fc04c412 1041 char hdr_arg[38];
31c42bff 1042 struct child_process child;
dab76d3a
JH
1043 int fsck_objects = (receive_fsck_objects >= 0
1044 ? receive_fsck_objects
1045 : transfer_fsck_objects >= 0
1046 ? transfer_fsck_objects
1047 : 0);
fc04c412
SP
1048
1049 hdr_err = parse_pack_header(&hdr);
49ecfa13
JK
1050 if (hdr_err) {
1051 if (err_fd > 0)
1052 close(err_fd);
fc04c412 1053 return hdr_err;
49ecfa13 1054 }
6e1c2344
RJ
1055 snprintf(hdr_arg, sizeof(hdr_arg),
1056 "--pack_header=%"PRIu32",%"PRIu32,
fc04c412
SP
1057 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
1058
5dbd7676
NTND
1059 if (si->nr_ours || si->nr_theirs) {
1060 alt_shallow_file = setup_temporary_shallow(si->shallow);
1061 argv_array_pushl(&av, "--shallow-file", alt_shallow_file, NULL);
1062 }
1063
31c42bff 1064 memset(&child, 0, sizeof(child));
fc04c412 1065 if (ntohl(hdr.hdr_entries) < unpack_limit) {
31c42bff 1066 argv_array_pushl(&av, "unpack-objects", hdr_arg, NULL);
c207e34f 1067 if (quiet)
31c42bff 1068 argv_array_push(&av, "-q");
dab76d3a 1069 if (fsck_objects)
31c42bff
NTND
1070 argv_array_push(&av, "--strict");
1071 child.argv = av.argv;
59bfdfb8 1072 child.no_stdout = 1;
a22e6f85 1073 child.err = err_fd;
59bfdfb8 1074 child.git_cmd = 1;
31c42bff
NTND
1075 status = run_command(&child);
1076 if (status)
1077 return "unpack-objects abnormal exit";
576162a4 1078 } else {
31c42bff 1079 int s;
576162a4 1080 char keep_arg[256];
576162a4 1081
85e72830 1082 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
576162a4
NP
1083 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
1084 strcpy(keep_arg + s, "localhost");
1085
31c42bff
NTND
1086 argv_array_pushl(&av, "index-pack",
1087 "--stdin", hdr_arg, keep_arg, NULL);
dab76d3a 1088 if (fsck_objects)
31c42bff 1089 argv_array_push(&av, "--strict");
f7c815c3 1090 if (fix_thin)
31c42bff
NTND
1091 argv_array_push(&av, "--fix-thin");
1092 child.argv = av.argv;
1093 child.out = -1;
1094 child.err = err_fd;
1095 child.git_cmd = 1;
1096 status = start_command(&child);
1097 if (status)
576162a4 1098 return "index-pack fork failed";
31c42bff
NTND
1099 pack_lockfile = index_pack_lockfile(child.out);
1100 close(child.out);
1101 status = finish_command(&child);
1102 if (status)
1103 return "index-pack abnormal exit";
1104 reprepare_packed_git();
cfee10a7 1105 }
31c42bff 1106 return NULL;
cfee10a7
JH
1107}
1108
5dbd7676 1109static const char *unpack_with_sideband(struct shallow_info *si)
a22e6f85
JK
1110{
1111 struct async muxer;
1112 const char *ret;
1113
1114 if (!use_sideband)
5dbd7676 1115 return unpack(0, si);
a22e6f85
JK
1116
1117 memset(&muxer, 0, sizeof(muxer));
1118 muxer.proc = copy_to_sideband;
1119 muxer.in = -1;
1120 if (start_async(&muxer))
1121 return NULL;
1122
5dbd7676 1123 ret = unpack(muxer.in, si);
a22e6f85
JK
1124
1125 finish_async(&muxer);
1126 return ret;
1127}
1128
0a1bc12b
NTND
1129static void prepare_shallow_update(struct command *commands,
1130 struct shallow_info *si)
1131{
1132 int i, j, k, bitmap_size = (si->ref->nr + 31) / 32;
1133
1134 si->used_shallow = xmalloc(sizeof(*si->used_shallow) *
1135 si->shallow->nr);
1136 assign_shallow_commits_to_refs(si, si->used_shallow, NULL);
1137
1138 si->need_reachability_test =
1139 xcalloc(si->shallow->nr, sizeof(*si->need_reachability_test));
1140 si->reachable =
1141 xcalloc(si->shallow->nr, sizeof(*si->reachable));
1142 si->shallow_ref = xcalloc(si->ref->nr, sizeof(*si->shallow_ref));
1143
1144 for (i = 0; i < si->nr_ours; i++)
1145 si->need_reachability_test[si->ours[i]] = 1;
1146
1147 for (i = 0; i < si->shallow->nr; i++) {
1148 if (!si->used_shallow[i])
1149 continue;
1150 for (j = 0; j < bitmap_size; j++) {
1151 if (!si->used_shallow[i][j])
1152 continue;
1153 si->need_reachability_test[i]++;
1154 for (k = 0; k < 32; k++)
1155 if (si->used_shallow[i][j] & (1 << k))
1156 si->shallow_ref[j * 32 + k]++;
1157 }
1158
1159 /*
1160 * true for those associated with some refs and belong
1161 * in "ours" list aka "step 7 not done yet"
1162 */
1163 si->need_reachability_test[i] =
1164 si->need_reachability_test[i] > 1;
1165 }
1166
1167 /*
1168 * keep hooks happy by forcing a temporary shallow file via
1169 * env variable because we can't add --shallow-file to every
1170 * command. check_everything_connected() will be done with
1171 * true .git/shallow though.
1172 */
1173 setenv(GIT_SHALLOW_FILE_ENVIRONMENT, alt_shallow_file, 1);
1174}
1175
5dbd7676
NTND
1176static void update_shallow_info(struct command *commands,
1177 struct shallow_info *si,
1178 struct sha1_array *ref)
1179{
1180 struct command *cmd;
1181 int *ref_status;
1182 remove_nonexistent_theirs_shallow(si);
0a1bc12b
NTND
1183 if (!si->nr_ours && !si->nr_theirs) {
1184 shallow_update = 0;
5dbd7676 1185 return;
0a1bc12b 1186 }
5dbd7676
NTND
1187
1188 for (cmd = commands; cmd; cmd = cmd->next) {
1189 if (is_null_sha1(cmd->new_sha1))
1190 continue;
1191 sha1_array_append(ref, cmd->new_sha1);
1192 cmd->index = ref->nr - 1;
1193 }
1194 si->ref = ref;
1195
0a1bc12b
NTND
1196 if (shallow_update) {
1197 prepare_shallow_update(commands, si);
1198 return;
1199 }
1200
5dbd7676
NTND
1201 ref_status = xmalloc(sizeof(*ref_status) * ref->nr);
1202 assign_shallow_commits_to_refs(si, NULL, ref_status);
1203 for (cmd = commands; cmd; cmd = cmd->next) {
1204 if (is_null_sha1(cmd->new_sha1))
1205 continue;
1206 if (ref_status[cmd->index]) {
1207 cmd->error_string = "shallow update not allowed";
1208 cmd->skip_update = 1;
1209 }
1210 }
5dbd7676
NTND
1211 free(ref_status);
1212}
1213
5e1c71fd 1214static void report(struct command *commands, const char *unpack_status)
cfee10a7
JH
1215{
1216 struct command *cmd;
38a81b4e
SP
1217 struct strbuf buf = STRBUF_INIT;
1218
1219 packet_buf_write(&buf, "unpack %s\n",
1220 unpack_status ? unpack_status : "ok");
cfee10a7
JH
1221 for (cmd = commands; cmd; cmd = cmd->next) {
1222 if (!cmd->error_string)
38a81b4e
SP
1223 packet_buf_write(&buf, "ok %s\n",
1224 cmd->ref_name);
cfee10a7 1225 else
38a81b4e
SP
1226 packet_buf_write(&buf, "ng %s %s\n",
1227 cmd->ref_name, cmd->error_string);
575f4974 1228 }
38a81b4e
SP
1229 packet_buf_flush(&buf);
1230
1231 if (use_sideband)
1232 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
1233 else
cdf4fb8e 1234 write_or_die(1, buf.buf, buf.len);
38a81b4e 1235 strbuf_release(&buf);
575f4974
LT
1236}
1237
5e1c71fd 1238static int delete_only(struct command *commands)
d4f694ba 1239{
5e1c71fd
JS
1240 struct command *cmd;
1241 for (cmd = commands; cmd; cmd = cmd->next) {
d4f694ba
JH
1242 if (!is_null_sha1(cmd->new_sha1))
1243 return 0;
d4f694ba
JH
1244 }
1245 return 1;
1246}
1247
be5908ae 1248int cmd_receive_pack(int argc, const char **argv, const char *prefix)
575f4974 1249{
42526b47
SP
1250 int advertise_refs = 0;
1251 int stateless_rpc = 0;
d0efc8a7 1252 int i;
d51428bf 1253 const char *dir = NULL;
5e1c71fd 1254 struct command *commands;
5dbd7676
NTND
1255 struct sha1_array shallow = SHA1_ARRAY_INIT;
1256 struct sha1_array ref = SHA1_ARRAY_INIT;
1257 struct shallow_info si;
575f4974 1258
bbc30f99
JK
1259 packet_trace_identity("receive-pack");
1260
575f4974
LT
1261 argv++;
1262 for (i = 1; i < argc; i++) {
be5908ae 1263 const char *arg = *argv++;
575f4974
LT
1264
1265 if (*arg == '-') {
c207e34f
CB
1266 if (!strcmp(arg, "--quiet")) {
1267 quiet = 1;
1268 continue;
1269 }
1270
42526b47
SP
1271 if (!strcmp(arg, "--advertise-refs")) {
1272 advertise_refs = 1;
1273 continue;
1274 }
1275 if (!strcmp(arg, "--stateless-rpc")) {
1276 stateless_rpc = 1;
1277 continue;
1278 }
f7c815c3
NTND
1279 if (!strcmp(arg, "--reject-thin-pack-for-testing")) {
1280 fix_thin = 0;
1281 continue;
1282 }
42526b47 1283
575f4974
LT
1284 usage(receive_pack_usage);
1285 }
d0efc8a7
LT
1286 if (dir)
1287 usage(receive_pack_usage);
d51428bf 1288 dir = arg;
575f4974
LT
1289 }
1290 if (!dir)
1291 usage(receive_pack_usage);
1292
e1464ca7 1293 setup_path();
5c09f321 1294
3159c8dc 1295 if (!enter_repo(dir, 0))
05ac6b34 1296 die("'%s' does not appear to be a git repository", dir);
575f4974 1297
ef90d6d4 1298 git_config(receive_pack_config, NULL);
6fb75bed 1299
e28714c5
JH
1300 if (0 <= transfer_unpack_limit)
1301 unpack_limit = transfer_unpack_limit;
1302 else if (0 <= receive_unpack_limit)
1303 unpack_limit = receive_unpack_limit;
1304
42526b47 1305 if (advertise_refs || !stateless_rpc) {
42526b47 1306 write_head_info();
42526b47
SP
1307 }
1308 if (advertise_refs)
1309 return 0;
575f4974 1310
5dbd7676 1311 if ((commands = read_head_info(&shallow)) != NULL) {
d4f694ba
JH
1312 const char *unpack_status = NULL;
1313
5dbd7676 1314 prepare_shallow_info(&si, &shallow);
0a1bc12b
NTND
1315 if (!si.nr_ours && !si.nr_theirs)
1316 shallow_update = 0;
5dbd7676
NTND
1317 if (!delete_only(commands)) {
1318 unpack_status = unpack_with_sideband(&si);
1319 update_shallow_info(commands, &si, &ref);
1320 }
0a1bc12b 1321 execute_commands(commands, unpack_status, &si);
576162a4 1322 if (pack_lockfile)
691f1a28 1323 unlink_or_warn(pack_lockfile);
cfee10a7 1324 if (report_status)
5e1c71fd 1325 report(commands, unpack_status);
5a7da2dc 1326 run_receive_hook(commands, "post-receive", 1);
8e663d9e 1327 run_update_post_hook(commands);
77e3efbf
JH
1328 if (auto_gc) {
1329 const char *argv_gc_auto[] = {
1330 "gc", "--auto", "--quiet", NULL,
1331 };
4b7f2fa4
JH
1332 int opt = RUN_GIT_CMD | RUN_COMMAND_STDOUT_TO_STDERR;
1333 run_command_v_opt(argv_gc_auto, opt);
77e3efbf
JH
1334 }
1335 if (auto_update_server_info)
1336 update_server_info(0);
5dbd7676 1337 clear_shallow_info(&si);
7f8e9828 1338 }
38a81b4e
SP
1339 if (use_sideband)
1340 packet_flush(1);
5dbd7676
NTND
1341 sha1_array_clear(&shallow);
1342 sha1_array_clear(&ref);
575f4974
LT
1343 return 0;
1344}