]> asedeno.scripts.mit.edu Git - git.git/blob - receive-pack.c
receive-pack: check object type of sha1 before using them as commits
[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_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' remotely", 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 object *old_object, *new_object;
182                 struct commit *old_commit, *new_commit;
183                 struct commit_list *bases, *ent;
184
185                 old_object = parse_object(old_sha1);
186                 new_object = parse_object(new_sha1);
187
188                 if (!old_object || !new_object ||
189                     old_object->type != OBJ_COMMIT ||
190                     new_object->type != OBJ_COMMIT) {
191                         error("bad sha1 objects for %s", name);
192                         return "bad ref";
193                 }
194                 old_commit = (struct commit *)old_object;
195                 new_commit = (struct commit *)new_object;
196                 bases = get_merge_bases(old_commit, new_commit, 1);
197                 for (ent = bases; ent; ent = ent->next)
198                         if (!hashcmp(old_sha1, ent->item->object.sha1))
199                                 break;
200                 free_commit_list(bases);
201                 if (!ent) {
202                         error("denying non-fast forward %s"
203                               " (you should pull first)", name);
204                         return "non-fast forward";
205                 }
206         }
207         if (run_update_hook(cmd)) {
208                 error("hook declined to update %s", name);
209                 return "hook declined";
210         }
211
212         if (is_null_sha1(new_sha1)) {
213                 if (!parse_object(old_sha1)) {
214                         warning ("Allowing deletion of corrupt ref.");
215                         old_sha1 = NULL;
216                 }
217                 if (delete_ref(name, old_sha1)) {
218                         error("failed to delete %s", name);
219                         return "failed to delete";
220                 }
221                 return NULL; /* good */
222         }
223         else {
224                 lock = lock_any_ref_for_update(name, old_sha1, 0);
225                 if (!lock) {
226                         error("failed to lock %s", name);
227                         return "failed to lock";
228                 }
229                 if (write_ref_sha1(lock, new_sha1, "push")) {
230                         return "failed to write"; /* error() already called */
231                 }
232                 return NULL; /* good */
233         }
234 }
235
236 static char update_post_hook[] = "hooks/post-update";
237
238 static void run_update_post_hook(struct command *cmd)
239 {
240         struct command *cmd_p;
241         int argc;
242         const char **argv;
243
244         for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
245                 if (cmd_p->error_string)
246                         continue;
247                 argc++;
248         }
249         if (!argc || access(update_post_hook, X_OK) < 0)
250                 return;
251         argv = xmalloc(sizeof(*argv) * (2 + argc));
252         argv[0] = update_post_hook;
253
254         for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
255                 char *p;
256                 if (cmd_p->error_string)
257                         continue;
258                 p = xmalloc(strlen(cmd_p->ref_name) + 1);
259                 strcpy(p, cmd_p->ref_name);
260                 argv[argc] = p;
261                 argc++;
262         }
263         argv[argc] = NULL;
264         run_command_v_opt(argv, RUN_COMMAND_NO_STDIN
265                 | RUN_COMMAND_STDOUT_TO_STDERR);
266 }
267
268 static void execute_commands(const char *unpacker_error)
269 {
270         struct command *cmd = commands;
271
272         if (unpacker_error) {
273                 while (cmd) {
274                         cmd->error_string = "n/a (unpacker error)";
275                         cmd = cmd->next;
276                 }
277                 return;
278         }
279
280         if (run_hook(pre_receive_hook)) {
281                 while (cmd) {
282                         cmd->error_string = "pre-receive hook declined";
283                         cmd = cmd->next;
284                 }
285                 return;
286         }
287
288         while (cmd) {
289                 cmd->error_string = update(cmd);
290                 cmd = cmd->next;
291         }
292 }
293
294 static void read_head_info(void)
295 {
296         struct command **p = &commands;
297         for (;;) {
298                 static char line[1000];
299                 unsigned char old_sha1[20], new_sha1[20];
300                 struct command *cmd;
301                 char *refname;
302                 int len, reflen;
303
304                 len = packet_read_line(0, line, sizeof(line));
305                 if (!len)
306                         break;
307                 if (line[len-1] == '\n')
308                         line[--len] = 0;
309                 if (len < 83 ||
310                     line[40] != ' ' ||
311                     line[81] != ' ' ||
312                     get_sha1_hex(line, old_sha1) ||
313                     get_sha1_hex(line + 41, new_sha1))
314                         die("protocol error: expected old/new/ref, got '%s'",
315                             line);
316
317                 refname = line + 82;
318                 reflen = strlen(refname);
319                 if (reflen + 82 < len) {
320                         if (strstr(refname + reflen + 1, "report-status"))
321                                 report_status = 1;
322                 }
323                 cmd = xmalloc(sizeof(struct command) + len - 80);
324                 hashcpy(cmd->old_sha1, old_sha1);
325                 hashcpy(cmd->new_sha1, new_sha1);
326                 memcpy(cmd->ref_name, line + 82, len - 81);
327                 cmd->error_string = NULL;
328                 cmd->next = NULL;
329                 *p = cmd;
330                 p = &cmd->next;
331         }
332 }
333
334 static const char *parse_pack_header(struct pack_header *hdr)
335 {
336         switch (read_pack_header(0, hdr)) {
337         case PH_ERROR_EOF:
338                 return "eof before pack header was fully read";
339
340         case PH_ERROR_PACK_SIGNATURE:
341                 return "protocol error (pack signature mismatch detected)";
342
343         case PH_ERROR_PROTOCOL:
344                 return "protocol error (pack version unsupported)";
345
346         default:
347                 return "unknown error in parse_pack_header";
348
349         case 0:
350                 return NULL;
351         }
352 }
353
354 static const char *pack_lockfile;
355
356 static const char *unpack(void)
357 {
358         struct pack_header hdr;
359         const char *hdr_err;
360         char hdr_arg[38];
361
362         hdr_err = parse_pack_header(&hdr);
363         if (hdr_err)
364                 return hdr_err;
365         snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%u,%u",
366                         ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
367
368         if (ntohl(hdr.hdr_entries) < unpack_limit) {
369                 int code;
370                 const char *unpacker[3];
371                 unpacker[0] = "unpack-objects";
372                 unpacker[1] = hdr_arg;
373                 unpacker[2] = NULL;
374                 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
375                 switch (code) {
376                 case 0:
377                         return NULL;
378                 case -ERR_RUN_COMMAND_FORK:
379                         return "unpack fork failed";
380                 case -ERR_RUN_COMMAND_EXEC:
381                         return "unpack execute failed";
382                 case -ERR_RUN_COMMAND_WAITPID:
383                         return "waitpid failed";
384                 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
385                         return "waitpid is confused";
386                 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
387                         return "unpacker died of signal";
388                 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
389                         return "unpacker died strangely";
390                 default:
391                         return "unpacker exited with error code";
392                 }
393         } else {
394                 const char *keeper[6];
395                 int s, status;
396                 char keep_arg[256];
397                 struct child_process ip;
398
399                 s = sprintf(keep_arg, "--keep=receive-pack %i on ", getpid());
400                 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
401                         strcpy(keep_arg + s, "localhost");
402
403                 keeper[0] = "index-pack";
404                 keeper[1] = "--stdin";
405                 keeper[2] = "--fix-thin";
406                 keeper[3] = hdr_arg;
407                 keeper[4] = keep_arg;
408                 keeper[5] = NULL;
409                 memset(&ip, 0, sizeof(ip));
410                 ip.argv = keeper;
411                 ip.out = -1;
412                 ip.git_cmd = 1;
413                 if (start_command(&ip))
414                         return "index-pack fork failed";
415                 pack_lockfile = index_pack_lockfile(ip.out);
416                 status = finish_command(&ip);
417                 if (!status) {
418                         reprepare_packed_git();
419                         return NULL;
420                 }
421                 return "index-pack abnormal exit";
422         }
423 }
424
425 static void report(const char *unpack_status)
426 {
427         struct command *cmd;
428         packet_write(1, "unpack %s\n",
429                      unpack_status ? unpack_status : "ok");
430         for (cmd = commands; cmd; cmd = cmd->next) {
431                 if (!cmd->error_string)
432                         packet_write(1, "ok %s\n",
433                                      cmd->ref_name);
434                 else
435                         packet_write(1, "ng %s %s\n",
436                                      cmd->ref_name, cmd->error_string);
437         }
438         packet_flush(1);
439 }
440
441 static int delete_only(struct command *cmd)
442 {
443         while (cmd) {
444                 if (!is_null_sha1(cmd->new_sha1))
445                         return 0;
446                 cmd = cmd->next;
447         }
448         return 1;
449 }
450
451 int main(int argc, char **argv)
452 {
453         int i;
454         char *dir = NULL;
455
456         argv++;
457         for (i = 1; i < argc; i++) {
458                 char *arg = *argv++;
459
460                 if (*arg == '-') {
461                         /* Do flag handling here */
462                         usage(receive_pack_usage);
463                 }
464                 if (dir)
465                         usage(receive_pack_usage);
466                 dir = arg;
467         }
468         if (!dir)
469                 usage(receive_pack_usage);
470
471         if (!enter_repo(dir, 0))
472                 die("'%s': unable to chdir or not a git archive", dir);
473
474         if (is_repository_shallow())
475                 die("attempt to push into a shallow repository");
476
477         git_config(receive_pack_config);
478
479         if (0 <= transfer_unpack_limit)
480                 unpack_limit = transfer_unpack_limit;
481         else if (0 <= receive_unpack_limit)
482                 unpack_limit = receive_unpack_limit;
483
484         write_head_info();
485
486         /* EOF */
487         packet_flush(1);
488
489         read_head_info();
490         if (commands) {
491                 const char *unpack_status = NULL;
492
493                 if (!delete_only(commands))
494                         unpack_status = unpack();
495                 execute_commands(unpack_status);
496                 if (pack_lockfile)
497                         unlink(pack_lockfile);
498                 if (report_status)
499                         report(unpack_status);
500                 run_hook(post_receive_hook);
501                 run_update_post_hook(commands);
502         }
503         return 0;
504 }