4 #include "environment.h"
9 #include "fetch-pack.h"
12 #include "send-pack.h"
21 #include "submodule.h"
22 #include "string-list.h"
23 #include "oid-array.h"
25 #include "transport-internal.h"
27 #include "object-store.h"
29 #include "bundle-uri.h"
32 static int transport_use_color
= -1;
33 static char transport_colors
[][COLOR_MAXLEN
] = {
35 GIT_COLOR_RED
/* REJECTED */
38 enum color_transport
{
39 TRANSPORT_COLOR_RESET
= 0,
40 TRANSPORT_COLOR_REJECTED
= 1
43 static int transport_color_config(void)
45 const char *keys
[] = {
46 "color.transport.reset",
47 "color.transport.rejected"
48 }, *key
= "color.transport";
51 static int initialized
;
57 if (!git_config_get_string(key
, &value
))
58 transport_use_color
= git_config_colorbool(key
, value
);
60 if (!want_color_stderr(transport_use_color
))
63 for (i
= 0; i
< ARRAY_SIZE(keys
); i
++)
64 if (!git_config_get_string(keys
[i
], &value
)) {
66 return config_error_nonbool(keys
[i
]);
67 if (color_parse(value
, transport_colors
[i
]) < 0)
74 static const char *transport_get_color(enum color_transport ix
)
76 if (want_color_stderr(transport_use_color
))
77 return transport_colors
[ix
];
81 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
85 for (ref
= refs
; ref
; ref
= ref
->next
) {
86 const char *localname
;
88 const char *remotename
;
91 * Check suitability for tracking. Must be successful /
92 * already up-to-date ref create/modify (not delete).
94 if (ref
->status
!= REF_STATUS_OK
&&
95 ref
->status
!= REF_STATUS_UPTODATE
)
99 if (is_null_oid(&ref
->new_oid
))
102 /* Follow symbolic refs (mainly for HEAD). */
103 localname
= ref
->peer_ref
->name
;
104 remotename
= ref
->name
;
105 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
107 if (tmp
&& flag
& REF_ISSYMREF
&&
108 starts_with(tmp
, "refs/heads/"))
111 /* Both source and destination must be local branches. */
112 if (!localname
|| !starts_with(localname
, "refs/heads/"))
114 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
118 int flag
= transport
->verbose
< 0 ? 0 : BRANCH_CONFIG_VERBOSE
;
119 install_branch_config(flag
, localname
+ 11,
120 transport
->remote
->name
, remotename
);
121 } else if (transport
->verbose
>= 0)
122 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
123 localname
+ 11, remotename
+ 11,
124 transport
->remote
->name
);
128 struct bundle_transport_data
{
130 struct bundle_header header
;
131 unsigned get_refs_from_bundle_called
: 1;
134 static void get_refs_from_bundle_inner(struct transport
*transport
)
136 struct bundle_transport_data
*data
= transport
->data
;
138 data
->get_refs_from_bundle_called
= 1;
142 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
144 die(_("could not read bundle '%s'"), transport
->url
);
146 transport
->hash_algo
= data
->header
.hash_algo
;
149 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
151 struct transport_ls_refs_options
*transport_options UNUSED
)
153 struct bundle_transport_data
*data
= transport
->data
;
154 struct ref
*result
= NULL
;
160 get_refs_from_bundle_inner(transport
);
162 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
163 struct string_list_item
*e
= data
->header
.references
.items
+ i
;
164 const char *name
= e
->string
;
165 struct ref
*ref
= alloc_ref(name
);
166 struct object_id
*oid
= e
->util
;
167 oidcpy(&ref
->old_oid
, oid
);
174 static int fetch_refs_from_bundle(struct transport
*transport
,
175 int nr_heads
, struct ref
**to_fetch
)
177 struct bundle_transport_data
*data
= transport
->data
;
178 struct strvec extra_index_pack_args
= STRVEC_INIT
;
181 if (transport
->progress
)
182 strvec_push(&extra_index_pack_args
, "-v");
184 if (!data
->get_refs_from_bundle_called
)
185 get_refs_from_bundle_inner(transport
);
186 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
187 &extra_index_pack_args
, 0);
188 transport
->hash_algo
= data
->header
.hash_algo
;
192 static int close_bundle(struct transport
*transport
)
194 struct bundle_transport_data
*data
= transport
->data
;
197 bundle_header_release(&data
->header
);
202 struct git_transport_data
{
203 struct git_transport_options options
;
204 struct child_process
*conn
;
206 unsigned finished_handshake
: 1;
207 enum protocol_version version
;
208 struct oid_array extra_have
;
209 struct oid_array shallow
;
212 static int set_git_option(struct git_transport_options
*opts
,
213 const char *name
, const char *value
)
215 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
216 opts
->uploadpack
= value
;
218 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
219 opts
->receivepack
= value
;
221 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
222 opts
->thin
= !!value
;
224 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
225 opts
->followtags
= !!value
;
227 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
228 opts
->keep
= !!value
;
230 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
231 opts
->update_shallow
= !!value
;
233 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
238 opts
->depth
= strtol(value
, &end
, 0);
240 die(_("transport: invalid depth option '%s'"), value
);
243 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
244 opts
->deepen_since
= value
;
246 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
247 opts
->deepen_not
= (const struct string_list
*)value
;
249 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
250 opts
->deepen_relative
= !!value
;
252 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
253 opts
->from_promisor
= !!value
;
255 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
256 list_objects_filter_die_if_populated(&opts
->filter_options
);
257 parse_list_objects_filter(&opts
->filter_options
, value
);
259 } else if (!strcmp(name
, TRANS_OPT_REFETCH
)) {
260 opts
->refetch
= !!value
;
262 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
263 opts
->reject_shallow
= !!value
;
269 static int connect_setup(struct transport
*transport
, int for_push
)
271 struct git_transport_data
*data
= transport
->data
;
272 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
277 switch (transport
->family
) {
278 case TRANSPORT_FAMILY_ALL
: break;
279 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
280 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
283 data
->conn
= git_connect(data
->fd
, transport
->url
,
284 for_push
? data
->options
.receivepack
:
285 data
->options
.uploadpack
,
291 static void die_if_server_options(struct transport
*transport
)
293 if (!transport
->server_options
|| !transport
->server_options
->nr
)
295 advise(_("see protocol.version in 'git help config' for more details"));
296 die(_("server options require protocol version 2 or later"));
300 * Obtains the protocol version from the transport and writes it to
301 * transport->data->version, first connecting if not already connected.
303 * If the protocol version is one that allows skipping the listing of remote
304 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
305 * this function returns NULL. Otherwise, this function returns the list of
308 static struct ref
*handshake(struct transport
*transport
, int for_push
,
309 struct transport_ls_refs_options
*options
,
312 struct git_transport_data
*data
= transport
->data
;
313 struct ref
*refs
= NULL
;
314 struct packet_reader reader
;
316 const char *server_sid
;
318 connect_setup(transport
, for_push
);
320 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
321 PACKET_READ_CHOMP_NEWLINE
|
322 PACKET_READ_GENTLE_ON_EOF
|
323 PACKET_READ_DIE_ON_ERR_PACKET
);
325 data
->version
= discover_version(&reader
);
326 switch (data
->version
) {
328 if (server_feature_v2("session-id", &server_sid
))
329 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
331 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
333 transport
->server_options
,
334 transport
->stateless_rpc
);
338 die_if_server_options(transport
);
339 get_remote_heads(&reader
, &refs
,
340 for_push
? REF_NORMAL
: 0,
343 server_sid
= server_feature_value("session-id", &sid_len
);
345 char *sid
= xstrndup(server_sid
, sid_len
);
346 trace2_data_string("transfer", NULL
, "server-sid", sid
);
350 case protocol_unknown_version
:
351 BUG("unknown protocol version");
353 data
->finished_handshake
= 1;
354 transport
->hash_algo
= reader
.hash_algo
;
356 if (reader
.line_peeked
)
357 BUG("buffer must be empty at the end of handshake()");
362 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
363 struct transport_ls_refs_options
*options
)
365 return handshake(transport
, for_push
, options
, 1);
368 static int get_bundle_uri(struct transport
*transport
)
370 struct git_transport_data
*data
= transport
->data
;
371 struct packet_reader reader
;
372 int stateless_rpc
= transport
->stateless_rpc
;
374 if (!transport
->bundles
) {
375 CALLOC_ARRAY(transport
->bundles
, 1);
376 init_bundle_list(transport
->bundles
);
379 if (!data
->finished_handshake
) {
380 struct ref
*refs
= handshake(transport
, 0, NULL
, 0);
387 * "Support" protocol v0 and v2 without bundle-uri support by
388 * silently degrading to a NOOP.
390 if (!server_supports_v2("bundle-uri"))
393 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
394 PACKET_READ_CHOMP_NEWLINE
|
395 PACKET_READ_GENTLE_ON_EOF
);
397 return get_remote_bundle_uri(data
->fd
[1], &reader
,
398 transport
->bundles
, stateless_rpc
);
401 static int fetch_refs_via_pack(struct transport
*transport
,
402 int nr_heads
, struct ref
**to_fetch
)
405 struct git_transport_data
*data
= transport
->data
;
406 struct ref
*refs
= NULL
;
407 struct fetch_pack_args args
;
408 struct ref
*refs_tmp
= NULL
;
410 memset(&args
, 0, sizeof(args
));
411 args
.uploadpack
= data
->options
.uploadpack
;
412 args
.keep_pack
= data
->options
.keep
;
414 args
.use_thin_pack
= data
->options
.thin
;
415 args
.include_tag
= data
->options
.followtags
;
416 args
.verbose
= (transport
->verbose
> 1);
417 args
.quiet
= (transport
->verbose
< 0);
418 args
.no_progress
= !transport
->progress
;
419 args
.depth
= data
->options
.depth
;
420 args
.deepen_since
= data
->options
.deepen_since
;
421 args
.deepen_not
= data
->options
.deepen_not
;
422 args
.deepen_relative
= data
->options
.deepen_relative
;
423 args
.check_self_contained_and_connected
=
424 data
->options
.check_self_contained_and_connected
;
425 args
.cloning
= transport
->cloning
;
426 args
.update_shallow
= data
->options
.update_shallow
;
427 args
.from_promisor
= data
->options
.from_promisor
;
428 list_objects_filter_copy(&args
.filter_options
,
429 &data
->options
.filter_options
);
430 args
.refetch
= data
->options
.refetch
;
431 args
.stateless_rpc
= transport
->stateless_rpc
;
432 args
.server_options
= transport
->server_options
;
433 args
.negotiation_tips
= data
->options
.negotiation_tips
;
434 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
436 if (!data
->finished_handshake
) {
438 int must_list_refs
= 0;
439 for (i
= 0; i
< nr_heads
; i
++) {
440 if (!to_fetch
[i
]->exact_oid
) {
445 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
448 if (data
->version
== protocol_unknown_version
)
449 BUG("unknown protocol version");
450 else if (data
->version
<= protocol_v1
)
451 die_if_server_options(transport
);
453 if (data
->options
.acked_commits
) {
454 if (data
->version
< protocol_v2
) {
455 warning(_("--negotiate-only requires protocol v2"));
457 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
458 warning(_("server does not support wait-for-done"));
461 negotiate_using_fetch(data
->options
.negotiation_tips
,
462 transport
->server_options
,
463 transport
->stateless_rpc
,
465 data
->options
.acked_commits
);
471 refs
= fetch_pack(&args
, data
->fd
,
472 refs_tmp
? refs_tmp
: transport
->remote_refs
,
473 to_fetch
, nr_heads
, &data
->shallow
,
474 &transport
->pack_lockfiles
, data
->version
);
476 data
->finished_handshake
= 0;
477 data
->options
.self_contained_and_connected
=
478 args
.self_contained_and_connected
;
479 data
->options
.connectivity_checked
= args
.connectivity_checked
;
483 if (report_unmatched_refs(to_fetch
, nr_heads
))
488 if (data
->fd
[1] >= 0)
490 if (finish_connect(data
->conn
))
496 list_objects_filter_release(&args
.filter_options
);
500 static int push_had_errors(struct ref
*ref
)
502 for (; ref
; ref
= ref
->next
) {
503 switch (ref
->status
) {
504 case REF_STATUS_NONE
:
505 case REF_STATUS_UPTODATE
:
515 int transport_refs_pushed(struct ref
*ref
)
517 for (; ref
; ref
= ref
->next
) {
518 switch(ref
->status
) {
519 case REF_STATUS_NONE
:
520 case REF_STATUS_UPTODATE
:
529 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
530 struct object_id
*new_oid
, int deletion
,
533 struct refspec_item rs
;
535 memset(&rs
, 0, sizeof(rs
));
539 if (!remote_find_tracking(remote
, &rs
)) {
541 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
543 delete_ref(NULL
, rs
.dst
, NULL
, 0);
545 update_ref("update by push", rs
.dst
, new_oid
,
551 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
554 struct object_id
*new_oid
;
555 struct ref_push_report
*report
;
557 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
560 report
= ref
->report
;
562 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
563 ref
->deletion
, verbose
);
565 for (; report
; report
= report
->next
) {
566 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
567 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
568 update_one_tracking_ref(remote
, refname
, new_oid
,
569 is_null_oid(new_oid
), verbose
);
573 static void print_ref_status(char flag
, const char *summary
,
574 struct ref
*to
, struct ref
*from
, const char *msg
,
575 struct ref_push_report
*report
,
576 int porcelain
, int summary_width
)
580 if (report
&& report
->ref_name
)
581 to_name
= report
->ref_name
;
587 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
589 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
591 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
593 fprintf(stdout
, "%s\n", summary
);
595 const char *red
= "", *reset
= "";
596 if (push_had_errors(to
)) {
597 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
598 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
600 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
603 fprintf(stderr
, "%s -> %s",
604 prettify_refname(from
->name
),
605 prettify_refname(to_name
));
607 fputs(prettify_refname(to_name
), stderr
);
617 static void print_ok_ref_status(struct ref
*ref
,
618 struct ref_push_report
*report
,
619 int porcelain
, int summary_width
)
621 struct object_id
*old_oid
;
622 struct object_id
*new_oid
;
623 const char *ref_name
;
626 if (report
&& report
->old_oid
)
627 old_oid
= report
->old_oid
;
629 old_oid
= &ref
->old_oid
;
630 if (report
&& report
->new_oid
)
631 new_oid
= report
->new_oid
;
633 new_oid
= &ref
->new_oid
;
634 if (report
&& report
->forced_update
)
635 forced_update
= report
->forced_update
;
637 forced_update
= ref
->forced_update
;
638 if (report
&& report
->ref_name
)
639 ref_name
= report
->ref_name
;
641 ref_name
= ref
->name
;
644 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
645 report
, porcelain
, summary_width
);
646 else if (is_null_oid(old_oid
))
647 print_ref_status('*',
648 (starts_with(ref_name
, "refs/tags/")
650 : (starts_with(ref_name
, "refs/heads/")
652 : "[new reference]")),
653 ref
, ref
->peer_ref
, NULL
,
654 report
, porcelain
, summary_width
);
656 struct strbuf quickref
= STRBUF_INIT
;
660 strbuf_add_unique_abbrev(&quickref
, old_oid
,
663 strbuf_addstr(&quickref
, "...");
665 msg
= "forced update";
667 strbuf_addstr(&quickref
, "..");
671 strbuf_add_unique_abbrev(&quickref
, new_oid
,
674 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
675 report
, porcelain
, summary_width
);
676 strbuf_release(&quickref
);
680 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
681 struct ref_push_report
*report
,
682 int porcelain
, int summary_width
)
685 char *url
= transport_anonymize_url(dest
);
686 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
690 switch(ref
->status
) {
691 case REF_STATUS_NONE
:
692 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
693 report
, porcelain
, summary_width
);
695 case REF_STATUS_REJECT_NODELETE
:
696 print_ref_status('!', "[rejected]", ref
, NULL
,
697 "remote does not support deleting refs",
698 report
, porcelain
, summary_width
);
700 case REF_STATUS_UPTODATE
:
701 print_ref_status('=', "[up to date]", ref
,
703 report
, porcelain
, summary_width
);
705 case REF_STATUS_REJECT_NONFASTFORWARD
:
706 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
708 report
, porcelain
, summary_width
);
710 case REF_STATUS_REJECT_ALREADY_EXISTS
:
711 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
713 report
, porcelain
, summary_width
);
715 case REF_STATUS_REJECT_FETCH_FIRST
:
716 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
718 report
, porcelain
, summary_width
);
720 case REF_STATUS_REJECT_NEEDS_FORCE
:
721 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
723 report
, porcelain
, summary_width
);
725 case REF_STATUS_REJECT_STALE
:
726 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
728 report
, porcelain
, summary_width
);
730 case REF_STATUS_REJECT_REMOTE_UPDATED
:
731 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
732 "remote ref updated since checkout",
733 report
, porcelain
, summary_width
);
735 case REF_STATUS_REJECT_SHALLOW
:
736 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
737 "new shallow roots not allowed",
738 report
, porcelain
, summary_width
);
740 case REF_STATUS_REMOTE_REJECT
:
741 print_ref_status('!', "[remote rejected]", ref
,
742 ref
->deletion
? NULL
: ref
->peer_ref
,
744 report
, porcelain
, summary_width
);
746 case REF_STATUS_EXPECTING_REPORT
:
747 print_ref_status('!', "[remote failure]", ref
,
748 ref
->deletion
? NULL
: ref
->peer_ref
,
749 "remote failed to report status",
750 report
, porcelain
, summary_width
);
752 case REF_STATUS_ATOMIC_PUSH_FAILED
:
753 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
754 "atomic push failed",
755 report
, porcelain
, summary_width
);
758 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
765 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
766 int porcelain
, int summary_width
)
768 struct ref_push_report
*report
;
772 return print_one_push_report(ref
, dest
, count
,
773 NULL
, porcelain
, summary_width
);
775 for (report
= ref
->report
; report
; report
= report
->next
)
776 print_one_push_report(ref
, dest
, count
+ n
++,
777 report
, porcelain
, summary_width
);
781 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
783 char hex
[GIT_MAX_HEXSZ
+ 1];
784 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
786 return (w
< sofar
) ? sofar
: w
;
789 int transport_summary_width(const struct ref
*refs
)
793 for (; refs
; refs
= refs
->next
) {
794 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
795 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
798 maxw
= FALLBACK_DEFAULT_ABBREV
;
799 return (2 * maxw
+ 3);
802 void transport_print_push_status(const char *dest
, struct ref
*refs
,
803 int verbose
, int porcelain
, unsigned int *reject_reasons
)
808 int summary_width
= transport_summary_width(refs
);
810 if (transport_color_config() < 0)
811 warning(_("could not parse transport.color.* config"));
813 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
816 for (ref
= refs
; ref
; ref
= ref
->next
)
817 if (ref
->status
== REF_STATUS_UPTODATE
)
818 n
+= print_one_push_status(ref
, dest
, n
,
819 porcelain
, summary_width
);
822 for (ref
= refs
; ref
; ref
= ref
->next
)
823 if (ref
->status
== REF_STATUS_OK
)
824 n
+= print_one_push_status(ref
, dest
, n
,
825 porcelain
, summary_width
);
828 for (ref
= refs
; ref
; ref
= ref
->next
) {
829 if (ref
->status
!= REF_STATUS_NONE
&&
830 ref
->status
!= REF_STATUS_UPTODATE
&&
831 ref
->status
!= REF_STATUS_OK
)
832 n
+= print_one_push_status(ref
, dest
, n
,
833 porcelain
, summary_width
);
834 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
835 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
836 *reject_reasons
|= REJECT_NON_FF_HEAD
;
838 *reject_reasons
|= REJECT_NON_FF_OTHER
;
839 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
840 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
841 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
842 *reject_reasons
|= REJECT_FETCH_FIRST
;
843 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
844 *reject_reasons
|= REJECT_NEEDS_FORCE
;
845 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
846 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
852 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
854 struct git_transport_data
*data
= transport
->data
;
855 struct send_pack_args args
;
858 if (transport_color_config() < 0)
861 if (!data
->finished_handshake
)
862 get_refs_via_connect(transport
, 1, NULL
);
864 memset(&args
, 0, sizeof(args
));
865 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
866 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
867 args
.use_thin_pack
= data
->options
.thin
;
868 args
.verbose
= (transport
->verbose
> 0);
869 args
.quiet
= (transport
->verbose
< 0);
870 args
.progress
= transport
->progress
;
871 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
872 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
873 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
874 args
.push_options
= transport
->push_options
;
875 args
.url
= transport
->url
;
877 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
878 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
879 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
880 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
882 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
884 switch (data
->version
) {
886 die(_("support for protocol v2 not implemented yet"));
890 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
893 case protocol_unknown_version
:
894 BUG("unknown protocol version");
900 * Atomic push may abort the connection early and close the pipe,
901 * which may cause an error for `finish_connect()`. Ignore this error
902 * for atomic git-push.
904 if (ret
|| args
.atomic
)
905 finish_connect(data
->conn
);
907 ret
= finish_connect(data
->conn
);
909 data
->finished_handshake
= 0;
914 static int connect_git(struct transport
*transport
, const char *name
,
915 const char *executable
, int fd
[2])
917 struct git_transport_data
*data
= transport
->data
;
918 data
->conn
= git_connect(data
->fd
, transport
->url
,
925 static int disconnect_git(struct transport
*transport
)
927 struct git_transport_data
*data
= transport
->data
;
929 if (data
->finished_handshake
&& !transport
->stateless_rpc
)
930 packet_flush(data
->fd
[1]);
932 if (data
->fd
[1] >= 0)
934 finish_connect(data
->conn
);
937 list_objects_filter_release(&data
->options
.filter_options
);
942 static struct transport_vtable taken_over_vtable
= {
943 .get_refs_list
= get_refs_via_connect
,
944 .get_bundle_uri
= get_bundle_uri
,
945 .fetch_refs
= fetch_refs_via_pack
,
946 .push_refs
= git_transport_push
,
947 .disconnect
= disconnect_git
950 void transport_take_over(struct transport
*transport
,
951 struct child_process
*child
)
953 struct git_transport_data
*data
;
955 if (!transport
->smart_options
)
956 BUG("taking over transport requires non-NULL "
957 "smart_options field.");
959 CALLOC_ARRAY(data
, 1);
960 data
->options
= *transport
->smart_options
;
962 data
->fd
[0] = data
->conn
->out
;
963 data
->fd
[1] = data
->conn
->in
;
964 data
->finished_handshake
= 0;
965 transport
->data
= data
;
967 transport
->vtable
= &taken_over_vtable
;
968 transport
->smart_options
= &(data
->options
);
970 transport
->cannot_reuse
= 1;
973 static int is_file(const char *url
)
978 return S_ISREG(buf
.st_mode
);
981 static int external_specification_len(const char *url
)
983 return strchr(url
, ':') - url
;
986 static const struct string_list
*protocol_allow_list(void)
988 static int enabled
= -1;
989 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
992 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
994 string_list_split(&allowed
, v
, ':', -1);
995 string_list_sort(&allowed
);
1002 return enabled
? &allowed
: NULL
;
1005 enum protocol_allow_config
{
1006 PROTOCOL_ALLOW_NEVER
= 0,
1007 PROTOCOL_ALLOW_USER_ONLY
,
1008 PROTOCOL_ALLOW_ALWAYS
1011 static enum protocol_allow_config
parse_protocol_config(const char *key
,
1014 if (!strcasecmp(value
, "always"))
1015 return PROTOCOL_ALLOW_ALWAYS
;
1016 else if (!strcasecmp(value
, "never"))
1017 return PROTOCOL_ALLOW_NEVER
;
1018 else if (!strcasecmp(value
, "user"))
1019 return PROTOCOL_ALLOW_USER_ONLY
;
1021 die(_("unknown value for config '%s': %s"), key
, value
);
1024 static enum protocol_allow_config
get_protocol_config(const char *type
)
1026 char *key
= xstrfmt("protocol.%s.allow", type
);
1029 /* first check the per-protocol config */
1030 if (!git_config_get_string(key
, &value
)) {
1031 enum protocol_allow_config ret
=
1032 parse_protocol_config(key
, value
);
1039 /* if defined, fallback to user-defined default for unknown protocols */
1040 if (!git_config_get_string("protocol.allow", &value
)) {
1041 enum protocol_allow_config ret
=
1042 parse_protocol_config("protocol.allow", value
);
1047 /* fallback to built-in defaults */
1049 if (!strcmp(type
, "http") ||
1050 !strcmp(type
, "https") ||
1051 !strcmp(type
, "git") ||
1052 !strcmp(type
, "ssh"))
1053 return PROTOCOL_ALLOW_ALWAYS
;
1055 /* known scary; err on the side of caution */
1056 if (!strcmp(type
, "ext"))
1057 return PROTOCOL_ALLOW_NEVER
;
1059 /* unknown; by default let them be used only directly by the user */
1060 return PROTOCOL_ALLOW_USER_ONLY
;
1063 int is_transport_allowed(const char *type
, int from_user
)
1065 const struct string_list
*allow_list
= protocol_allow_list();
1067 return string_list_has_string(allow_list
, type
);
1069 switch (get_protocol_config(type
)) {
1070 case PROTOCOL_ALLOW_ALWAYS
:
1072 case PROTOCOL_ALLOW_NEVER
:
1074 case PROTOCOL_ALLOW_USER_ONLY
:
1076 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1080 BUG("invalid protocol_allow_config type");
1083 void transport_check_allowed(const char *type
)
1085 if (!is_transport_allowed(type
, -1))
1086 die(_("transport '%s' not allowed"), type
);
1089 static struct transport_vtable bundle_vtable
= {
1090 .get_refs_list
= get_refs_from_bundle
,
1091 .fetch_refs
= fetch_refs_from_bundle
,
1092 .disconnect
= close_bundle
1095 static struct transport_vtable builtin_smart_vtable
= {
1096 .get_refs_list
= get_refs_via_connect
,
1097 .get_bundle_uri
= get_bundle_uri
,
1098 .fetch_refs
= fetch_refs_via_pack
,
1099 .push_refs
= git_transport_push
,
1100 .connect
= connect_git
,
1101 .disconnect
= disconnect_git
1104 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1107 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1109 ret
->progress
= isatty(2);
1110 string_list_init_dup(&ret
->pack_lockfiles
);
1112 CALLOC_ARRAY(ret
->bundles
, 1);
1113 init_bundle_list(ret
->bundles
);
1116 BUG("No remote provided to transport_get()");
1118 ret
->got_remote_refs
= 0;
1119 ret
->remote
= remote
;
1120 helper
= remote
->foreign_vcs
;
1122 if (!url
&& remote
->url
)
1123 url
= remote
->url
[0];
1126 /* maybe it is a foreign URL? */
1128 const char *p
= url
;
1130 while (is_urlschemechar(p
== url
, *p
))
1132 if (starts_with(p
, "::"))
1133 helper
= xstrndup(url
, p
- url
);
1137 transport_helper_init(ret
, helper
);
1138 } else if (starts_with(url
, "rsync:")) {
1139 die(_("git-over-rsync is no longer supported"));
1140 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1141 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1142 bundle_header_init(&data
->header
);
1143 transport_check_allowed("file");
1145 ret
->vtable
= &bundle_vtable
;
1146 ret
->smart_options
= NULL
;
1147 } else if (!is_url(url
)
1148 || starts_with(url
, "file://")
1149 || starts_with(url
, "git://")
1150 || starts_with(url
, "ssh://")
1151 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1152 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1155 * These are builtin smart transports; "allowed" transports
1156 * will be checked individually in git_connect.
1158 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1159 list_objects_filter_init(&data
->options
.filter_options
);
1161 ret
->vtable
= &builtin_smart_vtable
;
1162 ret
->smart_options
= &(data
->options
);
1165 data
->finished_handshake
= 0;
1167 /* Unknown protocol in URL. Pass to external handler. */
1168 int len
= external_specification_len(url
);
1169 char *handler
= xmemdupz(url
, len
);
1170 transport_helper_init(ret
, handler
);
1173 if (ret
->smart_options
) {
1174 ret
->smart_options
->thin
= 1;
1175 ret
->smart_options
->uploadpack
= "git-upload-pack";
1176 if (remote
->uploadpack
)
1177 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1178 ret
->smart_options
->receivepack
= "git-receive-pack";
1179 if (remote
->receivepack
)
1180 ret
->smart_options
->receivepack
= remote
->receivepack
;
1183 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1188 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1190 return transport
->hash_algo
;
1193 int transport_set_option(struct transport
*transport
,
1194 const char *name
, const char *value
)
1196 int git_reports
= 1, protocol_reports
= 1;
1198 if (transport
->smart_options
)
1199 git_reports
= set_git_option(transport
->smart_options
,
1202 if (transport
->vtable
->set_option
)
1203 protocol_reports
= transport
->vtable
->set_option(transport
,
1206 /* If either report is 0, report 0 (success). */
1207 if (!git_reports
|| !protocol_reports
)
1209 /* If either reports -1 (invalid value), report -1. */
1210 if ((git_reports
== -1) || (protocol_reports
== -1))
1212 /* Otherwise if both report unknown, report unknown. */
1216 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1220 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1222 transport
->verbose
= -1;
1225 * Rules used to determine whether to report progress (processing aborts
1226 * when a rule is satisfied):
1228 * . Report progress, if force_progress is 1 (ie. --progress).
1229 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1230 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1231 * . Report progress if isatty(2) is 1.
1233 if (force_progress
>= 0)
1234 transport
->progress
= !!force_progress
;
1236 transport
->progress
= verbosity
>= 0 && isatty(2);
1239 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1243 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1244 "not be found on any remote:\n"));
1245 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1246 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1247 fprintf(stderr
, _("\nPlease try\n\n"
1248 " git push --recurse-submodules=on-demand\n\n"
1249 "or cd to the path and use\n\n"
1251 "to push them to a remote.\n\n"));
1253 string_list_clear(needs_pushing
, 0);
1255 die(_("Aborting."));
1258 static int run_pre_push_hook(struct transport
*transport
,
1259 struct ref
*remote_refs
)
1263 struct child_process proc
= CHILD_PROCESS_INIT
;
1265 const char *hook_path
= find_hook("pre-push");
1270 strvec_push(&proc
.args
, hook_path
);
1271 strvec_push(&proc
.args
, transport
->remote
->name
);
1272 strvec_push(&proc
.args
, transport
->url
);
1275 proc
.trace2_hook_name
= "pre-push";
1277 if (start_command(&proc
)) {
1278 finish_command(&proc
);
1282 sigchain_push(SIGPIPE
, SIG_IGN
);
1284 strbuf_init(&buf
, 256);
1286 for (r
= remote_refs
; r
; r
= r
->next
) {
1287 if (!r
->peer_ref
) continue;
1288 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1289 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1290 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1291 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1294 strbuf_addf( &buf
, "%s %s %s %s\n",
1295 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1296 r
->name
, oid_to_hex(&r
->old_oid
));
1298 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1299 /* We do not mind if a hook does not read all refs. */
1306 strbuf_release(&buf
);
1312 sigchain_pop(SIGPIPE
);
1314 x
= finish_command(&proc
);
1321 int transport_push(struct repository
*r
,
1322 struct transport
*transport
,
1323 struct refspec
*rs
, int flags
,
1324 unsigned int *reject_reasons
)
1326 struct ref
*remote_refs
= NULL
;
1327 struct ref
*local_refs
= NULL
;
1328 int match_flags
= MATCH_REFS_NONE
;
1329 int verbose
= (transport
->verbose
> 0);
1330 int quiet
= (transport
->verbose
< 0);
1331 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1332 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1335 struct transport_ls_refs_options transport_options
=
1336 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1338 *reject_reasons
= 0;
1340 if (transport_color_config() < 0)
1343 if (!transport
->vtable
->push_refs
)
1346 local_refs
= get_local_heads();
1348 if (check_push_refs(local_refs
, rs
) < 0)
1351 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1353 trace2_region_enter("transport_push", "get_refs_list", r
);
1354 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1355 &transport_options
);
1356 trace2_region_leave("transport_push", "get_refs_list", r
);
1358 transport_ls_refs_options_release(&transport_options
);
1360 if (flags
& TRANSPORT_PUSH_ALL
)
1361 match_flags
|= MATCH_REFS_ALL
;
1362 if (flags
& TRANSPORT_PUSH_MIRROR
)
1363 match_flags
|= MATCH_REFS_MIRROR
;
1364 if (flags
& TRANSPORT_PUSH_PRUNE
)
1365 match_flags
|= MATCH_REFS_PRUNE
;
1366 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1367 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1369 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1372 if (transport
->smart_options
&&
1373 transport
->smart_options
->cas
&&
1374 !is_empty_cas(transport
->smart_options
->cas
))
1375 apply_push_cas(transport
->smart_options
->cas
,
1376 transport
->remote
, remote_refs
);
1378 set_ref_status_for_push(remote_refs
,
1379 flags
& TRANSPORT_PUSH_MIRROR
,
1380 flags
& TRANSPORT_PUSH_FORCE
);
1382 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1383 if (run_pre_push_hook(transport
, remote_refs
))
1386 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1387 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1388 !is_bare_repository()) {
1389 struct ref
*ref
= remote_refs
;
1390 struct oid_array commits
= OID_ARRAY_INIT
;
1392 trace2_region_enter("transport_push", "push_submodules", r
);
1393 for (; ref
; ref
= ref
->next
)
1394 if (!is_null_oid(&ref
->new_oid
))
1395 oid_array_append(&commits
,
1398 if (!push_unpushed_submodules(r
,
1402 transport
->push_options
,
1404 oid_array_clear(&commits
);
1405 trace2_region_leave("transport_push", "push_submodules", r
);
1406 die(_("failed to push all needed submodules"));
1408 oid_array_clear(&commits
);
1409 trace2_region_leave("transport_push", "push_submodules", r
);
1412 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1413 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1414 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1415 !pretend
)) && !is_bare_repository()) {
1416 struct ref
*ref
= remote_refs
;
1417 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1418 struct oid_array commits
= OID_ARRAY_INIT
;
1420 trace2_region_enter("transport_push", "check_submodules", r
);
1421 for (; ref
; ref
= ref
->next
)
1422 if (!is_null_oid(&ref
->new_oid
))
1423 oid_array_append(&commits
,
1426 if (find_unpushed_submodules(r
,
1428 transport
->remote
->name
,
1430 oid_array_clear(&commits
);
1431 trace2_region_leave("transport_push", "check_submodules", r
);
1432 die_with_unpushed_submodules(&needs_pushing
);
1434 string_list_clear(&needs_pushing
, 0);
1435 oid_array_clear(&commits
);
1436 trace2_region_leave("transport_push", "check_submodules", r
);
1439 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1440 trace2_region_enter("transport_push", "push_refs", r
);
1441 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1442 trace2_region_leave("transport_push", "push_refs", r
);
1445 err
= push_had_errors(remote_refs
);
1446 ret
= push_ret
| err
;
1449 transport_print_push_status(transport
->url
, remote_refs
,
1450 verbose
| porcelain
, porcelain
,
1453 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1454 set_upstreams(transport
, remote_refs
, pretend
);
1456 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1457 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1459 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1460 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1463 if (porcelain
&& !push_ret
)
1465 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1466 fprintf(stderr
, "Everything up-to-date\n");
1469 free_refs(local_refs
);
1470 free_refs(remote_refs
);
1474 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1475 struct transport_ls_refs_options
*transport_options
)
1477 if (!transport
->got_remote_refs
) {
1478 transport
->remote_refs
=
1479 transport
->vtable
->get_refs_list(transport
, 0,
1481 transport
->got_remote_refs
= 1;
1484 return transport
->remote_refs
;
1487 void transport_ls_refs_options_release(struct transport_ls_refs_options
*opts
)
1489 strvec_clear(&opts
->ref_prefixes
);
1490 free((char *)opts
->unborn_head_target
);
1493 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1496 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1497 struct ref
**heads
= NULL
;
1500 for (rm
= refs
; rm
; rm
= rm
->next
) {
1503 !is_null_oid(&rm
->old_oid
) &&
1504 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1506 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1507 heads
[nr_heads
++] = rm
;
1512 * When deepening of a shallow repository is requested,
1513 * then local and remote refs are likely to still be equal.
1514 * Just feed them all to the fetch method in that case.
1515 * This condition shouldn't be met in a non-deepening fetch
1516 * (see builtin/fetch.c:quickfetch()).
1518 ALLOC_ARRAY(heads
, nr_refs
);
1519 for (rm
= refs
; rm
; rm
= rm
->next
)
1520 heads
[nr_heads
++] = rm
;
1523 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1529 int transport_get_remote_bundle_uri(struct transport
*transport
)
1532 const struct transport_vtable
*vtable
= transport
->vtable
;
1534 /* Check config only once. */
1535 if (transport
->got_remote_bundle_uri
)
1537 transport
->got_remote_bundle_uri
= 1;
1540 * Don't request bundle-uri from the server unless configured to
1541 * do so by the transfer.bundleURI=true config option.
1543 if (git_config_get_bool("transfer.bundleuri", &value
) || !value
)
1546 if (!transport
->bundles
->baseURI
)
1547 transport
->bundles
->baseURI
= xstrdup(transport
->url
);
1549 if (!vtable
->get_bundle_uri
)
1550 return error(_("bundle-uri operation not supported by protocol"));
1552 if (vtable
->get_bundle_uri(transport
) < 0)
1553 return error(_("could not retrieve server-advertised bundle-uri list"));
1557 void transport_unlock_pack(struct transport
*transport
, unsigned int flags
)
1559 int in_signal_handler
= !!(flags
& TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER
);
1562 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1563 if (in_signal_handler
)
1564 unlink(transport
->pack_lockfiles
.items
[i
].string
);
1566 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1567 if (!in_signal_handler
)
1568 string_list_clear(&transport
->pack_lockfiles
, 0);
1571 int transport_connect(struct transport
*transport
, const char *name
,
1572 const char *exec
, int fd
[2])
1574 if (transport
->vtable
->connect
)
1575 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1577 die(_("operation not supported by protocol"));
1580 int transport_disconnect(struct transport
*transport
)
1583 if (transport
->vtable
->disconnect
)
1584 ret
= transport
->vtable
->disconnect(transport
);
1585 if (transport
->got_remote_refs
)
1586 free_refs((void *)transport
->remote_refs
);
1587 clear_bundle_list(transport
->bundles
);
1588 free(transport
->bundles
);
1594 * Strip username (and password) from a URL and return
1595 * it in a newly allocated string.
1597 char *transport_anonymize_url(const char *url
)
1599 char *scheme_prefix
, *anon_part
;
1600 size_t anon_len
, prefix_len
= 0;
1602 anon_part
= strchr(url
, '@');
1603 if (url_is_local_not_ssh(url
) || !anon_part
)
1606 anon_len
= strlen(++anon_part
);
1607 scheme_prefix
= strstr(url
, "://");
1608 if (!scheme_prefix
) {
1609 if (!strchr(anon_part
, ':'))
1610 /* cannot be "me@there:/path/name" */
1614 /* make sure scheme is reasonable */
1615 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1618 case '+': case '.': case '-':
1627 /* @ past the first slash does not count */
1628 cp
= strchr(scheme_prefix
+ 3, '/');
1629 if (cp
&& cp
< anon_part
)
1631 prefix_len
= scheme_prefix
- url
+ 3;
1633 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1634 (int)anon_len
, anon_part
);
1636 return xstrdup(url
);