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"
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
, int for_push
)
77 struct bundle_transport_data
*data
= transport
->data
;
78 struct ref
*result
= NULL
;
86 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
88 die ("Could not read bundle '%s'.", transport
->url
);
89 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
90 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
91 struct ref
*ref
= alloc_ref(e
->name
);
92 oidcpy(&ref
->old_oid
, &e
->oid
);
99 static int fetch_refs_from_bundle(struct transport
*transport
,
100 int nr_heads
, struct ref
**to_fetch
)
102 struct bundle_transport_data
*data
= transport
->data
;
103 return unbundle(&data
->header
, data
->fd
,
104 transport
->progress
? BUNDLE_VERBOSE
: 0);
107 static int close_bundle(struct transport
*transport
)
109 struct bundle_transport_data
*data
= transport
->data
;
116 struct git_transport_data
{
117 struct git_transport_options options
;
118 struct child_process
*conn
;
120 unsigned got_remote_heads
: 1;
121 struct oid_array extra_have
;
122 struct oid_array shallow
;
125 static int set_git_option(struct git_transport_options
*opts
,
126 const char *name
, const char *value
)
128 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
129 opts
->uploadpack
= value
;
131 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
132 opts
->receivepack
= value
;
134 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
135 opts
->thin
= !!value
;
137 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
138 opts
->followtags
= !!value
;
140 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
141 opts
->keep
= !!value
;
143 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
144 opts
->update_shallow
= !!value
;
146 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
151 opts
->depth
= strtol(value
, &end
, 0);
153 die(_("transport: invalid depth option '%s'"), value
);
156 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
157 opts
->deepen_since
= value
;
159 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
160 opts
->deepen_not
= (const struct string_list
*)value
;
162 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
163 opts
->deepen_relative
= !!value
;
165 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
166 opts
->from_promisor
= !!value
;
168 } else if (!strcmp(name
, TRANS_OPT_NO_DEPENDENTS
)) {
169 opts
->no_dependents
= !!value
;
171 } else if (!strcmp(name
, TRANS_OPT_LIST_OBJECTS_FILTER
)) {
172 parse_list_objects_filter(&opts
->filter_options
, value
);
178 static int connect_setup(struct transport
*transport
, int for_push
)
180 struct git_transport_data
*data
= transport
->data
;
181 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
186 switch (transport
->family
) {
187 case TRANSPORT_FAMILY_ALL
: break;
188 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
189 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
192 data
->conn
= git_connect(data
->fd
, transport
->url
,
193 for_push
? data
->options
.receivepack
:
194 data
->options
.uploadpack
,
200 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
)
202 struct git_transport_data
*data
= transport
->data
;
205 connect_setup(transport
, for_push
);
206 get_remote_heads(data
->fd
[0], NULL
, 0, &refs
,
207 for_push
? REF_NORMAL
: 0,
210 data
->got_remote_heads
= 1;
215 static int fetch_refs_via_pack(struct transport
*transport
,
216 int nr_heads
, struct ref
**to_fetch
)
219 struct git_transport_data
*data
= transport
->data
;
221 char *dest
= xstrdup(transport
->url
);
222 struct fetch_pack_args args
;
223 struct ref
*refs_tmp
= NULL
;
225 memset(&args
, 0, sizeof(args
));
226 args
.uploadpack
= data
->options
.uploadpack
;
227 args
.keep_pack
= data
->options
.keep
;
229 args
.use_thin_pack
= data
->options
.thin
;
230 args
.include_tag
= data
->options
.followtags
;
231 args
.verbose
= (transport
->verbose
> 1);
232 args
.quiet
= (transport
->verbose
< 0);
233 args
.no_progress
= !transport
->progress
;
234 args
.depth
= data
->options
.depth
;
235 args
.deepen_since
= data
->options
.deepen_since
;
236 args
.deepen_not
= data
->options
.deepen_not
;
237 args
.deepen_relative
= data
->options
.deepen_relative
;
238 args
.check_self_contained_and_connected
=
239 data
->options
.check_self_contained_and_connected
;
240 args
.cloning
= transport
->cloning
;
241 args
.update_shallow
= data
->options
.update_shallow
;
242 args
.from_promisor
= data
->options
.from_promisor
;
243 args
.no_dependents
= data
->options
.no_dependents
;
244 args
.filter_options
= data
->options
.filter_options
;
246 if (!data
->got_remote_heads
) {
247 connect_setup(transport
, 0);
248 get_remote_heads(data
->fd
[0], NULL
, 0, &refs_tmp
, 0,
249 NULL
, &data
->shallow
);
250 data
->got_remote_heads
= 1;
253 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
254 refs_tmp
? refs_tmp
: transport
->remote_refs
,
255 dest
, to_fetch
, nr_heads
, &data
->shallow
,
256 &transport
->pack_lockfile
);
259 if (finish_connect(data
->conn
))
262 data
->got_remote_heads
= 0;
263 data
->options
.self_contained_and_connected
=
264 args
.self_contained_and_connected
;
268 if (report_unmatched_refs(to_fetch
, nr_heads
))
277 static int push_had_errors(struct ref
*ref
)
279 for (; ref
; ref
= ref
->next
) {
280 switch (ref
->status
) {
281 case REF_STATUS_NONE
:
282 case REF_STATUS_UPTODATE
:
292 int transport_refs_pushed(struct ref
*ref
)
294 for (; ref
; ref
= ref
->next
) {
295 switch(ref
->status
) {
296 case REF_STATUS_NONE
:
297 case REF_STATUS_UPTODATE
:
306 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
310 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
316 if (!remote_find_tracking(remote
, &rs
)) {
318 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
320 delete_ref(NULL
, rs
.dst
, NULL
, 0);
322 update_ref("update by push", rs
.dst
, &ref
->new_oid
,
328 static void print_ref_status(char flag
, const char *summary
,
329 struct ref
*to
, struct ref
*from
, const char *msg
,
330 int porcelain
, int summary_width
)
334 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
336 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
338 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
340 fprintf(stdout
, "%s\n", summary
);
342 fprintf(stderr
, " %c %-*s ", flag
, summary_width
, summary
);
344 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
346 fputs(prettify_refname(to
->name
), stderr
);
356 static void print_ok_ref_status(struct ref
*ref
, int porcelain
, int summary_width
)
359 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
360 porcelain
, summary_width
);
361 else if (is_null_oid(&ref
->old_oid
))
362 print_ref_status('*',
363 (starts_with(ref
->name
, "refs/tags/") ? "[new tag]" :
365 ref
, ref
->peer_ref
, NULL
, porcelain
, summary_width
);
367 struct strbuf quickref
= STRBUF_INIT
;
371 strbuf_add_unique_abbrev(&quickref
, &ref
->old_oid
,
373 if (ref
->forced_update
) {
374 strbuf_addstr(&quickref
, "...");
376 msg
= "forced update";
378 strbuf_addstr(&quickref
, "..");
382 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
,
385 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
386 porcelain
, summary_width
);
387 strbuf_release(&quickref
);
391 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
392 int porcelain
, int summary_width
)
395 char *url
= transport_anonymize_url(dest
);
396 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
400 switch(ref
->status
) {
401 case REF_STATUS_NONE
:
402 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
403 porcelain
, summary_width
);
405 case REF_STATUS_REJECT_NODELETE
:
406 print_ref_status('!', "[rejected]", ref
, NULL
,
407 "remote does not support deleting refs",
408 porcelain
, summary_width
);
410 case REF_STATUS_UPTODATE
:
411 print_ref_status('=', "[up to date]", ref
,
412 ref
->peer_ref
, NULL
, porcelain
, summary_width
);
414 case REF_STATUS_REJECT_NONFASTFORWARD
:
415 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
416 "non-fast-forward", porcelain
, summary_width
);
418 case REF_STATUS_REJECT_ALREADY_EXISTS
:
419 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
420 "already exists", porcelain
, summary_width
);
422 case REF_STATUS_REJECT_FETCH_FIRST
:
423 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
424 "fetch first", porcelain
, summary_width
);
426 case REF_STATUS_REJECT_NEEDS_FORCE
:
427 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
428 "needs force", porcelain
, summary_width
);
430 case REF_STATUS_REJECT_STALE
:
431 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
432 "stale info", porcelain
, summary_width
);
434 case REF_STATUS_REJECT_SHALLOW
:
435 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
436 "new shallow roots not allowed",
437 porcelain
, summary_width
);
439 case REF_STATUS_REMOTE_REJECT
:
440 print_ref_status('!', "[remote rejected]", ref
,
441 ref
->deletion
? NULL
: ref
->peer_ref
,
442 ref
->remote_status
, porcelain
, summary_width
);
444 case REF_STATUS_EXPECTING_REPORT
:
445 print_ref_status('!', "[remote failure]", ref
,
446 ref
->deletion
? NULL
: ref
->peer_ref
,
447 "remote failed to report status",
448 porcelain
, summary_width
);
450 case REF_STATUS_ATOMIC_PUSH_FAILED
:
451 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
452 "atomic push failed", porcelain
, summary_width
);
455 print_ok_ref_status(ref
, porcelain
, summary_width
);
462 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
464 char hex
[GIT_MAX_HEXSZ
+ 1];
465 int w
= find_unique_abbrev_r(hex
, oid
, DEFAULT_ABBREV
);
467 return (w
< sofar
) ? sofar
: w
;
470 int transport_summary_width(const struct ref
*refs
)
474 for (; refs
; refs
= refs
->next
) {
475 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
476 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
479 maxw
= FALLBACK_DEFAULT_ABBREV
;
480 return (2 * maxw
+ 3);
483 void transport_print_push_status(const char *dest
, struct ref
*refs
,
484 int verbose
, int porcelain
, unsigned int *reject_reasons
)
489 int summary_width
= transport_summary_width(refs
);
491 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
494 for (ref
= refs
; ref
; ref
= ref
->next
)
495 if (ref
->status
== REF_STATUS_UPTODATE
)
496 n
+= print_one_push_status(ref
, dest
, n
,
497 porcelain
, summary_width
);
500 for (ref
= refs
; ref
; ref
= ref
->next
)
501 if (ref
->status
== REF_STATUS_OK
)
502 n
+= print_one_push_status(ref
, dest
, n
,
503 porcelain
, summary_width
);
506 for (ref
= refs
; ref
; ref
= ref
->next
) {
507 if (ref
->status
!= REF_STATUS_NONE
&&
508 ref
->status
!= REF_STATUS_UPTODATE
&&
509 ref
->status
!= REF_STATUS_OK
)
510 n
+= print_one_push_status(ref
, dest
, n
,
511 porcelain
, summary_width
);
512 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
513 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
514 *reject_reasons
|= REJECT_NON_FF_HEAD
;
516 *reject_reasons
|= REJECT_NON_FF_OTHER
;
517 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
518 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
519 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
520 *reject_reasons
|= REJECT_FETCH_FIRST
;
521 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
522 *reject_reasons
|= REJECT_NEEDS_FORCE
;
528 void transport_verify_remote_names(int nr_heads
, const char **heads
)
532 for (i
= 0; i
< nr_heads
; i
++) {
533 const char *local
= heads
[i
];
534 const char *remote
= strrchr(heads
[i
], ':');
539 /* A matching refspec is okay. */
540 if (remote
== local
&& remote
[1] == '\0')
543 remote
= remote
? (remote
+ 1) : local
;
544 if (check_refname_format(remote
,
545 REFNAME_ALLOW_ONELEVEL
|REFNAME_REFSPEC_PATTERN
))
546 die("remote part of refspec is not a valid name in %s",
551 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
553 struct git_transport_data
*data
= transport
->data
;
554 struct send_pack_args args
;
557 if (!data
->got_remote_heads
) {
558 struct ref
*tmp_refs
;
559 connect_setup(transport
, 1);
561 get_remote_heads(data
->fd
[0], NULL
, 0, &tmp_refs
, REF_NORMAL
,
562 NULL
, &data
->shallow
);
563 data
->got_remote_heads
= 1;
566 memset(&args
, 0, sizeof(args
));
567 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
568 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
569 args
.use_thin_pack
= data
->options
.thin
;
570 args
.verbose
= (transport
->verbose
> 0);
571 args
.quiet
= (transport
->verbose
< 0);
572 args
.progress
= transport
->progress
;
573 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
574 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
575 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
576 args
.push_options
= transport
->push_options
;
577 args
.url
= transport
->url
;
579 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
580 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
581 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
582 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
584 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
586 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
591 ret
|= finish_connect(data
->conn
);
593 data
->got_remote_heads
= 0;
598 static int connect_git(struct transport
*transport
, const char *name
,
599 const char *executable
, int fd
[2])
601 struct git_transport_data
*data
= transport
->data
;
602 data
->conn
= git_connect(data
->fd
, transport
->url
,
609 static int disconnect_git(struct transport
*transport
)
611 struct git_transport_data
*data
= transport
->data
;
613 if (data
->got_remote_heads
)
614 packet_flush(data
->fd
[1]);
617 finish_connect(data
->conn
);
624 static struct transport_vtable taken_over_vtable
= {
626 get_refs_via_connect
,
633 void transport_take_over(struct transport
*transport
,
634 struct child_process
*child
)
636 struct git_transport_data
*data
;
638 if (!transport
->smart_options
)
639 die("BUG: taking over transport requires non-NULL "
640 "smart_options field.");
642 data
= xcalloc(1, sizeof(*data
));
643 data
->options
= *transport
->smart_options
;
645 data
->fd
[0] = data
->conn
->out
;
646 data
->fd
[1] = data
->conn
->in
;
647 data
->got_remote_heads
= 0;
648 transport
->data
= data
;
650 transport
->vtable
= &taken_over_vtable
;
651 transport
->smart_options
= &(data
->options
);
653 transport
->cannot_reuse
= 1;
656 static int is_file(const char *url
)
661 return S_ISREG(buf
.st_mode
);
664 static int external_specification_len(const char *url
)
666 return strchr(url
, ':') - url
;
669 static const struct string_list
*protocol_whitelist(void)
671 static int enabled
= -1;
672 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
675 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
677 string_list_split(&allowed
, v
, ':', -1);
678 string_list_sort(&allowed
);
685 return enabled
? &allowed
: NULL
;
688 enum protocol_allow_config
{
689 PROTOCOL_ALLOW_NEVER
= 0,
690 PROTOCOL_ALLOW_USER_ONLY
,
691 PROTOCOL_ALLOW_ALWAYS
694 static enum protocol_allow_config
parse_protocol_config(const char *key
,
697 if (!strcasecmp(value
, "always"))
698 return PROTOCOL_ALLOW_ALWAYS
;
699 else if (!strcasecmp(value
, "never"))
700 return PROTOCOL_ALLOW_NEVER
;
701 else if (!strcasecmp(value
, "user"))
702 return PROTOCOL_ALLOW_USER_ONLY
;
704 die("unknown value for config '%s': %s", key
, value
);
707 static enum protocol_allow_config
get_protocol_config(const char *type
)
709 char *key
= xstrfmt("protocol.%s.allow", type
);
712 /* first check the per-protocol config */
713 if (!git_config_get_string(key
, &value
)) {
714 enum protocol_allow_config ret
=
715 parse_protocol_config(key
, value
);
722 /* if defined, fallback to user-defined default for unknown protocols */
723 if (!git_config_get_string("protocol.allow", &value
)) {
724 enum protocol_allow_config ret
=
725 parse_protocol_config("protocol.allow", value
);
730 /* fallback to built-in defaults */
732 if (!strcmp(type
, "http") ||
733 !strcmp(type
, "https") ||
734 !strcmp(type
, "git") ||
735 !strcmp(type
, "ssh") ||
736 !strcmp(type
, "file"))
737 return PROTOCOL_ALLOW_ALWAYS
;
739 /* known scary; err on the side of caution */
740 if (!strcmp(type
, "ext"))
741 return PROTOCOL_ALLOW_NEVER
;
743 /* unknown; by default let them be used only directly by the user */
744 return PROTOCOL_ALLOW_USER_ONLY
;
747 int is_transport_allowed(const char *type
, int from_user
)
749 const struct string_list
*whitelist
= protocol_whitelist();
751 return string_list_has_string(whitelist
, type
);
753 switch (get_protocol_config(type
)) {
754 case PROTOCOL_ALLOW_ALWAYS
:
756 case PROTOCOL_ALLOW_NEVER
:
758 case PROTOCOL_ALLOW_USER_ONLY
:
760 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
764 die("BUG: invalid protocol_allow_config type");
767 void transport_check_allowed(const char *type
)
769 if (!is_transport_allowed(type
, -1))
770 die("transport '%s' not allowed", type
);
773 static struct transport_vtable bundle_vtable
= {
775 get_refs_from_bundle
,
776 fetch_refs_from_bundle
,
782 static struct transport_vtable builtin_smart_vtable
= {
784 get_refs_via_connect
,
791 struct transport
*transport_get(struct remote
*remote
, const char *url
)
794 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
796 ret
->progress
= isatty(2);
799 die("No remote provided to transport_get()");
801 ret
->got_remote_refs
= 0;
802 ret
->remote
= remote
;
803 helper
= remote
->foreign_vcs
;
805 if (!url
&& remote
->url
)
806 url
= remote
->url
[0];
809 /* maybe it is a foreign URL? */
813 while (is_urlschemechar(p
== url
, *p
))
815 if (starts_with(p
, "::"))
816 helper
= xstrndup(url
, p
- url
);
820 transport_helper_init(ret
, helper
);
821 } else if (starts_with(url
, "rsync:")) {
822 die("git-over-rsync is no longer supported");
823 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
824 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
825 transport_check_allowed("file");
827 ret
->vtable
= &bundle_vtable
;
828 ret
->smart_options
= NULL
;
829 } else if (!is_url(url
)
830 || starts_with(url
, "file://")
831 || starts_with(url
, "git://")
832 || starts_with(url
, "ssh://")
833 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
834 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
837 * These are builtin smart transports; "allowed" transports
838 * will be checked individually in git_connect.
840 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
842 ret
->vtable
= &builtin_smart_vtable
;
843 ret
->smart_options
= &(data
->options
);
846 data
->got_remote_heads
= 0;
848 /* Unknown protocol in URL. Pass to external handler. */
849 int len
= external_specification_len(url
);
850 char *handler
= xmemdupz(url
, len
);
851 transport_helper_init(ret
, handler
);
854 if (ret
->smart_options
) {
855 ret
->smart_options
->thin
= 1;
856 ret
->smart_options
->uploadpack
= "git-upload-pack";
857 if (remote
->uploadpack
)
858 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
859 ret
->smart_options
->receivepack
= "git-receive-pack";
860 if (remote
->receivepack
)
861 ret
->smart_options
->receivepack
= remote
->receivepack
;
867 int transport_set_option(struct transport
*transport
,
868 const char *name
, const char *value
)
870 int git_reports
= 1, protocol_reports
= 1;
872 if (transport
->smart_options
)
873 git_reports
= set_git_option(transport
->smart_options
,
876 if (transport
->vtable
->set_option
)
877 protocol_reports
= transport
->vtable
->set_option(transport
,
880 /* If either report is 0, report 0 (success). */
881 if (!git_reports
|| !protocol_reports
)
883 /* If either reports -1 (invalid value), report -1. */
884 if ((git_reports
== -1) || (protocol_reports
== -1))
886 /* Otherwise if both report unknown, report unknown. */
890 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
894 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
896 transport
->verbose
= -1;
899 * Rules used to determine whether to report progress (processing aborts
900 * when a rule is satisfied):
902 * . Report progress, if force_progress is 1 (ie. --progress).
903 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
904 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
905 * . Report progress if isatty(2) is 1.
907 if (force_progress
>= 0)
908 transport
->progress
= !!force_progress
;
910 transport
->progress
= verbosity
>= 0 && isatty(2);
913 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
917 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
918 "not be found on any remote:\n"));
919 for (i
= 0; i
< needs_pushing
->nr
; i
++)
920 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
921 fprintf(stderr
, _("\nPlease try\n\n"
922 " git push --recurse-submodules=on-demand\n\n"
923 "or cd to the path and use\n\n"
925 "to push them to a remote.\n\n"));
927 string_list_clear(needs_pushing
, 0);
932 static int run_pre_push_hook(struct transport
*transport
,
933 struct ref
*remote_refs
)
937 struct child_process proc
= CHILD_PROCESS_INIT
;
941 if (!(argv
[0] = find_hook("pre-push")))
944 argv
[1] = transport
->remote
->name
;
945 argv
[2] = transport
->url
;
951 if (start_command(&proc
)) {
952 finish_command(&proc
);
956 sigchain_push(SIGPIPE
, SIG_IGN
);
958 strbuf_init(&buf
, 256);
960 for (r
= remote_refs
; r
; r
= r
->next
) {
961 if (!r
->peer_ref
) continue;
962 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
963 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
964 if (r
->status
== REF_STATUS_UPTODATE
) continue;
967 strbuf_addf( &buf
, "%s %s %s %s\n",
968 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
969 r
->name
, oid_to_hex(&r
->old_oid
));
971 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
972 /* We do not mind if a hook does not read all refs. */
979 strbuf_release(&buf
);
985 sigchain_pop(SIGPIPE
);
987 x
= finish_command(&proc
);
994 int transport_push(struct transport
*transport
,
995 int refspec_nr
, const char **refspec
, int flags
,
996 unsigned int *reject_reasons
)
999 transport_verify_remote_names(refspec_nr
, refspec
);
1001 if (transport
->vtable
->push_refs
) {
1002 struct ref
*remote_refs
;
1003 struct ref
*local_refs
= get_local_heads();
1004 int match_flags
= MATCH_REFS_NONE
;
1005 int verbose
= (transport
->verbose
> 0);
1006 int quiet
= (transport
->verbose
< 0);
1007 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1008 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1009 int push_ret
, ret
, err
;
1011 if (check_push_refs(local_refs
, refspec_nr
, refspec
) < 0)
1014 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1);
1016 if (flags
& TRANSPORT_PUSH_ALL
)
1017 match_flags
|= MATCH_REFS_ALL
;
1018 if (flags
& TRANSPORT_PUSH_MIRROR
)
1019 match_flags
|= MATCH_REFS_MIRROR
;
1020 if (flags
& TRANSPORT_PUSH_PRUNE
)
1021 match_flags
|= MATCH_REFS_PRUNE
;
1022 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1023 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1025 if (match_push_refs(local_refs
, &remote_refs
,
1026 refspec_nr
, refspec
, match_flags
)) {
1030 if (transport
->smart_options
&&
1031 transport
->smart_options
->cas
&&
1032 !is_empty_cas(transport
->smart_options
->cas
))
1033 apply_push_cas(transport
->smart_options
->cas
,
1034 transport
->remote
, remote_refs
);
1036 set_ref_status_for_push(remote_refs
,
1037 flags
& TRANSPORT_PUSH_MIRROR
,
1038 flags
& TRANSPORT_PUSH_FORCE
);
1040 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1041 if (run_pre_push_hook(transport
, remote_refs
))
1044 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1045 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1046 !is_bare_repository()) {
1047 struct ref
*ref
= remote_refs
;
1048 struct oid_array commits
= OID_ARRAY_INIT
;
1050 for (; ref
; ref
= ref
->next
)
1051 if (!is_null_oid(&ref
->new_oid
))
1052 oid_array_append(&commits
,
1055 if (!push_unpushed_submodules(&commits
,
1057 refspec
, refspec_nr
,
1058 transport
->push_options
,
1060 oid_array_clear(&commits
);
1061 die("Failed to push all needed submodules!");
1063 oid_array_clear(&commits
);
1066 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1067 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1068 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1069 !pretend
)) && !is_bare_repository()) {
1070 struct ref
*ref
= remote_refs
;
1071 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1072 struct oid_array commits
= OID_ARRAY_INIT
;
1074 for (; ref
; ref
= ref
->next
)
1075 if (!is_null_oid(&ref
->new_oid
))
1076 oid_array_append(&commits
,
1079 if (find_unpushed_submodules(&commits
, transport
->remote
->name
,
1081 oid_array_clear(&commits
);
1082 die_with_unpushed_submodules(&needs_pushing
);
1084 string_list_clear(&needs_pushing
, 0);
1085 oid_array_clear(&commits
);
1088 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
))
1089 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1092 err
= push_had_errors(remote_refs
);
1093 ret
= push_ret
| err
;
1096 transport_print_push_status(transport
->url
, remote_refs
,
1097 verbose
| porcelain
, porcelain
,
1100 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1101 set_upstreams(transport
, remote_refs
, pretend
);
1103 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1104 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1106 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1107 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1110 if (porcelain
&& !push_ret
)
1112 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1113 fprintf(stderr
, "Everything up-to-date\n");
1120 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
1122 if (!transport
->got_remote_refs
) {
1123 transport
->remote_refs
= transport
->vtable
->get_refs_list(transport
, 0);
1124 transport
->got_remote_refs
= 1;
1127 return transport
->remote_refs
;
1130 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1133 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1134 struct ref
**heads
= NULL
;
1137 for (rm
= refs
; rm
; rm
= rm
->next
) {
1140 !is_null_oid(&rm
->old_oid
) &&
1141 !oidcmp(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1143 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1144 heads
[nr_heads
++] = rm
;
1149 * When deepening of a shallow repository is requested,
1150 * then local and remote refs are likely to still be equal.
1151 * Just feed them all to the fetch method in that case.
1152 * This condition shouldn't be met in a non-deepening fetch
1153 * (see builtin/fetch.c:quickfetch()).
1155 ALLOC_ARRAY(heads
, nr_refs
);
1156 for (rm
= refs
; rm
; rm
= rm
->next
)
1157 heads
[nr_heads
++] = rm
;
1160 rc
= transport
->vtable
->fetch(transport
, nr_heads
, heads
);
1166 void transport_unlock_pack(struct transport
*transport
)
1168 if (transport
->pack_lockfile
) {
1169 unlink_or_warn(transport
->pack_lockfile
);
1170 FREE_AND_NULL(transport
->pack_lockfile
);
1174 int transport_connect(struct transport
*transport
, const char *name
,
1175 const char *exec
, int fd
[2])
1177 if (transport
->vtable
->connect
)
1178 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1180 die("Operation not supported by protocol");
1183 int transport_disconnect(struct transport
*transport
)
1186 if (transport
->vtable
->disconnect
)
1187 ret
= transport
->vtable
->disconnect(transport
);
1193 * Strip username (and password) from a URL and return
1194 * it in a newly allocated string.
1196 char *transport_anonymize_url(const char *url
)
1198 char *scheme_prefix
, *anon_part
;
1199 size_t anon_len
, prefix_len
= 0;
1201 anon_part
= strchr(url
, '@');
1202 if (url_is_local_not_ssh(url
) || !anon_part
)
1205 anon_len
= strlen(++anon_part
);
1206 scheme_prefix
= strstr(url
, "://");
1207 if (!scheme_prefix
) {
1208 if (!strchr(anon_part
, ':'))
1209 /* cannot be "me@there:/path/name" */
1213 /* make sure scheme is reasonable */
1214 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1217 case '+': case '.': case '-':
1226 /* @ past the first slash does not count */
1227 cp
= strchr(scheme_prefix
+ 3, '/');
1228 if (cp
&& cp
< anon_part
)
1230 prefix_len
= scheme_prefix
- url
+ 3;
1232 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1233 (int)anon_len
, anon_part
);
1235 return xstrdup(url
);
1238 static void read_alternate_refs(const char *path
,
1239 alternate_ref_fn
*cb
,
1242 struct child_process cmd
= CHILD_PROCESS_INIT
;
1243 struct strbuf line
= STRBUF_INIT
;
1247 argv_array_pushf(&cmd
.args
, "--git-dir=%s", path
);
1248 argv_array_push(&cmd
.args
, "for-each-ref");
1249 argv_array_push(&cmd
.args
, "--format=%(objectname) %(refname)");
1250 cmd
.env
= local_repo_env
;
1253 if (start_command(&cmd
))
1256 fh
= xfdopen(cmd
.out
, "r");
1257 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
1258 struct object_id oid
;
1260 if (get_oid_hex(line
.buf
, &oid
) ||
1261 line
.buf
[GIT_SHA1_HEXSZ
] != ' ') {
1262 warning("invalid line while parsing alternate refs: %s",
1267 cb(line
.buf
+ GIT_SHA1_HEXSZ
+ 1, &oid
, data
);
1271 finish_command(&cmd
);
1274 struct alternate_refs_data
{
1275 alternate_ref_fn
*fn
;
1279 static int refs_from_alternate_cb(struct alternate_object_database
*e
,
1282 struct strbuf path
= STRBUF_INIT
;
1284 struct alternate_refs_data
*cb
= data
;
1286 if (!strbuf_realpath(&path
, e
->path
, 0))
1288 if (!strbuf_strip_suffix(&path
, "/objects"))
1290 base_len
= path
.len
;
1292 /* Is this a git repository with refs? */
1293 strbuf_addstr(&path
, "/refs");
1294 if (!is_directory(path
.buf
))
1296 strbuf_setlen(&path
, base_len
);
1298 read_alternate_refs(path
.buf
, cb
->fn
, cb
->data
);
1301 strbuf_release(&path
);
1305 void for_each_alternate_ref(alternate_ref_fn fn
, void *data
)
1307 struct alternate_refs_data cb
;
1310 foreach_alt_odb(refs_from_alternate_cb
, &cb
);