4 #include "run-command.h"
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 install_branch_config(BRANCH_CONFIG_VERBOSE
,
113 localname
+ 11, transport
->remote
->name
,
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 struct ref
*get_refs_from_bundle(struct transport
*transport
,
130 struct transport_ls_refs_options
*transport_options
)
132 struct bundle_transport_data
*data
= transport
->data
;
133 struct ref
*result
= NULL
;
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
;
149 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
150 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
151 struct ref
*ref
= alloc_ref(e
->name
);
152 oidcpy(&ref
->old_oid
, &e
->oid
);
159 static int fetch_refs_from_bundle(struct transport
*transport
,
160 int nr_heads
, struct ref
**to_fetch
)
162 struct bundle_transport_data
*data
= transport
->data
;
165 if (!data
->get_refs_from_bundle_called
)
166 get_refs_from_bundle(transport
, 0, NULL
);
167 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
168 transport
->progress
? BUNDLE_VERBOSE
: 0);
169 transport
->hash_algo
= data
->header
.hash_algo
;
173 static int close_bundle(struct transport
*transport
)
175 struct bundle_transport_data
*data
= transport
->data
;
182 struct git_transport_data
{
183 struct git_transport_options options
;
184 struct child_process
*conn
;
186 unsigned got_remote_heads
: 1;
187 enum protocol_version version
;
188 struct oid_array extra_have
;
189 struct oid_array shallow
;
192 static int set_git_option(struct git_transport_options
*opts
,
193 const char *name
, const char *value
)
195 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
196 opts
->uploadpack
= value
;
198 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
199 opts
->receivepack
= value
;
201 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
202 opts
->thin
= !!value
;
204 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
205 opts
->followtags
= !!value
;
207 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
208 opts
->keep
= !!value
;
210 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
211 opts
->update_shallow
= !!value
;
213 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
218 opts
->depth
= strtol(value
, &end
, 0);
220 die(_("transport: invalid depth option '%s'"), value
);
223 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
224 opts
->deepen_since
= value
;
226 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
227 opts
->deepen_not
= (const struct string_list
*)value
;
229 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
230 opts
->deepen_relative
= !!value
;
232 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
233 opts
->from_promisor
= !!value
;
235 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
236 list_objects_filter_die_if_populated(&opts
->filter_options
);
237 parse_list_objects_filter(&opts
->filter_options
, value
);
239 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
240 opts
->reject_shallow
= !!value
;
246 static int connect_setup(struct transport
*transport
, int for_push
)
248 struct git_transport_data
*data
= transport
->data
;
249 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
254 switch (transport
->family
) {
255 case TRANSPORT_FAMILY_ALL
: break;
256 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
257 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
260 data
->conn
= git_connect(data
->fd
, transport
->url
,
261 for_push
? data
->options
.receivepack
:
262 data
->options
.uploadpack
,
268 static void die_if_server_options(struct transport
*transport
)
270 if (!transport
->server_options
|| !transport
->server_options
->nr
)
272 advise(_("see protocol.version in 'git help config' for more details"));
273 die(_("server options require protocol version 2 or later"));
277 * Obtains the protocol version from the transport and writes it to
278 * transport->data->version, first connecting if not already connected.
280 * If the protocol version is one that allows skipping the listing of remote
281 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
282 * this function returns NULL. Otherwise, this function returns the list of
285 static struct ref
*handshake(struct transport
*transport
, int for_push
,
286 struct transport_ls_refs_options
*options
,
289 struct git_transport_data
*data
= transport
->data
;
290 struct ref
*refs
= NULL
;
291 struct packet_reader reader
;
293 const char *server_sid
;
295 connect_setup(transport
, for_push
);
297 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
298 PACKET_READ_CHOMP_NEWLINE
|
299 PACKET_READ_GENTLE_ON_EOF
|
300 PACKET_READ_DIE_ON_ERR_PACKET
);
302 data
->version
= discover_version(&reader
);
303 switch (data
->version
) {
305 if (server_feature_v2("session-id", &server_sid
))
306 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
308 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
310 transport
->server_options
,
311 transport
->stateless_rpc
);
315 die_if_server_options(transport
);
316 get_remote_heads(&reader
, &refs
,
317 for_push
? REF_NORMAL
: 0,
320 server_sid
= server_feature_value("session-id", &sid_len
);
322 char *sid
= xstrndup(server_sid
, sid_len
);
323 trace2_data_string("transfer", NULL
, "server-sid", sid
);
327 case protocol_unknown_version
:
328 BUG("unknown protocol version");
330 data
->got_remote_heads
= 1;
331 transport
->hash_algo
= reader
.hash_algo
;
333 if (reader
.line_peeked
)
334 BUG("buffer must be empty at the end of handshake()");
339 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
340 struct transport_ls_refs_options
*options
)
342 return handshake(transport
, for_push
, options
, 1);
345 static int fetch_refs_via_pack(struct transport
*transport
,
346 int nr_heads
, struct ref
**to_fetch
)
349 struct git_transport_data
*data
= transport
->data
;
350 struct ref
*refs
= NULL
;
351 struct fetch_pack_args args
;
352 struct ref
*refs_tmp
= NULL
;
354 memset(&args
, 0, sizeof(args
));
355 args
.uploadpack
= data
->options
.uploadpack
;
356 args
.keep_pack
= data
->options
.keep
;
358 args
.use_thin_pack
= data
->options
.thin
;
359 args
.include_tag
= data
->options
.followtags
;
360 args
.verbose
= (transport
->verbose
> 1);
361 args
.quiet
= (transport
->verbose
< 0);
362 args
.no_progress
= !transport
->progress
;
363 args
.depth
= data
->options
.depth
;
364 args
.deepen_since
= data
->options
.deepen_since
;
365 args
.deepen_not
= data
->options
.deepen_not
;
366 args
.deepen_relative
= data
->options
.deepen_relative
;
367 args
.check_self_contained_and_connected
=
368 data
->options
.check_self_contained_and_connected
;
369 args
.cloning
= transport
->cloning
;
370 args
.update_shallow
= data
->options
.update_shallow
;
371 args
.from_promisor
= data
->options
.from_promisor
;
372 args
.filter_options
= data
->options
.filter_options
;
373 args
.stateless_rpc
= transport
->stateless_rpc
;
374 args
.server_options
= transport
->server_options
;
375 args
.negotiation_tips
= data
->options
.negotiation_tips
;
376 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
378 if (!data
->got_remote_heads
) {
380 int must_list_refs
= 0;
381 for (i
= 0; i
< nr_heads
; i
++) {
382 if (!to_fetch
[i
]->exact_oid
) {
387 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
390 if (data
->version
== protocol_unknown_version
)
391 BUG("unknown protocol version");
392 else if (data
->version
<= protocol_v1
)
393 die_if_server_options(transport
);
395 refs
= fetch_pack(&args
, data
->fd
,
396 refs_tmp
? refs_tmp
: transport
->remote_refs
,
397 to_fetch
, nr_heads
, &data
->shallow
,
398 &transport
->pack_lockfiles
, data
->version
);
402 if (finish_connect(data
->conn
))
405 data
->got_remote_heads
= 0;
406 data
->options
.self_contained_and_connected
=
407 args
.self_contained_and_connected
;
408 data
->options
.connectivity_checked
= args
.connectivity_checked
;
412 if (report_unmatched_refs(to_fetch
, nr_heads
))
420 static int push_had_errors(struct ref
*ref
)
422 for (; ref
; ref
= ref
->next
) {
423 switch (ref
->status
) {
424 case REF_STATUS_NONE
:
425 case REF_STATUS_UPTODATE
:
435 int transport_refs_pushed(struct ref
*ref
)
437 for (; ref
; ref
= ref
->next
) {
438 switch(ref
->status
) {
439 case REF_STATUS_NONE
:
440 case REF_STATUS_UPTODATE
:
449 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
450 struct object_id
*new_oid
, int deletion
,
453 struct refspec_item rs
;
455 memset(&rs
, 0, sizeof(rs
));
459 if (!remote_find_tracking(remote
, &rs
)) {
461 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
463 delete_ref(NULL
, rs
.dst
, NULL
, 0);
465 update_ref("update by push", rs
.dst
, new_oid
,
471 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
474 struct object_id
*new_oid
;
475 struct ref_push_report
*report
;
477 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
480 report
= ref
->report
;
482 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
483 ref
->deletion
, verbose
);
485 for (; report
; report
= report
->next
) {
486 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
487 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
488 update_one_tracking_ref(remote
, refname
, new_oid
,
489 is_null_oid(new_oid
), verbose
);
493 static void print_ref_status(char flag
, const char *summary
,
494 struct ref
*to
, struct ref
*from
, const char *msg
,
495 struct ref_push_report
*report
,
496 int porcelain
, int summary_width
)
500 if (report
&& report
->ref_name
)
501 to_name
= report
->ref_name
;
507 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
509 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
511 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
513 fprintf(stdout
, "%s\n", summary
);
515 const char *red
= "", *reset
= "";
516 if (push_had_errors(to
)) {
517 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
518 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
520 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
523 fprintf(stderr
, "%s -> %s",
524 prettify_refname(from
->name
),
525 prettify_refname(to_name
));
527 fputs(prettify_refname(to_name
), stderr
);
537 static void print_ok_ref_status(struct ref
*ref
,
538 struct ref_push_report
*report
,
539 int porcelain
, int summary_width
)
541 struct object_id
*old_oid
;
542 struct object_id
*new_oid
;
543 const char *ref_name
;
546 if (report
&& report
->old_oid
)
547 old_oid
= report
->old_oid
;
549 old_oid
= &ref
->old_oid
;
550 if (report
&& report
->new_oid
)
551 new_oid
= report
->new_oid
;
553 new_oid
= &ref
->new_oid
;
554 if (report
&& report
->forced_update
)
555 forced_update
= report
->forced_update
;
557 forced_update
= ref
->forced_update
;
558 if (report
&& report
->ref_name
)
559 ref_name
= report
->ref_name
;
561 ref_name
= ref
->name
;
564 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
565 report
, porcelain
, summary_width
);
566 else if (is_null_oid(old_oid
))
567 print_ref_status('*',
568 (starts_with(ref_name
, "refs/tags/")
570 : (starts_with(ref_name
, "refs/heads/")
572 : "[new reference]")),
573 ref
, ref
->peer_ref
, NULL
,
574 report
, porcelain
, summary_width
);
576 struct strbuf quickref
= STRBUF_INIT
;
580 strbuf_add_unique_abbrev(&quickref
, old_oid
,
583 strbuf_addstr(&quickref
, "...");
585 msg
= "forced update";
587 strbuf_addstr(&quickref
, "..");
591 strbuf_add_unique_abbrev(&quickref
, new_oid
,
594 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
595 report
, porcelain
, summary_width
);
596 strbuf_release(&quickref
);
600 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
601 struct ref_push_report
*report
,
602 int porcelain
, int summary_width
)
605 char *url
= transport_anonymize_url(dest
);
606 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
610 switch(ref
->status
) {
611 case REF_STATUS_NONE
:
612 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
613 report
, porcelain
, summary_width
);
615 case REF_STATUS_REJECT_NODELETE
:
616 print_ref_status('!', "[rejected]", ref
, NULL
,
617 "remote does not support deleting refs",
618 report
, porcelain
, summary_width
);
620 case REF_STATUS_UPTODATE
:
621 print_ref_status('=', "[up to date]", ref
,
623 report
, porcelain
, summary_width
);
625 case REF_STATUS_REJECT_NONFASTFORWARD
:
626 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
628 report
, porcelain
, summary_width
);
630 case REF_STATUS_REJECT_ALREADY_EXISTS
:
631 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
633 report
, porcelain
, summary_width
);
635 case REF_STATUS_REJECT_FETCH_FIRST
:
636 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
638 report
, porcelain
, summary_width
);
640 case REF_STATUS_REJECT_NEEDS_FORCE
:
641 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
643 report
, porcelain
, summary_width
);
645 case REF_STATUS_REJECT_STALE
:
646 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
648 report
, porcelain
, summary_width
);
650 case REF_STATUS_REJECT_REMOTE_UPDATED
:
651 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
652 "remote ref updated since checkout",
653 report
, porcelain
, summary_width
);
655 case REF_STATUS_REJECT_SHALLOW
:
656 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
657 "new shallow roots not allowed",
658 report
, porcelain
, summary_width
);
660 case REF_STATUS_REMOTE_REJECT
:
661 print_ref_status('!', "[remote rejected]", ref
,
662 ref
->deletion
? NULL
: ref
->peer_ref
,
664 report
, porcelain
, summary_width
);
666 case REF_STATUS_EXPECTING_REPORT
:
667 print_ref_status('!', "[remote failure]", ref
,
668 ref
->deletion
? NULL
: ref
->peer_ref
,
669 "remote failed to report status",
670 report
, porcelain
, summary_width
);
672 case REF_STATUS_ATOMIC_PUSH_FAILED
:
673 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
674 "atomic push failed",
675 report
, porcelain
, summary_width
);
678 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
685 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
686 int porcelain
, int summary_width
)
688 struct ref_push_report
*report
;
692 return print_one_push_report(ref
, dest
, count
,
693 NULL
, porcelain
, summary_width
);
695 for (report
= ref
->report
; report
; report
= report
->next
)
696 print_one_push_report(ref
, dest
, count
+ n
++,
697 report
, porcelain
, summary_width
);
701 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
703 char hex
[GIT_MAX_HEXSZ
+ 1];
704 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
706 return (w
< sofar
) ? sofar
: w
;
709 int transport_summary_width(const struct ref
*refs
)
713 for (; refs
; refs
= refs
->next
) {
714 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
715 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
718 maxw
= FALLBACK_DEFAULT_ABBREV
;
719 return (2 * maxw
+ 3);
722 void transport_print_push_status(const char *dest
, struct ref
*refs
,
723 int verbose
, int porcelain
, unsigned int *reject_reasons
)
728 int summary_width
= transport_summary_width(refs
);
730 if (transport_color_config() < 0)
731 warning(_("could not parse transport.color.* config"));
733 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
736 for (ref
= refs
; ref
; ref
= ref
->next
)
737 if (ref
->status
== REF_STATUS_UPTODATE
)
738 n
+= print_one_push_status(ref
, dest
, n
,
739 porcelain
, summary_width
);
742 for (ref
= refs
; ref
; ref
= ref
->next
)
743 if (ref
->status
== REF_STATUS_OK
)
744 n
+= print_one_push_status(ref
, dest
, n
,
745 porcelain
, summary_width
);
748 for (ref
= refs
; ref
; ref
= ref
->next
) {
749 if (ref
->status
!= REF_STATUS_NONE
&&
750 ref
->status
!= REF_STATUS_UPTODATE
&&
751 ref
->status
!= REF_STATUS_OK
)
752 n
+= print_one_push_status(ref
, dest
, n
,
753 porcelain
, summary_width
);
754 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
755 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
756 *reject_reasons
|= REJECT_NON_FF_HEAD
;
758 *reject_reasons
|= REJECT_NON_FF_OTHER
;
759 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
760 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
761 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
762 *reject_reasons
|= REJECT_FETCH_FIRST
;
763 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
764 *reject_reasons
|= REJECT_NEEDS_FORCE
;
765 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
766 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
772 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
774 struct git_transport_data
*data
= transport
->data
;
775 struct send_pack_args args
;
778 if (transport_color_config() < 0)
781 if (!data
->got_remote_heads
)
782 get_refs_via_connect(transport
, 1, NULL
);
784 memset(&args
, 0, sizeof(args
));
785 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
786 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
787 args
.use_thin_pack
= data
->options
.thin
;
788 args
.verbose
= (transport
->verbose
> 0);
789 args
.quiet
= (transport
->verbose
< 0);
790 args
.progress
= transport
->progress
;
791 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
792 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
793 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
794 args
.push_options
= transport
->push_options
;
795 args
.url
= transport
->url
;
797 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
798 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
799 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
800 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
802 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
804 switch (data
->version
) {
806 die(_("support for protocol v2 not implemented yet"));
810 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
813 case protocol_unknown_version
:
814 BUG("unknown protocol version");
820 * Atomic push may abort the connection early and close the pipe,
821 * which may cause an error for `finish_connect()`. Ignore this error
822 * for atomic git-push.
824 if (ret
|| args
.atomic
)
825 finish_connect(data
->conn
);
827 ret
= finish_connect(data
->conn
);
829 data
->got_remote_heads
= 0;
834 static int connect_git(struct transport
*transport
, const char *name
,
835 const char *executable
, int fd
[2])
837 struct git_transport_data
*data
= transport
->data
;
838 data
->conn
= git_connect(data
->fd
, transport
->url
,
845 static int disconnect_git(struct transport
*transport
)
847 struct git_transport_data
*data
= transport
->data
;
849 if (data
->got_remote_heads
&& !transport
->stateless_rpc
)
850 packet_flush(data
->fd
[1]);
853 finish_connect(data
->conn
);
860 static struct transport_vtable taken_over_vtable
= {
862 get_refs_via_connect
,
869 void transport_take_over(struct transport
*transport
,
870 struct child_process
*child
)
872 struct git_transport_data
*data
;
874 if (!transport
->smart_options
)
875 BUG("taking over transport requires non-NULL "
876 "smart_options field.");
878 CALLOC_ARRAY(data
, 1);
879 data
->options
= *transport
->smart_options
;
881 data
->fd
[0] = data
->conn
->out
;
882 data
->fd
[1] = data
->conn
->in
;
883 data
->got_remote_heads
= 0;
884 transport
->data
= data
;
886 transport
->vtable
= &taken_over_vtable
;
887 transport
->smart_options
= &(data
->options
);
889 transport
->cannot_reuse
= 1;
892 static int is_file(const char *url
)
897 return S_ISREG(buf
.st_mode
);
900 static int external_specification_len(const char *url
)
902 return strchr(url
, ':') - url
;
905 static const struct string_list
*protocol_whitelist(void)
907 static int enabled
= -1;
908 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
911 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
913 string_list_split(&allowed
, v
, ':', -1);
914 string_list_sort(&allowed
);
921 return enabled
? &allowed
: NULL
;
924 enum protocol_allow_config
{
925 PROTOCOL_ALLOW_NEVER
= 0,
926 PROTOCOL_ALLOW_USER_ONLY
,
927 PROTOCOL_ALLOW_ALWAYS
930 static enum protocol_allow_config
parse_protocol_config(const char *key
,
933 if (!strcasecmp(value
, "always"))
934 return PROTOCOL_ALLOW_ALWAYS
;
935 else if (!strcasecmp(value
, "never"))
936 return PROTOCOL_ALLOW_NEVER
;
937 else if (!strcasecmp(value
, "user"))
938 return PROTOCOL_ALLOW_USER_ONLY
;
940 die(_("unknown value for config '%s': %s"), key
, value
);
943 static enum protocol_allow_config
get_protocol_config(const char *type
)
945 char *key
= xstrfmt("protocol.%s.allow", type
);
948 /* first check the per-protocol config */
949 if (!git_config_get_string(key
, &value
)) {
950 enum protocol_allow_config ret
=
951 parse_protocol_config(key
, value
);
958 /* if defined, fallback to user-defined default for unknown protocols */
959 if (!git_config_get_string("protocol.allow", &value
)) {
960 enum protocol_allow_config ret
=
961 parse_protocol_config("protocol.allow", value
);
966 /* fallback to built-in defaults */
968 if (!strcmp(type
, "http") ||
969 !strcmp(type
, "https") ||
970 !strcmp(type
, "git") ||
971 !strcmp(type
, "ssh") ||
972 !strcmp(type
, "file"))
973 return PROTOCOL_ALLOW_ALWAYS
;
975 /* known scary; err on the side of caution */
976 if (!strcmp(type
, "ext"))
977 return PROTOCOL_ALLOW_NEVER
;
979 /* unknown; by default let them be used only directly by the user */
980 return PROTOCOL_ALLOW_USER_ONLY
;
983 int is_transport_allowed(const char *type
, int from_user
)
985 const struct string_list
*whitelist
= protocol_whitelist();
987 return string_list_has_string(whitelist
, type
);
989 switch (get_protocol_config(type
)) {
990 case PROTOCOL_ALLOW_ALWAYS
:
992 case PROTOCOL_ALLOW_NEVER
:
994 case PROTOCOL_ALLOW_USER_ONLY
:
996 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1000 BUG("invalid protocol_allow_config type");
1003 void transport_check_allowed(const char *type
)
1005 if (!is_transport_allowed(type
, -1))
1006 die(_("transport '%s' not allowed"), type
);
1009 static struct transport_vtable bundle_vtable
= {
1011 get_refs_from_bundle
,
1012 fetch_refs_from_bundle
,
1018 static struct transport_vtable builtin_smart_vtable
= {
1020 get_refs_via_connect
,
1021 fetch_refs_via_pack
,
1027 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1030 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1032 ret
->progress
= isatty(2);
1033 string_list_init(&ret
->pack_lockfiles
, 1);
1036 BUG("No remote provided to transport_get()");
1038 ret
->got_remote_refs
= 0;
1039 ret
->remote
= remote
;
1040 helper
= remote
->foreign_vcs
;
1042 if (!url
&& remote
->url
)
1043 url
= remote
->url
[0];
1046 /* maybe it is a foreign URL? */
1048 const char *p
= url
;
1050 while (is_urlschemechar(p
== url
, *p
))
1052 if (starts_with(p
, "::"))
1053 helper
= xstrndup(url
, p
- url
);
1057 transport_helper_init(ret
, helper
);
1058 } else if (starts_with(url
, "rsync:")) {
1059 die(_("git-over-rsync is no longer supported"));
1060 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1061 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1062 transport_check_allowed("file");
1064 ret
->vtable
= &bundle_vtable
;
1065 ret
->smart_options
= NULL
;
1066 } else if (!is_url(url
)
1067 || starts_with(url
, "file://")
1068 || starts_with(url
, "git://")
1069 || starts_with(url
, "ssh://")
1070 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1071 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1074 * These are builtin smart transports; "allowed" transports
1075 * will be checked individually in git_connect.
1077 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1079 ret
->vtable
= &builtin_smart_vtable
;
1080 ret
->smart_options
= &(data
->options
);
1083 data
->got_remote_heads
= 0;
1085 /* Unknown protocol in URL. Pass to external handler. */
1086 int len
= external_specification_len(url
);
1087 char *handler
= xmemdupz(url
, len
);
1088 transport_helper_init(ret
, handler
);
1091 if (ret
->smart_options
) {
1092 ret
->smart_options
->thin
= 1;
1093 ret
->smart_options
->uploadpack
= "git-upload-pack";
1094 if (remote
->uploadpack
)
1095 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1096 ret
->smart_options
->receivepack
= "git-receive-pack";
1097 if (remote
->receivepack
)
1098 ret
->smart_options
->receivepack
= remote
->receivepack
;
1101 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1106 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1108 return transport
->hash_algo
;
1111 int transport_set_option(struct transport
*transport
,
1112 const char *name
, const char *value
)
1114 int git_reports
= 1, protocol_reports
= 1;
1116 if (transport
->smart_options
)
1117 git_reports
= set_git_option(transport
->smart_options
,
1120 if (transport
->vtable
->set_option
)
1121 protocol_reports
= transport
->vtable
->set_option(transport
,
1124 /* If either report is 0, report 0 (success). */
1125 if (!git_reports
|| !protocol_reports
)
1127 /* If either reports -1 (invalid value), report -1. */
1128 if ((git_reports
== -1) || (protocol_reports
== -1))
1130 /* Otherwise if both report unknown, report unknown. */
1134 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1138 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1140 transport
->verbose
= -1;
1143 * Rules used to determine whether to report progress (processing aborts
1144 * when a rule is satisfied):
1146 * . Report progress, if force_progress is 1 (ie. --progress).
1147 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1148 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1149 * . Report progress if isatty(2) is 1.
1151 if (force_progress
>= 0)
1152 transport
->progress
= !!force_progress
;
1154 transport
->progress
= verbosity
>= 0 && isatty(2);
1157 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1161 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1162 "not be found on any remote:\n"));
1163 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1164 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1165 fprintf(stderr
, _("\nPlease try\n\n"
1166 " git push --recurse-submodules=on-demand\n\n"
1167 "or cd to the path and use\n\n"
1169 "to push them to a remote.\n\n"));
1171 string_list_clear(needs_pushing
, 0);
1173 die(_("Aborting."));
1176 static int run_pre_push_hook(struct transport
*transport
,
1177 struct ref
*remote_refs
)
1181 struct child_process proc
= CHILD_PROCESS_INIT
;
1183 const char *argv
[4];
1185 if (!(argv
[0] = find_hook("pre-push")))
1188 argv
[1] = transport
->remote
->name
;
1189 argv
[2] = transport
->url
;
1194 proc
.trace2_hook_name
= "pre-push";
1196 if (start_command(&proc
)) {
1197 finish_command(&proc
);
1201 sigchain_push(SIGPIPE
, SIG_IGN
);
1203 strbuf_init(&buf
, 256);
1205 for (r
= remote_refs
; r
; r
= r
->next
) {
1206 if (!r
->peer_ref
) continue;
1207 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1208 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1209 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1210 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1213 strbuf_addf( &buf
, "%s %s %s %s\n",
1214 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1215 r
->name
, oid_to_hex(&r
->old_oid
));
1217 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1218 /* We do not mind if a hook does not read all refs. */
1225 strbuf_release(&buf
);
1231 sigchain_pop(SIGPIPE
);
1233 x
= finish_command(&proc
);
1240 int transport_push(struct repository
*r
,
1241 struct transport
*transport
,
1242 struct refspec
*rs
, int flags
,
1243 unsigned int *reject_reasons
)
1245 *reject_reasons
= 0;
1247 if (transport_color_config() < 0)
1250 if (transport
->vtable
->push_refs
) {
1251 struct ref
*remote_refs
;
1252 struct ref
*local_refs
= get_local_heads();
1253 int match_flags
= MATCH_REFS_NONE
;
1254 int verbose
= (transport
->verbose
> 0);
1255 int quiet
= (transport
->verbose
< 0);
1256 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1257 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1258 int push_ret
, ret
, err
;
1259 struct transport_ls_refs_options transport_options
=
1260 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1262 if (check_push_refs(local_refs
, rs
) < 0)
1265 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1267 trace2_region_enter("transport_push", "get_refs_list", r
);
1268 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1269 &transport_options
);
1270 trace2_region_leave("transport_push", "get_refs_list", r
);
1272 strvec_clear(&transport_options
.ref_prefixes
);
1274 if (flags
& TRANSPORT_PUSH_ALL
)
1275 match_flags
|= MATCH_REFS_ALL
;
1276 if (flags
& TRANSPORT_PUSH_MIRROR
)
1277 match_flags
|= MATCH_REFS_MIRROR
;
1278 if (flags
& TRANSPORT_PUSH_PRUNE
)
1279 match_flags
|= MATCH_REFS_PRUNE
;
1280 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1281 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1283 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1286 if (transport
->smart_options
&&
1287 transport
->smart_options
->cas
&&
1288 !is_empty_cas(transport
->smart_options
->cas
))
1289 apply_push_cas(transport
->smart_options
->cas
,
1290 transport
->remote
, remote_refs
);
1292 set_ref_status_for_push(remote_refs
,
1293 flags
& TRANSPORT_PUSH_MIRROR
,
1294 flags
& TRANSPORT_PUSH_FORCE
);
1296 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1297 if (run_pre_push_hook(transport
, remote_refs
))
1300 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1301 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1302 !is_bare_repository()) {
1303 struct ref
*ref
= remote_refs
;
1304 struct oid_array commits
= OID_ARRAY_INIT
;
1306 trace2_region_enter("transport_push", "push_submodules", r
);
1307 for (; ref
; ref
= ref
->next
)
1308 if (!is_null_oid(&ref
->new_oid
))
1309 oid_array_append(&commits
,
1312 if (!push_unpushed_submodules(r
,
1316 transport
->push_options
,
1318 oid_array_clear(&commits
);
1319 trace2_region_leave("transport_push", "push_submodules", r
);
1320 die(_("failed to push all needed submodules"));
1322 oid_array_clear(&commits
);
1323 trace2_region_leave("transport_push", "push_submodules", r
);
1326 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1327 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1328 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1329 !pretend
)) && !is_bare_repository()) {
1330 struct ref
*ref
= remote_refs
;
1331 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1332 struct oid_array commits
= OID_ARRAY_INIT
;
1334 trace2_region_enter("transport_push", "check_submodules", r
);
1335 for (; ref
; ref
= ref
->next
)
1336 if (!is_null_oid(&ref
->new_oid
))
1337 oid_array_append(&commits
,
1340 if (find_unpushed_submodules(r
,
1342 transport
->remote
->name
,
1344 oid_array_clear(&commits
);
1345 trace2_region_leave("transport_push", "check_submodules", r
);
1346 die_with_unpushed_submodules(&needs_pushing
);
1348 string_list_clear(&needs_pushing
, 0);
1349 oid_array_clear(&commits
);
1350 trace2_region_leave("transport_push", "check_submodules", r
);
1353 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1354 trace2_region_enter("transport_push", "push_refs", r
);
1355 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1356 trace2_region_leave("transport_push", "push_refs", r
);
1359 err
= push_had_errors(remote_refs
);
1360 ret
= push_ret
| err
;
1363 transport_print_push_status(transport
->url
, remote_refs
,
1364 verbose
| porcelain
, porcelain
,
1367 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1368 set_upstreams(transport
, remote_refs
, pretend
);
1370 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1371 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1373 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1374 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1377 if (porcelain
&& !push_ret
)
1379 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1380 fprintf(stderr
, "Everything up-to-date\n");
1387 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1388 struct transport_ls_refs_options
*transport_options
)
1390 if (!transport
->got_remote_refs
) {
1391 transport
->remote_refs
=
1392 transport
->vtable
->get_refs_list(transport
, 0,
1394 transport
->got_remote_refs
= 1;
1397 return transport
->remote_refs
;
1400 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1403 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1404 struct ref
**heads
= NULL
;
1407 for (rm
= refs
; rm
; rm
= rm
->next
) {
1410 !is_null_oid(&rm
->old_oid
) &&
1411 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1413 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1414 heads
[nr_heads
++] = rm
;
1419 * When deepening of a shallow repository is requested,
1420 * then local and remote refs are likely to still be equal.
1421 * Just feed them all to the fetch method in that case.
1422 * This condition shouldn't be met in a non-deepening fetch
1423 * (see builtin/fetch.c:quickfetch()).
1425 ALLOC_ARRAY(heads
, nr_refs
);
1426 for (rm
= refs
; rm
; rm
= rm
->next
)
1427 heads
[nr_heads
++] = rm
;
1430 rc
= transport
->vtable
->fetch(transport
, nr_heads
, heads
);
1436 void transport_unlock_pack(struct transport
*transport
)
1440 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1441 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1442 string_list_clear(&transport
->pack_lockfiles
, 0);
1445 int transport_connect(struct transport
*transport
, const char *name
,
1446 const char *exec
, int fd
[2])
1448 if (transport
->vtable
->connect
)
1449 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1451 die(_("operation not supported by protocol"));
1454 int transport_disconnect(struct transport
*transport
)
1457 if (transport
->vtable
->disconnect
)
1458 ret
= transport
->vtable
->disconnect(transport
);
1459 if (transport
->got_remote_refs
)
1460 free_refs((void *)transport
->remote_refs
);
1466 * Strip username (and password) from a URL and return
1467 * it in a newly allocated string.
1469 char *transport_anonymize_url(const char *url
)
1471 char *scheme_prefix
, *anon_part
;
1472 size_t anon_len
, prefix_len
= 0;
1474 anon_part
= strchr(url
, '@');
1475 if (url_is_local_not_ssh(url
) || !anon_part
)
1478 anon_len
= strlen(++anon_part
);
1479 scheme_prefix
= strstr(url
, "://");
1480 if (!scheme_prefix
) {
1481 if (!strchr(anon_part
, ':'))
1482 /* cannot be "me@there:/path/name" */
1486 /* make sure scheme is reasonable */
1487 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1490 case '+': case '.': case '-':
1499 /* @ past the first slash does not count */
1500 cp
= strchr(scheme_prefix
+ 3, '/');
1501 if (cp
&& cp
< anon_part
)
1503 prefix_len
= scheme_prefix
- url
+ 3;
1505 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1506 (int)anon_len
, anon_part
);
1508 return xstrdup(url
);