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