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 string_list_item
*e
= data
->header
.references
.items
+ i
;
151 const char *name
= e
->string
;
152 struct ref
*ref
= alloc_ref(name
);
153 struct object_id
*oid
= e
->util
;
154 oidcpy(&ref
->old_oid
, oid
);
161 static int fetch_refs_from_bundle(struct transport
*transport
,
162 int nr_heads
, struct ref
**to_fetch
)
164 struct bundle_transport_data
*data
= transport
->data
;
167 if (!data
->get_refs_from_bundle_called
)
168 get_refs_from_bundle(transport
, 0, NULL
);
169 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
170 transport
->progress
? BUNDLE_VERBOSE
: 0);
171 transport
->hash_algo
= data
->header
.hash_algo
;
175 static int close_bundle(struct transport
*transport
)
177 struct bundle_transport_data
*data
= transport
->data
;
180 bundle_header_release(&data
->header
);
185 struct git_transport_data
{
186 struct git_transport_options options
;
187 struct child_process
*conn
;
189 unsigned got_remote_heads
: 1;
190 enum protocol_version version
;
191 struct oid_array extra_have
;
192 struct oid_array shallow
;
195 static int set_git_option(struct git_transport_options
*opts
,
196 const char *name
, const char *value
)
198 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
199 opts
->uploadpack
= value
;
201 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
202 opts
->receivepack
= value
;
204 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
205 opts
->thin
= !!value
;
207 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
208 opts
->followtags
= !!value
;
210 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
211 opts
->keep
= !!value
;
213 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
214 opts
->update_shallow
= !!value
;
216 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
221 opts
->depth
= strtol(value
, &end
, 0);
223 die(_("transport: invalid depth option '%s'"), value
);
226 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
227 opts
->deepen_since
= value
;
229 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
230 opts
->deepen_not
= (const struct string_list
*)value
;
232 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
233 opts
->deepen_relative
= !!value
;
235 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
236 opts
->from_promisor
= !!value
;
238 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
239 list_objects_filter_die_if_populated(&opts
->filter_options
);
240 parse_list_objects_filter(&opts
->filter_options
, value
);
242 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
243 opts
->reject_shallow
= !!value
;
249 static int connect_setup(struct transport
*transport
, int for_push
)
251 struct git_transport_data
*data
= transport
->data
;
252 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
257 switch (transport
->family
) {
258 case TRANSPORT_FAMILY_ALL
: break;
259 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
260 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
263 data
->conn
= git_connect(data
->fd
, transport
->url
,
264 for_push
? data
->options
.receivepack
:
265 data
->options
.uploadpack
,
271 static void die_if_server_options(struct transport
*transport
)
273 if (!transport
->server_options
|| !transport
->server_options
->nr
)
275 advise(_("see protocol.version in 'git help config' for more details"));
276 die(_("server options require protocol version 2 or later"));
280 * Obtains the protocol version from the transport and writes it to
281 * transport->data->version, first connecting if not already connected.
283 * If the protocol version is one that allows skipping the listing of remote
284 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
285 * this function returns NULL. Otherwise, this function returns the list of
288 static struct ref
*handshake(struct transport
*transport
, int for_push
,
289 struct transport_ls_refs_options
*options
,
292 struct git_transport_data
*data
= transport
->data
;
293 struct ref
*refs
= NULL
;
294 struct packet_reader reader
;
296 const char *server_sid
;
298 connect_setup(transport
, for_push
);
300 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
301 PACKET_READ_CHOMP_NEWLINE
|
302 PACKET_READ_GENTLE_ON_EOF
|
303 PACKET_READ_DIE_ON_ERR_PACKET
);
305 data
->version
= discover_version(&reader
);
306 switch (data
->version
) {
308 if (server_feature_v2("session-id", &server_sid
))
309 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
311 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
313 transport
->server_options
,
314 transport
->stateless_rpc
);
318 die_if_server_options(transport
);
319 get_remote_heads(&reader
, &refs
,
320 for_push
? REF_NORMAL
: 0,
323 server_sid
= server_feature_value("session-id", &sid_len
);
325 char *sid
= xstrndup(server_sid
, sid_len
);
326 trace2_data_string("transfer", NULL
, "server-sid", sid
);
330 case protocol_unknown_version
:
331 BUG("unknown protocol version");
333 data
->got_remote_heads
= 1;
334 transport
->hash_algo
= reader
.hash_algo
;
336 if (reader
.line_peeked
)
337 BUG("buffer must be empty at the end of handshake()");
342 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
343 struct transport_ls_refs_options
*options
)
345 return handshake(transport
, for_push
, options
, 1);
348 static int fetch_refs_via_pack(struct transport
*transport
,
349 int nr_heads
, struct ref
**to_fetch
)
352 struct git_transport_data
*data
= transport
->data
;
353 struct ref
*refs
= NULL
;
354 struct fetch_pack_args args
;
355 struct ref
*refs_tmp
= NULL
;
357 memset(&args
, 0, sizeof(args
));
358 args
.uploadpack
= data
->options
.uploadpack
;
359 args
.keep_pack
= data
->options
.keep
;
361 args
.use_thin_pack
= data
->options
.thin
;
362 args
.include_tag
= data
->options
.followtags
;
363 args
.verbose
= (transport
->verbose
> 1);
364 args
.quiet
= (transport
->verbose
< 0);
365 args
.no_progress
= !transport
->progress
;
366 args
.depth
= data
->options
.depth
;
367 args
.deepen_since
= data
->options
.deepen_since
;
368 args
.deepen_not
= data
->options
.deepen_not
;
369 args
.deepen_relative
= data
->options
.deepen_relative
;
370 args
.check_self_contained_and_connected
=
371 data
->options
.check_self_contained_and_connected
;
372 args
.cloning
= transport
->cloning
;
373 args
.update_shallow
= data
->options
.update_shallow
;
374 args
.from_promisor
= data
->options
.from_promisor
;
375 args
.filter_options
= data
->options
.filter_options
;
376 args
.stateless_rpc
= transport
->stateless_rpc
;
377 args
.server_options
= transport
->server_options
;
378 args
.negotiation_tips
= data
->options
.negotiation_tips
;
379 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
381 if (!data
->got_remote_heads
) {
383 int must_list_refs
= 0;
384 for (i
= 0; i
< nr_heads
; i
++) {
385 if (!to_fetch
[i
]->exact_oid
) {
390 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
393 if (data
->version
== protocol_unknown_version
)
394 BUG("unknown protocol version");
395 else if (data
->version
<= protocol_v1
)
396 die_if_server_options(transport
);
398 if (data
->options
.acked_commits
) {
399 if (data
->version
< protocol_v2
) {
400 warning(_("--negotiate-only requires protocol v2"));
402 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
403 warning(_("server does not support wait-for-done"));
406 negotiate_using_fetch(data
->options
.negotiation_tips
,
407 transport
->server_options
,
408 transport
->stateless_rpc
,
410 data
->options
.acked_commits
);
416 refs
= fetch_pack(&args
, data
->fd
,
417 refs_tmp
? refs_tmp
: transport
->remote_refs
,
418 to_fetch
, nr_heads
, &data
->shallow
,
419 &transport
->pack_lockfiles
, data
->version
);
421 data
->got_remote_heads
= 0;
422 data
->options
.self_contained_and_connected
=
423 args
.self_contained_and_connected
;
424 data
->options
.connectivity_checked
= args
.connectivity_checked
;
428 if (report_unmatched_refs(to_fetch
, nr_heads
))
433 if (data
->fd
[1] >= 0)
435 if (finish_connect(data
->conn
))
444 static int push_had_errors(struct ref
*ref
)
446 for (; ref
; ref
= ref
->next
) {
447 switch (ref
->status
) {
448 case REF_STATUS_NONE
:
449 case REF_STATUS_UPTODATE
:
459 int transport_refs_pushed(struct ref
*ref
)
461 for (; ref
; ref
= ref
->next
) {
462 switch(ref
->status
) {
463 case REF_STATUS_NONE
:
464 case REF_STATUS_UPTODATE
:
473 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
474 struct object_id
*new_oid
, int deletion
,
477 struct refspec_item rs
;
479 memset(&rs
, 0, sizeof(rs
));
483 if (!remote_find_tracking(remote
, &rs
)) {
485 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
487 delete_ref(NULL
, rs
.dst
, NULL
, 0);
489 update_ref("update by push", rs
.dst
, new_oid
,
495 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
498 struct object_id
*new_oid
;
499 struct ref_push_report
*report
;
501 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
504 report
= ref
->report
;
506 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
507 ref
->deletion
, verbose
);
509 for (; report
; report
= report
->next
) {
510 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
511 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
512 update_one_tracking_ref(remote
, refname
, new_oid
,
513 is_null_oid(new_oid
), verbose
);
517 static void print_ref_status(char flag
, const char *summary
,
518 struct ref
*to
, struct ref
*from
, const char *msg
,
519 struct ref_push_report
*report
,
520 int porcelain
, int summary_width
)
524 if (report
&& report
->ref_name
)
525 to_name
= report
->ref_name
;
531 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
533 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
535 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
537 fprintf(stdout
, "%s\n", summary
);
539 const char *red
= "", *reset
= "";
540 if (push_had_errors(to
)) {
541 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
542 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
544 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
547 fprintf(stderr
, "%s -> %s",
548 prettify_refname(from
->name
),
549 prettify_refname(to_name
));
551 fputs(prettify_refname(to_name
), stderr
);
561 static void print_ok_ref_status(struct ref
*ref
,
562 struct ref_push_report
*report
,
563 int porcelain
, int summary_width
)
565 struct object_id
*old_oid
;
566 struct object_id
*new_oid
;
567 const char *ref_name
;
570 if (report
&& report
->old_oid
)
571 old_oid
= report
->old_oid
;
573 old_oid
= &ref
->old_oid
;
574 if (report
&& report
->new_oid
)
575 new_oid
= report
->new_oid
;
577 new_oid
= &ref
->new_oid
;
578 if (report
&& report
->forced_update
)
579 forced_update
= report
->forced_update
;
581 forced_update
= ref
->forced_update
;
582 if (report
&& report
->ref_name
)
583 ref_name
= report
->ref_name
;
585 ref_name
= ref
->name
;
588 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
589 report
, porcelain
, summary_width
);
590 else if (is_null_oid(old_oid
))
591 print_ref_status('*',
592 (starts_with(ref_name
, "refs/tags/")
594 : (starts_with(ref_name
, "refs/heads/")
596 : "[new reference]")),
597 ref
, ref
->peer_ref
, NULL
,
598 report
, porcelain
, summary_width
);
600 struct strbuf quickref
= STRBUF_INIT
;
604 strbuf_add_unique_abbrev(&quickref
, old_oid
,
607 strbuf_addstr(&quickref
, "...");
609 msg
= "forced update";
611 strbuf_addstr(&quickref
, "..");
615 strbuf_add_unique_abbrev(&quickref
, new_oid
,
618 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
619 report
, porcelain
, summary_width
);
620 strbuf_release(&quickref
);
624 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
625 struct ref_push_report
*report
,
626 int porcelain
, int summary_width
)
629 char *url
= transport_anonymize_url(dest
);
630 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
634 switch(ref
->status
) {
635 case REF_STATUS_NONE
:
636 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
637 report
, porcelain
, summary_width
);
639 case REF_STATUS_REJECT_NODELETE
:
640 print_ref_status('!', "[rejected]", ref
, NULL
,
641 "remote does not support deleting refs",
642 report
, porcelain
, summary_width
);
644 case REF_STATUS_UPTODATE
:
645 print_ref_status('=', "[up to date]", ref
,
647 report
, porcelain
, summary_width
);
649 case REF_STATUS_REJECT_NONFASTFORWARD
:
650 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
652 report
, porcelain
, summary_width
);
654 case REF_STATUS_REJECT_ALREADY_EXISTS
:
655 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
657 report
, porcelain
, summary_width
);
659 case REF_STATUS_REJECT_FETCH_FIRST
:
660 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
662 report
, porcelain
, summary_width
);
664 case REF_STATUS_REJECT_NEEDS_FORCE
:
665 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
667 report
, porcelain
, summary_width
);
669 case REF_STATUS_REJECT_STALE
:
670 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
672 report
, porcelain
, summary_width
);
674 case REF_STATUS_REJECT_REMOTE_UPDATED
:
675 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
676 "remote ref updated since checkout",
677 report
, porcelain
, summary_width
);
679 case REF_STATUS_REJECT_SHALLOW
:
680 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
681 "new shallow roots not allowed",
682 report
, porcelain
, summary_width
);
684 case REF_STATUS_REMOTE_REJECT
:
685 print_ref_status('!', "[remote rejected]", ref
,
686 ref
->deletion
? NULL
: ref
->peer_ref
,
688 report
, porcelain
, summary_width
);
690 case REF_STATUS_EXPECTING_REPORT
:
691 print_ref_status('!', "[remote failure]", ref
,
692 ref
->deletion
? NULL
: ref
->peer_ref
,
693 "remote failed to report status",
694 report
, porcelain
, summary_width
);
696 case REF_STATUS_ATOMIC_PUSH_FAILED
:
697 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
698 "atomic push failed",
699 report
, porcelain
, summary_width
);
702 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
709 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
710 int porcelain
, int summary_width
)
712 struct ref_push_report
*report
;
716 return print_one_push_report(ref
, dest
, count
,
717 NULL
, porcelain
, summary_width
);
719 for (report
= ref
->report
; report
; report
= report
->next
)
720 print_one_push_report(ref
, dest
, count
+ n
++,
721 report
, porcelain
, summary_width
);
725 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
727 char hex
[GIT_MAX_HEXSZ
+ 1];
728 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
730 return (w
< sofar
) ? sofar
: w
;
733 int transport_summary_width(const struct ref
*refs
)
737 for (; refs
; refs
= refs
->next
) {
738 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
739 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
742 maxw
= FALLBACK_DEFAULT_ABBREV
;
743 return (2 * maxw
+ 3);
746 void transport_print_push_status(const char *dest
, struct ref
*refs
,
747 int verbose
, int porcelain
, unsigned int *reject_reasons
)
752 int summary_width
= transport_summary_width(refs
);
754 if (transport_color_config() < 0)
755 warning(_("could not parse transport.color.* config"));
757 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
760 for (ref
= refs
; ref
; ref
= ref
->next
)
761 if (ref
->status
== REF_STATUS_UPTODATE
)
762 n
+= print_one_push_status(ref
, dest
, n
,
763 porcelain
, summary_width
);
766 for (ref
= refs
; ref
; ref
= ref
->next
)
767 if (ref
->status
== REF_STATUS_OK
)
768 n
+= print_one_push_status(ref
, dest
, n
,
769 porcelain
, summary_width
);
772 for (ref
= refs
; ref
; ref
= ref
->next
) {
773 if (ref
->status
!= REF_STATUS_NONE
&&
774 ref
->status
!= REF_STATUS_UPTODATE
&&
775 ref
->status
!= REF_STATUS_OK
)
776 n
+= print_one_push_status(ref
, dest
, n
,
777 porcelain
, summary_width
);
778 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
779 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
780 *reject_reasons
|= REJECT_NON_FF_HEAD
;
782 *reject_reasons
|= REJECT_NON_FF_OTHER
;
783 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
784 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
785 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
786 *reject_reasons
|= REJECT_FETCH_FIRST
;
787 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
788 *reject_reasons
|= REJECT_NEEDS_FORCE
;
789 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
790 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
796 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
798 struct git_transport_data
*data
= transport
->data
;
799 struct send_pack_args args
;
802 if (transport_color_config() < 0)
805 if (!data
->got_remote_heads
)
806 get_refs_via_connect(transport
, 1, NULL
);
808 memset(&args
, 0, sizeof(args
));
809 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
810 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
811 args
.use_thin_pack
= data
->options
.thin
;
812 args
.verbose
= (transport
->verbose
> 0);
813 args
.quiet
= (transport
->verbose
< 0);
814 args
.progress
= transport
->progress
;
815 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
816 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
817 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
818 args
.push_options
= transport
->push_options
;
819 args
.url
= transport
->url
;
821 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
822 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
823 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
824 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
826 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
828 switch (data
->version
) {
830 die(_("support for protocol v2 not implemented yet"));
834 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
837 case protocol_unknown_version
:
838 BUG("unknown protocol version");
844 * Atomic push may abort the connection early and close the pipe,
845 * which may cause an error for `finish_connect()`. Ignore this error
846 * for atomic git-push.
848 if (ret
|| args
.atomic
)
849 finish_connect(data
->conn
);
851 ret
= finish_connect(data
->conn
);
853 data
->got_remote_heads
= 0;
858 static int connect_git(struct transport
*transport
, const char *name
,
859 const char *executable
, int fd
[2])
861 struct git_transport_data
*data
= transport
->data
;
862 data
->conn
= git_connect(data
->fd
, transport
->url
,
869 static int disconnect_git(struct transport
*transport
)
871 struct git_transport_data
*data
= transport
->data
;
873 if (data
->got_remote_heads
&& !transport
->stateless_rpc
)
874 packet_flush(data
->fd
[1]);
876 if (data
->fd
[1] >= 0)
878 finish_connect(data
->conn
);
885 static struct transport_vtable taken_over_vtable
= {
887 get_refs_via_connect
,
894 void transport_take_over(struct transport
*transport
,
895 struct child_process
*child
)
897 struct git_transport_data
*data
;
899 if (!transport
->smart_options
)
900 BUG("taking over transport requires non-NULL "
901 "smart_options field.");
903 CALLOC_ARRAY(data
, 1);
904 data
->options
= *transport
->smart_options
;
906 data
->fd
[0] = data
->conn
->out
;
907 data
->fd
[1] = data
->conn
->in
;
908 data
->got_remote_heads
= 0;
909 transport
->data
= data
;
911 transport
->vtable
= &taken_over_vtable
;
912 transport
->smart_options
= &(data
->options
);
914 transport
->cannot_reuse
= 1;
917 static int is_file(const char *url
)
922 return S_ISREG(buf
.st_mode
);
925 static int external_specification_len(const char *url
)
927 return strchr(url
, ':') - url
;
930 static const struct string_list
*protocol_whitelist(void)
932 static int enabled
= -1;
933 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
936 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
938 string_list_split(&allowed
, v
, ':', -1);
939 string_list_sort(&allowed
);
946 return enabled
? &allowed
: NULL
;
949 enum protocol_allow_config
{
950 PROTOCOL_ALLOW_NEVER
= 0,
951 PROTOCOL_ALLOW_USER_ONLY
,
952 PROTOCOL_ALLOW_ALWAYS
955 static enum protocol_allow_config
parse_protocol_config(const char *key
,
958 if (!strcasecmp(value
, "always"))
959 return PROTOCOL_ALLOW_ALWAYS
;
960 else if (!strcasecmp(value
, "never"))
961 return PROTOCOL_ALLOW_NEVER
;
962 else if (!strcasecmp(value
, "user"))
963 return PROTOCOL_ALLOW_USER_ONLY
;
965 die(_("unknown value for config '%s': %s"), key
, value
);
968 static enum protocol_allow_config
get_protocol_config(const char *type
)
970 char *key
= xstrfmt("protocol.%s.allow", type
);
973 /* first check the per-protocol config */
974 if (!git_config_get_string(key
, &value
)) {
975 enum protocol_allow_config ret
=
976 parse_protocol_config(key
, value
);
983 /* if defined, fallback to user-defined default for unknown protocols */
984 if (!git_config_get_string("protocol.allow", &value
)) {
985 enum protocol_allow_config ret
=
986 parse_protocol_config("protocol.allow", value
);
991 /* fallback to built-in defaults */
993 if (!strcmp(type
, "http") ||
994 !strcmp(type
, "https") ||
995 !strcmp(type
, "git") ||
996 !strcmp(type
, "ssh") ||
997 !strcmp(type
, "file"))
998 return PROTOCOL_ALLOW_ALWAYS
;
1000 /* known scary; err on the side of caution */
1001 if (!strcmp(type
, "ext"))
1002 return PROTOCOL_ALLOW_NEVER
;
1004 /* unknown; by default let them be used only directly by the user */
1005 return PROTOCOL_ALLOW_USER_ONLY
;
1008 int is_transport_allowed(const char *type
, int from_user
)
1010 const struct string_list
*whitelist
= protocol_whitelist();
1012 return string_list_has_string(whitelist
, type
);
1014 switch (get_protocol_config(type
)) {
1015 case PROTOCOL_ALLOW_ALWAYS
:
1017 case PROTOCOL_ALLOW_NEVER
:
1019 case PROTOCOL_ALLOW_USER_ONLY
:
1021 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1025 BUG("invalid protocol_allow_config type");
1028 void transport_check_allowed(const char *type
)
1030 if (!is_transport_allowed(type
, -1))
1031 die(_("transport '%s' not allowed"), type
);
1034 static struct transport_vtable bundle_vtable
= {
1036 get_refs_from_bundle
,
1037 fetch_refs_from_bundle
,
1043 static struct transport_vtable builtin_smart_vtable
= {
1045 get_refs_via_connect
,
1046 fetch_refs_via_pack
,
1052 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1055 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1057 ret
->progress
= isatty(2);
1058 string_list_init_dup(&ret
->pack_lockfiles
);
1061 BUG("No remote provided to transport_get()");
1063 ret
->got_remote_refs
= 0;
1064 ret
->remote
= remote
;
1065 helper
= remote
->foreign_vcs
;
1067 if (!url
&& remote
->url
)
1068 url
= remote
->url
[0];
1071 /* maybe it is a foreign URL? */
1073 const char *p
= url
;
1075 while (is_urlschemechar(p
== url
, *p
))
1077 if (starts_with(p
, "::"))
1078 helper
= xstrndup(url
, p
- url
);
1082 transport_helper_init(ret
, helper
);
1083 } else if (starts_with(url
, "rsync:")) {
1084 die(_("git-over-rsync is no longer supported"));
1085 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1086 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1087 bundle_header_init(&data
->header
);
1088 transport_check_allowed("file");
1090 ret
->vtable
= &bundle_vtable
;
1091 ret
->smart_options
= NULL
;
1092 } else if (!is_url(url
)
1093 || starts_with(url
, "file://")
1094 || starts_with(url
, "git://")
1095 || starts_with(url
, "ssh://")
1096 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1097 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1100 * These are builtin smart transports; "allowed" transports
1101 * will be checked individually in git_connect.
1103 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1105 ret
->vtable
= &builtin_smart_vtable
;
1106 ret
->smart_options
= &(data
->options
);
1109 data
->got_remote_heads
= 0;
1111 /* Unknown protocol in URL. Pass to external handler. */
1112 int len
= external_specification_len(url
);
1113 char *handler
= xmemdupz(url
, len
);
1114 transport_helper_init(ret
, handler
);
1117 if (ret
->smart_options
) {
1118 ret
->smart_options
->thin
= 1;
1119 ret
->smart_options
->uploadpack
= "git-upload-pack";
1120 if (remote
->uploadpack
)
1121 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1122 ret
->smart_options
->receivepack
= "git-receive-pack";
1123 if (remote
->receivepack
)
1124 ret
->smart_options
->receivepack
= remote
->receivepack
;
1127 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1132 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1134 return transport
->hash_algo
;
1137 int transport_set_option(struct transport
*transport
,
1138 const char *name
, const char *value
)
1140 int git_reports
= 1, protocol_reports
= 1;
1142 if (transport
->smart_options
)
1143 git_reports
= set_git_option(transport
->smart_options
,
1146 if (transport
->vtable
->set_option
)
1147 protocol_reports
= transport
->vtable
->set_option(transport
,
1150 /* If either report is 0, report 0 (success). */
1151 if (!git_reports
|| !protocol_reports
)
1153 /* If either reports -1 (invalid value), report -1. */
1154 if ((git_reports
== -1) || (protocol_reports
== -1))
1156 /* Otherwise if both report unknown, report unknown. */
1160 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1164 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1166 transport
->verbose
= -1;
1169 * Rules used to determine whether to report progress (processing aborts
1170 * when a rule is satisfied):
1172 * . Report progress, if force_progress is 1 (ie. --progress).
1173 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1174 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1175 * . Report progress if isatty(2) is 1.
1177 if (force_progress
>= 0)
1178 transport
->progress
= !!force_progress
;
1180 transport
->progress
= verbosity
>= 0 && isatty(2);
1183 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1187 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1188 "not be found on any remote:\n"));
1189 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1190 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1191 fprintf(stderr
, _("\nPlease try\n\n"
1192 " git push --recurse-submodules=on-demand\n\n"
1193 "or cd to the path and use\n\n"
1195 "to push them to a remote.\n\n"));
1197 string_list_clear(needs_pushing
, 0);
1199 die(_("Aborting."));
1202 static int run_pre_push_hook(struct transport
*transport
,
1203 struct ref
*remote_refs
)
1207 struct child_process proc
= CHILD_PROCESS_INIT
;
1209 const char *argv
[4];
1211 if (!(argv
[0] = find_hook("pre-push")))
1214 argv
[1] = transport
->remote
->name
;
1215 argv
[2] = transport
->url
;
1220 proc
.trace2_hook_name
= "pre-push";
1222 if (start_command(&proc
)) {
1223 finish_command(&proc
);
1227 sigchain_push(SIGPIPE
, SIG_IGN
);
1229 strbuf_init(&buf
, 256);
1231 for (r
= remote_refs
; r
; r
= r
->next
) {
1232 if (!r
->peer_ref
) continue;
1233 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1234 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1235 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1236 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1239 strbuf_addf( &buf
, "%s %s %s %s\n",
1240 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1241 r
->name
, oid_to_hex(&r
->old_oid
));
1243 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1244 /* We do not mind if a hook does not read all refs. */
1251 strbuf_release(&buf
);
1257 sigchain_pop(SIGPIPE
);
1259 x
= finish_command(&proc
);
1266 int transport_push(struct repository
*r
,
1267 struct transport
*transport
,
1268 struct refspec
*rs
, int flags
,
1269 unsigned int *reject_reasons
)
1271 *reject_reasons
= 0;
1273 if (transport_color_config() < 0)
1276 if (transport
->vtable
->push_refs
) {
1277 struct ref
*remote_refs
;
1278 struct ref
*local_refs
= get_local_heads();
1279 int match_flags
= MATCH_REFS_NONE
;
1280 int verbose
= (transport
->verbose
> 0);
1281 int quiet
= (transport
->verbose
< 0);
1282 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1283 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1284 int push_ret
, ret
, err
;
1285 struct transport_ls_refs_options transport_options
=
1286 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1288 if (check_push_refs(local_refs
, rs
) < 0)
1291 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1293 trace2_region_enter("transport_push", "get_refs_list", r
);
1294 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1295 &transport_options
);
1296 trace2_region_leave("transport_push", "get_refs_list", r
);
1298 strvec_clear(&transport_options
.ref_prefixes
);
1300 if (flags
& TRANSPORT_PUSH_ALL
)
1301 match_flags
|= MATCH_REFS_ALL
;
1302 if (flags
& TRANSPORT_PUSH_MIRROR
)
1303 match_flags
|= MATCH_REFS_MIRROR
;
1304 if (flags
& TRANSPORT_PUSH_PRUNE
)
1305 match_flags
|= MATCH_REFS_PRUNE
;
1306 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1307 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1309 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1312 if (transport
->smart_options
&&
1313 transport
->smart_options
->cas
&&
1314 !is_empty_cas(transport
->smart_options
->cas
))
1315 apply_push_cas(transport
->smart_options
->cas
,
1316 transport
->remote
, remote_refs
);
1318 set_ref_status_for_push(remote_refs
,
1319 flags
& TRANSPORT_PUSH_MIRROR
,
1320 flags
& TRANSPORT_PUSH_FORCE
);
1322 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1323 if (run_pre_push_hook(transport
, remote_refs
))
1326 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1327 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1328 !is_bare_repository()) {
1329 struct ref
*ref
= remote_refs
;
1330 struct oid_array commits
= OID_ARRAY_INIT
;
1332 trace2_region_enter("transport_push", "push_submodules", r
);
1333 for (; ref
; ref
= ref
->next
)
1334 if (!is_null_oid(&ref
->new_oid
))
1335 oid_array_append(&commits
,
1338 if (!push_unpushed_submodules(r
,
1342 transport
->push_options
,
1344 oid_array_clear(&commits
);
1345 trace2_region_leave("transport_push", "push_submodules", r
);
1346 die(_("failed to push all needed submodules"));
1348 oid_array_clear(&commits
);
1349 trace2_region_leave("transport_push", "push_submodules", r
);
1352 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1353 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1354 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1355 !pretend
)) && !is_bare_repository()) {
1356 struct ref
*ref
= remote_refs
;
1357 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1358 struct oid_array commits
= OID_ARRAY_INIT
;
1360 trace2_region_enter("transport_push", "check_submodules", r
);
1361 for (; ref
; ref
= ref
->next
)
1362 if (!is_null_oid(&ref
->new_oid
))
1363 oid_array_append(&commits
,
1366 if (find_unpushed_submodules(r
,
1368 transport
->remote
->name
,
1370 oid_array_clear(&commits
);
1371 trace2_region_leave("transport_push", "check_submodules", r
);
1372 die_with_unpushed_submodules(&needs_pushing
);
1374 string_list_clear(&needs_pushing
, 0);
1375 oid_array_clear(&commits
);
1376 trace2_region_leave("transport_push", "check_submodules", r
);
1379 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1380 trace2_region_enter("transport_push", "push_refs", r
);
1381 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1382 trace2_region_leave("transport_push", "push_refs", r
);
1385 err
= push_had_errors(remote_refs
);
1386 ret
= push_ret
| err
;
1389 transport_print_push_status(transport
->url
, remote_refs
,
1390 verbose
| porcelain
, porcelain
,
1393 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1394 set_upstreams(transport
, remote_refs
, pretend
);
1396 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1397 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1399 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1400 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1403 if (porcelain
&& !push_ret
)
1405 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1406 fprintf(stderr
, "Everything up-to-date\n");
1413 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1414 struct transport_ls_refs_options
*transport_options
)
1416 if (!transport
->got_remote_refs
) {
1417 transport
->remote_refs
=
1418 transport
->vtable
->get_refs_list(transport
, 0,
1420 transport
->got_remote_refs
= 1;
1423 return transport
->remote_refs
;
1426 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1429 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1430 struct ref
**heads
= NULL
;
1433 for (rm
= refs
; rm
; rm
= rm
->next
) {
1436 !is_null_oid(&rm
->old_oid
) &&
1437 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1439 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1440 heads
[nr_heads
++] = rm
;
1445 * When deepening of a shallow repository is requested,
1446 * then local and remote refs are likely to still be equal.
1447 * Just feed them all to the fetch method in that case.
1448 * This condition shouldn't be met in a non-deepening fetch
1449 * (see builtin/fetch.c:quickfetch()).
1451 ALLOC_ARRAY(heads
, nr_refs
);
1452 for (rm
= refs
; rm
; rm
= rm
->next
)
1453 heads
[nr_heads
++] = rm
;
1456 rc
= transport
->vtable
->fetch(transport
, nr_heads
, heads
);
1462 void transport_unlock_pack(struct transport
*transport
)
1466 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1467 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1468 string_list_clear(&transport
->pack_lockfiles
, 0);
1471 int transport_connect(struct transport
*transport
, const char *name
,
1472 const char *exec
, int fd
[2])
1474 if (transport
->vtable
->connect
)
1475 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1477 die(_("operation not supported by protocol"));
1480 int transport_disconnect(struct transport
*transport
)
1483 if (transport
->vtable
->disconnect
)
1484 ret
= transport
->vtable
->disconnect(transport
);
1485 if (transport
->got_remote_refs
)
1486 free_refs((void *)transport
->remote_refs
);
1492 * Strip username (and password) from a URL and return
1493 * it in a newly allocated string.
1495 char *transport_anonymize_url(const char *url
)
1497 char *scheme_prefix
, *anon_part
;
1498 size_t anon_len
, prefix_len
= 0;
1500 anon_part
= strchr(url
, '@');
1501 if (url_is_local_not_ssh(url
) || !anon_part
)
1504 anon_len
= strlen(++anon_part
);
1505 scheme_prefix
= strstr(url
, "://");
1506 if (!scheme_prefix
) {
1507 if (!strchr(anon_part
, ':'))
1508 /* cannot be "me@there:/path/name" */
1512 /* make sure scheme is reasonable */
1513 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1516 case '+': case '.': case '-':
1525 /* @ past the first slash does not count */
1526 cp
= strchr(scheme_prefix
+ 3, '/');
1527 if (cp
&& cp
< anon_part
)
1529 prefix_len
= scheme_prefix
- url
+ 3;
1531 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1532 (int)anon_len
, anon_part
);
1534 return xstrdup(url
);