5 #include "run-command.h"
10 #include "transport.h"
12 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
14 static int deny_deletes = 0;
15 static int deny_non_fast_forwards = 0;
16 static int receive_fsck_objects;
17 static int receive_unpack_limit = -1;
18 static int transfer_unpack_limit = -1;
19 static int unpack_limit = 100;
20 static int report_status;
22 static char capabilities[] = " report-status delete-refs ";
23 static int capabilities_sent;
25 static int receive_pack_config(const char *var, const char *value, void *cb)
27 if (strcmp(var, "receive.denydeletes") == 0) {
28 deny_deletes = git_config_bool(var, value);
32 if (strcmp(var, "receive.denynonfastforwards") == 0) {
33 deny_non_fast_forwards = git_config_bool(var, value);
37 if (strcmp(var, "receive.unpacklimit") == 0) {
38 receive_unpack_limit = git_config_int(var, value);
42 if (strcmp(var, "transfer.unpacklimit") == 0) {
43 transfer_unpack_limit = git_config_int(var, value);
47 if (strcmp(var, "receive.fsckobjects") == 0) {
48 receive_fsck_objects = git_config_bool(var, value);
52 return git_default_config(var, value, cb);
55 static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
57 if (capabilities_sent)
58 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
60 packet_write(1, "%s %s%c%s\n",
61 sha1_to_hex(sha1), path, 0, capabilities);
62 capabilities_sent = 1;
66 static void write_head_info(void)
68 for_each_ref(show_ref, NULL);
69 if (!capabilities_sent)
70 show_ref("capabilities^{}", null_sha1, 0, NULL);
76 const char *error_string;
77 unsigned char old_sha1[20];
78 unsigned char new_sha1[20];
79 char ref_name[FLEX_ARRAY]; /* more */
82 static struct command *commands;
84 static const char pre_receive_hook[] = "hooks/pre-receive";
85 static const char post_receive_hook[] = "hooks/post-receive";
87 static int hook_status(int code, const char *hook_name)
92 case -ERR_RUN_COMMAND_FORK:
93 return error("hook fork failed");
94 case -ERR_RUN_COMMAND_EXEC:
95 return error("hook execute failed");
96 case -ERR_RUN_COMMAND_PIPE:
97 return error("hook pipe failed");
98 case -ERR_RUN_COMMAND_WAITPID:
99 return error("waitpid failed");
100 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
101 return error("waitpid is confused");
102 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
103 return error("%s died of signal", hook_name);
104 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
105 return error("%s died strangely", hook_name);
107 error("%s exited with error code %d", hook_name, -code);
112 static int run_hook(const char *hook_name)
114 static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
116 struct child_process proc;
118 int have_input = 0, code;
120 for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
121 if (!cmd->error_string)
125 if (!have_input || access(hook_name, X_OK) < 0)
131 memset(&proc, 0, sizeof(proc));
134 proc.stdout_to_stderr = 1;
136 code = start_command(&proc);
138 return hook_status(code, hook_name);
139 for (cmd = commands; cmd; cmd = cmd->next) {
140 if (!cmd->error_string) {
141 size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n",
142 sha1_to_hex(cmd->old_sha1),
143 sha1_to_hex(cmd->new_sha1),
145 if (write_in_full(proc.in, buf, n) != n)
150 return hook_status(finish_command(&proc), hook_name);
153 static int run_update_hook(struct command *cmd)
155 static const char update_hook[] = "hooks/update";
156 struct child_process proc;
159 if (access(update_hook, X_OK) < 0)
162 argv[0] = update_hook;
163 argv[1] = cmd->ref_name;
164 argv[2] = sha1_to_hex(cmd->old_sha1);
165 argv[3] = sha1_to_hex(cmd->new_sha1);
168 memset(&proc, 0, sizeof(proc));
171 proc.stdout_to_stderr = 1;
173 return hook_status(run_command(&proc), update_hook);
176 static const char *update(struct command *cmd)
178 const char *name = cmd->ref_name;
179 unsigned char *old_sha1 = cmd->old_sha1;
180 unsigned char *new_sha1 = cmd->new_sha1;
181 struct ref_lock *lock;
183 /* only refs/... are allowed */
184 if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) {
185 error("refusing to create funny ref '%s' remotely", name);
186 return "funny refname";
189 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
190 error("unpack should have generated %s, "
191 "but I can't find it!", sha1_to_hex(new_sha1));
194 if (deny_deletes && is_null_sha1(new_sha1) &&
195 !is_null_sha1(old_sha1) &&
196 !prefixcmp(name, "refs/heads/")) {
197 error("denying ref deletion for %s", name);
198 return "deletion prohibited";
200 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
201 !is_null_sha1(old_sha1) &&
202 !prefixcmp(name, "refs/heads/")) {
203 struct object *old_object, *new_object;
204 struct commit *old_commit, *new_commit;
205 struct commit_list *bases, *ent;
207 old_object = parse_object(old_sha1);
208 new_object = parse_object(new_sha1);
210 if (!old_object || !new_object ||
211 old_object->type != OBJ_COMMIT ||
212 new_object->type != OBJ_COMMIT) {
213 error("bad sha1 objects for %s", name);
216 old_commit = (struct commit *)old_object;
217 new_commit = (struct commit *)new_object;
218 bases = get_merge_bases(old_commit, new_commit, 1);
219 for (ent = bases; ent; ent = ent->next)
220 if (!hashcmp(old_sha1, ent->item->object.sha1))
222 free_commit_list(bases);
224 error("denying non-fast forward %s"
225 " (you should pull first)", name);
226 return "non-fast forward";
229 if (run_update_hook(cmd)) {
230 error("hook declined to update %s", name);
231 return "hook declined";
234 if (is_null_sha1(new_sha1)) {
235 if (!parse_object(old_sha1)) {
236 warning ("Allowing deletion of corrupt ref.");
239 if (delete_ref(name, old_sha1, 0)) {
240 error("failed to delete %s", name);
241 return "failed to delete";
243 return NULL; /* good */
246 lock = lock_any_ref_for_update(name, old_sha1, 0);
248 error("failed to lock %s", name);
249 return "failed to lock";
251 if (write_ref_sha1(lock, new_sha1, "push")) {
252 return "failed to write"; /* error() already called */
254 return NULL; /* good */
258 static char update_post_hook[] = "hooks/post-update";
260 static void run_update_post_hook(struct command *cmd)
262 struct command *cmd_p;
266 for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
267 if (cmd_p->error_string)
271 if (!argc || access(update_post_hook, X_OK) < 0)
273 argv = xmalloc(sizeof(*argv) * (2 + argc));
274 argv[0] = update_post_hook;
276 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
278 if (cmd_p->error_string)
280 p = xmalloc(strlen(cmd_p->ref_name) + 1);
281 strcpy(p, cmd_p->ref_name);
286 run_command_v_opt(argv, RUN_COMMAND_NO_STDIN
287 | RUN_COMMAND_STDOUT_TO_STDERR);
290 static void execute_commands(const char *unpacker_error)
292 struct command *cmd = commands;
294 if (unpacker_error) {
296 cmd->error_string = "n/a (unpacker error)";
302 if (run_hook(pre_receive_hook)) {
304 cmd->error_string = "pre-receive hook declined";
311 cmd->error_string = update(cmd);
316 static void read_head_info(void)
318 struct command **p = &commands;
320 static char line[1000];
321 unsigned char old_sha1[20], new_sha1[20];
326 len = packet_read_line(0, line, sizeof(line));
329 if (line[len-1] == '\n')
334 get_sha1_hex(line, old_sha1) ||
335 get_sha1_hex(line + 41, new_sha1))
336 die("protocol error: expected old/new/ref, got '%s'",
340 reflen = strlen(refname);
341 if (reflen + 82 < len) {
342 if (strstr(refname + reflen + 1, "report-status"))
345 cmd = xmalloc(sizeof(struct command) + len - 80);
346 hashcpy(cmd->old_sha1, old_sha1);
347 hashcpy(cmd->new_sha1, new_sha1);
348 memcpy(cmd->ref_name, line + 82, len - 81);
349 cmd->error_string = NULL;
356 static const char *parse_pack_header(struct pack_header *hdr)
358 switch (read_pack_header(0, hdr)) {
360 return "eof before pack header was fully read";
362 case PH_ERROR_PACK_SIGNATURE:
363 return "protocol error (pack signature mismatch detected)";
365 case PH_ERROR_PROTOCOL:
366 return "protocol error (pack version unsupported)";
369 return "unknown error in parse_pack_header";
376 static const char *pack_lockfile;
378 static const char *unpack(void)
380 struct pack_header hdr;
384 hdr_err = parse_pack_header(&hdr);
387 snprintf(hdr_arg, sizeof(hdr_arg),
388 "--pack_header=%"PRIu32",%"PRIu32,
389 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
391 if (ntohl(hdr.hdr_entries) < unpack_limit) {
393 const char *unpacker[4];
394 unpacker[i++] = "unpack-objects";
395 if (receive_fsck_objects)
396 unpacker[i++] = "--strict";
397 unpacker[i++] = hdr_arg;
398 unpacker[i++] = NULL;
399 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
403 case -ERR_RUN_COMMAND_FORK:
404 return "unpack fork failed";
405 case -ERR_RUN_COMMAND_EXEC:
406 return "unpack execute failed";
407 case -ERR_RUN_COMMAND_WAITPID:
408 return "waitpid failed";
409 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
410 return "waitpid is confused";
411 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
412 return "unpacker died of signal";
413 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
414 return "unpacker died strangely";
416 return "unpacker exited with error code";
419 const char *keeper[7];
420 int s, status, i = 0;
422 struct child_process ip;
424 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
425 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
426 strcpy(keep_arg + s, "localhost");
428 keeper[i++] = "index-pack";
429 keeper[i++] = "--stdin";
430 if (receive_fsck_objects)
431 keeper[i++] = "--strict";
432 keeper[i++] = "--fix-thin";
433 keeper[i++] = hdr_arg;
434 keeper[i++] = keep_arg;
436 memset(&ip, 0, sizeof(ip));
440 if (start_command(&ip))
441 return "index-pack fork failed";
442 pack_lockfile = index_pack_lockfile(ip.out);
444 status = finish_command(&ip);
446 reprepare_packed_git();
449 return "index-pack abnormal exit";
453 static void report(const char *unpack_status)
456 packet_write(1, "unpack %s\n",
457 unpack_status ? unpack_status : "ok");
458 for (cmd = commands; cmd; cmd = cmd->next) {
459 if (!cmd->error_string)
460 packet_write(1, "ok %s\n",
463 packet_write(1, "ng %s %s\n",
464 cmd->ref_name, cmd->error_string);
469 static int delete_only(struct command *cmd)
472 if (!is_null_sha1(cmd->new_sha1))
479 static int add_refs_from_alternate(struct alternate_object_database *e, void *unused)
483 struct remote *remote;
484 struct transport *transport;
485 const struct ref *extra;
488 other = xstrdup(make_absolute_path(e->base));
492 while (other[len-1] == '/')
494 if (len < 8 || memcmp(other + len - 8, "/objects", 8))
496 /* Is this a git repository with refs? */
497 memcpy(other + len - 8, "/refs", 6);
498 if (!is_directory(other))
500 other[len - 8] = '\0';
501 remote = remote_get(other);
502 transport = transport_get(remote, other);
503 for (extra = transport_get_remote_refs(transport);
505 extra = extra->next) {
506 add_extra_ref(".have", extra->old_sha1, 0);
508 transport_disconnect(transport);
513 static void add_alternate_refs(void)
515 foreach_alt_odb(add_refs_from_alternate, NULL);
518 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
524 for (i = 1; i < argc; i++) {
525 const char *arg = *argv++;
528 /* Do flag handling here */
529 usage(receive_pack_usage);
532 usage(receive_pack_usage);
536 usage(receive_pack_usage);
540 if (!enter_repo(dir, 0))
541 die("'%s': unable to chdir or not a git archive", dir);
543 if (is_repository_shallow())
544 die("attempt to push into a shallow repository");
546 git_config(receive_pack_config, NULL);
548 if (0 <= transfer_unpack_limit)
549 unpack_limit = transfer_unpack_limit;
550 else if (0 <= receive_unpack_limit)
551 unpack_limit = receive_unpack_limit;
553 add_alternate_refs();
562 const char *unpack_status = NULL;
564 if (!delete_only(commands))
565 unpack_status = unpack();
566 execute_commands(unpack_status);
568 unlink(pack_lockfile);
570 report(unpack_status);
571 run_hook(post_receive_hook);
572 run_update_post_hook(commands);