1 #include "git-compat-util.h"
4 #include "environment.h"
9 #include "fetch-pack.h"
12 #include "send-pack.h"
19 #include "submodule.h"
20 #include "string-list.h"
21 #include "oid-array.h"
24 #include "transport-internal.h"
26 #include "object-name.h"
28 #include "bundle-uri.h"
30 static int transport_use_color
= -1;
31 static char transport_colors
[][COLOR_MAXLEN
] = {
33 GIT_COLOR_RED
/* REJECTED */
36 enum color_transport
{
37 TRANSPORT_COLOR_RESET
= 0,
38 TRANSPORT_COLOR_REJECTED
= 1
41 static int transport_color_config(void)
43 const char *keys
[] = {
44 "color.transport.reset",
45 "color.transport.rejected"
46 }, *key
= "color.transport";
49 static int initialized
;
55 if (!git_config_get_string(key
, &value
))
56 transport_use_color
= git_config_colorbool(key
, value
);
58 if (!want_color_stderr(transport_use_color
))
61 for (i
= 0; i
< ARRAY_SIZE(keys
); i
++)
62 if (!git_config_get_string(keys
[i
], &value
)) {
64 return config_error_nonbool(keys
[i
]);
65 if (color_parse(value
, transport_colors
[i
]) < 0)
72 static const char *transport_get_color(enum color_transport ix
)
74 if (want_color_stderr(transport_use_color
))
75 return transport_colors
[ix
];
79 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
83 for (ref
= refs
; ref
; ref
= ref
->next
) {
84 const char *localname
;
86 const char *remotename
;
89 * Check suitability for tracking. Must be successful /
90 * already up-to-date ref create/modify (not delete).
92 if (ref
->status
!= REF_STATUS_OK
&&
93 ref
->status
!= REF_STATUS_UPTODATE
)
97 if (is_null_oid(&ref
->new_oid
))
100 /* Follow symbolic refs (mainly for HEAD). */
101 localname
= ref
->peer_ref
->name
;
102 remotename
= ref
->name
;
103 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
105 if (tmp
&& flag
& REF_ISSYMREF
&&
106 starts_with(tmp
, "refs/heads/"))
109 /* Both source and destination must be local branches. */
110 if (!localname
|| !starts_with(localname
, "refs/heads/"))
112 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
116 int flag
= transport
->verbose
< 0 ? 0 : BRANCH_CONFIG_VERBOSE
;
117 install_branch_config(flag
, localname
+ 11,
118 transport
->remote
->name
, remotename
);
119 } else if (transport
->verbose
>= 0)
120 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
121 localname
+ 11, remotename
+ 11,
122 transport
->remote
->name
);
126 struct bundle_transport_data
{
128 struct bundle_header header
;
129 unsigned get_refs_from_bundle_called
: 1;
132 static void get_refs_from_bundle_inner(struct transport
*transport
)
134 struct bundle_transport_data
*data
= transport
->data
;
136 data
->get_refs_from_bundle_called
= 1;
140 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
142 die(_("could not read bundle '%s'"), transport
->url
);
144 transport
->hash_algo
= data
->header
.hash_algo
;
147 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
149 struct transport_ls_refs_options
*transport_options UNUSED
)
151 struct bundle_transport_data
*data
= transport
->data
;
152 struct ref
*result
= NULL
;
158 get_refs_from_bundle_inner(transport
);
160 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
161 struct string_list_item
*e
= data
->header
.references
.items
+ i
;
162 const char *name
= e
->string
;
163 struct ref
*ref
= alloc_ref(name
);
164 struct object_id
*oid
= e
->util
;
165 oidcpy(&ref
->old_oid
, oid
);
172 static int fetch_refs_from_bundle(struct transport
*transport
,
174 struct ref
**to_fetch UNUSED
)
176 struct bundle_transport_data
*data
= transport
->data
;
177 struct strvec extra_index_pack_args
= STRVEC_INIT
;
180 if (transport
->progress
)
181 strvec_push(&extra_index_pack_args
, "-v");
183 if (!data
->get_refs_from_bundle_called
)
184 get_refs_from_bundle_inner(transport
);
185 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
186 &extra_index_pack_args
, 0);
187 transport
->hash_algo
= data
->header
.hash_algo
;
191 static int close_bundle(struct transport
*transport
)
193 struct bundle_transport_data
*data
= transport
->data
;
196 bundle_header_release(&data
->header
);
201 struct git_transport_data
{
202 struct git_transport_options options
;
203 struct child_process
*conn
;
205 unsigned finished_handshake
: 1;
206 enum protocol_version version
;
207 struct oid_array extra_have
;
208 struct oid_array shallow
;
211 static int set_git_option(struct git_transport_options
*opts
,
212 const char *name
, const char *value
)
214 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
215 opts
->uploadpack
= value
;
217 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
218 opts
->receivepack
= value
;
220 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
221 opts
->thin
= !!value
;
223 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
224 opts
->followtags
= !!value
;
226 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
227 opts
->keep
= !!value
;
229 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
230 opts
->update_shallow
= !!value
;
232 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
237 opts
->depth
= strtol(value
, &end
, 0);
239 die(_("transport: invalid depth option '%s'"), value
);
242 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
243 opts
->deepen_since
= value
;
245 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
246 opts
->deepen_not
= (const struct string_list
*)value
;
248 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
249 opts
->deepen_relative
= !!value
;
251 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
252 opts
->from_promisor
= !!value
;
254 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
255 list_objects_filter_die_if_populated(&opts
->filter_options
);
256 parse_list_objects_filter(&opts
->filter_options
, value
);
258 } else if (!strcmp(name
, TRANS_OPT_REFETCH
)) {
259 opts
->refetch
= !!value
;
261 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
262 opts
->reject_shallow
= !!value
;
268 static int connect_setup(struct transport
*transport
, int for_push
)
270 struct git_transport_data
*data
= transport
->data
;
271 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
276 switch (transport
->family
) {
277 case TRANSPORT_FAMILY_ALL
: break;
278 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
279 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
282 data
->conn
= git_connect(data
->fd
, transport
->url
,
287 data
->options
.receivepack
:
288 data
->options
.uploadpack
,
294 static void die_if_server_options(struct transport
*transport
)
296 if (!transport
->server_options
|| !transport
->server_options
->nr
)
298 advise(_("see protocol.version in 'git help config' for more details"));
299 die(_("server options require protocol version 2 or later"));
303 * Obtains the protocol version from the transport and writes it to
304 * transport->data->version, first connecting if not already connected.
306 * If the protocol version is one that allows skipping the listing of remote
307 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
308 * this function returns NULL. Otherwise, this function returns the list of
311 static struct ref
*handshake(struct transport
*transport
, int for_push
,
312 struct transport_ls_refs_options
*options
,
315 struct git_transport_data
*data
= transport
->data
;
316 struct ref
*refs
= NULL
;
317 struct packet_reader reader
;
319 const char *server_sid
;
321 connect_setup(transport
, for_push
);
323 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
324 PACKET_READ_CHOMP_NEWLINE
|
325 PACKET_READ_GENTLE_ON_EOF
|
326 PACKET_READ_DIE_ON_ERR_PACKET
);
328 data
->version
= discover_version(&reader
);
329 switch (data
->version
) {
331 if (server_feature_v2("session-id", &server_sid
))
332 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
334 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
336 transport
->server_options
,
337 transport
->stateless_rpc
);
341 die_if_server_options(transport
);
342 get_remote_heads(&reader
, &refs
,
343 for_push
? REF_NORMAL
: 0,
346 server_sid
= server_feature_value("session-id", &sid_len
);
348 char *sid
= xstrndup(server_sid
, sid_len
);
349 trace2_data_string("transfer", NULL
, "server-sid", sid
);
353 case protocol_unknown_version
:
354 BUG("unknown protocol version");
356 data
->finished_handshake
= 1;
357 transport
->hash_algo
= reader
.hash_algo
;
359 if (reader
.line_peeked
)
360 BUG("buffer must be empty at the end of handshake()");
365 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
366 struct transport_ls_refs_options
*options
)
368 return handshake(transport
, for_push
, options
, 1);
371 static int get_bundle_uri(struct transport
*transport
)
373 struct git_transport_data
*data
= transport
->data
;
374 struct packet_reader reader
;
375 int stateless_rpc
= transport
->stateless_rpc
;
377 if (!transport
->bundles
) {
378 CALLOC_ARRAY(transport
->bundles
, 1);
379 init_bundle_list(transport
->bundles
);
382 if (!data
->finished_handshake
) {
383 struct ref
*refs
= handshake(transport
, 0, NULL
, 0);
390 * "Support" protocol v0 and v2 without bundle-uri support by
391 * silently degrading to a NOOP.
393 if (!server_supports_v2("bundle-uri"))
396 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
397 PACKET_READ_CHOMP_NEWLINE
|
398 PACKET_READ_GENTLE_ON_EOF
);
400 return get_remote_bundle_uri(data
->fd
[1], &reader
,
401 transport
->bundles
, stateless_rpc
);
404 static int fetch_refs_via_pack(struct transport
*transport
,
405 int nr_heads
, struct ref
**to_fetch
)
408 struct git_transport_data
*data
= transport
->data
;
409 struct ref
*refs
= NULL
;
410 struct fetch_pack_args args
;
411 struct ref
*refs_tmp
= NULL
;
413 memset(&args
, 0, sizeof(args
));
414 args
.uploadpack
= data
->options
.uploadpack
;
415 args
.keep_pack
= data
->options
.keep
;
417 args
.use_thin_pack
= data
->options
.thin
;
418 args
.include_tag
= data
->options
.followtags
;
419 args
.verbose
= (transport
->verbose
> 1);
420 args
.quiet
= (transport
->verbose
< 0);
421 args
.no_progress
= !transport
->progress
;
422 args
.depth
= data
->options
.depth
;
423 args
.deepen_since
= data
->options
.deepen_since
;
424 args
.deepen_not
= data
->options
.deepen_not
;
425 args
.deepen_relative
= data
->options
.deepen_relative
;
426 args
.check_self_contained_and_connected
=
427 data
->options
.check_self_contained_and_connected
;
428 args
.cloning
= transport
->cloning
;
429 args
.update_shallow
= data
->options
.update_shallow
;
430 args
.from_promisor
= data
->options
.from_promisor
;
431 list_objects_filter_copy(&args
.filter_options
,
432 &data
->options
.filter_options
);
433 args
.refetch
= data
->options
.refetch
;
434 args
.stateless_rpc
= transport
->stateless_rpc
;
435 args
.server_options
= transport
->server_options
;
436 args
.negotiation_tips
= data
->options
.negotiation_tips
;
437 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
439 if (!data
->finished_handshake
) {
441 int must_list_refs
= 0;
442 for (i
= 0; i
< nr_heads
; i
++) {
443 if (!to_fetch
[i
]->exact_oid
) {
448 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
451 if (data
->version
== protocol_unknown_version
)
452 BUG("unknown protocol version");
453 else if (data
->version
<= protocol_v1
)
454 die_if_server_options(transport
);
456 if (data
->options
.acked_commits
) {
457 if (data
->version
< protocol_v2
) {
458 warning(_("--negotiate-only requires protocol v2"));
460 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
461 warning(_("server does not support wait-for-done"));
464 negotiate_using_fetch(data
->options
.negotiation_tips
,
465 transport
->server_options
,
466 transport
->stateless_rpc
,
468 data
->options
.acked_commits
);
474 refs
= fetch_pack(&args
, data
->fd
,
475 refs_tmp
? refs_tmp
: transport
->remote_refs
,
476 to_fetch
, nr_heads
, &data
->shallow
,
477 &transport
->pack_lockfiles
, data
->version
);
479 data
->finished_handshake
= 0;
480 data
->options
.self_contained_and_connected
=
481 args
.self_contained_and_connected
;
482 data
->options
.connectivity_checked
= args
.connectivity_checked
;
486 if (report_unmatched_refs(to_fetch
, nr_heads
))
491 if (data
->fd
[1] >= 0)
493 if (finish_connect(data
->conn
))
499 list_objects_filter_release(&args
.filter_options
);
503 static int push_had_errors(struct ref
*ref
)
505 for (; ref
; ref
= ref
->next
) {
506 switch (ref
->status
) {
507 case REF_STATUS_NONE
:
508 case REF_STATUS_UPTODATE
:
518 int transport_refs_pushed(struct ref
*ref
)
520 for (; ref
; ref
= ref
->next
) {
521 switch(ref
->status
) {
522 case REF_STATUS_NONE
:
523 case REF_STATUS_UPTODATE
:
532 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
533 struct object_id
*new_oid
, int deletion
,
536 struct refspec_item rs
;
538 memset(&rs
, 0, sizeof(rs
));
542 if (!remote_find_tracking(remote
, &rs
)) {
544 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
546 delete_ref(NULL
, rs
.dst
, NULL
, 0);
548 update_ref("update by push", rs
.dst
, new_oid
,
554 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
557 struct object_id
*new_oid
;
558 struct ref_push_report
*report
;
560 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
563 report
= ref
->report
;
565 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
566 ref
->deletion
, verbose
);
568 for (; report
; report
= report
->next
) {
569 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
570 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
571 update_one_tracking_ref(remote
, refname
, new_oid
,
572 is_null_oid(new_oid
), verbose
);
576 static void print_ref_status(char flag
, const char *summary
,
577 struct ref
*to
, struct ref
*from
, const char *msg
,
578 struct ref_push_report
*report
,
579 int porcelain
, int summary_width
)
583 if (report
&& report
->ref_name
)
584 to_name
= report
->ref_name
;
590 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
592 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
594 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
596 fprintf(stdout
, "%s\n", summary
);
598 const char *red
= "", *reset
= "";
599 if (push_had_errors(to
)) {
600 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
601 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
603 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
606 fprintf(stderr
, "%s -> %s",
607 prettify_refname(from
->name
),
608 prettify_refname(to_name
));
610 fputs(prettify_refname(to_name
), stderr
);
620 static void print_ok_ref_status(struct ref
*ref
,
621 struct ref_push_report
*report
,
622 int porcelain
, int summary_width
)
624 struct object_id
*old_oid
;
625 struct object_id
*new_oid
;
626 const char *ref_name
;
629 if (report
&& report
->old_oid
)
630 old_oid
= report
->old_oid
;
632 old_oid
= &ref
->old_oid
;
633 if (report
&& report
->new_oid
)
634 new_oid
= report
->new_oid
;
636 new_oid
= &ref
->new_oid
;
637 if (report
&& report
->forced_update
)
638 forced_update
= report
->forced_update
;
640 forced_update
= ref
->forced_update
;
641 if (report
&& report
->ref_name
)
642 ref_name
= report
->ref_name
;
644 ref_name
= ref
->name
;
647 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
648 report
, porcelain
, summary_width
);
649 else if (is_null_oid(old_oid
))
650 print_ref_status('*',
651 (starts_with(ref_name
, "refs/tags/")
653 : (starts_with(ref_name
, "refs/heads/")
655 : "[new reference]")),
656 ref
, ref
->peer_ref
, NULL
,
657 report
, porcelain
, summary_width
);
659 struct strbuf quickref
= STRBUF_INIT
;
663 strbuf_add_unique_abbrev(&quickref
, old_oid
,
666 strbuf_addstr(&quickref
, "...");
668 msg
= "forced update";
670 strbuf_addstr(&quickref
, "..");
674 strbuf_add_unique_abbrev(&quickref
, new_oid
,
677 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
678 report
, porcelain
, summary_width
);
679 strbuf_release(&quickref
);
683 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
684 struct ref_push_report
*report
,
685 int porcelain
, int summary_width
)
688 char *url
= transport_anonymize_url(dest
);
689 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
693 switch(ref
->status
) {
694 case REF_STATUS_NONE
:
695 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
696 report
, porcelain
, summary_width
);
698 case REF_STATUS_REJECT_NODELETE
:
699 print_ref_status('!', "[rejected]", ref
, NULL
,
700 "remote does not support deleting refs",
701 report
, porcelain
, summary_width
);
703 case REF_STATUS_UPTODATE
:
704 print_ref_status('=', "[up to date]", ref
,
706 report
, porcelain
, summary_width
);
708 case REF_STATUS_REJECT_NONFASTFORWARD
:
709 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
711 report
, porcelain
, summary_width
);
713 case REF_STATUS_REJECT_ALREADY_EXISTS
:
714 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
716 report
, porcelain
, summary_width
);
718 case REF_STATUS_REJECT_FETCH_FIRST
:
719 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
721 report
, porcelain
, summary_width
);
723 case REF_STATUS_REJECT_NEEDS_FORCE
:
724 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
726 report
, porcelain
, summary_width
);
728 case REF_STATUS_REJECT_STALE
:
729 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
731 report
, porcelain
, summary_width
);
733 case REF_STATUS_REJECT_REMOTE_UPDATED
:
734 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
735 "remote ref updated since checkout",
736 report
, porcelain
, summary_width
);
738 case REF_STATUS_REJECT_SHALLOW
:
739 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
740 "new shallow roots not allowed",
741 report
, porcelain
, summary_width
);
743 case REF_STATUS_REMOTE_REJECT
:
744 print_ref_status('!', "[remote rejected]", ref
,
745 ref
->deletion
? NULL
: ref
->peer_ref
,
747 report
, porcelain
, summary_width
);
749 case REF_STATUS_EXPECTING_REPORT
:
750 print_ref_status('!', "[remote failure]", ref
,
751 ref
->deletion
? NULL
: ref
->peer_ref
,
752 "remote failed to report status",
753 report
, porcelain
, summary_width
);
755 case REF_STATUS_ATOMIC_PUSH_FAILED
:
756 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
757 "atomic push failed",
758 report
, porcelain
, summary_width
);
761 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
768 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
769 int porcelain
, int summary_width
)
771 struct ref_push_report
*report
;
775 return print_one_push_report(ref
, dest
, count
,
776 NULL
, porcelain
, summary_width
);
778 for (report
= ref
->report
; report
; report
= report
->next
)
779 print_one_push_report(ref
, dest
, count
+ n
++,
780 report
, porcelain
, summary_width
);
784 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
786 char hex
[GIT_MAX_HEXSZ
+ 1];
787 int w
= repo_find_unique_abbrev_r(the_repository
, hex
, oid
,
790 return (w
< sofar
) ? sofar
: w
;
793 int transport_summary_width(const struct ref
*refs
)
797 for (; refs
; refs
= refs
->next
) {
798 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
799 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
802 maxw
= FALLBACK_DEFAULT_ABBREV
;
803 return (2 * maxw
+ 3);
806 void transport_print_push_status(const char *dest
, struct ref
*refs
,
807 int verbose
, int porcelain
, unsigned int *reject_reasons
)
812 int summary_width
= transport_summary_width(refs
);
814 if (transport_color_config() < 0)
815 warning(_("could not parse transport.color.* config"));
817 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
820 for (ref
= refs
; ref
; ref
= ref
->next
)
821 if (ref
->status
== REF_STATUS_UPTODATE
)
822 n
+= print_one_push_status(ref
, dest
, n
,
823 porcelain
, summary_width
);
826 for (ref
= refs
; ref
; ref
= ref
->next
)
827 if (ref
->status
== REF_STATUS_OK
)
828 n
+= print_one_push_status(ref
, dest
, n
,
829 porcelain
, summary_width
);
832 for (ref
= refs
; ref
; ref
= ref
->next
) {
833 if (ref
->status
!= REF_STATUS_NONE
&&
834 ref
->status
!= REF_STATUS_UPTODATE
&&
835 ref
->status
!= REF_STATUS_OK
)
836 n
+= print_one_push_status(ref
, dest
, n
,
837 porcelain
, summary_width
);
838 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
839 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
840 *reject_reasons
|= REJECT_NON_FF_HEAD
;
842 *reject_reasons
|= REJECT_NON_FF_OTHER
;
843 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
844 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
845 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
846 *reject_reasons
|= REJECT_FETCH_FIRST
;
847 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
848 *reject_reasons
|= REJECT_NEEDS_FORCE
;
849 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
850 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
856 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
858 struct git_transport_data
*data
= transport
->data
;
859 struct send_pack_args args
;
862 if (transport_color_config() < 0)
865 if (!data
->finished_handshake
)
866 get_refs_via_connect(transport
, 1, NULL
);
868 memset(&args
, 0, sizeof(args
));
869 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
870 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
871 args
.use_thin_pack
= data
->options
.thin
;
872 args
.verbose
= (transport
->verbose
> 0);
873 args
.quiet
= (transport
->verbose
< 0);
874 args
.progress
= transport
->progress
;
875 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
876 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
877 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
878 args
.push_options
= transport
->push_options
;
879 args
.url
= transport
->url
;
881 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
882 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
883 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
884 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
886 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
888 switch (data
->version
) {
890 die(_("support for protocol v2 not implemented yet"));
894 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
897 case protocol_unknown_version
:
898 BUG("unknown protocol version");
904 * Atomic push may abort the connection early and close the pipe,
905 * which may cause an error for `finish_connect()`. Ignore this error
906 * for atomic git-push.
908 if (ret
|| args
.atomic
)
909 finish_connect(data
->conn
);
911 ret
= finish_connect(data
->conn
);
913 data
->finished_handshake
= 0;
918 static int connect_git(struct transport
*transport
, const char *name
,
919 const char *executable
, int fd
[2])
921 struct git_transport_data
*data
= transport
->data
;
922 data
->conn
= git_connect(data
->fd
, transport
->url
,
923 name
, executable
, 0);
929 static int disconnect_git(struct transport
*transport
)
931 struct git_transport_data
*data
= transport
->data
;
933 if (data
->finished_handshake
&& !transport
->stateless_rpc
)
934 packet_flush(data
->fd
[1]);
936 if (data
->fd
[1] >= 0)
938 finish_connect(data
->conn
);
941 list_objects_filter_release(&data
->options
.filter_options
);
946 static struct transport_vtable taken_over_vtable
= {
947 .get_refs_list
= get_refs_via_connect
,
948 .get_bundle_uri
= get_bundle_uri
,
949 .fetch_refs
= fetch_refs_via_pack
,
950 .push_refs
= git_transport_push
,
951 .disconnect
= disconnect_git
954 void transport_take_over(struct transport
*transport
,
955 struct child_process
*child
)
957 struct git_transport_data
*data
;
959 if (!transport
->smart_options
)
960 BUG("taking over transport requires non-NULL "
961 "smart_options field.");
963 CALLOC_ARRAY(data
, 1);
964 data
->options
= *transport
->smart_options
;
966 data
->fd
[0] = data
->conn
->out
;
967 data
->fd
[1] = data
->conn
->in
;
968 data
->finished_handshake
= 0;
969 transport
->data
= data
;
971 transport
->vtable
= &taken_over_vtable
;
972 transport
->smart_options
= &(data
->options
);
974 transport
->cannot_reuse
= 1;
977 static int is_file(const char *url
)
982 return S_ISREG(buf
.st_mode
);
985 static int external_specification_len(const char *url
)
987 return strchr(url
, ':') - url
;
990 static const struct string_list
*protocol_allow_list(void)
992 static int enabled
= -1;
993 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
996 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
998 string_list_split(&allowed
, v
, ':', -1);
999 string_list_sort(&allowed
);
1006 return enabled
? &allowed
: NULL
;
1009 enum protocol_allow_config
{
1010 PROTOCOL_ALLOW_NEVER
= 0,
1011 PROTOCOL_ALLOW_USER_ONLY
,
1012 PROTOCOL_ALLOW_ALWAYS
1015 static enum protocol_allow_config
parse_protocol_config(const char *key
,
1018 if (!strcasecmp(value
, "always"))
1019 return PROTOCOL_ALLOW_ALWAYS
;
1020 else if (!strcasecmp(value
, "never"))
1021 return PROTOCOL_ALLOW_NEVER
;
1022 else if (!strcasecmp(value
, "user"))
1023 return PROTOCOL_ALLOW_USER_ONLY
;
1025 die(_("unknown value for config '%s': %s"), key
, value
);
1028 static enum protocol_allow_config
get_protocol_config(const char *type
)
1030 char *key
= xstrfmt("protocol.%s.allow", type
);
1033 /* first check the per-protocol config */
1034 if (!git_config_get_string(key
, &value
)) {
1035 enum protocol_allow_config ret
=
1036 parse_protocol_config(key
, value
);
1043 /* if defined, fallback to user-defined default for unknown protocols */
1044 if (!git_config_get_string("protocol.allow", &value
)) {
1045 enum protocol_allow_config ret
=
1046 parse_protocol_config("protocol.allow", value
);
1051 /* fallback to built-in defaults */
1053 if (!strcmp(type
, "http") ||
1054 !strcmp(type
, "https") ||
1055 !strcmp(type
, "git") ||
1056 !strcmp(type
, "ssh"))
1057 return PROTOCOL_ALLOW_ALWAYS
;
1059 /* known scary; err on the side of caution */
1060 if (!strcmp(type
, "ext"))
1061 return PROTOCOL_ALLOW_NEVER
;
1063 /* unknown; by default let them be used only directly by the user */
1064 return PROTOCOL_ALLOW_USER_ONLY
;
1067 int is_transport_allowed(const char *type
, int from_user
)
1069 const struct string_list
*allow_list
= protocol_allow_list();
1071 return string_list_has_string(allow_list
, type
);
1073 switch (get_protocol_config(type
)) {
1074 case PROTOCOL_ALLOW_ALWAYS
:
1076 case PROTOCOL_ALLOW_NEVER
:
1078 case PROTOCOL_ALLOW_USER_ONLY
:
1080 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1084 BUG("invalid protocol_allow_config type");
1087 void transport_check_allowed(const char *type
)
1089 if (!is_transport_allowed(type
, -1))
1090 die(_("transport '%s' not allowed"), type
);
1093 static struct transport_vtable bundle_vtable
= {
1094 .get_refs_list
= get_refs_from_bundle
,
1095 .fetch_refs
= fetch_refs_from_bundle
,
1096 .disconnect
= close_bundle
1099 static struct transport_vtable builtin_smart_vtable
= {
1100 .get_refs_list
= get_refs_via_connect
,
1101 .get_bundle_uri
= get_bundle_uri
,
1102 .fetch_refs
= fetch_refs_via_pack
,
1103 .push_refs
= git_transport_push
,
1104 .connect
= connect_git
,
1105 .disconnect
= disconnect_git
1108 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1111 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1113 ret
->progress
= isatty(2);
1114 string_list_init_dup(&ret
->pack_lockfiles
);
1116 CALLOC_ARRAY(ret
->bundles
, 1);
1117 init_bundle_list(ret
->bundles
);
1120 BUG("No remote provided to transport_get()");
1122 ret
->got_remote_refs
= 0;
1123 ret
->remote
= remote
;
1124 helper
= remote
->foreign_vcs
;
1126 if (!url
&& remote
->url
)
1127 url
= remote
->url
[0];
1130 /* maybe it is a foreign URL? */
1132 const char *p
= url
;
1134 while (is_urlschemechar(p
== url
, *p
))
1136 if (starts_with(p
, "::"))
1137 helper
= xstrndup(url
, p
- url
);
1141 transport_helper_init(ret
, helper
);
1142 } else if (starts_with(url
, "rsync:")) {
1143 die(_("git-over-rsync is no longer supported"));
1144 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1145 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1146 bundle_header_init(&data
->header
);
1147 transport_check_allowed("file");
1149 ret
->vtable
= &bundle_vtable
;
1150 ret
->smart_options
= NULL
;
1151 } else if (!is_url(url
)
1152 || starts_with(url
, "file://")
1153 || starts_with(url
, "git://")
1154 || starts_with(url
, "ssh://")
1155 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1156 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1159 * These are builtin smart transports; "allowed" transports
1160 * will be checked individually in git_connect.
1162 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1163 list_objects_filter_init(&data
->options
.filter_options
);
1165 ret
->vtable
= &builtin_smart_vtable
;
1166 ret
->smart_options
= &(data
->options
);
1169 data
->finished_handshake
= 0;
1171 /* Unknown protocol in URL. Pass to external handler. */
1172 int len
= external_specification_len(url
);
1173 char *handler
= xmemdupz(url
, len
);
1174 transport_helper_init(ret
, handler
);
1177 if (ret
->smart_options
) {
1178 ret
->smart_options
->thin
= 1;
1179 ret
->smart_options
->uploadpack
= "git-upload-pack";
1180 if (remote
->uploadpack
)
1181 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1182 ret
->smart_options
->receivepack
= "git-receive-pack";
1183 if (remote
->receivepack
)
1184 ret
->smart_options
->receivepack
= remote
->receivepack
;
1187 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1192 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1194 return transport
->hash_algo
;
1197 int transport_set_option(struct transport
*transport
,
1198 const char *name
, const char *value
)
1200 int git_reports
= 1, protocol_reports
= 1;
1202 if (transport
->smart_options
)
1203 git_reports
= set_git_option(transport
->smart_options
,
1206 if (transport
->vtable
->set_option
)
1207 protocol_reports
= transport
->vtable
->set_option(transport
,
1210 /* If either report is 0, report 0 (success). */
1211 if (!git_reports
|| !protocol_reports
)
1213 /* If either reports -1 (invalid value), report -1. */
1214 if ((git_reports
== -1) || (protocol_reports
== -1))
1216 /* Otherwise if both report unknown, report unknown. */
1220 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1224 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1226 transport
->verbose
= -1;
1229 * Rules used to determine whether to report progress (processing aborts
1230 * when a rule is satisfied):
1232 * . Report progress, if force_progress is 1 (ie. --progress).
1233 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1234 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1235 * . Report progress if isatty(2) is 1.
1237 if (force_progress
>= 0)
1238 transport
->progress
= !!force_progress
;
1240 transport
->progress
= verbosity
>= 0 && isatty(2);
1243 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1247 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1248 "not be found on any remote:\n"));
1249 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1250 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1251 fprintf(stderr
, _("\nPlease try\n\n"
1252 " git push --recurse-submodules=on-demand\n\n"
1253 "or cd to the path and use\n\n"
1255 "to push them to a remote.\n\n"));
1257 string_list_clear(needs_pushing
, 0);
1259 die(_("Aborting."));
1262 static int run_pre_push_hook(struct transport
*transport
,
1263 struct ref
*remote_refs
)
1267 struct child_process proc
= CHILD_PROCESS_INIT
;
1269 const char *hook_path
= find_hook("pre-push");
1274 strvec_push(&proc
.args
, hook_path
);
1275 strvec_push(&proc
.args
, transport
->remote
->name
);
1276 strvec_push(&proc
.args
, transport
->url
);
1279 proc
.trace2_hook_name
= "pre-push";
1281 if (start_command(&proc
)) {
1282 finish_command(&proc
);
1286 sigchain_push(SIGPIPE
, SIG_IGN
);
1288 strbuf_init(&buf
, 256);
1290 for (r
= remote_refs
; r
; r
= r
->next
) {
1291 if (!r
->peer_ref
) continue;
1292 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1293 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1294 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1295 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1298 strbuf_addf( &buf
, "%s %s %s %s\n",
1299 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1300 r
->name
, oid_to_hex(&r
->old_oid
));
1302 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1303 /* We do not mind if a hook does not read all refs. */
1310 strbuf_release(&buf
);
1316 sigchain_pop(SIGPIPE
);
1318 x
= finish_command(&proc
);
1325 int transport_push(struct repository
*r
,
1326 struct transport
*transport
,
1327 struct refspec
*rs
, int flags
,
1328 unsigned int *reject_reasons
)
1330 struct ref
*remote_refs
= NULL
;
1331 struct ref
*local_refs
= NULL
;
1332 int match_flags
= MATCH_REFS_NONE
;
1333 int verbose
= (transport
->verbose
> 0);
1334 int quiet
= (transport
->verbose
< 0);
1335 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1336 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1339 struct transport_ls_refs_options transport_options
=
1340 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1342 *reject_reasons
= 0;
1344 if (transport_color_config() < 0)
1347 if (!transport
->vtable
->push_refs
)
1350 local_refs
= get_local_heads();
1352 if (check_push_refs(local_refs
, rs
) < 0)
1355 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1357 trace2_region_enter("transport_push", "get_refs_list", r
);
1358 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1359 &transport_options
);
1360 trace2_region_leave("transport_push", "get_refs_list", r
);
1362 transport_ls_refs_options_release(&transport_options
);
1364 if (flags
& TRANSPORT_PUSH_ALL
)
1365 match_flags
|= MATCH_REFS_ALL
;
1366 if (flags
& TRANSPORT_PUSH_MIRROR
)
1367 match_flags
|= MATCH_REFS_MIRROR
;
1368 if (flags
& TRANSPORT_PUSH_PRUNE
)
1369 match_flags
|= MATCH_REFS_PRUNE
;
1370 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1371 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1373 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1376 if (transport
->smart_options
&&
1377 transport
->smart_options
->cas
&&
1378 !is_empty_cas(transport
->smart_options
->cas
))
1379 apply_push_cas(transport
->smart_options
->cas
,
1380 transport
->remote
, remote_refs
);
1382 set_ref_status_for_push(remote_refs
,
1383 flags
& TRANSPORT_PUSH_MIRROR
,
1384 flags
& TRANSPORT_PUSH_FORCE
);
1386 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1387 if (run_pre_push_hook(transport
, remote_refs
))
1390 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1391 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1392 !is_bare_repository()) {
1393 struct ref
*ref
= remote_refs
;
1394 struct oid_array commits
= OID_ARRAY_INIT
;
1396 trace2_region_enter("transport_push", "push_submodules", r
);
1397 for (; ref
; ref
= ref
->next
)
1398 if (!is_null_oid(&ref
->new_oid
))
1399 oid_array_append(&commits
,
1402 if (!push_unpushed_submodules(r
,
1406 transport
->push_options
,
1408 oid_array_clear(&commits
);
1409 trace2_region_leave("transport_push", "push_submodules", r
);
1410 die(_("failed to push all needed submodules"));
1412 oid_array_clear(&commits
);
1413 trace2_region_leave("transport_push", "push_submodules", r
);
1416 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1417 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1418 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1419 !pretend
)) && !is_bare_repository()) {
1420 struct ref
*ref
= remote_refs
;
1421 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1422 struct oid_array commits
= OID_ARRAY_INIT
;
1424 trace2_region_enter("transport_push", "check_submodules", r
);
1425 for (; ref
; ref
= ref
->next
)
1426 if (!is_null_oid(&ref
->new_oid
))
1427 oid_array_append(&commits
,
1430 if (find_unpushed_submodules(r
,
1432 transport
->remote
->name
,
1434 oid_array_clear(&commits
);
1435 trace2_region_leave("transport_push", "check_submodules", r
);
1436 die_with_unpushed_submodules(&needs_pushing
);
1438 string_list_clear(&needs_pushing
, 0);
1439 oid_array_clear(&commits
);
1440 trace2_region_leave("transport_push", "check_submodules", r
);
1443 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1444 trace2_region_enter("transport_push", "push_refs", r
);
1445 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1446 trace2_region_leave("transport_push", "push_refs", r
);
1449 err
= push_had_errors(remote_refs
);
1450 ret
= push_ret
| err
;
1453 transport_print_push_status(transport
->url
, remote_refs
,
1454 verbose
| porcelain
, porcelain
,
1457 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1458 set_upstreams(transport
, remote_refs
, pretend
);
1460 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1461 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1463 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1464 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1467 if (porcelain
&& !push_ret
)
1469 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1470 /* stable plumbing output; do not modify or localize */
1471 fprintf(stderr
, "Everything up-to-date\n");
1474 free_refs(local_refs
);
1475 free_refs(remote_refs
);
1479 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1480 struct transport_ls_refs_options
*transport_options
)
1482 if (!transport
->got_remote_refs
) {
1483 transport
->remote_refs
=
1484 transport
->vtable
->get_refs_list(transport
, 0,
1486 transport
->got_remote_refs
= 1;
1489 return transport
->remote_refs
;
1492 void transport_ls_refs_options_release(struct transport_ls_refs_options
*opts
)
1494 strvec_clear(&opts
->ref_prefixes
);
1495 free((char *)opts
->unborn_head_target
);
1498 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1501 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1502 struct ref
**heads
= NULL
;
1505 for (rm
= refs
; rm
; rm
= rm
->next
) {
1508 !is_null_oid(&rm
->old_oid
) &&
1509 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1511 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1512 heads
[nr_heads
++] = rm
;
1517 * When deepening of a shallow repository is requested,
1518 * then local and remote refs are likely to still be equal.
1519 * Just feed them all to the fetch method in that case.
1520 * This condition shouldn't be met in a non-deepening fetch
1521 * (see builtin/fetch.c:quickfetch()).
1523 ALLOC_ARRAY(heads
, nr_refs
);
1524 for (rm
= refs
; rm
; rm
= rm
->next
)
1525 heads
[nr_heads
++] = rm
;
1528 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1534 int transport_get_remote_bundle_uri(struct transport
*transport
)
1537 const struct transport_vtable
*vtable
= transport
->vtable
;
1539 /* Check config only once. */
1540 if (transport
->got_remote_bundle_uri
)
1542 transport
->got_remote_bundle_uri
= 1;
1545 * Don't request bundle-uri from the server unless configured to
1546 * do so by the transfer.bundleURI=true config option.
1548 if (git_config_get_bool("transfer.bundleuri", &value
) || !value
)
1551 if (!transport
->bundles
->baseURI
)
1552 transport
->bundles
->baseURI
= xstrdup(transport
->url
);
1554 if (!vtable
->get_bundle_uri
)
1555 return error(_("bundle-uri operation not supported by protocol"));
1557 if (vtable
->get_bundle_uri(transport
) < 0)
1558 return error(_("could not retrieve server-advertised bundle-uri list"));
1562 void transport_unlock_pack(struct transport
*transport
, unsigned int flags
)
1564 int in_signal_handler
= !!(flags
& TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER
);
1567 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1568 if (in_signal_handler
)
1569 unlink(transport
->pack_lockfiles
.items
[i
].string
);
1571 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1572 if (!in_signal_handler
)
1573 string_list_clear(&transport
->pack_lockfiles
, 0);
1576 int transport_connect(struct transport
*transport
, const char *name
,
1577 const char *exec
, int fd
[2])
1579 if (transport
->vtable
->connect
)
1580 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1582 die(_("operation not supported by protocol"));
1585 int transport_disconnect(struct transport
*transport
)
1588 if (transport
->vtable
->disconnect
)
1589 ret
= transport
->vtable
->disconnect(transport
);
1590 if (transport
->got_remote_refs
)
1591 free_refs((void *)transport
->remote_refs
);
1592 clear_bundle_list(transport
->bundles
);
1593 free(transport
->bundles
);
1599 * Strip username (and password) from a URL and return
1600 * it in a newly allocated string.
1602 char *transport_anonymize_url(const char *url
)
1604 char *scheme_prefix
, *anon_part
;
1605 size_t anon_len
, prefix_len
= 0;
1607 anon_part
= strchr(url
, '@');
1608 if (url_is_local_not_ssh(url
) || !anon_part
)
1611 anon_len
= strlen(++anon_part
);
1612 scheme_prefix
= strstr(url
, "://");
1613 if (!scheme_prefix
) {
1614 if (!strchr(anon_part
, ':'))
1615 /* cannot be "me@there:/path/name" */
1619 /* make sure scheme is reasonable */
1620 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1623 case '+': case '.': case '-':
1632 /* @ past the first slash does not count */
1633 cp
= strchr(scheme_prefix
+ 3, '/');
1634 if (cp
&& cp
< anon_part
)
1636 prefix_len
= scheme_prefix
- url
+ 3;
1638 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1639 (int)anon_len
, anon_part
);
1641 return xstrdup(url
);