have index-pack create .keep file more carefully
[git/dscho.git] / receive-pack.c
blobb8d12700d80254e59ee8f1b017f19a465dfeccb7
1 #include "cache.h"
2 #include "pack.h"
3 #include "refs.h"
4 #include "pkt-line.h"
5 #include "run-command.h"
6 #include "commit.h"
7 #include "object.h"
9 static const char receive_pack_usage[] = "git-receive-pack <git-dir>";
11 static int deny_non_fast_forwards = 0;
12 static int unpack_limit = 5000;
13 static int report_status;
15 static char capabilities[] = "report-status";
16 static int capabilities_sent;
18 static int receive_pack_config(const char *var, const char *value)
20 git_default_config(var, value);
22 if (strcmp(var, "receive.denynonfastforwards") == 0)
24 deny_non_fast_forwards = git_config_bool(var, value);
25 return 0;
28 if (strcmp(var, "receive.unpacklimit") == 0)
30 unpack_limit = git_config_int(var, value);
31 return 0;
34 return 0;
37 static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
39 if (capabilities_sent)
40 packet_write(1, "%s %s\n", sha1_to_hex(sha1), path);
41 else
42 packet_write(1, "%s %s%c%s\n",
43 sha1_to_hex(sha1), path, 0, capabilities);
44 capabilities_sent = 1;
45 return 0;
48 static void write_head_info(void)
50 for_each_ref(show_ref, NULL);
51 if (!capabilities_sent)
52 show_ref("capabilities^{}", null_sha1, 0, NULL);
56 struct command {
57 struct command *next;
58 const char *error_string;
59 unsigned char old_sha1[20];
60 unsigned char new_sha1[20];
61 char ref_name[FLEX_ARRAY]; /* more */
64 static struct command *commands;
66 static char update_hook[] = "hooks/update";
68 static int run_update_hook(const char *refname,
69 char *old_hex, char *new_hex)
71 int code;
73 if (access(update_hook, X_OK) < 0)
74 return 0;
75 code = run_command(update_hook, refname, old_hex, new_hex, NULL);
76 switch (code) {
77 case 0:
78 return 0;
79 case -ERR_RUN_COMMAND_FORK:
80 return error("hook fork failed");
81 case -ERR_RUN_COMMAND_EXEC:
82 return error("hook execute failed");
83 case -ERR_RUN_COMMAND_WAITPID:
84 return error("waitpid failed");
85 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
86 return error("waitpid is confused");
87 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
88 return error("%s died of signal", update_hook);
89 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
90 return error("%s died strangely", update_hook);
91 default:
92 error("%s exited with error code %d", update_hook, -code);
93 return -code;
97 static int update(struct command *cmd)
99 const char *name = cmd->ref_name;
100 unsigned char *old_sha1 = cmd->old_sha1;
101 unsigned char *new_sha1 = cmd->new_sha1;
102 char new_hex[41], old_hex[41];
103 struct ref_lock *lock;
105 cmd->error_string = NULL;
106 if (!strncmp(name, "refs/", 5) && check_ref_format(name + 5)) {
107 cmd->error_string = "funny refname";
108 return error("refusing to create funny ref '%s' locally",
109 name);
112 strcpy(new_hex, sha1_to_hex(new_sha1));
113 strcpy(old_hex, sha1_to_hex(old_sha1));
114 if (!has_sha1_file(new_sha1)) {
115 cmd->error_string = "bad pack";
116 return error("unpack should have generated %s, "
117 "but I can't find it!", new_hex);
119 if (deny_non_fast_forwards && !is_null_sha1(old_sha1)) {
120 struct commit *old_commit, *new_commit;
121 struct commit_list *bases, *ent;
123 old_commit = (struct commit *)parse_object(old_sha1);
124 new_commit = (struct commit *)parse_object(new_sha1);
125 bases = get_merge_bases(old_commit, new_commit, 1);
126 for (ent = bases; ent; ent = ent->next)
127 if (!hashcmp(old_sha1, ent->item->object.sha1))
128 break;
129 free_commit_list(bases);
130 if (!ent)
131 return error("denying non-fast forward;"
132 " you should pull first");
134 if (run_update_hook(name, old_hex, new_hex)) {
135 cmd->error_string = "hook declined";
136 return error("hook declined to update %s", name);
139 lock = lock_any_ref_for_update(name, old_sha1);
140 if (!lock) {
141 cmd->error_string = "failed to lock";
142 return error("failed to lock %s", name);
144 write_ref_sha1(lock, new_sha1, "push");
146 fprintf(stderr, "%s: %s -> %s\n", name, old_hex, new_hex);
147 return 0;
150 static char update_post_hook[] = "hooks/post-update";
152 static void run_update_post_hook(struct command *cmd)
154 struct command *cmd_p;
155 int argc;
156 const char **argv;
158 if (access(update_post_hook, X_OK) < 0)
159 return;
160 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
161 if (cmd_p->error_string)
162 continue;
163 argc++;
165 argv = xmalloc(sizeof(*argv) * (1 + argc));
166 argv[0] = update_post_hook;
168 for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) {
169 char *p;
170 if (cmd_p->error_string)
171 continue;
172 p = xmalloc(strlen(cmd_p->ref_name) + 1);
173 strcpy(p, cmd_p->ref_name);
174 argv[argc] = p;
175 argc++;
177 argv[argc] = NULL;
178 run_command_v_opt(argc, argv, RUN_COMMAND_NO_STDIO);
182 * This gets called after(if) we've successfully
183 * unpacked the data payload.
185 static void execute_commands(void)
187 struct command *cmd = commands;
189 while (cmd) {
190 update(cmd);
191 cmd = cmd->next;
193 run_update_post_hook(commands);
196 static void read_head_info(void)
198 struct command **p = &commands;
199 for (;;) {
200 static char line[1000];
201 unsigned char old_sha1[20], new_sha1[20];
202 struct command *cmd;
203 char *refname;
204 int len, reflen;
206 len = packet_read_line(0, line, sizeof(line));
207 if (!len)
208 break;
209 if (line[len-1] == '\n')
210 line[--len] = 0;
211 if (len < 83 ||
212 line[40] != ' ' ||
213 line[81] != ' ' ||
214 get_sha1_hex(line, old_sha1) ||
215 get_sha1_hex(line + 41, new_sha1))
216 die("protocol error: expected old/new/ref, got '%s'",
217 line);
219 refname = line + 82;
220 reflen = strlen(refname);
221 if (reflen + 82 < len) {
222 if (strstr(refname + reflen + 1, "report-status"))
223 report_status = 1;
225 cmd = xmalloc(sizeof(struct command) + len - 80);
226 hashcpy(cmd->old_sha1, old_sha1);
227 hashcpy(cmd->new_sha1, new_sha1);
228 memcpy(cmd->ref_name, line + 82, len - 81);
229 cmd->error_string = "n/a (unpacker error)";
230 cmd->next = NULL;
231 *p = cmd;
232 p = &cmd->next;
236 static const char *parse_pack_header(struct pack_header *hdr)
238 char *c = (char*)hdr;
239 ssize_t remaining = sizeof(struct pack_header);
240 do {
241 ssize_t r = xread(0, c, remaining);
242 if (r <= 0)
243 return "eof before pack header was fully read";
244 remaining -= r;
245 c += r;
246 } while (remaining > 0);
247 if (hdr->hdr_signature != htonl(PACK_SIGNATURE))
248 return "protocol error (pack signature mismatch detected)";
249 if (!pack_version_ok(hdr->hdr_version))
250 return "protocol error (pack version unsupported)";
251 return NULL;
254 static const char *unpack(void)
256 struct pack_header hdr;
257 const char *hdr_err;
258 char hdr_arg[38];
259 int code;
261 hdr_err = parse_pack_header(&hdr);
262 if (hdr_err)
263 return hdr_err;
264 snprintf(hdr_arg, sizeof(hdr_arg), "--pack_header=%u,%u",
265 ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries));
267 if (ntohl(hdr.hdr_entries) < unpack_limit) {
268 const char *unpacker[3];
269 unpacker[0] = "unpack-objects";
270 unpacker[1] = hdr_arg;
271 unpacker[2] = NULL;
272 code = run_command_v_opt(1, unpacker, RUN_GIT_CMD);
273 } else {
274 const char *keeper[6];
275 char my_host[255], keep_arg[128 + 255];
277 if (gethostname(my_host, sizeof(my_host)))
278 strcpy(my_host, "localhost");
279 snprintf(keep_arg, sizeof(keep_arg),
280 "--keep=receive-pack %i on %s",
281 getpid(), my_host);
283 keeper[0] = "index-pack";
284 keeper[1] = "--stdin";
285 keeper[2] = "--fix-thin";
286 keeper[3] = hdr_arg;
287 keeper[4] = keep_arg;
288 keeper[5] = NULL;
289 code = run_command_v_opt(1, keeper, RUN_GIT_CMD);
290 if (!code)
291 reprepare_packed_git();
294 switch (code) {
295 case 0:
296 return NULL;
297 case -ERR_RUN_COMMAND_FORK:
298 return "unpack fork failed";
299 case -ERR_RUN_COMMAND_EXEC:
300 return "unpack execute failed";
301 case -ERR_RUN_COMMAND_WAITPID:
302 return "waitpid failed";
303 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
304 return "waitpid is confused";
305 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
306 return "unpacker died of signal";
307 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
308 return "unpacker died strangely";
309 default:
310 return "unpacker exited with error code";
314 static void report(const char *unpack_status)
316 struct command *cmd;
317 packet_write(1, "unpack %s\n",
318 unpack_status ? unpack_status : "ok");
319 for (cmd = commands; cmd; cmd = cmd->next) {
320 if (!cmd->error_string)
321 packet_write(1, "ok %s\n",
322 cmd->ref_name);
323 else
324 packet_write(1, "ng %s %s\n",
325 cmd->ref_name, cmd->error_string);
327 packet_flush(1);
330 int main(int argc, char **argv)
332 int i;
333 char *dir = NULL;
335 argv++;
336 for (i = 1; i < argc; i++) {
337 char *arg = *argv++;
339 if (*arg == '-') {
340 /* Do flag handling here */
341 usage(receive_pack_usage);
343 if (dir)
344 usage(receive_pack_usage);
345 dir = arg;
347 if (!dir)
348 usage(receive_pack_usage);
350 if (!enter_repo(dir, 0))
351 die("'%s': unable to chdir or not a git archive", dir);
353 setup_ident();
354 git_config(receive_pack_config);
356 write_head_info();
358 /* EOF */
359 packet_flush(1);
361 read_head_info();
362 if (commands) {
363 const char *unpack_status = unpack();
364 if (!unpack_status)
365 execute_commands();
366 if (report_status)
367 report(unpack_status);
369 return 0;