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
,
255 static void die_if_server_options(struct transport
*transport
)
257 if (!transport
->server_options
|| !transport
->server_options
->nr
)
259 advise(_("see protocol.version in 'git help config' for more details"));
260 die(_("server options require protocol version 2 or later"));
264 * Obtains the protocol version from the transport and writes it to
265 * transport->data->version, first connecting if not already connected.
267 * If the protocol version is one that allows skipping the listing of remote
268 * refs, and must_list_refs is 0, the listing of remote refs is skipped and
269 * this function returns NULL. Otherwise, this function returns the list of
272 static struct ref
*handshake(struct transport
*transport
, int for_push
,
273 const struct argv_array
*ref_prefixes
,
276 struct git_transport_data
*data
= transport
->data
;
277 struct ref
*refs
= NULL
;
278 struct packet_reader reader
;
280 connect_setup(transport
, for_push
);
282 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
283 PACKET_READ_CHOMP_NEWLINE
|
284 PACKET_READ_GENTLE_ON_EOF
|
285 PACKET_READ_DIE_ON_ERR_PACKET
);
287 data
->version
= discover_version(&reader
);
288 switch (data
->version
) {
291 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
293 transport
->server_options
);
297 die_if_server_options(transport
);
298 get_remote_heads(&reader
, &refs
,
299 for_push
? REF_NORMAL
: 0,
303 case protocol_unknown_version
:
304 BUG("unknown protocol version");
306 data
->got_remote_heads
= 1;
308 if (reader
.line_peeked
)
309 BUG("buffer must be empty at the end of handshake()");
314 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
315 const struct argv_array
*ref_prefixes
)
317 return handshake(transport
, for_push
, ref_prefixes
, 1);
320 static int fetch_refs_via_pack(struct transport
*transport
,
321 int nr_heads
, struct ref
**to_fetch
)
324 struct git_transport_data
*data
= transport
->data
;
325 struct ref
*refs
= NULL
;
326 struct fetch_pack_args args
;
327 struct ref
*refs_tmp
= NULL
;
329 memset(&args
, 0, sizeof(args
));
330 args
.uploadpack
= data
->options
.uploadpack
;
331 args
.keep_pack
= data
->options
.keep
;
333 args
.use_thin_pack
= data
->options
.thin
;
334 args
.include_tag
= data
->options
.followtags
;
335 args
.verbose
= (transport
->verbose
> 1);
336 args
.quiet
= (transport
->verbose
< 0);
337 args
.no_progress
= !transport
->progress
;
338 args
.depth
= data
->options
.depth
;
339 args
.deepen_since
= data
->options
.deepen_since
;
340 args
.deepen_not
= data
->options
.deepen_not
;
341 args
.deepen_relative
= data
->options
.deepen_relative
;
342 args
.check_self_contained_and_connected
=
343 data
->options
.check_self_contained_and_connected
;
344 args
.cloning
= transport
->cloning
;
345 args
.update_shallow
= data
->options
.update_shallow
;
346 args
.from_promisor
= data
->options
.from_promisor
;
347 args
.no_dependents
= data
->options
.no_dependents
;
348 args
.filter_options
= data
->options
.filter_options
;
349 args
.stateless_rpc
= transport
->stateless_rpc
;
350 args
.server_options
= transport
->server_options
;
351 args
.negotiation_tips
= data
->options
.negotiation_tips
;
353 if (!data
->got_remote_heads
) {
355 int must_list_refs
= 0;
356 for (i
= 0; i
< nr_heads
; i
++) {
357 if (!to_fetch
[i
]->exact_oid
) {
362 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
365 switch (data
->version
) {
367 refs
= fetch_pack(&args
, data
->fd
,
368 refs_tmp
? refs_tmp
: transport
->remote_refs
,
369 to_fetch
, nr_heads
, &data
->shallow
,
370 &transport
->pack_lockfile
, data
->version
);
374 die_if_server_options(transport
);
375 refs
= fetch_pack(&args
, data
->fd
,
376 refs_tmp
? refs_tmp
: transport
->remote_refs
,
377 to_fetch
, nr_heads
, &data
->shallow
,
378 &transport
->pack_lockfile
, data
->version
);
380 case protocol_unknown_version
:
381 BUG("unknown protocol version");
386 if (finish_connect(data
->conn
))
389 data
->got_remote_heads
= 0;
390 data
->options
.self_contained_and_connected
=
391 args
.self_contained_and_connected
;
392 data
->options
.connectivity_checked
= args
.connectivity_checked
;
396 if (report_unmatched_refs(to_fetch
, nr_heads
))
404 static int push_had_errors(struct ref
*ref
)
406 for (; ref
; ref
= ref
->next
) {
407 switch (ref
->status
) {
408 case REF_STATUS_NONE
:
409 case REF_STATUS_UPTODATE
:
419 int transport_refs_pushed(struct ref
*ref
)
421 for (; ref
; ref
= ref
->next
) {
422 switch(ref
->status
) {
423 case REF_STATUS_NONE
:
424 case REF_STATUS_UPTODATE
:
433 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
435 struct refspec_item rs
;
437 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
443 if (!remote_find_tracking(remote
, &rs
)) {
445 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
447 delete_ref(NULL
, rs
.dst
, NULL
, 0);
449 update_ref("update by push", rs
.dst
, &ref
->new_oid
,
455 static void print_ref_status(char flag
, const char *summary
,
456 struct ref
*to
, struct ref
*from
, const char *msg
,
457 int porcelain
, int summary_width
)
461 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
463 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
465 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
467 fprintf(stdout
, "%s\n", summary
);
469 const char *red
= "", *reset
= "";
470 if (push_had_errors(to
)) {
471 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
472 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
474 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
477 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
479 fputs(prettify_refname(to
->name
), stderr
);
489 static void print_ok_ref_status(struct ref
*ref
, int porcelain
, int summary_width
)
492 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
493 porcelain
, summary_width
);
494 else if (is_null_oid(&ref
->old_oid
))
495 print_ref_status('*',
496 (starts_with(ref
->name
, "refs/tags/") ? "[new tag]" :
498 ref
, ref
->peer_ref
, NULL
, porcelain
, summary_width
);
500 struct strbuf quickref
= STRBUF_INIT
;
504 strbuf_add_unique_abbrev(&quickref
, &ref
->old_oid
,
506 if (ref
->forced_update
) {
507 strbuf_addstr(&quickref
, "...");
509 msg
= "forced update";
511 strbuf_addstr(&quickref
, "..");
515 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
,
518 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
519 porcelain
, summary_width
);
520 strbuf_release(&quickref
);
524 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
525 int porcelain
, int summary_width
)
528 char *url
= transport_anonymize_url(dest
);
529 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
533 switch(ref
->status
) {
534 case REF_STATUS_NONE
:
535 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
536 porcelain
, summary_width
);
538 case REF_STATUS_REJECT_NODELETE
:
539 print_ref_status('!', "[rejected]", ref
, NULL
,
540 "remote does not support deleting refs",
541 porcelain
, summary_width
);
543 case REF_STATUS_UPTODATE
:
544 print_ref_status('=', "[up to date]", ref
,
545 ref
->peer_ref
, NULL
, porcelain
, summary_width
);
547 case REF_STATUS_REJECT_NONFASTFORWARD
:
548 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
549 "non-fast-forward", porcelain
, summary_width
);
551 case REF_STATUS_REJECT_ALREADY_EXISTS
:
552 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
553 "already exists", porcelain
, summary_width
);
555 case REF_STATUS_REJECT_FETCH_FIRST
:
556 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
557 "fetch first", porcelain
, summary_width
);
559 case REF_STATUS_REJECT_NEEDS_FORCE
:
560 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
561 "needs force", porcelain
, summary_width
);
563 case REF_STATUS_REJECT_STALE
:
564 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
565 "stale info", porcelain
, summary_width
);
567 case REF_STATUS_REJECT_SHALLOW
:
568 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
569 "new shallow roots not allowed",
570 porcelain
, summary_width
);
572 case REF_STATUS_REMOTE_REJECT
:
573 print_ref_status('!', "[remote rejected]", ref
,
574 ref
->deletion
? NULL
: ref
->peer_ref
,
575 ref
->remote_status
, porcelain
, summary_width
);
577 case REF_STATUS_EXPECTING_REPORT
:
578 print_ref_status('!', "[remote failure]", ref
,
579 ref
->deletion
? NULL
: ref
->peer_ref
,
580 "remote failed to report status",
581 porcelain
, summary_width
);
583 case REF_STATUS_ATOMIC_PUSH_FAILED
:
584 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
585 "atomic push failed", porcelain
, summary_width
);
588 print_ok_ref_status(ref
, porcelain
, summary_width
);
595 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
597 char hex
[GIT_MAX_HEXSZ
+ 1];
598 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
600 return (w
< sofar
) ? sofar
: w
;
603 int transport_summary_width(const struct ref
*refs
)
607 for (; refs
; refs
= refs
->next
) {
608 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
609 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
612 maxw
= FALLBACK_DEFAULT_ABBREV
;
613 return (2 * maxw
+ 3);
616 void transport_print_push_status(const char *dest
, struct ref
*refs
,
617 int verbose
, int porcelain
, unsigned int *reject_reasons
)
622 int summary_width
= transport_summary_width(refs
);
624 if (transport_color_config() < 0)
625 warning(_("could not parse transport.color.* config"));
627 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
630 for (ref
= refs
; ref
; ref
= ref
->next
)
631 if (ref
->status
== REF_STATUS_UPTODATE
)
632 n
+= print_one_push_status(ref
, dest
, n
,
633 porcelain
, summary_width
);
636 for (ref
= refs
; ref
; ref
= ref
->next
)
637 if (ref
->status
== REF_STATUS_OK
)
638 n
+= print_one_push_status(ref
, dest
, n
,
639 porcelain
, summary_width
);
642 for (ref
= refs
; ref
; ref
= ref
->next
) {
643 if (ref
->status
!= REF_STATUS_NONE
&&
644 ref
->status
!= REF_STATUS_UPTODATE
&&
645 ref
->status
!= REF_STATUS_OK
)
646 n
+= print_one_push_status(ref
, dest
, n
,
647 porcelain
, summary_width
);
648 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
649 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
650 *reject_reasons
|= REJECT_NON_FF_HEAD
;
652 *reject_reasons
|= REJECT_NON_FF_OTHER
;
653 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
654 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
655 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
656 *reject_reasons
|= REJECT_FETCH_FIRST
;
657 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
658 *reject_reasons
|= REJECT_NEEDS_FORCE
;
664 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
666 struct git_transport_data
*data
= transport
->data
;
667 struct send_pack_args args
;
670 if (transport_color_config() < 0)
673 if (!data
->got_remote_heads
)
674 get_refs_via_connect(transport
, 1, NULL
);
676 memset(&args
, 0, sizeof(args
));
677 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
678 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
679 args
.use_thin_pack
= data
->options
.thin
;
680 args
.verbose
= (transport
->verbose
> 0);
681 args
.quiet
= (transport
->verbose
< 0);
682 args
.progress
= transport
->progress
;
683 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
684 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
685 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
686 args
.push_options
= transport
->push_options
;
687 args
.url
= transport
->url
;
689 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
690 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
691 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
692 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
694 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
696 switch (data
->version
) {
698 die(_("support for protocol v2 not implemented yet"));
702 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
705 case protocol_unknown_version
:
706 BUG("unknown protocol version");
711 ret
|= finish_connect(data
->conn
);
713 data
->got_remote_heads
= 0;
718 static int connect_git(struct transport
*transport
, const char *name
,
719 const char *executable
, int fd
[2])
721 struct git_transport_data
*data
= transport
->data
;
722 data
->conn
= git_connect(data
->fd
, transport
->url
,
729 static int disconnect_git(struct transport
*transport
)
731 struct git_transport_data
*data
= transport
->data
;
733 if (data
->got_remote_heads
)
734 packet_flush(data
->fd
[1]);
737 finish_connect(data
->conn
);
744 static struct transport_vtable taken_over_vtable
= {
747 get_refs_via_connect
,
754 void transport_take_over(struct transport
*transport
,
755 struct child_process
*child
)
757 struct git_transport_data
*data
;
759 if (!transport
->smart_options
)
760 BUG("taking over transport requires non-NULL "
761 "smart_options field.");
763 data
= xcalloc(1, sizeof(*data
));
764 data
->options
= *transport
->smart_options
;
766 data
->fd
[0] = data
->conn
->out
;
767 data
->fd
[1] = data
->conn
->in
;
768 data
->got_remote_heads
= 0;
769 transport
->data
= data
;
771 transport
->vtable
= &taken_over_vtable
;
772 transport
->smart_options
= &(data
->options
);
774 transport
->cannot_reuse
= 1;
777 static int is_file(const char *url
)
782 return S_ISREG(buf
.st_mode
);
785 static int external_specification_len(const char *url
)
787 return strchr(url
, ':') - url
;
790 static const struct string_list
*protocol_whitelist(void)
792 static int enabled
= -1;
793 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
796 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
798 string_list_split(&allowed
, v
, ':', -1);
799 string_list_sort(&allowed
);
806 return enabled
? &allowed
: NULL
;
809 enum protocol_allow_config
{
810 PROTOCOL_ALLOW_NEVER
= 0,
811 PROTOCOL_ALLOW_USER_ONLY
,
812 PROTOCOL_ALLOW_ALWAYS
815 static enum protocol_allow_config
parse_protocol_config(const char *key
,
818 if (!strcasecmp(value
, "always"))
819 return PROTOCOL_ALLOW_ALWAYS
;
820 else if (!strcasecmp(value
, "never"))
821 return PROTOCOL_ALLOW_NEVER
;
822 else if (!strcasecmp(value
, "user"))
823 return PROTOCOL_ALLOW_USER_ONLY
;
825 die(_("unknown value for config '%s': %s"), key
, value
);
828 static enum protocol_allow_config
get_protocol_config(const char *type
)
830 char *key
= xstrfmt("protocol.%s.allow", type
);
833 /* first check the per-protocol config */
834 if (!git_config_get_string(key
, &value
)) {
835 enum protocol_allow_config ret
=
836 parse_protocol_config(key
, value
);
843 /* if defined, fallback to user-defined default for unknown protocols */
844 if (!git_config_get_string("protocol.allow", &value
)) {
845 enum protocol_allow_config ret
=
846 parse_protocol_config("protocol.allow", value
);
851 /* fallback to built-in defaults */
853 if (!strcmp(type
, "http") ||
854 !strcmp(type
, "https") ||
855 !strcmp(type
, "git") ||
856 !strcmp(type
, "ssh") ||
857 !strcmp(type
, "file"))
858 return PROTOCOL_ALLOW_ALWAYS
;
860 /* known scary; err on the side of caution */
861 if (!strcmp(type
, "ext"))
862 return PROTOCOL_ALLOW_NEVER
;
864 /* unknown; by default let them be used only directly by the user */
865 return PROTOCOL_ALLOW_USER_ONLY
;
868 int is_transport_allowed(const char *type
, int from_user
)
870 const struct string_list
*whitelist
= protocol_whitelist();
872 return string_list_has_string(whitelist
, type
);
874 switch (get_protocol_config(type
)) {
875 case PROTOCOL_ALLOW_ALWAYS
:
877 case PROTOCOL_ALLOW_NEVER
:
879 case PROTOCOL_ALLOW_USER_ONLY
:
881 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
885 BUG("invalid protocol_allow_config type");
888 void transport_check_allowed(const char *type
)
890 if (!is_transport_allowed(type
, -1))
891 die(_("transport '%s' not allowed"), type
);
894 static struct transport_vtable bundle_vtable
= {
897 get_refs_from_bundle
,
898 fetch_refs_from_bundle
,
904 static struct transport_vtable builtin_smart_vtable
= {
907 get_refs_via_connect
,
914 struct transport
*transport_get(struct remote
*remote
, const char *url
)
917 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
919 ret
->progress
= isatty(2);
922 BUG("No remote provided to transport_get()");
924 ret
->got_remote_refs
= 0;
925 ret
->remote
= remote
;
926 helper
= remote
->foreign_vcs
;
928 if (!url
&& remote
->url
)
929 url
= remote
->url
[0];
932 /* maybe it is a foreign URL? */
936 while (is_urlschemechar(p
== url
, *p
))
938 if (starts_with(p
, "::"))
939 helper
= xstrndup(url
, p
- url
);
943 transport_helper_init(ret
, helper
);
944 } else if (starts_with(url
, "rsync:")) {
945 die(_("git-over-rsync is no longer supported"));
946 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
947 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
948 transport_check_allowed("file");
950 ret
->vtable
= &bundle_vtable
;
951 ret
->smart_options
= NULL
;
952 } else if (!is_url(url
)
953 || starts_with(url
, "file://")
954 || starts_with(url
, "git://")
955 || starts_with(url
, "ssh://")
956 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
957 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
960 * These are builtin smart transports; "allowed" transports
961 * will be checked individually in git_connect.
963 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
965 ret
->vtable
= &builtin_smart_vtable
;
966 ret
->smart_options
= &(data
->options
);
969 data
->got_remote_heads
= 0;
971 /* Unknown protocol in URL. Pass to external handler. */
972 int len
= external_specification_len(url
);
973 char *handler
= xmemdupz(url
, len
);
974 transport_helper_init(ret
, handler
);
977 if (ret
->smart_options
) {
978 ret
->smart_options
->thin
= 1;
979 ret
->smart_options
->uploadpack
= "git-upload-pack";
980 if (remote
->uploadpack
)
981 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
982 ret
->smart_options
->receivepack
= "git-receive-pack";
983 if (remote
->receivepack
)
984 ret
->smart_options
->receivepack
= remote
->receivepack
;
990 int transport_set_option(struct transport
*transport
,
991 const char *name
, const char *value
)
993 int git_reports
= 1, protocol_reports
= 1;
995 if (transport
->smart_options
)
996 git_reports
= set_git_option(transport
->smart_options
,
999 if (transport
->vtable
->set_option
)
1000 protocol_reports
= transport
->vtable
->set_option(transport
,
1003 /* If either report is 0, report 0 (success). */
1004 if (!git_reports
|| !protocol_reports
)
1006 /* If either reports -1 (invalid value), report -1. */
1007 if ((git_reports
== -1) || (protocol_reports
== -1))
1009 /* Otherwise if both report unknown, report unknown. */
1013 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1017 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1019 transport
->verbose
= -1;
1022 * Rules used to determine whether to report progress (processing aborts
1023 * when a rule is satisfied):
1025 * . Report progress, if force_progress is 1 (ie. --progress).
1026 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1027 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1028 * . Report progress if isatty(2) is 1.
1030 if (force_progress
>= 0)
1031 transport
->progress
= !!force_progress
;
1033 transport
->progress
= verbosity
>= 0 && isatty(2);
1036 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1040 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1041 "not be found on any remote:\n"));
1042 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1043 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1044 fprintf(stderr
, _("\nPlease try\n\n"
1045 " git push --recurse-submodules=on-demand\n\n"
1046 "or cd to the path and use\n\n"
1048 "to push them to a remote.\n\n"));
1050 string_list_clear(needs_pushing
, 0);
1052 die(_("Aborting."));
1055 static int run_pre_push_hook(struct transport
*transport
,
1056 struct ref
*remote_refs
)
1060 struct child_process proc
= CHILD_PROCESS_INIT
;
1062 const char *argv
[4];
1064 if (!(argv
[0] = find_hook("pre-push")))
1067 argv
[1] = transport
->remote
->name
;
1068 argv
[2] = transport
->url
;
1073 proc
.trace2_hook_name
= "pre-push";
1075 if (start_command(&proc
)) {
1076 finish_command(&proc
);
1080 sigchain_push(SIGPIPE
, SIG_IGN
);
1082 strbuf_init(&buf
, 256);
1084 for (r
= remote_refs
; r
; r
= r
->next
) {
1085 if (!r
->peer_ref
) continue;
1086 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1087 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1088 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1091 strbuf_addf( &buf
, "%s %s %s %s\n",
1092 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1093 r
->name
, oid_to_hex(&r
->old_oid
));
1095 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1096 /* We do not mind if a hook does not read all refs. */
1103 strbuf_release(&buf
);
1109 sigchain_pop(SIGPIPE
);
1111 x
= finish_command(&proc
);
1118 int transport_push(struct repository
*r
,
1119 struct transport
*transport
,
1120 struct refspec
*rs
, int flags
,
1121 unsigned int *reject_reasons
)
1123 *reject_reasons
= 0;
1125 if (transport_color_config() < 0)
1128 if (transport
->vtable
->push_refs
) {
1129 struct ref
*remote_refs
;
1130 struct ref
*local_refs
= get_local_heads();
1131 int match_flags
= MATCH_REFS_NONE
;
1132 int verbose
= (transport
->verbose
> 0);
1133 int quiet
= (transport
->verbose
< 0);
1134 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1135 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1136 int push_ret
, ret
, err
;
1137 struct argv_array ref_prefixes
= ARGV_ARRAY_INIT
;
1139 if (check_push_refs(local_refs
, rs
) < 0)
1142 refspec_ref_prefixes(rs
, &ref_prefixes
);
1144 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1147 argv_array_clear(&ref_prefixes
);
1149 if (flags
& TRANSPORT_PUSH_ALL
)
1150 match_flags
|= MATCH_REFS_ALL
;
1151 if (flags
& TRANSPORT_PUSH_MIRROR
)
1152 match_flags
|= MATCH_REFS_MIRROR
;
1153 if (flags
& TRANSPORT_PUSH_PRUNE
)
1154 match_flags
|= MATCH_REFS_PRUNE
;
1155 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1156 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1158 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1161 if (transport
->smart_options
&&
1162 transport
->smart_options
->cas
&&
1163 !is_empty_cas(transport
->smart_options
->cas
))
1164 apply_push_cas(transport
->smart_options
->cas
,
1165 transport
->remote
, remote_refs
);
1167 set_ref_status_for_push(remote_refs
,
1168 flags
& TRANSPORT_PUSH_MIRROR
,
1169 flags
& TRANSPORT_PUSH_FORCE
);
1171 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1172 if (run_pre_push_hook(transport
, remote_refs
))
1175 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1176 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1177 !is_bare_repository()) {
1178 struct ref
*ref
= remote_refs
;
1179 struct oid_array commits
= OID_ARRAY_INIT
;
1181 for (; ref
; ref
= ref
->next
)
1182 if (!is_null_oid(&ref
->new_oid
))
1183 oid_array_append(&commits
,
1186 if (!push_unpushed_submodules(r
,
1190 transport
->push_options
,
1192 oid_array_clear(&commits
);
1193 die(_("failed to push all needed submodules"));
1195 oid_array_clear(&commits
);
1198 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1199 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1200 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1201 !pretend
)) && !is_bare_repository()) {
1202 struct ref
*ref
= remote_refs
;
1203 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1204 struct oid_array commits
= OID_ARRAY_INIT
;
1206 for (; ref
; ref
= ref
->next
)
1207 if (!is_null_oid(&ref
->new_oid
))
1208 oid_array_append(&commits
,
1211 if (find_unpushed_submodules(r
,
1213 transport
->remote
->name
,
1215 oid_array_clear(&commits
);
1216 die_with_unpushed_submodules(&needs_pushing
);
1218 string_list_clear(&needs_pushing
, 0);
1219 oid_array_clear(&commits
);
1222 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
))
1223 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1226 err
= push_had_errors(remote_refs
);
1227 ret
= push_ret
| err
;
1229 if ((flags
& TRANSPORT_PUSH_ATOMIC
) && err
) {
1231 for (it
= remote_refs
; it
; it
= it
->next
)
1232 switch (it
->status
) {
1233 case REF_STATUS_NONE
:
1234 case REF_STATUS_UPTODATE
:
1236 it
->status
= REF_STATUS_ATOMIC_PUSH_FAILED
;
1244 transport_print_push_status(transport
->url
, remote_refs
,
1245 verbose
| porcelain
, porcelain
,
1248 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1249 set_upstreams(transport
, remote_refs
, pretend
);
1251 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1252 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1254 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1255 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1258 if (porcelain
&& !push_ret
)
1260 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1261 fprintf(stderr
, "Everything up-to-date\n");
1268 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1269 const struct argv_array
*ref_prefixes
)
1271 if (!transport
->got_remote_refs
) {
1272 transport
->remote_refs
=
1273 transport
->vtable
->get_refs_list(transport
, 0,
1275 transport
->got_remote_refs
= 1;
1278 return transport
->remote_refs
;
1281 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1284 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1285 struct ref
**heads
= NULL
;
1288 if (!transport
->vtable
->fetch_without_list
)
1290 * Some transports (e.g. the built-in bundle transport and the
1291 * transport helper interface) do not work when fetching is
1292 * done immediately after transport creation. List the remote
1293 * refs anyway (if not already listed) as a workaround.
1295 transport_get_remote_refs(transport
, NULL
);
1297 for (rm
= refs
; rm
; rm
= rm
->next
) {
1300 !is_null_oid(&rm
->old_oid
) &&
1301 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1303 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1304 heads
[nr_heads
++] = rm
;
1309 * When deepening of a shallow repository is requested,
1310 * then local and remote refs are likely to still be equal.
1311 * Just feed them all to the fetch method in that case.
1312 * This condition shouldn't be met in a non-deepening fetch
1313 * (see builtin/fetch.c:quickfetch()).
1315 ALLOC_ARRAY(heads
, nr_refs
);
1316 for (rm
= refs
; rm
; rm
= rm
->next
)
1317 heads
[nr_heads
++] = rm
;
1320 rc
= transport
->vtable
->fetch(transport
, nr_heads
, heads
);
1326 void transport_unlock_pack(struct transport
*transport
)
1328 if (transport
->pack_lockfile
) {
1329 unlink_or_warn(transport
->pack_lockfile
);
1330 FREE_AND_NULL(transport
->pack_lockfile
);
1334 int transport_connect(struct transport
*transport
, const char *name
,
1335 const char *exec
, int fd
[2])
1337 if (transport
->vtable
->connect
)
1338 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1340 die(_("operation not supported by protocol"));
1343 int transport_disconnect(struct transport
*transport
)
1346 if (transport
->vtable
->disconnect
)
1347 ret
= transport
->vtable
->disconnect(transport
);
1353 * Strip username (and password) from a URL and return
1354 * it in a newly allocated string.
1356 char *transport_anonymize_url(const char *url
)
1358 char *scheme_prefix
, *anon_part
;
1359 size_t anon_len
, prefix_len
= 0;
1361 anon_part
= strchr(url
, '@');
1362 if (url_is_local_not_ssh(url
) || !anon_part
)
1365 anon_len
= strlen(++anon_part
);
1366 scheme_prefix
= strstr(url
, "://");
1367 if (!scheme_prefix
) {
1368 if (!strchr(anon_part
, ':'))
1369 /* cannot be "me@there:/path/name" */
1373 /* make sure scheme is reasonable */
1374 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1377 case '+': case '.': case '-':
1386 /* @ past the first slash does not count */
1387 cp
= strchr(scheme_prefix
+ 3, '/');
1388 if (cp
&& cp
< anon_part
)
1390 prefix_len
= scheme_prefix
- url
+ 3;
1392 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1393 (int)anon_len
, anon_part
);
1395 return xstrdup(url
);