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