1 #include "git-compat-util.h"
4 #include "environment.h"
9 #include "fetch-pack.h"
12 #include "send-pack.h"
21 #include "submodule.h"
22 #include "string-list.h"
23 #include "oid-array.h"
26 #include "transport-internal.h"
28 #include "object-name.h"
29 #include "object-store-ll.h"
31 #include "bundle-uri.h"
33 static int transport_use_color
= -1;
34 static char transport_colors
[][COLOR_MAXLEN
] = {
36 GIT_COLOR_RED
/* REJECTED */
39 enum color_transport
{
40 TRANSPORT_COLOR_RESET
= 0,
41 TRANSPORT_COLOR_REJECTED
= 1
44 static int transport_color_config(void)
46 const char *keys
[] = {
47 "color.transport.reset",
48 "color.transport.rejected"
49 }, *key
= "color.transport";
52 static int initialized
;
58 if (!git_config_get_string(key
, &value
))
59 transport_use_color
= git_config_colorbool(key
, value
);
61 if (!want_color_stderr(transport_use_color
))
64 for (i
= 0; i
< ARRAY_SIZE(keys
); i
++)
65 if (!git_config_get_string(keys
[i
], &value
)) {
67 return config_error_nonbool(keys
[i
]);
68 if (color_parse(value
, transport_colors
[i
]) < 0)
75 static const char *transport_get_color(enum color_transport ix
)
77 if (want_color_stderr(transport_use_color
))
78 return transport_colors
[ix
];
82 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
86 for (ref
= refs
; ref
; ref
= ref
->next
) {
87 const char *localname
;
89 const char *remotename
;
92 * Check suitability for tracking. Must be successful /
93 * already up-to-date ref create/modify (not delete).
95 if (ref
->status
!= REF_STATUS_OK
&&
96 ref
->status
!= REF_STATUS_UPTODATE
)
100 if (is_null_oid(&ref
->new_oid
))
103 /* Follow symbolic refs (mainly for HEAD). */
104 localname
= ref
->peer_ref
->name
;
105 remotename
= ref
->name
;
106 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
108 if (tmp
&& flag
& REF_ISSYMREF
&&
109 starts_with(tmp
, "refs/heads/"))
112 /* Both source and destination must be local branches. */
113 if (!localname
|| !starts_with(localname
, "refs/heads/"))
115 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
119 int flag
= transport
->verbose
< 0 ? 0 : BRANCH_CONFIG_VERBOSE
;
120 install_branch_config(flag
, localname
+ 11,
121 transport
->remote
->name
, remotename
);
122 } else if (transport
->verbose
>= 0)
123 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
124 localname
+ 11, remotename
+ 11,
125 transport
->remote
->name
);
129 struct bundle_transport_data
{
131 struct bundle_header header
;
132 unsigned get_refs_from_bundle_called
: 1;
135 static void get_refs_from_bundle_inner(struct transport
*transport
)
137 struct bundle_transport_data
*data
= transport
->data
;
139 data
->get_refs_from_bundle_called
= 1;
143 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
145 die(_("could not read bundle '%s'"), transport
->url
);
147 transport
->hash_algo
= data
->header
.hash_algo
;
150 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
152 struct transport_ls_refs_options
*transport_options UNUSED
)
154 struct bundle_transport_data
*data
= transport
->data
;
155 struct ref
*result
= NULL
;
161 get_refs_from_bundle_inner(transport
);
163 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
164 struct string_list_item
*e
= data
->header
.references
.items
+ i
;
165 const char *name
= e
->string
;
166 struct ref
*ref
= alloc_ref(name
);
167 struct object_id
*oid
= e
->util
;
168 oidcpy(&ref
->old_oid
, oid
);
175 static int fetch_refs_from_bundle(struct transport
*transport
,
177 struct ref
**to_fetch UNUSED
)
179 struct bundle_transport_data
*data
= transport
->data
;
180 struct strvec extra_index_pack_args
= STRVEC_INIT
;
183 if (transport
->progress
)
184 strvec_push(&extra_index_pack_args
, "-v");
186 if (!data
->get_refs_from_bundle_called
)
187 get_refs_from_bundle_inner(transport
);
188 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
189 &extra_index_pack_args
, 0);
190 transport
->hash_algo
= data
->header
.hash_algo
;
194 static int close_bundle(struct transport
*transport
)
196 struct bundle_transport_data
*data
= transport
->data
;
199 bundle_header_release(&data
->header
);
204 struct git_transport_data
{
205 struct git_transport_options options
;
206 struct child_process
*conn
;
208 unsigned finished_handshake
: 1;
209 enum protocol_version version
;
210 struct oid_array extra_have
;
211 struct oid_array shallow
;
214 static int set_git_option(struct git_transport_options
*opts
,
215 const char *name
, const char *value
)
217 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
218 opts
->uploadpack
= value
;
220 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
221 opts
->receivepack
= value
;
223 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
224 opts
->thin
= !!value
;
226 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
227 opts
->followtags
= !!value
;
229 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
230 opts
->keep
= !!value
;
232 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
233 opts
->update_shallow
= !!value
;
235 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
240 opts
->depth
= strtol(value
, &end
, 0);
242 die(_("transport: invalid depth option '%s'"), value
);
245 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
246 opts
->deepen_since
= value
;
248 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
249 opts
->deepen_not
= (const struct string_list
*)value
;
251 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
252 opts
->deepen_relative
= !!value
;
254 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
255 opts
->from_promisor
= !!value
;
257 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
258 list_objects_filter_die_if_populated(&opts
->filter_options
);
259 parse_list_objects_filter(&opts
->filter_options
, value
);
261 } else if (!strcmp(name
, TRANS_OPT_REFETCH
)) {
262 opts
->refetch
= !!value
;
264 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
265 opts
->reject_shallow
= !!value
;
271 static int connect_setup(struct transport
*transport
, int for_push
)
273 struct git_transport_data
*data
= transport
->data
;
274 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
279 switch (transport
->family
) {
280 case TRANSPORT_FAMILY_ALL
: break;
281 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
282 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
285 data
->conn
= git_connect(data
->fd
, transport
->url
,
290 data
->options
.receivepack
:
291 data
->options
.uploadpack
,
297 static void die_if_server_options(struct transport
*transport
)
299 if (!transport
->server_options
|| !transport
->server_options
->nr
)
301 advise(_("see protocol.version in 'git help config' for more details"));
302 die(_("server options require protocol version 2 or later"));
306 * Obtains the protocol version from the transport and writes it to
307 * transport->data->version, first connecting if not already connected.
309 * If the protocol version is one that allows skipping the listing of remote
310 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
311 * this function returns NULL. Otherwise, this function returns the list of
314 static struct ref
*handshake(struct transport
*transport
, int for_push
,
315 struct transport_ls_refs_options
*options
,
318 struct git_transport_data
*data
= transport
->data
;
319 struct ref
*refs
= NULL
;
320 struct packet_reader reader
;
322 const char *server_sid
;
324 connect_setup(transport
, for_push
);
326 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
327 PACKET_READ_CHOMP_NEWLINE
|
328 PACKET_READ_GENTLE_ON_EOF
|
329 PACKET_READ_DIE_ON_ERR_PACKET
);
331 data
->version
= discover_version(&reader
);
332 switch (data
->version
) {
334 if (server_feature_v2("session-id", &server_sid
))
335 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
337 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
339 transport
->server_options
,
340 transport
->stateless_rpc
);
344 die_if_server_options(transport
);
345 get_remote_heads(&reader
, &refs
,
346 for_push
? REF_NORMAL
: 0,
349 server_sid
= server_feature_value("session-id", &sid_len
);
351 char *sid
= xstrndup(server_sid
, sid_len
);
352 trace2_data_string("transfer", NULL
, "server-sid", sid
);
356 case protocol_unknown_version
:
357 BUG("unknown protocol version");
359 data
->finished_handshake
= 1;
360 transport
->hash_algo
= reader
.hash_algo
;
362 if (reader
.line_peeked
)
363 BUG("buffer must be empty at the end of handshake()");
368 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
369 struct transport_ls_refs_options
*options
)
371 return handshake(transport
, for_push
, options
, 1);
374 static int get_bundle_uri(struct transport
*transport
)
376 struct git_transport_data
*data
= transport
->data
;
377 struct packet_reader reader
;
378 int stateless_rpc
= transport
->stateless_rpc
;
380 if (!transport
->bundles
) {
381 CALLOC_ARRAY(transport
->bundles
, 1);
382 init_bundle_list(transport
->bundles
);
385 if (!data
->finished_handshake
) {
386 struct ref
*refs
= handshake(transport
, 0, NULL
, 0);
393 * "Support" protocol v0 and v2 without bundle-uri support by
394 * silently degrading to a NOOP.
396 if (!server_supports_v2("bundle-uri"))
399 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
400 PACKET_READ_CHOMP_NEWLINE
|
401 PACKET_READ_GENTLE_ON_EOF
);
403 return get_remote_bundle_uri(data
->fd
[1], &reader
,
404 transport
->bundles
, stateless_rpc
);
407 static int fetch_refs_via_pack(struct transport
*transport
,
408 int nr_heads
, struct ref
**to_fetch
)
411 struct git_transport_data
*data
= transport
->data
;
412 struct ref
*refs
= NULL
;
413 struct fetch_pack_args args
;
414 struct ref
*refs_tmp
= NULL
;
416 memset(&args
, 0, sizeof(args
));
417 args
.uploadpack
= data
->options
.uploadpack
;
418 args
.keep_pack
= data
->options
.keep
;
420 args
.use_thin_pack
= data
->options
.thin
;
421 args
.include_tag
= data
->options
.followtags
;
422 args
.verbose
= (transport
->verbose
> 1);
423 args
.quiet
= (transport
->verbose
< 0);
424 args
.no_progress
= !transport
->progress
;
425 args
.depth
= data
->options
.depth
;
426 args
.deepen_since
= data
->options
.deepen_since
;
427 args
.deepen_not
= data
->options
.deepen_not
;
428 args
.deepen_relative
= data
->options
.deepen_relative
;
429 args
.check_self_contained_and_connected
=
430 data
->options
.check_self_contained_and_connected
;
431 args
.cloning
= transport
->cloning
;
432 args
.update_shallow
= data
->options
.update_shallow
;
433 args
.from_promisor
= data
->options
.from_promisor
;
434 list_objects_filter_copy(&args
.filter_options
,
435 &data
->options
.filter_options
);
436 args
.refetch
= data
->options
.refetch
;
437 args
.stateless_rpc
= transport
->stateless_rpc
;
438 args
.server_options
= transport
->server_options
;
439 args
.negotiation_tips
= data
->options
.negotiation_tips
;
440 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
442 if (!data
->finished_handshake
) {
444 int must_list_refs
= 0;
445 for (i
= 0; i
< nr_heads
; i
++) {
446 if (!to_fetch
[i
]->exact_oid
) {
451 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
454 if (data
->version
== protocol_unknown_version
)
455 BUG("unknown protocol version");
456 else if (data
->version
<= protocol_v1
)
457 die_if_server_options(transport
);
459 if (data
->options
.acked_commits
) {
460 if (data
->version
< protocol_v2
) {
461 warning(_("--negotiate-only requires protocol v2"));
463 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
464 warning(_("server does not support wait-for-done"));
467 negotiate_using_fetch(data
->options
.negotiation_tips
,
468 transport
->server_options
,
469 transport
->stateless_rpc
,
471 data
->options
.acked_commits
);
477 refs
= fetch_pack(&args
, data
->fd
,
478 refs_tmp
? refs_tmp
: transport
->remote_refs
,
479 to_fetch
, nr_heads
, &data
->shallow
,
480 &transport
->pack_lockfiles
, data
->version
);
482 data
->finished_handshake
= 0;
483 data
->options
.self_contained_and_connected
=
484 args
.self_contained_and_connected
;
485 data
->options
.connectivity_checked
= args
.connectivity_checked
;
489 if (report_unmatched_refs(to_fetch
, nr_heads
))
494 if (data
->fd
[1] >= 0)
496 if (finish_connect(data
->conn
))
502 list_objects_filter_release(&args
.filter_options
);
506 static int push_had_errors(struct ref
*ref
)
508 for (; ref
; ref
= ref
->next
) {
509 switch (ref
->status
) {
510 case REF_STATUS_NONE
:
511 case REF_STATUS_UPTODATE
:
521 int transport_refs_pushed(struct ref
*ref
)
523 for (; ref
; ref
= ref
->next
) {
524 switch(ref
->status
) {
525 case REF_STATUS_NONE
:
526 case REF_STATUS_UPTODATE
:
535 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
536 struct object_id
*new_oid
, int deletion
,
539 struct refspec_item rs
;
541 memset(&rs
, 0, sizeof(rs
));
545 if (!remote_find_tracking(remote
, &rs
)) {
547 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
549 delete_ref(NULL
, rs
.dst
, NULL
, 0);
551 update_ref("update by push", rs
.dst
, new_oid
,
557 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
560 struct object_id
*new_oid
;
561 struct ref_push_report
*report
;
563 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
566 report
= ref
->report
;
568 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
569 ref
->deletion
, verbose
);
571 for (; report
; report
= report
->next
) {
572 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
573 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
574 update_one_tracking_ref(remote
, refname
, new_oid
,
575 is_null_oid(new_oid
), verbose
);
579 static void print_ref_status(char flag
, const char *summary
,
580 struct ref
*to
, struct ref
*from
, const char *msg
,
581 struct ref_push_report
*report
,
582 int porcelain
, int summary_width
)
586 if (report
&& report
->ref_name
)
587 to_name
= report
->ref_name
;
593 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
595 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
597 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
599 fprintf(stdout
, "%s\n", summary
);
601 const char *red
= "", *reset
= "";
602 if (push_had_errors(to
)) {
603 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
604 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
606 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
609 fprintf(stderr
, "%s -> %s",
610 prettify_refname(from
->name
),
611 prettify_refname(to_name
));
613 fputs(prettify_refname(to_name
), stderr
);
623 static void print_ok_ref_status(struct ref
*ref
,
624 struct ref_push_report
*report
,
625 int porcelain
, int summary_width
)
627 struct object_id
*old_oid
;
628 struct object_id
*new_oid
;
629 const char *ref_name
;
632 if (report
&& report
->old_oid
)
633 old_oid
= report
->old_oid
;
635 old_oid
= &ref
->old_oid
;
636 if (report
&& report
->new_oid
)
637 new_oid
= report
->new_oid
;
639 new_oid
= &ref
->new_oid
;
640 if (report
&& report
->forced_update
)
641 forced_update
= report
->forced_update
;
643 forced_update
= ref
->forced_update
;
644 if (report
&& report
->ref_name
)
645 ref_name
= report
->ref_name
;
647 ref_name
= ref
->name
;
650 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
651 report
, porcelain
, summary_width
);
652 else if (is_null_oid(old_oid
))
653 print_ref_status('*',
654 (starts_with(ref_name
, "refs/tags/")
656 : (starts_with(ref_name
, "refs/heads/")
658 : "[new reference]")),
659 ref
, ref
->peer_ref
, NULL
,
660 report
, porcelain
, summary_width
);
662 struct strbuf quickref
= STRBUF_INIT
;
666 strbuf_add_unique_abbrev(&quickref
, old_oid
,
669 strbuf_addstr(&quickref
, "...");
671 msg
= "forced update";
673 strbuf_addstr(&quickref
, "..");
677 strbuf_add_unique_abbrev(&quickref
, new_oid
,
680 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
681 report
, porcelain
, summary_width
);
682 strbuf_release(&quickref
);
686 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
687 struct ref_push_report
*report
,
688 int porcelain
, int summary_width
)
691 char *url
= transport_anonymize_url(dest
);
692 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
696 switch(ref
->status
) {
697 case REF_STATUS_NONE
:
698 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
699 report
, porcelain
, summary_width
);
701 case REF_STATUS_REJECT_NODELETE
:
702 print_ref_status('!', "[rejected]", ref
, NULL
,
703 "remote does not support deleting refs",
704 report
, porcelain
, summary_width
);
706 case REF_STATUS_UPTODATE
:
707 print_ref_status('=', "[up to date]", ref
,
709 report
, porcelain
, summary_width
);
711 case REF_STATUS_REJECT_NONFASTFORWARD
:
712 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
714 report
, porcelain
, summary_width
);
716 case REF_STATUS_REJECT_ALREADY_EXISTS
:
717 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
719 report
, porcelain
, summary_width
);
721 case REF_STATUS_REJECT_FETCH_FIRST
:
722 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
724 report
, porcelain
, summary_width
);
726 case REF_STATUS_REJECT_NEEDS_FORCE
:
727 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
729 report
, porcelain
, summary_width
);
731 case REF_STATUS_REJECT_STALE
:
732 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
734 report
, porcelain
, summary_width
);
736 case REF_STATUS_REJECT_REMOTE_UPDATED
:
737 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
738 "remote ref updated since checkout",
739 report
, porcelain
, summary_width
);
741 case REF_STATUS_REJECT_SHALLOW
:
742 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
743 "new shallow roots not allowed",
744 report
, porcelain
, summary_width
);
746 case REF_STATUS_REMOTE_REJECT
:
747 print_ref_status('!', "[remote rejected]", ref
,
748 ref
->deletion
? NULL
: ref
->peer_ref
,
750 report
, porcelain
, summary_width
);
752 case REF_STATUS_EXPECTING_REPORT
:
753 print_ref_status('!', "[remote failure]", ref
,
754 ref
->deletion
? NULL
: ref
->peer_ref
,
755 "remote failed to report status",
756 report
, porcelain
, summary_width
);
758 case REF_STATUS_ATOMIC_PUSH_FAILED
:
759 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
760 "atomic push failed",
761 report
, porcelain
, summary_width
);
764 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
771 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
772 int porcelain
, int summary_width
)
774 struct ref_push_report
*report
;
778 return print_one_push_report(ref
, dest
, count
,
779 NULL
, porcelain
, summary_width
);
781 for (report
= ref
->report
; report
; report
= report
->next
)
782 print_one_push_report(ref
, dest
, count
+ n
++,
783 report
, porcelain
, summary_width
);
787 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
789 char hex
[GIT_MAX_HEXSZ
+ 1];
790 int w
= repo_find_unique_abbrev_r(the_repository
, hex
, oid
,
793 return (w
< sofar
) ? sofar
: w
;
796 int transport_summary_width(const struct ref
*refs
)
800 for (; refs
; refs
= refs
->next
) {
801 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
802 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
805 maxw
= FALLBACK_DEFAULT_ABBREV
;
806 return (2 * maxw
+ 3);
809 void transport_print_push_status(const char *dest
, struct ref
*refs
,
810 int verbose
, int porcelain
, unsigned int *reject_reasons
)
815 int summary_width
= transport_summary_width(refs
);
817 if (transport_color_config() < 0)
818 warning(_("could not parse transport.color.* config"));
820 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
823 for (ref
= refs
; ref
; ref
= ref
->next
)
824 if (ref
->status
== REF_STATUS_UPTODATE
)
825 n
+= print_one_push_status(ref
, dest
, n
,
826 porcelain
, summary_width
);
829 for (ref
= refs
; ref
; ref
= ref
->next
)
830 if (ref
->status
== REF_STATUS_OK
)
831 n
+= print_one_push_status(ref
, dest
, n
,
832 porcelain
, summary_width
);
835 for (ref
= refs
; ref
; ref
= ref
->next
) {
836 if (ref
->status
!= REF_STATUS_NONE
&&
837 ref
->status
!= REF_STATUS_UPTODATE
&&
838 ref
->status
!= REF_STATUS_OK
)
839 n
+= print_one_push_status(ref
, dest
, n
,
840 porcelain
, summary_width
);
841 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
842 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
843 *reject_reasons
|= REJECT_NON_FF_HEAD
;
845 *reject_reasons
|= REJECT_NON_FF_OTHER
;
846 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
847 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
848 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
849 *reject_reasons
|= REJECT_FETCH_FIRST
;
850 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
851 *reject_reasons
|= REJECT_NEEDS_FORCE
;
852 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
853 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
859 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
861 struct git_transport_data
*data
= transport
->data
;
862 struct send_pack_args args
;
865 if (transport_color_config() < 0)
868 if (!data
->finished_handshake
)
869 get_refs_via_connect(transport
, 1, NULL
);
871 memset(&args
, 0, sizeof(args
));
872 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
873 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
874 args
.use_thin_pack
= data
->options
.thin
;
875 args
.verbose
= (transport
->verbose
> 0);
876 args
.quiet
= (transport
->verbose
< 0);
877 args
.progress
= transport
->progress
;
878 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
879 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
880 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
881 args
.push_options
= transport
->push_options
;
882 args
.url
= transport
->url
;
884 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
885 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
886 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
887 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
889 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
891 switch (data
->version
) {
893 die(_("support for protocol v2 not implemented yet"));
897 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
900 case protocol_unknown_version
:
901 BUG("unknown protocol version");
907 * Atomic push may abort the connection early and close the pipe,
908 * which may cause an error for `finish_connect()`. Ignore this error
909 * for atomic git-push.
911 if (ret
|| args
.atomic
)
912 finish_connect(data
->conn
);
914 ret
= finish_connect(data
->conn
);
916 data
->finished_handshake
= 0;
921 static int connect_git(struct transport
*transport
, const char *name
,
922 const char *executable
, int fd
[2])
924 struct git_transport_data
*data
= transport
->data
;
925 data
->conn
= git_connect(data
->fd
, transport
->url
,
926 name
, executable
, 0);
932 static int disconnect_git(struct transport
*transport
)
934 struct git_transport_data
*data
= transport
->data
;
936 if (data
->finished_handshake
&& !transport
->stateless_rpc
)
937 packet_flush(data
->fd
[1]);
939 if (data
->fd
[1] >= 0)
941 finish_connect(data
->conn
);
944 list_objects_filter_release(&data
->options
.filter_options
);
949 static struct transport_vtable taken_over_vtable
= {
950 .get_refs_list
= get_refs_via_connect
,
951 .get_bundle_uri
= get_bundle_uri
,
952 .fetch_refs
= fetch_refs_via_pack
,
953 .push_refs
= git_transport_push
,
954 .disconnect
= disconnect_git
957 void transport_take_over(struct transport
*transport
,
958 struct child_process
*child
)
960 struct git_transport_data
*data
;
962 if (!transport
->smart_options
)
963 BUG("taking over transport requires non-NULL "
964 "smart_options field.");
966 CALLOC_ARRAY(data
, 1);
967 data
->options
= *transport
->smart_options
;
969 data
->fd
[0] = data
->conn
->out
;
970 data
->fd
[1] = data
->conn
->in
;
971 data
->finished_handshake
= 0;
972 transport
->data
= data
;
974 transport
->vtable
= &taken_over_vtable
;
975 transport
->smart_options
= &(data
->options
);
977 transport
->cannot_reuse
= 1;
980 static int is_file(const char *url
)
985 return S_ISREG(buf
.st_mode
);
988 static int external_specification_len(const char *url
)
990 return strchr(url
, ':') - url
;
993 static const struct string_list
*protocol_allow_list(void)
995 static int enabled
= -1;
996 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
999 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
1001 string_list_split(&allowed
, v
, ':', -1);
1002 string_list_sort(&allowed
);
1009 return enabled
? &allowed
: NULL
;
1012 enum protocol_allow_config
{
1013 PROTOCOL_ALLOW_NEVER
= 0,
1014 PROTOCOL_ALLOW_USER_ONLY
,
1015 PROTOCOL_ALLOW_ALWAYS
1018 static enum protocol_allow_config
parse_protocol_config(const char *key
,
1021 if (!strcasecmp(value
, "always"))
1022 return PROTOCOL_ALLOW_ALWAYS
;
1023 else if (!strcasecmp(value
, "never"))
1024 return PROTOCOL_ALLOW_NEVER
;
1025 else if (!strcasecmp(value
, "user"))
1026 return PROTOCOL_ALLOW_USER_ONLY
;
1028 die(_("unknown value for config '%s': %s"), key
, value
);
1031 static enum protocol_allow_config
get_protocol_config(const char *type
)
1033 char *key
= xstrfmt("protocol.%s.allow", type
);
1036 /* first check the per-protocol config */
1037 if (!git_config_get_string(key
, &value
)) {
1038 enum protocol_allow_config ret
=
1039 parse_protocol_config(key
, value
);
1046 /* if defined, fallback to user-defined default for unknown protocols */
1047 if (!git_config_get_string("protocol.allow", &value
)) {
1048 enum protocol_allow_config ret
=
1049 parse_protocol_config("protocol.allow", value
);
1054 /* fallback to built-in defaults */
1056 if (!strcmp(type
, "http") ||
1057 !strcmp(type
, "https") ||
1058 !strcmp(type
, "git") ||
1059 !strcmp(type
, "ssh"))
1060 return PROTOCOL_ALLOW_ALWAYS
;
1062 /* known scary; err on the side of caution */
1063 if (!strcmp(type
, "ext"))
1064 return PROTOCOL_ALLOW_NEVER
;
1066 /* unknown; by default let them be used only directly by the user */
1067 return PROTOCOL_ALLOW_USER_ONLY
;
1070 int is_transport_allowed(const char *type
, int from_user
)
1072 const struct string_list
*allow_list
= protocol_allow_list();
1074 return string_list_has_string(allow_list
, type
);
1076 switch (get_protocol_config(type
)) {
1077 case PROTOCOL_ALLOW_ALWAYS
:
1079 case PROTOCOL_ALLOW_NEVER
:
1081 case PROTOCOL_ALLOW_USER_ONLY
:
1083 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1087 BUG("invalid protocol_allow_config type");
1090 void transport_check_allowed(const char *type
)
1092 if (!is_transport_allowed(type
, -1))
1093 die(_("transport '%s' not allowed"), type
);
1096 static struct transport_vtable bundle_vtable
= {
1097 .get_refs_list
= get_refs_from_bundle
,
1098 .fetch_refs
= fetch_refs_from_bundle
,
1099 .disconnect
= close_bundle
1102 static struct transport_vtable builtin_smart_vtable
= {
1103 .get_refs_list
= get_refs_via_connect
,
1104 .get_bundle_uri
= get_bundle_uri
,
1105 .fetch_refs
= fetch_refs_via_pack
,
1106 .push_refs
= git_transport_push
,
1107 .connect
= connect_git
,
1108 .disconnect
= disconnect_git
1111 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1114 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1116 ret
->progress
= isatty(2);
1117 string_list_init_dup(&ret
->pack_lockfiles
);
1119 CALLOC_ARRAY(ret
->bundles
, 1);
1120 init_bundle_list(ret
->bundles
);
1123 BUG("No remote provided to transport_get()");
1125 ret
->got_remote_refs
= 0;
1126 ret
->remote
= remote
;
1127 helper
= remote
->foreign_vcs
;
1129 if (!url
&& remote
->url
)
1130 url
= remote
->url
[0];
1133 /* maybe it is a foreign URL? */
1135 const char *p
= url
;
1137 while (is_urlschemechar(p
== url
, *p
))
1139 if (starts_with(p
, "::"))
1140 helper
= xstrndup(url
, p
- url
);
1144 transport_helper_init(ret
, helper
);
1145 } else if (starts_with(url
, "rsync:")) {
1146 die(_("git-over-rsync is no longer supported"));
1147 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1148 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1149 bundle_header_init(&data
->header
);
1150 transport_check_allowed("file");
1152 ret
->vtable
= &bundle_vtable
;
1153 ret
->smart_options
= NULL
;
1154 } else if (!is_url(url
)
1155 || starts_with(url
, "file://")
1156 || starts_with(url
, "git://")
1157 || starts_with(url
, "ssh://")
1158 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1159 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1162 * These are builtin smart transports; "allowed" transports
1163 * will be checked individually in git_connect.
1165 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1166 list_objects_filter_init(&data
->options
.filter_options
);
1168 ret
->vtable
= &builtin_smart_vtable
;
1169 ret
->smart_options
= &(data
->options
);
1172 data
->finished_handshake
= 0;
1174 /* Unknown protocol in URL. Pass to external handler. */
1175 int len
= external_specification_len(url
);
1176 char *handler
= xmemdupz(url
, len
);
1177 transport_helper_init(ret
, handler
);
1180 if (ret
->smart_options
) {
1181 ret
->smart_options
->thin
= 1;
1182 ret
->smart_options
->uploadpack
= "git-upload-pack";
1183 if (remote
->uploadpack
)
1184 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1185 ret
->smart_options
->receivepack
= "git-receive-pack";
1186 if (remote
->receivepack
)
1187 ret
->smart_options
->receivepack
= remote
->receivepack
;
1190 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1195 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1197 return transport
->hash_algo
;
1200 int transport_set_option(struct transport
*transport
,
1201 const char *name
, const char *value
)
1203 int git_reports
= 1, protocol_reports
= 1;
1205 if (transport
->smart_options
)
1206 git_reports
= set_git_option(transport
->smart_options
,
1209 if (transport
->vtable
->set_option
)
1210 protocol_reports
= transport
->vtable
->set_option(transport
,
1213 /* If either report is 0, report 0 (success). */
1214 if (!git_reports
|| !protocol_reports
)
1216 /* If either reports -1 (invalid value), report -1. */
1217 if ((git_reports
== -1) || (protocol_reports
== -1))
1219 /* Otherwise if both report unknown, report unknown. */
1223 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1227 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1229 transport
->verbose
= -1;
1232 * Rules used to determine whether to report progress (processing aborts
1233 * when a rule is satisfied):
1235 * . Report progress, if force_progress is 1 (ie. --progress).
1236 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1237 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1238 * . Report progress if isatty(2) is 1.
1240 if (force_progress
>= 0)
1241 transport
->progress
= !!force_progress
;
1243 transport
->progress
= verbosity
>= 0 && isatty(2);
1246 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1250 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1251 "not be found on any remote:\n"));
1252 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1253 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1254 fprintf(stderr
, _("\nPlease try\n\n"
1255 " git push --recurse-submodules=on-demand\n\n"
1256 "or cd to the path and use\n\n"
1258 "to push them to a remote.\n\n"));
1260 string_list_clear(needs_pushing
, 0);
1262 die(_("Aborting."));
1265 static int run_pre_push_hook(struct transport
*transport
,
1266 struct ref
*remote_refs
)
1270 struct child_process proc
= CHILD_PROCESS_INIT
;
1272 const char *hook_path
= find_hook("pre-push");
1277 strvec_push(&proc
.args
, hook_path
);
1278 strvec_push(&proc
.args
, transport
->remote
->name
);
1279 strvec_push(&proc
.args
, transport
->url
);
1282 proc
.trace2_hook_name
= "pre-push";
1284 if (start_command(&proc
)) {
1285 finish_command(&proc
);
1289 sigchain_push(SIGPIPE
, SIG_IGN
);
1291 strbuf_init(&buf
, 256);
1293 for (r
= remote_refs
; r
; r
= r
->next
) {
1294 if (!r
->peer_ref
) continue;
1295 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1296 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1297 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1298 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1301 strbuf_addf( &buf
, "%s %s %s %s\n",
1302 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1303 r
->name
, oid_to_hex(&r
->old_oid
));
1305 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1306 /* We do not mind if a hook does not read all refs. */
1313 strbuf_release(&buf
);
1319 sigchain_pop(SIGPIPE
);
1321 x
= finish_command(&proc
);
1328 int transport_push(struct repository
*r
,
1329 struct transport
*transport
,
1330 struct refspec
*rs
, int flags
,
1331 unsigned int *reject_reasons
)
1333 struct ref
*remote_refs
= NULL
;
1334 struct ref
*local_refs
= NULL
;
1335 int match_flags
= MATCH_REFS_NONE
;
1336 int verbose
= (transport
->verbose
> 0);
1337 int quiet
= (transport
->verbose
< 0);
1338 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1339 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1342 struct transport_ls_refs_options transport_options
=
1343 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1345 *reject_reasons
= 0;
1347 if (transport_color_config() < 0)
1350 if (!transport
->vtable
->push_refs
)
1353 local_refs
= get_local_heads();
1355 if (check_push_refs(local_refs
, rs
) < 0)
1358 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1360 trace2_region_enter("transport_push", "get_refs_list", r
);
1361 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1362 &transport_options
);
1363 trace2_region_leave("transport_push", "get_refs_list", r
);
1365 transport_ls_refs_options_release(&transport_options
);
1367 if (flags
& TRANSPORT_PUSH_ALL
)
1368 match_flags
|= MATCH_REFS_ALL
;
1369 if (flags
& TRANSPORT_PUSH_MIRROR
)
1370 match_flags
|= MATCH_REFS_MIRROR
;
1371 if (flags
& TRANSPORT_PUSH_PRUNE
)
1372 match_flags
|= MATCH_REFS_PRUNE
;
1373 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1374 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1376 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1379 if (transport
->smart_options
&&
1380 transport
->smart_options
->cas
&&
1381 !is_empty_cas(transport
->smart_options
->cas
))
1382 apply_push_cas(transport
->smart_options
->cas
,
1383 transport
->remote
, remote_refs
);
1385 set_ref_status_for_push(remote_refs
,
1386 flags
& TRANSPORT_PUSH_MIRROR
,
1387 flags
& TRANSPORT_PUSH_FORCE
);
1389 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1390 if (run_pre_push_hook(transport
, remote_refs
))
1393 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1394 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1395 !is_bare_repository()) {
1396 struct ref
*ref
= remote_refs
;
1397 struct oid_array commits
= OID_ARRAY_INIT
;
1399 trace2_region_enter("transport_push", "push_submodules", r
);
1400 for (; ref
; ref
= ref
->next
)
1401 if (!is_null_oid(&ref
->new_oid
))
1402 oid_array_append(&commits
,
1405 if (!push_unpushed_submodules(r
,
1409 transport
->push_options
,
1411 oid_array_clear(&commits
);
1412 trace2_region_leave("transport_push", "push_submodules", r
);
1413 die(_("failed to push all needed submodules"));
1415 oid_array_clear(&commits
);
1416 trace2_region_leave("transport_push", "push_submodules", r
);
1419 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1420 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1421 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1422 !pretend
)) && !is_bare_repository()) {
1423 struct ref
*ref
= remote_refs
;
1424 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1425 struct oid_array commits
= OID_ARRAY_INIT
;
1427 trace2_region_enter("transport_push", "check_submodules", r
);
1428 for (; ref
; ref
= ref
->next
)
1429 if (!is_null_oid(&ref
->new_oid
))
1430 oid_array_append(&commits
,
1433 if (find_unpushed_submodules(r
,
1435 transport
->remote
->name
,
1437 oid_array_clear(&commits
);
1438 trace2_region_leave("transport_push", "check_submodules", r
);
1439 die_with_unpushed_submodules(&needs_pushing
);
1441 string_list_clear(&needs_pushing
, 0);
1442 oid_array_clear(&commits
);
1443 trace2_region_leave("transport_push", "check_submodules", r
);
1446 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1447 trace2_region_enter("transport_push", "push_refs", r
);
1448 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1449 trace2_region_leave("transport_push", "push_refs", r
);
1452 err
= push_had_errors(remote_refs
);
1453 ret
= push_ret
| err
;
1456 transport_print_push_status(transport
->url
, remote_refs
,
1457 verbose
| porcelain
, porcelain
,
1460 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1461 set_upstreams(transport
, remote_refs
, pretend
);
1463 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1464 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1466 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1467 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1470 if (porcelain
&& !push_ret
)
1472 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1473 fprintf(stderr
, "Everything up-to-date\n");
1476 free_refs(local_refs
);
1477 free_refs(remote_refs
);
1481 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1482 struct transport_ls_refs_options
*transport_options
)
1484 if (!transport
->got_remote_refs
) {
1485 transport
->remote_refs
=
1486 transport
->vtable
->get_refs_list(transport
, 0,
1488 transport
->got_remote_refs
= 1;
1491 return transport
->remote_refs
;
1494 void transport_ls_refs_options_release(struct transport_ls_refs_options
*opts
)
1496 strvec_clear(&opts
->ref_prefixes
);
1497 free((char *)opts
->unborn_head_target
);
1500 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1503 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1504 struct ref
**heads
= NULL
;
1507 for (rm
= refs
; rm
; rm
= rm
->next
) {
1510 !is_null_oid(&rm
->old_oid
) &&
1511 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1513 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1514 heads
[nr_heads
++] = rm
;
1519 * When deepening of a shallow repository is requested,
1520 * then local and remote refs are likely to still be equal.
1521 * Just feed them all to the fetch method in that case.
1522 * This condition shouldn't be met in a non-deepening fetch
1523 * (see builtin/fetch.c:quickfetch()).
1525 ALLOC_ARRAY(heads
, nr_refs
);
1526 for (rm
= refs
; rm
; rm
= rm
->next
)
1527 heads
[nr_heads
++] = rm
;
1530 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1536 int transport_get_remote_bundle_uri(struct transport
*transport
)
1539 const struct transport_vtable
*vtable
= transport
->vtable
;
1541 /* Check config only once. */
1542 if (transport
->got_remote_bundle_uri
)
1544 transport
->got_remote_bundle_uri
= 1;
1547 * Don't request bundle-uri from the server unless configured to
1548 * do so by the transfer.bundleURI=true config option.
1550 if (git_config_get_bool("transfer.bundleuri", &value
) || !value
)
1553 if (!transport
->bundles
->baseURI
)
1554 transport
->bundles
->baseURI
= xstrdup(transport
->url
);
1556 if (!vtable
->get_bundle_uri
)
1557 return error(_("bundle-uri operation not supported by protocol"));
1559 if (vtable
->get_bundle_uri(transport
) < 0)
1560 return error(_("could not retrieve server-advertised bundle-uri list"));
1564 void transport_unlock_pack(struct transport
*transport
, unsigned int flags
)
1566 int in_signal_handler
= !!(flags
& TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER
);
1569 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1570 if (in_signal_handler
)
1571 unlink(transport
->pack_lockfiles
.items
[i
].string
);
1573 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1574 if (!in_signal_handler
)
1575 string_list_clear(&transport
->pack_lockfiles
, 0);
1578 int transport_connect(struct transport
*transport
, const char *name
,
1579 const char *exec
, int fd
[2])
1581 if (transport
->vtable
->connect
)
1582 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1584 die(_("operation not supported by protocol"));
1587 int transport_disconnect(struct transport
*transport
)
1590 if (transport
->vtable
->disconnect
)
1591 ret
= transport
->vtable
->disconnect(transport
);
1592 if (transport
->got_remote_refs
)
1593 free_refs((void *)transport
->remote_refs
);
1594 clear_bundle_list(transport
->bundles
);
1595 free(transport
->bundles
);
1601 * Strip username (and password) from a URL and return
1602 * it in a newly allocated string.
1604 char *transport_anonymize_url(const char *url
)
1606 char *scheme_prefix
, *anon_part
;
1607 size_t anon_len
, prefix_len
= 0;
1609 anon_part
= strchr(url
, '@');
1610 if (url_is_local_not_ssh(url
) || !anon_part
)
1613 anon_len
= strlen(++anon_part
);
1614 scheme_prefix
= strstr(url
, "://");
1615 if (!scheme_prefix
) {
1616 if (!strchr(anon_part
, ':'))
1617 /* cannot be "me@there:/path/name" */
1621 /* make sure scheme is reasonable */
1622 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1625 case '+': case '.': case '-':
1634 /* @ past the first slash does not count */
1635 cp
= strchr(scheme_prefix
+ 3, '/');
1636 if (cp
&& cp
< anon_part
)
1638 prefix_len
= scheme_prefix
- url
+ 3;
1640 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1641 (int)anon_len
, anon_part
);
1643 return xstrdup(url
);