4 #include "run-command.h"
6 #include "fetch-pack.h"
16 #include "submodule.h"
17 #include "string-list.h"
18 #include "sha1-array.h"
20 #include "transport-internal.h"
21 #include "object-store.h"
24 static int transport_use_color
= -1;
25 static char transport_colors
[][COLOR_MAXLEN
] = {
27 GIT_COLOR_RED
/* REJECTED */
30 enum color_transport
{
31 TRANSPORT_COLOR_RESET
= 0,
32 TRANSPORT_COLOR_REJECTED
= 1
35 static int transport_color_config(void)
37 const char *keys
[] = {
38 "color.transport.reset",
39 "color.transport.rejected"
40 }, *key
= "color.transport";
43 static int initialized
;
49 if (!git_config_get_string(key
, &value
))
50 transport_use_color
= git_config_colorbool(key
, value
);
52 if (!want_color_stderr(transport_use_color
))
55 for (i
= 0; i
< ARRAY_SIZE(keys
); i
++)
56 if (!git_config_get_string(keys
[i
], &value
)) {
58 return config_error_nonbool(keys
[i
]);
59 if (color_parse(value
, transport_colors
[i
]) < 0)
66 static const char *transport_get_color(enum color_transport ix
)
68 if (want_color_stderr(transport_use_color
))
69 return transport_colors
[ix
];
73 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
77 for (ref
= refs
; ref
; ref
= ref
->next
) {
78 const char *localname
;
80 const char *remotename
;
83 * Check suitability for tracking. Must be successful /
84 * already up-to-date ref create/modify (not delete).
86 if (ref
->status
!= REF_STATUS_OK
&&
87 ref
->status
!= REF_STATUS_UPTODATE
)
91 if (is_null_oid(&ref
->new_oid
))
94 /* Follow symbolic refs (mainly for HEAD). */
95 localname
= ref
->peer_ref
->name
;
96 remotename
= ref
->name
;
97 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
99 if (tmp
&& flag
& REF_ISSYMREF
&&
100 starts_with(tmp
, "refs/heads/"))
103 /* Both source and destination must be local branches. */
104 if (!localname
|| !starts_with(localname
, "refs/heads/"))
106 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
110 install_branch_config(BRANCH_CONFIG_VERBOSE
,
111 localname
+ 11, transport
->remote
->name
,
114 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
115 localname
+ 11, remotename
+ 11,
116 transport
->remote
->name
);
120 struct bundle_transport_data
{
122 struct bundle_header header
;
125 static struct ref
*get_refs_from_bundle(struct transport
*transport
, int for_push
)
127 struct bundle_transport_data
*data
= transport
->data
;
128 struct ref
*result
= NULL
;
136 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
138 die ("Could not read bundle '%s'.", transport
->url
);
139 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
140 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
141 struct ref
*ref
= alloc_ref(e
->name
);
142 oidcpy(&ref
->old_oid
, &e
->oid
);
149 static int fetch_refs_from_bundle(struct transport
*transport
,
150 int nr_heads
, struct ref
**to_fetch
)
152 struct bundle_transport_data
*data
= transport
->data
;
153 return unbundle(&data
->header
, data
->fd
,
154 transport
->progress
? BUNDLE_VERBOSE
: 0);
157 static int close_bundle(struct transport
*transport
)
159 struct bundle_transport_data
*data
= transport
->data
;
166 struct git_transport_data
{
167 struct git_transport_options options
;
168 struct child_process
*conn
;
170 unsigned got_remote_heads
: 1;
171 struct oid_array extra_have
;
172 struct oid_array shallow
;
175 static int set_git_option(struct git_transport_options
*opts
,
176 const char *name
, const char *value
)
178 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
179 opts
->uploadpack
= value
;
181 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
182 opts
->receivepack
= value
;
184 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
185 opts
->thin
= !!value
;
187 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
188 opts
->followtags
= !!value
;
190 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
191 opts
->keep
= !!value
;
193 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
194 opts
->update_shallow
= !!value
;
196 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
201 opts
->depth
= strtol(value
, &end
, 0);
203 die(_("transport: invalid depth option '%s'"), value
);
206 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
207 opts
->deepen_since
= value
;
209 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
210 opts
->deepen_not
= (const struct string_list
*)value
;
212 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
213 opts
->deepen_relative
= !!value
;
215 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
216 opts
->from_promisor
= !!value
;
218 } else if (!strcmp(name
, TRANS_OPT_NO_DEPENDENTS
)) {
219 opts
->no_dependents
= !!value
;
221 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
222 parse_list_objects_filter(&opts
->filter_options
, value
);
228 static int connect_setup(struct transport
*transport
, int for_push
)
230 struct git_transport_data
*data
= transport
->data
;
231 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
236 switch (transport
->family
) {
237 case TRANSPORT_FAMILY_ALL
: break;
238 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
239 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
242 data
->conn
= git_connect(data
->fd
, transport
->url
,
243 for_push
? data
->options
.receivepack
:
244 data
->options
.uploadpack
,
250 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
)
252 struct git_transport_data
*data
= transport
->data
;
255 connect_setup(transport
, for_push
);
256 get_remote_heads(data
->fd
[0], NULL
, 0, &refs
,
257 for_push
? REF_NORMAL
: 0,
260 data
->got_remote_heads
= 1;
265 static int fetch_refs_via_pack(struct transport
*transport
,
266 int nr_heads
, struct ref
**to_fetch
)
269 struct git_transport_data
*data
= transport
->data
;
271 char *dest
= xstrdup(transport
->url
);
272 struct fetch_pack_args args
;
273 struct ref
*refs_tmp
= NULL
;
275 memset(&args
, 0, sizeof(args
));
276 args
.uploadpack
= data
->options
.uploadpack
;
277 args
.keep_pack
= data
->options
.keep
;
279 args
.use_thin_pack
= data
->options
.thin
;
280 args
.include_tag
= data
->options
.followtags
;
281 args
.verbose
= (transport
->verbose
> 1);
282 args
.quiet
= (transport
->verbose
< 0);
283 args
.no_progress
= !transport
->progress
;
284 args
.depth
= data
->options
.depth
;
285 args
.deepen_since
= data
->options
.deepen_since
;
286 args
.deepen_not
= data
->options
.deepen_not
;
287 args
.deepen_relative
= data
->options
.deepen_relative
;
288 args
.check_self_contained_and_connected
=
289 data
->options
.check_self_contained_and_connected
;
290 args
.cloning
= transport
->cloning
;
291 args
.update_shallow
= data
->options
.update_shallow
;
292 args
.from_promisor
= data
->options
.from_promisor
;
293 args
.no_dependents
= data
->options
.no_dependents
;
294 args
.filter_options
= data
->options
.filter_options
;
296 if (!data
->got_remote_heads
) {
297 connect_setup(transport
, 0);
298 get_remote_heads(data
->fd
[0], NULL
, 0, &refs_tmp
, 0,
299 NULL
, &data
->shallow
);
300 data
->got_remote_heads
= 1;
303 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
304 refs_tmp
? refs_tmp
: transport
->remote_refs
,
305 dest
, to_fetch
, nr_heads
, &data
->shallow
,
306 &transport
->pack_lockfile
);
309 if (finish_connect(data
->conn
))
312 data
->got_remote_heads
= 0;
313 data
->options
.self_contained_and_connected
=
314 args
.self_contained_and_connected
;
318 if (report_unmatched_refs(to_fetch
, nr_heads
))
327 static int push_had_errors(struct ref
*ref
)
329 for (; ref
; ref
= ref
->next
) {
330 switch (ref
->status
) {
331 case REF_STATUS_NONE
:
332 case REF_STATUS_UPTODATE
:
342 int transport_refs_pushed(struct ref
*ref
)
344 for (; ref
; ref
= ref
->next
) {
345 switch(ref
->status
) {
346 case REF_STATUS_NONE
:
347 case REF_STATUS_UPTODATE
:
356 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
360 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
366 if (!remote_find_tracking(remote
, &rs
)) {
368 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
370 delete_ref(NULL
, rs
.dst
, NULL
, 0);
372 update_ref("update by push", rs
.dst
, &ref
->new_oid
,
378 static void print_ref_status(char flag
, const char *summary
,
379 struct ref
*to
, struct ref
*from
, const char *msg
,
380 int porcelain
, int summary_width
)
384 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
386 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
388 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
390 fprintf(stdout
, "%s\n", summary
);
392 const char *red
= "", *reset
= "";
393 if (push_had_errors(to
)) {
394 red
= transport_get_color(TRANSPORT_COLOR_REJECTED
);
395 reset
= transport_get_color(TRANSPORT_COLOR_RESET
);
397 fprintf(stderr
, " %s%c %-*s%s ", red
, flag
, summary_width
,
400 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
402 fputs(prettify_refname(to
->name
), stderr
);
412 static void print_ok_ref_status(struct ref
*ref
, int porcelain
, int summary_width
)
415 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
416 porcelain
, summary_width
);
417 else if (is_null_oid(&ref
->old_oid
))
418 print_ref_status('*',
419 (starts_with(ref
->name
, "refs/tags/") ? "[new tag]" :
421 ref
, ref
->peer_ref
, NULL
, porcelain
, summary_width
);
423 struct strbuf quickref
= STRBUF_INIT
;
427 strbuf_add_unique_abbrev(&quickref
, &ref
->old_oid
,
429 if (ref
->forced_update
) {
430 strbuf_addstr(&quickref
, "...");
432 msg
= "forced update";
434 strbuf_addstr(&quickref
, "..");
438 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
,
441 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
442 porcelain
, summary_width
);
443 strbuf_release(&quickref
);
447 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
448 int porcelain
, int summary_width
)
451 char *url
= transport_anonymize_url(dest
);
452 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
456 switch(ref
->status
) {
457 case REF_STATUS_NONE
:
458 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
459 porcelain
, summary_width
);
461 case REF_STATUS_REJECT_NODELETE
:
462 print_ref_status('!', "[rejected]", ref
, NULL
,
463 "remote does not support deleting refs",
464 porcelain
, summary_width
);
466 case REF_STATUS_UPTODATE
:
467 print_ref_status('=', "[up to date]", ref
,
468 ref
->peer_ref
, NULL
, porcelain
, summary_width
);
470 case REF_STATUS_REJECT_NONFASTFORWARD
:
471 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
472 "non-fast-forward", porcelain
, summary_width
);
474 case REF_STATUS_REJECT_ALREADY_EXISTS
:
475 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
476 "already exists", porcelain
, summary_width
);
478 case REF_STATUS_REJECT_FETCH_FIRST
:
479 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
480 "fetch first", porcelain
, summary_width
);
482 case REF_STATUS_REJECT_NEEDS_FORCE
:
483 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
484 "needs force", porcelain
, summary_width
);
486 case REF_STATUS_REJECT_STALE
:
487 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
488 "stale info", porcelain
, summary_width
);
490 case REF_STATUS_REJECT_SHALLOW
:
491 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
492 "new shallow roots not allowed",
493 porcelain
, summary_width
);
495 case REF_STATUS_REMOTE_REJECT
:
496 print_ref_status('!', "[remote rejected]", ref
,
497 ref
->deletion
? NULL
: ref
->peer_ref
,
498 ref
->remote_status
, porcelain
, summary_width
);
500 case REF_STATUS_EXPECTING_REPORT
:
501 print_ref_status('!', "[remote failure]", ref
,
502 ref
->deletion
? NULL
: ref
->peer_ref
,
503 "remote failed to report status",
504 porcelain
, summary_width
);
506 case REF_STATUS_ATOMIC_PUSH_FAILED
:
507 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
508 "atomic push failed", porcelain
, summary_width
);
511 print_ok_ref_status(ref
, porcelain
, summary_width
);
518 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
520 char hex
[GIT_MAX_HEXSZ
+ 1];
521 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
523 return (w
< sofar
) ? sofar
: w
;
526 int transport_summary_width(const struct ref
*refs
)
530 for (; refs
; refs
= refs
->next
) {
531 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
532 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
535 maxw
= FALLBACK_DEFAULT_ABBREV
;
536 return (2 * maxw
+ 3);
539 void transport_print_push_status(const char *dest
, struct ref
*refs
,
540 int verbose
, int porcelain
, unsigned int *reject_reasons
)
545 int summary_width
= transport_summary_width(refs
);
547 if (transport_color_config() < 0)
548 warning(_("could not parse transport.color.* config"));
550 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
553 for (ref
= refs
; ref
; ref
= ref
->next
)
554 if (ref
->status
== REF_STATUS_UPTODATE
)
555 n
+= print_one_push_status(ref
, dest
, n
,
556 porcelain
, summary_width
);
559 for (ref
= refs
; ref
; ref
= ref
->next
)
560 if (ref
->status
== REF_STATUS_OK
)
561 n
+= print_one_push_status(ref
, dest
, n
,
562 porcelain
, summary_width
);
565 for (ref
= refs
; ref
; ref
= ref
->next
) {
566 if (ref
->status
!= REF_STATUS_NONE
&&
567 ref
->status
!= REF_STATUS_UPTODATE
&&
568 ref
->status
!= REF_STATUS_OK
)
569 n
+= print_one_push_status(ref
, dest
, n
,
570 porcelain
, summary_width
);
571 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
572 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
573 *reject_reasons
|= REJECT_NON_FF_HEAD
;
575 *reject_reasons
|= REJECT_NON_FF_OTHER
;
576 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
577 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
578 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
579 *reject_reasons
|= REJECT_FETCH_FIRST
;
580 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
581 *reject_reasons
|= REJECT_NEEDS_FORCE
;
587 void transport_verify_remote_names(int nr_heads
, const char **heads
)
591 for (i
= 0; i
< nr_heads
; i
++) {
592 const char *local
= heads
[i
];
593 const char *remote
= strrchr(heads
[i
], ':');
598 /* A matching refspec is okay. */
599 if (remote
== local
&& remote
[1] == '\0')
602 remote
= remote
? (remote
+ 1) : local
;
603 if (check_refname_format(remote
,
604 REFNAME_ALLOW_ONELEVEL
|REFNAME_REFSPEC_PATTERN
))
605 die("remote part of refspec is not a valid name in %s",
610 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
612 struct git_transport_data
*data
= transport
->data
;
613 struct send_pack_args args
;
616 if (transport_color_config() < 0)
619 if (!data
->got_remote_heads
) {
620 struct ref
*tmp_refs
;
621 connect_setup(transport
, 1);
623 get_remote_heads(data
->fd
[0], NULL
, 0, &tmp_refs
, REF_NORMAL
,
624 NULL
, &data
->shallow
);
625 data
->got_remote_heads
= 1;
628 memset(&args
, 0, sizeof(args
));
629 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
630 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
631 args
.use_thin_pack
= data
->options
.thin
;
632 args
.verbose
= (transport
->verbose
> 0);
633 args
.quiet
= (transport
->verbose
< 0);
634 args
.progress
= transport
->progress
;
635 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
636 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
637 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
638 args
.push_options
= transport
->push_options
;
639 args
.url
= transport
->url
;
641 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
642 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
643 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
644 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
646 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
648 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
653 ret
|= finish_connect(data
->conn
);
655 data
->got_remote_heads
= 0;
660 static int connect_git(struct transport
*transport
, const char *name
,
661 const char *executable
, int fd
[2])
663 struct git_transport_data
*data
= transport
->data
;
664 data
->conn
= git_connect(data
->fd
, transport
->url
,
671 static int disconnect_git(struct transport
*transport
)
673 struct git_transport_data
*data
= transport
->data
;
675 if (data
->got_remote_heads
)
676 packet_flush(data
->fd
[1]);
679 finish_connect(data
->conn
);
686 static struct transport_vtable taken_over_vtable
= {
688 get_refs_via_connect
,
695 void transport_take_over(struct transport
*transport
,
696 struct child_process
*child
)
698 struct git_transport_data
*data
;
700 if (!transport
->smart_options
)
701 die("BUG: taking over transport requires non-NULL "
702 "smart_options field.");
704 data
= xcalloc(1, sizeof(*data
));
705 data
->options
= *transport
->smart_options
;
707 data
->fd
[0] = data
->conn
->out
;
708 data
->fd
[1] = data
->conn
->in
;
709 data
->got_remote_heads
= 0;
710 transport
->data
= data
;
712 transport
->vtable
= &taken_over_vtable
;
713 transport
->smart_options
= &(data
->options
);
715 transport
->cannot_reuse
= 1;
718 static int is_file(const char *url
)
723 return S_ISREG(buf
.st_mode
);
726 static int external_specification_len(const char *url
)
728 return strchr(url
, ':') - url
;
731 static const struct string_list
*protocol_whitelist(void)
733 static int enabled
= -1;
734 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
737 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
739 string_list_split(&allowed
, v
, ':', -1);
740 string_list_sort(&allowed
);
747 return enabled
? &allowed
: NULL
;
750 enum protocol_allow_config
{
751 PROTOCOL_ALLOW_NEVER
= 0,
752 PROTOCOL_ALLOW_USER_ONLY
,
753 PROTOCOL_ALLOW_ALWAYS
756 static enum protocol_allow_config
parse_protocol_config(const char *key
,
759 if (!strcasecmp(value
, "always"))
760 return PROTOCOL_ALLOW_ALWAYS
;
761 else if (!strcasecmp(value
, "never"))
762 return PROTOCOL_ALLOW_NEVER
;
763 else if (!strcasecmp(value
, "user"))
764 return PROTOCOL_ALLOW_USER_ONLY
;
766 die("unknown value for config '%s': %s", key
, value
);
769 static enum protocol_allow_config
get_protocol_config(const char *type
)
771 char *key
= xstrfmt("protocol.%s.allow", type
);
774 /* first check the per-protocol config */
775 if (!git_config_get_string(key
, &value
)) {
776 enum protocol_allow_config ret
=
777 parse_protocol_config(key
, value
);
784 /* if defined, fallback to user-defined default for unknown protocols */
785 if (!git_config_get_string("protocol.allow", &value
)) {
786 enum protocol_allow_config ret
=
787 parse_protocol_config("protocol.allow", value
);
792 /* fallback to built-in defaults */
794 if (!strcmp(type
, "http") ||
795 !strcmp(type
, "https") ||
796 !strcmp(type
, "git") ||
797 !strcmp(type
, "ssh") ||
798 !strcmp(type
, "file"))
799 return PROTOCOL_ALLOW_ALWAYS
;
801 /* known scary; err on the side of caution */
802 if (!strcmp(type
, "ext"))
803 return PROTOCOL_ALLOW_NEVER
;
805 /* unknown; by default let them be used only directly by the user */
806 return PROTOCOL_ALLOW_USER_ONLY
;
809 int is_transport_allowed(const char *type
, int from_user
)
811 const struct string_list
*whitelist
= protocol_whitelist();
813 return string_list_has_string(whitelist
, type
);
815 switch (get_protocol_config(type
)) {
816 case PROTOCOL_ALLOW_ALWAYS
:
818 case PROTOCOL_ALLOW_NEVER
:
820 case PROTOCOL_ALLOW_USER_ONLY
:
822 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
826 die("BUG: invalid protocol_allow_config type");
829 void transport_check_allowed(const char *type
)
831 if (!is_transport_allowed(type
, -1))
832 die("transport '%s' not allowed", type
);
835 static struct transport_vtable bundle_vtable
= {
837 get_refs_from_bundle
,
838 fetch_refs_from_bundle
,
844 static struct transport_vtable builtin_smart_vtable
= {
846 get_refs_via_connect
,
853 struct transport
*transport_get(struct remote
*remote
, const char *url
)
856 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
858 ret
->progress
= isatty(2);
861 die("No remote provided to transport_get()");
863 ret
->got_remote_refs
= 0;
864 ret
->remote
= remote
;
865 helper
= remote
->foreign_vcs
;
867 if (!url
&& remote
->url
)
868 url
= remote
->url
[0];
871 /* maybe it is a foreign URL? */
875 while (is_urlschemechar(p
== url
, *p
))
877 if (starts_with(p
, "::"))
878 helper
= xstrndup(url
, p
- url
);
882 transport_helper_init(ret
, helper
);
883 } else if (starts_with(url
, "rsync:")) {
884 die("git-over-rsync is no longer supported");
885 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
886 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
887 transport_check_allowed("file");
889 ret
->vtable
= &bundle_vtable
;
890 ret
->smart_options
= NULL
;
891 } else if (!is_url(url
)
892 || starts_with(url
, "file://")
893 || starts_with(url
, "git://")
894 || starts_with(url
, "ssh://")
895 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
896 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
899 * These are builtin smart transports; "allowed" transports
900 * will be checked individually in git_connect.
902 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
904 ret
->vtable
= &builtin_smart_vtable
;
905 ret
->smart_options
= &(data
->options
);
908 data
->got_remote_heads
= 0;
910 /* Unknown protocol in URL. Pass to external handler. */
911 int len
= external_specification_len(url
);
912 char *handler
= xmemdupz(url
, len
);
913 transport_helper_init(ret
, handler
);
916 if (ret
->smart_options
) {
917 ret
->smart_options
->thin
= 1;
918 ret
->smart_options
->uploadpack
= "git-upload-pack";
919 if (remote
->uploadpack
)
920 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
921 ret
->smart_options
->receivepack
= "git-receive-pack";
922 if (remote
->receivepack
)
923 ret
->smart_options
->receivepack
= remote
->receivepack
;
929 int transport_set_option(struct transport
*transport
,
930 const char *name
, const char *value
)
932 int git_reports
= 1, protocol_reports
= 1;
934 if (transport
->smart_options
)
935 git_reports
= set_git_option(transport
->smart_options
,
938 if (transport
->vtable
->set_option
)
939 protocol_reports
= transport
->vtable
->set_option(transport
,
942 /* If either report is 0, report 0 (success). */
943 if (!git_reports
|| !protocol_reports
)
945 /* If either reports -1 (invalid value), report -1. */
946 if ((git_reports
== -1) || (protocol_reports
== -1))
948 /* Otherwise if both report unknown, report unknown. */
952 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
956 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
958 transport
->verbose
= -1;
961 * Rules used to determine whether to report progress (processing aborts
962 * when a rule is satisfied):
964 * . Report progress, if force_progress is 1 (ie. --progress).
965 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
966 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
967 * . Report progress if isatty(2) is 1.
969 if (force_progress
>= 0)
970 transport
->progress
= !!force_progress
;
972 transport
->progress
= verbosity
>= 0 && isatty(2);
975 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
979 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
980 "not be found on any remote:\n"));
981 for (i
= 0; i
< needs_pushing
->nr
; i
++)
982 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
983 fprintf(stderr
, _("\nPlease try\n\n"
984 " git push --recurse-submodules=on-demand\n\n"
985 "or cd to the path and use\n\n"
987 "to push them to a remote.\n\n"));
989 string_list_clear(needs_pushing
, 0);
994 static int run_pre_push_hook(struct transport
*transport
,
995 struct ref
*remote_refs
)
999 struct child_process proc
= CHILD_PROCESS_INIT
;
1001 const char *argv
[4];
1003 if (!(argv
[0] = find_hook("pre-push")))
1006 argv
[1] = transport
->remote
->name
;
1007 argv
[2] = transport
->url
;
1013 if (start_command(&proc
)) {
1014 finish_command(&proc
);
1018 sigchain_push(SIGPIPE
, SIG_IGN
);
1020 strbuf_init(&buf
, 256);
1022 for (r
= remote_refs
; r
; r
= r
->next
) {
1023 if (!r
->peer_ref
) continue;
1024 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
1025 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
1026 if (r
->status
== REF_STATUS_UPTODATE
) continue;
1029 strbuf_addf( &buf
, "%s %s %s %s\n",
1030 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
1031 r
->name
, oid_to_hex(&r
->old_oid
));
1033 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
1034 /* We do not mind if a hook does not read all refs. */
1041 strbuf_release(&buf
);
1047 sigchain_pop(SIGPIPE
);
1049 x
= finish_command(&proc
);
1056 int transport_push(struct transport
*transport
,
1057 int refspec_nr
, const char **refspec
, int flags
,
1058 unsigned int *reject_reasons
)
1060 *reject_reasons
= 0;
1061 transport_verify_remote_names(refspec_nr
, refspec
);
1063 if (transport_color_config() < 0)
1066 if (transport
->vtable
->push_refs
) {
1067 struct ref
*remote_refs
;
1068 struct ref
*local_refs
= get_local_heads();
1069 int match_flags
= MATCH_REFS_NONE
;
1070 int verbose
= (transport
->verbose
> 0);
1071 int quiet
= (transport
->verbose
< 0);
1072 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1073 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1074 int push_ret
, ret
, err
;
1076 if (check_push_refs(local_refs
, refspec_nr
, refspec
) < 0)
1079 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1);
1081 if (flags
& TRANSPORT_PUSH_ALL
)
1082 match_flags
|= MATCH_REFS_ALL
;
1083 if (flags
& TRANSPORT_PUSH_MIRROR
)
1084 match_flags
|= MATCH_REFS_MIRROR
;
1085 if (flags
& TRANSPORT_PUSH_PRUNE
)
1086 match_flags
|= MATCH_REFS_PRUNE
;
1087 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1088 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1090 if (match_push_refs(local_refs
, &remote_refs
,
1091 refspec_nr
, refspec
, match_flags
)) {
1095 if (transport
->smart_options
&&
1096 transport
->smart_options
->cas
&&
1097 !is_empty_cas(transport
->smart_options
->cas
))
1098 apply_push_cas(transport
->smart_options
->cas
,
1099 transport
->remote
, remote_refs
);
1101 set_ref_status_for_push(remote_refs
,
1102 flags
& TRANSPORT_PUSH_MIRROR
,
1103 flags
& TRANSPORT_PUSH_FORCE
);
1105 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1106 if (run_pre_push_hook(transport
, remote_refs
))
1109 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1110 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1111 !is_bare_repository()) {
1112 struct ref
*ref
= remote_refs
;
1113 struct oid_array commits
= OID_ARRAY_INIT
;
1115 for (; ref
; ref
= ref
->next
)
1116 if (!is_null_oid(&ref
->new_oid
))
1117 oid_array_append(&commits
,
1120 if (!push_unpushed_submodules(&commits
,
1122 refspec
, refspec_nr
,
1123 transport
->push_options
,
1125 oid_array_clear(&commits
);
1126 die("Failed to push all needed submodules!");
1128 oid_array_clear(&commits
);
1131 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1132 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1133 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1134 !pretend
)) && !is_bare_repository()) {
1135 struct ref
*ref
= remote_refs
;
1136 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1137 struct oid_array commits
= OID_ARRAY_INIT
;
1139 for (; ref
; ref
= ref
->next
)
1140 if (!is_null_oid(&ref
->new_oid
))
1141 oid_array_append(&commits
,
1144 if (find_unpushed_submodules(&commits
, transport
->remote
->name
,
1146 oid_array_clear(&commits
);
1147 die_with_unpushed_submodules(&needs_pushing
);
1149 string_list_clear(&needs_pushing
, 0);
1150 oid_array_clear(&commits
);
1153 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
))
1154 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1157 err
= push_had_errors(remote_refs
);
1158 ret
= push_ret
| err
;
1161 transport_print_push_status(transport
->url
, remote_refs
,
1162 verbose
| porcelain
, porcelain
,
1165 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1166 set_upstreams(transport
, remote_refs
, pretend
);
1168 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1169 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1171 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1172 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1175 if (porcelain
&& !push_ret
)
1177 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1178 fprintf(stderr
, "Everything up-to-date\n");
1185 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
1187 if (!transport
->got_remote_refs
) {
1188 transport
->remote_refs
= transport
->vtable
->get_refs_list(transport
, 0);
1189 transport
->got_remote_refs
= 1;
1192 return transport
->remote_refs
;
1195 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1198 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1199 struct ref
**heads
= NULL
;
1202 for (rm
= refs
; rm
; rm
= rm
->next
) {
1205 !is_null_oid(&rm
->old_oid
) &&
1206 !oidcmp(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1208 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1209 heads
[nr_heads
++] = rm
;
1214 * When deepening of a shallow repository is requested,
1215 * then local and remote refs are likely to still be equal.
1216 * Just feed them all to the fetch method in that case.
1217 * This condition shouldn't be met in a non-deepening fetch
1218 * (see builtin/fetch.c:quickfetch()).
1220 ALLOC_ARRAY(heads
, nr_refs
);
1221 for (rm
= refs
; rm
; rm
= rm
->next
)
1222 heads
[nr_heads
++] = rm
;
1225 rc
= transport
->vtable
->fetch(transport
, nr_heads
, heads
);
1231 void transport_unlock_pack(struct transport
*transport
)
1233 if (transport
->pack_lockfile
) {
1234 unlink_or_warn(transport
->pack_lockfile
);
1235 FREE_AND_NULL(transport
->pack_lockfile
);
1239 int transport_connect(struct transport
*transport
, const char *name
,
1240 const char *exec
, int fd
[2])
1242 if (transport
->vtable
->connect
)
1243 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1245 die("Operation not supported by protocol");
1248 int transport_disconnect(struct transport
*transport
)
1251 if (transport
->vtable
->disconnect
)
1252 ret
= transport
->vtable
->disconnect(transport
);
1258 * Strip username (and password) from a URL and return
1259 * it in a newly allocated string.
1261 char *transport_anonymize_url(const char *url
)
1263 char *scheme_prefix
, *anon_part
;
1264 size_t anon_len
, prefix_len
= 0;
1266 anon_part
= strchr(url
, '@');
1267 if (url_is_local_not_ssh(url
) || !anon_part
)
1270 anon_len
= strlen(++anon_part
);
1271 scheme_prefix
= strstr(url
, "://");
1272 if (!scheme_prefix
) {
1273 if (!strchr(anon_part
, ':'))
1274 /* cannot be "me@there:/path/name" */
1278 /* make sure scheme is reasonable */
1279 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1282 case '+': case '.': case '-':
1291 /* @ past the first slash does not count */
1292 cp
= strchr(scheme_prefix
+ 3, '/');
1293 if (cp
&& cp
< anon_part
)
1295 prefix_len
= scheme_prefix
- url
+ 3;
1297 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1298 (int)anon_len
, anon_part
);
1300 return xstrdup(url
);
1303 static void read_alternate_refs(const char *path
,
1304 alternate_ref_fn
*cb
,
1307 struct child_process cmd
= CHILD_PROCESS_INIT
;
1308 struct strbuf line
= STRBUF_INIT
;
1312 argv_array_pushf(&cmd
.args
, "--git-dir=%s", path
);
1313 argv_array_push(&cmd
.args
, "for-each-ref");
1314 argv_array_push(&cmd
.args
, "--format=%(objectname) %(refname)");
1315 cmd
.env
= local_repo_env
;
1318 if (start_command(&cmd
))
1321 fh
= xfdopen(cmd
.out
, "r");
1322 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
1323 struct object_id oid
;
1325 if (get_oid_hex(line
.buf
, &oid
) ||
1326 line
.buf
[GIT_SHA1_HEXSZ
] != ' ') {
1327 warning("invalid line while parsing alternate refs: %s",
1332 cb(line
.buf
+ GIT_SHA1_HEXSZ
+ 1, &oid
, data
);
1336 finish_command(&cmd
);
1339 struct alternate_refs_data
{
1340 alternate_ref_fn
*fn
;
1344 static int refs_from_alternate_cb(struct alternate_object_database
*e
,
1347 struct strbuf path
= STRBUF_INIT
;
1349 struct alternate_refs_data
*cb
= data
;
1351 if (!strbuf_realpath(&path
, e
->path
, 0))
1353 if (!strbuf_strip_suffix(&path
, "/objects"))
1355 base_len
= path
.len
;
1357 /* Is this a git repository with refs? */
1358 strbuf_addstr(&path
, "/refs");
1359 if (!is_directory(path
.buf
))
1361 strbuf_setlen(&path
, base_len
);
1363 read_alternate_refs(path
.buf
, cb
->fn
, cb
->data
);
1366 strbuf_release(&path
);
1370 void for_each_alternate_ref(alternate_ref_fn fn
, void *data
)
1372 struct alternate_refs_data cb
;
1375 foreach_alt_odb(refs_from_alternate_cb
, &cb
);