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