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