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