1 #include "git-compat-util.h"
4 #include "environment.h"
9 #include "fetch-pack.h"
12 #include "send-pack.h"
19 #include "submodule.h"
20 #include "string-list.h"
21 #include "oid-array.h"
24 #include "transport-internal.h"
26 #include "object-name.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
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
104 localname
, RESOLVE_REF_READING
,
106 if (tmp
&& flag
& REF_ISSYMREF
&&
107 starts_with(tmp
, "refs/heads/"))
110 /* Both source and destination must be local branches. */
111 if (!localname
|| !starts_with(localname
, "refs/heads/"))
113 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
117 int flag
= transport
->verbose
< 0 ? 0 : BRANCH_CONFIG_VERBOSE
;
118 install_branch_config(flag
, localname
+ 11,
119 transport
->remote
->name
, remotename
);
120 } else if (transport
->verbose
>= 0)
121 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
122 localname
+ 11, remotename
+ 11,
123 transport
->remote
->name
);
127 struct bundle_transport_data
{
129 struct bundle_header header
;
130 unsigned get_refs_from_bundle_called
: 1;
133 static void get_refs_from_bundle_inner(struct transport
*transport
)
135 struct bundle_transport_data
*data
= transport
->data
;
137 data
->get_refs_from_bundle_called
= 1;
141 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
143 die(_("could not read bundle '%s'"), transport
->url
);
145 transport
->hash_algo
= data
->header
.hash_algo
;
148 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
150 struct transport_ls_refs_options
*transport_options UNUSED
)
152 struct bundle_transport_data
*data
= transport
->data
;
153 struct ref
*result
= NULL
;
159 get_refs_from_bundle_inner(transport
);
161 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
162 struct string_list_item
*e
= data
->header
.references
.items
+ i
;
163 const char *name
= e
->string
;
164 struct ref
*ref
= alloc_ref(name
);
165 struct object_id
*oid
= e
->util
;
166 oidcpy(&ref
->old_oid
, oid
);
173 static int fetch_refs_from_bundle(struct transport
*transport
,
175 struct ref
**to_fetch UNUSED
)
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
,
288 data
->options
.receivepack
:
289 data
->options
.uploadpack
,
295 static void die_if_server_options(struct transport
*transport
)
297 if (!transport
->server_options
|| !transport
->server_options
->nr
)
299 advise(_("see protocol.version in 'git help config' for more details"));
300 die(_("server options require protocol version 2 or later"));
304 * Obtains the protocol version from the transport and writes it to
305 * transport->data->version, first connecting if not already connected.
307 * If the protocol version is one that allows skipping the listing of remote
308 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
309 * this function returns NULL. Otherwise, this function returns the list of
312 static struct ref
*handshake(struct transport
*transport
, int for_push
,
313 struct transport_ls_refs_options
*options
,
316 struct git_transport_data
*data
= transport
->data
;
317 struct ref
*refs
= NULL
;
318 struct packet_reader reader
;
320 const char *server_sid
;
322 connect_setup(transport
, for_push
);
324 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
325 PACKET_READ_CHOMP_NEWLINE
|
326 PACKET_READ_GENTLE_ON_EOF
|
327 PACKET_READ_DIE_ON_ERR_PACKET
);
329 data
->version
= discover_version(&reader
);
330 switch (data
->version
) {
332 if (server_feature_v2("session-id", &server_sid
))
333 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
335 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
337 transport
->server_options
,
338 transport
->stateless_rpc
);
342 die_if_server_options(transport
);
343 get_remote_heads(&reader
, &refs
,
344 for_push
? REF_NORMAL
: 0,
347 server_sid
= server_feature_value("session-id", &sid_len
);
349 char *sid
= xstrndup(server_sid
, sid_len
);
350 trace2_data_string("transfer", NULL
, "server-sid", sid
);
354 case protocol_unknown_version
:
355 BUG("unknown protocol version");
357 data
->finished_handshake
= 1;
358 transport
->hash_algo
= reader
.hash_algo
;
360 if (reader
.line_peeked
)
361 BUG("buffer must be empty at the end of handshake()");
366 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
367 struct transport_ls_refs_options
*options
)
369 return handshake(transport
, for_push
, options
, 1);
372 static int get_bundle_uri(struct transport
*transport
)
374 struct git_transport_data
*data
= transport
->data
;
375 struct packet_reader reader
;
376 int stateless_rpc
= transport
->stateless_rpc
;
378 if (!transport
->bundles
) {
379 CALLOC_ARRAY(transport
->bundles
, 1);
380 init_bundle_list(transport
->bundles
);
383 if (!data
->finished_handshake
) {
384 struct ref
*refs
= handshake(transport
, 0, NULL
, 0);
391 * "Support" protocol v0 and v2 without bundle-uri support by
392 * silently degrading to a NOOP.
394 if (!server_supports_v2("bundle-uri"))
397 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
398 PACKET_READ_CHOMP_NEWLINE
|
399 PACKET_READ_GENTLE_ON_EOF
);
401 return get_remote_bundle_uri(data
->fd
[1], &reader
,
402 transport
->bundles
, stateless_rpc
);
405 static int fetch_refs_via_pack(struct transport
*transport
,
406 int nr_heads
, struct ref
**to_fetch
)
409 struct git_transport_data
*data
= transport
->data
;
410 struct ref
*refs
= NULL
;
411 struct fetch_pack_args args
;
412 struct ref
*refs_tmp
= NULL
;
414 memset(&args
, 0, sizeof(args
));
415 args
.uploadpack
= data
->options
.uploadpack
;
416 args
.keep_pack
= data
->options
.keep
;
418 args
.use_thin_pack
= data
->options
.thin
;
419 args
.include_tag
= data
->options
.followtags
;
420 args
.verbose
= (transport
->verbose
> 1);
421 args
.quiet
= (transport
->verbose
< 0);
422 args
.no_progress
= !transport
->progress
;
423 args
.depth
= data
->options
.depth
;
424 args
.deepen_since
= data
->options
.deepen_since
;
425 args
.deepen_not
= data
->options
.deepen_not
;
426 args
.deepen_relative
= data
->options
.deepen_relative
;
427 args
.check_self_contained_and_connected
=
428 data
->options
.check_self_contained_and_connected
;
429 args
.cloning
= transport
->cloning
;
430 args
.update_shallow
= data
->options
.update_shallow
;
431 args
.from_promisor
= data
->options
.from_promisor
;
432 list_objects_filter_copy(&args
.filter_options
,
433 &data
->options
.filter_options
);
434 args
.refetch
= data
->options
.refetch
;
435 args
.stateless_rpc
= transport
->stateless_rpc
;
436 args
.server_options
= transport
->server_options
;
437 args
.negotiation_tips
= data
->options
.negotiation_tips
;
438 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
440 if (!data
->finished_handshake
) {
442 int must_list_refs
= 0;
443 for (i
= 0; i
< nr_heads
; i
++) {
444 if (!to_fetch
[i
]->exact_oid
) {
449 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
452 if (data
->version
== protocol_unknown_version
)
453 BUG("unknown protocol version");
454 else if (data
->version
<= protocol_v1
)
455 die_if_server_options(transport
);
457 if (data
->options
.acked_commits
) {
458 if (data
->version
< protocol_v2
) {
459 warning(_("--negotiate-only requires protocol v2"));
461 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
462 warning(_("server does not support wait-for-done"));
465 negotiate_using_fetch(data
->options
.negotiation_tips
,
466 transport
->server_options
,
467 transport
->stateless_rpc
,
469 data
->options
.acked_commits
);
475 refs
= fetch_pack(&args
, data
->fd
,
476 refs_tmp
? refs_tmp
: transport
->remote_refs
,
477 to_fetch
, nr_heads
, &data
->shallow
,
478 &transport
->pack_lockfiles
, data
->version
);
480 data
->finished_handshake
= 0;
481 data
->options
.self_contained_and_connected
=
482 args
.self_contained_and_connected
;
483 data
->options
.connectivity_checked
= args
.connectivity_checked
;
487 if (report_unmatched_refs(to_fetch
, nr_heads
))
492 if (data
->fd
[1] >= 0)
494 if (finish_connect(data
->conn
))
500 list_objects_filter_release(&args
.filter_options
);
504 static int push_had_errors(struct ref
*ref
)
506 for (; ref
; ref
= ref
->next
) {
507 switch (ref
->status
) {
508 case REF_STATUS_NONE
:
509 case REF_STATUS_UPTODATE
:
519 int transport_refs_pushed(struct ref
*ref
)
521 for (; ref
; ref
= ref
->next
) {
522 switch(ref
->status
) {
523 case REF_STATUS_NONE
:
524 case REF_STATUS_UPTODATE
:
533 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
534 struct object_id
*new_oid
, int deletion
,
537 struct refspec_item rs
;
539 memset(&rs
, 0, sizeof(rs
));
543 if (!remote_find_tracking(remote
, &rs
)) {
545 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
547 refs_delete_ref(get_main_ref_store(the_repository
),
548 NULL
, rs
.dst
, NULL
, 0);
550 refs_update_ref(get_main_ref_store(the_repository
),
551 "update by push", rs
.dst
, new_oid
,
557 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
560 struct object_id
*new_oid
;
561 struct ref_push_report
*report
;
563 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
566 report
= ref
->report
;
568 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
569 ref
->deletion
, verbose
);
571 for (; report
; report
= report
->next
) {
572 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
573 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
574 update_one_tracking_ref(remote
, refname
, new_oid
,
575 is_null_oid(new_oid
), verbose
);
579 static void print_ref_status(char flag
, const char *summary
,
580 struct ref
*to
, struct ref
*from
, const char *msg
,
581 struct ref_push_report
*report
,
582 int porcelain
, int summary_width
)
586 if (report
&& report
->ref_name
)
587 to_name
= report
->ref_name
;
593 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
595 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
597 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
599 fprintf(stdout
, "%s\n", summary
);
601 const char *red
= "", *reset
= "";
602 if (push_had_errors(to
)) {
603 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
604 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
606 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
609 fprintf(stderr
, "%s -> %s",
610 prettify_refname(from
->name
),
611 prettify_refname(to_name
));
613 fputs(prettify_refname(to_name
), stderr
);
623 static void print_ok_ref_status(struct ref
*ref
,
624 struct ref_push_report
*report
,
625 int porcelain
, int summary_width
)
627 struct object_id
*old_oid
;
628 struct object_id
*new_oid
;
629 const char *ref_name
;
632 if (report
&& report
->old_oid
)
633 old_oid
= report
->old_oid
;
635 old_oid
= &ref
->old_oid
;
636 if (report
&& report
->new_oid
)
637 new_oid
= report
->new_oid
;
639 new_oid
= &ref
->new_oid
;
640 if (report
&& report
->forced_update
)
641 forced_update
= report
->forced_update
;
643 forced_update
= ref
->forced_update
;
644 if (report
&& report
->ref_name
)
645 ref_name
= report
->ref_name
;
647 ref_name
= ref
->name
;
650 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
651 report
, porcelain
, summary_width
);
652 else if (is_null_oid(old_oid
))
653 print_ref_status('*',
654 (starts_with(ref_name
, "refs/tags/")
656 : (starts_with(ref_name
, "refs/heads/")
658 : "[new reference]")),
659 ref
, ref
->peer_ref
, NULL
,
660 report
, porcelain
, summary_width
);
662 struct strbuf quickref
= STRBUF_INIT
;
666 strbuf_add_unique_abbrev(&quickref
, old_oid
,
669 strbuf_addstr(&quickref
, "...");
671 msg
= "forced update";
673 strbuf_addstr(&quickref
, "..");
677 strbuf_add_unique_abbrev(&quickref
, new_oid
,
680 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
681 report
, porcelain
, summary_width
);
682 strbuf_release(&quickref
);
686 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
687 struct ref_push_report
*report
,
688 int porcelain
, int summary_width
)
691 char *url
= transport_anonymize_url(dest
);
692 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
696 switch(ref
->status
) {
697 case REF_STATUS_NONE
:
698 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
699 report
, porcelain
, summary_width
);
701 case REF_STATUS_REJECT_NODELETE
:
702 print_ref_status('!', "[rejected]", ref
, NULL
,
703 "remote does not support deleting refs",
704 report
, porcelain
, summary_width
);
706 case REF_STATUS_UPTODATE
:
707 print_ref_status('=', "[up to date]", ref
,
709 report
, porcelain
, summary_width
);
711 case REF_STATUS_REJECT_NONFASTFORWARD
:
712 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
714 report
, porcelain
, summary_width
);
716 case REF_STATUS_REJECT_ALREADY_EXISTS
:
717 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
719 report
, porcelain
, summary_width
);
721 case REF_STATUS_REJECT_FETCH_FIRST
:
722 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
724 report
, porcelain
, summary_width
);
726 case REF_STATUS_REJECT_NEEDS_FORCE
:
727 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
729 report
, porcelain
, summary_width
);
731 case REF_STATUS_REJECT_STALE
:
732 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
734 report
, porcelain
, summary_width
);
736 case REF_STATUS_REJECT_REMOTE_UPDATED
:
737 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
738 "remote ref updated since checkout",
739 report
, porcelain
, summary_width
);
741 case REF_STATUS_REJECT_SHALLOW
:
742 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
743 "new shallow roots not allowed",
744 report
, porcelain
, summary_width
);
746 case REF_STATUS_REMOTE_REJECT
:
747 print_ref_status('!', "[remote rejected]", ref
,
748 ref
->deletion
? NULL
: ref
->peer_ref
,
750 report
, porcelain
, summary_width
);
752 case REF_STATUS_EXPECTING_REPORT
:
753 print_ref_status('!', "[remote failure]", ref
,
754 ref
->deletion
? NULL
: ref
->peer_ref
,
755 "remote failed to report status",
756 report
, porcelain
, summary_width
);
758 case REF_STATUS_ATOMIC_PUSH_FAILED
:
759 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
760 "atomic push failed",
761 report
, porcelain
, summary_width
);
764 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
771 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
772 int porcelain
, int summary_width
)
774 struct ref_push_report
*report
;
778 return print_one_push_report(ref
, dest
, count
,
779 NULL
, porcelain
, summary_width
);
781 for (report
= ref
->report
; report
; report
= report
->next
)
782 print_one_push_report(ref
, dest
, count
+ n
++,
783 report
, porcelain
, summary_width
);
787 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
789 char hex
[GIT_MAX_HEXSZ
+ 1];
790 int w
= repo_find_unique_abbrev_r(the_repository
, hex
, oid
,
793 return (w
< sofar
) ? sofar
: w
;
796 int transport_summary_width(const struct ref
*refs
)
800 for (; refs
; refs
= refs
->next
) {
801 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
802 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
805 maxw
= FALLBACK_DEFAULT_ABBREV
;
806 return (2 * maxw
+ 3);
809 void transport_print_push_status(const char *dest
, struct ref
*refs
,
810 int verbose
, int porcelain
, unsigned int *reject_reasons
)
815 int summary_width
= transport_summary_width(refs
);
817 if (transport_color_config() < 0)
818 warning(_("could not parse transport.color.* config"));
820 head
= refs_resolve_refdup(get_main_ref_store(the_repository
), "HEAD",
821 RESOLVE_REF_READING
, NULL
, NULL
);
824 for (ref
= refs
; ref
; ref
= ref
->next
)
825 if (ref
->status
== REF_STATUS_UPTODATE
)
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_OK
)
832 n
+= print_one_push_status(ref
, dest
, n
,
833 porcelain
, summary_width
);
836 for (ref
= refs
; ref
; ref
= ref
->next
) {
837 if (ref
->status
!= REF_STATUS_NONE
&&
838 ref
->status
!= REF_STATUS_UPTODATE
&&
839 ref
->status
!= REF_STATUS_OK
)
840 n
+= print_one_push_status(ref
, dest
, n
,
841 porcelain
, summary_width
);
842 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
843 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
844 *reject_reasons
|= REJECT_NON_FF_HEAD
;
846 *reject_reasons
|= REJECT_NON_FF_OTHER
;
847 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
848 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
849 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
850 *reject_reasons
|= REJECT_FETCH_FIRST
;
851 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
852 *reject_reasons
|= REJECT_NEEDS_FORCE
;
853 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
854 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
860 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
862 struct git_transport_data
*data
= transport
->data
;
863 struct send_pack_args args
;
866 if (transport_color_config() < 0)
869 if (!data
->finished_handshake
)
870 get_refs_via_connect(transport
, 1, NULL
);
872 memset(&args
, 0, sizeof(args
));
873 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
874 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
875 args
.use_thin_pack
= data
->options
.thin
;
876 args
.verbose
= (transport
->verbose
> 0);
877 args
.quiet
= (transport
->verbose
< 0);
878 args
.progress
= transport
->progress
;
879 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
880 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
881 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
882 args
.push_options
= transport
->push_options
;
883 args
.url
= transport
->url
;
885 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
886 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
887 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
888 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
890 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
892 switch (data
->version
) {
894 die(_("support for protocol v2 not implemented yet"));
898 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
901 case protocol_unknown_version
:
902 BUG("unknown protocol version");
908 * Atomic push may abort the connection early and close the pipe,
909 * which may cause an error for `finish_connect()`. Ignore this error
910 * for atomic git-push.
912 if (ret
|| args
.atomic
)
913 finish_connect(data
->conn
);
915 ret
= finish_connect(data
->conn
);
917 data
->finished_handshake
= 0;
922 static int connect_git(struct transport
*transport
, const char *name
,
923 const char *executable
, int fd
[2])
925 struct git_transport_data
*data
= transport
->data
;
926 data
->conn
= git_connect(data
->fd
, transport
->url
,
927 name
, executable
, 0);
933 static int disconnect_git(struct transport
*transport
)
935 struct git_transport_data
*data
= transport
->data
;
937 if (data
->finished_handshake
&& !transport
->stateless_rpc
)
938 packet_flush(data
->fd
[1]);
940 if (data
->fd
[1] >= 0)
942 finish_connect(data
->conn
);
945 list_objects_filter_release(&data
->options
.filter_options
);
950 static struct transport_vtable taken_over_vtable
= {
951 .get_refs_list
= get_refs_via_connect
,
952 .get_bundle_uri
= get_bundle_uri
,
953 .fetch_refs
= fetch_refs_via_pack
,
954 .push_refs
= git_transport_push
,
955 .disconnect
= disconnect_git
958 void transport_take_over(struct transport
*transport
,
959 struct child_process
*child
)
961 struct git_transport_data
*data
;
963 if (!transport
->smart_options
)
964 BUG("taking over transport requires non-NULL "
965 "smart_options field.");
967 CALLOC_ARRAY(data
, 1);
968 data
->options
= *transport
->smart_options
;
970 data
->fd
[0] = data
->conn
->out
;
971 data
->fd
[1] = data
->conn
->in
;
972 data
->finished_handshake
= 0;
973 transport
->data
= data
;
975 transport
->vtable
= &taken_over_vtable
;
976 transport
->smart_options
= &(data
->options
);
978 transport
->cannot_reuse
= 1;
981 static int is_file(const char *url
)
986 return S_ISREG(buf
.st_mode
);
989 static int external_specification_len(const char *url
)
991 return strchr(url
, ':') - url
;
994 static const struct string_list
*protocol_allow_list(void)
996 static int enabled
= -1;
997 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
1000 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
1002 string_list_split(&allowed
, v
, ':', -1);
1003 string_list_sort(&allowed
);
1010 return enabled
? &allowed
: NULL
;
1013 enum protocol_allow_config
{
1014 PROTOCOL_ALLOW_NEVER
= 0,
1015 PROTOCOL_ALLOW_USER_ONLY
,
1016 PROTOCOL_ALLOW_ALWAYS
1019 static enum protocol_allow_config
parse_protocol_config(const char *key
,
1022 if (!strcasecmp(value
, "always"))
1023 return PROTOCOL_ALLOW_ALWAYS
;
1024 else if (!strcasecmp(value
, "never"))
1025 return PROTOCOL_ALLOW_NEVER
;
1026 else if (!strcasecmp(value
, "user"))
1027 return PROTOCOL_ALLOW_USER_ONLY
;
1029 die(_("unknown value for config '%s': %s"), key
, value
);
1032 static enum protocol_allow_config
get_protocol_config(const char *type
)
1034 char *key
= xstrfmt("protocol.%s.allow", type
);
1037 /* first check the per-protocol config */
1038 if (!git_config_get_string(key
, &value
)) {
1039 enum protocol_allow_config ret
=
1040 parse_protocol_config(key
, value
);
1047 /* if defined, fallback to user-defined default for unknown protocols */
1048 if (!git_config_get_string("protocol.allow", &value
)) {
1049 enum protocol_allow_config ret
=
1050 parse_protocol_config("protocol.allow", value
);
1055 /* fallback to built-in defaults */
1057 if (!strcmp(type
, "http") ||
1058 !strcmp(type
, "https") ||
1059 !strcmp(type
, "git") ||
1060 !strcmp(type
, "ssh"))
1061 return PROTOCOL_ALLOW_ALWAYS
;
1063 /* known scary; err on the side of caution */
1064 if (!strcmp(type
, "ext"))
1065 return PROTOCOL_ALLOW_NEVER
;
1067 /* unknown; by default let them be used only directly by the user */
1068 return PROTOCOL_ALLOW_USER_ONLY
;
1071 int is_transport_allowed(const char *type
, int from_user
)
1073 const struct string_list
*allow_list
= protocol_allow_list();
1075 return string_list_has_string(allow_list
, type
);
1077 switch (get_protocol_config(type
)) {
1078 case PROTOCOL_ALLOW_ALWAYS
:
1080 case PROTOCOL_ALLOW_NEVER
:
1082 case PROTOCOL_ALLOW_USER_ONLY
:
1084 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1088 BUG("invalid protocol_allow_config type");
1091 void transport_check_allowed(const char *type
)
1093 if (!is_transport_allowed(type
, -1))
1094 die(_("transport '%s' not allowed"), type
);
1097 static struct transport_vtable bundle_vtable
= {
1098 .get_refs_list
= get_refs_from_bundle
,
1099 .fetch_refs
= fetch_refs_from_bundle
,
1100 .disconnect
= close_bundle
1103 static struct transport_vtable builtin_smart_vtable
= {
1104 .get_refs_list
= get_refs_via_connect
,
1105 .get_bundle_uri
= get_bundle_uri
,
1106 .fetch_refs
= fetch_refs_via_pack
,
1107 .push_refs
= git_transport_push
,
1108 .connect
= connect_git
,
1109 .disconnect
= disconnect_git
1112 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1115 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1117 ret
->progress
= isatty(2);
1118 string_list_init_dup(&ret
->pack_lockfiles
);
1120 CALLOC_ARRAY(ret
->bundles
, 1);
1121 init_bundle_list(ret
->bundles
);
1124 BUG("No remote provided to transport_get()");
1126 ret
->got_remote_refs
= 0;
1127 ret
->remote
= remote
;
1128 helper
= remote
->foreign_vcs
;
1130 if (!url
&& remote
->url
)
1131 url
= remote
->url
[0];
1134 /* maybe it is a foreign URL? */
1136 const char *p
= url
;
1138 while (is_urlschemechar(p
== url
, *p
))
1140 if (starts_with(p
, "::"))
1141 helper
= xstrndup(url
, p
- url
);
1145 transport_helper_init(ret
, helper
);
1146 } else if (starts_with(url
, "rsync:")) {
1147 die(_("git-over-rsync is no longer supported"));
1148 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1149 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1150 bundle_header_init(&data
->header
);
1151 transport_check_allowed("file");
1153 ret
->vtable
= &bundle_vtable
;
1154 ret
->smart_options
= NULL
;
1155 } else if (!is_url(url
)
1156 || starts_with(url
, "file://")
1157 || starts_with(url
, "git://")
1158 || starts_with(url
, "ssh://")
1159 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1160 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1163 * These are builtin smart transports; "allowed" transports
1164 * will be checked individually in git_connect.
1166 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1167 list_objects_filter_init(&data
->options
.filter_options
);
1169 ret
->vtable
= &builtin_smart_vtable
;
1170 ret
->smart_options
= &(data
->options
);
1173 data
->finished_handshake
= 0;
1175 /* Unknown protocol in URL. Pass to external handler. */
1176 int len
= external_specification_len(url
);
1177 char *handler
= xmemdupz(url
, len
);
1178 transport_helper_init(ret
, handler
);
1181 if (ret
->smart_options
) {
1182 ret
->smart_options
->thin
= 1;
1183 ret
->smart_options
->uploadpack
= "git-upload-pack";
1184 if (remote
->uploadpack
)
1185 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1186 ret
->smart_options
->receivepack
= "git-receive-pack";
1187 if (remote
->receivepack
)
1188 ret
->smart_options
->receivepack
= remote
->receivepack
;
1191 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1196 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1198 return transport
->hash_algo
;
1201 int transport_set_option(struct transport
*transport
,
1202 const char *name
, const char *value
)
1204 int git_reports
= 1, protocol_reports
= 1;
1206 if (transport
->smart_options
)
1207 git_reports
= set_git_option(transport
->smart_options
,
1210 if (transport
->vtable
->set_option
)
1211 protocol_reports
= transport
->vtable
->set_option(transport
,
1214 /* If either report is 0, report 0 (success). */
1215 if (!git_reports
|| !protocol_reports
)
1217 /* If either reports -1 (invalid value), report -1. */
1218 if ((git_reports
== -1) || (protocol_reports
== -1))
1220 /* Otherwise if both report unknown, report unknown. */
1224 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1228 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1230 transport
->verbose
= -1;
1233 * Rules used to determine whether to report progress (processing aborts
1234 * when a rule is satisfied):
1236 * . Report progress, if force_progress is 1 (ie. --progress).
1237 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1238 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1239 * . Report progress if isatty(2) is 1.
1241 if (force_progress
>= 0)
1242 transport
->progress
= !!force_progress
;
1244 transport
->progress
= verbosity
>= 0 && isatty(2);
1247 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1251 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1252 "not be found on any remote:\n"));
1253 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1254 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1255 fprintf(stderr
, _("\nPlease try\n\n"
1256 " git push --recurse-submodules=on-demand\n\n"
1257 "or cd to the path and use\n\n"
1259 "to push them to a remote.\n\n"));
1261 string_list_clear(needs_pushing
, 0);
1263 die(_("Aborting."));
1266 static int run_pre_push_hook(struct transport
*transport
,
1267 struct ref
*remote_refs
)
1271 struct child_process proc
= CHILD_PROCESS_INIT
;
1273 const char *hook_path
= find_hook("pre-push");
1278 strvec_push(&proc
.args
, hook_path
);
1279 strvec_push(&proc
.args
, transport
->remote
->name
);
1280 strvec_push(&proc
.args
, transport
->url
);
1283 proc
.trace2_hook_name
= "pre-push";
1285 if (start_command(&proc
)) {
1286 finish_command(&proc
);
1290 sigchain_push(SIGPIPE
, SIG_IGN
);
1292 strbuf_init(&buf
, 256);
1294 for (r
= remote_refs
; r
; r
= r
->next
) {
1295 if (!r
->peer_ref
) continue;
1296 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1297 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1298 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1299 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1302 strbuf_addf( &buf
, "%s %s %s %s\n",
1303 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1304 r
->name
, oid_to_hex(&r
->old_oid
));
1306 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1307 /* We do not mind if a hook does not read all refs. */
1314 strbuf_release(&buf
);
1320 sigchain_pop(SIGPIPE
);
1322 x
= finish_command(&proc
);
1329 int transport_push(struct repository
*r
,
1330 struct transport
*transport
,
1331 struct refspec
*rs
, int flags
,
1332 unsigned int *reject_reasons
)
1334 struct ref
*remote_refs
= NULL
;
1335 struct ref
*local_refs
= NULL
;
1336 int match_flags
= MATCH_REFS_NONE
;
1337 int verbose
= (transport
->verbose
> 0);
1338 int quiet
= (transport
->verbose
< 0);
1339 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1340 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1343 struct transport_ls_refs_options transport_options
=
1344 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1346 *reject_reasons
= 0;
1348 if (transport_color_config() < 0)
1351 if (!transport
->vtable
->push_refs
)
1354 local_refs
= get_local_heads();
1356 if (check_push_refs(local_refs
, rs
) < 0)
1359 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1361 trace2_region_enter("transport_push", "get_refs_list", r
);
1362 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1363 &transport_options
);
1364 trace2_region_leave("transport_push", "get_refs_list", r
);
1366 transport_ls_refs_options_release(&transport_options
);
1368 if (flags
& TRANSPORT_PUSH_ALL
)
1369 match_flags
|= MATCH_REFS_ALL
;
1370 if (flags
& TRANSPORT_PUSH_MIRROR
)
1371 match_flags
|= MATCH_REFS_MIRROR
;
1372 if (flags
& TRANSPORT_PUSH_PRUNE
)
1373 match_flags
|= MATCH_REFS_PRUNE
;
1374 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1375 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1377 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1380 if (transport
->smart_options
&&
1381 transport
->smart_options
->cas
&&
1382 !is_empty_cas(transport
->smart_options
->cas
))
1383 apply_push_cas(transport
->smart_options
->cas
,
1384 transport
->remote
, remote_refs
);
1386 set_ref_status_for_push(remote_refs
,
1387 flags
& TRANSPORT_PUSH_MIRROR
,
1388 flags
& TRANSPORT_PUSH_FORCE
);
1390 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1391 if (run_pre_push_hook(transport
, remote_refs
))
1394 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1395 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1396 !is_bare_repository()) {
1397 struct ref
*ref
= remote_refs
;
1398 struct oid_array commits
= OID_ARRAY_INIT
;
1400 trace2_region_enter("transport_push", "push_submodules", r
);
1401 for (; ref
; ref
= ref
->next
)
1402 if (!is_null_oid(&ref
->new_oid
))
1403 oid_array_append(&commits
,
1406 if (!push_unpushed_submodules(r
,
1410 transport
->push_options
,
1412 oid_array_clear(&commits
);
1413 trace2_region_leave("transport_push", "push_submodules", r
);
1414 die(_("failed to push all needed submodules"));
1416 oid_array_clear(&commits
);
1417 trace2_region_leave("transport_push", "push_submodules", r
);
1420 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1421 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1422 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1423 !pretend
)) && !is_bare_repository()) {
1424 struct ref
*ref
= remote_refs
;
1425 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1426 struct oid_array commits
= OID_ARRAY_INIT
;
1428 trace2_region_enter("transport_push", "check_submodules", r
);
1429 for (; ref
; ref
= ref
->next
)
1430 if (!is_null_oid(&ref
->new_oid
))
1431 oid_array_append(&commits
,
1434 if (find_unpushed_submodules(r
,
1436 transport
->remote
->name
,
1438 oid_array_clear(&commits
);
1439 trace2_region_leave("transport_push", "check_submodules", r
);
1440 die_with_unpushed_submodules(&needs_pushing
);
1442 string_list_clear(&needs_pushing
, 0);
1443 oid_array_clear(&commits
);
1444 trace2_region_leave("transport_push", "check_submodules", r
);
1447 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1448 trace2_region_enter("transport_push", "push_refs", r
);
1449 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1450 trace2_region_leave("transport_push", "push_refs", r
);
1453 err
= push_had_errors(remote_refs
);
1454 ret
= push_ret
| err
;
1457 transport_print_push_status(transport
->url
, remote_refs
,
1458 verbose
| porcelain
, porcelain
,
1461 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1462 set_upstreams(transport
, remote_refs
, pretend
);
1464 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1465 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1467 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1468 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1471 if (porcelain
&& !push_ret
)
1473 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1474 /* stable plumbing output; do not modify or localize */
1475 fprintf(stderr
, "Everything up-to-date\n");
1478 free_refs(local_refs
);
1479 free_refs(remote_refs
);
1483 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1484 struct transport_ls_refs_options
*transport_options
)
1486 if (!transport
->got_remote_refs
) {
1487 transport
->remote_refs
=
1488 transport
->vtable
->get_refs_list(transport
, 0,
1490 transport
->got_remote_refs
= 1;
1493 return transport
->remote_refs
;
1496 void transport_ls_refs_options_release(struct transport_ls_refs_options
*opts
)
1498 strvec_clear(&opts
->ref_prefixes
);
1499 free((char *)opts
->unborn_head_target
);
1502 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1505 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1506 struct ref
**heads
= NULL
;
1509 for (rm
= refs
; rm
; rm
= rm
->next
) {
1512 !is_null_oid(&rm
->old_oid
) &&
1513 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1515 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1516 heads
[nr_heads
++] = rm
;
1521 * When deepening of a shallow repository is requested,
1522 * then local and remote refs are likely to still be equal.
1523 * Just feed them all to the fetch method in that case.
1524 * This condition shouldn't be met in a non-deepening fetch
1525 * (see builtin/fetch.c:quickfetch()).
1527 ALLOC_ARRAY(heads
, nr_refs
);
1528 for (rm
= refs
; rm
; rm
= rm
->next
)
1529 heads
[nr_heads
++] = rm
;
1532 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1538 int transport_get_remote_bundle_uri(struct transport
*transport
)
1541 const struct transport_vtable
*vtable
= transport
->vtable
;
1543 /* Check config only once. */
1544 if (transport
->got_remote_bundle_uri
)
1546 transport
->got_remote_bundle_uri
= 1;
1549 * Don't request bundle-uri from the server unless configured to
1550 * do so by the transfer.bundleURI=true config option.
1552 if (git_config_get_bool("transfer.bundleuri", &value
) || !value
)
1555 if (!transport
->bundles
->baseURI
)
1556 transport
->bundles
->baseURI
= xstrdup(transport
->url
);
1558 if (!vtable
->get_bundle_uri
)
1559 return error(_("bundle-uri operation not supported by protocol"));
1561 if (vtable
->get_bundle_uri(transport
) < 0)
1562 return error(_("could not retrieve server-advertised bundle-uri list"));
1566 void transport_unlock_pack(struct transport
*transport
, unsigned int flags
)
1568 int in_signal_handler
= !!(flags
& TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER
);
1571 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1572 if (in_signal_handler
)
1573 unlink(transport
->pack_lockfiles
.items
[i
].string
);
1575 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1576 if (!in_signal_handler
)
1577 string_list_clear(&transport
->pack_lockfiles
, 0);
1580 int transport_connect(struct transport
*transport
, const char *name
,
1581 const char *exec
, int fd
[2])
1583 if (transport
->vtable
->connect
)
1584 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1586 die(_("operation not supported by protocol"));
1589 int transport_disconnect(struct transport
*transport
)
1592 if (transport
->vtable
->disconnect
)
1593 ret
= transport
->vtable
->disconnect(transport
);
1594 if (transport
->got_remote_refs
)
1595 free_refs((void *)transport
->remote_refs
);
1596 clear_bundle_list(transport
->bundles
);
1597 free(transport
->bundles
);
1603 * Strip username (and password) from a URL and return
1604 * it in a newly allocated string.
1606 char *transport_anonymize_url(const char *url
)
1608 char *scheme_prefix
, *anon_part
;
1609 size_t anon_len
, prefix_len
= 0;
1611 anon_part
= strchr(url
, '@');
1612 if (url_is_local_not_ssh(url
) || !anon_part
)
1615 anon_len
= strlen(++anon_part
);
1616 scheme_prefix
= strstr(url
, "://");
1617 if (!scheme_prefix
) {
1618 if (!strchr(anon_part
, ':'))
1619 /* cannot be "me@there:/path/name" */
1623 /* make sure scheme is reasonable */
1624 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1627 case '+': case '.': case '-':
1636 /* @ past the first slash does not count */
1637 cp
= strchr(scheme_prefix
+ 3, '/');
1638 if (cp
&& cp
< anon_part
)
1640 prefix_len
= scheme_prefix
- url
+ 3;
1642 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1643 (int)anon_len
, anon_part
);
1645 return xstrdup(url
);