6 #include "fetch-pack.h"
17 #include "submodule.h"
18 #include "string-list.h"
19 #include "oid-array.h"
21 #include "transport-internal.h"
23 #include "object-store.h"
26 static int transport_use_color
= -1;
27 static char transport_colors
[][COLOR_MAXLEN
] = {
29 GIT_COLOR_RED
/* REJECTED */
32 enum color_transport
{
33 TRANSPORT_COLOR_RESET
= 0,
34 TRANSPORT_COLOR_REJECTED
= 1
37 static int transport_color_config(void)
39 const char *keys
[] = {
40 "color.transport.reset",
41 "color.transport.rejected"
42 }, *key
= "color.transport";
45 static int initialized
;
51 if (!git_config_get_string(key
, &value
))
52 transport_use_color
= git_config_colorbool(key
, value
);
54 if (!want_color_stderr(transport_use_color
))
57 for (i
= 0; i
< ARRAY_SIZE(keys
); i
++)
58 if (!git_config_get_string(keys
[i
], &value
)) {
60 return config_error_nonbool(keys
[i
]);
61 if (color_parse(value
, transport_colors
[i
]) < 0)
68 static const char *transport_get_color(enum color_transport ix
)
70 if (want_color_stderr(transport_use_color
))
71 return transport_colors
[ix
];
75 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
79 for (ref
= refs
; ref
; ref
= ref
->next
) {
80 const char *localname
;
82 const char *remotename
;
85 * Check suitability for tracking. Must be successful /
86 * already up-to-date ref create/modify (not delete).
88 if (ref
->status
!= REF_STATUS_OK
&&
89 ref
->status
!= REF_STATUS_UPTODATE
)
93 if (is_null_oid(&ref
->new_oid
))
96 /* Follow symbolic refs (mainly for HEAD). */
97 localname
= ref
->peer_ref
->name
;
98 remotename
= ref
->name
;
99 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
101 if (tmp
&& flag
& REF_ISSYMREF
&&
102 starts_with(tmp
, "refs/heads/"))
105 /* Both source and destination must be local branches. */
106 if (!localname
|| !starts_with(localname
, "refs/heads/"))
108 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
112 int flag
= transport
->verbose
< 0 ? 0 : BRANCH_CONFIG_VERBOSE
;
113 install_branch_config(flag
, localname
+ 11,
114 transport
->remote
->name
, remotename
);
115 } else if (transport
->verbose
>= 0)
116 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
117 localname
+ 11, remotename
+ 11,
118 transport
->remote
->name
);
122 struct bundle_transport_data
{
124 struct bundle_header header
;
125 unsigned get_refs_from_bundle_called
: 1;
128 static void get_refs_from_bundle_inner(struct transport
*transport
)
130 struct bundle_transport_data
*data
= transport
->data
;
132 data
->get_refs_from_bundle_called
= 1;
136 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
138 die(_("could not read bundle '%s'"), transport
->url
);
140 transport
->hash_algo
= data
->header
.hash_algo
;
143 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
145 struct transport_ls_refs_options
*transport_options
)
147 struct bundle_transport_data
*data
= transport
->data
;
148 struct ref
*result
= NULL
;
154 get_refs_from_bundle_inner(transport
);
156 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
157 struct string_list_item
*e
= data
->header
.references
.items
+ i
;
158 const char *name
= e
->string
;
159 struct ref
*ref
= alloc_ref(name
);
160 struct object_id
*oid
= e
->util
;
161 oidcpy(&ref
->old_oid
, oid
);
168 static int fetch_refs_from_bundle(struct transport
*transport
,
169 int nr_heads
, struct ref
**to_fetch
)
171 struct bundle_transport_data
*data
= transport
->data
;
172 struct strvec extra_index_pack_args
= STRVEC_INIT
;
175 if (transport
->progress
)
176 strvec_push(&extra_index_pack_args
, "-v");
178 if (!data
->get_refs_from_bundle_called
)
179 get_refs_from_bundle_inner(transport
);
180 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
181 &extra_index_pack_args
);
182 transport
->hash_algo
= data
->header
.hash_algo
;
186 static int close_bundle(struct transport
*transport
)
188 struct bundle_transport_data
*data
= transport
->data
;
191 bundle_header_release(&data
->header
);
196 struct git_transport_data
{
197 struct git_transport_options options
;
198 struct child_process
*conn
;
200 unsigned got_remote_heads
: 1;
201 enum protocol_version version
;
202 struct oid_array extra_have
;
203 struct oid_array shallow
;
206 static int set_git_option(struct git_transport_options
*opts
,
207 const char *name
, const char *value
)
209 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
210 opts
->uploadpack
= value
;
212 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
213 opts
->receivepack
= value
;
215 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
216 opts
->thin
= !!value
;
218 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
219 opts
->followtags
= !!value
;
221 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
222 opts
->keep
= !!value
;
224 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
225 opts
->update_shallow
= !!value
;
227 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
232 opts
->depth
= strtol(value
, &end
, 0);
234 die(_("transport: invalid depth option '%s'"), value
);
237 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
238 opts
->deepen_since
= value
;
240 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
241 opts
->deepen_not
= (const struct string_list
*)value
;
243 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
244 opts
->deepen_relative
= !!value
;
246 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
247 opts
->from_promisor
= !!value
;
249 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
250 list_objects_filter_die_if_populated(&opts
->filter_options
);
251 parse_list_objects_filter(&opts
->filter_options
, value
);
253 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
254 opts
->reject_shallow
= !!value
;
260 static int connect_setup(struct transport
*transport
, int for_push
)
262 struct git_transport_data
*data
= transport
->data
;
263 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
268 switch (transport
->family
) {
269 case TRANSPORT_FAMILY_ALL
: break;
270 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
271 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
274 data
->conn
= git_connect(data
->fd
, transport
->url
,
275 for_push
? data
->options
.receivepack
:
276 data
->options
.uploadpack
,
282 static void die_if_server_options(struct transport
*transport
)
284 if (!transport
->server_options
|| !transport
->server_options
->nr
)
286 advise(_("see protocol.version in 'git help config' for more details"));
287 die(_("server options require protocol version 2 or later"));
291 * Obtains the protocol version from the transport and writes it to
292 * transport->data->version, first connecting if not already connected.
294 * If the protocol version is one that allows skipping the listing of remote
295 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
296 * this function returns NULL. Otherwise, this function returns the list of
299 static struct ref
*handshake(struct transport
*transport
, int for_push
,
300 struct transport_ls_refs_options
*options
,
303 struct git_transport_data
*data
= transport
->data
;
304 struct ref
*refs
= NULL
;
305 struct packet_reader reader
;
307 const char *server_sid
;
309 connect_setup(transport
, for_push
);
311 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
312 PACKET_READ_CHOMP_NEWLINE
|
313 PACKET_READ_GENTLE_ON_EOF
|
314 PACKET_READ_DIE_ON_ERR_PACKET
);
316 data
->version
= discover_version(&reader
);
317 switch (data
->version
) {
319 if (server_feature_v2("session-id", &server_sid
))
320 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
322 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
324 transport
->server_options
,
325 transport
->stateless_rpc
);
329 die_if_server_options(transport
);
330 get_remote_heads(&reader
, &refs
,
331 for_push
? REF_NORMAL
: 0,
334 server_sid
= server_feature_value("session-id", &sid_len
);
336 char *sid
= xstrndup(server_sid
, sid_len
);
337 trace2_data_string("transfer", NULL
, "server-sid", sid
);
341 case protocol_unknown_version
:
342 BUG("unknown protocol version");
344 data
->got_remote_heads
= 1;
345 transport
->hash_algo
= reader
.hash_algo
;
347 if (reader
.line_peeked
)
348 BUG("buffer must be empty at the end of handshake()");
353 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
354 struct transport_ls_refs_options
*options
)
356 return handshake(transport
, for_push
, options
, 1);
359 static int fetch_refs_via_pack(struct transport
*transport
,
360 int nr_heads
, struct ref
**to_fetch
)
363 struct git_transport_data
*data
= transport
->data
;
364 struct ref
*refs
= NULL
;
365 struct fetch_pack_args args
;
366 struct ref
*refs_tmp
= NULL
;
368 memset(&args
, 0, sizeof(args
));
369 args
.uploadpack
= data
->options
.uploadpack
;
370 args
.keep_pack
= data
->options
.keep
;
372 args
.use_thin_pack
= data
->options
.thin
;
373 args
.include_tag
= data
->options
.followtags
;
374 args
.verbose
= (transport
->verbose
> 1);
375 args
.quiet
= (transport
->verbose
< 0);
376 args
.no_progress
= !transport
->progress
;
377 args
.depth
= data
->options
.depth
;
378 args
.deepen_since
= data
->options
.deepen_since
;
379 args
.deepen_not
= data
->options
.deepen_not
;
380 args
.deepen_relative
= data
->options
.deepen_relative
;
381 args
.check_self_contained_and_connected
=
382 data
->options
.check_self_contained_and_connected
;
383 args
.cloning
= transport
->cloning
;
384 args
.update_shallow
= data
->options
.update_shallow
;
385 args
.from_promisor
= data
->options
.from_promisor
;
386 args
.filter_options
= data
->options
.filter_options
;
387 args
.stateless_rpc
= transport
->stateless_rpc
;
388 args
.server_options
= transport
->server_options
;
389 args
.negotiation_tips
= data
->options
.negotiation_tips
;
390 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
392 if (!data
->got_remote_heads
) {
394 int must_list_refs
= 0;
395 for (i
= 0; i
< nr_heads
; i
++) {
396 if (!to_fetch
[i
]->exact_oid
) {
401 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
404 if (data
->version
== protocol_unknown_version
)
405 BUG("unknown protocol version");
406 else if (data
->version
<= protocol_v1
)
407 die_if_server_options(transport
);
409 if (data
->options
.acked_commits
) {
410 if (data
->version
< protocol_v2
) {
411 warning(_("--negotiate-only requires protocol v2"));
413 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
414 warning(_("server does not support wait-for-done"));
417 negotiate_using_fetch(data
->options
.negotiation_tips
,
418 transport
->server_options
,
419 transport
->stateless_rpc
,
421 data
->options
.acked_commits
);
427 refs
= fetch_pack(&args
, data
->fd
,
428 refs_tmp
? refs_tmp
: transport
->remote_refs
,
429 to_fetch
, nr_heads
, &data
->shallow
,
430 &transport
->pack_lockfiles
, data
->version
);
432 data
->got_remote_heads
= 0;
433 data
->options
.self_contained_and_connected
=
434 args
.self_contained_and_connected
;
435 data
->options
.connectivity_checked
= args
.connectivity_checked
;
439 if (report_unmatched_refs(to_fetch
, nr_heads
))
444 if (data
->fd
[1] >= 0)
446 if (finish_connect(data
->conn
))
455 static int push_had_errors(struct ref
*ref
)
457 for (; ref
; ref
= ref
->next
) {
458 switch (ref
->status
) {
459 case REF_STATUS_NONE
:
460 case REF_STATUS_UPTODATE
:
470 int transport_refs_pushed(struct ref
*ref
)
472 for (; ref
; ref
= ref
->next
) {
473 switch(ref
->status
) {
474 case REF_STATUS_NONE
:
475 case REF_STATUS_UPTODATE
:
484 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
485 struct object_id
*new_oid
, int deletion
,
488 struct refspec_item rs
;
490 memset(&rs
, 0, sizeof(rs
));
494 if (!remote_find_tracking(remote
, &rs
)) {
496 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
498 delete_ref(NULL
, rs
.dst
, NULL
, 0);
500 update_ref("update by push", rs
.dst
, new_oid
,
506 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
509 struct object_id
*new_oid
;
510 struct ref_push_report
*report
;
512 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
515 report
= ref
->report
;
517 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
518 ref
->deletion
, verbose
);
520 for (; report
; report
= report
->next
) {
521 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
522 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
523 update_one_tracking_ref(remote
, refname
, new_oid
,
524 is_null_oid(new_oid
), verbose
);
528 static void print_ref_status(char flag
, const char *summary
,
529 struct ref
*to
, struct ref
*from
, const char *msg
,
530 struct ref_push_report
*report
,
531 int porcelain
, int summary_width
)
535 if (report
&& report
->ref_name
)
536 to_name
= report
->ref_name
;
542 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
544 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
546 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
548 fprintf(stdout
, "%s\n", summary
);
550 const char *red
= "", *reset
= "";
551 if (push_had_errors(to
)) {
552 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
553 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
555 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
558 fprintf(stderr
, "%s -> %s",
559 prettify_refname(from
->name
),
560 prettify_refname(to_name
));
562 fputs(prettify_refname(to_name
), stderr
);
572 static void print_ok_ref_status(struct ref
*ref
,
573 struct ref_push_report
*report
,
574 int porcelain
, int summary_width
)
576 struct object_id
*old_oid
;
577 struct object_id
*new_oid
;
578 const char *ref_name
;
581 if (report
&& report
->old_oid
)
582 old_oid
= report
->old_oid
;
584 old_oid
= &ref
->old_oid
;
585 if (report
&& report
->new_oid
)
586 new_oid
= report
->new_oid
;
588 new_oid
= &ref
->new_oid
;
589 if (report
&& report
->forced_update
)
590 forced_update
= report
->forced_update
;
592 forced_update
= ref
->forced_update
;
593 if (report
&& report
->ref_name
)
594 ref_name
= report
->ref_name
;
596 ref_name
= ref
->name
;
599 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
600 report
, porcelain
, summary_width
);
601 else if (is_null_oid(old_oid
))
602 print_ref_status('*',
603 (starts_with(ref_name
, "refs/tags/")
605 : (starts_with(ref_name
, "refs/heads/")
607 : "[new reference]")),
608 ref
, ref
->peer_ref
, NULL
,
609 report
, porcelain
, summary_width
);
611 struct strbuf quickref
= STRBUF_INIT
;
615 strbuf_add_unique_abbrev(&quickref
, old_oid
,
618 strbuf_addstr(&quickref
, "...");
620 msg
= "forced update";
622 strbuf_addstr(&quickref
, "..");
626 strbuf_add_unique_abbrev(&quickref
, new_oid
,
629 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
630 report
, porcelain
, summary_width
);
631 strbuf_release(&quickref
);
635 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
636 struct ref_push_report
*report
,
637 int porcelain
, int summary_width
)
640 char *url
= transport_anonymize_url(dest
);
641 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
645 switch(ref
->status
) {
646 case REF_STATUS_NONE
:
647 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
648 report
, porcelain
, summary_width
);
650 case REF_STATUS_REJECT_NODELETE
:
651 print_ref_status('!', "[rejected]", ref
, NULL
,
652 "remote does not support deleting refs",
653 report
, porcelain
, summary_width
);
655 case REF_STATUS_UPTODATE
:
656 print_ref_status('=', "[up to date]", ref
,
658 report
, porcelain
, summary_width
);
660 case REF_STATUS_REJECT_NONFASTFORWARD
:
661 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
663 report
, porcelain
, summary_width
);
665 case REF_STATUS_REJECT_ALREADY_EXISTS
:
666 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
668 report
, porcelain
, summary_width
);
670 case REF_STATUS_REJECT_FETCH_FIRST
:
671 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
673 report
, porcelain
, summary_width
);
675 case REF_STATUS_REJECT_NEEDS_FORCE
:
676 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
678 report
, porcelain
, summary_width
);
680 case REF_STATUS_REJECT_STALE
:
681 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
683 report
, porcelain
, summary_width
);
685 case REF_STATUS_REJECT_REMOTE_UPDATED
:
686 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
687 "remote ref updated since checkout",
688 report
, porcelain
, summary_width
);
690 case REF_STATUS_REJECT_SHALLOW
:
691 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
692 "new shallow roots not allowed",
693 report
, porcelain
, summary_width
);
695 case REF_STATUS_REMOTE_REJECT
:
696 print_ref_status('!', "[remote rejected]", ref
,
697 ref
->deletion
? NULL
: ref
->peer_ref
,
699 report
, porcelain
, summary_width
);
701 case REF_STATUS_EXPECTING_REPORT
:
702 print_ref_status('!', "[remote failure]", ref
,
703 ref
->deletion
? NULL
: ref
->peer_ref
,
704 "remote failed to report status",
705 report
, porcelain
, summary_width
);
707 case REF_STATUS_ATOMIC_PUSH_FAILED
:
708 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
709 "atomic push failed",
710 report
, porcelain
, summary_width
);
713 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
720 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
721 int porcelain
, int summary_width
)
723 struct ref_push_report
*report
;
727 return print_one_push_report(ref
, dest
, count
,
728 NULL
, porcelain
, summary_width
);
730 for (report
= ref
->report
; report
; report
= report
->next
)
731 print_one_push_report(ref
, dest
, count
+ n
++,
732 report
, porcelain
, summary_width
);
736 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
738 char hex
[GIT_MAX_HEXSZ
+ 1];
739 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
741 return (w
< sofar
) ? sofar
: w
;
744 int transport_summary_width(const struct ref
*refs
)
748 for (; refs
; refs
= refs
->next
) {
749 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
750 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
753 maxw
= FALLBACK_DEFAULT_ABBREV
;
754 return (2 * maxw
+ 3);
757 void transport_print_push_status(const char *dest
, struct ref
*refs
,
758 int verbose
, int porcelain
, unsigned int *reject_reasons
)
763 int summary_width
= transport_summary_width(refs
);
765 if (transport_color_config() < 0)
766 warning(_("could not parse transport.color.* config"));
768 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
771 for (ref
= refs
; ref
; ref
= ref
->next
)
772 if (ref
->status
== REF_STATUS_UPTODATE
)
773 n
+= print_one_push_status(ref
, dest
, n
,
774 porcelain
, summary_width
);
777 for (ref
= refs
; ref
; ref
= ref
->next
)
778 if (ref
->status
== REF_STATUS_OK
)
779 n
+= print_one_push_status(ref
, dest
, n
,
780 porcelain
, summary_width
);
783 for (ref
= refs
; ref
; ref
= ref
->next
) {
784 if (ref
->status
!= REF_STATUS_NONE
&&
785 ref
->status
!= REF_STATUS_UPTODATE
&&
786 ref
->status
!= REF_STATUS_OK
)
787 n
+= print_one_push_status(ref
, dest
, n
,
788 porcelain
, summary_width
);
789 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
790 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
791 *reject_reasons
|= REJECT_NON_FF_HEAD
;
793 *reject_reasons
|= REJECT_NON_FF_OTHER
;
794 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
795 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
796 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
797 *reject_reasons
|= REJECT_FETCH_FIRST
;
798 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
799 *reject_reasons
|= REJECT_NEEDS_FORCE
;
800 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
801 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
807 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
809 struct git_transport_data
*data
= transport
->data
;
810 struct send_pack_args args
;
813 if (transport_color_config() < 0)
816 if (!data
->got_remote_heads
)
817 get_refs_via_connect(transport
, 1, NULL
);
819 memset(&args
, 0, sizeof(args
));
820 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
821 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
822 args
.use_thin_pack
= data
->options
.thin
;
823 args
.verbose
= (transport
->verbose
> 0);
824 args
.quiet
= (transport
->verbose
< 0);
825 args
.progress
= transport
->progress
;
826 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
827 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
828 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
829 args
.push_options
= transport
->push_options
;
830 args
.url
= transport
->url
;
832 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
833 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
834 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
835 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
837 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
839 switch (data
->version
) {
841 die(_("support for protocol v2 not implemented yet"));
845 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
848 case protocol_unknown_version
:
849 BUG("unknown protocol version");
855 * Atomic push may abort the connection early and close the pipe,
856 * which may cause an error for `finish_connect()`. Ignore this error
857 * for atomic git-push.
859 if (ret
|| args
.atomic
)
860 finish_connect(data
->conn
);
862 ret
= finish_connect(data
->conn
);
864 data
->got_remote_heads
= 0;
869 static int connect_git(struct transport
*transport
, const char *name
,
870 const char *executable
, int fd
[2])
872 struct git_transport_data
*data
= transport
->data
;
873 data
->conn
= git_connect(data
->fd
, transport
->url
,
880 static int disconnect_git(struct transport
*transport
)
882 struct git_transport_data
*data
= transport
->data
;
884 if (data
->got_remote_heads
&& !transport
->stateless_rpc
)
885 packet_flush(data
->fd
[1]);
887 if (data
->fd
[1] >= 0)
889 finish_connect(data
->conn
);
896 static struct transport_vtable taken_over_vtable
= {
897 .get_refs_list
= get_refs_via_connect
,
898 .fetch_refs
= fetch_refs_via_pack
,
899 .push_refs
= git_transport_push
,
900 .disconnect
= disconnect_git
903 void transport_take_over(struct transport
*transport
,
904 struct child_process
*child
)
906 struct git_transport_data
*data
;
908 if (!transport
->smart_options
)
909 BUG("taking over transport requires non-NULL "
910 "smart_options field.");
912 CALLOC_ARRAY(data
, 1);
913 data
->options
= *transport
->smart_options
;
915 data
->fd
[0] = data
->conn
->out
;
916 data
->fd
[1] = data
->conn
->in
;
917 data
->got_remote_heads
= 0;
918 transport
->data
= data
;
920 transport
->vtable
= &taken_over_vtable
;
921 transport
->smart_options
= &(data
->options
);
923 transport
->cannot_reuse
= 1;
926 static int is_file(const char *url
)
931 return S_ISREG(buf
.st_mode
);
934 static int external_specification_len(const char *url
)
936 return strchr(url
, ':') - url
;
939 static const struct string_list
*protocol_whitelist(void)
941 static int enabled
= -1;
942 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
945 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
947 string_list_split(&allowed
, v
, ':', -1);
948 string_list_sort(&allowed
);
955 return enabled
? &allowed
: NULL
;
958 enum protocol_allow_config
{
959 PROTOCOL_ALLOW_NEVER
= 0,
960 PROTOCOL_ALLOW_USER_ONLY
,
961 PROTOCOL_ALLOW_ALWAYS
964 static enum protocol_allow_config
parse_protocol_config(const char *key
,
967 if (!strcasecmp(value
, "always"))
968 return PROTOCOL_ALLOW_ALWAYS
;
969 else if (!strcasecmp(value
, "never"))
970 return PROTOCOL_ALLOW_NEVER
;
971 else if (!strcasecmp(value
, "user"))
972 return PROTOCOL_ALLOW_USER_ONLY
;
974 die(_("unknown value for config '%s': %s"), key
, value
);
977 static enum protocol_allow_config
get_protocol_config(const char *type
)
979 char *key
= xstrfmt("protocol.%s.allow", type
);
982 /* first check the per-protocol config */
983 if (!git_config_get_string(key
, &value
)) {
984 enum protocol_allow_config ret
=
985 parse_protocol_config(key
, value
);
992 /* if defined, fallback to user-defined default for unknown protocols */
993 if (!git_config_get_string("protocol.allow", &value
)) {
994 enum protocol_allow_config ret
=
995 parse_protocol_config("protocol.allow", value
);
1000 /* fallback to built-in defaults */
1002 if (!strcmp(type
, "http") ||
1003 !strcmp(type
, "https") ||
1004 !strcmp(type
, "git") ||
1005 !strcmp(type
, "ssh") ||
1006 !strcmp(type
, "file"))
1007 return PROTOCOL_ALLOW_ALWAYS
;
1009 /* known scary; err on the side of caution */
1010 if (!strcmp(type
, "ext"))
1011 return PROTOCOL_ALLOW_NEVER
;
1013 /* unknown; by default let them be used only directly by the user */
1014 return PROTOCOL_ALLOW_USER_ONLY
;
1017 int is_transport_allowed(const char *type
, int from_user
)
1019 const struct string_list
*whitelist
= protocol_whitelist();
1021 return string_list_has_string(whitelist
, type
);
1023 switch (get_protocol_config(type
)) {
1024 case PROTOCOL_ALLOW_ALWAYS
:
1026 case PROTOCOL_ALLOW_NEVER
:
1028 case PROTOCOL_ALLOW_USER_ONLY
:
1030 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1034 BUG("invalid protocol_allow_config type");
1037 void transport_check_allowed(const char *type
)
1039 if (!is_transport_allowed(type
, -1))
1040 die(_("transport '%s' not allowed"), type
);
1043 static struct transport_vtable bundle_vtable
= {
1044 .get_refs_list
= get_refs_from_bundle
,
1045 .fetch_refs
= fetch_refs_from_bundle
,
1046 .disconnect
= close_bundle
1049 static struct transport_vtable builtin_smart_vtable
= {
1050 .get_refs_list
= get_refs_via_connect
,
1051 .fetch_refs
= fetch_refs_via_pack
,
1052 .push_refs
= git_transport_push
,
1053 .connect
= connect_git
,
1054 .disconnect
= disconnect_git
1057 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1060 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1062 ret
->progress
= isatty(2);
1063 string_list_init_dup(&ret
->pack_lockfiles
);
1066 BUG("No remote provided to transport_get()");
1068 ret
->got_remote_refs
= 0;
1069 ret
->remote
= remote
;
1070 helper
= remote
->foreign_vcs
;
1072 if (!url
&& remote
->url
)
1073 url
= remote
->url
[0];
1076 /* maybe it is a foreign URL? */
1078 const char *p
= url
;
1080 while (is_urlschemechar(p
== url
, *p
))
1082 if (starts_with(p
, "::"))
1083 helper
= xstrndup(url
, p
- url
);
1087 transport_helper_init(ret
, helper
);
1088 } else if (starts_with(url
, "rsync:")) {
1089 die(_("git-over-rsync is no longer supported"));
1090 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1091 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1092 bundle_header_init(&data
->header
);
1093 transport_check_allowed("file");
1095 ret
->vtable
= &bundle_vtable
;
1096 ret
->smart_options
= NULL
;
1097 } else if (!is_url(url
)
1098 || starts_with(url
, "file://")
1099 || starts_with(url
, "git://")
1100 || starts_with(url
, "ssh://")
1101 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1102 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1105 * These are builtin smart transports; "allowed" transports
1106 * will be checked individually in git_connect.
1108 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1110 ret
->vtable
= &builtin_smart_vtable
;
1111 ret
->smart_options
= &(data
->options
);
1114 data
->got_remote_heads
= 0;
1116 /* Unknown protocol in URL. Pass to external handler. */
1117 int len
= external_specification_len(url
);
1118 char *handler
= xmemdupz(url
, len
);
1119 transport_helper_init(ret
, handler
);
1122 if (ret
->smart_options
) {
1123 ret
->smart_options
->thin
= 1;
1124 ret
->smart_options
->uploadpack
= "git-upload-pack";
1125 if (remote
->uploadpack
)
1126 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1127 ret
->smart_options
->receivepack
= "git-receive-pack";
1128 if (remote
->receivepack
)
1129 ret
->smart_options
->receivepack
= remote
->receivepack
;
1132 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1137 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1139 return transport
->hash_algo
;
1142 int transport_set_option(struct transport
*transport
,
1143 const char *name
, const char *value
)
1145 int git_reports
= 1, protocol_reports
= 1;
1147 if (transport
->smart_options
)
1148 git_reports
= set_git_option(transport
->smart_options
,
1151 if (transport
->vtable
->set_option
)
1152 protocol_reports
= transport
->vtable
->set_option(transport
,
1155 /* If either report is 0, report 0 (success). */
1156 if (!git_reports
|| !protocol_reports
)
1158 /* If either reports -1 (invalid value), report -1. */
1159 if ((git_reports
== -1) || (protocol_reports
== -1))
1161 /* Otherwise if both report unknown, report unknown. */
1165 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1169 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1171 transport
->verbose
= -1;
1174 * Rules used to determine whether to report progress (processing aborts
1175 * when a rule is satisfied):
1177 * . Report progress, if force_progress is 1 (ie. --progress).
1178 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1179 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1180 * . Report progress if isatty(2) is 1.
1182 if (force_progress
>= 0)
1183 transport
->progress
= !!force_progress
;
1185 transport
->progress
= verbosity
>= 0 && isatty(2);
1188 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1192 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1193 "not be found on any remote:\n"));
1194 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1195 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1196 fprintf(stderr
, _("\nPlease try\n\n"
1197 " git push --recurse-submodules=on-demand\n\n"
1198 "or cd to the path and use\n\n"
1200 "to push them to a remote.\n\n"));
1202 string_list_clear(needs_pushing
, 0);
1204 die(_("Aborting."));
1207 static int run_pre_push_hook(struct transport
*transport
,
1208 struct ref
*remote_refs
)
1212 struct child_process proc
= CHILD_PROCESS_INIT
;
1214 const char *hook_path
= find_hook("pre-push");
1219 strvec_push(&proc
.args
, hook_path
);
1220 strvec_push(&proc
.args
, transport
->remote
->name
);
1221 strvec_push(&proc
.args
, transport
->url
);
1224 proc
.trace2_hook_name
= "pre-push";
1226 if (start_command(&proc
)) {
1227 finish_command(&proc
);
1231 sigchain_push(SIGPIPE
, SIG_IGN
);
1233 strbuf_init(&buf
, 256);
1235 for (r
= remote_refs
; r
; r
= r
->next
) {
1236 if (!r
->peer_ref
) continue;
1237 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1238 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1239 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1240 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1243 strbuf_addf( &buf
, "%s %s %s %s\n",
1244 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1245 r
->name
, oid_to_hex(&r
->old_oid
));
1247 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1248 /* We do not mind if a hook does not read all refs. */
1255 strbuf_release(&buf
);
1261 sigchain_pop(SIGPIPE
);
1263 x
= finish_command(&proc
);
1270 int transport_push(struct repository
*r
,
1271 struct transport
*transport
,
1272 struct refspec
*rs
, int flags
,
1273 unsigned int *reject_reasons
)
1275 *reject_reasons
= 0;
1277 if (transport_color_config() < 0)
1280 if (transport
->vtable
->push_refs
) {
1281 struct ref
*remote_refs
;
1282 struct ref
*local_refs
= get_local_heads();
1283 int match_flags
= MATCH_REFS_NONE
;
1284 int verbose
= (transport
->verbose
> 0);
1285 int quiet
= (transport
->verbose
< 0);
1286 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1287 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1288 int push_ret
, ret
, err
;
1289 struct transport_ls_refs_options transport_options
=
1290 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1292 if (check_push_refs(local_refs
, rs
) < 0)
1295 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1297 trace2_region_enter("transport_push", "get_refs_list", r
);
1298 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1299 &transport_options
);
1300 trace2_region_leave("transport_push", "get_refs_list", r
);
1302 transport_ls_refs_options_release(&transport_options
);
1304 if (flags
& TRANSPORT_PUSH_ALL
)
1305 match_flags
|= MATCH_REFS_ALL
;
1306 if (flags
& TRANSPORT_PUSH_MIRROR
)
1307 match_flags
|= MATCH_REFS_MIRROR
;
1308 if (flags
& TRANSPORT_PUSH_PRUNE
)
1309 match_flags
|= MATCH_REFS_PRUNE
;
1310 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1311 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1313 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1316 if (transport
->smart_options
&&
1317 transport
->smart_options
->cas
&&
1318 !is_empty_cas(transport
->smart_options
->cas
))
1319 apply_push_cas(transport
->smart_options
->cas
,
1320 transport
->remote
, remote_refs
);
1322 set_ref_status_for_push(remote_refs
,
1323 flags
& TRANSPORT_PUSH_MIRROR
,
1324 flags
& TRANSPORT_PUSH_FORCE
);
1326 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1327 if (run_pre_push_hook(transport
, remote_refs
))
1330 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1331 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1332 !is_bare_repository()) {
1333 struct ref
*ref
= remote_refs
;
1334 struct oid_array commits
= OID_ARRAY_INIT
;
1336 trace2_region_enter("transport_push", "push_submodules", r
);
1337 for (; ref
; ref
= ref
->next
)
1338 if (!is_null_oid(&ref
->new_oid
))
1339 oid_array_append(&commits
,
1342 if (!push_unpushed_submodules(r
,
1346 transport
->push_options
,
1348 oid_array_clear(&commits
);
1349 trace2_region_leave("transport_push", "push_submodules", r
);
1350 die(_("failed to push all needed submodules"));
1352 oid_array_clear(&commits
);
1353 trace2_region_leave("transport_push", "push_submodules", r
);
1356 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1357 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1358 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1359 !pretend
)) && !is_bare_repository()) {
1360 struct ref
*ref
= remote_refs
;
1361 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1362 struct oid_array commits
= OID_ARRAY_INIT
;
1364 trace2_region_enter("transport_push", "check_submodules", r
);
1365 for (; ref
; ref
= ref
->next
)
1366 if (!is_null_oid(&ref
->new_oid
))
1367 oid_array_append(&commits
,
1370 if (find_unpushed_submodules(r
,
1372 transport
->remote
->name
,
1374 oid_array_clear(&commits
);
1375 trace2_region_leave("transport_push", "check_submodules", r
);
1376 die_with_unpushed_submodules(&needs_pushing
);
1378 string_list_clear(&needs_pushing
, 0);
1379 oid_array_clear(&commits
);
1380 trace2_region_leave("transport_push", "check_submodules", r
);
1383 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1384 trace2_region_enter("transport_push", "push_refs", r
);
1385 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1386 trace2_region_leave("transport_push", "push_refs", r
);
1389 err
= push_had_errors(remote_refs
);
1390 ret
= push_ret
| err
;
1393 transport_print_push_status(transport
->url
, remote_refs
,
1394 verbose
| porcelain
, porcelain
,
1397 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1398 set_upstreams(transport
, remote_refs
, pretend
);
1400 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1401 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1403 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1404 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1407 if (porcelain
&& !push_ret
)
1409 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1410 fprintf(stderr
, "Everything up-to-date\n");
1417 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1418 struct transport_ls_refs_options
*transport_options
)
1420 if (!transport
->got_remote_refs
) {
1421 transport
->remote_refs
=
1422 transport
->vtable
->get_refs_list(transport
, 0,
1424 transport
->got_remote_refs
= 1;
1427 return transport
->remote_refs
;
1430 void transport_ls_refs_options_release(struct transport_ls_refs_options
*opts
)
1432 strvec_clear(&opts
->ref_prefixes
);
1433 free((char *)opts
->unborn_head_target
);
1436 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1439 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1440 struct ref
**heads
= NULL
;
1443 for (rm
= refs
; rm
; rm
= rm
->next
) {
1446 !is_null_oid(&rm
->old_oid
) &&
1447 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1449 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1450 heads
[nr_heads
++] = rm
;
1455 * When deepening of a shallow repository is requested,
1456 * then local and remote refs are likely to still be equal.
1457 * Just feed them all to the fetch method in that case.
1458 * This condition shouldn't be met in a non-deepening fetch
1459 * (see builtin/fetch.c:quickfetch()).
1461 ALLOC_ARRAY(heads
, nr_refs
);
1462 for (rm
= refs
; rm
; rm
= rm
->next
)
1463 heads
[nr_heads
++] = rm
;
1466 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1472 void transport_unlock_pack(struct transport
*transport
, unsigned int flags
)
1474 int in_signal_handler
= !!(flags
& TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER
);
1477 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1478 if (in_signal_handler
)
1479 unlink(transport
->pack_lockfiles
.items
[i
].string
);
1481 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1482 if (!in_signal_handler
)
1483 string_list_clear(&transport
->pack_lockfiles
, 0);
1486 int transport_connect(struct transport
*transport
, const char *name
,
1487 const char *exec
, int fd
[2])
1489 if (transport
->vtable
->connect
)
1490 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1492 die(_("operation not supported by protocol"));
1495 int transport_disconnect(struct transport
*transport
)
1498 if (transport
->vtable
->disconnect
)
1499 ret
= transport
->vtable
->disconnect(transport
);
1500 if (transport
->got_remote_refs
)
1501 free_refs((void *)transport
->remote_refs
);
1507 * Strip username (and password) from a URL and return
1508 * it in a newly allocated string.
1510 char *transport_anonymize_url(const char *url
)
1512 char *scheme_prefix
, *anon_part
;
1513 size_t anon_len
, prefix_len
= 0;
1515 anon_part
= strchr(url
, '@');
1516 if (url_is_local_not_ssh(url
) || !anon_part
)
1519 anon_len
= strlen(++anon_part
);
1520 scheme_prefix
= strstr(url
, "://");
1521 if (!scheme_prefix
) {
1522 if (!strchr(anon_part
, ':'))
1523 /* cannot be "me@there:/path/name" */
1527 /* make sure scheme is reasonable */
1528 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1531 case '+': case '.': case '-':
1540 /* @ past the first slash does not count */
1541 cp
= strchr(scheme_prefix
+ 3, '/');
1542 if (cp
&& cp
< anon_part
)
1544 prefix_len
= scheme_prefix
- url
+ 3;
1546 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1547 (int)anon_len
, anon_part
);
1549 return xstrdup(url
);