1 #include "git-compat-util.h"
8 #include "fetch-pack.h"
11 #include "send-pack.h"
20 #include "submodule.h"
21 #include "string-list.h"
22 #include "oid-array.h"
24 #include "transport-internal.h"
26 #include "object-store.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
,
173 int nr_heads
, struct ref
**to_fetch
)
175 struct bundle_transport_data
*data
= transport
->data
;
176 struct strvec extra_index_pack_args
= STRVEC_INIT
;
179 if (transport
->progress
)
180 strvec_push(&extra_index_pack_args
, "-v");
182 if (!data
->get_refs_from_bundle_called
)
183 get_refs_from_bundle_inner(transport
);
184 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
185 &extra_index_pack_args
, 0);
186 transport
->hash_algo
= data
->header
.hash_algo
;
190 static int close_bundle(struct transport
*transport
)
192 struct bundle_transport_data
*data
= transport
->data
;
195 bundle_header_release(&data
->header
);
200 struct git_transport_data
{
201 struct git_transport_options options
;
202 struct child_process
*conn
;
204 unsigned finished_handshake
: 1;
205 enum protocol_version version
;
206 struct oid_array extra_have
;
207 struct oid_array shallow
;
210 static int set_git_option(struct git_transport_options
*opts
,
211 const char *name
, const char *value
)
213 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
214 opts
->uploadpack
= value
;
216 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
217 opts
->receivepack
= value
;
219 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
220 opts
->thin
= !!value
;
222 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
223 opts
->followtags
= !!value
;
225 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
226 opts
->keep
= !!value
;
228 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
229 opts
->update_shallow
= !!value
;
231 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
236 opts
->depth
= strtol(value
, &end
, 0);
238 die(_("transport: invalid depth option '%s'"), value
);
241 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
242 opts
->deepen_since
= value
;
244 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
245 opts
->deepen_not
= (const struct string_list
*)value
;
247 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
248 opts
->deepen_relative
= !!value
;
250 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
251 opts
->from_promisor
= !!value
;
253 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
254 list_objects_filter_die_if_populated(&opts
->filter_options
);
255 parse_list_objects_filter(&opts
->filter_options
, value
);
257 } else if (!strcmp(name
, TRANS_OPT_REFETCH
)) {
258 opts
->refetch
= !!value
;
260 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
261 opts
->reject_shallow
= !!value
;
267 static int connect_setup(struct transport
*transport
, int for_push
)
269 struct git_transport_data
*data
= transport
->data
;
270 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
275 switch (transport
->family
) {
276 case TRANSPORT_FAMILY_ALL
: break;
277 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
278 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
281 data
->conn
= git_connect(data
->fd
, transport
->url
,
282 for_push
? data
->options
.receivepack
:
283 data
->options
.uploadpack
,
289 static void die_if_server_options(struct transport
*transport
)
291 if (!transport
->server_options
|| !transport
->server_options
->nr
)
293 advise(_("see protocol.version in 'git help config' for more details"));
294 die(_("server options require protocol version 2 or later"));
298 * Obtains the protocol version from the transport and writes it to
299 * transport->data->version, first connecting if not already connected.
301 * If the protocol version is one that allows skipping the listing of remote
302 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
303 * this function returns NULL. Otherwise, this function returns the list of
306 static struct ref
*handshake(struct transport
*transport
, int for_push
,
307 struct transport_ls_refs_options
*options
,
310 struct git_transport_data
*data
= transport
->data
;
311 struct ref
*refs
= NULL
;
312 struct packet_reader reader
;
314 const char *server_sid
;
316 connect_setup(transport
, for_push
);
318 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
319 PACKET_READ_CHOMP_NEWLINE
|
320 PACKET_READ_GENTLE_ON_EOF
|
321 PACKET_READ_DIE_ON_ERR_PACKET
);
323 data
->version
= discover_version(&reader
);
324 switch (data
->version
) {
326 if (server_feature_v2("session-id", &server_sid
))
327 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
329 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
331 transport
->server_options
,
332 transport
->stateless_rpc
);
336 die_if_server_options(transport
);
337 get_remote_heads(&reader
, &refs
,
338 for_push
? REF_NORMAL
: 0,
341 server_sid
= server_feature_value("session-id", &sid_len
);
343 char *sid
= xstrndup(server_sid
, sid_len
);
344 trace2_data_string("transfer", NULL
, "server-sid", sid
);
348 case protocol_unknown_version
:
349 BUG("unknown protocol version");
351 data
->finished_handshake
= 1;
352 transport
->hash_algo
= reader
.hash_algo
;
354 if (reader
.line_peeked
)
355 BUG("buffer must be empty at the end of handshake()");
360 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
361 struct transport_ls_refs_options
*options
)
363 return handshake(transport
, for_push
, options
, 1);
366 static int get_bundle_uri(struct transport
*transport
)
368 struct git_transport_data
*data
= transport
->data
;
369 struct packet_reader reader
;
370 int stateless_rpc
= transport
->stateless_rpc
;
372 if (!transport
->bundles
) {
373 CALLOC_ARRAY(transport
->bundles
, 1);
374 init_bundle_list(transport
->bundles
);
377 if (!data
->finished_handshake
) {
378 struct ref
*refs
= handshake(transport
, 0, NULL
, 0);
385 * "Support" protocol v0 and v2 without bundle-uri support by
386 * silently degrading to a NOOP.
388 if (!server_supports_v2("bundle-uri"))
391 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
392 PACKET_READ_CHOMP_NEWLINE
|
393 PACKET_READ_GENTLE_ON_EOF
);
395 return get_remote_bundle_uri(data
->fd
[1], &reader
,
396 transport
->bundles
, stateless_rpc
);
399 static int fetch_refs_via_pack(struct transport
*transport
,
400 int nr_heads
, struct ref
**to_fetch
)
403 struct git_transport_data
*data
= transport
->data
;
404 struct ref
*refs
= NULL
;
405 struct fetch_pack_args args
;
406 struct ref
*refs_tmp
= NULL
;
408 memset(&args
, 0, sizeof(args
));
409 args
.uploadpack
= data
->options
.uploadpack
;
410 args
.keep_pack
= data
->options
.keep
;
412 args
.use_thin_pack
= data
->options
.thin
;
413 args
.include_tag
= data
->options
.followtags
;
414 args
.verbose
= (transport
->verbose
> 1);
415 args
.quiet
= (transport
->verbose
< 0);
416 args
.no_progress
= !transport
->progress
;
417 args
.depth
= data
->options
.depth
;
418 args
.deepen_since
= data
->options
.deepen_since
;
419 args
.deepen_not
= data
->options
.deepen_not
;
420 args
.deepen_relative
= data
->options
.deepen_relative
;
421 args
.check_self_contained_and_connected
=
422 data
->options
.check_self_contained_and_connected
;
423 args
.cloning
= transport
->cloning
;
424 args
.update_shallow
= data
->options
.update_shallow
;
425 args
.from_promisor
= data
->options
.from_promisor
;
426 list_objects_filter_copy(&args
.filter_options
,
427 &data
->options
.filter_options
);
428 args
.refetch
= data
->options
.refetch
;
429 args
.stateless_rpc
= transport
->stateless_rpc
;
430 args
.server_options
= transport
->server_options
;
431 args
.negotiation_tips
= data
->options
.negotiation_tips
;
432 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
434 if (!data
->finished_handshake
) {
436 int must_list_refs
= 0;
437 for (i
= 0; i
< nr_heads
; i
++) {
438 if (!to_fetch
[i
]->exact_oid
) {
443 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
446 if (data
->version
== protocol_unknown_version
)
447 BUG("unknown protocol version");
448 else if (data
->version
<= protocol_v1
)
449 die_if_server_options(transport
);
451 if (data
->options
.acked_commits
) {
452 if (data
->version
< protocol_v2
) {
453 warning(_("--negotiate-only requires protocol v2"));
455 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
456 warning(_("server does not support wait-for-done"));
459 negotiate_using_fetch(data
->options
.negotiation_tips
,
460 transport
->server_options
,
461 transport
->stateless_rpc
,
463 data
->options
.acked_commits
);
469 refs
= fetch_pack(&args
, data
->fd
,
470 refs_tmp
? refs_tmp
: transport
->remote_refs
,
471 to_fetch
, nr_heads
, &data
->shallow
,
472 &transport
->pack_lockfiles
, data
->version
);
474 data
->finished_handshake
= 0;
475 data
->options
.self_contained_and_connected
=
476 args
.self_contained_and_connected
;
477 data
->options
.connectivity_checked
= args
.connectivity_checked
;
481 if (report_unmatched_refs(to_fetch
, nr_heads
))
486 if (data
->fd
[1] >= 0)
488 if (finish_connect(data
->conn
))
494 list_objects_filter_release(&args
.filter_options
);
498 static int push_had_errors(struct ref
*ref
)
500 for (; ref
; ref
= ref
->next
) {
501 switch (ref
->status
) {
502 case REF_STATUS_NONE
:
503 case REF_STATUS_UPTODATE
:
513 int transport_refs_pushed(struct ref
*ref
)
515 for (; ref
; ref
= ref
->next
) {
516 switch(ref
->status
) {
517 case REF_STATUS_NONE
:
518 case REF_STATUS_UPTODATE
:
527 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
528 struct object_id
*new_oid
, int deletion
,
531 struct refspec_item rs
;
533 memset(&rs
, 0, sizeof(rs
));
537 if (!remote_find_tracking(remote
, &rs
)) {
539 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
541 delete_ref(NULL
, rs
.dst
, NULL
, 0);
543 update_ref("update by push", rs
.dst
, new_oid
,
549 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
552 struct object_id
*new_oid
;
553 struct ref_push_report
*report
;
555 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
558 report
= ref
->report
;
560 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
561 ref
->deletion
, verbose
);
563 for (; report
; report
= report
->next
) {
564 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
565 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
566 update_one_tracking_ref(remote
, refname
, new_oid
,
567 is_null_oid(new_oid
), verbose
);
571 static void print_ref_status(char flag
, const char *summary
,
572 struct ref
*to
, struct ref
*from
, const char *msg
,
573 struct ref_push_report
*report
,
574 int porcelain
, int summary_width
)
578 if (report
&& report
->ref_name
)
579 to_name
= report
->ref_name
;
585 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
587 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
589 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
591 fprintf(stdout
, "%s\n", summary
);
593 const char *red
= "", *reset
= "";
594 if (push_had_errors(to
)) {
595 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
596 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
598 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
601 fprintf(stderr
, "%s -> %s",
602 prettify_refname(from
->name
),
603 prettify_refname(to_name
));
605 fputs(prettify_refname(to_name
), stderr
);
615 static void print_ok_ref_status(struct ref
*ref
,
616 struct ref_push_report
*report
,
617 int porcelain
, int summary_width
)
619 struct object_id
*old_oid
;
620 struct object_id
*new_oid
;
621 const char *ref_name
;
624 if (report
&& report
->old_oid
)
625 old_oid
= report
->old_oid
;
627 old_oid
= &ref
->old_oid
;
628 if (report
&& report
->new_oid
)
629 new_oid
= report
->new_oid
;
631 new_oid
= &ref
->new_oid
;
632 if (report
&& report
->forced_update
)
633 forced_update
= report
->forced_update
;
635 forced_update
= ref
->forced_update
;
636 if (report
&& report
->ref_name
)
637 ref_name
= report
->ref_name
;
639 ref_name
= ref
->name
;
642 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
643 report
, porcelain
, summary_width
);
644 else if (is_null_oid(old_oid
))
645 print_ref_status('*',
646 (starts_with(ref_name
, "refs/tags/")
648 : (starts_with(ref_name
, "refs/heads/")
650 : "[new reference]")),
651 ref
, ref
->peer_ref
, NULL
,
652 report
, porcelain
, summary_width
);
654 struct strbuf quickref
= STRBUF_INIT
;
658 strbuf_add_unique_abbrev(&quickref
, old_oid
,
661 strbuf_addstr(&quickref
, "...");
663 msg
= "forced update";
665 strbuf_addstr(&quickref
, "..");
669 strbuf_add_unique_abbrev(&quickref
, new_oid
,
672 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
673 report
, porcelain
, summary_width
);
674 strbuf_release(&quickref
);
678 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
679 struct ref_push_report
*report
,
680 int porcelain
, int summary_width
)
683 char *url
= transport_anonymize_url(dest
);
684 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
688 switch(ref
->status
) {
689 case REF_STATUS_NONE
:
690 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
691 report
, porcelain
, summary_width
);
693 case REF_STATUS_REJECT_NODELETE
:
694 print_ref_status('!', "[rejected]", ref
, NULL
,
695 "remote does not support deleting refs",
696 report
, porcelain
, summary_width
);
698 case REF_STATUS_UPTODATE
:
699 print_ref_status('=', "[up to date]", ref
,
701 report
, porcelain
, summary_width
);
703 case REF_STATUS_REJECT_NONFASTFORWARD
:
704 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
706 report
, porcelain
, summary_width
);
708 case REF_STATUS_REJECT_ALREADY_EXISTS
:
709 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
711 report
, porcelain
, summary_width
);
713 case REF_STATUS_REJECT_FETCH_FIRST
:
714 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
716 report
, porcelain
, summary_width
);
718 case REF_STATUS_REJECT_NEEDS_FORCE
:
719 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
721 report
, porcelain
, summary_width
);
723 case REF_STATUS_REJECT_STALE
:
724 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
726 report
, porcelain
, summary_width
);
728 case REF_STATUS_REJECT_REMOTE_UPDATED
:
729 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
730 "remote ref updated since checkout",
731 report
, porcelain
, summary_width
);
733 case REF_STATUS_REJECT_SHALLOW
:
734 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
735 "new shallow roots not allowed",
736 report
, porcelain
, summary_width
);
738 case REF_STATUS_REMOTE_REJECT
:
739 print_ref_status('!', "[remote rejected]", ref
,
740 ref
->deletion
? NULL
: ref
->peer_ref
,
742 report
, porcelain
, summary_width
);
744 case REF_STATUS_EXPECTING_REPORT
:
745 print_ref_status('!', "[remote failure]", ref
,
746 ref
->deletion
? NULL
: ref
->peer_ref
,
747 "remote failed to report status",
748 report
, porcelain
, summary_width
);
750 case REF_STATUS_ATOMIC_PUSH_FAILED
:
751 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
752 "atomic push failed",
753 report
, porcelain
, summary_width
);
756 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
763 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
764 int porcelain
, int summary_width
)
766 struct ref_push_report
*report
;
770 return print_one_push_report(ref
, dest
, count
,
771 NULL
, porcelain
, summary_width
);
773 for (report
= ref
->report
; report
; report
= report
->next
)
774 print_one_push_report(ref
, dest
, count
+ n
++,
775 report
, porcelain
, summary_width
);
779 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
781 char hex
[GIT_MAX_HEXSZ
+ 1];
782 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
784 return (w
< sofar
) ? sofar
: w
;
787 int transport_summary_width(const struct ref
*refs
)
791 for (; refs
; refs
= refs
->next
) {
792 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
793 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
796 maxw
= FALLBACK_DEFAULT_ABBREV
;
797 return (2 * maxw
+ 3);
800 void transport_print_push_status(const char *dest
, struct ref
*refs
,
801 int verbose
, int porcelain
, unsigned int *reject_reasons
)
806 int summary_width
= transport_summary_width(refs
);
808 if (transport_color_config() < 0)
809 warning(_("could not parse transport.color.* config"));
811 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
814 for (ref
= refs
; ref
; ref
= ref
->next
)
815 if (ref
->status
== REF_STATUS_UPTODATE
)
816 n
+= print_one_push_status(ref
, dest
, n
,
817 porcelain
, summary_width
);
820 for (ref
= refs
; ref
; ref
= ref
->next
)
821 if (ref
->status
== REF_STATUS_OK
)
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_NONE
&&
828 ref
->status
!= REF_STATUS_UPTODATE
&&
829 ref
->status
!= REF_STATUS_OK
)
830 n
+= print_one_push_status(ref
, dest
, n
,
831 porcelain
, summary_width
);
832 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
833 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
834 *reject_reasons
|= REJECT_NON_FF_HEAD
;
836 *reject_reasons
|= REJECT_NON_FF_OTHER
;
837 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
838 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
839 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
840 *reject_reasons
|= REJECT_FETCH_FIRST
;
841 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
842 *reject_reasons
|= REJECT_NEEDS_FORCE
;
843 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
844 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
850 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
852 struct git_transport_data
*data
= transport
->data
;
853 struct send_pack_args args
;
856 if (transport_color_config() < 0)
859 if (!data
->finished_handshake
)
860 get_refs_via_connect(transport
, 1, NULL
);
862 memset(&args
, 0, sizeof(args
));
863 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
864 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
865 args
.use_thin_pack
= data
->options
.thin
;
866 args
.verbose
= (transport
->verbose
> 0);
867 args
.quiet
= (transport
->verbose
< 0);
868 args
.progress
= transport
->progress
;
869 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
870 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
871 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
872 args
.push_options
= transport
->push_options
;
873 args
.url
= transport
->url
;
875 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
876 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
877 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
878 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
880 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
882 switch (data
->version
) {
884 die(_("support for protocol v2 not implemented yet"));
888 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
891 case protocol_unknown_version
:
892 BUG("unknown protocol version");
898 * Atomic push may abort the connection early and close the pipe,
899 * which may cause an error for `finish_connect()`. Ignore this error
900 * for atomic git-push.
902 if (ret
|| args
.atomic
)
903 finish_connect(data
->conn
);
905 ret
= finish_connect(data
->conn
);
907 data
->finished_handshake
= 0;
912 static int connect_git(struct transport
*transport
, const char *name
,
913 const char *executable
, int fd
[2])
915 struct git_transport_data
*data
= transport
->data
;
916 data
->conn
= git_connect(data
->fd
, transport
->url
,
923 static int disconnect_git(struct transport
*transport
)
925 struct git_transport_data
*data
= transport
->data
;
927 if (data
->finished_handshake
&& !transport
->stateless_rpc
)
928 packet_flush(data
->fd
[1]);
930 if (data
->fd
[1] >= 0)
932 finish_connect(data
->conn
);
935 list_objects_filter_release(&data
->options
.filter_options
);
940 static struct transport_vtable taken_over_vtable
= {
941 .get_refs_list
= get_refs_via_connect
,
942 .get_bundle_uri
= get_bundle_uri
,
943 .fetch_refs
= fetch_refs_via_pack
,
944 .push_refs
= git_transport_push
,
945 .disconnect
= disconnect_git
948 void transport_take_over(struct transport
*transport
,
949 struct child_process
*child
)
951 struct git_transport_data
*data
;
953 if (!transport
->smart_options
)
954 BUG("taking over transport requires non-NULL "
955 "smart_options field.");
957 CALLOC_ARRAY(data
, 1);
958 data
->options
= *transport
->smart_options
;
960 data
->fd
[0] = data
->conn
->out
;
961 data
->fd
[1] = data
->conn
->in
;
962 data
->finished_handshake
= 0;
963 transport
->data
= data
;
965 transport
->vtable
= &taken_over_vtable
;
966 transport
->smart_options
= &(data
->options
);
968 transport
->cannot_reuse
= 1;
971 static int is_file(const char *url
)
976 return S_ISREG(buf
.st_mode
);
979 static int external_specification_len(const char *url
)
981 return strchr(url
, ':') - url
;
984 static const struct string_list
*protocol_allow_list(void)
986 static int enabled
= -1;
987 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
990 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
992 string_list_split(&allowed
, v
, ':', -1);
993 string_list_sort(&allowed
);
1000 return enabled
? &allowed
: NULL
;
1003 enum protocol_allow_config
{
1004 PROTOCOL_ALLOW_NEVER
= 0,
1005 PROTOCOL_ALLOW_USER_ONLY
,
1006 PROTOCOL_ALLOW_ALWAYS
1009 static enum protocol_allow_config
parse_protocol_config(const char *key
,
1012 if (!strcasecmp(value
, "always"))
1013 return PROTOCOL_ALLOW_ALWAYS
;
1014 else if (!strcasecmp(value
, "never"))
1015 return PROTOCOL_ALLOW_NEVER
;
1016 else if (!strcasecmp(value
, "user"))
1017 return PROTOCOL_ALLOW_USER_ONLY
;
1019 die(_("unknown value for config '%s': %s"), key
, value
);
1022 static enum protocol_allow_config
get_protocol_config(const char *type
)
1024 char *key
= xstrfmt("protocol.%s.allow", type
);
1027 /* first check the per-protocol config */
1028 if (!git_config_get_string(key
, &value
)) {
1029 enum protocol_allow_config ret
=
1030 parse_protocol_config(key
, value
);
1037 /* if defined, fallback to user-defined default for unknown protocols */
1038 if (!git_config_get_string("protocol.allow", &value
)) {
1039 enum protocol_allow_config ret
=
1040 parse_protocol_config("protocol.allow", value
);
1045 /* fallback to built-in defaults */
1047 if (!strcmp(type
, "http") ||
1048 !strcmp(type
, "https") ||
1049 !strcmp(type
, "git") ||
1050 !strcmp(type
, "ssh"))
1051 return PROTOCOL_ALLOW_ALWAYS
;
1053 /* known scary; err on the side of caution */
1054 if (!strcmp(type
, "ext"))
1055 return PROTOCOL_ALLOW_NEVER
;
1057 /* unknown; by default let them be used only directly by the user */
1058 return PROTOCOL_ALLOW_USER_ONLY
;
1061 int is_transport_allowed(const char *type
, int from_user
)
1063 const struct string_list
*allow_list
= protocol_allow_list();
1065 return string_list_has_string(allow_list
, type
);
1067 switch (get_protocol_config(type
)) {
1068 case PROTOCOL_ALLOW_ALWAYS
:
1070 case PROTOCOL_ALLOW_NEVER
:
1072 case PROTOCOL_ALLOW_USER_ONLY
:
1074 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1078 BUG("invalid protocol_allow_config type");
1081 void transport_check_allowed(const char *type
)
1083 if (!is_transport_allowed(type
, -1))
1084 die(_("transport '%s' not allowed"), type
);
1087 static struct transport_vtable bundle_vtable
= {
1088 .get_refs_list
= get_refs_from_bundle
,
1089 .fetch_refs
= fetch_refs_from_bundle
,
1090 .disconnect
= close_bundle
1093 static struct transport_vtable builtin_smart_vtable
= {
1094 .get_refs_list
= get_refs_via_connect
,
1095 .get_bundle_uri
= get_bundle_uri
,
1096 .fetch_refs
= fetch_refs_via_pack
,
1097 .push_refs
= git_transport_push
,
1098 .connect
= connect_git
,
1099 .disconnect
= disconnect_git
1102 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1105 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1107 ret
->progress
= isatty(2);
1108 string_list_init_dup(&ret
->pack_lockfiles
);
1110 CALLOC_ARRAY(ret
->bundles
, 1);
1111 init_bundle_list(ret
->bundles
);
1114 BUG("No remote provided to transport_get()");
1116 ret
->got_remote_refs
= 0;
1117 ret
->remote
= remote
;
1118 helper
= remote
->foreign_vcs
;
1120 if (!url
&& remote
->url
)
1121 url
= remote
->url
[0];
1124 /* maybe it is a foreign URL? */
1126 const char *p
= url
;
1128 while (is_urlschemechar(p
== url
, *p
))
1130 if (starts_with(p
, "::"))
1131 helper
= xstrndup(url
, p
- url
);
1135 transport_helper_init(ret
, helper
);
1136 } else if (starts_with(url
, "rsync:")) {
1137 die(_("git-over-rsync is no longer supported"));
1138 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1139 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1140 bundle_header_init(&data
->header
);
1141 transport_check_allowed("file");
1143 ret
->vtable
= &bundle_vtable
;
1144 ret
->smart_options
= NULL
;
1145 } else if (!is_url(url
)
1146 || starts_with(url
, "file://")
1147 || starts_with(url
, "git://")
1148 || starts_with(url
, "ssh://")
1149 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1150 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1153 * These are builtin smart transports; "allowed" transports
1154 * will be checked individually in git_connect.
1156 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1157 list_objects_filter_init(&data
->options
.filter_options
);
1159 ret
->vtable
= &builtin_smart_vtable
;
1160 ret
->smart_options
= &(data
->options
);
1163 data
->finished_handshake
= 0;
1165 /* Unknown protocol in URL. Pass to external handler. */
1166 int len
= external_specification_len(url
);
1167 char *handler
= xmemdupz(url
, len
);
1168 transport_helper_init(ret
, handler
);
1171 if (ret
->smart_options
) {
1172 ret
->smart_options
->thin
= 1;
1173 ret
->smart_options
->uploadpack
= "git-upload-pack";
1174 if (remote
->uploadpack
)
1175 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1176 ret
->smart_options
->receivepack
= "git-receive-pack";
1177 if (remote
->receivepack
)
1178 ret
->smart_options
->receivepack
= remote
->receivepack
;
1181 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1186 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1188 return transport
->hash_algo
;
1191 int transport_set_option(struct transport
*transport
,
1192 const char *name
, const char *value
)
1194 int git_reports
= 1, protocol_reports
= 1;
1196 if (transport
->smart_options
)
1197 git_reports
= set_git_option(transport
->smart_options
,
1200 if (transport
->vtable
->set_option
)
1201 protocol_reports
= transport
->vtable
->set_option(transport
,
1204 /* If either report is 0, report 0 (success). */
1205 if (!git_reports
|| !protocol_reports
)
1207 /* If either reports -1 (invalid value), report -1. */
1208 if ((git_reports
== -1) || (protocol_reports
== -1))
1210 /* Otherwise if both report unknown, report unknown. */
1214 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1218 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1220 transport
->verbose
= -1;
1223 * Rules used to determine whether to report progress (processing aborts
1224 * when a rule is satisfied):
1226 * . Report progress, if force_progress is 1 (ie. --progress).
1227 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1228 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1229 * . Report progress if isatty(2) is 1.
1231 if (force_progress
>= 0)
1232 transport
->progress
= !!force_progress
;
1234 transport
->progress
= verbosity
>= 0 && isatty(2);
1237 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1241 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1242 "not be found on any remote:\n"));
1243 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1244 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1245 fprintf(stderr
, _("\nPlease try\n\n"
1246 " git push --recurse-submodules=on-demand\n\n"
1247 "or cd to the path and use\n\n"
1249 "to push them to a remote.\n\n"));
1251 string_list_clear(needs_pushing
, 0);
1253 die(_("Aborting."));
1256 static int run_pre_push_hook(struct transport
*transport
,
1257 struct ref
*remote_refs
)
1261 struct child_process proc
= CHILD_PROCESS_INIT
;
1263 const char *hook_path
= find_hook("pre-push");
1268 strvec_push(&proc
.args
, hook_path
);
1269 strvec_push(&proc
.args
, transport
->remote
->name
);
1270 strvec_push(&proc
.args
, transport
->url
);
1273 proc
.trace2_hook_name
= "pre-push";
1275 if (start_command(&proc
)) {
1276 finish_command(&proc
);
1280 sigchain_push(SIGPIPE
, SIG_IGN
);
1282 strbuf_init(&buf
, 256);
1284 for (r
= remote_refs
; r
; r
= r
->next
) {
1285 if (!r
->peer_ref
) continue;
1286 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1287 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1288 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1289 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1292 strbuf_addf( &buf
, "%s %s %s %s\n",
1293 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1294 r
->name
, oid_to_hex(&r
->old_oid
));
1296 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1297 /* We do not mind if a hook does not read all refs. */
1304 strbuf_release(&buf
);
1310 sigchain_pop(SIGPIPE
);
1312 x
= finish_command(&proc
);
1319 int transport_push(struct repository
*r
,
1320 struct transport
*transport
,
1321 struct refspec
*rs
, int flags
,
1322 unsigned int *reject_reasons
)
1324 struct ref
*remote_refs
= NULL
;
1325 struct ref
*local_refs
= NULL
;
1326 int match_flags
= MATCH_REFS_NONE
;
1327 int verbose
= (transport
->verbose
> 0);
1328 int quiet
= (transport
->verbose
< 0);
1329 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1330 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1333 struct transport_ls_refs_options transport_options
=
1334 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1336 *reject_reasons
= 0;
1338 if (transport_color_config() < 0)
1341 if (!transport
->vtable
->push_refs
)
1344 local_refs
= get_local_heads();
1346 if (check_push_refs(local_refs
, rs
) < 0)
1349 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1351 trace2_region_enter("transport_push", "get_refs_list", r
);
1352 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1353 &transport_options
);
1354 trace2_region_leave("transport_push", "get_refs_list", r
);
1356 transport_ls_refs_options_release(&transport_options
);
1358 if (flags
& TRANSPORT_PUSH_ALL
)
1359 match_flags
|= MATCH_REFS_ALL
;
1360 if (flags
& TRANSPORT_PUSH_MIRROR
)
1361 match_flags
|= MATCH_REFS_MIRROR
;
1362 if (flags
& TRANSPORT_PUSH_PRUNE
)
1363 match_flags
|= MATCH_REFS_PRUNE
;
1364 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1365 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1367 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1370 if (transport
->smart_options
&&
1371 transport
->smart_options
->cas
&&
1372 !is_empty_cas(transport
->smart_options
->cas
))
1373 apply_push_cas(transport
->smart_options
->cas
,
1374 transport
->remote
, remote_refs
);
1376 set_ref_status_for_push(remote_refs
,
1377 flags
& TRANSPORT_PUSH_MIRROR
,
1378 flags
& TRANSPORT_PUSH_FORCE
);
1380 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1381 if (run_pre_push_hook(transport
, remote_refs
))
1384 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1385 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1386 !is_bare_repository()) {
1387 struct ref
*ref
= remote_refs
;
1388 struct oid_array commits
= OID_ARRAY_INIT
;
1390 trace2_region_enter("transport_push", "push_submodules", r
);
1391 for (; ref
; ref
= ref
->next
)
1392 if (!is_null_oid(&ref
->new_oid
))
1393 oid_array_append(&commits
,
1396 if (!push_unpushed_submodules(r
,
1400 transport
->push_options
,
1402 oid_array_clear(&commits
);
1403 trace2_region_leave("transport_push", "push_submodules", r
);
1404 die(_("failed to push all needed submodules"));
1406 oid_array_clear(&commits
);
1407 trace2_region_leave("transport_push", "push_submodules", r
);
1410 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1411 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1412 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1413 !pretend
)) && !is_bare_repository()) {
1414 struct ref
*ref
= remote_refs
;
1415 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1416 struct oid_array commits
= OID_ARRAY_INIT
;
1418 trace2_region_enter("transport_push", "check_submodules", r
);
1419 for (; ref
; ref
= ref
->next
)
1420 if (!is_null_oid(&ref
->new_oid
))
1421 oid_array_append(&commits
,
1424 if (find_unpushed_submodules(r
,
1426 transport
->remote
->name
,
1428 oid_array_clear(&commits
);
1429 trace2_region_leave("transport_push", "check_submodules", r
);
1430 die_with_unpushed_submodules(&needs_pushing
);
1432 string_list_clear(&needs_pushing
, 0);
1433 oid_array_clear(&commits
);
1434 trace2_region_leave("transport_push", "check_submodules", r
);
1437 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1438 trace2_region_enter("transport_push", "push_refs", r
);
1439 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1440 trace2_region_leave("transport_push", "push_refs", r
);
1443 err
= push_had_errors(remote_refs
);
1444 ret
= push_ret
| err
;
1447 transport_print_push_status(transport
->url
, remote_refs
,
1448 verbose
| porcelain
, porcelain
,
1451 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1452 set_upstreams(transport
, remote_refs
, pretend
);
1454 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1455 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1457 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1458 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1461 if (porcelain
&& !push_ret
)
1463 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1464 fprintf(stderr
, "Everything up-to-date\n");
1467 free_refs(local_refs
);
1468 free_refs(remote_refs
);
1472 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1473 struct transport_ls_refs_options
*transport_options
)
1475 if (!transport
->got_remote_refs
) {
1476 transport
->remote_refs
=
1477 transport
->vtable
->get_refs_list(transport
, 0,
1479 transport
->got_remote_refs
= 1;
1482 return transport
->remote_refs
;
1485 void transport_ls_refs_options_release(struct transport_ls_refs_options
*opts
)
1487 strvec_clear(&opts
->ref_prefixes
);
1488 free((char *)opts
->unborn_head_target
);
1491 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1494 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1495 struct ref
**heads
= NULL
;
1498 for (rm
= refs
; rm
; rm
= rm
->next
) {
1501 !is_null_oid(&rm
->old_oid
) &&
1502 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1504 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1505 heads
[nr_heads
++] = rm
;
1510 * When deepening of a shallow repository is requested,
1511 * then local and remote refs are likely to still be equal.
1512 * Just feed them all to the fetch method in that case.
1513 * This condition shouldn't be met in a non-deepening fetch
1514 * (see builtin/fetch.c:quickfetch()).
1516 ALLOC_ARRAY(heads
, nr_refs
);
1517 for (rm
= refs
; rm
; rm
= rm
->next
)
1518 heads
[nr_heads
++] = rm
;
1521 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1527 int transport_get_remote_bundle_uri(struct transport
*transport
)
1530 const struct transport_vtable
*vtable
= transport
->vtable
;
1532 /* Check config only once. */
1533 if (transport
->got_remote_bundle_uri
)
1535 transport
->got_remote_bundle_uri
= 1;
1538 * Don't request bundle-uri from the server unless configured to
1539 * do so by the transfer.bundleURI=true config option.
1541 if (git_config_get_bool("transfer.bundleuri", &value
) || !value
)
1544 if (!transport
->bundles
->baseURI
)
1545 transport
->bundles
->baseURI
= xstrdup(transport
->url
);
1547 if (!vtable
->get_bundle_uri
)
1548 return error(_("bundle-uri operation not supported by protocol"));
1550 if (vtable
->get_bundle_uri(transport
) < 0)
1551 return error(_("could not retrieve server-advertised bundle-uri list"));
1555 void transport_unlock_pack(struct transport
*transport
, unsigned int flags
)
1557 int in_signal_handler
= !!(flags
& TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER
);
1560 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1561 if (in_signal_handler
)
1562 unlink(transport
->pack_lockfiles
.items
[i
].string
);
1564 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1565 if (!in_signal_handler
)
1566 string_list_clear(&transport
->pack_lockfiles
, 0);
1569 int transport_connect(struct transport
*transport
, const char *name
,
1570 const char *exec
, int fd
[2])
1572 if (transport
->vtable
->connect
)
1573 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1575 die(_("operation not supported by protocol"));
1578 int transport_disconnect(struct transport
*transport
)
1581 if (transport
->vtable
->disconnect
)
1582 ret
= transport
->vtable
->disconnect(transport
);
1583 if (transport
->got_remote_refs
)
1584 free_refs((void *)transport
->remote_refs
);
1585 clear_bundle_list(transport
->bundles
);
1586 free(transport
->bundles
);
1592 * Strip username (and password) from a URL and return
1593 * it in a newly allocated string.
1595 char *transport_anonymize_url(const char *url
)
1597 char *scheme_prefix
, *anon_part
;
1598 size_t anon_len
, prefix_len
= 0;
1600 anon_part
= strchr(url
, '@');
1601 if (url_is_local_not_ssh(url
) || !anon_part
)
1604 anon_len
= strlen(++anon_part
);
1605 scheme_prefix
= strstr(url
, "://");
1606 if (!scheme_prefix
) {
1607 if (!strchr(anon_part
, ':'))
1608 /* cannot be "me@there:/path/name" */
1612 /* make sure scheme is reasonable */
1613 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1616 case '+': case '.': case '-':
1625 /* @ past the first slash does not count */
1626 cp
= strchr(scheme_prefix
+ 3, '/');
1627 if (cp
&& cp
< anon_part
)
1629 prefix_len
= scheme_prefix
- url
+ 3;
1631 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1632 (int)anon_len
, anon_part
);
1634 return xstrdup(url
);