]>
Commit | Line | Data |
---|---|---|
1e4cd68c | 1 | #include "builtin.h" |
697cc8ef | 2 | #include "lockfile.h" |
fc04c412 | 3 | #include "pack.h" |
8a65ff76 | 4 | #include "refs.h" |
f3a3214e | 5 | #include "pkt-line.h" |
38a81b4e | 6 | #include "sideband.h" |
b1bf95bb | 7 | #include "run-command.h" |
576162a4 | 8 | #include "exec_cmd.h" |
11031d7e JS |
9 | #include "commit.h" |
10 | #include "object.h" | |
d79796bc | 11 | #include "remote.h" |
47a59185 | 12 | #include "connect.h" |
d79796bc | 13 | #include "transport.h" |
da3efdb1 | 14 | #include "string-list.h" |
cff38a5e | 15 | #include "sha1-array.h" |
52fed6e1 | 16 | #include "connected.h" |
31c42bff | 17 | #include "argv-array.h" |
ff5effdf | 18 | #include "version.h" |
ec7dbd14 | 19 | #include "sigchain.h" |
575f4974 | 20 | |
34263de0 | 21 | static const char receive_pack_usage[] = "git receive-pack <git-dir>"; |
575f4974 | 22 | |
986e8239 | 23 | enum deny_action { |
3d95d92b | 24 | DENY_UNCONFIGURED, |
986e8239 JK |
25 | DENY_IGNORE, |
26 | DENY_WARN, | |
4b05548f | 27 | DENY_REFUSE |
986e8239 JK |
28 | }; |
29 | ||
1b53a076 JH |
30 | static int deny_deletes; |
31 | static int deny_non_fast_forwards; | |
3d95d92b | 32 | static enum deny_action deny_current_branch = DENY_UNCONFIGURED; |
747ca245 | 33 | static enum deny_action deny_delete_current = DENY_UNCONFIGURED; |
dab76d3a JH |
34 | static int receive_fsck_objects = -1; |
35 | static int transfer_fsck_objects = -1; | |
e28714c5 JH |
36 | static int receive_unpack_limit = -1; |
37 | static int transfer_unpack_limit = -1; | |
46732fae | 38 | static int unpack_limit = 100; |
96f1e58f | 39 | static int report_status; |
38a81b4e | 40 | static int use_sideband; |
c207e34f | 41 | static int quiet; |
b74fce16 | 42 | static int prefer_ofs_delta = 1; |
77e3efbf JH |
43 | static int auto_update_server_info; |
44 | static int auto_gc = 1; | |
f7c815c3 | 45 | static int fix_thin = 1; |
747ca245 | 46 | static const char *head_name; |
96ec7b1e | 47 | static void *head_name_to_free; |
185c04e0 | 48 | static int sent_capabilities; |
0a1bc12b | 49 | static int shallow_update; |
5dbd7676 | 50 | static const char *alt_shallow_file; |
cfee10a7 | 51 | |
986e8239 JK |
52 | static enum deny_action parse_deny_action(const char *var, const char *value) |
53 | { | |
54 | if (value) { | |
55 | if (!strcasecmp(value, "ignore")) | |
56 | return DENY_IGNORE; | |
57 | if (!strcasecmp(value, "warn")) | |
58 | return DENY_WARN; | |
59 | if (!strcasecmp(value, "refuse")) | |
60 | return DENY_REFUSE; | |
61 | } | |
62 | if (git_config_bool(var, value)) | |
63 | return DENY_REFUSE; | |
64 | return DENY_IGNORE; | |
65 | } | |
66 | ||
ef90d6d4 | 67 | static int receive_pack_config(const char *var, const char *value, void *cb) |
6fb75bed | 68 | { |
daebaa78 JH |
69 | int status = parse_hide_refs_config(var, value, "receive"); |
70 | ||
71 | if (status) | |
72 | return status; | |
73 | ||
a240de11 JK |
74 | if (strcmp(var, "receive.denydeletes") == 0) { |
75 | deny_deletes = git_config_bool(var, value); | |
76 | return 0; | |
77 | } | |
78 | ||
e28714c5 | 79 | if (strcmp(var, "receive.denynonfastforwards") == 0) { |
6fb75bed SP |
80 | deny_non_fast_forwards = git_config_bool(var, value); |
81 | return 0; | |
82 | } | |
83 | ||
e28714c5 JH |
84 | if (strcmp(var, "receive.unpacklimit") == 0) { |
85 | receive_unpack_limit = git_config_int(var, value); | |
fc04c412 SP |
86 | return 0; |
87 | } | |
88 | ||
e28714c5 JH |
89 | if (strcmp(var, "transfer.unpacklimit") == 0) { |
90 | transfer_unpack_limit = git_config_int(var, value); | |
91 | return 0; | |
92 | } | |
93 | ||
20dc0016 MK |
94 | if (strcmp(var, "receive.fsckobjects") == 0) { |
95 | receive_fsck_objects = git_config_bool(var, value); | |
96 | return 0; | |
97 | } | |
98 | ||
dab76d3a JH |
99 | if (strcmp(var, "transfer.fsckobjects") == 0) { |
100 | transfer_fsck_objects = git_config_bool(var, value); | |
101 | return 0; | |
102 | } | |
103 | ||
986e8239 JK |
104 | if (!strcmp(var, "receive.denycurrentbranch")) { |
105 | deny_current_branch = parse_deny_action(var, value); | |
106 | return 0; | |
107 | } | |
108 | ||
747ca245 JH |
109 | if (strcmp(var, "receive.denydeletecurrent") == 0) { |
110 | deny_delete_current = parse_deny_action(var, value); | |
111 | return 0; | |
112 | } | |
113 | ||
b74fce16 NP |
114 | if (strcmp(var, "repack.usedeltabaseoffset") == 0) { |
115 | prefer_ofs_delta = git_config_bool(var, value); | |
116 | return 0; | |
117 | } | |
118 | ||
77e3efbf JH |
119 | if (strcmp(var, "receive.updateserverinfo") == 0) { |
120 | auto_update_server_info = git_config_bool(var, value); | |
121 | return 0; | |
122 | } | |
123 | ||
124 | if (strcmp(var, "receive.autogc") == 0) { | |
125 | auto_gc = git_config_bool(var, value); | |
126 | return 0; | |
127 | } | |
128 | ||
0a1bc12b NTND |
129 | if (strcmp(var, "receive.shallowupdate") == 0) { |
130 | shallow_update = git_config_bool(var, value); | |
131 | return 0; | |
132 | } | |
133 | ||
ef90d6d4 | 134 | return git_default_config(var, value, cb); |
6fb75bed SP |
135 | } |
136 | ||
bc98201d | 137 | static void show_ref(const char *path, const unsigned char *sha1) |
575f4974 | 138 | { |
daebaa78 JH |
139 | if (ref_is_hidden(path)) |
140 | return; | |
141 | ||
185c04e0 | 142 | if (sent_capabilities) |
cfee10a7 JH |
143 | packet_write(1, "%s %s\n", sha1_to_hex(sha1), path); |
144 | else | |
ff5effdf | 145 | packet_write(1, "%s %s%c%s%s agent=%s\n", |
185c04e0 | 146 | sha1_to_hex(sha1), path, 0, |
c207e34f | 147 | " report-status delete-refs side-band-64k quiet", |
ff5effdf JK |
148 | prefer_ofs_delta ? " ofs-delta" : "", |
149 | git_user_agent_sanitized()); | |
185c04e0 | 150 | sent_capabilities = 1; |
575f4974 LT |
151 | } |
152 | ||
bc98201d | 153 | static int show_ref_cb(const char *path, const unsigned char *sha1, int flag, void *unused) |
6b01ecfe JT |
154 | { |
155 | path = strip_namespace(path); | |
156 | /* | |
157 | * Advertise refs outside our current namespace as ".have" | |
158 | * refs, so that the client can use them to minimize data | |
159 | * transfer but will otherwise ignore them. This happens to | |
160 | * cover ".have" that are thrown in by add_one_alternate_ref() | |
161 | * to mark histories that are complete in our alternates as | |
162 | * well. | |
163 | */ | |
164 | if (!path) | |
165 | path = ".have"; | |
bc98201d MH |
166 | show_ref(path, sha1); |
167 | return 0; | |
6b01ecfe JT |
168 | } |
169 | ||
85f25104 | 170 | static void show_one_alternate_sha1(const unsigned char sha1[20], void *unused) |
b7a025d9 | 171 | { |
85f25104 | 172 | show_ref(".have", sha1); |
b7a025d9 MH |
173 | } |
174 | ||
175 | static void collect_one_alternate_ref(const struct ref *ref, void *data) | |
176 | { | |
177 | struct sha1_array *sa = data; | |
178 | sha1_array_append(sa, ref->old_sha1); | |
6b01ecfe JT |
179 | } |
180 | ||
8a65ff76 | 181 | static void write_head_info(void) |
575f4974 | 182 | { |
b7a025d9 MH |
183 | struct sha1_array sa = SHA1_ARRAY_INIT; |
184 | for_each_alternate_ref(collect_one_alternate_ref, &sa); | |
85f25104 | 185 | sha1_array_for_each_unique(&sa, show_one_alternate_sha1, NULL); |
b7a025d9 | 186 | sha1_array_clear(&sa); |
6b01ecfe | 187 | for_each_ref(show_ref_cb, NULL); |
185c04e0 | 188 | if (!sent_capabilities) |
bc98201d | 189 | show_ref("capabilities^{}", null_sha1); |
cfee10a7 | 190 | |
ad491366 NTND |
191 | advertise_shallow_grafts(1); |
192 | ||
b7a025d9 MH |
193 | /* EOF */ |
194 | packet_flush(1); | |
575f4974 LT |
195 | } |
196 | ||
eb1af2df LT |
197 | struct command { |
198 | struct command *next; | |
cfee10a7 | 199 | const char *error_string; |
160b81ed PYH |
200 | unsigned int skip_update:1, |
201 | did_not_exist:1; | |
5dbd7676 | 202 | int index; |
eb1af2df LT |
203 | unsigned char old_sha1[20]; |
204 | unsigned char new_sha1[20]; | |
8f1d2e6f | 205 | char ref_name[FLEX_ARRAY]; /* more */ |
575f4974 LT |
206 | }; |
207 | ||
466dbc42 SP |
208 | static void rp_error(const char *err, ...) __attribute__((format (printf, 1, 2))); |
209 | static void rp_warning(const char *err, ...) __attribute__((format (printf, 1, 2))); | |
210 | ||
211 | static void report_message(const char *prefix, const char *err, va_list params) | |
212 | { | |
213 | int sz = strlen(prefix); | |
214 | char msg[4096]; | |
215 | ||
216 | strncpy(msg, prefix, sz); | |
217 | sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params); | |
218 | if (sz > (sizeof(msg) - 1)) | |
219 | sz = sizeof(msg) - 1; | |
220 | msg[sz++] = '\n'; | |
221 | ||
222 | if (use_sideband) | |
223 | send_sideband(1, 2, msg, sz, use_sideband); | |
224 | else | |
225 | xwrite(2, msg, sz); | |
226 | } | |
227 | ||
228 | static void rp_warning(const char *err, ...) | |
229 | { | |
230 | va_list params; | |
231 | va_start(params, err); | |
232 | report_message("warning: ", err, params); | |
233 | va_end(params); | |
234 | } | |
235 | ||
236 | static void rp_error(const char *err, ...) | |
237 | { | |
238 | va_list params; | |
239 | va_start(params, err); | |
240 | report_message("error: ", err, params); | |
241 | va_end(params); | |
242 | } | |
243 | ||
6d525d38 SP |
244 | static int copy_to_sideband(int in, int out, void *arg) |
245 | { | |
246 | char data[128]; | |
247 | while (1) { | |
248 | ssize_t sz = xread(in, data, sizeof(data)); | |
249 | if (sz <= 0) | |
250 | break; | |
251 | send_sideband(1, 2, data, sz, use_sideband); | |
252 | } | |
253 | close(in); | |
254 | return 0; | |
255 | } | |
256 | ||
9684e44a JH |
257 | typedef int (*feed_fn)(void *, const char **, size_t *); |
258 | static int run_and_feed_hook(const char *hook_name, feed_fn feed, void *feed_state) | |
b1bf95bb | 259 | { |
d3180279 | 260 | struct child_process proc = CHILD_PROCESS_INIT; |
6d525d38 | 261 | struct async muxer; |
f43cd49f | 262 | const char *argv[2]; |
9684e44a | 263 | int code; |
b1bf95bb | 264 | |
5a7da2dc AS |
265 | argv[0] = find_hook(hook_name); |
266 | if (!argv[0]) | |
b1bf95bb | 267 | return 0; |
c8dd2771 | 268 | |
f43cd49f SP |
269 | argv[1] = NULL; |
270 | ||
f43cd49f SP |
271 | proc.argv = argv; |
272 | proc.in = -1; | |
273 | proc.stdout_to_stderr = 1; | |
274 | ||
6d525d38 SP |
275 | if (use_sideband) { |
276 | memset(&muxer, 0, sizeof(muxer)); | |
277 | muxer.proc = copy_to_sideband; | |
278 | muxer.in = -1; | |
279 | code = start_async(&muxer); | |
280 | if (code) | |
281 | return code; | |
282 | proc.err = muxer.in; | |
283 | } | |
284 | ||
f43cd49f | 285 | code = start_command(&proc); |
6d525d38 SP |
286 | if (code) { |
287 | if (use_sideband) | |
288 | finish_async(&muxer); | |
90e41a89 | 289 | return code; |
6d525d38 SP |
290 | } |
291 | ||
ec7dbd14 JH |
292 | sigchain_push(SIGPIPE, SIG_IGN); |
293 | ||
9684e44a JH |
294 | while (1) { |
295 | const char *buf; | |
296 | size_t n; | |
297 | if (feed(feed_state, &buf, &n)) | |
298 | break; | |
299 | if (write_in_full(proc.in, buf, n) != n) | |
300 | break; | |
c8dd2771 | 301 | } |
e72ae288 | 302 | close(proc.in); |
6d525d38 SP |
303 | if (use_sideband) |
304 | finish_async(&muxer); | |
ec7dbd14 JH |
305 | |
306 | sigchain_pop(SIGPIPE); | |
307 | ||
90e41a89 | 308 | return finish_command(&proc); |
b1bf95bb JW |
309 | } |
310 | ||
9684e44a JH |
311 | struct receive_hook_feed_state { |
312 | struct command *cmd; | |
cdc2b2f3 | 313 | int skip_broken; |
9684e44a JH |
314 | struct strbuf buf; |
315 | }; | |
316 | ||
317 | static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep) | |
318 | { | |
319 | struct receive_hook_feed_state *state = state_; | |
320 | struct command *cmd = state->cmd; | |
321 | ||
cdc2b2f3 JH |
322 | while (cmd && |
323 | state->skip_broken && (cmd->error_string || cmd->did_not_exist)) | |
9684e44a JH |
324 | cmd = cmd->next; |
325 | if (!cmd) | |
326 | return -1; /* EOF */ | |
327 | strbuf_reset(&state->buf); | |
328 | strbuf_addf(&state->buf, "%s %s %s\n", | |
329 | sha1_to_hex(cmd->old_sha1), sha1_to_hex(cmd->new_sha1), | |
330 | cmd->ref_name); | |
331 | state->cmd = cmd->next; | |
332 | if (bufp) { | |
333 | *bufp = state->buf.buf; | |
334 | *sizep = state->buf.len; | |
335 | } | |
336 | return 0; | |
337 | } | |
338 | ||
cdc2b2f3 JH |
339 | static int run_receive_hook(struct command *commands, const char *hook_name, |
340 | int skip_broken) | |
9684e44a JH |
341 | { |
342 | struct receive_hook_feed_state state; | |
343 | int status; | |
344 | ||
345 | strbuf_init(&state.buf, 0); | |
346 | state.cmd = commands; | |
cdc2b2f3 | 347 | state.skip_broken = skip_broken; |
9684e44a JH |
348 | if (feed_receive_hook(&state, NULL, NULL)) |
349 | return 0; | |
350 | state.cmd = commands; | |
351 | status = run_and_feed_hook(hook_name, feed_receive_hook, &state); | |
352 | strbuf_release(&state.buf); | |
353 | return status; | |
354 | } | |
355 | ||
1d9e8b56 SP |
356 | static int run_update_hook(struct command *cmd) |
357 | { | |
1d9e8b56 | 358 | const char *argv[5]; |
d3180279 | 359 | struct child_process proc = CHILD_PROCESS_INIT; |
6d525d38 | 360 | int code; |
1d9e8b56 | 361 | |
5a7da2dc AS |
362 | argv[0] = find_hook("update"); |
363 | if (!argv[0]) | |
1d9e8b56 SP |
364 | return 0; |
365 | ||
1d9e8b56 SP |
366 | argv[1] = cmd->ref_name; |
367 | argv[2] = sha1_to_hex(cmd->old_sha1); | |
368 | argv[3] = sha1_to_hex(cmd->new_sha1); | |
369 | argv[4] = NULL; | |
370 | ||
6d525d38 SP |
371 | proc.no_stdin = 1; |
372 | proc.stdout_to_stderr = 1; | |
373 | proc.err = use_sideband ? -1 : 0; | |
374 | proc.argv = argv; | |
375 | ||
376 | code = start_command(&proc); | |
377 | if (code) | |
378 | return code; | |
379 | if (use_sideband) | |
380 | copy_to_sideband(proc.err, -1, NULL); | |
381 | return finish_command(&proc); | |
1d9e8b56 SP |
382 | } |
383 | ||
986e8239 JK |
384 | static int is_ref_checked_out(const char *ref) |
385 | { | |
986e8239 JK |
386 | if (is_bare_repository()) |
387 | return 0; | |
388 | ||
747ca245 | 389 | if (!head_name) |
986e8239 | 390 | return 0; |
747ca245 | 391 | return !strcmp(head_name, ref); |
986e8239 JK |
392 | } |
393 | ||
acd2a45b JH |
394 | static char *refuse_unconfigured_deny_msg[] = { |
395 | "By default, updating the current branch in a non-bare repository", | |
396 | "is denied, because it will make the index and work tree inconsistent", | |
397 | "with what you pushed, and will require 'git reset --hard' to match", | |
398 | "the work tree to HEAD.", | |
3d95d92b JH |
399 | "", |
400 | "You can set 'receive.denyCurrentBranch' configuration variable to", | |
acd2a45b JH |
401 | "'ignore' or 'warn' in the remote repository to allow pushing into", |
402 | "its current branch; however, this is not recommended unless you", | |
403 | "arranged to update its work tree to match what you pushed in some", | |
404 | "other way.", | |
3d95d92b | 405 | "", |
acd2a45b JH |
406 | "To squelch this message and still keep the default behaviour, set", |
407 | "'receive.denyCurrentBranch' configuration variable to 'refuse'." | |
3d95d92b JH |
408 | }; |
409 | ||
acd2a45b | 410 | static void refuse_unconfigured_deny(void) |
3d95d92b JH |
411 | { |
412 | int i; | |
acd2a45b | 413 | for (i = 0; i < ARRAY_SIZE(refuse_unconfigured_deny_msg); i++) |
a886ba28 | 414 | rp_error("%s", refuse_unconfigured_deny_msg[i]); |
3d95d92b JH |
415 | } |
416 | ||
375881fa JH |
417 | static char *refuse_unconfigured_deny_delete_current_msg[] = { |
418 | "By default, deleting the current branch is denied, because the next", | |
419 | "'git clone' won't result in any file checked out, causing confusion.", | |
747ca245 JH |
420 | "", |
421 | "You can set 'receive.denyDeleteCurrent' configuration variable to", | |
375881fa JH |
422 | "'warn' or 'ignore' in the remote repository to allow deleting the", |
423 | "current branch, with or without a warning message.", | |
747ca245 | 424 | "", |
375881fa | 425 | "To squelch this message, you can set it to 'refuse'." |
747ca245 JH |
426 | }; |
427 | ||
375881fa | 428 | static void refuse_unconfigured_deny_delete_current(void) |
747ca245 JH |
429 | { |
430 | int i; | |
431 | for (i = 0; | |
375881fa | 432 | i < ARRAY_SIZE(refuse_unconfigured_deny_delete_current_msg); |
747ca245 | 433 | i++) |
a886ba28 | 434 | rp_error("%s", refuse_unconfigured_deny_delete_current_msg[i]); |
747ca245 JH |
435 | } |
436 | ||
0a1bc12b NTND |
437 | static int command_singleton_iterator(void *cb_data, unsigned char sha1[20]); |
438 | static int update_shallow_ref(struct command *cmd, struct shallow_info *si) | |
439 | { | |
440 | static struct lock_file shallow_lock; | |
441 | struct sha1_array extra = SHA1_ARRAY_INIT; | |
442 | const char *alt_file; | |
443 | uint32_t mask = 1 << (cmd->index % 32); | |
444 | int i; | |
445 | ||
6aa30857 | 446 | trace_printf_key(&trace_shallow, |
0a1bc12b NTND |
447 | "shallow: update_shallow_ref %s\n", cmd->ref_name); |
448 | for (i = 0; i < si->shallow->nr; i++) | |
449 | if (si->used_shallow[i] && | |
450 | (si->used_shallow[i][cmd->index / 32] & mask) && | |
451 | !delayed_reachability_test(si, i)) | |
452 | sha1_array_append(&extra, si->shallow->sha1[i]); | |
453 | ||
454 | setup_alternate_shallow(&shallow_lock, &alt_file, &extra); | |
455 | if (check_shallow_connected(command_singleton_iterator, | |
456 | 0, cmd, alt_file)) { | |
457 | rollback_lock_file(&shallow_lock); | |
458 | sha1_array_clear(&extra); | |
459 | return -1; | |
460 | } | |
461 | ||
462 | commit_lock_file(&shallow_lock); | |
463 | ||
464 | /* | |
465 | * Make sure setup_alternate_shallow() for the next ref does | |
466 | * not lose these new roots.. | |
467 | */ | |
468 | for (i = 0; i < extra.nr; i++) | |
469 | register_shallow(extra.sha1[i]); | |
470 | ||
471 | si->shallow_ref[cmd->index] = 0; | |
472 | sha1_array_clear(&extra); | |
473 | return 0; | |
474 | } | |
475 | ||
476 | static const char *update(struct command *cmd, struct shallow_info *si) | |
2eca23da | 477 | { |
cfee10a7 | 478 | const char *name = cmd->ref_name; |
6b01ecfe JT |
479 | struct strbuf namespaced_name_buf = STRBUF_INIT; |
480 | const char *namespaced_name; | |
cfee10a7 JH |
481 | unsigned char *old_sha1 = cmd->old_sha1; |
482 | unsigned char *new_sha1 = cmd->new_sha1; | |
2eca23da | 483 | |
061d6b9a | 484 | /* only refs/... are allowed */ |
59556548 | 485 | if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) { |
466dbc42 | 486 | rp_error("refusing to create funny ref '%s' remotely", name); |
8aaf7d64 | 487 | return "funny refname"; |
cfee10a7 | 488 | } |
d8a1deec | 489 | |
6b01ecfe JT |
490 | strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name); |
491 | namespaced_name = strbuf_detach(&namespaced_name_buf, NULL); | |
492 | ||
493 | if (is_ref_checked_out(namespaced_name)) { | |
3d95d92b JH |
494 | switch (deny_current_branch) { |
495 | case DENY_IGNORE: | |
986e8239 | 496 | break; |
3d95d92b | 497 | case DENY_WARN: |
466dbc42 | 498 | rp_warning("updating the current branch"); |
986e8239 | 499 | break; |
3d95d92b | 500 | case DENY_REFUSE: |
acd2a45b | 501 | case DENY_UNCONFIGURED: |
466dbc42 | 502 | rp_error("refusing to update checked out branch: %s", name); |
acd2a45b JH |
503 | if (deny_current_branch == DENY_UNCONFIGURED) |
504 | refuse_unconfigured_deny(); | |
3d95d92b JH |
505 | return "branch is currently checked out"; |
506 | } | |
986e8239 JK |
507 | } |
508 | ||
d4f694ba | 509 | if (!is_null_sha1(new_sha1) && !has_sha1_file(new_sha1)) { |
8aaf7d64 SP |
510 | error("unpack should have generated %s, " |
511 | "but I can't find it!", sha1_to_hex(new_sha1)); | |
512 | return "bad pack"; | |
cfee10a7 | 513 | } |
747ca245 JH |
514 | |
515 | if (!is_null_sha1(old_sha1) && is_null_sha1(new_sha1)) { | |
59556548 | 516 | if (deny_deletes && starts_with(name, "refs/heads/")) { |
466dbc42 | 517 | rp_error("denying ref deletion for %s", name); |
747ca245 JH |
518 | return "deletion prohibited"; |
519 | } | |
520 | ||
6b01ecfe | 521 | if (!strcmp(namespaced_name, head_name)) { |
747ca245 JH |
522 | switch (deny_delete_current) { |
523 | case DENY_IGNORE: | |
524 | break; | |
525 | case DENY_WARN: | |
466dbc42 | 526 | rp_warning("deleting the current branch"); |
747ca245 JH |
527 | break; |
528 | case DENY_REFUSE: | |
375881fa JH |
529 | case DENY_UNCONFIGURED: |
530 | if (deny_delete_current == DENY_UNCONFIGURED) | |
531 | refuse_unconfigured_deny_delete_current(); | |
466dbc42 | 532 | rp_error("refusing to delete the current branch: %s", name); |
747ca245 JH |
533 | return "deletion of the current branch prohibited"; |
534 | } | |
535 | } | |
a240de11 | 536 | } |
747ca245 | 537 | |
d4f694ba | 538 | if (deny_non_fast_forwards && !is_null_sha1(new_sha1) && |
ba988a83 | 539 | !is_null_sha1(old_sha1) && |
59556548 | 540 | starts_with(name, "refs/heads/")) { |
eab82707 | 541 | struct object *old_object, *new_object; |
11031d7e | 542 | struct commit *old_commit, *new_commit; |
11031d7e | 543 | |
eab82707 MK |
544 | old_object = parse_object(old_sha1); |
545 | new_object = parse_object(new_sha1); | |
546 | ||
547 | if (!old_object || !new_object || | |
548 | old_object->type != OBJ_COMMIT || | |
549 | new_object->type != OBJ_COMMIT) { | |
550 | error("bad sha1 objects for %s", name); | |
551 | return "bad ref"; | |
552 | } | |
553 | old_commit = (struct commit *)old_object; | |
554 | new_commit = (struct commit *)new_object; | |
5d55915c | 555 | if (!in_merge_bases(old_commit, new_commit)) { |
466dbc42 SP |
556 | rp_error("denying non-fast-forward %s" |
557 | " (you should pull first)", name); | |
a75d7b54 | 558 | return "non-fast-forward"; |
8aaf7d64 | 559 | } |
11031d7e | 560 | } |
1d9e8b56 | 561 | if (run_update_hook(cmd)) { |
466dbc42 | 562 | rp_error("hook declined to update %s", name); |
8aaf7d64 | 563 | return "hook declined"; |
b1bf95bb | 564 | } |
3159c8dc | 565 | |
d4f694ba | 566 | if (is_null_sha1(new_sha1)) { |
28391a80 | 567 | if (!parse_object(old_sha1)) { |
28391a80 | 568 | old_sha1 = NULL; |
160b81ed PYH |
569 | if (ref_exists(name)) { |
570 | rp_warning("Allowing deletion of corrupt ref."); | |
571 | } else { | |
572 | rp_warning("Deleting a non-existent ref."); | |
573 | cmd->did_not_exist = 1; | |
574 | } | |
28391a80 | 575 | } |
6b01ecfe | 576 | if (delete_ref(namespaced_name, old_sha1, 0)) { |
466dbc42 | 577 | rp_error("failed to delete %s", name); |
8aaf7d64 | 578 | return "failed to delete"; |
d4f694ba | 579 | } |
8aaf7d64 | 580 | return NULL; /* good */ |
d4f694ba JH |
581 | } |
582 | else { | |
6629ea2d RS |
583 | struct strbuf err = STRBUF_INIT; |
584 | struct ref_transaction *transaction; | |
585 | ||
0a1bc12b NTND |
586 | if (shallow_update && si->shallow_ref[cmd->index] && |
587 | update_shallow_ref(cmd, si)) | |
588 | return "shallow error"; | |
589 | ||
6629ea2d RS |
590 | transaction = ref_transaction_begin(&err); |
591 | if (!transaction || | |
592 | ref_transaction_update(transaction, namespaced_name, | |
593 | new_sha1, old_sha1, 0, 1, &err) || | |
594 | ref_transaction_commit(transaction, "push", &err)) { | |
595 | ref_transaction_free(transaction); | |
596 | ||
597 | rp_error("%s", err.buf); | |
598 | strbuf_release(&err); | |
599 | return "failed to update ref"; | |
ef203f08 | 600 | } |
6629ea2d RS |
601 | |
602 | ref_transaction_free(transaction); | |
603 | strbuf_release(&err); | |
8aaf7d64 | 604 | return NULL; /* good */ |
19614330 | 605 | } |
2eca23da LT |
606 | } |
607 | ||
5e1c71fd | 608 | static void run_update_post_hook(struct command *commands) |
19614330 | 609 | { |
5e1c71fd | 610 | struct command *cmd; |
6d525d38 | 611 | int argc; |
9201c707 | 612 | const char **argv; |
d3180279 | 613 | struct child_process proc = CHILD_PROCESS_INIT; |
5a7da2dc | 614 | char *hook; |
19614330 | 615 | |
5a7da2dc | 616 | hook = find_hook("post-update"); |
5e1c71fd | 617 | for (argc = 0, cmd = commands; cmd; cmd = cmd->next) { |
160b81ed | 618 | if (cmd->error_string || cmd->did_not_exist) |
19614330 JH |
619 | continue; |
620 | argc++; | |
621 | } | |
5a7da2dc | 622 | if (!argc || !hook) |
3e6e152c | 623 | return; |
5a7da2dc | 624 | |
3e6e152c | 625 | argv = xmalloc(sizeof(*argv) * (2 + argc)); |
5a7da2dc | 626 | argv[0] = hook; |
19614330 | 627 | |
5e1c71fd | 628 | for (argc = 1, cmd = commands; cmd; cmd = cmd->next) { |
160b81ed | 629 | if (cmd->error_string || cmd->did_not_exist) |
19614330 | 630 | continue; |
95244ae3 | 631 | argv[argc] = xstrdup(cmd->ref_name); |
19614330 JH |
632 | argc++; |
633 | } | |
634 | argv[argc] = NULL; | |
6d525d38 | 635 | |
6d525d38 SP |
636 | proc.no_stdin = 1; |
637 | proc.stdout_to_stderr = 1; | |
638 | proc.err = use_sideband ? -1 : 0; | |
639 | proc.argv = argv; | |
640 | ||
641 | if (!start_command(&proc)) { | |
642 | if (use_sideband) | |
643 | copy_to_sideband(proc.err, -1, NULL); | |
644 | finish_command(&proc); | |
645 | } | |
19614330 | 646 | } |
2eca23da | 647 | |
da3efdb1 JS |
648 | static void check_aliased_update(struct command *cmd, struct string_list *list) |
649 | { | |
6b01ecfe JT |
650 | struct strbuf buf = STRBUF_INIT; |
651 | const char *dst_name; | |
da3efdb1 JS |
652 | struct string_list_item *item; |
653 | struct command *dst_cmd; | |
654 | unsigned char sha1[20]; | |
655 | char cmd_oldh[41], cmd_newh[41], dst_oldh[41], dst_newh[41]; | |
656 | int flag; | |
657 | ||
6b01ecfe | 658 | strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name); |
8cad4744 | 659 | dst_name = resolve_ref_unsafe(buf.buf, sha1, 0, &flag); |
6b01ecfe | 660 | strbuf_release(&buf); |
da3efdb1 JS |
661 | |
662 | if (!(flag & REF_ISSYMREF)) | |
663 | return; | |
664 | ||
6b01ecfe JT |
665 | dst_name = strip_namespace(dst_name); |
666 | if (!dst_name) { | |
667 | rp_error("refusing update to broken symref '%s'", cmd->ref_name); | |
668 | cmd->skip_update = 1; | |
669 | cmd->error_string = "broken symref"; | |
670 | return; | |
671 | } | |
672 | ||
e8c8b713 | 673 | if ((item = string_list_lookup(list, dst_name)) == NULL) |
da3efdb1 JS |
674 | return; |
675 | ||
676 | cmd->skip_update = 1; | |
677 | ||
678 | dst_cmd = (struct command *) item->util; | |
679 | ||
680 | if (!hashcmp(cmd->old_sha1, dst_cmd->old_sha1) && | |
681 | !hashcmp(cmd->new_sha1, dst_cmd->new_sha1)) | |
682 | return; | |
683 | ||
684 | dst_cmd->skip_update = 1; | |
685 | ||
686 | strcpy(cmd_oldh, find_unique_abbrev(cmd->old_sha1, DEFAULT_ABBREV)); | |
0e71bc30 | 687 | strcpy(cmd_newh, find_unique_abbrev(cmd->new_sha1, DEFAULT_ABBREV)); |
da3efdb1 | 688 | strcpy(dst_oldh, find_unique_abbrev(dst_cmd->old_sha1, DEFAULT_ABBREV)); |
0e71bc30 | 689 | strcpy(dst_newh, find_unique_abbrev(dst_cmd->new_sha1, DEFAULT_ABBREV)); |
da3efdb1 JS |
690 | rp_error("refusing inconsistent update between symref '%s' (%s..%s) and" |
691 | " its target '%s' (%s..%s)", | |
692 | cmd->ref_name, cmd_oldh, cmd_newh, | |
693 | dst_cmd->ref_name, dst_oldh, dst_newh); | |
694 | ||
695 | cmd->error_string = dst_cmd->error_string = | |
696 | "inconsistent aliased update"; | |
697 | } | |
698 | ||
699 | static void check_aliased_updates(struct command *commands) | |
700 | { | |
701 | struct command *cmd; | |
183113a5 | 702 | struct string_list ref_list = STRING_LIST_INIT_NODUP; |
da3efdb1 JS |
703 | |
704 | for (cmd = commands; cmd; cmd = cmd->next) { | |
705 | struct string_list_item *item = | |
1d2f80fa | 706 | string_list_append(&ref_list, cmd->ref_name); |
da3efdb1 JS |
707 | item->util = (void *)cmd; |
708 | } | |
709 | sort_string_list(&ref_list); | |
710 | ||
ef7e93d9 CB |
711 | for (cmd = commands; cmd; cmd = cmd->next) { |
712 | if (!cmd->error_string) | |
713 | check_aliased_update(cmd, &ref_list); | |
714 | } | |
da3efdb1 JS |
715 | |
716 | string_list_clear(&ref_list, 0); | |
717 | } | |
718 | ||
52fed6e1 JH |
719 | static int command_singleton_iterator(void *cb_data, unsigned char sha1[20]) |
720 | { | |
721 | struct command **cmd_list = cb_data; | |
722 | struct command *cmd = *cmd_list; | |
723 | ||
ee6dfb2d | 724 | if (!cmd || is_null_sha1(cmd->new_sha1)) |
52fed6e1 JH |
725 | return -1; /* end of list */ |
726 | *cmd_list = NULL; /* this returns only one */ | |
727 | hashcpy(sha1, cmd->new_sha1); | |
728 | return 0; | |
729 | } | |
730 | ||
0a1bc12b NTND |
731 | static void set_connectivity_errors(struct command *commands, |
732 | struct shallow_info *si) | |
52fed6e1 JH |
733 | { |
734 | struct command *cmd; | |
735 | ||
736 | for (cmd = commands; cmd; cmd = cmd->next) { | |
737 | struct command *singleton = cmd; | |
0a1bc12b NTND |
738 | if (shallow_update && si->shallow_ref[cmd->index]) |
739 | /* to be checked in update_shallow_ref() */ | |
740 | continue; | |
52fed6e1 JH |
741 | if (!check_everything_connected(command_singleton_iterator, |
742 | 0, &singleton)) | |
743 | continue; | |
744 | cmd->error_string = "missing necessary objects"; | |
745 | } | |
746 | } | |
747 | ||
0a1bc12b NTND |
748 | struct iterate_data { |
749 | struct command *cmds; | |
750 | struct shallow_info *si; | |
751 | }; | |
752 | ||
52fed6e1 JH |
753 | static int iterate_receive_command_list(void *cb_data, unsigned char sha1[20]) |
754 | { | |
0a1bc12b NTND |
755 | struct iterate_data *data = cb_data; |
756 | struct command **cmd_list = &data->cmds; | |
52fed6e1 JH |
757 | struct command *cmd = *cmd_list; |
758 | ||
0a1bc12b NTND |
759 | for (; cmd; cmd = cmd->next) { |
760 | if (shallow_update && data->si->shallow_ref[cmd->index]) | |
761 | /* to be checked in update_shallow_ref() */ | |
762 | continue; | |
5dbd7676 | 763 | if (!is_null_sha1(cmd->new_sha1) && !cmd->skip_update) { |
ee6dfb2d JH |
764 | hashcpy(sha1, cmd->new_sha1); |
765 | *cmd_list = cmd->next; | |
766 | return 0; | |
767 | } | |
ee6dfb2d JH |
768 | } |
769 | *cmd_list = NULL; | |
770 | return -1; /* end of list */ | |
52fed6e1 JH |
771 | } |
772 | ||
daebaa78 JH |
773 | static void reject_updates_to_hidden(struct command *commands) |
774 | { | |
775 | struct command *cmd; | |
776 | ||
777 | for (cmd = commands; cmd; cmd = cmd->next) { | |
778 | if (cmd->error_string || !ref_is_hidden(cmd->ref_name)) | |
779 | continue; | |
780 | if (is_null_sha1(cmd->new_sha1)) | |
781 | cmd->error_string = "deny deleting a hidden ref"; | |
782 | else | |
783 | cmd->error_string = "deny updating a hidden ref"; | |
784 | } | |
785 | } | |
786 | ||
0a1bc12b NTND |
787 | static void execute_commands(struct command *commands, |
788 | const char *unpacker_error, | |
789 | struct shallow_info *si) | |
575f4974 | 790 | { |
0a1bc12b | 791 | int checked_connectivity; |
5e1c71fd | 792 | struct command *cmd; |
747ca245 | 793 | unsigned char sha1[20]; |
0a1bc12b | 794 | struct iterate_data data; |
8aaf7d64 SP |
795 | |
796 | if (unpacker_error) { | |
5e1c71fd | 797 | for (cmd = commands; cmd; cmd = cmd->next) |
74eb32d3 | 798 | cmd->error_string = "unpacker error"; |
8aaf7d64 SP |
799 | return; |
800 | } | |
801 | ||
0a1bc12b NTND |
802 | data.cmds = commands; |
803 | data.si = si; | |
804 | if (check_everything_connected(iterate_receive_command_list, 0, &data)) | |
805 | set_connectivity_errors(commands, si); | |
52fed6e1 | 806 | |
daebaa78 JH |
807 | reject_updates_to_hidden(commands); |
808 | ||
5a7da2dc | 809 | if (run_receive_hook(commands, "pre-receive", 0)) { |
ef7e93d9 CB |
810 | for (cmd = commands; cmd; cmd = cmd->next) { |
811 | if (!cmd->error_string) | |
812 | cmd->error_string = "pre-receive hook declined"; | |
813 | } | |
05ef58ec SP |
814 | return; |
815 | } | |
816 | ||
da3efdb1 JS |
817 | check_aliased_updates(commands); |
818 | ||
96ec7b1e NTND |
819 | free(head_name_to_free); |
820 | head_name = head_name_to_free = resolve_refdup("HEAD", sha1, 0, NULL); | |
747ca245 | 821 | |
0a1bc12b | 822 | checked_connectivity = 1; |
ef7e93d9 CB |
823 | for (cmd = commands; cmd; cmd = cmd->next) { |
824 | if (cmd->error_string) | |
825 | continue; | |
826 | ||
827 | if (cmd->skip_update) | |
828 | continue; | |
829 | ||
0a1bc12b NTND |
830 | cmd->error_string = update(cmd, si); |
831 | if (shallow_update && !cmd->error_string && | |
832 | si->shallow_ref[cmd->index]) { | |
833 | error("BUG: connectivity check has not been run on ref %s", | |
834 | cmd->ref_name); | |
835 | checked_connectivity = 0; | |
836 | } | |
837 | } | |
838 | ||
0179c945 JK |
839 | if (shallow_update && !checked_connectivity) |
840 | error("BUG: run 'git fsck' for safety.\n" | |
841 | "If there are errors, try to remove " | |
842 | "the reported refs above"); | |
575f4974 LT |
843 | } |
844 | ||
5dbd7676 | 845 | static struct command *read_head_info(struct sha1_array *shallow) |
575f4974 | 846 | { |
5e1c71fd | 847 | struct command *commands = NULL; |
eb1af2df | 848 | struct command **p = &commands; |
575f4974 | 849 | for (;;) { |
74543a04 | 850 | char *line; |
eb1af2df LT |
851 | unsigned char old_sha1[20], new_sha1[20]; |
852 | struct command *cmd; | |
cfee10a7 JH |
853 | char *refname; |
854 | int len, reflen; | |
eb1af2df | 855 | |
74543a04 JK |
856 | line = packet_read_line(0, &len); |
857 | if (!line) | |
575f4974 | 858 | break; |
5dbd7676 | 859 | |
92251b1b | 860 | if (len == 48 && starts_with(line, "shallow ")) { |
5dbd7676 NTND |
861 | if (get_sha1_hex(line + 8, old_sha1)) |
862 | die("protocol error: expected shallow sha, got '%s'", line + 8); | |
863 | sha1_array_append(shallow, old_sha1); | |
864 | continue; | |
865 | } | |
866 | ||
eb1af2df LT |
867 | if (len < 83 || |
868 | line[40] != ' ' || | |
869 | line[81] != ' ' || | |
870 | get_sha1_hex(line, old_sha1) || | |
871 | get_sha1_hex(line + 41, new_sha1)) | |
cfee10a7 JH |
872 | die("protocol error: expected old/new/ref, got '%s'", |
873 | line); | |
874 | ||
875 | refname = line + 82; | |
876 | reflen = strlen(refname); | |
877 | if (reflen + 82 < len) { | |
f47182c8 JH |
878 | const char *feature_list = refname + reflen + 1; |
879 | if (parse_feature_request(feature_list, "report-status")) | |
cfee10a7 | 880 | report_status = 1; |
f47182c8 | 881 | if (parse_feature_request(feature_list, "side-band-64k")) |
38a81b4e | 882 | use_sideband = LARGE_PACKET_MAX; |
c207e34f CB |
883 | if (parse_feature_request(feature_list, "quiet")) |
884 | quiet = 1; | |
cfee10a7 | 885 | } |
da3efdb1 | 886 | cmd = xcalloc(1, sizeof(struct command) + len - 80); |
e702496e SP |
887 | hashcpy(cmd->old_sha1, old_sha1); |
888 | hashcpy(cmd->new_sha1, new_sha1); | |
eb1af2df | 889 | memcpy(cmd->ref_name, line + 82, len - 81); |
eb1af2df LT |
890 | *p = cmd; |
891 | p = &cmd->next; | |
575f4974 | 892 | } |
5e1c71fd | 893 | return commands; |
575f4974 LT |
894 | } |
895 | ||
fc04c412 SP |
896 | static const char *parse_pack_header(struct pack_header *hdr) |
897 | { | |
a69e5429 JH |
898 | switch (read_pack_header(0, hdr)) { |
899 | case PH_ERROR_EOF: | |
900 | return "eof before pack header was fully read"; | |
901 | ||
902 | case PH_ERROR_PACK_SIGNATURE: | |
fc04c412 | 903 | return "protocol error (pack signature mismatch detected)"; |
a69e5429 JH |
904 | |
905 | case PH_ERROR_PROTOCOL: | |
fc04c412 | 906 | return "protocol error (pack version unsupported)"; |
a69e5429 JH |
907 | |
908 | default: | |
909 | return "unknown error in parse_pack_header"; | |
910 | ||
911 | case 0: | |
912 | return NULL; | |
913 | } | |
fc04c412 SP |
914 | } |
915 | ||
576162a4 NP |
916 | static const char *pack_lockfile; |
917 | ||
5dbd7676 | 918 | static const char *unpack(int err_fd, struct shallow_info *si) |
575f4974 | 919 | { |
fc04c412 | 920 | struct pack_header hdr; |
31c42bff | 921 | struct argv_array av = ARGV_ARRAY_INIT; |
fc04c412 | 922 | const char *hdr_err; |
31c42bff | 923 | int status; |
fc04c412 | 924 | char hdr_arg[38]; |
d3180279 | 925 | struct child_process child = CHILD_PROCESS_INIT; |
dab76d3a JH |
926 | int fsck_objects = (receive_fsck_objects >= 0 |
927 | ? receive_fsck_objects | |
928 | : transfer_fsck_objects >= 0 | |
929 | ? transfer_fsck_objects | |
930 | : 0); | |
fc04c412 SP |
931 | |
932 | hdr_err = parse_pack_header(&hdr); | |
49ecfa13 JK |
933 | if (hdr_err) { |
934 | if (err_fd > 0) | |
935 | close(err_fd); | |
fc04c412 | 936 | return hdr_err; |
49ecfa13 | 937 | } |
6e1c2344 RJ |
938 | snprintf(hdr_arg, sizeof(hdr_arg), |
939 | "--pack_header=%"PRIu32",%"PRIu32, | |
fc04c412 SP |
940 | ntohl(hdr.hdr_version), ntohl(hdr.hdr_entries)); |
941 | ||
5dbd7676 NTND |
942 | if (si->nr_ours || si->nr_theirs) { |
943 | alt_shallow_file = setup_temporary_shallow(si->shallow); | |
944 | argv_array_pushl(&av, "--shallow-file", alt_shallow_file, NULL); | |
945 | } | |
946 | ||
fc04c412 | 947 | if (ntohl(hdr.hdr_entries) < unpack_limit) { |
31c42bff | 948 | argv_array_pushl(&av, "unpack-objects", hdr_arg, NULL); |
c207e34f | 949 | if (quiet) |
31c42bff | 950 | argv_array_push(&av, "-q"); |
dab76d3a | 951 | if (fsck_objects) |
31c42bff NTND |
952 | argv_array_push(&av, "--strict"); |
953 | child.argv = av.argv; | |
59bfdfb8 | 954 | child.no_stdout = 1; |
a22e6f85 | 955 | child.err = err_fd; |
59bfdfb8 | 956 | child.git_cmd = 1; |
31c42bff NTND |
957 | status = run_command(&child); |
958 | if (status) | |
959 | return "unpack-objects abnormal exit"; | |
576162a4 | 960 | } else { |
31c42bff | 961 | int s; |
576162a4 | 962 | char keep_arg[256]; |
576162a4 | 963 | |
85e72830 | 964 | s = sprintf(keep_arg, "--keep=receive-pack %"PRIuMAX" on ", (uintmax_t) getpid()); |
576162a4 NP |
965 | if (gethostname(keep_arg + s, sizeof(keep_arg) - s)) |
966 | strcpy(keep_arg + s, "localhost"); | |
967 | ||
31c42bff NTND |
968 | argv_array_pushl(&av, "index-pack", |
969 | "--stdin", hdr_arg, keep_arg, NULL); | |
dab76d3a | 970 | if (fsck_objects) |
31c42bff | 971 | argv_array_push(&av, "--strict"); |
f7c815c3 | 972 | if (fix_thin) |
31c42bff NTND |
973 | argv_array_push(&av, "--fix-thin"); |
974 | child.argv = av.argv; | |
975 | child.out = -1; | |
976 | child.err = err_fd; | |
977 | child.git_cmd = 1; | |
978 | status = start_command(&child); | |
979 | if (status) | |
576162a4 | 980 | return "index-pack fork failed"; |
31c42bff NTND |
981 | pack_lockfile = index_pack_lockfile(child.out); |
982 | close(child.out); | |
983 | status = finish_command(&child); | |
984 | if (status) | |
985 | return "index-pack abnormal exit"; | |
986 | reprepare_packed_git(); | |
cfee10a7 | 987 | } |
31c42bff | 988 | return NULL; |
cfee10a7 JH |
989 | } |
990 | ||
5dbd7676 | 991 | static const char *unpack_with_sideband(struct shallow_info *si) |
a22e6f85 JK |
992 | { |
993 | struct async muxer; | |
994 | const char *ret; | |
995 | ||
996 | if (!use_sideband) | |
5dbd7676 | 997 | return unpack(0, si); |
a22e6f85 JK |
998 | |
999 | memset(&muxer, 0, sizeof(muxer)); | |
1000 | muxer.proc = copy_to_sideband; | |
1001 | muxer.in = -1; | |
1002 | if (start_async(&muxer)) | |
1003 | return NULL; | |
1004 | ||
5dbd7676 | 1005 | ret = unpack(muxer.in, si); |
a22e6f85 JK |
1006 | |
1007 | finish_async(&muxer); | |
1008 | return ret; | |
1009 | } | |
1010 | ||
0a1bc12b NTND |
1011 | static void prepare_shallow_update(struct command *commands, |
1012 | struct shallow_info *si) | |
1013 | { | |
1014 | int i, j, k, bitmap_size = (si->ref->nr + 31) / 32; | |
1015 | ||
1016 | si->used_shallow = xmalloc(sizeof(*si->used_shallow) * | |
1017 | si->shallow->nr); | |
1018 | assign_shallow_commits_to_refs(si, si->used_shallow, NULL); | |
1019 | ||
1020 | si->need_reachability_test = | |
1021 | xcalloc(si->shallow->nr, sizeof(*si->need_reachability_test)); | |
1022 | si->reachable = | |
1023 | xcalloc(si->shallow->nr, sizeof(*si->reachable)); | |
1024 | si->shallow_ref = xcalloc(si->ref->nr, sizeof(*si->shallow_ref)); | |
1025 | ||
1026 | for (i = 0; i < si->nr_ours; i++) | |
1027 | si->need_reachability_test[si->ours[i]] = 1; | |
1028 | ||
1029 | for (i = 0; i < si->shallow->nr; i++) { | |
1030 | if (!si->used_shallow[i]) | |
1031 | continue; | |
1032 | for (j = 0; j < bitmap_size; j++) { | |
1033 | if (!si->used_shallow[i][j]) | |
1034 | continue; | |
1035 | si->need_reachability_test[i]++; | |
1036 | for (k = 0; k < 32; k++) | |
1037 | if (si->used_shallow[i][j] & (1 << k)) | |
1038 | si->shallow_ref[j * 32 + k]++; | |
1039 | } | |
1040 | ||
1041 | /* | |
1042 | * true for those associated with some refs and belong | |
1043 | * in "ours" list aka "step 7 not done yet" | |
1044 | */ | |
1045 | si->need_reachability_test[i] = | |
1046 | si->need_reachability_test[i] > 1; | |
1047 | } | |
1048 | ||
1049 | /* | |
1050 | * keep hooks happy by forcing a temporary shallow file via | |
1051 | * env variable because we can't add --shallow-file to every | |
1052 | * command. check_everything_connected() will be done with | |
1053 | * true .git/shallow though. | |
1054 | */ | |
1055 | setenv(GIT_SHALLOW_FILE_ENVIRONMENT, alt_shallow_file, 1); | |
1056 | } | |
1057 | ||
5dbd7676 NTND |
1058 | static void update_shallow_info(struct command *commands, |
1059 | struct shallow_info *si, | |
1060 | struct sha1_array *ref) | |
1061 | { | |
1062 | struct command *cmd; | |
1063 | int *ref_status; | |
1064 | remove_nonexistent_theirs_shallow(si); | |
0a1bc12b NTND |
1065 | if (!si->nr_ours && !si->nr_theirs) { |
1066 | shallow_update = 0; | |
5dbd7676 | 1067 | return; |
0a1bc12b | 1068 | } |
5dbd7676 NTND |
1069 | |
1070 | for (cmd = commands; cmd; cmd = cmd->next) { | |
1071 | if (is_null_sha1(cmd->new_sha1)) | |
1072 | continue; | |
1073 | sha1_array_append(ref, cmd->new_sha1); | |
1074 | cmd->index = ref->nr - 1; | |
1075 | } | |
1076 | si->ref = ref; | |
1077 | ||
0a1bc12b NTND |
1078 | if (shallow_update) { |
1079 | prepare_shallow_update(commands, si); | |
1080 | return; | |
1081 | } | |
1082 | ||
5dbd7676 NTND |
1083 | ref_status = xmalloc(sizeof(*ref_status) * ref->nr); |
1084 | assign_shallow_commits_to_refs(si, NULL, ref_status); | |
1085 | for (cmd = commands; cmd; cmd = cmd->next) { | |
1086 | if (is_null_sha1(cmd->new_sha1)) | |
1087 | continue; | |
1088 | if (ref_status[cmd->index]) { | |
1089 | cmd->error_string = "shallow update not allowed"; | |
1090 | cmd->skip_update = 1; | |
1091 | } | |
1092 | } | |
5dbd7676 NTND |
1093 | free(ref_status); |
1094 | } | |
1095 | ||
5e1c71fd | 1096 | static void report(struct command *commands, const char *unpack_status) |
cfee10a7 JH |
1097 | { |
1098 | struct command *cmd; | |
38a81b4e SP |
1099 | struct strbuf buf = STRBUF_INIT; |
1100 | ||
1101 | packet_buf_write(&buf, "unpack %s\n", | |
1102 | unpack_status ? unpack_status : "ok"); | |
cfee10a7 JH |
1103 | for (cmd = commands; cmd; cmd = cmd->next) { |
1104 | if (!cmd->error_string) | |
38a81b4e SP |
1105 | packet_buf_write(&buf, "ok %s\n", |
1106 | cmd->ref_name); | |
cfee10a7 | 1107 | else |
38a81b4e SP |
1108 | packet_buf_write(&buf, "ng %s %s\n", |
1109 | cmd->ref_name, cmd->error_string); | |
575f4974 | 1110 | } |
38a81b4e SP |
1111 | packet_buf_flush(&buf); |
1112 | ||
1113 | if (use_sideband) | |
1114 | send_sideband(1, 1, buf.buf, buf.len, use_sideband); | |
1115 | else | |
cdf4fb8e | 1116 | write_or_die(1, buf.buf, buf.len); |
38a81b4e | 1117 | strbuf_release(&buf); |
575f4974 LT |
1118 | } |
1119 | ||
5e1c71fd | 1120 | static int delete_only(struct command *commands) |
d4f694ba | 1121 | { |
5e1c71fd JS |
1122 | struct command *cmd; |
1123 | for (cmd = commands; cmd; cmd = cmd->next) { | |
d4f694ba JH |
1124 | if (!is_null_sha1(cmd->new_sha1)) |
1125 | return 0; | |
d4f694ba JH |
1126 | } |
1127 | return 1; | |
1128 | } | |
1129 | ||
be5908ae | 1130 | int cmd_receive_pack(int argc, const char **argv, const char *prefix) |
575f4974 | 1131 | { |
42526b47 SP |
1132 | int advertise_refs = 0; |
1133 | int stateless_rpc = 0; | |
d0efc8a7 | 1134 | int i; |
d51428bf | 1135 | const char *dir = NULL; |
5e1c71fd | 1136 | struct command *commands; |
5dbd7676 NTND |
1137 | struct sha1_array shallow = SHA1_ARRAY_INIT; |
1138 | struct sha1_array ref = SHA1_ARRAY_INIT; | |
1139 | struct shallow_info si; | |
575f4974 | 1140 | |
bbc30f99 JK |
1141 | packet_trace_identity("receive-pack"); |
1142 | ||
575f4974 LT |
1143 | argv++; |
1144 | for (i = 1; i < argc; i++) { | |
be5908ae | 1145 | const char *arg = *argv++; |
575f4974 LT |
1146 | |
1147 | if (*arg == '-') { | |
c207e34f CB |
1148 | if (!strcmp(arg, "--quiet")) { |
1149 | quiet = 1; | |
1150 | continue; | |
1151 | } | |
1152 | ||
42526b47 SP |
1153 | if (!strcmp(arg, "--advertise-refs")) { |
1154 | advertise_refs = 1; | |
1155 | continue; | |
1156 | } | |
1157 | if (!strcmp(arg, "--stateless-rpc")) { | |
1158 | stateless_rpc = 1; | |
1159 | continue; | |
1160 | } | |
f7c815c3 NTND |
1161 | if (!strcmp(arg, "--reject-thin-pack-for-testing")) { |
1162 | fix_thin = 0; | |
1163 | continue; | |
1164 | } | |
42526b47 | 1165 | |
575f4974 LT |
1166 | usage(receive_pack_usage); |
1167 | } | |
d0efc8a7 LT |
1168 | if (dir) |
1169 | usage(receive_pack_usage); | |
d51428bf | 1170 | dir = arg; |
575f4974 LT |
1171 | } |
1172 | if (!dir) | |
1173 | usage(receive_pack_usage); | |
1174 | ||
e1464ca7 | 1175 | setup_path(); |
5c09f321 | 1176 | |
3159c8dc | 1177 | if (!enter_repo(dir, 0)) |
05ac6b34 | 1178 | die("'%s' does not appear to be a git repository", dir); |
575f4974 | 1179 | |
ef90d6d4 | 1180 | git_config(receive_pack_config, NULL); |
6fb75bed | 1181 | |
e28714c5 JH |
1182 | if (0 <= transfer_unpack_limit) |
1183 | unpack_limit = transfer_unpack_limit; | |
1184 | else if (0 <= receive_unpack_limit) | |
1185 | unpack_limit = receive_unpack_limit; | |
1186 | ||
42526b47 | 1187 | if (advertise_refs || !stateless_rpc) { |
42526b47 | 1188 | write_head_info(); |
42526b47 SP |
1189 | } |
1190 | if (advertise_refs) | |
1191 | return 0; | |
575f4974 | 1192 | |
5dbd7676 | 1193 | if ((commands = read_head_info(&shallow)) != NULL) { |
d4f694ba JH |
1194 | const char *unpack_status = NULL; |
1195 | ||
5dbd7676 | 1196 | prepare_shallow_info(&si, &shallow); |
0a1bc12b NTND |
1197 | if (!si.nr_ours && !si.nr_theirs) |
1198 | shallow_update = 0; | |
5dbd7676 NTND |
1199 | if (!delete_only(commands)) { |
1200 | unpack_status = unpack_with_sideband(&si); | |
1201 | update_shallow_info(commands, &si, &ref); | |
1202 | } | |
0a1bc12b | 1203 | execute_commands(commands, unpack_status, &si); |
576162a4 | 1204 | if (pack_lockfile) |
691f1a28 | 1205 | unlink_or_warn(pack_lockfile); |
cfee10a7 | 1206 | if (report_status) |
5e1c71fd | 1207 | report(commands, unpack_status); |
5a7da2dc | 1208 | run_receive_hook(commands, "post-receive", 1); |
8e663d9e | 1209 | run_update_post_hook(commands); |
77e3efbf JH |
1210 | if (auto_gc) { |
1211 | const char *argv_gc_auto[] = { | |
1212 | "gc", "--auto", "--quiet", NULL, | |
1213 | }; | |
4b7f2fa4 JH |
1214 | int opt = RUN_GIT_CMD | RUN_COMMAND_STDOUT_TO_STDERR; |
1215 | run_command_v_opt(argv_gc_auto, opt); | |
77e3efbf JH |
1216 | } |
1217 | if (auto_update_server_info) | |
1218 | update_server_info(0); | |
5dbd7676 | 1219 | clear_shallow_info(&si); |
7f8e9828 | 1220 | } |
38a81b4e SP |
1221 | if (use_sideband) |
1222 | packet_flush(1); | |
5dbd7676 NTND |
1223 | sha1_array_clear(&shallow); |
1224 | sha1_array_clear(&ref); | |
575f4974 LT |
1225 | return 0; |
1226 | } |