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