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