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
))
430 if (data
->fd
[1] >= 0)
432 if (finish_connect(data
->conn
))
441 static int push_had_errors(struct ref
*ref
)
443 for (; ref
; ref
= ref
->next
) {
444 switch (ref
->status
) {
445 case REF_STATUS_NONE
:
446 case REF_STATUS_UPTODATE
:
456 int transport_refs_pushed(struct ref
*ref
)
458 for (; ref
; ref
= ref
->next
) {
459 switch(ref
->status
) {
460 case REF_STATUS_NONE
:
461 case REF_STATUS_UPTODATE
:
470 static void update_one_tracking_ref(struct remote
*remote
, char *refname
,
471 struct object_id
*new_oid
, int deletion
,
474 struct refspec_item rs
;
476 memset(&rs
, 0, sizeof(rs
));
480 if (!remote_find_tracking(remote
, &rs
)) {
482 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
484 delete_ref(NULL
, rs
.dst
, NULL
, 0);
486 update_ref("update by push", rs
.dst
, new_oid
,
492 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
495 struct object_id
*new_oid
;
496 struct ref_push_report
*report
;
498 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
501 report
= ref
->report
;
503 update_one_tracking_ref(remote
, ref
->name
, &ref
->new_oid
,
504 ref
->deletion
, verbose
);
506 for (; report
; report
= report
->next
) {
507 refname
= report
->ref_name
? (char *)report
->ref_name
: ref
->name
;
508 new_oid
= report
->new_oid
? report
->new_oid
: &ref
->new_oid
;
509 update_one_tracking_ref(remote
, refname
, new_oid
,
510 is_null_oid(new_oid
), verbose
);
514 static void print_ref_status(char flag
, const char *summary
,
515 struct ref
*to
, struct ref
*from
, const char *msg
,
516 struct ref_push_report
*report
,
517 int porcelain
, int summary_width
)
521 if (report
&& report
->ref_name
)
522 to_name
= report
->ref_name
;
528 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to_name
);
530 fprintf(stdout
, "%c\t:%s\t", flag
, to_name
);
532 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
534 fprintf(stdout
, "%s\n", summary
);
536 const char *red
= "", *reset
= "";
537 if (push_had_errors(to
)) {
538 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
539 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
541 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
544 fprintf(stderr
, "%s -> %s",
545 prettify_refname(from
->name
),
546 prettify_refname(to_name
));
548 fputs(prettify_refname(to_name
), stderr
);
558 static void print_ok_ref_status(struct ref
*ref
,
559 struct ref_push_report
*report
,
560 int porcelain
, int summary_width
)
562 struct object_id
*old_oid
;
563 struct object_id
*new_oid
;
564 const char *ref_name
;
567 if (report
&& report
->old_oid
)
568 old_oid
= report
->old_oid
;
570 old_oid
= &ref
->old_oid
;
571 if (report
&& report
->new_oid
)
572 new_oid
= report
->new_oid
;
574 new_oid
= &ref
->new_oid
;
575 if (report
&& report
->forced_update
)
576 forced_update
= report
->forced_update
;
578 forced_update
= ref
->forced_update
;
579 if (report
&& report
->ref_name
)
580 ref_name
= report
->ref_name
;
582 ref_name
= ref
->name
;
585 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
586 report
, porcelain
, summary_width
);
587 else if (is_null_oid(old_oid
))
588 print_ref_status('*',
589 (starts_with(ref_name
, "refs/tags/")
591 : (starts_with(ref_name
, "refs/heads/")
593 : "[new reference]")),
594 ref
, ref
->peer_ref
, NULL
,
595 report
, porcelain
, summary_width
);
597 struct strbuf quickref
= STRBUF_INIT
;
601 strbuf_add_unique_abbrev(&quickref
, old_oid
,
604 strbuf_addstr(&quickref
, "...");
606 msg
= "forced update";
608 strbuf_addstr(&quickref
, "..");
612 strbuf_add_unique_abbrev(&quickref
, new_oid
,
615 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
616 report
, porcelain
, summary_width
);
617 strbuf_release(&quickref
);
621 static int print_one_push_report(struct ref
*ref
, const char *dest
, int count
,
622 struct ref_push_report
*report
,
623 int porcelain
, int summary_width
)
626 char *url
= transport_anonymize_url(dest
);
627 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
631 switch(ref
->status
) {
632 case REF_STATUS_NONE
:
633 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
634 report
, porcelain
, summary_width
);
636 case REF_STATUS_REJECT_NODELETE
:
637 print_ref_status('!', "[rejected]", ref
, NULL
,
638 "remote does not support deleting refs",
639 report
, porcelain
, summary_width
);
641 case REF_STATUS_UPTODATE
:
642 print_ref_status('=', "[up to date]", ref
,
644 report
, porcelain
, summary_width
);
646 case REF_STATUS_REJECT_NONFASTFORWARD
:
647 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
649 report
, porcelain
, summary_width
);
651 case REF_STATUS_REJECT_ALREADY_EXISTS
:
652 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
654 report
, porcelain
, summary_width
);
656 case REF_STATUS_REJECT_FETCH_FIRST
:
657 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
659 report
, porcelain
, summary_width
);
661 case REF_STATUS_REJECT_NEEDS_FORCE
:
662 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
664 report
, porcelain
, summary_width
);
666 case REF_STATUS_REJECT_STALE
:
667 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
669 report
, porcelain
, summary_width
);
671 case REF_STATUS_REJECT_REMOTE_UPDATED
:
672 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
673 "remote ref updated since checkout",
674 report
, porcelain
, summary_width
);
676 case REF_STATUS_REJECT_SHALLOW
:
677 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
678 "new shallow roots not allowed",
679 report
, porcelain
, summary_width
);
681 case REF_STATUS_REMOTE_REJECT
:
682 print_ref_status('!', "[remote rejected]", ref
,
683 ref
->deletion
? NULL
: ref
->peer_ref
,
685 report
, porcelain
, summary_width
);
687 case REF_STATUS_EXPECTING_REPORT
:
688 print_ref_status('!', "[remote failure]", ref
,
689 ref
->deletion
? NULL
: ref
->peer_ref
,
690 "remote failed to report status",
691 report
, porcelain
, summary_width
);
693 case REF_STATUS_ATOMIC_PUSH_FAILED
:
694 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
695 "atomic push failed",
696 report
, porcelain
, summary_width
);
699 print_ok_ref_status(ref
, report
, porcelain
, summary_width
);
706 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
707 int porcelain
, int summary_width
)
709 struct ref_push_report
*report
;
713 return print_one_push_report(ref
, dest
, count
,
714 NULL
, porcelain
, summary_width
);
716 for (report
= ref
->report
; report
; report
= report
->next
)
717 print_one_push_report(ref
, dest
, count
+ n
++,
718 report
, porcelain
, summary_width
);
722 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
724 char hex
[GIT_MAX_HEXSZ
+ 1];
725 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
727 return (w
< sofar
) ? sofar
: w
;
730 int transport_summary_width(const struct ref
*refs
)
734 for (; refs
; refs
= refs
->next
) {
735 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
736 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
739 maxw
= FALLBACK_DEFAULT_ABBREV
;
740 return (2 * maxw
+ 3);
743 void transport_print_push_status(const char *dest
, struct ref
*refs
,
744 int verbose
, int porcelain
, unsigned int *reject_reasons
)
749 int summary_width
= transport_summary_width(refs
);
751 if (transport_color_config() < 0)
752 warning(_("could not parse transport.color.* config"));
754 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
757 for (ref
= refs
; ref
; ref
= ref
->next
)
758 if (ref
->status
== REF_STATUS_UPTODATE
)
759 n
+= print_one_push_status(ref
, dest
, n
,
760 porcelain
, summary_width
);
763 for (ref
= refs
; ref
; ref
= ref
->next
)
764 if (ref
->status
== REF_STATUS_OK
)
765 n
+= print_one_push_status(ref
, dest
, n
,
766 porcelain
, summary_width
);
769 for (ref
= refs
; ref
; ref
= ref
->next
) {
770 if (ref
->status
!= REF_STATUS_NONE
&&
771 ref
->status
!= REF_STATUS_UPTODATE
&&
772 ref
->status
!= REF_STATUS_OK
)
773 n
+= print_one_push_status(ref
, dest
, n
,
774 porcelain
, summary_width
);
775 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
776 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
777 *reject_reasons
|= REJECT_NON_FF_HEAD
;
779 *reject_reasons
|= REJECT_NON_FF_OTHER
;
780 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
781 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
782 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
783 *reject_reasons
|= REJECT_FETCH_FIRST
;
784 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
785 *reject_reasons
|= REJECT_NEEDS_FORCE
;
786 } else if (ref
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) {
787 *reject_reasons
|= REJECT_REF_NEEDS_UPDATE
;
793 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
795 struct git_transport_data
*data
= transport
->data
;
796 struct send_pack_args args
;
799 if (transport_color_config() < 0)
802 if (!data
->got_remote_heads
)
803 get_refs_via_connect(transport
, 1, NULL
);
805 memset(&args
, 0, sizeof(args
));
806 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
807 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
808 args
.use_thin_pack
= data
->options
.thin
;
809 args
.verbose
= (transport
->verbose
> 0);
810 args
.quiet
= (transport
->verbose
< 0);
811 args
.progress
= transport
->progress
;
812 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
813 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
814 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
815 args
.push_options
= transport
->push_options
;
816 args
.url
= transport
->url
;
818 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
819 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
820 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
821 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
823 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
825 switch (data
->version
) {
827 die(_("support for protocol v2 not implemented yet"));
831 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
834 case protocol_unknown_version
:
835 BUG("unknown protocol version");
841 * Atomic push may abort the connection early and close the pipe,
842 * which may cause an error for `finish_connect()`. Ignore this error
843 * for atomic git-push.
845 if (ret
|| args
.atomic
)
846 finish_connect(data
->conn
);
848 ret
= finish_connect(data
->conn
);
850 data
->got_remote_heads
= 0;
855 static int connect_git(struct transport
*transport
, const char *name
,
856 const char *executable
, int fd
[2])
858 struct git_transport_data
*data
= transport
->data
;
859 data
->conn
= git_connect(data
->fd
, transport
->url
,
866 static int disconnect_git(struct transport
*transport
)
868 struct git_transport_data
*data
= transport
->data
;
870 if (data
->got_remote_heads
&& !transport
->stateless_rpc
)
871 packet_flush(data
->fd
[1]);
873 if (data
->fd
[1] >= 0)
875 finish_connect(data
->conn
);
882 static struct transport_vtable taken_over_vtable
= {
884 get_refs_via_connect
,
891 void transport_take_over(struct transport
*transport
,
892 struct child_process
*child
)
894 struct git_transport_data
*data
;
896 if (!transport
->smart_options
)
897 BUG("taking over transport requires non-NULL "
898 "smart_options field.");
900 CALLOC_ARRAY(data
, 1);
901 data
->options
= *transport
->smart_options
;
903 data
->fd
[0] = data
->conn
->out
;
904 data
->fd
[1] = data
->conn
->in
;
905 data
->got_remote_heads
= 0;
906 transport
->data
= data
;
908 transport
->vtable
= &taken_over_vtable
;
909 transport
->smart_options
= &(data
->options
);
911 transport
->cannot_reuse
= 1;
914 static int is_file(const char *url
)
919 return S_ISREG(buf
.st_mode
);
922 static int external_specification_len(const char *url
)
924 return strchr(url
, ':') - url
;
927 static const struct string_list
*protocol_whitelist(void)
929 static int enabled
= -1;
930 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
933 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
935 string_list_split(&allowed
, v
, ':', -1);
936 string_list_sort(&allowed
);
943 return enabled
? &allowed
: NULL
;
946 enum protocol_allow_config
{
947 PROTOCOL_ALLOW_NEVER
= 0,
948 PROTOCOL_ALLOW_USER_ONLY
,
949 PROTOCOL_ALLOW_ALWAYS
952 static enum protocol_allow_config
parse_protocol_config(const char *key
,
955 if (!strcasecmp(value
, "always"))
956 return PROTOCOL_ALLOW_ALWAYS
;
957 else if (!strcasecmp(value
, "never"))
958 return PROTOCOL_ALLOW_NEVER
;
959 else if (!strcasecmp(value
, "user"))
960 return PROTOCOL_ALLOW_USER_ONLY
;
962 die(_("unknown value for config '%s': %s"), key
, value
);
965 static enum protocol_allow_config
get_protocol_config(const char *type
)
967 char *key
= xstrfmt("protocol.%s.allow", type
);
970 /* first check the per-protocol config */
971 if (!git_config_get_string(key
, &value
)) {
972 enum protocol_allow_config ret
=
973 parse_protocol_config(key
, value
);
980 /* if defined, fallback to user-defined default for unknown protocols */
981 if (!git_config_get_string("protocol.allow", &value
)) {
982 enum protocol_allow_config ret
=
983 parse_protocol_config("protocol.allow", value
);
988 /* fallback to built-in defaults */
990 if (!strcmp(type
, "http") ||
991 !strcmp(type
, "https") ||
992 !strcmp(type
, "git") ||
993 !strcmp(type
, "ssh") ||
994 !strcmp(type
, "file"))
995 return PROTOCOL_ALLOW_ALWAYS
;
997 /* known scary; err on the side of caution */
998 if (!strcmp(type
, "ext"))
999 return PROTOCOL_ALLOW_NEVER
;
1001 /* unknown; by default let them be used only directly by the user */
1002 return PROTOCOL_ALLOW_USER_ONLY
;
1005 int is_transport_allowed(const char *type
, int from_user
)
1007 const struct string_list
*whitelist
= protocol_whitelist();
1009 return string_list_has_string(whitelist
, type
);
1011 switch (get_protocol_config(type
)) {
1012 case PROTOCOL_ALLOW_ALWAYS
:
1014 case PROTOCOL_ALLOW_NEVER
:
1016 case PROTOCOL_ALLOW_USER_ONLY
:
1018 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
1022 BUG("invalid protocol_allow_config type");
1025 void transport_check_allowed(const char *type
)
1027 if (!is_transport_allowed(type
, -1))
1028 die(_("transport '%s' not allowed"), type
);
1031 static struct transport_vtable bundle_vtable
= {
1033 get_refs_from_bundle
,
1034 fetch_refs_from_bundle
,
1040 static struct transport_vtable builtin_smart_vtable
= {
1042 get_refs_via_connect
,
1043 fetch_refs_via_pack
,
1049 struct transport
*transport_get(struct remote
*remote
, const char *url
)
1052 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
1054 ret
->progress
= isatty(2);
1055 string_list_init(&ret
->pack_lockfiles
, 1);
1058 BUG("No remote provided to transport_get()");
1060 ret
->got_remote_refs
= 0;
1061 ret
->remote
= remote
;
1062 helper
= remote
->foreign_vcs
;
1064 if (!url
&& remote
->url
)
1065 url
= remote
->url
[0];
1068 /* maybe it is a foreign URL? */
1070 const char *p
= url
;
1072 while (is_urlschemechar(p
== url
, *p
))
1074 if (starts_with(p
, "::"))
1075 helper
= xstrndup(url
, p
- url
);
1079 transport_helper_init(ret
, helper
);
1080 } else if (starts_with(url
, "rsync:")) {
1081 die(_("git-over-rsync is no longer supported"));
1082 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
1083 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
1084 transport_check_allowed("file");
1086 ret
->vtable
= &bundle_vtable
;
1087 ret
->smart_options
= NULL
;
1088 } else if (!is_url(url
)
1089 || starts_with(url
, "file://")
1090 || starts_with(url
, "git://")
1091 || starts_with(url
, "ssh://")
1092 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
1093 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
1096 * These are builtin smart transports; "allowed" transports
1097 * will be checked individually in git_connect.
1099 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
1101 ret
->vtable
= &builtin_smart_vtable
;
1102 ret
->smart_options
= &(data
->options
);
1105 data
->got_remote_heads
= 0;
1107 /* Unknown protocol in URL. Pass to external handler. */
1108 int len
= external_specification_len(url
);
1109 char *handler
= xmemdupz(url
, len
);
1110 transport_helper_init(ret
, handler
);
1113 if (ret
->smart_options
) {
1114 ret
->smart_options
->thin
= 1;
1115 ret
->smart_options
->uploadpack
= "git-upload-pack";
1116 if (remote
->uploadpack
)
1117 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1118 ret
->smart_options
->receivepack
= "git-receive-pack";
1119 if (remote
->receivepack
)
1120 ret
->smart_options
->receivepack
= remote
->receivepack
;
1123 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1128 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1130 return transport
->hash_algo
;
1133 int transport_set_option(struct transport
*transport
,
1134 const char *name
, const char *value
)
1136 int git_reports
= 1, protocol_reports
= 1;
1138 if (transport
->smart_options
)
1139 git_reports
= set_git_option(transport
->smart_options
,
1142 if (transport
->vtable
->set_option
)
1143 protocol_reports
= transport
->vtable
->set_option(transport
,
1146 /* If either report is 0, report 0 (success). */
1147 if (!git_reports
|| !protocol_reports
)
1149 /* If either reports -1 (invalid value), report -1. */
1150 if ((git_reports
== -1) || (protocol_reports
== -1))
1152 /* Otherwise if both report unknown, report unknown. */
1156 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1160 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1162 transport
->verbose
= -1;
1165 * Rules used to determine whether to report progress (processing aborts
1166 * when a rule is satisfied):
1168 * . Report progress, if force_progress is 1 (ie. --progress).
1169 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1170 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1171 * . Report progress if isatty(2) is 1.
1173 if (force_progress
>= 0)
1174 transport
->progress
= !!force_progress
;
1176 transport
->progress
= verbosity
>= 0 && isatty(2);
1179 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1183 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1184 "not be found on any remote:\n"));
1185 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1186 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1187 fprintf(stderr
, _("\nPlease try\n\n"
1188 " git push --recurse-submodules=on-demand\n\n"
1189 "or cd to the path and use\n\n"
1191 "to push them to a remote.\n\n"));
1193 string_list_clear(needs_pushing
, 0);
1195 die(_("Aborting."));
1198 static int run_pre_push_hook(struct transport
*transport
,
1199 struct ref
*remote_refs
)
1203 struct child_process proc
= CHILD_PROCESS_INIT
;
1205 const char *argv
[4];
1207 if (!(argv
[0] = find_hook("pre-push")))
1210 argv
[1] = transport
->remote
->name
;
1211 argv
[2] = transport
->url
;
1216 proc
.trace2_hook_name
= "pre-push";
1218 if (start_command(&proc
)) {
1219 finish_command(&proc
);
1223 sigchain_push(SIGPIPE
, SIG_IGN
);
1225 strbuf_init(&buf
, 256);
1227 for (r
= remote_refs
; r
; r
= r
->next
) {
1228 if (!r
->peer_ref
) continue;
1229 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1230 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1231 if (r
->status
== REF_STATUS_REJECT_REMOTE_UPDATED
) continue;
1232 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1235 strbuf_addf( &buf
, "%s %s %s %s\n",
1236 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1237 r
->name
, oid_to_hex(&r
->old_oid
));
1239 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1240 /* We do not mind if a hook does not read all refs. */
1247 strbuf_release(&buf
);
1253 sigchain_pop(SIGPIPE
);
1255 x
= finish_command(&proc
);
1262 int transport_push(struct repository
*r
,
1263 struct transport
*transport
,
1264 struct refspec
*rs
, int flags
,
1265 unsigned int *reject_reasons
)
1267 *reject_reasons
= 0;
1269 if (transport_color_config() < 0)
1272 if (transport
->vtable
->push_refs
) {
1273 struct ref
*remote_refs
;
1274 struct ref
*local_refs
= get_local_heads();
1275 int match_flags
= MATCH_REFS_NONE
;
1276 int verbose
= (transport
->verbose
> 0);
1277 int quiet
= (transport
->verbose
< 0);
1278 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1279 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1280 int push_ret
, ret
, err
;
1281 struct transport_ls_refs_options transport_options
=
1282 TRANSPORT_LS_REFS_OPTIONS_INIT
;
1284 if (check_push_refs(local_refs
, rs
) < 0)
1287 refspec_ref_prefixes(rs
, &transport_options
.ref_prefixes
);
1289 trace2_region_enter("transport_push", "get_refs_list", r
);
1290 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1291 &transport_options
);
1292 trace2_region_leave("transport_push", "get_refs_list", r
);
1294 strvec_clear(&transport_options
.ref_prefixes
);
1296 if (flags
& TRANSPORT_PUSH_ALL
)
1297 match_flags
|= MATCH_REFS_ALL
;
1298 if (flags
& TRANSPORT_PUSH_MIRROR
)
1299 match_flags
|= MATCH_REFS_MIRROR
;
1300 if (flags
& TRANSPORT_PUSH_PRUNE
)
1301 match_flags
|= MATCH_REFS_PRUNE
;
1302 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1303 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1305 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1308 if (transport
->smart_options
&&
1309 transport
->smart_options
->cas
&&
1310 !is_empty_cas(transport
->smart_options
->cas
))
1311 apply_push_cas(transport
->smart_options
->cas
,
1312 transport
->remote
, remote_refs
);
1314 set_ref_status_for_push(remote_refs
,
1315 flags
& TRANSPORT_PUSH_MIRROR
,
1316 flags
& TRANSPORT_PUSH_FORCE
);
1318 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1319 if (run_pre_push_hook(transport
, remote_refs
))
1322 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1323 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1324 !is_bare_repository()) {
1325 struct ref
*ref
= remote_refs
;
1326 struct oid_array commits
= OID_ARRAY_INIT
;
1328 trace2_region_enter("transport_push", "push_submodules", r
);
1329 for (; ref
; ref
= ref
->next
)
1330 if (!is_null_oid(&ref
->new_oid
))
1331 oid_array_append(&commits
,
1334 if (!push_unpushed_submodules(r
,
1338 transport
->push_options
,
1340 oid_array_clear(&commits
);
1341 trace2_region_leave("transport_push", "push_submodules", r
);
1342 die(_("failed to push all needed submodules"));
1344 oid_array_clear(&commits
);
1345 trace2_region_leave("transport_push", "push_submodules", r
);
1348 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1349 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1350 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1351 !pretend
)) && !is_bare_repository()) {
1352 struct ref
*ref
= remote_refs
;
1353 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1354 struct oid_array commits
= OID_ARRAY_INIT
;
1356 trace2_region_enter("transport_push", "check_submodules", r
);
1357 for (; ref
; ref
= ref
->next
)
1358 if (!is_null_oid(&ref
->new_oid
))
1359 oid_array_append(&commits
,
1362 if (find_unpushed_submodules(r
,
1364 transport
->remote
->name
,
1366 oid_array_clear(&commits
);
1367 trace2_region_leave("transport_push", "check_submodules", r
);
1368 die_with_unpushed_submodules(&needs_pushing
);
1370 string_list_clear(&needs_pushing
, 0);
1371 oid_array_clear(&commits
);
1372 trace2_region_leave("transport_push", "check_submodules", r
);
1375 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1376 trace2_region_enter("transport_push", "push_refs", r
);
1377 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1378 trace2_region_leave("transport_push", "push_refs", r
);
1381 err
= push_had_errors(remote_refs
);
1382 ret
= push_ret
| err
;
1385 transport_print_push_status(transport
->url
, remote_refs
,
1386 verbose
| porcelain
, porcelain
,
1389 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1390 set_upstreams(transport
, remote_refs
, pretend
);
1392 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1393 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1395 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1396 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1399 if (porcelain
&& !push_ret
)
1401 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1402 fprintf(stderr
, "Everything up-to-date\n");
1409 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1410 struct transport_ls_refs_options
*transport_options
)
1412 if (!transport
->got_remote_refs
) {
1413 transport
->remote_refs
=
1414 transport
->vtable
->get_refs_list(transport
, 0,
1416 transport
->got_remote_refs
= 1;
1419 return transport
->remote_refs
;
1422 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1425 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1426 struct ref
**heads
= NULL
;
1429 for (rm
= refs
; rm
; rm
= rm
->next
) {
1432 !is_null_oid(&rm
->old_oid
) &&
1433 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1435 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1436 heads
[nr_heads
++] = rm
;
1441 * When deepening of a shallow repository is requested,
1442 * then local and remote refs are likely to still be equal.
1443 * Just feed them all to the fetch method in that case.
1444 * This condition shouldn't be met in a non-deepening fetch
1445 * (see builtin/fetch.c:quickfetch()).
1447 ALLOC_ARRAY(heads
, nr_refs
);
1448 for (rm
= refs
; rm
; rm
= rm
->next
)
1449 heads
[nr_heads
++] = rm
;
1452 rc
= transport
->vtable
->fetch(transport
, nr_heads
, heads
);
1458 void transport_unlock_pack(struct transport
*transport
)
1462 for (i
= 0; i
< transport
->pack_lockfiles
.nr
; i
++)
1463 unlink_or_warn(transport
->pack_lockfiles
.items
[i
].string
);
1464 string_list_clear(&transport
->pack_lockfiles
, 0);
1467 int transport_connect(struct transport
*transport
, const char *name
,
1468 const char *exec
, int fd
[2])
1470 if (transport
->vtable
->connect
)
1471 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1473 die(_("operation not supported by protocol"));
1476 int transport_disconnect(struct transport
*transport
)
1479 if (transport
->vtable
->disconnect
)
1480 ret
= transport
->vtable
->disconnect(transport
);
1481 if (transport
->got_remote_refs
)
1482 free_refs((void *)transport
->remote_refs
);
1488 * Strip username (and password) from a URL and return
1489 * it in a newly allocated string.
1491 char *transport_anonymize_url(const char *url
)
1493 char *scheme_prefix
, *anon_part
;
1494 size_t anon_len
, prefix_len
= 0;
1496 anon_part
= strchr(url
, '@');
1497 if (url_is_local_not_ssh(url
) || !anon_part
)
1500 anon_len
= strlen(++anon_part
);
1501 scheme_prefix
= strstr(url
, "://");
1502 if (!scheme_prefix
) {
1503 if (!strchr(anon_part
, ':'))
1504 /* cannot be "me@there:/path/name" */
1508 /* make sure scheme is reasonable */
1509 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1512 case '+': case '.': case '-':
1521 /* @ past the first slash does not count */
1522 cp
= strchr(scheme_prefix
+ 3, '/');
1523 if (cp
&& cp
< anon_part
)
1525 prefix_len
= scheme_prefix
- url
+ 3;
1527 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1528 (int)anon_len
, anon_part
);
1530 return xstrdup(url
);