8 #include "fetch-pack.h"
11 #include "send-pack.h"
20 #include "submodule.h"
21 #include "string-list.h"
22 #include "oid-array.h"
24 #include "transport-internal.h"
26 #include "object-store.h"
28 #include "bundle-uri.h"
31 static int transport_use_color
= -1;
32 static char transport_colors
[][COLOR_MAXLEN
] = {
34 GIT_COLOR_RED
/* REJECTED */
37 enum color_transport
{
38 TRANSPORT_COLOR_RESET
= 0,
39 TRANSPORT_COLOR_REJECTED
= 1
42 static int transport_color_config(void)
44 const char *keys
[] = {
45 "color.transport.reset",
46 "color.transport.rejected"
47 }, *key
= "color.transport";
50 static int initialized
;
56 if (!git_config_get_string(key
, &value
))
57 transport_use_color
= git_config_colorbool(key
, value
);
59 if (!want_color_stderr(transport_use_color
))
62 for (i
= 0; i
< ARRAY_SIZE(keys
); i
++)
63 if (!git_config_get_string(keys
[i
], &value
)) {
65 return config_error_nonbool(keys
[i
]);
66 if (color_parse(value
, transport_colors
[i
]) < 0)
73 static const char *transport_get_color(enum color_transport ix
)
75 if (want_color_stderr(transport_use_color
))
76 return transport_colors
[ix
];
80 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
84 for (ref
= refs
; ref
; ref
= ref
->next
) {
85 const char *localname
;
87 const char *remotename
;
90 * Check suitability for tracking. Must be successful /
91 * already up-to-date ref create/modify (not delete).
93 if (ref
->status
!= REF_STATUS_OK
&&
94 ref
->status
!= REF_STATUS_UPTODATE
)
98 if (is_null_oid(&ref
->new_oid
))
101 /* Follow symbolic refs (mainly for HEAD). */
102 localname
= ref
->peer_ref
->name
;
103 remotename
= ref
->name
;
104 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
106 if (tmp
&& flag
& REF_ISSYMREF
&&
107 starts_with(tmp
, "refs/heads/"))
110 /* Both source and destination must be local branches. */
111 if (!localname
|| !starts_with(localname
, "refs/heads/"))
113 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
117 int flag
= transport
->verbose
< 0 ? 0 : BRANCH_CONFIG_VERBOSE
;
118 install_branch_config(flag
, localname
+ 11,
119 transport
->remote
->name
, remotename
);
120 } else if (transport
->verbose
>= 0)
121 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
122 localname
+ 11, remotename
+ 11,
123 transport
->remote
->name
);
127 struct bundle_transport_data
{
129 struct bundle_header header
;
130 unsigned get_refs_from_bundle_called
: 1;
133 static void get_refs_from_bundle_inner(struct transport
*transport
)
135 struct bundle_transport_data
*data
= transport
->data
;
137 data
->get_refs_from_bundle_called
= 1;
141 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
143 die(_("could not read bundle '%s'"), transport
->url
);
145 transport
->hash_algo
= data
->header
.hash_algo
;
148 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
150 struct transport_ls_refs_options
*transport_options UNUSED
)
152 struct bundle_transport_data
*data
= transport
->data
;
153 struct ref
*result
= NULL
;
159 get_refs_from_bundle_inner(transport
);
161 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
162 struct string_list_item
*e
= data
->header
.references
.items
+ i
;
163 const char *name
= e
->string
;
164 struct ref
*ref
= alloc_ref(name
);
165 struct object_id
*oid
= e
->util
;
166 oidcpy(&ref
->old_oid
, oid
);
173 static int fetch_refs_from_bundle(struct transport
*transport
,
174 int nr_heads
, struct ref
**to_fetch
)
176 struct bundle_transport_data
*data
= transport
->data
;
177 struct strvec extra_index_pack_args
= STRVEC_INIT
;
180 if (transport
->progress
)
181 strvec_push(&extra_index_pack_args
, "-v");
183 if (!data
->get_refs_from_bundle_called
)
184 get_refs_from_bundle_inner(transport
);
185 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
186 &extra_index_pack_args
, 0);
187 transport
->hash_algo
= data
->header
.hash_algo
;
191 static int close_bundle(struct transport
*transport
)
193 struct bundle_transport_data
*data
= transport
->data
;
196 bundle_header_release(&data
->header
);
201 struct git_transport_data
{
202 struct git_transport_options options
;
203 struct child_process
*conn
;
205 unsigned finished_handshake
: 1;
206 enum protocol_version version
;
207 struct oid_array extra_have
;
208 struct oid_array shallow
;
211 static int set_git_option(struct git_transport_options
*opts
,
212 const char *name
, const char *value
)
214 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
215 opts
->uploadpack
= value
;
217 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
218 opts
->receivepack
= value
;
220 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
221 opts
->thin
= !!value
;
223 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
224 opts
->followtags
= !!value
;
226 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
227 opts
->keep
= !!value
;
229 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
230 opts
->update_shallow
= !!value
;
232 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
237 opts
->depth
= strtol(value
, &end
, 0);
239 die(_("transport: invalid depth option '%s'"), value
);
242 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
243 opts
->deepen_since
= value
;
245 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
246 opts
->deepen_not
= (const struct string_list
*)value
;
248 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
249 opts
->deepen_relative
= !!value
;
251 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
252 opts
->from_promisor
= !!value
;
254 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
255 list_objects_filter_die_if_populated(&opts
->filter_options
);
256 parse_list_objects_filter(&opts
->filter_options
, value
);
258 } else if (!strcmp(name
, TRANS_OPT_REFETCH
)) {
259 opts
->refetch
= !!value
;
261 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
262 opts
->reject_shallow
= !!value
;
268 static int connect_setup(struct transport
*transport
, int for_push
)
270 struct git_transport_data
*data
= transport
->data
;
271 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
276 switch (transport
->family
) {
277 case TRANSPORT_FAMILY_ALL
: break;
278 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
279 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
282 data
->conn
= git_connect(data
->fd
, transport
->url
,
283 for_push
? data
->options
.receivepack
:
284 data
->options
.uploadpack
,
290 static void die_if_server_options(struct transport
*transport
)
292 if (!transport
->server_options
|| !transport
->server_options
->nr
)
294 advise(_("see protocol.version in 'git help config' for more details"));
295 die(_("server options require protocol version 2 or later"));
299 * Obtains the protocol version from the transport and writes it to
300 * transport->data->version, first connecting if not already connected.
302 * If the protocol version is one that allows skipping the listing of remote
303 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
304 * this function returns NULL. Otherwise, this function returns the list of
307 static struct ref
*handshake(struct transport
*transport
, int for_push
,
308 struct transport_ls_refs_options
*options
,
311 struct git_transport_data
*data
= transport
->data
;
312 struct ref
*refs
= NULL
;
313 struct packet_reader reader
;
315 const char *server_sid
;
317 connect_setup(transport
, for_push
);
319 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
320 PACKET_READ_CHOMP_NEWLINE
|
321 PACKET_READ_GENTLE_ON_EOF
|
322 PACKET_READ_DIE_ON_ERR_PACKET
);
324 data
->version
= discover_version(&reader
);
325 switch (data
->version
) {
327 if (server_feature_v2("session-id", &server_sid
))
328 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
330 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
332 transport
->server_options
,
333 transport
->stateless_rpc
);
337 die_if_server_options(transport
);
338 get_remote_heads(&reader
, &refs
,
339 for_push
? REF_NORMAL
: 0,
342 server_sid
= server_feature_value("session-id", &sid_len
);
344 char *sid
= xstrndup(server_sid
, sid_len
);
345 trace2_data_string("transfer", NULL
, "server-sid", sid
);
349 case protocol_unknown_version
:
350 BUG("unknown protocol version");
352 data
->finished_handshake
= 1;
353 transport
->hash_algo
= reader
.hash_algo
;
355 if (reader
.line_peeked
)
356 BUG("buffer must be empty at the end of handshake()");
361 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
362 struct transport_ls_refs_options
*options
)
364 return handshake(transport
, for_push
, options
, 1);
367 static int get_bundle_uri(struct transport
*transport
)
369 struct git_transport_data
*data
= transport
->data
;
370 struct packet_reader reader
;
371 int stateless_rpc
= transport
->stateless_rpc
;
373 if (!transport
->bundles
) {
374 CALLOC_ARRAY(transport
->bundles
, 1);
375 init_bundle_list(transport
->bundles
);
378 if (!data
->finished_handshake
) {
379 struct ref
*refs
= handshake(transport
, 0, NULL
, 0);
386 * "Support" protocol v0 and v2 without bundle-uri support by
387 * silently degrading to a NOOP.
389 if (!server_supports_v2("bundle-uri"))
392 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
393 PACKET_READ_CHOMP_NEWLINE
|
394 PACKET_READ_GENTLE_ON_EOF
);
396 return get_remote_bundle_uri(data
->fd
[1], &reader
,
397 transport
->bundles
, stateless_rpc
);
400 static int fetch_refs_via_pack(struct transport
*transport
,
401 int nr_heads
, struct ref
**to_fetch
)
404 struct git_transport_data
*data
= transport
->data
;
405 struct ref
*refs
= NULL
;
406 struct fetch_pack_args args
;
407 struct ref
*refs_tmp
= NULL
;
409 memset(&args
, 0, sizeof(args
));
410 args
.uploadpack
= data
->options
.uploadpack
;
411 args
.keep_pack
= data
->options
.keep
;
413 args
.use_thin_pack
= data
->options
.thin
;
414 args
.include_tag
= data
->options
.followtags
;
415 args
.verbose
= (transport
->verbose
> 1);
416 args
.quiet
= (transport
->verbose
< 0);
417 args
.no_progress
= !transport
->progress
;
418 args
.depth
= data
->options
.depth
;
419 args
.deepen_since
= data
->options
.deepen_since
;
420 args
.deepen_not
= data
->options
.deepen_not
;
421 args
.deepen_relative
= data
->options
.deepen_relative
;
422 args
.check_self_contained_and_connected
=
423 data
->options
.check_self_contained_and_connected
;
424 args
.cloning
= transport
->cloning
;
425 args
.update_shallow
= data
->options
.update_shallow
;
426 args
.from_promisor
= data
->options
.from_promisor
;
427 list_objects_filter_copy(&args
.filter_options
,
428 &data
->options
.filter_options
);
429 args
.refetch
= data
->options
.refetch
;
430 args
.stateless_rpc
= transport
->stateless_rpc
;
431 args
.server_options
= transport
->server_options
;
432 args
.negotiation_tips
= data
->options
.negotiation_tips
;
433 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
435 if (!data
->finished_handshake
) {
437 int must_list_refs
= 0;
438 for (i
= 0; i
< nr_heads
; i
++) {
439 if (!to_fetch
[i
]->exact_oid
) {
444 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
447 if (data
->version
== protocol_unknown_version
)
448 BUG("unknown protocol version");
449 else if (data
->version
<= protocol_v1
)
450 die_if_server_options(transport
);
452 if (data
->options
.acked_commits
) {
453 if (data
->version
< protocol_v2
) {
454 warning(_("--negotiate-only requires protocol v2"));
456 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
457 warning(_("server does not support wait-for-done"));
460 negotiate_using_fetch(data
->options
.negotiation_tips
,
461 transport
->server_options
,
462 transport
->stateless_rpc
,
464 data
->options
.acked_commits
);
470 refs
= fetch_pack(&args
, data
->fd
,
471 refs_tmp
? refs_tmp
: transport
->remote_refs
,
472 to_fetch
, nr_heads
, &data
->shallow
,
473 &transport
->pack_lockfiles
, data
->version
);
475 data
->finished_handshake
= 0;
476 data
->options
.self_contained_and_connected
=
477 args
.self_contained_and_connected
;
478 data
->options
.connectivity_checked
= args
.connectivity_checked
;
482 if (report_unmatched_refs(to_fetch
, nr_heads
))
487 if (data
->fd
[1] >= 0)
489 if (finish_connect(data
->conn
))
495 list_objects_filter_release(&args
.filter_options
);
499 static int push_had_errors(struct ref
*ref
)
501 for (; ref
; ref
= ref
->next
) {
502 switch (ref
->status
) {
503 case REF_STATUS_NONE
:
504 case REF_STATUS_UPTODATE
:
514 int transport_refs_pushed(struct ref
*ref
)
516 for (; ref
; ref
= ref
->next
) {
517 switch(ref
->status
) {
518 case REF_STATUS_NONE
:
519 case REF_STATUS_UPTODATE
:
528 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
529 struct object_id
*new_oid
, int deletion
,
532 struct refspec_item rs
;
534 memset(&rs
, 0, sizeof(rs
));
538 if (!remote_find_tracking(remote
, &rs
)) {
540 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
542 delete_ref(NULL
, rs
.dst
, NULL
, 0);
544 update_ref("update by push", rs
.dst
, new_oid
,
550 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
553 struct object_id
*new_oid
;
554 struct ref_push_report
*report
;
556 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
559 report
= ref
->report
;
561 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
562 ref
->deletion
, verbose
);
564 for (; report
; report
= report
->next
) {
565 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
566 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
567 update_one_tracking_ref(remote
, refname
, new_oid
,
568 is_null_oid(new_oid
), verbose
);
572 static void print_ref_status(char flag
, const char *summary
,
573 struct ref
*to
, struct ref
*from
, const char *msg
,
574 struct ref_push_report
*report
,
575 int porcelain
, int summary_width
)
579 if (report
&& report
->ref_name
)
580 to_name
= report
->ref_name
;
586 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
588 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
590 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
592 fprintf(stdout
, "%s\n", summary
);
594 const char *red
= "", *reset
= "";
595 if (push_had_errors(to
)) {
596 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
597 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
599 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
602 fprintf(stderr
, "%s -> %s",
603 prettify_refname(from
->name
),
604 prettify_refname(to_name
));
606 fputs(prettify_refname(to_name
), stderr
);
616 static void print_ok_ref_status(struct ref
*ref
,
617 struct ref_push_report
*report
,
618 int porcelain
, int summary_width
)
620 struct object_id
*old_oid
;
621 struct object_id
*new_oid
;
622 const char *ref_name
;
625 if (report
&& report
->old_oid
)
626 old_oid
= report
->old_oid
;
628 old_oid
= &ref
->old_oid
;
629 if (report
&& report
->new_oid
)
630 new_oid
= report
->new_oid
;
632 new_oid
= &ref
->new_oid
;
633 if (report
&& report
->forced_update
)
634 forced_update
= report
->forced_update
;
636 forced_update
= ref
->forced_update
;
637 if (report
&& report
->ref_name
)
638 ref_name
= report
->ref_name
;
640 ref_name
= ref
->name
;
643 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
644 report
, porcelain
, summary_width
);
645 else if (is_null_oid(old_oid
))
646 print_ref_status('*',
647 (starts_with(ref_name
, "refs/tags/")
649 : (starts_with(ref_name
, "refs/heads/")
651 : "[new reference]")),
652 ref
, ref
->peer_ref
, NULL
,
653 report
, porcelain
, summary_width
);
655 struct strbuf quickref
= STRBUF_INIT
;
659 strbuf_add_unique_abbrev(&quickref
, old_oid
,
662 strbuf_addstr(&quickref
, "...");
664 msg
= "forced update";
666 strbuf_addstr(&quickref
, "..");
670 strbuf_add_unique_abbrev(&quickref
, new_oid
,
673 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
674 report
, porcelain
, summary_width
);
675 strbuf_release(&quickref
);
679 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
680 struct ref_push_report
*report
,
681 int porcelain
, int summary_width
)
684 char *url
= transport_anonymize_url(dest
);
685 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
689 switch(ref
->status
) {
690 case REF_STATUS_NONE
:
691 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
692 report
, porcelain
, summary_width
);
694 case REF_STATUS_REJECT_NODELETE
:
695 print_ref_status('!', "[rejected]", ref
, NULL
,
696 "remote does not support deleting refs",
697 report
, porcelain
, summary_width
);
699 case REF_STATUS_UPTODATE
:
700 print_ref_status('=', "[up to date]", ref
,
702 report
, porcelain
, summary_width
);
704 case REF_STATUS_REJECT_NONFASTFORWARD
:
705 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
707 report
, porcelain
, summary_width
);
709 case REF_STATUS_REJECT_ALREADY_EXISTS
:
710 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
712 report
, porcelain
, summary_width
);
714 case REF_STATUS_REJECT_FETCH_FIRST
:
715 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
717 report
, porcelain
, summary_width
);
719 case REF_STATUS_REJECT_NEEDS_FORCE
:
720 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
722 report
, porcelain
, summary_width
);
724 case REF_STATUS_REJECT_STALE
:
725 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
727 report
, porcelain
, summary_width
);
729 case REF_STATUS_REJECT_REMOTE_UPDATED
:
730 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
731 "remote ref updated since checkout",
732 report
, porcelain
, summary_width
);
734 case REF_STATUS_REJECT_SHALLOW
:
735 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
736 "new shallow roots not allowed",
737 report
, porcelain
, summary_width
);
739 case REF_STATUS_REMOTE_REJECT
:
740 print_ref_status('!', "[remote rejected]", ref
,
741 ref
->deletion
? NULL
: ref
->peer_ref
,
743 report
, porcelain
, summary_width
);
745 case REF_STATUS_EXPECTING_REPORT
:
746 print_ref_status('!', "[remote failure]", ref
,
747 ref
->deletion
? NULL
: ref
->peer_ref
,
748 "remote failed to report status",
749 report
, porcelain
, summary_width
);
751 case REF_STATUS_ATOMIC_PUSH_FAILED
:
752 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
753 "atomic push failed",
754 report
, porcelain
, summary_width
);
757 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
764 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
765 int porcelain
, int summary_width
)
767 struct ref_push_report
*report
;
771 return print_one_push_report(ref
, dest
, count
,
772 NULL
, porcelain
, summary_width
);
774 for (report
= ref
->report
; report
; report
= report
->next
)
775 print_one_push_report(ref
, dest
, count
+ n
++,
776 report
, porcelain
, summary_width
);
780 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
782 char hex
[GIT_MAX_HEXSZ
+ 1];
783 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
785 return (w
< sofar
) ? sofar
: w
;
788 int transport_summary_width(const struct ref
*refs
)
792 for (; refs
; refs
= refs
->next
) {
793 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
794 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
797 maxw
= FALLBACK_DEFAULT_ABBREV
;
798 return (2 * maxw
+ 3);
801 void transport_print_push_status(const char *dest
, struct ref
*refs
,
802 int verbose
, int porcelain
, unsigned int *reject_reasons
)
807 int summary_width
= transport_summary_width(refs
);
809 if (transport_color_config() < 0)
810 warning(_("could not parse transport.color.* config"));
812 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
815 for (ref
= refs
; ref
; ref
= ref
->next
)
816 if (ref
->status
== REF_STATUS_UPTODATE
)
817 n
+= print_one_push_status(ref
, dest
, n
,
818 porcelain
, summary_width
);
821 for (ref
= refs
; ref
; ref
= ref
->next
)
822 if (ref
->status
== REF_STATUS_OK
)
823 n
+= print_one_push_status(ref
, dest
, n
,
824 porcelain
, summary_width
);
827 for (ref
= refs
; ref
; ref
= ref
->next
) {
828 if (ref
->status
!= REF_STATUS_NONE
&&
829 ref
->status
!= REF_STATUS_UPTODATE
&&
830 ref
->status
!= REF_STATUS_OK
)
831 n
+= print_one_push_status(ref
, dest
, n
,
832 porcelain
, summary_width
);
833 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
834 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
835 *reject_reasons
|= REJECT_NON_FF_HEAD
;
837 *reject_reasons
|= REJECT_NON_FF_OTHER
;
838 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
839 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
840 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
841 *reject_reasons
|= REJECT_FETCH_FIRST
;
842 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
843 *reject_reasons
|= REJECT_NEEDS_FORCE
;
844 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
845 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
851 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
853 struct git_transport_data
*data
= transport
->data
;
854 struct send_pack_args args
;
857 if (transport_color_config() < 0)
860 if (!data
->finished_handshake
)
861 get_refs_via_connect(transport
, 1, NULL
);
863 memset(&args
, 0, sizeof(args
));
864 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
865 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
866 args
.use_thin_pack
= data
->options
.thin
;
867 args
.verbose
= (transport
->verbose
> 0);
868 args
.quiet
= (transport
->verbose
< 0);
869 args
.progress
= transport
->progress
;
870 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
871 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
872 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
873 args
.push_options
= transport
->push_options
;
874 args
.url
= transport
->url
;
876 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
877 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
878 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
879 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
881 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
883 switch (data
->version
) {
885 die(_("support for protocol v2 not implemented yet"));
889 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
892 case protocol_unknown_version
:
893 BUG("unknown protocol version");
899 * Atomic push may abort the connection early and close the pipe,
900 * which may cause an error for `finish_connect()`. Ignore this error
901 * for atomic git-push.
903 if (ret
|| args
.atomic
)
904 finish_connect(data
->conn
);
906 ret
= finish_connect(data
->conn
);
908 data
->finished_handshake
= 0;
913 static int connect_git(struct transport
*transport
, const char *name
,
914 const char *executable
, int fd
[2])
916 struct git_transport_data
*data
= transport
->data
;
917 data
->conn
= git_connect(data
->fd
, transport
->url
,
924 static int disconnect_git(struct transport
*transport
)
926 struct git_transport_data
*data
= transport
->data
;
928 if (data
->finished_handshake
&& !transport
->stateless_rpc
)
929 packet_flush(data
->fd
[1]);
931 if (data
->fd
[1] >= 0)
933 finish_connect(data
->conn
);
936 list_objects_filter_release(&data
->options
.filter_options
);
941 static struct transport_vtable taken_over_vtable
= {
942 .get_refs_list
= get_refs_via_connect
,
943 .get_bundle_uri
= get_bundle_uri
,
944 .fetch_refs
= fetch_refs_via_pack
,
945 .push_refs
= git_transport_push
,
946 .disconnect
= disconnect_git
949 void transport_take_over(struct transport
*transport
,
950 struct child_process
*child
)
952 struct git_transport_data
*data
;
954 if (!transport
->smart_options
)
955 BUG("taking over transport requires non-NULL "
956 "smart_options field.");
958 CALLOC_ARRAY(data
, 1);
959 data
->options
= *transport
->smart_options
;
961 data
->fd
[0] = data
->conn
->out
;
962 data
->fd
[1] = data
->conn
->in
;
963 data
->finished_handshake
= 0;
964 transport
->data
= data
;
966 transport
->vtable
= &taken_over_vtable
;
967 transport
->smart_options
= &(data
->options
);
969 transport
->cannot_reuse
= 1;
972 static int is_file(const char *url
)
977 return S_ISREG(buf
.st_mode
);
980 static int external_specification_len(const char *url
)
982 return strchr(url
, ':') - url
;
985 static const struct string_list
*protocol_allow_list(void)
987 static int enabled
= -1;
988 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
991 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
993 string_list_split(&allowed
, v
, ':', -1);
994 string_list_sort(&allowed
);
1001 return enabled
? &allowed
: NULL
;
1004 enum protocol_allow_config
{
1005 PROTOCOL_ALLOW_NEVER
= 0,
1006 PROTOCOL_ALLOW_USER_ONLY
,
1007 PROTOCOL_ALLOW_ALWAYS
1010 static enum protocol_allow_config
parse_protocol_config(const char *key
,
1013 if (!strcasecmp(value
, "always"))
1014 return PROTOCOL_ALLOW_ALWAYS
;
1015 else if (!strcasecmp(value
, "never"))
1016 return PROTOCOL_ALLOW_NEVER
;
1017 else if (!strcasecmp(value
, "user"))
1018 return PROTOCOL_ALLOW_USER_ONLY
;
1020 die(_("unknown value for config '%s': %s"), key
, value
);
1023 static enum protocol_allow_config
get_protocol_config(const char *type
)
1025 char *key
= xstrfmt("protocol.%s.allow", type
);
1028 /* first check the per-protocol config */
1029 if (!git_config_get_string(key
, &value
)) {
1030 enum protocol_allow_config ret
=
1031 parse_protocol_config(key
, value
);
1038 /* if defined, fallback to user-defined default for unknown protocols */
1039 if (!git_config_get_string("protocol.allow", &value
)) {
1040 enum protocol_allow_config ret
=
1041 parse_protocol_config("protocol.allow", value
);
1046 /* fallback to built-in defaults */
1048 if (!strcmp(type
, "http") ||
1049 !strcmp(type
, "https") ||
1050 !strcmp(type
, "git") ||
1051 !strcmp(type
, "ssh"))
1052 return PROTOCOL_ALLOW_ALWAYS
;
1054 /* known scary; err on the side of caution */
1055 if (!strcmp(type
, "ext"))
1056 return PROTOCOL_ALLOW_NEVER
;
1058 /* unknown; by default let them be used only directly by the user */
1059 return PROTOCOL_ALLOW_USER_ONLY
;
1062 int is_transport_allowed(const char *type
, int from_user
)
1064 const struct string_list
*allow_list
= protocol_allow_list();
1066 return string_list_has_string(allow_list
, type
);
1068 switch (get_protocol_config(type
)) {
1069 case PROTOCOL_ALLOW_ALWAYS
:
1071 case PROTOCOL_ALLOW_NEVER
:
1073 case PROTOCOL_ALLOW_USER_ONLY
:
1075 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1079 BUG("invalid protocol_allow_config type");
1082 void transport_check_allowed(const char *type
)
1084 if (!is_transport_allowed(type
, -1))
1085 die(_("transport '%s' not allowed"), type
);
1088 static struct transport_vtable bundle_vtable
= {
1089 .get_refs_list
= get_refs_from_bundle
,
1090 .fetch_refs
= fetch_refs_from_bundle
,
1091 .disconnect
= close_bundle
1094 static struct transport_vtable builtin_smart_vtable
= {
1095 .get_refs_list
= get_refs_via_connect
,
1096 .get_bundle_uri
= get_bundle_uri
,
1097 .fetch_refs
= fetch_refs_via_pack
,
1098 .push_refs
= git_transport_push
,
1099 .connect
= connect_git
,
1100 .disconnect
= disconnect_git
1103 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1106 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1108 ret
->progress
= isatty(2);
1109 string_list_init_dup(&ret
->pack_lockfiles
);
1111 CALLOC_ARRAY(ret
->bundles
, 1);
1112 init_bundle_list(ret
->bundles
);
1115 BUG("No remote provided to transport_get()");
1117 ret
->got_remote_refs
= 0;
1118 ret
->remote
= remote
;
1119 helper
= remote
->foreign_vcs
;
1121 if (!url
&& remote
->url
)
1122 url
= remote
->url
[0];
1125 /* maybe it is a foreign URL? */
1127 const char *p
= url
;
1129 while (is_urlschemechar(p
== url
, *p
))
1131 if (starts_with(p
, "::"))
1132 helper
= xstrndup(url
, p
- url
);
1136 transport_helper_init(ret
, helper
);
1137 } else if (starts_with(url
, "rsync:")) {
1138 die(_("git-over-rsync is no longer supported"));
1139 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1140 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1141 bundle_header_init(&data
->header
);
1142 transport_check_allowed("file");
1144 ret
->vtable
= &bundle_vtable
;
1145 ret
->smart_options
= NULL
;
1146 } else if (!is_url(url
)
1147 || starts_with(url
, "file://")
1148 || starts_with(url
, "git://")
1149 || starts_with(url
, "ssh://")
1150 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1151 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1154 * These are builtin smart transports; "allowed" transports
1155 * will be checked individually in git_connect.
1157 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1158 list_objects_filter_init(&data
->options
.filter_options
);
1160 ret
->vtable
= &builtin_smart_vtable
;
1161 ret
->smart_options
= &(data
->options
);
1164 data
->finished_handshake
= 0;
1166 /* Unknown protocol in URL. Pass to external handler. */
1167 int len
= external_specification_len(url
);
1168 char *handler
= xmemdupz(url
, len
);
1169 transport_helper_init(ret
, handler
);
1172 if (ret
->smart_options
) {
1173 ret
->smart_options
->thin
= 1;
1174 ret
->smart_options
->uploadpack
= "git-upload-pack";
1175 if (remote
->uploadpack
)
1176 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1177 ret
->smart_options
->receivepack
= "git-receive-pack";
1178 if (remote
->receivepack
)
1179 ret
->smart_options
->receivepack
= remote
->receivepack
;
1182 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1187 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1189 return transport
->hash_algo
;
1192 int transport_set_option(struct transport
*transport
,
1193 const char *name
, const char *value
)
1195 int git_reports
= 1, protocol_reports
= 1;
1197 if (transport
->smart_options
)
1198 git_reports
= set_git_option(transport
->smart_options
,
1201 if (transport
->vtable
->set_option
)
1202 protocol_reports
= transport
->vtable
->set_option(transport
,
1205 /* If either report is 0, report 0 (success). */
1206 if (!git_reports
|| !protocol_reports
)
1208 /* If either reports -1 (invalid value), report -1. */
1209 if ((git_reports
== -1) || (protocol_reports
== -1))
1211 /* Otherwise if both report unknown, report unknown. */
1215 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1219 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1221 transport
->verbose
= -1;
1224 * Rules used to determine whether to report progress (processing aborts
1225 * when a rule is satisfied):
1227 * . Report progress, if force_progress is 1 (ie. --progress).
1228 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1229 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1230 * . Report progress if isatty(2) is 1.
1232 if (force_progress
>= 0)
1233 transport
->progress
= !!force_progress
;
1235 transport
->progress
= verbosity
>= 0 && isatty(2);
1238 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1242 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1243 "not be found on any remote:\n"));
1244 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1245 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1246 fprintf(stderr
, _("\nPlease try\n\n"
1247 " git push --recurse-submodules=on-demand\n\n"
1248 "or cd to the path and use\n\n"
1250 "to push them to a remote.\n\n"));
1252 string_list_clear(needs_pushing
, 0);
1254 die(_("Aborting."));
1257 static int run_pre_push_hook(struct transport
*transport
,
1258 struct ref
*remote_refs
)
1262 struct child_process proc
= CHILD_PROCESS_INIT
;
1264 const char *hook_path
= find_hook("pre-push");
1269 strvec_push(&proc
.args
, hook_path
);
1270 strvec_push(&proc
.args
, transport
->remote
->name
);
1271 strvec_push(&proc
.args
, transport
->url
);
1274 proc
.trace2_hook_name
= "pre-push";
1276 if (start_command(&proc
)) {
1277 finish_command(&proc
);
1281 sigchain_push(SIGPIPE
, SIG_IGN
);
1283 strbuf_init(&buf
, 256);
1285 for (r
= remote_refs
; r
; r
= r
->next
) {
1286 if (!r
->peer_ref
) continue;
1287 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1288 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1289 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1290 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1293 strbuf_addf( &buf
, "%s %s %s %s\n",
1294 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1295 r
->name
, oid_to_hex(&r
->old_oid
));
1297 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1298 /* We do not mind if a hook does not read all refs. */
1305 strbuf_release(&buf
);
1311 sigchain_pop(SIGPIPE
);
1313 x
= finish_command(&proc
);
1320 int transport_push(struct repository
*r
,
1321 struct transport
*transport
,
1322 struct refspec
*rs
, int flags
,
1323 unsigned int *reject_reasons
)
1325 struct ref
*remote_refs
= NULL
;
1326 struct ref
*local_refs
= NULL
;
1327 int match_flags
= MATCH_REFS_NONE
;
1328 int verbose
= (transport
->verbose
> 0);
1329 int quiet
= (transport
->verbose
< 0);
1330 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1331 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1334 struct transport_ls_refs_options transport_options
=
1335 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1337 *reject_reasons
= 0;
1339 if (transport_color_config() < 0)
1342 if (!transport
->vtable
->push_refs
)
1345 local_refs
= get_local_heads();
1347 if (check_push_refs(local_refs
, rs
) < 0)
1350 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1352 trace2_region_enter("transport_push", "get_refs_list", r
);
1353 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1354 &transport_options
);
1355 trace2_region_leave("transport_push", "get_refs_list", r
);
1357 transport_ls_refs_options_release(&transport_options
);
1359 if (flags
& TRANSPORT_PUSH_ALL
)
1360 match_flags
|= MATCH_REFS_ALL
;
1361 if (flags
& TRANSPORT_PUSH_MIRROR
)
1362 match_flags
|= MATCH_REFS_MIRROR
;
1363 if (flags
& TRANSPORT_PUSH_PRUNE
)
1364 match_flags
|= MATCH_REFS_PRUNE
;
1365 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1366 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1368 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1371 if (transport
->smart_options
&&
1372 transport
->smart_options
->cas
&&
1373 !is_empty_cas(transport
->smart_options
->cas
))
1374 apply_push_cas(transport
->smart_options
->cas
,
1375 transport
->remote
, remote_refs
);
1377 set_ref_status_for_push(remote_refs
,
1378 flags
& TRANSPORT_PUSH_MIRROR
,
1379 flags
& TRANSPORT_PUSH_FORCE
);
1381 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1382 if (run_pre_push_hook(transport
, remote_refs
))
1385 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1386 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1387 !is_bare_repository()) {
1388 struct ref
*ref
= remote_refs
;
1389 struct oid_array commits
= OID_ARRAY_INIT
;
1391 trace2_region_enter("transport_push", "push_submodules", r
);
1392 for (; ref
; ref
= ref
->next
)
1393 if (!is_null_oid(&ref
->new_oid
))
1394 oid_array_append(&commits
,
1397 if (!push_unpushed_submodules(r
,
1401 transport
->push_options
,
1403 oid_array_clear(&commits
);
1404 trace2_region_leave("transport_push", "push_submodules", r
);
1405 die(_("failed to push all needed submodules"));
1407 oid_array_clear(&commits
);
1408 trace2_region_leave("transport_push", "push_submodules", r
);
1411 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1412 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1413 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1414 !pretend
)) && !is_bare_repository()) {
1415 struct ref
*ref
= remote_refs
;
1416 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1417 struct oid_array commits
= OID_ARRAY_INIT
;
1419 trace2_region_enter("transport_push", "check_submodules", r
);
1420 for (; ref
; ref
= ref
->next
)
1421 if (!is_null_oid(&ref
->new_oid
))
1422 oid_array_append(&commits
,
1425 if (find_unpushed_submodules(r
,
1427 transport
->remote
->name
,
1429 oid_array_clear(&commits
);
1430 trace2_region_leave("transport_push", "check_submodules", r
);
1431 die_with_unpushed_submodules(&needs_pushing
);
1433 string_list_clear(&needs_pushing
, 0);
1434 oid_array_clear(&commits
);
1435 trace2_region_leave("transport_push", "check_submodules", r
);
1438 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1439 trace2_region_enter("transport_push", "push_refs", r
);
1440 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1441 trace2_region_leave("transport_push", "push_refs", r
);
1444 err
= push_had_errors(remote_refs
);
1445 ret
= push_ret
| err
;
1448 transport_print_push_status(transport
->url
, remote_refs
,
1449 verbose
| porcelain
, porcelain
,
1452 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1453 set_upstreams(transport
, remote_refs
, pretend
);
1455 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1456 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1458 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1459 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1462 if (porcelain
&& !push_ret
)
1464 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1465 fprintf(stderr
, "Everything up-to-date\n");
1468 free_refs(local_refs
);
1469 free_refs(remote_refs
);
1473 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1474 struct transport_ls_refs_options
*transport_options
)
1476 if (!transport
->got_remote_refs
) {
1477 transport
->remote_refs
=
1478 transport
->vtable
->get_refs_list(transport
, 0,
1480 transport
->got_remote_refs
= 1;
1483 return transport
->remote_refs
;
1486 void transport_ls_refs_options_release(struct transport_ls_refs_options
*opts
)
1488 strvec_clear(&opts
->ref_prefixes
);
1489 free((char *)opts
->unborn_head_target
);
1492 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1495 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1496 struct ref
**heads
= NULL
;
1499 for (rm
= refs
; rm
; rm
= rm
->next
) {
1502 !is_null_oid(&rm
->old_oid
) &&
1503 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1505 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1506 heads
[nr_heads
++] = rm
;
1511 * When deepening of a shallow repository is requested,
1512 * then local and remote refs are likely to still be equal.
1513 * Just feed them all to the fetch method in that case.
1514 * This condition shouldn't be met in a non-deepening fetch
1515 * (see builtin/fetch.c:quickfetch()).
1517 ALLOC_ARRAY(heads
, nr_refs
);
1518 for (rm
= refs
; rm
; rm
= rm
->next
)
1519 heads
[nr_heads
++] = rm
;
1522 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1528 int transport_get_remote_bundle_uri(struct transport
*transport
)
1531 const struct transport_vtable
*vtable
= transport
->vtable
;
1533 /* Check config only once. */
1534 if (transport
->got_remote_bundle_uri
)
1536 transport
->got_remote_bundle_uri
= 1;
1539 * Don't request bundle-uri from the server unless configured to
1540 * do so by the transfer.bundleURI=true config option.
1542 if (git_config_get_bool("transfer.bundleuri", &value
) || !value
)
1545 if (!transport
->bundles
->baseURI
)
1546 transport
->bundles
->baseURI
= xstrdup(transport
->url
);
1548 if (!vtable
->get_bundle_uri
)
1549 return error(_("bundle-uri operation not supported by protocol"));
1551 if (vtable
->get_bundle_uri(transport
) < 0)
1552 return error(_("could not retrieve server-advertised bundle-uri list"));
1556 void transport_unlock_pack(struct transport
*transport
, unsigned int flags
)
1558 int in_signal_handler
= !!(flags
& TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER
);
1561 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1562 if (in_signal_handler
)
1563 unlink(transport
->pack_lockfiles
.items
[i
].string
);
1565 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1566 if (!in_signal_handler
)
1567 string_list_clear(&transport
->pack_lockfiles
, 0);
1570 int transport_connect(struct transport
*transport
, const char *name
,
1571 const char *exec
, int fd
[2])
1573 if (transport
->vtable
->connect
)
1574 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1576 die(_("operation not supported by protocol"));
1579 int transport_disconnect(struct transport
*transport
)
1582 if (transport
->vtable
->disconnect
)
1583 ret
= transport
->vtable
->disconnect(transport
);
1584 if (transport
->got_remote_refs
)
1585 free_refs((void *)transport
->remote_refs
);
1586 clear_bundle_list(transport
->bundles
);
1587 free(transport
->bundles
);
1593 * Strip username (and password) from a URL and return
1594 * it in a newly allocated string.
1596 char *transport_anonymize_url(const char *url
)
1598 char *scheme_prefix
, *anon_part
;
1599 size_t anon_len
, prefix_len
= 0;
1601 anon_part
= strchr(url
, '@');
1602 if (url_is_local_not_ssh(url
) || !anon_part
)
1605 anon_len
= strlen(++anon_part
);
1606 scheme_prefix
= strstr(url
, "://");
1607 if (!scheme_prefix
) {
1608 if (!strchr(anon_part
, ':'))
1609 /* cannot be "me@there:/path/name" */
1613 /* make sure scheme is reasonable */
1614 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1617 case '+': case '.': case '-':
1626 /* @ past the first slash does not count */
1627 cp
= strchr(scheme_prefix
+ 3, '/');
1628 if (cp
&& cp
< anon_part
)
1630 prefix_len
= scheme_prefix
- url
+ 3;
1632 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1633 (int)anon_len
, anon_part
);
1635 return xstrdup(url
);