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-store.h"
31 #include "bundle-uri.h"
34 static int transport_use_color
= -1;
35 static char transport_colors
[][COLOR_MAXLEN
] = {
37 GIT_COLOR_RED
/* REJECTED */
40 enum color_transport
{
41 TRANSPORT_COLOR_RESET
= 0,
42 TRANSPORT_COLOR_REJECTED
= 1
45 static int transport_color_config(void)
47 const char *keys
[] = {
48 "color.transport.reset",
49 "color.transport.rejected"
50 }, *key
= "color.transport";
53 static int initialized
;
59 if (!git_config_get_string(key
, &value
))
60 transport_use_color
= git_config_colorbool(key
, value
);
62 if (!want_color_stderr(transport_use_color
))
65 for (i
= 0; i
< ARRAY_SIZE(keys
); i
++)
66 if (!git_config_get_string(keys
[i
], &value
)) {
68 return config_error_nonbool(keys
[i
]);
69 if (color_parse(value
, transport_colors
[i
]) < 0)
76 static const char *transport_get_color(enum color_transport ix
)
78 if (want_color_stderr(transport_use_color
))
79 return transport_colors
[ix
];
83 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
87 for (ref
= refs
; ref
; ref
= ref
->next
) {
88 const char *localname
;
90 const char *remotename
;
93 * Check suitability for tracking. Must be successful /
94 * already up-to-date ref create/modify (not delete).
96 if (ref
->status
!= REF_STATUS_OK
&&
97 ref
->status
!= REF_STATUS_UPTODATE
)
101 if (is_null_oid(&ref
->new_oid
))
104 /* Follow symbolic refs (mainly for HEAD). */
105 localname
= ref
->peer_ref
->name
;
106 remotename
= ref
->name
;
107 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
109 if (tmp
&& flag
& REF_ISSYMREF
&&
110 starts_with(tmp
, "refs/heads/"))
113 /* Both source and destination must be local branches. */
114 if (!localname
|| !starts_with(localname
, "refs/heads/"))
116 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
120 int flag
= transport
->verbose
< 0 ? 0 : BRANCH_CONFIG_VERBOSE
;
121 install_branch_config(flag
, localname
+ 11,
122 transport
->remote
->name
, remotename
);
123 } else if (transport
->verbose
>= 0)
124 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
125 localname
+ 11, remotename
+ 11,
126 transport
->remote
->name
);
130 struct bundle_transport_data
{
132 struct bundle_header header
;
133 unsigned get_refs_from_bundle_called
: 1;
136 static void get_refs_from_bundle_inner(struct transport
*transport
)
138 struct bundle_transport_data
*data
= transport
->data
;
140 data
->get_refs_from_bundle_called
= 1;
144 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
146 die(_("could not read bundle '%s'"), transport
->url
);
148 transport
->hash_algo
= data
->header
.hash_algo
;
151 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
153 struct transport_ls_refs_options
*transport_options UNUSED
)
155 struct bundle_transport_data
*data
= transport
->data
;
156 struct ref
*result
= NULL
;
162 get_refs_from_bundle_inner(transport
);
164 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
165 struct string_list_item
*e
= data
->header
.references
.items
+ i
;
166 const char *name
= e
->string
;
167 struct ref
*ref
= alloc_ref(name
);
168 struct object_id
*oid
= e
->util
;
169 oidcpy(&ref
->old_oid
, oid
);
176 static int fetch_refs_from_bundle(struct transport
*transport
,
177 int nr_heads
, struct ref
**to_fetch
)
179 struct bundle_transport_data
*data
= transport
->data
;
180 struct strvec extra_index_pack_args
= STRVEC_INIT
;
183 if (transport
->progress
)
184 strvec_push(&extra_index_pack_args
, "-v");
186 if (!data
->get_refs_from_bundle_called
)
187 get_refs_from_bundle_inner(transport
);
188 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
189 &extra_index_pack_args
, 0);
190 transport
->hash_algo
= data
->header
.hash_algo
;
194 static int close_bundle(struct transport
*transport
)
196 struct bundle_transport_data
*data
= transport
->data
;
199 bundle_header_release(&data
->header
);
204 struct git_transport_data
{
205 struct git_transport_options options
;
206 struct child_process
*conn
;
208 unsigned finished_handshake
: 1;
209 enum protocol_version version
;
210 struct oid_array extra_have
;
211 struct oid_array shallow
;
214 static int set_git_option(struct git_transport_options
*opts
,
215 const char *name
, const char *value
)
217 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
218 opts
->uploadpack
= value
;
220 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
221 opts
->receivepack
= value
;
223 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
224 opts
->thin
= !!value
;
226 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
227 opts
->followtags
= !!value
;
229 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
230 opts
->keep
= !!value
;
232 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
233 opts
->update_shallow
= !!value
;
235 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
240 opts
->depth
= strtol(value
, &end
, 0);
242 die(_("transport: invalid depth option '%s'"), value
);
245 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
246 opts
->deepen_since
= value
;
248 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
249 opts
->deepen_not
= (const struct string_list
*)value
;
251 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
252 opts
->deepen_relative
= !!value
;
254 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
255 opts
->from_promisor
= !!value
;
257 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
258 list_objects_filter_die_if_populated(&opts
->filter_options
);
259 parse_list_objects_filter(&opts
->filter_options
, value
);
261 } else if (!strcmp(name
, TRANS_OPT_REFETCH
)) {
262 opts
->refetch
= !!value
;
264 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
265 opts
->reject_shallow
= !!value
;
271 static int connect_setup(struct transport
*transport
, int for_push
)
273 struct git_transport_data
*data
= transport
->data
;
274 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
279 switch (transport
->family
) {
280 case TRANSPORT_FAMILY_ALL
: break;
281 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
282 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
285 data
->conn
= git_connect(data
->fd
, transport
->url
,
286 for_push
? data
->options
.receivepack
:
287 data
->options
.uploadpack
,
293 static void die_if_server_options(struct transport
*transport
)
295 if (!transport
->server_options
|| !transport
->server_options
->nr
)
297 advise(_("see protocol.version in 'git help config' for more details"));
298 die(_("server options require protocol version 2 or later"));
302 * Obtains the protocol version from the transport and writes it to
303 * transport->data->version, first connecting if not already connected.
305 * If the protocol version is one that allows skipping the listing of remote
306 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
307 * this function returns NULL. Otherwise, this function returns the list of
310 static struct ref
*handshake(struct transport
*transport
, int for_push
,
311 struct transport_ls_refs_options
*options
,
314 struct git_transport_data
*data
= transport
->data
;
315 struct ref
*refs
= NULL
;
316 struct packet_reader reader
;
318 const char *server_sid
;
320 connect_setup(transport
, for_push
);
322 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
323 PACKET_READ_CHOMP_NEWLINE
|
324 PACKET_READ_GENTLE_ON_EOF
|
325 PACKET_READ_DIE_ON_ERR_PACKET
);
327 data
->version
= discover_version(&reader
);
328 switch (data
->version
) {
330 if (server_feature_v2("session-id", &server_sid
))
331 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
333 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
335 transport
->server_options
,
336 transport
->stateless_rpc
);
340 die_if_server_options(transport
);
341 get_remote_heads(&reader
, &refs
,
342 for_push
? REF_NORMAL
: 0,
345 server_sid
= server_feature_value("session-id", &sid_len
);
347 char *sid
= xstrndup(server_sid
, sid_len
);
348 trace2_data_string("transfer", NULL
, "server-sid", sid
);
352 case protocol_unknown_version
:
353 BUG("unknown protocol version");
355 data
->finished_handshake
= 1;
356 transport
->hash_algo
= reader
.hash_algo
;
358 if (reader
.line_peeked
)
359 BUG("buffer must be empty at the end of handshake()");
364 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
365 struct transport_ls_refs_options
*options
)
367 return handshake(transport
, for_push
, options
, 1);
370 static int get_bundle_uri(struct transport
*transport
)
372 struct git_transport_data
*data
= transport
->data
;
373 struct packet_reader reader
;
374 int stateless_rpc
= transport
->stateless_rpc
;
376 if (!transport
->bundles
) {
377 CALLOC_ARRAY(transport
->bundles
, 1);
378 init_bundle_list(transport
->bundles
);
381 if (!data
->finished_handshake
) {
382 struct ref
*refs
= handshake(transport
, 0, NULL
, 0);
389 * "Support" protocol v0 and v2 without bundle-uri support by
390 * silently degrading to a NOOP.
392 if (!server_supports_v2("bundle-uri"))
395 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
396 PACKET_READ_CHOMP_NEWLINE
|
397 PACKET_READ_GENTLE_ON_EOF
);
399 return get_remote_bundle_uri(data
->fd
[1], &reader
,
400 transport
->bundles
, stateless_rpc
);
403 static int fetch_refs_via_pack(struct transport
*transport
,
404 int nr_heads
, struct ref
**to_fetch
)
407 struct git_transport_data
*data
= transport
->data
;
408 struct ref
*refs
= NULL
;
409 struct fetch_pack_args args
;
410 struct ref
*refs_tmp
= NULL
;
412 memset(&args
, 0, sizeof(args
));
413 args
.uploadpack
= data
->options
.uploadpack
;
414 args
.keep_pack
= data
->options
.keep
;
416 args
.use_thin_pack
= data
->options
.thin
;
417 args
.include_tag
= data
->options
.followtags
;
418 args
.verbose
= (transport
->verbose
> 1);
419 args
.quiet
= (transport
->verbose
< 0);
420 args
.no_progress
= !transport
->progress
;
421 args
.depth
= data
->options
.depth
;
422 args
.deepen_since
= data
->options
.deepen_since
;
423 args
.deepen_not
= data
->options
.deepen_not
;
424 args
.deepen_relative
= data
->options
.deepen_relative
;
425 args
.check_self_contained_and_connected
=
426 data
->options
.check_self_contained_and_connected
;
427 args
.cloning
= transport
->cloning
;
428 args
.update_shallow
= data
->options
.update_shallow
;
429 args
.from_promisor
= data
->options
.from_promisor
;
430 list_objects_filter_copy(&args
.filter_options
,
431 &data
->options
.filter_options
);
432 args
.refetch
= data
->options
.refetch
;
433 args
.stateless_rpc
= transport
->stateless_rpc
;
434 args
.server_options
= transport
->server_options
;
435 args
.negotiation_tips
= data
->options
.negotiation_tips
;
436 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
438 if (!data
->finished_handshake
) {
440 int must_list_refs
= 0;
441 for (i
= 0; i
< nr_heads
; i
++) {
442 if (!to_fetch
[i
]->exact_oid
) {
447 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
450 if (data
->version
== protocol_unknown_version
)
451 BUG("unknown protocol version");
452 else if (data
->version
<= protocol_v1
)
453 die_if_server_options(transport
);
455 if (data
->options
.acked_commits
) {
456 if (data
->version
< protocol_v2
) {
457 warning(_("--negotiate-only requires protocol v2"));
459 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
460 warning(_("server does not support wait-for-done"));
463 negotiate_using_fetch(data
->options
.negotiation_tips
,
464 transport
->server_options
,
465 transport
->stateless_rpc
,
467 data
->options
.acked_commits
);
473 refs
= fetch_pack(&args
, data
->fd
,
474 refs_tmp
? refs_tmp
: transport
->remote_refs
,
475 to_fetch
, nr_heads
, &data
->shallow
,
476 &transport
->pack_lockfiles
, data
->version
);
478 data
->finished_handshake
= 0;
479 data
->options
.self_contained_and_connected
=
480 args
.self_contained_and_connected
;
481 data
->options
.connectivity_checked
= args
.connectivity_checked
;
485 if (report_unmatched_refs(to_fetch
, nr_heads
))
490 if (data
->fd
[1] >= 0)
492 if (finish_connect(data
->conn
))
498 list_objects_filter_release(&args
.filter_options
);
502 static int push_had_errors(struct ref
*ref
)
504 for (; ref
; ref
= ref
->next
) {
505 switch (ref
->status
) {
506 case REF_STATUS_NONE
:
507 case REF_STATUS_UPTODATE
:
517 int transport_refs_pushed(struct ref
*ref
)
519 for (; ref
; ref
= ref
->next
) {
520 switch(ref
->status
) {
521 case REF_STATUS_NONE
:
522 case REF_STATUS_UPTODATE
:
531 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
532 struct object_id
*new_oid
, int deletion
,
535 struct refspec_item rs
;
537 memset(&rs
, 0, sizeof(rs
));
541 if (!remote_find_tracking(remote
, &rs
)) {
543 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
545 delete_ref(NULL
, rs
.dst
, NULL
, 0);
547 update_ref("update by push", rs
.dst
, new_oid
,
553 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
556 struct object_id
*new_oid
;
557 struct ref_push_report
*report
;
559 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
562 report
= ref
->report
;
564 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
565 ref
->deletion
, verbose
);
567 for (; report
; report
= report
->next
) {
568 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
569 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
570 update_one_tracking_ref(remote
, refname
, new_oid
,
571 is_null_oid(new_oid
), verbose
);
575 static void print_ref_status(char flag
, const char *summary
,
576 struct ref
*to
, struct ref
*from
, const char *msg
,
577 struct ref_push_report
*report
,
578 int porcelain
, int summary_width
)
582 if (report
&& report
->ref_name
)
583 to_name
= report
->ref_name
;
589 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
591 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
593 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
595 fprintf(stdout
, "%s\n", summary
);
597 const char *red
= "", *reset
= "";
598 if (push_had_errors(to
)) {
599 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
600 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
602 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
605 fprintf(stderr
, "%s -> %s",
606 prettify_refname(from
->name
),
607 prettify_refname(to_name
));
609 fputs(prettify_refname(to_name
), stderr
);
619 static void print_ok_ref_status(struct ref
*ref
,
620 struct ref_push_report
*report
,
621 int porcelain
, int summary_width
)
623 struct object_id
*old_oid
;
624 struct object_id
*new_oid
;
625 const char *ref_name
;
628 if (report
&& report
->old_oid
)
629 old_oid
= report
->old_oid
;
631 old_oid
= &ref
->old_oid
;
632 if (report
&& report
->new_oid
)
633 new_oid
= report
->new_oid
;
635 new_oid
= &ref
->new_oid
;
636 if (report
&& report
->forced_update
)
637 forced_update
= report
->forced_update
;
639 forced_update
= ref
->forced_update
;
640 if (report
&& report
->ref_name
)
641 ref_name
= report
->ref_name
;
643 ref_name
= ref
->name
;
646 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
647 report
, porcelain
, summary_width
);
648 else if (is_null_oid(old_oid
))
649 print_ref_status('*',
650 (starts_with(ref_name
, "refs/tags/")
652 : (starts_with(ref_name
, "refs/heads/")
654 : "[new reference]")),
655 ref
, ref
->peer_ref
, NULL
,
656 report
, porcelain
, summary_width
);
658 struct strbuf quickref
= STRBUF_INIT
;
662 strbuf_add_unique_abbrev(&quickref
, old_oid
,
665 strbuf_addstr(&quickref
, "...");
667 msg
= "forced update";
669 strbuf_addstr(&quickref
, "..");
673 strbuf_add_unique_abbrev(&quickref
, new_oid
,
676 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
677 report
, porcelain
, summary_width
);
678 strbuf_release(&quickref
);
682 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
683 struct ref_push_report
*report
,
684 int porcelain
, int summary_width
)
687 char *url
= transport_anonymize_url(dest
);
688 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
692 switch(ref
->status
) {
693 case REF_STATUS_NONE
:
694 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
695 report
, porcelain
, summary_width
);
697 case REF_STATUS_REJECT_NODELETE
:
698 print_ref_status('!', "[rejected]", ref
, NULL
,
699 "remote does not support deleting refs",
700 report
, porcelain
, summary_width
);
702 case REF_STATUS_UPTODATE
:
703 print_ref_status('=', "[up to date]", ref
,
705 report
, porcelain
, summary_width
);
707 case REF_STATUS_REJECT_NONFASTFORWARD
:
708 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
710 report
, porcelain
, summary_width
);
712 case REF_STATUS_REJECT_ALREADY_EXISTS
:
713 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
715 report
, porcelain
, summary_width
);
717 case REF_STATUS_REJECT_FETCH_FIRST
:
718 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
720 report
, porcelain
, summary_width
);
722 case REF_STATUS_REJECT_NEEDS_FORCE
:
723 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
725 report
, porcelain
, summary_width
);
727 case REF_STATUS_REJECT_STALE
:
728 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
730 report
, porcelain
, summary_width
);
732 case REF_STATUS_REJECT_REMOTE_UPDATED
:
733 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
734 "remote ref updated since checkout",
735 report
, porcelain
, summary_width
);
737 case REF_STATUS_REJECT_SHALLOW
:
738 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
739 "new shallow roots not allowed",
740 report
, porcelain
, summary_width
);
742 case REF_STATUS_REMOTE_REJECT
:
743 print_ref_status('!', "[remote rejected]", ref
,
744 ref
->deletion
? NULL
: ref
->peer_ref
,
746 report
, porcelain
, summary_width
);
748 case REF_STATUS_EXPECTING_REPORT
:
749 print_ref_status('!', "[remote failure]", ref
,
750 ref
->deletion
? NULL
: ref
->peer_ref
,
751 "remote failed to report status",
752 report
, porcelain
, summary_width
);
754 case REF_STATUS_ATOMIC_PUSH_FAILED
:
755 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
756 "atomic push failed",
757 report
, porcelain
, summary_width
);
760 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
767 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
768 int porcelain
, int summary_width
)
770 struct ref_push_report
*report
;
774 return print_one_push_report(ref
, dest
, count
,
775 NULL
, porcelain
, summary_width
);
777 for (report
= ref
->report
; report
; report
= report
->next
)
778 print_one_push_report(ref
, dest
, count
+ n
++,
779 report
, porcelain
, summary_width
);
783 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
785 char hex
[GIT_MAX_HEXSZ
+ 1];
786 int w
= repo_find_unique_abbrev_r(the_repository
, hex
, oid
,
789 return (w
< sofar
) ? sofar
: w
;
792 int transport_summary_width(const struct ref
*refs
)
796 for (; refs
; refs
= refs
->next
) {
797 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
798 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
801 maxw
= FALLBACK_DEFAULT_ABBREV
;
802 return (2 * maxw
+ 3);
805 void transport_print_push_status(const char *dest
, struct ref
*refs
,
806 int verbose
, int porcelain
, unsigned int *reject_reasons
)
811 int summary_width
= transport_summary_width(refs
);
813 if (transport_color_config() < 0)
814 warning(_("could not parse transport.color.* config"));
816 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
819 for (ref
= refs
; ref
; ref
= ref
->next
)
820 if (ref
->status
== REF_STATUS_UPTODATE
)
821 n
+= print_one_push_status(ref
, dest
, n
,
822 porcelain
, summary_width
);
825 for (ref
= refs
; ref
; ref
= ref
->next
)
826 if (ref
->status
== REF_STATUS_OK
)
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_NONE
&&
833 ref
->status
!= REF_STATUS_UPTODATE
&&
834 ref
->status
!= REF_STATUS_OK
)
835 n
+= print_one_push_status(ref
, dest
, n
,
836 porcelain
, summary_width
);
837 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
838 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
839 *reject_reasons
|= REJECT_NON_FF_HEAD
;
841 *reject_reasons
|= REJECT_NON_FF_OTHER
;
842 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
843 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
844 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
845 *reject_reasons
|= REJECT_FETCH_FIRST
;
846 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
847 *reject_reasons
|= REJECT_NEEDS_FORCE
;
848 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
849 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
855 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
857 struct git_transport_data
*data
= transport
->data
;
858 struct send_pack_args args
;
861 if (transport_color_config() < 0)
864 if (!data
->finished_handshake
)
865 get_refs_via_connect(transport
, 1, NULL
);
867 memset(&args
, 0, sizeof(args
));
868 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
869 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
870 args
.use_thin_pack
= data
->options
.thin
;
871 args
.verbose
= (transport
->verbose
> 0);
872 args
.quiet
= (transport
->verbose
< 0);
873 args
.progress
= transport
->progress
;
874 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
875 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
876 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
877 args
.push_options
= transport
->push_options
;
878 args
.url
= transport
->url
;
880 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
881 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
882 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
883 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
885 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
887 switch (data
->version
) {
889 die(_("support for protocol v2 not implemented yet"));
893 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
896 case protocol_unknown_version
:
897 BUG("unknown protocol version");
903 * Atomic push may abort the connection early and close the pipe,
904 * which may cause an error for `finish_connect()`. Ignore this error
905 * for atomic git-push.
907 if (ret
|| args
.atomic
)
908 finish_connect(data
->conn
);
910 ret
= finish_connect(data
->conn
);
912 data
->finished_handshake
= 0;
917 static int connect_git(struct transport
*transport
, const char *name
,
918 const char *executable
, int fd
[2])
920 struct git_transport_data
*data
= transport
->data
;
921 data
->conn
= git_connect(data
->fd
, transport
->url
,
928 static int disconnect_git(struct transport
*transport
)
930 struct git_transport_data
*data
= transport
->data
;
932 if (data
->finished_handshake
&& !transport
->stateless_rpc
)
933 packet_flush(data
->fd
[1]);
935 if (data
->fd
[1] >= 0)
937 finish_connect(data
->conn
);
940 list_objects_filter_release(&data
->options
.filter_options
);
945 static struct transport_vtable taken_over_vtable
= {
946 .get_refs_list
= get_refs_via_connect
,
947 .get_bundle_uri
= get_bundle_uri
,
948 .fetch_refs
= fetch_refs_via_pack
,
949 .push_refs
= git_transport_push
,
950 .disconnect
= disconnect_git
953 void transport_take_over(struct transport
*transport
,
954 struct child_process
*child
)
956 struct git_transport_data
*data
;
958 if (!transport
->smart_options
)
959 BUG("taking over transport requires non-NULL "
960 "smart_options field.");
962 CALLOC_ARRAY(data
, 1);
963 data
->options
= *transport
->smart_options
;
965 data
->fd
[0] = data
->conn
->out
;
966 data
->fd
[1] = data
->conn
->in
;
967 data
->finished_handshake
= 0;
968 transport
->data
= data
;
970 transport
->vtable
= &taken_over_vtable
;
971 transport
->smart_options
= &(data
->options
);
973 transport
->cannot_reuse
= 1;
976 static int is_file(const char *url
)
981 return S_ISREG(buf
.st_mode
);
984 static int external_specification_len(const char *url
)
986 return strchr(url
, ':') - url
;
989 static const struct string_list
*protocol_allow_list(void)
991 static int enabled
= -1;
992 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
995 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
997 string_list_split(&allowed
, v
, ':', -1);
998 string_list_sort(&allowed
);
1005 return enabled
? &allowed
: NULL
;
1008 enum protocol_allow_config
{
1009 PROTOCOL_ALLOW_NEVER
= 0,
1010 PROTOCOL_ALLOW_USER_ONLY
,
1011 PROTOCOL_ALLOW_ALWAYS
1014 static enum protocol_allow_config
parse_protocol_config(const char *key
,
1017 if (!strcasecmp(value
, "always"))
1018 return PROTOCOL_ALLOW_ALWAYS
;
1019 else if (!strcasecmp(value
, "never"))
1020 return PROTOCOL_ALLOW_NEVER
;
1021 else if (!strcasecmp(value
, "user"))
1022 return PROTOCOL_ALLOW_USER_ONLY
;
1024 die(_("unknown value for config '%s': %s"), key
, value
);
1027 static enum protocol_allow_config
get_protocol_config(const char *type
)
1029 char *key
= xstrfmt("protocol.%s.allow", type
);
1032 /* first check the per-protocol config */
1033 if (!git_config_get_string(key
, &value
)) {
1034 enum protocol_allow_config ret
=
1035 parse_protocol_config(key
, value
);
1042 /* if defined, fallback to user-defined default for unknown protocols */
1043 if (!git_config_get_string("protocol.allow", &value
)) {
1044 enum protocol_allow_config ret
=
1045 parse_protocol_config("protocol.allow", value
);
1050 /* fallback to built-in defaults */
1052 if (!strcmp(type
, "http") ||
1053 !strcmp(type
, "https") ||
1054 !strcmp(type
, "git") ||
1055 !strcmp(type
, "ssh"))
1056 return PROTOCOL_ALLOW_ALWAYS
;
1058 /* known scary; err on the side of caution */
1059 if (!strcmp(type
, "ext"))
1060 return PROTOCOL_ALLOW_NEVER
;
1062 /* unknown; by default let them be used only directly by the user */
1063 return PROTOCOL_ALLOW_USER_ONLY
;
1066 int is_transport_allowed(const char *type
, int from_user
)
1068 const struct string_list
*allow_list
= protocol_allow_list();
1070 return string_list_has_string(allow_list
, type
);
1072 switch (get_protocol_config(type
)) {
1073 case PROTOCOL_ALLOW_ALWAYS
:
1075 case PROTOCOL_ALLOW_NEVER
:
1077 case PROTOCOL_ALLOW_USER_ONLY
:
1079 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1083 BUG("invalid protocol_allow_config type");
1086 void transport_check_allowed(const char *type
)
1088 if (!is_transport_allowed(type
, -1))
1089 die(_("transport '%s' not allowed"), type
);
1092 static struct transport_vtable bundle_vtable
= {
1093 .get_refs_list
= get_refs_from_bundle
,
1094 .fetch_refs
= fetch_refs_from_bundle
,
1095 .disconnect
= close_bundle
1098 static struct transport_vtable builtin_smart_vtable
= {
1099 .get_refs_list
= get_refs_via_connect
,
1100 .get_bundle_uri
= get_bundle_uri
,
1101 .fetch_refs
= fetch_refs_via_pack
,
1102 .push_refs
= git_transport_push
,
1103 .connect
= connect_git
,
1104 .disconnect
= disconnect_git
1107 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1110 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1112 ret
->progress
= isatty(2);
1113 string_list_init_dup(&ret
->pack_lockfiles
);
1115 CALLOC_ARRAY(ret
->bundles
, 1);
1116 init_bundle_list(ret
->bundles
);
1119 BUG("No remote provided to transport_get()");
1121 ret
->got_remote_refs
= 0;
1122 ret
->remote
= remote
;
1123 helper
= remote
->foreign_vcs
;
1125 if (!url
&& remote
->url
)
1126 url
= remote
->url
[0];
1129 /* maybe it is a foreign URL? */
1131 const char *p
= url
;
1133 while (is_urlschemechar(p
== url
, *p
))
1135 if (starts_with(p
, "::"))
1136 helper
= xstrndup(url
, p
- url
);
1140 transport_helper_init(ret
, helper
);
1141 } else if (starts_with(url
, "rsync:")) {
1142 die(_("git-over-rsync is no longer supported"));
1143 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1144 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1145 bundle_header_init(&data
->header
);
1146 transport_check_allowed("file");
1148 ret
->vtable
= &bundle_vtable
;
1149 ret
->smart_options
= NULL
;
1150 } else if (!is_url(url
)
1151 || starts_with(url
, "file://")
1152 || starts_with(url
, "git://")
1153 || starts_with(url
, "ssh://")
1154 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1155 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1158 * These are builtin smart transports; "allowed" transports
1159 * will be checked individually in git_connect.
1161 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1162 list_objects_filter_init(&data
->options
.filter_options
);
1164 ret
->vtable
= &builtin_smart_vtable
;
1165 ret
->smart_options
= &(data
->options
);
1168 data
->finished_handshake
= 0;
1170 /* Unknown protocol in URL. Pass to external handler. */
1171 int len
= external_specification_len(url
);
1172 char *handler
= xmemdupz(url
, len
);
1173 transport_helper_init(ret
, handler
);
1176 if (ret
->smart_options
) {
1177 ret
->smart_options
->thin
= 1;
1178 ret
->smart_options
->uploadpack
= "git-upload-pack";
1179 if (remote
->uploadpack
)
1180 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1181 ret
->smart_options
->receivepack
= "git-receive-pack";
1182 if (remote
->receivepack
)
1183 ret
->smart_options
->receivepack
= remote
->receivepack
;
1186 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1191 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1193 return transport
->hash_algo
;
1196 int transport_set_option(struct transport
*transport
,
1197 const char *name
, const char *value
)
1199 int git_reports
= 1, protocol_reports
= 1;
1201 if (transport
->smart_options
)
1202 git_reports
= set_git_option(transport
->smart_options
,
1205 if (transport
->vtable
->set_option
)
1206 protocol_reports
= transport
->vtable
->set_option(transport
,
1209 /* If either report is 0, report 0 (success). */
1210 if (!git_reports
|| !protocol_reports
)
1212 /* If either reports -1 (invalid value), report -1. */
1213 if ((git_reports
== -1) || (protocol_reports
== -1))
1215 /* Otherwise if both report unknown, report unknown. */
1219 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1223 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1225 transport
->verbose
= -1;
1228 * Rules used to determine whether to report progress (processing aborts
1229 * when a rule is satisfied):
1231 * . Report progress, if force_progress is 1 (ie. --progress).
1232 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1233 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1234 * . Report progress if isatty(2) is 1.
1236 if (force_progress
>= 0)
1237 transport
->progress
= !!force_progress
;
1239 transport
->progress
= verbosity
>= 0 && isatty(2);
1242 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1246 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1247 "not be found on any remote:\n"));
1248 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1249 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1250 fprintf(stderr
, _("\nPlease try\n\n"
1251 " git push --recurse-submodules=on-demand\n\n"
1252 "or cd to the path and use\n\n"
1254 "to push them to a remote.\n\n"));
1256 string_list_clear(needs_pushing
, 0);
1258 die(_("Aborting."));
1261 static int run_pre_push_hook(struct transport
*transport
,
1262 struct ref
*remote_refs
)
1266 struct child_process proc
= CHILD_PROCESS_INIT
;
1268 const char *hook_path
= find_hook("pre-push");
1273 strvec_push(&proc
.args
, hook_path
);
1274 strvec_push(&proc
.args
, transport
->remote
->name
);
1275 strvec_push(&proc
.args
, transport
->url
);
1278 proc
.trace2_hook_name
= "pre-push";
1280 if (start_command(&proc
)) {
1281 finish_command(&proc
);
1285 sigchain_push(SIGPIPE
, SIG_IGN
);
1287 strbuf_init(&buf
, 256);
1289 for (r
= remote_refs
; r
; r
= r
->next
) {
1290 if (!r
->peer_ref
) continue;
1291 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1292 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1293 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1294 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1297 strbuf_addf( &buf
, "%s %s %s %s\n",
1298 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1299 r
->name
, oid_to_hex(&r
->old_oid
));
1301 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1302 /* We do not mind if a hook does not read all refs. */
1309 strbuf_release(&buf
);
1315 sigchain_pop(SIGPIPE
);
1317 x
= finish_command(&proc
);
1324 int transport_push(struct repository
*r
,
1325 struct transport
*transport
,
1326 struct refspec
*rs
, int flags
,
1327 unsigned int *reject_reasons
)
1329 struct ref
*remote_refs
= NULL
;
1330 struct ref
*local_refs
= NULL
;
1331 int match_flags
= MATCH_REFS_NONE
;
1332 int verbose
= (transport
->verbose
> 0);
1333 int quiet
= (transport
->verbose
< 0);
1334 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1335 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1338 struct transport_ls_refs_options transport_options
=
1339 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1341 *reject_reasons
= 0;
1343 if (transport_color_config() < 0)
1346 if (!transport
->vtable
->push_refs
)
1349 local_refs
= get_local_heads();
1351 if (check_push_refs(local_refs
, rs
) < 0)
1354 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1356 trace2_region_enter("transport_push", "get_refs_list", r
);
1357 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1358 &transport_options
);
1359 trace2_region_leave("transport_push", "get_refs_list", r
);
1361 transport_ls_refs_options_release(&transport_options
);
1363 if (flags
& TRANSPORT_PUSH_ALL
)
1364 match_flags
|= MATCH_REFS_ALL
;
1365 if (flags
& TRANSPORT_PUSH_MIRROR
)
1366 match_flags
|= MATCH_REFS_MIRROR
;
1367 if (flags
& TRANSPORT_PUSH_PRUNE
)
1368 match_flags
|= MATCH_REFS_PRUNE
;
1369 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1370 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1372 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1375 if (transport
->smart_options
&&
1376 transport
->smart_options
->cas
&&
1377 !is_empty_cas(transport
->smart_options
->cas
))
1378 apply_push_cas(transport
->smart_options
->cas
,
1379 transport
->remote
, remote_refs
);
1381 set_ref_status_for_push(remote_refs
,
1382 flags
& TRANSPORT_PUSH_MIRROR
,
1383 flags
& TRANSPORT_PUSH_FORCE
);
1385 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1386 if (run_pre_push_hook(transport
, remote_refs
))
1389 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1390 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1391 !is_bare_repository()) {
1392 struct ref
*ref
= remote_refs
;
1393 struct oid_array commits
= OID_ARRAY_INIT
;
1395 trace2_region_enter("transport_push", "push_submodules", r
);
1396 for (; ref
; ref
= ref
->next
)
1397 if (!is_null_oid(&ref
->new_oid
))
1398 oid_array_append(&commits
,
1401 if (!push_unpushed_submodules(r
,
1405 transport
->push_options
,
1407 oid_array_clear(&commits
);
1408 trace2_region_leave("transport_push", "push_submodules", r
);
1409 die(_("failed to push all needed submodules"));
1411 oid_array_clear(&commits
);
1412 trace2_region_leave("transport_push", "push_submodules", r
);
1415 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1416 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1417 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1418 !pretend
)) && !is_bare_repository()) {
1419 struct ref
*ref
= remote_refs
;
1420 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1421 struct oid_array commits
= OID_ARRAY_INIT
;
1423 trace2_region_enter("transport_push", "check_submodules", r
);
1424 for (; ref
; ref
= ref
->next
)
1425 if (!is_null_oid(&ref
->new_oid
))
1426 oid_array_append(&commits
,
1429 if (find_unpushed_submodules(r
,
1431 transport
->remote
->name
,
1433 oid_array_clear(&commits
);
1434 trace2_region_leave("transport_push", "check_submodules", r
);
1435 die_with_unpushed_submodules(&needs_pushing
);
1437 string_list_clear(&needs_pushing
, 0);
1438 oid_array_clear(&commits
);
1439 trace2_region_leave("transport_push", "check_submodules", r
);
1442 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1443 trace2_region_enter("transport_push", "push_refs", r
);
1444 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1445 trace2_region_leave("transport_push", "push_refs", r
);
1448 err
= push_had_errors(remote_refs
);
1449 ret
= push_ret
| err
;
1452 transport_print_push_status(transport
->url
, remote_refs
,
1453 verbose
| porcelain
, porcelain
,
1456 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1457 set_upstreams(transport
, remote_refs
, pretend
);
1459 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1460 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1462 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1463 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1466 if (porcelain
&& !push_ret
)
1468 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1469 fprintf(stderr
, "Everything up-to-date\n");
1472 free_refs(local_refs
);
1473 free_refs(remote_refs
);
1477 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1478 struct transport_ls_refs_options
*transport_options
)
1480 if (!transport
->got_remote_refs
) {
1481 transport
->remote_refs
=
1482 transport
->vtable
->get_refs_list(transport
, 0,
1484 transport
->got_remote_refs
= 1;
1487 return transport
->remote_refs
;
1490 void transport_ls_refs_options_release(struct transport_ls_refs_options
*opts
)
1492 strvec_clear(&opts
->ref_prefixes
);
1493 free((char *)opts
->unborn_head_target
);
1496 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1499 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1500 struct ref
**heads
= NULL
;
1503 for (rm
= refs
; rm
; rm
= rm
->next
) {
1506 !is_null_oid(&rm
->old_oid
) &&
1507 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1509 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1510 heads
[nr_heads
++] = rm
;
1515 * When deepening of a shallow repository is requested,
1516 * then local and remote refs are likely to still be equal.
1517 * Just feed them all to the fetch method in that case.
1518 * This condition shouldn't be met in a non-deepening fetch
1519 * (see builtin/fetch.c:quickfetch()).
1521 ALLOC_ARRAY(heads
, nr_refs
);
1522 for (rm
= refs
; rm
; rm
= rm
->next
)
1523 heads
[nr_heads
++] = rm
;
1526 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1532 int transport_get_remote_bundle_uri(struct transport
*transport
)
1535 const struct transport_vtable
*vtable
= transport
->vtable
;
1537 /* Check config only once. */
1538 if (transport
->got_remote_bundle_uri
)
1540 transport
->got_remote_bundle_uri
= 1;
1543 * Don't request bundle-uri from the server unless configured to
1544 * do so by the transfer.bundleURI=true config option.
1546 if (git_config_get_bool("transfer.bundleuri", &value
) || !value
)
1549 if (!transport
->bundles
->baseURI
)
1550 transport
->bundles
->baseURI
= xstrdup(transport
->url
);
1552 if (!vtable
->get_bundle_uri
)
1553 return error(_("bundle-uri operation not supported by protocol"));
1555 if (vtable
->get_bundle_uri(transport
) < 0)
1556 return error(_("could not retrieve server-advertised bundle-uri list"));
1560 void transport_unlock_pack(struct transport
*transport
, unsigned int flags
)
1562 int in_signal_handler
= !!(flags
& TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER
);
1565 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1566 if (in_signal_handler
)
1567 unlink(transport
->pack_lockfiles
.items
[i
].string
);
1569 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1570 if (!in_signal_handler
)
1571 string_list_clear(&transport
->pack_lockfiles
, 0);
1574 int transport_connect(struct transport
*transport
, const char *name
,
1575 const char *exec
, int fd
[2])
1577 if (transport
->vtable
->connect
)
1578 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1580 die(_("operation not supported by protocol"));
1583 int transport_disconnect(struct transport
*transport
)
1586 if (transport
->vtable
->disconnect
)
1587 ret
= transport
->vtable
->disconnect(transport
);
1588 if (transport
->got_remote_refs
)
1589 free_refs((void *)transport
->remote_refs
);
1590 clear_bundle_list(transport
->bundles
);
1591 free(transport
->bundles
);
1597 * Strip username (and password) from a URL and return
1598 * it in a newly allocated string.
1600 char *transport_anonymize_url(const char *url
)
1602 char *scheme_prefix
, *anon_part
;
1603 size_t anon_len
, prefix_len
= 0;
1605 anon_part
= strchr(url
, '@');
1606 if (url_is_local_not_ssh(url
) || !anon_part
)
1609 anon_len
= strlen(++anon_part
);
1610 scheme_prefix
= strstr(url
, "://");
1611 if (!scheme_prefix
) {
1612 if (!strchr(anon_part
, ':'))
1613 /* cannot be "me@there:/path/name" */
1617 /* make sure scheme is reasonable */
1618 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1621 case '+': case '.': case '-':
1630 /* @ past the first slash does not count */
1631 cp
= strchr(scheme_prefix
+ 3, '/');
1632 if (cp
&& cp
< anon_part
)
1634 prefix_len
= scheme_prefix
- url
+ 3;
1636 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1637 (int)anon_len
, anon_part
);
1639 return xstrdup(url
);