]>
Commit | Line | Data |
---|---|---|
1e4cd68c | 1 | #include "builtin.h" |
0b027f6c | 2 | #include "abspath.h" |
a49d2834 | 3 | #include "repository.h" |
b2141fc1 | 4 | #include "config.h" |
32a8f510 | 5 | #include "environment.h" |
f394e093 | 6 | #include "gettext.h" |
41771fa4 | 7 | #include "hex.h" |
697cc8ef | 8 | #include "lockfile.h" |
fc04c412 | 9 | #include "pack.h" |
8a65ff76 | 10 | #include "refs.h" |
f3a3214e | 11 | #include "pkt-line.h" |
38a81b4e | 12 | #include "sideband.h" |
b1bf95bb | 13 | #include "run-command.h" |
5e3aba33 | 14 | #include "hook.h" |
d807c4a0 | 15 | #include "exec-cmd.h" |
11031d7e JS |
16 | #include "commit.h" |
17 | #include "object.h" | |
d79796bc | 18 | #include "remote.h" |
47a59185 | 19 | #include "connect.h" |
da3efdb1 | 20 | #include "string-list.h" |
fe299ec5 | 21 | #include "oid-array.h" |
52fed6e1 | 22 | #include "connected.h" |
dbbcd44f | 23 | #include "strvec.h" |
ff5effdf | 24 | #include "version.h" |
d05b9618 JH |
25 | #include "tag.h" |
26 | #include "gpg-interface.h" | |
ec7dbd14 | 27 | #include "sigchain.h" |
5d477a33 | 28 | #include "fsck.h" |
722ff7f8 | 29 | #include "tmp-objdir.h" |
ab6eea6f | 30 | #include "oidset.h" |
3836d88a | 31 | #include "packfile.h" |
dabab1d6 | 32 | #include "object-name.h" |
cbd53a21 | 33 | #include "object-store.h" |
aa9bab29 | 34 | #include "protocol.h" |
64043556 | 35 | #include "commit-reach.h" |
74ea5c95 EN |
36 | #include "trace.h" |
37 | #include "trace2.h" | |
4ef34648 | 38 | #include "worktree.h" |
120ad2b0 | 39 | #include "shallow.h" |
d5ebb50d | 40 | #include "wrapper.h" |
575f4974 | 41 | |
1b68387e SS |
42 | static const char * const receive_pack_usage[] = { |
43 | N_("git receive-pack <git-dir>"), | |
44 | NULL | |
45 | }; | |
575f4974 | 46 | |
986e8239 | 47 | enum deny_action { |
3d95d92b | 48 | DENY_UNCONFIGURED, |
986e8239 JK |
49 | DENY_IGNORE, |
50 | DENY_WARN, | |
1404bcbb JS |
51 | DENY_REFUSE, |
52 | DENY_UPDATE_INSTEAD | |
986e8239 JK |
53 | }; |
54 | ||
1b53a076 JH |
55 | static int deny_deletes; |
56 | static int deny_non_fast_forwards; | |
3d95d92b | 57 | static enum deny_action deny_current_branch = DENY_UNCONFIGURED; |
747ca245 | 58 | static enum deny_action deny_delete_current = DENY_UNCONFIGURED; |
dab76d3a JH |
59 | static int receive_fsck_objects = -1; |
60 | static int transfer_fsck_objects = -1; | |
5d477a33 | 61 | static struct strbuf fsck_msg_types = STRBUF_INIT; |
e28714c5 JH |
62 | static int receive_unpack_limit = -1; |
63 | static int transfer_unpack_limit = -1; | |
1b70fe5d | 64 | static int advertise_atomic_push = 1; |
c714e45f | 65 | static int advertise_push_options; |
8073d75b | 66 | static int advertise_sid; |
46732fae | 67 | static int unpack_limit = 100; |
c08db5a2 | 68 | static off_t max_input_size; |
96f1e58f | 69 | static int report_status; |
63518a57 | 70 | static int report_status_v2; |
38a81b4e | 71 | static int use_sideband; |
68deed29 | 72 | static int use_atomic; |
c714e45f | 73 | static int use_push_options; |
c207e34f | 74 | static int quiet; |
b74fce16 | 75 | static int prefer_ofs_delta = 1; |
77e3efbf JH |
76 | static int auto_update_server_info; |
77 | static int auto_gc = 1; | |
1b68387e | 78 | static int reject_thin; |
5732373d JH |
79 | static int stateless_rpc; |
80 | static const char *service_dir; | |
747ca245 | 81 | static const char *head_name; |
96ec7b1e | 82 | static void *head_name_to_free; |
185c04e0 | 83 | static int sent_capabilities; |
0a1bc12b | 84 | static int shallow_update; |
5dbd7676 | 85 | static const char *alt_shallow_file; |
a85b377d | 86 | static struct strbuf push_cert = STRBUF_INIT; |
a09c985e | 87 | static struct object_id push_cert_oid; |
d05b9618 | 88 | static struct signature_check sigcheck; |
b89363e4 JH |
89 | static const char *push_cert_nonce; |
90 | static const char *cert_nonce_seed; | |
9b67eb6f | 91 | static struct string_list hidden_refs = STRING_LIST_INIT_DUP; |
b89363e4 JH |
92 | |
93 | static const char *NONCE_UNSOLICITED = "UNSOLICITED"; | |
94 | static const char *NONCE_BAD = "BAD"; | |
95 | static const char *NONCE_MISSING = "MISSING"; | |
96 | static const char *NONCE_OK = "OK"; | |
5732373d | 97 | static const char *NONCE_SLOP = "SLOP"; |
b89363e4 | 98 | static const char *nonce_status; |
5732373d | 99 | static long nonce_stamp_slop; |
dddbad72 | 100 | static timestamp_t nonce_stamp_slop_limit; |
222368c6 | 101 | static struct ref_transaction *transaction; |
cfee10a7 | 102 | |
83558686 JK |
103 | static enum { |
104 | KEEPALIVE_NEVER = 0, | |
105 | KEEPALIVE_AFTER_NUL, | |
106 | KEEPALIVE_ALWAYS | |
107 | } use_keepalive; | |
108 | static int keepalive_in_sec = 5; | |
109 | ||
722ff7f8 JK |
110 | static struct tmp_objdir *tmp_objdir; |
111 | ||
31e8595a JX |
112 | static struct proc_receive_ref { |
113 | unsigned int want_add:1, | |
114 | want_delete:1, | |
115 | want_modify:1, | |
116 | negative_ref:1; | |
117 | char *ref_prefix; | |
118 | struct proc_receive_ref *next; | |
119 | } *proc_receive_ref; | |
120 | ||
121 | static void proc_receive_ref_append(const char *prefix); | |
122 | ||
986e8239 JK |
123 | static enum deny_action parse_deny_action(const char *var, const char *value) |
124 | { | |
125 | if (value) { | |
126 | if (!strcasecmp(value, "ignore")) | |
127 | return DENY_IGNORE; | |
128 | if (!strcasecmp(value, "warn")) | |
129 | return DENY_WARN; | |
130 | if (!strcasecmp(value, "refuse")) | |
131 | return DENY_REFUSE; | |
1404bcbb JS |
132 | if (!strcasecmp(value, "updateinstead")) |
133 | return DENY_UPDATE_INSTEAD; | |
986e8239 JK |
134 | } |
135 | if (git_config_bool(var, value)) | |
136 | return DENY_REFUSE; | |
137 | return DENY_IGNORE; | |
138 | } | |
139 | ||
ef90d6d4 | 140 | static int receive_pack_config(const char *var, const char *value, void *cb) |
6fb75bed | 141 | { |
9b67eb6f | 142 | int status = parse_hide_refs_config(var, value, "receive", &hidden_refs); |
daebaa78 JH |
143 | |
144 | if (status) | |
145 | return status; | |
146 | ||
a240de11 JK |
147 | if (strcmp(var, "receive.denydeletes") == 0) { |
148 | deny_deletes = git_config_bool(var, value); | |
149 | return 0; | |
150 | } | |
151 | ||
e28714c5 | 152 | if (strcmp(var, "receive.denynonfastforwards") == 0) { |
6fb75bed SP |
153 | deny_non_fast_forwards = git_config_bool(var, value); |
154 | return 0; | |
155 | } | |
156 | ||
e28714c5 JH |
157 | if (strcmp(var, "receive.unpacklimit") == 0) { |
158 | receive_unpack_limit = git_config_int(var, value); | |
fc04c412 SP |
159 | return 0; |
160 | } | |
161 | ||
e28714c5 JH |
162 | if (strcmp(var, "transfer.unpacklimit") == 0) { |
163 | transfer_unpack_limit = git_config_int(var, value); | |
164 | return 0; | |
165 | } | |
166 | ||
cd94c6f9 JS |
167 | if (strcmp(var, "receive.fsck.skiplist") == 0) { |
168 | const char *path; | |
169 | ||
170 | if (git_config_pathname(&path, var, value)) | |
171 | return 1; | |
172 | strbuf_addf(&fsck_msg_types, "%cskiplist=%s", | |
173 | fsck_msg_types.len ? ',' : '=', path); | |
174 | free((char *)path); | |
175 | return 0; | |
176 | } | |
177 | ||
5d477a33 JS |
178 | if (skip_prefix(var, "receive.fsck.", &var)) { |
179 | if (is_valid_msg_type(var, value)) | |
180 | strbuf_addf(&fsck_msg_types, "%c%s=%s", | |
181 | fsck_msg_types.len ? ',' : '=', var, value); | |
182 | else | |
c25edee9 | 183 | warning("skipping unknown msg id '%s'", var); |
5d477a33 JS |
184 | return 0; |
185 | } | |
186 | ||
20dc0016 MK |
187 | if (strcmp(var, "receive.fsckobjects") == 0) { |
188 | receive_fsck_objects = git_config_bool(var, value); | |
189 | return 0; | |
190 | } | |
191 | ||
dab76d3a JH |
192 | if (strcmp(var, "transfer.fsckobjects") == 0) { |
193 | transfer_fsck_objects = git_config_bool(var, value); | |
194 | return 0; | |
195 | } | |
196 | ||
986e8239 JK |
197 | if (!strcmp(var, "receive.denycurrentbranch")) { |
198 | deny_current_branch = parse_deny_action(var, value); | |
199 | return 0; | |
200 | } | |
201 | ||
747ca245 JH |
202 | if (strcmp(var, "receive.denydeletecurrent") == 0) { |
203 | deny_delete_current = parse_deny_action(var, value); | |
204 | return 0; | |
205 | } | |
206 | ||
b74fce16 NP |
207 | if (strcmp(var, "repack.usedeltabaseoffset") == 0) { |
208 | prefer_ofs_delta = git_config_bool(var, value); | |
209 | return 0; | |
210 | } | |
211 | ||
77e3efbf JH |
212 | if (strcmp(var, "receive.updateserverinfo") == 0) { |
213 | auto_update_server_info = git_config_bool(var, value); | |
214 | return 0; | |
215 | } | |
216 | ||
217 | if (strcmp(var, "receive.autogc") == 0) { | |
218 | auto_gc = git_config_bool(var, value); | |
219 | return 0; | |
220 | } | |
221 | ||
0a1bc12b NTND |
222 | if (strcmp(var, "receive.shallowupdate") == 0) { |
223 | shallow_update = git_config_bool(var, value); | |
224 | return 0; | |
225 | } | |
226 | ||
b89363e4 JH |
227 | if (strcmp(var, "receive.certnonceseed") == 0) |
228 | return git_config_string(&cert_nonce_seed, var, value); | |
a85b377d | 229 | |
5732373d JH |
230 | if (strcmp(var, "receive.certnonceslop") == 0) { |
231 | nonce_stamp_slop_limit = git_config_ulong(var, value); | |
232 | return 0; | |
233 | } | |
234 | ||
1b70fe5d RS |
235 | if (strcmp(var, "receive.advertiseatomic") == 0) { |
236 | advertise_atomic_push = git_config_bool(var, value); | |
237 | return 0; | |
238 | } | |
239 | ||
c714e45f SB |
240 | if (strcmp(var, "receive.advertisepushoptions") == 0) { |
241 | advertise_push_options = git_config_bool(var, value); | |
242 | return 0; | |
243 | } | |
244 | ||
83558686 JK |
245 | if (strcmp(var, "receive.keepalive") == 0) { |
246 | keepalive_in_sec = git_config_int(var, value); | |
247 | return 0; | |
248 | } | |
249 | ||
c08db5a2 JK |
250 | if (strcmp(var, "receive.maxinputsize") == 0) { |
251 | max_input_size = git_config_int64(var, value); | |
252 | return 0; | |
253 | } | |
254 | ||
31e8595a JX |
255 | if (strcmp(var, "receive.procreceiverefs") == 0) { |
256 | if (!value) | |
257 | return config_error_nonbool(var); | |
258 | proc_receive_ref_append(value); | |
259 | return 0; | |
260 | } | |
261 | ||
8073d75b JS |
262 | if (strcmp(var, "transfer.advertisesid") == 0) { |
263 | advertise_sid = git_config_bool(var, value); | |
264 | return 0; | |
265 | } | |
266 | ||
ef90d6d4 | 267 | return git_default_config(var, value, cb); |
6fb75bed SP |
268 | } |
269 | ||
1b7ba794 | 270 | static void show_ref(const char *path, const struct object_id *oid) |
575f4974 | 271 | { |
52d2ae58 | 272 | if (sent_capabilities) { |
1b7ba794 | 273 | packet_write_fmt(1, "%s %s\n", oid_to_hex(oid), path); |
52d2ae58 JH |
274 | } else { |
275 | struct strbuf cap = STRBUF_INIT; | |
276 | ||
277 | strbuf_addstr(&cap, | |
63518a57 | 278 | "report-status report-status-v2 delete-refs side-band-64k quiet"); |
1b70fe5d RS |
279 | if (advertise_atomic_push) |
280 | strbuf_addstr(&cap, " atomic"); | |
52d2ae58 JH |
281 | if (prefer_ofs_delta) |
282 | strbuf_addstr(&cap, " ofs-delta"); | |
b89363e4 JH |
283 | if (push_cert_nonce) |
284 | strbuf_addf(&cap, " push-cert=%s", push_cert_nonce); | |
c714e45f SB |
285 | if (advertise_push_options) |
286 | strbuf_addstr(&cap, " push-options"); | |
8073d75b JS |
287 | if (advertise_sid) |
288 | strbuf_addf(&cap, " session-id=%s", trace2_session_id()); | |
bf30dbf8 | 289 | strbuf_addf(&cap, " object-format=%s", the_hash_algo->name); |
52d2ae58 | 290 | strbuf_addf(&cap, " agent=%s", git_user_agent_sanitized()); |
81c634e9 | 291 | packet_write_fmt(1, "%s %s%c%s\n", |
1b7ba794 | 292 | oid_to_hex(oid), path, 0, cap.buf); |
52d2ae58 JH |
293 | strbuf_release(&cap); |
294 | sent_capabilities = 1; | |
295 | } | |
575f4974 LT |
296 | } |
297 | ||
78a766ab | 298 | static int show_ref_cb(const char *path_full, const struct object_id *oid, |
5cf88fd8 | 299 | int flag UNUSED, void *data) |
6b01ecfe | 300 | { |
8b24b9e7 | 301 | struct oidset *seen = data; |
78a766ab LF |
302 | const char *path = strip_namespace(path_full); |
303 | ||
9b67eb6f | 304 | if (ref_is_hidden(path, path_full, &hidden_refs)) |
78a766ab LF |
305 | return 0; |
306 | ||
6b01ecfe JT |
307 | /* |
308 | * Advertise refs outside our current namespace as ".have" | |
309 | * refs, so that the client can use them to minimize data | |
fea6c47f | 310 | * transfer but will otherwise ignore them. |
6b01ecfe | 311 | */ |
8b24b9e7 JK |
312 | if (!path) { |
313 | if (oidset_insert(seen, oid)) | |
314 | return 0; | |
6b01ecfe | 315 | path = ".have"; |
63d428e6 JK |
316 | } else { |
317 | oidset_insert(seen, oid); | |
8b24b9e7 | 318 | } |
1b7ba794 | 319 | show_ref(path, oid); |
bc98201d | 320 | return 0; |
6b01ecfe JT |
321 | } |
322 | ||
bdf4276c | 323 | static void show_one_alternate_ref(const struct object_id *oid, |
ab6eea6f | 324 | void *data) |
b7a025d9 | 325 | { |
ab6eea6f | 326 | struct oidset *seen = data; |
b7a025d9 | 327 | |
ab6eea6f JK |
328 | if (oidset_insert(seen, oid)) |
329 | return; | |
330 | ||
1b7ba794 | 331 | show_ref(".have", oid); |
6b01ecfe JT |
332 | } |
333 | ||
8a65ff76 | 334 | static void write_head_info(void) |
575f4974 | 335 | { |
ab6eea6f | 336 | static struct oidset seen = OIDSET_INIT; |
2b2a5be3 | 337 | |
63d428e6 | 338 | for_each_ref(show_ref_cb, &seen); |
ab6eea6f JK |
339 | for_each_alternate_ref(show_one_alternate_ref, &seen); |
340 | oidset_clear(&seen); | |
185c04e0 | 341 | if (!sent_capabilities) |
14228447 | 342 | show_ref("capabilities^{}", null_oid()); |
cfee10a7 | 343 | |
ad491366 NTND |
344 | advertise_shallow_grafts(1); |
345 | ||
b7a025d9 MH |
346 | /* EOF */ |
347 | packet_flush(1); | |
575f4974 LT |
348 | } |
349 | ||
15d3af5e JX |
350 | #define RUN_PROC_RECEIVE_SCHEDULED 1 |
351 | #define RUN_PROC_RECEIVE_RETURNED 2 | |
eb1af2df LT |
352 | struct command { |
353 | struct command *next; | |
cfee10a7 | 354 | const char *error_string; |
15d3af5e | 355 | struct ref_push_report *report; |
160b81ed | 356 | unsigned int skip_update:1, |
15d3af5e JX |
357 | did_not_exist:1, |
358 | run_proc_receive:2; | |
5dbd7676 | 359 | int index; |
9c44ea44 | 360 | struct object_id old_oid; |
361 | struct object_id new_oid; | |
8f1d2e6f | 362 | char ref_name[FLEX_ARRAY]; /* more */ |
575f4974 LT |
363 | }; |
364 | ||
31e8595a JX |
365 | static void proc_receive_ref_append(const char *prefix) |
366 | { | |
367 | struct proc_receive_ref *ref_pattern; | |
368 | char *p; | |
369 | int len; | |
370 | ||
ca56dadb | 371 | CALLOC_ARRAY(ref_pattern, 1); |
31e8595a JX |
372 | p = strchr(prefix, ':'); |
373 | if (p) { | |
374 | while (prefix < p) { | |
375 | if (*prefix == 'a') | |
376 | ref_pattern->want_add = 1; | |
377 | else if (*prefix == 'd') | |
378 | ref_pattern->want_delete = 1; | |
379 | else if (*prefix == 'm') | |
380 | ref_pattern->want_modify = 1; | |
381 | else if (*prefix == '!') | |
382 | ref_pattern->negative_ref = 1; | |
383 | prefix++; | |
384 | } | |
385 | prefix++; | |
386 | } else { | |
387 | ref_pattern->want_add = 1; | |
388 | ref_pattern->want_delete = 1; | |
389 | ref_pattern->want_modify = 1; | |
390 | } | |
391 | len = strlen(prefix); | |
392 | while (len && prefix[len - 1] == '/') | |
393 | len--; | |
394 | ref_pattern->ref_prefix = xmemdupz(prefix, len); | |
395 | if (!proc_receive_ref) { | |
396 | proc_receive_ref = ref_pattern; | |
397 | } else { | |
398 | struct proc_receive_ref *end; | |
399 | ||
400 | end = proc_receive_ref; | |
401 | while (end->next) | |
402 | end = end->next; | |
403 | end->next = ref_pattern; | |
404 | } | |
405 | } | |
406 | ||
407 | static int proc_receive_ref_matches(struct command *cmd) | |
408 | { | |
409 | struct proc_receive_ref *p; | |
410 | ||
411 | if (!proc_receive_ref) | |
412 | return 0; | |
413 | ||
414 | for (p = proc_receive_ref; p; p = p->next) { | |
415 | const char *match = p->ref_prefix; | |
416 | const char *remains; | |
417 | ||
418 | if (!p->want_add && is_null_oid(&cmd->old_oid)) | |
419 | continue; | |
420 | else if (!p->want_delete && is_null_oid(&cmd->new_oid)) | |
421 | continue; | |
422 | else if (!p->want_modify && | |
423 | !is_null_oid(&cmd->old_oid) && | |
424 | !is_null_oid(&cmd->new_oid)) | |
425 | continue; | |
426 | ||
427 | if (skip_prefix(cmd->ref_name, match, &remains) && | |
428 | (!*remains || *remains == '/')) { | |
429 | if (!p->negative_ref) | |
430 | return 1; | |
431 | } else if (p->negative_ref) { | |
432 | return 1; | |
433 | } | |
434 | } | |
435 | return 0; | |
436 | } | |
437 | ||
466dbc42 SP |
438 | static void report_message(const char *prefix, const char *err, va_list params) |
439 | { | |
b7115a35 | 440 | int sz; |
466dbc42 SP |
441 | char msg[4096]; |
442 | ||
b7115a35 | 443 | sz = xsnprintf(msg, sizeof(msg), "%s", prefix); |
466dbc42 SP |
444 | sz += vsnprintf(msg + sz, sizeof(msg) - sz, err, params); |
445 | if (sz > (sizeof(msg) - 1)) | |
446 | sz = sizeof(msg) - 1; | |
447 | msg[sz++] = '\n'; | |
448 | ||
449 | if (use_sideband) | |
450 | send_sideband(1, 2, msg, sz, use_sideband); | |
451 | else | |
452 | xwrite(2, msg, sz); | |
453 | } | |
454 | ||
103e02c7 | 455 | __attribute__((format (printf, 1, 2))) |
466dbc42 SP |
456 | static void rp_warning(const char *err, ...) |
457 | { | |
458 | va_list params; | |
459 | va_start(params, err); | |
460 | report_message("warning: ", err, params); | |
461 | va_end(params); | |
462 | } | |
463 | ||
103e02c7 | 464 | __attribute__((format (printf, 1, 2))) |
466dbc42 SP |
465 | static void rp_error(const char *err, ...) |
466 | { | |
467 | va_list params; | |
468 | va_start(params, err); | |
469 | report_message("error: ", err, params); | |
470 | va_end(params); | |
471 | } | |
472 | ||
5cf88fd8 | 473 | static int copy_to_sideband(int in, int out UNUSED, void *arg UNUSED) |
6d525d38 SP |
474 | { |
475 | char data[128]; | |
83558686 JK |
476 | int keepalive_active = 0; |
477 | ||
478 | if (keepalive_in_sec <= 0) | |
479 | use_keepalive = KEEPALIVE_NEVER; | |
480 | if (use_keepalive == KEEPALIVE_ALWAYS) | |
481 | keepalive_active = 1; | |
482 | ||
6d525d38 | 483 | while (1) { |
83558686 JK |
484 | ssize_t sz; |
485 | ||
486 | if (keepalive_active) { | |
487 | struct pollfd pfd; | |
488 | int ret; | |
489 | ||
490 | pfd.fd = in; | |
491 | pfd.events = POLLIN; | |
492 | ret = poll(&pfd, 1, 1000 * keepalive_in_sec); | |
493 | ||
494 | if (ret < 0) { | |
495 | if (errno == EINTR) | |
496 | continue; | |
497 | else | |
498 | break; | |
499 | } else if (ret == 0) { | |
500 | /* no data; send a keepalive packet */ | |
501 | static const char buf[] = "0005\1"; | |
502 | write_or_die(1, buf, sizeof(buf) - 1); | |
503 | continue; | |
504 | } /* else there is actual data to read */ | |
505 | } | |
506 | ||
507 | sz = xread(in, data, sizeof(data)); | |
6d525d38 SP |
508 | if (sz <= 0) |
509 | break; | |
83558686 JK |
510 | |
511 | if (use_keepalive == KEEPALIVE_AFTER_NUL && !keepalive_active) { | |
512 | const char *p = memchr(data, '\0', sz); | |
513 | if (p) { | |
514 | /* | |
515 | * The NUL tells us to start sending keepalives. Make | |
516 | * sure we send any other data we read along | |
517 | * with it. | |
518 | */ | |
519 | keepalive_active = 1; | |
520 | send_sideband(1, 2, data, p - data, use_sideband); | |
521 | send_sideband(1, 2, p + 1, sz - (p - data + 1), use_sideband); | |
522 | continue; | |
523 | } | |
524 | } | |
525 | ||
526 | /* | |
527 | * Either we're not looking for a NUL signal, or we didn't see | |
528 | * it yet; just pass along the data. | |
529 | */ | |
6d525d38 SP |
530 | send_sideband(1, 2, data, sz, use_sideband); |
531 | } | |
532 | close(in); | |
533 | return 0; | |
534 | } | |
535 | ||
3013118e | 536 | static void hmac_hash(unsigned char *out, |
b89363e4 JH |
537 | const char *key_in, size_t key_len, |
538 | const char *text, size_t text_len) | |
539 | { | |
fabec2c5 | 540 | unsigned char key[GIT_MAX_BLKSZ]; |
541 | unsigned char k_ipad[GIT_MAX_BLKSZ]; | |
542 | unsigned char k_opad[GIT_MAX_BLKSZ]; | |
b89363e4 | 543 | int i; |
fabec2c5 | 544 | git_hash_ctx ctx; |
b89363e4 JH |
545 | |
546 | /* RFC 2104 2. (1) */ | |
fabec2c5 | 547 | memset(key, '\0', GIT_MAX_BLKSZ); |
548 | if (the_hash_algo->blksz < key_len) { | |
549 | the_hash_algo->init_fn(&ctx); | |
550 | the_hash_algo->update_fn(&ctx, key_in, key_len); | |
551 | the_hash_algo->final_fn(key, &ctx); | |
b89363e4 JH |
552 | } else { |
553 | memcpy(key, key_in, key_len); | |
554 | } | |
555 | ||
556 | /* RFC 2104 2. (2) & (5) */ | |
557 | for (i = 0; i < sizeof(key); i++) { | |
558 | k_ipad[i] = key[i] ^ 0x36; | |
559 | k_opad[i] = key[i] ^ 0x5c; | |
560 | } | |
561 | ||
562 | /* RFC 2104 2. (3) & (4) */ | |
fabec2c5 | 563 | the_hash_algo->init_fn(&ctx); |
564 | the_hash_algo->update_fn(&ctx, k_ipad, sizeof(k_ipad)); | |
565 | the_hash_algo->update_fn(&ctx, text, text_len); | |
566 | the_hash_algo->final_fn(out, &ctx); | |
b89363e4 JH |
567 | |
568 | /* RFC 2104 2. (6) & (7) */ | |
fabec2c5 | 569 | the_hash_algo->init_fn(&ctx); |
570 | the_hash_algo->update_fn(&ctx, k_opad, sizeof(k_opad)); | |
571 | the_hash_algo->update_fn(&ctx, out, the_hash_algo->rawsz); | |
572 | the_hash_algo->final_fn(out, &ctx); | |
b89363e4 JH |
573 | } |
574 | ||
dddbad72 | 575 | static char *prepare_push_cert_nonce(const char *path, timestamp_t stamp) |
b89363e4 JH |
576 | { |
577 | struct strbuf buf = STRBUF_INIT; | |
fabec2c5 | 578 | unsigned char hash[GIT_MAX_RAWSZ]; |
b89363e4 | 579 | |
cb71f8bd | 580 | strbuf_addf(&buf, "%s:%"PRItime, path, stamp); |
3013118e | 581 | hmac_hash(hash, buf.buf, buf.len, cert_nonce_seed, strlen(cert_nonce_seed)); |
b89363e4 JH |
582 | strbuf_release(&buf); |
583 | ||
3013118e | 584 | /* RFC 2104 5. HMAC-SHA1 or HMAC-SHA256 */ |
fc06be3b | 585 | strbuf_addf(&buf, "%"PRItime"-%.*s", stamp, (int)the_hash_algo->hexsz, hash_to_hex(hash)); |
b89363e4 JH |
586 | return strbuf_detach(&buf, NULL); |
587 | } | |
588 | ||
cbaf82cc JT |
589 | static char *find_header(const char *msg, size_t len, const char *key, |
590 | const char **next_line) | |
b89363e4 | 591 | { |
cfc5cf42 JC |
592 | size_t out_len; |
593 | const char *val = find_header_mem(msg, len, key, &out_len); | |
594 | ||
595 | if (!val) | |
596 | return NULL; | |
597 | ||
598 | if (next_line) | |
599 | *next_line = val + out_len + 1; | |
600 | ||
601 | return xmemdupz(val, out_len); | |
b89363e4 JH |
602 | } |
603 | ||
edc6dccf | 604 | /* |
605 | * Return zero if a and b are equal up to n bytes and nonzero if they are not. | |
606 | * This operation is guaranteed to run in constant time to avoid leaking data. | |
607 | */ | |
608 | static int constant_memequal(const char *a, const char *b, size_t n) | |
609 | { | |
610 | int res = 0; | |
719483e5 JH |
611 | size_t i; |
612 | ||
613 | for (i = 0; i < n; i++) | |
edc6dccf | 614 | res |= a[i] ^ b[i]; |
615 | return res; | |
616 | } | |
617 | ||
b89363e4 JH |
618 | static const char *check_nonce(const char *buf, size_t len) |
619 | { | |
cbaf82cc | 620 | char *nonce = find_header(buf, len, "nonce", NULL); |
dddbad72 | 621 | timestamp_t stamp, ostamp; |
5732373d | 622 | char *bohmac, *expect = NULL; |
b89363e4 | 623 | const char *retval = NONCE_BAD; |
edc6dccf | 624 | size_t noncelen; |
b89363e4 JH |
625 | |
626 | if (!nonce) { | |
627 | retval = NONCE_MISSING; | |
628 | goto leave; | |
629 | } else if (!push_cert_nonce) { | |
630 | retval = NONCE_UNSOLICITED; | |
631 | goto leave; | |
632 | } else if (!strcmp(push_cert_nonce, nonce)) { | |
633 | retval = NONCE_OK; | |
634 | goto leave; | |
635 | } | |
636 | ||
5732373d JH |
637 | if (!stateless_rpc) { |
638 | /* returned nonce MUST match what we gave out earlier */ | |
639 | retval = NONCE_BAD; | |
640 | goto leave; | |
641 | } | |
642 | ||
643 | /* | |
644 | * In stateless mode, we may be receiving a nonce issued by | |
645 | * another instance of the server that serving the same | |
646 | * repository, and the timestamps may not match, but the | |
647 | * nonce-seed and dir should match, so we can recompute and | |
648 | * report the time slop. | |
649 | * | |
650 | * In addition, when a nonce issued by another instance has | |
651 | * timestamp within receive.certnonceslop seconds, we pretend | |
652 | * as if we issued that nonce when reporting to the hook. | |
653 | */ | |
654 | ||
655 | /* nonce is concat(<seconds-since-epoch>, "-", <hmac>) */ | |
656 | if (*nonce <= '0' || '9' < *nonce) { | |
657 | retval = NONCE_BAD; | |
658 | goto leave; | |
659 | } | |
1aeb7e75 | 660 | stamp = parse_timestamp(nonce, &bohmac, 10); |
5732373d JH |
661 | if (bohmac == nonce || bohmac[0] != '-') { |
662 | retval = NONCE_BAD; | |
663 | goto leave; | |
664 | } | |
665 | ||
edc6dccf | 666 | noncelen = strlen(nonce); |
5732373d | 667 | expect = prepare_push_cert_nonce(service_dir, stamp); |
edc6dccf | 668 | if (noncelen != strlen(expect)) { |
669 | /* This is not even the right size. */ | |
670 | retval = NONCE_BAD; | |
671 | goto leave; | |
672 | } | |
673 | if (constant_memequal(expect, nonce, noncelen)) { | |
5732373d JH |
674 | /* Not what we would have signed earlier */ |
675 | retval = NONCE_BAD; | |
676 | goto leave; | |
677 | } | |
678 | ||
679 | /* | |
680 | * By how many seconds is this nonce stale? Negative value | |
681 | * would mean it was issued by another server with its clock | |
682 | * skewed in the future. | |
683 | */ | |
1aeb7e75 | 684 | ostamp = parse_timestamp(push_cert_nonce, NULL, 10); |
5732373d JH |
685 | nonce_stamp_slop = (long)ostamp - (long)stamp; |
686 | ||
687 | if (nonce_stamp_slop_limit && | |
31a8aa1e | 688 | labs(nonce_stamp_slop) <= nonce_stamp_slop_limit) { |
5732373d JH |
689 | /* |
690 | * Pretend as if the received nonce (which passes the | |
691 | * HMAC check, so it is not a forged by third-party) | |
692 | * is what we issued. | |
693 | */ | |
694 | free((void *)push_cert_nonce); | |
695 | push_cert_nonce = xstrdup(nonce); | |
696 | retval = NONCE_OK; | |
697 | } else { | |
698 | retval = NONCE_SLOP; | |
699 | } | |
b89363e4 JH |
700 | |
701 | leave: | |
702 | free(nonce); | |
5732373d | 703 | free(expect); |
b89363e4 JH |
704 | return retval; |
705 | } | |
706 | ||
cbaf82cc JT |
707 | /* |
708 | * Return 1 if there is no push_cert or if the push options in push_cert are | |
709 | * the same as those in the argument; 0 otherwise. | |
710 | */ | |
711 | static int check_cert_push_options(const struct string_list *push_options) | |
712 | { | |
713 | const char *buf = push_cert.buf; | |
714 | int len = push_cert.len; | |
715 | ||
716 | char *option; | |
717 | const char *next_line; | |
718 | int options_seen = 0; | |
719 | ||
720 | int retval = 1; | |
721 | ||
722 | if (!len) | |
723 | return 1; | |
724 | ||
725 | while ((option = find_header(buf, len, "push-option", &next_line))) { | |
726 | len -= (next_line - buf); | |
727 | buf = next_line; | |
728 | options_seen++; | |
729 | if (options_seen > push_options->nr | |
730 | || strcmp(option, | |
731 | push_options->items[options_seen - 1].string)) { | |
732 | retval = 0; | |
733 | goto leave; | |
734 | } | |
735 | free(option); | |
736 | } | |
737 | ||
738 | if (options_seen != push_options->nr) | |
739 | retval = 0; | |
740 | ||
741 | leave: | |
742 | free(option); | |
743 | return retval; | |
744 | } | |
745 | ||
a85b377d JH |
746 | static void prepare_push_cert_sha1(struct child_process *proc) |
747 | { | |
748 | static int already_done; | |
a85b377d JH |
749 | |
750 | if (!push_cert.len) | |
751 | return; | |
752 | ||
753 | if (!already_done) { | |
d05b9618 JH |
754 | int bogs /* beginning_of_gpg_sig */; |
755 | ||
a85b377d | 756 | already_done = 1; |
c80d226a | 757 | if (write_object_file(push_cert.buf, push_cert.len, OBJ_BLOB, |
a09c985e PO |
758 | &push_cert_oid)) |
759 | oidclr(&push_cert_oid); | |
d05b9618 JH |
760 | |
761 | memset(&sigcheck, '\0', sizeof(sigcheck)); | |
d05b9618 | 762 | |
482c1191 | 763 | bogs = parse_signed_buffer(push_cert.buf, push_cert.len); |
02769437 FS |
764 | sigcheck.payload = xmemdupz(push_cert.buf, bogs); |
765 | sigcheck.payload_len = bogs; | |
766 | check_signature(&sigcheck, push_cert.buf + bogs, | |
767 | push_cert.len - bogs); | |
d05b9618 | 768 | |
b89363e4 | 769 | nonce_status = check_nonce(push_cert.buf, bogs); |
a85b377d | 770 | } |
a09c985e | 771 | if (!is_null_oid(&push_cert_oid)) { |
29fda24d | 772 | strvec_pushf(&proc->env, "GIT_PUSH_CERT=%s", |
f6d8942b | 773 | oid_to_hex(&push_cert_oid)); |
29fda24d | 774 | strvec_pushf(&proc->env, "GIT_PUSH_CERT_SIGNER=%s", |
f6d8942b | 775 | sigcheck.signer ? sigcheck.signer : ""); |
29fda24d | 776 | strvec_pushf(&proc->env, "GIT_PUSH_CERT_KEY=%s", |
f6d8942b | 777 | sigcheck.key ? sigcheck.key : ""); |
29fda24d | 778 | strvec_pushf(&proc->env, "GIT_PUSH_CERT_STATUS=%c", |
f6d8942b | 779 | sigcheck.result); |
b89363e4 | 780 | if (push_cert_nonce) { |
29fda24d | 781 | strvec_pushf(&proc->env, |
f6d8942b JK |
782 | "GIT_PUSH_CERT_NONCE=%s", |
783 | push_cert_nonce); | |
29fda24d | 784 | strvec_pushf(&proc->env, |
f6d8942b JK |
785 | "GIT_PUSH_CERT_NONCE_STATUS=%s", |
786 | nonce_status); | |
5732373d | 787 | if (nonce_status == NONCE_SLOP) |
29fda24d | 788 | strvec_pushf(&proc->env, |
f6d8942b JK |
789 | "GIT_PUSH_CERT_NONCE_SLOP=%ld", |
790 | nonce_stamp_slop); | |
b89363e4 | 791 | } |
a85b377d JH |
792 | } |
793 | } | |
794 | ||
77a9745d SB |
795 | struct receive_hook_feed_state { |
796 | struct command *cmd; | |
195d6eae | 797 | struct ref_push_report *report; |
77a9745d SB |
798 | int skip_broken; |
799 | struct strbuf buf; | |
800 | const struct string_list *push_options; | |
801 | }; | |
802 | ||
9684e44a | 803 | typedef int (*feed_fn)(void *, const char **, size_t *); |
77a9745d SB |
804 | static int run_and_feed_hook(const char *hook_name, feed_fn feed, |
805 | struct receive_hook_feed_state *feed_state) | |
b1bf95bb | 806 | { |
d3180279 | 807 | struct child_process proc = CHILD_PROCESS_INIT; |
6d525d38 | 808 | struct async muxer; |
9684e44a | 809 | int code; |
7f14609e | 810 | const char *hook_path = find_hook(hook_name); |
b1bf95bb | 811 | |
7f14609e | 812 | if (!hook_path) |
b1bf95bb | 813 | return 0; |
c8dd2771 | 814 | |
7f14609e | 815 | strvec_push(&proc.args, hook_path); |
f43cd49f SP |
816 | proc.in = -1; |
817 | proc.stdout_to_stderr = 1; | |
6206286e JH |
818 | proc.trace2_hook_name = hook_name; |
819 | ||
77a9745d | 820 | if (feed_state->push_options) { |
99d60545 | 821 | size_t i; |
77a9745d | 822 | for (i = 0; i < feed_state->push_options->nr; i++) |
29fda24d | 823 | strvec_pushf(&proc.env, |
99d60545 ÆAB |
824 | "GIT_PUSH_OPTION_%"PRIuMAX"=%s", |
825 | (uintmax_t)i, | |
f6d8942b | 826 | feed_state->push_options->items[i].string); |
29fda24d | 827 | strvec_pushf(&proc.env, "GIT_PUSH_OPTION_COUNT=%"PRIuMAX"", |
99d60545 | 828 | (uintmax_t)feed_state->push_options->nr); |
77a9745d | 829 | } else |
29fda24d | 830 | strvec_pushf(&proc.env, "GIT_PUSH_OPTION_COUNT"); |
f43cd49f | 831 | |
722ff7f8 | 832 | if (tmp_objdir) |
29fda24d | 833 | strvec_pushv(&proc.env, tmp_objdir_env(tmp_objdir)); |
722ff7f8 | 834 | |
6d525d38 SP |
835 | if (use_sideband) { |
836 | memset(&muxer, 0, sizeof(muxer)); | |
837 | muxer.proc = copy_to_sideband; | |
838 | muxer.in = -1; | |
839 | code = start_async(&muxer); | |
840 | if (code) | |
841 | return code; | |
842 | proc.err = muxer.in; | |
843 | } | |
844 | ||
5d222c09 RS |
845 | prepare_push_cert_sha1(&proc); |
846 | ||
f43cd49f | 847 | code = start_command(&proc); |
6d525d38 SP |
848 | if (code) { |
849 | if (use_sideband) | |
850 | finish_async(&muxer); | |
90e41a89 | 851 | return code; |
6d525d38 SP |
852 | } |
853 | ||
ec7dbd14 JH |
854 | sigchain_push(SIGPIPE, SIG_IGN); |
855 | ||
9684e44a JH |
856 | while (1) { |
857 | const char *buf; | |
858 | size_t n; | |
859 | if (feed(feed_state, &buf, &n)) | |
860 | break; | |
06f46f23 | 861 | if (write_in_full(proc.in, buf, n) < 0) |
9684e44a | 862 | break; |
c8dd2771 | 863 | } |
e72ae288 | 864 | close(proc.in); |
6d525d38 SP |
865 | if (use_sideband) |
866 | finish_async(&muxer); | |
ec7dbd14 JH |
867 | |
868 | sigchain_pop(SIGPIPE); | |
869 | ||
90e41a89 | 870 | return finish_command(&proc); |
b1bf95bb JW |
871 | } |
872 | ||
9684e44a JH |
873 | static int feed_receive_hook(void *state_, const char **bufp, size_t *sizep) |
874 | { | |
875 | struct receive_hook_feed_state *state = state_; | |
876 | struct command *cmd = state->cmd; | |
877 | ||
cdc2b2f3 JH |
878 | while (cmd && |
879 | state->skip_broken && (cmd->error_string || cmd->did_not_exist)) | |
9684e44a JH |
880 | cmd = cmd->next; |
881 | if (!cmd) | |
882 | return -1; /* EOF */ | |
195d6eae JX |
883 | if (!bufp) |
884 | return 0; /* OK, can feed something. */ | |
9684e44a | 885 | strbuf_reset(&state->buf); |
195d6eae JX |
886 | if (!state->report) |
887 | state->report = cmd->report; | |
888 | if (state->report) { | |
889 | struct object_id *old_oid; | |
890 | struct object_id *new_oid; | |
891 | const char *ref_name; | |
892 | ||
893 | old_oid = state->report->old_oid ? state->report->old_oid : &cmd->old_oid; | |
894 | new_oid = state->report->new_oid ? state->report->new_oid : &cmd->new_oid; | |
895 | ref_name = state->report->ref_name ? state->report->ref_name : cmd->ref_name; | |
896 | strbuf_addf(&state->buf, "%s %s %s\n", | |
897 | oid_to_hex(old_oid), oid_to_hex(new_oid), | |
898 | ref_name); | |
899 | state->report = state->report->next; | |
900 | if (!state->report) | |
901 | state->cmd = cmd->next; | |
902 | } else { | |
903 | strbuf_addf(&state->buf, "%s %s %s\n", | |
904 | oid_to_hex(&cmd->old_oid), oid_to_hex(&cmd->new_oid), | |
905 | cmd->ref_name); | |
906 | state->cmd = cmd->next; | |
907 | } | |
9684e44a JH |
908 | if (bufp) { |
909 | *bufp = state->buf.buf; | |
910 | *sizep = state->buf.len; | |
911 | } | |
912 | return 0; | |
913 | } | |
914 | ||
77a9745d SB |
915 | static int run_receive_hook(struct command *commands, |
916 | const char *hook_name, | |
917 | int skip_broken, | |
918 | const struct string_list *push_options) | |
9684e44a JH |
919 | { |
920 | struct receive_hook_feed_state state; | |
921 | int status; | |
922 | ||
923 | strbuf_init(&state.buf, 0); | |
924 | state.cmd = commands; | |
cdc2b2f3 | 925 | state.skip_broken = skip_broken; |
195d6eae | 926 | state.report = NULL; |
9684e44a JH |
927 | if (feed_receive_hook(&state, NULL, NULL)) |
928 | return 0; | |
929 | state.cmd = commands; | |
77a9745d | 930 | state.push_options = push_options; |
9684e44a JH |
931 | status = run_and_feed_hook(hook_name, feed_receive_hook, &state); |
932 | strbuf_release(&state.buf); | |
933 | return status; | |
934 | } | |
935 | ||
1d9e8b56 SP |
936 | static int run_update_hook(struct command *cmd) |
937 | { | |
d3180279 | 938 | struct child_process proc = CHILD_PROCESS_INIT; |
6d525d38 | 939 | int code; |
7f14609e | 940 | const char *hook_path = find_hook("update"); |
1d9e8b56 | 941 | |
7f14609e | 942 | if (!hook_path) |
1d9e8b56 SP |
943 | return 0; |
944 | ||
7f14609e ÆAB |
945 | strvec_push(&proc.args, hook_path); |
946 | strvec_push(&proc.args, cmd->ref_name); | |
947 | strvec_push(&proc.args, oid_to_hex(&cmd->old_oid)); | |
948 | strvec_push(&proc.args, oid_to_hex(&cmd->new_oid)); | |
1d9e8b56 | 949 | |
6d525d38 SP |
950 | proc.no_stdin = 1; |
951 | proc.stdout_to_stderr = 1; | |
952 | proc.err = use_sideband ? -1 : 0; | |
6206286e | 953 | proc.trace2_hook_name = "update"; |
6d525d38 SP |
954 | |
955 | code = start_command(&proc); | |
956 | if (code) | |
957 | return code; | |
958 | if (use_sideband) | |
959 | copy_to_sideband(proc.err, -1, NULL); | |
960 | return finish_command(&proc); | |
1d9e8b56 SP |
961 | } |
962 | ||
15d3af5e JX |
963 | static struct command *find_command_by_refname(struct command *list, |
964 | const char *refname) | |
965 | { | |
966 | for (; list; list = list->next) | |
967 | if (!strcmp(list->ref_name, refname)) | |
968 | return list; | |
969 | return NULL; | |
970 | } | |
971 | ||
972 | static int read_proc_receive_report(struct packet_reader *reader, | |
973 | struct command *commands, | |
974 | struct strbuf *errmsg) | |
975 | { | |
976 | struct command *cmd; | |
977 | struct command *hint = NULL; | |
978 | struct ref_push_report *report = NULL; | |
979 | int new_report = 0; | |
980 | int code = 0; | |
981 | int once = 0; | |
f65003b4 | 982 | int response = 0; |
15d3af5e JX |
983 | |
984 | for (;;) { | |
985 | struct object_id old_oid, new_oid; | |
986 | const char *head; | |
987 | const char *refname; | |
988 | char *p; | |
f65003b4 JX |
989 | enum packet_read_status status; |
990 | ||
991 | status = packet_reader_read(reader); | |
992 | if (status != PACKET_READ_NORMAL) { | |
993 | /* Check whether proc-receive exited abnormally */ | |
994 | if (status == PACKET_READ_EOF && !response) { | |
995 | strbuf_addstr(errmsg, "proc-receive exited abnormally"); | |
996 | return -1; | |
997 | } | |
15d3af5e | 998 | break; |
f65003b4 JX |
999 | } |
1000 | response++; | |
15d3af5e JX |
1001 | |
1002 | head = reader->line; | |
1003 | p = strchr(head, ' '); | |
1004 | if (!p) { | |
1005 | strbuf_addf(errmsg, "proc-receive reported incomplete status line: '%s'\n", head); | |
1006 | code = -1; | |
1007 | continue; | |
1008 | } | |
1009 | *p++ = '\0'; | |
1010 | if (!strcmp(head, "option")) { | |
1011 | const char *key, *val; | |
1012 | ||
1013 | if (!hint || !(report || new_report)) { | |
1014 | if (!once++) | |
1015 | strbuf_addstr(errmsg, "proc-receive reported 'option' without a matching 'ok/ng' directive\n"); | |
1016 | code = -1; | |
1017 | continue; | |
1018 | } | |
1019 | if (new_report) { | |
1020 | if (!hint->report) { | |
ca56dadb | 1021 | CALLOC_ARRAY(hint->report, 1); |
15d3af5e JX |
1022 | report = hint->report; |
1023 | } else { | |
1024 | report = hint->report; | |
1025 | while (report->next) | |
1026 | report = report->next; | |
1027 | report->next = xcalloc(1, sizeof(struct ref_push_report)); | |
1028 | report = report->next; | |
1029 | } | |
1030 | new_report = 0; | |
1031 | } | |
1032 | key = p; | |
1033 | p = strchr(key, ' '); | |
1034 | if (p) | |
1035 | *p++ = '\0'; | |
1036 | val = p; | |
1037 | if (!strcmp(key, "refname")) | |
1038 | report->ref_name = xstrdup_or_null(val); | |
1039 | else if (!strcmp(key, "old-oid") && val && | |
1040 | !parse_oid_hex(val, &old_oid, &val)) | |
1041 | report->old_oid = oiddup(&old_oid); | |
1042 | else if (!strcmp(key, "new-oid") && val && | |
1043 | !parse_oid_hex(val, &new_oid, &val)) | |
1044 | report->new_oid = oiddup(&new_oid); | |
1045 | else if (!strcmp(key, "forced-update")) | |
1046 | report->forced_update = 1; | |
1047 | else if (!strcmp(key, "fall-through")) | |
1048 | /* Fall through, let 'receive-pack' to execute it. */ | |
1049 | hint->run_proc_receive = 0; | |
1050 | continue; | |
1051 | } | |
1052 | ||
1053 | report = NULL; | |
1054 | new_report = 0; | |
1055 | refname = p; | |
1056 | p = strchr(refname, ' '); | |
1057 | if (p) | |
1058 | *p++ = '\0'; | |
1059 | if (strcmp(head, "ok") && strcmp(head, "ng")) { | |
1060 | strbuf_addf(errmsg, "proc-receive reported bad status '%s' on ref '%s'\n", | |
1061 | head, refname); | |
1062 | code = -1; | |
1063 | continue; | |
1064 | } | |
1065 | ||
1066 | /* first try searching at our hint, falling back to all refs */ | |
1067 | if (hint) | |
1068 | hint = find_command_by_refname(hint, refname); | |
1069 | if (!hint) | |
1070 | hint = find_command_by_refname(commands, refname); | |
1071 | if (!hint) { | |
1072 | strbuf_addf(errmsg, "proc-receive reported status on unknown ref: %s\n", | |
1073 | refname); | |
1074 | code = -1; | |
1075 | continue; | |
1076 | } | |
1077 | if (!hint->run_proc_receive) { | |
1078 | strbuf_addf(errmsg, "proc-receive reported status on unexpected ref: %s\n", | |
1079 | refname); | |
1080 | code = -1; | |
1081 | continue; | |
1082 | } | |
1083 | hint->run_proc_receive |= RUN_PROC_RECEIVE_RETURNED; | |
1084 | if (!strcmp(head, "ng")) { | |
1085 | if (p) | |
1086 | hint->error_string = xstrdup(p); | |
1087 | else | |
1088 | hint->error_string = "failed"; | |
1089 | code = -1; | |
1090 | continue; | |
1091 | } | |
1092 | new_report = 1; | |
1093 | } | |
1094 | ||
1095 | for (cmd = commands; cmd; cmd = cmd->next) | |
1096 | if (cmd->run_proc_receive && !cmd->error_string && | |
1097 | !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED)) { | |
1098 | cmd->error_string = "proc-receive failed to report status"; | |
1099 | code = -1; | |
1100 | } | |
1101 | return code; | |
1102 | } | |
1103 | ||
1104 | static int run_proc_receive_hook(struct command *commands, | |
1105 | const struct string_list *push_options) | |
1106 | { | |
1107 | struct child_process proc = CHILD_PROCESS_INIT; | |
1108 | struct async muxer; | |
1109 | struct command *cmd; | |
15d3af5e JX |
1110 | struct packet_reader reader; |
1111 | struct strbuf cap = STRBUF_INIT; | |
1112 | struct strbuf errmsg = STRBUF_INIT; | |
1113 | int hook_use_push_options = 0; | |
1114 | int version = 0; | |
1115 | int code; | |
7f14609e | 1116 | const char *hook_path = find_hook("proc-receive"); |
15d3af5e | 1117 | |
7f14609e | 1118 | if (!hook_path) { |
15d3af5e JX |
1119 | rp_error("cannot find hook 'proc-receive'"); |
1120 | return -1; | |
1121 | } | |
15d3af5e | 1122 | |
7f14609e | 1123 | strvec_push(&proc.args, hook_path); |
15d3af5e JX |
1124 | proc.in = -1; |
1125 | proc.out = -1; | |
1126 | proc.trace2_hook_name = "proc-receive"; | |
1127 | ||
1128 | if (use_sideband) { | |
1129 | memset(&muxer, 0, sizeof(muxer)); | |
1130 | muxer.proc = copy_to_sideband; | |
1131 | muxer.in = -1; | |
1132 | code = start_async(&muxer); | |
1133 | if (code) | |
1134 | return code; | |
1135 | proc.err = muxer.in; | |
1136 | } else { | |
1137 | proc.err = 0; | |
1138 | } | |
1139 | ||
1140 | code = start_command(&proc); | |
1141 | if (code) { | |
1142 | if (use_sideband) | |
1143 | finish_async(&muxer); | |
1144 | return code; | |
1145 | } | |
1146 | ||
1147 | sigchain_push(SIGPIPE, SIG_IGN); | |
1148 | ||
1149 | /* Version negotiaton */ | |
1150 | packet_reader_init(&reader, proc.out, NULL, 0, | |
1151 | PACKET_READ_CHOMP_NEWLINE | | |
1152 | PACKET_READ_GENTLE_ON_EOF); | |
1153 | if (use_atomic) | |
1154 | strbuf_addstr(&cap, " atomic"); | |
1155 | if (use_push_options) | |
1156 | strbuf_addstr(&cap, " push-options"); | |
1157 | if (cap.len) { | |
f65003b4 | 1158 | code = packet_write_fmt_gently(proc.in, "version=1%c%s\n", '\0', cap.buf + 1); |
15d3af5e JX |
1159 | strbuf_release(&cap); |
1160 | } else { | |
f65003b4 | 1161 | code = packet_write_fmt_gently(proc.in, "version=1\n"); |
15d3af5e | 1162 | } |
f65003b4 JX |
1163 | if (!code) |
1164 | code = packet_flush_gently(proc.in); | |
1165 | ||
1166 | if (!code) | |
1167 | for (;;) { | |
1168 | int linelen; | |
1169 | enum packet_read_status status; | |
1170 | ||
1171 | status = packet_reader_read(&reader); | |
1172 | if (status != PACKET_READ_NORMAL) { | |
1173 | /* Check whether proc-receive exited abnormally */ | |
1174 | if (status == PACKET_READ_EOF) | |
1175 | code = -1; | |
1176 | break; | |
1177 | } | |
15d3af5e | 1178 | |
f65003b4 JX |
1179 | if (reader.pktlen > 8 && starts_with(reader.line, "version=")) { |
1180 | version = atoi(reader.line + 8); | |
1181 | linelen = strlen(reader.line); | |
1182 | if (linelen < reader.pktlen) { | |
1183 | const char *feature_list = reader.line + linelen + 1; | |
1184 | if (parse_feature_request(feature_list, "push-options")) | |
1185 | hook_use_push_options = 1; | |
1186 | } | |
15d3af5e JX |
1187 | } |
1188 | } | |
f65003b4 JX |
1189 | |
1190 | if (code) { | |
1191 | strbuf_addstr(&errmsg, "fail to negotiate version with proc-receive hook"); | |
1192 | goto cleanup; | |
15d3af5e JX |
1193 | } |
1194 | ||
80ffeb94 JX |
1195 | switch (version) { |
1196 | case 0: | |
1197 | /* fallthrough */ | |
1198 | case 1: | |
1199 | break; | |
1200 | default: | |
15d3af5e JX |
1201 | strbuf_addf(&errmsg, "proc-receive version '%d' is not supported", |
1202 | version); | |
1203 | code = -1; | |
1204 | goto cleanup; | |
1205 | } | |
1206 | ||
1207 | /* Send commands */ | |
1208 | for (cmd = commands; cmd; cmd = cmd->next) { | |
1209 | if (!cmd->run_proc_receive || cmd->skip_update || cmd->error_string) | |
1210 | continue; | |
f65003b4 JX |
1211 | code = packet_write_fmt_gently(proc.in, "%s %s %s", |
1212 | oid_to_hex(&cmd->old_oid), | |
1213 | oid_to_hex(&cmd->new_oid), | |
1214 | cmd->ref_name); | |
1215 | if (code) | |
1216 | break; | |
1217 | } | |
1218 | if (!code) | |
1219 | code = packet_flush_gently(proc.in); | |
1220 | if (code) { | |
1221 | strbuf_addstr(&errmsg, "fail to write commands to proc-receive hook"); | |
1222 | goto cleanup; | |
15d3af5e | 1223 | } |
15d3af5e JX |
1224 | |
1225 | /* Send push options */ | |
1226 | if (hook_use_push_options) { | |
1227 | struct string_list_item *item; | |
1228 | ||
f65003b4 JX |
1229 | for_each_string_list_item(item, push_options) { |
1230 | code = packet_write_fmt_gently(proc.in, "%s", item->string); | |
1231 | if (code) | |
1232 | break; | |
1233 | } | |
1234 | if (!code) | |
1235 | code = packet_flush_gently(proc.in); | |
1236 | if (code) { | |
1237 | strbuf_addstr(&errmsg, | |
1238 | "fail to write push-options to proc-receive hook"); | |
1239 | goto cleanup; | |
1240 | } | |
15d3af5e JX |
1241 | } |
1242 | ||
1243 | /* Read result from proc-receive */ | |
1244 | code = read_proc_receive_report(&reader, commands, &errmsg); | |
1245 | ||
1246 | cleanup: | |
1247 | close(proc.in); | |
1248 | close(proc.out); | |
1249 | if (use_sideband) | |
1250 | finish_async(&muxer); | |
1251 | if (finish_command(&proc)) | |
1252 | code = -1; | |
1253 | if (errmsg.len >0) { | |
1254 | char *p = errmsg.buf; | |
1255 | ||
1256 | p += errmsg.len - 1; | |
1257 | if (*p == '\n') | |
1258 | *p = '\0'; | |
1259 | rp_error("%s", errmsg.buf); | |
1260 | strbuf_release(&errmsg); | |
1261 | } | |
1262 | sigchain_pop(SIGPIPE); | |
1263 | ||
1264 | return code; | |
1265 | } | |
1266 | ||
8ba35a2d VA |
1267 | static char *refuse_unconfigured_deny_msg = |
1268 | N_("By default, updating the current branch in a non-bare repository\n" | |
1269 | "is denied, because it will make the index and work tree inconsistent\n" | |
1270 | "with what you pushed, and will require 'git reset --hard' to match\n" | |
1271 | "the work tree to HEAD.\n" | |
1272 | "\n" | |
2ddaa427 AH |
1273 | "You can set the 'receive.denyCurrentBranch' configuration variable\n" |
1274 | "to 'ignore' or 'warn' in the remote repository to allow pushing into\n" | |
8ba35a2d VA |
1275 | "its current branch; however, this is not recommended unless you\n" |
1276 | "arranged to update its work tree to match what you pushed in some\n" | |
1277 | "other way.\n" | |
1278 | "\n" | |
1279 | "To squelch this message and still keep the default behaviour, set\n" | |
1280 | "'receive.denyCurrentBranch' configuration variable to 'refuse'."); | |
3d95d92b | 1281 | |
acd2a45b | 1282 | static void refuse_unconfigured_deny(void) |
3d95d92b | 1283 | { |
8ba35a2d | 1284 | rp_error("%s", _(refuse_unconfigured_deny_msg)); |
3d95d92b JH |
1285 | } |
1286 | ||
8ba35a2d VA |
1287 | static char *refuse_unconfigured_deny_delete_current_msg = |
1288 | N_("By default, deleting the current branch is denied, because the next\n" | |
1289 | "'git clone' won't result in any file checked out, causing confusion.\n" | |
1290 | "\n" | |
1291 | "You can set 'receive.denyDeleteCurrent' configuration variable to\n" | |
1292 | "'warn' or 'ignore' in the remote repository to allow deleting the\n" | |
1293 | "current branch, with or without a warning message.\n" | |
1294 | "\n" | |
1295 | "To squelch this message, you can set it to 'refuse'."); | |
747ca245 | 1296 | |
375881fa | 1297 | static void refuse_unconfigured_deny_delete_current(void) |
747ca245 | 1298 | { |
8ba35a2d | 1299 | rp_error("%s", _(refuse_unconfigured_deny_delete_current_msg)); |
747ca245 JH |
1300 | } |
1301 | ||
9fec7b21 | 1302 | static const struct object_id *command_singleton_iterator(void *cb_data); |
0a1bc12b NTND |
1303 | static int update_shallow_ref(struct command *cmd, struct shallow_info *si) |
1304 | { | |
cac4b8e2 | 1305 | struct shallow_lock shallow_lock = SHALLOW_LOCK_INIT; |
910650d2 | 1306 | struct oid_array extra = OID_ARRAY_INIT; |
7043c707 | 1307 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
0a1bc12b NTND |
1308 | uint32_t mask = 1 << (cmd->index % 32); |
1309 | int i; | |
1310 | ||
6aa30857 | 1311 | trace_printf_key(&trace_shallow, |
0a1bc12b NTND |
1312 | "shallow: update_shallow_ref %s\n", cmd->ref_name); |
1313 | for (i = 0; i < si->shallow->nr; i++) | |
1314 | if (si->used_shallow[i] && | |
1315 | (si->used_shallow[i][cmd->index / 32] & mask) && | |
1316 | !delayed_reachability_test(si, i)) | |
910650d2 | 1317 | oid_array_append(&extra, &si->shallow->oid[i]); |
0a1bc12b | 1318 | |
722ff7f8 | 1319 | opt.env = tmp_objdir_env(tmp_objdir); |
7043c707 JK |
1320 | setup_alternate_shallow(&shallow_lock, &opt.shallow_file, &extra); |
1321 | if (check_connected(command_singleton_iterator, cmd, &opt)) { | |
37b9dcab | 1322 | rollback_shallow_file(the_repository, &shallow_lock); |
910650d2 | 1323 | oid_array_clear(&extra); |
0a1bc12b NTND |
1324 | return -1; |
1325 | } | |
1326 | ||
37b9dcab | 1327 | commit_shallow_file(the_repository, &shallow_lock); |
0a1bc12b NTND |
1328 | |
1329 | /* | |
1330 | * Make sure setup_alternate_shallow() for the next ref does | |
1331 | * not lose these new roots.. | |
1332 | */ | |
1333 | for (i = 0; i < extra.nr; i++) | |
19143f13 | 1334 | register_shallow(the_repository, &extra.oid[i]); |
0a1bc12b NTND |
1335 | |
1336 | si->shallow_ref[cmd->index] = 0; | |
910650d2 | 1337 | oid_array_clear(&extra); |
0a1bc12b NTND |
1338 | return 0; |
1339 | } | |
1340 | ||
1a51b524 JH |
1341 | /* |
1342 | * NEEDSWORK: we should consolidate various implementions of "are we | |
1343 | * on an unborn branch?" test into one, and make the unified one more | |
1344 | * robust. !get_sha1() based check used here and elsewhere would not | |
1345 | * allow us to tell an unborn branch from corrupt ref, for example. | |
1346 | * For the purpose of fixing "deploy-to-update does not work when | |
1347 | * pushing into an empty repository" issue, this should suffice for | |
1348 | * now. | |
1349 | */ | |
1350 | static int head_has_history(void) | |
1351 | { | |
15be4a5d | 1352 | struct object_id oid; |
1a51b524 | 1353 | |
d850b7a5 | 1354 | return !repo_get_oid(the_repository, "HEAD", &oid); |
1a51b524 JH |
1355 | } |
1356 | ||
21b138d0 | 1357 | static const char *push_to_deploy(unsigned char *sha1, |
22f9b7f3 | 1358 | struct strvec *env, |
21b138d0 | 1359 | const char *work_tree) |
1404bcbb | 1360 | { |
1404bcbb JS |
1361 | struct child_process child = CHILD_PROCESS_INIT; |
1362 | ||
2b709893 ÆAB |
1363 | strvec_pushl(&child.args, "update-index", "-q", "--ignore-submodules", |
1364 | "--refresh", NULL); | |
29fda24d | 1365 | strvec_pushv(&child.env, env->v); |
1404bcbb JS |
1366 | child.dir = work_tree; |
1367 | child.no_stdin = 1; | |
1368 | child.stdout_to_stderr = 1; | |
1369 | child.git_cmd = 1; | |
21b138d0 | 1370 | if (run_command(&child)) |
1404bcbb | 1371 | return "Up-to-date check failed"; |
1404bcbb JS |
1372 | |
1373 | /* run_command() does not clean up completely; reinitialize */ | |
1374 | child_process_init(&child); | |
2b709893 ÆAB |
1375 | strvec_pushl(&child.args, "diff-files", "--quiet", |
1376 | "--ignore-submodules", "--", NULL); | |
29fda24d | 1377 | strvec_pushv(&child.env, env->v); |
1404bcbb JS |
1378 | child.dir = work_tree; |
1379 | child.no_stdin = 1; | |
1380 | child.stdout_to_stderr = 1; | |
1381 | child.git_cmd = 1; | |
21b138d0 | 1382 | if (run_command(&child)) |
1404bcbb | 1383 | return "Working directory has unstaged changes"; |
1404bcbb JS |
1384 | |
1385 | child_process_init(&child); | |
2b709893 ÆAB |
1386 | strvec_pushl(&child.args, "diff-index", "--quiet", "--cached", |
1387 | "--ignore-submodules", | |
1388 | /* diff-index with either HEAD or an empty tree */ | |
1389 | head_has_history() ? "HEAD" : empty_tree_oid_hex(), | |
1390 | "--", NULL); | |
29fda24d | 1391 | strvec_pushv(&child.env, env->v); |
1404bcbb JS |
1392 | child.no_stdin = 1; |
1393 | child.no_stdout = 1; | |
1394 | child.stdout_to_stderr = 0; | |
1395 | child.git_cmd = 1; | |
21b138d0 | 1396 | if (run_command(&child)) |
1404bcbb | 1397 | return "Working directory has staged changes"; |
1404bcbb | 1398 | |
1404bcbb | 1399 | child_process_init(&child); |
2b709893 ÆAB |
1400 | strvec_pushl(&child.args, "read-tree", "-u", "-m", hash_to_hex(sha1), |
1401 | NULL); | |
29fda24d | 1402 | strvec_pushv(&child.env, env->v); |
1404bcbb JS |
1403 | child.dir = work_tree; |
1404 | child.no_stdin = 1; | |
1405 | child.no_stdout = 1; | |
1406 | child.stdout_to_stderr = 0; | |
1407 | child.git_cmd = 1; | |
21b138d0 | 1408 | if (run_command(&child)) |
1404bcbb | 1409 | return "Could not update working tree to new HEAD"; |
1404bcbb | 1410 | |
1404bcbb JS |
1411 | return NULL; |
1412 | } | |
1413 | ||
08553319 JH |
1414 | static const char *push_to_checkout_hook = "push-to-checkout"; |
1415 | ||
fc06be3b | 1416 | static const char *push_to_checkout(unsigned char *hash, |
a8cc5943 | 1417 | int *invoked_hook, |
22f9b7f3 | 1418 | struct strvec *env, |
08553319 JH |
1419 | const char *work_tree) |
1420 | { | |
306f445e | 1421 | struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT; |
a8cc5943 | 1422 | opt.invoked_hook = invoked_hook; |
306f445e | 1423 | |
22f9b7f3 | 1424 | strvec_pushf(env, "GIT_WORK_TREE=%s", absolute_path(work_tree)); |
306f445e ES |
1425 | strvec_pushv(&opt.env, env->v); |
1426 | strvec_push(&opt.args, hash_to_hex(hash)); | |
1427 | if (run_hooks_opt(push_to_checkout_hook, &opt)) | |
08553319 JH |
1428 | return "push-to-checkout hook declined"; |
1429 | else | |
1430 | return NULL; | |
1431 | } | |
1432 | ||
4ef34648 | 1433 | static const char *update_worktree(unsigned char *sha1, const struct worktree *worktree) |
21b138d0 | 1434 | { |
38baae6c | 1435 | const char *retval, *git_dir; |
22f9b7f3 | 1436 | struct strvec env = STRVEC_INIT; |
a8cc5943 | 1437 | int invoked_hook; |
21b138d0 | 1438 | |
38baae6c AK |
1439 | if (!worktree || !worktree->path) |
1440 | BUG("worktree->path must be non-NULL"); | |
4ef34648 | 1441 | |
9fdf4f1d | 1442 | if (worktree->is_bare) |
21b138d0 | 1443 | return "denyCurrentBranch = updateInstead needs a worktree"; |
38baae6c | 1444 | git_dir = get_worktree_git_dir(worktree); |
21b138d0 | 1445 | |
22f9b7f3 | 1446 | strvec_pushf(&env, "GIT_DIR=%s", absolute_path(git_dir)); |
21b138d0 | 1447 | |
a8cc5943 ÆAB |
1448 | retval = push_to_checkout(sha1, &invoked_hook, &env, worktree->path); |
1449 | if (!invoked_hook) | |
38baae6c | 1450 | retval = push_to_deploy(sha1, &env, worktree->path); |
21b138d0 | 1451 | |
22f9b7f3 | 1452 | strvec_clear(&env); |
21b138d0 JH |
1453 | return retval; |
1454 | } | |
1455 | ||
0a1bc12b | 1456 | static const char *update(struct command *cmd, struct shallow_info *si) |
2eca23da | 1457 | { |
cfee10a7 | 1458 | const char *name = cmd->ref_name; |
6b01ecfe | 1459 | struct strbuf namespaced_name_buf = STRBUF_INIT; |
bda6e828 JS |
1460 | static char *namespaced_name; |
1461 | const char *ret; | |
9c44ea44 | 1462 | struct object_id *old_oid = &cmd->old_oid; |
1463 | struct object_id *new_oid = &cmd->new_oid; | |
b072a25f | 1464 | int do_update_worktree = 0; |
c8dd491f AK |
1465 | struct worktree **worktrees = get_worktrees(); |
1466 | const struct worktree *worktree = | |
9fdf4f1d | 1467 | find_shared_symref(worktrees, "HEAD", name); |
2eca23da | 1468 | |
061d6b9a | 1469 | /* only refs/... are allowed */ |
7c3c5502 ZH |
1470 | if (!starts_with(name, "refs/") || |
1471 | check_refname_format(name + 5, is_null_oid(new_oid) ? | |
1472 | REFNAME_ALLOW_ONELEVEL : 0)) { | |
d81ba50a | 1473 | rp_error("refusing to update funny ref '%s' remotely", name); |
c8dd491f AK |
1474 | ret = "funny refname"; |
1475 | goto out; | |
cfee10a7 | 1476 | } |
d8a1deec | 1477 | |
6b01ecfe | 1478 | strbuf_addf(&namespaced_name_buf, "%s%s", get_git_namespace(), name); |
bda6e828 | 1479 | free(namespaced_name); |
6b01ecfe JT |
1480 | namespaced_name = strbuf_detach(&namespaced_name_buf, NULL); |
1481 | ||
9fdf4f1d | 1482 | if (worktree && !worktree->is_bare) { |
3d95d92b JH |
1483 | switch (deny_current_branch) { |
1484 | case DENY_IGNORE: | |
986e8239 | 1485 | break; |
3d95d92b | 1486 | case DENY_WARN: |
466dbc42 | 1487 | rp_warning("updating the current branch"); |
986e8239 | 1488 | break; |
3d95d92b | 1489 | case DENY_REFUSE: |
acd2a45b | 1490 | case DENY_UNCONFIGURED: |
466dbc42 | 1491 | rp_error("refusing to update checked out branch: %s", name); |
acd2a45b JH |
1492 | if (deny_current_branch == DENY_UNCONFIGURED) |
1493 | refuse_unconfigured_deny(); | |
c8dd491f AK |
1494 | ret = "branch is currently checked out"; |
1495 | goto out; | |
1404bcbb | 1496 | case DENY_UPDATE_INSTEAD: |
b072a25f JH |
1497 | /* pass -- let other checks intervene first */ |
1498 | do_update_worktree = 1; | |
1404bcbb | 1499 | break; |
3d95d92b | 1500 | } |
986e8239 JK |
1501 | } |
1502 | ||
bc726bd0 | 1503 | if (!is_null_oid(new_oid) && !repo_has_object_file(the_repository, new_oid)) { |
8aaf7d64 | 1504 | error("unpack should have generated %s, " |
9c44ea44 | 1505 | "but I can't find it!", oid_to_hex(new_oid)); |
c8dd491f AK |
1506 | ret = "bad pack"; |
1507 | goto out; | |
cfee10a7 | 1508 | } |
747ca245 | 1509 | |
9c44ea44 | 1510 | if (!is_null_oid(old_oid) && is_null_oid(new_oid)) { |
59556548 | 1511 | if (deny_deletes && starts_with(name, "refs/heads/")) { |
466dbc42 | 1512 | rp_error("denying ref deletion for %s", name); |
c8dd491f AK |
1513 | ret = "deletion prohibited"; |
1514 | goto out; | |
747ca245 JH |
1515 | } |
1516 | ||
4ef34648 | 1517 | if (worktree || (head_name && !strcmp(namespaced_name, head_name))) { |
747ca245 JH |
1518 | switch (deny_delete_current) { |
1519 | case DENY_IGNORE: | |
1520 | break; | |
1521 | case DENY_WARN: | |
466dbc42 | 1522 | rp_warning("deleting the current branch"); |
747ca245 JH |
1523 | break; |
1524 | case DENY_REFUSE: | |
375881fa | 1525 | case DENY_UNCONFIGURED: |
1404bcbb | 1526 | case DENY_UPDATE_INSTEAD: |
375881fa JH |
1527 | if (deny_delete_current == DENY_UNCONFIGURED) |
1528 | refuse_unconfigured_deny_delete_current(); | |
466dbc42 | 1529 | rp_error("refusing to delete the current branch: %s", name); |
c8dd491f AK |
1530 | ret = "deletion of the current branch prohibited"; |
1531 | goto out; | |
1404bcbb | 1532 | default: |
c8dd491f AK |
1533 | ret = "Invalid denyDeleteCurrent setting"; |
1534 | goto out; | |
747ca245 JH |
1535 | } |
1536 | } | |
a240de11 | 1537 | } |
747ca245 | 1538 | |
9c44ea44 | 1539 | if (deny_non_fast_forwards && !is_null_oid(new_oid) && |
1540 | !is_null_oid(old_oid) && | |
59556548 | 1541 | starts_with(name, "refs/heads/")) { |
eab82707 | 1542 | struct object *old_object, *new_object; |
11031d7e | 1543 | struct commit *old_commit, *new_commit; |
11031d7e | 1544 | |
109cd76d SB |
1545 | old_object = parse_object(the_repository, old_oid); |
1546 | new_object = parse_object(the_repository, new_oid); | |
eab82707 MK |
1547 | |
1548 | if (!old_object || !new_object || | |
1549 | old_object->type != OBJ_COMMIT || | |
1550 | new_object->type != OBJ_COMMIT) { | |
1551 | error("bad sha1 objects for %s", name); | |
c8dd491f AK |
1552 | ret = "bad ref"; |
1553 | goto out; | |
eab82707 MK |
1554 | } |
1555 | old_commit = (struct commit *)old_object; | |
1556 | new_commit = (struct commit *)new_object; | |
cb338c23 | 1557 | if (!repo_in_merge_bases(the_repository, old_commit, new_commit)) { |
466dbc42 SP |
1558 | rp_error("denying non-fast-forward %s" |
1559 | " (you should pull first)", name); | |
c8dd491f AK |
1560 | ret = "non-fast-forward"; |
1561 | goto out; | |
8aaf7d64 | 1562 | } |
11031d7e | 1563 | } |
1d9e8b56 | 1564 | if (run_update_hook(cmd)) { |
466dbc42 | 1565 | rp_error("hook declined to update %s", name); |
c8dd491f AK |
1566 | ret = "hook declined"; |
1567 | goto out; | |
b1bf95bb | 1568 | } |
3159c8dc | 1569 | |
b072a25f | 1570 | if (do_update_worktree) { |
38baae6c | 1571 | ret = update_worktree(new_oid->hash, worktree); |
b072a25f | 1572 | if (ret) |
c8dd491f | 1573 | goto out; |
b072a25f JH |
1574 | } |
1575 | ||
9c44ea44 | 1576 | if (is_null_oid(new_oid)) { |
222368c6 | 1577 | struct strbuf err = STRBUF_INIT; |
109cd76d | 1578 | if (!parse_object(the_repository, old_oid)) { |
9c44ea44 | 1579 | old_oid = NULL; |
160b81ed | 1580 | if (ref_exists(name)) { |
c25edee9 | 1581 | rp_warning("allowing deletion of corrupt ref"); |
160b81ed | 1582 | } else { |
c25edee9 | 1583 | rp_warning("deleting a non-existent ref"); |
160b81ed PYH |
1584 | cmd->did_not_exist = 1; |
1585 | } | |
28391a80 | 1586 | } |
222368c6 SB |
1587 | if (ref_transaction_delete(transaction, |
1588 | namespaced_name, | |
89f3bbdd | 1589 | old_oid, |
fb5a6bb6 | 1590 | 0, "push", &err)) { |
222368c6 | 1591 | rp_error("%s", err.buf); |
c8dd491f AK |
1592 | ret = "failed to delete"; |
1593 | } else { | |
1594 | ret = NULL; /* good */ | |
d4f694ba | 1595 | } |
222368c6 | 1596 | strbuf_release(&err); |
d4f694ba JH |
1597 | } |
1598 | else { | |
6629ea2d | 1599 | struct strbuf err = STRBUF_INIT; |
0a1bc12b | 1600 | if (shallow_update && si->shallow_ref[cmd->index] && |
c8dd491f AK |
1601 | update_shallow_ref(cmd, si)) { |
1602 | ret = "shallow error"; | |
1603 | goto out; | |
1604 | } | |
0a1bc12b | 1605 | |
222368c6 SB |
1606 | if (ref_transaction_update(transaction, |
1607 | namespaced_name, | |
89f3bbdd | 1608 | new_oid, old_oid, |
1d147bdf | 1609 | 0, "push", |
222368c6 | 1610 | &err)) { |
6629ea2d | 1611 | rp_error("%s", err.buf); |
c8dd491f AK |
1612 | ret = "failed to update ref"; |
1613 | } else { | |
1614 | ret = NULL; /* good */ | |
ef203f08 | 1615 | } |
6629ea2d | 1616 | strbuf_release(&err); |
19614330 | 1617 | } |
c8dd491f AK |
1618 | |
1619 | out: | |
1620 | free_worktrees(worktrees); | |
1621 | return ret; | |
2eca23da LT |
1622 | } |
1623 | ||
5e1c71fd | 1624 | static void run_update_post_hook(struct command *commands) |
19614330 | 1625 | { |
5e1c71fd | 1626 | struct command *cmd; |
d3180279 | 1627 | struct child_process proc = CHILD_PROCESS_INIT; |
dcf69262 | 1628 | const char *hook; |
19614330 | 1629 | |
5a7da2dc | 1630 | hook = find_hook("post-update"); |
dce96c41 | 1631 | if (!hook) |
3e6e152c | 1632 | return; |
5a7da2dc | 1633 | |
850d2fec | 1634 | for (cmd = commands; cmd; cmd = cmd->next) { |
160b81ed | 1635 | if (cmd->error_string || cmd->did_not_exist) |
19614330 | 1636 | continue; |
d70a9eb6 | 1637 | if (!proc.args.nr) |
22f9b7f3 JK |
1638 | strvec_push(&proc.args, hook); |
1639 | strvec_push(&proc.args, cmd->ref_name); | |
19614330 | 1640 | } |
d70a9eb6 | 1641 | if (!proc.args.nr) |
dce96c41 | 1642 | return; |
6d525d38 | 1643 | |
6d525d38 SP |
1644 | proc.no_stdin = 1; |
1645 | proc.stdout_to_stderr = 1; | |
1646 | proc.err = use_sideband ? -1 : 0; | |
6206286e | 1647 | proc.trace2_hook_name = "post-update"; |
6d525d38 SP |
1648 | |
1649 | if (!start_command(&proc)) { | |
1650 | if (use_sideband) | |
1651 | copy_to_sideband(proc.err, -1, NULL); | |
1652 | finish_command(&proc); | |
1653 | } | |
19614330 | 1654 | } |
2eca23da | 1655 | |
99036237 ÆAB |
1656 | static void check_aliased_update_internal(struct command *cmd, |
1657 | struct string_list *list, | |
1658 | const char *dst_name, int flag) | |
da3efdb1 JS |
1659 | { |
1660 | struct string_list_item *item; | |
1661 | struct command *dst_cmd; | |
da3efdb1 JS |
1662 | |
1663 | if (!(flag & REF_ISSYMREF)) | |
1664 | return; | |
1665 | ||
6b01ecfe JT |
1666 | if (!dst_name) { |
1667 | rp_error("refusing update to broken symref '%s'", cmd->ref_name); | |
1668 | cmd->skip_update = 1; | |
1669 | cmd->error_string = "broken symref"; | |
1670 | return; | |
1671 | } | |
ded83936 | 1672 | dst_name = strip_namespace(dst_name); |
6b01ecfe | 1673 | |
afe8a907 | 1674 | if (!(item = string_list_lookup(list, dst_name))) |
da3efdb1 JS |
1675 | return; |
1676 | ||
1677 | cmd->skip_update = 1; | |
1678 | ||
1679 | dst_cmd = (struct command *) item->util; | |
1680 | ||
4a7e27e9 JK |
1681 | if (oideq(&cmd->old_oid, &dst_cmd->old_oid) && |
1682 | oideq(&cmd->new_oid, &dst_cmd->new_oid)) | |
da3efdb1 JS |
1683 | return; |
1684 | ||
1685 | dst_cmd->skip_update = 1; | |
1686 | ||
da3efdb1 JS |
1687 | rp_error("refusing inconsistent update between symref '%s' (%s..%s) and" |
1688 | " its target '%s' (%s..%s)", | |
ef2ed501 | 1689 | cmd->ref_name, |
d850b7a5 ÆAB |
1690 | repo_find_unique_abbrev(the_repository, &cmd->old_oid, DEFAULT_ABBREV), |
1691 | repo_find_unique_abbrev(the_repository, &cmd->new_oid, DEFAULT_ABBREV), | |
ef2ed501 | 1692 | dst_cmd->ref_name, |
d850b7a5 ÆAB |
1693 | repo_find_unique_abbrev(the_repository, &dst_cmd->old_oid, DEFAULT_ABBREV), |
1694 | repo_find_unique_abbrev(the_repository, &dst_cmd->new_oid, DEFAULT_ABBREV)); | |
da3efdb1 JS |
1695 | |
1696 | cmd->error_string = dst_cmd->error_string = | |
1697 | "inconsistent aliased update"; | |
1698 | } | |
1699 | ||
99036237 ÆAB |
1700 | static void check_aliased_update(struct command *cmd, struct string_list *list) |
1701 | { | |
1702 | struct strbuf buf = STRBUF_INIT; | |
1703 | const char *dst_name; | |
1704 | int flag; | |
1705 | ||
1706 | strbuf_addf(&buf, "%s%s", get_git_namespace(), cmd->ref_name); | |
1707 | dst_name = resolve_ref_unsafe(buf.buf, 0, NULL, &flag); | |
1708 | check_aliased_update_internal(cmd, list, dst_name, flag); | |
1709 | strbuf_release(&buf); | |
1710 | } | |
1711 | ||
da3efdb1 JS |
1712 | static void check_aliased_updates(struct command *commands) |
1713 | { | |
1714 | struct command *cmd; | |
183113a5 | 1715 | struct string_list ref_list = STRING_LIST_INIT_NODUP; |
da3efdb1 JS |
1716 | |
1717 | for (cmd = commands; cmd; cmd = cmd->next) { | |
1718 | struct string_list_item *item = | |
1d2f80fa | 1719 | string_list_append(&ref_list, cmd->ref_name); |
da3efdb1 JS |
1720 | item->util = (void *)cmd; |
1721 | } | |
3383e199 | 1722 | string_list_sort(&ref_list); |
da3efdb1 | 1723 | |
ef7e93d9 CB |
1724 | for (cmd = commands; cmd; cmd = cmd->next) { |
1725 | if (!cmd->error_string) | |
1726 | check_aliased_update(cmd, &ref_list); | |
1727 | } | |
da3efdb1 JS |
1728 | |
1729 | string_list_clear(&ref_list, 0); | |
1730 | } | |
1731 | ||
9fec7b21 | 1732 | static const struct object_id *command_singleton_iterator(void *cb_data) |
52fed6e1 JH |
1733 | { |
1734 | struct command **cmd_list = cb_data; | |
1735 | struct command *cmd = *cmd_list; | |
1736 | ||
9c44ea44 | 1737 | if (!cmd || is_null_oid(&cmd->new_oid)) |
9fec7b21 | 1738 | return NULL; |
52fed6e1 | 1739 | *cmd_list = NULL; /* this returns only one */ |
9fec7b21 | 1740 | return &cmd->new_oid; |
52fed6e1 JH |
1741 | } |
1742 | ||
0a1bc12b NTND |
1743 | static void set_connectivity_errors(struct command *commands, |
1744 | struct shallow_info *si) | |
52fed6e1 JH |
1745 | { |
1746 | struct command *cmd; | |
1747 | ||
1748 | for (cmd = commands; cmd; cmd = cmd->next) { | |
1749 | struct command *singleton = cmd; | |
722ff7f8 JK |
1750 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
1751 | ||
0a1bc12b NTND |
1752 | if (shallow_update && si->shallow_ref[cmd->index]) |
1753 | /* to be checked in update_shallow_ref() */ | |
1754 | continue; | |
722ff7f8 JK |
1755 | |
1756 | opt.env = tmp_objdir_env(tmp_objdir); | |
7043c707 | 1757 | if (!check_connected(command_singleton_iterator, &singleton, |
722ff7f8 | 1758 | &opt)) |
52fed6e1 | 1759 | continue; |
722ff7f8 | 1760 | |
52fed6e1 JH |
1761 | cmd->error_string = "missing necessary objects"; |
1762 | } | |
1763 | } | |
1764 | ||
0a1bc12b NTND |
1765 | struct iterate_data { |
1766 | struct command *cmds; | |
1767 | struct shallow_info *si; | |
1768 | }; | |
1769 | ||
9fec7b21 | 1770 | static const struct object_id *iterate_receive_command_list(void *cb_data) |
52fed6e1 | 1771 | { |
0a1bc12b NTND |
1772 | struct iterate_data *data = cb_data; |
1773 | struct command **cmd_list = &data->cmds; | |
52fed6e1 JH |
1774 | struct command *cmd = *cmd_list; |
1775 | ||
0a1bc12b NTND |
1776 | for (; cmd; cmd = cmd->next) { |
1777 | if (shallow_update && data->si->shallow_ref[cmd->index]) | |
1778 | /* to be checked in update_shallow_ref() */ | |
1779 | continue; | |
9c44ea44 | 1780 | if (!is_null_oid(&cmd->new_oid) && !cmd->skip_update) { |
ee6dfb2d | 1781 | *cmd_list = cmd->next; |
9fec7b21 | 1782 | return &cmd->new_oid; |
ee6dfb2d | 1783 | } |
ee6dfb2d | 1784 | } |
9fec7b21 | 1785 | return NULL; |
52fed6e1 JH |
1786 | } |
1787 | ||
daebaa78 JH |
1788 | static void reject_updates_to_hidden(struct command *commands) |
1789 | { | |
78a766ab LF |
1790 | struct strbuf refname_full = STRBUF_INIT; |
1791 | size_t prefix_len; | |
daebaa78 JH |
1792 | struct command *cmd; |
1793 | ||
78a766ab LF |
1794 | strbuf_addstr(&refname_full, get_git_namespace()); |
1795 | prefix_len = refname_full.len; | |
1796 | ||
daebaa78 | 1797 | for (cmd = commands; cmd; cmd = cmd->next) { |
78a766ab LF |
1798 | if (cmd->error_string) |
1799 | continue; | |
1800 | ||
1801 | strbuf_setlen(&refname_full, prefix_len); | |
1802 | strbuf_addstr(&refname_full, cmd->ref_name); | |
1803 | ||
9b67eb6f | 1804 | if (!ref_is_hidden(cmd->ref_name, refname_full.buf, &hidden_refs)) |
daebaa78 | 1805 | continue; |
9c44ea44 | 1806 | if (is_null_oid(&cmd->new_oid)) |
daebaa78 JH |
1807 | cmd->error_string = "deny deleting a hidden ref"; |
1808 | else | |
1809 | cmd->error_string = "deny updating a hidden ref"; | |
1810 | } | |
78a766ab LF |
1811 | |
1812 | strbuf_release(&refname_full); | |
daebaa78 JH |
1813 | } |
1814 | ||
a6a84319 SB |
1815 | static int should_process_cmd(struct command *cmd) |
1816 | { | |
1817 | return !cmd->error_string && !cmd->skip_update; | |
1818 | } | |
1819 | ||
07b1d8f1 | 1820 | static void BUG_if_skipped_connectivity_check(struct command *commands, |
a6a84319 SB |
1821 | struct shallow_info *si) |
1822 | { | |
1823 | struct command *cmd; | |
a6a84319 SB |
1824 | |
1825 | for (cmd = commands; cmd; cmd = cmd->next) { | |
07b1d8f1 ÆAB |
1826 | if (should_process_cmd(cmd) && si->shallow_ref[cmd->index]) |
1827 | bug("connectivity check has not been run on ref %s", | |
1828 | cmd->ref_name); | |
a6a84319 | 1829 | } |
07b1d8f1 | 1830 | BUG_if_bug("connectivity check skipped???"); |
a6a84319 SB |
1831 | } |
1832 | ||
a1a26145 SB |
1833 | static void execute_commands_non_atomic(struct command *commands, |
1834 | struct shallow_info *si) | |
1835 | { | |
1836 | struct command *cmd; | |
222368c6 SB |
1837 | struct strbuf err = STRBUF_INIT; |
1838 | ||
a1a26145 | 1839 | for (cmd = commands; cmd; cmd = cmd->next) { |
15d3af5e | 1840 | if (!should_process_cmd(cmd) || cmd->run_proc_receive) |
a1a26145 SB |
1841 | continue; |
1842 | ||
222368c6 SB |
1843 | transaction = ref_transaction_begin(&err); |
1844 | if (!transaction) { | |
1845 | rp_error("%s", err.buf); | |
1846 | strbuf_reset(&err); | |
1847 | cmd->error_string = "transaction failed to start"; | |
1848 | continue; | |
1849 | } | |
1850 | ||
a1a26145 | 1851 | cmd->error_string = update(cmd, si); |
222368c6 SB |
1852 | |
1853 | if (!cmd->error_string | |
1854 | && ref_transaction_commit(transaction, &err)) { | |
1855 | rp_error("%s", err.buf); | |
1856 | strbuf_reset(&err); | |
1857 | cmd->error_string = "failed to update ref"; | |
1858 | } | |
1859 | ref_transaction_free(transaction); | |
a1a26145 | 1860 | } |
68deed29 SB |
1861 | strbuf_release(&err); |
1862 | } | |
222368c6 | 1863 | |
68deed29 SB |
1864 | static void execute_commands_atomic(struct command *commands, |
1865 | struct shallow_info *si) | |
1866 | { | |
1867 | struct command *cmd; | |
1868 | struct strbuf err = STRBUF_INIT; | |
1869 | const char *reported_error = "atomic push failure"; | |
1870 | ||
1871 | transaction = ref_transaction_begin(&err); | |
1872 | if (!transaction) { | |
1873 | rp_error("%s", err.buf); | |
1874 | strbuf_reset(&err); | |
1875 | reported_error = "transaction failed to start"; | |
1876 | goto failure; | |
1877 | } | |
1878 | ||
1879 | for (cmd = commands; cmd; cmd = cmd->next) { | |
15d3af5e | 1880 | if (!should_process_cmd(cmd) || cmd->run_proc_receive) |
68deed29 SB |
1881 | continue; |
1882 | ||
1883 | cmd->error_string = update(cmd, si); | |
1884 | ||
1885 | if (cmd->error_string) | |
1886 | goto failure; | |
1887 | } | |
1888 | ||
1889 | if (ref_transaction_commit(transaction, &err)) { | |
1890 | rp_error("%s", err.buf); | |
1891 | reported_error = "atomic transaction failed"; | |
1892 | goto failure; | |
1893 | } | |
1894 | goto cleanup; | |
1895 | ||
1896 | failure: | |
1897 | for (cmd = commands; cmd; cmd = cmd->next) | |
1898 | if (!cmd->error_string) | |
1899 | cmd->error_string = reported_error; | |
1900 | ||
1901 | cleanup: | |
1902 | ref_transaction_free(transaction); | |
222368c6 | 1903 | strbuf_release(&err); |
a1a26145 SB |
1904 | } |
1905 | ||
0a1bc12b NTND |
1906 | static void execute_commands(struct command *commands, |
1907 | const char *unpacker_error, | |
77a9745d SB |
1908 | struct shallow_info *si, |
1909 | const struct string_list *push_options) | |
575f4974 | 1910 | { |
d415092a | 1911 | struct check_connected_options opt = CHECK_CONNECTED_INIT; |
5e1c71fd | 1912 | struct command *cmd; |
0a1bc12b | 1913 | struct iterate_data data; |
d415092a JK |
1914 | struct async muxer; |
1915 | int err_fd = 0; | |
15d3af5e | 1916 | int run_proc_receive = 0; |
8aaf7d64 SP |
1917 | |
1918 | if (unpacker_error) { | |
5e1c71fd | 1919 | for (cmd = commands; cmd; cmd = cmd->next) |
74eb32d3 | 1920 | cmd->error_string = "unpacker error"; |
8aaf7d64 SP |
1921 | return; |
1922 | } | |
1923 | ||
d415092a JK |
1924 | if (use_sideband) { |
1925 | memset(&muxer, 0, sizeof(muxer)); | |
1926 | muxer.proc = copy_to_sideband; | |
1927 | muxer.in = -1; | |
1928 | if (!start_async(&muxer)) | |
1929 | err_fd = muxer.in; | |
1930 | /* ...else, continue without relaying sideband */ | |
1931 | } | |
1932 | ||
0a1bc12b NTND |
1933 | data.cmds = commands; |
1934 | data.si = si; | |
d415092a | 1935 | opt.err_fd = err_fd; |
6b4cd2f8 | 1936 | opt.progress = err_fd && !quiet; |
722ff7f8 | 1937 | opt.env = tmp_objdir_env(tmp_objdir); |
bcec6780 PS |
1938 | opt.exclude_hidden_refs_section = "receive"; |
1939 | ||
d415092a | 1940 | if (check_connected(iterate_receive_command_list, &data, &opt)) |
0a1bc12b | 1941 | set_connectivity_errors(commands, si); |
52fed6e1 | 1942 | |
d415092a JK |
1943 | if (use_sideband) |
1944 | finish_async(&muxer); | |
1945 | ||
daebaa78 JH |
1946 | reject_updates_to_hidden(commands); |
1947 | ||
15d3af5e JX |
1948 | /* |
1949 | * Try to find commands that have special prefix in their reference names, | |
1950 | * and mark them to run an external "proc-receive" hook later. | |
1951 | */ | |
31e8595a JX |
1952 | if (proc_receive_ref) { |
1953 | for (cmd = commands; cmd; cmd = cmd->next) { | |
1954 | if (!should_process_cmd(cmd)) | |
1955 | continue; | |
15d3af5e | 1956 | |
31e8595a JX |
1957 | if (proc_receive_ref_matches(cmd)) { |
1958 | cmd->run_proc_receive = RUN_PROC_RECEIVE_SCHEDULED; | |
1959 | run_proc_receive = 1; | |
1960 | } | |
15d3af5e JX |
1961 | } |
1962 | } | |
1963 | ||
77a9745d | 1964 | if (run_receive_hook(commands, "pre-receive", 0, push_options)) { |
ef7e93d9 CB |
1965 | for (cmd = commands; cmd; cmd = cmd->next) { |
1966 | if (!cmd->error_string) | |
1967 | cmd->error_string = "pre-receive hook declined"; | |
1968 | } | |
05ef58ec SP |
1969 | return; |
1970 | } | |
1971 | ||
54077640 CB |
1972 | /* |
1973 | * If there is no command ready to run, should return directly to destroy | |
1974 | * temporary data in the quarantine area. | |
1975 | */ | |
1976 | for (cmd = commands; cmd && cmd->error_string; cmd = cmd->next) | |
1977 | ; /* nothing */ | |
1978 | if (!cmd) | |
1979 | return; | |
1980 | ||
722ff7f8 JK |
1981 | /* |
1982 | * Now we'll start writing out refs, which means the objects need | |
1983 | * to be in their final positions so that other processes can see them. | |
1984 | */ | |
1985 | if (tmp_objdir_migrate(tmp_objdir) < 0) { | |
1986 | for (cmd = commands; cmd; cmd = cmd->next) { | |
1987 | if (!cmd->error_string) | |
1988 | cmd->error_string = "unable to migrate objects to permanent storage"; | |
1989 | } | |
1990 | return; | |
1991 | } | |
1992 | tmp_objdir = NULL; | |
1993 | ||
da3efdb1 JS |
1994 | check_aliased_updates(commands); |
1995 | ||
96ec7b1e | 1996 | free(head_name_to_free); |
efbd4fdf | 1997 | head_name = head_name_to_free = resolve_refdup("HEAD", 0, NULL, NULL); |
747ca245 | 1998 | |
15d3af5e JX |
1999 | if (run_proc_receive && |
2000 | run_proc_receive_hook(commands, push_options)) | |
2001 | for (cmd = commands; cmd; cmd = cmd->next) | |
2002 | if (!cmd->error_string && | |
2003 | !(cmd->run_proc_receive & RUN_PROC_RECEIVE_RETURNED) && | |
2004 | (cmd->run_proc_receive || use_atomic)) | |
2005 | cmd->error_string = "fail to run proc-receive hook"; | |
2006 | ||
68deed29 SB |
2007 | if (use_atomic) |
2008 | execute_commands_atomic(commands, si); | |
2009 | else | |
2010 | execute_commands_non_atomic(commands, si); | |
0a1bc12b | 2011 | |
a6a84319 | 2012 | if (shallow_update) |
07b1d8f1 | 2013 | BUG_if_skipped_connectivity_check(commands, si); |
575f4974 LT |
2014 | } |
2015 | ||
39895c74 JH |
2016 | static struct command **queue_command(struct command **tail, |
2017 | const char *line, | |
2018 | int linelen) | |
2019 | { | |
9c44ea44 | 2020 | struct object_id old_oid, new_oid; |
39895c74 JH |
2021 | struct command *cmd; |
2022 | const char *refname; | |
2023 | int reflen; | |
9c44ea44 | 2024 | const char *p; |
39895c74 | 2025 | |
9c44ea44 | 2026 | if (parse_oid_hex(line, &old_oid, &p) || |
2027 | *p++ != ' ' || | |
2028 | parse_oid_hex(p, &new_oid, &p) || | |
2029 | *p++ != ' ') | |
39895c74 JH |
2030 | die("protocol error: expected old/new/ref, got '%s'", line); |
2031 | ||
9c44ea44 | 2032 | refname = p; |
2033 | reflen = linelen - (p - line); | |
ddd0bfac | 2034 | FLEX_ALLOC_MEM(cmd, ref_name, refname, reflen); |
9c44ea44 | 2035 | oidcpy(&cmd->old_oid, &old_oid); |
2036 | oidcpy(&cmd->new_oid, &new_oid); | |
39895c74 JH |
2037 | *tail = cmd; |
2038 | return &cmd->next; | |
2039 | } | |
2040 | ||
1fdd31cf ÆAB |
2041 | static void free_commands(struct command *commands) |
2042 | { | |
2043 | while (commands) { | |
2044 | struct command *next = commands->next; | |
2045 | ||
2046 | free(commands); | |
2047 | commands = next; | |
2048 | } | |
2049 | } | |
2050 | ||
4adf569d JH |
2051 | static void queue_commands_from_cert(struct command **tail, |
2052 | struct strbuf *push_cert) | |
2053 | { | |
2054 | const char *boc, *eoc; | |
2055 | ||
2056 | if (*tail) | |
2057 | die("protocol error: got both push certificate and unsigned commands"); | |
2058 | ||
2059 | boc = strstr(push_cert->buf, "\n\n"); | |
2060 | if (!boc) | |
2061 | die("malformed push certificate %.*s", 100, push_cert->buf); | |
2062 | else | |
2063 | boc += 2; | |
482c1191 | 2064 | eoc = push_cert->buf + parse_signed_buffer(push_cert->buf, push_cert->len); |
4adf569d JH |
2065 | |
2066 | while (boc < eoc) { | |
2067 | const char *eol = memchr(boc, '\n', eoc - boc); | |
f2214ded | 2068 | tail = queue_command(tail, boc, eol ? eol - boc : eoc - boc); |
4adf569d JH |
2069 | boc = eol ? eol + 1 : eoc; |
2070 | } | |
2071 | } | |
2072 | ||
01f9ec64 MS |
2073 | static struct command *read_head_info(struct packet_reader *reader, |
2074 | struct oid_array *shallow) | |
575f4974 | 2075 | { |
5e1c71fd | 2076 | struct command *commands = NULL; |
eb1af2df | 2077 | struct command **p = &commands; |
575f4974 | 2078 | for (;;) { |
01f9ec64 | 2079 | int linelen; |
eb1af2df | 2080 | |
01f9ec64 | 2081 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
575f4974 | 2082 | break; |
5dbd7676 | 2083 | |
01f9ec64 | 2084 | if (reader->pktlen > 8 && starts_with(reader->line, "shallow ")) { |
9c44ea44 | 2085 | struct object_id oid; |
01f9ec64 | 2086 | if (get_oid_hex(reader->line + 8, &oid)) |
c09b71cc | 2087 | die("protocol error: expected shallow sha, got '%s'", |
01f9ec64 | 2088 | reader->line + 8); |
910650d2 | 2089 | oid_array_append(shallow, &oid); |
5dbd7676 NTND |
2090 | continue; |
2091 | } | |
2092 | ||
01f9ec64 MS |
2093 | linelen = strlen(reader->line); |
2094 | if (linelen < reader->pktlen) { | |
2095 | const char *feature_list = reader->line + linelen + 1; | |
bb095d08 | 2096 | const char *hash = NULL; |
a2a066d9 | 2097 | const char *client_sid; |
bb095d08 | 2098 | int len = 0; |
f47182c8 | 2099 | if (parse_feature_request(feature_list, "report-status")) |
cfee10a7 | 2100 | report_status = 1; |
63518a57 JX |
2101 | if (parse_feature_request(feature_list, "report-status-v2")) |
2102 | report_status_v2 = 1; | |
f47182c8 | 2103 | if (parse_feature_request(feature_list, "side-band-64k")) |
38a81b4e | 2104 | use_sideband = LARGE_PACKET_MAX; |
c207e34f CB |
2105 | if (parse_feature_request(feature_list, "quiet")) |
2106 | quiet = 1; | |
1b70fe5d RS |
2107 | if (advertise_atomic_push |
2108 | && parse_feature_request(feature_list, "atomic")) | |
2109 | use_atomic = 1; | |
c714e45f SB |
2110 | if (advertise_push_options |
2111 | && parse_feature_request(feature_list, "push-options")) | |
2112 | use_push_options = 1; | |
bb095d08 | 2113 | hash = parse_feature_value(feature_list, "object-format", &len, NULL); |
2114 | if (!hash) { | |
2115 | hash = hash_algos[GIT_HASH_SHA1].name; | |
2116 | len = strlen(hash); | |
2117 | } | |
2118 | if (xstrncmpz(the_hash_algo->name, hash, len)) | |
2119 | die("error: unsupported object format '%s'", hash); | |
a2a066d9 JS |
2120 | client_sid = parse_feature_value(feature_list, "session-id", &len, NULL); |
2121 | if (client_sid) { | |
2122 | char *sid = xstrndup(client_sid, len); | |
2123 | trace2_data_string("transfer", NULL, "client-sid", client_sid); | |
2124 | free(sid); | |
2125 | } | |
cfee10a7 | 2126 | } |
0e3c339b | 2127 | |
01f9ec64 | 2128 | if (!strcmp(reader->line, "push-cert")) { |
a85b377d | 2129 | int true_flush = 0; |
01f9ec64 MS |
2130 | int saved_options = reader->options; |
2131 | reader->options &= ~PACKET_READ_CHOMP_NEWLINE; | |
a85b377d JH |
2132 | |
2133 | for (;;) { | |
01f9ec64 MS |
2134 | packet_reader_read(reader); |
2135 | if (reader->status == PACKET_READ_FLUSH) { | |
a85b377d JH |
2136 | true_flush = 1; |
2137 | break; | |
2138 | } | |
01f9ec64 MS |
2139 | if (reader->status != PACKET_READ_NORMAL) { |
2140 | die("protocol error: got an unexpected packet"); | |
2141 | } | |
2142 | if (!strcmp(reader->line, "push-cert-end\n")) | |
a85b377d | 2143 | break; /* end of cert */ |
01f9ec64 | 2144 | strbuf_addstr(&push_cert, reader->line); |
a85b377d | 2145 | } |
01f9ec64 | 2146 | reader->options = saved_options; |
a85b377d JH |
2147 | |
2148 | if (true_flush) | |
2149 | break; | |
2150 | continue; | |
2151 | } | |
2152 | ||
01f9ec64 | 2153 | p = queue_command(p, reader->line, linelen); |
575f4974 | 2154 | } |
4adf569d JH |
2155 | |
2156 | if (push_cert.len) | |
2157 | queue_commands_from_cert(p, &push_cert); | |
2158 | ||
5e1c71fd | 2159 | return commands; |
575f4974 LT |
2160 | } |
2161 | ||
01f9ec64 MS |
2162 | static void read_push_options(struct packet_reader *reader, |
2163 | struct string_list *options) | |
c714e45f SB |
2164 | { |
2165 | while (1) { | |
01f9ec64 | 2166 | if (packet_reader_read(reader) != PACKET_READ_NORMAL) |
c714e45f SB |
2167 | break; |
2168 | ||
01f9ec64 | 2169 | string_list_append(options, reader->line); |
c714e45f SB |
2170 | } |
2171 | } | |
2172 | ||
fc04c412 SP |
2173 | static const char *parse_pack_header(struct pack_header *hdr) |
2174 | { | |
a69e5429 JH |
2175 | switch (read_pack_header(0, hdr)) { |
2176 | case PH_ERROR_EOF: | |
2177 | return "eof before pack header was fully read"; | |
2178 | ||
2179 | case PH_ERROR_PACK_SIGNATURE: | |
fc04c412 | 2180 | return "protocol error (pack signature mismatch detected)"; |
a69e5429 JH |
2181 | |
2182 | case PH_ERROR_PROTOCOL: | |
fc04c412 | 2183 | return "protocol error (pack version unsupported)"; |
a69e5429 JH |
2184 | |
2185 | default: | |
2186 | return "unknown error in parse_pack_header"; | |
2187 | ||
2188 | case 0: | |
2189 | return NULL; | |
2190 | } | |
fc04c412 SP |
2191 | } |
2192 | ||
576162a4 NP |
2193 | static const char *pack_lockfile; |
2194 | ||
22f9b7f3 | 2195 | static void push_header_arg(struct strvec *args, struct pack_header *hdr) |
446d5d91 | 2196 | { |
22f9b7f3 | 2197 | strvec_pushf(args, "--pack_header=%"PRIu32",%"PRIu32, |
f6d8942b | 2198 | ntohl(hdr->hdr_version), ntohl(hdr->hdr_entries)); |
446d5d91 JK |
2199 | } |
2200 | ||
5dbd7676 | 2201 | static const char *unpack(int err_fd, struct shallow_info *si) |
575f4974 | 2202 | { |
fc04c412 SP |
2203 | struct pack_header hdr; |
2204 | const char *hdr_err; | |
31c42bff | 2205 | int status; |
d3180279 | 2206 | struct child_process child = CHILD_PROCESS_INIT; |
dab76d3a JH |
2207 | int fsck_objects = (receive_fsck_objects >= 0 |
2208 | ? receive_fsck_objects | |
2209 | : transfer_fsck_objects >= 0 | |
2210 | ? transfer_fsck_objects | |
2211 | : 0); | |
fc04c412 SP |
2212 | |
2213 | hdr_err = parse_pack_header(&hdr); | |
49ecfa13 JK |
2214 | if (hdr_err) { |
2215 | if (err_fd > 0) | |
2216 | close(err_fd); | |
fc04c412 | 2217 | return hdr_err; |
49ecfa13 | 2218 | } |
fc04c412 | 2219 | |
5dbd7676 NTND |
2220 | if (si->nr_ours || si->nr_theirs) { |
2221 | alt_shallow_file = setup_temporary_shallow(si->shallow); | |
22f9b7f3 JK |
2222 | strvec_push(&child.args, "--shallow-file"); |
2223 | strvec_push(&child.args, alt_shallow_file); | |
5dbd7676 NTND |
2224 | } |
2225 | ||
b3cecf49 | 2226 | tmp_objdir = tmp_objdir_create("incoming"); |
6cdad1f1 JK |
2227 | if (!tmp_objdir) { |
2228 | if (err_fd > 0) | |
2229 | close(err_fd); | |
722ff7f8 | 2230 | return "unable to create temporary object directory"; |
6cdad1f1 | 2231 | } |
c21fa3bb | 2232 | strvec_pushv(&child.env, tmp_objdir_env(tmp_objdir)); |
722ff7f8 JK |
2233 | |
2234 | /* | |
2235 | * Normally we just pass the tmp_objdir environment to the child | |
2236 | * processes that do the heavy lifting, but we may need to see these | |
2237 | * objects ourselves to set up shallow information. | |
2238 | */ | |
2239 | tmp_objdir_add_as_alternate(tmp_objdir); | |
2240 | ||
fc04c412 | 2241 | if (ntohl(hdr.hdr_entries) < unpack_limit) { |
22f9b7f3 | 2242 | strvec_push(&child.args, "unpack-objects"); |
446d5d91 | 2243 | push_header_arg(&child.args, &hdr); |
c207e34f | 2244 | if (quiet) |
22f9b7f3 | 2245 | strvec_push(&child.args, "-q"); |
dab76d3a | 2246 | if (fsck_objects) |
22f9b7f3 | 2247 | strvec_pushf(&child.args, "--strict%s", |
f6d8942b | 2248 | fsck_msg_types.buf); |
c08db5a2 | 2249 | if (max_input_size) |
22f9b7f3 | 2250 | strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX, |
f6d8942b | 2251 | (uintmax_t)max_input_size); |
59bfdfb8 | 2252 | child.no_stdout = 1; |
a22e6f85 | 2253 | child.err = err_fd; |
59bfdfb8 | 2254 | child.git_cmd = 1; |
31c42bff NTND |
2255 | status = run_command(&child); |
2256 | if (status) | |
2257 | return "unpack-objects abnormal exit"; | |
576162a4 | 2258 | } else { |
da25bdb7 | 2259 | char hostname[HOST_NAME_MAX + 1]; |
576162a4 | 2260 | |
22f9b7f3 | 2261 | strvec_pushl(&child.args, "index-pack", "--stdin", NULL); |
446d5d91 | 2262 | push_header_arg(&child.args, &hdr); |
b26cb7c7 | 2263 | |
5781a9a2 | 2264 | if (xgethostname(hostname, sizeof(hostname))) |
b26cb7c7 | 2265 | xsnprintf(hostname, sizeof(hostname), "localhost"); |
22f9b7f3 | 2266 | strvec_pushf(&child.args, |
f6d8942b JK |
2267 | "--keep=receive-pack %"PRIuMAX" on %s", |
2268 | (uintmax_t)getpid(), | |
2269 | hostname); | |
b26cb7c7 | 2270 | |
d06303bb | 2271 | if (!quiet && err_fd) |
22f9b7f3 | 2272 | strvec_push(&child.args, "--show-resolving-progress"); |
83558686 | 2273 | if (use_sideband) |
22f9b7f3 | 2274 | strvec_push(&child.args, "--report-end-of-input"); |
dab76d3a | 2275 | if (fsck_objects) |
22f9b7f3 | 2276 | strvec_pushf(&child.args, "--strict%s", |
f6d8942b | 2277 | fsck_msg_types.buf); |
1b68387e | 2278 | if (!reject_thin) |
22f9b7f3 | 2279 | strvec_push(&child.args, "--fix-thin"); |
c08db5a2 | 2280 | if (max_input_size) |
22f9b7f3 | 2281 | strvec_pushf(&child.args, "--max-input-size=%"PRIuMAX, |
f6d8942b | 2282 | (uintmax_t)max_input_size); |
31c42bff NTND |
2283 | child.out = -1; |
2284 | child.err = err_fd; | |
2285 | child.git_cmd = 1; | |
2286 | status = start_command(&child); | |
2287 | if (status) | |
576162a4 | 2288 | return "index-pack fork failed"; |
5476e1ef | 2289 | pack_lockfile = index_pack_lockfile(child.out, NULL); |
31c42bff NTND |
2290 | close(child.out); |
2291 | status = finish_command(&child); | |
2292 | if (status) | |
2293 | return "index-pack abnormal exit"; | |
a49d2834 | 2294 | reprepare_packed_git(the_repository); |
cfee10a7 | 2295 | } |
31c42bff | 2296 | return NULL; |
cfee10a7 JH |
2297 | } |
2298 | ||
5dbd7676 | 2299 | static const char *unpack_with_sideband(struct shallow_info *si) |
a22e6f85 JK |
2300 | { |
2301 | struct async muxer; | |
2302 | const char *ret; | |
2303 | ||
2304 | if (!use_sideband) | |
5dbd7676 | 2305 | return unpack(0, si); |
a22e6f85 | 2306 | |
83558686 | 2307 | use_keepalive = KEEPALIVE_AFTER_NUL; |
a22e6f85 JK |
2308 | memset(&muxer, 0, sizeof(muxer)); |
2309 | muxer.proc = copy_to_sideband; | |
2310 | muxer.in = -1; | |
2311 | if (start_async(&muxer)) | |
2312 | return NULL; | |
2313 | ||
5dbd7676 | 2314 | ret = unpack(muxer.in, si); |
a22e6f85 JK |
2315 | |
2316 | finish_async(&muxer); | |
2317 | return ret; | |
2318 | } | |
2319 | ||
c95fc72f | 2320 | static void prepare_shallow_update(struct shallow_info *si) |
0a1bc12b | 2321 | { |
42c78a21 | 2322 | int i, j, k, bitmap_size = DIV_ROUND_UP(si->ref->nr, 32); |
0a1bc12b | 2323 | |
b32fa95f | 2324 | ALLOC_ARRAY(si->used_shallow, si->shallow->nr); |
0a1bc12b NTND |
2325 | assign_shallow_commits_to_refs(si, si->used_shallow, NULL); |
2326 | ||
486f4bd1 JH |
2327 | CALLOC_ARRAY(si->need_reachability_test, si->shallow->nr); |
2328 | CALLOC_ARRAY(si->reachable, si->shallow->nr); | |
2329 | CALLOC_ARRAY(si->shallow_ref, si->ref->nr); | |
0a1bc12b NTND |
2330 | |
2331 | for (i = 0; i < si->nr_ours; i++) | |
2332 | si->need_reachability_test[si->ours[i]] = 1; | |
2333 | ||
2334 | for (i = 0; i < si->shallow->nr; i++) { | |
2335 | if (!si->used_shallow[i]) | |
2336 | continue; | |
2337 | for (j = 0; j < bitmap_size; j++) { | |
2338 | if (!si->used_shallow[i][j]) | |
2339 | continue; | |
2340 | si->need_reachability_test[i]++; | |
2341 | for (k = 0; k < 32; k++) | |
9a93c668 | 2342 | if (si->used_shallow[i][j] & (1U << k)) |
0a1bc12b NTND |
2343 | si->shallow_ref[j * 32 + k]++; |
2344 | } | |
2345 | ||
2346 | /* | |
2347 | * true for those associated with some refs and belong | |
2348 | * in "ours" list aka "step 7 not done yet" | |
2349 | */ | |
2350 | si->need_reachability_test[i] = | |
2351 | si->need_reachability_test[i] > 1; | |
2352 | } | |
2353 | ||
2354 | /* | |
2355 | * keep hooks happy by forcing a temporary shallow file via | |
2356 | * env variable because we can't add --shallow-file to every | |
7987d223 | 2357 | * command. check_connected() will be done with |
0a1bc12b NTND |
2358 | * true .git/shallow though. |
2359 | */ | |
2360 | setenv(GIT_SHALLOW_FILE_ENVIRONMENT, alt_shallow_file, 1); | |
2361 | } | |
2362 | ||
5dbd7676 NTND |
2363 | static void update_shallow_info(struct command *commands, |
2364 | struct shallow_info *si, | |
910650d2 | 2365 | struct oid_array *ref) |
5dbd7676 NTND |
2366 | { |
2367 | struct command *cmd; | |
2368 | int *ref_status; | |
2369 | remove_nonexistent_theirs_shallow(si); | |
0a1bc12b NTND |
2370 | if (!si->nr_ours && !si->nr_theirs) { |
2371 | shallow_update = 0; | |
5dbd7676 | 2372 | return; |
0a1bc12b | 2373 | } |
5dbd7676 NTND |
2374 | |
2375 | for (cmd = commands; cmd; cmd = cmd->next) { | |
9c44ea44 | 2376 | if (is_null_oid(&cmd->new_oid)) |
5dbd7676 | 2377 | continue; |
910650d2 | 2378 | oid_array_append(ref, &cmd->new_oid); |
5dbd7676 NTND |
2379 | cmd->index = ref->nr - 1; |
2380 | } | |
2381 | si->ref = ref; | |
2382 | ||
0a1bc12b | 2383 | if (shallow_update) { |
c95fc72f | 2384 | prepare_shallow_update(si); |
0a1bc12b NTND |
2385 | return; |
2386 | } | |
2387 | ||
b32fa95f | 2388 | ALLOC_ARRAY(ref_status, ref->nr); |
5dbd7676 NTND |
2389 | assign_shallow_commits_to_refs(si, NULL, ref_status); |
2390 | for (cmd = commands; cmd; cmd = cmd->next) { | |
9c44ea44 | 2391 | if (is_null_oid(&cmd->new_oid)) |
5dbd7676 NTND |
2392 | continue; |
2393 | if (ref_status[cmd->index]) { | |
2394 | cmd->error_string = "shallow update not allowed"; | |
2395 | cmd->skip_update = 1; | |
2396 | } | |
2397 | } | |
5dbd7676 NTND |
2398 | free(ref_status); |
2399 | } | |
2400 | ||
5e1c71fd | 2401 | static void report(struct command *commands, const char *unpack_status) |
cfee10a7 JH |
2402 | { |
2403 | struct command *cmd; | |
38a81b4e SP |
2404 | struct strbuf buf = STRBUF_INIT; |
2405 | ||
2406 | packet_buf_write(&buf, "unpack %s\n", | |
2407 | unpack_status ? unpack_status : "ok"); | |
cfee10a7 JH |
2408 | for (cmd = commands; cmd; cmd = cmd->next) { |
2409 | if (!cmd->error_string) | |
38a81b4e SP |
2410 | packet_buf_write(&buf, "ok %s\n", |
2411 | cmd->ref_name); | |
cfee10a7 | 2412 | else |
38a81b4e SP |
2413 | packet_buf_write(&buf, "ng %s %s\n", |
2414 | cmd->ref_name, cmd->error_string); | |
575f4974 | 2415 | } |
38a81b4e SP |
2416 | packet_buf_flush(&buf); |
2417 | ||
2418 | if (use_sideband) | |
2419 | send_sideband(1, 1, buf.buf, buf.len, use_sideband); | |
2420 | else | |
cdf4fb8e | 2421 | write_or_die(1, buf.buf, buf.len); |
38a81b4e | 2422 | strbuf_release(&buf); |
575f4974 LT |
2423 | } |
2424 | ||
63518a57 JX |
2425 | static void report_v2(struct command *commands, const char *unpack_status) |
2426 | { | |
2427 | struct command *cmd; | |
2428 | struct strbuf buf = STRBUF_INIT; | |
2429 | struct ref_push_report *report; | |
2430 | ||
2431 | packet_buf_write(&buf, "unpack %s\n", | |
2432 | unpack_status ? unpack_status : "ok"); | |
2433 | for (cmd = commands; cmd; cmd = cmd->next) { | |
2434 | int count = 0; | |
2435 | ||
2436 | if (cmd->error_string) { | |
2437 | packet_buf_write(&buf, "ng %s %s\n", | |
2438 | cmd->ref_name, | |
2439 | cmd->error_string); | |
2440 | continue; | |
2441 | } | |
2442 | packet_buf_write(&buf, "ok %s\n", | |
2443 | cmd->ref_name); | |
2444 | for (report = cmd->report; report; report = report->next) { | |
2445 | if (count++ > 0) | |
2446 | packet_buf_write(&buf, "ok %s\n", | |
2447 | cmd->ref_name); | |
2448 | if (report->ref_name) | |
2449 | packet_buf_write(&buf, "option refname %s\n", | |
2450 | report->ref_name); | |
2451 | if (report->old_oid) | |
2452 | packet_buf_write(&buf, "option old-oid %s\n", | |
2453 | oid_to_hex(report->old_oid)); | |
2454 | if (report->new_oid) | |
2455 | packet_buf_write(&buf, "option new-oid %s\n", | |
2456 | oid_to_hex(report->new_oid)); | |
2457 | if (report->forced_update) | |
2458 | packet_buf_write(&buf, "option forced-update\n"); | |
2459 | } | |
2460 | } | |
2461 | packet_buf_flush(&buf); | |
2462 | ||
2463 | if (use_sideband) | |
2464 | send_sideband(1, 1, buf.buf, buf.len, use_sideband); | |
2465 | else | |
2466 | write_or_die(1, buf.buf, buf.len); | |
2467 | strbuf_release(&buf); | |
2468 | } | |
2469 | ||
5e1c71fd | 2470 | static int delete_only(struct command *commands) |
d4f694ba | 2471 | { |
5e1c71fd JS |
2472 | struct command *cmd; |
2473 | for (cmd = commands; cmd; cmd = cmd->next) { | |
9c44ea44 | 2474 | if (!is_null_oid(&cmd->new_oid)) |
d4f694ba | 2475 | return 0; |
d4f694ba JH |
2476 | } |
2477 | return 1; | |
2478 | } | |
2479 | ||
be5908ae | 2480 | int cmd_receive_pack(int argc, const char **argv, const char *prefix) |
575f4974 | 2481 | { |
42526b47 | 2482 | int advertise_refs = 0; |
5e1c71fd | 2483 | struct command *commands; |
910650d2 | 2484 | struct oid_array shallow = OID_ARRAY_INIT; |
2485 | struct oid_array ref = OID_ARRAY_INIT; | |
5dbd7676 | 2486 | struct shallow_info si; |
01f9ec64 | 2487 | struct packet_reader reader; |
575f4974 | 2488 | |
1b68387e SS |
2489 | struct option options[] = { |
2490 | OPT__QUIET(&quiet, N_("quiet")), | |
2491 | OPT_HIDDEN_BOOL(0, "stateless-rpc", &stateless_rpc, NULL), | |
98e2d9d6 ÆAB |
2492 | OPT_HIDDEN_BOOL(0, "http-backend-info-refs", &advertise_refs, NULL), |
2493 | OPT_ALIAS(0, "advertise-refs", "http-backend-info-refs"), | |
1b68387e SS |
2494 | OPT_HIDDEN_BOOL(0, "reject-thin-pack-for-testing", &reject_thin, NULL), |
2495 | OPT_END() | |
2496 | }; | |
bbc30f99 | 2497 | |
1b68387e | 2498 | packet_trace_identity("receive-pack"); |
575f4974 | 2499 | |
1b68387e | 2500 | argc = parse_options(argc, argv, prefix, options, receive_pack_usage, 0); |
c207e34f | 2501 | |
1b68387e | 2502 | if (argc > 1) |
c25edee9 | 2503 | usage_msg_opt(_("too many arguments"), receive_pack_usage, options); |
1b68387e | 2504 | if (argc == 0) |
c25edee9 | 2505 | usage_msg_opt(_("you must specify a directory"), receive_pack_usage, options); |
42526b47 | 2506 | |
1b68387e | 2507 | service_dir = argv[0]; |
575f4974 | 2508 | |
e1464ca7 | 2509 | setup_path(); |
5c09f321 | 2510 | |
5732373d JH |
2511 | if (!enter_repo(service_dir, 0)) |
2512 | die("'%s' does not appear to be a git repository", service_dir); | |
575f4974 | 2513 | |
ef90d6d4 | 2514 | git_config(receive_pack_config, NULL); |
b89363e4 | 2515 | if (cert_nonce_seed) |
5732373d | 2516 | push_cert_nonce = prepare_push_cert_nonce(service_dir, time(NULL)); |
6fb75bed | 2517 | |
e28714c5 JH |
2518 | if (0 <= transfer_unpack_limit) |
2519 | unpack_limit = transfer_unpack_limit; | |
2520 | else if (0 <= receive_unpack_limit) | |
2521 | unpack_limit = receive_unpack_limit; | |
2522 | ||
aa9bab29 | 2523 | switch (determine_protocol_version_server()) { |
8f6982b4 BW |
2524 | case protocol_v2: |
2525 | /* | |
2526 | * push support for protocol v2 has not been implemented yet, | |
2527 | * so ignore the request to use v2 and fallback to using v0. | |
2528 | */ | |
2529 | break; | |
aa9bab29 BW |
2530 | case protocol_v1: |
2531 | /* | |
2532 | * v1 is just the original protocol with a version string, | |
2533 | * so just fall through after writing the version string. | |
2534 | */ | |
2535 | if (advertise_refs || !stateless_rpc) | |
2536 | packet_write_fmt(1, "version 1\n"); | |
2537 | ||
2538 | /* fallthrough */ | |
2539 | case protocol_v0: | |
2540 | break; | |
2541 | case protocol_unknown_version: | |
2542 | BUG("unknown protocol version"); | |
2543 | } | |
2544 | ||
42526b47 | 2545 | if (advertise_refs || !stateless_rpc) { |
42526b47 | 2546 | write_head_info(); |
42526b47 SP |
2547 | } |
2548 | if (advertise_refs) | |
2549 | return 0; | |
575f4974 | 2550 | |
2d103c31 MS |
2551 | packet_reader_init(&reader, 0, NULL, 0, |
2552 | PACKET_READ_CHOMP_NEWLINE | | |
2553 | PACKET_READ_DIE_ON_ERR_PACKET); | |
01f9ec64 | 2554 | |
afe8a907 | 2555 | if ((commands = read_head_info(&reader, &shallow))) { |
d4f694ba | 2556 | const char *unpack_status = NULL; |
77a9745d | 2557 | struct string_list push_options = STRING_LIST_INIT_DUP; |
d4f694ba | 2558 | |
c714e45f | 2559 | if (use_push_options) |
01f9ec64 | 2560 | read_push_options(&reader, &push_options); |
cbaf82cc JT |
2561 | if (!check_cert_push_options(&push_options)) { |
2562 | struct command *cmd; | |
2563 | for (cmd = commands; cmd; cmd = cmd->next) | |
2564 | cmd->error_string = "inconsistent push options"; | |
2565 | } | |
c714e45f | 2566 | |
5dbd7676 | 2567 | prepare_shallow_info(&si, &shallow); |
0a1bc12b NTND |
2568 | if (!si.nr_ours && !si.nr_theirs) |
2569 | shallow_update = 0; | |
5dbd7676 NTND |
2570 | if (!delete_only(commands)) { |
2571 | unpack_status = unpack_with_sideband(&si); | |
2572 | update_shallow_info(commands, &si, &ref); | |
2573 | } | |
83558686 | 2574 | use_keepalive = KEEPALIVE_ALWAYS; |
77a9745d SB |
2575 | execute_commands(commands, unpack_status, &si, |
2576 | &push_options); | |
576162a4 | 2577 | if (pack_lockfile) |
691f1a28 | 2578 | unlink_or_warn(pack_lockfile); |
d34182b9 | 2579 | sigchain_push(SIGPIPE, SIG_IGN); |
63518a57 JX |
2580 | if (report_status_v2) |
2581 | report_v2(commands, unpack_status); | |
2582 | else if (report_status) | |
5e1c71fd | 2583 | report(commands, unpack_status); |
d34182b9 | 2584 | sigchain_pop(SIGPIPE); |
77a9745d SB |
2585 | run_receive_hook(commands, "post-receive", 1, |
2586 | &push_options); | |
8e663d9e | 2587 | run_update_post_hook(commands); |
1fdd31cf | 2588 | free_commands(commands); |
4432dd6b | 2589 | string_list_clear(&push_options, 0); |
77e3efbf | 2590 | if (auto_gc) { |
860a2ebe LF |
2591 | struct child_process proc = CHILD_PROCESS_INIT; |
2592 | ||
2593 | proc.no_stdin = 1; | |
2594 | proc.stdout_to_stderr = 1; | |
2595 | proc.err = use_sideband ? -1 : 0; | |
c4dee2c0 | 2596 | proc.git_cmd = proc.close_object_store = 1; |
2b709893 ÆAB |
2597 | strvec_pushl(&proc.args, "gc", "--auto", "--quiet", |
2598 | NULL); | |
860a2ebe | 2599 | |
860a2ebe LF |
2600 | if (!start_command(&proc)) { |
2601 | if (use_sideband) | |
2602 | copy_to_sideband(proc.err, -1, NULL); | |
2603 | finish_command(&proc); | |
2604 | } | |
77e3efbf JH |
2605 | } |
2606 | if (auto_update_server_info) | |
2607 | update_server_info(0); | |
5dbd7676 | 2608 | clear_shallow_info(&si); |
7f8e9828 | 2609 | } |
38a81b4e SP |
2610 | if (use_sideband) |
2611 | packet_flush(1); | |
910650d2 | 2612 | oid_array_clear(&shallow); |
2613 | oid_array_clear(&ref); | |
9b67eb6f | 2614 | string_list_clear(&hidden_refs, 0); |
b89363e4 | 2615 | free((void *)push_cert_nonce); |
575f4974 LT |
2616 | return 0; |
2617 | } |