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"
22 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
26 for (ref
= refs
; ref
; ref
= ref
->next
) {
27 const char *localname
;
29 const char *remotename
;
32 * Check suitability for tracking. Must be successful /
33 * already up-to-date ref create/modify (not delete).
35 if (ref
->status
!= REF_STATUS_OK
&&
36 ref
->status
!= REF_STATUS_UPTODATE
)
40 if (is_null_oid(&ref
->new_oid
))
43 /* Follow symbolic refs (mainly for HEAD). */
44 localname
= ref
->peer_ref
->name
;
45 remotename
= ref
->name
;
46 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
48 if (tmp
&& flag
& REF_ISSYMREF
&&
49 starts_with(tmp
, "refs/heads/"))
52 /* Both source and destination must be local branches. */
53 if (!localname
|| !starts_with(localname
, "refs/heads/"))
55 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
59 install_branch_config(BRANCH_CONFIG_VERBOSE
,
60 localname
+ 11, transport
->remote
->name
,
63 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
64 localname
+ 11, remotename
+ 11,
65 transport
->remote
->name
);
69 struct bundle_transport_data
{
71 struct bundle_header header
;
74 static struct ref
*get_refs_from_bundle(struct transport
*transport
, int for_push
)
76 struct bundle_transport_data
*data
= transport
->data
;
77 struct ref
*result
= NULL
;
85 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
87 die ("Could not read bundle '%s'.", transport
->url
);
88 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
89 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
90 struct ref
*ref
= alloc_ref(e
->name
);
91 oidcpy(&ref
->old_oid
, &e
->oid
);
98 static int fetch_refs_from_bundle(struct transport
*transport
,
99 int nr_heads
, struct ref
**to_fetch
)
101 struct bundle_transport_data
*data
= transport
->data
;
102 return unbundle(&data
->header
, data
->fd
,
103 transport
->progress
? BUNDLE_VERBOSE
: 0);
106 static int close_bundle(struct transport
*transport
)
108 struct bundle_transport_data
*data
= transport
->data
;
115 struct git_transport_data
{
116 struct git_transport_options options
;
117 struct child_process
*conn
;
119 unsigned got_remote_heads
: 1;
120 struct oid_array extra_have
;
121 struct oid_array shallow
;
124 static int set_git_option(struct git_transport_options
*opts
,
125 const char *name
, const char *value
)
127 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
128 opts
->uploadpack
= value
;
130 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
131 opts
->receivepack
= value
;
133 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
134 opts
->thin
= !!value
;
136 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
137 opts
->followtags
= !!value
;
139 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
140 opts
->keep
= !!value
;
142 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
143 opts
->update_shallow
= !!value
;
145 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
150 opts
->depth
= strtol(value
, &end
, 0);
152 die(_("transport: invalid depth option '%s'"), value
);
155 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
156 opts
->deepen_since
= value
;
158 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
159 opts
->deepen_not
= (const struct string_list
*)value
;
161 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
162 opts
->deepen_relative
= !!value
;
164 } else if (!strcmp(name
, TRANS_OPT_FROM_PROMISOR
)) {
165 opts
->from_promisor
= !!value
;
167 } else if (!strcmp(name
, TRANS_OPT_NO_DEPENDENTS
)) {
168 opts
->no_dependents
= !!value
;
174 static int connect_setup(struct transport
*transport
, int for_push
)
176 struct git_transport_data
*data
= transport
->data
;
177 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
182 switch (transport
->family
) {
183 case TRANSPORT_FAMILY_ALL
: break;
184 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
185 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
188 data
->conn
= git_connect(data
->fd
, transport
->url
,
189 for_push
? data
->options
.receivepack
:
190 data
->options
.uploadpack
,
196 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
)
198 struct git_transport_data
*data
= transport
->data
;
201 connect_setup(transport
, for_push
);
202 get_remote_heads(data
->fd
[0], NULL
, 0, &refs
,
203 for_push
? REF_NORMAL
: 0,
206 data
->got_remote_heads
= 1;
211 static int fetch_refs_via_pack(struct transport
*transport
,
212 int nr_heads
, struct ref
**to_fetch
)
215 struct git_transport_data
*data
= transport
->data
;
217 char *dest
= xstrdup(transport
->url
);
218 struct fetch_pack_args args
;
219 struct ref
*refs_tmp
= NULL
;
221 memset(&args
, 0, sizeof(args
));
222 args
.uploadpack
= data
->options
.uploadpack
;
223 args
.keep_pack
= data
->options
.keep
;
225 args
.use_thin_pack
= data
->options
.thin
;
226 args
.include_tag
= data
->options
.followtags
;
227 args
.verbose
= (transport
->verbose
> 1);
228 args
.quiet
= (transport
->verbose
< 0);
229 args
.no_progress
= !transport
->progress
;
230 args
.depth
= data
->options
.depth
;
231 args
.deepen_since
= data
->options
.deepen_since
;
232 args
.deepen_not
= data
->options
.deepen_not
;
233 args
.deepen_relative
= data
->options
.deepen_relative
;
234 args
.check_self_contained_and_connected
=
235 data
->options
.check_self_contained_and_connected
;
236 args
.cloning
= transport
->cloning
;
237 args
.update_shallow
= data
->options
.update_shallow
;
238 args
.from_promisor
= data
->options
.from_promisor
;
239 args
.no_dependents
= data
->options
.no_dependents
;
241 if (!data
->got_remote_heads
) {
242 connect_setup(transport
, 0);
243 get_remote_heads(data
->fd
[0], NULL
, 0, &refs_tmp
, 0,
244 NULL
, &data
->shallow
);
245 data
->got_remote_heads
= 1;
248 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
249 refs_tmp
? refs_tmp
: transport
->remote_refs
,
250 dest
, to_fetch
, nr_heads
, &data
->shallow
,
251 &transport
->pack_lockfile
);
254 if (finish_connect(data
->conn
))
257 data
->got_remote_heads
= 0;
258 data
->options
.self_contained_and_connected
=
259 args
.self_contained_and_connected
;
263 if (report_unmatched_refs(to_fetch
, nr_heads
))
272 static int push_had_errors(struct ref
*ref
)
274 for (; ref
; ref
= ref
->next
) {
275 switch (ref
->status
) {
276 case REF_STATUS_NONE
:
277 case REF_STATUS_UPTODATE
:
287 int transport_refs_pushed(struct ref
*ref
)
289 for (; ref
; ref
= ref
->next
) {
290 switch(ref
->status
) {
291 case REF_STATUS_NONE
:
292 case REF_STATUS_UPTODATE
:
301 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
305 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
311 if (!remote_find_tracking(remote
, &rs
)) {
313 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
315 delete_ref(NULL
, rs
.dst
, NULL
, 0);
317 update_ref("update by push", rs
.dst
, &ref
->new_oid
,
323 static void print_ref_status(char flag
, const char *summary
,
324 struct ref
*to
, struct ref
*from
, const char *msg
,
325 int porcelain
, int summary_width
)
329 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
331 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
333 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
335 fprintf(stdout
, "%s\n", summary
);
337 fprintf(stderr
, " %c %-*s ", flag
, summary_width
, summary
);
339 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
341 fputs(prettify_refname(to
->name
), stderr
);
351 static void print_ok_ref_status(struct ref
*ref
, int porcelain
, int summary_width
)
354 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
355 porcelain
, summary_width
);
356 else if (is_null_oid(&ref
->old_oid
))
357 print_ref_status('*',
358 (starts_with(ref
->name
, "refs/tags/") ? "[new tag]" :
360 ref
, ref
->peer_ref
, NULL
, porcelain
, summary_width
);
362 struct strbuf quickref
= STRBUF_INIT
;
366 strbuf_add_unique_abbrev(&quickref
, ref
->old_oid
.hash
,
368 if (ref
->forced_update
) {
369 strbuf_addstr(&quickref
, "...");
371 msg
= "forced update";
373 strbuf_addstr(&quickref
, "..");
377 strbuf_add_unique_abbrev(&quickref
, ref
->new_oid
.hash
,
380 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
381 porcelain
, summary_width
);
382 strbuf_release(&quickref
);
386 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
387 int porcelain
, int summary_width
)
390 char *url
= transport_anonymize_url(dest
);
391 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
395 switch(ref
->status
) {
396 case REF_STATUS_NONE
:
397 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
398 porcelain
, summary_width
);
400 case REF_STATUS_REJECT_NODELETE
:
401 print_ref_status('!', "[rejected]", ref
, NULL
,
402 "remote does not support deleting refs",
403 porcelain
, summary_width
);
405 case REF_STATUS_UPTODATE
:
406 print_ref_status('=', "[up to date]", ref
,
407 ref
->peer_ref
, NULL
, porcelain
, summary_width
);
409 case REF_STATUS_REJECT_NONFASTFORWARD
:
410 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
411 "non-fast-forward", porcelain
, summary_width
);
413 case REF_STATUS_REJECT_ALREADY_EXISTS
:
414 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
415 "already exists", porcelain
, summary_width
);
417 case REF_STATUS_REJECT_FETCH_FIRST
:
418 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
419 "fetch first", porcelain
, summary_width
);
421 case REF_STATUS_REJECT_NEEDS_FORCE
:
422 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
423 "needs force", porcelain
, summary_width
);
425 case REF_STATUS_REJECT_STALE
:
426 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
427 "stale info", porcelain
, summary_width
);
429 case REF_STATUS_REJECT_SHALLOW
:
430 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
431 "new shallow roots not allowed",
432 porcelain
, summary_width
);
434 case REF_STATUS_REMOTE_REJECT
:
435 print_ref_status('!', "[remote rejected]", ref
,
436 ref
->deletion
? NULL
: ref
->peer_ref
,
437 ref
->remote_status
, porcelain
, summary_width
);
439 case REF_STATUS_EXPECTING_REPORT
:
440 print_ref_status('!', "[remote failure]", ref
,
441 ref
->deletion
? NULL
: ref
->peer_ref
,
442 "remote failed to report status",
443 porcelain
, summary_width
);
445 case REF_STATUS_ATOMIC_PUSH_FAILED
:
446 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
447 "atomic push failed", porcelain
, summary_width
);
450 print_ok_ref_status(ref
, porcelain
, summary_width
);
457 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
459 char hex
[GIT_MAX_HEXSZ
+ 1];
460 int w
= find_unique_abbrev_r(hex
, oid
->hash
, DEFAULT_ABBREV
);
462 return (w
< sofar
) ? sofar
: w
;
465 int transport_summary_width(const struct ref
*refs
)
469 for (; refs
; refs
= refs
->next
) {
470 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
471 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
474 maxw
= FALLBACK_DEFAULT_ABBREV
;
475 return (2 * maxw
+ 3);
478 void transport_print_push_status(const char *dest
, struct ref
*refs
,
479 int verbose
, int porcelain
, unsigned int *reject_reasons
)
484 int summary_width
= transport_summary_width(refs
);
486 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
489 for (ref
= refs
; ref
; ref
= ref
->next
)
490 if (ref
->status
== REF_STATUS_UPTODATE
)
491 n
+= print_one_push_status(ref
, dest
, n
,
492 porcelain
, summary_width
);
495 for (ref
= refs
; ref
; ref
= ref
->next
)
496 if (ref
->status
== REF_STATUS_OK
)
497 n
+= print_one_push_status(ref
, dest
, n
,
498 porcelain
, summary_width
);
501 for (ref
= refs
; ref
; ref
= ref
->next
) {
502 if (ref
->status
!= REF_STATUS_NONE
&&
503 ref
->status
!= REF_STATUS_UPTODATE
&&
504 ref
->status
!= REF_STATUS_OK
)
505 n
+= print_one_push_status(ref
, dest
, n
,
506 porcelain
, summary_width
);
507 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
508 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
509 *reject_reasons
|= REJECT_NON_FF_HEAD
;
511 *reject_reasons
|= REJECT_NON_FF_OTHER
;
512 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
513 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
514 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
515 *reject_reasons
|= REJECT_FETCH_FIRST
;
516 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
517 *reject_reasons
|= REJECT_NEEDS_FORCE
;
523 void transport_verify_remote_names(int nr_heads
, const char **heads
)
527 for (i
= 0; i
< nr_heads
; i
++) {
528 const char *local
= heads
[i
];
529 const char *remote
= strrchr(heads
[i
], ':');
534 /* A matching refspec is okay. */
535 if (remote
== local
&& remote
[1] == '\0')
538 remote
= remote
? (remote
+ 1) : local
;
539 if (check_refname_format(remote
,
540 REFNAME_ALLOW_ONELEVEL
|REFNAME_REFSPEC_PATTERN
))
541 die("remote part of refspec is not a valid name in %s",
546 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
548 struct git_transport_data
*data
= transport
->data
;
549 struct send_pack_args args
;
552 if (!data
->got_remote_heads
) {
553 struct ref
*tmp_refs
;
554 connect_setup(transport
, 1);
556 get_remote_heads(data
->fd
[0], NULL
, 0, &tmp_refs
, REF_NORMAL
,
557 NULL
, &data
->shallow
);
558 data
->got_remote_heads
= 1;
561 memset(&args
, 0, sizeof(args
));
562 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
563 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
564 args
.use_thin_pack
= data
->options
.thin
;
565 args
.verbose
= (transport
->verbose
> 0);
566 args
.quiet
= (transport
->verbose
< 0);
567 args
.progress
= transport
->progress
;
568 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
569 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
570 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
571 args
.push_options
= transport
->push_options
;
572 args
.url
= transport
->url
;
574 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
575 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
576 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
577 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
579 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
581 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
586 ret
|= finish_connect(data
->conn
);
588 data
->got_remote_heads
= 0;
593 static int connect_git(struct transport
*transport
, const char *name
,
594 const char *executable
, int fd
[2])
596 struct git_transport_data
*data
= transport
->data
;
597 data
->conn
= git_connect(data
->fd
, transport
->url
,
604 static int disconnect_git(struct transport
*transport
)
606 struct git_transport_data
*data
= transport
->data
;
608 if (data
->got_remote_heads
)
609 packet_flush(data
->fd
[1]);
612 finish_connect(data
->conn
);
619 static struct transport_vtable taken_over_vtable
= {
621 get_refs_via_connect
,
628 void transport_take_over(struct transport
*transport
,
629 struct child_process
*child
)
631 struct git_transport_data
*data
;
633 if (!transport
->smart_options
)
634 die("BUG: taking over transport requires non-NULL "
635 "smart_options field.");
637 data
= xcalloc(1, sizeof(*data
));
638 data
->options
= *transport
->smart_options
;
640 data
->fd
[0] = data
->conn
->out
;
641 data
->fd
[1] = data
->conn
->in
;
642 data
->got_remote_heads
= 0;
643 transport
->data
= data
;
645 transport
->vtable
= &taken_over_vtable
;
646 transport
->smart_options
= &(data
->options
);
648 transport
->cannot_reuse
= 1;
651 static int is_file(const char *url
)
656 return S_ISREG(buf
.st_mode
);
659 static int external_specification_len(const char *url
)
661 return strchr(url
, ':') - url
;
664 static const struct string_list
*protocol_whitelist(void)
666 static int enabled
= -1;
667 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
670 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
672 string_list_split(&allowed
, v
, ':', -1);
673 string_list_sort(&allowed
);
680 return enabled
? &allowed
: NULL
;
683 enum protocol_allow_config
{
684 PROTOCOL_ALLOW_NEVER
= 0,
685 PROTOCOL_ALLOW_USER_ONLY
,
686 PROTOCOL_ALLOW_ALWAYS
689 static enum protocol_allow_config
parse_protocol_config(const char *key
,
692 if (!strcasecmp(value
, "always"))
693 return PROTOCOL_ALLOW_ALWAYS
;
694 else if (!strcasecmp(value
, "never"))
695 return PROTOCOL_ALLOW_NEVER
;
696 else if (!strcasecmp(value
, "user"))
697 return PROTOCOL_ALLOW_USER_ONLY
;
699 die("unknown value for config '%s': %s", key
, value
);
702 static enum protocol_allow_config
get_protocol_config(const char *type
)
704 char *key
= xstrfmt("protocol.%s.allow", type
);
707 /* first check the per-protocol config */
708 if (!git_config_get_string(key
, &value
)) {
709 enum protocol_allow_config ret
=
710 parse_protocol_config(key
, value
);
717 /* if defined, fallback to user-defined default for unknown protocols */
718 if (!git_config_get_string("protocol.allow", &value
)) {
719 enum protocol_allow_config ret
=
720 parse_protocol_config("protocol.allow", value
);
725 /* fallback to built-in defaults */
727 if (!strcmp(type
, "http") ||
728 !strcmp(type
, "https") ||
729 !strcmp(type
, "git") ||
730 !strcmp(type
, "ssh") ||
731 !strcmp(type
, "file"))
732 return PROTOCOL_ALLOW_ALWAYS
;
734 /* known scary; err on the side of caution */
735 if (!strcmp(type
, "ext"))
736 return PROTOCOL_ALLOW_NEVER
;
738 /* unknown; by default let them be used only directly by the user */
739 return PROTOCOL_ALLOW_USER_ONLY
;
742 int is_transport_allowed(const char *type
, int from_user
)
744 const struct string_list
*whitelist
= protocol_whitelist();
746 return string_list_has_string(whitelist
, type
);
748 switch (get_protocol_config(type
)) {
749 case PROTOCOL_ALLOW_ALWAYS
:
751 case PROTOCOL_ALLOW_NEVER
:
753 case PROTOCOL_ALLOW_USER_ONLY
:
755 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
759 die("BUG: invalid protocol_allow_config type");
762 void transport_check_allowed(const char *type
)
764 if (!is_transport_allowed(type
, -1))
765 die("transport '%s' not allowed", type
);
768 static struct transport_vtable bundle_vtable
= {
770 get_refs_from_bundle
,
771 fetch_refs_from_bundle
,
777 static struct transport_vtable builtin_smart_vtable
= {
779 get_refs_via_connect
,
786 struct transport
*transport_get(struct remote
*remote
, const char *url
)
789 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
791 ret
->progress
= isatty(2);
794 die("No remote provided to transport_get()");
796 ret
->got_remote_refs
= 0;
797 ret
->remote
= remote
;
798 helper
= remote
->foreign_vcs
;
800 if (!url
&& remote
->url
)
801 url
= remote
->url
[0];
804 /* maybe it is a foreign URL? */
808 while (is_urlschemechar(p
== url
, *p
))
810 if (starts_with(p
, "::"))
811 helper
= xstrndup(url
, p
- url
);
815 transport_helper_init(ret
, helper
);
816 } else if (starts_with(url
, "rsync:")) {
817 die("git-over-rsync is no longer supported");
818 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
819 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
820 transport_check_allowed("file");
822 ret
->vtable
= &bundle_vtable
;
823 ret
->smart_options
= NULL
;
824 } else if (!is_url(url
)
825 || starts_with(url
, "file://")
826 || starts_with(url
, "git://")
827 || starts_with(url
, "ssh://")
828 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
829 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
832 * These are builtin smart transports; "allowed" transports
833 * will be checked individually in git_connect.
835 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
837 ret
->vtable
= &builtin_smart_vtable
;
838 ret
->smart_options
= &(data
->options
);
841 data
->got_remote_heads
= 0;
843 /* Unknown protocol in URL. Pass to external handler. */
844 int len
= external_specification_len(url
);
845 char *handler
= xmemdupz(url
, len
);
846 transport_helper_init(ret
, handler
);
849 if (ret
->smart_options
) {
850 ret
->smart_options
->thin
= 1;
851 ret
->smart_options
->uploadpack
= "git-upload-pack";
852 if (remote
->uploadpack
)
853 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
854 ret
->smart_options
->receivepack
= "git-receive-pack";
855 if (remote
->receivepack
)
856 ret
->smart_options
->receivepack
= remote
->receivepack
;
862 int transport_set_option(struct transport
*transport
,
863 const char *name
, const char *value
)
865 int git_reports
= 1, protocol_reports
= 1;
867 if (transport
->smart_options
)
868 git_reports
= set_git_option(transport
->smart_options
,
871 if (transport
->vtable
->set_option
)
872 protocol_reports
= transport
->vtable
->set_option(transport
,
875 /* If either report is 0, report 0 (success). */
876 if (!git_reports
|| !protocol_reports
)
878 /* If either reports -1 (invalid value), report -1. */
879 if ((git_reports
== -1) || (protocol_reports
== -1))
881 /* Otherwise if both report unknown, report unknown. */
885 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
889 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
891 transport
->verbose
= -1;
894 * Rules used to determine whether to report progress (processing aborts
895 * when a rule is satisfied):
897 * . Report progress, if force_progress is 1 (ie. --progress).
898 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
899 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
900 * . Report progress if isatty(2) is 1.
902 if (force_progress
>= 0)
903 transport
->progress
= !!force_progress
;
905 transport
->progress
= verbosity
>= 0 && isatty(2);
908 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
912 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
913 "not be found on any remote:\n"));
914 for (i
= 0; i
< needs_pushing
->nr
; i
++)
915 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
916 fprintf(stderr
, _("\nPlease try\n\n"
917 " git push --recurse-submodules=on-demand\n\n"
918 "or cd to the path and use\n\n"
920 "to push them to a remote.\n\n"));
922 string_list_clear(needs_pushing
, 0);
927 static int run_pre_push_hook(struct transport
*transport
,
928 struct ref
*remote_refs
)
932 struct child_process proc
= CHILD_PROCESS_INIT
;
936 if (!(argv
[0] = find_hook("pre-push")))
939 argv
[1] = transport
->remote
->name
;
940 argv
[2] = transport
->url
;
946 if (start_command(&proc
)) {
947 finish_command(&proc
);
951 sigchain_push(SIGPIPE
, SIG_IGN
);
953 strbuf_init(&buf
, 256);
955 for (r
= remote_refs
; r
; r
= r
->next
) {
956 if (!r
->peer_ref
) continue;
957 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
958 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
959 if (r
->status
== REF_STATUS_UPTODATE
) continue;
962 strbuf_addf( &buf
, "%s %s %s %s\n",
963 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
964 r
->name
, oid_to_hex(&r
->old_oid
));
966 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
967 /* We do not mind if a hook does not read all refs. */
974 strbuf_release(&buf
);
980 sigchain_pop(SIGPIPE
);
982 x
= finish_command(&proc
);
989 int transport_push(struct transport
*transport
,
990 int refspec_nr
, const char **refspec
, int flags
,
991 unsigned int *reject_reasons
)
994 transport_verify_remote_names(refspec_nr
, refspec
);
996 if (transport
->vtable
->push_refs
) {
997 struct ref
*remote_refs
;
998 struct ref
*local_refs
= get_local_heads();
999 int match_flags
= MATCH_REFS_NONE
;
1000 int verbose
= (transport
->verbose
> 0);
1001 int quiet
= (transport
->verbose
< 0);
1002 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1003 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1004 int push_ret
, ret
, err
;
1006 if (check_push_refs(local_refs
, refspec_nr
, refspec
) < 0)
1009 remote_refs
= transport
->vtable
->get_refs_list(transport
, 1);
1011 if (flags
& TRANSPORT_PUSH_ALL
)
1012 match_flags
|= MATCH_REFS_ALL
;
1013 if (flags
& TRANSPORT_PUSH_MIRROR
)
1014 match_flags
|= MATCH_REFS_MIRROR
;
1015 if (flags
& TRANSPORT_PUSH_PRUNE
)
1016 match_flags
|= MATCH_REFS_PRUNE
;
1017 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1018 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1020 if (match_push_refs(local_refs
, &remote_refs
,
1021 refspec_nr
, refspec
, match_flags
)) {
1025 if (transport
->smart_options
&&
1026 transport
->smart_options
->cas
&&
1027 !is_empty_cas(transport
->smart_options
->cas
))
1028 apply_push_cas(transport
->smart_options
->cas
,
1029 transport
->remote
, remote_refs
);
1031 set_ref_status_for_push(remote_refs
,
1032 flags
& TRANSPORT_PUSH_MIRROR
,
1033 flags
& TRANSPORT_PUSH_FORCE
);
1035 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1036 if (run_pre_push_hook(transport
, remote_refs
))
1039 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1040 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1041 !is_bare_repository()) {
1042 struct ref
*ref
= remote_refs
;
1043 struct oid_array commits
= OID_ARRAY_INIT
;
1045 for (; ref
; ref
= ref
->next
)
1046 if (!is_null_oid(&ref
->new_oid
))
1047 oid_array_append(&commits
,
1050 if (!push_unpushed_submodules(&commits
,
1052 refspec
, refspec_nr
,
1053 transport
->push_options
,
1055 oid_array_clear(&commits
);
1056 die("Failed to push all needed submodules!");
1058 oid_array_clear(&commits
);
1061 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1062 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1063 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1064 !pretend
)) && !is_bare_repository()) {
1065 struct ref
*ref
= remote_refs
;
1066 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1067 struct oid_array commits
= OID_ARRAY_INIT
;
1069 for (; ref
; ref
= ref
->next
)
1070 if (!is_null_oid(&ref
->new_oid
))
1071 oid_array_append(&commits
,
1074 if (find_unpushed_submodules(&commits
, transport
->remote
->name
,
1076 oid_array_clear(&commits
);
1077 die_with_unpushed_submodules(&needs_pushing
);
1079 string_list_clear(&needs_pushing
, 0);
1080 oid_array_clear(&commits
);
1083 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
))
1084 push_ret
= transport
->vtable
->push_refs(transport
, remote_refs
, flags
);
1087 err
= push_had_errors(remote_refs
);
1088 ret
= push_ret
| err
;
1091 transport_print_push_status(transport
->url
, remote_refs
,
1092 verbose
| porcelain
, porcelain
,
1095 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1096 set_upstreams(transport
, remote_refs
, pretend
);
1098 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1099 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1101 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1102 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1105 if (porcelain
&& !push_ret
)
1107 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1108 fprintf(stderr
, "Everything up-to-date\n");
1115 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
1117 if (!transport
->got_remote_refs
) {
1118 transport
->remote_refs
= transport
->vtable
->get_refs_list(transport
, 0);
1119 transport
->got_remote_refs
= 1;
1122 return transport
->remote_refs
;
1125 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1128 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1129 struct ref
**heads
= NULL
;
1132 for (rm
= refs
; rm
; rm
= rm
->next
) {
1135 !is_null_oid(&rm
->old_oid
) &&
1136 !oidcmp(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1138 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1139 heads
[nr_heads
++] = rm
;
1144 * When deepening of a shallow repository is requested,
1145 * then local and remote refs are likely to still be equal.
1146 * Just feed them all to the fetch method in that case.
1147 * This condition shouldn't be met in a non-deepening fetch
1148 * (see builtin/fetch.c:quickfetch()).
1150 ALLOC_ARRAY(heads
, nr_refs
);
1151 for (rm
= refs
; rm
; rm
= rm
->next
)
1152 heads
[nr_heads
++] = rm
;
1155 rc
= transport
->vtable
->fetch(transport
, nr_heads
, heads
);
1161 void transport_unlock_pack(struct transport
*transport
)
1163 if (transport
->pack_lockfile
) {
1164 unlink_or_warn(transport
->pack_lockfile
);
1165 FREE_AND_NULL(transport
->pack_lockfile
);
1169 int transport_connect(struct transport
*transport
, const char *name
,
1170 const char *exec
, int fd
[2])
1172 if (transport
->vtable
->connect
)
1173 return transport
->vtable
->connect(transport
, name
, exec
, fd
);
1175 die("Operation not supported by protocol");
1178 int transport_disconnect(struct transport
*transport
)
1181 if (transport
->vtable
->disconnect
)
1182 ret
= transport
->vtable
->disconnect(transport
);
1188 * Strip username (and password) from a URL and return
1189 * it in a newly allocated string.
1191 char *transport_anonymize_url(const char *url
)
1193 char *scheme_prefix
, *anon_part
;
1194 size_t anon_len
, prefix_len
= 0;
1196 anon_part
= strchr(url
, '@');
1197 if (url_is_local_not_ssh(url
) || !anon_part
)
1200 anon_len
= strlen(++anon_part
);
1201 scheme_prefix
= strstr(url
, "://");
1202 if (!scheme_prefix
) {
1203 if (!strchr(anon_part
, ':'))
1204 /* cannot be "me@there:/path/name" */
1208 /* make sure scheme is reasonable */
1209 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1212 case '+': case '.': case '-':
1221 /* @ past the first slash does not count */
1222 cp
= strchr(scheme_prefix
+ 3, '/');
1223 if (cp
&& cp
< anon_part
)
1225 prefix_len
= scheme_prefix
- url
+ 3;
1227 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1228 (int)anon_len
, anon_part
);
1230 return xstrdup(url
);
1233 static void read_alternate_refs(const char *path
,
1234 alternate_ref_fn
*cb
,
1237 struct child_process cmd
= CHILD_PROCESS_INIT
;
1238 struct strbuf line
= STRBUF_INIT
;
1242 argv_array_pushf(&cmd
.args
, "--git-dir=%s", path
);
1243 argv_array_push(&cmd
.args
, "for-each-ref");
1244 argv_array_push(&cmd
.args
, "--format=%(objectname) %(refname)");
1245 cmd
.env
= local_repo_env
;
1248 if (start_command(&cmd
))
1251 fh
= xfdopen(cmd
.out
, "r");
1252 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
1253 struct object_id oid
;
1255 if (get_oid_hex(line
.buf
, &oid
) ||
1256 line
.buf
[GIT_SHA1_HEXSZ
] != ' ') {
1257 warning("invalid line while parsing alternate refs: %s",
1262 cb(line
.buf
+ GIT_SHA1_HEXSZ
+ 1, &oid
, data
);
1266 finish_command(&cmd
);
1269 struct alternate_refs_data
{
1270 alternate_ref_fn
*fn
;
1274 static int refs_from_alternate_cb(struct alternate_object_database
*e
,
1277 struct strbuf path
= STRBUF_INIT
;
1279 struct alternate_refs_data
*cb
= data
;
1281 if (!strbuf_realpath(&path
, e
->path
, 0))
1283 if (!strbuf_strip_suffix(&path
, "/objects"))
1285 base_len
= path
.len
;
1287 /* Is this a git repository with refs? */
1288 strbuf_addstr(&path
, "/refs");
1289 if (!is_directory(path
.buf
))
1291 strbuf_setlen(&path
, base_len
);
1293 read_alternate_refs(path
.buf
, cb
->fn
, cb
->data
);
1296 strbuf_release(&path
);
1300 void for_each_alternate_ref(alternate_ref_fn fn
, void *data
)
1302 struct alternate_refs_data cb
;
1305 foreach_alt_odb(refs_from_alternate_cb
, &cb
);