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