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 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 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 if (data
->options
.acked_commits
) {
396 if (data
->version
< protocol_v2
) {
397 warning(_("--negotiate-only requires protocol v2"));
399 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
400 warning(_("server does not support wait-for-done"));
403 negotiate_using_fetch(data
->options
.negotiation_tips
,
404 transport
->server_options
,
405 transport
->stateless_rpc
,
407 data
->options
.acked_commits
);
413 refs
= fetch_pack(&args
, data
->fd
,
414 refs_tmp
? refs_tmp
: transport
->remote_refs
,
415 to_fetch
, nr_heads
, &data
->shallow
,
416 &transport
->pack_lockfiles
, data
->version
);
418 data
->got_remote_heads
= 0;
419 data
->options
.self_contained_and_connected
=
420 args
.self_contained_and_connected
;
421 data
->options
.connectivity_checked
= args
.connectivity_checked
;
425 if (report_unmatched_refs(to_fetch
, nr_heads
))
431 if (finish_connect(data
->conn
))
440 static int push_had_errors(struct ref
*ref
)
442 for (; ref
; ref
= ref
->next
) {
443 switch (ref
->status
) {
444 case REF_STATUS_NONE
:
445 case REF_STATUS_UPTODATE
:
455 int transport_refs_pushed(struct ref
*ref
)
457 for (; ref
; ref
= ref
->next
) {
458 switch(ref
->status
) {
459 case REF_STATUS_NONE
:
460 case REF_STATUS_UPTODATE
:
469 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
470 struct object_id
*new_oid
, int deletion
,
473 struct refspec_item rs
;
475 memset(&rs
, 0, sizeof(rs
));
479 if (!remote_find_tracking(remote
, &rs
)) {
481 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
483 delete_ref(NULL
, rs
.dst
, NULL
, 0);
485 update_ref("update by push", rs
.dst
, new_oid
,
491 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
494 struct object_id
*new_oid
;
495 struct ref_push_report
*report
;
497 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
500 report
= ref
->report
;
502 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
503 ref
->deletion
, verbose
);
505 for (; report
; report
= report
->next
) {
506 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
507 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
508 update_one_tracking_ref(remote
, refname
, new_oid
,
509 is_null_oid(new_oid
), verbose
);
513 static void print_ref_status(char flag
, const char *summary
,
514 struct ref
*to
, struct ref
*from
, const char *msg
,
515 struct ref_push_report
*report
,
516 int porcelain
, int summary_width
)
520 if (report
&& report
->ref_name
)
521 to_name
= report
->ref_name
;
527 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
529 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
531 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
533 fprintf(stdout
, "%s\n", summary
);
535 const char *red
= "", *reset
= "";
536 if (push_had_errors(to
)) {
537 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
538 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
540 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
543 fprintf(stderr
, "%s -> %s",
544 prettify_refname(from
->name
),
545 prettify_refname(to_name
));
547 fputs(prettify_refname(to_name
), stderr
);
557 static void print_ok_ref_status(struct ref
*ref
,
558 struct ref_push_report
*report
,
559 int porcelain
, int summary_width
)
561 struct object_id
*old_oid
;
562 struct object_id
*new_oid
;
563 const char *ref_name
;
566 if (report
&& report
->old_oid
)
567 old_oid
= report
->old_oid
;
569 old_oid
= &ref
->old_oid
;
570 if (report
&& report
->new_oid
)
571 new_oid
= report
->new_oid
;
573 new_oid
= &ref
->new_oid
;
574 if (report
&& report
->forced_update
)
575 forced_update
= report
->forced_update
;
577 forced_update
= ref
->forced_update
;
578 if (report
&& report
->ref_name
)
579 ref_name
= report
->ref_name
;
581 ref_name
= ref
->name
;
584 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
585 report
, porcelain
, summary_width
);
586 else if (is_null_oid(old_oid
))
587 print_ref_status('*',
588 (starts_with(ref_name
, "refs/tags/")
590 : (starts_with(ref_name
, "refs/heads/")
592 : "[new reference]")),
593 ref
, ref
->peer_ref
, NULL
,
594 report
, porcelain
, summary_width
);
596 struct strbuf quickref
= STRBUF_INIT
;
600 strbuf_add_unique_abbrev(&quickref
, old_oid
,
603 strbuf_addstr(&quickref
, "...");
605 msg
= "forced update";
607 strbuf_addstr(&quickref
, "..");
611 strbuf_add_unique_abbrev(&quickref
, new_oid
,
614 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
615 report
, porcelain
, summary_width
);
616 strbuf_release(&quickref
);
620 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
621 struct ref_push_report
*report
,
622 int porcelain
, int summary_width
)
625 char *url
= transport_anonymize_url(dest
);
626 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
630 switch(ref
->status
) {
631 case REF_STATUS_NONE
:
632 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
633 report
, porcelain
, summary_width
);
635 case REF_STATUS_REJECT_NODELETE
:
636 print_ref_status('!', "[rejected]", ref
, NULL
,
637 "remote does not support deleting refs",
638 report
, porcelain
, summary_width
);
640 case REF_STATUS_UPTODATE
:
641 print_ref_status('=', "[up to date]", ref
,
643 report
, porcelain
, summary_width
);
645 case REF_STATUS_REJECT_NONFASTFORWARD
:
646 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
648 report
, porcelain
, summary_width
);
650 case REF_STATUS_REJECT_ALREADY_EXISTS
:
651 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
653 report
, porcelain
, summary_width
);
655 case REF_STATUS_REJECT_FETCH_FIRST
:
656 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
658 report
, porcelain
, summary_width
);
660 case REF_STATUS_REJECT_NEEDS_FORCE
:
661 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
663 report
, porcelain
, summary_width
);
665 case REF_STATUS_REJECT_STALE
:
666 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
668 report
, porcelain
, summary_width
);
670 case REF_STATUS_REJECT_REMOTE_UPDATED
:
671 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
672 "remote ref updated since checkout",
673 report
, porcelain
, summary_width
);
675 case REF_STATUS_REJECT_SHALLOW
:
676 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
677 "new shallow roots not allowed",
678 report
, porcelain
, summary_width
);
680 case REF_STATUS_REMOTE_REJECT
:
681 print_ref_status('!', "[remote rejected]", ref
,
682 ref
->deletion
? NULL
: ref
->peer_ref
,
684 report
, porcelain
, summary_width
);
686 case REF_STATUS_EXPECTING_REPORT
:
687 print_ref_status('!', "[remote failure]", ref
,
688 ref
->deletion
? NULL
: ref
->peer_ref
,
689 "remote failed to report status",
690 report
, porcelain
, summary_width
);
692 case REF_STATUS_ATOMIC_PUSH_FAILED
:
693 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
694 "atomic push failed",
695 report
, porcelain
, summary_width
);
698 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
705 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
706 int porcelain
, int summary_width
)
708 struct ref_push_report
*report
;
712 return print_one_push_report(ref
, dest
, count
,
713 NULL
, porcelain
, summary_width
);
715 for (report
= ref
->report
; report
; report
= report
->next
)
716 print_one_push_report(ref
, dest
, count
+ n
++,
717 report
, porcelain
, summary_width
);
721 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
723 char hex
[GIT_MAX_HEXSZ
+ 1];
724 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
726 return (w
< sofar
) ? sofar
: w
;
729 int transport_summary_width(const struct ref
*refs
)
733 for (; refs
; refs
= refs
->next
) {
734 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
735 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
738 maxw
= FALLBACK_DEFAULT_ABBREV
;
739 return (2 * maxw
+ 3);
742 void transport_print_push_status(const char *dest
, struct ref
*refs
,
743 int verbose
, int porcelain
, unsigned int *reject_reasons
)
748 int summary_width
= transport_summary_width(refs
);
750 if (transport_color_config() < 0)
751 warning(_("could not parse transport.color.* config"));
753 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
756 for (ref
= refs
; ref
; ref
= ref
->next
)
757 if (ref
->status
== REF_STATUS_UPTODATE
)
758 n
+= print_one_push_status(ref
, dest
, n
,
759 porcelain
, summary_width
);
762 for (ref
= refs
; ref
; ref
= ref
->next
)
763 if (ref
->status
== REF_STATUS_OK
)
764 n
+= print_one_push_status(ref
, dest
, n
,
765 porcelain
, summary_width
);
768 for (ref
= refs
; ref
; ref
= ref
->next
) {
769 if (ref
->status
!= REF_STATUS_NONE
&&
770 ref
->status
!= REF_STATUS_UPTODATE
&&
771 ref
->status
!= REF_STATUS_OK
)
772 n
+= print_one_push_status(ref
, dest
, n
,
773 porcelain
, summary_width
);
774 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
775 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
776 *reject_reasons
|= REJECT_NON_FF_HEAD
;
778 *reject_reasons
|= REJECT_NON_FF_OTHER
;
779 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
780 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
781 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
782 *reject_reasons
|= REJECT_FETCH_FIRST
;
783 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
784 *reject_reasons
|= REJECT_NEEDS_FORCE
;
785 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
786 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
792 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
794 struct git_transport_data
*data
= transport
->data
;
795 struct send_pack_args args
;
798 if (transport_color_config() < 0)
801 if (!data
->got_remote_heads
)
802 get_refs_via_connect(transport
, 1, NULL
);
804 memset(&args
, 0, sizeof(args
));
805 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
806 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
807 args
.use_thin_pack
= data
->options
.thin
;
808 args
.verbose
= (transport
->verbose
> 0);
809 args
.quiet
= (transport
->verbose
< 0);
810 args
.progress
= transport
->progress
;
811 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
812 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
813 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
814 args
.push_options
= transport
->push_options
;
815 args
.url
= transport
->url
;
817 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
818 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
819 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
820 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
822 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
824 switch (data
->version
) {
826 die(_("support for protocol v2 not implemented yet"));
830 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
833 case protocol_unknown_version
:
834 BUG("unknown protocol version");
840 * Atomic push may abort the connection early and close the pipe,
841 * which may cause an error for `finish_connect()`. Ignore this error
842 * for atomic git-push.
844 if (ret
|| args
.atomic
)
845 finish_connect(data
->conn
);
847 ret
= finish_connect(data
->conn
);
849 data
->got_remote_heads
= 0;
854 static int connect_git(struct transport
*transport
, const char *name
,
855 const char *executable
, int fd
[2])
857 struct git_transport_data
*data
= transport
->data
;
858 data
->conn
= git_connect(data
->fd
, transport
->url
,
865 static int disconnect_git(struct transport
*transport
)
867 struct git_transport_data
*data
= transport
->data
;
869 if (data
->got_remote_heads
&& !transport
->stateless_rpc
)
870 packet_flush(data
->fd
[1]);
873 finish_connect(data
->conn
);
880 static struct transport_vtable taken_over_vtable
= {
882 get_refs_via_connect
,
889 void transport_take_over(struct transport
*transport
,
890 struct child_process
*child
)
892 struct git_transport_data
*data
;
894 if (!transport
->smart_options
)
895 BUG("taking over transport requires non-NULL "
896 "smart_options field.");
898 CALLOC_ARRAY(data
, 1);
899 data
->options
= *transport
->smart_options
;
901 data
->fd
[0] = data
->conn
->out
;
902 data
->fd
[1] = data
->conn
->in
;
903 data
->got_remote_heads
= 0;
904 transport
->data
= data
;
906 transport
->vtable
= &taken_over_vtable
;
907 transport
->smart_options
= &(data
->options
);
909 transport
->cannot_reuse
= 1;
912 static int is_file(const char *url
)
917 return S_ISREG(buf
.st_mode
);
920 static int external_specification_len(const char *url
)
922 return strchr(url
, ':') - url
;
925 static const struct string_list
*protocol_whitelist(void)
927 static int enabled
= -1;
928 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
931 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
933 string_list_split(&allowed
, v
, ':', -1);
934 string_list_sort(&allowed
);
941 return enabled
? &allowed
: NULL
;
944 enum protocol_allow_config
{
945 PROTOCOL_ALLOW_NEVER
= 0,
946 PROTOCOL_ALLOW_USER_ONLY
,
947 PROTOCOL_ALLOW_ALWAYS
950 static enum protocol_allow_config
parse_protocol_config(const char *key
,
953 if (!strcasecmp(value
, "always"))
954 return PROTOCOL_ALLOW_ALWAYS
;
955 else if (!strcasecmp(value
, "never"))
956 return PROTOCOL_ALLOW_NEVER
;
957 else if (!strcasecmp(value
, "user"))
958 return PROTOCOL_ALLOW_USER_ONLY
;
960 die(_("unknown value for config '%s': %s"), key
, value
);
963 static enum protocol_allow_config
get_protocol_config(const char *type
)
965 char *key
= xstrfmt("protocol.%s.allow", type
);
968 /* first check the per-protocol config */
969 if (!git_config_get_string(key
, &value
)) {
970 enum protocol_allow_config ret
=
971 parse_protocol_config(key
, value
);
978 /* if defined, fallback to user-defined default for unknown protocols */
979 if (!git_config_get_string("protocol.allow", &value
)) {
980 enum protocol_allow_config ret
=
981 parse_protocol_config("protocol.allow", value
);
986 /* fallback to built-in defaults */
988 if (!strcmp(type
, "http") ||
989 !strcmp(type
, "https") ||
990 !strcmp(type
, "git") ||
991 !strcmp(type
, "ssh") ||
992 !strcmp(type
, "file"))
993 return PROTOCOL_ALLOW_ALWAYS
;
995 /* known scary; err on the side of caution */
996 if (!strcmp(type
, "ext"))
997 return PROTOCOL_ALLOW_NEVER
;
999 /* unknown; by default let them be used only directly by the user */
1000 return PROTOCOL_ALLOW_USER_ONLY
;
1003 int is_transport_allowed(const char *type
, int from_user
)
1005 const struct string_list
*whitelist
= protocol_whitelist();
1007 return string_list_has_string(whitelist
, type
);
1009 switch (get_protocol_config(type
)) {
1010 case PROTOCOL_ALLOW_ALWAYS
:
1012 case PROTOCOL_ALLOW_NEVER
:
1014 case PROTOCOL_ALLOW_USER_ONLY
:
1016 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1020 BUG("invalid protocol_allow_config type");
1023 void transport_check_allowed(const char *type
)
1025 if (!is_transport_allowed(type
, -1))
1026 die(_("transport '%s' not allowed"), type
);
1029 static struct transport_vtable bundle_vtable
= {
1031 get_refs_from_bundle
,
1032 fetch_refs_from_bundle
,
1038 static struct transport_vtable builtin_smart_vtable
= {
1040 get_refs_via_connect
,
1041 fetch_refs_via_pack
,
1047 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1050 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1052 ret
->progress
= isatty(2);
1053 string_list_init(&ret
->pack_lockfiles
, 1);
1056 BUG("No remote provided to transport_get()");
1058 ret
->got_remote_refs
= 0;
1059 ret
->remote
= remote
;
1060 helper
= remote
->foreign_vcs
;
1062 if (!url
&& remote
->url
)
1063 url
= remote
->url
[0];
1066 /* maybe it is a foreign URL? */
1068 const char *p
= url
;
1070 while (is_urlschemechar(p
== url
, *p
))
1072 if (starts_with(p
, "::"))
1073 helper
= xstrndup(url
, p
- url
);
1077 transport_helper_init(ret
, helper
);
1078 } else if (starts_with(url
, "rsync:")) {
1079 die(_("git-over-rsync is no longer supported"));
1080 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1081 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1082 transport_check_allowed("file");
1084 ret
->vtable
= &bundle_vtable
;
1085 ret
->smart_options
= NULL
;
1086 } else if (!is_url(url
)
1087 || starts_with(url
, "file://")
1088 || starts_with(url
, "git://")
1089 || starts_with(url
, "ssh://")
1090 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1091 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1094 * These are builtin smart transports; "allowed" transports
1095 * will be checked individually in git_connect.
1097 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1099 ret
->vtable
= &builtin_smart_vtable
;
1100 ret
->smart_options
= &(data
->options
);
1103 data
->got_remote_heads
= 0;
1105 /* Unknown protocol in URL. Pass to external handler. */
1106 int len
= external_specification_len(url
);
1107 char *handler
= xmemdupz(url
, len
);
1108 transport_helper_init(ret
, handler
);
1111 if (ret
->smart_options
) {
1112 ret
->smart_options
->thin
= 1;
1113 ret
->smart_options
->uploadpack
= "git-upload-pack";
1114 if (remote
->uploadpack
)
1115 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1116 ret
->smart_options
->receivepack
= "git-receive-pack";
1117 if (remote
->receivepack
)
1118 ret
->smart_options
->receivepack
= remote
->receivepack
;
1121 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1126 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1128 return transport
->hash_algo
;
1131 int transport_set_option(struct transport
*transport
,
1132 const char *name
, const char *value
)
1134 int git_reports
= 1, protocol_reports
= 1;
1136 if (transport
->smart_options
)
1137 git_reports
= set_git_option(transport
->smart_options
,
1140 if (transport
->vtable
->set_option
)
1141 protocol_reports
= transport
->vtable
->set_option(transport
,
1144 /* If either report is 0, report 0 (success). */
1145 if (!git_reports
|| !protocol_reports
)
1147 /* If either reports -1 (invalid value), report -1. */
1148 if ((git_reports
== -1) || (protocol_reports
== -1))
1150 /* Otherwise if both report unknown, report unknown. */
1154 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1158 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1160 transport
->verbose
= -1;
1163 * Rules used to determine whether to report progress (processing aborts
1164 * when a rule is satisfied):
1166 * . Report progress, if force_progress is 1 (ie. --progress).
1167 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1168 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1169 * . Report progress if isatty(2) is 1.
1171 if (force_progress
>= 0)
1172 transport
->progress
= !!force_progress
;
1174 transport
->progress
= verbosity
>= 0 && isatty(2);
1177 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1181 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1182 "not be found on any remote:\n"));
1183 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1184 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1185 fprintf(stderr
, _("\nPlease try\n\n"
1186 " git push --recurse-submodules=on-demand\n\n"
1187 "or cd to the path and use\n\n"
1189 "to push them to a remote.\n\n"));
1191 string_list_clear(needs_pushing
, 0);
1193 die(_("Aborting."));
1196 static int run_pre_push_hook(struct transport
*transport
,
1197 struct ref
*remote_refs
)
1201 struct child_process proc
= CHILD_PROCESS_INIT
;
1203 const char *argv
[4];
1205 if (!(argv
[0] = find_hook("pre-push")))
1208 argv
[1] = transport
->remote
->name
;
1209 argv
[2] = transport
->url
;
1214 proc
.trace2_hook_name
= "pre-push";
1216 if (start_command(&proc
)) {
1217 finish_command(&proc
);
1221 sigchain_push(SIGPIPE
, SIG_IGN
);
1223 strbuf_init(&buf
, 256);
1225 for (r
= remote_refs
; r
; r
= r
->next
) {
1226 if (!r
->peer_ref
) continue;
1227 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1228 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1229 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1230 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1233 strbuf_addf( &buf
, "%s %s %s %s\n",
1234 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1235 r
->name
, oid_to_hex(&r
->old_oid
));
1237 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1238 /* We do not mind if a hook does not read all refs. */
1245 strbuf_release(&buf
);
1251 sigchain_pop(SIGPIPE
);
1253 x
= finish_command(&proc
);
1260 int transport_push(struct repository
*r
,
1261 struct transport
*transport
,
1262 struct refspec
*rs
, int flags
,
1263 unsigned int *reject_reasons
)
1265 *reject_reasons
= 0;
1267 if (transport_color_config() < 0)
1270 if (transport
->vtable
->push_refs
) {
1271 struct ref
*remote_refs
;
1272 struct ref
*local_refs
= get_local_heads();
1273 int match_flags
= MATCH_REFS_NONE
;
1274 int verbose
= (transport
->verbose
> 0);
1275 int quiet
= (transport
->verbose
< 0);
1276 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1277 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1278 int push_ret
, ret
, err
;
1279 struct transport_ls_refs_options transport_options
=
1280 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1282 if (check_push_refs(local_refs
, rs
) < 0)
1285 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1287 trace2_region_enter("transport_push", "get_refs_list", r
);
1288 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1289 &transport_options
);
1290 trace2_region_leave("transport_push", "get_refs_list", r
);
1292 strvec_clear(&transport_options
.ref_prefixes
);
1294 if (flags
& TRANSPORT_PUSH_ALL
)
1295 match_flags
|= MATCH_REFS_ALL
;
1296 if (flags
& TRANSPORT_PUSH_MIRROR
)
1297 match_flags
|= MATCH_REFS_MIRROR
;
1298 if (flags
& TRANSPORT_PUSH_PRUNE
)
1299 match_flags
|= MATCH_REFS_PRUNE
;
1300 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1301 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1303 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1306 if (transport
->smart_options
&&
1307 transport
->smart_options
->cas
&&
1308 !is_empty_cas(transport
->smart_options
->cas
))
1309 apply_push_cas(transport
->smart_options
->cas
,
1310 transport
->remote
, remote_refs
);
1312 set_ref_status_for_push(remote_refs
,
1313 flags
& TRANSPORT_PUSH_MIRROR
,
1314 flags
& TRANSPORT_PUSH_FORCE
);
1316 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1317 if (run_pre_push_hook(transport
, remote_refs
))
1320 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1321 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1322 !is_bare_repository()) {
1323 struct ref
*ref
= remote_refs
;
1324 struct oid_array commits
= OID_ARRAY_INIT
;
1326 trace2_region_enter("transport_push", "push_submodules", r
);
1327 for (; ref
; ref
= ref
->next
)
1328 if (!is_null_oid(&ref
->new_oid
))
1329 oid_array_append(&commits
,
1332 if (!push_unpushed_submodules(r
,
1336 transport
->push_options
,
1338 oid_array_clear(&commits
);
1339 trace2_region_leave("transport_push", "push_submodules", r
);
1340 die(_("failed to push all needed submodules"));
1342 oid_array_clear(&commits
);
1343 trace2_region_leave("transport_push", "push_submodules", r
);
1346 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1347 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1348 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1349 !pretend
)) && !is_bare_repository()) {
1350 struct ref
*ref
= remote_refs
;
1351 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1352 struct oid_array commits
= OID_ARRAY_INIT
;
1354 trace2_region_enter("transport_push", "check_submodules", r
);
1355 for (; ref
; ref
= ref
->next
)
1356 if (!is_null_oid(&ref
->new_oid
))
1357 oid_array_append(&commits
,
1360 if (find_unpushed_submodules(r
,
1362 transport
->remote
->name
,
1364 oid_array_clear(&commits
);
1365 trace2_region_leave("transport_push", "check_submodules", r
);
1366 die_with_unpushed_submodules(&needs_pushing
);
1368 string_list_clear(&needs_pushing
, 0);
1369 oid_array_clear(&commits
);
1370 trace2_region_leave("transport_push", "check_submodules", r
);
1373 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1374 trace2_region_enter("transport_push", "push_refs", r
);
1375 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1376 trace2_region_leave("transport_push", "push_refs", r
);
1379 err
= push_had_errors(remote_refs
);
1380 ret
= push_ret
| err
;
1383 transport_print_push_status(transport
->url
, remote_refs
,
1384 verbose
| porcelain
, porcelain
,
1387 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1388 set_upstreams(transport
, remote_refs
, pretend
);
1390 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1391 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1393 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1394 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1397 if (porcelain
&& !push_ret
)
1399 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1400 fprintf(stderr
, "Everything up-to-date\n");
1407 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1408 struct transport_ls_refs_options
*transport_options
)
1410 if (!transport
->got_remote_refs
) {
1411 transport
->remote_refs
=
1412 transport
->vtable
->get_refs_list(transport
, 0,
1414 transport
->got_remote_refs
= 1;
1417 return transport
->remote_refs
;
1420 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1423 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1424 struct ref
**heads
= NULL
;
1427 for (rm
= refs
; rm
; rm
= rm
->next
) {
1430 !is_null_oid(&rm
->old_oid
) &&
1431 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1433 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1434 heads
[nr_heads
++] = rm
;
1439 * When deepening of a shallow repository is requested,
1440 * then local and remote refs are likely to still be equal.
1441 * Just feed them all to the fetch method in that case.
1442 * This condition shouldn't be met in a non-deepening fetch
1443 * (see builtin/fetch.c:quickfetch()).
1445 ALLOC_ARRAY(heads
, nr_refs
);
1446 for (rm
= refs
; rm
; rm
= rm
->next
)
1447 heads
[nr_heads
++] = rm
;
1450 rc
= transport
->vtable
->fetch(transport
, nr_heads
, heads
);
1456 void transport_unlock_pack(struct transport
*transport
)
1460 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1461 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1462 string_list_clear(&transport
->pack_lockfiles
, 0);
1465 int transport_connect(struct transport
*transport
, const char *name
,
1466 const char *exec
, int fd
[2])
1468 if (transport
->vtable
->connect
)
1469 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1471 die(_("operation not supported by protocol"));
1474 int transport_disconnect(struct transport
*transport
)
1477 if (transport
->vtable
->disconnect
)
1478 ret
= transport
->vtable
->disconnect(transport
);
1479 if (transport
->got_remote_refs
)
1480 free_refs((void *)transport
->remote_refs
);
1486 * Strip username (and password) from a URL and return
1487 * it in a newly allocated string.
1489 char *transport_anonymize_url(const char *url
)
1491 char *scheme_prefix
, *anon_part
;
1492 size_t anon_len
, prefix_len
= 0;
1494 anon_part
= strchr(url
, '@');
1495 if (url_is_local_not_ssh(url
) || !anon_part
)
1498 anon_len
= strlen(++anon_part
);
1499 scheme_prefix
= strstr(url
, "://");
1500 if (!scheme_prefix
) {
1501 if (!strchr(anon_part
, ':'))
1502 /* cannot be "me@there:/path/name" */
1506 /* make sure scheme is reasonable */
1507 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1510 case '+': case '.': case '-':
1519 /* @ past the first slash does not count */
1520 cp
= strchr(scheme_prefix
+ 3, '/');
1521 if (cp
&& cp
< anon_part
)
1523 prefix_len
= scheme_prefix
- url
+ 3;
1525 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1526 (int)anon_len
, anon_part
);
1528 return xstrdup(url
);