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(&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 struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
256 const struct argv_array
*ref_prefixes
)
258 struct git_transport_data
*data
= transport
->data
;
259 struct ref
*refs
= NULL
;
260 struct packet_reader reader
;
262 connect_setup(transport
, for_push
);
264 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
265 PACKET_READ_CHOMP_NEWLINE
|
266 PACKET_READ_GENTLE_ON_EOF
);
268 data
->version
= discover_version(&reader
);
269 switch (data
->version
) {
271 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
272 ref_prefixes
, transport
->server_options
);
276 get_remote_heads(&reader
, &refs
,
277 for_push
? REF_NORMAL
: 0,
281 case protocol_unknown_version
:
282 BUG("unknown protocol version");
284 data
->got_remote_heads
= 1;
289 static int fetch_refs_via_pack(struct transport
*transport
,
290 int nr_heads
, struct ref
**to_fetch
)
293 struct git_transport_data
*data
= transport
->data
;
294 struct ref
*refs
= NULL
;
295 char *dest
= xstrdup(transport
->url
);
296 struct fetch_pack_args args
;
297 struct ref
*refs_tmp
= NULL
;
299 memset(&args
, 0, sizeof(args
));
300 args
.uploadpack
= data
->options
.uploadpack
;
301 args
.keep_pack
= data
->options
.keep
;
303 args
.use_thin_pack
= data
->options
.thin
;
304 args
.include_tag
= data
->options
.followtags
;
305 args
.verbose
= (transport
->verbose
> 1);
306 args
.quiet
= (transport
->verbose
< 0);
307 args
.no_progress
= !transport
->progress
;
308 args
.depth
= data
->options
.depth
;
309 args
.deepen_since
= data
->options
.deepen_since
;
310 args
.deepen_not
= data
->options
.deepen_not
;
311 args
.deepen_relative
= data
->options
.deepen_relative
;
312 args
.check_self_contained_and_connected
=
313 data
->options
.check_self_contained_and_connected
;
314 args
.cloning
= transport
->cloning
;
315 args
.update_shallow
= data
->options
.update_shallow
;
316 args
.from_promisor
= data
->options
.from_promisor
;
317 args
.no_dependents
= data
->options
.no_dependents
;
318 args
.filter_options
= data
->options
.filter_options
;
319 args
.stateless_rpc
= transport
->stateless_rpc
;
320 args
.server_options
= transport
->server_options
;
321 args
.negotiation_tips
= data
->options
.negotiation_tips
;
323 if (!data
->got_remote_heads
)
324 refs_tmp
= get_refs_via_connect(transport
, 0, NULL
);
326 switch (data
->version
) {
328 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
329 refs_tmp
? refs_tmp
: transport
->remote_refs
,
330 dest
, to_fetch
, nr_heads
, &data
->shallow
,
331 &transport
->pack_lockfile
, data
->version
);
335 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
336 refs_tmp
? refs_tmp
: transport
->remote_refs
,
337 dest
, to_fetch
, nr_heads
, &data
->shallow
,
338 &transport
->pack_lockfile
, data
->version
);
340 case protocol_unknown_version
:
341 BUG("unknown protocol version");
346 if (finish_connect(data
->conn
))
349 data
->got_remote_heads
= 0;
350 data
->options
.self_contained_and_connected
=
351 args
.self_contained_and_connected
;
355 if (report_unmatched_refs(to_fetch
, nr_heads
))
364 static int push_had_errors(struct ref
*ref
)
366 for (; ref
; ref
= ref
->next
) {
367 switch (ref
->status
) {
368 case REF_STATUS_NONE
:
369 case REF_STATUS_UPTODATE
:
379 int transport_refs_pushed(struct ref
*ref
)
381 for (; ref
; ref
= ref
->next
) {
382 switch(ref
->status
) {
383 case REF_STATUS_NONE
:
384 case REF_STATUS_UPTODATE
:
393 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
395 struct refspec_item rs
;
397 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
403 if (!remote_find_tracking(remote
, &rs
)) {
405 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
407 delete_ref(NULL
, rs
.dst
, NULL
, 0);
409 update_ref("update by push", rs
.dst
, &ref
->new_oid
,
415 static void print_ref_status(char flag
, const char *summary
,
416 struct ref
*to
, struct ref
*from
, const char *msg
,
417 int porcelain
, int summary_width
)
421 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
423 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
425 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
427 fprintf(stdout
, "%s\n", summary
);
429 const char *red
= "", *reset
= "";
430 if (push_had_errors(to
)) {
431 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
432 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
434 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
437 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
439 fputs(prettify_refname(to
->name
), stderr
);
449 static void print_ok_ref_status(struct ref
*ref
, int porcelain
, int summary_width
)
452 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
453 porcelain
, summary_width
);
454 else if (is_null_oid(&ref
->old_oid
))
455 print_ref_status('*',
456 (starts_with(ref
->name
, "refs/tags/") ? "[new tag]" :
458 ref
, ref
->peer_ref
, NULL
, porcelain
, summary_width
);
460 struct strbuf quickref
= STRBUF_INIT
;
464 strbuf_add_unique_abbrev(&quickref
, &ref
->old_oid
,
466 if (ref
->forced_update
) {
467 strbuf_addstr(&quickref
, "...");
469 msg
= "forced update";
471 strbuf_addstr(&quickref
, "..");
475 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
,
478 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
479 porcelain
, summary_width
);
480 strbuf_release(&quickref
);
484 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
485 int porcelain
, int summary_width
)
488 char *url
= transport_anonymize_url(dest
);
489 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
493 switch(ref
->status
) {
494 case REF_STATUS_NONE
:
495 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
496 porcelain
, summary_width
);
498 case REF_STATUS_REJECT_NODELETE
:
499 print_ref_status('!', "[rejected]", ref
, NULL
,
500 "remote does not support deleting refs",
501 porcelain
, summary_width
);
503 case REF_STATUS_UPTODATE
:
504 print_ref_status('=', "[up to date]", ref
,
505 ref
->peer_ref
, NULL
, porcelain
, summary_width
);
507 case REF_STATUS_REJECT_NONFASTFORWARD
:
508 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
509 "non-fast-forward", porcelain
, summary_width
);
511 case REF_STATUS_REJECT_ALREADY_EXISTS
:
512 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
513 "already exists", porcelain
, summary_width
);
515 case REF_STATUS_REJECT_FETCH_FIRST
:
516 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
517 "fetch first", porcelain
, summary_width
);
519 case REF_STATUS_REJECT_NEEDS_FORCE
:
520 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
521 "needs force", porcelain
, summary_width
);
523 case REF_STATUS_REJECT_STALE
:
524 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
525 "stale info", porcelain
, summary_width
);
527 case REF_STATUS_REJECT_SHALLOW
:
528 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
529 "new shallow roots not allowed",
530 porcelain
, summary_width
);
532 case REF_STATUS_REMOTE_REJECT
:
533 print_ref_status('!', "[remote rejected]", ref
,
534 ref
->deletion
? NULL
: ref
->peer_ref
,
535 ref
->remote_status
, porcelain
, summary_width
);
537 case REF_STATUS_EXPECTING_REPORT
:
538 print_ref_status('!', "[remote failure]", ref
,
539 ref
->deletion
? NULL
: ref
->peer_ref
,
540 "remote failed to report status",
541 porcelain
, summary_width
);
543 case REF_STATUS_ATOMIC_PUSH_FAILED
:
544 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
545 "atomic push failed", porcelain
, summary_width
);
548 print_ok_ref_status(ref
, porcelain
, summary_width
);
555 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
557 char hex
[GIT_MAX_HEXSZ
+ 1];
558 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
560 return (w
< sofar
) ? sofar
: w
;
563 int transport_summary_width(const struct ref
*refs
)
567 for (; refs
; refs
= refs
->next
) {
568 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
569 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
572 maxw
= FALLBACK_DEFAULT_ABBREV
;
573 return (2 * maxw
+ 3);
576 void transport_print_push_status(const char *dest
, struct ref
*refs
,
577 int verbose
, int porcelain
, unsigned int *reject_reasons
)
582 int summary_width
= transport_summary_width(refs
);
584 if (transport_color_config() < 0)
585 warning(_("could not parse transport.color.* config"));
587 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
590 for (ref
= refs
; ref
; ref
= ref
->next
)
591 if (ref
->status
== REF_STATUS_UPTODATE
)
592 n
+= print_one_push_status(ref
, dest
, n
,
593 porcelain
, summary_width
);
596 for (ref
= refs
; ref
; ref
= ref
->next
)
597 if (ref
->status
== REF_STATUS_OK
)
598 n
+= print_one_push_status(ref
, dest
, n
,
599 porcelain
, summary_width
);
602 for (ref
= refs
; ref
; ref
= ref
->next
) {
603 if (ref
->status
!= REF_STATUS_NONE
&&
604 ref
->status
!= REF_STATUS_UPTODATE
&&
605 ref
->status
!= REF_STATUS_OK
)
606 n
+= print_one_push_status(ref
, dest
, n
,
607 porcelain
, summary_width
);
608 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
609 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
610 *reject_reasons
|= REJECT_NON_FF_HEAD
;
612 *reject_reasons
|= REJECT_NON_FF_OTHER
;
613 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
614 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
615 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
616 *reject_reasons
|= REJECT_FETCH_FIRST
;
617 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
618 *reject_reasons
|= REJECT_NEEDS_FORCE
;
624 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
626 struct git_transport_data
*data
= transport
->data
;
627 struct send_pack_args args
;
630 if (transport_color_config() < 0)
633 if (!data
->got_remote_heads
)
634 get_refs_via_connect(transport
, 1, NULL
);
636 memset(&args
, 0, sizeof(args
));
637 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
638 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
639 args
.use_thin_pack
= data
->options
.thin
;
640 args
.verbose
= (transport
->verbose
> 0);
641 args
.quiet
= (transport
->verbose
< 0);
642 args
.progress
= transport
->progress
;
643 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
644 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
645 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
646 args
.push_options
= transport
->push_options
;
647 args
.url
= transport
->url
;
649 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
650 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
651 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
652 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
654 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
656 switch (data
->version
) {
658 die("support for protocol v2 not implemented yet");
662 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
665 case protocol_unknown_version
:
666 BUG("unknown protocol version");
671 ret
|= finish_connect(data
->conn
);
673 data
->got_remote_heads
= 0;
678 static int connect_git(struct transport
*transport
, const char *name
,
679 const char *executable
, int fd
[2])
681 struct git_transport_data
*data
= transport
->data
;
682 data
->conn
= git_connect(data
->fd
, transport
->url
,
689 static int disconnect_git(struct transport
*transport
)
691 struct git_transport_data
*data
= transport
->data
;
693 if (data
->got_remote_heads
)
694 packet_flush(data
->fd
[1]);
697 finish_connect(data
->conn
);
704 static struct transport_vtable taken_over_vtable
= {
706 get_refs_via_connect
,
713 void transport_take_over(struct transport
*transport
,
714 struct child_process
*child
)
716 struct git_transport_data
*data
;
718 if (!transport
->smart_options
)
719 BUG("taking over transport requires non-NULL "
720 "smart_options field.");
722 data
= xcalloc(1, sizeof(*data
));
723 data
->options
= *transport
->smart_options
;
725 data
->fd
[0] = data
->conn
->out
;
726 data
->fd
[1] = data
->conn
->in
;
727 data
->got_remote_heads
= 0;
728 transport
->data
= data
;
730 transport
->vtable
= &taken_over_vtable
;
731 transport
->smart_options
= &(data
->options
);
733 transport
->cannot_reuse
= 1;
736 static int is_file(const char *url
)
741 return S_ISREG(buf
.st_mode
);
744 static int external_specification_len(const char *url
)
746 return strchr(url
, ':') - url
;
749 static const struct string_list
*protocol_whitelist(void)
751 static int enabled
= -1;
752 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
755 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
757 string_list_split(&allowed
, v
, ':', -1);
758 string_list_sort(&allowed
);
765 return enabled
? &allowed
: NULL
;
768 enum protocol_allow_config
{
769 PROTOCOL_ALLOW_NEVER
= 0,
770 PROTOCOL_ALLOW_USER_ONLY
,
771 PROTOCOL_ALLOW_ALWAYS
774 static enum protocol_allow_config
parse_protocol_config(const char *key
,
777 if (!strcasecmp(value
, "always"))
778 return PROTOCOL_ALLOW_ALWAYS
;
779 else if (!strcasecmp(value
, "never"))
780 return PROTOCOL_ALLOW_NEVER
;
781 else if (!strcasecmp(value
, "user"))
782 return PROTOCOL_ALLOW_USER_ONLY
;
784 die("unknown value for config '%s': %s", key
, value
);
787 static enum protocol_allow_config
get_protocol_config(const char *type
)
789 char *key
= xstrfmt("protocol.%s.allow", type
);
792 /* first check the per-protocol config */
793 if (!git_config_get_string(key
, &value
)) {
794 enum protocol_allow_config ret
=
795 parse_protocol_config(key
, value
);
802 /* if defined, fallback to user-defined default for unknown protocols */
803 if (!git_config_get_string("protocol.allow", &value
)) {
804 enum protocol_allow_config ret
=
805 parse_protocol_config("protocol.allow", value
);
810 /* fallback to built-in defaults */
812 if (!strcmp(type
, "http") ||
813 !strcmp(type
, "https") ||
814 !strcmp(type
, "git") ||
815 !strcmp(type
, "ssh") ||
816 !strcmp(type
, "file"))
817 return PROTOCOL_ALLOW_ALWAYS
;
819 /* known scary; err on the side of caution */
820 if (!strcmp(type
, "ext"))
821 return PROTOCOL_ALLOW_NEVER
;
823 /* unknown; by default let them be used only directly by the user */
824 return PROTOCOL_ALLOW_USER_ONLY
;
827 int is_transport_allowed(const char *type
, int from_user
)
829 const struct string_list
*whitelist
= protocol_whitelist();
831 return string_list_has_string(whitelist
, type
);
833 switch (get_protocol_config(type
)) {
834 case PROTOCOL_ALLOW_ALWAYS
:
836 case PROTOCOL_ALLOW_NEVER
:
838 case PROTOCOL_ALLOW_USER_ONLY
:
840 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
844 BUG("invalid protocol_allow_config type");
847 void transport_check_allowed(const char *type
)
849 if (!is_transport_allowed(type
, -1))
850 die("transport '%s' not allowed", type
);
853 static struct transport_vtable bundle_vtable
= {
855 get_refs_from_bundle
,
856 fetch_refs_from_bundle
,
862 static struct transport_vtable builtin_smart_vtable
= {
864 get_refs_via_connect
,
871 struct transport
*transport_get(struct remote
*remote
, const char *url
)
874 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
876 ret
->progress
= isatty(2);
879 die("No remote provided to transport_get()");
881 ret
->got_remote_refs
= 0;
882 ret
->remote
= remote
;
883 helper
= remote
->foreign_vcs
;
885 if (!url
&& remote
->url
)
886 url
= remote
->url
[0];
889 /* maybe it is a foreign URL? */
893 while (is_urlschemechar(p
== url
, *p
))
895 if (starts_with(p
, "::"))
896 helper
= xstrndup(url
, p
- url
);
900 transport_helper_init(ret
, helper
);
901 } else if (starts_with(url
, "rsync:")) {
902 die("git-over-rsync is no longer supported");
903 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
904 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
905 transport_check_allowed("file");
907 ret
->vtable
= &bundle_vtable
;
908 ret
->smart_options
= NULL
;
909 } else if (!is_url(url
)
910 || starts_with(url
, "file://")
911 || starts_with(url
, "git://")
912 || starts_with(url
, "ssh://")
913 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
914 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
917 * These are builtin smart transports; "allowed" transports
918 * will be checked individually in git_connect.
920 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
922 ret
->vtable
= &builtin_smart_vtable
;
923 ret
->smart_options
= &(data
->options
);
926 data
->got_remote_heads
= 0;
928 /* Unknown protocol in URL. Pass to external handler. */
929 int len
= external_specification_len(url
);
930 char *handler
= xmemdupz(url
, len
);
931 transport_helper_init(ret
, handler
);
934 if (ret
->smart_options
) {
935 ret
->smart_options
->thin
= 1;
936 ret
->smart_options
->uploadpack
= "git-upload-pack";
937 if (remote
->uploadpack
)
938 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
939 ret
->smart_options
->receivepack
= "git-receive-pack";
940 if (remote
->receivepack
)
941 ret
->smart_options
->receivepack
= remote
->receivepack
;
947 int transport_set_option(struct transport
*transport
,
948 const char *name
, const char *value
)
950 int git_reports
= 1, protocol_reports
= 1;
952 if (transport
->smart_options
)
953 git_reports
= set_git_option(transport
->smart_options
,
956 if (transport
->vtable
->set_option
)
957 protocol_reports
= transport
->vtable
->set_option(transport
,
960 /* If either report is 0, report 0 (success). */
961 if (!git_reports
|| !protocol_reports
)
963 /* If either reports -1 (invalid value), report -1. */
964 if ((git_reports
== -1) || (protocol_reports
== -1))
966 /* Otherwise if both report unknown, report unknown. */
970 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
974 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
976 transport
->verbose
= -1;
979 * Rules used to determine whether to report progress (processing aborts
980 * when a rule is satisfied):
982 * . Report progress, if force_progress is 1 (ie. --progress).
983 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
984 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
985 * . Report progress if isatty(2) is 1.
987 if (force_progress
>= 0)
988 transport
->progress
= !!force_progress
;
990 transport
->progress
= verbosity
>= 0 && isatty(2);
993 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
997 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
998 "not be found on any remote:\n"));
999 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1000 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1001 fprintf(stderr
, _("\nPlease try\n\n"
1002 " git push --recurse-submodules=on-demand\n\n"
1003 "or cd to the path and use\n\n"
1005 "to push them to a remote.\n\n"));
1007 string_list_clear(needs_pushing
, 0);
1009 die(_("Aborting."));
1012 static int run_pre_push_hook(struct transport
*transport
,
1013 struct ref
*remote_refs
)
1017 struct child_process proc
= CHILD_PROCESS_INIT
;
1019 const char *argv
[4];
1021 if (!(argv
[0] = find_hook("pre-push")))
1024 argv
[1] = transport
->remote
->name
;
1025 argv
[2] = transport
->url
;
1031 if (start_command(&proc
)) {
1032 finish_command(&proc
);
1036 sigchain_push(SIGPIPE
, SIG_IGN
);
1038 strbuf_init(&buf
, 256);
1040 for (r
= remote_refs
; r
; r
= r
->next
) {
1041 if (!r
->peer_ref
) continue;
1042 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1043 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1044 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1047 strbuf_addf( &buf
, "%s %s %s %s\n",
1048 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1049 r
->name
, oid_to_hex(&r
->old_oid
));
1051 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1052 /* We do not mind if a hook does not read all refs. */
1059 strbuf_release(&buf
);
1065 sigchain_pop(SIGPIPE
);
1067 x
= finish_command(&proc
);
1074 int transport_push(struct transport
*transport
,
1075 struct refspec
*rs
, int flags
,
1076 unsigned int *reject_reasons
)
1078 *reject_reasons
= 0;
1080 if (transport_color_config() < 0)
1083 if (transport
->vtable
->push_refs
) {
1084 struct ref
*remote_refs
;
1085 struct ref
*local_refs
= get_local_heads();
1086 int match_flags
= MATCH_REFS_NONE
;
1087 int verbose
= (transport
->verbose
> 0);
1088 int quiet
= (transport
->verbose
< 0);
1089 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1090 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1091 int push_ret
, ret
, err
;
1092 struct argv_array ref_prefixes
= ARGV_ARRAY_INIT
;
1094 if (check_push_refs(local_refs
, rs
) < 0)
1097 refspec_ref_prefixes(rs
, &ref_prefixes
);
1099 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1102 argv_array_clear(&ref_prefixes
);
1104 if (flags
& TRANSPORT_PUSH_ALL
)
1105 match_flags
|= MATCH_REFS_ALL
;
1106 if (flags
& TRANSPORT_PUSH_MIRROR
)
1107 match_flags
|= MATCH_REFS_MIRROR
;
1108 if (flags
& TRANSPORT_PUSH_PRUNE
)
1109 match_flags
|= MATCH_REFS_PRUNE
;
1110 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1111 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1113 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1116 if (transport
->smart_options
&&
1117 transport
->smart_options
->cas
&&
1118 !is_empty_cas(transport
->smart_options
->cas
))
1119 apply_push_cas(transport
->smart_options
->cas
,
1120 transport
->remote
, remote_refs
);
1122 set_ref_status_for_push(remote_refs
,
1123 flags
& TRANSPORT_PUSH_MIRROR
,
1124 flags
& TRANSPORT_PUSH_FORCE
);
1126 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1127 if (run_pre_push_hook(transport
, remote_refs
))
1130 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1131 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1132 !is_bare_repository()) {
1133 struct ref
*ref
= remote_refs
;
1134 struct oid_array commits
= OID_ARRAY_INIT
;
1136 for (; ref
; ref
= ref
->next
)
1137 if (!is_null_oid(&ref
->new_oid
))
1138 oid_array_append(&commits
,
1141 if (!push_unpushed_submodules(&commits
,
1144 transport
->push_options
,
1146 oid_array_clear(&commits
);
1147 die("Failed to push all needed submodules!");
1149 oid_array_clear(&commits
);
1152 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1153 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1154 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1155 !pretend
)) && !is_bare_repository()) {
1156 struct ref
*ref
= remote_refs
;
1157 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1158 struct oid_array commits
= OID_ARRAY_INIT
;
1160 for (; ref
; ref
= ref
->next
)
1161 if (!is_null_oid(&ref
->new_oid
))
1162 oid_array_append(&commits
,
1165 if (find_unpushed_submodules(&commits
, transport
->remote
->name
,
1167 oid_array_clear(&commits
);
1168 die_with_unpushed_submodules(&needs_pushing
);
1170 string_list_clear(&needs_pushing
, 0);
1171 oid_array_clear(&commits
);
1174 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
))
1175 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1178 err
= push_had_errors(remote_refs
);
1179 ret
= push_ret
| err
;
1182 transport_print_push_status(transport
->url
, remote_refs
,
1183 verbose
| porcelain
, porcelain
,
1186 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1187 set_upstreams(transport
, remote_refs
, pretend
);
1189 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1190 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1192 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1193 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1196 if (porcelain
&& !push_ret
)
1198 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1199 fprintf(stderr
, "Everything up-to-date\n");
1206 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1207 const struct argv_array
*ref_prefixes
)
1209 if (!transport
->got_remote_refs
) {
1210 transport
->remote_refs
=
1211 transport
->vtable
->get_refs_list(transport
, 0,
1213 transport
->got_remote_refs
= 1;
1216 return transport
->remote_refs
;
1219 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1222 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1223 struct ref
**heads
= NULL
;
1226 for (rm
= refs
; rm
; rm
= rm
->next
) {
1229 !is_null_oid(&rm
->old_oid
) &&
1230 !oidcmp(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1232 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1233 heads
[nr_heads
++] = rm
;
1238 * When deepening of a shallow repository is requested,
1239 * then local and remote refs are likely to still be equal.
1240 * Just feed them all to the fetch method in that case.
1241 * This condition shouldn't be met in a non-deepening fetch
1242 * (see builtin/fetch.c:quickfetch()).
1244 ALLOC_ARRAY(heads
, nr_refs
);
1245 for (rm
= refs
; rm
; rm
= rm
->next
)
1246 heads
[nr_heads
++] = rm
;
1249 rc
= transport
->vtable
->fetch(transport
, nr_heads
, heads
);
1255 void transport_unlock_pack(struct transport
*transport
)
1257 if (transport
->pack_lockfile
) {
1258 unlink_or_warn(transport
->pack_lockfile
);
1259 FREE_AND_NULL(transport
->pack_lockfile
);
1263 int transport_connect(struct transport
*transport
, const char *name
,
1264 const char *exec
, int fd
[2])
1266 if (transport
->vtable
->connect
)
1267 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1269 die("Operation not supported by protocol");
1272 int transport_disconnect(struct transport
*transport
)
1275 if (transport
->vtable
->disconnect
)
1276 ret
= transport
->vtable
->disconnect(transport
);
1282 * Strip username (and password) from a URL and return
1283 * it in a newly allocated string.
1285 char *transport_anonymize_url(const char *url
)
1287 char *scheme_prefix
, *anon_part
;
1288 size_t anon_len
, prefix_len
= 0;
1290 anon_part
= strchr(url
, '@');
1291 if (url_is_local_not_ssh(url
) || !anon_part
)
1294 anon_len
= strlen(++anon_part
);
1295 scheme_prefix
= strstr(url
, "://");
1296 if (!scheme_prefix
) {
1297 if (!strchr(anon_part
, ':'))
1298 /* cannot be "me@there:/path/name" */
1302 /* make sure scheme is reasonable */
1303 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1306 case '+': case '.': case '-':
1315 /* @ past the first slash does not count */
1316 cp
= strchr(scheme_prefix
+ 3, '/');
1317 if (cp
&& cp
< anon_part
)
1319 prefix_len
= scheme_prefix
- url
+ 3;
1321 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1322 (int)anon_len
, anon_part
);
1324 return xstrdup(url
);
1327 static void read_alternate_refs(const char *path
,
1328 alternate_ref_fn
*cb
,
1331 struct child_process cmd
= CHILD_PROCESS_INIT
;
1332 struct strbuf line
= STRBUF_INIT
;
1336 argv_array_pushf(&cmd
.args
, "--git-dir=%s", path
);
1337 argv_array_push(&cmd
.args
, "for-each-ref");
1338 argv_array_push(&cmd
.args
, "--format=%(objectname) %(refname)");
1339 cmd
.env
= local_repo_env
;
1342 if (start_command(&cmd
))
1345 fh
= xfdopen(cmd
.out
, "r");
1346 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
1347 struct object_id oid
;
1349 if (get_oid_hex(line
.buf
, &oid
) ||
1350 line
.buf
[GIT_SHA1_HEXSZ
] != ' ') {
1351 warning("invalid line while parsing alternate refs: %s",
1356 cb(line
.buf
+ GIT_SHA1_HEXSZ
+ 1, &oid
, data
);
1360 finish_command(&cmd
);
1363 struct alternate_refs_data
{
1364 alternate_ref_fn
*fn
;
1368 static int refs_from_alternate_cb(struct alternate_object_database
*e
,
1371 struct strbuf path
= STRBUF_INIT
;
1373 struct alternate_refs_data
*cb
= data
;
1375 if (!strbuf_realpath(&path
, e
->path
, 0))
1377 if (!strbuf_strip_suffix(&path
, "/objects"))
1379 base_len
= path
.len
;
1381 /* Is this a git repository with refs? */
1382 strbuf_addstr(&path
, "/refs");
1383 if (!is_directory(path
.buf
))
1385 strbuf_setlen(&path
, base_len
);
1387 read_alternate_refs(path
.buf
, cb
->fn
, cb
->data
);
1390 strbuf_release(&path
);
1394 void for_each_alternate_ref(alternate_ref_fn fn
, void *data
)
1396 struct alternate_refs_data cb
;
1399 foreach_alt_odb(refs_from_alternate_cb
, &cb
);