]>
Commit | Line | Data |
---|---|---|
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" | |
d79796bc JH |
9 | #include "remote.h" |
10 | #include "transport.h" | |
575f4974 | 11 | |
34263de0 | 12 | static const char receive_pack_usage[] = "git receive-pack <git-dir>"; |
575f4974 | 13 | |
986e8239 | 14 | enum deny_action { |
3d95d92b | 15 | DENY_UNCONFIGURED, |
986e8239 JK |
16 | DENY_IGNORE, |
17 | DENY_WARN, | |
18 | DENY_REFUSE, | |
19 | }; | |
20 | ||
a240de11 | 21 | static int deny_deletes = 0; |
6fb75bed | 22 | static int deny_non_fast_forwards = 0; |
3d95d92b | 23 | static enum deny_action deny_current_branch = DENY_UNCONFIGURED; |
20dc0016 | 24 | static int receive_fsck_objects; |
e28714c5 JH |
25 | static int receive_unpack_limit = -1; |
26 | static int transfer_unpack_limit = -1; | |
46732fae | 27 | static int unpack_limit = 100; |
96f1e58f | 28 | static int report_status; |
cfee10a7 | 29 | |
d4f694ba | 30 | static char capabilities[] = " report-status delete-refs "; |
96f1e58f | 31 | static int capabilities_sent; |
cfee10a7 | 32 | |
986e8239 JK |
33 | static enum deny_action parse_deny_action(const char *var, const char *value) |
34 | { | |
35 | if (value) { | |
36 | if (!strcasecmp(value, "ignore")) | |
37 | return DENY_IGNORE; | |
38 | if (!strcasecmp(value, "warn")) | |
39 | return DENY_WARN; | |
40 | if (!strcasecmp(value, "refuse")) | |
41 | return DENY_REFUSE; | |
42 | } | |
43 | if (git_config_bool(var, value)) | |
44 | return DENY_REFUSE; | |
45 | return DENY_IGNORE; | |
46 | } | |
47 | ||
ef90d6d4 | 48 | static int receive_pack_config(const char *var, const char *value, void *cb) |
6fb75bed | 49 | { |
a240de11 JK |
50 | if (strcmp(var, "receive.denydeletes") == 0) { |
51 | deny_deletes = git_config_bool(var, value); | |
52 | return 0; | |
53 | } | |
54 | ||
e28714c5 | 55 | if (strcmp(var, "receive.denynonfastforwards") == 0) { |
6fb75bed SP |
56 | deny_non_fast_forwards = git_config_bool(var, value); |
57 | return 0; | |
58 | } | |
59 | ||
e28714c5 JH |
60 | if (strcmp(var, "receive.unpacklimit") == 0) { |
61 | receive_unpack_limit = git_config_int(var, value); | |
fc04c412 SP |
62 | return 0; |
63 | } | |
64 | ||
e28714c5 JH |
65 | if (strcmp(var, "transfer.unpacklimit") == 0) { |
66 | transfer_unpack_limit = git_config_int(var, value); | |
67 | return 0; | |
68 | } | |
69 | ||
20dc0016 MK |
70 | if (strcmp(var, "receive.fsckobjects") == 0) { |
71 | receive_fsck_objects = git_config_bool(var, value); | |
72 | return 0; | |
73 | } | |
74 | ||
986e8239 JK |
75 | if (!strcmp(var, "receive.denycurrentbranch")) { |
76 | deny_current_branch = parse_deny_action(var, value); | |
77 | return 0; | |
78 | } | |
79 | ||
ef90d6d4 | 80 | return git_default_config(var, value, cb); |
6fb75bed SP |
81 | } |
82 | ||
8da19775 | 83 | static int show_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data) |
575f4974 | 84 | { |
cfee10a7 JH |
85 | if (capabilities_sent) |
86 | packet_write(1, "%s %s\n", sha1_to_hex(sha1), path); | |
87 | else | |
88 | packet_write(1, "%s %s%c%s\n", | |
89 | sha1_to_hex(sha1), path, 0, capabilities); | |
90 | capabilities_sent = 1; | |
8a65ff76 | 91 | return 0; |
575f4974 LT |
92 | } |
93 | ||
8a65ff76 | 94 | static void write_head_info(void) |
575f4974 | 95 | { |
cb5d709f | 96 | for_each_ref(show_ref, NULL); |
cfee10a7 | 97 | if (!capabilities_sent) |
8da19775 | 98 | show_ref("capabilities^{}", null_sha1, 0, NULL); |
cfee10a7 | 99 | |
575f4974 LT |
100 | } |
101 | ||
eb1af2df LT |
102 | struct command { |
103 | struct command *next; | |
cfee10a7 | 104 | const char *error_string; |
eb1af2df LT |
105 | unsigned char old_sha1[20]; |
106 | unsigned char new_sha1[20]; | |
8f1d2e6f | 107 | char ref_name[FLEX_ARRAY]; /* more */ |
575f4974 LT |
108 | }; |
109 | ||
96f1e58f | 110 | static struct command *commands; |
575f4974 | 111 | |
05ef58ec SP |
112 | static const char pre_receive_hook[] = "hooks/pre-receive"; |
113 | static const char post_receive_hook[] = "hooks/post-receive"; | |
b1bf95bb | 114 | |
6c319a22 SP |
115 | static int hook_status(int code, const char *hook_name) |
116 | { | |
117 | switch (code) { | |
118 | case 0: | |
119 | return 0; | |
120 | case -ERR_RUN_COMMAND_FORK: | |
121 | return error("hook fork failed"); | |
122 | case -ERR_RUN_COMMAND_EXEC: | |
123 | return error("hook execute failed"); | |
f43cd49f SP |
124 | case -ERR_RUN_COMMAND_PIPE: |
125 | return error("hook pipe failed"); | |
6c319a22 SP |
126 | case -ERR_RUN_COMMAND_WAITPID: |
127 | return error("waitpid failed"); | |
128 | case -ERR_RUN_COMMAND_WAITPID_WRONG_PID: | |
129 | return error("waitpid is confused"); | |
130 | case -ERR_RUN_COMMAND_WAITPID_SIGNAL: | |
131 | return error("%s died of signal", hook_name); | |
132 | case -ERR_RUN_COMMAND_WAITPID_NOEXIT: | |
133 | return error("%s died strangely", hook_name); | |
134 | default: | |
135 | error("%s exited with error code %d", hook_name, -code); | |
136 | return -code; | |
137 | } | |
138 | } | |
139 | ||
ae98a008 | 140 | static int run_receive_hook(const char *hook_name) |
b1bf95bb | 141 | { |
f43cd49f | 142 | static char buf[sizeof(commands->old_sha1) * 2 + PATH_MAX + 4]; |
c8dd2771 | 143 | struct command *cmd; |
f43cd49f SP |
144 | struct child_process proc; |
145 | const char *argv[2]; | |
146 | int have_input = 0, code; | |
c8dd2771 | 147 | |
f43cd49f | 148 | for (cmd = commands; !have_input && cmd; cmd = cmd->next) { |
c8dd2771 | 149 | if (!cmd->error_string) |
f43cd49f | 150 | have_input = 1; |
c8dd2771 | 151 | } |
b1bf95bb | 152 | |
f43cd49f | 153 | if (!have_input || access(hook_name, X_OK) < 0) |
b1bf95bb | 154 | return 0; |
c8dd2771 | 155 | |
c8dd2771 | 156 | argv[0] = hook_name; |
f43cd49f SP |
157 | argv[1] = NULL; |
158 | ||
159 | memset(&proc, 0, sizeof(proc)); | |
160 | proc.argv = argv; | |
161 | proc.in = -1; | |
162 | proc.stdout_to_stderr = 1; | |
163 | ||
164 | code = start_command(&proc); | |
165 | if (code) | |
166 | return hook_status(code, hook_name); | |
167 | for (cmd = commands; cmd; cmd = cmd->next) { | |
c8dd2771 | 168 | if (!cmd->error_string) { |
f43cd49f SP |
169 | size_t n = snprintf(buf, sizeof(buf), "%s %s %s\n", |
170 | sha1_to_hex(cmd->old_sha1), | |
171 | sha1_to_hex(cmd->new_sha1), | |
172 | cmd->ref_name); | |
173 | if (write_in_full(proc.in, buf, n) != n) | |
174 | break; | |
c8dd2771 | 175 | } |
c8dd2771 | 176 | } |
e72ae288 | 177 | close(proc.in); |
f43cd49f | 178 | return hook_status(finish_command(&proc), hook_name); |
b1bf95bb JW |
179 | } |
180 | ||
1d9e8b56 SP |
181 | static int run_update_hook(struct command *cmd) |
182 | { | |
183 | static const char update_hook[] = "hooks/update"; | |
184 | struct child_process proc; | |
185 | const char *argv[5]; | |
186 | ||
187 | if (access(update_hook, X_OK) < 0) | |
188 | return 0; | |
189 | ||
190 | argv[0] = update_hook; | |
191 | argv[1] = cmd->ref_name; | |
192 | argv[2] = sha1_to_hex(cmd->old_sha1); | |
193 | argv[3] = sha1_to_hex(cmd->new_sha1); | |
194 | argv[4] = NULL; | |
195 | ||
196 | memset(&proc, 0, sizeof(proc)); | |
197 | proc.argv = argv; | |
198 | proc.no_stdin = 1; | |
199 | proc.stdout_to_stderr = 1; | |
200 | ||
201 | return hook_status(run_command(&proc), update_hook); | |
202 | } | |
203 | ||
986e8239 JK |
204 | static int is_ref_checked_out(const char *ref) |
205 | { | |
206 | unsigned char sha1[20]; | |
207 | const char *head; | |
208 | ||
209 | if (is_bare_repository()) | |
210 | return 0; | |
211 | ||
212 | head = resolve_ref("HEAD", sha1, 0, NULL); | |
213 | if (!head) | |
214 | return 0; | |
215 | return !strcmp(head, ref); | |
216 | } | |
217 | ||
3d95d92b JH |
218 | static char *warn_unconfigured_deny_msg[] = { |
219 | "Updating the currently checked out branch may cause confusion,", | |
220 | "as the index and work tree do not reflect changes that are in HEAD.", | |
221 | "As a result, you may see the changes you just pushed into it", | |
222 | "reverted when you run 'git diff' over there, and you may want", | |
223 | "to run 'git reset --hard' before starting to work to recover.", | |
224 | "", | |
225 | "You can set 'receive.denyCurrentBranch' configuration variable to", | |
226 | "'refuse' in the remote repository to forbid pushing into its", | |
227 | "current branch." | |
228 | "", | |
229 | "To allow pushing into the current branch, you can set it to 'ignore';", | |
230 | "but this is not recommended unless you arranged to update its work", | |
231 | "tree to match what you pushed in some other way.", | |
232 | "", | |
233 | "To squelch this message, you can set it to 'warn'.", | |
234 | "", | |
235 | "Note that the default will change in a future version of git", | |
236 | "to refuse updating the current branch unless you have the", | |
237 | "configuration variable set to either 'ignore' or 'warn'." | |
238 | }; | |
239 | ||
240 | static void warn_unconfigured_deny(void) | |
241 | { | |
242 | int i; | |
243 | for (i = 0; i < ARRAY_SIZE(warn_unconfigured_deny_msg); i++) | |
244 | warning(warn_unconfigured_deny_msg[i]); | |
245 | } | |
246 | ||
8aaf7d64 | 247 | static const char *update(struct command *cmd) |
2eca23da | 248 | { |
cfee10a7 JH |
249 | const char *name = cmd->ref_name; |
250 | unsigned char *old_sha1 = cmd->old_sha1; | |
251 | unsigned char *new_sha1 = cmd->new_sha1; | |
3159c8dc | 252 | struct ref_lock *lock; |
2eca23da | 253 | |
061d6b9a MK |
254 | /* only refs/... are allowed */ |
255 | if (prefixcmp(name, "refs/") || check_ref_format(name + 5)) { | |
0b8293f6 | 256 | error("refusing to create funny ref '%s' remotely", name); |
8aaf7d64 | 257 | return "funny refname"; |
cfee10a7 | 258 | } |
d8a1deec | 259 | |
3d95d92b JH |
260 | if (is_ref_checked_out(name)) { |
261 | switch (deny_current_branch) { | |
262 | case DENY_IGNORE: | |
986e8239 | 263 | break; |
3d95d92b JH |
264 | case DENY_UNCONFIGURED: |
265 | case DENY_WARN: | |
266 | warning("updating the current branch"); | |
267 | if (deny_current_branch == DENY_UNCONFIGURED) | |
268 | warn_unconfigured_deny(); | |
986e8239 | 269 | break; |
3d95d92b JH |
270 | case DENY_REFUSE: |
271 | error("refusing to update checked out branch: %s", name); | |
272 | return "branch is currently checked out"; | |
273 | } | |
986e8239 JK |
274 | } |
275 | ||
d4f694ba | 276 | if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) { |
8aaf7d64 SP |
277 | error("unpack should have generated %s, " |
278 | "but I can't find it!", sha1_to_hex(new_sha1)); | |
279 | return "bad pack"; | |
cfee10a7 | 280 | } |
a240de11 JK |
281 | if (deny_deletes && is_null_sha1(new_sha1) && |
282 | !is_null_sha1(old_sha1) && | |
283 | !prefixcmp(name, "refs/heads/")) { | |
284 | error("denying ref deletion for %s", name); | |
285 | return "deletion prohibited"; | |
286 | } | |
d4f694ba | 287 | if (deny_non_fast_forwards && !is_null_sha1(new_sha1) && |
ba988a83 | 288 | !is_null_sha1(old_sha1) && |
cc44c765 | 289 | !prefixcmp(name, "refs/heads/")) { |
eab82707 | 290 | struct object *old_object, *new_object; |
11031d7e | 291 | struct commit *old_commit, *new_commit; |
9edd7e46 | 292 | struct commit_list *bases, *ent; |
11031d7e | 293 | |
eab82707 MK |
294 | old_object = parse_object(old_sha1); |
295 | new_object = parse_object(new_sha1); | |
296 | ||
297 | if (!old_object || !new_object || | |
298 | old_object->type != OBJ_COMMIT || | |
299 | new_object->type != OBJ_COMMIT) { | |
300 | error("bad sha1 objects for %s", name); | |
301 | return "bad ref"; | |
302 | } | |
303 | old_commit = (struct commit *)old_object; | |
304 | new_commit = (struct commit *)new_object; | |
9edd7e46 JS |
305 | bases = get_merge_bases(old_commit, new_commit, 1); |
306 | for (ent = bases; ent; ent = ent->next) | |
307 | if (!hashcmp(old_sha1, ent->item->object.sha1)) | |
11031d7e | 308 | break; |
9edd7e46 | 309 | free_commit_list(bases); |
8aaf7d64 SP |
310 | if (!ent) { |
311 | error("denying non-fast forward %s" | |
312 | " (you should pull first)", name); | |
313 | return "non-fast forward"; | |
314 | } | |
11031d7e | 315 | } |
1d9e8b56 | 316 | if (run_update_hook(cmd)) { |
8aaf7d64 SP |
317 | error("hook declined to update %s", name); |
318 | return "hook declined"; | |
b1bf95bb | 319 | } |
3159c8dc | 320 | |
d4f694ba | 321 | if (is_null_sha1(new_sha1)) { |
28391a80 JS |
322 | if (!parse_object(old_sha1)) { |
323 | warning ("Allowing deletion of corrupt ref."); | |
324 | old_sha1 = NULL; | |
325 | } | |
eca35a25 | 326 | if (delete_ref(name, old_sha1, 0)) { |
8aaf7d64 SP |
327 | error("failed to delete %s", name); |
328 | return "failed to delete"; | |
d4f694ba | 329 | } |
8aaf7d64 | 330 | return NULL; /* good */ |
d4f694ba JH |
331 | } |
332 | else { | |
68db31cc | 333 | lock = lock_any_ref_for_update(name, old_sha1, 0); |
d4f694ba | 334 | if (!lock) { |
8aaf7d64 SP |
335 | error("failed to lock %s", name); |
336 | return "failed to lock"; | |
d4f694ba | 337 | } |
ef203f08 | 338 | if (write_ref_sha1(lock, new_sha1, "push")) { |
8aaf7d64 | 339 | return "failed to write"; /* error() already called */ |
ef203f08 | 340 | } |
8aaf7d64 | 341 | return NULL; /* good */ |
19614330 | 342 | } |
2eca23da LT |
343 | } |
344 | ||
19614330 JH |
345 | static char update_post_hook[] = "hooks/post-update"; |
346 | ||
347 | static void run_update_post_hook(struct command *cmd) | |
348 | { | |
349 | struct command *cmd_p; | |
350 | int argc; | |
9201c707 | 351 | const char **argv; |
19614330 | 352 | |
3e6e152c | 353 | for (argc = 0, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) { |
cfee10a7 | 354 | if (cmd_p->error_string) |
19614330 JH |
355 | continue; |
356 | argc++; | |
357 | } | |
3e6e152c SP |
358 | if (!argc || access(update_post_hook, X_OK) < 0) |
359 | return; | |
360 | argv = xmalloc(sizeof(*argv) * (2 + argc)); | |
19614330 JH |
361 | argv[0] = update_post_hook; |
362 | ||
363 | for (argc = 1, cmd_p = cmd; cmd_p; cmd_p = cmd_p->next) { | |
9201c707 | 364 | char *p; |
cfee10a7 | 365 | if (cmd_p->error_string) |
19614330 | 366 | continue; |
9201c707 JH |
367 | p = xmalloc(strlen(cmd_p->ref_name) + 1); |
368 | strcpy(p, cmd_p->ref_name); | |
369 | argv[argc] = p; | |
19614330 JH |
370 | argc++; |
371 | } | |
372 | argv[argc] = NULL; | |
95d3c4f5 SP |
373 | run_command_v_opt(argv, RUN_COMMAND_NO_STDIN |
374 | | RUN_COMMAND_STDOUT_TO_STDERR); | |
19614330 | 375 | } |
2eca23da | 376 | |
8aaf7d64 | 377 | static void execute_commands(const char *unpacker_error) |
575f4974 | 378 | { |
eb1af2df | 379 | struct command *cmd = commands; |
8aaf7d64 SP |
380 | |
381 | if (unpacker_error) { | |
382 | while (cmd) { | |
383 | cmd->error_string = "n/a (unpacker error)"; | |
384 | cmd = cmd->next; | |
385 | } | |
386 | return; | |
387 | } | |
388 | ||
ae98a008 | 389 | if (run_receive_hook(pre_receive_hook)) { |
05ef58ec SP |
390 | while (cmd) { |
391 | cmd->error_string = "pre-receive hook declined"; | |
392 | cmd = cmd->next; | |
393 | } | |
394 | return; | |
395 | } | |
396 | ||
eb1af2df | 397 | while (cmd) { |
8aaf7d64 | 398 | cmd->error_string = update(cmd); |
eb1af2df | 399 | cmd = cmd->next; |
575f4974 LT |
400 | } |
401 | } | |
402 | ||
403 | static void read_head_info(void) | |
404 | { | |
eb1af2df | 405 | struct command **p = &commands; |
575f4974 LT |
406 | for (;;) { |
407 | static char line[1000]; | |
eb1af2df LT |
408 | unsigned char old_sha1[20], new_sha1[20]; |
409 | struct command *cmd; | |
cfee10a7 JH |
410 | char *refname; |
411 | int len, reflen; | |
eb1af2df LT |
412 | |
413 | len = packet_read_line(0, line, sizeof(line)); | |
575f4974 LT |
414 | if (!len) |
415 | break; | |
eb1af2df LT |
416 | if (line[len-1] == '\n') |
417 | line[--len] = 0; | |
418 | if (len < 83 || | |
419 | line[40] != ' ' || | |
420 | line[81] != ' ' || | |
421 | get_sha1_hex(line, old_sha1) || | |
422 | get_sha1_hex(line + 41, new_sha1)) | |
cfee10a7 JH |
423 | die("protocol error: expected old/new/ref, got '%s'", |
424 | line); | |
425 | ||
426 | refname = line + 82; | |
427 | reflen = strlen(refname); | |
428 | if (reflen + 82 < len) { | |
429 | if (strstr(refname + reflen + 1, "report-status")) | |
430 | report_status = 1; | |
431 | } | |
eb1af2df | 432 | cmd = xmalloc(sizeof(struct command) + len - 80); |
e702496e SP |
433 | hashcpy(cmd->old_sha1, old_sha1); |
434 | hashcpy(cmd->new_sha1, new_sha1); | |
eb1af2df | 435 | memcpy(cmd->ref_name, line + 82, len - 81); |
8aaf7d64 | 436 | cmd->error_string = NULL; |
eb1af2df LT |
437 | cmd->next = NULL; |
438 | *p = cmd; | |
439 | p = &cmd->next; | |
575f4974 LT |
440 | } |
441 | } | |
442 | ||
fc04c412 SP |
443 | static const char *parse_pack_header(struct pack_header *hdr) |
444 | { | |
a69e5429 JH |
445 | switch (read_pack_header(0, hdr)) { |
446 | case PH_ERROR_EOF: | |
447 | return "eof before pack header was fully read"; | |
448 | ||
449 | case PH_ERROR_PACK_SIGNATURE: | |
fc04c412 | 450 | return "protocol error (pack signature mismatch detected)"; |
a69e5429 JH |
451 | |
452 | case PH_ERROR_PROTOCOL: | |
fc04c412 | 453 | return "protocol error (pack version unsupported)"; |
a69e5429 JH |
454 | |
455 | default: | |
456 | return "unknown error in parse_pack_header"; | |
457 | ||
458 | case 0: | |
459 | return NULL; | |
460 | } | |
fc04c412 SP |
461 | } |
462 | ||
576162a4 NP |
463 | static const char *pack_lockfile; |
464 | ||
861ed121 | 465 | static const char *unpack(void) |
575f4974 | 466 | { |
fc04c412 SP |
467 | struct pack_header hdr; |
468 | const char *hdr_err; | |
469 | char hdr_arg[38]; | |
fc04c412 SP |
470 | |
471 | hdr_err = parse_pack_header(&hdr); | |
472 | if (hdr_err) | |
473 | return hdr_err; | |
6e1c2344 RJ |
474 | snprintf(hdr_arg, sizeof(hdr_arg), |
475 | "--pack_header=%"PRIu32",%"PRIu32, | |
fc04c412 SP |
476 | ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries)); |
477 | ||
478 | if (ntohl(hdr.hdr_entries) < unpack_limit) { | |
20dc0016 MK |
479 | int code, i = 0; |
480 | const char *unpacker[4]; | |
481 | unpacker[i++] = "unpack-objects"; | |
482 | if (receive_fsck_objects) | |
483 | unpacker[i++] = "--strict"; | |
484 | unpacker[i++] = hdr_arg; | |
485 | unpacker[i++] = NULL; | |
9b0b5093 | 486 | code = run_command_v_opt(unpacker, RUN_GIT_CMD); |
576162a4 | 487 | switch (code) { |
fc04c412 SP |
488 | case 0: |
489 | return NULL; | |
490 | case -ERR_RUN_COMMAND_FORK: | |
491 | return "unpack fork failed"; | |
492 | case -ERR_RUN_COMMAND_EXEC: | |
493 | return "unpack execute failed"; | |
494 | case -ERR_RUN_COMMAND_WAITPID: | |
495 | return "waitpid failed"; | |
496 | case -ERR_RUN_COMMAND_WAITPID_WRONG_PID: | |
497 | return "waitpid is confused"; | |
498 | case -ERR_RUN_COMMAND_WAITPID_SIGNAL: | |
499 | return "unpacker died of signal"; | |
500 | case -ERR_RUN_COMMAND_WAITPID_NOEXIT: | |
501 | return "unpacker died strangely"; | |
502 | default: | |
503 | return "unpacker exited with error code"; | |
576162a4 NP |
504 | } |
505 | } else { | |
20dc0016 MK |
506 | const char *keeper[7]; |
507 | int s, status, i = 0; | |
576162a4 | 508 | char keep_arg[256]; |
e8016abf | 509 | struct child_process ip; |
576162a4 | 510 | |
85e72830 | 511 | s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid()); |
576162a4 NP |
512 | if (gethostname(keep_arg + s, sizeof(keep_arg) - s)) |
513 | strcpy(keep_arg + s, "localhost"); | |
514 | ||
20dc0016 MK |
515 | keeper[i++] = "index-pack"; |
516 | keeper[i++] = "--stdin"; | |
517 | if (receive_fsck_objects) | |
518 | keeper[i++] = "--strict"; | |
519 | keeper[i++] = "--fix-thin"; | |
520 | keeper[i++] = hdr_arg; | |
521 | keeper[i++] = keep_arg; | |
522 | keeper[i++] = NULL; | |
e8016abf SP |
523 | memset(&ip, 0, sizeof(ip)); |
524 | ip.argv = keeper; | |
525 | ip.out = -1; | |
526 | ip.git_cmd = 1; | |
527 | if (start_command(&ip)) | |
576162a4 | 528 | return "index-pack fork failed"; |
106764e6 | 529 | pack_lockfile = index_pack_lockfile(ip.out); |
e72ae288 | 530 | close(ip.out); |
e8016abf SP |
531 | status = finish_command(&ip); |
532 | if (!status) { | |
576162a4 NP |
533 | reprepare_packed_git(); |
534 | return NULL; | |
535 | } | |
536 | return "index-pack abnormal exit"; | |
cfee10a7 JH |
537 | } |
538 | } | |
539 | ||
540 | static void report(const char *unpack_status) | |
541 | { | |
542 | struct command *cmd; | |
543 | packet_write(1, "unpack %s\n", | |
544 | unpack_status ? unpack_status : "ok"); | |
545 | for (cmd = commands; cmd; cmd = cmd->next) { | |
546 | if (!cmd->error_string) | |
547 | packet_write(1, "ok %s\n", | |
548 | cmd->ref_name); | |
549 | else | |
550 | packet_write(1, "ng %s %s\n", | |
551 | cmd->ref_name, cmd->error_string); | |
575f4974 | 552 | } |
cfee10a7 | 553 | packet_flush(1); |
575f4974 LT |
554 | } |
555 | ||
d4f694ba JH |
556 | static int delete_only(struct command *cmd) |
557 | { | |
558 | while (cmd) { | |
559 | if (!is_null_sha1(cmd->new_sha1)) | |
560 | return 0; | |
561 | cmd = cmd->next; | |
562 | } | |
563 | return 1; | |
564 | } | |
565 | ||
d79796bc JH |
566 | static int add_refs_from_alternate(struct alternate_object_database *e, void *unused) |
567 | { | |
b8492539 JH |
568 | char *other; |
569 | size_t len; | |
d79796bc JH |
570 | struct remote *remote; |
571 | struct transport *transport; | |
572 | const struct ref *extra; | |
573 | ||
b8492539 JH |
574 | e->name[-1] = '\0'; |
575 | other = xstrdup(make_absolute_path(e->base)); | |
576 | e->name[-1] = '/'; | |
577 | len = strlen(other); | |
578 | ||
d79796bc JH |
579 | while (other[len-1] == '/') |
580 | other[--len] = '\0'; | |
581 | if (len < 8 || memcmp(other + len - 8, "/objects", 8)) | |
582 | return 0; | |
583 | /* Is this a git repository with refs? */ | |
584 | memcpy(other + len - 8, "/refs", 6); | |
585 | if (!is_directory(other)) | |
586 | return 0; | |
587 | other[len - 8] = '\0'; | |
588 | remote = remote_get(other); | |
589 | transport = transport_get(remote, other); | |
590 | for (extra = transport_get_remote_refs(transport); | |
591 | extra; | |
592 | extra = extra->next) { | |
593 | add_extra_ref(".have", extra->old_sha1, 0); | |
594 | } | |
595 | transport_disconnect(transport); | |
596 | free(other); | |
597 | return 0; | |
598 | } | |
599 | ||
600 | static void add_alternate_refs(void) | |
601 | { | |
602 | foreach_alt_odb(add_refs_from_alternate, NULL); | |
603 | } | |
604 | ||
be5908ae | 605 | int cmd_receive_pack(int argc, const char **argv, const char *prefix) |
575f4974 | 606 | { |
d0efc8a7 | 607 | int i; |
8d630132 | 608 | char *dir = NULL; |
575f4974 LT |
609 | |
610 | argv++; | |
611 | for (i = 1; i < argc; i++) { | |
be5908ae | 612 | const char *arg = *argv++; |
575f4974 LT |
613 | |
614 | if (*arg == '-') { | |
575f4974 LT |
615 | /* Do flag handling here */ |
616 | usage(receive_pack_usage); | |
617 | } | |
d0efc8a7 LT |
618 | if (dir) |
619 | usage(receive_pack_usage); | |
be5908ae | 620 | dir = xstrdup(arg); |
575f4974 LT |
621 | } |
622 | if (!dir) | |
623 | usage(receive_pack_usage); | |
624 | ||
e1464ca7 | 625 | setup_path(); |
5c09f321 | 626 | |
3159c8dc | 627 | if (!enter_repo(dir, 0)) |
8d630132 | 628 | die("'%s': unable to chdir or not a git archive", dir); |
575f4974 | 629 | |
a0022eeb JH |
630 | if (is_repository_shallow()) |
631 | die("attempt to push into a shallow repository"); | |
632 | ||
ef90d6d4 | 633 | git_config(receive_pack_config, NULL); |
6fb75bed | 634 | |
e28714c5 JH |
635 | if (0 <= transfer_unpack_limit) |
636 | unpack_limit = transfer_unpack_limit; | |
637 | else if (0 <= receive_unpack_limit) | |
638 | unpack_limit = receive_unpack_limit; | |
639 | ||
d79796bc | 640 | add_alternate_refs(); |
8a65ff76 | 641 | write_head_info(); |
d79796bc | 642 | clear_extra_refs(); |
575f4974 LT |
643 | |
644 | /* EOF */ | |
f3a3214e | 645 | packet_flush(1); |
575f4974 LT |
646 | |
647 | read_head_info(); | |
7f8e9828 | 648 | if (commands) { |
d4f694ba JH |
649 | const char *unpack_status = NULL; |
650 | ||
651 | if (!delete_only(commands)) | |
652 | unpack_status = unpack(); | |
8aaf7d64 | 653 | execute_commands(unpack_status); |
576162a4 NP |
654 | if (pack_lockfile) |
655 | unlink(pack_lockfile); | |
cfee10a7 JH |
656 | if (report_status) |
657 | report(unpack_status); | |
ae98a008 | 658 | run_receive_hook(post_receive_hook); |
8e663d9e | 659 | run_update_post_hook(commands); |
7f8e9828 | 660 | } |
575f4974 LT |
661 | return 0; |
662 | } |