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