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
;
165 struct strvec extra_index_pack_args
= STRVEC_INIT
;
168 if (transport
->progress
)
169 strvec_push(&extra_index_pack_args
, "-v");
171 if (!data
->get_refs_from_bundle_called
)
172 get_refs_from_bundle(transport
, 0, NULL
);
173 ret
= unbundle(the_repository
, &data
->header
, data
->fd
,
174 &extra_index_pack_args
);
175 transport
->hash_algo
= data
->header
.hash_algo
;
179 static int close_bundle(struct transport
*transport
)
181 struct bundle_transport_data
*data
= transport
->data
;
184 bundle_header_release(&data
->header
);
189 struct git_transport_data
{
190 struct git_transport_options options
;
191 struct child_process
*conn
;
193 unsigned got_remote_heads
: 1;
194 enum protocol_version version
;
195 struct oid_array extra_have
;
196 struct oid_array shallow
;
199 static int set_git_option(struct git_transport_options
*opts
,
200 const char *name
, const char *value
)
202 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
203 opts
->uploadpack
= value
;
205 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
206 opts
->receivepack
= value
;
208 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
209 opts
->thin
= !!value
;
211 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
212 opts
->followtags
= !!value
;
214 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
215 opts
->keep
= !!value
;
217 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
218 opts
->update_shallow
= !!value
;
220 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
225 opts
->depth
= strtol(value
, &end
, 0);
227 die(_("transport: invalid depth option '%s'"), value
);
230 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
231 opts
->deepen_since
= value
;
233 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
234 opts
->deepen_not
= (const struct string_list
*)value
;
236 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
237 opts
->deepen_relative
= !!value
;
239 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
240 opts
->from_promisor
= !!value
;
242 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
243 list_objects_filter_die_if_populated(&opts
->filter_options
);
244 parse_list_objects_filter(&opts
->filter_options
, value
);
246 } else if (!strcmp(name
, TRANS_OPT_REJECT_SHALLOW
)) {
247 opts
->reject_shallow
= !!value
;
253 static int connect_setup(struct transport
*transport
, int for_push
)
255 struct git_transport_data
*data
= transport
->data
;
256 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
261 switch (transport
->family
) {
262 case TRANSPORT_FAMILY_ALL
: break;
263 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
264 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
267 data
->conn
= git_connect(data
->fd
, transport
->url
,
268 for_push
? data
->options
.receivepack
:
269 data
->options
.uploadpack
,
275 static void die_if_server_options(struct transport
*transport
)
277 if (!transport
->server_options
|| !transport
->server_options
->nr
)
279 advise(_("see protocol.version in 'git help config' for more details"));
280 die(_("server options require protocol version 2 or later"));
284 * Obtains the protocol version from the transport and writes it to
285 * transport->data->version, first connecting if not already connected.
287 * If the protocol version is one that allows skipping the listing of remote
288 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
289 * this function returns NULL. Otherwise, this function returns the list of
292 static struct ref
*handshake(struct transport
*transport
, int for_push
,
293 struct transport_ls_refs_options
*options
,
296 struct git_transport_data
*data
= transport
->data
;
297 struct ref
*refs
= NULL
;
298 struct packet_reader reader
;
300 const char *server_sid
;
302 connect_setup(transport
, for_push
);
304 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
305 PACKET_READ_CHOMP_NEWLINE
|
306 PACKET_READ_GENTLE_ON_EOF
|
307 PACKET_READ_DIE_ON_ERR_PACKET
);
309 data
->version
= discover_version(&reader
);
310 switch (data
->version
) {
312 if (server_feature_v2("session-id", &server_sid
))
313 trace2_data_string("transfer", NULL
, "server-sid", server_sid
);
315 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
317 transport
->server_options
,
318 transport
->stateless_rpc
);
322 die_if_server_options(transport
);
323 get_remote_heads(&reader
, &refs
,
324 for_push
? REF_NORMAL
: 0,
327 server_sid
= server_feature_value("session-id", &sid_len
);
329 char *sid
= xstrndup(server_sid
, sid_len
);
330 trace2_data_string("transfer", NULL
, "server-sid", sid
);
334 case protocol_unknown_version
:
335 BUG("unknown protocol version");
337 data
->got_remote_heads
= 1;
338 transport
->hash_algo
= reader
.hash_algo
;
340 if (reader
.line_peeked
)
341 BUG("buffer must be empty at the end of handshake()");
346 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
347 struct transport_ls_refs_options
*options
)
349 return handshake(transport
, for_push
, options
, 1);
352 static int fetch_refs_via_pack(struct transport
*transport
,
353 int nr_heads
, struct ref
**to_fetch
)
356 struct git_transport_data
*data
= transport
->data
;
357 struct ref
*refs
= NULL
;
358 struct fetch_pack_args args
;
359 struct ref
*refs_tmp
= NULL
;
361 memset(&args
, 0, sizeof(args
));
362 args
.uploadpack
= data
->options
.uploadpack
;
363 args
.keep_pack
= data
->options
.keep
;
365 args
.use_thin_pack
= data
->options
.thin
;
366 args
.include_tag
= data
->options
.followtags
;
367 args
.verbose
= (transport
->verbose
> 1);
368 args
.quiet
= (transport
->verbose
< 0);
369 args
.no_progress
= !transport
->progress
;
370 args
.depth
= data
->options
.depth
;
371 args
.deepen_since
= data
->options
.deepen_since
;
372 args
.deepen_not
= data
->options
.deepen_not
;
373 args
.deepen_relative
= data
->options
.deepen_relative
;
374 args
.check_self_contained_and_connected
=
375 data
->options
.check_self_contained_and_connected
;
376 args
.cloning
= transport
->cloning
;
377 args
.update_shallow
= data
->options
.update_shallow
;
378 args
.from_promisor
= data
->options
.from_promisor
;
379 args
.filter_options
= data
->options
.filter_options
;
380 args
.stateless_rpc
= transport
->stateless_rpc
;
381 args
.server_options
= transport
->server_options
;
382 args
.negotiation_tips
= data
->options
.negotiation_tips
;
383 args
.reject_shallow_remote
= transport
->smart_options
->reject_shallow
;
385 if (!data
->got_remote_heads
) {
387 int must_list_refs
= 0;
388 for (i
= 0; i
< nr_heads
; i
++) {
389 if (!to_fetch
[i
]->exact_oid
) {
394 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
397 if (data
->version
== protocol_unknown_version
)
398 BUG("unknown protocol version");
399 else if (data
->version
<= protocol_v1
)
400 die_if_server_options(transport
);
402 if (data
->options
.acked_commits
) {
403 if (data
->version
< protocol_v2
) {
404 warning(_("--negotiate-only requires protocol v2"));
406 } else if (!server_supports_feature("fetch", "wait-for-done", 0)) {
407 warning(_("server does not support wait-for-done"));
410 negotiate_using_fetch(data
->options
.negotiation_tips
,
411 transport
->server_options
,
412 transport
->stateless_rpc
,
414 data
->options
.acked_commits
);
420 refs
= fetch_pack(&args
, data
->fd
,
421 refs_tmp
? refs_tmp
: transport
->remote_refs
,
422 to_fetch
, nr_heads
, &data
->shallow
,
423 &transport
->pack_lockfiles
, data
->version
);
425 data
->got_remote_heads
= 0;
426 data
->options
.self_contained_and_connected
=
427 args
.self_contained_and_connected
;
428 data
->options
.connectivity_checked
= args
.connectivity_checked
;
432 if (report_unmatched_refs(to_fetch
, nr_heads
))
437 if (data
->fd
[1] >= 0)
439 if (finish_connect(data
->conn
))
448 static int push_had_errors(struct ref
*ref
)
450 for (; ref
; ref
= ref
->next
) {
451 switch (ref
->status
) {
452 case REF_STATUS_NONE
:
453 case REF_STATUS_UPTODATE
:
463 int transport_refs_pushed(struct ref
*ref
)
465 for (; ref
; ref
= ref
->next
) {
466 switch(ref
->status
) {
467 case REF_STATUS_NONE
:
468 case REF_STATUS_UPTODATE
:
477 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
478 struct object_id
*new_oid
, int deletion
,
481 struct refspec_item rs
;
483 memset(&rs
, 0, sizeof(rs
));
487 if (!remote_find_tracking(remote
, &rs
)) {
489 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
491 delete_ref(NULL
, rs
.dst
, NULL
, 0);
493 update_ref("update by push", rs
.dst
, new_oid
,
499 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
502 struct object_id
*new_oid
;
503 struct ref_push_report
*report
;
505 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
508 report
= ref
->report
;
510 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
511 ref
->deletion
, verbose
);
513 for (; report
; report
= report
->next
) {
514 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
515 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
516 update_one_tracking_ref(remote
, refname
, new_oid
,
517 is_null_oid(new_oid
), verbose
);
521 static void print_ref_status(char flag
, const char *summary
,
522 struct ref
*to
, struct ref
*from
, const char *msg
,
523 struct ref_push_report
*report
,
524 int porcelain
, int summary_width
)
528 if (report
&& report
->ref_name
)
529 to_name
= report
->ref_name
;
535 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
537 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
539 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
541 fprintf(stdout
, "%s\n", summary
);
543 const char *red
= "", *reset
= "";
544 if (push_had_errors(to
)) {
545 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
546 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
548 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
551 fprintf(stderr
, "%s -> %s",
552 prettify_refname(from
->name
),
553 prettify_refname(to_name
));
555 fputs(prettify_refname(to_name
), stderr
);
565 static void print_ok_ref_status(struct ref
*ref
,
566 struct ref_push_report
*report
,
567 int porcelain
, int summary_width
)
569 struct object_id
*old_oid
;
570 struct object_id
*new_oid
;
571 const char *ref_name
;
574 if (report
&& report
->old_oid
)
575 old_oid
= report
->old_oid
;
577 old_oid
= &ref
->old_oid
;
578 if (report
&& report
->new_oid
)
579 new_oid
= report
->new_oid
;
581 new_oid
= &ref
->new_oid
;
582 if (report
&& report
->forced_update
)
583 forced_update
= report
->forced_update
;
585 forced_update
= ref
->forced_update
;
586 if (report
&& report
->ref_name
)
587 ref_name
= report
->ref_name
;
589 ref_name
= ref
->name
;
592 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
593 report
, porcelain
, summary_width
);
594 else if (is_null_oid(old_oid
))
595 print_ref_status('*',
596 (starts_with(ref_name
, "refs/tags/")
598 : (starts_with(ref_name
, "refs/heads/")
600 : "[new reference]")),
601 ref
, ref
->peer_ref
, NULL
,
602 report
, porcelain
, summary_width
);
604 struct strbuf quickref
= STRBUF_INIT
;
608 strbuf_add_unique_abbrev(&quickref
, old_oid
,
611 strbuf_addstr(&quickref
, "...");
613 msg
= "forced update";
615 strbuf_addstr(&quickref
, "..");
619 strbuf_add_unique_abbrev(&quickref
, new_oid
,
622 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
623 report
, porcelain
, summary_width
);
624 strbuf_release(&quickref
);
628 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
629 struct ref_push_report
*report
,
630 int porcelain
, int summary_width
)
633 char *url
= transport_anonymize_url(dest
);
634 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
638 switch(ref
->status
) {
639 case REF_STATUS_NONE
:
640 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
641 report
, porcelain
, summary_width
);
643 case REF_STATUS_REJECT_NODELETE
:
644 print_ref_status('!', "[rejected]", ref
, NULL
,
645 "remote does not support deleting refs",
646 report
, porcelain
, summary_width
);
648 case REF_STATUS_UPTODATE
:
649 print_ref_status('=', "[up to date]", ref
,
651 report
, porcelain
, summary_width
);
653 case REF_STATUS_REJECT_NONFASTFORWARD
:
654 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
656 report
, porcelain
, summary_width
);
658 case REF_STATUS_REJECT_ALREADY_EXISTS
:
659 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
661 report
, porcelain
, summary_width
);
663 case REF_STATUS_REJECT_FETCH_FIRST
:
664 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
666 report
, porcelain
, summary_width
);
668 case REF_STATUS_REJECT_NEEDS_FORCE
:
669 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
671 report
, porcelain
, summary_width
);
673 case REF_STATUS_REJECT_STALE
:
674 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
676 report
, porcelain
, summary_width
);
678 case REF_STATUS_REJECT_REMOTE_UPDATED
:
679 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
680 "remote ref updated since checkout",
681 report
, porcelain
, summary_width
);
683 case REF_STATUS_REJECT_SHALLOW
:
684 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
685 "new shallow roots not allowed",
686 report
, porcelain
, summary_width
);
688 case REF_STATUS_REMOTE_REJECT
:
689 print_ref_status('!', "[remote rejected]", ref
,
690 ref
->deletion
? NULL
: ref
->peer_ref
,
692 report
, porcelain
, summary_width
);
694 case REF_STATUS_EXPECTING_REPORT
:
695 print_ref_status('!', "[remote failure]", ref
,
696 ref
->deletion
? NULL
: ref
->peer_ref
,
697 "remote failed to report status",
698 report
, porcelain
, summary_width
);
700 case REF_STATUS_ATOMIC_PUSH_FAILED
:
701 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
702 "atomic push failed",
703 report
, porcelain
, summary_width
);
706 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
713 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
714 int porcelain
, int summary_width
)
716 struct ref_push_report
*report
;
720 return print_one_push_report(ref
, dest
, count
,
721 NULL
, porcelain
, summary_width
);
723 for (report
= ref
->report
; report
; report
= report
->next
)
724 print_one_push_report(ref
, dest
, count
+ n
++,
725 report
, porcelain
, summary_width
);
729 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
731 char hex
[GIT_MAX_HEXSZ
+ 1];
732 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
734 return (w
< sofar
) ? sofar
: w
;
737 int transport_summary_width(const struct ref
*refs
)
741 for (; refs
; refs
= refs
->next
) {
742 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
743 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
746 maxw
= FALLBACK_DEFAULT_ABBREV
;
747 return (2 * maxw
+ 3);
750 void transport_print_push_status(const char *dest
, struct ref
*refs
,
751 int verbose
, int porcelain
, unsigned int *reject_reasons
)
756 int summary_width
= transport_summary_width(refs
);
758 if (transport_color_config() < 0)
759 warning(_("could not parse transport.color.* config"));
761 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
764 for (ref
= refs
; ref
; ref
= ref
->next
)
765 if (ref
->status
== REF_STATUS_UPTODATE
)
766 n
+= print_one_push_status(ref
, dest
, n
,
767 porcelain
, summary_width
);
770 for (ref
= refs
; ref
; ref
= ref
->next
)
771 if (ref
->status
== REF_STATUS_OK
)
772 n
+= print_one_push_status(ref
, dest
, n
,
773 porcelain
, summary_width
);
776 for (ref
= refs
; ref
; ref
= ref
->next
) {
777 if (ref
->status
!= REF_STATUS_NONE
&&
778 ref
->status
!= REF_STATUS_UPTODATE
&&
779 ref
->status
!= REF_STATUS_OK
)
780 n
+= print_one_push_status(ref
, dest
, n
,
781 porcelain
, summary_width
);
782 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
783 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
784 *reject_reasons
|= REJECT_NON_FF_HEAD
;
786 *reject_reasons
|= REJECT_NON_FF_OTHER
;
787 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
788 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
789 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
790 *reject_reasons
|= REJECT_FETCH_FIRST
;
791 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
792 *reject_reasons
|= REJECT_NEEDS_FORCE
;
793 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
794 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
800 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
802 struct git_transport_data
*data
= transport
->data
;
803 struct send_pack_args args
;
806 if (transport_color_config() < 0)
809 if (!data
->got_remote_heads
)
810 get_refs_via_connect(transport
, 1, NULL
);
812 memset(&args
, 0, sizeof(args
));
813 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
814 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
815 args
.use_thin_pack
= data
->options
.thin
;
816 args
.verbose
= (transport
->verbose
> 0);
817 args
.quiet
= (transport
->verbose
< 0);
818 args
.progress
= transport
->progress
;
819 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
820 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
821 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
822 args
.push_options
= transport
->push_options
;
823 args
.url
= transport
->url
;
825 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
826 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
827 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
828 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
830 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
832 switch (data
->version
) {
834 die(_("support for protocol v2 not implemented yet"));
838 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
841 case protocol_unknown_version
:
842 BUG("unknown protocol version");
848 * Atomic push may abort the connection early and close the pipe,
849 * which may cause an error for `finish_connect()`. Ignore this error
850 * for atomic git-push.
852 if (ret
|| args
.atomic
)
853 finish_connect(data
->conn
);
855 ret
= finish_connect(data
->conn
);
857 data
->got_remote_heads
= 0;
862 static int connect_git(struct transport
*transport
, const char *name
,
863 const char *executable
, int fd
[2])
865 struct git_transport_data
*data
= transport
->data
;
866 data
->conn
= git_connect(data
->fd
, transport
->url
,
873 static int disconnect_git(struct transport
*transport
)
875 struct git_transport_data
*data
= transport
->data
;
877 if (data
->got_remote_heads
&& !transport
->stateless_rpc
)
878 packet_flush(data
->fd
[1]);
880 if (data
->fd
[1] >= 0)
882 finish_connect(data
->conn
);
889 static struct transport_vtable taken_over_vtable
= {
890 .get_refs_list
= get_refs_via_connect
,
891 .fetch_refs
= fetch_refs_via_pack
,
892 .push_refs
= git_transport_push
,
893 .disconnect
= disconnect_git
896 void transport_take_over(struct transport
*transport
,
897 struct child_process
*child
)
899 struct git_transport_data
*data
;
901 if (!transport
->smart_options
)
902 BUG("taking over transport requires non-NULL "
903 "smart_options field.");
905 CALLOC_ARRAY(data
, 1);
906 data
->options
= *transport
->smart_options
;
908 data
->fd
[0] = data
->conn
->out
;
909 data
->fd
[1] = data
->conn
->in
;
910 data
->got_remote_heads
= 0;
911 transport
->data
= data
;
913 transport
->vtable
= &taken_over_vtable
;
914 transport
->smart_options
= &(data
->options
);
916 transport
->cannot_reuse
= 1;
919 static int is_file(const char *url
)
924 return S_ISREG(buf
.st_mode
);
927 static int external_specification_len(const char *url
)
929 return strchr(url
, ':') - url
;
932 static const struct string_list
*protocol_whitelist(void)
934 static int enabled
= -1;
935 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
938 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
940 string_list_split(&allowed
, v
, ':', -1);
941 string_list_sort(&allowed
);
948 return enabled
? &allowed
: NULL
;
951 enum protocol_allow_config
{
952 PROTOCOL_ALLOW_NEVER
= 0,
953 PROTOCOL_ALLOW_USER_ONLY
,
954 PROTOCOL_ALLOW_ALWAYS
957 static enum protocol_allow_config
parse_protocol_config(const char *key
,
960 if (!strcasecmp(value
, "always"))
961 return PROTOCOL_ALLOW_ALWAYS
;
962 else if (!strcasecmp(value
, "never"))
963 return PROTOCOL_ALLOW_NEVER
;
964 else if (!strcasecmp(value
, "user"))
965 return PROTOCOL_ALLOW_USER_ONLY
;
967 die(_("unknown value for config '%s': %s"), key
, value
);
970 static enum protocol_allow_config
get_protocol_config(const char *type
)
972 char *key
= xstrfmt("protocol.%s.allow", type
);
975 /* first check the per-protocol config */
976 if (!git_config_get_string(key
, &value
)) {
977 enum protocol_allow_config ret
=
978 parse_protocol_config(key
, value
);
985 /* if defined, fallback to user-defined default for unknown protocols */
986 if (!git_config_get_string("protocol.allow", &value
)) {
987 enum protocol_allow_config ret
=
988 parse_protocol_config("protocol.allow", value
);
993 /* fallback to built-in defaults */
995 if (!strcmp(type
, "http") ||
996 !strcmp(type
, "https") ||
997 !strcmp(type
, "git") ||
998 !strcmp(type
, "ssh") ||
999 !strcmp(type
, "file"))
1000 return PROTOCOL_ALLOW_ALWAYS
;
1002 /* known scary; err on the side of caution */
1003 if (!strcmp(type
, "ext"))
1004 return PROTOCOL_ALLOW_NEVER
;
1006 /* unknown; by default let them be used only directly by the user */
1007 return PROTOCOL_ALLOW_USER_ONLY
;
1010 int is_transport_allowed(const char *type
, int from_user
)
1012 const struct string_list
*whitelist
= protocol_whitelist();
1014 return string_list_has_string(whitelist
, type
);
1016 switch (get_protocol_config(type
)) {
1017 case PROTOCOL_ALLOW_ALWAYS
:
1019 case PROTOCOL_ALLOW_NEVER
:
1021 case PROTOCOL_ALLOW_USER_ONLY
:
1023 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1027 BUG("invalid protocol_allow_config type");
1030 void transport_check_allowed(const char *type
)
1032 if (!is_transport_allowed(type
, -1))
1033 die(_("transport '%s' not allowed"), type
);
1036 static struct transport_vtable bundle_vtable
= {
1037 .get_refs_list
= get_refs_from_bundle
,
1038 .fetch_refs
= fetch_refs_from_bundle
,
1039 .disconnect
= close_bundle
1042 static struct transport_vtable builtin_smart_vtable
= {
1043 .get_refs_list
= get_refs_via_connect
,
1044 .fetch_refs
= fetch_refs_via_pack
,
1045 .push_refs
= git_transport_push
,
1046 .connect
= connect_git
,
1047 .disconnect
= disconnect_git
1050 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1053 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1055 ret
->progress
= isatty(2);
1056 string_list_init_dup(&ret
->pack_lockfiles
);
1059 BUG("No remote provided to transport_get()");
1061 ret
->got_remote_refs
= 0;
1062 ret
->remote
= remote
;
1063 helper
= remote
->foreign_vcs
;
1065 if (!url
&& remote
->url
)
1066 url
= remote
->url
[0];
1069 /* maybe it is a foreign URL? */
1071 const char *p
= url
;
1073 while (is_urlschemechar(p
== url
, *p
))
1075 if (starts_with(p
, "::"))
1076 helper
= xstrndup(url
, p
- url
);
1080 transport_helper_init(ret
, helper
);
1081 } else if (starts_with(url
, "rsync:")) {
1082 die(_("git-over-rsync is no longer supported"));
1083 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1084 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1085 bundle_header_init(&data
->header
);
1086 transport_check_allowed("file");
1088 ret
->vtable
= &bundle_vtable
;
1089 ret
->smart_options
= NULL
;
1090 } else if (!is_url(url
)
1091 || starts_with(url
, "file://")
1092 || starts_with(url
, "git://")
1093 || starts_with(url
, "ssh://")
1094 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1095 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1098 * These are builtin smart transports; "allowed" transports
1099 * will be checked individually in git_connect.
1101 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1103 ret
->vtable
= &builtin_smart_vtable
;
1104 ret
->smart_options
= &(data
->options
);
1107 data
->got_remote_heads
= 0;
1109 /* Unknown protocol in URL. Pass to external handler. */
1110 int len
= external_specification_len(url
);
1111 char *handler
= xmemdupz(url
, len
);
1112 transport_helper_init(ret
, handler
);
1115 if (ret
->smart_options
) {
1116 ret
->smart_options
->thin
= 1;
1117 ret
->smart_options
->uploadpack
= "git-upload-pack";
1118 if (remote
->uploadpack
)
1119 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1120 ret
->smart_options
->receivepack
= "git-receive-pack";
1121 if (remote
->receivepack
)
1122 ret
->smart_options
->receivepack
= remote
->receivepack
;
1125 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1130 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1132 return transport
->hash_algo
;
1135 int transport_set_option(struct transport
*transport
,
1136 const char *name
, const char *value
)
1138 int git_reports
= 1, protocol_reports
= 1;
1140 if (transport
->smart_options
)
1141 git_reports
= set_git_option(transport
->smart_options
,
1144 if (transport
->vtable
->set_option
)
1145 protocol_reports
= transport
->vtable
->set_option(transport
,
1148 /* If either report is 0, report 0 (success). */
1149 if (!git_reports
|| !protocol_reports
)
1151 /* If either reports -1 (invalid value), report -1. */
1152 if ((git_reports
== -1) || (protocol_reports
== -1))
1154 /* Otherwise if both report unknown, report unknown. */
1158 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1162 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1164 transport
->verbose
= -1;
1167 * Rules used to determine whether to report progress (processing aborts
1168 * when a rule is satisfied):
1170 * . Report progress, if force_progress is 1 (ie. --progress).
1171 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1172 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1173 * . Report progress if isatty(2) is 1.
1175 if (force_progress
>= 0)
1176 transport
->progress
= !!force_progress
;
1178 transport
->progress
= verbosity
>= 0 && isatty(2);
1181 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1185 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1186 "not be found on any remote:\n"));
1187 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1188 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1189 fprintf(stderr
, _("\nPlease try\n\n"
1190 " git push --recurse-submodules=on-demand\n\n"
1191 "or cd to the path and use\n\n"
1193 "to push them to a remote.\n\n"));
1195 string_list_clear(needs_pushing
, 0);
1197 die(_("Aborting."));
1200 static int run_pre_push_hook(struct transport
*transport
,
1201 struct ref
*remote_refs
)
1205 struct child_process proc
= CHILD_PROCESS_INIT
;
1207 const char *argv
[4];
1209 if (!(argv
[0] = find_hook("pre-push")))
1212 argv
[1] = transport
->remote
->name
;
1213 argv
[2] = transport
->url
;
1218 proc
.trace2_hook_name
= "pre-push";
1220 if (start_command(&proc
)) {
1221 finish_command(&proc
);
1225 sigchain_push(SIGPIPE
, SIG_IGN
);
1227 strbuf_init(&buf
, 256);
1229 for (r
= remote_refs
; r
; r
= r
->next
) {
1230 if (!r
->peer_ref
) continue;
1231 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1232 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1233 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1234 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1237 strbuf_addf( &buf
, "%s %s %s %s\n",
1238 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1239 r
->name
, oid_to_hex(&r
->old_oid
));
1241 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1242 /* We do not mind if a hook does not read all refs. */
1249 strbuf_release(&buf
);
1255 sigchain_pop(SIGPIPE
);
1257 x
= finish_command(&proc
);
1264 int transport_push(struct repository
*r
,
1265 struct transport
*transport
,
1266 struct refspec
*rs
, int flags
,
1267 unsigned int *reject_reasons
)
1269 *reject_reasons
= 0;
1271 if (transport_color_config() < 0)
1274 if (transport
->vtable
->push_refs
) {
1275 struct ref
*remote_refs
;
1276 struct ref
*local_refs
= get_local_heads();
1277 int match_flags
= MATCH_REFS_NONE
;
1278 int verbose
= (transport
->verbose
> 0);
1279 int quiet
= (transport
->verbose
< 0);
1280 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1281 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1282 int push_ret
, ret
, err
;
1283 struct transport_ls_refs_options transport_options
=
1284 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1286 if (check_push_refs(local_refs
, rs
) < 0)
1289 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1291 trace2_region_enter("transport_push", "get_refs_list", r
);
1292 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1293 &transport_options
);
1294 trace2_region_leave("transport_push", "get_refs_list", r
);
1296 strvec_clear(&transport_options
.ref_prefixes
);
1298 if (flags
& TRANSPORT_PUSH_ALL
)
1299 match_flags
|= MATCH_REFS_ALL
;
1300 if (flags
& TRANSPORT_PUSH_MIRROR
)
1301 match_flags
|= MATCH_REFS_MIRROR
;
1302 if (flags
& TRANSPORT_PUSH_PRUNE
)
1303 match_flags
|= MATCH_REFS_PRUNE
;
1304 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1305 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1307 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1310 if (transport
->smart_options
&&
1311 transport
->smart_options
->cas
&&
1312 !is_empty_cas(transport
->smart_options
->cas
))
1313 apply_push_cas(transport
->smart_options
->cas
,
1314 transport
->remote
, remote_refs
);
1316 set_ref_status_for_push(remote_refs
,
1317 flags
& TRANSPORT_PUSH_MIRROR
,
1318 flags
& TRANSPORT_PUSH_FORCE
);
1320 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1321 if (run_pre_push_hook(transport
, remote_refs
))
1324 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1325 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1326 !is_bare_repository()) {
1327 struct ref
*ref
= remote_refs
;
1328 struct oid_array commits
= OID_ARRAY_INIT
;
1330 trace2_region_enter("transport_push", "push_submodules", r
);
1331 for (; ref
; ref
= ref
->next
)
1332 if (!is_null_oid(&ref
->new_oid
))
1333 oid_array_append(&commits
,
1336 if (!push_unpushed_submodules(r
,
1340 transport
->push_options
,
1342 oid_array_clear(&commits
);
1343 trace2_region_leave("transport_push", "push_submodules", r
);
1344 die(_("failed to push all needed submodules"));
1346 oid_array_clear(&commits
);
1347 trace2_region_leave("transport_push", "push_submodules", r
);
1350 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1351 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1352 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1353 !pretend
)) && !is_bare_repository()) {
1354 struct ref
*ref
= remote_refs
;
1355 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1356 struct oid_array commits
= OID_ARRAY_INIT
;
1358 trace2_region_enter("transport_push", "check_submodules", r
);
1359 for (; ref
; ref
= ref
->next
)
1360 if (!is_null_oid(&ref
->new_oid
))
1361 oid_array_append(&commits
,
1364 if (find_unpushed_submodules(r
,
1366 transport
->remote
->name
,
1368 oid_array_clear(&commits
);
1369 trace2_region_leave("transport_push", "check_submodules", r
);
1370 die_with_unpushed_submodules(&needs_pushing
);
1372 string_list_clear(&needs_pushing
, 0);
1373 oid_array_clear(&commits
);
1374 trace2_region_leave("transport_push", "check_submodules", r
);
1377 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1378 trace2_region_enter("transport_push", "push_refs", r
);
1379 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1380 trace2_region_leave("transport_push", "push_refs", r
);
1383 err
= push_had_errors(remote_refs
);
1384 ret
= push_ret
| err
;
1387 transport_print_push_status(transport
->url
, remote_refs
,
1388 verbose
| porcelain
, porcelain
,
1391 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1392 set_upstreams(transport
, remote_refs
, pretend
);
1394 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1395 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1397 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1398 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1401 if (porcelain
&& !push_ret
)
1403 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1404 fprintf(stderr
, "Everything up-to-date\n");
1411 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1412 struct transport_ls_refs_options
*transport_options
)
1414 if (!transport
->got_remote_refs
) {
1415 transport
->remote_refs
=
1416 transport
->vtable
->get_refs_list(transport
, 0,
1418 transport
->got_remote_refs
= 1;
1421 return transport
->remote_refs
;
1424 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1427 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1428 struct ref
**heads
= NULL
;
1431 for (rm
= refs
; rm
; rm
= rm
->next
) {
1434 !is_null_oid(&rm
->old_oid
) &&
1435 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1437 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1438 heads
[nr_heads
++] = rm
;
1443 * When deepening of a shallow repository is requested,
1444 * then local and remote refs are likely to still be equal.
1445 * Just feed them all to the fetch method in that case.
1446 * This condition shouldn't be met in a non-deepening fetch
1447 * (see builtin/fetch.c:quickfetch()).
1449 ALLOC_ARRAY(heads
, nr_refs
);
1450 for (rm
= refs
; rm
; rm
= rm
->next
)
1451 heads
[nr_heads
++] = rm
;
1454 rc
= transport
->vtable
->fetch_refs(transport
, nr_heads
, heads
);
1460 void transport_unlock_pack(struct transport
*transport
)
1464 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1465 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1466 string_list_clear(&transport
->pack_lockfiles
, 0);
1469 int transport_connect(struct transport
*transport
, const char *name
,
1470 const char *exec
, int fd
[2])
1472 if (transport
->vtable
->connect
)
1473 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1475 die(_("operation not supported by protocol"));
1478 int transport_disconnect(struct transport
*transport
)
1481 if (transport
->vtable
->disconnect
)
1482 ret
= transport
->vtable
->disconnect(transport
);
1483 if (transport
->got_remote_refs
)
1484 free_refs((void *)transport
->remote_refs
);
1490 * Strip username (and password) from a URL and return
1491 * it in a newly allocated string.
1493 char *transport_anonymize_url(const char *url
)
1495 char *scheme_prefix
, *anon_part
;
1496 size_t anon_len
, prefix_len
= 0;
1498 anon_part
= strchr(url
, '@');
1499 if (url_is_local_not_ssh(url
) || !anon_part
)
1502 anon_len
= strlen(++anon_part
);
1503 scheme_prefix
= strstr(url
, "://");
1504 if (!scheme_prefix
) {
1505 if (!strchr(anon_part
, ':'))
1506 /* cannot be "me@there:/path/name" */
1510 /* make sure scheme is reasonable */
1511 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1514 case '+': case '.': case '-':
1523 /* @ past the first slash does not count */
1524 cp
= strchr(scheme_prefix
+ 3, '/');
1525 if (cp
&& cp
< anon_part
)
1527 prefix_len
= scheme_prefix
- url
+ 3;
1529 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1530 (int)anon_len
, anon_part
);
1532 return xstrdup(url
);