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