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