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