]>
Commit | Line | Data |
---|---|---|
36bf1958 EN |
1 | #include "git-compat-util.h" |
2 | #include "alloc.h" | |
b2141fc1 | 3 | #include "config.h" |
41771fa4 | 4 | #include "hex.h" |
9b288516 | 5 | #include "transport.h" |
5e3aba33 | 6 | #include "hook.h" |
c29727d5 DB |
7 | #include "pkt-line.h" |
8 | #include "fetch-pack.h" | |
47a59185 JH |
9 | #include "remote.h" |
10 | #include "connect.h" | |
40cb4fab | 11 | #include "send-pack.h" |
c29727d5 | 12 | #include "walker.h" |
c7a8a162 | 13 | #include "bundle.h" |
cd547b48 | 14 | #include "dir.h" |
36bf1958 | 15 | #include "gettext.h" |
cd547b48 | 16 | #include "refs.h" |
ec0cb496 | 17 | #include "refspec.h" |
e9fcd1e2 | 18 | #include "branch.h" |
638794cd | 19 | #include "url.h" |
d2b17b32 | 20 | #include "submodule.h" |
a762e51e | 21 | #include "string-list.h" |
fe299ec5 | 22 | #include "oid-array.h" |
af65f68c | 23 | #include "sigchain.h" |
e967ca38 | 24 | #include "transport-internal.h" |
ad6ac124 | 25 | #include "protocol.h" |
0d4a1321 | 26 | #include "object-store.h" |
960786e7 | 27 | #include "color.h" |
0cfde740 | 28 | #include "bundle-uri.h" |
960786e7 RD |
29 | |
30 | static int transport_use_color = -1; | |
31 | static char transport_colors[][COLOR_MAXLEN] = { | |
32 | GIT_COLOR_RESET, | |
33 | GIT_COLOR_RED /* REJECTED */ | |
34 | }; | |
35 | ||
36 | enum color_transport { | |
37 | TRANSPORT_COLOR_RESET = 0, | |
38 | TRANSPORT_COLOR_REJECTED = 1 | |
39 | }; | |
40 | ||
41 | static int transport_color_config(void) | |
42 | { | |
43 | const char *keys[] = { | |
44 | "color.transport.reset", | |
45 | "color.transport.rejected" | |
46 | }, *key = "color.transport"; | |
47 | char *value; | |
48 | int i; | |
49 | static int initialized; | |
50 | ||
51 | if (initialized) | |
52 | return 0; | |
53 | initialized = 1; | |
54 | ||
55 | if (!git_config_get_string(key, &value)) | |
56 | transport_use_color = git_config_colorbool(key, value); | |
57 | ||
58 | if (!want_color_stderr(transport_use_color)) | |
59 | return 0; | |
60 | ||
61 | for (i = 0; i < ARRAY_SIZE(keys); i++) | |
62 | if (!git_config_get_string(keys[i], &value)) { | |
63 | if (!value) | |
64 | return config_error_nonbool(keys[i]); | |
65 | if (color_parse(value, transport_colors[i]) < 0) | |
66 | return -1; | |
67 | } | |
68 | ||
69 | return 0; | |
70 | } | |
71 | ||
72 | static const char *transport_get_color(enum color_transport ix) | |
73 | { | |
74 | if (want_color_stderr(transport_use_color)) | |
75 | return transport_colors[ix]; | |
76 | return ""; | |
77 | } | |
cd547b48 | 78 | |
e9fcd1e2 IL |
79 | static void set_upstreams(struct transport *transport, struct ref *refs, |
80 | int pretend) | |
81 | { | |
82 | struct ref *ref; | |
83 | for (ref = refs; ref; ref = ref->next) { | |
84 | const char *localname; | |
85 | const char *tmp; | |
86 | const char *remotename; | |
e9fcd1e2 IL |
87 | int flag = 0; |
88 | /* | |
89 | * Check suitability for tracking. Must be successful / | |
90 | * already up-to-date ref create/modify (not delete). | |
91 | */ | |
92 | if (ref->status != REF_STATUS_OK && | |
93 | ref->status != REF_STATUS_UPTODATE) | |
94 | continue; | |
95 | if (!ref->peer_ref) | |
96 | continue; | |
f4e54d02 | 97 | if (is_null_oid(&ref->new_oid)) |
e9fcd1e2 IL |
98 | continue; |
99 | ||
100 | /* Follow symbolic refs (mainly for HEAD). */ | |
101 | localname = ref->peer_ref->name; | |
102 | remotename = ref->name; | |
7695d118 | 103 | tmp = resolve_ref_unsafe(localname, RESOLVE_REF_READING, |
744c040b | 104 | NULL, &flag); |
e9fcd1e2 | 105 | if (tmp && flag & REF_ISSYMREF && |
59556548 | 106 | starts_with(tmp, "refs/heads/")) |
e9fcd1e2 IL |
107 | localname = tmp; |
108 | ||
109 | /* Both source and destination must be local branches. */ | |
59556548 | 110 | if (!localname || !starts_with(localname, "refs/heads/")) |
e9fcd1e2 | 111 | continue; |
59556548 | 112 | if (!remotename || !starts_with(remotename, "refs/heads/")) |
e9fcd1e2 IL |
113 | continue; |
114 | ||
f3cce896 ØW |
115 | if (!pretend) { |
116 | int flag = transport->verbose < 0 ? 0 : BRANCH_CONFIG_VERBOSE; | |
117 | install_branch_config(flag, localname + 11, | |
118 | transport->remote->name, remotename); | |
119 | } else if (transport->verbose >= 0) | |
7ab1d44f | 120 | printf(_("Would set upstream of '%s' to '%s' of '%s'\n"), |
e9fcd1e2 IL |
121 | localname + 11, remotename + 11, |
122 | transport->remote->name); | |
123 | } | |
124 | } | |
125 | ||
c7a8a162 JS |
126 | struct bundle_transport_data { |
127 | int fd; | |
128 | struct bundle_header header; | |
fddf2ebe | 129 | unsigned get_refs_from_bundle_called : 1; |
c7a8a162 JS |
130 | }; |
131 | ||
0f0d118c | 132 | static void get_refs_from_bundle_inner(struct transport *transport) |
c7a8a162 JS |
133 | { |
134 | struct bundle_transport_data *data = transport->data; | |
64fcef2d | 135 | |
fddf2ebe JT |
136 | data->get_refs_from_bundle_called = 1; |
137 | ||
c7a8a162 JS |
138 | if (data->fd > 0) |
139 | close(data->fd); | |
140 | data->fd = read_bundle_header(transport->url, &data->header); | |
141 | if (data->fd < 0) | |
68e39e41 | 142 | die(_("could not read bundle '%s'"), transport->url); |
6161ce7b | 143 | |
144 | transport->hash_algo = data->header.hash_algo; | |
0f0d118c ÆAB |
145 | } |
146 | ||
147 | static struct ref *get_refs_from_bundle(struct transport *transport, | |
148 | int for_push, | |
5cf88fd8 | 149 | struct transport_ls_refs_options *transport_options UNUSED) |
0f0d118c ÆAB |
150 | { |
151 | struct bundle_transport_data *data = transport->data; | |
152 | struct ref *result = NULL; | |
153 | int i; | |
154 | ||
155 | if (for_push) | |
156 | return NULL; | |
157 | ||
158 | get_refs_from_bundle_inner(transport); | |
6161ce7b | 159 | |
c7a8a162 | 160 | for (i = 0; i < data->header.references.nr; i++) { |
10b635b7 ÆAB |
161 | struct string_list_item *e = data->header.references.items + i; |
162 | const char *name = e->string; | |
15e7c7dc | 163 | struct ref *ref = alloc_ref(name); |
10b635b7 | 164 | struct object_id *oid = e->util; |
15e7c7dc | 165 | oidcpy(&ref->old_oid, oid); |
c7a8a162 JS |
166 | ref->next = result; |
167 | result = ref; | |
168 | } | |
169 | return result; | |
170 | } | |
171 | ||
1788c39c | 172 | static int fetch_refs_from_bundle(struct transport *transport, |
b3edf335 JK |
173 | int nr_heads UNUSED, |
174 | struct ref **to_fetch UNUSED) | |
c7a8a162 JS |
175 | { |
176 | struct bundle_transport_data *data = transport->data; | |
7366096d | 177 | struct strvec extra_index_pack_args = STRVEC_INIT; |
6161ce7b | 178 | int ret; |
fddf2ebe | 179 | |
7366096d ÆAB |
180 | if (transport->progress) |
181 | strvec_push(&extra_index_pack_args, "-v"); | |
182 | ||
fddf2ebe | 183 | if (!data->get_refs_from_bundle_called) |
0f0d118c | 184 | get_refs_from_bundle_inner(transport); |
6161ce7b | 185 | ret = unbundle(the_repository, &data->header, data->fd, |
89bd7fed | 186 | &extra_index_pack_args, 0); |
6161ce7b | 187 | transport->hash_algo = data->header.hash_algo; |
188 | return ret; | |
c7a8a162 JS |
189 | } |
190 | ||
191 | static int close_bundle(struct transport *transport) | |
192 | { | |
193 | struct bundle_transport_data *data = transport->data; | |
194 | if (data->fd > 0) | |
195 | close(data->fd); | |
10b635b7 | 196 | bundle_header_release(&data->header); |
f4e95765 | 197 | free(data); |
c7a8a162 JS |
198 | return 0; |
199 | } | |
200 | ||
9b288516 | 201 | struct git_transport_data { |
aa5af974 | 202 | struct git_transport_options options; |
ba227857 DB |
203 | struct child_process *conn; |
204 | int fd[2]; | |
1b759e0c | 205 | unsigned finished_handshake : 1; |
432e9565 | 206 | enum protocol_version version; |
910650d2 | 207 | struct oid_array extra_have; |
208 | struct oid_array shallow; | |
9b288516 DB |
209 | }; |
210 | ||
aa5af974 | 211 | static int set_git_option(struct git_transport_options *opts, |
9b288516 DB |
212 | const char *name, const char *value) |
213 | { | |
c29727d5 | 214 | if (!strcmp(name, TRANS_OPT_UPLOADPACK)) { |
aa5af974 | 215 | opts->uploadpack = value; |
c29727d5 DB |
216 | return 0; |
217 | } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) { | |
aa5af974 | 218 | opts->receivepack = value; |
9b288516 DB |
219 | return 0; |
220 | } else if (!strcmp(name, TRANS_OPT_THIN)) { | |
aa5af974 | 221 | opts->thin = !!value; |
9b288516 | 222 | return 0; |
41fa7d2e | 223 | } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) { |
aa5af974 | 224 | opts->followtags = !!value; |
41fa7d2e | 225 | return 0; |
c29727d5 | 226 | } else if (!strcmp(name, TRANS_OPT_KEEP)) { |
aa5af974 | 227 | opts->keep = !!value; |
c29727d5 | 228 | return 0; |
48d25cae NTND |
229 | } else if (!strcmp(name, TRANS_OPT_UPDATE_SHALLOW)) { |
230 | opts->update_shallow = !!value; | |
231 | return 0; | |
c29727d5 DB |
232 | } else if (!strcmp(name, TRANS_OPT_DEPTH)) { |
233 | if (!value) | |
aa5af974 | 234 | opts->depth = 0; |
e7622ce8 NTND |
235 | else { |
236 | char *end; | |
237 | opts->depth = strtol(value, &end, 0); | |
238 | if (*end) | |
7ab1d44f | 239 | die(_("transport: invalid depth option '%s'"), value); |
e7622ce8 | 240 | } |
c29727d5 | 241 | return 0; |
508ea882 NTND |
242 | } else if (!strcmp(name, TRANS_OPT_DEEPEN_SINCE)) { |
243 | opts->deepen_since = value; | |
244 | return 0; | |
a45a2600 NTND |
245 | } else if (!strcmp(name, TRANS_OPT_DEEPEN_NOT)) { |
246 | opts->deepen_not = (const struct string_list *)value; | |
247 | return 0; | |
cccf74e2 NTND |
248 | } else if (!strcmp(name, TRANS_OPT_DEEPEN_RELATIVE)) { |
249 | opts->deepen_relative = !!value; | |
250 | return 0; | |
88e2f9ed JT |
251 | } else if (!strcmp(name, TRANS_OPT_FROM_PROMISOR)) { |
252 | opts->from_promisor = !!value; | |
253 | return 0; | |
640d8b72 | 254 | } else if (!strcmp(name, TRANS_OPT_LIST_OBJECTS_FILTER)) { |
489fc9ee | 255 | list_objects_filter_die_if_populated(&opts->filter_options); |
640d8b72 JH |
256 | parse_list_objects_filter(&opts->filter_options, value); |
257 | return 0; | |
3c7bab06 RC |
258 | } else if (!strcmp(name, TRANS_OPT_REFETCH)) { |
259 | opts->refetch = !!value; | |
260 | return 0; | |
4fe788b1 LL |
261 | } else if (!strcmp(name, TRANS_OPT_REJECT_SHALLOW)) { |
262 | opts->reject_shallow = !!value; | |
263 | return 0; | |
9b288516 DB |
264 | } |
265 | return 1; | |
266 | } | |
267 | ||
f3ee9ca5 | 268 | static int connect_setup(struct transport *transport, int for_push) |
ba227857 DB |
269 | { |
270 | struct git_transport_data *data = transport->data; | |
f3ee9ca5 | 271 | int flags = transport->verbose > 0 ? CONNECT_VERBOSE : 0; |
61b075bd IL |
272 | |
273 | if (data->conn) | |
274 | return 0; | |
275 | ||
c915f11e EW |
276 | switch (transport->family) { |
277 | case TRANSPORT_FAMILY_ALL: break; | |
278 | case TRANSPORT_FAMILY_IPV4: flags |= CONNECT_IPV4; break; | |
279 | case TRANSPORT_FAMILY_IPV6: flags |= CONNECT_IPV6; break; | |
280 | } | |
281 | ||
5a277f3f JH |
282 | data->conn = git_connect(data->fd, transport->url, |
283 | for_push ? data->options.receivepack : | |
284 | data->options.uploadpack, | |
f3ee9ca5 | 285 | flags); |
61b075bd | 286 | |
ba227857 DB |
287 | return 0; |
288 | } | |
289 | ||
35eb8240 JT |
290 | static void die_if_server_options(struct transport *transport) |
291 | { | |
292 | if (!transport->server_options || !transport->server_options->nr) | |
293 | return; | |
294 | advise(_("see protocol.version in 'git help config' for more details")); | |
295 | die(_("server options require protocol version 2 or later")); | |
296 | } | |
297 | ||
99bcb883 JT |
298 | /* |
299 | * Obtains the protocol version from the transport and writes it to | |
300 | * transport->data->version, first connecting if not already connected. | |
301 | * | |
302 | * If the protocol version is one that allows skipping the listing of remote | |
303 | * refs, and must_list_refs is 0, the listing of remote refs is skipped and | |
304 | * this function returns NULL. Otherwise, this function returns the list of | |
305 | * remote refs. | |
306 | */ | |
307 | static struct ref *handshake(struct transport *transport, int for_push, | |
39835409 | 308 | struct transport_ls_refs_options *options, |
99bcb883 | 309 | int must_list_refs) |
c29727d5 DB |
310 | { |
311 | struct git_transport_data *data = transport->data; | |
ad6ac124 BW |
312 | struct ref *refs = NULL; |
313 | struct packet_reader reader; | |
23bf486a JS |
314 | int sid_len; |
315 | const char *server_sid; | |
c29727d5 | 316 | |
f3ee9ca5 | 317 | connect_setup(transport, for_push); |
ad6ac124 BW |
318 | |
319 | packet_reader_init(&reader, data->fd[0], NULL, 0, | |
320 | PACKET_READ_CHOMP_NEWLINE | | |
2d103c31 MS |
321 | PACKET_READ_GENTLE_ON_EOF | |
322 | PACKET_READ_DIE_ON_ERR_PACKET); | |
ad6ac124 | 323 | |
432e9565 BW |
324 | data->version = discover_version(&reader); |
325 | switch (data->version) { | |
8f6982b4 | 326 | case protocol_v2: |
23bf486a JS |
327 | if (server_feature_v2("session-id", &server_sid)) |
328 | trace2_data_string("transfer", NULL, "server-sid", server_sid); | |
99bcb883 JT |
329 | if (must_list_refs) |
330 | get_remote_refs(data->fd[1], &reader, &refs, for_push, | |
39835409 | 331 | options, |
b0df0c16 DL |
332 | transport->server_options, |
333 | transport->stateless_rpc); | |
8f6982b4 | 334 | break; |
ad6ac124 BW |
335 | case protocol_v1: |
336 | case protocol_v0: | |
35eb8240 | 337 | die_if_server_options(transport); |
ad6ac124 BW |
338 | get_remote_heads(&reader, &refs, |
339 | for_push ? REF_NORMAL : 0, | |
340 | &data->extra_have, | |
341 | &data->shallow); | |
23bf486a JS |
342 | server_sid = server_feature_value("session-id", &sid_len); |
343 | if (server_sid) { | |
344 | char *sid = xstrndup(server_sid, sid_len); | |
345 | trace2_data_string("transfer", NULL, "server-sid", sid); | |
346 | free(sid); | |
347 | } | |
ad6ac124 BW |
348 | break; |
349 | case protocol_unknown_version: | |
350 | BUG("unknown protocol version"); | |
351 | } | |
1b759e0c | 352 | data->finished_handshake = 1; |
7c97af4d | 353 | transport->hash_algo = reader.hash_algo; |
c29727d5 | 354 | |
99bcb883 JT |
355 | if (reader.line_peeked) |
356 | BUG("buffer must be empty at the end of handshake()"); | |
357 | ||
c29727d5 DB |
358 | return refs; |
359 | } | |
360 | ||
99bcb883 | 361 | static struct ref *get_refs_via_connect(struct transport *transport, int for_push, |
39835409 | 362 | struct transport_ls_refs_options *options) |
99bcb883 | 363 | { |
39835409 | 364 | return handshake(transport, for_push, options, 1); |
99bcb883 JT |
365 | } |
366 | ||
0cfde740 ÆAB |
367 | static int get_bundle_uri(struct transport *transport) |
368 | { | |
369 | struct git_transport_data *data = transport->data; | |
370 | struct packet_reader reader; | |
371 | int stateless_rpc = transport->stateless_rpc; | |
372 | ||
373 | if (!transport->bundles) { | |
374 | CALLOC_ARRAY(transport->bundles, 1); | |
375 | init_bundle_list(transport->bundles); | |
376 | } | |
377 | ||
70b9c103 ÆAB |
378 | if (!data->finished_handshake) { |
379 | struct ref *refs = handshake(transport, 0, NULL, 0); | |
380 | ||
381 | if (refs) | |
382 | free_refs(refs); | |
383 | } | |
384 | ||
0cfde740 ÆAB |
385 | /* |
386 | * "Support" protocol v0 and v2 without bundle-uri support by | |
387 | * silently degrading to a NOOP. | |
388 | */ | |
0903d8bb | 389 | if (!server_supports_v2("bundle-uri")) |
0cfde740 ÆAB |
390 | return 0; |
391 | ||
392 | packet_reader_init(&reader, data->fd[0], NULL, 0, | |
393 | PACKET_READ_CHOMP_NEWLINE | | |
394 | PACKET_READ_GENTLE_ON_EOF); | |
395 | ||
396 | return get_remote_bundle_uri(data->fd[1], &reader, | |
397 | transport->bundles, stateless_rpc); | |
398 | } | |
399 | ||
1788c39c | 400 | static int fetch_refs_via_pack(struct transport *transport, |
e2842b39 | 401 | int nr_heads, struct ref **to_fetch) |
c29727d5 | 402 | { |
e70a65c5 | 403 | int ret = 0; |
c29727d5 | 404 | struct git_transport_data *data = transport->data; |
432e9565 | 405 | struct ref *refs = NULL; |
c29727d5 | 406 | struct fetch_pack_args args; |
00183cbb | 407 | struct ref *refs_tmp = NULL; |
c29727d5 | 408 | |
fa740529 | 409 | memset(&args, 0, sizeof(args)); |
aa5af974 IL |
410 | args.uploadpack = data->options.uploadpack; |
411 | args.keep_pack = data->options.keep; | |
fa740529 | 412 | args.lock_pack = 1; |
aa5af974 IL |
413 | args.use_thin_pack = data->options.thin; |
414 | args.include_tag = data->options.followtags; | |
bd2c86ef | 415 | args.verbose = (transport->verbose > 1); |
fe8aa148 | 416 | args.quiet = (transport->verbose < 0); |
d01b3c02 | 417 | args.no_progress = !transport->progress; |
aa5af974 | 418 | args.depth = data->options.depth; |
508ea882 | 419 | args.deepen_since = data->options.deepen_since; |
a45a2600 | 420 | args.deepen_not = data->options.deepen_not; |
cccf74e2 | 421 | args.deepen_relative = data->options.deepen_relative; |
c6807a40 NTND |
422 | args.check_self_contained_and_connected = |
423 | data->options.check_self_contained_and_connected; | |
beea4152 | 424 | args.cloning = transport->cloning; |
48d25cae | 425 | args.update_shallow = data->options.update_shallow; |
88e2f9ed | 426 | args.from_promisor = data->options.from_promisor; |
3f0e86a1 JK |
427 | list_objects_filter_copy(&args.filter_options, |
428 | &data->options.filter_options); | |
3c7bab06 | 429 | args.refetch = data->options.refetch; |
edc9caf7 | 430 | args.stateless_rpc = transport->stateless_rpc; |
5e3548ef | 431 | args.server_options = transport->server_options; |
3390e42a | 432 | args.negotiation_tips = data->options.negotiation_tips; |
4fe788b1 | 433 | args.reject_shallow_remote = transport->smart_options->reject_shallow; |
c29727d5 | 434 | |
1b759e0c | 435 | if (!data->finished_handshake) { |
01775651 JT |
436 | int i; |
437 | int must_list_refs = 0; | |
438 | for (i = 0; i < nr_heads; i++) { | |
439 | if (!to_fetch[i]->exact_oid) { | |
440 | must_list_refs = 1; | |
441 | break; | |
442 | } | |
443 | } | |
444 | refs_tmp = handshake(transport, 0, NULL, must_list_refs); | |
445 | } | |
ba227857 | 446 | |
dde72f94 | 447 | if (data->version == protocol_unknown_version) |
432e9565 | 448 | BUG("unknown protocol version"); |
dde72f94 DL |
449 | else if (data->version <= protocol_v1) |
450 | die_if_server_options(transport); | |
451 | ||
9c1e657a JT |
452 | if (data->options.acked_commits) { |
453 | if (data->version < protocol_v2) { | |
454 | warning(_("--negotiate-only requires protocol v2")); | |
455 | ret = -1; | |
456 | } else if (!server_supports_feature("fetch", "wait-for-done", 0)) { | |
457 | warning(_("server does not support wait-for-done")); | |
458 | ret = -1; | |
459 | } else { | |
460 | negotiate_using_fetch(data->options.negotiation_tips, | |
461 | transport->server_options, | |
462 | transport->stateless_rpc, | |
463 | data->fd, | |
464 | data->options.acked_commits); | |
465 | ret = 0; | |
466 | } | |
467 | goto cleanup; | |
468 | } | |
469 | ||
dde72f94 DL |
470 | refs = fetch_pack(&args, data->fd, |
471 | refs_tmp ? refs_tmp : transport->remote_refs, | |
472 | to_fetch, nr_heads, &data->shallow, | |
34e849b0 | 473 | &transport->pack_lockfiles, data->version); |
ba227857 | 474 | |
1b759e0c | 475 | data->finished_handshake = 0; |
c6807a40 NTND |
476 | data->options.self_contained_and_connected = |
477 | args.self_contained_and_connected; | |
cf1e7c07 | 478 | data->options.connectivity_checked = args.connectivity_checked; |
c29727d5 | 479 | |
afe8a907 | 480 | if (!refs) |
e70a65c5 MM |
481 | ret = -1; |
482 | if (report_unmatched_refs(to_fetch, nr_heads)) | |
483 | ret = -1; | |
484 | ||
9c1e657a JT |
485 | cleanup: |
486 | close(data->fd[0]); | |
ae1a7eef JK |
487 | if (data->fd[1] >= 0) |
488 | close(data->fd[1]); | |
9c1e657a JT |
489 | if (finish_connect(data->conn)) |
490 | ret = -1; | |
491 | data->conn = NULL; | |
492 | ||
00183cbb | 493 | free_refs(refs_tmp); |
e2842b39 | 494 | free_refs(refs); |
3f0e86a1 | 495 | list_objects_filter_release(&args.filter_options); |
e70a65c5 | 496 | return ret; |
c29727d5 DB |
497 | } |
498 | ||
481c7a6d JK |
499 | static int push_had_errors(struct ref *ref) |
500 | { | |
501 | for (; ref; ref = ref->next) { | |
502 | switch (ref->status) { | |
503 | case REF_STATUS_NONE: | |
504 | case REF_STATUS_UPTODATE: | |
505 | case REF_STATUS_OK: | |
506 | break; | |
507 | default: | |
508 | return 1; | |
509 | } | |
510 | } | |
511 | return 0; | |
512 | } | |
513 | ||
f1863d0d | 514 | int transport_refs_pushed(struct ref *ref) |
64fcef2d DB |
515 | { |
516 | for (; ref; ref = ref->next) { | |
517 | switch(ref->status) { | |
518 | case REF_STATUS_NONE: | |
519 | case REF_STATUS_UPTODATE: | |
520 | break; | |
521 | default: | |
522 | return 1; | |
523 | } | |
524 | } | |
525 | return 0; | |
526 | } | |
527 | ||
1702ae6f JX |
528 | static void update_one_tracking_ref(struct remote *remote, char *refname, |
529 | struct object_id *new_oid, int deletion, | |
530 | int verbose) | |
64fcef2d | 531 | { |
0ad4a5ff | 532 | struct refspec_item rs; |
64fcef2d | 533 | |
95e7c385 | 534 | memset(&rs, 0, sizeof(rs)); |
1702ae6f | 535 | rs.src = refname; |
64fcef2d DB |
536 | rs.dst = NULL; |
537 | ||
538 | if (!remote_find_tracking(remote, &rs)) { | |
539 | if (verbose) | |
540 | fprintf(stderr, "updating local tracking ref '%s'\n", rs.dst); | |
1702ae6f | 541 | if (deletion) |
755b49ae | 542 | delete_ref(NULL, rs.dst, NULL, 0); |
1702ae6f JX |
543 | else |
544 | update_ref("update by push", rs.dst, new_oid, | |
ae077771 | 545 | NULL, 0, 0); |
64fcef2d DB |
546 | free(rs.dst); |
547 | } | |
548 | } | |
549 | ||
1702ae6f JX |
550 | void transport_update_tracking_ref(struct remote *remote, struct ref *ref, int verbose) |
551 | { | |
552 | char *refname; | |
553 | struct object_id *new_oid; | |
554 | struct ref_push_report *report; | |
555 | ||
556 | if (ref->status != REF_STATUS_OK && ref->status != REF_STATUS_UPTODATE) | |
557 | return; | |
558 | ||
559 | report = ref->report; | |
560 | if (!report) | |
561 | update_one_tracking_ref(remote, ref->name, &ref->new_oid, | |
562 | ref->deletion, verbose); | |
563 | else | |
564 | for (; report; report = report->next) { | |
565 | refname = report->ref_name ? (char *)report->ref_name : ref->name; | |
566 | new_oid = report->new_oid ? report->new_oid : &ref->new_oid; | |
567 | update_one_tracking_ref(remote, refname, new_oid, | |
568 | is_null_oid(new_oid), verbose); | |
569 | } | |
570 | } | |
571 | ||
7101e10c JH |
572 | static void print_ref_status(char flag, const char *summary, |
573 | struct ref *to, struct ref *from, const char *msg, | |
63518a57 | 574 | struct ref_push_report *report, |
7101e10c | 575 | int porcelain, int summary_width) |
64fcef2d | 576 | { |
63518a57 JX |
577 | const char *to_name; |
578 | ||
579 | if (report && report->ref_name) | |
580 | to_name = report->ref_name; | |
581 | else | |
582 | to_name = to->name; | |
583 | ||
1965ff74 LA |
584 | if (porcelain) { |
585 | if (from) | |
63518a57 | 586 | fprintf(stdout, "%c\t%s:%s\t", flag, from->name, to_name); |
1965ff74 | 587 | else |
63518a57 | 588 | fprintf(stdout, "%c\t:%s\t", flag, to_name); |
1965ff74 LA |
589 | if (msg) |
590 | fprintf(stdout, "%s (%s)\n", summary, msg); | |
591 | else | |
592 | fprintf(stdout, "%s\n", summary); | |
593 | } else { | |
960786e7 RD |
594 | const char *red = "", *reset = ""; |
595 | if (push_had_errors(to)) { | |
596 | red = transport_get_color(TRANSPORT_COLOR_REJECTED); | |
597 | reset = transport_get_color(TRANSPORT_COLOR_RESET); | |
598 | } | |
599 | fprintf(stderr, " %s%c %-*s%s ", red, flag, summary_width, | |
600 | summary, reset); | |
1965ff74 | 601 | if (from) |
63518a57 JX |
602 | fprintf(stderr, "%s -> %s", |
603 | prettify_refname(from->name), | |
604 | prettify_refname(to_name)); | |
1965ff74 | 605 | else |
63518a57 | 606 | fputs(prettify_refname(to_name), stderr); |
1965ff74 LA |
607 | if (msg) { |
608 | fputs(" (", stderr); | |
609 | fputs(msg, stderr); | |
610 | fputc(')', stderr); | |
611 | } | |
612 | fputc('\n', stderr); | |
64fcef2d | 613 | } |
64fcef2d DB |
614 | } |
615 | ||
63518a57 JX |
616 | static void print_ok_ref_status(struct ref *ref, |
617 | struct ref_push_report *report, | |
618 | int porcelain, int summary_width) | |
64fcef2d | 619 | { |
63518a57 JX |
620 | struct object_id *old_oid; |
621 | struct object_id *new_oid; | |
622 | const char *ref_name; | |
623 | int forced_update; | |
624 | ||
625 | if (report && report->old_oid) | |
626 | old_oid = report->old_oid; | |
627 | else | |
628 | old_oid = &ref->old_oid; | |
629 | if (report && report->new_oid) | |
630 | new_oid = report->new_oid; | |
631 | else | |
632 | new_oid = &ref->new_oid; | |
633 | if (report && report->forced_update) | |
634 | forced_update = report->forced_update; | |
635 | else | |
636 | forced_update = ref->forced_update; | |
637 | if (report && report->ref_name) | |
638 | ref_name = report->ref_name; | |
639 | else | |
640 | ref_name = ref->name; | |
641 | ||
64fcef2d | 642 | if (ref->deletion) |
7101e10c | 643 | print_ref_status('-', "[deleted]", ref, NULL, NULL, |
63518a57 JX |
644 | report, porcelain, summary_width); |
645 | else if (is_null_oid(old_oid)) | |
64fcef2d | 646 | print_ref_status('*', |
63518a57 | 647 | (starts_with(ref_name, "refs/tags/") |
917c6125 | 648 | ? "[new tag]" |
63518a57 | 649 | : (starts_with(ref_name, "refs/heads/") |
917c6125 JX |
650 | ? "[new branch]" |
651 | : "[new reference]")), | |
63518a57 JX |
652 | ref, ref->peer_ref, NULL, |
653 | report, porcelain, summary_width); | |
64fcef2d | 654 | else { |
bd22d4ff | 655 | struct strbuf quickref = STRBUF_INIT; |
64fcef2d DB |
656 | char type; |
657 | const char *msg; | |
658 | ||
63518a57 | 659 | strbuf_add_unique_abbrev(&quickref, old_oid, |
1eb47f16 | 660 | DEFAULT_ABBREV); |
63518a57 | 661 | if (forced_update) { |
bd22d4ff | 662 | strbuf_addstr(&quickref, "..."); |
64fcef2d DB |
663 | type = '+'; |
664 | msg = "forced update"; | |
665 | } else { | |
bd22d4ff | 666 | strbuf_addstr(&quickref, ".."); |
64fcef2d DB |
667 | type = ' '; |
668 | msg = NULL; | |
669 | } | |
63518a57 | 670 | strbuf_add_unique_abbrev(&quickref, new_oid, |
1eb47f16 | 671 | DEFAULT_ABBREV); |
64fcef2d | 672 | |
7101e10c | 673 | print_ref_status(type, quickref.buf, ref, ref->peer_ref, msg, |
63518a57 | 674 | report, porcelain, summary_width); |
bd22d4ff | 675 | strbuf_release(&quickref); |
64fcef2d DB |
676 | } |
677 | } | |
678 | ||
63518a57 JX |
679 | static int print_one_push_report(struct ref *ref, const char *dest, int count, |
680 | struct ref_push_report *report, | |
7101e10c | 681 | int porcelain, int summary_width) |
64fcef2d | 682 | { |
882d49ca JK |
683 | if (!count) { |
684 | char *url = transport_anonymize_url(dest); | |
685 | fprintf(porcelain ? stdout : stderr, "To %s\n", url); | |
686 | free(url); | |
687 | } | |
64fcef2d DB |
688 | |
689 | switch(ref->status) { | |
690 | case REF_STATUS_NONE: | |
7101e10c | 691 | print_ref_status('X', "[no match]", ref, NULL, NULL, |
63518a57 | 692 | report, porcelain, summary_width); |
64fcef2d DB |
693 | break; |
694 | case REF_STATUS_REJECT_NODELETE: | |
695 | print_ref_status('!', "[rejected]", ref, NULL, | |
7101e10c | 696 | "remote does not support deleting refs", |
63518a57 | 697 | report, porcelain, summary_width); |
64fcef2d DB |
698 | break; |
699 | case REF_STATUS_UPTODATE: | |
700 | print_ref_status('=', "[up to date]", ref, | |
63518a57 JX |
701 | ref->peer_ref, NULL, |
702 | report, porcelain, summary_width); | |
64fcef2d DB |
703 | break; |
704 | case REF_STATUS_REJECT_NONFASTFORWARD: | |
705 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
63518a57 JX |
706 | "non-fast-forward", |
707 | report, porcelain, summary_width); | |
64fcef2d | 708 | break; |
dbfeddb1 CR |
709 | case REF_STATUS_REJECT_ALREADY_EXISTS: |
710 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
63518a57 JX |
711 | "already exists", |
712 | report, porcelain, summary_width); | |
dbfeddb1 | 713 | break; |
75e5c0dc JH |
714 | case REF_STATUS_REJECT_FETCH_FIRST: |
715 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
63518a57 JX |
716 | "fetch first", |
717 | report, porcelain, summary_width); | |
75e5c0dc JH |
718 | break; |
719 | case REF_STATUS_REJECT_NEEDS_FORCE: | |
720 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
63518a57 JX |
721 | "needs force", |
722 | report, porcelain, summary_width); | |
75e5c0dc | 723 | break; |
631b5ef2 JH |
724 | case REF_STATUS_REJECT_STALE: |
725 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
63518a57 JX |
726 | "stale info", |
727 | report, porcelain, summary_width); | |
631b5ef2 | 728 | break; |
99a1f9ae SK |
729 | case REF_STATUS_REJECT_REMOTE_UPDATED: |
730 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
731 | "remote ref updated since checkout", | |
732 | report, porcelain, summary_width); | |
733 | break; | |
4820a33b NTND |
734 | case REF_STATUS_REJECT_SHALLOW: |
735 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
7101e10c | 736 | "new shallow roots not allowed", |
63518a57 | 737 | report, porcelain, summary_width); |
4820a33b | 738 | break; |
64fcef2d DB |
739 | case REF_STATUS_REMOTE_REJECT: |
740 | print_ref_status('!', "[remote rejected]", ref, | |
7101e10c | 741 | ref->deletion ? NULL : ref->peer_ref, |
63518a57 JX |
742 | ref->remote_status, |
743 | report, porcelain, summary_width); | |
64fcef2d DB |
744 | break; |
745 | case REF_STATUS_EXPECTING_REPORT: | |
746 | print_ref_status('!', "[remote failure]", ref, | |
7101e10c JH |
747 | ref->deletion ? NULL : ref->peer_ref, |
748 | "remote failed to report status", | |
63518a57 | 749 | report, porcelain, summary_width); |
64fcef2d | 750 | break; |
4ff17f10 RS |
751 | case REF_STATUS_ATOMIC_PUSH_FAILED: |
752 | print_ref_status('!', "[rejected]", ref, ref->peer_ref, | |
63518a57 JX |
753 | "atomic push failed", |
754 | report, porcelain, summary_width); | |
4ff17f10 | 755 | break; |
64fcef2d | 756 | case REF_STATUS_OK: |
63518a57 | 757 | print_ok_ref_status(ref, report, porcelain, summary_width); |
64fcef2d DB |
758 | break; |
759 | } | |
760 | ||
761 | return 1; | |
762 | } | |
763 | ||
63518a57 JX |
764 | static int print_one_push_status(struct ref *ref, const char *dest, int count, |
765 | int porcelain, int summary_width) | |
766 | { | |
767 | struct ref_push_report *report; | |
768 | int n = 0; | |
769 | ||
770 | if (!ref->report) | |
771 | return print_one_push_report(ref, dest, count, | |
772 | NULL, porcelain, summary_width); | |
773 | ||
774 | for (report = ref->report; report; report = report->next) | |
775 | print_one_push_report(ref, dest, count + n++, | |
776 | report, porcelain, summary_width); | |
777 | return n; | |
778 | } | |
779 | ||
db98d9ba JH |
780 | static int measure_abbrev(const struct object_id *oid, int sofar) |
781 | { | |
dc01505f | 782 | char hex[GIT_MAX_HEXSZ + 1]; |
aab9583f | 783 | int w = find_unique_abbrev_r(hex, oid, DEFAULT_ABBREV); |
db98d9ba JH |
784 | |
785 | return (w < sofar) ? sofar : w; | |
786 | } | |
787 | ||
11fd66de JH |
788 | int transport_summary_width(const struct ref *refs) |
789 | { | |
db98d9ba JH |
790 | int maxw = -1; |
791 | ||
792 | for (; refs; refs = refs->next) { | |
793 | maxw = measure_abbrev(&refs->old_oid, maxw); | |
794 | maxw = measure_abbrev(&refs->new_oid, maxw); | |
795 | } | |
796 | if (maxw < 0) | |
797 | maxw = FALLBACK_DEFAULT_ABBREV; | |
798 | return (2 * maxw + 3); | |
11fd66de JH |
799 | } |
800 | ||
f1863d0d | 801 | void transport_print_push_status(const char *dest, struct ref *refs, |
10643d4e | 802 | int verbose, int porcelain, unsigned int *reject_reasons) |
64fcef2d DB |
803 | { |
804 | struct ref *ref; | |
805 | int n = 0; | |
f25950f3 | 806 | char *head; |
11fd66de | 807 | int summary_width = transport_summary_width(refs); |
f25950f3 | 808 | |
960786e7 RD |
809 | if (transport_color_config() < 0) |
810 | warning(_("could not parse transport.color.* config")); | |
811 | ||
efbd4fdf | 812 | head = resolve_refdup("HEAD", RESOLVE_REF_READING, NULL, NULL); |
64fcef2d DB |
813 | |
814 | if (verbose) { | |
815 | for (ref = refs; ref; ref = ref->next) | |
816 | if (ref->status == REF_STATUS_UPTODATE) | |
7101e10c JH |
817 | n += print_one_push_status(ref, dest, n, |
818 | porcelain, summary_width); | |
64fcef2d DB |
819 | } |
820 | ||
821 | for (ref = refs; ref; ref = ref->next) | |
822 | if (ref->status == REF_STATUS_OK) | |
7101e10c JH |
823 | n += print_one_push_status(ref, dest, n, |
824 | porcelain, summary_width); | |
64fcef2d | 825 | |
10643d4e | 826 | *reject_reasons = 0; |
64fcef2d DB |
827 | for (ref = refs; ref; ref = ref->next) { |
828 | if (ref->status != REF_STATUS_NONE && | |
829 | ref->status != REF_STATUS_UPTODATE && | |
830 | ref->status != REF_STATUS_OK) | |
7101e10c JH |
831 | n += print_one_push_status(ref, dest, n, |
832 | porcelain, summary_width); | |
10643d4e | 833 | if (ref->status == REF_STATUS_REJECT_NONFASTFORWARD) { |
1d2c14df | 834 | if (head != NULL && !strcmp(head, ref->name)) |
10643d4e | 835 | *reject_reasons |= REJECT_NON_FF_HEAD; |
f25950f3 | 836 | else |
10643d4e | 837 | *reject_reasons |= REJECT_NON_FF_OTHER; |
dbfeddb1 CR |
838 | } else if (ref->status == REF_STATUS_REJECT_ALREADY_EXISTS) { |
839 | *reject_reasons |= REJECT_ALREADY_EXISTS; | |
75e5c0dc JH |
840 | } else if (ref->status == REF_STATUS_REJECT_FETCH_FIRST) { |
841 | *reject_reasons |= REJECT_FETCH_FIRST; | |
842 | } else if (ref->status == REF_STATUS_REJECT_NEEDS_FORCE) { | |
843 | *reject_reasons |= REJECT_NEEDS_FORCE; | |
3b990aa6 SK |
844 | } else if (ref->status == REF_STATUS_REJECT_REMOTE_UPDATED) { |
845 | *reject_reasons |= REJECT_REF_NEEDS_UPDATE; | |
f25950f3 | 846 | } |
64fcef2d | 847 | } |
dc76c7f7 | 848 | free(head); |
64fcef2d DB |
849 | } |
850 | ||
64fcef2d | 851 | static int git_transport_push(struct transport *transport, struct ref *remote_refs, int flags) |
f3fa1838 | 852 | { |
9b288516 | 853 | struct git_transport_data *data = transport->data; |
40cb4fab | 854 | struct send_pack_args args; |
432e9565 | 855 | int ret = 0; |
64fcef2d | 856 | |
960786e7 RD |
857 | if (transport_color_config() < 0) |
858 | return -1; | |
859 | ||
1b759e0c | 860 | if (!data->finished_handshake) |
834cf34b | 861 | get_refs_via_connect(transport, 1, NULL); |
9b288516 | 862 | |
de1a2fdd | 863 | memset(&args, 0, sizeof(args)); |
94c89ba6 | 864 | args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR); |
40cb4fab | 865 | args.force_update = !!(flags & TRANSPORT_PUSH_FORCE); |
aa5af974 | 866 | args.use_thin_pack = data->options.thin; |
8afd8dc0 TRC |
867 | args.verbose = (transport->verbose > 0); |
868 | args.quiet = (transport->verbose < 0); | |
d7c411b7 | 869 | args.progress = transport->progress; |
40cb4fab | 870 | args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN); |
77555854 | 871 | args.porcelain = !!(flags & TRANSPORT_PUSH_PORCELAIN); |
d0e8e09c | 872 | args.atomic = !!(flags & TRANSPORT_PUSH_ATOMIC); |
f6a4e61f | 873 | args.push_options = transport->push_options; |
9be89160 | 874 | args.url = transport->url; |
40cb4fab | 875 | |
30261094 DB |
876 | if (flags & TRANSPORT_PUSH_CERT_ALWAYS) |
877 | args.push_cert = SEND_PACK_PUSH_CERT_ALWAYS; | |
878 | else if (flags & TRANSPORT_PUSH_CERT_IF_ASKED) | |
879 | args.push_cert = SEND_PACK_PUSH_CERT_IF_ASKED; | |
880 | else | |
881 | args.push_cert = SEND_PACK_PUSH_CERT_NEVER; | |
882 | ||
432e9565 | 883 | switch (data->version) { |
8f6982b4 | 884 | case protocol_v2: |
68e39e41 | 885 | die(_("support for protocol v2 not implemented yet")); |
8f6982b4 | 886 | break; |
432e9565 BW |
887 | case protocol_v1: |
888 | case protocol_v0: | |
889 | ret = send_pack(&args, data->fd, data->conn, remote_refs, | |
890 | &data->extra_have); | |
891 | break; | |
892 | case protocol_unknown_version: | |
893 | BUG("unknown protocol version"); | |
894 | } | |
64fcef2d DB |
895 | |
896 | close(data->fd[1]); | |
897 | close(data->fd[0]); | |
7dcbeaa0 JX |
898 | /* |
899 | * Atomic push may abort the connection early and close the pipe, | |
900 | * which may cause an error for `finish_connect()`. Ignore this error | |
901 | * for atomic git-push. | |
902 | */ | |
903 | if (ret || args.atomic) | |
904 | finish_connect(data->conn); | |
905 | else | |
906 | ret = finish_connect(data->conn); | |
64fcef2d | 907 | data->conn = NULL; |
1b759e0c | 908 | data->finished_handshake = 0; |
64fcef2d DB |
909 | |
910 | return ret; | |
9b288516 DB |
911 | } |
912 | ||
b236752a IL |
913 | static int connect_git(struct transport *transport, const char *name, |
914 | const char *executable, int fd[2]) | |
915 | { | |
916 | struct git_transport_data *data = transport->data; | |
917 | data->conn = git_connect(data->fd, transport->url, | |
918 | executable, 0); | |
919 | fd[0] = data->fd[0]; | |
920 | fd[1] = data->fd[1]; | |
921 | return 0; | |
922 | } | |
923 | ||
f4e95765 SP |
924 | static int disconnect_git(struct transport *transport) |
925 | { | |
ba227857 DB |
926 | struct git_transport_data *data = transport->data; |
927 | if (data->conn) { | |
1b759e0c | 928 | if (data->finished_handshake && !transport->stateless_rpc) |
61b075bd | 929 | packet_flush(data->fd[1]); |
ba227857 | 930 | close(data->fd[0]); |
ae1a7eef JK |
931 | if (data->fd[1] >= 0) |
932 | close(data->fd[1]); | |
ba227857 DB |
933 | finish_connect(data->conn); |
934 | } | |
935 | ||
dd49699d | 936 | list_objects_filter_release(&data->options.filter_options); |
ba227857 | 937 | free(data); |
f4e95765 SP |
938 | return 0; |
939 | } | |
940 | ||
e967ca38 | 941 | static struct transport_vtable taken_over_vtable = { |
1fd88224 | 942 | .get_refs_list = get_refs_via_connect, |
0cfde740 | 943 | .get_bundle_uri = get_bundle_uri, |
1fd88224 ÆAB |
944 | .fetch_refs = fetch_refs_via_pack, |
945 | .push_refs = git_transport_push, | |
946 | .disconnect = disconnect_git | |
e967ca38 JT |
947 | }; |
948 | ||
61b075bd IL |
949 | void transport_take_over(struct transport *transport, |
950 | struct child_process *child) | |
951 | { | |
952 | struct git_transport_data *data; | |
953 | ||
954 | if (!transport->smart_options) | |
033abf97 | 955 | BUG("taking over transport requires non-NULL " |
61b075bd IL |
956 | "smart_options field."); |
957 | ||
ca56dadb | 958 | CALLOC_ARRAY(data, 1); |
61b075bd IL |
959 | data->options = *transport->smart_options; |
960 | data->conn = child; | |
961 | data->fd[0] = data->conn->out; | |
962 | data->fd[1] = data->conn->in; | |
1b759e0c | 963 | data->finished_handshake = 0; |
61b075bd IL |
964 | transport->data = data; |
965 | ||
e967ca38 | 966 | transport->vtable = &taken_over_vtable; |
61b075bd | 967 | transport->smart_options = &(data->options); |
b26ed430 JH |
968 | |
969 | transport->cannot_reuse = 1; | |
61b075bd IL |
970 | } |
971 | ||
9b288516 DB |
972 | static int is_file(const char *url) |
973 | { | |
974 | struct stat buf; | |
975 | if (stat(url, &buf)) | |
976 | return 0; | |
977 | return S_ISREG(buf.st_mode); | |
978 | } | |
979 | ||
25d5cc48 IL |
980 | static int external_specification_len(const char *url) |
981 | { | |
982 | return strchr(url, ':') - url; | |
983 | } | |
984 | ||
f5adaa5c | 985 | static const struct string_list *protocol_allow_list(void) |
a5adaced | 986 | { |
5088d3b3 JK |
987 | static int enabled = -1; |
988 | static struct string_list allowed = STRING_LIST_INIT_DUP; | |
a5adaced | 989 | |
5088d3b3 JK |
990 | if (enabled < 0) { |
991 | const char *v = getenv("GIT_ALLOW_PROTOCOL"); | |
992 | if (v) { | |
993 | string_list_split(&allowed, v, ':', -1); | |
994 | string_list_sort(&allowed); | |
995 | enabled = 1; | |
996 | } else { | |
997 | enabled = 0; | |
998 | } | |
999 | } | |
1000 | ||
1001 | return enabled ? &allowed : NULL; | |
1002 | } | |
a5adaced | 1003 | |
f1762d77 BW |
1004 | enum protocol_allow_config { |
1005 | PROTOCOL_ALLOW_NEVER = 0, | |
1006 | PROTOCOL_ALLOW_USER_ONLY, | |
1007 | PROTOCOL_ALLOW_ALWAYS | |
1008 | }; | |
1009 | ||
1010 | static enum protocol_allow_config parse_protocol_config(const char *key, | |
1011 | const char *value) | |
5088d3b3 | 1012 | { |
f1762d77 BW |
1013 | if (!strcasecmp(value, "always")) |
1014 | return PROTOCOL_ALLOW_ALWAYS; | |
1015 | else if (!strcasecmp(value, "never")) | |
1016 | return PROTOCOL_ALLOW_NEVER; | |
1017 | else if (!strcasecmp(value, "user")) | |
1018 | return PROTOCOL_ALLOW_USER_ONLY; | |
1019 | ||
68e39e41 | 1020 | die(_("unknown value for config '%s': %s"), key, value); |
5088d3b3 JK |
1021 | } |
1022 | ||
f1762d77 | 1023 | static enum protocol_allow_config get_protocol_config(const char *type) |
5088d3b3 | 1024 | { |
f1762d77 BW |
1025 | char *key = xstrfmt("protocol.%s.allow", type); |
1026 | char *value; | |
1027 | ||
1028 | /* first check the per-protocol config */ | |
1029 | if (!git_config_get_string(key, &value)) { | |
1030 | enum protocol_allow_config ret = | |
1031 | parse_protocol_config(key, value); | |
1032 | free(key); | |
1033 | free(value); | |
1034 | return ret; | |
1035 | } | |
1036 | free(key); | |
1037 | ||
1038 | /* if defined, fallback to user-defined default for unknown protocols */ | |
1039 | if (!git_config_get_string("protocol.allow", &value)) { | |
1040 | enum protocol_allow_config ret = | |
1041 | parse_protocol_config("protocol.allow", value); | |
1042 | free(value); | |
1043 | return ret; | |
1044 | } | |
1045 | ||
1046 | /* fallback to built-in defaults */ | |
1047 | /* known safe */ | |
1048 | if (!strcmp(type, "http") || | |
1049 | !strcmp(type, "https") || | |
1050 | !strcmp(type, "git") || | |
a1d4f67c | 1051 | !strcmp(type, "ssh")) |
f1762d77 BW |
1052 | return PROTOCOL_ALLOW_ALWAYS; |
1053 | ||
1054 | /* known scary; err on the side of caution */ | |
1055 | if (!strcmp(type, "ext")) | |
1056 | return PROTOCOL_ALLOW_NEVER; | |
1057 | ||
1058 | /* unknown; by default let them be used only directly by the user */ | |
1059 | return PROTOCOL_ALLOW_USER_ONLY; | |
5088d3b3 JK |
1060 | } |
1061 | ||
a768a022 | 1062 | int is_transport_allowed(const char *type, int from_user) |
5088d3b3 | 1063 | { |
f5adaa5c DS |
1064 | const struct string_list *allow_list = protocol_allow_list(); |
1065 | if (allow_list) | |
1066 | return string_list_has_string(allow_list, type); | |
f1762d77 BW |
1067 | |
1068 | switch (get_protocol_config(type)) { | |
1069 | case PROTOCOL_ALLOW_ALWAYS: | |
1070 | return 1; | |
1071 | case PROTOCOL_ALLOW_NEVER: | |
1072 | return 0; | |
1073 | case PROTOCOL_ALLOW_USER_ONLY: | |
a768a022 BW |
1074 | if (from_user < 0) |
1075 | from_user = git_env_bool("GIT_PROTOCOL_FROM_USER", 1); | |
1076 | return from_user; | |
f1762d77 BW |
1077 | } |
1078 | ||
033abf97 | 1079 | BUG("invalid protocol_allow_config type"); |
5088d3b3 JK |
1080 | } |
1081 | ||
1082 | void transport_check_allowed(const char *type) | |
1083 | { | |
a768a022 | 1084 | if (!is_transport_allowed(type, -1)) |
68e39e41 | 1085 | die(_("transport '%s' not allowed"), type); |
a5adaced JK |
1086 | } |
1087 | ||
e967ca38 | 1088 | static struct transport_vtable bundle_vtable = { |
1fd88224 ÆAB |
1089 | .get_refs_list = get_refs_from_bundle, |
1090 | .fetch_refs = fetch_refs_from_bundle, | |
1091 | .disconnect = close_bundle | |
e967ca38 JT |
1092 | }; |
1093 | ||
1094 | static struct transport_vtable builtin_smart_vtable = { | |
1fd88224 | 1095 | .get_refs_list = get_refs_via_connect, |
0cfde740 | 1096 | .get_bundle_uri = get_bundle_uri, |
1fd88224 ÆAB |
1097 | .fetch_refs = fetch_refs_via_pack, |
1098 | .push_refs = git_transport_push, | |
1099 | .connect = connect_git, | |
1100 | .disconnect = disconnect_git | |
e967ca38 JT |
1101 | }; |
1102 | ||
e5f4e214 | 1103 | struct transport *transport_get(struct remote *remote, const char *url) |
9b288516 | 1104 | { |
4da50460 | 1105 | const char *helper; |
8eb554ae SP |
1106 | struct transport *ret = xcalloc(1, sizeof(*ret)); |
1107 | ||
d01b3c02 | 1108 | ret->progress = isatty(2); |
bc40dfb1 | 1109 | string_list_init_dup(&ret->pack_lockfiles); |
d01b3c02 | 1110 | |
0cfde740 ÆAB |
1111 | CALLOC_ARRAY(ret->bundles, 1); |
1112 | init_bundle_list(ret->bundles); | |
1113 | ||
c1d45cf7 | 1114 | if (!remote) |
1a07e59c | 1115 | BUG("No remote provided to transport_get()"); |
c1d45cf7 | 1116 | |
b0d66e15 | 1117 | ret->got_remote_refs = 0; |
8eb554ae | 1118 | ret->remote = remote; |
4da50460 | 1119 | helper = remote->foreign_vcs; |
fb0cc87e | 1120 | |
cb21d8f0 | 1121 | if (!url && remote->url) |
fb0cc87e | 1122 | url = remote->url[0]; |
8eb554ae | 1123 | ret->url = url; |
8eb554ae | 1124 | |
87422439 JS |
1125 | /* maybe it is a foreign URL? */ |
1126 | if (url) { | |
1127 | const char *p = url; | |
1128 | ||
638794cd | 1129 | while (is_urlschemechar(p == url, *p)) |
87422439 | 1130 | p++; |
59556548 | 1131 | if (starts_with(p, "::")) |
4da50460 | 1132 | helper = xstrndup(url, p - url); |
87422439 JS |
1133 | } |
1134 | ||
4da50460 IL |
1135 | if (helper) { |
1136 | transport_helper_init(ret, helper); | |
59556548 | 1137 | } else if (starts_with(url, "rsync:")) { |
68e39e41 | 1138 | die(_("git-over-rsync is no longer supported")); |
c59ab2e5 | 1139 | } else if (url_is_local_not_ssh(url) && is_file(url) && is_bundle(url, 1)) { |
c7a8a162 | 1140 | struct bundle_transport_data *data = xcalloc(1, sizeof(*data)); |
10b635b7 | 1141 | bundle_header_init(&data->header); |
a5adaced | 1142 | transport_check_allowed("file"); |
c7a8a162 | 1143 | ret->data = data; |
e967ca38 | 1144 | ret->vtable = &bundle_vtable; |
aa5af974 | 1145 | ret->smart_options = NULL; |
25d5cc48 | 1146 | } else if (!is_url(url) |
59556548 CC |
1147 | || starts_with(url, "file://") |
1148 | || starts_with(url, "git://") | |
1149 | || starts_with(url, "ssh://") | |
07c7782c CMN |
1150 | || starts_with(url, "git+ssh://") /* deprecated - do not use */ |
1151 | || starts_with(url, "ssh+git://") /* deprecated - do not use */ | |
1152 | ) { | |
a5adaced JK |
1153 | /* |
1154 | * These are builtin smart transports; "allowed" transports | |
1155 | * will be checked individually in git_connect. | |
1156 | */ | |
9b288516 | 1157 | struct git_transport_data *data = xcalloc(1, sizeof(*data)); |
2a01bded | 1158 | list_objects_filter_init(&data->options.filter_options); |
9b288516 | 1159 | ret->data = data; |
e967ca38 | 1160 | ret->vtable = &builtin_smart_vtable; |
aa5af974 | 1161 | ret->smart_options = &(data->options); |
824d5776 | 1162 | |
ba227857 | 1163 | data->conn = NULL; |
1b759e0c | 1164 | data->finished_handshake = 0; |
25d5cc48 IL |
1165 | } else { |
1166 | /* Unknown protocol in URL. Pass to external handler. */ | |
1167 | int len = external_specification_len(url); | |
6b33894f | 1168 | char *handler = xmemdupz(url, len); |
25d5cc48 | 1169 | transport_helper_init(ret, handler); |
9b288516 | 1170 | } |
8eb554ae | 1171 | |
aa5af974 IL |
1172 | if (ret->smart_options) { |
1173 | ret->smart_options->thin = 1; | |
1174 | ret->smart_options->uploadpack = "git-upload-pack"; | |
1175 | if (remote->uploadpack) | |
1176 | ret->smart_options->uploadpack = remote->uploadpack; | |
1177 | ret->smart_options->receivepack = "git-receive-pack"; | |
1178 | if (remote->receivepack) | |
1179 | ret->smart_options->receivepack = remote->receivepack; | |
1180 | } | |
1181 | ||
7c97af4d | 1182 | ret->hash_algo = &hash_algos[GIT_HASH_SHA1]; |
1183 | ||
9b288516 DB |
1184 | return ret; |
1185 | } | |
1186 | ||
7c97af4d | 1187 | const struct git_hash_algo *transport_get_hash_algo(struct transport *transport) |
1188 | { | |
1189 | return transport->hash_algo; | |
1190 | } | |
1191 | ||
9b288516 DB |
1192 | int transport_set_option(struct transport *transport, |
1193 | const char *name, const char *value) | |
1194 | { | |
aa5af974 IL |
1195 | int git_reports = 1, protocol_reports = 1; |
1196 | ||
1197 | if (transport->smart_options) | |
1198 | git_reports = set_git_option(transport->smart_options, | |
1199 | name, value); | |
1200 | ||
e967ca38 JT |
1201 | if (transport->vtable->set_option) |
1202 | protocol_reports = transport->vtable->set_option(transport, | |
1203 | name, value); | |
aa5af974 IL |
1204 | |
1205 | /* If either report is 0, report 0 (success). */ | |
1206 | if (!git_reports || !protocol_reports) | |
1207 | return 0; | |
1208 | /* If either reports -1 (invalid value), report -1. */ | |
1209 | if ((git_reports == -1) || (protocol_reports == -1)) | |
1210 | return -1; | |
1211 | /* Otherwise if both report unknown, report unknown. */ | |
ab865e6e | 1212 | return 1; |
9b288516 DB |
1213 | } |
1214 | ||
d01b3c02 TRC |
1215 | void transport_set_verbosity(struct transport *transport, int verbosity, |
1216 | int force_progress) | |
bde873c5 | 1217 | { |
bd2c86ef | 1218 | if (verbosity >= 1) |
bde873c5 TRC |
1219 | transport->verbose = verbosity <= 3 ? verbosity : 3; |
1220 | if (verbosity < 0) | |
1221 | transport->verbose = -1; | |
d01b3c02 TRC |
1222 | |
1223 | /** | |
1224 | * Rules used to determine whether to report progress (processing aborts | |
1225 | * when a rule is satisfied): | |
1226 | * | |
01fdc21f CB |
1227 | * . Report progress, if force_progress is 1 (ie. --progress). |
1228 | * . Don't report progress, if force_progress is 0 (ie. --no-progress). | |
1229 | * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ). | |
1230 | * . Report progress if isatty(2) is 1. | |
d01b3c02 | 1231 | **/ |
01fdc21f CB |
1232 | if (force_progress >= 0) |
1233 | transport->progress = !!force_progress; | |
1234 | else | |
1235 | transport->progress = verbosity >= 0 && isatty(2); | |
bde873c5 TRC |
1236 | } |
1237 | ||
a762e51e HV |
1238 | static void die_with_unpushed_submodules(struct string_list *needs_pushing) |
1239 | { | |
1240 | int i; | |
1241 | ||
7ab1d44f VA |
1242 | fprintf(stderr, _("The following submodule paths contain changes that can\n" |
1243 | "not be found on any remote:\n")); | |
a762e51e | 1244 | for (i = 0; i < needs_pushing->nr; i++) |
5cb5fe4a | 1245 | fprintf(stderr, " %s\n", needs_pushing->items[i].string); |
7ab1d44f VA |
1246 | fprintf(stderr, _("\nPlease try\n\n" |
1247 | " git push --recurse-submodules=on-demand\n\n" | |
1248 | "or cd to the path and use\n\n" | |
1249 | " git push\n\n" | |
1250 | "to push them to a remote.\n\n")); | |
a762e51e HV |
1251 | |
1252 | string_list_clear(needs_pushing, 0); | |
1253 | ||
7ab1d44f | 1254 | die(_("Aborting.")); |
a762e51e HV |
1255 | } |
1256 | ||
ec55559f AS |
1257 | static int run_pre_push_hook(struct transport *transport, |
1258 | struct ref *remote_refs) | |
1259 | { | |
1260 | int ret = 0, x; | |
1261 | struct ref *r; | |
d3180279 | 1262 | struct child_process proc = CHILD_PROCESS_INIT; |
ec55559f | 1263 | struct strbuf buf; |
7f14609e | 1264 | const char *hook_path = find_hook("pre-push"); |
ec55559f | 1265 | |
7f14609e | 1266 | if (!hook_path) |
ec55559f AS |
1267 | return 0; |
1268 | ||
7f14609e ÆAB |
1269 | strvec_push(&proc.args, hook_path); |
1270 | strvec_push(&proc.args, transport->remote->name); | |
1271 | strvec_push(&proc.args, transport->url); | |
ec55559f | 1272 | |
ec55559f | 1273 | proc.in = -1; |
6206286e | 1274 | proc.trace2_hook_name = "pre-push"; |
ec55559f AS |
1275 | |
1276 | if (start_command(&proc)) { | |
1277 | finish_command(&proc); | |
1278 | return -1; | |
1279 | } | |
1280 | ||
af65f68c CB |
1281 | sigchain_push(SIGPIPE, SIG_IGN); |
1282 | ||
ec55559f AS |
1283 | strbuf_init(&buf, 256); |
1284 | ||
1285 | for (r = remote_refs; r; r = r->next) { | |
1286 | if (!r->peer_ref) continue; | |
1287 | if (r->status == REF_STATUS_REJECT_NONFASTFORWARD) continue; | |
631b5ef2 | 1288 | if (r->status == REF_STATUS_REJECT_STALE) continue; |
99a1f9ae | 1289 | if (r->status == REF_STATUS_REJECT_REMOTE_UPDATED) continue; |
ec55559f AS |
1290 | if (r->status == REF_STATUS_UPTODATE) continue; |
1291 | ||
1292 | strbuf_reset(&buf); | |
1293 | strbuf_addf( &buf, "%s %s %s %s\n", | |
f4e54d02 | 1294 | r->peer_ref->name, oid_to_hex(&r->new_oid), |
1295 | r->name, oid_to_hex(&r->old_oid)); | |
ec55559f | 1296 | |
af65f68c CB |
1297 | if (write_in_full(proc.in, buf.buf, buf.len) < 0) { |
1298 | /* We do not mind if a hook does not read all refs. */ | |
1299 | if (errno != EPIPE) | |
1300 | ret = -1; | |
ec55559f AS |
1301 | break; |
1302 | } | |
1303 | } | |
1304 | ||
1305 | strbuf_release(&buf); | |
1306 | ||
1307 | x = close(proc.in); | |
1308 | if (!ret) | |
1309 | ret = x; | |
1310 | ||
af65f68c CB |
1311 | sigchain_pop(SIGPIPE); |
1312 | ||
ec55559f AS |
1313 | x = finish_command(&proc); |
1314 | if (!ret) | |
1315 | ret = x; | |
1316 | ||
1317 | return ret; | |
1318 | } | |
1319 | ||
6c6d5d07 NTND |
1320 | int transport_push(struct repository *r, |
1321 | struct transport *transport, | |
306f22db | 1322 | struct refspec *rs, int flags, |
10643d4e | 1323 | unsigned int *reject_reasons) |
9b288516 | 1324 | { |
35919bf1 FH |
1325 | struct ref *remote_refs = NULL; |
1326 | struct ref *local_refs = NULL; | |
6448182a FH |
1327 | int match_flags = MATCH_REFS_NONE; |
1328 | int verbose = (transport->verbose > 0); | |
1329 | int quiet = (transport->verbose < 0); | |
1330 | int porcelain = flags & TRANSPORT_PUSH_PORCELAIN; | |
1331 | int pretend = flags & TRANSPORT_PUSH_DRY_RUN; | |
35919bf1 FH |
1332 | int push_ret, err; |
1333 | int ret = -1; | |
6448182a FH |
1334 | struct transport_ls_refs_options transport_options = |
1335 | TRANSPORT_LS_REFS_OPTIONS_INIT; | |
1336 | ||
10643d4e | 1337 | *reject_reasons = 0; |
64fcef2d | 1338 | |
960786e7 | 1339 | if (transport_color_config() < 0) |
35919bf1 | 1340 | goto done; |
960786e7 | 1341 | |
6448182a | 1342 | if (!transport->vtable->push_refs) |
35919bf1 | 1343 | goto done; |
ba928c13 | 1344 | |
6448182a | 1345 | local_refs = get_local_heads(); |
5b872fff | 1346 | |
6448182a | 1347 | if (check_push_refs(local_refs, rs) < 0) |
35919bf1 | 1348 | goto done; |
5b872fff | 1349 | |
6448182a | 1350 | refspec_ref_prefixes(rs, &transport_options.ref_prefixes); |
ba928c13 | 1351 | |
6448182a FH |
1352 | trace2_region_enter("transport_push", "get_refs_list", r); |
1353 | remote_refs = transport->vtable->get_refs_list(transport, 1, | |
1354 | &transport_options); | |
1355 | trace2_region_leave("transport_push", "get_refs_list", r); | |
64fcef2d | 1356 | |
6448182a | 1357 | transport_ls_refs_options_release(&transport_options); |
64fcef2d | 1358 | |
6448182a FH |
1359 | if (flags & TRANSPORT_PUSH_ALL) |
1360 | match_flags |= MATCH_REFS_ALL; | |
1361 | if (flags & TRANSPORT_PUSH_MIRROR) | |
1362 | match_flags |= MATCH_REFS_MIRROR; | |
1363 | if (flags & TRANSPORT_PUSH_PRUNE) | |
1364 | match_flags |= MATCH_REFS_PRUNE; | |
1365 | if (flags & TRANSPORT_PUSH_FOLLOW_TAGS) | |
1366 | match_flags |= MATCH_REFS_FOLLOW_TAGS; | |
91048a95 | 1367 | |
6448182a | 1368 | if (match_push_refs(local_refs, &remote_refs, rs, match_flags)) |
35919bf1 | 1369 | goto done; |
20e8b465 | 1370 | |
6448182a FH |
1371 | if (transport->smart_options && |
1372 | transport->smart_options->cas && | |
1373 | !is_empty_cas(transport->smart_options->cas)) | |
1374 | apply_push_cas(transport->smart_options->cas, | |
1375 | transport->remote, remote_refs); | |
ec55559f | 1376 | |
6448182a FH |
1377 | set_ref_status_for_push(remote_refs, |
1378 | flags & TRANSPORT_PUSH_MIRROR, | |
1379 | flags & TRANSPORT_PUSH_FORCE); | |
1380 | ||
1381 | if (!(flags & TRANSPORT_PUSH_NO_HOOK)) | |
1382 | if (run_pre_push_hook(transport, remote_refs)) | |
35919bf1 | 1383 | goto done; |
6448182a FH |
1384 | |
1385 | if ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND | | |
1386 | TRANSPORT_RECURSE_SUBMODULES_ONLY)) && | |
1387 | !is_bare_repository()) { | |
1388 | struct ref *ref = remote_refs; | |
1389 | struct oid_array commits = OID_ARRAY_INIT; | |
1390 | ||
1391 | trace2_region_enter("transport_push", "push_submodules", r); | |
1392 | for (; ref; ref = ref->next) | |
1393 | if (!is_null_oid(&ref->new_oid)) | |
1394 | oid_array_append(&commits, | |
1395 | &ref->new_oid); | |
1396 | ||
1397 | if (!push_unpushed_submodules(r, | |
1398 | &commits, | |
1399 | transport->remote, | |
1400 | rs, | |
1401 | transport->push_options, | |
1402 | pretend)) { | |
910650d2 | 1403 | oid_array_clear(&commits); |
360c7ba3 | 1404 | trace2_region_leave("transport_push", "push_submodules", r); |
6448182a | 1405 | die(_("failed to push all needed submodules")); |
eb21c732 | 1406 | } |
6448182a FH |
1407 | oid_array_clear(&commits); |
1408 | trace2_region_leave("transport_push", "push_submodules", r); | |
1409 | } | |
eb21c732 | 1410 | |
6448182a FH |
1411 | if (((flags & TRANSPORT_RECURSE_SUBMODULES_CHECK) || |
1412 | ((flags & (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND | | |
1413 | TRANSPORT_RECURSE_SUBMODULES_ONLY)) && | |
1414 | !pretend)) && !is_bare_repository()) { | |
1415 | struct ref *ref = remote_refs; | |
1416 | struct string_list needs_pushing = STRING_LIST_INIT_DUP; | |
1417 | struct oid_array commits = OID_ARRAY_INIT; | |
1418 | ||
1419 | trace2_region_enter("transport_push", "check_submodules", r); | |
1420 | for (; ref; ref = ref->next) | |
1421 | if (!is_null_oid(&ref->new_oid)) | |
1422 | oid_array_append(&commits, | |
1423 | &ref->new_oid); | |
1424 | ||
1425 | if (find_unpushed_submodules(r, | |
1426 | &commits, | |
1427 | transport->remote->name, | |
1428 | &needs_pushing)) { | |
910650d2 | 1429 | oid_array_clear(&commits); |
360c7ba3 | 1430 | trace2_region_leave("transport_push", "check_submodules", r); |
6448182a | 1431 | die_with_unpushed_submodules(&needs_pushing); |
d2b17b32 | 1432 | } |
6448182a FH |
1433 | string_list_clear(&needs_pushing, 0); |
1434 | oid_array_clear(&commits); | |
1435 | trace2_region_leave("transport_push", "check_submodules", r); | |
1436 | } | |
d2b17b32 | 1437 | |
6448182a FH |
1438 | if (!(flags & TRANSPORT_RECURSE_SUBMODULES_ONLY)) { |
1439 | trace2_region_enter("transport_push", "push_refs", r); | |
1440 | push_ret = transport->vtable->push_refs(transport, remote_refs, flags); | |
1441 | trace2_region_leave("transport_push", "push_refs", r); | |
1442 | } else | |
1443 | push_ret = 0; | |
1444 | err = push_had_errors(remote_refs); | |
1445 | ret = push_ret | err; | |
1446 | ||
1447 | if (!quiet || err) | |
1448 | transport_print_push_status(transport->url, remote_refs, | |
1449 | verbose | porcelain, porcelain, | |
1450 | reject_reasons); | |
1451 | ||
1452 | if (flags & TRANSPORT_PUSH_SET_UPSTREAM) | |
1453 | set_upstreams(transport, remote_refs, pretend); | |
1454 | ||
1455 | if (!(flags & (TRANSPORT_PUSH_DRY_RUN | | |
1456 | TRANSPORT_RECURSE_SUBMODULES_ONLY))) { | |
1457 | struct ref *ref; | |
1458 | for (ref = remote_refs; ref; ref = ref->next) | |
1459 | transport_update_tracking_ref(transport->remote, ref, verbose); | |
1460 | } | |
64fcef2d | 1461 | |
6448182a FH |
1462 | if (porcelain && !push_ret) |
1463 | puts("Done"); | |
1464 | else if (!quiet && !ret && !transport_refs_pushed(remote_refs)) | |
1465 | fprintf(stderr, "Everything up-to-date\n"); | |
77555854 | 1466 | |
35919bf1 | 1467 | done: |
8c49d704 FH |
1468 | free_refs(local_refs); |
1469 | free_refs(remote_refs); | |
6448182a | 1470 | return ret; |
9b288516 DB |
1471 | } |
1472 | ||
1af8ae1c | 1473 | const struct ref *transport_get_remote_refs(struct transport *transport, |
39835409 | 1474 | struct transport_ls_refs_options *transport_options) |
c29727d5 | 1475 | { |
b0d66e15 | 1476 | if (!transport->got_remote_refs) { |
1af8ae1c BW |
1477 | transport->remote_refs = |
1478 | transport->vtable->get_refs_list(transport, 0, | |
39835409 | 1479 | transport_options); |
b0d66e15 TRC |
1480 | transport->got_remote_refs = 1; |
1481 | } | |
61b075bd | 1482 | |
c29727d5 DB |
1483 | return transport->remote_refs; |
1484 | } | |
1485 | ||
f36d4f83 ÆAB |
1486 | void transport_ls_refs_options_release(struct transport_ls_refs_options *opts) |
1487 | { | |
1488 | strvec_clear(&opts->ref_prefixes); | |
1489 | free((char *)opts->unborn_head_target); | |
1490 | } | |
1491 | ||
e2842b39 | 1492 | int transport_fetch_refs(struct transport *transport, struct ref *refs) |
c29727d5 | 1493 | { |
425b1393 | 1494 | int rc; |
86386829 | 1495 | int nr_heads = 0, nr_alloc = 0, nr_refs = 0; |
37148311 DB |
1496 | struct ref **heads = NULL; |
1497 | struct ref *rm; | |
c29727d5 DB |
1498 | |
1499 | for (rm = refs; rm; rm = rm->next) { | |
86386829 | 1500 | nr_refs++; |
c29727d5 | 1501 | if (rm->peer_ref && |
f4e54d02 | 1502 | !is_null_oid(&rm->old_oid) && |
4a7e27e9 | 1503 | oideq(&rm->peer_ref->old_oid, &rm->old_oid)) |
c29727d5 | 1504 | continue; |
7a2bff45 | 1505 | ALLOC_GROW(heads, nr_heads + 1, nr_alloc); |
425b1393 | 1506 | heads[nr_heads++] = rm; |
c29727d5 DB |
1507 | } |
1508 | ||
86386829 NP |
1509 | if (!nr_heads) { |
1510 | /* | |
1511 | * When deepening of a shallow repository is requested, | |
1512 | * then local and remote refs are likely to still be equal. | |
1513 | * Just feed them all to the fetch method in that case. | |
1514 | * This condition shouldn't be met in a non-deepening fetch | |
09b7e220 | 1515 | * (see builtin/fetch.c:quickfetch()). |
86386829 | 1516 | */ |
b32fa95f | 1517 | ALLOC_ARRAY(heads, nr_refs); |
86386829 NP |
1518 | for (rm = refs; rm; rm = rm->next) |
1519 | heads[nr_heads++] = rm; | |
1520 | } | |
1521 | ||
9b1cdd33 | 1522 | rc = transport->vtable->fetch_refs(transport, nr_heads, heads); |
61b075bd | 1523 | |
c29727d5 | 1524 | free(heads); |
425b1393 | 1525 | return rc; |
c29727d5 DB |
1526 | } |
1527 | ||
0cfde740 ÆAB |
1528 | int transport_get_remote_bundle_uri(struct transport *transport) |
1529 | { | |
7cce9074 | 1530 | int value = 0; |
0cfde740 ÆAB |
1531 | const struct transport_vtable *vtable = transport->vtable; |
1532 | ||
1533 | /* Check config only once. */ | |
1534 | if (transport->got_remote_bundle_uri) | |
1535 | return 0; | |
1536 | transport->got_remote_bundle_uri = 1; | |
1537 | ||
7cce9074 ÆAB |
1538 | /* |
1539 | * Don't request bundle-uri from the server unless configured to | |
1540 | * do so by the transfer.bundleURI=true config option. | |
1541 | */ | |
1542 | if (git_config_get_bool("transfer.bundleuri", &value) || !value) | |
1543 | return 0; | |
1544 | ||
ebc39479 DS |
1545 | if (!transport->bundles->baseURI) |
1546 | transport->bundles->baseURI = xstrdup(transport->url); | |
1547 | ||
0cfde740 ÆAB |
1548 | if (!vtable->get_bundle_uri) |
1549 | return error(_("bundle-uri operation not supported by protocol")); | |
1550 | ||
1551 | if (vtable->get_bundle_uri(transport) < 0) | |
1552 | return error(_("could not retrieve server-advertised bundle-uri list")); | |
1553 | return 0; | |
1554 | } | |
1555 | ||
58d4d7f1 | 1556 | void transport_unlock_pack(struct transport *transport, unsigned int flags) |
1788c39c | 1557 | { |
58d4d7f1 | 1558 | int in_signal_handler = !!(flags & TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER); |
9da69a65 JT |
1559 | int i; |
1560 | ||
1561 | for (i = 0; i < transport->pack_lockfiles.nr; i++) | |
58d4d7f1 PS |
1562 | if (in_signal_handler) |
1563 | unlink(transport->pack_lockfiles.items[i].string); | |
1564 | else | |
1565 | unlink_or_warn(transport->pack_lockfiles.items[i].string); | |
1566 | if (!in_signal_handler) | |
1567 | string_list_clear(&transport->pack_lockfiles, 0); | |
1788c39c SP |
1568 | } |
1569 | ||
b236752a IL |
1570 | int transport_connect(struct transport *transport, const char *name, |
1571 | const char *exec, int fd[2]) | |
1572 | { | |
e967ca38 JT |
1573 | if (transport->vtable->connect) |
1574 | return transport->vtable->connect(transport, name, exec, fd); | |
b236752a | 1575 | else |
68e39e41 | 1576 | die(_("operation not supported by protocol")); |
b236752a IL |
1577 | } |
1578 | ||
9b288516 DB |
1579 | int transport_disconnect(struct transport *transport) |
1580 | { | |
1581 | int ret = 0; | |
e967ca38 JT |
1582 | if (transport->vtable->disconnect) |
1583 | ret = transport->vtable->disconnect(transport); | |
68ffe095 AH |
1584 | if (transport->got_remote_refs) |
1585 | free_refs((void *)transport->remote_refs); | |
0cfde740 ÆAB |
1586 | clear_bundle_list(transport->bundles); |
1587 | free(transport->bundles); | |
9b288516 DB |
1588 | free(transport); |
1589 | return ret; | |
1590 | } | |
47abd85b AE |
1591 | |
1592 | /* | |
a7793a74 | 1593 | * Strip username (and password) from a URL and return |
47abd85b AE |
1594 | * it in a newly allocated string. |
1595 | */ | |
1596 | char *transport_anonymize_url(const char *url) | |
1597 | { | |
21f9d0f6 | 1598 | char *scheme_prefix, *anon_part; |
47abd85b AE |
1599 | size_t anon_len, prefix_len = 0; |
1600 | ||
1601 | anon_part = strchr(url, '@'); | |
c59ab2e5 | 1602 | if (url_is_local_not_ssh(url) || !anon_part) |
47abd85b AE |
1603 | goto literal_copy; |
1604 | ||
1605 | anon_len = strlen(++anon_part); | |
1606 | scheme_prefix = strstr(url, "://"); | |
1607 | if (!scheme_prefix) { | |
1608 | if (!strchr(anon_part, ':')) | |
1609 | /* cannot be "me@there:/path/name" */ | |
1610 | goto literal_copy; | |
1611 | } else { | |
1612 | const char *cp; | |
1613 | /* make sure scheme is reasonable */ | |
1614 | for (cp = url; cp < scheme_prefix; cp++) { | |
1615 | switch (*cp) { | |
1616 | /* RFC 1738 2.1 */ | |
1617 | case '+': case '.': case '-': | |
1618 | break; /* ok */ | |
1619 | default: | |
1620 | if (isalnum(*cp)) | |
1621 | break; | |
1622 | /* it isn't */ | |
1623 | goto literal_copy; | |
1624 | } | |
1625 | } | |
1626 | /* @ past the first slash does not count */ | |
1627 | cp = strchr(scheme_prefix + 3, '/'); | |
1628 | if (cp && cp < anon_part) | |
1629 | goto literal_copy; | |
1630 | prefix_len = scheme_prefix - url + 3; | |
1631 | } | |
21f9d0f6 JK |
1632 | return xstrfmt("%.*s%.*s", (int)prefix_len, url, |
1633 | (int)anon_len, anon_part); | |
47abd85b AE |
1634 | literal_copy: |
1635 | return xstrdup(url); | |
1636 | } |