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