4 #include "run-command.h"
6 #include "fetch-pack.h"
17 #include "submodule.h"
18 #include "string-list.h"
19 #include "sha1-array.h"
21 #include "transport-internal.h"
23 #include "object-store.h"
26 static int transport_use_color
= -1;
27 static char transport_colors
[][COLOR_MAXLEN
] = {
29 GIT_COLOR_RED
/* REJECTED */
32 enum color_transport
{
33 TRANSPORT_COLOR_RESET
= 0,
34 TRANSPORT_COLOR_REJECTED
= 1
37 static int transport_color_config(void)
39 const char *keys
[] = {
40 "color.transport.reset",
41 "color.transport.rejected"
42 }, *key
= "color.transport";
45 static int initialized
;
51 if (!git_config_get_string(key
, &value
))
52 transport_use_color
= git_config_colorbool(key
, value
);
54 if (!want_color_stderr(transport_use_color
))
57 for (i
= 0; i
< ARRAY_SIZE(keys
); i
++)
58 if (!git_config_get_string(keys
[i
], &value
)) {
60 return config_error_nonbool(keys
[i
]);
61 if (color_parse(value
, transport_colors
[i
]) < 0)
68 static const char *transport_get_color(enum color_transport ix
)
70 if (want_color_stderr(transport_use_color
))
71 return transport_colors
[ix
];
75 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
79 for (ref
= refs
; ref
; ref
= ref
->next
) {
80 const char *localname
;
82 const char *remotename
;
85 * Check suitability for tracking. Must be successful /
86 * already up-to-date ref create/modify (not delete).
88 if (ref
->status
!= REF_STATUS_OK
&&
89 ref
->status
!= REF_STATUS_UPTODATE
)
93 if (is_null_oid(&ref
->new_oid
))
96 /* Follow symbolic refs (mainly for HEAD). */
97 localname
= ref
->peer_ref
->name
;
98 remotename
= ref
->name
;
99 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
101 if (tmp
&& flag
& REF_ISSYMREF
&&
102 starts_with(tmp
, "refs/heads/"))
105 /* Both source and destination must be local branches. */
106 if (!localname
|| !starts_with(localname
, "refs/heads/"))
108 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
112 install_branch_config(BRANCH_CONFIG_VERBOSE
,
113 localname
+ 11, transport
->remote
->name
,
116 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
117 localname
+ 11, remotename
+ 11,
118 transport
->remote
->name
);
122 struct bundle_transport_data
{
124 struct bundle_header header
;
127 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
129 const struct argv_array
*ref_prefixes
)
131 struct bundle_transport_data
*data
= transport
->data
;
132 struct ref
*result
= NULL
;
140 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
142 die(_("could not read bundle '%s'"), transport
->url
);
143 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
144 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
145 struct ref
*ref
= alloc_ref(e
->name
);
146 oidcpy(&ref
->old_oid
, &e
->oid
);
153 static int fetch_refs_from_bundle(struct transport
*transport
,
154 int nr_heads
, struct ref
**to_fetch
)
156 struct bundle_transport_data
*data
= transport
->data
;
157 return unbundle(the_repository
, &data
->header
, data
->fd
,
158 transport
->progress
? BUNDLE_VERBOSE
: 0);
161 static int close_bundle(struct transport
*transport
)
163 struct bundle_transport_data
*data
= transport
->data
;
170 struct git_transport_data
{
171 struct git_transport_options options
;
172 struct child_process
*conn
;
174 unsigned got_remote_heads
: 1;
175 enum protocol_version version
;
176 struct oid_array extra_have
;
177 struct oid_array shallow
;
180 static int set_git_option(struct git_transport_options
*opts
,
181 const char *name
, const char *value
)
183 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
184 opts
->uploadpack
= value
;
186 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
187 opts
->receivepack
= value
;
189 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
190 opts
->thin
= !!value
;
192 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
193 opts
->followtags
= !!value
;
195 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
196 opts
->keep
= !!value
;
198 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
199 opts
->update_shallow
= !!value
;
201 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
206 opts
->depth
= strtol(value
, &end
, 0);
208 die(_("transport: invalid depth option '%s'"), value
);
211 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
212 opts
->deepen_since
= value
;
214 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
215 opts
->deepen_not
= (const struct string_list
*)value
;
217 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
218 opts
->deepen_relative
= !!value
;
220 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
221 opts
->from_promisor
= !!value
;
223 } else if (!strcmp(name
, TRANS_OPT_NO_DEPENDENTS
)) {
224 opts
->no_dependents
= !!value
;
226 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
227 parse_list_objects_filter(&opts
->filter_options
, value
);
233 static int connect_setup(struct transport
*transport
, int for_push
)
235 struct git_transport_data
*data
= transport
->data
;
236 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
241 switch (transport
->family
) {
242 case TRANSPORT_FAMILY_ALL
: break;
243 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
244 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
247 data
->conn
= git_connect(data
->fd
, transport
->url
,
248 for_push
? data
->options
.receivepack
:
249 data
->options
.uploadpack
,
256 * Obtains the protocol version from the transport and writes it to
257 * transport->data->version, first connecting if not already connected.
259 * If the protocol version is one that allows skipping the listing of remote
260 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
261 * this function returns NULL. Otherwise, this function returns the list of
264 static struct ref
*handshake(struct transport
*transport
, int for_push
,
265 const struct argv_array
*ref_prefixes
,
268 struct git_transport_data
*data
= transport
->data
;
269 struct ref
*refs
= NULL
;
270 struct packet_reader reader
;
272 connect_setup(transport
, for_push
);
274 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
275 PACKET_READ_CHOMP_NEWLINE
|
276 PACKET_READ_GENTLE_ON_EOF
|
277 PACKET_READ_DIE_ON_ERR_PACKET
);
279 data
->version
= discover_version(&reader
);
280 switch (data
->version
) {
283 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
285 transport
->server_options
);
289 get_remote_heads(&reader
, &refs
,
290 for_push
? REF_NORMAL
: 0,
294 case protocol_unknown_version
:
295 BUG("unknown protocol version");
297 data
->got_remote_heads
= 1;
299 if (reader
.line_peeked
)
300 BUG("buffer must be empty at the end of handshake()");
305 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
306 const struct argv_array
*ref_prefixes
)
308 return handshake(transport
, for_push
, ref_prefixes
, 1);
311 static int fetch_refs_via_pack(struct transport
*transport
,
312 int nr_heads
, struct ref
**to_fetch
)
315 struct git_transport_data
*data
= transport
->data
;
316 struct ref
*refs
= NULL
;
317 char *dest
= xstrdup(transport
->url
);
318 struct fetch_pack_args args
;
319 struct ref
*refs_tmp
= NULL
;
321 memset(&args
, 0, sizeof(args
));
322 args
.uploadpack
= data
->options
.uploadpack
;
323 args
.keep_pack
= data
->options
.keep
;
325 args
.use_thin_pack
= data
->options
.thin
;
326 args
.include_tag
= data
->options
.followtags
;
327 args
.verbose
= (transport
->verbose
> 1);
328 args
.quiet
= (transport
->verbose
< 0);
329 args
.no_progress
= !transport
->progress
;
330 args
.depth
= data
->options
.depth
;
331 args
.deepen_since
= data
->options
.deepen_since
;
332 args
.deepen_not
= data
->options
.deepen_not
;
333 args
.deepen_relative
= data
->options
.deepen_relative
;
334 args
.check_self_contained_and_connected
=
335 data
->options
.check_self_contained_and_connected
;
336 args
.cloning
= transport
->cloning
;
337 args
.update_shallow
= data
->options
.update_shallow
;
338 args
.from_promisor
= data
->options
.from_promisor
;
339 args
.no_dependents
= data
->options
.no_dependents
;
340 args
.filter_options
= data
->options
.filter_options
;
341 args
.stateless_rpc
= transport
->stateless_rpc
;
342 args
.server_options
= transport
->server_options
;
343 args
.negotiation_tips
= data
->options
.negotiation_tips
;
345 if (!data
->got_remote_heads
) {
347 int must_list_refs
= 0;
348 for (i
= 0; i
< nr_heads
; i
++) {
349 if (!to_fetch
[i
]->exact_oid
) {
354 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
357 switch (data
->version
) {
359 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
360 refs_tmp
? refs_tmp
: transport
->remote_refs
,
361 dest
, to_fetch
, nr_heads
, &data
->shallow
,
362 &transport
->pack_lockfile
, data
->version
);
366 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
367 refs_tmp
? refs_tmp
: transport
->remote_refs
,
368 dest
, to_fetch
, nr_heads
, &data
->shallow
,
369 &transport
->pack_lockfile
, data
->version
);
371 case protocol_unknown_version
:
372 BUG("unknown protocol version");
377 if (finish_connect(data
->conn
))
380 data
->got_remote_heads
= 0;
381 data
->options
.self_contained_and_connected
=
382 args
.self_contained_and_connected
;
383 data
->options
.connectivity_checked
= args
.connectivity_checked
;
387 if (report_unmatched_refs(to_fetch
, nr_heads
))
396 static int push_had_errors(struct ref
*ref
)
398 for (; ref
; ref
= ref
->next
) {
399 switch (ref
->status
) {
400 case REF_STATUS_NONE
:
401 case REF_STATUS_UPTODATE
:
411 int transport_refs_pushed(struct ref
*ref
)
413 for (; ref
; ref
= ref
->next
) {
414 switch(ref
->status
) {
415 case REF_STATUS_NONE
:
416 case REF_STATUS_UPTODATE
:
425 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
427 struct refspec_item rs
;
429 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
435 if (!remote_find_tracking(remote
, &rs
)) {
437 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
439 delete_ref(NULL
, rs
.dst
, NULL
, 0);
441 update_ref("update by push", rs
.dst
, &ref
->new_oid
,
447 static void print_ref_status(char flag
, const char *summary
,
448 struct ref
*to
, struct ref
*from
, const char *msg
,
449 int porcelain
, int summary_width
)
453 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
455 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
457 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
459 fprintf(stdout
, "%s\n", summary
);
461 const char *red
= "", *reset
= "";
462 if (push_had_errors(to
)) {
463 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
464 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
466 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
469 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
471 fputs(prettify_refname(to
->name
), stderr
);
481 static void print_ok_ref_status(struct ref
*ref
, int porcelain
, int summary_width
)
484 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
485 porcelain
, summary_width
);
486 else if (is_null_oid(&ref
->old_oid
))
487 print_ref_status('*',
488 (starts_with(ref
->name
, "refs/tags/") ? "[new tag]" :
490 ref
, ref
->peer_ref
, NULL
, porcelain
, summary_width
);
492 struct strbuf quickref
= STRBUF_INIT
;
496 strbuf_add_unique_abbrev(&quickref
, &ref
->old_oid
,
498 if (ref
->forced_update
) {
499 strbuf_addstr(&quickref
, "...");
501 msg
= "forced update";
503 strbuf_addstr(&quickref
, "..");
507 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
,
510 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
511 porcelain
, summary_width
);
512 strbuf_release(&quickref
);
516 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
517 int porcelain
, int summary_width
)
520 char *url
= transport_anonymize_url(dest
);
521 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
525 switch(ref
->status
) {
526 case REF_STATUS_NONE
:
527 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
528 porcelain
, summary_width
);
530 case REF_STATUS_REJECT_NODELETE
:
531 print_ref_status('!', "[rejected]", ref
, NULL
,
532 "remote does not support deleting refs",
533 porcelain
, summary_width
);
535 case REF_STATUS_UPTODATE
:
536 print_ref_status('=', "[up to date]", ref
,
537 ref
->peer_ref
, NULL
, porcelain
, summary_width
);
539 case REF_STATUS_REJECT_NONFASTFORWARD
:
540 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
541 "non-fast-forward", porcelain
, summary_width
);
543 case REF_STATUS_REJECT_ALREADY_EXISTS
:
544 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
545 "already exists", porcelain
, summary_width
);
547 case REF_STATUS_REJECT_FETCH_FIRST
:
548 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
549 "fetch first", porcelain
, summary_width
);
551 case REF_STATUS_REJECT_NEEDS_FORCE
:
552 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
553 "needs force", porcelain
, summary_width
);
555 case REF_STATUS_REJECT_STALE
:
556 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
557 "stale info", porcelain
, summary_width
);
559 case REF_STATUS_REJECT_SHALLOW
:
560 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
561 "new shallow roots not allowed",
562 porcelain
, summary_width
);
564 case REF_STATUS_REMOTE_REJECT
:
565 print_ref_status('!', "[remote rejected]", ref
,
566 ref
->deletion
? NULL
: ref
->peer_ref
,
567 ref
->remote_status
, porcelain
, summary_width
);
569 case REF_STATUS_EXPECTING_REPORT
:
570 print_ref_status('!', "[remote failure]", ref
,
571 ref
->deletion
? NULL
: ref
->peer_ref
,
572 "remote failed to report status",
573 porcelain
, summary_width
);
575 case REF_STATUS_ATOMIC_PUSH_FAILED
:
576 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
577 "atomic push failed", porcelain
, summary_width
);
580 print_ok_ref_status(ref
, porcelain
, summary_width
);
587 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
589 char hex
[GIT_MAX_HEXSZ
+ 1];
590 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
592 return (w
< sofar
) ? sofar
: w
;
595 int transport_summary_width(const struct ref
*refs
)
599 for (; refs
; refs
= refs
->next
) {
600 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
601 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
604 maxw
= FALLBACK_DEFAULT_ABBREV
;
605 return (2 * maxw
+ 3);
608 void transport_print_push_status(const char *dest
, struct ref
*refs
,
609 int verbose
, int porcelain
, unsigned int *reject_reasons
)
614 int summary_width
= transport_summary_width(refs
);
616 if (transport_color_config() < 0)
617 warning(_("could not parse transport.color.* config"));
619 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
622 for (ref
= refs
; ref
; ref
= ref
->next
)
623 if (ref
->status
== REF_STATUS_UPTODATE
)
624 n
+= print_one_push_status(ref
, dest
, n
,
625 porcelain
, summary_width
);
628 for (ref
= refs
; ref
; ref
= ref
->next
)
629 if (ref
->status
== REF_STATUS_OK
)
630 n
+= print_one_push_status(ref
, dest
, n
,
631 porcelain
, summary_width
);
634 for (ref
= refs
; ref
; ref
= ref
->next
) {
635 if (ref
->status
!= REF_STATUS_NONE
&&
636 ref
->status
!= REF_STATUS_UPTODATE
&&
637 ref
->status
!= REF_STATUS_OK
)
638 n
+= print_one_push_status(ref
, dest
, n
,
639 porcelain
, summary_width
);
640 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
641 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
642 *reject_reasons
|= REJECT_NON_FF_HEAD
;
644 *reject_reasons
|= REJECT_NON_FF_OTHER
;
645 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
646 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
647 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
648 *reject_reasons
|= REJECT_FETCH_FIRST
;
649 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
650 *reject_reasons
|= REJECT_NEEDS_FORCE
;
656 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
658 struct git_transport_data
*data
= transport
->data
;
659 struct send_pack_args args
;
662 if (transport_color_config() < 0)
665 if (!data
->got_remote_heads
)
666 get_refs_via_connect(transport
, 1, NULL
);
668 memset(&args
, 0, sizeof(args
));
669 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
670 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
671 args
.use_thin_pack
= data
->options
.thin
;
672 args
.verbose
= (transport
->verbose
> 0);
673 args
.quiet
= (transport
->verbose
< 0);
674 args
.progress
= transport
->progress
;
675 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
676 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
677 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
678 args
.push_options
= transport
->push_options
;
679 args
.url
= transport
->url
;
681 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
682 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
683 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
684 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
686 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
688 switch (data
->version
) {
690 die(_("support for protocol v2 not implemented yet"));
694 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
697 case protocol_unknown_version
:
698 BUG("unknown protocol version");
703 ret
|= finish_connect(data
->conn
);
705 data
->got_remote_heads
= 0;
710 static int connect_git(struct transport
*transport
, const char *name
,
711 const char *executable
, int fd
[2])
713 struct git_transport_data
*data
= transport
->data
;
714 data
->conn
= git_connect(data
->fd
, transport
->url
,
721 static int disconnect_git(struct transport
*transport
)
723 struct git_transport_data
*data
= transport
->data
;
725 if (data
->got_remote_heads
)
726 packet_flush(data
->fd
[1]);
729 finish_connect(data
->conn
);
736 static struct transport_vtable taken_over_vtable
= {
739 get_refs_via_connect
,
746 void transport_take_over(struct transport
*transport
,
747 struct child_process
*child
)
749 struct git_transport_data
*data
;
751 if (!transport
->smart_options
)
752 BUG("taking over transport requires non-NULL "
753 "smart_options field.");
755 data
= xcalloc(1, sizeof(*data
));
756 data
->options
= *transport
->smart_options
;
758 data
->fd
[0] = data
->conn
->out
;
759 data
->fd
[1] = data
->conn
->in
;
760 data
->got_remote_heads
= 0;
761 transport
->data
= data
;
763 transport
->vtable
= &taken_over_vtable
;
764 transport
->smart_options
= &(data
->options
);
766 transport
->cannot_reuse
= 1;
769 static int is_file(const char *url
)
774 return S_ISREG(buf
.st_mode
);
777 static int external_specification_len(const char *url
)
779 return strchr(url
, ':') - url
;
782 static const struct string_list
*protocol_whitelist(void)
784 static int enabled
= -1;
785 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
788 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
790 string_list_split(&allowed
, v
, ':', -1);
791 string_list_sort(&allowed
);
798 return enabled
? &allowed
: NULL
;
801 enum protocol_allow_config
{
802 PROTOCOL_ALLOW_NEVER
= 0,
803 PROTOCOL_ALLOW_USER_ONLY
,
804 PROTOCOL_ALLOW_ALWAYS
807 static enum protocol_allow_config
parse_protocol_config(const char *key
,
810 if (!strcasecmp(value
, "always"))
811 return PROTOCOL_ALLOW_ALWAYS
;
812 else if (!strcasecmp(value
, "never"))
813 return PROTOCOL_ALLOW_NEVER
;
814 else if (!strcasecmp(value
, "user"))
815 return PROTOCOL_ALLOW_USER_ONLY
;
817 die(_("unknown value for config '%s': %s"), key
, value
);
820 static enum protocol_allow_config
get_protocol_config(const char *type
)
822 char *key
= xstrfmt("protocol.%s.allow", type
);
825 /* first check the per-protocol config */
826 if (!git_config_get_string(key
, &value
)) {
827 enum protocol_allow_config ret
=
828 parse_protocol_config(key
, value
);
835 /* if defined, fallback to user-defined default for unknown protocols */
836 if (!git_config_get_string("protocol.allow", &value
)) {
837 enum protocol_allow_config ret
=
838 parse_protocol_config("protocol.allow", value
);
843 /* fallback to built-in defaults */
845 if (!strcmp(type
, "http") ||
846 !strcmp(type
, "https") ||
847 !strcmp(type
, "git") ||
848 !strcmp(type
, "ssh") ||
849 !strcmp(type
, "file"))
850 return PROTOCOL_ALLOW_ALWAYS
;
852 /* known scary; err on the side of caution */
853 if (!strcmp(type
, "ext"))
854 return PROTOCOL_ALLOW_NEVER
;
856 /* unknown; by default let them be used only directly by the user */
857 return PROTOCOL_ALLOW_USER_ONLY
;
860 int is_transport_allowed(const char *type
, int from_user
)
862 const struct string_list
*whitelist
= protocol_whitelist();
864 return string_list_has_string(whitelist
, type
);
866 switch (get_protocol_config(type
)) {
867 case PROTOCOL_ALLOW_ALWAYS
:
869 case PROTOCOL_ALLOW_NEVER
:
871 case PROTOCOL_ALLOW_USER_ONLY
:
873 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
877 BUG("invalid protocol_allow_config type");
880 void transport_check_allowed(const char *type
)
882 if (!is_transport_allowed(type
, -1))
883 die(_("transport '%s' not allowed"), type
);
886 static struct transport_vtable bundle_vtable
= {
889 get_refs_from_bundle
,
890 fetch_refs_from_bundle
,
896 static struct transport_vtable builtin_smart_vtable
= {
899 get_refs_via_connect
,
906 struct transport
*transport_get(struct remote
*remote
, const char *url
)
909 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
911 ret
->progress
= isatty(2);
914 BUG("No remote provided to transport_get()");
916 ret
->got_remote_refs
= 0;
917 ret
->remote
= remote
;
918 helper
= remote
->foreign_vcs
;
920 if (!url
&& remote
->url
)
921 url
= remote
->url
[0];
924 /* maybe it is a foreign URL? */
928 while (is_urlschemechar(p
== url
, *p
))
930 if (starts_with(p
, "::"))
931 helper
= xstrndup(url
, p
- url
);
935 transport_helper_init(ret
, helper
);
936 } else if (starts_with(url
, "rsync:")) {
937 die(_("git-over-rsync is no longer supported"));
938 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
939 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
940 transport_check_allowed("file");
942 ret
->vtable
= &bundle_vtable
;
943 ret
->smart_options
= NULL
;
944 } else if (!is_url(url
)
945 || starts_with(url
, "file://")
946 || starts_with(url
, "git://")
947 || starts_with(url
, "ssh://")
948 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
949 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
952 * These are builtin smart transports; "allowed" transports
953 * will be checked individually in git_connect.
955 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
957 ret
->vtable
= &builtin_smart_vtable
;
958 ret
->smart_options
= &(data
->options
);
961 data
->got_remote_heads
= 0;
963 /* Unknown protocol in URL. Pass to external handler. */
964 int len
= external_specification_len(url
);
965 char *handler
= xmemdupz(url
, len
);
966 transport_helper_init(ret
, handler
);
969 if (ret
->smart_options
) {
970 ret
->smart_options
->thin
= 1;
971 ret
->smart_options
->uploadpack
= "git-upload-pack";
972 if (remote
->uploadpack
)
973 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
974 ret
->smart_options
->receivepack
= "git-receive-pack";
975 if (remote
->receivepack
)
976 ret
->smart_options
->receivepack
= remote
->receivepack
;
982 int transport_set_option(struct transport
*transport
,
983 const char *name
, const char *value
)
985 int git_reports
= 1, protocol_reports
= 1;
987 if (transport
->smart_options
)
988 git_reports
= set_git_option(transport
->smart_options
,
991 if (transport
->vtable
->set_option
)
992 protocol_reports
= transport
->vtable
->set_option(transport
,
995 /* If either report is 0, report 0 (success). */
996 if (!git_reports
|| !protocol_reports
)
998 /* If either reports -1 (invalid value), report -1. */
999 if ((git_reports
== -1) || (protocol_reports
== -1))
1001 /* Otherwise if both report unknown, report unknown. */
1005 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1009 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1011 transport
->verbose
= -1;
1014 * Rules used to determine whether to report progress (processing aborts
1015 * when a rule is satisfied):
1017 * . Report progress, if force_progress is 1 (ie. --progress).
1018 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1019 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1020 * . Report progress if isatty(2) is 1.
1022 if (force_progress
>= 0)
1023 transport
->progress
= !!force_progress
;
1025 transport
->progress
= verbosity
>= 0 && isatty(2);
1028 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1032 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1033 "not be found on any remote:\n"));
1034 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1035 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1036 fprintf(stderr
, _("\nPlease try\n\n"
1037 " git push --recurse-submodules=on-demand\n\n"
1038 "or cd to the path and use\n\n"
1040 "to push them to a remote.\n\n"));
1042 string_list_clear(needs_pushing
, 0);
1044 die(_("Aborting."));
1047 static int run_pre_push_hook(struct transport
*transport
,
1048 struct ref
*remote_refs
)
1052 struct child_process proc
= CHILD_PROCESS_INIT
;
1054 const char *argv
[4];
1056 if (!(argv
[0] = find_hook("pre-push")))
1059 argv
[1] = transport
->remote
->name
;
1060 argv
[2] = transport
->url
;
1065 proc
.trace2_hook_name
= "pre-push";
1067 if (start_command(&proc
)) {
1068 finish_command(&proc
);
1072 sigchain_push(SIGPIPE
, SIG_IGN
);
1074 strbuf_init(&buf
, 256);
1076 for (r
= remote_refs
; r
; r
= r
->next
) {
1077 if (!r
->peer_ref
) continue;
1078 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1079 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1080 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1083 strbuf_addf( &buf
, "%s %s %s %s\n",
1084 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1085 r
->name
, oid_to_hex(&r
->old_oid
));
1087 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1088 /* We do not mind if a hook does not read all refs. */
1095 strbuf_release(&buf
);
1101 sigchain_pop(SIGPIPE
);
1103 x
= finish_command(&proc
);
1110 int transport_push(struct repository
*r
,
1111 struct transport
*transport
,
1112 struct refspec
*rs
, int flags
,
1113 unsigned int *reject_reasons
)
1115 *reject_reasons
= 0;
1117 if (transport_color_config() < 0)
1120 if (transport
->vtable
->push_refs
) {
1121 struct ref
*remote_refs
;
1122 struct ref
*local_refs
= get_local_heads();
1123 int match_flags
= MATCH_REFS_NONE
;
1124 int verbose
= (transport
->verbose
> 0);
1125 int quiet
= (transport
->verbose
< 0);
1126 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1127 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1128 int push_ret
, ret
, err
;
1129 struct argv_array ref_prefixes
= ARGV_ARRAY_INIT
;
1131 if (check_push_refs(local_refs
, rs
) < 0)
1134 refspec_ref_prefixes(rs
, &ref_prefixes
);
1136 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1139 argv_array_clear(&ref_prefixes
);
1141 if (flags
& TRANSPORT_PUSH_ALL
)
1142 match_flags
|= MATCH_REFS_ALL
;
1143 if (flags
& TRANSPORT_PUSH_MIRROR
)
1144 match_flags
|= MATCH_REFS_MIRROR
;
1145 if (flags
& TRANSPORT_PUSH_PRUNE
)
1146 match_flags
|= MATCH_REFS_PRUNE
;
1147 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1148 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1150 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1153 if (transport
->smart_options
&&
1154 transport
->smart_options
->cas
&&
1155 !is_empty_cas(transport
->smart_options
->cas
))
1156 apply_push_cas(transport
->smart_options
->cas
,
1157 transport
->remote
, remote_refs
);
1159 set_ref_status_for_push(remote_refs
,
1160 flags
& TRANSPORT_PUSH_MIRROR
,
1161 flags
& TRANSPORT_PUSH_FORCE
);
1163 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1164 if (run_pre_push_hook(transport
, remote_refs
))
1167 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1168 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1169 !is_bare_repository()) {
1170 struct ref
*ref
= remote_refs
;
1171 struct oid_array commits
= OID_ARRAY_INIT
;
1173 for (; ref
; ref
= ref
->next
)
1174 if (!is_null_oid(&ref
->new_oid
))
1175 oid_array_append(&commits
,
1178 if (!push_unpushed_submodules(r
,
1182 transport
->push_options
,
1184 oid_array_clear(&commits
);
1185 die(_("failed to push all needed submodules"));
1187 oid_array_clear(&commits
);
1190 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1191 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1192 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1193 !pretend
)) && !is_bare_repository()) {
1194 struct ref
*ref
= remote_refs
;
1195 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1196 struct oid_array commits
= OID_ARRAY_INIT
;
1198 for (; ref
; ref
= ref
->next
)
1199 if (!is_null_oid(&ref
->new_oid
))
1200 oid_array_append(&commits
,
1203 if (find_unpushed_submodules(r
,
1205 transport
->remote
->name
,
1207 oid_array_clear(&commits
);
1208 die_with_unpushed_submodules(&needs_pushing
);
1210 string_list_clear(&needs_pushing
, 0);
1211 oid_array_clear(&commits
);
1214 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
))
1215 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1218 err
= push_had_errors(remote_refs
);
1219 ret
= push_ret
| err
;
1222 transport_print_push_status(transport
->url
, remote_refs
,
1223 verbose
| porcelain
, porcelain
,
1226 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1227 set_upstreams(transport
, remote_refs
, pretend
);
1229 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1230 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1232 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1233 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1236 if (porcelain
&& !push_ret
)
1238 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1239 fprintf(stderr
, "Everything up-to-date\n");
1246 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1247 const struct argv_array
*ref_prefixes
)
1249 if (!transport
->got_remote_refs
) {
1250 transport
->remote_refs
=
1251 transport
->vtable
->get_refs_list(transport
, 0,
1253 transport
->got_remote_refs
= 1;
1256 return transport
->remote_refs
;
1259 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1262 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1263 struct ref
**heads
= NULL
;
1266 if (!transport
->vtable
->fetch_without_list
)
1268 * Some transports (e.g. the built-in bundle transport and the
1269 * transport helper interface) do not work when fetching is
1270 * done immediately after transport creation. List the remote
1271 * refs anyway (if not already listed) as a workaround.
1273 transport_get_remote_refs(transport
, NULL
);
1275 for (rm
= refs
; rm
; rm
= rm
->next
) {
1278 !is_null_oid(&rm
->old_oid
) &&
1279 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1281 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1282 heads
[nr_heads
++] = rm
;
1287 * When deepening of a shallow repository is requested,
1288 * then local and remote refs are likely to still be equal.
1289 * Just feed them all to the fetch method in that case.
1290 * This condition shouldn't be met in a non-deepening fetch
1291 * (see builtin/fetch.c:quickfetch()).
1293 ALLOC_ARRAY(heads
, nr_refs
);
1294 for (rm
= refs
; rm
; rm
= rm
->next
)
1295 heads
[nr_heads
++] = rm
;
1298 rc
= transport
->vtable
->fetch(transport
, nr_heads
, heads
);
1304 void transport_unlock_pack(struct transport
*transport
)
1306 if (transport
->pack_lockfile
) {
1307 unlink_or_warn(transport
->pack_lockfile
);
1308 FREE_AND_NULL(transport
->pack_lockfile
);
1312 int transport_connect(struct transport
*transport
, const char *name
,
1313 const char *exec
, int fd
[2])
1315 if (transport
->vtable
->connect
)
1316 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1318 die(_("operation not supported by protocol"));
1321 int transport_disconnect(struct transport
*transport
)
1324 if (transport
->vtable
->disconnect
)
1325 ret
= transport
->vtable
->disconnect(transport
);
1331 * Strip username (and password) from a URL and return
1332 * it in a newly allocated string.
1334 char *transport_anonymize_url(const char *url
)
1336 char *scheme_prefix
, *anon_part
;
1337 size_t anon_len
, prefix_len
= 0;
1339 anon_part
= strchr(url
, '@');
1340 if (url_is_local_not_ssh(url
) || !anon_part
)
1343 anon_len
= strlen(++anon_part
);
1344 scheme_prefix
= strstr(url
, "://");
1345 if (!scheme_prefix
) {
1346 if (!strchr(anon_part
, ':'))
1347 /* cannot be "me@there:/path/name" */
1351 /* make sure scheme is reasonable */
1352 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1355 case '+': case '.': case '-':
1364 /* @ past the first slash does not count */
1365 cp
= strchr(scheme_prefix
+ 3, '/');
1366 if (cp
&& cp
< anon_part
)
1368 prefix_len
= scheme_prefix
- url
+ 3;
1370 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1371 (int)anon_len
, anon_part
);
1373 return xstrdup(url
);
1376 static void fill_alternate_refs_command(struct child_process
*cmd
,
1377 const char *repo_path
)
1381 if (!git_config_get_value("core.alternateRefsCommand", &value
)) {
1384 argv_array_push(&cmd
->args
, value
);
1385 argv_array_push(&cmd
->args
, repo_path
);
1389 argv_array_pushf(&cmd
->args
, "--git-dir=%s", repo_path
);
1390 argv_array_push(&cmd
->args
, "for-each-ref");
1391 argv_array_push(&cmd
->args
, "--format=%(objectname)");
1393 if (!git_config_get_value("core.alternateRefsPrefixes", &value
)) {
1394 argv_array_push(&cmd
->args
, "--");
1395 argv_array_split(&cmd
->args
, value
);
1399 cmd
->env
= local_repo_env
;
1403 static void read_alternate_refs(const char *path
,
1404 alternate_ref_fn
*cb
,
1407 struct child_process cmd
= CHILD_PROCESS_INIT
;
1408 struct strbuf line
= STRBUF_INIT
;
1411 fill_alternate_refs_command(&cmd
, path
);
1413 if (start_command(&cmd
))
1416 fh
= xfdopen(cmd
.out
, "r");
1417 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
1418 struct object_id oid
;
1421 if (parse_oid_hex(line
.buf
, &oid
, &p
) || *p
) {
1422 warning(_("invalid line while parsing alternate refs: %s"),
1431 finish_command(&cmd
);
1434 struct alternate_refs_data
{
1435 alternate_ref_fn
*fn
;
1439 static int refs_from_alternate_cb(struct object_directory
*e
,
1442 struct strbuf path
= STRBUF_INIT
;
1444 struct alternate_refs_data
*cb
= data
;
1446 if (!strbuf_realpath(&path
, e
->path
, 0))
1448 if (!strbuf_strip_suffix(&path
, "/objects"))
1450 base_len
= path
.len
;
1452 /* Is this a git repository with refs? */
1453 strbuf_addstr(&path
, "/refs");
1454 if (!is_directory(path
.buf
))
1456 strbuf_setlen(&path
, base_len
);
1458 read_alternate_refs(path
.buf
, cb
->fn
, cb
->data
);
1461 strbuf_release(&path
);
1465 void for_each_alternate_ref(alternate_ref_fn fn
, void *data
)
1467 struct alternate_refs_data cb
;
1470 foreach_alt_odb(refs_from_alternate_cb
, &cb
);