]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-receive-pack.c
send-pack: demultiplex a sideband stream with status data
[thirdparty/git.git] / builtin-receive-pack.c
CommitLineData
575f4974 1#include "cache.h"
fc04c412 2#include "pack.h"
8a65ff76 3#include "refs.h"
f3a3214e 4#include "pkt-line.h"
b1bf95bb 5#include "run-command.h"
576162a4 6#include "exec_cmd.h"
11031d7e
JS
7#include "commit.h"
8#include "object.h"
d79796bc
JH
9#include "remote.h"
10#include "transport.h"
575f4974 11
34263de0 12static const char receive_pack_usage[] = "git receive-pack <git-dir>";
575f4974 13
986e8239 14enum deny_action {
3d95d92b 15 DENY_UNCONFIGURED,
986e8239
JK
16 DENY_IGNORE,
17 DENY_WARN,
18 DENY_REFUSE,
19};
20
1b53a076
JH
21static int deny_deletes;
22static int deny_non_fast_forwards;
3d95d92b 23static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
747ca245 24static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
20dc0016 25static int receive_fsck_objects;
e28714c5
JH
26static int receive_unpack_limit = -1;
27static int transfer_unpack_limit = -1;
46732fae 28static int unpack_limit = 100;
96f1e58f 29static int report_status;
b74fce16 30static int prefer_ofs_delta = 1;
77e3efbf
JH
31static int auto_update_server_info;
32static int auto_gc = 1;
747ca245 33static const char *head_name;
b74fce16 34static char *capabilities_to_send;
cfee10a7 35
986e8239
JK
36static enum deny_action parse_deny_action(const char *var, const char *value)
37{
38 if (value) {
39 if (!strcasecmp(value, "ignore"))
40 return DENY_IGNORE;
41 if (!strcasecmp(value, "warn"))
42 return DENY_WARN;
43 if (!strcasecmp(value, "refuse"))
44 return DENY_REFUSE;
45 }
46 if (git_config_bool(var, value))
47 return DENY_REFUSE;
48 return DENY_IGNORE;
49}
50
ef90d6d4 51static int receive_pack_config(const char *var, const char *value, void *cb)
6fb75bed 52{
a240de11
JK
53 if (strcmp(var, "receive.denydeletes") == 0) {
54 deny_deletes = git_config_bool(var, value);
55 return 0;
56 }
57
e28714c5 58 if (strcmp(var, "receive.denynonfastforwards") == 0) {
6fb75bed
SP
59 deny_non_fast_forwards = git_config_bool(var, value);
60 return 0;
61 }
62
e28714c5
JH
63 if (strcmp(var, "receive.unpacklimit") == 0) {
64 receive_unpack_limit = git_config_int(var, value);
fc04c412
SP
65 return 0;
66 }
67
e28714c5
JH
68 if (strcmp(var, "transfer.unpacklimit") == 0) {
69 transfer_unpack_limit = git_config_int(var, value);
70 return 0;
71 }
72
20dc0016
MK
73 if (strcmp(var, "receive.fsckobjects") == 0) {
74 receive_fsck_objects = git_config_bool(var, value);
75 return 0;
76 }
77
986e8239
JK
78 if (!strcmp(var, "receive.denycurrentbranch")) {
79 deny_current_branch = parse_deny_action(var, value);
80 return 0;
81 }
82
747ca245
JH
83 if (strcmp(var, "receive.denydeletecurrent") == 0) {
84 deny_delete_current = parse_deny_action(var, value);
85 return 0;
86 }
87
b74fce16
NP
88 if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
89 prefer_ofs_delta = git_config_bool(var, value);
90 return 0;
91 }
92
77e3efbf
JH
93 if (strcmp(var, "receive.updateserverinfo") == 0) {
94 auto_update_server_info = git_config_bool(var, value);
95 return 0;
96 }
97
98 if (strcmp(var, "receive.autogc") == 0) {
99 auto_gc = git_config_bool(var, value);
100 return 0;
101 }
102
ef90d6d4 103 return git_default_config(var, value, cb);
6fb75bed
SP
104}
105
8da19775 106static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
575f4974 107{
b74fce16 108 if (!capabilities_to_send)
cfee10a7
JH
109 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
110 else
111 packet_write(1, "%s %s%c%s\n",
b74fce16
NP
112 sha1_to_hex(sha1), path, 0, capabilities_to_send);
113 capabilities_to_send = NULL;
8a65ff76 114 return 0;
575f4974
LT
115}
116
8a65ff76 117static void write_head_info(void)
575f4974 118{
cb5d709f 119 for_each_ref(show_ref, NULL);
b74fce16 120 if (capabilities_to_send)
8da19775 121 show_ref("capabilities^{}", null_sha1, 0, NULL);
cfee10a7 122
575f4974
LT
123}
124
eb1af2df
LT
125struct command {
126 struct command *next;
cfee10a7 127 const char *error_string;
eb1af2df
LT
128 unsigned char old_sha1[20];
129 unsigned char new_sha1[20];
8f1d2e6f 130 char ref_name[FLEX_ARRAY]; /* more */
575f4974
LT
131};
132
96f1e58f 133static struct command *commands;
575f4974 134
05ef58ec
SP
135static const char pre_receive_hook[] = "hooks/pre-receive";
136static const char post_receive_hook[] = "hooks/post-receive";
b1bf95bb 137
ae98a008 138static int run_receive_hook(const char *hook_name)
b1bf95bb 139{
f43cd49f 140 static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
c8dd2771 141 struct command *cmd;
f43cd49f
SP
142 struct child_process proc;
143 const char *argv[2];
144 int have_input = 0, code;
c8dd2771 145
f43cd49f 146 for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
c8dd2771 147 if (!cmd->error_string)
f43cd49f 148 have_input = 1;
c8dd2771 149 }
b1bf95bb 150
f43cd49f 151 if (!have_input || access(hook_name, X_OK) < 0)
b1bf95bb 152 return 0;
c8dd2771 153
c8dd2771 154 argv[0] = hook_name;
f43cd49f
SP
155 argv[1] = NULL;
156
157 memset(&proc, 0, sizeof(proc));
158 proc.argv = argv;
159 proc.in = -1;
160 proc.stdout_to_stderr = 1;
161
162 code = start_command(&proc);
163 if (code)
90e41a89 164 return code;
f43cd49f 165 for (cmd = commands; cmd; cmd = cmd->next) {
c8dd2771 166 if (!cmd->error_string) {
f43cd49f
SP
167 size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n",
168 sha1_to_hex(cmd->old_sha1),
169 sha1_to_hex(cmd->new_sha1),
170 cmd->ref_name);
171 if (write_in_full(proc.in, buf, n) != n)
172 break;
c8dd2771 173 }
c8dd2771 174 }
e72ae288 175 close(proc.in);
90e41a89 176 return finish_command(&proc);
b1bf95bb
JW
177}
178
1d9e8b56
SP
179static int run_update_hook(struct command *cmd)
180{
181 static const char update_hook[] = "hooks/update";
1d9e8b56
SP
182 const char *argv[5];
183
184 if (access(update_hook, X_OK) < 0)
185 return 0;
186
187 argv[0] = update_hook;
188 argv[1] = cmd->ref_name;
189 argv[2] = sha1_to_hex(cmd->old_sha1);
190 argv[3] = sha1_to_hex(cmd->new_sha1);
191 argv[4] = NULL;
192
90e41a89
JS
193 return run_command_v_opt(argv, RUN_COMMAND_NO_STDIN |
194 RUN_COMMAND_STDOUT_TO_STDERR);
1d9e8b56
SP
195}
196
986e8239
JK
197static int is_ref_checked_out(const char *ref)
198{
986e8239
JK
199 if (is_bare_repository())
200 return 0;
201
747ca245 202 if (!head_name)
986e8239 203 return 0;
747ca245 204 return !strcmp(head_name, ref);
986e8239
JK
205}
206
3d95d92b
JH
207static char *warn_unconfigured_deny_msg[] = {
208 "Updating the currently checked out branch may cause confusion,",
209 "as the index and work tree do not reflect changes that are in HEAD.",
210 "As a result, you may see the changes you just pushed into it",
211 "reverted when you run 'git diff' over there, and you may want",
212 "to run 'git reset --hard' before starting to work to recover.",
213 "",
214 "You can set 'receive.denyCurrentBranch' configuration variable to",
215 "'refuse' in the remote repository to forbid pushing into its",
216 "current branch."
217 "",
218 "To allow pushing into the current branch, you can set it to 'ignore';",
219 "but this is not recommended unless you arranged to update its work",
220 "tree to match what you pushed in some other way.",
221 "",
222 "To squelch this message, you can set it to 'warn'.",
223 "",
224 "Note that the default will change in a future version of git",
225 "to refuse updating the current branch unless you have the",
226 "configuration variable set to either 'ignore' or 'warn'."
227};
228
229static void warn_unconfigured_deny(void)
230{
231 int i;
232 for (i = 0; i < ARRAY_SIZE(warn_unconfigured_deny_msg); i++)
88a667f0 233 warning("%s", warn_unconfigured_deny_msg[i]);
3d95d92b
JH
234}
235
747ca245
JH
236static char *warn_unconfigured_deny_delete_current_msg[] = {
237 "Deleting the current branch can cause confusion by making the next",
238 "'git clone' not check out any file.",
239 "",
240 "You can set 'receive.denyDeleteCurrent' configuration variable to",
241 "'refuse' in the remote repository to disallow deleting the current",
242 "branch.",
243 "",
244 "You can set it to 'ignore' to allow such a delete without a warning.",
245 "",
246 "To make this warning message less loud, you can set it to 'warn'.",
247 "",
248 "Note that the default will change in a future version of git",
249 "to refuse deleting the current branch unless you have the",
250 "configuration variable set to either 'ignore' or 'warn'."
251};
252
253static void warn_unconfigured_deny_delete_current(void)
254{
255 int i;
256 for (i = 0;
257 i < ARRAY_SIZE(warn_unconfigured_deny_delete_current_msg);
258 i++)
88a667f0 259 warning("%s", warn_unconfigured_deny_delete_current_msg[i]);
747ca245
JH
260}
261
8aaf7d64 262static const char *update(struct command *cmd)
2eca23da 263{
cfee10a7
JH
264 const char *name = cmd->ref_name;
265 unsigned char *old_sha1 = cmd->old_sha1;
266 unsigned char *new_sha1 = cmd->new_sha1;
3159c8dc 267 struct ref_lock *lock;
2eca23da 268
061d6b9a
MK
269 /* only refs/... are allowed */
270 if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) {
0b8293f6 271 error("refusing to create funny ref '%s' remotely", name);
8aaf7d64 272 return "funny refname";
cfee10a7 273 }
d8a1deec 274
3d95d92b
JH
275 if (is_ref_checked_out(name)) {
276 switch (deny_current_branch) {
277 case DENY_IGNORE:
986e8239 278 break;
3d95d92b
JH
279 case DENY_UNCONFIGURED:
280 case DENY_WARN:
281 warning("updating the current branch");
282 if (deny_current_branch == DENY_UNCONFIGURED)
283 warn_unconfigured_deny();
986e8239 284 break;
3d95d92b
JH
285 case DENY_REFUSE:
286 error("refusing to update checked out branch: %s", name);
287 return "branch is currently checked out";
288 }
986e8239
JK
289 }
290
d4f694ba 291 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
8aaf7d64
SP
292 error("unpack should have generated %s, "
293 "but I can't find it!", sha1_to_hex(new_sha1));
294 return "bad pack";
cfee10a7 295 }
747ca245
JH
296
297 if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
298 if (deny_deletes && !prefixcmp(name, "refs/heads/")) {
299 error("denying ref deletion for %s", name);
300 return "deletion prohibited";
301 }
302
303 if (!strcmp(name, head_name)) {
304 switch (deny_delete_current) {
305 case DENY_IGNORE:
306 break;
307 case DENY_WARN:
308 case DENY_UNCONFIGURED:
309 if (deny_delete_current == DENY_UNCONFIGURED)
310 warn_unconfigured_deny_delete_current();
311 warning("deleting the current branch");
312 break;
313 case DENY_REFUSE:
314 error("refusing to delete the current branch: %s", name);
315 return "deletion of the current branch prohibited";
316 }
317 }
a240de11 318 }
747ca245 319
d4f694ba 320 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
ba988a83 321 !is_null_sha1(old_sha1) &&
cc44c765 322 !prefixcmp(name, "refs/heads/")) {
eab82707 323 struct object *old_object, *new_object;
11031d7e 324 struct commit *old_commit, *new_commit;
9edd7e46 325 struct commit_list *bases, *ent;
11031d7e 326
eab82707
MK
327 old_object = parse_object(old_sha1);
328 new_object = parse_object(new_sha1);
329
330 if (!old_object || !new_object ||
331 old_object->type != OBJ_COMMIT ||
332 new_object->type != OBJ_COMMIT) {
333 error("bad sha1 objects for %s", name);
334 return "bad ref";
335 }
336 old_commit = (struct commit *)old_object;
337 new_commit = (struct commit *)new_object;
9edd7e46
JS
338 bases = get_merge_bases(old_commit, new_commit, 1);
339 for (ent = bases; ent; ent = ent->next)
340 if (!hashcmp(old_sha1, ent->item->object.sha1))
11031d7e 341 break;
9edd7e46 342 free_commit_list(bases);
8aaf7d64 343 if (!ent) {
a75d7b54 344 error("denying non-fast-forward %s"
8aaf7d64 345 " (you should pull first)", name);
a75d7b54 346 return "non-fast-forward";
8aaf7d64 347 }
11031d7e 348 }
1d9e8b56 349 if (run_update_hook(cmd)) {
8aaf7d64
SP
350 error("hook declined to update %s", name);
351 return "hook declined";
b1bf95bb 352 }
3159c8dc 353
d4f694ba 354 if (is_null_sha1(new_sha1)) {
28391a80
JS
355 if (!parse_object(old_sha1)) {
356 warning ("Allowing deletion of corrupt ref.");
357 old_sha1 = NULL;
358 }
eca35a25 359 if (delete_ref(name, old_sha1, 0)) {
8aaf7d64
SP
360 error("failed to delete %s", name);
361 return "failed to delete";
d4f694ba 362 }
8aaf7d64 363 return NULL; /* good */
d4f694ba
JH
364 }
365 else {
68db31cc 366 lock = lock_any_ref_for_update(name, old_sha1, 0);
d4f694ba 367 if (!lock) {
8aaf7d64
SP
368 error("failed to lock %s", name);
369 return "failed to lock";
d4f694ba 370 }
ef203f08 371 if (write_ref_sha1(lock, new_sha1, "push")) {
8aaf7d64 372 return "failed to write"; /* error() already called */
ef203f08 373 }
8aaf7d64 374 return NULL; /* good */
19614330 375 }
2eca23da
LT
376}
377
19614330
JH
378static char update_post_hook[] = "hooks/post-update";
379
380static void run_update_post_hook(struct command *cmd)
381{
382 struct command *cmd_p;
2ff4d1ab 383 int argc, status;
9201c707 384 const char **argv;
19614330 385
3e6e152c 386 for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
cfee10a7 387 if (cmd_p->error_string)
19614330
JH
388 continue;
389 argc++;
390 }
3e6e152c
SP
391 if (!argc || access(update_post_hook, X_OK) < 0)
392 return;
393 argv = xmalloc(sizeof(*argv) * (2 + argc));
19614330
JH
394 argv[0] = update_post_hook;
395
396 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
9201c707 397 char *p;
cfee10a7 398 if (cmd_p->error_string)
19614330 399 continue;
9201c707
JH
400 p = xmalloc(strlen(cmd_p->ref_name) + 1);
401 strcpy(p, cmd_p->ref_name);
402 argv[argc] = p;
19614330
JH
403 argc++;
404 }
405 argv[argc] = NULL;
2ff4d1ab
JS
406 status = run_command_v_opt(argv, RUN_COMMAND_NO_STDIN
407 | RUN_COMMAND_STDOUT_TO_STDERR);
19614330 408}
2eca23da 409
8aaf7d64 410static void execute_commands(const char *unpacker_error)
575f4974 411{
eb1af2df 412 struct command *cmd = commands;
747ca245 413 unsigned char sha1[20];
8aaf7d64
SP
414
415 if (unpacker_error) {
416 while (cmd) {
417 cmd->error_string = "n/a (unpacker error)";
418 cmd = cmd->next;
419 }
420 return;
421 }
422
ae98a008 423 if (run_receive_hook(pre_receive_hook)) {
05ef58ec
SP
424 while (cmd) {
425 cmd->error_string = "pre-receive hook declined";
426 cmd = cmd->next;
427 }
428 return;
429 }
430
747ca245
JH
431 head_name = resolve_ref("HEAD", sha1, 0, NULL);
432
eb1af2df 433 while (cmd) {
8aaf7d64 434 cmd->error_string = update(cmd);
eb1af2df 435 cmd = cmd->next;
575f4974
LT
436 }
437}
438
439static void read_head_info(void)
440{
eb1af2df 441 struct command **p = &commands;
575f4974
LT
442 for (;;) {
443 static char line[1000];
eb1af2df
LT
444 unsigned char old_sha1[20], new_sha1[20];
445 struct command *cmd;
cfee10a7
JH
446 char *refname;
447 int len, reflen;
eb1af2df
LT
448
449 len = packet_read_line(0, line, sizeof(line));
575f4974
LT
450 if (!len)
451 break;
eb1af2df
LT
452 if (line[len-1] == '\n')
453 line[--len] = 0;
454 if (len < 83 ||
455 line[40] != ' ' ||
456 line[81] != ' ' ||
457 get_sha1_hex(line, old_sha1) ||
458 get_sha1_hex(line + 41, new_sha1))
cfee10a7
JH
459 die("protocol error: expected old/new/ref, got '%s'",
460 line);
461
462 refname = line + 82;
463 reflen = strlen(refname);
464 if (reflen + 82 < len) {
465 if (strstr(refname + reflen + 1, "report-status"))
466 report_status = 1;
467 }
eb1af2df 468 cmd = xmalloc(sizeof(struct command) + len - 80);
e702496e
SP
469 hashcpy(cmd->old_sha1, old_sha1);
470 hashcpy(cmd->new_sha1, new_sha1);
eb1af2df 471 memcpy(cmd->ref_name, line + 82, len - 81);
8aaf7d64 472 cmd->error_string = NULL;
eb1af2df
LT
473 cmd->next = NULL;
474 *p = cmd;
475 p = &cmd->next;
575f4974
LT
476 }
477}
478
fc04c412
SP
479static const char *parse_pack_header(struct pack_header *hdr)
480{
a69e5429
JH
481 switch (read_pack_header(0, hdr)) {
482 case PH_ERROR_EOF:
483 return "eof before pack header was fully read";
484
485 case PH_ERROR_PACK_SIGNATURE:
fc04c412 486 return "protocol error (pack signature mismatch detected)";
a69e5429
JH
487
488 case PH_ERROR_PROTOCOL:
fc04c412 489 return "protocol error (pack version unsupported)";
a69e5429
JH
490
491 default:
492 return "unknown error in parse_pack_header";
493
494 case 0:
495 return NULL;
496 }
fc04c412
SP
497}
498
576162a4
NP
499static const char *pack_lockfile;
500
861ed121 501static const char *unpack(void)
575f4974 502{
fc04c412
SP
503 struct pack_header hdr;
504 const char *hdr_err;
505 char hdr_arg[38];
fc04c412
SP
506
507 hdr_err = parse_pack_header(&hdr);
508 if (hdr_err)
509 return hdr_err;
6e1c2344
RJ
510 snprintf(hdr_arg, sizeof(hdr_arg),
511 "--pack_header=%"PRIu32",%"PRIu32,
fc04c412
SP
512 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
513
514 if (ntohl(hdr.hdr_entries) < unpack_limit) {
20dc0016
MK
515 int code, i = 0;
516 const char *unpacker[4];
517 unpacker[i++] = "unpack-objects";
518 if (receive_fsck_objects)
519 unpacker[i++] = "--strict";
520 unpacker[i++] = hdr_arg;
521 unpacker[i++] = NULL;
9b0b5093 522 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
2ff4d1ab 523 if (!code)
fc04c412 524 return NULL;
2ff4d1ab 525 return "unpack-objects abnormal exit";
576162a4 526 } else {
20dc0016
MK
527 const char *keeper[7];
528 int s, status, i = 0;
576162a4 529 char keep_arg[256];
e8016abf 530 struct child_process ip;
576162a4 531
85e72830 532 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
576162a4
NP
533 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
534 strcpy(keep_arg + s, "localhost");
535
20dc0016
MK
536 keeper[i++] = "index-pack";
537 keeper[i++] = "--stdin";
538 if (receive_fsck_objects)
539 keeper[i++] = "--strict";
540 keeper[i++] = "--fix-thin";
541 keeper[i++] = hdr_arg;
542 keeper[i++] = keep_arg;
543 keeper[i++] = NULL;
e8016abf
SP
544 memset(&ip, 0, sizeof(ip));
545 ip.argv = keeper;
546 ip.out = -1;
547 ip.git_cmd = 1;
2ff4d1ab
JS
548 status = start_command(&ip);
549 if (status) {
576162a4 550 return "index-pack fork failed";
2ff4d1ab 551 }
106764e6 552 pack_lockfile = index_pack_lockfile(ip.out);
e72ae288 553 close(ip.out);
e8016abf
SP
554 status = finish_command(&ip);
555 if (!status) {
576162a4
NP
556 reprepare_packed_git();
557 return NULL;
558 }
559 return "index-pack abnormal exit";
cfee10a7
JH
560 }
561}
562
563static void report(const char *unpack_status)
564{
565 struct command *cmd;
566 packet_write(1, "unpack %s\n",
567 unpack_status ? unpack_status : "ok");
568 for (cmd = commands; cmd; cmd = cmd->next) {
569 if (!cmd->error_string)
570 packet_write(1, "ok %s\n",
571 cmd->ref_name);
572 else
573 packet_write(1, "ng %s %s\n",
574 cmd->ref_name, cmd->error_string);
575f4974 575 }
cfee10a7 576 packet_flush(1);
575f4974
LT
577}
578
d4f694ba
JH
579static int delete_only(struct command *cmd)
580{
581 while (cmd) {
582 if (!is_null_sha1(cmd->new_sha1))
583 return 0;
584 cmd = cmd->next;
585 }
586 return 1;
587}
588
d79796bc
JH
589static int add_refs_from_alternate(struct alternate_object_database *e, void *unused)
590{
b8492539
JH
591 char *other;
592 size_t len;
d79796bc
JH
593 struct remote *remote;
594 struct transport *transport;
595 const struct ref *extra;
596
b8492539
JH
597 e->name[-1] = '\0';
598 other = xstrdup(make_absolute_path(e->base));
599 e->name[-1] = '/';
600 len = strlen(other);
601
d79796bc
JH
602 while (other[len-1] == '/')
603 other[--len] = '\0';
604 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
605 return 0;
606 /* Is this a git repository with refs? */
607 memcpy(other + len - 8, "/refs", 6);
608 if (!is_directory(other))
609 return 0;
610 other[len - 8] = '\0';
611 remote = remote_get(other);
612 transport = transport_get(remote, other);
613 for (extra = transport_get_remote_refs(transport);
614 extra;
615 extra = extra->next) {
616 add_extra_ref(".have", extra->old_sha1, 0);
617 }
618 transport_disconnect(transport);
619 free(other);
620 return 0;
621}
622
623static void add_alternate_refs(void)
624{
625 foreach_alt_odb(add_refs_from_alternate, NULL);
626}
627
be5908ae 628int cmd_receive_pack(int argc, const char **argv, const char *prefix)
575f4974 629{
42526b47
SP
630 int advertise_refs = 0;
631 int stateless_rpc = 0;
d0efc8a7 632 int i;
8d630132 633 char *dir = NULL;
575f4974
LT
634
635 argv++;
636 for (i = 1; i < argc; i++) {
be5908ae 637 const char *arg = *argv++;
575f4974
LT
638
639 if (*arg == '-') {
42526b47
SP
640 if (!strcmp(arg, "--advertise-refs")) {
641 advertise_refs = 1;
642 continue;
643 }
644 if (!strcmp(arg, "--stateless-rpc")) {
645 stateless_rpc = 1;
646 continue;
647 }
648
575f4974
LT
649 usage(receive_pack_usage);
650 }
d0efc8a7
LT
651 if (dir)
652 usage(receive_pack_usage);
be5908ae 653 dir = xstrdup(arg);
575f4974
LT
654 }
655 if (!dir)
656 usage(receive_pack_usage);
657
e1464ca7 658 setup_path();
5c09f321 659
3159c8dc 660 if (!enter_repo(dir, 0))
05ac6b34 661 die("'%s' does not appear to be a git repository", dir);
575f4974 662
a0022eeb
JH
663 if (is_repository_shallow())
664 die("attempt to push into a shallow repository");
665
ef90d6d4 666 git_config(receive_pack_config, NULL);
6fb75bed 667
e28714c5
JH
668 if (0 <= transfer_unpack_limit)
669 unpack_limit = transfer_unpack_limit;
670 else if (0 <= receive_unpack_limit)
671 unpack_limit = receive_unpack_limit;
672
b74fce16
NP
673 capabilities_to_send = (prefer_ofs_delta) ?
674 " report-status delete-refs ofs-delta " :
675 " report-status delete-refs ";
676
42526b47
SP
677 if (advertise_refs || !stateless_rpc) {
678 add_alternate_refs();
679 write_head_info();
680 clear_extra_refs();
575f4974 681
42526b47
SP
682 /* EOF */
683 packet_flush(1);
684 }
685 if (advertise_refs)
686 return 0;
575f4974
LT
687
688 read_head_info();
7f8e9828 689 if (commands) {
d4f694ba
JH
690 const char *unpack_status = NULL;
691
692 if (!delete_only(commands))
693 unpack_status = unpack();
8aaf7d64 694 execute_commands(unpack_status);
576162a4 695 if (pack_lockfile)
691f1a28 696 unlink_or_warn(pack_lockfile);
cfee10a7
JH
697 if (report_status)
698 report(unpack_status);
ae98a008 699 run_receive_hook(post_receive_hook);
8e663d9e 700 run_update_post_hook(commands);
77e3efbf
JH
701 if (auto_gc) {
702 const char *argv_gc_auto[] = {
703 "gc", "--auto", "--quiet", NULL,
704 };
705 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
706 }
707 if (auto_update_server_info)
708 update_server_info(0);
7f8e9828 709 }
575f4974
LT
710 return 0;
711}