1 #include "git-compat-util.h"
7 #include "fetch-pack.h"
10 #include "send-pack.h"
19 #include "submodule.h"
20 #include "string-list.h"
21 #include "oid-array.h"
23 #include "transport-internal.h"
25 #include "object-store.h"
27 #include "bundle-uri.h"
29 static int transport_use_color
= -1;
30 static char transport_colors
[][COLOR_MAXLEN
] = {
32 GIT_COLOR_RED
/* REJECTED */
35 enum color_transport
{
36 TRANSPORT_COLOR_RESET
= 0,
37 TRANSPORT_COLOR_REJECTED
= 1
40 static int transport_color_config(void)
42 const char *keys
[] = {
43 "color.transport.reset",
44 "color.transport.rejected"
45 }, *key
= "color.transport";
48 static int initialized
;
54 if (!git_config_get_string(key
, &value
))
55 transport_use_color
= git_config_colorbool(key
, value
);
57 if (!want_color_stderr(transport_use_color
))
60 for (i
= 0; i
< ARRAY_SIZE(keys
); i
++)
61 if (!git_config_get_string(keys
[i
], &value
)) {
63 return config_error_nonbool(keys
[i
]);
64 if (color_parse(value
, transport_colors
[i
]) < 0)
71 static const char *transport_get_color(enum color_transport ix
)
73 if (want_color_stderr(transport_use_color
))
74 return transport_colors
[ix
];
78 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
82 for (ref
= refs
; ref
; ref
= ref
->next
) {
83 const char *localname
;
85 const char *remotename
;
88 * Check suitability for tracking. Must be successful /
89 * already up-to-date ref create/modify (not delete).
91 if (ref
->status
!= REF_STATUS_OK
&&
92 ref
->status
!= REF_STATUS_UPTODATE
)
96 if (is_null_oid(&ref
->new_oid
))
99 /* Follow symbolic refs (mainly for HEAD). */
100 localname
= ref
->peer_ref
->name
;
101 remotename
= ref
->name
;
102 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
104 if (tmp
&& flag
& REF_ISSYMREF
&&
105 starts_with(tmp
, "refs/heads/"))
108 /* Both source and destination must be local branches. */
109 if (!localname
|| !starts_with(localname
, "refs/heads/"))
111 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
115 int flag
= transport
->verbose
< 0 ? 0 : BRANCH_CONFIG_VERBOSE
;
116 install_branch_config(flag
, localname
+ 11,
117 transport
->remote
->name
, remotename
);
118 } else if (transport
->verbose
>= 0)
119 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
120 localname
+ 11, remotename
+ 11,
121 transport
->remote
->name
);
125 struct bundle_transport_data
{
127 struct bundle_header header
;
128 unsigned get_refs_from_bundle_called
: 1;
131 static void get_refs_from_bundle_inner(struct transport
*transport
)
133 struct bundle_transport_data
*data
= transport
->data
;
135 data
->get_refs_from_bundle_called
= 1;
139 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
141 die(_("could not read bundle '%s'"), transport
->url
);
143 transport
->hash_algo
= data
->header
.hash_algo
;
146 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
148 struct transport_ls_refs_options
*transport_options UNUSED
)
150 struct bundle_transport_data
*data
= transport
->data
;
151 struct ref
*result
= NULL
;
157 get_refs_from_bundle_inner(transport
);
159 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
160 struct string_list_item
*e
= data
->header
.references
.items
+ i
;
161 const char *name
= e
->string
;
162 struct ref
*ref
= alloc_ref(name
);
163 struct object_id
*oid
= e
->util
;
164 oidcpy(&ref
->old_oid
, oid
);
171 static int fetch_refs_from_bundle(struct transport
*transport
,
172 int nr_heads
, struct ref
**to_fetch
)
174 struct bundle_transport_data
*data
= transport
->data
;
175 struct strvec extra_index_pack_args
= STRVEC_INIT
;
178 if (transport
->progress
)
179 strvec_push(&extra_index_pack_args
, "-v");
181 if (!data
->get_refs_from_bundle_called
)
182 get_refs_from_bundle_inner(transport
);
183 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
184 &extra_index_pack_args
, 0);
185 transport
->hash_algo
= data
->header
.hash_algo
;
189 static int close_bundle(struct transport
*transport
)
191 struct bundle_transport_data
*data
= transport
->data
;
194 bundle_header_release(&data
->header
);
199 struct git_transport_data
{
200 struct git_transport_options options
;
201 struct child_process
*conn
;
203 unsigned finished_handshake
: 1;
204 enum protocol_version version
;
205 struct oid_array extra_have
;
206 struct oid_array shallow
;
209 static int set_git_option(struct git_transport_options
*opts
,
210 const char *name
, const char *value
)
212 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
213 opts
->uploadpack
= value
;
215 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
216 opts
->receivepack
= value
;
218 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
219 opts
->thin
= !!value
;
221 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
222 opts
->followtags
= !!value
;
224 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
225 opts
->keep
= !!value
;
227 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
228 opts
->update_shallow
= !!value
;
230 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
235 opts
->depth
= strtol(value
, &end
, 0);
237 die(_("transport: invalid depth option '%s'"), value
);
240 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
241 opts
->deepen_since
= value
;
243 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
244 opts
->deepen_not
= (const struct string_list
*)value
;
246 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
247 opts
->deepen_relative
= !!value
;
249 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
250 opts
->from_promisor
= !!value
;
252 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
253 list_objects_filter_die_if_populated(&opts
->filter_options
);
254 parse_list_objects_filter(&opts
->filter_options
, value
);
256 } else if (!strcmp(name
, TRANS_OPT_REFETCH
)) {
257 opts
->refetch
= !!value
;
259 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
260 opts
->reject_shallow
= !!value
;
266 static int connect_setup(struct transport
*transport
, int for_push
)
268 struct git_transport_data
*data
= transport
->data
;
269 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
274 switch (transport
->family
) {
275 case TRANSPORT_FAMILY_ALL
: break;
276 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
277 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
280 data
->conn
= git_connect(data
->fd
, transport
->url
,
281 for_push
? data
->options
.receivepack
:
282 data
->options
.uploadpack
,
288 static void die_if_server_options(struct transport
*transport
)
290 if (!transport
->server_options
|| !transport
->server_options
->nr
)
292 advise(_("see protocol.version in 'git help config' for more details"));
293 die(_("server options require protocol version 2 or later"));
297 * Obtains the protocol version from the transport and writes it to
298 * transport->data->version, first connecting if not already connected.
300 * If the protocol version is one that allows skipping the listing of remote
301 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
302 * this function returns NULL. Otherwise, this function returns the list of
305 static struct ref
*handshake(struct transport
*transport
, int for_push
,
306 struct transport_ls_refs_options
*options
,
309 struct git_transport_data
*data
= transport
->data
;
310 struct ref
*refs
= NULL
;
311 struct packet_reader reader
;
313 const char *server_sid
;
315 connect_setup(transport
, for_push
);
317 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
318 PACKET_READ_CHOMP_NEWLINE
|
319 PACKET_READ_GENTLE_ON_EOF
|
320 PACKET_READ_DIE_ON_ERR_PACKET
);
322 data
->version
= discover_version(&reader
);
323 switch (data
->version
) {
325 if (server_feature_v2("session-id", &server_sid
))
326 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
328 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
330 transport
->server_options
,
331 transport
->stateless_rpc
);
335 die_if_server_options(transport
);
336 get_remote_heads(&reader
, &refs
,
337 for_push
? REF_NORMAL
: 0,
340 server_sid
= server_feature_value("session-id", &sid_len
);
342 char *sid
= xstrndup(server_sid
, sid_len
);
343 trace2_data_string("transfer", NULL
, "server-sid", sid
);
347 case protocol_unknown_version
:
348 BUG("unknown protocol version");
350 data
->finished_handshake
= 1;
351 transport
->hash_algo
= reader
.hash_algo
;
353 if (reader
.line_peeked
)
354 BUG("buffer must be empty at the end of handshake()");
359 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
360 struct transport_ls_refs_options
*options
)
362 return handshake(transport
, for_push
, options
, 1);
365 static int get_bundle_uri(struct transport
*transport
)
367 struct git_transport_data
*data
= transport
->data
;
368 struct packet_reader reader
;
369 int stateless_rpc
= transport
->stateless_rpc
;
371 if (!transport
->bundles
) {
372 CALLOC_ARRAY(transport
->bundles
, 1);
373 init_bundle_list(transport
->bundles
);
376 if (!data
->finished_handshake
) {
377 struct ref
*refs
= handshake(transport
, 0, NULL
, 0);
384 * "Support" protocol v0 and v2 without bundle-uri support by
385 * silently degrading to a NOOP.
387 if (!server_supports_v2("bundle-uri"))
390 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
391 PACKET_READ_CHOMP_NEWLINE
|
392 PACKET_READ_GENTLE_ON_EOF
);
394 return get_remote_bundle_uri(data
->fd
[1], &reader
,
395 transport
->bundles
, stateless_rpc
);
398 static int fetch_refs_via_pack(struct transport
*transport
,
399 int nr_heads
, struct ref
**to_fetch
)
402 struct git_transport_data
*data
= transport
->data
;
403 struct ref
*refs
= NULL
;
404 struct fetch_pack_args args
;
405 struct ref
*refs_tmp
= NULL
;
407 memset(&args
, 0, sizeof(args
));
408 args
.uploadpack
= data
->options
.uploadpack
;
409 args
.keep_pack
= data
->options
.keep
;
411 args
.use_thin_pack
= data
->options
.thin
;
412 args
.include_tag
= data
->options
.followtags
;
413 args
.verbose
= (transport
->verbose
> 1);
414 args
.quiet
= (transport
->verbose
< 0);
415 args
.no_progress
= !transport
->progress
;
416 args
.depth
= data
->options
.depth
;
417 args
.deepen_since
= data
->options
.deepen_since
;
418 args
.deepen_not
= data
->options
.deepen_not
;
419 args
.deepen_relative
= data
->options
.deepen_relative
;
420 args
.check_self_contained_and_connected
=
421 data
->options
.check_self_contained_and_connected
;
422 args
.cloning
= transport
->cloning
;
423 args
.update_shallow
= data
->options
.update_shallow
;
424 args
.from_promisor
= data
->options
.from_promisor
;
425 list_objects_filter_copy(&args
.filter_options
,
426 &data
->options
.filter_options
);
427 args
.refetch
= data
->options
.refetch
;
428 args
.stateless_rpc
= transport
->stateless_rpc
;
429 args
.server_options
= transport
->server_options
;
430 args
.negotiation_tips
= data
->options
.negotiation_tips
;
431 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
433 if (!data
->finished_handshake
) {
435 int must_list_refs
= 0;
436 for (i
= 0; i
< nr_heads
; i
++) {
437 if (!to_fetch
[i
]->exact_oid
) {
442 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
445 if (data
->version
== protocol_unknown_version
)
446 BUG("unknown protocol version");
447 else if (data
->version
<= protocol_v1
)
448 die_if_server_options(transport
);
450 if (data
->options
.acked_commits
) {
451 if (data
->version
< protocol_v2
) {
452 warning(_("--negotiate-only requires protocol v2"));
454 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
455 warning(_("server does not support wait-for-done"));
458 negotiate_using_fetch(data
->options
.negotiation_tips
,
459 transport
->server_options
,
460 transport
->stateless_rpc
,
462 data
->options
.acked_commits
);
468 refs
= fetch_pack(&args
, data
->fd
,
469 refs_tmp
? refs_tmp
: transport
->remote_refs
,
470 to_fetch
, nr_heads
, &data
->shallow
,
471 &transport
->pack_lockfiles
, data
->version
);
473 data
->finished_handshake
= 0;
474 data
->options
.self_contained_and_connected
=
475 args
.self_contained_and_connected
;
476 data
->options
.connectivity_checked
= args
.connectivity_checked
;
480 if (report_unmatched_refs(to_fetch
, nr_heads
))
485 if (data
->fd
[1] >= 0)
487 if (finish_connect(data
->conn
))
493 list_objects_filter_release(&args
.filter_options
);
497 static int push_had_errors(struct ref
*ref
)
499 for (; ref
; ref
= ref
->next
) {
500 switch (ref
->status
) {
501 case REF_STATUS_NONE
:
502 case REF_STATUS_UPTODATE
:
512 int transport_refs_pushed(struct ref
*ref
)
514 for (; ref
; ref
= ref
->next
) {
515 switch(ref
->status
) {
516 case REF_STATUS_NONE
:
517 case REF_STATUS_UPTODATE
:
526 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
527 struct object_id
*new_oid
, int deletion
,
530 struct refspec_item rs
;
532 memset(&rs
, 0, sizeof(rs
));
536 if (!remote_find_tracking(remote
, &rs
)) {
538 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
540 delete_ref(NULL
, rs
.dst
, NULL
, 0);
542 update_ref("update by push", rs
.dst
, new_oid
,
548 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
551 struct object_id
*new_oid
;
552 struct ref_push_report
*report
;
554 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
557 report
= ref
->report
;
559 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
560 ref
->deletion
, verbose
);
562 for (; report
; report
= report
->next
) {
563 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
564 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
565 update_one_tracking_ref(remote
, refname
, new_oid
,
566 is_null_oid(new_oid
), verbose
);
570 static void print_ref_status(char flag
, const char *summary
,
571 struct ref
*to
, struct ref
*from
, const char *msg
,
572 struct ref_push_report
*report
,
573 int porcelain
, int summary_width
)
577 if (report
&& report
->ref_name
)
578 to_name
= report
->ref_name
;
584 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
586 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
588 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
590 fprintf(stdout
, "%s\n", summary
);
592 const char *red
= "", *reset
= "";
593 if (push_had_errors(to
)) {
594 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
595 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
597 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
600 fprintf(stderr
, "%s -> %s",
601 prettify_refname(from
->name
),
602 prettify_refname(to_name
));
604 fputs(prettify_refname(to_name
), stderr
);
614 static void print_ok_ref_status(struct ref
*ref
,
615 struct ref_push_report
*report
,
616 int porcelain
, int summary_width
)
618 struct object_id
*old_oid
;
619 struct object_id
*new_oid
;
620 const char *ref_name
;
623 if (report
&& report
->old_oid
)
624 old_oid
= report
->old_oid
;
626 old_oid
= &ref
->old_oid
;
627 if (report
&& report
->new_oid
)
628 new_oid
= report
->new_oid
;
630 new_oid
= &ref
->new_oid
;
631 if (report
&& report
->forced_update
)
632 forced_update
= report
->forced_update
;
634 forced_update
= ref
->forced_update
;
635 if (report
&& report
->ref_name
)
636 ref_name
= report
->ref_name
;
638 ref_name
= ref
->name
;
641 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
642 report
, porcelain
, summary_width
);
643 else if (is_null_oid(old_oid
))
644 print_ref_status('*',
645 (starts_with(ref_name
, "refs/tags/")
647 : (starts_with(ref_name
, "refs/heads/")
649 : "[new reference]")),
650 ref
, ref
->peer_ref
, NULL
,
651 report
, porcelain
, summary_width
);
653 struct strbuf quickref
= STRBUF_INIT
;
657 strbuf_add_unique_abbrev(&quickref
, old_oid
,
660 strbuf_addstr(&quickref
, "...");
662 msg
= "forced update";
664 strbuf_addstr(&quickref
, "..");
668 strbuf_add_unique_abbrev(&quickref
, new_oid
,
671 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
672 report
, porcelain
, summary_width
);
673 strbuf_release(&quickref
);
677 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
678 struct ref_push_report
*report
,
679 int porcelain
, int summary_width
)
682 char *url
= transport_anonymize_url(dest
);
683 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
687 switch(ref
->status
) {
688 case REF_STATUS_NONE
:
689 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
690 report
, porcelain
, summary_width
);
692 case REF_STATUS_REJECT_NODELETE
:
693 print_ref_status('!', "[rejected]", ref
, NULL
,
694 "remote does not support deleting refs",
695 report
, porcelain
, summary_width
);
697 case REF_STATUS_UPTODATE
:
698 print_ref_status('=', "[up to date]", ref
,
700 report
, porcelain
, summary_width
);
702 case REF_STATUS_REJECT_NONFASTFORWARD
:
703 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
705 report
, porcelain
, summary_width
);
707 case REF_STATUS_REJECT_ALREADY_EXISTS
:
708 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
710 report
, porcelain
, summary_width
);
712 case REF_STATUS_REJECT_FETCH_FIRST
:
713 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
715 report
, porcelain
, summary_width
);
717 case REF_STATUS_REJECT_NEEDS_FORCE
:
718 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
720 report
, porcelain
, summary_width
);
722 case REF_STATUS_REJECT_STALE
:
723 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
725 report
, porcelain
, summary_width
);
727 case REF_STATUS_REJECT_REMOTE_UPDATED
:
728 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
729 "remote ref updated since checkout",
730 report
, porcelain
, summary_width
);
732 case REF_STATUS_REJECT_SHALLOW
:
733 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
734 "new shallow roots not allowed",
735 report
, porcelain
, summary_width
);
737 case REF_STATUS_REMOTE_REJECT
:
738 print_ref_status('!', "[remote rejected]", ref
,
739 ref
->deletion
? NULL
: ref
->peer_ref
,
741 report
, porcelain
, summary_width
);
743 case REF_STATUS_EXPECTING_REPORT
:
744 print_ref_status('!', "[remote failure]", ref
,
745 ref
->deletion
? NULL
: ref
->peer_ref
,
746 "remote failed to report status",
747 report
, porcelain
, summary_width
);
749 case REF_STATUS_ATOMIC_PUSH_FAILED
:
750 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
751 "atomic push failed",
752 report
, porcelain
, summary_width
);
755 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
762 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
763 int porcelain
, int summary_width
)
765 struct ref_push_report
*report
;
769 return print_one_push_report(ref
, dest
, count
,
770 NULL
, porcelain
, summary_width
);
772 for (report
= ref
->report
; report
; report
= report
->next
)
773 print_one_push_report(ref
, dest
, count
+ n
++,
774 report
, porcelain
, summary_width
);
778 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
780 char hex
[GIT_MAX_HEXSZ
+ 1];
781 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
783 return (w
< sofar
) ? sofar
: w
;
786 int transport_summary_width(const struct ref
*refs
)
790 for (; refs
; refs
= refs
->next
) {
791 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
792 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
795 maxw
= FALLBACK_DEFAULT_ABBREV
;
796 return (2 * maxw
+ 3);
799 void transport_print_push_status(const char *dest
, struct ref
*refs
,
800 int verbose
, int porcelain
, unsigned int *reject_reasons
)
805 int summary_width
= transport_summary_width(refs
);
807 if (transport_color_config() < 0)
808 warning(_("could not parse transport.color.* config"));
810 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
813 for (ref
= refs
; ref
; ref
= ref
->next
)
814 if (ref
->status
== REF_STATUS_UPTODATE
)
815 n
+= print_one_push_status(ref
, dest
, n
,
816 porcelain
, summary_width
);
819 for (ref
= refs
; ref
; ref
= ref
->next
)
820 if (ref
->status
== REF_STATUS_OK
)
821 n
+= print_one_push_status(ref
, dest
, n
,
822 porcelain
, summary_width
);
825 for (ref
= refs
; ref
; ref
= ref
->next
) {
826 if (ref
->status
!= REF_STATUS_NONE
&&
827 ref
->status
!= REF_STATUS_UPTODATE
&&
828 ref
->status
!= REF_STATUS_OK
)
829 n
+= print_one_push_status(ref
, dest
, n
,
830 porcelain
, summary_width
);
831 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
832 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
833 *reject_reasons
|= REJECT_NON_FF_HEAD
;
835 *reject_reasons
|= REJECT_NON_FF_OTHER
;
836 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
837 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
838 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
839 *reject_reasons
|= REJECT_FETCH_FIRST
;
840 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
841 *reject_reasons
|= REJECT_NEEDS_FORCE
;
842 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
843 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
849 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
851 struct git_transport_data
*data
= transport
->data
;
852 struct send_pack_args args
;
855 if (transport_color_config() < 0)
858 if (!data
->finished_handshake
)
859 get_refs_via_connect(transport
, 1, NULL
);
861 memset(&args
, 0, sizeof(args
));
862 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
863 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
864 args
.use_thin_pack
= data
->options
.thin
;
865 args
.verbose
= (transport
->verbose
> 0);
866 args
.quiet
= (transport
->verbose
< 0);
867 args
.progress
= transport
->progress
;
868 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
869 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
870 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
871 args
.push_options
= transport
->push_options
;
872 args
.url
= transport
->url
;
874 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
875 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
876 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
877 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
879 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
881 switch (data
->version
) {
883 die(_("support for protocol v2 not implemented yet"));
887 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
890 case protocol_unknown_version
:
891 BUG("unknown protocol version");
897 * Atomic push may abort the connection early and close the pipe,
898 * which may cause an error for `finish_connect()`. Ignore this error
899 * for atomic git-push.
901 if (ret
|| args
.atomic
)
902 finish_connect(data
->conn
);
904 ret
= finish_connect(data
->conn
);
906 data
->finished_handshake
= 0;
911 static int connect_git(struct transport
*transport
, const char *name
,
912 const char *executable
, int fd
[2])
914 struct git_transport_data
*data
= transport
->data
;
915 data
->conn
= git_connect(data
->fd
, transport
->url
,
922 static int disconnect_git(struct transport
*transport
)
924 struct git_transport_data
*data
= transport
->data
;
926 if (data
->finished_handshake
&& !transport
->stateless_rpc
)
927 packet_flush(data
->fd
[1]);
929 if (data
->fd
[1] >= 0)
931 finish_connect(data
->conn
);
934 list_objects_filter_release(&data
->options
.filter_options
);
939 static struct transport_vtable taken_over_vtable
= {
940 .get_refs_list
= get_refs_via_connect
,
941 .get_bundle_uri
= get_bundle_uri
,
942 .fetch_refs
= fetch_refs_via_pack
,
943 .push_refs
= git_transport_push
,
944 .disconnect
= disconnect_git
947 void transport_take_over(struct transport
*transport
,
948 struct child_process
*child
)
950 struct git_transport_data
*data
;
952 if (!transport
->smart_options
)
953 BUG("taking over transport requires non-NULL "
954 "smart_options field.");
956 CALLOC_ARRAY(data
, 1);
957 data
->options
= *transport
->smart_options
;
959 data
->fd
[0] = data
->conn
->out
;
960 data
->fd
[1] = data
->conn
->in
;
961 data
->finished_handshake
= 0;
962 transport
->data
= data
;
964 transport
->vtable
= &taken_over_vtable
;
965 transport
->smart_options
= &(data
->options
);
967 transport
->cannot_reuse
= 1;
970 static int is_file(const char *url
)
975 return S_ISREG(buf
.st_mode
);
978 static int external_specification_len(const char *url
)
980 return strchr(url
, ':') - url
;
983 static const struct string_list
*protocol_allow_list(void)
985 static int enabled
= -1;
986 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
989 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
991 string_list_split(&allowed
, v
, ':', -1);
992 string_list_sort(&allowed
);
999 return enabled
? &allowed
: NULL
;
1002 enum protocol_allow_config
{
1003 PROTOCOL_ALLOW_NEVER
= 0,
1004 PROTOCOL_ALLOW_USER_ONLY
,
1005 PROTOCOL_ALLOW_ALWAYS
1008 static enum protocol_allow_config
parse_protocol_config(const char *key
,
1011 if (!strcasecmp(value
, "always"))
1012 return PROTOCOL_ALLOW_ALWAYS
;
1013 else if (!strcasecmp(value
, "never"))
1014 return PROTOCOL_ALLOW_NEVER
;
1015 else if (!strcasecmp(value
, "user"))
1016 return PROTOCOL_ALLOW_USER_ONLY
;
1018 die(_("unknown value for config '%s': %s"), key
, value
);
1021 static enum protocol_allow_config
get_protocol_config(const char *type
)
1023 char *key
= xstrfmt("protocol.%s.allow", type
);
1026 /* first check the per-protocol config */
1027 if (!git_config_get_string(key
, &value
)) {
1028 enum protocol_allow_config ret
=
1029 parse_protocol_config(key
, value
);
1036 /* if defined, fallback to user-defined default for unknown protocols */
1037 if (!git_config_get_string("protocol.allow", &value
)) {
1038 enum protocol_allow_config ret
=
1039 parse_protocol_config("protocol.allow", value
);
1044 /* fallback to built-in defaults */
1046 if (!strcmp(type
, "http") ||
1047 !strcmp(type
, "https") ||
1048 !strcmp(type
, "git") ||
1049 !strcmp(type
, "ssh"))
1050 return PROTOCOL_ALLOW_ALWAYS
;
1052 /* known scary; err on the side of caution */
1053 if (!strcmp(type
, "ext"))
1054 return PROTOCOL_ALLOW_NEVER
;
1056 /* unknown; by default let them be used only directly by the user */
1057 return PROTOCOL_ALLOW_USER_ONLY
;
1060 int is_transport_allowed(const char *type
, int from_user
)
1062 const struct string_list
*allow_list
= protocol_allow_list();
1064 return string_list_has_string(allow_list
, type
);
1066 switch (get_protocol_config(type
)) {
1067 case PROTOCOL_ALLOW_ALWAYS
:
1069 case PROTOCOL_ALLOW_NEVER
:
1071 case PROTOCOL_ALLOW_USER_ONLY
:
1073 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1077 BUG("invalid protocol_allow_config type");
1080 void transport_check_allowed(const char *type
)
1082 if (!is_transport_allowed(type
, -1))
1083 die(_("transport '%s' not allowed"), type
);
1086 static struct transport_vtable bundle_vtable
= {
1087 .get_refs_list
= get_refs_from_bundle
,
1088 .fetch_refs
= fetch_refs_from_bundle
,
1089 .disconnect
= close_bundle
1092 static struct transport_vtable builtin_smart_vtable
= {
1093 .get_refs_list
= get_refs_via_connect
,
1094 .get_bundle_uri
= get_bundle_uri
,
1095 .fetch_refs
= fetch_refs_via_pack
,
1096 .push_refs
= git_transport_push
,
1097 .connect
= connect_git
,
1098 .disconnect
= disconnect_git
1101 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1104 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1106 ret
->progress
= isatty(2);
1107 string_list_init_dup(&ret
->pack_lockfiles
);
1109 CALLOC_ARRAY(ret
->bundles
, 1);
1110 init_bundle_list(ret
->bundles
);
1113 BUG("No remote provided to transport_get()");
1115 ret
->got_remote_refs
= 0;
1116 ret
->remote
= remote
;
1117 helper
= remote
->foreign_vcs
;
1119 if (!url
&& remote
->url
)
1120 url
= remote
->url
[0];
1123 /* maybe it is a foreign URL? */
1125 const char *p
= url
;
1127 while (is_urlschemechar(p
== url
, *p
))
1129 if (starts_with(p
, "::"))
1130 helper
= xstrndup(url
, p
- url
);
1134 transport_helper_init(ret
, helper
);
1135 } else if (starts_with(url
, "rsync:")) {
1136 die(_("git-over-rsync is no longer supported"));
1137 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1138 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1139 bundle_header_init(&data
->header
);
1140 transport_check_allowed("file");
1142 ret
->vtable
= &bundle_vtable
;
1143 ret
->smart_options
= NULL
;
1144 } else if (!is_url(url
)
1145 || starts_with(url
, "file://")
1146 || starts_with(url
, "git://")
1147 || starts_with(url
, "ssh://")
1148 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1149 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1152 * These are builtin smart transports; "allowed" transports
1153 * will be checked individually in git_connect.
1155 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1156 list_objects_filter_init(&data
->options
.filter_options
);
1158 ret
->vtable
= &builtin_smart_vtable
;
1159 ret
->smart_options
= &(data
->options
);
1162 data
->finished_handshake
= 0;
1164 /* Unknown protocol in URL. Pass to external handler. */
1165 int len
= external_specification_len(url
);
1166 char *handler
= xmemdupz(url
, len
);
1167 transport_helper_init(ret
, handler
);
1170 if (ret
->smart_options
) {
1171 ret
->smart_options
->thin
= 1;
1172 ret
->smart_options
->uploadpack
= "git-upload-pack";
1173 if (remote
->uploadpack
)
1174 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1175 ret
->smart_options
->receivepack
= "git-receive-pack";
1176 if (remote
->receivepack
)
1177 ret
->smart_options
->receivepack
= remote
->receivepack
;
1180 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1185 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1187 return transport
->hash_algo
;
1190 int transport_set_option(struct transport
*transport
,
1191 const char *name
, const char *value
)
1193 int git_reports
= 1, protocol_reports
= 1;
1195 if (transport
->smart_options
)
1196 git_reports
= set_git_option(transport
->smart_options
,
1199 if (transport
->vtable
->set_option
)
1200 protocol_reports
= transport
->vtable
->set_option(transport
,
1203 /* If either report is 0, report 0 (success). */
1204 if (!git_reports
|| !protocol_reports
)
1206 /* If either reports -1 (invalid value), report -1. */
1207 if ((git_reports
== -1) || (protocol_reports
== -1))
1209 /* Otherwise if both report unknown, report unknown. */
1213 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1217 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1219 transport
->verbose
= -1;
1222 * Rules used to determine whether to report progress (processing aborts
1223 * when a rule is satisfied):
1225 * . Report progress, if force_progress is 1 (ie. --progress).
1226 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1227 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1228 * . Report progress if isatty(2) is 1.
1230 if (force_progress
>= 0)
1231 transport
->progress
= !!force_progress
;
1233 transport
->progress
= verbosity
>= 0 && isatty(2);
1236 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1240 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1241 "not be found on any remote:\n"));
1242 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1243 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1244 fprintf(stderr
, _("\nPlease try\n\n"
1245 " git push --recurse-submodules=on-demand\n\n"
1246 "or cd to the path and use\n\n"
1248 "to push them to a remote.\n\n"));
1250 string_list_clear(needs_pushing
, 0);
1252 die(_("Aborting."));
1255 static int run_pre_push_hook(struct transport
*transport
,
1256 struct ref
*remote_refs
)
1260 struct child_process proc
= CHILD_PROCESS_INIT
;
1262 const char *hook_path
= find_hook("pre-push");
1267 strvec_push(&proc
.args
, hook_path
);
1268 strvec_push(&proc
.args
, transport
->remote
->name
);
1269 strvec_push(&proc
.args
, transport
->url
);
1272 proc
.trace2_hook_name
= "pre-push";
1274 if (start_command(&proc
)) {
1275 finish_command(&proc
);
1279 sigchain_push(SIGPIPE
, SIG_IGN
);
1281 strbuf_init(&buf
, 256);
1283 for (r
= remote_refs
; r
; r
= r
->next
) {
1284 if (!r
->peer_ref
) continue;
1285 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1286 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1287 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1288 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1291 strbuf_addf( &buf
, "%s %s %s %s\n",
1292 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1293 r
->name
, oid_to_hex(&r
->old_oid
));
1295 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1296 /* We do not mind if a hook does not read all refs. */
1303 strbuf_release(&buf
);
1309 sigchain_pop(SIGPIPE
);
1311 x
= finish_command(&proc
);
1318 int transport_push(struct repository
*r
,
1319 struct transport
*transport
,
1320 struct refspec
*rs
, int flags
,
1321 unsigned int *reject_reasons
)
1323 struct ref
*remote_refs
= NULL
;
1324 struct ref
*local_refs
= NULL
;
1325 int match_flags
= MATCH_REFS_NONE
;
1326 int verbose
= (transport
->verbose
> 0);
1327 int quiet
= (transport
->verbose
< 0);
1328 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1329 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1332 struct transport_ls_refs_options transport_options
=
1333 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1335 *reject_reasons
= 0;
1337 if (transport_color_config() < 0)
1340 if (!transport
->vtable
->push_refs
)
1343 local_refs
= get_local_heads();
1345 if (check_push_refs(local_refs
, rs
) < 0)
1348 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1350 trace2_region_enter("transport_push", "get_refs_list", r
);
1351 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1352 &transport_options
);
1353 trace2_region_leave("transport_push", "get_refs_list", r
);
1355 transport_ls_refs_options_release(&transport_options
);
1357 if (flags
& TRANSPORT_PUSH_ALL
)
1358 match_flags
|= MATCH_REFS_ALL
;
1359 if (flags
& TRANSPORT_PUSH_MIRROR
)
1360 match_flags
|= MATCH_REFS_MIRROR
;
1361 if (flags
& TRANSPORT_PUSH_PRUNE
)
1362 match_flags
|= MATCH_REFS_PRUNE
;
1363 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1364 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1366 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1369 if (transport
->smart_options
&&
1370 transport
->smart_options
->cas
&&
1371 !is_empty_cas(transport
->smart_options
->cas
))
1372 apply_push_cas(transport
->smart_options
->cas
,
1373 transport
->remote
, remote_refs
);
1375 set_ref_status_for_push(remote_refs
,
1376 flags
& TRANSPORT_PUSH_MIRROR
,
1377 flags
& TRANSPORT_PUSH_FORCE
);
1379 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1380 if (run_pre_push_hook(transport
, remote_refs
))
1383 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1384 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1385 !is_bare_repository()) {
1386 struct ref
*ref
= remote_refs
;
1387 struct oid_array commits
= OID_ARRAY_INIT
;
1389 trace2_region_enter("transport_push", "push_submodules", r
);
1390 for (; ref
; ref
= ref
->next
)
1391 if (!is_null_oid(&ref
->new_oid
))
1392 oid_array_append(&commits
,
1395 if (!push_unpushed_submodules(r
,
1399 transport
->push_options
,
1401 oid_array_clear(&commits
);
1402 trace2_region_leave("transport_push", "push_submodules", r
);
1403 die(_("failed to push all needed submodules"));
1405 oid_array_clear(&commits
);
1406 trace2_region_leave("transport_push", "push_submodules", r
);
1409 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1410 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1411 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1412 !pretend
)) && !is_bare_repository()) {
1413 struct ref
*ref
= remote_refs
;
1414 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1415 struct oid_array commits
= OID_ARRAY_INIT
;
1417 trace2_region_enter("transport_push", "check_submodules", r
);
1418 for (; ref
; ref
= ref
->next
)
1419 if (!is_null_oid(&ref
->new_oid
))
1420 oid_array_append(&commits
,
1423 if (find_unpushed_submodules(r
,
1425 transport
->remote
->name
,
1427 oid_array_clear(&commits
);
1428 trace2_region_leave("transport_push", "check_submodules", r
);
1429 die_with_unpushed_submodules(&needs_pushing
);
1431 string_list_clear(&needs_pushing
, 0);
1432 oid_array_clear(&commits
);
1433 trace2_region_leave("transport_push", "check_submodules", r
);
1436 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1437 trace2_region_enter("transport_push", "push_refs", r
);
1438 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1439 trace2_region_leave("transport_push", "push_refs", r
);
1442 err
= push_had_errors(remote_refs
);
1443 ret
= push_ret
| err
;
1446 transport_print_push_status(transport
->url
, remote_refs
,
1447 verbose
| porcelain
, porcelain
,
1450 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1451 set_upstreams(transport
, remote_refs
, pretend
);
1453 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1454 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1456 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1457 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1460 if (porcelain
&& !push_ret
)
1462 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1463 fprintf(stderr
, "Everything up-to-date\n");
1466 free_refs(local_refs
);
1467 free_refs(remote_refs
);
1471 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1472 struct transport_ls_refs_options
*transport_options
)
1474 if (!transport
->got_remote_refs
) {
1475 transport
->remote_refs
=
1476 transport
->vtable
->get_refs_list(transport
, 0,
1478 transport
->got_remote_refs
= 1;
1481 return transport
->remote_refs
;
1484 void transport_ls_refs_options_release(struct transport_ls_refs_options
*opts
)
1486 strvec_clear(&opts
->ref_prefixes
);
1487 free((char *)opts
->unborn_head_target
);
1490 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1493 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1494 struct ref
**heads
= NULL
;
1497 for (rm
= refs
; rm
; rm
= rm
->next
) {
1500 !is_null_oid(&rm
->old_oid
) &&
1501 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1503 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1504 heads
[nr_heads
++] = rm
;
1509 * When deepening of a shallow repository is requested,
1510 * then local and remote refs are likely to still be equal.
1511 * Just feed them all to the fetch method in that case.
1512 * This condition shouldn't be met in a non-deepening fetch
1513 * (see builtin/fetch.c:quickfetch()).
1515 ALLOC_ARRAY(heads
, nr_refs
);
1516 for (rm
= refs
; rm
; rm
= rm
->next
)
1517 heads
[nr_heads
++] = rm
;
1520 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1526 int transport_get_remote_bundle_uri(struct transport
*transport
)
1529 const struct transport_vtable
*vtable
= transport
->vtable
;
1531 /* Check config only once. */
1532 if (transport
->got_remote_bundle_uri
)
1534 transport
->got_remote_bundle_uri
= 1;
1537 * Don't request bundle-uri from the server unless configured to
1538 * do so by the transfer.bundleURI=true config option.
1540 if (git_config_get_bool("transfer.bundleuri", &value
) || !value
)
1543 if (!transport
->bundles
->baseURI
)
1544 transport
->bundles
->baseURI
= xstrdup(transport
->url
);
1546 if (!vtable
->get_bundle_uri
)
1547 return error(_("bundle-uri operation not supported by protocol"));
1549 if (vtable
->get_bundle_uri(transport
) < 0)
1550 return error(_("could not retrieve server-advertised bundle-uri list"));
1554 void transport_unlock_pack(struct transport
*transport
, unsigned int flags
)
1556 int in_signal_handler
= !!(flags
& TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER
);
1559 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1560 if (in_signal_handler
)
1561 unlink(transport
->pack_lockfiles
.items
[i
].string
);
1563 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1564 if (!in_signal_handler
)
1565 string_list_clear(&transport
->pack_lockfiles
, 0);
1568 int transport_connect(struct transport
*transport
, const char *name
,
1569 const char *exec
, int fd
[2])
1571 if (transport
->vtable
->connect
)
1572 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1574 die(_("operation not supported by protocol"));
1577 int transport_disconnect(struct transport
*transport
)
1580 if (transport
->vtable
->disconnect
)
1581 ret
= transport
->vtable
->disconnect(transport
);
1582 if (transport
->got_remote_refs
)
1583 free_refs((void *)transport
->remote_refs
);
1584 clear_bundle_list(transport
->bundles
);
1585 free(transport
->bundles
);
1591 * Strip username (and password) from a URL and return
1592 * it in a newly allocated string.
1594 char *transport_anonymize_url(const char *url
)
1596 char *scheme_prefix
, *anon_part
;
1597 size_t anon_len
, prefix_len
= 0;
1599 anon_part
= strchr(url
, '@');
1600 if (url_is_local_not_ssh(url
) || !anon_part
)
1603 anon_len
= strlen(++anon_part
);
1604 scheme_prefix
= strstr(url
, "://");
1605 if (!scheme_prefix
) {
1606 if (!strchr(anon_part
, ':'))
1607 /* cannot be "me@there:/path/name" */
1611 /* make sure scheme is reasonable */
1612 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1615 case '+': case '.': case '-':
1624 /* @ past the first slash does not count */
1625 cp
= strchr(scheme_prefix
+ 3, '/');
1626 if (cp
&& cp
< anon_part
)
1628 prefix_len
= scheme_prefix
- url
+ 3;
1630 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1631 (int)anon_len
, anon_part
);
1633 return xstrdup(url
);