Code

write_head_info(): handle "extra refs" locally
[git.git] / builtin / receive-pack.c
1 #include "builtin.h"
2 #include "pack.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "sideband.h"
6 #include "run-command.h"
7 #include "exec_cmd.h"
8 #include "commit.h"
9 #include "object.h"
10 #include "remote.h"
11 #include "transport.h"
12 #include "string-list.h"
13 #include "sha1-array.h"
14 #include "connected.h"
16 static const char receive_pack_usage[] = "git receive-pack <git-dir>";
18 enum deny_action {
19         DENY_UNCONFIGURED,
20         DENY_IGNORE,
21         DENY_WARN,
22         DENY_REFUSE
23 };
25 static int deny_deletes;
26 static int deny_non_fast_forwards;
27 static enum deny_action deny_current_branch = DENY_UNCONFIGURED;
28 static enum deny_action deny_delete_current = DENY_UNCONFIGURED;
29 static int receive_fsck_objects = -1;
30 static int transfer_fsck_objects = -1;
31 static int receive_unpack_limit = -1;
32 static int transfer_unpack_limit = -1;
33 static int unpack_limit = 100;
34 static int report_status;
35 static int use_sideband;
36 static int prefer_ofs_delta = 1;
37 static int auto_update_server_info;
38 static int auto_gc = 1;
39 static const char *head_name;
40 static int sent_capabilities;
42 static enum deny_action parse_deny_action(const char *var, const char *value)
43 {
44         if (value) {
45                 if (!strcasecmp(value, "ignore"))
46                         return DENY_IGNORE;
47                 if (!strcasecmp(value, "warn"))
48                         return DENY_WARN;
49                 if (!strcasecmp(value, "refuse"))
50                         return DENY_REFUSE;
51         }
52         if (git_config_bool(var, value))
53                 return DENY_REFUSE;
54         return DENY_IGNORE;
55 }
57 static int receive_pack_config(const char *var, const char *value, void *cb)
58 {
59         if (strcmp(var, "receive.denydeletes") == 0) {
60                 deny_deletes = git_config_bool(var, value);
61                 return 0;
62         }
64         if (strcmp(var, "receive.denynonfastforwards") == 0) {
65                 deny_non_fast_forwards = git_config_bool(var, value);
66                 return 0;
67         }
69         if (strcmp(var, "receive.unpacklimit") == 0) {
70                 receive_unpack_limit = git_config_int(var, value);
71                 return 0;
72         }
74         if (strcmp(var, "transfer.unpacklimit") == 0) {
75                 transfer_unpack_limit = git_config_int(var, value);
76                 return 0;
77         }
79         if (strcmp(var, "receive.fsckobjects") == 0) {
80                 receive_fsck_objects = git_config_bool(var, value);
81                 return 0;
82         }
84         if (strcmp(var, "transfer.fsckobjects") == 0) {
85                 transfer_fsck_objects = git_config_bool(var, value);
86                 return 0;
87         }
89         if (!strcmp(var, "receive.denycurrentbranch")) {
90                 deny_current_branch = parse_deny_action(var, value);
91                 return 0;
92         }
94         if (strcmp(var, "receive.denydeletecurrent") == 0) {
95                 deny_delete_current = parse_deny_action(var, value);
96                 return 0;
97         }
99         if (strcmp(var, "repack.usedeltabaseoffset") == 0) {
100                 prefer_ofs_delta = git_config_bool(var, value);
101                 return 0;
102         }
104         if (strcmp(var, "receive.updateserverinfo") == 0) {
105                 auto_update_server_info = git_config_bool(var, value);
106                 return 0;
107         }
109         if (strcmp(var, "receive.autogc") == 0) {
110                 auto_gc = git_config_bool(var, value);
111                 return 0;
112         }
114         return git_default_config(var, value, cb);
117 static void show_ref(const char *path, const unsigned char *sha1)
119         if (sent_capabilities)
120                 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
121         else
122                 packet_write(1, "%s %s%c%s%s\n",
123                              sha1_to_hex(sha1), path, 0,
124                              " report-status delete-refs side-band-64k",
125                              prefer_ofs_delta ? " ofs-delta" : "");
126         sent_capabilities = 1;
129 static int show_ref_cb(const char *path, const unsigned char *sha1, int flag, void *unused)
131         path = strip_namespace(path);
132         /*
133          * Advertise refs outside our current namespace as ".have"
134          * refs, so that the client can use them to minimize data
135          * transfer but will otherwise ignore them. This happens to
136          * cover ".have" that are thrown in by add_one_alternate_ref()
137          * to mark histories that are complete in our alternates as
138          * well.
139          */
140         if (!path)
141                 path = ".have";
142         show_ref(path, sha1);
143         return 0;
146 static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused)
148         show_ref(".have", sha1);
151 static void collect_one_alternate_ref(const struct ref *ref, void *data)
153         struct sha1_array *sa = data;
154         sha1_array_append(sa, ref->old_sha1);
157 static void write_head_info(void)
159         struct sha1_array sa = SHA1_ARRAY_INIT;
160         for_each_alternate_ref(collect_one_alternate_ref, &sa);
161         sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL);
162         sha1_array_clear(&sa);
163         for_each_ref(show_ref_cb, NULL);
164         if (!sent_capabilities)
165                 show_ref("capabilities^{}", null_sha1);
167         /* EOF */
168         packet_flush(1);
171 struct command {
172         struct command *next;
173         const char *error_string;
174         unsigned int skip_update:1,
175                      did_not_exist:1;
176         unsigned char old_sha1[20];
177         unsigned char new_sha1[20];
178         char ref_name[FLEX_ARRAY]; /* more */
179 };
181 static const char pre_receive_hook[] = "hooks/pre-receive";
182 static const char post_receive_hook[] = "hooks/post-receive";
184 static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2)));
185 static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
187 static void report_message(const char *prefix, const char *err, va_list params)
189         int sz = strlen(prefix);
190         char msg[4096];
192         strncpy(msg, prefix, sz);
193         sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params);
194         if (sz > (sizeof(msg) - 1))
195                 sz = sizeof(msg) - 1;
196         msg[sz++] = '\n';
198         if (use_sideband)
199                 send_sideband(1, 2, msg, sz, use_sideband);
200         else
201                 xwrite(2, msg, sz);
204 static void rp_warning(const char *err, ...)
206         va_list params;
207         va_start(params, err);
208         report_message("warning: ", err, params);
209         va_end(params);
212 static void rp_error(const char *err, ...)
214         va_list params;
215         va_start(params, err);
216         report_message("error: ", err, params);
217         va_end(params);
220 static int copy_to_sideband(int in, int out, void *arg)
222         char data[128];
223         while (1) {
224                 ssize_t sz = xread(in, data, sizeof(data));
225                 if (sz <= 0)
226                         break;
227                 send_sideband(1, 2, data, sz, use_sideband);
228         }
229         close(in);
230         return 0;
233 typedef int (*feed_fn)(void *, const char **, size_t *);
234 static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state)
236         struct child_process proc;
237         struct async muxer;
238         const char *argv[2];
239         int code;
241         if (access(hook_name, X_OK) < 0)
242                 return 0;
244         argv[0] = hook_name;
245         argv[1] = NULL;
247         memset(&proc, 0, sizeof(proc));
248         proc.argv = argv;
249         proc.in = -1;
250         proc.stdout_to_stderr = 1;
252         if (use_sideband) {
253                 memset(&muxer, 0, sizeof(muxer));
254                 muxer.proc = copy_to_sideband;
255                 muxer.in = -1;
256                 code = start_async(&muxer);
257                 if (code)
258                         return code;
259                 proc.err = muxer.in;
260         }
262         code = start_command(&proc);
263         if (code) {
264                 if (use_sideband)
265                         finish_async(&muxer);
266                 return code;
267         }
269         while (1) {
270                 const char *buf;
271                 size_t n;
272                 if (feed(feed_state, &buf, &n))
273                         break;
274                 if (write_in_full(proc.in, buf, n) != n)
275                         break;
276         }
277         close(proc.in);
278         if (use_sideband)
279                 finish_async(&muxer);
280         return finish_command(&proc);
283 struct receive_hook_feed_state {
284         struct command *cmd;
285         int skip_broken;
286         struct strbuf buf;
287 };
289 static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep)
291         struct receive_hook_feed_state *state = state_;
292         struct command *cmd = state->cmd;
294         while (cmd &&
295                state->skip_broken && (cmd->error_string || cmd->did_not_exist))
296                 cmd = cmd->next;
297         if (!cmd)
298                 return -1; /* EOF */
299         strbuf_reset(&state->buf);
300         strbuf_addf(&state->buf, "%s %s %s\n",
301                     sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1),
302                     cmd->ref_name);
303         state->cmd = cmd->next;
304         if (bufp) {
305                 *bufp = state->buf.buf;
306                 *sizep = state->buf.len;
307         }
308         return 0;
311 static int run_receive_hook(struct command *commands, const char *hook_name,
312                             int skip_broken)
314         struct receive_hook_feed_state state;
315         int status;
317         strbuf_init(&state.buf, 0);
318         state.cmd = commands;
319         state.skip_broken = skip_broken;
320         if (feed_receive_hook(&state, NULL, NULL))
321                 return 0;
322         state.cmd = commands;
323         status = run_and_feed_hook(hook_name, feed_receive_hook, &state);
324         strbuf_release(&state.buf);
325         return status;
328 static int run_update_hook(struct command *cmd)
330         static const char update_hook[] = "hooks/update";
331         const char *argv[5];
332         struct child_process proc;
333         int code;
335         if (access(update_hook, X_OK) < 0)
336                 return 0;
338         argv[0] = update_hook;
339         argv[1] = cmd->ref_name;
340         argv[2] = sha1_to_hex(cmd->old_sha1);
341         argv[3] = sha1_to_hex(cmd->new_sha1);
342         argv[4] = NULL;
344         memset(&proc, 0, sizeof(proc));
345         proc.no_stdin = 1;
346         proc.stdout_to_stderr = 1;
347         proc.err = use_sideband ? -1 : 0;
348         proc.argv = argv;
350         code = start_command(&proc);
351         if (code)
352                 return code;
353         if (use_sideband)
354                 copy_to_sideband(proc.err, -1, NULL);
355         return finish_command(&proc);
358 static int is_ref_checked_out(const char *ref)
360         if (is_bare_repository())
361                 return 0;
363         if (!head_name)
364                 return 0;
365         return !strcmp(head_name, ref);
368 static char *refuse_unconfigured_deny_msg[] = {
369         "By default, updating the current branch in a non-bare repository",
370         "is denied, because it will make the index and work tree inconsistent",
371         "with what you pushed, and will require 'git reset --hard' to match",
372         "the work tree to HEAD.",
373         "",
374         "You can set 'receive.denyCurrentBranch' configuration variable to",
375         "'ignore' or 'warn' in the remote repository to allow pushing into",
376         "its current branch; however, this is not recommended unless you",
377         "arranged to update its work tree to match what you pushed in some",
378         "other way.",
379         "",
380         "To squelch this message and still keep the default behaviour, set",
381         "'receive.denyCurrentBranch' configuration variable to 'refuse'."
382 };
384 static void refuse_unconfigured_deny(void)
386         int i;
387         for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++)
388                 rp_error("%s", refuse_unconfigured_deny_msg[i]);
391 static char *refuse_unconfigured_deny_delete_current_msg[] = {
392         "By default, deleting the current branch is denied, because the next",
393         "'git clone' won't result in any file checked out, causing confusion.",
394         "",
395         "You can set 'receive.denyDeleteCurrent' configuration variable to",
396         "'warn' or 'ignore' in the remote repository to allow deleting the",
397         "current branch, with or without a warning message.",
398         "",
399         "To squelch this message, you can set it to 'refuse'."
400 };
402 static void refuse_unconfigured_deny_delete_current(void)
404         int i;
405         for (i = 0;
406              i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg);
407              i++)
408                 rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]);
411 static const char *update(struct command *cmd)
413         const char *name = cmd->ref_name;
414         struct strbuf namespaced_name_buf = STRBUF_INIT;
415         const char *namespaced_name;
416         unsigned char *old_sha1 = cmd->old_sha1;
417         unsigned char *new_sha1 = cmd->new_sha1;
418         struct ref_lock *lock;
420         /* only refs/... are allowed */
421         if (prefixcmp(name, "refs/") || check_refname_format(name + 5, 0)) {
422                 rp_error("refusing to create funny ref '%s' remotely", name);
423                 return "funny refname";
424         }
426         strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name);
427         namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
429         if (is_ref_checked_out(namespaced_name)) {
430                 switch (deny_current_branch) {
431                 case DENY_IGNORE:
432                         break;
433                 case DENY_WARN:
434                         rp_warning("updating the current branch");
435                         break;
436                 case DENY_REFUSE:
437                 case DENY_UNCONFIGURED:
438                         rp_error("refusing to update checked out branch: %s", name);
439                         if (deny_current_branch == DENY_UNCONFIGURED)
440                                 refuse_unconfigured_deny();
441                         return "branch is currently checked out";
442                 }
443         }
445         if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
446                 error("unpack should have generated %s, "
447                       "but I can't find it!", sha1_to_hex(new_sha1));
448                 return "bad pack";
449         }
451         if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) {
452                 if (deny_deletes && !prefixcmp(name, "refs/heads/")) {
453                         rp_error("denying ref deletion for %s", name);
454                         return "deletion prohibited";
455                 }
457                 if (!strcmp(namespaced_name, head_name)) {
458                         switch (deny_delete_current) {
459                         case DENY_IGNORE:
460                                 break;
461                         case DENY_WARN:
462                                 rp_warning("deleting the current branch");
463                                 break;
464                         case DENY_REFUSE:
465                         case DENY_UNCONFIGURED:
466                                 if (deny_delete_current == DENY_UNCONFIGURED)
467                                         refuse_unconfigured_deny_delete_current();
468                                 rp_error("refusing to delete the current branch: %s", name);
469                                 return "deletion of the current branch prohibited";
470                         }
471                 }
472         }
474         if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
475             !is_null_sha1(old_sha1) &&
476             !prefixcmp(name, "refs/heads/")) {
477                 struct object *old_object, *new_object;
478                 struct commit *old_commit, *new_commit;
479                 struct commit_list *bases, *ent;
481                 old_object = parse_object(old_sha1);
482                 new_object = parse_object(new_sha1);
484                 if (!old_object || !new_object ||
485                     old_object->type != OBJ_COMMIT ||
486                     new_object->type != OBJ_COMMIT) {
487                         error("bad sha1 objects for %s", name);
488                         return "bad ref";
489                 }
490                 old_commit = (struct commit *)old_object;
491                 new_commit = (struct commit *)new_object;
492                 bases = get_merge_bases(old_commit, new_commit, 1);
493                 for (ent = bases; ent; ent = ent->next)
494                         if (!hashcmp(old_sha1, ent->item->object.sha1))
495                                 break;
496                 free_commit_list(bases);
497                 if (!ent) {
498                         rp_error("denying non-fast-forward %s"
499                                  " (you should pull first)", name);
500                         return "non-fast-forward";
501                 }
502         }
503         if (run_update_hook(cmd)) {
504                 rp_error("hook declined to update %s", name);
505                 return "hook declined";
506         }
508         if (is_null_sha1(new_sha1)) {
509                 if (!parse_object(old_sha1)) {
510                         old_sha1 = NULL;
511                         if (ref_exists(name)) {
512                                 rp_warning("Allowing deletion of corrupt ref.");
513                         } else {
514                                 rp_warning("Deleting a non-existent ref.");
515                                 cmd->did_not_exist = 1;
516                         }
517                 }
518                 if (delete_ref(namespaced_name, old_sha1, 0)) {
519                         rp_error("failed to delete %s", name);
520                         return "failed to delete";
521                 }
522                 return NULL; /* good */
523         }
524         else {
525                 lock = lock_any_ref_for_update(namespaced_name, old_sha1, 0);
526                 if (!lock) {
527                         rp_error("failed to lock %s", name);
528                         return "failed to lock";
529                 }
530                 if (write_ref_sha1(lock, new_sha1, "push")) {
531                         return "failed to write"; /* error() already called */
532                 }
533                 return NULL; /* good */
534         }
537 static char update_post_hook[] = "hooks/post-update";
539 static void run_update_post_hook(struct command *commands)
541         struct command *cmd;
542         int argc;
543         const char **argv;
544         struct child_process proc;
546         for (argc = 0, cmd = commands; cmd; cmd = cmd->next) {
547                 if (cmd->error_string || cmd->did_not_exist)
548                         continue;
549                 argc++;
550         }
551         if (!argc || access(update_post_hook, X_OK) < 0)
552                 return;
553         argv = xmalloc(sizeof(*argv) * (2 + argc));
554         argv[0] = update_post_hook;
556         for (argc = 1, cmd = commands; cmd; cmd = cmd->next) {
557                 char *p;
558                 if (cmd->error_string || cmd->did_not_exist)
559                         continue;
560                 p = xmalloc(strlen(cmd->ref_name) + 1);
561                 strcpy(p, cmd->ref_name);
562                 argv[argc] = p;
563                 argc++;
564         }
565         argv[argc] = NULL;
567         memset(&proc, 0, sizeof(proc));
568         proc.no_stdin = 1;
569         proc.stdout_to_stderr = 1;
570         proc.err = use_sideband ? -1 : 0;
571         proc.argv = argv;
573         if (!start_command(&proc)) {
574                 if (use_sideband)
575                         copy_to_sideband(proc.err, -1, NULL);
576                 finish_command(&proc);
577         }
580 static void check_aliased_update(struct command *cmd, struct string_list *list)
582         struct strbuf buf = STRBUF_INIT;
583         const char *dst_name;
584         struct string_list_item *item;
585         struct command *dst_cmd;
586         unsigned char sha1[20];
587         char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41];
588         int flag;
590         strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name);
591         dst_name = resolve_ref(buf.buf, sha1, 0, &flag);
592         strbuf_release(&buf);
594         if (!(flag & REF_ISSYMREF))
595                 return;
597         dst_name = strip_namespace(dst_name);
598         if (!dst_name) {
599                 rp_error("refusing update to broken symref '%s'", cmd->ref_name);
600                 cmd->skip_update = 1;
601                 cmd->error_string = "broken symref";
602                 return;
603         }
605         if ((item = string_list_lookup(list, dst_name)) == NULL)
606                 return;
608         cmd->skip_update = 1;
610         dst_cmd = (struct command *) item->util;
612         if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) &&
613             !hashcmp(cmd->new_sha1, dst_cmd->new_sha1))
614                 return;
616         dst_cmd->skip_update = 1;
618         strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV));
619         strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV));
620         strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV));
621         strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV));
622         rp_error("refusing inconsistent update between symref '%s' (%s..%s) and"
623                  " its target '%s' (%s..%s)",
624                  cmd->ref_name, cmd_oldh, cmd_newh,
625                  dst_cmd->ref_name, dst_oldh, dst_newh);
627         cmd->error_string = dst_cmd->error_string =
628                 "inconsistent aliased update";
631 static void check_aliased_updates(struct command *commands)
633         struct command *cmd;
634         struct string_list ref_list = STRING_LIST_INIT_NODUP;
636         for (cmd = commands; cmd; cmd = cmd->next) {
637                 struct string_list_item *item =
638                         string_list_append(&ref_list, cmd->ref_name);
639                 item->util = (void *)cmd;
640         }
641         sort_string_list(&ref_list);
643         for (cmd = commands; cmd; cmd = cmd->next)
644                 check_aliased_update(cmd, &ref_list);
646         string_list_clear(&ref_list, 0);
649 static int command_singleton_iterator(void *cb_data, unsigned char sha1[20])
651         struct command **cmd_list = cb_data;
652         struct command *cmd = *cmd_list;
654         if (!cmd || is_null_sha1(cmd->new_sha1))
655                 return -1; /* end of list */
656         *cmd_list = NULL; /* this returns only one */
657         hashcpy(sha1, cmd->new_sha1);
658         return 0;
661 static void set_connectivity_errors(struct command *commands)
663         struct command *cmd;
665         for (cmd = commands; cmd; cmd = cmd->next) {
666                 struct command *singleton = cmd;
667                 if (!check_everything_connected(command_singleton_iterator,
668                                                 0, &singleton))
669                         continue;
670                 cmd->error_string = "missing necessary objects";
671         }
674 static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20])
676         struct command **cmd_list = cb_data;
677         struct command *cmd = *cmd_list;
679         while (cmd) {
680                 if (!is_null_sha1(cmd->new_sha1)) {
681                         hashcpy(sha1, cmd->new_sha1);
682                         *cmd_list = cmd->next;
683                         return 0;
684                 }
685                 cmd = cmd->next;
686         }
687         *cmd_list = NULL;
688         return -1; /* end of list */
691 static void execute_commands(struct command *commands, const char *unpacker_error)
693         struct command *cmd;
694         unsigned char sha1[20];
696         if (unpacker_error) {
697                 for (cmd = commands; cmd; cmd = cmd->next)
698                         cmd->error_string = "n/a (unpacker error)";
699                 return;
700         }
702         cmd = commands;
703         if (check_everything_connected(iterate_receive_command_list,
704                                        0, &cmd))
705                 set_connectivity_errors(commands);
707         if (run_receive_hook(commands, pre_receive_hook, 0)) {
708                 for (cmd = commands; cmd; cmd = cmd->next)
709                         cmd->error_string = "pre-receive hook declined";
710                 return;
711         }
713         check_aliased_updates(commands);
715         free((char *)head_name);
716         head_name = resolve_ref("HEAD", sha1, 0, NULL);
717         if (head_name)
718                 head_name = xstrdup(head_name);
720         for (cmd = commands; cmd; cmd = cmd->next)
721                 if (!cmd->skip_update)
722                         cmd->error_string = update(cmd);
725 static struct command *read_head_info(void)
727         struct command *commands = NULL;
728         struct command **p = &commands;
729         for (;;) {
730                 static char line[1000];
731                 unsigned char old_sha1[20], new_sha1[20];
732                 struct command *cmd;
733                 char *refname;
734                 int len, reflen;
736                 len = packet_read_line(0, line, sizeof(line));
737                 if (!len)
738                         break;
739                 if (line[len-1] == '\n')
740                         line[--len] = 0;
741                 if (len < 83 ||
742                     line[40] != ' ' ||
743                     line[81] != ' ' ||
744                     get_sha1_hex(line, old_sha1) ||
745                     get_sha1_hex(line + 41, new_sha1))
746                         die("protocol error: expected old/new/ref, got '%s'",
747                             line);
749                 refname = line + 82;
750                 reflen = strlen(refname);
751                 if (reflen + 82 < len) {
752                         if (strstr(refname + reflen + 1, "report-status"))
753                                 report_status = 1;
754                         if (strstr(refname + reflen + 1, "side-band-64k"))
755                                 use_sideband = LARGE_PACKET_MAX;
756                 }
757                 cmd = xcalloc(1, sizeof(struct command) + len - 80);
758                 hashcpy(cmd->old_sha1, old_sha1);
759                 hashcpy(cmd->new_sha1, new_sha1);
760                 memcpy(cmd->ref_name, line + 82, len - 81);
761                 *p = cmd;
762                 p = &cmd->next;
763         }
764         return commands;
767 static const char *parse_pack_header(struct pack_header *hdr)
769         switch (read_pack_header(0, hdr)) {
770         case PH_ERROR_EOF:
771                 return "eof before pack header was fully read";
773         case PH_ERROR_PACK_SIGNATURE:
774                 return "protocol error (pack signature mismatch detected)";
776         case PH_ERROR_PROTOCOL:
777                 return "protocol error (pack version unsupported)";
779         default:
780                 return "unknown error in parse_pack_header";
782         case 0:
783                 return NULL;
784         }
787 static const char *pack_lockfile;
789 static const char *unpack(void)
791         struct pack_header hdr;
792         const char *hdr_err;
793         char hdr_arg[38];
794         int fsck_objects = (receive_fsck_objects >= 0
795                             ? receive_fsck_objects
796                             : transfer_fsck_objects >= 0
797                             ? transfer_fsck_objects
798                             : 0);
800         hdr_err = parse_pack_header(&hdr);
801         if (hdr_err)
802                 return hdr_err;
803         snprintf(hdr_arg, sizeof(hdr_arg),
804                         "--pack_header=%"PRIu32",%"PRIu32,
805                         ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
807         if (ntohl(hdr.hdr_entries) < unpack_limit) {
808                 int code, i = 0;
809                 const char *unpacker[4];
810                 unpacker[i++] = "unpack-objects";
811                 if (fsck_objects)
812                         unpacker[i++] = "--strict";
813                 unpacker[i++] = hdr_arg;
814                 unpacker[i++] = NULL;
815                 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
816                 if (!code)
817                         return NULL;
818                 return "unpack-objects abnormal exit";
819         } else {
820                 const char *keeper[7];
821                 int s, status, i = 0;
822                 char keep_arg[256];
823                 struct child_process ip;
825                 s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid());
826                 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
827                         strcpy(keep_arg + s, "localhost");
829                 keeper[i++] = "index-pack";
830                 keeper[i++] = "--stdin";
831                 if (fsck_objects)
832                         keeper[i++] = "--strict";
833                 keeper[i++] = "--fix-thin";
834                 keeper[i++] = hdr_arg;
835                 keeper[i++] = keep_arg;
836                 keeper[i++] = NULL;
837                 memset(&ip, 0, sizeof(ip));
838                 ip.argv = keeper;
839                 ip.out = -1;
840                 ip.git_cmd = 1;
841                 status = start_command(&ip);
842                 if (status) {
843                         return "index-pack fork failed";
844                 }
845                 pack_lockfile = index_pack_lockfile(ip.out);
846                 close(ip.out);
847                 status = finish_command(&ip);
848                 if (!status) {
849                         reprepare_packed_git();
850                         return NULL;
851                 }
852                 return "index-pack abnormal exit";
853         }
856 static void report(struct command *commands, const char *unpack_status)
858         struct command *cmd;
859         struct strbuf buf = STRBUF_INIT;
861         packet_buf_write(&buf, "unpack %s\n",
862                          unpack_status ? unpack_status : "ok");
863         for (cmd = commands; cmd; cmd = cmd->next) {
864                 if (!cmd->error_string)
865                         packet_buf_write(&buf, "ok %s\n",
866                                          cmd->ref_name);
867                 else
868                         packet_buf_write(&buf, "ng %s %s\n",
869                                          cmd->ref_name, cmd->error_string);
870         }
871         packet_buf_flush(&buf);
873         if (use_sideband)
874                 send_sideband(1, 1, buf.buf, buf.len, use_sideband);
875         else
876                 safe_write(1, buf.buf, buf.len);
877         strbuf_release(&buf);
880 static int delete_only(struct command *commands)
882         struct command *cmd;
883         for (cmd = commands; cmd; cmd = cmd->next) {
884                 if (!is_null_sha1(cmd->new_sha1))
885                         return 0;
886         }
887         return 1;
890 int cmd_receive_pack(int argc, const char **argv, const char *prefix)
892         int advertise_refs = 0;
893         int stateless_rpc = 0;
894         int i;
895         char *dir = NULL;
896         struct command *commands;
898         packet_trace_identity("receive-pack");
900         argv++;
901         for (i = 1; i < argc; i++) {
902                 const char *arg = *argv++;
904                 if (*arg == '-') {
905                         if (!strcmp(arg, "--advertise-refs")) {
906                                 advertise_refs = 1;
907                                 continue;
908                         }
909                         if (!strcmp(arg, "--stateless-rpc")) {
910                                 stateless_rpc = 1;
911                                 continue;
912                         }
914                         usage(receive_pack_usage);
915                 }
916                 if (dir)
917                         usage(receive_pack_usage);
918                 dir = xstrdup(arg);
919         }
920         if (!dir)
921                 usage(receive_pack_usage);
923         setup_path();
925         if (!enter_repo(dir, 0))
926                 die("'%s' does not appear to be a git repository", dir);
928         if (is_repository_shallow())
929                 die("attempt to push into a shallow repository");
931         git_config(receive_pack_config, NULL);
933         if (0 <= transfer_unpack_limit)
934                 unpack_limit = transfer_unpack_limit;
935         else if (0 <= receive_unpack_limit)
936                 unpack_limit = receive_unpack_limit;
938         if (advertise_refs || !stateless_rpc) {
939                 write_head_info();
940         }
941         if (advertise_refs)
942                 return 0;
944         if ((commands = read_head_info()) != NULL) {
945                 const char *unpack_status = NULL;
947                 if (!delete_only(commands))
948                         unpack_status = unpack();
949                 execute_commands(commands, unpack_status);
950                 if (pack_lockfile)
951                         unlink_or_warn(pack_lockfile);
952                 if (report_status)
953                         report(commands, unpack_status);
954                 run_receive_hook(commands, post_receive_hook, 1);
955                 run_update_post_hook(commands);
956                 if (auto_gc) {
957                         const char *argv_gc_auto[] = {
958                                 "gc", "--auto", "--quiet", NULL,
959                         };
960                         run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
961                 }
962                 if (auto_update_server_info)
963                         update_server_info(0);
964         }
965         if (use_sideband)
966                 packet_flush(1);
967         return 0;