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