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"
23 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
27 for (ref
= refs
; ref
; ref
= ref
->next
) {
28 const char *localname
;
30 const char *remotename
;
33 * Check suitability for tracking. Must be successful /
34 * already up-to-date ref create/modify (not delete).
36 if (ref
->status
!= REF_STATUS_OK
&&
37 ref
->status
!= REF_STATUS_UPTODATE
)
41 if (is_null_oid(&ref
->new_oid
))
44 /* Follow symbolic refs (mainly for HEAD). */
45 localname
= ref
->peer_ref
->name
;
46 remotename
= ref
->name
;
47 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
49 if (tmp
&& flag
& REF_ISSYMREF
&&
50 starts_with(tmp
, "refs/heads/"))
53 /* Both source and destination must be local branches. */
54 if (!localname
|| !starts_with(localname
, "refs/heads/"))
56 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
60 install_branch_config(BRANCH_CONFIG_VERBOSE
,
61 localname
+ 11, transport
->remote
->name
,
64 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
65 localname
+ 11, remotename
+ 11,
66 transport
->remote
->name
);
70 struct bundle_transport_data
{
72 struct bundle_header header
;
75 static struct ref
*get_refs_from_bundle(struct transport
*transport
,
77 const struct argv_array
*ref_prefixes
)
79 struct bundle_transport_data
*data
= transport
->data
;
80 struct ref
*result
= NULL
;
88 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
90 die ("Could not read bundle '%s'.", transport
->url
);
91 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
92 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
93 struct ref
*ref
= alloc_ref(e
->name
);
94 oidcpy(&ref
->old_oid
, &e
->oid
);
101 static int fetch_refs_from_bundle(struct transport
*transport
,
102 int nr_heads
, struct ref
**to_fetch
)
104 struct bundle_transport_data
*data
= transport
->data
;
105 return unbundle(&data
->header
, data
->fd
,
106 transport
->progress
? BUNDLE_VERBOSE
: 0);
109 static int close_bundle(struct transport
*transport
)
111 struct bundle_transport_data
*data
= transport
->data
;
118 struct git_transport_data
{
119 struct git_transport_options options
;
120 struct child_process
*conn
;
122 unsigned got_remote_heads
: 1;
123 enum protocol_version version
;
124 struct oid_array extra_have
;
125 struct oid_array shallow
;
128 static int set_git_option(struct git_transport_options
*opts
,
129 const char *name
, const char *value
)
131 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
132 opts
->uploadpack
= value
;
134 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
135 opts
->receivepack
= value
;
137 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
138 opts
->thin
= !!value
;
140 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
141 opts
->followtags
= !!value
;
143 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
144 opts
->keep
= !!value
;
146 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
147 opts
->update_shallow
= !!value
;
149 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
154 opts
->depth
= strtol(value
, &end
, 0);
156 die(_("transport: invalid depth option '%s'"), value
);
159 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
160 opts
->deepen_since
= value
;
162 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
163 opts
->deepen_not
= (const struct string_list
*)value
;
165 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
166 opts
->deepen_relative
= !!value
;
172 static int connect_setup(struct transport
*transport
, int for_push
)
174 struct git_transport_data
*data
= transport
->data
;
175 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
180 switch (transport
->family
) {
181 case TRANSPORT_FAMILY_ALL
: break;
182 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
183 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
186 data
->conn
= git_connect(data
->fd
, transport
->url
,
187 for_push
? data
->options
.receivepack
:
188 data
->options
.uploadpack
,
194 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
,
195 const struct argv_array
*ref_prefixes
)
197 struct git_transport_data
*data
= transport
->data
;
198 struct ref
*refs
= NULL
;
199 struct packet_reader reader
;
201 connect_setup(transport
, for_push
);
203 packet_reader_init(&reader
, data
->fd
[0], NULL
, 0,
204 PACKET_READ_CHOMP_NEWLINE
|
205 PACKET_READ_GENTLE_ON_EOF
);
207 data
->version
= discover_version(&reader
);
208 switch (data
->version
) {
210 get_remote_refs(data
->fd
[1], &reader
, &refs
, for_push
,
215 get_remote_heads(&reader
, &refs
,
216 for_push
? REF_NORMAL
: 0,
220 case protocol_unknown_version
:
221 BUG("unknown protocol version");
223 data
->got_remote_heads
= 1;
228 static int fetch_refs_via_pack(struct transport
*transport
,
229 int nr_heads
, struct ref
**to_fetch
)
232 struct git_transport_data
*data
= transport
->data
;
233 struct ref
*refs
= NULL
;
234 char *dest
= xstrdup(transport
->url
);
235 struct fetch_pack_args args
;
236 struct ref
*refs_tmp
= NULL
;
238 memset(&args
, 0, sizeof(args
));
239 args
.uploadpack
= data
->options
.uploadpack
;
240 args
.keep_pack
= data
->options
.keep
;
242 args
.use_thin_pack
= data
->options
.thin
;
243 args
.include_tag
= data
->options
.followtags
;
244 args
.verbose
= (transport
->verbose
> 1);
245 args
.quiet
= (transport
->verbose
< 0);
246 args
.no_progress
= !transport
->progress
;
247 args
.depth
= data
->options
.depth
;
248 args
.deepen_since
= data
->options
.deepen_since
;
249 args
.deepen_not
= data
->options
.deepen_not
;
250 args
.deepen_relative
= data
->options
.deepen_relative
;
251 args
.check_self_contained_and_connected
=
252 data
->options
.check_self_contained_and_connected
;
253 args
.cloning
= transport
->cloning
;
254 args
.update_shallow
= data
->options
.update_shallow
;
255 args
.stateless_rpc
= transport
->stateless_rpc
;
257 if (!data
->got_remote_heads
)
258 refs_tmp
= get_refs_via_connect(transport
, 0, NULL
);
260 switch (data
->version
) {
262 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
263 refs_tmp
? refs_tmp
: transport
->remote_refs
,
264 dest
, to_fetch
, nr_heads
, &data
->shallow
,
265 &transport
->pack_lockfile
, data
->version
);
269 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
270 refs_tmp
? refs_tmp
: transport
->remote_refs
,
271 dest
, to_fetch
, nr_heads
, &data
->shallow
,
272 &transport
->pack_lockfile
, data
->version
);
274 case protocol_unknown_version
:
275 BUG("unknown protocol version");
280 if (finish_connect(data
->conn
))
283 data
->got_remote_heads
= 0;
284 data
->options
.self_contained_and_connected
=
285 args
.self_contained_and_connected
;
289 if (report_unmatched_refs(to_fetch
, nr_heads
))
298 static int push_had_errors(struct ref
*ref
)
300 for (; ref
; ref
= ref
->next
) {
301 switch (ref
->status
) {
302 case REF_STATUS_NONE
:
303 case REF_STATUS_UPTODATE
:
313 int transport_refs_pushed(struct ref
*ref
)
315 for (; ref
; ref
= ref
->next
) {
316 switch(ref
->status
) {
317 case REF_STATUS_NONE
:
318 case REF_STATUS_UPTODATE
:
327 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
331 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
337 if (!remote_find_tracking(remote
, &rs
)) {
339 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
341 delete_ref(NULL
, rs
.dst
, NULL
, 0);
343 update_ref("update by push", rs
.dst
, &ref
->new_oid
,
349 static void print_ref_status(char flag
, const char *summary
,
350 struct ref
*to
, struct ref
*from
, const char *msg
,
351 int porcelain
, int summary_width
)
355 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
357 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
359 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
361 fprintf(stdout
, "%s\n", summary
);
363 fprintf(stderr
, " %c %-*s ", flag
, summary_width
, summary
);
365 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
367 fputs(prettify_refname(to
->name
), stderr
);
377 static void print_ok_ref_status(struct ref
*ref
, int porcelain
, int summary_width
)
380 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
381 porcelain
, summary_width
);
382 else if (is_null_oid(&ref
->old_oid
))
383 print_ref_status('*',
384 (starts_with(ref
->name
, "refs/tags/") ? "[new tag]" :
386 ref
, ref
->peer_ref
, NULL
, porcelain
, summary_width
);
388 struct strbuf quickref
= STRBUF_INIT
;
392 strbuf_add_unique_abbrev(&quickref
, ref
->old_oid
.hash
,
394 if (ref
->forced_update
) {
395 strbuf_addstr(&quickref
, "...");
397 msg
= "forced update";
399 strbuf_addstr(&quickref
, "..");
403 strbuf_add_unique_abbrev(&quickref
, ref
->new_oid
.hash
,
406 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
407 porcelain
, summary_width
);
408 strbuf_release(&quickref
);
412 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
413 int porcelain
, int summary_width
)
416 char *url
= transport_anonymize_url(dest
);
417 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
421 switch(ref
->status
) {
422 case REF_STATUS_NONE
:
423 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
424 porcelain
, summary_width
);
426 case REF_STATUS_REJECT_NODELETE
:
427 print_ref_status('!', "[rejected]", ref
, NULL
,
428 "remote does not support deleting refs",
429 porcelain
, summary_width
);
431 case REF_STATUS_UPTODATE
:
432 print_ref_status('=', "[up to date]", ref
,
433 ref
->peer_ref
, NULL
, porcelain
, summary_width
);
435 case REF_STATUS_REJECT_NONFASTFORWARD
:
436 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
437 "non-fast-forward", porcelain
, summary_width
);
439 case REF_STATUS_REJECT_ALREADY_EXISTS
:
440 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
441 "already exists", porcelain
, summary_width
);
443 case REF_STATUS_REJECT_FETCH_FIRST
:
444 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
445 "fetch first", porcelain
, summary_width
);
447 case REF_STATUS_REJECT_NEEDS_FORCE
:
448 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
449 "needs force", porcelain
, summary_width
);
451 case REF_STATUS_REJECT_STALE
:
452 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
453 "stale info", porcelain
, summary_width
);
455 case REF_STATUS_REJECT_SHALLOW
:
456 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
457 "new shallow roots not allowed",
458 porcelain
, summary_width
);
460 case REF_STATUS_REMOTE_REJECT
:
461 print_ref_status('!', "[remote rejected]", ref
,
462 ref
->deletion
? NULL
: ref
->peer_ref
,
463 ref
->remote_status
, porcelain
, summary_width
);
465 case REF_STATUS_EXPECTING_REPORT
:
466 print_ref_status('!', "[remote failure]", ref
,
467 ref
->deletion
? NULL
: ref
->peer_ref
,
468 "remote failed to report status",
469 porcelain
, summary_width
);
471 case REF_STATUS_ATOMIC_PUSH_FAILED
:
472 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
473 "atomic push failed", porcelain
, summary_width
);
476 print_ok_ref_status(ref
, porcelain
, summary_width
);
483 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
485 char hex
[GIT_MAX_HEXSZ
+ 1];
486 int w
= find_unique_abbrev_r(hex
, oid
->hash
, DEFAULT_ABBREV
);
488 return (w
< sofar
) ? sofar
: w
;
491 int transport_summary_width(const struct ref
*refs
)
495 for (; refs
; refs
= refs
->next
) {
496 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
497 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
500 maxw
= FALLBACK_DEFAULT_ABBREV
;
501 return (2 * maxw
+ 3);
504 void transport_print_push_status(const char *dest
, struct ref
*refs
,
505 int verbose
, int porcelain
, unsigned int *reject_reasons
)
510 int summary_width
= transport_summary_width(refs
);
512 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
515 for (ref
= refs
; ref
; ref
= ref
->next
)
516 if (ref
->status
== REF_STATUS_UPTODATE
)
517 n
+= print_one_push_status(ref
, dest
, n
,
518 porcelain
, summary_width
);
521 for (ref
= refs
; ref
; ref
= ref
->next
)
522 if (ref
->status
== REF_STATUS_OK
)
523 n
+= print_one_push_status(ref
, dest
, n
,
524 porcelain
, summary_width
);
527 for (ref
= refs
; ref
; ref
= ref
->next
) {
528 if (ref
->status
!= REF_STATUS_NONE
&&
529 ref
->status
!= REF_STATUS_UPTODATE
&&
530 ref
->status
!= REF_STATUS_OK
)
531 n
+= print_one_push_status(ref
, dest
, n
,
532 porcelain
, summary_width
);
533 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
534 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
535 *reject_reasons
|= REJECT_NON_FF_HEAD
;
537 *reject_reasons
|= REJECT_NON_FF_OTHER
;
538 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
539 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
540 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
541 *reject_reasons
|= REJECT_FETCH_FIRST
;
542 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
543 *reject_reasons
|= REJECT_NEEDS_FORCE
;
549 void transport_verify_remote_names(int nr_heads
, const char **heads
)
553 for (i
= 0; i
< nr_heads
; i
++) {
554 const char *local
= heads
[i
];
555 const char *remote
= strrchr(heads
[i
], ':');
560 /* A matching refspec is okay. */
561 if (remote
== local
&& remote
[1] == '\0')
564 remote
= remote
? (remote
+ 1) : local
;
565 if (check_refname_format(remote
,
566 REFNAME_ALLOW_ONELEVEL
|REFNAME_REFSPEC_PATTERN
))
567 die("remote part of refspec is not a valid name in %s",
572 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
574 struct git_transport_data
*data
= transport
->data
;
575 struct send_pack_args args
;
578 if (!data
->got_remote_heads
)
579 get_refs_via_connect(transport
, 1, NULL
);
581 memset(&args
, 0, sizeof(args
));
582 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
583 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
584 args
.use_thin_pack
= data
->options
.thin
;
585 args
.verbose
= (transport
->verbose
> 0);
586 args
.quiet
= (transport
->verbose
< 0);
587 args
.progress
= transport
->progress
;
588 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
589 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
590 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
591 args
.push_options
= transport
->push_options
;
592 args
.url
= transport
->url
;
594 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
595 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
596 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
597 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
599 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
601 switch (data
->version
) {
603 die("support for protocol v2 not implemented yet");
607 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
610 case protocol_unknown_version
:
611 BUG("unknown protocol version");
616 ret
|= finish_connect(data
->conn
);
618 data
->got_remote_heads
= 0;
623 static int connect_git(struct transport
*transport
, const char *name
,
624 const char *executable
, int fd
[2])
626 struct git_transport_data
*data
= transport
->data
;
627 data
->conn
= git_connect(data
->fd
, transport
->url
,
634 static int disconnect_git(struct transport
*transport
)
636 struct git_transport_data
*data
= transport
->data
;
638 if (data
->got_remote_heads
)
639 packet_flush(data
->fd
[1]);
642 finish_connect(data
->conn
);
649 static struct transport_vtable taken_over_vtable
= {
651 get_refs_via_connect
,
658 void transport_take_over(struct transport
*transport
,
659 struct child_process
*child
)
661 struct git_transport_data
*data
;
663 if (!transport
->smart_options
)
664 die("BUG: taking over transport requires non-NULL "
665 "smart_options field.");
667 data
= xcalloc(1, sizeof(*data
));
668 data
->options
= *transport
->smart_options
;
670 data
->fd
[0] = data
->conn
->out
;
671 data
->fd
[1] = data
->conn
->in
;
672 data
->got_remote_heads
= 0;
673 transport
->data
= data
;
675 transport
->vtable
= &taken_over_vtable
;
676 transport
->smart_options
= &(data
->options
);
678 transport
->cannot_reuse
= 1;
681 static int is_file(const char *url
)
686 return S_ISREG(buf
.st_mode
);
689 static int external_specification_len(const char *url
)
691 return strchr(url
, ':') - url
;
694 static const struct string_list
*protocol_whitelist(void)
696 static int enabled
= -1;
697 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
700 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
702 string_list_split(&allowed
, v
, ':', -1);
703 string_list_sort(&allowed
);
710 return enabled
? &allowed
: NULL
;
713 enum protocol_allow_config
{
714 PROTOCOL_ALLOW_NEVER
= 0,
715 PROTOCOL_ALLOW_USER_ONLY
,
716 PROTOCOL_ALLOW_ALWAYS
719 static enum protocol_allow_config
parse_protocol_config(const char *key
,
722 if (!strcasecmp(value
, "always"))
723 return PROTOCOL_ALLOW_ALWAYS
;
724 else if (!strcasecmp(value
, "never"))
725 return PROTOCOL_ALLOW_NEVER
;
726 else if (!strcasecmp(value
, "user"))
727 return PROTOCOL_ALLOW_USER_ONLY
;
729 die("unknown value for config '%s': %s", key
, value
);
732 static enum protocol_allow_config
get_protocol_config(const char *type
)
734 char *key
= xstrfmt("protocol.%s.allow", type
);
737 /* first check the per-protocol config */
738 if (!git_config_get_string(key
, &value
)) {
739 enum protocol_allow_config ret
=
740 parse_protocol_config(key
, value
);
747 /* if defined, fallback to user-defined default for unknown protocols */
748 if (!git_config_get_string("protocol.allow", &value
)) {
749 enum protocol_allow_config ret
=
750 parse_protocol_config("protocol.allow", value
);
755 /* fallback to built-in defaults */
757 if (!strcmp(type
, "http") ||
758 !strcmp(type
, "https") ||
759 !strcmp(type
, "git") ||
760 !strcmp(type
, "ssh") ||
761 !strcmp(type
, "file"))
762 return PROTOCOL_ALLOW_ALWAYS
;
764 /* known scary; err on the side of caution */
765 if (!strcmp(type
, "ext"))
766 return PROTOCOL_ALLOW_NEVER
;
768 /* unknown; by default let them be used only directly by the user */
769 return PROTOCOL_ALLOW_USER_ONLY
;
772 int is_transport_allowed(const char *type
, int from_user
)
774 const struct string_list
*whitelist
= protocol_whitelist();
776 return string_list_has_string(whitelist
, type
);
778 switch (get_protocol_config(type
)) {
779 case PROTOCOL_ALLOW_ALWAYS
:
781 case PROTOCOL_ALLOW_NEVER
:
783 case PROTOCOL_ALLOW_USER_ONLY
:
785 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
789 die("BUG: invalid protocol_allow_config type");
792 void transport_check_allowed(const char *type
)
794 if (!is_transport_allowed(type
, -1))
795 die("transport '%s' not allowed", type
);
798 static struct transport_vtable bundle_vtable
= {
800 get_refs_from_bundle
,
801 fetch_refs_from_bundle
,
807 static struct transport_vtable builtin_smart_vtable
= {
809 get_refs_via_connect
,
816 struct transport
*transport_get(struct remote
*remote
, const char *url
)
819 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
821 ret
->progress
= isatty(2);
824 die("No remote provided to transport_get()");
826 ret
->got_remote_refs
= 0;
827 ret
->remote
= remote
;
828 helper
= remote
->foreign_vcs
;
830 if (!url
&& remote
->url
)
831 url
= remote
->url
[0];
834 /* maybe it is a foreign URL? */
838 while (is_urlschemechar(p
== url
, *p
))
840 if (starts_with(p
, "::"))
841 helper
= xstrndup(url
, p
- url
);
845 transport_helper_init(ret
, helper
);
846 } else if (starts_with(url
, "rsync:")) {
847 die("git-over-rsync is no longer supported");
848 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
849 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
850 transport_check_allowed("file");
852 ret
->vtable
= &bundle_vtable
;
853 ret
->smart_options
= NULL
;
854 } else if (!is_url(url
)
855 || starts_with(url
, "file://")
856 || starts_with(url
, "git://")
857 || starts_with(url
, "ssh://")
858 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
859 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
862 * These are builtin smart transports; "allowed" transports
863 * will be checked individually in git_connect.
865 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
867 ret
->vtable
= &builtin_smart_vtable
;
868 ret
->smart_options
= &(data
->options
);
871 data
->got_remote_heads
= 0;
873 /* Unknown protocol in URL. Pass to external handler. */
874 int len
= external_specification_len(url
);
875 char *handler
= xmemdupz(url
, len
);
876 transport_helper_init(ret
, handler
);
879 if (ret
->smart_options
) {
880 ret
->smart_options
->thin
= 1;
881 ret
->smart_options
->uploadpack
= "git-upload-pack";
882 if (remote
->uploadpack
)
883 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
884 ret
->smart_options
->receivepack
= "git-receive-pack";
885 if (remote
->receivepack
)
886 ret
->smart_options
->receivepack
= remote
->receivepack
;
892 int transport_set_option(struct transport
*transport
,
893 const char *name
, const char *value
)
895 int git_reports
= 1, protocol_reports
= 1;
897 if (transport
->smart_options
)
898 git_reports
= set_git_option(transport
->smart_options
,
901 if (transport
->vtable
->set_option
)
902 protocol_reports
= transport
->vtable
->set_option(transport
,
905 /* If either report is 0, report 0 (success). */
906 if (!git_reports
|| !protocol_reports
)
908 /* If either reports -1 (invalid value), report -1. */
909 if ((git_reports
== -1) || (protocol_reports
== -1))
911 /* Otherwise if both report unknown, report unknown. */
915 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
919 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
921 transport
->verbose
= -1;
924 * Rules used to determine whether to report progress (processing aborts
925 * when a rule is satisfied):
927 * . Report progress, if force_progress is 1 (ie. --progress).
928 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
929 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
930 * . Report progress if isatty(2) is 1.
932 if (force_progress
>= 0)
933 transport
->progress
= !!force_progress
;
935 transport
->progress
= verbosity
>= 0 && isatty(2);
938 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
942 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
943 "not be found on any remote:\n"));
944 for (i
= 0; i
< needs_pushing
->nr
; i
++)
945 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
946 fprintf(stderr
, _("\nPlease try\n\n"
947 " git push --recurse-submodules=on-demand\n\n"
948 "or cd to the path and use\n\n"
950 "to push them to a remote.\n\n"));
952 string_list_clear(needs_pushing
, 0);
957 static int run_pre_push_hook(struct transport
*transport
,
958 struct ref
*remote_refs
)
962 struct child_process proc
= CHILD_PROCESS_INIT
;
966 if (!(argv
[0] = find_hook("pre-push")))
969 argv
[1] = transport
->remote
->name
;
970 argv
[2] = transport
->url
;
976 if (start_command(&proc
)) {
977 finish_command(&proc
);
981 sigchain_push(SIGPIPE
, SIG_IGN
);
983 strbuf_init(&buf
, 256);
985 for (r
= remote_refs
; r
; r
= r
->next
) {
986 if (!r
->peer_ref
) continue;
987 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
988 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
989 if (r
->status
== REF_STATUS_UPTODATE
) continue;
992 strbuf_addf( &buf
, "%s %s %s %s\n",
993 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
994 r
->name
, oid_to_hex(&r
->old_oid
));
996 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
997 /* We do not mind if a hook does not read all refs. */
1004 strbuf_release(&buf
);
1010 sigchain_pop(SIGPIPE
);
1012 x
= finish_command(&proc
);
1019 int transport_push(struct transport
*transport
,
1020 int refspec_nr
, const char **refspec
, int flags
,
1021 unsigned int *reject_reasons
)
1023 *reject_reasons
= 0;
1024 transport_verify_remote_names(refspec_nr
, refspec
);
1026 if (transport
->vtable
->push_refs
) {
1027 struct ref
*remote_refs
;
1028 struct ref
*local_refs
= get_local_heads();
1029 int match_flags
= MATCH_REFS_NONE
;
1030 int verbose
= (transport
->verbose
> 0);
1031 int quiet
= (transport
->verbose
< 0);
1032 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1033 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1034 int push_ret
, ret
, err
;
1035 struct refspec
*tmp_rs
;
1036 struct argv_array ref_prefixes
= ARGV_ARRAY_INIT
;
1039 if (check_push_refs(local_refs
, refspec_nr
, refspec
) < 0)
1042 tmp_rs
= parse_push_refspec(refspec_nr
, refspec
);
1043 for (i
= 0; i
< refspec_nr
; i
++) {
1044 const char *prefix
= NULL
;
1047 prefix
= tmp_rs
[i
].dst
;
1048 else if (tmp_rs
[i
].src
&& !tmp_rs
[i
].exact_sha1
)
1049 prefix
= tmp_rs
[i
].src
;
1052 const char *glob
= strchr(prefix
, '*');
1054 argv_array_pushf(&ref_prefixes
, "%.*s",
1055 (int)(glob
- prefix
),
1058 expand_ref_prefix(&ref_prefixes
, prefix
);
1062 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1,
1065 argv_array_clear(&ref_prefixes
);
1066 free_refspec(refspec_nr
, tmp_rs
);
1068 if (flags
& TRANSPORT_PUSH_ALL
)
1069 match_flags
|= MATCH_REFS_ALL
;
1070 if (flags
& TRANSPORT_PUSH_MIRROR
)
1071 match_flags
|= MATCH_REFS_MIRROR
;
1072 if (flags
& TRANSPORT_PUSH_PRUNE
)
1073 match_flags
|= MATCH_REFS_PRUNE
;
1074 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1075 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1077 if (match_push_refs(local_refs
, &remote_refs
,
1078 refspec_nr
, refspec
, match_flags
)) {
1082 if (transport
->smart_options
&&
1083 transport
->smart_options
->cas
&&
1084 !is_empty_cas(transport
->smart_options
->cas
))
1085 apply_push_cas(transport
->smart_options
->cas
,
1086 transport
->remote
, remote_refs
);
1088 set_ref_status_for_push(remote_refs
,
1089 flags
& TRANSPORT_PUSH_MIRROR
,
1090 flags
& TRANSPORT_PUSH_FORCE
);
1092 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1093 if (run_pre_push_hook(transport
, remote_refs
))
1096 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1097 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1098 !is_bare_repository()) {
1099 struct ref
*ref
= remote_refs
;
1100 struct oid_array commits
= OID_ARRAY_INIT
;
1102 for (; ref
; ref
= ref
->next
)
1103 if (!is_null_oid(&ref
->new_oid
))
1104 oid_array_append(&commits
,
1107 if (!push_unpushed_submodules(&commits
,
1109 refspec
, refspec_nr
,
1110 transport
->push_options
,
1112 oid_array_clear(&commits
);
1113 die("Failed to push all needed submodules!");
1115 oid_array_clear(&commits
);
1118 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1119 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1120 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1121 !pretend
)) && !is_bare_repository()) {
1122 struct ref
*ref
= remote_refs
;
1123 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1124 struct oid_array commits
= OID_ARRAY_INIT
;
1126 for (; ref
; ref
= ref
->next
)
1127 if (!is_null_oid(&ref
->new_oid
))
1128 oid_array_append(&commits
,
1131 if (find_unpushed_submodules(&commits
, transport
->remote
->name
,
1133 oid_array_clear(&commits
);
1134 die_with_unpushed_submodules(&needs_pushing
);
1136 string_list_clear(&needs_pushing
, 0);
1137 oid_array_clear(&commits
);
1140 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
))
1141 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1144 err
= push_had_errors(remote_refs
);
1145 ret
= push_ret
| err
;
1148 transport_print_push_status(transport
->url
, remote_refs
,
1149 verbose
| porcelain
, porcelain
,
1152 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1153 set_upstreams(transport
, remote_refs
, pretend
);
1155 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1156 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1158 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1159 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1162 if (porcelain
&& !push_ret
)
1164 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1165 fprintf(stderr
, "Everything up-to-date\n");
1172 const struct ref
*transport_get_remote_refs(struct transport
*transport
,
1173 const struct argv_array
*ref_prefixes
)
1175 if (!transport
->got_remote_refs
) {
1176 transport
->remote_refs
=
1177 transport
->vtable
->get_refs_list(transport
, 0,
1179 transport
->got_remote_refs
= 1;
1182 return transport
->remote_refs
;
1185 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1188 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1189 struct ref
**heads
= NULL
;
1192 for (rm
= refs
; rm
; rm
= rm
->next
) {
1195 !is_null_oid(&rm
->old_oid
) &&
1196 !oidcmp(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1198 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1199 heads
[nr_heads
++] = rm
;
1204 * When deepening of a shallow repository is requested,
1205 * then local and remote refs are likely to still be equal.
1206 * Just feed them all to the fetch method in that case.
1207 * This condition shouldn't be met in a non-deepening fetch
1208 * (see builtin/fetch.c:quickfetch()).
1210 ALLOC_ARRAY(heads
, nr_refs
);
1211 for (rm
= refs
; rm
; rm
= rm
->next
)
1212 heads
[nr_heads
++] = rm
;
1215 rc
= transport
->vtable
->fetch(transport
, nr_heads
, heads
);
1221 void transport_unlock_pack(struct transport
*transport
)
1223 if (transport
->pack_lockfile
) {
1224 unlink_or_warn(transport
->pack_lockfile
);
1225 FREE_AND_NULL(transport
->pack_lockfile
);
1229 int transport_connect(struct transport
*transport
, const char *name
,
1230 const char *exec
, int fd
[2])
1232 if (transport
->vtable
->connect
)
1233 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1235 die("Operation not supported by protocol");
1238 int transport_disconnect(struct transport
*transport
)
1241 if (transport
->vtable
->disconnect
)
1242 ret
= transport
->vtable
->disconnect(transport
);
1248 * Strip username (and password) from a URL and return
1249 * it in a newly allocated string.
1251 char *transport_anonymize_url(const char *url
)
1253 char *scheme_prefix
, *anon_part
;
1254 size_t anon_len
, prefix_len
= 0;
1256 anon_part
= strchr(url
, '@');
1257 if (url_is_local_not_ssh(url
) || !anon_part
)
1260 anon_len
= strlen(++anon_part
);
1261 scheme_prefix
= strstr(url
, "://");
1262 if (!scheme_prefix
) {
1263 if (!strchr(anon_part
, ':'))
1264 /* cannot be "me@there:/path/name" */
1268 /* make sure scheme is reasonable */
1269 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1272 case '+': case '.': case '-':
1281 /* @ past the first slash does not count */
1282 cp
= strchr(scheme_prefix
+ 3, '/');
1283 if (cp
&& cp
< anon_part
)
1285 prefix_len
= scheme_prefix
- url
+ 3;
1287 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1288 (int)anon_len
, anon_part
);
1290 return xstrdup(url
);
1293 static void read_alternate_refs(const char *path
,
1294 alternate_ref_fn
*cb
,
1297 struct child_process cmd
= CHILD_PROCESS_INIT
;
1298 struct strbuf line
= STRBUF_INIT
;
1302 argv_array_pushf(&cmd
.args
, "--git-dir=%s", path
);
1303 argv_array_push(&cmd
.args
, "for-each-ref");
1304 argv_array_push(&cmd
.args
, "--format=%(objectname) %(refname)");
1305 cmd
.env
= local_repo_env
;
1308 if (start_command(&cmd
))
1311 fh
= xfdopen(cmd
.out
, "r");
1312 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
1313 struct object_id oid
;
1315 if (get_oid_hex(line
.buf
, &oid
) ||
1316 line
.buf
[GIT_SHA1_HEXSZ
] != ' ') {
1317 warning("invalid line while parsing alternate refs: %s",
1322 cb(line
.buf
+ GIT_SHA1_HEXSZ
+ 1, &oid
, data
);
1326 finish_command(&cmd
);
1329 struct alternate_refs_data
{
1330 alternate_ref_fn
*fn
;
1334 static int refs_from_alternate_cb(struct alternate_object_database
*e
,
1337 struct strbuf path
= STRBUF_INIT
;
1339 struct alternate_refs_data
*cb
= data
;
1341 if (!strbuf_realpath(&path
, e
->path
, 0))
1343 if (!strbuf_strip_suffix(&path
, "/objects"))
1345 base_len
= path
.len
;
1347 /* Is this a git repository with refs? */
1348 strbuf_addstr(&path
, "/refs");
1349 if (!is_directory(path
.buf
))
1351 strbuf_setlen(&path
, base_len
);
1353 read_alternate_refs(path
.buf
, cb
->fn
, cb
->data
);
1356 strbuf_release(&path
);
1360 void for_each_alternate_ref(alternate_ref_fn fn
, void *data
)
1362 struct alternate_refs_data cb
;
1365 foreach_alt_odb(refs_from_alternate_cb
, &cb
);