5 #include "run-command.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)
23 if (strcmp(var, "receive.denynonfastforwards") == 0) {
24 deny_non_fast_forwards = git_config_bool(var, value);
28 if (strcmp(var, "receive.unpacklimit") == 0) {
29 receive_unpack_limit = git_config_int(var, value);
33 if (strcmp(var, "transfer.unpacklimit") == 0) {
34 transfer_unpack_limit = git_config_int(var, value);
38 return git_default_config(var, value);
41 static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
43 if (capabilities_sent)
44 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
46 packet_write(1, "%s %s%c%s\n",
47 sha1_to_hex(sha1), path, 0, capabilities);
48 capabilities_sent = 1;
52 static void write_head_info(void)
54 for_each_ref(show_ref, NULL);
55 if (!capabilities_sent)
56 show_ref("capabilities^{}", null_sha1, 0, NULL);
62 const char *error_string;
63 unsigned char old_sha1[20];
64 unsigned char new_sha1[20];
65 char ref_name[FLEX_ARRAY]; /* more */
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)
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);
93 error("%s exited with error code %d", hook_name, -code);
98 static int run_hook(const char *hook_name)
100 static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4];
102 struct child_process proc;
104 int have_input = 0, code;
106 for (cmd = commands; !have_input && cmd; cmd = cmd->next) {
107 if (!cmd->error_string)
111 if (!have_input || access(hook_name, X_OK) < 0)
117 memset(&proc, 0, sizeof(proc));
120 proc.stdout_to_stderr = 1;
122 code = start_command(&proc);
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),
131 if (write_in_full(proc.in, buf, n) != n)
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;
144 if (access(update_hook, X_OK) < 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);
153 memset(&proc, 0, sizeof(proc));
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 if (!prefixcmp(name, "refs/") && check_ref_format(name + 5)) {
169 error("refusing to create funny ref '%s' remotely", name);
170 return "funny refname";
173 if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) {
174 error("unpack should have generated %s, "
175 "but I can't find it!", sha1_to_hex(new_sha1));
178 if (deny_non_fast_forwards && !is_null_sha1(new_sha1) &&
179 !is_null_sha1(old_sha1) &&
180 !prefixcmp(name, "refs/heads/")) {
181 struct commit *old_commit, *new_commit;
182 struct commit_list *bases, *ent;
184 old_commit = (struct commit *)parse_object(old_sha1);
185 new_commit = (struct commit *)parse_object(new_sha1);
186 bases = get_merge_bases(old_commit, new_commit, 1);
187 for (ent = bases; ent; ent = ent->next)
188 if (!hashcmp(old_sha1, ent->item->object.sha1))
190 free_commit_list(bases);
192 error("denying non-fast forward %s"
193 " (you should pull first)", name);
194 return "non-fast forward";
197 if (run_update_hook(cmd)) {
198 error("hook declined to update %s", name);
199 return "hook declined";
202 if (is_null_sha1(new_sha1)) {
203 if (delete_ref(name, old_sha1)) {
204 error("failed to delete %s", name);
205 return "failed to delete";
207 return NULL; /* good */
210 lock = lock_any_ref_for_update(name, old_sha1, 0);
212 error("failed to lock %s", name);
213 return "failed to lock";
215 if (write_ref_sha1(lock, new_sha1, "push")) {
216 return "failed to write"; /* error() already called */
218 return NULL; /* good */
222 static char update_post_hook[] = "hooks/post-update";
224 static void run_update_post_hook(struct command *cmd)
226 struct command *cmd_p;
230 for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
231 if (cmd_p->error_string)
235 if (!argc || access(update_post_hook, X_OK) < 0)
237 argv = xmalloc(sizeof(*argv) * (2 + argc));
238 argv[0] = update_post_hook;
240 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
242 if (cmd_p->error_string)
244 p = xmalloc(strlen(cmd_p->ref_name) + 1);
245 strcpy(p, cmd_p->ref_name);
250 run_command_v_opt(argv, RUN_COMMAND_NO_STDIN
251 | RUN_COMMAND_STDOUT_TO_STDERR);
254 static void execute_commands(const char *unpacker_error)
256 struct command *cmd = commands;
258 if (unpacker_error) {
260 cmd->error_string = "n/a (unpacker error)";
266 if (run_hook(pre_receive_hook)) {
268 cmd->error_string = "pre-receive hook declined";
275 cmd->error_string = update(cmd);
280 static void read_head_info(void)
282 struct command **p = &commands;
284 static char line[1000];
285 unsigned char old_sha1[20], new_sha1[20];
290 len = packet_read_line(0, line, sizeof(line));
293 if (line[len-1] == '\n')
298 get_sha1_hex(line, old_sha1) ||
299 get_sha1_hex(line + 41, new_sha1))
300 die("protocol error: expected old/new/ref, got '%s'",
304 reflen = strlen(refname);
305 if (reflen + 82 < len) {
306 if (strstr(refname + reflen + 1, "report-status"))
309 cmd = xmalloc(sizeof(struct command) + len - 80);
310 hashcpy(cmd->old_sha1, old_sha1);
311 hashcpy(cmd->new_sha1, new_sha1);
312 memcpy(cmd->ref_name, line + 82, len - 81);
313 cmd->error_string = NULL;
320 static const char *parse_pack_header(struct pack_header *hdr)
322 switch (read_pack_header(0, hdr)) {
324 return "eof before pack header was fully read";
326 case PH_ERROR_PACK_SIGNATURE:
327 return "protocol error (pack signature mismatch detected)";
329 case PH_ERROR_PROTOCOL:
330 return "protocol error (pack version unsupported)";
333 return "unknown error in parse_pack_header";
340 static const char *pack_lockfile;
342 static const char *unpack(void)
344 struct pack_header hdr;
348 hdr_err = parse_pack_header(&hdr);
351 snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%u,%u",
352 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
354 if (ntohl(hdr.hdr_entries) < unpack_limit) {
356 const char *unpacker[3];
357 unpacker[0] = "unpack-objects";
358 unpacker[1] = hdr_arg;
360 code = run_command_v_opt(unpacker, RUN_GIT_CMD);
364 case -ERR_RUN_COMMAND_FORK:
365 return "unpack fork failed";
366 case -ERR_RUN_COMMAND_EXEC:
367 return "unpack execute failed";
368 case -ERR_RUN_COMMAND_WAITPID:
369 return "waitpid failed";
370 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
371 return "waitpid is confused";
372 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
373 return "unpacker died of signal";
374 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
375 return "unpacker died strangely";
377 return "unpacker exited with error code";
380 const char *keeper[6];
383 struct child_process ip;
385 s = sprintf(keep_arg, "--keep=receive-pack %i on ", getpid());
386 if (gethostname(keep_arg + s, sizeof(keep_arg) - s))
387 strcpy(keep_arg + s, "localhost");
389 keeper[0] = "index-pack";
390 keeper[1] = "--stdin";
391 keeper[2] = "--fix-thin";
393 keeper[4] = keep_arg;
395 memset(&ip, 0, sizeof(ip));
399 if (start_command(&ip))
400 return "index-pack fork failed";
401 pack_lockfile = index_pack_lockfile(ip.out);
402 status = finish_command(&ip);
404 reprepare_packed_git();
407 return "index-pack abnormal exit";
411 static void report(const char *unpack_status)
414 packet_write(1, "unpack %s\n",
415 unpack_status ? unpack_status : "ok");
416 for (cmd = commands; cmd; cmd = cmd->next) {
417 if (!cmd->error_string)
418 packet_write(1, "ok %s\n",
421 packet_write(1, "ng %s %s\n",
422 cmd->ref_name, cmd->error_string);
427 static int delete_only(struct command *cmd)
430 if (!is_null_sha1(cmd->new_sha1))
437 int main(int argc, char **argv)
443 for (i = 1; i < argc; i++) {
447 /* Do flag handling here */
448 usage(receive_pack_usage);
451 usage(receive_pack_usage);
455 usage(receive_pack_usage);
457 if (!enter_repo(dir, 0))
458 die("'%s': unable to chdir or not a git archive", dir);
460 if (is_repository_shallow())
461 die("attempt to push into a shallow repository");
463 git_config(receive_pack_config);
465 if (0 <= transfer_unpack_limit)
466 unpack_limit = transfer_unpack_limit;
467 else if (0 <= receive_unpack_limit)
468 unpack_limit = receive_unpack_limit;
477 const char *unpack_status = NULL;
479 if (!delete_only(commands))
480 unpack_status = unpack();
481 execute_commands(unpack_status);
483 unlink(pack_lockfile);
485 report(unpack_status);
486 run_hook(post_receive_hook);
487 run_update_post_hook(commands);