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
,
155 struct ref
**fetched_refs
)
157 struct bundle_transport_data
*data
= transport
->data
;
158 return unbundle(&data
->header
, data
->fd
,
159 transport
->progress
? BUNDLE_VERBOSE
: 0);
162 static int close_bundle(struct transport
*transport
)
164 struct bundle_transport_data
*data
= transport
->data
;
171 struct git_transport_data
{
172 struct git_transport_options options
;
173 struct child_process
*conn
;
175 unsigned got_remote_heads
: 1;
176 enum protocol_version version
;
177 struct oid_array extra_have
;
178 struct oid_array shallow
;
181 static int set_git_option(struct git_transport_options
*opts
,
182 const char *name
, const char *value
)
184 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
185 opts
->uploadpack
= value
;
187 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
188 opts
->receivepack
= value
;
190 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
191 opts
->thin
= !!value
;
193 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
194 opts
->followtags
= !!value
;
196 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
197 opts
->keep
= !!value
;
199 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
200 opts
->update_shallow
= !!value
;
202 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
207 opts
->depth
= strtol(value
, &end
, 0);
209 die(_("transport: invalid depth option '%s'"), value
);
212 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
213 opts
->deepen_since
= value
;
215 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
216 opts
->deepen_not
= (const struct string_list
*)value
;
218 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
219 opts
->deepen_relative
= !!value
;
221 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
222 opts
->from_promisor
= !!value
;
224 } else if (!strcmp(name
, TRANS_OPT_NO_DEPENDENTS
)) {
225 opts
->no_dependents
= !!value
;
227 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
228 parse_list_objects_filter(&opts
->filter_options
, value
);
234 static int connect_setup(struct transport
*transport
, int for_push
)
236 struct git_transport_data
*data
= transport
->data
;
237 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
242 switch (transport
->family
) {
243 case TRANSPORT_FAMILY_ALL
: break;
244 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
245 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
248 data
->conn
= git_connect(data
->fd
, transport
->url
,
249 for_push
? data
->options
.receivepack
:
250 data
->options
.uploadpack
,
256 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
257 const struct argv_array
*ref_prefixes
)
259 struct git_transport_data
*data
= transport
->data
;
260 struct ref
*refs
= NULL
;
261 struct packet_reader reader
;
263 connect_setup(transport
, for_push
);
265 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
266 PACKET_READ_CHOMP_NEWLINE
|
267 PACKET_READ_GENTLE_ON_EOF
);
269 data
->version
= discover_version(&reader
);
270 switch (data
->version
) {
272 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
273 ref_prefixes
, transport
->server_options
);
277 get_remote_heads(&reader
, &refs
,
278 for_push
? REF_NORMAL
: 0,
282 case protocol_unknown_version
:
283 BUG("unknown protocol version");
285 data
->got_remote_heads
= 1;
290 static int fetch_refs_via_pack(struct transport
*transport
,
291 int nr_heads
, struct ref
**to_fetch
,
292 struct ref
**fetched_refs
)
295 struct git_transport_data
*data
= transport
->data
;
296 struct ref
*refs
= NULL
;
297 char *dest
= xstrdup(transport
->url
);
298 struct fetch_pack_args args
;
299 struct ref
*refs_tmp
= NULL
;
301 memset(&args
, 0, sizeof(args
));
302 args
.uploadpack
= data
->options
.uploadpack
;
303 args
.keep_pack
= data
->options
.keep
;
305 args
.use_thin_pack
= data
->options
.thin
;
306 args
.include_tag
= data
->options
.followtags
;
307 args
.verbose
= (transport
->verbose
> 1);
308 args
.quiet
= (transport
->verbose
< 0);
309 args
.no_progress
= !transport
->progress
;
310 args
.depth
= data
->options
.depth
;
311 args
.deepen_since
= data
->options
.deepen_since
;
312 args
.deepen_not
= data
->options
.deepen_not
;
313 args
.deepen_relative
= data
->options
.deepen_relative
;
314 args
.check_self_contained_and_connected
=
315 data
->options
.check_self_contained_and_connected
;
316 args
.cloning
= transport
->cloning
;
317 args
.update_shallow
= data
->options
.update_shallow
;
318 args
.from_promisor
= data
->options
.from_promisor
;
319 args
.no_dependents
= data
->options
.no_dependents
;
320 args
.filter_options
= data
->options
.filter_options
;
321 args
.stateless_rpc
= transport
->stateless_rpc
;
322 args
.server_options
= transport
->server_options
;
324 if (!data
->got_remote_heads
)
325 refs_tmp
= get_refs_via_connect(transport
, 0, NULL
);
327 switch (data
->version
) {
329 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
330 refs_tmp
? refs_tmp
: transport
->remote_refs
,
331 dest
, to_fetch
, nr_heads
, &data
->shallow
,
332 &transport
->pack_lockfile
, data
->version
);
336 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
337 refs_tmp
? refs_tmp
: transport
->remote_refs
,
338 dest
, to_fetch
, nr_heads
, &data
->shallow
,
339 &transport
->pack_lockfile
, data
->version
);
341 case protocol_unknown_version
:
342 BUG("unknown protocol version");
347 if (finish_connect(data
->conn
))
350 data
->got_remote_heads
= 0;
351 data
->options
.self_contained_and_connected
=
352 args
.self_contained_and_connected
;
353 data
->options
.connectivity_checked
= args
.connectivity_checked
;
357 if (report_unmatched_refs(to_fetch
, nr_heads
))
361 *fetched_refs
= refs
;
370 static int push_had_errors(struct ref
*ref
)
372 for (; ref
; ref
= ref
->next
) {
373 switch (ref
->status
) {
374 case REF_STATUS_NONE
:
375 case REF_STATUS_UPTODATE
:
385 int transport_refs_pushed(struct ref
*ref
)
387 for (; ref
; ref
= ref
->next
) {
388 switch(ref
->status
) {
389 case REF_STATUS_NONE
:
390 case REF_STATUS_UPTODATE
:
399 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
401 struct refspec_item rs
;
403 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
409 if (!remote_find_tracking(remote
, &rs
)) {
411 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
413 delete_ref(NULL
, rs
.dst
, NULL
, 0);
415 update_ref("update by push", rs
.dst
, &ref
->new_oid
,
421 static void print_ref_status(char flag
, const char *summary
,
422 struct ref
*to
, struct ref
*from
, const char *msg
,
423 int porcelain
, int summary_width
)
427 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
429 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
431 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
433 fprintf(stdout
, "%s\n", summary
);
435 const char *red
= "", *reset
= "";
436 if (push_had_errors(to
)) {
437 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
438 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
440 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
443 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
445 fputs(prettify_refname(to
->name
), stderr
);
455 static void print_ok_ref_status(struct ref
*ref
, int porcelain
, int summary_width
)
458 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
459 porcelain
, summary_width
);
460 else if (is_null_oid(&ref
->old_oid
))
461 print_ref_status('*',
462 (starts_with(ref
->name
, "refs/tags/") ? "[new tag]" :
464 ref
, ref
->peer_ref
, NULL
, porcelain
, summary_width
);
466 struct strbuf quickref
= STRBUF_INIT
;
470 strbuf_add_unique_abbrev(&quickref
, &ref
->old_oid
,
472 if (ref
->forced_update
) {
473 strbuf_addstr(&quickref
, "...");
475 msg
= "forced update";
477 strbuf_addstr(&quickref
, "..");
481 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
,
484 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
485 porcelain
, summary_width
);
486 strbuf_release(&quickref
);
490 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
491 int porcelain
, int summary_width
)
494 char *url
= transport_anonymize_url(dest
);
495 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
499 switch(ref
->status
) {
500 case REF_STATUS_NONE
:
501 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
502 porcelain
, summary_width
);
504 case REF_STATUS_REJECT_NODELETE
:
505 print_ref_status('!', "[rejected]", ref
, NULL
,
506 "remote does not support deleting refs",
507 porcelain
, summary_width
);
509 case REF_STATUS_UPTODATE
:
510 print_ref_status('=', "[up to date]", ref
,
511 ref
->peer_ref
, NULL
, porcelain
, summary_width
);
513 case REF_STATUS_REJECT_NONFASTFORWARD
:
514 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
515 "non-fast-forward", porcelain
, summary_width
);
517 case REF_STATUS_REJECT_ALREADY_EXISTS
:
518 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
519 "already exists", porcelain
, summary_width
);
521 case REF_STATUS_REJECT_FETCH_FIRST
:
522 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
523 "fetch first", porcelain
, summary_width
);
525 case REF_STATUS_REJECT_NEEDS_FORCE
:
526 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
527 "needs force", porcelain
, summary_width
);
529 case REF_STATUS_REJECT_STALE
:
530 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
531 "stale info", porcelain
, summary_width
);
533 case REF_STATUS_REJECT_SHALLOW
:
534 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
535 "new shallow roots not allowed",
536 porcelain
, summary_width
);
538 case REF_STATUS_REMOTE_REJECT
:
539 print_ref_status('!', "[remote rejected]", ref
,
540 ref
->deletion
? NULL
: ref
->peer_ref
,
541 ref
->remote_status
, porcelain
, summary_width
);
543 case REF_STATUS_EXPECTING_REPORT
:
544 print_ref_status('!', "[remote failure]", ref
,
545 ref
->deletion
? NULL
: ref
->peer_ref
,
546 "remote failed to report status",
547 porcelain
, summary_width
);
549 case REF_STATUS_ATOMIC_PUSH_FAILED
:
550 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
551 "atomic push failed", porcelain
, summary_width
);
554 print_ok_ref_status(ref
, porcelain
, summary_width
);
561 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
563 char hex
[GIT_MAX_HEXSZ
+ 1];
564 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
566 return (w
< sofar
) ? sofar
: w
;
569 int transport_summary_width(const struct ref
*refs
)
573 for (; refs
; refs
= refs
->next
) {
574 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
575 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
578 maxw
= FALLBACK_DEFAULT_ABBREV
;
579 return (2 * maxw
+ 3);
582 void transport_print_push_status(const char *dest
, struct ref
*refs
,
583 int verbose
, int porcelain
, unsigned int *reject_reasons
)
588 int summary_width
= transport_summary_width(refs
);
590 if (transport_color_config() < 0)
591 warning(_("could not parse transport.color.* config"));
593 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
596 for (ref
= refs
; ref
; ref
= ref
->next
)
597 if (ref
->status
== REF_STATUS_UPTODATE
)
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_OK
)
604 n
+= print_one_push_status(ref
, dest
, n
,
605 porcelain
, summary_width
);
608 for (ref
= refs
; ref
; ref
= ref
->next
) {
609 if (ref
->status
!= REF_STATUS_NONE
&&
610 ref
->status
!= REF_STATUS_UPTODATE
&&
611 ref
->status
!= REF_STATUS_OK
)
612 n
+= print_one_push_status(ref
, dest
, n
,
613 porcelain
, summary_width
);
614 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
615 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
616 *reject_reasons
|= REJECT_NON_FF_HEAD
;
618 *reject_reasons
|= REJECT_NON_FF_OTHER
;
619 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
620 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
621 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
622 *reject_reasons
|= REJECT_FETCH_FIRST
;
623 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
624 *reject_reasons
|= REJECT_NEEDS_FORCE
;
630 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
632 struct git_transport_data
*data
= transport
->data
;
633 struct send_pack_args args
;
636 if (transport_color_config() < 0)
639 if (!data
->got_remote_heads
)
640 get_refs_via_connect(transport
, 1, NULL
);
642 memset(&args
, 0, sizeof(args
));
643 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
644 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
645 args
.use_thin_pack
= data
->options
.thin
;
646 args
.verbose
= (transport
->verbose
> 0);
647 args
.quiet
= (transport
->verbose
< 0);
648 args
.progress
= transport
->progress
;
649 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
650 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
651 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
652 args
.push_options
= transport
->push_options
;
653 args
.url
= transport
->url
;
655 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
656 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
657 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
658 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
660 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
662 switch (data
->version
) {
664 die("support for protocol v2 not implemented yet");
668 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
671 case protocol_unknown_version
:
672 BUG("unknown protocol version");
677 ret
|= finish_connect(data
->conn
);
679 data
->got_remote_heads
= 0;
684 static int connect_git(struct transport
*transport
, const char *name
,
685 const char *executable
, int fd
[2])
687 struct git_transport_data
*data
= transport
->data
;
688 data
->conn
= git_connect(data
->fd
, transport
->url
,
695 static int disconnect_git(struct transport
*transport
)
697 struct git_transport_data
*data
= transport
->data
;
699 if (data
->got_remote_heads
)
700 packet_flush(data
->fd
[1]);
703 finish_connect(data
->conn
);
710 static struct transport_vtable taken_over_vtable
= {
712 get_refs_via_connect
,
719 void transport_take_over(struct transport
*transport
,
720 struct child_process
*child
)
722 struct git_transport_data
*data
;
724 if (!transport
->smart_options
)
725 BUG("taking over transport requires non-NULL "
726 "smart_options field.");
728 data
= xcalloc(1, sizeof(*data
));
729 data
->options
= *transport
->smart_options
;
731 data
->fd
[0] = data
->conn
->out
;
732 data
->fd
[1] = data
->conn
->in
;
733 data
->got_remote_heads
= 0;
734 transport
->data
= data
;
736 transport
->vtable
= &taken_over_vtable
;
737 transport
->smart_options
= &(data
->options
);
739 transport
->cannot_reuse
= 1;
742 static int is_file(const char *url
)
747 return S_ISREG(buf
.st_mode
);
750 static int external_specification_len(const char *url
)
752 return strchr(url
, ':') - url
;
755 static const struct string_list
*protocol_whitelist(void)
757 static int enabled
= -1;
758 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
761 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
763 string_list_split(&allowed
, v
, ':', -1);
764 string_list_sort(&allowed
);
771 return enabled
? &allowed
: NULL
;
774 enum protocol_allow_config
{
775 PROTOCOL_ALLOW_NEVER
= 0,
776 PROTOCOL_ALLOW_USER_ONLY
,
777 PROTOCOL_ALLOW_ALWAYS
780 static enum protocol_allow_config
parse_protocol_config(const char *key
,
783 if (!strcasecmp(value
, "always"))
784 return PROTOCOL_ALLOW_ALWAYS
;
785 else if (!strcasecmp(value
, "never"))
786 return PROTOCOL_ALLOW_NEVER
;
787 else if (!strcasecmp(value
, "user"))
788 return PROTOCOL_ALLOW_USER_ONLY
;
790 die("unknown value for config '%s': %s", key
, value
);
793 static enum protocol_allow_config
get_protocol_config(const char *type
)
795 char *key
= xstrfmt("protocol.%s.allow", type
);
798 /* first check the per-protocol config */
799 if (!git_config_get_string(key
, &value
)) {
800 enum protocol_allow_config ret
=
801 parse_protocol_config(key
, value
);
808 /* if defined, fallback to user-defined default for unknown protocols */
809 if (!git_config_get_string("protocol.allow", &value
)) {
810 enum protocol_allow_config ret
=
811 parse_protocol_config("protocol.allow", value
);
816 /* fallback to built-in defaults */
818 if (!strcmp(type
, "http") ||
819 !strcmp(type
, "https") ||
820 !strcmp(type
, "git") ||
821 !strcmp(type
, "ssh") ||
822 !strcmp(type
, "file"))
823 return PROTOCOL_ALLOW_ALWAYS
;
825 /* known scary; err on the side of caution */
826 if (!strcmp(type
, "ext"))
827 return PROTOCOL_ALLOW_NEVER
;
829 /* unknown; by default let them be used only directly by the user */
830 return PROTOCOL_ALLOW_USER_ONLY
;
833 int is_transport_allowed(const char *type
, int from_user
)
835 const struct string_list
*whitelist
= protocol_whitelist();
837 return string_list_has_string(whitelist
, type
);
839 switch (get_protocol_config(type
)) {
840 case PROTOCOL_ALLOW_ALWAYS
:
842 case PROTOCOL_ALLOW_NEVER
:
844 case PROTOCOL_ALLOW_USER_ONLY
:
846 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
850 BUG("invalid protocol_allow_config type");
853 void transport_check_allowed(const char *type
)
855 if (!is_transport_allowed(type
, -1))
856 die("transport '%s' not allowed", type
);
859 static struct transport_vtable bundle_vtable
= {
861 get_refs_from_bundle
,
862 fetch_refs_from_bundle
,
868 static struct transport_vtable builtin_smart_vtable
= {
870 get_refs_via_connect
,
877 struct transport
*transport_get(struct remote
*remote
, const char *url
)
880 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
882 ret
->progress
= isatty(2);
885 die("No remote provided to transport_get()");
887 ret
->got_remote_refs
= 0;
888 ret
->remote
= remote
;
889 helper
= remote
->foreign_vcs
;
891 if (!url
&& remote
->url
)
892 url
= remote
->url
[0];
895 /* maybe it is a foreign URL? */
899 while (is_urlschemechar(p
== url
, *p
))
901 if (starts_with(p
, "::"))
902 helper
= xstrndup(url
, p
- url
);
906 transport_helper_init(ret
, helper
);
907 } else if (starts_with(url
, "rsync:")) {
908 die("git-over-rsync is no longer supported");
909 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
910 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
911 transport_check_allowed("file");
913 ret
->vtable
= &bundle_vtable
;
914 ret
->smart_options
= NULL
;
915 } else if (!is_url(url
)
916 || starts_with(url
, "file://")
917 || starts_with(url
, "git://")
918 || starts_with(url
, "ssh://")
919 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
920 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
923 * These are builtin smart transports; "allowed" transports
924 * will be checked individually in git_connect.
926 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
928 ret
->vtable
= &builtin_smart_vtable
;
929 ret
->smart_options
= &(data
->options
);
932 data
->got_remote_heads
= 0;
934 /* Unknown protocol in URL. Pass to external handler. */
935 int len
= external_specification_len(url
);
936 char *handler
= xmemdupz(url
, len
);
937 transport_helper_init(ret
, handler
);
940 if (ret
->smart_options
) {
941 ret
->smart_options
->thin
= 1;
942 ret
->smart_options
->uploadpack
= "git-upload-pack";
943 if (remote
->uploadpack
)
944 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
945 ret
->smart_options
->receivepack
= "git-receive-pack";
946 if (remote
->receivepack
)
947 ret
->smart_options
->receivepack
= remote
->receivepack
;
953 int transport_set_option(struct transport
*transport
,
954 const char *name
, const char *value
)
956 int git_reports
= 1, protocol_reports
= 1;
958 if (transport
->smart_options
)
959 git_reports
= set_git_option(transport
->smart_options
,
962 if (transport
->vtable
->set_option
)
963 protocol_reports
= transport
->vtable
->set_option(transport
,
966 /* If either report is 0, report 0 (success). */
967 if (!git_reports
|| !protocol_reports
)
969 /* If either reports -1 (invalid value), report -1. */
970 if ((git_reports
== -1) || (protocol_reports
== -1))
972 /* Otherwise if both report unknown, report unknown. */
976 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
980 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
982 transport
->verbose
= -1;
985 * Rules used to determine whether to report progress (processing aborts
986 * when a rule is satisfied):
988 * . Report progress, if force_progress is 1 (ie. --progress).
989 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
990 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
991 * . Report progress if isatty(2) is 1.
993 if (force_progress
>= 0)
994 transport
->progress
= !!force_progress
;
996 transport
->progress
= verbosity
>= 0 && isatty(2);
999 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
1003 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
1004 "not be found on any remote:\n"));
1005 for (i
= 0; i
< needs_pushing
->nr
; i
++)
1006 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
1007 fprintf(stderr
, _("\nPlease try\n\n"
1008 " git push --recurse-submodules=on-demand\n\n"
1009 "or cd to the path and use\n\n"
1011 "to push them to a remote.\n\n"));
1013 string_list_clear(needs_pushing
, 0);
1015 die(_("Aborting."));
1018 static int run_pre_push_hook(struct transport
*transport
,
1019 struct ref
*remote_refs
)
1023 struct child_process proc
= CHILD_PROCESS_INIT
;
1025 const char *argv
[4];
1027 if (!(argv
[0] = find_hook("pre-push")))
1030 argv
[1] = transport
->remote
->name
;
1031 argv
[2] = transport
->url
;
1037 if (start_command(&proc
)) {
1038 finish_command(&proc
);
1042 sigchain_push(SIGPIPE
, SIG_IGN
);
1044 strbuf_init(&buf
, 256);
1046 for (r
= remote_refs
; r
; r
= r
->next
) {
1047 if (!r
->peer_ref
) continue;
1048 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1049 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1050 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1053 strbuf_addf( &buf
, "%s %s %s %s\n",
1054 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1055 r
->name
, oid_to_hex(&r
->old_oid
));
1057 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1058 /* We do not mind if a hook does not read all refs. */
1065 strbuf_release(&buf
);
1071 sigchain_pop(SIGPIPE
);
1073 x
= finish_command(&proc
);
1080 int transport_push(struct transport
*transport
,
1081 struct refspec
*rs
, int flags
,
1082 unsigned int *reject_reasons
)
1084 *reject_reasons
= 0;
1086 if (transport_color_config() < 0)
1089 if (transport
->vtable
->push_refs
) {
1090 struct ref
*remote_refs
;
1091 struct ref
*local_refs
= get_local_heads();
1092 int match_flags
= MATCH_REFS_NONE
;
1093 int verbose
= (transport
->verbose
> 0);
1094 int quiet
= (transport
->verbose
< 0);
1095 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1096 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1097 int push_ret
, ret
, err
;
1098 struct argv_array ref_prefixes
= ARGV_ARRAY_INIT
;
1100 if (check_push_refs(local_refs
, rs
) < 0)
1103 refspec_ref_prefixes(rs
, &ref_prefixes
);
1105 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1108 argv_array_clear(&ref_prefixes
);
1110 if (flags
& TRANSPORT_PUSH_ALL
)
1111 match_flags
|= MATCH_REFS_ALL
;
1112 if (flags
& TRANSPORT_PUSH_MIRROR
)
1113 match_flags
|= MATCH_REFS_MIRROR
;
1114 if (flags
& TRANSPORT_PUSH_PRUNE
)
1115 match_flags
|= MATCH_REFS_PRUNE
;
1116 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1117 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1119 if (match_push_refs(local_refs
, &remote_refs
, rs
, match_flags
))
1122 if (transport
->smart_options
&&
1123 transport
->smart_options
->cas
&&
1124 !is_empty_cas(transport
->smart_options
->cas
))
1125 apply_push_cas(transport
->smart_options
->cas
,
1126 transport
->remote
, remote_refs
);
1128 set_ref_status_for_push(remote_refs
,
1129 flags
& TRANSPORT_PUSH_MIRROR
,
1130 flags
& TRANSPORT_PUSH_FORCE
);
1132 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1133 if (run_pre_push_hook(transport
, remote_refs
))
1136 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1137 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1138 !is_bare_repository()) {
1139 struct ref
*ref
= remote_refs
;
1140 struct oid_array commits
= OID_ARRAY_INIT
;
1142 for (; ref
; ref
= ref
->next
)
1143 if (!is_null_oid(&ref
->new_oid
))
1144 oid_array_append(&commits
,
1147 if (!push_unpushed_submodules(&commits
,
1150 transport
->push_options
,
1152 oid_array_clear(&commits
);
1153 die("Failed to push all needed submodules!");
1155 oid_array_clear(&commits
);
1158 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1159 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1160 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1161 !pretend
)) && !is_bare_repository()) {
1162 struct ref
*ref
= remote_refs
;
1163 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1164 struct oid_array commits
= OID_ARRAY_INIT
;
1166 for (; ref
; ref
= ref
->next
)
1167 if (!is_null_oid(&ref
->new_oid
))
1168 oid_array_append(&commits
,
1171 if (find_unpushed_submodules(&commits
, transport
->remote
->name
,
1173 oid_array_clear(&commits
);
1174 die_with_unpushed_submodules(&needs_pushing
);
1176 string_list_clear(&needs_pushing
, 0);
1177 oid_array_clear(&commits
);
1180 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
))
1181 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1184 err
= push_had_errors(remote_refs
);
1185 ret
= push_ret
| err
;
1188 transport_print_push_status(transport
->url
, remote_refs
,
1189 verbose
| porcelain
, porcelain
,
1192 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1193 set_upstreams(transport
, remote_refs
, pretend
);
1195 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1196 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1198 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1199 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1202 if (porcelain
&& !push_ret
)
1204 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1205 fprintf(stderr
, "Everything up-to-date\n");
1212 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1213 const struct argv_array
*ref_prefixes
)
1215 if (!transport
->got_remote_refs
) {
1216 transport
->remote_refs
=
1217 transport
->vtable
->get_refs_list(transport
, 0,
1219 transport
->got_remote_refs
= 1;
1222 return transport
->remote_refs
;
1225 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
,
1226 struct ref
**fetched_refs
)
1229 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1230 struct ref
**heads
= NULL
;
1231 struct ref
*nop_head
= NULL
, **nop_tail
= &nop_head
;
1234 for (rm
= refs
; rm
; rm
= rm
->next
) {
1237 !is_null_oid(&rm
->old_oid
) &&
1238 !oidcmp(&rm
->peer_ref
->old_oid
, &rm
->old_oid
)) {
1240 * These need to be reported as fetched, but we don't
1241 * actually need to fetch them.
1244 struct ref
*nop_ref
= copy_ref(rm
);
1245 *nop_tail
= nop_ref
;
1246 nop_tail
= &nop_ref
->next
;
1250 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1251 heads
[nr_heads
++] = rm
;
1256 * When deepening of a shallow repository is requested,
1257 * then local and remote refs are likely to still be equal.
1258 * Just feed them all to the fetch method in that case.
1259 * This condition shouldn't be met in a non-deepening fetch
1260 * (see builtin/fetch.c:quickfetch()).
1262 ALLOC_ARRAY(heads
, nr_refs
);
1263 for (rm
= refs
; rm
; rm
= rm
->next
)
1264 heads
[nr_heads
++] = rm
;
1267 rc
= transport
->vtable
->fetch(transport
, nr_heads
, heads
, fetched_refs
);
1268 if (fetched_refs
&& nop_head
) {
1269 *nop_tail
= *fetched_refs
;
1270 *fetched_refs
= nop_head
;
1277 void transport_unlock_pack(struct transport
*transport
)
1279 if (transport
->pack_lockfile
) {
1280 unlink_or_warn(transport
->pack_lockfile
);
1281 FREE_AND_NULL(transport
->pack_lockfile
);
1285 int transport_connect(struct transport
*transport
, const char *name
,
1286 const char *exec
, int fd
[2])
1288 if (transport
->vtable
->connect
)
1289 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1291 die("Operation not supported by protocol");
1294 int transport_disconnect(struct transport
*transport
)
1297 if (transport
->vtable
->disconnect
)
1298 ret
= transport
->vtable
->disconnect(transport
);
1304 * Strip username (and password) from a URL and return
1305 * it in a newly allocated string.
1307 char *transport_anonymize_url(const char *url
)
1309 char *scheme_prefix
, *anon_part
;
1310 size_t anon_len
, prefix_len
= 0;
1312 anon_part
= strchr(url
, '@');
1313 if (url_is_local_not_ssh(url
) || !anon_part
)
1316 anon_len
= strlen(++anon_part
);
1317 scheme_prefix
= strstr(url
, "://");
1318 if (!scheme_prefix
) {
1319 if (!strchr(anon_part
, ':'))
1320 /* cannot be "me@there:/path/name" */
1324 /* make sure scheme is reasonable */
1325 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1328 case '+': case '.': case '-':
1337 /* @ past the first slash does not count */
1338 cp
= strchr(scheme_prefix
+ 3, '/');
1339 if (cp
&& cp
< anon_part
)
1341 prefix_len
= scheme_prefix
- url
+ 3;
1343 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1344 (int)anon_len
, anon_part
);
1346 return xstrdup(url
);
1349 static void read_alternate_refs(const char *path
,
1350 alternate_ref_fn
*cb
,
1353 struct child_process cmd
= CHILD_PROCESS_INIT
;
1354 struct strbuf line
= STRBUF_INIT
;
1358 argv_array_pushf(&cmd
.args
, "--git-dir=%s", path
);
1359 argv_array_push(&cmd
.args
, "for-each-ref");
1360 argv_array_push(&cmd
.args
, "--format=%(objectname) %(refname)");
1361 cmd
.env
= local_repo_env
;
1364 if (start_command(&cmd
))
1367 fh
= xfdopen(cmd
.out
, "r");
1368 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
1369 struct object_id oid
;
1371 if (get_oid_hex(line
.buf
, &oid
) ||
1372 line
.buf
[GIT_SHA1_HEXSZ
] != ' ') {
1373 warning("invalid line while parsing alternate refs: %s",
1378 cb(line
.buf
+ GIT_SHA1_HEXSZ
+ 1, &oid
, data
);
1382 finish_command(&cmd
);
1385 struct alternate_refs_data
{
1386 alternate_ref_fn
*fn
;
1390 static int refs_from_alternate_cb(struct alternate_object_database
*e
,
1393 struct strbuf path
= STRBUF_INIT
;
1395 struct alternate_refs_data
*cb
= data
;
1397 if (!strbuf_realpath(&path
, e
->path
, 0))
1399 if (!strbuf_strip_suffix(&path
, "/objects"))
1401 base_len
= path
.len
;
1403 /* Is this a git repository with refs? */
1404 strbuf_addstr(&path
, "/refs");
1405 if (!is_directory(path
.buf
))
1407 strbuf_setlen(&path
, base_len
);
1409 read_alternate_refs(path
.buf
, cb
->fn
, cb
->data
);
1412 strbuf_release(&path
);
1416 void for_each_alternate_ref(alternate_ref_fn fn
, void *data
)
1418 struct alternate_refs_data cb
;
1421 foreach_alt_odb(refs_from_alternate_cb
, &cb
);