1 #include "git-compat-util.h"
5 #include "environment.h"
10 #include "fetch-pack.h"
13 #include "send-pack.h"
22 #include "submodule.h"
23 #include "string-list.h"
24 #include "oid-array.h"
27 #include "transport-internal.h"
29 #include "object-name.h"
30 #include "object-store-ll.h"
32 #include "bundle-uri.h"
35 static int transport_use_color
= -1;
36 static char transport_colors
[][COLOR_MAXLEN
] = {
38 GIT_COLOR_RED
/* REJECTED */
41 enum color_transport
{
42 TRANSPORT_COLOR_RESET
= 0,
43 TRANSPORT_COLOR_REJECTED
= 1
46 static int transport_color_config(void)
48 const char *keys
[] = {
49 "color.transport.reset",
50 "color.transport.rejected"
51 }, *key
= "color.transport";
54 static int initialized
;
60 if (!git_config_get_string(key
, &value
))
61 transport_use_color
= git_config_colorbool(key
, value
);
63 if (!want_color_stderr(transport_use_color
))
66 for (i
= 0; i
< ARRAY_SIZE(keys
); i
++)
67 if (!git_config_get_string(keys
[i
], &value
)) {
69 return config_error_nonbool(keys
[i
]);
70 if (color_parse(value
, transport_colors
[i
]) < 0)
77 static const char *transport_get_color(enum color_transport ix
)
79 if (want_color_stderr(transport_use_color
))
80 return transport_colors
[ix
];
84 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
88 for (ref
= refs
; ref
; ref
= ref
->next
) {
89 const char *localname
;
91 const char *remotename
;
94 * Check suitability for tracking. Must be successful /
95 * already up-to-date ref create/modify (not delete).
97 if (ref
->status
!= REF_STATUS_OK
&&
98 ref
->status
!= REF_STATUS_UPTODATE
)
102 if (is_null_oid(&ref
->new_oid
))
105 /* Follow symbolic refs (mainly for HEAD). */
106 localname
= ref
->peer_ref
->name
;
107 remotename
= ref
->name
;
108 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
110 if (tmp
&& flag
& REF_ISSYMREF
&&
111 starts_with(tmp
, "refs/heads/"))
114 /* Both source and destination must be local branches. */
115 if (!localname
|| !starts_with(localname
, "refs/heads/"))
117 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
121 int flag
= transport
->verbose
< 0 ? 0 : BRANCH_CONFIG_VERBOSE
;
122 install_branch_config(flag
, localname
+ 11,
123 transport
->remote
->name
, remotename
);
124 } else if (transport
->verbose
>= 0)
125 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
126 localname
+ 11, remotename
+ 11,
127 transport
->remote
->name
);
131 struct bundle_transport_data
{
133 struct bundle_header header
;
134 unsigned get_refs_from_bundle_called
: 1;
137 static void get_refs_from_bundle_inner(struct transport
*transport
)
139 struct bundle_transport_data
*data
= transport
->data
;
141 data
->get_refs_from_bundle_called
= 1;
145 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
147 die(_("could not read bundle '%s'"), transport
->url
);
149 transport
->hash_algo
= data
->header
.hash_algo
;
152 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
154 struct transport_ls_refs_options
*transport_options UNUSED
)
156 struct bundle_transport_data
*data
= transport
->data
;
157 struct ref
*result
= NULL
;
163 get_refs_from_bundle_inner(transport
);
165 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
166 struct string_list_item
*e
= data
->header
.references
.items
+ i
;
167 const char *name
= e
->string
;
168 struct ref
*ref
= alloc_ref(name
);
169 struct object_id
*oid
= e
->util
;
170 oidcpy(&ref
->old_oid
, oid
);
177 static int fetch_refs_from_bundle(struct transport
*transport
,
179 struct ref
**to_fetch UNUSED
)
181 struct bundle_transport_data
*data
= transport
->data
;
182 struct strvec extra_index_pack_args
= STRVEC_INIT
;
185 if (transport
->progress
)
186 strvec_push(&extra_index_pack_args
, "-v");
188 if (!data
->get_refs_from_bundle_called
)
189 get_refs_from_bundle_inner(transport
);
190 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
191 &extra_index_pack_args
, 0);
192 transport
->hash_algo
= data
->header
.hash_algo
;
196 static int close_bundle(struct transport
*transport
)
198 struct bundle_transport_data
*data
= transport
->data
;
201 bundle_header_release(&data
->header
);
206 struct git_transport_data
{
207 struct git_transport_options options
;
208 struct child_process
*conn
;
210 unsigned finished_handshake
: 1;
211 enum protocol_version version
;
212 struct oid_array extra_have
;
213 struct oid_array shallow
;
216 static int set_git_option(struct git_transport_options
*opts
,
217 const char *name
, const char *value
)
219 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
220 opts
->uploadpack
= value
;
222 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
223 opts
->receivepack
= value
;
225 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
226 opts
->thin
= !!value
;
228 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
229 opts
->followtags
= !!value
;
231 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
232 opts
->keep
= !!value
;
234 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
235 opts
->update_shallow
= !!value
;
237 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
242 opts
->depth
= strtol(value
, &end
, 0);
244 die(_("transport: invalid depth option '%s'"), value
);
247 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
248 opts
->deepen_since
= value
;
250 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
251 opts
->deepen_not
= (const struct string_list
*)value
;
253 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
254 opts
->deepen_relative
= !!value
;
256 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
257 opts
->from_promisor
= !!value
;
259 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
260 list_objects_filter_die_if_populated(&opts
->filter_options
);
261 parse_list_objects_filter(&opts
->filter_options
, value
);
263 } else if (!strcmp(name
, TRANS_OPT_REFETCH
)) {
264 opts
->refetch
= !!value
;
266 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
267 opts
->reject_shallow
= !!value
;
273 static int connect_setup(struct transport
*transport
, int for_push
)
275 struct git_transport_data
*data
= transport
->data
;
276 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
281 switch (transport
->family
) {
282 case TRANSPORT_FAMILY_ALL
: break;
283 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
284 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
287 data
->conn
= git_connect(data
->fd
, transport
->url
,
292 data
->options
.receivepack
:
293 data
->options
.uploadpack
,
299 static void die_if_server_options(struct transport
*transport
)
301 if (!transport
->server_options
|| !transport
->server_options
->nr
)
303 advise(_("see protocol.version in 'git help config' for more details"));
304 die(_("server options require protocol version 2 or later"));
308 * Obtains the protocol version from the transport and writes it to
309 * transport->data->version, first connecting if not already connected.
311 * If the protocol version is one that allows skipping the listing of remote
312 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
313 * this function returns NULL. Otherwise, this function returns the list of
316 static struct ref
*handshake(struct transport
*transport
, int for_push
,
317 struct transport_ls_refs_options
*options
,
320 struct git_transport_data
*data
= transport
->data
;
321 struct ref
*refs
= NULL
;
322 struct packet_reader reader
;
324 const char *server_sid
;
326 connect_setup(transport
, for_push
);
328 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
329 PACKET_READ_CHOMP_NEWLINE
|
330 PACKET_READ_GENTLE_ON_EOF
|
331 PACKET_READ_DIE_ON_ERR_PACKET
);
333 data
->version
= discover_version(&reader
);
334 switch (data
->version
) {
336 if (server_feature_v2("session-id", &server_sid
))
337 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
339 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
341 transport
->server_options
,
342 transport
->stateless_rpc
);
346 die_if_server_options(transport
);
347 get_remote_heads(&reader
, &refs
,
348 for_push
? REF_NORMAL
: 0,
351 server_sid
= server_feature_value("session-id", &sid_len
);
353 char *sid
= xstrndup(server_sid
, sid_len
);
354 trace2_data_string("transfer", NULL
, "server-sid", sid
);
358 case protocol_unknown_version
:
359 BUG("unknown protocol version");
361 data
->finished_handshake
= 1;
362 transport
->hash_algo
= reader
.hash_algo
;
364 if (reader
.line_peeked
)
365 BUG("buffer must be empty at the end of handshake()");
370 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
371 struct transport_ls_refs_options
*options
)
373 return handshake(transport
, for_push
, options
, 1);
376 static int get_bundle_uri(struct transport
*transport
)
378 struct git_transport_data
*data
= transport
->data
;
379 struct packet_reader reader
;
380 int stateless_rpc
= transport
->stateless_rpc
;
382 if (!transport
->bundles
) {
383 CALLOC_ARRAY(transport
->bundles
, 1);
384 init_bundle_list(transport
->bundles
);
387 if (!data
->finished_handshake
) {
388 struct ref
*refs
= handshake(transport
, 0, NULL
, 0);
395 * "Support" protocol v0 and v2 without bundle-uri support by
396 * silently degrading to a NOOP.
398 if (!server_supports_v2("bundle-uri"))
401 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
402 PACKET_READ_CHOMP_NEWLINE
|
403 PACKET_READ_GENTLE_ON_EOF
);
405 return get_remote_bundle_uri(data
->fd
[1], &reader
,
406 transport
->bundles
, stateless_rpc
);
409 static int fetch_refs_via_pack(struct transport
*transport
,
410 int nr_heads
, struct ref
**to_fetch
)
413 struct git_transport_data
*data
= transport
->data
;
414 struct ref
*refs
= NULL
;
415 struct fetch_pack_args args
;
416 struct ref
*refs_tmp
= NULL
;
418 memset(&args
, 0, sizeof(args
));
419 args
.uploadpack
= data
->options
.uploadpack
;
420 args
.keep_pack
= data
->options
.keep
;
422 args
.use_thin_pack
= data
->options
.thin
;
423 args
.include_tag
= data
->options
.followtags
;
424 args
.verbose
= (transport
->verbose
> 1);
425 args
.quiet
= (transport
->verbose
< 0);
426 args
.no_progress
= !transport
->progress
;
427 args
.depth
= data
->options
.depth
;
428 args
.deepen_since
= data
->options
.deepen_since
;
429 args
.deepen_not
= data
->options
.deepen_not
;
430 args
.deepen_relative
= data
->options
.deepen_relative
;
431 args
.check_self_contained_and_connected
=
432 data
->options
.check_self_contained_and_connected
;
433 args
.cloning
= transport
->cloning
;
434 args
.update_shallow
= data
->options
.update_shallow
;
435 args
.from_promisor
= data
->options
.from_promisor
;
436 list_objects_filter_copy(&args
.filter_options
,
437 &data
->options
.filter_options
);
438 args
.refetch
= data
->options
.refetch
;
439 args
.stateless_rpc
= transport
->stateless_rpc
;
440 args
.server_options
= transport
->server_options
;
441 args
.negotiation_tips
= data
->options
.negotiation_tips
;
442 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
444 if (!data
->finished_handshake
) {
446 int must_list_refs
= 0;
447 for (i
= 0; i
< nr_heads
; i
++) {
448 if (!to_fetch
[i
]->exact_oid
) {
453 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
456 if (data
->version
== protocol_unknown_version
)
457 BUG("unknown protocol version");
458 else if (data
->version
<= protocol_v1
)
459 die_if_server_options(transport
);
461 if (data
->options
.acked_commits
) {
462 if (data
->version
< protocol_v2
) {
463 warning(_("--negotiate-only requires protocol v2"));
465 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
466 warning(_("server does not support wait-for-done"));
469 negotiate_using_fetch(data
->options
.negotiation_tips
,
470 transport
->server_options
,
471 transport
->stateless_rpc
,
473 data
->options
.acked_commits
);
479 refs
= fetch_pack(&args
, data
->fd
,
480 refs_tmp
? refs_tmp
: transport
->remote_refs
,
481 to_fetch
, nr_heads
, &data
->shallow
,
482 &transport
->pack_lockfiles
, data
->version
);
484 data
->finished_handshake
= 0;
485 data
->options
.self_contained_and_connected
=
486 args
.self_contained_and_connected
;
487 data
->options
.connectivity_checked
= args
.connectivity_checked
;
491 if (report_unmatched_refs(to_fetch
, nr_heads
))
496 if (data
->fd
[1] >= 0)
498 if (finish_connect(data
->conn
))
504 list_objects_filter_release(&args
.filter_options
);
508 static int push_had_errors(struct ref
*ref
)
510 for (; ref
; ref
= ref
->next
) {
511 switch (ref
->status
) {
512 case REF_STATUS_NONE
:
513 case REF_STATUS_UPTODATE
:
523 int transport_refs_pushed(struct ref
*ref
)
525 for (; ref
; ref
= ref
->next
) {
526 switch(ref
->status
) {
527 case REF_STATUS_NONE
:
528 case REF_STATUS_UPTODATE
:
537 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
538 struct object_id
*new_oid
, int deletion
,
541 struct refspec_item rs
;
543 memset(&rs
, 0, sizeof(rs
));
547 if (!remote_find_tracking(remote
, &rs
)) {
549 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
551 delete_ref(NULL
, rs
.dst
, NULL
, 0);
553 update_ref("update by push", rs
.dst
, new_oid
,
559 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
562 struct object_id
*new_oid
;
563 struct ref_push_report
*report
;
565 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
568 report
= ref
->report
;
570 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
571 ref
->deletion
, verbose
);
573 for (; report
; report
= report
->next
) {
574 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
575 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
576 update_one_tracking_ref(remote
, refname
, new_oid
,
577 is_null_oid(new_oid
), verbose
);
581 static void print_ref_status(char flag
, const char *summary
,
582 struct ref
*to
, struct ref
*from
, const char *msg
,
583 struct ref_push_report
*report
,
584 int porcelain
, int summary_width
)
588 if (report
&& report
->ref_name
)
589 to_name
= report
->ref_name
;
595 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
597 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
599 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
601 fprintf(stdout
, "%s\n", summary
);
603 const char *red
= "", *reset
= "";
604 if (push_had_errors(to
)) {
605 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
606 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
608 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
611 fprintf(stderr
, "%s -> %s",
612 prettify_refname(from
->name
),
613 prettify_refname(to_name
));
615 fputs(prettify_refname(to_name
), stderr
);
625 static void print_ok_ref_status(struct ref
*ref
,
626 struct ref_push_report
*report
,
627 int porcelain
, int summary_width
)
629 struct object_id
*old_oid
;
630 struct object_id
*new_oid
;
631 const char *ref_name
;
634 if (report
&& report
->old_oid
)
635 old_oid
= report
->old_oid
;
637 old_oid
= &ref
->old_oid
;
638 if (report
&& report
->new_oid
)
639 new_oid
= report
->new_oid
;
641 new_oid
= &ref
->new_oid
;
642 if (report
&& report
->forced_update
)
643 forced_update
= report
->forced_update
;
645 forced_update
= ref
->forced_update
;
646 if (report
&& report
->ref_name
)
647 ref_name
= report
->ref_name
;
649 ref_name
= ref
->name
;
652 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
653 report
, porcelain
, summary_width
);
654 else if (is_null_oid(old_oid
))
655 print_ref_status('*',
656 (starts_with(ref_name
, "refs/tags/")
658 : (starts_with(ref_name
, "refs/heads/")
660 : "[new reference]")),
661 ref
, ref
->peer_ref
, NULL
,
662 report
, porcelain
, summary_width
);
664 struct strbuf quickref
= STRBUF_INIT
;
668 strbuf_add_unique_abbrev(&quickref
, old_oid
,
671 strbuf_addstr(&quickref
, "...");
673 msg
= "forced update";
675 strbuf_addstr(&quickref
, "..");
679 strbuf_add_unique_abbrev(&quickref
, new_oid
,
682 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
683 report
, porcelain
, summary_width
);
684 strbuf_release(&quickref
);
688 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
689 struct ref_push_report
*report
,
690 int porcelain
, int summary_width
)
693 char *url
= transport_anonymize_url(dest
);
694 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
698 switch(ref
->status
) {
699 case REF_STATUS_NONE
:
700 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
701 report
, porcelain
, summary_width
);
703 case REF_STATUS_REJECT_NODELETE
:
704 print_ref_status('!', "[rejected]", ref
, NULL
,
705 "remote does not support deleting refs",
706 report
, porcelain
, summary_width
);
708 case REF_STATUS_UPTODATE
:
709 print_ref_status('=', "[up to date]", ref
,
711 report
, porcelain
, summary_width
);
713 case REF_STATUS_REJECT_NONFASTFORWARD
:
714 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
716 report
, porcelain
, summary_width
);
718 case REF_STATUS_REJECT_ALREADY_EXISTS
:
719 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
721 report
, porcelain
, summary_width
);
723 case REF_STATUS_REJECT_FETCH_FIRST
:
724 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
726 report
, porcelain
, summary_width
);
728 case REF_STATUS_REJECT_NEEDS_FORCE
:
729 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
731 report
, porcelain
, summary_width
);
733 case REF_STATUS_REJECT_STALE
:
734 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
736 report
, porcelain
, summary_width
);
738 case REF_STATUS_REJECT_REMOTE_UPDATED
:
739 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
740 "remote ref updated since checkout",
741 report
, porcelain
, summary_width
);
743 case REF_STATUS_REJECT_SHALLOW
:
744 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
745 "new shallow roots not allowed",
746 report
, porcelain
, summary_width
);
748 case REF_STATUS_REMOTE_REJECT
:
749 print_ref_status('!', "[remote rejected]", ref
,
750 ref
->deletion
? NULL
: ref
->peer_ref
,
752 report
, porcelain
, summary_width
);
754 case REF_STATUS_EXPECTING_REPORT
:
755 print_ref_status('!', "[remote failure]", ref
,
756 ref
->deletion
? NULL
: ref
->peer_ref
,
757 "remote failed to report status",
758 report
, porcelain
, summary_width
);
760 case REF_STATUS_ATOMIC_PUSH_FAILED
:
761 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
762 "atomic push failed",
763 report
, porcelain
, summary_width
);
766 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
773 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
774 int porcelain
, int summary_width
)
776 struct ref_push_report
*report
;
780 return print_one_push_report(ref
, dest
, count
,
781 NULL
, porcelain
, summary_width
);
783 for (report
= ref
->report
; report
; report
= report
->next
)
784 print_one_push_report(ref
, dest
, count
+ n
++,
785 report
, porcelain
, summary_width
);
789 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
791 char hex
[GIT_MAX_HEXSZ
+ 1];
792 int w
= repo_find_unique_abbrev_r(the_repository
, hex
, oid
,
795 return (w
< sofar
) ? sofar
: w
;
798 int transport_summary_width(const struct ref
*refs
)
802 for (; refs
; refs
= refs
->next
) {
803 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
804 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
807 maxw
= FALLBACK_DEFAULT_ABBREV
;
808 return (2 * maxw
+ 3);
811 void transport_print_push_status(const char *dest
, struct ref
*refs
,
812 int verbose
, int porcelain
, unsigned int *reject_reasons
)
817 int summary_width
= transport_summary_width(refs
);
819 if (transport_color_config() < 0)
820 warning(_("could not parse transport.color.* config"));
822 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
825 for (ref
= refs
; ref
; ref
= ref
->next
)
826 if (ref
->status
== REF_STATUS_UPTODATE
)
827 n
+= print_one_push_status(ref
, dest
, n
,
828 porcelain
, summary_width
);
831 for (ref
= refs
; ref
; ref
= ref
->next
)
832 if (ref
->status
== REF_STATUS_OK
)
833 n
+= print_one_push_status(ref
, dest
, n
,
834 porcelain
, summary_width
);
837 for (ref
= refs
; ref
; ref
= ref
->next
) {
838 if (ref
->status
!= REF_STATUS_NONE
&&
839 ref
->status
!= REF_STATUS_UPTODATE
&&
840 ref
->status
!= REF_STATUS_OK
)
841 n
+= print_one_push_status(ref
, dest
, n
,
842 porcelain
, summary_width
);
843 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
844 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
845 *reject_reasons
|= REJECT_NON_FF_HEAD
;
847 *reject_reasons
|= REJECT_NON_FF_OTHER
;
848 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
849 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
850 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
851 *reject_reasons
|= REJECT_FETCH_FIRST
;
852 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
853 *reject_reasons
|= REJECT_NEEDS_FORCE
;
854 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
855 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
861 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
863 struct git_transport_data
*data
= transport
->data
;
864 struct send_pack_args args
;
867 if (transport_color_config() < 0)
870 if (!data
->finished_handshake
)
871 get_refs_via_connect(transport
, 1, NULL
);
873 memset(&args
, 0, sizeof(args
));
874 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
875 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
876 args
.use_thin_pack
= data
->options
.thin
;
877 args
.verbose
= (transport
->verbose
> 0);
878 args
.quiet
= (transport
->verbose
< 0);
879 args
.progress
= transport
->progress
;
880 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
881 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
882 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
883 args
.push_options
= transport
->push_options
;
884 args
.url
= transport
->url
;
886 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
887 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
888 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
889 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
891 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
893 switch (data
->version
) {
895 die(_("support for protocol v2 not implemented yet"));
899 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
902 case protocol_unknown_version
:
903 BUG("unknown protocol version");
909 * Atomic push may abort the connection early and close the pipe,
910 * which may cause an error for `finish_connect()`. Ignore this error
911 * for atomic git-push.
913 if (ret
|| args
.atomic
)
914 finish_connect(data
->conn
);
916 ret
= finish_connect(data
->conn
);
918 data
->finished_handshake
= 0;
923 static int connect_git(struct transport
*transport
, const char *name
,
924 const char *executable
, int fd
[2])
926 struct git_transport_data
*data
= transport
->data
;
927 data
->conn
= git_connect(data
->fd
, transport
->url
,
928 name
, executable
, 0);
934 static int disconnect_git(struct transport
*transport
)
936 struct git_transport_data
*data
= transport
->data
;
938 if (data
->finished_handshake
&& !transport
->stateless_rpc
)
939 packet_flush(data
->fd
[1]);
941 if (data
->fd
[1] >= 0)
943 finish_connect(data
->conn
);
946 list_objects_filter_release(&data
->options
.filter_options
);
951 static struct transport_vtable taken_over_vtable
= {
952 .get_refs_list
= get_refs_via_connect
,
953 .get_bundle_uri
= get_bundle_uri
,
954 .fetch_refs
= fetch_refs_via_pack
,
955 .push_refs
= git_transport_push
,
956 .disconnect
= disconnect_git
959 void transport_take_over(struct transport
*transport
,
960 struct child_process
*child
)
962 struct git_transport_data
*data
;
964 if (!transport
->smart_options
)
965 BUG("taking over transport requires non-NULL "
966 "smart_options field.");
968 CALLOC_ARRAY(data
, 1);
969 data
->options
= *transport
->smart_options
;
971 data
->fd
[0] = data
->conn
->out
;
972 data
->fd
[1] = data
->conn
->in
;
973 data
->finished_handshake
= 0;
974 transport
->data
= data
;
976 transport
->vtable
= &taken_over_vtable
;
977 transport
->smart_options
= &(data
->options
);
979 transport
->cannot_reuse
= 1;
982 static int is_file(const char *url
)
987 return S_ISREG(buf
.st_mode
);
990 static int external_specification_len(const char *url
)
992 return strchr(url
, ':') - url
;
995 static const struct string_list
*protocol_allow_list(void)
997 static int enabled
= -1;
998 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
1001 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
1003 string_list_split(&allowed
, v
, ':', -1);
1004 string_list_sort(&allowed
);
1011 return enabled
? &allowed
: NULL
;
1014 enum protocol_allow_config
{
1015 PROTOCOL_ALLOW_NEVER
= 0,
1016 PROTOCOL_ALLOW_USER_ONLY
,
1017 PROTOCOL_ALLOW_ALWAYS
1020 static enum protocol_allow_config
parse_protocol_config(const char *key
,
1023 if (!strcasecmp(value
, "always"))
1024 return PROTOCOL_ALLOW_ALWAYS
;
1025 else if (!strcasecmp(value
, "never"))
1026 return PROTOCOL_ALLOW_NEVER
;
1027 else if (!strcasecmp(value
, "user"))
1028 return PROTOCOL_ALLOW_USER_ONLY
;
1030 die(_("unknown value for config '%s': %s"), key
, value
);
1033 static enum protocol_allow_config
get_protocol_config(const char *type
)
1035 char *key
= xstrfmt("protocol.%s.allow", type
);
1038 /* first check the per-protocol config */
1039 if (!git_config_get_string(key
, &value
)) {
1040 enum protocol_allow_config ret
=
1041 parse_protocol_config(key
, value
);
1048 /* if defined, fallback to user-defined default for unknown protocols */
1049 if (!git_config_get_string("protocol.allow", &value
)) {
1050 enum protocol_allow_config ret
=
1051 parse_protocol_config("protocol.allow", value
);
1056 /* fallback to built-in defaults */
1058 if (!strcmp(type
, "http") ||
1059 !strcmp(type
, "https") ||
1060 !strcmp(type
, "git") ||
1061 !strcmp(type
, "ssh"))
1062 return PROTOCOL_ALLOW_ALWAYS
;
1064 /* known scary; err on the side of caution */
1065 if (!strcmp(type
, "ext"))
1066 return PROTOCOL_ALLOW_NEVER
;
1068 /* unknown; by default let them be used only directly by the user */
1069 return PROTOCOL_ALLOW_USER_ONLY
;
1072 int is_transport_allowed(const char *type
, int from_user
)
1074 const struct string_list
*allow_list
= protocol_allow_list();
1076 return string_list_has_string(allow_list
, type
);
1078 switch (get_protocol_config(type
)) {
1079 case PROTOCOL_ALLOW_ALWAYS
:
1081 case PROTOCOL_ALLOW_NEVER
:
1083 case PROTOCOL_ALLOW_USER_ONLY
:
1085 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1089 BUG("invalid protocol_allow_config type");
1092 void transport_check_allowed(const char *type
)
1094 if (!is_transport_allowed(type
, -1))
1095 die(_("transport '%s' not allowed"), type
);
1098 static struct transport_vtable bundle_vtable
= {
1099 .get_refs_list
= get_refs_from_bundle
,
1100 .fetch_refs
= fetch_refs_from_bundle
,
1101 .disconnect
= close_bundle
1104 static struct transport_vtable builtin_smart_vtable
= {
1105 .get_refs_list
= get_refs_via_connect
,
1106 .get_bundle_uri
= get_bundle_uri
,
1107 .fetch_refs
= fetch_refs_via_pack
,
1108 .push_refs
= git_transport_push
,
1109 .connect
= connect_git
,
1110 .disconnect
= disconnect_git
1113 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1116 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1118 ret
->progress
= isatty(2);
1119 string_list_init_dup(&ret
->pack_lockfiles
);
1121 CALLOC_ARRAY(ret
->bundles
, 1);
1122 init_bundle_list(ret
->bundles
);
1125 BUG("No remote provided to transport_get()");
1127 ret
->got_remote_refs
= 0;
1128 ret
->remote
= remote
;
1129 helper
= remote
->foreign_vcs
;
1131 if (!url
&& remote
->url
)
1132 url
= remote
->url
[0];
1135 /* maybe it is a foreign URL? */
1137 const char *p
= url
;
1139 while (is_urlschemechar(p
== url
, *p
))
1141 if (starts_with(p
, "::"))
1142 helper
= xstrndup(url
, p
- url
);
1146 transport_helper_init(ret
, helper
);
1147 } else if (starts_with(url
, "rsync:")) {
1148 die(_("git-over-rsync is no longer supported"));
1149 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1150 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1151 bundle_header_init(&data
->header
);
1152 transport_check_allowed("file");
1154 ret
->vtable
= &bundle_vtable
;
1155 ret
->smart_options
= NULL
;
1156 } else if (!is_url(url
)
1157 || starts_with(url
, "file://")
1158 || starts_with(url
, "git://")
1159 || starts_with(url
, "ssh://")
1160 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1161 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1164 * These are builtin smart transports; "allowed" transports
1165 * will be checked individually in git_connect.
1167 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1168 list_objects_filter_init(&data
->options
.filter_options
);
1170 ret
->vtable
= &builtin_smart_vtable
;
1171 ret
->smart_options
= &(data
->options
);
1174 data
->finished_handshake
= 0;
1176 /* Unknown protocol in URL. Pass to external handler. */
1177 int len
= external_specification_len(url
);
1178 char *handler
= xmemdupz(url
, len
);
1179 transport_helper_init(ret
, handler
);
1182 if (ret
->smart_options
) {
1183 ret
->smart_options
->thin
= 1;
1184 ret
->smart_options
->uploadpack
= "git-upload-pack";
1185 if (remote
->uploadpack
)
1186 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1187 ret
->smart_options
->receivepack
= "git-receive-pack";
1188 if (remote
->receivepack
)
1189 ret
->smart_options
->receivepack
= remote
->receivepack
;
1192 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1197 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1199 return transport
->hash_algo
;
1202 int transport_set_option(struct transport
*transport
,
1203 const char *name
, const char *value
)
1205 int git_reports
= 1, protocol_reports
= 1;
1207 if (transport
->smart_options
)
1208 git_reports
= set_git_option(transport
->smart_options
,
1211 if (transport
->vtable
->set_option
)
1212 protocol_reports
= transport
->vtable
->set_option(transport
,
1215 /* If either report is 0, report 0 (success). */
1216 if (!git_reports
|| !protocol_reports
)
1218 /* If either reports -1 (invalid value), report -1. */
1219 if ((git_reports
== -1) || (protocol_reports
== -1))
1221 /* Otherwise if both report unknown, report unknown. */
1225 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1229 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1231 transport
->verbose
= -1;
1234 * Rules used to determine whether to report progress (processing aborts
1235 * when a rule is satisfied):
1237 * . Report progress, if force_progress is 1 (ie. --progress).
1238 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1239 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1240 * . Report progress if isatty(2) is 1.
1242 if (force_progress
>= 0)
1243 transport
->progress
= !!force_progress
;
1245 transport
->progress
= verbosity
>= 0 && isatty(2);
1248 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1252 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1253 "not be found on any remote:\n"));
1254 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1255 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1256 fprintf(stderr
, _("\nPlease try\n\n"
1257 " git push --recurse-submodules=on-demand\n\n"
1258 "or cd to the path and use\n\n"
1260 "to push them to a remote.\n\n"));
1262 string_list_clear(needs_pushing
, 0);
1264 die(_("Aborting."));
1267 static int run_pre_push_hook(struct transport
*transport
,
1268 struct ref
*remote_refs
)
1272 struct child_process proc
= CHILD_PROCESS_INIT
;
1274 const char *hook_path
= find_hook("pre-push");
1279 strvec_push(&proc
.args
, hook_path
);
1280 strvec_push(&proc
.args
, transport
->remote
->name
);
1281 strvec_push(&proc
.args
, transport
->url
);
1284 proc
.trace2_hook_name
= "pre-push";
1286 if (start_command(&proc
)) {
1287 finish_command(&proc
);
1291 sigchain_push(SIGPIPE
, SIG_IGN
);
1293 strbuf_init(&buf
, 256);
1295 for (r
= remote_refs
; r
; r
= r
->next
) {
1296 if (!r
->peer_ref
) continue;
1297 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1298 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1299 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1300 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1303 strbuf_addf( &buf
, "%s %s %s %s\n",
1304 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1305 r
->name
, oid_to_hex(&r
->old_oid
));
1307 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1308 /* We do not mind if a hook does not read all refs. */
1315 strbuf_release(&buf
);
1321 sigchain_pop(SIGPIPE
);
1323 x
= finish_command(&proc
);
1330 int transport_push(struct repository
*r
,
1331 struct transport
*transport
,
1332 struct refspec
*rs
, int flags
,
1333 unsigned int *reject_reasons
)
1335 struct ref
*remote_refs
= NULL
;
1336 struct ref
*local_refs
= NULL
;
1337 int match_flags
= MATCH_REFS_NONE
;
1338 int verbose
= (transport
->verbose
> 0);
1339 int quiet
= (transport
->verbose
< 0);
1340 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1341 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1344 struct transport_ls_refs_options transport_options
=
1345 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1347 *reject_reasons
= 0;
1349 if (transport_color_config() < 0)
1352 if (!transport
->vtable
->push_refs
)
1355 local_refs
= get_local_heads();
1357 if (check_push_refs(local_refs
, rs
) < 0)
1360 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1362 trace2_region_enter("transport_push", "get_refs_list", r
);
1363 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1364 &transport_options
);
1365 trace2_region_leave("transport_push", "get_refs_list", r
);
1367 transport_ls_refs_options_release(&transport_options
);
1369 if (flags
& TRANSPORT_PUSH_ALL
)
1370 match_flags
|= MATCH_REFS_ALL
;
1371 if (flags
& TRANSPORT_PUSH_MIRROR
)
1372 match_flags
|= MATCH_REFS_MIRROR
;
1373 if (flags
& TRANSPORT_PUSH_PRUNE
)
1374 match_flags
|= MATCH_REFS_PRUNE
;
1375 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1376 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1378 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1381 if (transport
->smart_options
&&
1382 transport
->smart_options
->cas
&&
1383 !is_empty_cas(transport
->smart_options
->cas
))
1384 apply_push_cas(transport
->smart_options
->cas
,
1385 transport
->remote
, remote_refs
);
1387 set_ref_status_for_push(remote_refs
,
1388 flags
& TRANSPORT_PUSH_MIRROR
,
1389 flags
& TRANSPORT_PUSH_FORCE
);
1391 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1392 if (run_pre_push_hook(transport
, remote_refs
))
1395 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1396 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1397 !is_bare_repository()) {
1398 struct ref
*ref
= remote_refs
;
1399 struct oid_array commits
= OID_ARRAY_INIT
;
1401 trace2_region_enter("transport_push", "push_submodules", r
);
1402 for (; ref
; ref
= ref
->next
)
1403 if (!is_null_oid(&ref
->new_oid
))
1404 oid_array_append(&commits
,
1407 if (!push_unpushed_submodules(r
,
1411 transport
->push_options
,
1413 oid_array_clear(&commits
);
1414 trace2_region_leave("transport_push", "push_submodules", r
);
1415 die(_("failed to push all needed submodules"));
1417 oid_array_clear(&commits
);
1418 trace2_region_leave("transport_push", "push_submodules", r
);
1421 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1422 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1423 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1424 !pretend
)) && !is_bare_repository()) {
1425 struct ref
*ref
= remote_refs
;
1426 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1427 struct oid_array commits
= OID_ARRAY_INIT
;
1429 trace2_region_enter("transport_push", "check_submodules", r
);
1430 for (; ref
; ref
= ref
->next
)
1431 if (!is_null_oid(&ref
->new_oid
))
1432 oid_array_append(&commits
,
1435 if (find_unpushed_submodules(r
,
1437 transport
->remote
->name
,
1439 oid_array_clear(&commits
);
1440 trace2_region_leave("transport_push", "check_submodules", r
);
1441 die_with_unpushed_submodules(&needs_pushing
);
1443 string_list_clear(&needs_pushing
, 0);
1444 oid_array_clear(&commits
);
1445 trace2_region_leave("transport_push", "check_submodules", r
);
1448 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1449 trace2_region_enter("transport_push", "push_refs", r
);
1450 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1451 trace2_region_leave("transport_push", "push_refs", r
);
1454 err
= push_had_errors(remote_refs
);
1455 ret
= push_ret
| err
;
1458 transport_print_push_status(transport
->url
, remote_refs
,
1459 verbose
| porcelain
, porcelain
,
1462 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1463 set_upstreams(transport
, remote_refs
, pretend
);
1465 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1466 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1468 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1469 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1472 if (porcelain
&& !push_ret
)
1474 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1475 fprintf(stderr
, "Everything up-to-date\n");
1478 free_refs(local_refs
);
1479 free_refs(remote_refs
);
1483 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1484 struct transport_ls_refs_options
*transport_options
)
1486 if (!transport
->got_remote_refs
) {
1487 transport
->remote_refs
=
1488 transport
->vtable
->get_refs_list(transport
, 0,
1490 transport
->got_remote_refs
= 1;
1493 return transport
->remote_refs
;
1496 void transport_ls_refs_options_release(struct transport_ls_refs_options
*opts
)
1498 strvec_clear(&opts
->ref_prefixes
);
1499 free((char *)opts
->unborn_head_target
);
1502 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1505 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1506 struct ref
**heads
= NULL
;
1509 for (rm
= refs
; rm
; rm
= rm
->next
) {
1512 !is_null_oid(&rm
->old_oid
) &&
1513 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1515 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1516 heads
[nr_heads
++] = rm
;
1521 * When deepening of a shallow repository is requested,
1522 * then local and remote refs are likely to still be equal.
1523 * Just feed them all to the fetch method in that case.
1524 * This condition shouldn't be met in a non-deepening fetch
1525 * (see builtin/fetch.c:quickfetch()).
1527 ALLOC_ARRAY(heads
, nr_refs
);
1528 for (rm
= refs
; rm
; rm
= rm
->next
)
1529 heads
[nr_heads
++] = rm
;
1532 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1538 int transport_get_remote_bundle_uri(struct transport
*transport
)
1541 const struct transport_vtable
*vtable
= transport
->vtable
;
1543 /* Check config only once. */
1544 if (transport
->got_remote_bundle_uri
)
1546 transport
->got_remote_bundle_uri
= 1;
1549 * Don't request bundle-uri from the server unless configured to
1550 * do so by the transfer.bundleURI=true config option.
1552 if (git_config_get_bool("transfer.bundleuri", &value
) || !value
)
1555 if (!transport
->bundles
->baseURI
)
1556 transport
->bundles
->baseURI
= xstrdup(transport
->url
);
1558 if (!vtable
->get_bundle_uri
)
1559 return error(_("bundle-uri operation not supported by protocol"));
1561 if (vtable
->get_bundle_uri(transport
) < 0)
1562 return error(_("could not retrieve server-advertised bundle-uri list"));
1566 void transport_unlock_pack(struct transport
*transport
, unsigned int flags
)
1568 int in_signal_handler
= !!(flags
& TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER
);
1571 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1572 if (in_signal_handler
)
1573 unlink(transport
->pack_lockfiles
.items
[i
].string
);
1575 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1576 if (!in_signal_handler
)
1577 string_list_clear(&transport
->pack_lockfiles
, 0);
1580 int transport_connect(struct transport
*transport
, const char *name
,
1581 const char *exec
, int fd
[2])
1583 if (transport
->vtable
->connect
)
1584 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1586 die(_("operation not supported by protocol"));
1589 int transport_disconnect(struct transport
*transport
)
1592 if (transport
->vtable
->disconnect
)
1593 ret
= transport
->vtable
->disconnect(transport
);
1594 if (transport
->got_remote_refs
)
1595 free_refs((void *)transport
->remote_refs
);
1596 clear_bundle_list(transport
->bundles
);
1597 free(transport
->bundles
);
1603 * Strip username (and password) from a URL and return
1604 * it in a newly allocated string.
1606 char *transport_anonymize_url(const char *url
)
1608 char *scheme_prefix
, *anon_part
;
1609 size_t anon_len
, prefix_len
= 0;
1611 anon_part
= strchr(url
, '@');
1612 if (url_is_local_not_ssh(url
) || !anon_part
)
1615 anon_len
= strlen(++anon_part
);
1616 scheme_prefix
= strstr(url
, "://");
1617 if (!scheme_prefix
) {
1618 if (!strchr(anon_part
, ':'))
1619 /* cannot be "me@there:/path/name" */
1623 /* make sure scheme is reasonable */
1624 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1627 case '+': case '.': case '-':
1636 /* @ past the first slash does not count */
1637 cp
= strchr(scheme_prefix
+ 3, '/');
1638 if (cp
&& cp
< anon_part
)
1640 prefix_len
= scheme_prefix
- url
+ 3;
1642 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1643 (int)anon_len
, anon_part
);
1645 return xstrdup(url
);