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