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 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
;
125 unsigned get_refs_from_bundle_called
: 1;
128 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
130 const struct argv_array
*ref_prefixes
)
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_NO_DEPENDENTS
)) {
236 opts
->no_dependents
= !!value
;
238 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
239 list_objects_filter_die_if_populated(&opts
->filter_options
);
240 parse_list_objects_filter(&opts
->filter_options
, value
);
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 const struct argv_array
*ref_prefixes
,
289 struct git_transport_data
*data
= transport
->data
;
290 struct ref
*refs
= NULL
;
291 struct packet_reader reader
;
293 connect_setup(transport
, for_push
);
295 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
296 PACKET_READ_CHOMP_NEWLINE
|
297 PACKET_READ_GENTLE_ON_EOF
|
298 PACKET_READ_DIE_ON_ERR_PACKET
);
300 data
->version
= discover_version(&reader
);
301 switch (data
->version
) {
304 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
306 transport
->server_options
);
310 die_if_server_options(transport
);
311 get_remote_heads(&reader
, &refs
,
312 for_push
? REF_NORMAL
: 0,
316 case protocol_unknown_version
:
317 BUG("unknown protocol version");
319 data
->got_remote_heads
= 1;
320 transport
->hash_algo
= reader
.hash_algo
;
322 if (reader
.line_peeked
)
323 BUG("buffer must be empty at the end of handshake()");
328 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
329 const struct argv_array
*ref_prefixes
)
331 return handshake(transport
, for_push
, ref_prefixes
, 1);
334 static int fetch_refs_via_pack(struct transport
*transport
,
335 int nr_heads
, struct ref
**to_fetch
)
338 struct git_transport_data
*data
= transport
->data
;
339 struct ref
*refs
= NULL
;
340 struct fetch_pack_args args
;
341 struct ref
*refs_tmp
= NULL
;
343 memset(&args
, 0, sizeof(args
));
344 args
.uploadpack
= data
->options
.uploadpack
;
345 args
.keep_pack
= data
->options
.keep
;
347 args
.use_thin_pack
= data
->options
.thin
;
348 args
.include_tag
= data
->options
.followtags
;
349 args
.verbose
= (transport
->verbose
> 1);
350 args
.quiet
= (transport
->verbose
< 0);
351 args
.no_progress
= !transport
->progress
;
352 args
.depth
= data
->options
.depth
;
353 args
.deepen_since
= data
->options
.deepen_since
;
354 args
.deepen_not
= data
->options
.deepen_not
;
355 args
.deepen_relative
= data
->options
.deepen_relative
;
356 args
.check_self_contained_and_connected
=
357 data
->options
.check_self_contained_and_connected
;
358 args
.cloning
= transport
->cloning
;
359 args
.update_shallow
= data
->options
.update_shallow
;
360 args
.from_promisor
= data
->options
.from_promisor
;
361 args
.no_dependents
= data
->options
.no_dependents
;
362 args
.filter_options
= data
->options
.filter_options
;
363 args
.stateless_rpc
= transport
->stateless_rpc
;
364 args
.server_options
= transport
->server_options
;
365 args
.negotiation_tips
= data
->options
.negotiation_tips
;
367 if (!data
->got_remote_heads
) {
369 int must_list_refs
= 0;
370 for (i
= 0; i
< nr_heads
; i
++) {
371 if (!to_fetch
[i
]->exact_oid
) {
376 refs_tmp
= handshake(transport
, 0, NULL
, must_list_refs
);
379 switch (data
->version
) {
381 refs
= fetch_pack(&args
, data
->fd
,
382 refs_tmp
? refs_tmp
: transport
->remote_refs
,
383 to_fetch
, nr_heads
, &data
->shallow
,
384 &transport
->pack_lockfile
, data
->version
);
388 die_if_server_options(transport
);
389 refs
= fetch_pack(&args
, data
->fd
,
390 refs_tmp
? refs_tmp
: transport
->remote_refs
,
391 to_fetch
, nr_heads
, &data
->shallow
,
392 &transport
->pack_lockfile
, data
->version
);
394 case protocol_unknown_version
:
395 BUG("unknown protocol version");
400 if (finish_connect(data
->conn
))
403 data
->got_remote_heads
= 0;
404 data
->options
.self_contained_and_connected
=
405 args
.self_contained_and_connected
;
406 data
->options
.connectivity_checked
= args
.connectivity_checked
;
410 if (report_unmatched_refs(to_fetch
, nr_heads
))
418 static int push_had_errors(struct ref
*ref
)
420 for (; ref
; ref
= ref
->next
) {
421 switch (ref
->status
) {
422 case REF_STATUS_NONE
:
423 case REF_STATUS_UPTODATE
:
433 int transport_refs_pushed(struct ref
*ref
)
435 for (; ref
; ref
= ref
->next
) {
436 switch(ref
->status
) {
437 case REF_STATUS_NONE
:
438 case REF_STATUS_UPTODATE
:
447 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
449 struct refspec_item rs
;
451 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
457 if (!remote_find_tracking(remote
, &rs
)) {
459 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
461 delete_ref(NULL
, rs
.dst
, NULL
, 0);
463 update_ref("update by push", rs
.dst
, &ref
->new_oid
,
469 static void print_ref_status(char flag
, const char *summary
,
470 struct ref
*to
, struct ref
*from
, const char *msg
,
471 int porcelain
, int summary_width
)
475 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
477 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
479 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
481 fprintf(stdout
, "%s\n", summary
);
483 const char *red
= "", *reset
= "";
484 if (push_had_errors(to
)) {
485 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
486 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
488 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
491 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
493 fputs(prettify_refname(to
->name
), stderr
);
503 static void print_ok_ref_status(struct ref
*ref
, int porcelain
, int summary_width
)
506 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
507 porcelain
, summary_width
);
508 else if (is_null_oid(&ref
->old_oid
))
509 print_ref_status('*',
510 (starts_with(ref
->name
, "refs/tags/") ? "[new tag]" :
512 ref
, ref
->peer_ref
, NULL
, porcelain
, summary_width
);
514 struct strbuf quickref
= STRBUF_INIT
;
518 strbuf_add_unique_abbrev(&quickref
, &ref
->old_oid
,
520 if (ref
->forced_update
) {
521 strbuf_addstr(&quickref
, "...");
523 msg
= "forced update";
525 strbuf_addstr(&quickref
, "..");
529 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
,
532 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
533 porcelain
, summary_width
);
534 strbuf_release(&quickref
);
538 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
539 int porcelain
, int summary_width
)
542 char *url
= transport_anonymize_url(dest
);
543 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
547 switch(ref
->status
) {
548 case REF_STATUS_NONE
:
549 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
550 porcelain
, summary_width
);
552 case REF_STATUS_REJECT_NODELETE
:
553 print_ref_status('!', "[rejected]", ref
, NULL
,
554 "remote does not support deleting refs",
555 porcelain
, summary_width
);
557 case REF_STATUS_UPTODATE
:
558 print_ref_status('=', "[up to date]", ref
,
559 ref
->peer_ref
, NULL
, porcelain
, summary_width
);
561 case REF_STATUS_REJECT_NONFASTFORWARD
:
562 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
563 "non-fast-forward", porcelain
, summary_width
);
565 case REF_STATUS_REJECT_ALREADY_EXISTS
:
566 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
567 "already exists", porcelain
, summary_width
);
569 case REF_STATUS_REJECT_FETCH_FIRST
:
570 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
571 "fetch first", porcelain
, summary_width
);
573 case REF_STATUS_REJECT_NEEDS_FORCE
:
574 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
575 "needs force", porcelain
, summary_width
);
577 case REF_STATUS_REJECT_STALE
:
578 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
579 "stale info", porcelain
, summary_width
);
581 case REF_STATUS_REJECT_SHALLOW
:
582 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
583 "new shallow roots not allowed",
584 porcelain
, summary_width
);
586 case REF_STATUS_REMOTE_REJECT
:
587 print_ref_status('!', "[remote rejected]", ref
,
588 ref
->deletion
? NULL
: ref
->peer_ref
,
589 ref
->remote_status
, porcelain
, summary_width
);
591 case REF_STATUS_EXPECTING_REPORT
:
592 print_ref_status('!', "[remote failure]", ref
,
593 ref
->deletion
? NULL
: ref
->peer_ref
,
594 "remote failed to report status",
595 porcelain
, summary_width
);
597 case REF_STATUS_ATOMIC_PUSH_FAILED
:
598 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
599 "atomic push failed", porcelain
, summary_width
);
602 print_ok_ref_status(ref
, porcelain
, summary_width
);
609 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
611 char hex
[GIT_MAX_HEXSZ
+ 1];
612 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
614 return (w
< sofar
) ? sofar
: w
;
617 int transport_summary_width(const struct ref
*refs
)
621 for (; refs
; refs
= refs
->next
) {
622 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
623 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
626 maxw
= FALLBACK_DEFAULT_ABBREV
;
627 return (2 * maxw
+ 3);
630 void transport_print_push_status(const char *dest
, struct ref
*refs
,
631 int verbose
, int porcelain
, unsigned int *reject_reasons
)
636 int summary_width
= transport_summary_width(refs
);
638 if (transport_color_config() < 0)
639 warning(_("could not parse transport.color.* config"));
641 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
644 for (ref
= refs
; ref
; ref
= ref
->next
)
645 if (ref
->status
== REF_STATUS_UPTODATE
)
646 n
+= print_one_push_status(ref
, dest
, n
,
647 porcelain
, summary_width
);
650 for (ref
= refs
; ref
; ref
= ref
->next
)
651 if (ref
->status
== REF_STATUS_OK
)
652 n
+= print_one_push_status(ref
, dest
, n
,
653 porcelain
, summary_width
);
656 for (ref
= refs
; ref
; ref
= ref
->next
) {
657 if (ref
->status
!= REF_STATUS_NONE
&&
658 ref
->status
!= REF_STATUS_UPTODATE
&&
659 ref
->status
!= REF_STATUS_OK
)
660 n
+= print_one_push_status(ref
, dest
, n
,
661 porcelain
, summary_width
);
662 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
663 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
664 *reject_reasons
|= REJECT_NON_FF_HEAD
;
666 *reject_reasons
|= REJECT_NON_FF_OTHER
;
667 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
668 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
669 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
670 *reject_reasons
|= REJECT_FETCH_FIRST
;
671 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
672 *reject_reasons
|= REJECT_NEEDS_FORCE
;
678 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
680 struct git_transport_data
*data
= transport
->data
;
681 struct send_pack_args args
;
684 if (transport_color_config() < 0)
687 if (!data
->got_remote_heads
)
688 get_refs_via_connect(transport
, 1, NULL
);
690 memset(&args
, 0, sizeof(args
));
691 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
692 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
693 args
.use_thin_pack
= data
->options
.thin
;
694 args
.verbose
= (transport
->verbose
> 0);
695 args
.quiet
= (transport
->verbose
< 0);
696 args
.progress
= transport
->progress
;
697 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
698 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
699 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
700 args
.push_options
= transport
->push_options
;
701 args
.url
= transport
->url
;
703 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
704 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
705 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
706 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
708 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
710 switch (data
->version
) {
712 die(_("support for protocol v2 not implemented yet"));
716 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
719 case protocol_unknown_version
:
720 BUG("unknown protocol version");
726 * Atomic push may abort the connection early and close the pipe,
727 * which may cause an error for `finish_connect()`. Ignore this error
728 * for atomic git-push.
730 if (ret
|| args
.atomic
)
731 finish_connect(data
->conn
);
733 ret
= finish_connect(data
->conn
);
735 data
->got_remote_heads
= 0;
740 static int connect_git(struct transport
*transport
, const char *name
,
741 const char *executable
, int fd
[2])
743 struct git_transport_data
*data
= transport
->data
;
744 data
->conn
= git_connect(data
->fd
, transport
->url
,
751 static int disconnect_git(struct transport
*transport
)
753 struct git_transport_data
*data
= transport
->data
;
755 if (data
->got_remote_heads
&& !transport
->stateless_rpc
)
756 packet_flush(data
->fd
[1]);
759 finish_connect(data
->conn
);
766 static struct transport_vtable taken_over_vtable
= {
768 get_refs_via_connect
,
775 void transport_take_over(struct transport
*transport
,
776 struct child_process
*child
)
778 struct git_transport_data
*data
;
780 if (!transport
->smart_options
)
781 BUG("taking over transport requires non-NULL "
782 "smart_options field.");
784 data
= xcalloc(1, sizeof(*data
));
785 data
->options
= *transport
->smart_options
;
787 data
->fd
[0] = data
->conn
->out
;
788 data
->fd
[1] = data
->conn
->in
;
789 data
->got_remote_heads
= 0;
790 transport
->data
= data
;
792 transport
->vtable
= &taken_over_vtable
;
793 transport
->smart_options
= &(data
->options
);
795 transport
->cannot_reuse
= 1;
798 static int is_file(const char *url
)
803 return S_ISREG(buf
.st_mode
);
806 static int external_specification_len(const char *url
)
808 return strchr(url
, ':') - url
;
811 static const struct string_list
*protocol_whitelist(void)
813 static int enabled
= -1;
814 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
817 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
819 string_list_split(&allowed
, v
, ':', -1);
820 string_list_sort(&allowed
);
827 return enabled
? &allowed
: NULL
;
830 enum protocol_allow_config
{
831 PROTOCOL_ALLOW_NEVER
= 0,
832 PROTOCOL_ALLOW_USER_ONLY
,
833 PROTOCOL_ALLOW_ALWAYS
836 static enum protocol_allow_config
parse_protocol_config(const char *key
,
839 if (!strcasecmp(value
, "always"))
840 return PROTOCOL_ALLOW_ALWAYS
;
841 else if (!strcasecmp(value
, "never"))
842 return PROTOCOL_ALLOW_NEVER
;
843 else if (!strcasecmp(value
, "user"))
844 return PROTOCOL_ALLOW_USER_ONLY
;
846 die(_("unknown value for config '%s': %s"), key
, value
);
849 static enum protocol_allow_config
get_protocol_config(const char *type
)
851 char *key
= xstrfmt("protocol.%s.allow", type
);
854 /* first check the per-protocol config */
855 if (!git_config_get_string(key
, &value
)) {
856 enum protocol_allow_config ret
=
857 parse_protocol_config(key
, value
);
864 /* if defined, fallback to user-defined default for unknown protocols */
865 if (!git_config_get_string("protocol.allow", &value
)) {
866 enum protocol_allow_config ret
=
867 parse_protocol_config("protocol.allow", value
);
872 /* fallback to built-in defaults */
874 if (!strcmp(type
, "http") ||
875 !strcmp(type
, "https") ||
876 !strcmp(type
, "git") ||
877 !strcmp(type
, "ssh") ||
878 !strcmp(type
, "file"))
879 return PROTOCOL_ALLOW_ALWAYS
;
881 /* known scary; err on the side of caution */
882 if (!strcmp(type
, "ext"))
883 return PROTOCOL_ALLOW_NEVER
;
885 /* unknown; by default let them be used only directly by the user */
886 return PROTOCOL_ALLOW_USER_ONLY
;
889 int is_transport_allowed(const char *type
, int from_user
)
891 const struct string_list
*whitelist
= protocol_whitelist();
893 return string_list_has_string(whitelist
, type
);
895 switch (get_protocol_config(type
)) {
896 case PROTOCOL_ALLOW_ALWAYS
:
898 case PROTOCOL_ALLOW_NEVER
:
900 case PROTOCOL_ALLOW_USER_ONLY
:
902 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
906 BUG("invalid protocol_allow_config type");
909 void transport_check_allowed(const char *type
)
911 if (!is_transport_allowed(type
, -1))
912 die(_("transport '%s' not allowed"), type
);
915 static struct transport_vtable bundle_vtable
= {
917 get_refs_from_bundle
,
918 fetch_refs_from_bundle
,
924 static struct transport_vtable builtin_smart_vtable
= {
926 get_refs_via_connect
,
933 struct transport
*transport_get(struct remote
*remote
, const char *url
)
936 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
938 ret
->progress
= isatty(2);
941 BUG("No remote provided to transport_get()");
943 ret
->got_remote_refs
= 0;
944 ret
->remote
= remote
;
945 helper
= remote
->foreign_vcs
;
947 if (!url
&& remote
->url
)
948 url
= remote
->url
[0];
951 /* maybe it is a foreign URL? */
955 while (is_urlschemechar(p
== url
, *p
))
957 if (starts_with(p
, "::"))
958 helper
= xstrndup(url
, p
- url
);
962 transport_helper_init(ret
, helper
);
963 } else if (starts_with(url
, "rsync:")) {
964 die(_("git-over-rsync is no longer supported"));
965 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
966 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
967 transport_check_allowed("file");
969 ret
->vtable
= &bundle_vtable
;
970 ret
->smart_options
= NULL
;
971 } else if (!is_url(url
)
972 || starts_with(url
, "file://")
973 || starts_with(url
, "git://")
974 || starts_with(url
, "ssh://")
975 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
976 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
979 * These are builtin smart transports; "allowed" transports
980 * will be checked individually in git_connect.
982 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
984 ret
->vtable
= &builtin_smart_vtable
;
985 ret
->smart_options
= &(data
->options
);
988 data
->got_remote_heads
= 0;
990 /* Unknown protocol in URL. Pass to external handler. */
991 int len
= external_specification_len(url
);
992 char *handler
= xmemdupz(url
, len
);
993 transport_helper_init(ret
, handler
);
996 if (ret
->smart_options
) {
997 ret
->smart_options
->thin
= 1;
998 ret
->smart_options
->uploadpack
= "git-upload-pack";
999 if (remote
->uploadpack
)
1000 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1001 ret
->smart_options
->receivepack
= "git-receive-pack";
1002 if (remote
->receivepack
)
1003 ret
->smart_options
->receivepack
= remote
->receivepack
;
1006 ret
->hash_algo
= &hash_algos
[GIT_HASH_SHA1
];
1011 const struct git_hash_algo
*transport_get_hash_algo(struct transport
*transport
)
1013 return transport
->hash_algo
;
1016 int transport_set_option(struct transport
*transport
,
1017 const char *name
, const char *value
)
1019 int git_reports
= 1, protocol_reports
= 1;
1021 if (transport
->smart_options
)
1022 git_reports
= set_git_option(transport
->smart_options
,
1025 if (transport
->vtable
->set_option
)
1026 protocol_reports
= transport
->vtable
->set_option(transport
,
1029 /* If either report is 0, report 0 (success). */
1030 if (!git_reports
|| !protocol_reports
)
1032 /* If either reports -1 (invalid value), report -1. */
1033 if ((git_reports
== -1) || (protocol_reports
== -1))
1035 /* Otherwise if both report unknown, report unknown. */
1039 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
1043 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
1045 transport
->verbose
= -1;
1048 * Rules used to determine whether to report progress (processing aborts
1049 * when a rule is satisfied):
1051 * . Report progress, if force_progress is 1 (ie. --progress).
1052 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
1053 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1054 * . Report progress if isatty(2) is 1.
1056 if (force_progress
>= 0)
1057 transport
->progress
= !!force_progress
;
1059 transport
->progress
= verbosity
>= 0 && isatty(2);
1062 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1066 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1067 "not be found on any remote:\n"));
1068 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1069 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1070 fprintf(stderr
, _("\nPlease try\n\n"
1071 " git push --recurse-submodules=on-demand\n\n"
1072 "or cd to the path and use\n\n"
1074 "to push them to a remote.\n\n"));
1076 string_list_clear(needs_pushing
, 0);
1078 die(_("Aborting."));
1081 static int run_pre_push_hook(struct transport
*transport
,
1082 struct ref
*remote_refs
)
1086 struct child_process proc
= CHILD_PROCESS_INIT
;
1088 const char *argv
[4];
1090 if (!(argv
[0] = find_hook("pre-push")))
1093 argv
[1] = transport
->remote
->name
;
1094 argv
[2] = transport
->url
;
1099 proc
.trace2_hook_name
= "pre-push";
1101 if (start_command(&proc
)) {
1102 finish_command(&proc
);
1106 sigchain_push(SIGPIPE
, SIG_IGN
);
1108 strbuf_init(&buf
, 256);
1110 for (r
= remote_refs
; r
; r
= r
->next
) {
1111 if (!r
->peer_ref
) continue;
1112 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1113 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1114 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1117 strbuf_addf( &buf
, "%s %s %s %s\n",
1118 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1119 r
->name
, oid_to_hex(&r
->old_oid
));
1121 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1122 /* We do not mind if a hook does not read all refs. */
1129 strbuf_release(&buf
);
1135 sigchain_pop(SIGPIPE
);
1137 x
= finish_command(&proc
);
1144 int transport_push(struct repository
*r
,
1145 struct transport
*transport
,
1146 struct refspec
*rs
, int flags
,
1147 unsigned int *reject_reasons
)
1149 *reject_reasons
= 0;
1151 if (transport_color_config() < 0)
1154 if (transport
->vtable
->push_refs
) {
1155 struct ref
*remote_refs
;
1156 struct ref
*local_refs
= get_local_heads();
1157 int match_flags
= MATCH_REFS_NONE
;
1158 int verbose
= (transport
->verbose
> 0);
1159 int quiet
= (transport
->verbose
< 0);
1160 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1161 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1162 int push_ret
, ret
, err
;
1163 struct argv_array ref_prefixes
= ARGV_ARRAY_INIT
;
1165 if (check_push_refs(local_refs
, rs
) < 0)
1168 refspec_ref_prefixes(rs
, &ref_prefixes
);
1170 trace2_region_enter("transport_push", "get_refs_list", r
);
1171 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1173 trace2_region_leave("transport_push", "get_refs_list", r
);
1175 argv_array_clear(&ref_prefixes
);
1177 if (flags
& TRANSPORT_PUSH_ALL
)
1178 match_flags
|= MATCH_REFS_ALL
;
1179 if (flags
& TRANSPORT_PUSH_MIRROR
)
1180 match_flags
|= MATCH_REFS_MIRROR
;
1181 if (flags
& TRANSPORT_PUSH_PRUNE
)
1182 match_flags
|= MATCH_REFS_PRUNE
;
1183 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1184 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1186 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1189 if (transport
->smart_options
&&
1190 transport
->smart_options
->cas
&&
1191 !is_empty_cas(transport
->smart_options
->cas
))
1192 apply_push_cas(transport
->smart_options
->cas
,
1193 transport
->remote
, remote_refs
);
1195 set_ref_status_for_push(remote_refs
,
1196 flags
& TRANSPORT_PUSH_MIRROR
,
1197 flags
& TRANSPORT_PUSH_FORCE
);
1199 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1200 if (run_pre_push_hook(transport
, remote_refs
))
1203 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1204 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1205 !is_bare_repository()) {
1206 struct ref
*ref
= remote_refs
;
1207 struct oid_array commits
= OID_ARRAY_INIT
;
1209 trace2_region_enter("transport_push", "push_submodules", r
);
1210 for (; ref
; ref
= ref
->next
)
1211 if (!is_null_oid(&ref
->new_oid
))
1212 oid_array_append(&commits
,
1215 if (!push_unpushed_submodules(r
,
1219 transport
->push_options
,
1221 oid_array_clear(&commits
);
1222 trace2_region_leave("transport_push", "push_submodules", r
);
1223 die(_("failed to push all needed submodules"));
1225 oid_array_clear(&commits
);
1226 trace2_region_leave("transport_push", "push_submodules", r
);
1229 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1230 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1231 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1232 !pretend
)) && !is_bare_repository()) {
1233 struct ref
*ref
= remote_refs
;
1234 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1235 struct oid_array commits
= OID_ARRAY_INIT
;
1237 trace2_region_enter("transport_push", "check_submodules", r
);
1238 for (; ref
; ref
= ref
->next
)
1239 if (!is_null_oid(&ref
->new_oid
))
1240 oid_array_append(&commits
,
1243 if (find_unpushed_submodules(r
,
1245 transport
->remote
->name
,
1247 oid_array_clear(&commits
);
1248 trace2_region_leave("transport_push", "check_submodules", r
);
1249 die_with_unpushed_submodules(&needs_pushing
);
1251 string_list_clear(&needs_pushing
, 0);
1252 oid_array_clear(&commits
);
1253 trace2_region_leave("transport_push", "check_submodules", r
);
1256 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
)) {
1257 trace2_region_enter("transport_push", "push_refs", r
);
1258 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1259 trace2_region_leave("transport_push", "push_refs", r
);
1262 err
= push_had_errors(remote_refs
);
1263 ret
= push_ret
| err
;
1266 transport_print_push_status(transport
->url
, remote_refs
,
1267 verbose
| porcelain
, porcelain
,
1270 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1271 set_upstreams(transport
, remote_refs
, pretend
);
1273 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1274 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1276 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1277 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1280 if (porcelain
&& !push_ret
)
1282 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1283 fprintf(stderr
, "Everything up-to-date\n");
1290 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1291 const struct argv_array
*ref_prefixes
)
1293 if (!transport
->got_remote_refs
) {
1294 transport
->remote_refs
=
1295 transport
->vtable
->get_refs_list(transport
, 0,
1297 transport
->got_remote_refs
= 1;
1300 return transport
->remote_refs
;
1303 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1306 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1307 struct ref
**heads
= NULL
;
1310 for (rm
= refs
; rm
; rm
= rm
->next
) {
1313 !is_null_oid(&rm
->old_oid
) &&
1314 oideq(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1316 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1317 heads
[nr_heads
++] = rm
;
1322 * When deepening of a shallow repository is requested,
1323 * then local and remote refs are likely to still be equal.
1324 * Just feed them all to the fetch method in that case.
1325 * This condition shouldn't be met in a non-deepening fetch
1326 * (see builtin/fetch.c:quickfetch()).
1328 ALLOC_ARRAY(heads
, nr_refs
);
1329 for (rm
= refs
; rm
; rm
= rm
->next
)
1330 heads
[nr_heads
++] = rm
;
1333 rc
= transport
->vtable
->fetch(transport
, nr_heads
, heads
);
1339 void transport_unlock_pack(struct transport
*transport
)
1341 if (transport
->pack_lockfile
) {
1342 unlink_or_warn(transport
->pack_lockfile
);
1343 FREE_AND_NULL(transport
->pack_lockfile
);
1347 int transport_connect(struct transport
*transport
, const char *name
,
1348 const char *exec
, int fd
[2])
1350 if (transport
->vtable
->connect
)
1351 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1353 die(_("operation not supported by protocol"));
1356 int transport_disconnect(struct transport
*transport
)
1359 if (transport
->vtable
->disconnect
)
1360 ret
= transport
->vtable
->disconnect(transport
);
1366 * Strip username (and password) from a URL and return
1367 * it in a newly allocated string.
1369 char *transport_anonymize_url(const char *url
)
1371 char *scheme_prefix
, *anon_part
;
1372 size_t anon_len
, prefix_len
= 0;
1374 anon_part
= strchr(url
, '@');
1375 if (url_is_local_not_ssh(url
) || !anon_part
)
1378 anon_len
= strlen(++anon_part
);
1379 scheme_prefix
= strstr(url
, "://");
1380 if (!scheme_prefix
) {
1381 if (!strchr(anon_part
, ':'))
1382 /* cannot be "me@there:/path/name" */
1386 /* make sure scheme is reasonable */
1387 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1390 case '+': case '.': case '-':
1399 /* @ past the first slash does not count */
1400 cp
= strchr(scheme_prefix
+ 3, '/');
1401 if (cp
&& cp
< anon_part
)
1403 prefix_len
= scheme_prefix
- url
+ 3;
1405 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1406 (int)anon_len
, anon_part
);
1408 return xstrdup(url
);