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