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