4 #include "run-command.h"
6 #include "fetch-pack.h"
16 #include "submodule.h"
17 #include "string-list.h"
18 #include "sha1-array.h"
21 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
25 for (ref
= refs
; ref
; ref
= ref
->next
) {
26 const char *localname
;
28 const char *remotename
;
29 unsigned char sha
[20];
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
;
168 static int connect_setup(struct transport
*transport
, int for_push
)
170 struct git_transport_data
*data
= transport
->data
;
171 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
176 switch (transport
->family
) {
177 case TRANSPORT_FAMILY_ALL
: break;
178 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
179 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
182 data
->conn
= git_connect(data
->fd
, transport
->url
,
183 for_push
? data
->options
.receivepack
:
184 data
->options
.uploadpack
,
190 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
)
192 struct git_transport_data
*data
= transport
->data
;
195 connect_setup(transport
, for_push
);
196 get_remote_heads(data
->fd
[0], NULL
, 0, &refs
,
197 for_push
? REF_NORMAL
: 0,
200 data
->got_remote_heads
= 1;
205 static int fetch_refs_via_pack(struct transport
*transport
,
206 int nr_heads
, struct ref
**to_fetch
)
209 struct git_transport_data
*data
= transport
->data
;
211 char *dest
= xstrdup(transport
->url
);
212 struct fetch_pack_args args
;
213 struct ref
*refs_tmp
= NULL
;
215 memset(&args
, 0, sizeof(args
));
216 args
.uploadpack
= data
->options
.uploadpack
;
217 args
.keep_pack
= data
->options
.keep
;
219 args
.use_thin_pack
= data
->options
.thin
;
220 args
.include_tag
= data
->options
.followtags
;
221 args
.verbose
= (transport
->verbose
> 1);
222 args
.quiet
= (transport
->verbose
< 0);
223 args
.no_progress
= !transport
->progress
;
224 args
.depth
= data
->options
.depth
;
225 args
.deepen_since
= data
->options
.deepen_since
;
226 args
.deepen_not
= data
->options
.deepen_not
;
227 args
.deepen_relative
= data
->options
.deepen_relative
;
228 args
.check_self_contained_and_connected
=
229 data
->options
.check_self_contained_and_connected
;
230 args
.cloning
= transport
->cloning
;
231 args
.update_shallow
= data
->options
.update_shallow
;
233 if (!data
->got_remote_heads
) {
234 connect_setup(transport
, 0);
235 get_remote_heads(data
->fd
[0], NULL
, 0, &refs_tmp
, 0,
236 NULL
, &data
->shallow
);
237 data
->got_remote_heads
= 1;
240 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
241 refs_tmp
? refs_tmp
: transport
->remote_refs
,
242 dest
, to_fetch
, nr_heads
, &data
->shallow
,
243 &transport
->pack_lockfile
);
246 if (finish_connect(data
->conn
))
249 data
->got_remote_heads
= 0;
250 data
->options
.self_contained_and_connected
=
251 args
.self_contained_and_connected
;
255 if (report_unmatched_refs(to_fetch
, nr_heads
))
264 static int push_had_errors(struct ref
*ref
)
266 for (; ref
; ref
= ref
->next
) {
267 switch (ref
->status
) {
268 case REF_STATUS_NONE
:
269 case REF_STATUS_UPTODATE
:
279 int transport_refs_pushed(struct ref
*ref
)
281 for (; ref
; ref
= ref
->next
) {
282 switch(ref
->status
) {
283 case REF_STATUS_NONE
:
284 case REF_STATUS_UPTODATE
:
293 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
297 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
303 if (!remote_find_tracking(remote
, &rs
)) {
305 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
307 delete_ref(NULL
, rs
.dst
, NULL
, 0);
309 update_ref("update by push", rs
.dst
,
310 ref
->new_oid
.hash
, NULL
, 0, 0);
315 static void print_ref_status(char flag
, const char *summary
,
316 struct ref
*to
, struct ref
*from
, const char *msg
,
317 int porcelain
, int summary_width
)
321 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
323 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
325 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
327 fprintf(stdout
, "%s\n", summary
);
329 fprintf(stderr
, " %c %-*s ", flag
, summary_width
, summary
);
331 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
333 fputs(prettify_refname(to
->name
), stderr
);
343 static void print_ok_ref_status(struct ref
*ref
, int porcelain
, int summary_width
)
346 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
347 porcelain
, summary_width
);
348 else if (is_null_oid(&ref
->old_oid
))
349 print_ref_status('*',
350 (starts_with(ref
->name
, "refs/tags/") ? "[new tag]" :
352 ref
, ref
->peer_ref
, NULL
, porcelain
, summary_width
);
354 struct strbuf quickref
= STRBUF_INIT
;
358 strbuf_add_unique_abbrev(&quickref
, ref
->old_oid
.hash
,
360 if (ref
->forced_update
) {
361 strbuf_addstr(&quickref
, "...");
363 msg
= "forced update";
365 strbuf_addstr(&quickref
, "..");
369 strbuf_add_unique_abbrev(&quickref
, ref
->new_oid
.hash
,
372 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
373 porcelain
, summary_width
);
374 strbuf_release(&quickref
);
378 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
379 int porcelain
, int summary_width
)
382 char *url
= transport_anonymize_url(dest
);
383 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
387 switch(ref
->status
) {
388 case REF_STATUS_NONE
:
389 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
390 porcelain
, summary_width
);
392 case REF_STATUS_REJECT_NODELETE
:
393 print_ref_status('!', "[rejected]", ref
, NULL
,
394 "remote does not support deleting refs",
395 porcelain
, summary_width
);
397 case REF_STATUS_UPTODATE
:
398 print_ref_status('=', "[up to date]", ref
,
399 ref
->peer_ref
, NULL
, porcelain
, summary_width
);
401 case REF_STATUS_REJECT_NONFASTFORWARD
:
402 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
403 "non-fast-forward", porcelain
, summary_width
);
405 case REF_STATUS_REJECT_ALREADY_EXISTS
:
406 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
407 "already exists", porcelain
, summary_width
);
409 case REF_STATUS_REJECT_FETCH_FIRST
:
410 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
411 "fetch first", porcelain
, summary_width
);
413 case REF_STATUS_REJECT_NEEDS_FORCE
:
414 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
415 "needs force", porcelain
, summary_width
);
417 case REF_STATUS_REJECT_STALE
:
418 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
419 "stale info", porcelain
, summary_width
);
421 case REF_STATUS_REJECT_SHALLOW
:
422 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
423 "new shallow roots not allowed",
424 porcelain
, summary_width
);
426 case REF_STATUS_REMOTE_REJECT
:
427 print_ref_status('!', "[remote rejected]", ref
,
428 ref
->deletion
? NULL
: ref
->peer_ref
,
429 ref
->remote_status
, porcelain
, summary_width
);
431 case REF_STATUS_EXPECTING_REPORT
:
432 print_ref_status('!', "[remote failure]", ref
,
433 ref
->deletion
? NULL
: ref
->peer_ref
,
434 "remote failed to report status",
435 porcelain
, summary_width
);
437 case REF_STATUS_ATOMIC_PUSH_FAILED
:
438 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
439 "atomic push failed", porcelain
, summary_width
);
442 print_ok_ref_status(ref
, porcelain
, summary_width
);
449 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
451 char hex
[GIT_MAX_HEXSZ
+ 1];
452 int w
= find_unique_abbrev_r(hex
, oid
->hash
, DEFAULT_ABBREV
);
454 return (w
< sofar
) ? sofar
: w
;
457 int transport_summary_width(const struct ref
*refs
)
461 for (; refs
; refs
= refs
->next
) {
462 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
463 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
466 maxw
= FALLBACK_DEFAULT_ABBREV
;
467 return (2 * maxw
+ 3);
470 void transport_print_push_status(const char *dest
, struct ref
*refs
,
471 int verbose
, int porcelain
, unsigned int *reject_reasons
)
475 struct object_id head_oid
;
477 int summary_width
= transport_summary_width(refs
);
479 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, head_oid
.hash
, NULL
);
482 for (ref
= refs
; ref
; ref
= ref
->next
)
483 if (ref
->status
== REF_STATUS_UPTODATE
)
484 n
+= print_one_push_status(ref
, dest
, n
,
485 porcelain
, summary_width
);
488 for (ref
= refs
; ref
; ref
= ref
->next
)
489 if (ref
->status
== REF_STATUS_OK
)
490 n
+= print_one_push_status(ref
, dest
, n
,
491 porcelain
, summary_width
);
494 for (ref
= refs
; ref
; ref
= ref
->next
) {
495 if (ref
->status
!= REF_STATUS_NONE
&&
496 ref
->status
!= REF_STATUS_UPTODATE
&&
497 ref
->status
!= REF_STATUS_OK
)
498 n
+= print_one_push_status(ref
, dest
, n
,
499 porcelain
, summary_width
);
500 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
501 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
502 *reject_reasons
|= REJECT_NON_FF_HEAD
;
504 *reject_reasons
|= REJECT_NON_FF_OTHER
;
505 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
506 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
507 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
508 *reject_reasons
|= REJECT_FETCH_FIRST
;
509 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
510 *reject_reasons
|= REJECT_NEEDS_FORCE
;
516 void transport_verify_remote_names(int nr_heads
, const char **heads
)
520 for (i
= 0; i
< nr_heads
; i
++) {
521 const char *local
= heads
[i
];
522 const char *remote
= strrchr(heads
[i
], ':');
527 /* A matching refspec is okay. */
528 if (remote
== local
&& remote
[1] == '\0')
531 remote
= remote
? (remote
+ 1) : local
;
532 if (check_refname_format(remote
,
533 REFNAME_ALLOW_ONELEVEL
|REFNAME_REFSPEC_PATTERN
))
534 die("remote part of refspec is not a valid name in %s",
539 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
541 struct git_transport_data
*data
= transport
->data
;
542 struct send_pack_args args
;
545 if (!data
->got_remote_heads
) {
546 struct ref
*tmp_refs
;
547 connect_setup(transport
, 1);
549 get_remote_heads(data
->fd
[0], NULL
, 0, &tmp_refs
, REF_NORMAL
,
550 NULL
, &data
->shallow
);
551 data
->got_remote_heads
= 1;
554 memset(&args
, 0, sizeof(args
));
555 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
556 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
557 args
.use_thin_pack
= data
->options
.thin
;
558 args
.verbose
= (transport
->verbose
> 0);
559 args
.quiet
= (transport
->verbose
< 0);
560 args
.progress
= transport
->progress
;
561 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
562 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
563 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
564 args
.push_options
= transport
->push_options
;
565 args
.url
= transport
->url
;
567 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
568 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
569 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
570 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
572 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
574 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
579 ret
|= finish_connect(data
->conn
);
581 data
->got_remote_heads
= 0;
586 static int connect_git(struct transport
*transport
, const char *name
,
587 const char *executable
, int fd
[2])
589 struct git_transport_data
*data
= transport
->data
;
590 data
->conn
= git_connect(data
->fd
, transport
->url
,
597 static int disconnect_git(struct transport
*transport
)
599 struct git_transport_data
*data
= transport
->data
;
601 if (data
->got_remote_heads
)
602 packet_flush(data
->fd
[1]);
605 finish_connect(data
->conn
);
612 void transport_take_over(struct transport
*transport
,
613 struct child_process
*child
)
615 struct git_transport_data
*data
;
617 if (!transport
->smart_options
)
618 die("BUG: taking over transport requires non-NULL "
619 "smart_options field.");
621 data
= xcalloc(1, sizeof(*data
));
622 data
->options
= *transport
->smart_options
;
624 data
->fd
[0] = data
->conn
->out
;
625 data
->fd
[1] = data
->conn
->in
;
626 data
->got_remote_heads
= 0;
627 transport
->data
= data
;
629 transport
->set_option
= NULL
;
630 transport
->get_refs_list
= get_refs_via_connect
;
631 transport
->fetch
= fetch_refs_via_pack
;
632 transport
->push
= NULL
;
633 transport
->push_refs
= git_transport_push
;
634 transport
->disconnect
= disconnect_git
;
635 transport
->smart_options
= &(data
->options
);
637 transport
->cannot_reuse
= 1;
640 static int is_file(const char *url
)
645 return S_ISREG(buf
.st_mode
);
648 static int external_specification_len(const char *url
)
650 return strchr(url
, ':') - url
;
653 static const struct string_list
*protocol_whitelist(void)
655 static int enabled
= -1;
656 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
659 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
661 string_list_split(&allowed
, v
, ':', -1);
662 string_list_sort(&allowed
);
669 return enabled
? &allowed
: NULL
;
672 enum protocol_allow_config
{
673 PROTOCOL_ALLOW_NEVER
= 0,
674 PROTOCOL_ALLOW_USER_ONLY
,
675 PROTOCOL_ALLOW_ALWAYS
678 static enum protocol_allow_config
parse_protocol_config(const char *key
,
681 if (!strcasecmp(value
, "always"))
682 return PROTOCOL_ALLOW_ALWAYS
;
683 else if (!strcasecmp(value
, "never"))
684 return PROTOCOL_ALLOW_NEVER
;
685 else if (!strcasecmp(value
, "user"))
686 return PROTOCOL_ALLOW_USER_ONLY
;
688 die("unknown value for config '%s': %s", key
, value
);
691 static enum protocol_allow_config
get_protocol_config(const char *type
)
693 char *key
= xstrfmt("protocol.%s.allow", type
);
696 /* first check the per-protocol config */
697 if (!git_config_get_string(key
, &value
)) {
698 enum protocol_allow_config ret
=
699 parse_protocol_config(key
, value
);
706 /* if defined, fallback to user-defined default for unknown protocols */
707 if (!git_config_get_string("protocol.allow", &value
)) {
708 enum protocol_allow_config ret
=
709 parse_protocol_config("protocol.allow", value
);
714 /* fallback to built-in defaults */
716 if (!strcmp(type
, "http") ||
717 !strcmp(type
, "https") ||
718 !strcmp(type
, "git") ||
719 !strcmp(type
, "ssh") ||
720 !strcmp(type
, "file"))
721 return PROTOCOL_ALLOW_ALWAYS
;
723 /* known scary; err on the side of caution */
724 if (!strcmp(type
, "ext"))
725 return PROTOCOL_ALLOW_NEVER
;
727 /* unknown; by default let them be used only directly by the user */
728 return PROTOCOL_ALLOW_USER_ONLY
;
731 int is_transport_allowed(const char *type
, int from_user
)
733 const struct string_list
*whitelist
= protocol_whitelist();
735 return string_list_has_string(whitelist
, type
);
737 switch (get_protocol_config(type
)) {
738 case PROTOCOL_ALLOW_ALWAYS
:
740 case PROTOCOL_ALLOW_NEVER
:
742 case PROTOCOL_ALLOW_USER_ONLY
:
744 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
748 die("BUG: invalid protocol_allow_config type");
751 void transport_check_allowed(const char *type
)
753 if (!is_transport_allowed(type
, -1))
754 die("transport '%s' not allowed", type
);
757 struct transport
*transport_get(struct remote
*remote
, const char *url
)
760 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
762 ret
->progress
= isatty(2);
765 die("No remote provided to transport_get()");
767 ret
->got_remote_refs
= 0;
768 ret
->remote
= remote
;
769 helper
= remote
->foreign_vcs
;
771 if (!url
&& remote
->url
)
772 url
= remote
->url
[0];
775 /* maybe it is a foreign URL? */
779 while (is_urlschemechar(p
== url
, *p
))
781 if (starts_with(p
, "::"))
782 helper
= xstrndup(url
, p
- url
);
786 transport_helper_init(ret
, helper
);
787 } else if (starts_with(url
, "rsync:")) {
788 die("git-over-rsync is no longer supported");
789 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
790 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
791 transport_check_allowed("file");
793 ret
->get_refs_list
= get_refs_from_bundle
;
794 ret
->fetch
= fetch_refs_from_bundle
;
795 ret
->disconnect
= close_bundle
;
796 ret
->smart_options
= NULL
;
797 } else if (!is_url(url
)
798 || starts_with(url
, "file://")
799 || starts_with(url
, "git://")
800 || starts_with(url
, "ssh://")
801 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
802 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
805 * These are builtin smart transports; "allowed" transports
806 * will be checked individually in git_connect.
808 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
810 ret
->set_option
= NULL
;
811 ret
->get_refs_list
= get_refs_via_connect
;
812 ret
->fetch
= fetch_refs_via_pack
;
813 ret
->push_refs
= git_transport_push
;
814 ret
->connect
= connect_git
;
815 ret
->disconnect
= disconnect_git
;
816 ret
->smart_options
= &(data
->options
);
819 data
->got_remote_heads
= 0;
821 /* Unknown protocol in URL. Pass to external handler. */
822 int len
= external_specification_len(url
);
823 char *handler
= xmemdupz(url
, len
);
824 transport_helper_init(ret
, handler
);
827 if (ret
->smart_options
) {
828 ret
->smart_options
->thin
= 1;
829 ret
->smart_options
->uploadpack
= "git-upload-pack";
830 if (remote
->uploadpack
)
831 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
832 ret
->smart_options
->receivepack
= "git-receive-pack";
833 if (remote
->receivepack
)
834 ret
->smart_options
->receivepack
= remote
->receivepack
;
840 int transport_set_option(struct transport
*transport
,
841 const char *name
, const char *value
)
843 int git_reports
= 1, protocol_reports
= 1;
845 if (transport
->smart_options
)
846 git_reports
= set_git_option(transport
->smart_options
,
849 if (transport
->set_option
)
850 protocol_reports
= transport
->set_option(transport
, name
,
853 /* If either report is 0, report 0 (success). */
854 if (!git_reports
|| !protocol_reports
)
856 /* If either reports -1 (invalid value), report -1. */
857 if ((git_reports
== -1) || (protocol_reports
== -1))
859 /* Otherwise if both report unknown, report unknown. */
863 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
867 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
869 transport
->verbose
= -1;
872 * Rules used to determine whether to report progress (processing aborts
873 * when a rule is satisfied):
875 * . Report progress, if force_progress is 1 (ie. --progress).
876 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
877 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
878 * . Report progress if isatty(2) is 1.
880 if (force_progress
>= 0)
881 transport
->progress
= !!force_progress
;
883 transport
->progress
= verbosity
>= 0 && isatty(2);
886 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
890 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
891 "not be found on any remote:\n"));
892 for (i
= 0; i
< needs_pushing
->nr
; i
++)
893 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
894 fprintf(stderr
, _("\nPlease try\n\n"
895 " git push --recurse-submodules=on-demand\n\n"
896 "or cd to the path and use\n\n"
898 "to push them to a remote.\n\n"));
900 string_list_clear(needs_pushing
, 0);
905 static int run_pre_push_hook(struct transport
*transport
,
906 struct ref
*remote_refs
)
910 struct child_process proc
= CHILD_PROCESS_INIT
;
914 if (!(argv
[0] = find_hook("pre-push")))
917 argv
[1] = transport
->remote
->name
;
918 argv
[2] = transport
->url
;
924 if (start_command(&proc
)) {
925 finish_command(&proc
);
929 sigchain_push(SIGPIPE
, SIG_IGN
);
931 strbuf_init(&buf
, 256);
933 for (r
= remote_refs
; r
; r
= r
->next
) {
934 if (!r
->peer_ref
) continue;
935 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
936 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
937 if (r
->status
== REF_STATUS_UPTODATE
) continue;
940 strbuf_addf( &buf
, "%s %s %s %s\n",
941 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
942 r
->name
, oid_to_hex(&r
->old_oid
));
944 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
945 /* We do not mind if a hook does not read all refs. */
952 strbuf_release(&buf
);
958 sigchain_pop(SIGPIPE
);
960 x
= finish_command(&proc
);
967 int transport_push(struct transport
*transport
,
968 int refspec_nr
, const char **refspec
, int flags
,
969 unsigned int *reject_reasons
)
972 transport_verify_remote_names(refspec_nr
, refspec
);
974 if (transport
->push
) {
975 /* Maybe FIXME. But no important transport uses this case. */
976 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
977 die("This transport does not support using --set-upstream");
979 return transport
->push(transport
, refspec_nr
, refspec
, flags
);
980 } else if (transport
->push_refs
) {
981 struct ref
*remote_refs
;
982 struct ref
*local_refs
= get_local_heads();
983 int match_flags
= MATCH_REFS_NONE
;
984 int verbose
= (transport
->verbose
> 0);
985 int quiet
= (transport
->verbose
< 0);
986 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
987 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
988 int push_ret
, ret
, err
;
990 if (check_push_refs(local_refs
, refspec_nr
, refspec
) < 0)
993 remote_refs
= transport
->get_refs_list(transport
, 1);
995 if (flags
& TRANSPORT_PUSH_ALL
)
996 match_flags
|= MATCH_REFS_ALL
;
997 if (flags
& TRANSPORT_PUSH_MIRROR
)
998 match_flags
|= MATCH_REFS_MIRROR
;
999 if (flags
& TRANSPORT_PUSH_PRUNE
)
1000 match_flags
|= MATCH_REFS_PRUNE
;
1001 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
1002 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
1004 if (match_push_refs(local_refs
, &remote_refs
,
1005 refspec_nr
, refspec
, match_flags
)) {
1009 if (transport
->smart_options
&&
1010 transport
->smart_options
->cas
&&
1011 !is_empty_cas(transport
->smart_options
->cas
))
1012 apply_push_cas(transport
->smart_options
->cas
,
1013 transport
->remote
, remote_refs
);
1015 set_ref_status_for_push(remote_refs
,
1016 flags
& TRANSPORT_PUSH_MIRROR
,
1017 flags
& TRANSPORT_PUSH_FORCE
);
1019 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1020 if (run_pre_push_hook(transport
, remote_refs
))
1023 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1024 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1025 !is_bare_repository()) {
1026 struct ref
*ref
= remote_refs
;
1027 struct oid_array commits
= OID_ARRAY_INIT
;
1029 for (; ref
; ref
= ref
->next
)
1030 if (!is_null_oid(&ref
->new_oid
))
1031 oid_array_append(&commits
,
1034 if (!push_unpushed_submodules(&commits
,
1036 refspec
, refspec_nr
,
1037 transport
->push_options
,
1039 oid_array_clear(&commits
);
1040 die("Failed to push all needed submodules!");
1042 oid_array_clear(&commits
);
1045 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1046 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1047 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1048 !pretend
)) && !is_bare_repository()) {
1049 struct ref
*ref
= remote_refs
;
1050 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1051 struct oid_array commits
= OID_ARRAY_INIT
;
1053 for (; ref
; ref
= ref
->next
)
1054 if (!is_null_oid(&ref
->new_oid
))
1055 oid_array_append(&commits
,
1058 if (find_unpushed_submodules(&commits
, transport
->remote
->name
,
1060 oid_array_clear(&commits
);
1061 die_with_unpushed_submodules(&needs_pushing
);
1063 string_list_clear(&needs_pushing
, 0);
1064 oid_array_clear(&commits
);
1067 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
))
1068 push_ret
= transport
->push_refs(transport
, remote_refs
, flags
);
1071 err
= push_had_errors(remote_refs
);
1072 ret
= push_ret
| err
;
1075 transport_print_push_status(transport
->url
, remote_refs
,
1076 verbose
| porcelain
, porcelain
,
1079 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1080 set_upstreams(transport
, remote_refs
, pretend
);
1082 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1083 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1085 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1086 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1089 if (porcelain
&& !push_ret
)
1091 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1092 fprintf(stderr
, "Everything up-to-date\n");
1099 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
1101 if (!transport
->got_remote_refs
) {
1102 transport
->remote_refs
= transport
->get_refs_list(transport
, 0);
1103 transport
->got_remote_refs
= 1;
1106 return transport
->remote_refs
;
1109 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1112 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1113 struct ref
**heads
= NULL
;
1116 for (rm
= refs
; rm
; rm
= rm
->next
) {
1119 !is_null_oid(&rm
->old_oid
) &&
1120 !oidcmp(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1122 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1123 heads
[nr_heads
++] = rm
;
1128 * When deepening of a shallow repository is requested,
1129 * then local and remote refs are likely to still be equal.
1130 * Just feed them all to the fetch method in that case.
1131 * This condition shouldn't be met in a non-deepening fetch
1132 * (see builtin/fetch.c:quickfetch()).
1134 ALLOC_ARRAY(heads
, nr_refs
);
1135 for (rm
= refs
; rm
; rm
= rm
->next
)
1136 heads
[nr_heads
++] = rm
;
1139 rc
= transport
->fetch(transport
, nr_heads
, heads
);
1145 void transport_unlock_pack(struct transport
*transport
)
1147 if (transport
->pack_lockfile
) {
1148 unlink_or_warn(transport
->pack_lockfile
);
1149 FREE_AND_NULL(transport
->pack_lockfile
);
1153 int transport_connect(struct transport
*transport
, const char *name
,
1154 const char *exec
, int fd
[2])
1156 if (transport
->connect
)
1157 return transport
->connect(transport
, name
, exec
, fd
);
1159 die("Operation not supported by protocol");
1162 int transport_disconnect(struct transport
*transport
)
1165 if (transport
->disconnect
)
1166 ret
= transport
->disconnect(transport
);
1172 * Strip username (and password) from a URL and return
1173 * it in a newly allocated string.
1175 char *transport_anonymize_url(const char *url
)
1177 char *scheme_prefix
, *anon_part
;
1178 size_t anon_len
, prefix_len
= 0;
1180 anon_part
= strchr(url
, '@');
1181 if (url_is_local_not_ssh(url
) || !anon_part
)
1184 anon_len
= strlen(++anon_part
);
1185 scheme_prefix
= strstr(url
, "://");
1186 if (!scheme_prefix
) {
1187 if (!strchr(anon_part
, ':'))
1188 /* cannot be "me@there:/path/name" */
1192 /* make sure scheme is reasonable */
1193 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1196 case '+': case '.': case '-':
1205 /* @ past the first slash does not count */
1206 cp
= strchr(scheme_prefix
+ 3, '/');
1207 if (cp
&& cp
< anon_part
)
1209 prefix_len
= scheme_prefix
- url
+ 3;
1211 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1212 (int)anon_len
, anon_part
);
1214 return xstrdup(url
);
1217 static void read_alternate_refs(const char *path
,
1218 alternate_ref_fn
*cb
,
1221 struct child_process cmd
= CHILD_PROCESS_INIT
;
1222 struct strbuf line
= STRBUF_INIT
;
1226 argv_array_pushf(&cmd
.args
, "--git-dir=%s", path
);
1227 argv_array_push(&cmd
.args
, "for-each-ref");
1228 argv_array_push(&cmd
.args
, "--format=%(objectname) %(refname)");
1229 cmd
.env
= local_repo_env
;
1232 if (start_command(&cmd
))
1235 fh
= xfdopen(cmd
.out
, "r");
1236 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
1237 struct object_id oid
;
1239 if (get_oid_hex(line
.buf
, &oid
) ||
1240 line
.buf
[GIT_SHA1_HEXSZ
] != ' ') {
1241 warning("invalid line while parsing alternate refs: %s",
1246 cb(line
.buf
+ GIT_SHA1_HEXSZ
+ 1, &oid
, data
);
1250 finish_command(&cmd
);
1253 struct alternate_refs_data
{
1254 alternate_ref_fn
*fn
;
1258 static int refs_from_alternate_cb(struct alternate_object_database
*e
,
1261 struct strbuf path
= STRBUF_INIT
;
1263 struct alternate_refs_data
*cb
= data
;
1265 if (!strbuf_realpath(&path
, e
->path
, 0))
1267 if (!strbuf_strip_suffix(&path
, "/objects"))
1269 base_len
= path
.len
;
1271 /* Is this a git repository with refs? */
1272 strbuf_addstr(&path
, "/refs");
1273 if (!is_directory(path
.buf
))
1275 strbuf_setlen(&path
, base_len
);
1277 read_alternate_refs(path
.buf
, cb
->fn
, cb
->data
);
1280 strbuf_release(&path
);
1284 void for_each_alternate_ref(alternate_ref_fn fn
, void *data
)
1286 struct alternate_refs_data cb
;
1289 foreach_alt_odb(refs_from_alternate_cb
, &cb
);