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