]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-receive-pack.c
Allow alternate "low-level" emit function from xdl_diff
[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
d0efc8a7 12static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
575f4974 13
6fb75bed 14static int deny_non_fast_forwards = 0;
20dc0016 15static int receive_fsck_objects;
e28714c5
JH
16static int receive_unpack_limit = -1;
17static int transfer_unpack_limit = -1;
46732fae 18static int unpack_limit = 100;
96f1e58f 19static int report_status;
cfee10a7 20
d4f694ba 21static char capabilities[] = " report-status delete-refs ";
96f1e58f 22static int capabilities_sent;
cfee10a7 23
ef90d6d4 24static int receive_pack_config(const char *var, const char *value, void *cb)
6fb75bed 25{
e28714c5 26 if (strcmp(var, "receive.denynonfastforwards") == 0) {
6fb75bed
SP
27 deny_non_fast_forwards = git_config_bool(var, value);
28 return 0;
29 }
30
e28714c5
JH
31 if (strcmp(var, "receive.unpacklimit") == 0) {
32 receive_unpack_limit = git_config_int(var, value);
fc04c412
SP
33 return 0;
34 }
35
e28714c5
JH
36 if (strcmp(var, "transfer.unpacklimit") == 0) {
37 transfer_unpack_limit = git_config_int(var, value);
38 return 0;
39 }
40
20dc0016
MK
41 if (strcmp(var, "receive.fsckobjects") == 0) {
42 receive_fsck_objects = git_config_bool(var, value);
43 return 0;
44 }
45
ef90d6d4 46 return git_default_config(var, value, cb);
6fb75bed
SP
47}
48
8da19775 49static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
575f4974 50{
cfee10a7
JH
51 if (capabilities_sent)
52 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
53 else
54 packet_write(1, "%s %s%c%s\n",
55 sha1_to_hex(sha1), path, 0, capabilities);
56 capabilities_sent = 1;
8a65ff76 57 return 0;
575f4974
LT
58}
59
8a65ff76 60static void write_head_info(void)
575f4974 61{
cb5d709f 62 for_each_ref(show_ref, NULL);
cfee10a7 63 if (!capabilities_sent)
8da19775 64 show_ref("capabilities^{}", null_sha1, 0, NULL);
cfee10a7 65
575f4974
LT
66}
67
eb1af2df
LT
68struct command {
69 struct command *next;
cfee10a7 70 const char *error_string;
eb1af2df
LT
71 unsigned char old_sha1[20];
72 unsigned char new_sha1[20];
8f1d2e6f 73 char ref_name[FLEX_ARRAY]; /* more */
575f4974
LT
74};
75
96f1e58f 76static struct command *commands;
575f4974 77
05ef58ec
SP
78static const char pre_receive_hook[] = "hooks/pre-receive";
79static const char post_receive_hook[] = "hooks/post-receive";
b1bf95bb 80
6c319a22
SP
81static int hook_status(int code, const char *hook_name)
82{
83 switch (code) {
84 case 0:
85 return 0;
86 case -ERR_RUN_COMMAND_FORK:
87 return error("hook fork failed");
88 case -ERR_RUN_COMMAND_EXEC:
89 return error("hook execute failed");
f43cd49f
SP
90 case -ERR_RUN_COMMAND_PIPE:
91 return error("hook pipe failed");
6c319a22
SP
92 case -ERR_RUN_COMMAND_WAITPID:
93 return error("waitpid failed");
94 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
95 return error("waitpid is confused");
96 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
97 return error("%s died of signal", hook_name);
98 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
99 return error("%s died strangely", hook_name);
100 default:
101 error("%s exited with error code %d", hook_name, -code);
102 return -code;
103 }
104}
105
f43cd49f 106static int run_hook(const char *hook_name)
b1bf95bb 107{
f43cd49f 108 static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
c8dd2771 109 struct command *cmd;
f43cd49f
SP
110 struct child_process proc;
111 const char *argv[2];
112 int have_input = 0, code;
c8dd2771 113
f43cd49f 114 for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
c8dd2771 115 if (!cmd->error_string)
f43cd49f 116 have_input = 1;
c8dd2771 117 }
b1bf95bb 118
f43cd49f 119 if (!have_input || access(hook_name, X_OK) < 0)
b1bf95bb 120 return 0;
c8dd2771 121
c8dd2771 122 argv[0] = hook_name;
f43cd49f
SP
123 argv[1] = NULL;
124
125 memset(&proc, 0, sizeof(proc));
126 proc.argv = argv;
127 proc.in = -1;
128 proc.stdout_to_stderr = 1;
129
130 code = start_command(&proc);
131 if (code)
132 return hook_status(code, hook_name);
133 for (cmd = commands; cmd; cmd = cmd->next) {
c8dd2771 134 if (!cmd->error_string) {
f43cd49f
SP
135 size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n",
136 sha1_to_hex(cmd->old_sha1),
137 sha1_to_hex(cmd->new_sha1),
138 cmd->ref_name);
139 if (write_in_full(proc.in, buf, n) != n)
140 break;
c8dd2771 141 }
c8dd2771 142 }
e72ae288 143 close(proc.in);
f43cd49f 144 return hook_status(finish_command(&proc), hook_name);
b1bf95bb
JW
145}
146
1d9e8b56
SP
147static int run_update_hook(struct command *cmd)
148{
149 static const char update_hook[] = "hooks/update";
150 struct child_process proc;
151 const char *argv[5];
152
153 if (access(update_hook, X_OK) < 0)
154 return 0;
155
156 argv[0] = update_hook;
157 argv[1] = cmd->ref_name;
158 argv[2] = sha1_to_hex(cmd->old_sha1);
159 argv[3] = sha1_to_hex(cmd->new_sha1);
160 argv[4] = NULL;
161
162 memset(&proc, 0, sizeof(proc));
163 proc.argv = argv;
164 proc.no_stdin = 1;
165 proc.stdout_to_stderr = 1;
166
167 return hook_status(run_command(&proc), update_hook);
168}
169
8aaf7d64 170static const char *update(struct command *cmd)
2eca23da 171{
cfee10a7
JH
172 const char *name = cmd->ref_name;
173 unsigned char *old_sha1 = cmd->old_sha1;
174 unsigned char *new_sha1 = cmd->new_sha1;
3159c8dc 175 struct ref_lock *lock;
2eca23da 176
061d6b9a
MK
177 /* only refs/... are allowed */
178 if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) {
0b8293f6 179 error("refusing to create funny ref '%s' remotely", name);
8aaf7d64 180 return "funny refname";
cfee10a7 181 }
d8a1deec 182
d4f694ba 183 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
8aaf7d64
SP
184 error("unpack should have generated %s, "
185 "but I can't find it!", sha1_to_hex(new_sha1));
186 return "bad pack";
cfee10a7 187 }
d4f694ba 188 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
ba988a83 189 !is_null_sha1(old_sha1) &&
cc44c765 190 !prefixcmp(name, "refs/heads/")) {
eab82707 191 struct object *old_object, *new_object;
11031d7e 192 struct commit *old_commit, *new_commit;
9edd7e46 193 struct commit_list *bases, *ent;
11031d7e 194
eab82707
MK
195 old_object = parse_object(old_sha1);
196 new_object = parse_object(new_sha1);
197
198 if (!old_object || !new_object ||
199 old_object->type != OBJ_COMMIT ||
200 new_object->type != OBJ_COMMIT) {
201 error("bad sha1 objects for %s", name);
202 return "bad ref";
203 }
204 old_commit = (struct commit *)old_object;
205 new_commit = (struct commit *)new_object;
9edd7e46
JS
206 bases = get_merge_bases(old_commit, new_commit, 1);
207 for (ent = bases; ent; ent = ent->next)
208 if (!hashcmp(old_sha1, ent->item->object.sha1))
11031d7e 209 break;
9edd7e46 210 free_commit_list(bases);
8aaf7d64
SP
211 if (!ent) {
212 error("denying non-fast forward %s"
213 " (you should pull first)", name);
214 return "non-fast forward";
215 }
11031d7e 216 }
1d9e8b56 217 if (run_update_hook(cmd)) {
8aaf7d64
SP
218 error("hook declined to update %s", name);
219 return "hook declined";
b1bf95bb 220 }
3159c8dc 221
d4f694ba 222 if (is_null_sha1(new_sha1)) {
28391a80
JS
223 if (!parse_object(old_sha1)) {
224 warning ("Allowing deletion of corrupt ref.");
225 old_sha1 = NULL;
226 }
d4f694ba 227 if (delete_ref(name, old_sha1)) {
8aaf7d64
SP
228 error("failed to delete %s", name);
229 return "failed to delete";
d4f694ba 230 }
8aaf7d64 231 return NULL; /* good */
d4f694ba
JH
232 }
233 else {
68db31cc 234 lock = lock_any_ref_for_update(name, old_sha1, 0);
d4f694ba 235 if (!lock) {
8aaf7d64
SP
236 error("failed to lock %s", name);
237 return "failed to lock";
d4f694ba 238 }
ef203f08 239 if (write_ref_sha1(lock, new_sha1, "push")) {
8aaf7d64 240 return "failed to write"; /* error() already called */
ef203f08 241 }
8aaf7d64 242 return NULL; /* good */
19614330 243 }
2eca23da
LT
244}
245
19614330
JH
246static char update_post_hook[] = "hooks/post-update";
247
248static void run_update_post_hook(struct command *cmd)
249{
250 struct command *cmd_p;
251 int argc;
9201c707 252 const char **argv;
19614330 253
3e6e152c 254 for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
cfee10a7 255 if (cmd_p->error_string)
19614330
JH
256 continue;
257 argc++;
258 }
3e6e152c
SP
259 if (!argc || access(update_post_hook, X_OK) < 0)
260 return;
261 argv = xmalloc(sizeof(*argv) * (2 + argc));
19614330
JH
262 argv[0] = update_post_hook;
263
264 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
9201c707 265 char *p;
cfee10a7 266 if (cmd_p->error_string)
19614330 267 continue;
9201c707
JH
268 p = xmalloc(strlen(cmd_p->ref_name) + 1);
269 strcpy(p, cmd_p->ref_name);
270 argv[argc] = p;
19614330
JH
271 argc++;
272 }
273 argv[argc] = NULL;
95d3c4f5
SP
274 run_command_v_opt(argv, RUN_COMMAND_NO_STDIN
275 | RUN_COMMAND_STDOUT_TO_STDERR);
19614330 276}
2eca23da 277
8aaf7d64 278static void execute_commands(const char *unpacker_error)
575f4974 279{
eb1af2df 280 struct command *cmd = commands;
8aaf7d64
SP
281
282 if (unpacker_error) {
283 while (cmd) {
284 cmd->error_string = "n/a (unpacker error)";
285 cmd = cmd->next;
286 }
287 return;
288 }
289
f43cd49f 290 if (run_hook(pre_receive_hook)) {
05ef58ec
SP
291 while (cmd) {
292 cmd->error_string = "pre-receive hook declined";
293 cmd = cmd->next;
294 }
295 return;
296 }
297
eb1af2df 298 while (cmd) {
8aaf7d64 299 cmd->error_string = update(cmd);
eb1af2df 300 cmd = cmd->next;
575f4974
LT
301 }
302}
303
304static void read_head_info(void)
305{
eb1af2df 306 struct command **p = &commands;
575f4974
LT
307 for (;;) {
308 static char line[1000];
eb1af2df
LT
309 unsigned char old_sha1[20], new_sha1[20];
310 struct command *cmd;
cfee10a7
JH
311 char *refname;
312 int len, reflen;
eb1af2df
LT
313
314 len = packet_read_line(0, line, sizeof(line));
575f4974
LT
315 if (!len)
316 break;
eb1af2df
LT
317 if (line[len-1] == '\n')
318 line[--len] = 0;
319 if (len < 83 ||
320 line[40] != ' ' ||
321 line[81] != ' ' ||
322 get_sha1_hex(line, old_sha1) ||
323 get_sha1_hex(line + 41, new_sha1))
cfee10a7
JH
324 die("protocol error: expected old/new/ref, got '%s'",
325 line);
326
327 refname = line + 82;
328 reflen = strlen(refname);
329 if (reflen + 82 < len) {
330 if (strstr(refname + reflen + 1, "report-status"))
331 report_status = 1;
332 }
eb1af2df 333 cmd = xmalloc(sizeof(struct command) + len - 80);
e702496e
SP
334 hashcpy(cmd->old_sha1, old_sha1);
335 hashcpy(cmd->new_sha1, new_sha1);
eb1af2df 336 memcpy(cmd->ref_name, line + 82, len - 81);
8aaf7d64 337 cmd->error_string = NULL;
eb1af2df
LT
338 cmd->next = NULL;
339 *p = cmd;
340 p = &cmd->next;
575f4974
LT
341 }
342}
343
fc04c412
SP
344static const char *parse_pack_header(struct pack_header *hdr)
345{
a69e5429
JH
346 switch (read_pack_header(0, hdr)) {
347 case PH_ERROR_EOF:
348 return "eof before pack header was fully read";
349
350 case PH_ERROR_PACK_SIGNATURE:
fc04c412 351 return "protocol error (pack signature mismatch detected)";
a69e5429
JH
352
353 case PH_ERROR_PROTOCOL:
fc04c412 354 return "protocol error (pack version unsupported)";
a69e5429
JH
355
356 default:
357 return "unknown error in parse_pack_header";
358
359 case 0:
360 return NULL;
361 }
fc04c412
SP
362}
363
576162a4
NP
364static const char *pack_lockfile;
365
861ed121 366static const char *unpack(void)
575f4974 367{
fc04c412
SP
368 struct pack_header hdr;
369 const char *hdr_err;
370 char hdr_arg[38];
fc04c412
SP
371
372 hdr_err = parse_pack_header(&hdr);
373 if (hdr_err)
374 return hdr_err;
6e1c2344
RJ
375 snprintf(hdr_arg, sizeof(hdr_arg),
376 "--pack_header=%"PRIu32",%"PRIu32,
fc04c412
SP
377 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
378
379 if (ntohl(hdr.hdr_entries) < unpack_limit) {
20dc0016
MK
380 int code, i = 0;
381 const char *unpacker[4];
382 unpacker[i++] = "unpack-objects";
383 if (receive_fsck_objects)
384 unpacker[i++] = "--strict";
385 unpacker[i++] = hdr_arg;
386 unpacker[i++] = NULL;
9b0b5093 387 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
576162a4 388 switch (code) {
fc04c412
SP
389 case 0:
390 return NULL;
391 case -ERR_RUN_COMMAND_FORK:
392 return "unpack fork failed";
393 case -ERR_RUN_COMMAND_EXEC:
394 return "unpack execute failed";
395 case -ERR_RUN_COMMAND_WAITPID:
396 return "waitpid failed";
397 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
398 return "waitpid is confused";
399 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
400 return "unpacker died of signal";
401 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
402 return "unpacker died strangely";
403 default:
404 return "unpacker exited with error code";
576162a4
NP
405 }
406 } else {
20dc0016
MK
407 const char *keeper[7];
408 int s, status, i = 0;
576162a4 409 char keep_arg[256];
e8016abf 410 struct child_process ip;
576162a4 411
85e72830 412 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
576162a4
NP
413 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
414 strcpy(keep_arg + s, "localhost");
415
20dc0016
MK
416 keeper[i++] = "index-pack";
417 keeper[i++] = "--stdin";
418 if (receive_fsck_objects)
419 keeper[i++] = "--strict";
420 keeper[i++] = "--fix-thin";
421 keeper[i++] = hdr_arg;
422 keeper[i++] = keep_arg;
423 keeper[i++] = NULL;
e8016abf
SP
424 memset(&ip, 0, sizeof(ip));
425 ip.argv = keeper;
426 ip.out = -1;
427 ip.git_cmd = 1;
428 if (start_command(&ip))
576162a4 429 return "index-pack fork failed";
106764e6 430 pack_lockfile = index_pack_lockfile(ip.out);
e72ae288 431 close(ip.out);
e8016abf
SP
432 status = finish_command(&ip);
433 if (!status) {
576162a4
NP
434 reprepare_packed_git();
435 return NULL;
436 }
437 return "index-pack abnormal exit";
cfee10a7
JH
438 }
439}
440
441static void report(const char *unpack_status)
442{
443 struct command *cmd;
444 packet_write(1, "unpack %s\n",
445 unpack_status ? unpack_status : "ok");
446 for (cmd = commands; cmd; cmd = cmd->next) {
447 if (!cmd->error_string)
448 packet_write(1, "ok %s\n",
449 cmd->ref_name);
450 else
451 packet_write(1, "ng %s %s\n",
452 cmd->ref_name, cmd->error_string);
575f4974 453 }
cfee10a7 454 packet_flush(1);
575f4974
LT
455}
456
d4f694ba
JH
457static int delete_only(struct command *cmd)
458{
459 while (cmd) {
460 if (!is_null_sha1(cmd->new_sha1))
461 return 0;
462 cmd = cmd->next;
463 }
464 return 1;
465}
466
d79796bc
JH
467static int add_refs_from_alternate(struct alternate_object_database *e, void *unused)
468{
469 char *other = xstrdup(make_absolute_path(e->base));
470 size_t len = strlen(other);
471 struct remote *remote;
472 struct transport *transport;
473 const struct ref *extra;
474
475 while (other[len-1] == '/')
476 other[--len] = '\0';
477 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
478 return 0;
479 /* Is this a git repository with refs? */
480 memcpy(other + len - 8, "/refs", 6);
481 if (!is_directory(other))
482 return 0;
483 other[len - 8] = '\0';
484 remote = remote_get(other);
485 transport = transport_get(remote, other);
486 for (extra = transport_get_remote_refs(transport);
487 extra;
488 extra = extra->next) {
489 add_extra_ref(".have", extra->old_sha1, 0);
490 }
491 transport_disconnect(transport);
492 free(other);
493 return 0;
494}
495
496static void add_alternate_refs(void)
497{
498 foreach_alt_odb(add_refs_from_alternate, NULL);
499}
500
be5908ae 501int cmd_receive_pack(int argc, const char **argv, const char *prefix)
575f4974 502{
d0efc8a7 503 int i;
8d630132 504 char *dir = NULL;
575f4974
LT
505
506 argv++;
507 for (i = 1; i < argc; i++) {
be5908ae 508 const char *arg = *argv++;
575f4974
LT
509
510 if (*arg == '-') {
575f4974
LT
511 /* Do flag handling here */
512 usage(receive_pack_usage);
513 }
d0efc8a7
LT
514 if (dir)
515 usage(receive_pack_usage);
be5908ae 516 dir = xstrdup(arg);
575f4974
LT
517 }
518 if (!dir)
519 usage(receive_pack_usage);
520
e1464ca7 521 setup_path();
5c09f321 522
3159c8dc 523 if (!enter_repo(dir, 0))
8d630132 524 die("'%s': unable to chdir or not a git archive", dir);
575f4974 525
a0022eeb
JH
526 if (is_repository_shallow())
527 die("attempt to push into a shallow repository");
528
ef90d6d4 529 git_config(receive_pack_config, NULL);
6fb75bed 530
e28714c5
JH
531 if (0 <= transfer_unpack_limit)
532 unpack_limit = transfer_unpack_limit;
533 else if (0 <= receive_unpack_limit)
534 unpack_limit = receive_unpack_limit;
535
d79796bc 536 add_alternate_refs();
8a65ff76 537 write_head_info();
d79796bc 538 clear_extra_refs();
575f4974
LT
539
540 /* EOF */
f3a3214e 541 packet_flush(1);
575f4974
LT
542
543 read_head_info();
7f8e9828 544 if (commands) {
d4f694ba
JH
545 const char *unpack_status = NULL;
546
547 if (!delete_only(commands))
548 unpack_status = unpack();
8aaf7d64 549 execute_commands(unpack_status);
576162a4
NP
550 if (pack_lockfile)
551 unlink(pack_lockfile);
cfee10a7
JH
552 if (report_status)
553 report(unpack_status);
f43cd49f 554 run_hook(post_receive_hook);
8e663d9e 555 run_update_post_hook(commands);
7f8e9828 556 }
575f4974
LT
557 return 0;
558}