Code

Tighten refspec processing
[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"
10 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
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;
18 static char capabilities[] = " report-status delete-refs ";
19 static int capabilities_sent;
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         }
28         if (strcmp(var, "receive.unpacklimit") == 0) {
29                 receive_unpack_limit = git_config_int(var, value);
30                 return 0;
31         }
33         if (strcmp(var, "transfer.unpacklimit") == 0) {
34                 transfer_unpack_limit = git_config_int(var, value);
35                 return 0;
36         }
38         return git_default_config(var, value);
39 }
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 }
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);
58 }
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 };
68 static struct command *commands;
70 static const char pre_receive_hook[] = "hooks/pre-receive";
71 static const char post_receive_hook[] = "hooks/post-receive";
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 }
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;
106         for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
107                 if (!cmd->error_string)
108                         have_input = 1;
109         }
111         if (!have_input || access(hook_name, X_OK) < 0)
112                 return 0;
114         argv[0] = hook_name;
115         argv[1] = NULL;
117         memset(&proc, 0, sizeof(proc));
118         proc.argv = argv;
119         proc.in = -1;
120         proc.stdout_to_stderr = 1;
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);
138 static int run_update_hook(struct command *cmd)
140         static const char update_hook[] = "hooks/update";
141         struct child_process proc;
142         const char *argv[5];
144         if (access(update_hook, X_OK) < 0)
145                 return 0;
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;
153         memset(&proc, 0, sizeof(proc));
154         proc.argv = argv;
155         proc.no_stdin = 1;
156         proc.stdout_to_stderr = 1;
158         return hook_status(run_command(&proc), update_hook);
161 static const char *update(struct command *cmd)
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;
168         /* only refs/... are allowed */
169         if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) {
170                 error("refusing to create funny ref '%s' remotely", name);
171                 return "funny refname";
172         }
174         if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
175                 error("unpack should have generated %s, "
176                       "but I can't find it!", sha1_to_hex(new_sha1));
177                 return "bad pack";
178         }
179         if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
180             !is_null_sha1(old_sha1) &&
181             !prefixcmp(name, "refs/heads/")) {
182                 struct object *old_object, *new_object;
183                 struct commit *old_commit, *new_commit;
184                 struct commit_list *bases, *ent;
186                 old_object = parse_object(old_sha1);
187                 new_object = parse_object(new_sha1);
189                 if (!old_object || !new_object ||
190                     old_object->type != OBJ_COMMIT ||
191                     new_object->type != OBJ_COMMIT) {
192                         error("bad sha1 objects for %s", name);
193                         return "bad ref";
194                 }
195                 old_commit = (struct commit *)old_object;
196                 new_commit = (struct commit *)new_object;
197                 bases = get_merge_bases(old_commit, new_commit, 1);
198                 for (ent = bases; ent; ent = ent->next)
199                         if (!hashcmp(old_sha1, ent->item->object.sha1))
200                                 break;
201                 free_commit_list(bases);
202                 if (!ent) {
203                         error("denying non-fast forward %s"
204                               " (you should pull first)", name);
205                         return "non-fast forward";
206                 }
207         }
208         if (run_update_hook(cmd)) {
209                 error("hook declined to update %s", name);
210                 return "hook declined";
211         }
213         if (is_null_sha1(new_sha1)) {
214                 if (!parse_object(old_sha1)) {
215                         warning ("Allowing deletion of corrupt ref.");
216                         old_sha1 = NULL;
217                 }
218                 if (delete_ref(name, old_sha1)) {
219                         error("failed to delete %s", name);
220                         return "failed to delete";
221                 }
222                 return NULL; /* good */
223         }
224         else {
225                 lock = lock_any_ref_for_update(name, old_sha1, 0);
226                 if (!lock) {
227                         error("failed to lock %s", name);
228                         return "failed to lock";
229                 }
230                 if (write_ref_sha1(lock, new_sha1, "push")) {
231                         return "failed to write"; /* error() already called */
232                 }
233                 return NULL; /* good */
234         }
237 static char update_post_hook[] = "hooks/post-update";
239 static void run_update_post_hook(struct command *cmd)
241         struct command *cmd_p;
242         int argc;
243         const char **argv;
245         for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
246                 if (cmd_p->error_string)
247                         continue;
248                 argc++;
249         }
250         if (!argc || access(update_post_hook, X_OK) < 0)
251                 return;
252         argv = xmalloc(sizeof(*argv) * (2 + argc));
253         argv[0] = update_post_hook;
255         for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
256                 char *p;
257                 if (cmd_p->error_string)
258                         continue;
259                 p = xmalloc(strlen(cmd_p->ref_name) + 1);
260                 strcpy(p, cmd_p->ref_name);
261                 argv[argc] = p;
262                 argc++;
263         }
264         argv[argc] = NULL;
265         run_command_v_opt(argv, RUN_COMMAND_NO_STDIN
266                 | RUN_COMMAND_STDOUT_TO_STDERR);
269 static void execute_commands(const char *unpacker_error)
271         struct command *cmd = commands;
273         if (unpacker_error) {
274                 while (cmd) {
275                         cmd->error_string = "n/a (unpacker error)";
276                         cmd = cmd->next;
277                 }
278                 return;
279         }
281         if (run_hook(pre_receive_hook)) {
282                 while (cmd) {
283                         cmd->error_string = "pre-receive hook declined";
284                         cmd = cmd->next;
285                 }
286                 return;
287         }
289         while (cmd) {
290                 cmd->error_string = update(cmd);
291                 cmd = cmd->next;
292         }
295 static void read_head_info(void)
297         struct command **p = &commands;
298         for (;;) {
299                 static char line[1000];
300                 unsigned char old_sha1[20], new_sha1[20];
301                 struct command *cmd;
302                 char *refname;
303                 int len, reflen;
305                 len = packet_read_line(0, line, sizeof(line));
306                 if (!len)
307                         break;
308                 if (line[len-1] == '\n')
309                         line[--len] = 0;
310                 if (len < 83 ||
311                     line[40] != ' ' ||
312                     line[81] != ' ' ||
313                     get_sha1_hex(line, old_sha1) ||
314                     get_sha1_hex(line + 41, new_sha1))
315                         die("protocol error: expected old/new/ref, got '%s'",
316                             line);
318                 refname = line + 82;
319                 reflen = strlen(refname);
320                 if (reflen + 82 < len) {
321                         if (strstr(refname + reflen + 1, "report-status"))
322                                 report_status = 1;
323                 }
324                 cmd = xmalloc(sizeof(struct command) + len - 80);
325                 hashcpy(cmd->old_sha1, old_sha1);
326                 hashcpy(cmd->new_sha1, new_sha1);
327                 memcpy(cmd->ref_name, line + 82, len - 81);
328                 cmd->error_string = NULL;
329                 cmd->next = NULL;
330                 *p = cmd;
331                 p = &cmd->next;
332         }
335 static const char *parse_pack_header(struct pack_header *hdr)
337         switch (read_pack_header(0, hdr)) {
338         case PH_ERROR_EOF:
339                 return "eof before pack header was fully read";
341         case PH_ERROR_PACK_SIGNATURE:
342                 return "protocol error (pack signature mismatch detected)";
344         case PH_ERROR_PROTOCOL:
345                 return "protocol error (pack version unsupported)";
347         default:
348                 return "unknown error in parse_pack_header";
350         case 0:
351                 return NULL;
352         }
355 static const char *pack_lockfile;
357 static const char *unpack(void)
359         struct pack_header hdr;
360         const char *hdr_err;
361         char hdr_arg[38];
363         hdr_err = parse_pack_header(&hdr);
364         if (hdr_err)
365                 return hdr_err;
366         snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%u,%u",
367                         ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
369         if (ntohl(hdr.hdr_entries) < unpack_limit) {
370                 int code;
371                 const char *unpacker[3];
372                 unpacker[0] = "unpack-objects";
373                 unpacker[1] = hdr_arg;
374                 unpacker[2] = NULL;
375                 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
376                 switch (code) {
377                 case 0:
378                         return NULL;
379                 case -ERR_RUN_COMMAND_FORK:
380                         return "unpack fork failed";
381                 case -ERR_RUN_COMMAND_EXEC:
382                         return "unpack execute failed";
383                 case -ERR_RUN_COMMAND_WAITPID:
384                         return "waitpid failed";
385                 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
386                         return "waitpid is confused";
387                 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
388                         return "unpacker died of signal";
389                 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
390                         return "unpacker died strangely";
391                 default:
392                         return "unpacker exited with error code";
393                 }
394         } else {
395                 const char *keeper[6];
396                 int s, status;
397                 char keep_arg[256];
398                 struct child_process ip;
400                 s = sprintf(keep_arg, "--keep=receive-pack %i on ", getpid());
401                 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
402                         strcpy(keep_arg + s, "localhost");
404                 keeper[0] = "index-pack";
405                 keeper[1] = "--stdin";
406                 keeper[2] = "--fix-thin";
407                 keeper[3] = hdr_arg;
408                 keeper[4] = keep_arg;
409                 keeper[5] = NULL;
410                 memset(&ip, 0, sizeof(ip));
411                 ip.argv = keeper;
412                 ip.out = -1;
413                 ip.git_cmd = 1;
414                 if (start_command(&ip))
415                         return "index-pack fork failed";
416                 pack_lockfile = index_pack_lockfile(ip.out);
417                 status = finish_command(&ip);
418                 if (!status) {
419                         reprepare_packed_git();
420                         return NULL;
421                 }
422                 return "index-pack abnormal exit";
423         }
426 static void report(const char *unpack_status)
428         struct command *cmd;
429         packet_write(1, "unpack %s\n",
430                      unpack_status ? unpack_status : "ok");
431         for (cmd = commands; cmd; cmd = cmd->next) {
432                 if (!cmd->error_string)
433                         packet_write(1, "ok %s\n",
434                                      cmd->ref_name);
435                 else
436                         packet_write(1, "ng %s %s\n",
437                                      cmd->ref_name, cmd->error_string);
438         }
439         packet_flush(1);
442 static int delete_only(struct command *cmd)
444         while (cmd) {
445                 if (!is_null_sha1(cmd->new_sha1))
446                         return 0;
447                 cmd = cmd->next;
448         }
449         return 1;
452 int main(int argc, char **argv)
454         int i;
455         char *dir = NULL;
457         argv++;
458         for (i = 1; i < argc; i++) {
459                 char *arg = *argv++;
461                 if (*arg == '-') {
462                         /* Do flag handling here */
463                         usage(receive_pack_usage);
464                 }
465                 if (dir)
466                         usage(receive_pack_usage);
467                 dir = arg;
468         }
469         if (!dir)
470                 usage(receive_pack_usage);
472         setup_path(NULL);
474         if (!enter_repo(dir, 0))
475                 die("'%s': unable to chdir or not a git archive", dir);
477         if (is_repository_shallow())
478                 die("attempt to push into a shallow repository");
480         git_config(receive_pack_config);
482         if (0 <= transfer_unpack_limit)
483                 unpack_limit = transfer_unpack_limit;
484         else if (0 <= receive_unpack_limit)
485                 unpack_limit = receive_unpack_limit;
487         write_head_info();
489         /* EOF */
490         packet_flush(1);
492         read_head_info();
493         if (commands) {
494                 const char *unpack_status = NULL;
496                 if (!delete_only(commands))
497                         unpack_status = unpack();
498                 execute_commands(unpack_status);
499                 if (pack_lockfile)
500                         unlink(pack_lockfile);
501                 if (report_status)
502                         report(unpack_status);
503                 run_hook(post_receive_hook);
504                 run_update_post_hook(commands);
505         }
506         return 0;