1 #include "git-compat-util.h"
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"
30 static int transport_use_color
= -1;
31 static char transport_colors
[][COLOR_MAXLEN
] = {
33 GIT_COLOR_RED
/* REJECTED */
36 enum color_transport
{
37 TRANSPORT_COLOR_RESET
= 0,
38 TRANSPORT_COLOR_REJECTED
= 1
41 static int transport_color_config(void)
43 const char *keys
[] = {
44 "color.transport.reset",
45 "color.transport.rejected"
46 }, *key
= "color.transport";
49 static int initialized
;
55 if (!git_config_get_string(key
, &value
))
56 transport_use_color
= git_config_colorbool(key
, value
);
58 if (!want_color_stderr(transport_use_color
))
61 for (i
= 0; i
< ARRAY_SIZE(keys
); i
++)
62 if (!git_config_get_string(keys
[i
], &value
)) {
64 return config_error_nonbool(keys
[i
]);
65 if (color_parse(value
, transport_colors
[i
]) < 0)
72 static const char *transport_get_color(enum color_transport ix
)
74 if (want_color_stderr(transport_use_color
))
75 return transport_colors
[ix
];
79 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
83 for (ref
= refs
; ref
; ref
= ref
->next
) {
84 const char *localname
;
86 const char *remotename
;
89 * Check suitability for tracking. Must be successful /
90 * already up-to-date ref create/modify (not delete).
92 if (ref
->status
!= REF_STATUS_OK
&&
93 ref
->status
!= REF_STATUS_UPTODATE
)
97 if (is_null_oid(&ref
->new_oid
))
100 /* Follow symbolic refs (mainly for HEAD). */
101 localname
= ref
->peer_ref
->name
;
102 remotename
= ref
->name
;
103 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
105 if (tmp
&& flag
& REF_ISSYMREF
&&
106 starts_with(tmp
, "refs/heads/"))
109 /* Both source and destination must be local branches. */
110 if (!localname
|| !starts_with(localname
, "refs/heads/"))
112 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
116 int flag
= transport
->verbose
< 0 ? 0 : BRANCH_CONFIG_VERBOSE
;
117 install_branch_config(flag
, localname
+ 11,
118 transport
->remote
->name
, remotename
);
119 } else if (transport
->verbose
>= 0)
120 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
121 localname
+ 11, remotename
+ 11,
122 transport
->remote
->name
);
126 struct bundle_transport_data
{
128 struct bundle_header header
;
129 unsigned get_refs_from_bundle_called
: 1;
132 static void get_refs_from_bundle_inner(struct transport
*transport
)
134 struct bundle_transport_data
*data
= transport
->data
;
136 data
->get_refs_from_bundle_called
= 1;
140 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
142 die(_("could not read bundle '%s'"), transport
->url
);
144 transport
->hash_algo
= data
->header
.hash_algo
;
147 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
149 struct transport_ls_refs_options
*transport_options UNUSED
)
151 struct bundle_transport_data
*data
= transport
->data
;
152 struct ref
*result
= NULL
;
158 get_refs_from_bundle_inner(transport
);
160 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
161 struct string_list_item
*e
= data
->header
.references
.items
+ i
;
162 const char *name
= e
->string
;
163 struct ref
*ref
= alloc_ref(name
);
164 struct object_id
*oid
= e
->util
;
165 oidcpy(&ref
->old_oid
, oid
);
172 static int fetch_refs_from_bundle(struct transport
*transport
,
173 int nr_heads
, struct ref
**to_fetch
)
175 struct bundle_transport_data
*data
= transport
->data
;
176 struct strvec extra_index_pack_args
= STRVEC_INIT
;
179 if (transport
->progress
)
180 strvec_push(&extra_index_pack_args
, "-v");
182 if (!data
->get_refs_from_bundle_called
)
183 get_refs_from_bundle_inner(transport
);
184 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
185 &extra_index_pack_args
, 0);
186 transport
->hash_algo
= data
->header
.hash_algo
;
190 static int close_bundle(struct transport
*transport
)
192 struct bundle_transport_data
*data
= transport
->data
;
195 bundle_header_release(&data
->header
);
200 struct git_transport_data
{
201 struct git_transport_options options
;
202 struct child_process
*conn
;
204 unsigned finished_handshake
: 1;
205 enum protocol_version version
;
206 struct oid_array extra_have
;
207 struct oid_array shallow
;
210 static int set_git_option(struct git_transport_options
*opts
,
211 const char *name
, const char *value
)
213 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
214 opts
->uploadpack
= value
;
216 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
217 opts
->receivepack
= value
;
219 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
220 opts
->thin
= !!value
;
222 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
223 opts
->followtags
= !!value
;
225 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
226 opts
->keep
= !!value
;
228 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
229 opts
->update_shallow
= !!value
;
231 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
236 opts
->depth
= strtol(value
, &end
, 0);
238 die(_("transport: invalid depth option '%s'"), value
);
241 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
242 opts
->deepen_since
= value
;
244 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
245 opts
->deepen_not
= (const struct string_list
*)value
;
247 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
248 opts
->deepen_relative
= !!value
;
250 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
251 opts
->from_promisor
= !!value
;
253 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
254 list_objects_filter_die_if_populated(&opts
->filter_options
);
255 parse_list_objects_filter(&opts
->filter_options
, value
);
257 } else if (!strcmp(name
, TRANS_OPT_REFETCH
)) {
258 opts
->refetch
= !!value
;
260 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
261 opts
->reject_shallow
= !!value
;
267 static int connect_setup(struct transport
*transport
, int for_push
)
269 struct git_transport_data
*data
= transport
->data
;
270 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
275 switch (transport
->family
) {
276 case TRANSPORT_FAMILY_ALL
: break;
277 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
278 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
281 data
->conn
= git_connect(data
->fd
, transport
->url
,
286 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
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
788 return (w
< sofar
) ? sofar
: w
;
791 int transport_summary_width(const struct ref
*refs
)
795 for (; refs
; refs
= refs
->next
) {
796 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
797 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
800 maxw
= FALLBACK_DEFAULT_ABBREV
;
801 return (2 * maxw
+ 3);
804 void transport_print_push_status(const char *dest
, struct ref
*refs
,
805 int verbose
, int porcelain
, unsigned int *reject_reasons
)
810 int summary_width
= transport_summary_width(refs
);
812 if (transport_color_config() < 0)
813 warning(_("could not parse transport.color.* config"));
815 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
818 for (ref
= refs
; ref
; ref
= ref
->next
)
819 if (ref
->status
== REF_STATUS_UPTODATE
)
820 n
+= print_one_push_status(ref
, dest
, n
,
821 porcelain
, summary_width
);
824 for (ref
= refs
; ref
; ref
= ref
->next
)
825 if (ref
->status
== REF_STATUS_OK
)
826 n
+= print_one_push_status(ref
, dest
, n
,
827 porcelain
, summary_width
);
830 for (ref
= refs
; ref
; ref
= ref
->next
) {
831 if (ref
->status
!= REF_STATUS_NONE
&&
832 ref
->status
!= REF_STATUS_UPTODATE
&&
833 ref
->status
!= REF_STATUS_OK
)
834 n
+= print_one_push_status(ref
, dest
, n
,
835 porcelain
, summary_width
);
836 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
837 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
838 *reject_reasons
|= REJECT_NON_FF_HEAD
;
840 *reject_reasons
|= REJECT_NON_FF_OTHER
;
841 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
842 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
843 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
844 *reject_reasons
|= REJECT_FETCH_FIRST
;
845 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
846 *reject_reasons
|= REJECT_NEEDS_FORCE
;
847 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
848 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
854 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
856 struct git_transport_data
*data
= transport
->data
;
857 struct send_pack_args args
;
860 if (transport_color_config() < 0)
863 if (!data
->finished_handshake
)
864 get_refs_via_connect(transport
, 1, NULL
);
866 memset(&args
, 0, sizeof(args
));
867 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
868 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
869 args
.use_thin_pack
= data
->options
.thin
;
870 args
.verbose
= (transport
->verbose
> 0);
871 args
.quiet
= (transport
->verbose
< 0);
872 args
.progress
= transport
->progress
;
873 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
874 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
875 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
876 args
.push_options
= transport
->push_options
;
877 args
.url
= transport
->url
;
879 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
880 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
881 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
882 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
884 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
886 switch (data
->version
) {
888 die(_("support for protocol v2 not implemented yet"));
892 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
895 case protocol_unknown_version
:
896 BUG("unknown protocol version");
902 * Atomic push may abort the connection early and close the pipe,
903 * which may cause an error for `finish_connect()`. Ignore this error
904 * for atomic git-push.
906 if (ret
|| args
.atomic
)
907 finish_connect(data
->conn
);
909 ret
= finish_connect(data
->conn
);
911 data
->finished_handshake
= 0;
916 static int connect_git(struct transport
*transport
, const char *name
,
917 const char *executable
, int fd
[2])
919 struct git_transport_data
*data
= transport
->data
;
920 data
->conn
= git_connect(data
->fd
, transport
->url
,
921 name
, executable
, 0);
927 static int disconnect_git(struct transport
*transport
)
929 struct git_transport_data
*data
= transport
->data
;
931 if (data
->finished_handshake
&& !transport
->stateless_rpc
)
932 packet_flush(data
->fd
[1]);
934 if (data
->fd
[1] >= 0)
936 finish_connect(data
->conn
);
939 list_objects_filter_release(&data
->options
.filter_options
);
944 static struct transport_vtable taken_over_vtable
= {
945 .get_refs_list
= get_refs_via_connect
,
946 .get_bundle_uri
= get_bundle_uri
,
947 .fetch_refs
= fetch_refs_via_pack
,
948 .push_refs
= git_transport_push
,
949 .disconnect
= disconnect_git
952 void transport_take_over(struct transport
*transport
,
953 struct child_process
*child
)
955 struct git_transport_data
*data
;
957 if (!transport
->smart_options
)
958 BUG("taking over transport requires non-NULL "
959 "smart_options field.");
961 CALLOC_ARRAY(data
, 1);
962 data
->options
= *transport
->smart_options
;
964 data
->fd
[0] = data
->conn
->out
;
965 data
->fd
[1] = data
->conn
->in
;
966 data
->finished_handshake
= 0;
967 transport
->data
= data
;
969 transport
->vtable
= &taken_over_vtable
;
970 transport
->smart_options
= &(data
->options
);
972 transport
->cannot_reuse
= 1;
975 static int is_file(const char *url
)
980 return S_ISREG(buf
.st_mode
);
983 static int external_specification_len(const char *url
)
985 return strchr(url
, ':') - url
;
988 static const struct string_list
*protocol_allow_list(void)
990 static int enabled
= -1;
991 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
994 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
996 string_list_split(&allowed
, v
, ':', -1);
997 string_list_sort(&allowed
);
1004 return enabled
? &allowed
: NULL
;
1007 enum protocol_allow_config
{
1008 PROTOCOL_ALLOW_NEVER
= 0,
1009 PROTOCOL_ALLOW_USER_ONLY
,
1010 PROTOCOL_ALLOW_ALWAYS
1013 static enum protocol_allow_config
parse_protocol_config(const char *key
,
1016 if (!strcasecmp(value
, "always"))
1017 return PROTOCOL_ALLOW_ALWAYS
;
1018 else if (!strcasecmp(value
, "never"))
1019 return PROTOCOL_ALLOW_NEVER
;
1020 else if (!strcasecmp(value
, "user"))
1021 return PROTOCOL_ALLOW_USER_ONLY
;
1023 die(_("unknown value for config '%s': %s"), key
, value
);
1026 static enum protocol_allow_config
get_protocol_config(const char *type
)
1028 char *key
= xstrfmt("protocol.%s.allow", type
);
1031 /* first check the per-protocol config */
1032 if (!git_config_get_string(key
, &value
)) {
1033 enum protocol_allow_config ret
=
1034 parse_protocol_config(key
, value
);
1041 /* if defined, fallback to user-defined default for unknown protocols */
1042 if (!git_config_get_string("protocol.allow", &value
)) {
1043 enum protocol_allow_config ret
=
1044 parse_protocol_config("protocol.allow", value
);
1049 /* fallback to built-in defaults */
1051 if (!strcmp(type
, "http") ||
1052 !strcmp(type
, "https") ||
1053 !strcmp(type
, "git") ||
1054 !strcmp(type
, "ssh"))
1055 return PROTOCOL_ALLOW_ALWAYS
;
1057 /* known scary; err on the side of caution */
1058 if (!strcmp(type
, "ext"))
1059 return PROTOCOL_ALLOW_NEVER
;
1061 /* unknown; by default let them be used only directly by the user */
1062 return PROTOCOL_ALLOW_USER_ONLY
;
1065 int is_transport_allowed(const char *type
, int from_user
)
1067 const struct string_list
*allow_list
= protocol_allow_list();
1069 return string_list_has_string(allow_list
, type
);
1071 switch (get_protocol_config(type
)) {
1072 case PROTOCOL_ALLOW_ALWAYS
:
1074 case PROTOCOL_ALLOW_NEVER
:
1076 case PROTOCOL_ALLOW_USER_ONLY
:
1078 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1082 BUG("invalid protocol_allow_config type");
1085 void transport_check_allowed(const char *type
)
1087 if (!is_transport_allowed(type
, -1))
1088 die(_("transport '%s' not allowed"), type
);
1091 static struct transport_vtable bundle_vtable
= {
1092 .get_refs_list
= get_refs_from_bundle
,
1093 .fetch_refs
= fetch_refs_from_bundle
,
1094 .disconnect
= close_bundle
1097 static struct transport_vtable builtin_smart_vtable
= {
1098 .get_refs_list
= get_refs_via_connect
,
1099 .get_bundle_uri
= get_bundle_uri
,
1100 .fetch_refs
= fetch_refs_via_pack
,
1101 .push_refs
= git_transport_push
,
1102 .connect
= connect_git
,
1103 .disconnect
= disconnect_git
1106 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1109 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1111 ret
->progress
= isatty(2);
1112 string_list_init_dup(&ret
->pack_lockfiles
);
1114 CALLOC_ARRAY(ret
->bundles
, 1);
1115 init_bundle_list(ret
->bundles
);
1118 BUG("No remote provided to transport_get()");
1120 ret
->got_remote_refs
= 0;
1121 ret
->remote
= remote
;
1122 helper
= remote
->foreign_vcs
;
1124 if (!url
&& remote
->url
)
1125 url
= remote
->url
[0];
1128 /* maybe it is a foreign URL? */
1130 const char *p
= url
;
1132 while (is_urlschemechar(p
== url
, *p
))
1134 if (starts_with(p
, "::"))
1135 helper
= xstrndup(url
, p
- url
);
1139 transport_helper_init(ret
, helper
);
1140 } else if (starts_with(url
, "rsync:")) {
1141 die(_("git-over-rsync is no longer supported"));
1142 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1143 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1144 bundle_header_init(&data
->header
);
1145 transport_check_allowed("file");
1147 ret
->vtable
= &bundle_vtable
;
1148 ret
->smart_options
= NULL
;
1149 } else if (!is_url(url
)
1150 || starts_with(url
, "file://")
1151 || starts_with(url
, "git://")
1152 || starts_with(url
, "ssh://")
1153 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1154 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1157 * These are builtin smart transports; "allowed" transports
1158 * will be checked individually in git_connect.
1160 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1161 list_objects_filter_init(&data
->options
.filter_options
);
1163 ret
->vtable
= &builtin_smart_vtable
;
1164 ret
->smart_options
= &(data
->options
);
1167 data
->finished_handshake
= 0;
1169 /* Unknown protocol in URL. Pass to external handler. */
1170 int len
= external_specification_len(url
);
1171 char *handler
= xmemdupz(url
, len
);
1172 transport_helper_init(ret
, handler
);
1175 if (ret
->smart_options
) {
1176 ret
->smart_options
->thin
= 1;
1177 ret
->smart_options
->uploadpack
= "git-upload-pack";
1178 if (remote
->uploadpack
)
1179 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1180 ret
->smart_options
->receivepack
= "git-receive-pack";
1181 if (remote
->receivepack
)
1182 ret
->smart_options
->receivepack
= remote
->receivepack
;
1185 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1190 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1192 return transport
->hash_algo
;
1195 int transport_set_option(struct transport
*transport
,
1196 const char *name
, const char *value
)
1198 int git_reports
= 1, protocol_reports
= 1;
1200 if (transport
->smart_options
)
1201 git_reports
= set_git_option(transport
->smart_options
,
1204 if (transport
->vtable
->set_option
)
1205 protocol_reports
= transport
->vtable
->set_option(transport
,
1208 /* If either report is 0, report 0 (success). */
1209 if (!git_reports
|| !protocol_reports
)
1211 /* If either reports -1 (invalid value), report -1. */
1212 if ((git_reports
== -1) || (protocol_reports
== -1))
1214 /* Otherwise if both report unknown, report unknown. */
1218 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1222 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1224 transport
->verbose
= -1;
1227 * Rules used to determine whether to report progress (processing aborts
1228 * when a rule is satisfied):
1230 * . Report progress, if force_progress is 1 (ie. --progress).
1231 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1232 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1233 * . Report progress if isatty(2) is 1.
1235 if (force_progress
>= 0)
1236 transport
->progress
= !!force_progress
;
1238 transport
->progress
= verbosity
>= 0 && isatty(2);
1241 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1245 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1246 "not be found on any remote:\n"));
1247 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1248 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1249 fprintf(stderr
, _("\nPlease try\n\n"
1250 " git push --recurse-submodules=on-demand\n\n"
1251 "or cd to the path and use\n\n"
1253 "to push them to a remote.\n\n"));
1255 string_list_clear(needs_pushing
, 0);
1257 die(_("Aborting."));
1260 static int run_pre_push_hook(struct transport
*transport
,
1261 struct ref
*remote_refs
)
1265 struct child_process proc
= CHILD_PROCESS_INIT
;
1267 const char *hook_path
= find_hook("pre-push");
1272 strvec_push(&proc
.args
, hook_path
);
1273 strvec_push(&proc
.args
, transport
->remote
->name
);
1274 strvec_push(&proc
.args
, transport
->url
);
1277 proc
.trace2_hook_name
= "pre-push";
1279 if (start_command(&proc
)) {
1280 finish_command(&proc
);
1284 sigchain_push(SIGPIPE
, SIG_IGN
);
1286 strbuf_init(&buf
, 256);
1288 for (r
= remote_refs
; r
; r
= r
->next
) {
1289 if (!r
->peer_ref
) continue;
1290 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1291 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1292 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1293 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1296 strbuf_addf( &buf
, "%s %s %s %s\n",
1297 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1298 r
->name
, oid_to_hex(&r
->old_oid
));
1300 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1301 /* We do not mind if a hook does not read all refs. */
1308 strbuf_release(&buf
);
1314 sigchain_pop(SIGPIPE
);
1316 x
= finish_command(&proc
);
1323 int transport_push(struct repository
*r
,
1324 struct transport
*transport
,
1325 struct refspec
*rs
, int flags
,
1326 unsigned int *reject_reasons
)
1328 struct ref
*remote_refs
= NULL
;
1329 struct ref
*local_refs
= NULL
;
1330 int match_flags
= MATCH_REFS_NONE
;
1331 int verbose
= (transport
->verbose
> 0);
1332 int quiet
= (transport
->verbose
< 0);
1333 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1334 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1337 struct transport_ls_refs_options transport_options
=
1338 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1340 *reject_reasons
= 0;
1342 if (transport_color_config() < 0)
1345 if (!transport
->vtable
->push_refs
)
1348 local_refs
= get_local_heads();
1350 if (check_push_refs(local_refs
, rs
) < 0)
1353 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1355 trace2_region_enter("transport_push", "get_refs_list", r
);
1356 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1357 &transport_options
);
1358 trace2_region_leave("transport_push", "get_refs_list", r
);
1360 transport_ls_refs_options_release(&transport_options
);
1362 if (flags
& TRANSPORT_PUSH_ALL
)
1363 match_flags
|= MATCH_REFS_ALL
;
1364 if (flags
& TRANSPORT_PUSH_MIRROR
)
1365 match_flags
|= MATCH_REFS_MIRROR
;
1366 if (flags
& TRANSPORT_PUSH_PRUNE
)
1367 match_flags
|= MATCH_REFS_PRUNE
;
1368 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1369 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1371 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1374 if (transport
->smart_options
&&
1375 transport
->smart_options
->cas
&&
1376 !is_empty_cas(transport
->smart_options
->cas
))
1377 apply_push_cas(transport
->smart_options
->cas
,
1378 transport
->remote
, remote_refs
);
1380 set_ref_status_for_push(remote_refs
,
1381 flags
& TRANSPORT_PUSH_MIRROR
,
1382 flags
& TRANSPORT_PUSH_FORCE
);
1384 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1385 if (run_pre_push_hook(transport
, remote_refs
))
1388 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1389 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1390 !is_bare_repository()) {
1391 struct ref
*ref
= remote_refs
;
1392 struct oid_array commits
= OID_ARRAY_INIT
;
1394 trace2_region_enter("transport_push", "push_submodules", r
);
1395 for (; ref
; ref
= ref
->next
)
1396 if (!is_null_oid(&ref
->new_oid
))
1397 oid_array_append(&commits
,
1400 if (!push_unpushed_submodules(r
,
1404 transport
->push_options
,
1406 oid_array_clear(&commits
);
1407 trace2_region_leave("transport_push", "push_submodules", r
);
1408 die(_("failed to push all needed submodules"));
1410 oid_array_clear(&commits
);
1411 trace2_region_leave("transport_push", "push_submodules", r
);
1414 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1415 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1416 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1417 !pretend
)) && !is_bare_repository()) {
1418 struct ref
*ref
= remote_refs
;
1419 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1420 struct oid_array commits
= OID_ARRAY_INIT
;
1422 trace2_region_enter("transport_push", "check_submodules", r
);
1423 for (; ref
; ref
= ref
->next
)
1424 if (!is_null_oid(&ref
->new_oid
))
1425 oid_array_append(&commits
,
1428 if (find_unpushed_submodules(r
,
1430 transport
->remote
->name
,
1432 oid_array_clear(&commits
);
1433 trace2_region_leave("transport_push", "check_submodules", r
);
1434 die_with_unpushed_submodules(&needs_pushing
);
1436 string_list_clear(&needs_pushing
, 0);
1437 oid_array_clear(&commits
);
1438 trace2_region_leave("transport_push", "check_submodules", r
);
1441 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1442 trace2_region_enter("transport_push", "push_refs", r
);
1443 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1444 trace2_region_leave("transport_push", "push_refs", r
);
1447 err
= push_had_errors(remote_refs
);
1448 ret
= push_ret
| err
;
1451 transport_print_push_status(transport
->url
, remote_refs
,
1452 verbose
| porcelain
, porcelain
,
1455 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1456 set_upstreams(transport
, remote_refs
, pretend
);
1458 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1459 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1461 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1462 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1465 if (porcelain
&& !push_ret
)
1467 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1468 fprintf(stderr
, "Everything up-to-date\n");
1471 free_refs(local_refs
);
1472 free_refs(remote_refs
);
1476 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1477 struct transport_ls_refs_options
*transport_options
)
1479 if (!transport
->got_remote_refs
) {
1480 transport
->remote_refs
=
1481 transport
->vtable
->get_refs_list(transport
, 0,
1483 transport
->got_remote_refs
= 1;
1486 return transport
->remote_refs
;
1489 void transport_ls_refs_options_release(struct transport_ls_refs_options
*opts
)
1491 strvec_clear(&opts
->ref_prefixes
);
1492 free((char *)opts
->unborn_head_target
);
1495 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1498 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1499 struct ref
**heads
= NULL
;
1502 for (rm
= refs
; rm
; rm
= rm
->next
) {
1505 !is_null_oid(&rm
->old_oid
) &&
1506 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1508 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1509 heads
[nr_heads
++] = rm
;
1514 * When deepening of a shallow repository is requested,
1515 * then local and remote refs are likely to still be equal.
1516 * Just feed them all to the fetch method in that case.
1517 * This condition shouldn't be met in a non-deepening fetch
1518 * (see builtin/fetch.c:quickfetch()).
1520 ALLOC_ARRAY(heads
, nr_refs
);
1521 for (rm
= refs
; rm
; rm
= rm
->next
)
1522 heads
[nr_heads
++] = rm
;
1525 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1531 int transport_get_remote_bundle_uri(struct transport
*transport
)
1534 const struct transport_vtable
*vtable
= transport
->vtable
;
1536 /* Check config only once. */
1537 if (transport
->got_remote_bundle_uri
)
1539 transport
->got_remote_bundle_uri
= 1;
1542 * Don't request bundle-uri from the server unless configured to
1543 * do so by the transfer.bundleURI=true config option.
1545 if (git_config_get_bool("transfer.bundleuri", &value
) || !value
)
1548 if (!transport
->bundles
->baseURI
)
1549 transport
->bundles
->baseURI
= xstrdup(transport
->url
);
1551 if (!vtable
->get_bundle_uri
)
1552 return error(_("bundle-uri operation not supported by protocol"));
1554 if (vtable
->get_bundle_uri(transport
) < 0)
1555 return error(_("could not retrieve server-advertised bundle-uri list"));
1559 void transport_unlock_pack(struct transport
*transport
, unsigned int flags
)
1561 int in_signal_handler
= !!(flags
& TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER
);
1564 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1565 if (in_signal_handler
)
1566 unlink(transport
->pack_lockfiles
.items
[i
].string
);
1568 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1569 if (!in_signal_handler
)
1570 string_list_clear(&transport
->pack_lockfiles
, 0);
1573 int transport_connect(struct transport
*transport
, const char *name
,
1574 const char *exec
, int fd
[2])
1576 if (transport
->vtable
->connect
)
1577 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1579 die(_("operation not supported by protocol"));
1582 int transport_disconnect(struct transport
*transport
)
1585 if (transport
->vtable
->disconnect
)
1586 ret
= transport
->vtable
->disconnect(transport
);
1587 if (transport
->got_remote_refs
)
1588 free_refs((void *)transport
->remote_refs
);
1589 clear_bundle_list(transport
->bundles
);
1590 free(transport
->bundles
);
1596 * Strip username (and password) from a URL and return
1597 * it in a newly allocated string.
1599 char *transport_anonymize_url(const char *url
)
1601 char *scheme_prefix
, *anon_part
;
1602 size_t anon_len
, prefix_len
= 0;
1604 anon_part
= strchr(url
, '@');
1605 if (url_is_local_not_ssh(url
) || !anon_part
)
1608 anon_len
= strlen(++anon_part
);
1609 scheme_prefix
= strstr(url
, "://");
1610 if (!scheme_prefix
) {
1611 if (!strchr(anon_part
, ':'))
1612 /* cannot be "me@there:/path/name" */
1616 /* make sure scheme is reasonable */
1617 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1620 case '+': case '.': case '-':
1629 /* @ past the first slash does not count */
1630 cp
= strchr(scheme_prefix
+ 3, '/');
1631 if (cp
&& cp
< anon_part
)
1633 prefix_len
= scheme_prefix
- url
+ 3;
1635 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1636 (int)anon_len
, anon_part
);
1638 return xstrdup(url
);