6 #include "fetch-pack.h"
17 #include "submodule.h"
18 #include "string-list.h"
19 #include "oid-array.h"
21 #include "transport-internal.h"
23 #include "object-store.h"
25 #include "bundle-uri.h"
27 static int transport_use_color
= -1;
28 static char transport_colors
[][COLOR_MAXLEN
] = {
30 GIT_COLOR_RED
/* REJECTED */
33 enum color_transport
{
34 TRANSPORT_COLOR_RESET
= 0,
35 TRANSPORT_COLOR_REJECTED
= 1
38 static int transport_color_config(void)
40 const char *keys
[] = {
41 "color.transport.reset",
42 "color.transport.rejected"
43 }, *key
= "color.transport";
46 static int initialized
;
52 if (!git_config_get_string(key
, &value
))
53 transport_use_color
= git_config_colorbool(key
, value
);
55 if (!want_color_stderr(transport_use_color
))
58 for (i
= 0; i
< ARRAY_SIZE(keys
); i
++)
59 if (!git_config_get_string(keys
[i
], &value
)) {
61 return config_error_nonbool(keys
[i
]);
62 if (color_parse(value
, transport_colors
[i
]) < 0)
69 static const char *transport_get_color(enum color_transport ix
)
71 if (want_color_stderr(transport_use_color
))
72 return transport_colors
[ix
];
76 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
80 for (ref
= refs
; ref
; ref
= ref
->next
) {
81 const char *localname
;
83 const char *remotename
;
86 * Check suitability for tracking. Must be successful /
87 * already up-to-date ref create/modify (not delete).
89 if (ref
->status
!= REF_STATUS_OK
&&
90 ref
->status
!= REF_STATUS_UPTODATE
)
94 if (is_null_oid(&ref
->new_oid
))
97 /* Follow symbolic refs (mainly for HEAD). */
98 localname
= ref
->peer_ref
->name
;
99 remotename
= ref
->name
;
100 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
102 if (tmp
&& flag
& REF_ISSYMREF
&&
103 starts_with(tmp
, "refs/heads/"))
106 /* Both source and destination must be local branches. */
107 if (!localname
|| !starts_with(localname
, "refs/heads/"))
109 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
113 int flag
= transport
->verbose
< 0 ? 0 : BRANCH_CONFIG_VERBOSE
;
114 install_branch_config(flag
, localname
+ 11,
115 transport
->remote
->name
, remotename
);
116 } else if (transport
->verbose
>= 0)
117 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
118 localname
+ 11, remotename
+ 11,
119 transport
->remote
->name
);
123 struct bundle_transport_data
{
125 struct bundle_header header
;
126 unsigned get_refs_from_bundle_called
: 1;
129 static void get_refs_from_bundle_inner(struct transport
*transport
)
131 struct bundle_transport_data
*data
= transport
->data
;
133 data
->get_refs_from_bundle_called
= 1;
137 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
139 die(_("could not read bundle '%s'"), transport
->url
);
141 transport
->hash_algo
= data
->header
.hash_algo
;
144 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
146 struct transport_ls_refs_options
*transport_options UNUSED
)
148 struct bundle_transport_data
*data
= transport
->data
;
149 struct ref
*result
= NULL
;
155 get_refs_from_bundle_inner(transport
);
157 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
158 struct string_list_item
*e
= data
->header
.references
.items
+ i
;
159 const char *name
= e
->string
;
160 struct ref
*ref
= alloc_ref(name
);
161 struct object_id
*oid
= e
->util
;
162 oidcpy(&ref
->old_oid
, oid
);
169 static int fetch_refs_from_bundle(struct transport
*transport
,
170 int nr_heads
, struct ref
**to_fetch
)
172 struct bundle_transport_data
*data
= transport
->data
;
173 struct strvec extra_index_pack_args
= STRVEC_INIT
;
176 if (transport
->progress
)
177 strvec_push(&extra_index_pack_args
, "-v");
179 if (!data
->get_refs_from_bundle_called
)
180 get_refs_from_bundle_inner(transport
);
181 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
182 &extra_index_pack_args
, 0);
183 transport
->hash_algo
= data
->header
.hash_algo
;
187 static int close_bundle(struct transport
*transport
)
189 struct bundle_transport_data
*data
= transport
->data
;
192 bundle_header_release(&data
->header
);
197 struct git_transport_data
{
198 struct git_transport_options options
;
199 struct child_process
*conn
;
201 unsigned finished_handshake
: 1;
202 enum protocol_version version
;
203 struct oid_array extra_have
;
204 struct oid_array shallow
;
207 static int set_git_option(struct git_transport_options
*opts
,
208 const char *name
, const char *value
)
210 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
211 opts
->uploadpack
= value
;
213 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
214 opts
->receivepack
= value
;
216 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
217 opts
->thin
= !!value
;
219 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
220 opts
->followtags
= !!value
;
222 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
223 opts
->keep
= !!value
;
225 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
226 opts
->update_shallow
= !!value
;
228 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
233 opts
->depth
= strtol(value
, &end
, 0);
235 die(_("transport: invalid depth option '%s'"), value
);
238 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
239 opts
->deepen_since
= value
;
241 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
242 opts
->deepen_not
= (const struct string_list
*)value
;
244 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
245 opts
->deepen_relative
= !!value
;
247 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
248 opts
->from_promisor
= !!value
;
250 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
251 list_objects_filter_die_if_populated(&opts
->filter_options
);
252 parse_list_objects_filter(&opts
->filter_options
, value
);
254 } else if (!strcmp(name
, TRANS_OPT_REFETCH
)) {
255 opts
->refetch
= !!value
;
257 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
258 opts
->reject_shallow
= !!value
;
264 static int connect_setup(struct transport
*transport
, int for_push
)
266 struct git_transport_data
*data
= transport
->data
;
267 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
272 switch (transport
->family
) {
273 case TRANSPORT_FAMILY_ALL
: break;
274 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
275 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
278 data
->conn
= git_connect(data
->fd
, transport
->url
,
279 for_push
? data
->options
.receivepack
:
280 data
->options
.uploadpack
,
286 static void die_if_server_options(struct transport
*transport
)
288 if (!transport
->server_options
|| !transport
->server_options
->nr
)
290 advise(_("see protocol.version in 'git help config' for more details"));
291 die(_("server options require protocol version 2 or later"));
295 * Obtains the protocol version from the transport and writes it to
296 * transport->data->version, first connecting if not already connected.
298 * If the protocol version is one that allows skipping the listing of remote
299 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
300 * this function returns NULL. Otherwise, this function returns the list of
303 static struct ref
*handshake(struct transport
*transport
, int for_push
,
304 struct transport_ls_refs_options
*options
,
307 struct git_transport_data
*data
= transport
->data
;
308 struct ref
*refs
= NULL
;
309 struct packet_reader reader
;
311 const char *server_sid
;
313 connect_setup(transport
, for_push
);
315 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
316 PACKET_READ_CHOMP_NEWLINE
|
317 PACKET_READ_GENTLE_ON_EOF
|
318 PACKET_READ_DIE_ON_ERR_PACKET
);
320 data
->version
= discover_version(&reader
);
321 switch (data
->version
) {
323 if (server_feature_v2("session-id", &server_sid
))
324 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
326 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
328 transport
->server_options
,
329 transport
->stateless_rpc
);
333 die_if_server_options(transport
);
334 get_remote_heads(&reader
, &refs
,
335 for_push
? REF_NORMAL
: 0,
338 server_sid
= server_feature_value("session-id", &sid_len
);
340 char *sid
= xstrndup(server_sid
, sid_len
);
341 trace2_data_string("transfer", NULL
, "server-sid", sid
);
345 case protocol_unknown_version
:
346 BUG("unknown protocol version");
348 data
->finished_handshake
= 1;
349 transport
->hash_algo
= reader
.hash_algo
;
351 if (reader
.line_peeked
)
352 BUG("buffer must be empty at the end of handshake()");
357 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
358 struct transport_ls_refs_options
*options
)
360 return handshake(transport
, for_push
, options
, 1);
363 static int get_bundle_uri(struct transport
*transport
)
365 struct git_transport_data
*data
= transport
->data
;
366 struct packet_reader reader
;
367 int stateless_rpc
= transport
->stateless_rpc
;
369 if (!transport
->bundles
) {
370 CALLOC_ARRAY(transport
->bundles
, 1);
371 init_bundle_list(transport
->bundles
);
374 if (!data
->finished_handshake
) {
375 struct ref
*refs
= handshake(transport
, 0, NULL
, 0);
382 * "Support" protocol v0 and v2 without bundle-uri support by
383 * silently degrading to a NOOP.
385 if (!server_supports_v2("bundle-uri"))
388 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
389 PACKET_READ_CHOMP_NEWLINE
|
390 PACKET_READ_GENTLE_ON_EOF
);
392 return get_remote_bundle_uri(data
->fd
[1], &reader
,
393 transport
->bundles
, stateless_rpc
);
396 static int fetch_refs_via_pack(struct transport
*transport
,
397 int nr_heads
, struct ref
**to_fetch
)
400 struct git_transport_data
*data
= transport
->data
;
401 struct ref
*refs
= NULL
;
402 struct fetch_pack_args args
;
403 struct ref
*refs_tmp
= NULL
;
405 memset(&args
, 0, sizeof(args
));
406 args
.uploadpack
= data
->options
.uploadpack
;
407 args
.keep_pack
= data
->options
.keep
;
409 args
.use_thin_pack
= data
->options
.thin
;
410 args
.include_tag
= data
->options
.followtags
;
411 args
.verbose
= (transport
->verbose
> 1);
412 args
.quiet
= (transport
->verbose
< 0);
413 args
.no_progress
= !transport
->progress
;
414 args
.depth
= data
->options
.depth
;
415 args
.deepen_since
= data
->options
.deepen_since
;
416 args
.deepen_not
= data
->options
.deepen_not
;
417 args
.deepen_relative
= data
->options
.deepen_relative
;
418 args
.check_self_contained_and_connected
=
419 data
->options
.check_self_contained_and_connected
;
420 args
.cloning
= transport
->cloning
;
421 args
.update_shallow
= data
->options
.update_shallow
;
422 args
.from_promisor
= data
->options
.from_promisor
;
423 list_objects_filter_copy(&args
.filter_options
,
424 &data
->options
.filter_options
);
425 args
.refetch
= data
->options
.refetch
;
426 args
.stateless_rpc
= transport
->stateless_rpc
;
427 args
.server_options
= transport
->server_options
;
428 args
.negotiation_tips
= data
->options
.negotiation_tips
;
429 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
431 if (!data
->finished_handshake
) {
433 int must_list_refs
= 0;
434 for (i
= 0; i
< nr_heads
; i
++) {
435 if (!to_fetch
[i
]->exact_oid
) {
440 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
443 if (data
->version
== protocol_unknown_version
)
444 BUG("unknown protocol version");
445 else if (data
->version
<= protocol_v1
)
446 die_if_server_options(transport
);
448 if (data
->options
.acked_commits
) {
449 if (data
->version
< protocol_v2
) {
450 warning(_("--negotiate-only requires protocol v2"));
452 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
453 warning(_("server does not support wait-for-done"));
456 negotiate_using_fetch(data
->options
.negotiation_tips
,
457 transport
->server_options
,
458 transport
->stateless_rpc
,
460 data
->options
.acked_commits
);
466 refs
= fetch_pack(&args
, data
->fd
,
467 refs_tmp
? refs_tmp
: transport
->remote_refs
,
468 to_fetch
, nr_heads
, &data
->shallow
,
469 &transport
->pack_lockfiles
, data
->version
);
471 data
->finished_handshake
= 0;
472 data
->options
.self_contained_and_connected
=
473 args
.self_contained_and_connected
;
474 data
->options
.connectivity_checked
= args
.connectivity_checked
;
478 if (report_unmatched_refs(to_fetch
, nr_heads
))
483 if (data
->fd
[1] >= 0)
485 if (finish_connect(data
->conn
))
491 list_objects_filter_release(&args
.filter_options
);
495 static int push_had_errors(struct ref
*ref
)
497 for (; ref
; ref
= ref
->next
) {
498 switch (ref
->status
) {
499 case REF_STATUS_NONE
:
500 case REF_STATUS_UPTODATE
:
510 int transport_refs_pushed(struct ref
*ref
)
512 for (; ref
; ref
= ref
->next
) {
513 switch(ref
->status
) {
514 case REF_STATUS_NONE
:
515 case REF_STATUS_UPTODATE
:
524 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
525 struct object_id
*new_oid
, int deletion
,
528 struct refspec_item rs
;
530 memset(&rs
, 0, sizeof(rs
));
534 if (!remote_find_tracking(remote
, &rs
)) {
536 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
538 delete_ref(NULL
, rs
.dst
, NULL
, 0);
540 update_ref("update by push", rs
.dst
, new_oid
,
546 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
549 struct object_id
*new_oid
;
550 struct ref_push_report
*report
;
552 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
555 report
= ref
->report
;
557 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
558 ref
->deletion
, verbose
);
560 for (; report
; report
= report
->next
) {
561 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
562 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
563 update_one_tracking_ref(remote
, refname
, new_oid
,
564 is_null_oid(new_oid
), verbose
);
568 static void print_ref_status(char flag
, const char *summary
,
569 struct ref
*to
, struct ref
*from
, const char *msg
,
570 struct ref_push_report
*report
,
571 int porcelain
, int summary_width
)
575 if (report
&& report
->ref_name
)
576 to_name
= report
->ref_name
;
582 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
584 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
586 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
588 fprintf(stdout
, "%s\n", summary
);
590 const char *red
= "", *reset
= "";
591 if (push_had_errors(to
)) {
592 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
593 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
595 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
598 fprintf(stderr
, "%s -> %s",
599 prettify_refname(from
->name
),
600 prettify_refname(to_name
));
602 fputs(prettify_refname(to_name
), stderr
);
612 static void print_ok_ref_status(struct ref
*ref
,
613 struct ref_push_report
*report
,
614 int porcelain
, int summary_width
)
616 struct object_id
*old_oid
;
617 struct object_id
*new_oid
;
618 const char *ref_name
;
621 if (report
&& report
->old_oid
)
622 old_oid
= report
->old_oid
;
624 old_oid
= &ref
->old_oid
;
625 if (report
&& report
->new_oid
)
626 new_oid
= report
->new_oid
;
628 new_oid
= &ref
->new_oid
;
629 if (report
&& report
->forced_update
)
630 forced_update
= report
->forced_update
;
632 forced_update
= ref
->forced_update
;
633 if (report
&& report
->ref_name
)
634 ref_name
= report
->ref_name
;
636 ref_name
= ref
->name
;
639 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
640 report
, porcelain
, summary_width
);
641 else if (is_null_oid(old_oid
))
642 print_ref_status('*',
643 (starts_with(ref_name
, "refs/tags/")
645 : (starts_with(ref_name
, "refs/heads/")
647 : "[new reference]")),
648 ref
, ref
->peer_ref
, NULL
,
649 report
, porcelain
, summary_width
);
651 struct strbuf quickref
= STRBUF_INIT
;
655 strbuf_add_unique_abbrev(&quickref
, old_oid
,
658 strbuf_addstr(&quickref
, "...");
660 msg
= "forced update";
662 strbuf_addstr(&quickref
, "..");
666 strbuf_add_unique_abbrev(&quickref
, new_oid
,
669 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
670 report
, porcelain
, summary_width
);
671 strbuf_release(&quickref
);
675 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
676 struct ref_push_report
*report
,
677 int porcelain
, int summary_width
)
680 char *url
= transport_anonymize_url(dest
);
681 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
685 switch(ref
->status
) {
686 case REF_STATUS_NONE
:
687 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
688 report
, porcelain
, summary_width
);
690 case REF_STATUS_REJECT_NODELETE
:
691 print_ref_status('!', "[rejected]", ref
, NULL
,
692 "remote does not support deleting refs",
693 report
, porcelain
, summary_width
);
695 case REF_STATUS_UPTODATE
:
696 print_ref_status('=', "[up to date]", ref
,
698 report
, porcelain
, summary_width
);
700 case REF_STATUS_REJECT_NONFASTFORWARD
:
701 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
703 report
, porcelain
, summary_width
);
705 case REF_STATUS_REJECT_ALREADY_EXISTS
:
706 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
708 report
, porcelain
, summary_width
);
710 case REF_STATUS_REJECT_FETCH_FIRST
:
711 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
713 report
, porcelain
, summary_width
);
715 case REF_STATUS_REJECT_NEEDS_FORCE
:
716 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
718 report
, porcelain
, summary_width
);
720 case REF_STATUS_REJECT_STALE
:
721 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
723 report
, porcelain
, summary_width
);
725 case REF_STATUS_REJECT_REMOTE_UPDATED
:
726 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
727 "remote ref updated since checkout",
728 report
, porcelain
, summary_width
);
730 case REF_STATUS_REJECT_SHALLOW
:
731 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
732 "new shallow roots not allowed",
733 report
, porcelain
, summary_width
);
735 case REF_STATUS_REMOTE_REJECT
:
736 print_ref_status('!', "[remote rejected]", ref
,
737 ref
->deletion
? NULL
: ref
->peer_ref
,
739 report
, porcelain
, summary_width
);
741 case REF_STATUS_EXPECTING_REPORT
:
742 print_ref_status('!', "[remote failure]", ref
,
743 ref
->deletion
? NULL
: ref
->peer_ref
,
744 "remote failed to report status",
745 report
, porcelain
, summary_width
);
747 case REF_STATUS_ATOMIC_PUSH_FAILED
:
748 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
749 "atomic push failed",
750 report
, porcelain
, summary_width
);
753 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
760 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
761 int porcelain
, int summary_width
)
763 struct ref_push_report
*report
;
767 return print_one_push_report(ref
, dest
, count
,
768 NULL
, porcelain
, summary_width
);
770 for (report
= ref
->report
; report
; report
= report
->next
)
771 print_one_push_report(ref
, dest
, count
+ n
++,
772 report
, porcelain
, summary_width
);
776 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
778 char hex
[GIT_MAX_HEXSZ
+ 1];
779 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
781 return (w
< sofar
) ? sofar
: w
;
784 int transport_summary_width(const struct ref
*refs
)
788 for (; refs
; refs
= refs
->next
) {
789 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
790 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
793 maxw
= FALLBACK_DEFAULT_ABBREV
;
794 return (2 * maxw
+ 3);
797 void transport_print_push_status(const char *dest
, struct ref
*refs
,
798 int verbose
, int porcelain
, unsigned int *reject_reasons
)
803 int summary_width
= transport_summary_width(refs
);
805 if (transport_color_config() < 0)
806 warning(_("could not parse transport.color.* config"));
808 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
811 for (ref
= refs
; ref
; ref
= ref
->next
)
812 if (ref
->status
== REF_STATUS_UPTODATE
)
813 n
+= print_one_push_status(ref
, dest
, n
,
814 porcelain
, summary_width
);
817 for (ref
= refs
; ref
; ref
= ref
->next
)
818 if (ref
->status
== REF_STATUS_OK
)
819 n
+= print_one_push_status(ref
, dest
, n
,
820 porcelain
, summary_width
);
823 for (ref
= refs
; ref
; ref
= ref
->next
) {
824 if (ref
->status
!= REF_STATUS_NONE
&&
825 ref
->status
!= REF_STATUS_UPTODATE
&&
826 ref
->status
!= REF_STATUS_OK
)
827 n
+= print_one_push_status(ref
, dest
, n
,
828 porcelain
, summary_width
);
829 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
830 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
831 *reject_reasons
|= REJECT_NON_FF_HEAD
;
833 *reject_reasons
|= REJECT_NON_FF_OTHER
;
834 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
835 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
836 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
837 *reject_reasons
|= REJECT_FETCH_FIRST
;
838 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
839 *reject_reasons
|= REJECT_NEEDS_FORCE
;
840 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
841 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
847 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
849 struct git_transport_data
*data
= transport
->data
;
850 struct send_pack_args args
;
853 if (transport_color_config() < 0)
856 if (!data
->finished_handshake
)
857 get_refs_via_connect(transport
, 1, NULL
);
859 memset(&args
, 0, sizeof(args
));
860 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
861 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
862 args
.use_thin_pack
= data
->options
.thin
;
863 args
.verbose
= (transport
->verbose
> 0);
864 args
.quiet
= (transport
->verbose
< 0);
865 args
.progress
= transport
->progress
;
866 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
867 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
868 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
869 args
.push_options
= transport
->push_options
;
870 args
.url
= transport
->url
;
872 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
873 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
874 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
875 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
877 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
879 switch (data
->version
) {
881 die(_("support for protocol v2 not implemented yet"));
885 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
888 case protocol_unknown_version
:
889 BUG("unknown protocol version");
895 * Atomic push may abort the connection early and close the pipe,
896 * which may cause an error for `finish_connect()`. Ignore this error
897 * for atomic git-push.
899 if (ret
|| args
.atomic
)
900 finish_connect(data
->conn
);
902 ret
= finish_connect(data
->conn
);
904 data
->finished_handshake
= 0;
909 static int connect_git(struct transport
*transport
, const char *name
,
910 const char *executable
, int fd
[2])
912 struct git_transport_data
*data
= transport
->data
;
913 data
->conn
= git_connect(data
->fd
, transport
->url
,
920 static int disconnect_git(struct transport
*transport
)
922 struct git_transport_data
*data
= transport
->data
;
924 if (data
->finished_handshake
&& !transport
->stateless_rpc
)
925 packet_flush(data
->fd
[1]);
927 if (data
->fd
[1] >= 0)
929 finish_connect(data
->conn
);
932 list_objects_filter_release(&data
->options
.filter_options
);
937 static struct transport_vtable taken_over_vtable
= {
938 .get_refs_list
= get_refs_via_connect
,
939 .get_bundle_uri
= get_bundle_uri
,
940 .fetch_refs
= fetch_refs_via_pack
,
941 .push_refs
= git_transport_push
,
942 .disconnect
= disconnect_git
945 void transport_take_over(struct transport
*transport
,
946 struct child_process
*child
)
948 struct git_transport_data
*data
;
950 if (!transport
->smart_options
)
951 BUG("taking over transport requires non-NULL "
952 "smart_options field.");
954 CALLOC_ARRAY(data
, 1);
955 data
->options
= *transport
->smart_options
;
957 data
->fd
[0] = data
->conn
->out
;
958 data
->fd
[1] = data
->conn
->in
;
959 data
->finished_handshake
= 0;
960 transport
->data
= data
;
962 transport
->vtable
= &taken_over_vtable
;
963 transport
->smart_options
= &(data
->options
);
965 transport
->cannot_reuse
= 1;
968 static int is_file(const char *url
)
973 return S_ISREG(buf
.st_mode
);
976 static int external_specification_len(const char *url
)
978 return strchr(url
, ':') - url
;
981 static const struct string_list
*protocol_allow_list(void)
983 static int enabled
= -1;
984 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
987 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
989 string_list_split(&allowed
, v
, ':', -1);
990 string_list_sort(&allowed
);
997 return enabled
? &allowed
: NULL
;
1000 enum protocol_allow_config
{
1001 PROTOCOL_ALLOW_NEVER
= 0,
1002 PROTOCOL_ALLOW_USER_ONLY
,
1003 PROTOCOL_ALLOW_ALWAYS
1006 static enum protocol_allow_config
parse_protocol_config(const char *key
,
1009 if (!strcasecmp(value
, "always"))
1010 return PROTOCOL_ALLOW_ALWAYS
;
1011 else if (!strcasecmp(value
, "never"))
1012 return PROTOCOL_ALLOW_NEVER
;
1013 else if (!strcasecmp(value
, "user"))
1014 return PROTOCOL_ALLOW_USER_ONLY
;
1016 die(_("unknown value for config '%s': %s"), key
, value
);
1019 static enum protocol_allow_config
get_protocol_config(const char *type
)
1021 char *key
= xstrfmt("protocol.%s.allow", type
);
1024 /* first check the per-protocol config */
1025 if (!git_config_get_string(key
, &value
)) {
1026 enum protocol_allow_config ret
=
1027 parse_protocol_config(key
, value
);
1034 /* if defined, fallback to user-defined default for unknown protocols */
1035 if (!git_config_get_string("protocol.allow", &value
)) {
1036 enum protocol_allow_config ret
=
1037 parse_protocol_config("protocol.allow", value
);
1042 /* fallback to built-in defaults */
1044 if (!strcmp(type
, "http") ||
1045 !strcmp(type
, "https") ||
1046 !strcmp(type
, "git") ||
1047 !strcmp(type
, "ssh"))
1048 return PROTOCOL_ALLOW_ALWAYS
;
1050 /* known scary; err on the side of caution */
1051 if (!strcmp(type
, "ext"))
1052 return PROTOCOL_ALLOW_NEVER
;
1054 /* unknown; by default let them be used only directly by the user */
1055 return PROTOCOL_ALLOW_USER_ONLY
;
1058 int is_transport_allowed(const char *type
, int from_user
)
1060 const struct string_list
*allow_list
= protocol_allow_list();
1062 return string_list_has_string(allow_list
, type
);
1064 switch (get_protocol_config(type
)) {
1065 case PROTOCOL_ALLOW_ALWAYS
:
1067 case PROTOCOL_ALLOW_NEVER
:
1069 case PROTOCOL_ALLOW_USER_ONLY
:
1071 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1075 BUG("invalid protocol_allow_config type");
1078 void transport_check_allowed(const char *type
)
1080 if (!is_transport_allowed(type
, -1))
1081 die(_("transport '%s' not allowed"), type
);
1084 static struct transport_vtable bundle_vtable
= {
1085 .get_refs_list
= get_refs_from_bundle
,
1086 .fetch_refs
= fetch_refs_from_bundle
,
1087 .disconnect
= close_bundle
1090 static struct transport_vtable builtin_smart_vtable
= {
1091 .get_refs_list
= get_refs_via_connect
,
1092 .get_bundle_uri
= get_bundle_uri
,
1093 .fetch_refs
= fetch_refs_via_pack
,
1094 .push_refs
= git_transport_push
,
1095 .connect
= connect_git
,
1096 .disconnect
= disconnect_git
1099 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1102 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1104 ret
->progress
= isatty(2);
1105 string_list_init_dup(&ret
->pack_lockfiles
);
1107 CALLOC_ARRAY(ret
->bundles
, 1);
1108 init_bundle_list(ret
->bundles
);
1111 BUG("No remote provided to transport_get()");
1113 ret
->got_remote_refs
= 0;
1114 ret
->remote
= remote
;
1115 helper
= remote
->foreign_vcs
;
1117 if (!url
&& remote
->url
)
1118 url
= remote
->url
[0];
1121 /* maybe it is a foreign URL? */
1123 const char *p
= url
;
1125 while (is_urlschemechar(p
== url
, *p
))
1127 if (starts_with(p
, "::"))
1128 helper
= xstrndup(url
, p
- url
);
1132 transport_helper_init(ret
, helper
);
1133 } else if (starts_with(url
, "rsync:")) {
1134 die(_("git-over-rsync is no longer supported"));
1135 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1136 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1137 bundle_header_init(&data
->header
);
1138 transport_check_allowed("file");
1140 ret
->vtable
= &bundle_vtable
;
1141 ret
->smart_options
= NULL
;
1142 } else if (!is_url(url
)
1143 || starts_with(url
, "file://")
1144 || starts_with(url
, "git://")
1145 || starts_with(url
, "ssh://")
1146 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1147 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1150 * These are builtin smart transports; "allowed" transports
1151 * will be checked individually in git_connect.
1153 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1154 list_objects_filter_init(&data
->options
.filter_options
);
1156 ret
->vtable
= &builtin_smart_vtable
;
1157 ret
->smart_options
= &(data
->options
);
1160 data
->finished_handshake
= 0;
1162 /* Unknown protocol in URL. Pass to external handler. */
1163 int len
= external_specification_len(url
);
1164 char *handler
= xmemdupz(url
, len
);
1165 transport_helper_init(ret
, handler
);
1168 if (ret
->smart_options
) {
1169 ret
->smart_options
->thin
= 1;
1170 ret
->smart_options
->uploadpack
= "git-upload-pack";
1171 if (remote
->uploadpack
)
1172 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1173 ret
->smart_options
->receivepack
= "git-receive-pack";
1174 if (remote
->receivepack
)
1175 ret
->smart_options
->receivepack
= remote
->receivepack
;
1178 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1183 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1185 return transport
->hash_algo
;
1188 int transport_set_option(struct transport
*transport
,
1189 const char *name
, const char *value
)
1191 int git_reports
= 1, protocol_reports
= 1;
1193 if (transport
->smart_options
)
1194 git_reports
= set_git_option(transport
->smart_options
,
1197 if (transport
->vtable
->set_option
)
1198 protocol_reports
= transport
->vtable
->set_option(transport
,
1201 /* If either report is 0, report 0 (success). */
1202 if (!git_reports
|| !protocol_reports
)
1204 /* If either reports -1 (invalid value), report -1. */
1205 if ((git_reports
== -1) || (protocol_reports
== -1))
1207 /* Otherwise if both report unknown, report unknown. */
1211 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1215 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1217 transport
->verbose
= -1;
1220 * Rules used to determine whether to report progress (processing aborts
1221 * when a rule is satisfied):
1223 * . Report progress, if force_progress is 1 (ie. --progress).
1224 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1225 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1226 * . Report progress if isatty(2) is 1.
1228 if (force_progress
>= 0)
1229 transport
->progress
= !!force_progress
;
1231 transport
->progress
= verbosity
>= 0 && isatty(2);
1234 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1238 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1239 "not be found on any remote:\n"));
1240 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1241 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1242 fprintf(stderr
, _("\nPlease try\n\n"
1243 " git push --recurse-submodules=on-demand\n\n"
1244 "or cd to the path and use\n\n"
1246 "to push them to a remote.\n\n"));
1248 string_list_clear(needs_pushing
, 0);
1250 die(_("Aborting."));
1253 static int run_pre_push_hook(struct transport
*transport
,
1254 struct ref
*remote_refs
)
1258 struct child_process proc
= CHILD_PROCESS_INIT
;
1260 const char *hook_path
= find_hook("pre-push");
1265 strvec_push(&proc
.args
, hook_path
);
1266 strvec_push(&proc
.args
, transport
->remote
->name
);
1267 strvec_push(&proc
.args
, transport
->url
);
1270 proc
.trace2_hook_name
= "pre-push";
1272 if (start_command(&proc
)) {
1273 finish_command(&proc
);
1277 sigchain_push(SIGPIPE
, SIG_IGN
);
1279 strbuf_init(&buf
, 256);
1281 for (r
= remote_refs
; r
; r
= r
->next
) {
1282 if (!r
->peer_ref
) continue;
1283 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1284 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1285 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1286 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1289 strbuf_addf( &buf
, "%s %s %s %s\n",
1290 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1291 r
->name
, oid_to_hex(&r
->old_oid
));
1293 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1294 /* We do not mind if a hook does not read all refs. */
1301 strbuf_release(&buf
);
1307 sigchain_pop(SIGPIPE
);
1309 x
= finish_command(&proc
);
1316 int transport_push(struct repository
*r
,
1317 struct transport
*transport
,
1318 struct refspec
*rs
, int flags
,
1319 unsigned int *reject_reasons
)
1321 struct ref
*remote_refs
= NULL
;
1322 struct ref
*local_refs
= NULL
;
1323 int match_flags
= MATCH_REFS_NONE
;
1324 int verbose
= (transport
->verbose
> 0);
1325 int quiet
= (transport
->verbose
< 0);
1326 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1327 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1330 struct transport_ls_refs_options transport_options
=
1331 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1333 *reject_reasons
= 0;
1335 if (transport_color_config() < 0)
1338 if (!transport
->vtable
->push_refs
)
1341 local_refs
= get_local_heads();
1343 if (check_push_refs(local_refs
, rs
) < 0)
1346 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1348 trace2_region_enter("transport_push", "get_refs_list", r
);
1349 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1350 &transport_options
);
1351 trace2_region_leave("transport_push", "get_refs_list", r
);
1353 transport_ls_refs_options_release(&transport_options
);
1355 if (flags
& TRANSPORT_PUSH_ALL
)
1356 match_flags
|= MATCH_REFS_ALL
;
1357 if (flags
& TRANSPORT_PUSH_MIRROR
)
1358 match_flags
|= MATCH_REFS_MIRROR
;
1359 if (flags
& TRANSPORT_PUSH_PRUNE
)
1360 match_flags
|= MATCH_REFS_PRUNE
;
1361 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1362 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1364 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1367 if (transport
->smart_options
&&
1368 transport
->smart_options
->cas
&&
1369 !is_empty_cas(transport
->smart_options
->cas
))
1370 apply_push_cas(transport
->smart_options
->cas
,
1371 transport
->remote
, remote_refs
);
1373 set_ref_status_for_push(remote_refs
,
1374 flags
& TRANSPORT_PUSH_MIRROR
,
1375 flags
& TRANSPORT_PUSH_FORCE
);
1377 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1378 if (run_pre_push_hook(transport
, remote_refs
))
1381 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1382 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1383 !is_bare_repository()) {
1384 struct ref
*ref
= remote_refs
;
1385 struct oid_array commits
= OID_ARRAY_INIT
;
1387 trace2_region_enter("transport_push", "push_submodules", r
);
1388 for (; ref
; ref
= ref
->next
)
1389 if (!is_null_oid(&ref
->new_oid
))
1390 oid_array_append(&commits
,
1393 if (!push_unpushed_submodules(r
,
1397 transport
->push_options
,
1399 oid_array_clear(&commits
);
1400 trace2_region_leave("transport_push", "push_submodules", r
);
1401 die(_("failed to push all needed submodules"));
1403 oid_array_clear(&commits
);
1404 trace2_region_leave("transport_push", "push_submodules", r
);
1407 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1408 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1409 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1410 !pretend
)) && !is_bare_repository()) {
1411 struct ref
*ref
= remote_refs
;
1412 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1413 struct oid_array commits
= OID_ARRAY_INIT
;
1415 trace2_region_enter("transport_push", "check_submodules", r
);
1416 for (; ref
; ref
= ref
->next
)
1417 if (!is_null_oid(&ref
->new_oid
))
1418 oid_array_append(&commits
,
1421 if (find_unpushed_submodules(r
,
1423 transport
->remote
->name
,
1425 oid_array_clear(&commits
);
1426 trace2_region_leave("transport_push", "check_submodules", r
);
1427 die_with_unpushed_submodules(&needs_pushing
);
1429 string_list_clear(&needs_pushing
, 0);
1430 oid_array_clear(&commits
);
1431 trace2_region_leave("transport_push", "check_submodules", r
);
1434 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1435 trace2_region_enter("transport_push", "push_refs", r
);
1436 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1437 trace2_region_leave("transport_push", "push_refs", r
);
1440 err
= push_had_errors(remote_refs
);
1441 ret
= push_ret
| err
;
1444 transport_print_push_status(transport
->url
, remote_refs
,
1445 verbose
| porcelain
, porcelain
,
1448 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1449 set_upstreams(transport
, remote_refs
, pretend
);
1451 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1452 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1454 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1455 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1458 if (porcelain
&& !push_ret
)
1460 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1461 fprintf(stderr
, "Everything up-to-date\n");
1464 free_refs(local_refs
);
1465 free_refs(remote_refs
);
1469 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1470 struct transport_ls_refs_options
*transport_options
)
1472 if (!transport
->got_remote_refs
) {
1473 transport
->remote_refs
=
1474 transport
->vtable
->get_refs_list(transport
, 0,
1476 transport
->got_remote_refs
= 1;
1479 return transport
->remote_refs
;
1482 void transport_ls_refs_options_release(struct transport_ls_refs_options
*opts
)
1484 strvec_clear(&opts
->ref_prefixes
);
1485 free((char *)opts
->unborn_head_target
);
1488 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1491 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1492 struct ref
**heads
= NULL
;
1495 for (rm
= refs
; rm
; rm
= rm
->next
) {
1498 !is_null_oid(&rm
->old_oid
) &&
1499 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1501 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1502 heads
[nr_heads
++] = rm
;
1507 * When deepening of a shallow repository is requested,
1508 * then local and remote refs are likely to still be equal.
1509 * Just feed them all to the fetch method in that case.
1510 * This condition shouldn't be met in a non-deepening fetch
1511 * (see builtin/fetch.c:quickfetch()).
1513 ALLOC_ARRAY(heads
, nr_refs
);
1514 for (rm
= refs
; rm
; rm
= rm
->next
)
1515 heads
[nr_heads
++] = rm
;
1518 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1524 int transport_get_remote_bundle_uri(struct transport
*transport
)
1527 const struct transport_vtable
*vtable
= transport
->vtable
;
1529 /* Check config only once. */
1530 if (transport
->got_remote_bundle_uri
)
1532 transport
->got_remote_bundle_uri
= 1;
1535 * Don't request bundle-uri from the server unless configured to
1536 * do so by the transfer.bundleURI=true config option.
1538 if (git_config_get_bool("transfer.bundleuri", &value
) || !value
)
1541 if (!transport
->bundles
->baseURI
)
1542 transport
->bundles
->baseURI
= xstrdup(transport
->url
);
1544 if (!vtable
->get_bundle_uri
)
1545 return error(_("bundle-uri operation not supported by protocol"));
1547 if (vtable
->get_bundle_uri(transport
) < 0)
1548 return error(_("could not retrieve server-advertised bundle-uri list"));
1552 void transport_unlock_pack(struct transport
*transport
, unsigned int flags
)
1554 int in_signal_handler
= !!(flags
& TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER
);
1557 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1558 if (in_signal_handler
)
1559 unlink(transport
->pack_lockfiles
.items
[i
].string
);
1561 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1562 if (!in_signal_handler
)
1563 string_list_clear(&transport
->pack_lockfiles
, 0);
1566 int transport_connect(struct transport
*transport
, const char *name
,
1567 const char *exec
, int fd
[2])
1569 if (transport
->vtable
->connect
)
1570 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1572 die(_("operation not supported by protocol"));
1575 int transport_disconnect(struct transport
*transport
)
1578 if (transport
->vtable
->disconnect
)
1579 ret
= transport
->vtable
->disconnect(transport
);
1580 if (transport
->got_remote_refs
)
1581 free_refs((void *)transport
->remote_refs
);
1582 clear_bundle_list(transport
->bundles
);
1583 free(transport
->bundles
);
1589 * Strip username (and password) from a URL and return
1590 * it in a newly allocated string.
1592 char *transport_anonymize_url(const char *url
)
1594 char *scheme_prefix
, *anon_part
;
1595 size_t anon_len
, prefix_len
= 0;
1597 anon_part
= strchr(url
, '@');
1598 if (url_is_local_not_ssh(url
) || !anon_part
)
1601 anon_len
= strlen(++anon_part
);
1602 scheme_prefix
= strstr(url
, "://");
1603 if (!scheme_prefix
) {
1604 if (!strchr(anon_part
, ':'))
1605 /* cannot be "me@there:/path/name" */
1609 /* make sure scheme is reasonable */
1610 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1613 case '+': case '.': case '-':
1622 /* @ past the first slash does not count */
1623 cp
= strchr(scheme_prefix
+ 3, '/');
1624 if (cp
&& cp
< anon_part
)
1626 prefix_len
= scheme_prefix
- url
+ 3;
1628 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1629 (int)anon_len
, anon_part
);
1631 return xstrdup(url
);