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