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