1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
6 #include "environment.h"
11 #include "fetch-pack.h"
14 #include "send-pack.h"
21 #include "submodule.h"
23 #include "string-list.h"
24 #include "oid-array.h"
27 #include "transport-internal.h"
29 #include "object-name.h"
31 #include "bundle-uri.h"
33 static int transport_use_color
= -1;
34 static char transport_colors
[][COLOR_MAXLEN
] = {
36 GIT_COLOR_RED
/* REJECTED */
39 enum color_transport
{
40 TRANSPORT_COLOR_RESET
= 0,
41 TRANSPORT_COLOR_REJECTED
= 1
44 static int transport_color_config(void)
46 const char *keys
[] = {
47 "color.transport.reset",
48 "color.transport.rejected"
49 }, *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 (size_t 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
= refs_resolve_ref_unsafe(get_main_ref_store(the_repository
),
106 localname
, RESOLVE_REF_READING
,
108 if (tmp
&& flag
& REF_ISSYMREF
&&
109 starts_with(tmp
, "refs/heads/"))
112 /* Both source and destination must be local branches. */
113 if (!localname
|| !starts_with(localname
, "refs/heads/"))
115 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
119 int flag
= transport
->verbose
< 0 ? 0 : BRANCH_CONFIG_VERBOSE
;
120 install_branch_config(flag
, localname
+ 11,
121 transport
->remote
->name
, remotename
);
122 } else if (transport
->verbose
>= 0)
123 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
124 localname
+ 11, remotename
+ 11,
125 transport
->remote
->name
);
129 struct bundle_transport_data
{
131 struct bundle_header header
;
132 unsigned get_refs_from_bundle_called
: 1;
135 static void get_refs_from_bundle_inner(struct transport
*transport
)
137 struct bundle_transport_data
*data
= transport
->data
;
139 data
->get_refs_from_bundle_called
= 1;
143 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
145 die(_("could not read bundle '%s'"), transport
->url
);
147 transport
->hash_algo
= data
->header
.hash_algo
;
150 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
152 struct transport_ls_refs_options
*transport_options UNUSED
)
154 struct bundle_transport_data
*data
= transport
->data
;
155 struct ref
*result
= NULL
;
160 get_refs_from_bundle_inner(transport
);
162 for (size_t 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_fsck_config_cb(const char *var
, const char *value
,
175 const struct config_context
*ctx UNUSED
, void *cb
)
177 struct strbuf
*msg_types
= cb
;
180 ret
= fetch_pack_fsck_config(var
, value
, msg_types
);
187 static int fetch_refs_from_bundle(struct transport
*transport
,
189 struct ref
**to_fetch UNUSED
)
191 struct unbundle_opts opts
= {
192 .flags
= fetch_pack_fsck_objects() ? VERIFY_BUNDLE_FSCK
: 0,
194 struct bundle_transport_data
*data
= transport
->data
;
195 struct strvec extra_index_pack_args
= STRVEC_INIT
;
196 struct strbuf msg_types
= STRBUF_INIT
;
199 if (transport
->progress
)
200 strvec_push(&extra_index_pack_args
, "-v");
202 if (!data
->get_refs_from_bundle_called
)
203 get_refs_from_bundle_inner(transport
);
205 git_config(fetch_fsck_config_cb
, &msg_types
);
206 opts
.fsck_msg_types
= msg_types
.buf
;
208 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
209 &extra_index_pack_args
, &opts
);
210 data
->fd
= -1; /* `unbundle()` closes the file descriptor */
211 transport
->hash_algo
= data
->header
.hash_algo
;
213 strvec_clear(&extra_index_pack_args
);
214 strbuf_release(&msg_types
);
218 static int close_bundle(struct transport
*transport
)
220 struct bundle_transport_data
*data
= transport
->data
;
223 bundle_header_release(&data
->header
);
228 struct git_transport_data
{
229 struct git_transport_options options
;
230 struct child_process
*conn
;
232 unsigned finished_handshake
: 1;
233 enum protocol_version version
;
234 struct oid_array extra_have
;
235 struct oid_array shallow
;
238 static int set_git_option(struct git_transport_options
*opts
,
239 const char *name
, const char *value
)
241 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
242 opts
->uploadpack
= value
;
244 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
245 opts
->receivepack
= value
;
247 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
248 opts
->thin
= !!value
;
250 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
251 opts
->followtags
= !!value
;
253 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
254 opts
->keep
= !!value
;
256 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
257 opts
->update_shallow
= !!value
;
259 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
264 opts
->depth
= strtol(value
, &end
, 0);
266 die(_("transport: invalid depth option '%s'"), value
);
269 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
270 opts
->deepen_since
= value
;
272 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
273 opts
->deepen_not
= (const struct string_list
*)value
;
275 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
276 opts
->deepen_relative
= !!value
;
278 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
279 opts
->from_promisor
= !!value
;
281 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
282 list_objects_filter_die_if_populated(&opts
->filter_options
);
283 parse_list_objects_filter(&opts
->filter_options
, value
);
285 } else if (!strcmp(name
, TRANS_OPT_REFETCH
)) {
286 opts
->refetch
= !!value
;
288 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
289 opts
->reject_shallow
= !!value
;
295 static int connect_setup(struct transport
*transport
, int for_push
)
297 struct git_transport_data
*data
= transport
->data
;
298 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
303 switch (transport
->family
) {
304 case TRANSPORT_FAMILY_ALL
: break;
305 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
306 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
309 data
->conn
= git_connect(data
->fd
, transport
->url
,
314 data
->options
.receivepack
:
315 data
->options
.uploadpack
,
321 static void die_if_server_options(struct transport
*transport
)
323 if (!transport
->server_options
|| !transport
->server_options
->nr
)
325 advise(_("see protocol.version in 'git help config' for more details"));
326 die(_("server options require protocol version 2 or later"));
330 * Obtains the protocol version from the transport and writes it to
331 * transport->data->version, first connecting if not already connected.
333 * If the protocol version is one that allows skipping the listing of remote
334 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
335 * this function returns NULL. Otherwise, this function returns the list of
338 static struct ref
*handshake(struct transport
*transport
, int for_push
,
339 struct transport_ls_refs_options
*options
,
342 struct git_transport_data
*data
= transport
->data
;
343 struct ref
*refs
= NULL
;
344 struct packet_reader reader
;
346 const char *server_sid
;
348 connect_setup(transport
, for_push
);
350 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
351 PACKET_READ_CHOMP_NEWLINE
|
352 PACKET_READ_GENTLE_ON_EOF
|
353 PACKET_READ_DIE_ON_ERR_PACKET
);
355 data
->version
= discover_version(&reader
);
356 switch (data
->version
) {
358 if ((!transport
->server_options
|| !transport
->server_options
->nr
) &&
359 transport
->remote
->server_options
.nr
)
360 transport
->server_options
= &transport
->remote
->server_options
;
361 if (server_feature_v2("session-id", &server_sid
))
362 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
364 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
366 transport
->server_options
,
367 transport
->stateless_rpc
);
371 die_if_server_options(transport
);
372 get_remote_heads(&reader
, &refs
,
373 for_push
? REF_NORMAL
: 0,
376 server_sid
= server_feature_value("session-id", &sid_len
);
378 char *sid
= xstrndup(server_sid
, sid_len
);
379 trace2_data_string("transfer", NULL
, "server-sid", sid
);
383 case protocol_unknown_version
:
384 BUG("unknown protocol version");
386 data
->finished_handshake
= 1;
387 transport
->hash_algo
= reader
.hash_algo
;
389 if (reader
.line_peeked
)
390 BUG("buffer must be empty at the end of handshake()");
395 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
396 struct transport_ls_refs_options
*options
)
398 return handshake(transport
, for_push
, options
, 1);
401 static int get_bundle_uri(struct transport
*transport
)
403 struct git_transport_data
*data
= transport
->data
;
404 struct packet_reader reader
;
405 int stateless_rpc
= transport
->stateless_rpc
;
407 if (!transport
->bundles
) {
408 CALLOC_ARRAY(transport
->bundles
, 1);
409 init_bundle_list(transport
->bundles
);
412 if (!data
->finished_handshake
) {
413 struct ref
*refs
= handshake(transport
, 0, NULL
, 0);
420 * "Support" protocol v0 and v2 without bundle-uri support by
421 * silently degrading to a NOOP.
423 if (!server_supports_v2("bundle-uri"))
426 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
427 PACKET_READ_CHOMP_NEWLINE
|
428 PACKET_READ_GENTLE_ON_EOF
);
430 return get_remote_bundle_uri(data
->fd
[1], &reader
,
431 transport
->bundles
, stateless_rpc
);
434 static int fetch_refs_via_pack(struct transport
*transport
,
435 int nr_heads
, struct ref
**to_fetch
)
438 struct git_transport_data
*data
= transport
->data
;
439 struct ref
*refs
= NULL
;
440 struct fetch_pack_args args
;
441 struct ref
*refs_tmp
= NULL
, **to_fetch_dup
= NULL
;
443 memset(&args
, 0, sizeof(args
));
444 args
.uploadpack
= data
->options
.uploadpack
;
445 args
.keep_pack
= data
->options
.keep
;
447 args
.use_thin_pack
= data
->options
.thin
;
448 args
.include_tag
= data
->options
.followtags
;
449 args
.verbose
= (transport
->verbose
> 1);
450 args
.quiet
= (transport
->verbose
< 0);
451 args
.no_progress
= !transport
->progress
;
452 args
.depth
= data
->options
.depth
;
453 args
.deepen_since
= data
->options
.deepen_since
;
454 args
.deepen_not
= data
->options
.deepen_not
;
455 args
.deepen_relative
= data
->options
.deepen_relative
;
456 args
.check_self_contained_and_connected
=
457 data
->options
.check_self_contained_and_connected
;
458 args
.cloning
= transport
->cloning
;
459 args
.update_shallow
= data
->options
.update_shallow
;
460 args
.from_promisor
= data
->options
.from_promisor
;
461 list_objects_filter_copy(&args
.filter_options
,
462 &data
->options
.filter_options
);
463 args
.refetch
= data
->options
.refetch
;
464 args
.stateless_rpc
= transport
->stateless_rpc
;
465 args
.server_options
= transport
->server_options
;
466 args
.negotiation_tips
= data
->options
.negotiation_tips
;
467 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
469 if (!data
->finished_handshake
) {
471 int must_list_refs
= 0;
472 for (i
= 0; i
< nr_heads
; i
++) {
473 if (!to_fetch
[i
]->exact_oid
) {
478 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
481 if (data
->version
== protocol_unknown_version
)
482 BUG("unknown protocol version");
483 else if (data
->version
<= protocol_v1
)
484 die_if_server_options(transport
);
486 if (data
->options
.acked_commits
) {
487 if (data
->version
< protocol_v2
) {
488 warning(_("--negotiate-only requires protocol v2"));
490 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
491 warning(_("server does not support wait-for-done"));
494 negotiate_using_fetch(data
->options
.negotiation_tips
,
495 transport
->server_options
,
496 transport
->stateless_rpc
,
498 data
->options
.acked_commits
);
505 * Create a shallow copy of `sought` so that we can free all of its entries.
506 * This is because `fetch_pack()` will modify the array to evict some
507 * entries, but won't free those.
509 DUP_ARRAY(to_fetch_dup
, to_fetch
, nr_heads
);
510 to_fetch
= to_fetch_dup
;
512 refs
= fetch_pack(&args
, data
->fd
,
513 refs_tmp
? refs_tmp
: transport
->remote_refs
,
514 to_fetch
, nr_heads
, &data
->shallow
,
515 &transport
->pack_lockfiles
, data
->version
);
517 data
->finished_handshake
= 0;
518 data
->options
.self_contained_and_connected
=
519 args
.self_contained_and_connected
;
520 data
->options
.connectivity_checked
= args
.connectivity_checked
;
524 if (report_unmatched_refs(to_fetch
, nr_heads
))
529 if (data
->fd
[1] >= 0)
531 if (finish_connect(data
->conn
))
538 list_objects_filter_release(&args
.filter_options
);
542 static int push_had_errors(struct ref
*ref
)
544 for (; ref
; ref
= ref
->next
) {
545 switch (ref
->status
) {
546 case REF_STATUS_NONE
:
547 case REF_STATUS_UPTODATE
:
557 int transport_refs_pushed(struct ref
*ref
)
559 for (; ref
; ref
= ref
->next
) {
560 switch(ref
->status
) {
561 case REF_STATUS_NONE
:
562 case REF_STATUS_UPTODATE
:
571 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
572 struct object_id
*new_oid
, int deletion
,
575 struct refspec_item rs
;
577 memset(&rs
, 0, sizeof(rs
));
581 if (!remote_find_tracking(remote
, &rs
)) {
583 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
585 refs_delete_ref(get_main_ref_store(the_repository
),
586 NULL
, rs
.dst
, NULL
, 0);
588 refs_update_ref(get_main_ref_store(the_repository
),
589 "update by push", rs
.dst
, new_oid
,
595 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
598 struct object_id
*new_oid
;
599 struct ref_push_report
*report
;
601 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
604 report
= ref
->report
;
606 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
607 ref
->deletion
, verbose
);
609 for (; report
; report
= report
->next
) {
610 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
611 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
612 update_one_tracking_ref(remote
, refname
, new_oid
,
613 is_null_oid(new_oid
), verbose
);
617 static void print_ref_status(char flag
, const char *summary
,
618 struct ref
*to
, struct ref
*from
, const char *msg
,
619 struct ref_push_report
*report
,
620 int porcelain
, int summary_width
)
624 if (report
&& report
->ref_name
)
625 to_name
= report
->ref_name
;
631 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
633 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
635 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
637 fprintf(stdout
, "%s\n", summary
);
639 const char *red
= "", *reset
= "";
640 if (push_had_errors(to
)) {
641 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
642 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
644 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
647 fprintf(stderr
, "%s -> %s",
648 prettify_refname(from
->name
),
649 prettify_refname(to_name
));
651 fputs(prettify_refname(to_name
), stderr
);
661 static void print_ok_ref_status(struct ref
*ref
,
662 struct ref_push_report
*report
,
663 int porcelain
, int summary_width
)
665 struct object_id
*old_oid
;
666 struct object_id
*new_oid
;
667 const char *ref_name
;
670 if (report
&& report
->old_oid
)
671 old_oid
= report
->old_oid
;
673 old_oid
= &ref
->old_oid
;
674 if (report
&& report
->new_oid
)
675 new_oid
= report
->new_oid
;
677 new_oid
= &ref
->new_oid
;
678 if (report
&& report
->forced_update
)
679 forced_update
= report
->forced_update
;
681 forced_update
= ref
->forced_update
;
682 if (report
&& report
->ref_name
)
683 ref_name
= report
->ref_name
;
685 ref_name
= ref
->name
;
688 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
689 report
, porcelain
, summary_width
);
690 else if (is_null_oid(old_oid
))
691 print_ref_status('*',
692 (starts_with(ref_name
, "refs/tags/")
694 : (starts_with(ref_name
, "refs/heads/")
696 : "[new reference]")),
697 ref
, ref
->peer_ref
, NULL
,
698 report
, porcelain
, summary_width
);
700 struct strbuf quickref
= STRBUF_INIT
;
704 strbuf_add_unique_abbrev(&quickref
, old_oid
,
707 strbuf_addstr(&quickref
, "...");
709 msg
= "forced update";
711 strbuf_addstr(&quickref
, "..");
715 strbuf_add_unique_abbrev(&quickref
, new_oid
,
718 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
719 report
, porcelain
, summary_width
);
720 strbuf_release(&quickref
);
724 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
725 struct ref_push_report
*report
,
726 int porcelain
, int summary_width
)
729 char *url
= transport_anonymize_url(dest
);
730 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
734 switch(ref
->status
) {
735 case REF_STATUS_NONE
:
736 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
737 report
, porcelain
, summary_width
);
739 case REF_STATUS_REJECT_NODELETE
:
740 print_ref_status('!', "[rejected]", ref
, NULL
,
741 "remote does not support deleting refs",
742 report
, porcelain
, summary_width
);
744 case REF_STATUS_UPTODATE
:
745 print_ref_status('=', "[up to date]", ref
,
747 report
, porcelain
, summary_width
);
749 case REF_STATUS_REJECT_NONFASTFORWARD
:
750 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
752 report
, porcelain
, summary_width
);
754 case REF_STATUS_REJECT_ALREADY_EXISTS
:
755 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
757 report
, porcelain
, summary_width
);
759 case REF_STATUS_REJECT_FETCH_FIRST
:
760 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
762 report
, porcelain
, summary_width
);
764 case REF_STATUS_REJECT_NEEDS_FORCE
:
765 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
767 report
, porcelain
, summary_width
);
769 case REF_STATUS_REJECT_STALE
:
770 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
772 report
, porcelain
, summary_width
);
774 case REF_STATUS_REJECT_REMOTE_UPDATED
:
775 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
776 "remote ref updated since checkout",
777 report
, porcelain
, summary_width
);
779 case REF_STATUS_REJECT_SHALLOW
:
780 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
781 "new shallow roots not allowed",
782 report
, porcelain
, summary_width
);
784 case REF_STATUS_REMOTE_REJECT
:
785 print_ref_status('!', "[remote rejected]", ref
,
786 ref
->deletion
? NULL
: ref
->peer_ref
,
788 report
, porcelain
, summary_width
);
790 case REF_STATUS_EXPECTING_REPORT
:
791 print_ref_status('!', "[remote failure]", ref
,
792 ref
->deletion
? NULL
: ref
->peer_ref
,
793 "remote failed to report status",
794 report
, porcelain
, summary_width
);
796 case REF_STATUS_ATOMIC_PUSH_FAILED
:
797 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
798 "atomic push failed",
799 report
, porcelain
, summary_width
);
802 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
809 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
810 int porcelain
, int summary_width
)
812 struct ref_push_report
*report
;
816 return print_one_push_report(ref
, dest
, count
,
817 NULL
, porcelain
, summary_width
);
819 for (report
= ref
->report
; report
; report
= report
->next
)
820 print_one_push_report(ref
, dest
, count
+ n
++,
821 report
, porcelain
, summary_width
);
825 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
827 char hex
[GIT_MAX_HEXSZ
+ 1];
828 int w
= repo_find_unique_abbrev_r(the_repository
, hex
, oid
,
831 return (w
< sofar
) ? sofar
: w
;
834 int transport_summary_width(const struct ref
*refs
)
838 for (; refs
; refs
= refs
->next
) {
839 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
840 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
843 maxw
= FALLBACK_DEFAULT_ABBREV
;
844 return (2 * maxw
+ 3);
847 void transport_print_push_status(const char *dest
, struct ref
*refs
,
848 int verbose
, int porcelain
, unsigned int *reject_reasons
)
853 int summary_width
= transport_summary_width(refs
);
855 if (transport_color_config() < 0)
856 warning(_("could not parse transport.color.* config"));
858 head
= refs_resolve_refdup(get_main_ref_store(the_repository
), "HEAD",
859 RESOLVE_REF_READING
, NULL
, NULL
);
862 for (ref
= refs
; ref
; ref
= ref
->next
)
863 if (ref
->status
== REF_STATUS_UPTODATE
)
864 n
+= print_one_push_status(ref
, dest
, n
,
865 porcelain
, summary_width
);
868 for (ref
= refs
; ref
; ref
= ref
->next
)
869 if (ref
->status
== REF_STATUS_OK
)
870 n
+= print_one_push_status(ref
, dest
, n
,
871 porcelain
, summary_width
);
874 for (ref
= refs
; ref
; ref
= ref
->next
) {
875 if (ref
->status
!= REF_STATUS_NONE
&&
876 ref
->status
!= REF_STATUS_UPTODATE
&&
877 ref
->status
!= REF_STATUS_OK
)
878 n
+= print_one_push_status(ref
, dest
, n
,
879 porcelain
, summary_width
);
880 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
881 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
882 *reject_reasons
|= REJECT_NON_FF_HEAD
;
884 *reject_reasons
|= REJECT_NON_FF_OTHER
;
885 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
886 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
887 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
888 *reject_reasons
|= REJECT_FETCH_FIRST
;
889 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
890 *reject_reasons
|= REJECT_NEEDS_FORCE
;
891 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
892 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
898 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
900 struct git_transport_data
*data
= transport
->data
;
901 struct send_pack_args args
;
904 if (transport_color_config() < 0)
907 if (!data
->finished_handshake
)
908 get_refs_via_connect(transport
, 1, NULL
);
910 memset(&args
, 0, sizeof(args
));
911 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
912 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
913 args
.use_thin_pack
= data
->options
.thin
;
914 args
.verbose
= (transport
->verbose
> 0);
915 args
.quiet
= (transport
->verbose
< 0);
916 args
.progress
= transport
->progress
;
917 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
918 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
919 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
920 args
.push_options
= transport
->push_options
;
921 args
.url
= transport
->url
;
923 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
924 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
925 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
926 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
928 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
930 switch (data
->version
) {
932 die(_("support for protocol v2 not implemented yet"));
936 ret
= send_pack(the_repository
, &args
, data
->fd
, data
->conn
, remote_refs
,
939 * Ignore the specific error code to maintain consistent behavior
940 * with the "push_refs()" function across different transports,
941 * such as "push_refs_with_push()" for HTTP protocol.
943 if (ret
== ERROR_SEND_PACK_BAD_REF_STATUS
)
946 case protocol_unknown_version
:
947 BUG("unknown protocol version");
952 ret
|= finish_connect(data
->conn
);
954 data
->finished_handshake
= 0;
959 static int connect_git(struct transport
*transport
, const char *name
,
960 const char *executable
, int fd
[2])
962 struct git_transport_data
*data
= transport
->data
;
963 data
->conn
= git_connect(data
->fd
, transport
->url
,
964 name
, executable
, 0);
970 static int disconnect_git(struct transport
*transport
)
972 struct git_transport_data
*data
= transport
->data
;
974 if (data
->finished_handshake
&& !transport
->stateless_rpc
)
975 packet_flush(data
->fd
[1]);
977 if (data
->fd
[1] >= 0)
979 finish_connect(data
->conn
);
982 if (data
->options
.negotiation_tips
) {
983 oid_array_clear(data
->options
.negotiation_tips
);
984 free(data
->options
.negotiation_tips
);
986 list_objects_filter_release(&data
->options
.filter_options
);
987 oid_array_clear(&data
->extra_have
);
988 oid_array_clear(&data
->shallow
);
993 static struct transport_vtable taken_over_vtable
= {
994 .get_refs_list
= get_refs_via_connect
,
995 .get_bundle_uri
= get_bundle_uri
,
996 .fetch_refs
= fetch_refs_via_pack
,
997 .push_refs
= git_transport_push
,
998 .disconnect
= disconnect_git
1001 void transport_take_over(struct transport
*transport
,
1002 struct child_process
*child
)
1004 struct git_transport_data
*data
;
1006 if (!transport
->smart_options
)
1007 BUG("taking over transport requires non-NULL "
1008 "smart_options field.");
1010 CALLOC_ARRAY(data
, 1);
1011 data
->options
= *transport
->smart_options
;
1013 data
->fd
[0] = data
->conn
->out
;
1014 data
->fd
[1] = data
->conn
->in
;
1015 data
->finished_handshake
= 0;
1016 transport
->data
= data
;
1018 transport
->vtable
= &taken_over_vtable
;
1019 transport
->smart_options
= &(data
->options
);
1021 transport
->cannot_reuse
= 1;
1024 static int is_file(const char *url
)
1027 if (stat(url
, &buf
))
1029 return S_ISREG(buf
.st_mode
);
1032 static int external_specification_len(const char *url
)
1034 return strchr(url
, ':') - url
;
1037 static const struct string_list
*protocol_allow_list(void)
1039 static int enabled
= -1;
1040 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
1043 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
1045 string_list_split(&allowed
, v
, ':', -1);
1046 string_list_sort(&allowed
);
1053 return enabled
? &allowed
: NULL
;
1056 enum protocol_allow_config
{
1057 PROTOCOL_ALLOW_NEVER
= 0,
1058 PROTOCOL_ALLOW_USER_ONLY
,
1059 PROTOCOL_ALLOW_ALWAYS
1062 static enum protocol_allow_config
parse_protocol_config(const char *key
,
1065 if (!strcasecmp(value
, "always"))
1066 return PROTOCOL_ALLOW_ALWAYS
;
1067 else if (!strcasecmp(value
, "never"))
1068 return PROTOCOL_ALLOW_NEVER
;
1069 else if (!strcasecmp(value
, "user"))
1070 return PROTOCOL_ALLOW_USER_ONLY
;
1072 die(_("unknown value for config '%s': %s"), key
, value
);
1075 static enum protocol_allow_config
get_protocol_config(const char *type
)
1077 char *key
= xstrfmt("protocol.%s.allow", type
);
1080 /* first check the per-protocol config */
1081 if (!git_config_get_string(key
, &value
)) {
1082 enum protocol_allow_config ret
=
1083 parse_protocol_config(key
, value
);
1090 /* if defined, fallback to user-defined default for unknown protocols */
1091 if (!git_config_get_string("protocol.allow", &value
)) {
1092 enum protocol_allow_config ret
=
1093 parse_protocol_config("protocol.allow", value
);
1098 /* fallback to built-in defaults */
1100 if (!strcmp(type
, "http") ||
1101 !strcmp(type
, "https") ||
1102 !strcmp(type
, "git") ||
1103 !strcmp(type
, "ssh"))
1104 return PROTOCOL_ALLOW_ALWAYS
;
1106 /* known scary; err on the side of caution */
1107 if (!strcmp(type
, "ext"))
1108 return PROTOCOL_ALLOW_NEVER
;
1110 /* unknown; by default let them be used only directly by the user */
1111 return PROTOCOL_ALLOW_USER_ONLY
;
1114 int is_transport_allowed(const char *type
, int from_user
)
1116 const struct string_list
*allow_list
= protocol_allow_list();
1118 return string_list_has_string(allow_list
, type
);
1120 switch (get_protocol_config(type
)) {
1121 case PROTOCOL_ALLOW_ALWAYS
:
1123 case PROTOCOL_ALLOW_NEVER
:
1125 case PROTOCOL_ALLOW_USER_ONLY
:
1127 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1131 BUG("invalid protocol_allow_config type");
1134 int parse_transport_option(const char *var
, const char *value
,
1135 struct string_list
*transport_options
)
1138 return config_error_nonbool(var
);
1140 string_list_clear(transport_options
, 0);
1142 string_list_append(transport_options
, value
);
1146 void transport_check_allowed(const char *type
)
1148 if (!is_transport_allowed(type
, -1))
1149 die(_("transport '%s' not allowed"), type
);
1152 static struct transport_vtable bundle_vtable
= {
1153 .get_refs_list
= get_refs_from_bundle
,
1154 .fetch_refs
= fetch_refs_from_bundle
,
1155 .disconnect
= close_bundle
1158 static struct transport_vtable builtin_smart_vtable
= {
1159 .get_refs_list
= get_refs_via_connect
,
1160 .get_bundle_uri
= get_bundle_uri
,
1161 .fetch_refs
= fetch_refs_via_pack
,
1162 .push_refs
= git_transport_push
,
1163 .connect
= connect_git
,
1164 .disconnect
= disconnect_git
1167 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1170 char *helper_to_free
= NULL
;
1172 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1174 ret
->progress
= isatty(2);
1175 string_list_init_dup(&ret
->pack_lockfiles
);
1177 CALLOC_ARRAY(ret
->bundles
, 1);
1178 init_bundle_list(ret
->bundles
);
1181 BUG("No remote provided to transport_get()");
1183 ret
->got_remote_refs
= 0;
1184 ret
->remote
= remote
;
1185 helper
= remote
->foreign_vcs
;
1188 url
= remote
->url
.v
[0];
1192 while (is_urlschemechar(p
== url
, *p
))
1194 if (starts_with(p
, "::"))
1195 helper
= helper_to_free
= xstrndup(url
, p
- url
);
1198 transport_helper_init(ret
, helper
);
1199 free(helper_to_free
);
1200 } else if (starts_with(url
, "rsync:")) {
1201 die(_("git-over-rsync is no longer supported"));
1202 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1203 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1204 bundle_header_init(&data
->header
);
1205 transport_check_allowed("file");
1207 ret
->vtable
= &bundle_vtable
;
1208 ret
->smart_options
= NULL
;
1209 } else if (!is_url(url
)
1210 || starts_with(url
, "file://")
1211 || starts_with(url
, "git://")
1212 || starts_with(url
, "ssh://")
1213 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1214 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1217 * These are builtin smart transports; "allowed" transports
1218 * will be checked individually in git_connect.
1220 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1221 list_objects_filter_init(&data
->options
.filter_options
);
1223 ret
->vtable
= &builtin_smart_vtable
;
1224 ret
->smart_options
= &(data
->options
);
1227 data
->finished_handshake
= 0;
1229 /* Unknown protocol in URL. Pass to external handler. */
1230 int len
= external_specification_len(url
);
1231 char *handler
= xmemdupz(url
, len
);
1232 transport_helper_init(ret
, handler
);
1236 if (ret
->smart_options
) {
1237 ret
->smart_options
->thin
= 1;
1238 ret
->smart_options
->uploadpack
= "git-upload-pack";
1239 if (remote
->uploadpack
)
1240 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1241 ret
->smart_options
->receivepack
= "git-receive-pack";
1242 if (remote
->receivepack
)
1243 ret
->smart_options
->receivepack
= remote
->receivepack
;
1246 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1_LEGACY
];
1251 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1253 return transport
->hash_algo
;
1256 int transport_set_option(struct transport
*transport
,
1257 const char *name
, const char *value
)
1259 int git_reports
= 1, protocol_reports
= 1;
1261 if (transport
->smart_options
)
1262 git_reports
= set_git_option(transport
->smart_options
,
1265 if (transport
->vtable
->set_option
)
1266 protocol_reports
= transport
->vtable
->set_option(transport
,
1269 /* If either report is 0, report 0 (success). */
1270 if (!git_reports
|| !protocol_reports
)
1272 /* If either reports -1 (invalid value), report -1. */
1273 if ((git_reports
== -1) || (protocol_reports
== -1))
1275 /* Otherwise if both report unknown, report unknown. */
1279 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1283 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1285 transport
->verbose
= -1;
1288 * Rules used to determine whether to report progress (processing aborts
1289 * when a rule is satisfied):
1291 * . Report progress, if force_progress is 1 (ie. --progress).
1292 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1293 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1294 * . Report progress if isatty(2) is 1.
1296 if (force_progress
>= 0)
1297 transport
->progress
= !!force_progress
;
1299 transport
->progress
= verbosity
>= 0 && isatty(2);
1302 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1304 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1305 "not be found on any remote:\n"));
1306 for (size_t i
= 0; i
< needs_pushing
->nr
; i
++)
1307 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1308 fprintf(stderr
, _("\nPlease try\n\n"
1309 " git push --recurse-submodules=on-demand\n\n"
1310 "or cd to the path and use\n\n"
1312 "to push them to a remote.\n\n"));
1314 string_list_clear(needs_pushing
, 0);
1316 die(_("Aborting."));
1319 static int run_pre_push_hook(struct transport
*transport
,
1320 struct ref
*remote_refs
)
1324 struct child_process proc
= CHILD_PROCESS_INIT
;
1326 const char *hook_path
= find_hook(the_repository
, "pre-push");
1331 strvec_push(&proc
.args
, hook_path
);
1332 strvec_push(&proc
.args
, transport
->remote
->name
);
1333 strvec_push(&proc
.args
, transport
->url
);
1336 proc
.trace2_hook_name
= "pre-push";
1338 if (start_command(&proc
)) {
1339 finish_command(&proc
);
1343 sigchain_push(SIGPIPE
, SIG_IGN
);
1345 strbuf_init(&buf
, 256);
1347 for (r
= remote_refs
; r
; r
= r
->next
) {
1348 if (!r
->peer_ref
) continue;
1349 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1350 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1351 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1352 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1355 strbuf_addf( &buf
, "%s %s %s %s\n",
1356 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1357 r
->name
, oid_to_hex(&r
->old_oid
));
1359 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1360 /* We do not mind if a hook does not read all refs. */
1367 strbuf_release(&buf
);
1373 sigchain_pop(SIGPIPE
);
1375 x
= finish_command(&proc
);
1382 int transport_push(struct repository
*r
,
1383 struct transport
*transport
,
1384 struct refspec
*rs
, int flags
,
1385 unsigned int *reject_reasons
)
1387 struct ref
*remote_refs
= NULL
;
1388 struct ref
*local_refs
= NULL
;
1389 int match_flags
= MATCH_REFS_NONE
;
1390 int verbose
= (transport
->verbose
> 0);
1391 int quiet
= (transport
->verbose
< 0);
1392 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1393 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1396 struct transport_ls_refs_options transport_options
=
1397 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1399 *reject_reasons
= 0;
1401 if (transport_color_config() < 0)
1404 if (!transport
->vtable
->push_refs
)
1407 local_refs
= get_local_heads();
1409 if (check_push_refs(local_refs
, rs
) < 0)
1412 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1414 trace2_region_enter("transport_push", "get_refs_list", r
);
1415 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1416 &transport_options
);
1417 trace2_region_leave("transport_push", "get_refs_list", r
);
1419 transport_ls_refs_options_release(&transport_options
);
1421 if (flags
& TRANSPORT_PUSH_ALL
)
1422 match_flags
|= MATCH_REFS_ALL
;
1423 if (flags
& TRANSPORT_PUSH_MIRROR
)
1424 match_flags
|= MATCH_REFS_MIRROR
;
1425 if (flags
& TRANSPORT_PUSH_PRUNE
)
1426 match_flags
|= MATCH_REFS_PRUNE
;
1427 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1428 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1430 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1433 if (transport
->smart_options
&&
1434 transport
->smart_options
->cas
&&
1435 !is_empty_cas(transport
->smart_options
->cas
))
1436 apply_push_cas(transport
->smart_options
->cas
,
1437 transport
->remote
, remote_refs
);
1439 set_ref_status_for_push(remote_refs
,
1440 flags
& TRANSPORT_PUSH_MIRROR
,
1441 flags
& TRANSPORT_PUSH_FORCE
);
1443 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1444 if (run_pre_push_hook(transport
, remote_refs
))
1447 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1448 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1449 !is_bare_repository()) {
1450 struct ref
*ref
= remote_refs
;
1451 struct oid_array commits
= OID_ARRAY_INIT
;
1453 trace2_region_enter("transport_push", "push_submodules", r
);
1454 for (; ref
; ref
= ref
->next
)
1455 if (!is_null_oid(&ref
->new_oid
))
1456 oid_array_append(&commits
,
1459 if (!push_unpushed_submodules(r
,
1463 transport
->push_options
,
1465 oid_array_clear(&commits
);
1466 trace2_region_leave("transport_push", "push_submodules", r
);
1467 die(_("failed to push all needed submodules"));
1469 oid_array_clear(&commits
);
1470 trace2_region_leave("transport_push", "push_submodules", r
);
1473 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1474 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1475 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1476 !pretend
)) && !is_bare_repository()) {
1477 struct ref
*ref
= remote_refs
;
1478 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1479 struct oid_array commits
= OID_ARRAY_INIT
;
1481 trace2_region_enter("transport_push", "check_submodules", r
);
1482 for (; ref
; ref
= ref
->next
)
1483 if (!is_null_oid(&ref
->new_oid
))
1484 oid_array_append(&commits
,
1487 if (find_unpushed_submodules(r
,
1489 transport
->remote
->name
,
1491 oid_array_clear(&commits
);
1492 trace2_region_leave("transport_push", "check_submodules", r
);
1493 die_with_unpushed_submodules(&needs_pushing
);
1495 string_list_clear(&needs_pushing
, 0);
1496 oid_array_clear(&commits
);
1497 trace2_region_leave("transport_push", "check_submodules", r
);
1500 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1501 trace2_region_enter("transport_push", "push_refs", r
);
1502 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1503 trace2_region_leave("transport_push", "push_refs", r
);
1506 err
= push_had_errors(remote_refs
);
1507 ret
= push_ret
| err
;
1510 transport_print_push_status(transport
->url
, remote_refs
,
1511 verbose
| porcelain
, porcelain
,
1514 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1515 set_upstreams(transport
, remote_refs
, pretend
);
1517 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1518 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1520 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1521 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1524 if (porcelain
&& !push_ret
)
1526 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1527 /* stable plumbing output; do not modify or localize */
1528 fprintf(stderr
, "Everything up-to-date\n");
1531 free_refs(local_refs
);
1532 free_refs(remote_refs
);
1536 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1537 struct transport_ls_refs_options
*transport_options
)
1539 if (!transport
->got_remote_refs
) {
1540 transport
->remote_refs
=
1541 transport
->vtable
->get_refs_list(transport
, 0,
1543 transport
->got_remote_refs
= 1;
1546 return transport
->remote_refs
;
1549 void transport_ls_refs_options_release(struct transport_ls_refs_options
*opts
)
1551 strvec_clear(&opts
->ref_prefixes
);
1552 free((char *)opts
->unborn_head_target
);
1555 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1558 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1559 struct ref
**heads
= NULL
;
1562 for (rm
= refs
; rm
; rm
= rm
->next
) {
1565 !is_null_oid(&rm
->old_oid
) &&
1566 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1568 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1569 heads
[nr_heads
++] = rm
;
1574 * When deepening of a shallow repository is requested,
1575 * then local and remote refs are likely to still be equal.
1576 * Just feed them all to the fetch method in that case.
1577 * This condition shouldn't be met in a non-deepening fetch
1578 * (see builtin/fetch.c:quickfetch()).
1580 ALLOC_ARRAY(heads
, nr_refs
);
1581 for (rm
= refs
; rm
; rm
= rm
->next
)
1582 heads
[nr_heads
++] = rm
;
1585 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1591 int transport_get_remote_bundle_uri(struct transport
*transport
)
1594 const struct transport_vtable
*vtable
= transport
->vtable
;
1596 /* Check config only once. */
1597 if (transport
->got_remote_bundle_uri
)
1599 transport
->got_remote_bundle_uri
= 1;
1602 * Don't request bundle-uri from the server unless configured to
1603 * do so by the transfer.bundleURI=true config option.
1605 if (git_config_get_bool("transfer.bundleuri", &value
) || !value
)
1608 if (!transport
->bundles
->baseURI
)
1609 transport
->bundles
->baseURI
= xstrdup(transport
->url
);
1611 if (!vtable
->get_bundle_uri
)
1612 return error(_("bundle-uri operation not supported by protocol"));
1614 if (vtable
->get_bundle_uri(transport
) < 0)
1615 return error(_("could not retrieve server-advertised bundle-uri list"));
1619 void transport_unlock_pack(struct transport
*transport
, unsigned int flags
)
1621 int in_signal_handler
= !!(flags
& TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER
);
1623 for (size_t i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1624 if (in_signal_handler
)
1625 unlink(transport
->pack_lockfiles
.items
[i
].string
);
1627 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1628 if (!in_signal_handler
)
1629 string_list_clear(&transport
->pack_lockfiles
, 0);
1632 int transport_connect(struct transport
*transport
, const char *name
,
1633 const char *exec
, int fd
[2])
1635 if (transport
->vtable
->connect
)
1636 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1638 die(_("operation not supported by protocol"));
1641 int transport_disconnect(struct transport
*transport
)
1644 if (transport
->vtable
->disconnect
)
1645 ret
= transport
->vtable
->disconnect(transport
);
1646 if (transport
->got_remote_refs
)
1647 free_refs((void *)transport
->remote_refs
);
1648 clear_bundle_list(transport
->bundles
);
1649 free(transport
->bundles
);
1655 * Strip username (and password) from a URL and return
1656 * it in a newly allocated string.
1658 char *transport_anonymize_url(const char *url
)
1660 char *scheme_prefix
, *anon_part
;
1661 size_t anon_len
, prefix_len
= 0;
1663 anon_part
= strchr(url
, '@');
1664 if (url_is_local_not_ssh(url
) || !anon_part
)
1667 anon_len
= strlen(++anon_part
);
1668 scheme_prefix
= strstr(url
, "://");
1669 if (!scheme_prefix
) {
1670 if (!strchr(anon_part
, ':'))
1671 /* cannot be "me@there:/path/name" */
1675 /* make sure scheme is reasonable */
1676 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1679 case '+': case '.': case '-':
1688 /* @ past the first slash does not count */
1689 cp
= strchr(scheme_prefix
+ 3, '/');
1690 if (cp
&& cp
< anon_part
)
1692 prefix_len
= scheme_prefix
- url
+ 3;
1694 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1695 (int)anon_len
, anon_part
);
1697 return xstrdup(url
);