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
;
31 * Check suitability for tracking. Must be successful /
32 * already up-to-date ref create/modify (not delete).
34 if (ref
->status
!= REF_STATUS_OK
&&
35 ref
->status
!= REF_STATUS_UPTODATE
)
39 if (is_null_oid(&ref
->new_oid
))
42 /* Follow symbolic refs (mainly for HEAD). */
43 localname
= ref
->peer_ref
->name
;
44 remotename
= ref
->name
;
45 tmp
= resolve_ref_unsafe(localname
, RESOLVE_REF_READING
,
47 if (tmp
&& flag
& REF_ISSYMREF
&&
48 starts_with(tmp
, "refs/heads/"))
51 /* Both source and destination must be local branches. */
52 if (!localname
|| !starts_with(localname
, "refs/heads/"))
54 if (!remotename
|| !starts_with(remotename
, "refs/heads/"))
58 install_branch_config(BRANCH_CONFIG_VERBOSE
,
59 localname
+ 11, transport
->remote
->name
,
62 printf(_("Would set upstream of '%s' to '%s' of '%s'\n"),
63 localname
+ 11, remotename
+ 11,
64 transport
->remote
->name
);
68 struct bundle_transport_data
{
70 struct bundle_header header
;
73 static struct ref
*get_refs_from_bundle(struct transport
*transport
, int for_push
)
75 struct bundle_transport_data
*data
= transport
->data
;
76 struct ref
*result
= NULL
;
84 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
86 die ("Could not read bundle '%s'.", transport
->url
);
87 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
88 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
89 struct ref
*ref
= alloc_ref(e
->name
);
90 oidcpy(&ref
->old_oid
, &e
->oid
);
97 static int fetch_refs_from_bundle(struct transport
*transport
,
98 int nr_heads
, struct ref
**to_fetch
)
100 struct bundle_transport_data
*data
= transport
->data
;
101 return unbundle(&data
->header
, data
->fd
,
102 transport
->progress
? BUNDLE_VERBOSE
: 0);
105 static int close_bundle(struct transport
*transport
)
107 struct bundle_transport_data
*data
= transport
->data
;
114 struct git_transport_data
{
115 struct git_transport_options options
;
116 struct child_process
*conn
;
118 unsigned got_remote_heads
: 1;
119 struct oid_array extra_have
;
120 struct oid_array shallow
;
123 static int set_git_option(struct git_transport_options
*opts
,
124 const char *name
, const char *value
)
126 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
127 opts
->uploadpack
= value
;
129 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
130 opts
->receivepack
= value
;
132 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
133 opts
->thin
= !!value
;
135 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
136 opts
->followtags
= !!value
;
138 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
139 opts
->keep
= !!value
;
141 } else if (!strcmp(name
, TRANS_OPT_UPDATE_SHALLOW
)) {
142 opts
->update_shallow
= !!value
;
144 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
149 opts
->depth
= strtol(value
, &end
, 0);
151 die(_("transport: invalid depth option '%s'"), value
);
154 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_SINCE
)) {
155 opts
->deepen_since
= value
;
157 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_NOT
)) {
158 opts
->deepen_not
= (const struct string_list
*)value
;
160 } else if (!strcmp(name
, TRANS_OPT_DEEPEN_RELATIVE
)) {
161 opts
->deepen_relative
= !!value
;
167 static int connect_setup(struct transport
*transport
, int for_push
)
169 struct git_transport_data
*data
= transport
->data
;
170 int flags
= transport
->verbose
> 0 ? CONNECT_VERBOSE
: 0;
175 switch (transport
->family
) {
176 case TRANSPORT_FAMILY_ALL
: break;
177 case TRANSPORT_FAMILY_IPV4
: flags
|= CONNECT_IPV4
; break;
178 case TRANSPORT_FAMILY_IPV6
: flags
|= CONNECT_IPV6
; break;
181 data
->conn
= git_connect(data
->fd
, transport
->url
,
182 for_push
? data
->options
.receivepack
:
183 data
->options
.uploadpack
,
189 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
)
191 struct git_transport_data
*data
= transport
->data
;
194 connect_setup(transport
, for_push
);
195 get_remote_heads(data
->fd
[0], NULL
, 0, &refs
,
196 for_push
? REF_NORMAL
: 0,
199 data
->got_remote_heads
= 1;
204 static int fetch_refs_via_pack(struct transport
*transport
,
205 int nr_heads
, struct ref
**to_fetch
)
208 struct git_transport_data
*data
= transport
->data
;
210 char *dest
= xstrdup(transport
->url
);
211 struct fetch_pack_args args
;
212 struct ref
*refs_tmp
= NULL
;
214 memset(&args
, 0, sizeof(args
));
215 args
.uploadpack
= data
->options
.uploadpack
;
216 args
.keep_pack
= data
->options
.keep
;
218 args
.use_thin_pack
= data
->options
.thin
;
219 args
.include_tag
= data
->options
.followtags
;
220 args
.verbose
= (transport
->verbose
> 1);
221 args
.quiet
= (transport
->verbose
< 0);
222 args
.no_progress
= !transport
->progress
;
223 args
.depth
= data
->options
.depth
;
224 args
.deepen_since
= data
->options
.deepen_since
;
225 args
.deepen_not
= data
->options
.deepen_not
;
226 args
.deepen_relative
= data
->options
.deepen_relative
;
227 args
.check_self_contained_and_connected
=
228 data
->options
.check_self_contained_and_connected
;
229 args
.cloning
= transport
->cloning
;
230 args
.update_shallow
= data
->options
.update_shallow
;
232 if (!data
->got_remote_heads
) {
233 connect_setup(transport
, 0);
234 get_remote_heads(data
->fd
[0], NULL
, 0, &refs_tmp
, 0,
235 NULL
, &data
->shallow
);
236 data
->got_remote_heads
= 1;
239 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
240 refs_tmp
? refs_tmp
: transport
->remote_refs
,
241 dest
, to_fetch
, nr_heads
, &data
->shallow
,
242 &transport
->pack_lockfile
);
245 if (finish_connect(data
->conn
))
248 data
->got_remote_heads
= 0;
249 data
->options
.self_contained_and_connected
=
250 args
.self_contained_and_connected
;
254 if (report_unmatched_refs(to_fetch
, nr_heads
))
263 static int push_had_errors(struct ref
*ref
)
265 for (; ref
; ref
= ref
->next
) {
266 switch (ref
->status
) {
267 case REF_STATUS_NONE
:
268 case REF_STATUS_UPTODATE
:
278 int transport_refs_pushed(struct ref
*ref
)
280 for (; ref
; ref
= ref
->next
) {
281 switch(ref
->status
) {
282 case REF_STATUS_NONE
:
283 case REF_STATUS_UPTODATE
:
292 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
296 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
302 if (!remote_find_tracking(remote
, &rs
)) {
304 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
306 delete_ref(NULL
, rs
.dst
, NULL
, 0);
308 update_ref("update by push", rs
.dst
, &ref
->new_oid
,
314 static void print_ref_status(char flag
, const char *summary
,
315 struct ref
*to
, struct ref
*from
, const char *msg
,
316 int porcelain
, int summary_width
)
320 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
322 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
324 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
326 fprintf(stdout
, "%s\n", summary
);
328 fprintf(stderr
, " %c %-*s ", flag
, summary_width
, summary
);
330 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
332 fputs(prettify_refname(to
->name
), stderr
);
342 static void print_ok_ref_status(struct ref
*ref
, int porcelain
, int summary_width
)
345 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
346 porcelain
, summary_width
);
347 else if (is_null_oid(&ref
->old_oid
))
348 print_ref_status('*',
349 (starts_with(ref
->name
, "refs/tags/") ? "[new tag]" :
351 ref
, ref
->peer_ref
, NULL
, porcelain
, summary_width
);
353 struct strbuf quickref
= STRBUF_INIT
;
357 strbuf_add_unique_abbrev(&quickref
, ref
->old_oid
.hash
,
359 if (ref
->forced_update
) {
360 strbuf_addstr(&quickref
, "...");
362 msg
= "forced update";
364 strbuf_addstr(&quickref
, "..");
368 strbuf_add_unique_abbrev(&quickref
, ref
->new_oid
.hash
,
371 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
372 porcelain
, summary_width
);
373 strbuf_release(&quickref
);
377 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
378 int porcelain
, int summary_width
)
381 char *url
= transport_anonymize_url(dest
);
382 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
386 switch(ref
->status
) {
387 case REF_STATUS_NONE
:
388 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
389 porcelain
, summary_width
);
391 case REF_STATUS_REJECT_NODELETE
:
392 print_ref_status('!', "[rejected]", ref
, NULL
,
393 "remote does not support deleting refs",
394 porcelain
, summary_width
);
396 case REF_STATUS_UPTODATE
:
397 print_ref_status('=', "[up to date]", ref
,
398 ref
->peer_ref
, NULL
, porcelain
, summary_width
);
400 case REF_STATUS_REJECT_NONFASTFORWARD
:
401 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
402 "non-fast-forward", porcelain
, summary_width
);
404 case REF_STATUS_REJECT_ALREADY_EXISTS
:
405 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
406 "already exists", porcelain
, summary_width
);
408 case REF_STATUS_REJECT_FETCH_FIRST
:
409 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
410 "fetch first", porcelain
, summary_width
);
412 case REF_STATUS_REJECT_NEEDS_FORCE
:
413 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
414 "needs force", porcelain
, summary_width
);
416 case REF_STATUS_REJECT_STALE
:
417 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
418 "stale info", porcelain
, summary_width
);
420 case REF_STATUS_REJECT_SHALLOW
:
421 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
422 "new shallow roots not allowed",
423 porcelain
, summary_width
);
425 case REF_STATUS_REMOTE_REJECT
:
426 print_ref_status('!', "[remote rejected]", ref
,
427 ref
->deletion
? NULL
: ref
->peer_ref
,
428 ref
->remote_status
, porcelain
, summary_width
);
430 case REF_STATUS_EXPECTING_REPORT
:
431 print_ref_status('!', "[remote failure]", ref
,
432 ref
->deletion
? NULL
: ref
->peer_ref
,
433 "remote failed to report status",
434 porcelain
, summary_width
);
436 case REF_STATUS_ATOMIC_PUSH_FAILED
:
437 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
438 "atomic push failed", porcelain
, summary_width
);
441 print_ok_ref_status(ref
, porcelain
, summary_width
);
448 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
450 char hex
[GIT_MAX_HEXSZ
+ 1];
451 int w
= find_unique_abbrev_r(hex
, oid
->hash
, DEFAULT_ABBREV
);
453 return (w
< sofar
) ? sofar
: w
;
456 int transport_summary_width(const struct ref
*refs
)
460 for (; refs
; refs
= refs
->next
) {
461 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
462 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
465 maxw
= FALLBACK_DEFAULT_ABBREV
;
466 return (2 * maxw
+ 3);
469 void transport_print_push_status(const char *dest
, struct ref
*refs
,
470 int verbose
, int porcelain
, unsigned int *reject_reasons
)
475 int summary_width
= transport_summary_width(refs
);
477 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, NULL
, NULL
);
480 for (ref
= refs
; ref
; ref
= ref
->next
)
481 if (ref
->status
== REF_STATUS_UPTODATE
)
482 n
+= print_one_push_status(ref
, dest
, n
,
483 porcelain
, summary_width
);
486 for (ref
= refs
; ref
; ref
= ref
->next
)
487 if (ref
->status
== REF_STATUS_OK
)
488 n
+= print_one_push_status(ref
, dest
, n
,
489 porcelain
, summary_width
);
492 for (ref
= refs
; ref
; ref
= ref
->next
) {
493 if (ref
->status
!= REF_STATUS_NONE
&&
494 ref
->status
!= REF_STATUS_UPTODATE
&&
495 ref
->status
!= REF_STATUS_OK
)
496 n
+= print_one_push_status(ref
, dest
, n
,
497 porcelain
, summary_width
);
498 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
499 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
500 *reject_reasons
|= REJECT_NON_FF_HEAD
;
502 *reject_reasons
|= REJECT_NON_FF_OTHER
;
503 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
504 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
505 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
506 *reject_reasons
|= REJECT_FETCH_FIRST
;
507 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
508 *reject_reasons
|= REJECT_NEEDS_FORCE
;
514 void transport_verify_remote_names(int nr_heads
, const char **heads
)
518 for (i
= 0; i
< nr_heads
; i
++) {
519 const char *local
= heads
[i
];
520 const char *remote
= strrchr(heads
[i
], ':');
525 /* A matching refspec is okay. */
526 if (remote
== local
&& remote
[1] == '\0')
529 remote
= remote
? (remote
+ 1) : local
;
530 if (check_refname_format(remote
,
531 REFNAME_ALLOW_ONELEVEL
|REFNAME_REFSPEC_PATTERN
))
532 die("remote part of refspec is not a valid name in %s",
537 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
539 struct git_transport_data
*data
= transport
->data
;
540 struct send_pack_args args
;
543 if (!data
->got_remote_heads
) {
544 struct ref
*tmp_refs
;
545 connect_setup(transport
, 1);
547 get_remote_heads(data
->fd
[0], NULL
, 0, &tmp_refs
, REF_NORMAL
,
548 NULL
, &data
->shallow
);
549 data
->got_remote_heads
= 1;
552 memset(&args
, 0, sizeof(args
));
553 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
554 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
555 args
.use_thin_pack
= data
->options
.thin
;
556 args
.verbose
= (transport
->verbose
> 0);
557 args
.quiet
= (transport
->verbose
< 0);
558 args
.progress
= transport
->progress
;
559 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
560 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
561 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
562 args
.push_options
= transport
->push_options
;
563 args
.url
= transport
->url
;
565 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
566 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
567 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
568 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
570 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
572 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
577 ret
|= finish_connect(data
->conn
);
579 data
->got_remote_heads
= 0;
584 static int connect_git(struct transport
*transport
, const char *name
,
585 const char *executable
, int fd
[2])
587 struct git_transport_data
*data
= transport
->data
;
588 data
->conn
= git_connect(data
->fd
, transport
->url
,
595 static int disconnect_git(struct transport
*transport
)
597 struct git_transport_data
*data
= transport
->data
;
599 if (data
->got_remote_heads
)
600 packet_flush(data
->fd
[1]);
603 finish_connect(data
->conn
);
610 void transport_take_over(struct transport
*transport
,
611 struct child_process
*child
)
613 struct git_transport_data
*data
;
615 if (!transport
->smart_options
)
616 die("BUG: taking over transport requires non-NULL "
617 "smart_options field.");
619 data
= xcalloc(1, sizeof(*data
));
620 data
->options
= *transport
->smart_options
;
622 data
->fd
[0] = data
->conn
->out
;
623 data
->fd
[1] = data
->conn
->in
;
624 data
->got_remote_heads
= 0;
625 transport
->data
= data
;
627 transport
->set_option
= NULL
;
628 transport
->get_refs_list
= get_refs_via_connect
;
629 transport
->fetch
= fetch_refs_via_pack
;
630 transport
->push_refs
= git_transport_push
;
631 transport
->disconnect
= disconnect_git
;
632 transport
->smart_options
= &(data
->options
);
634 transport
->cannot_reuse
= 1;
637 static int is_file(const char *url
)
642 return S_ISREG(buf
.st_mode
);
645 static int external_specification_len(const char *url
)
647 return strchr(url
, ':') - url
;
650 static const struct string_list
*protocol_whitelist(void)
652 static int enabled
= -1;
653 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
656 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
658 string_list_split(&allowed
, v
, ':', -1);
659 string_list_sort(&allowed
);
666 return enabled
? &allowed
: NULL
;
669 enum protocol_allow_config
{
670 PROTOCOL_ALLOW_NEVER
= 0,
671 PROTOCOL_ALLOW_USER_ONLY
,
672 PROTOCOL_ALLOW_ALWAYS
675 static enum protocol_allow_config
parse_protocol_config(const char *key
,
678 if (!strcasecmp(value
, "always"))
679 return PROTOCOL_ALLOW_ALWAYS
;
680 else if (!strcasecmp(value
, "never"))
681 return PROTOCOL_ALLOW_NEVER
;
682 else if (!strcasecmp(value
, "user"))
683 return PROTOCOL_ALLOW_USER_ONLY
;
685 die("unknown value for config '%s': %s", key
, value
);
688 static enum protocol_allow_config
get_protocol_config(const char *type
)
690 char *key
= xstrfmt("protocol.%s.allow", type
);
693 /* first check the per-protocol config */
694 if (!git_config_get_string(key
, &value
)) {
695 enum protocol_allow_config ret
=
696 parse_protocol_config(key
, value
);
703 /* if defined, fallback to user-defined default for unknown protocols */
704 if (!git_config_get_string("protocol.allow", &value
)) {
705 enum protocol_allow_config ret
=
706 parse_protocol_config("protocol.allow", value
);
711 /* fallback to built-in defaults */
713 if (!strcmp(type
, "http") ||
714 !strcmp(type
, "https") ||
715 !strcmp(type
, "git") ||
716 !strcmp(type
, "ssh") ||
717 !strcmp(type
, "file"))
718 return PROTOCOL_ALLOW_ALWAYS
;
720 /* known scary; err on the side of caution */
721 if (!strcmp(type
, "ext"))
722 return PROTOCOL_ALLOW_NEVER
;
724 /* unknown; by default let them be used only directly by the user */
725 return PROTOCOL_ALLOW_USER_ONLY
;
728 int is_transport_allowed(const char *type
, int from_user
)
730 const struct string_list
*whitelist
= protocol_whitelist();
732 return string_list_has_string(whitelist
, type
);
734 switch (get_protocol_config(type
)) {
735 case PROTOCOL_ALLOW_ALWAYS
:
737 case PROTOCOL_ALLOW_NEVER
:
739 case PROTOCOL_ALLOW_USER_ONLY
:
741 from_user
= git_env_bool("GIT_PROTOCOL_FROM_USER", 1);
745 die("BUG: invalid protocol_allow_config type");
748 void transport_check_allowed(const char *type
)
750 if (!is_transport_allowed(type
, -1))
751 die("transport '%s' not allowed", type
);
754 struct transport
*transport_get(struct remote
*remote
, const char *url
)
757 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
759 ret
->progress
= isatty(2);
762 die("No remote provided to transport_get()");
764 ret
->got_remote_refs
= 0;
765 ret
->remote
= remote
;
766 helper
= remote
->foreign_vcs
;
768 if (!url
&& remote
->url
)
769 url
= remote
->url
[0];
772 /* maybe it is a foreign URL? */
776 while (is_urlschemechar(p
== url
, *p
))
778 if (starts_with(p
, "::"))
779 helper
= xstrndup(url
, p
- url
);
783 transport_helper_init(ret
, helper
);
784 } else if (starts_with(url
, "rsync:")) {
785 die("git-over-rsync is no longer supported");
786 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
787 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
788 transport_check_allowed("file");
790 ret
->get_refs_list
= get_refs_from_bundle
;
791 ret
->fetch
= fetch_refs_from_bundle
;
792 ret
->disconnect
= close_bundle
;
793 ret
->smart_options
= NULL
;
794 } else if (!is_url(url
)
795 || starts_with(url
, "file://")
796 || starts_with(url
, "git://")
797 || starts_with(url
, "ssh://")
798 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
799 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
802 * These are builtin smart transports; "allowed" transports
803 * will be checked individually in git_connect.
805 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
807 ret
->set_option
= NULL
;
808 ret
->get_refs_list
= get_refs_via_connect
;
809 ret
->fetch
= fetch_refs_via_pack
;
810 ret
->push_refs
= git_transport_push
;
811 ret
->connect
= connect_git
;
812 ret
->disconnect
= disconnect_git
;
813 ret
->smart_options
= &(data
->options
);
816 data
->got_remote_heads
= 0;
818 /* Unknown protocol in URL. Pass to external handler. */
819 int len
= external_specification_len(url
);
820 char *handler
= xmemdupz(url
, len
);
821 transport_helper_init(ret
, handler
);
824 if (ret
->smart_options
) {
825 ret
->smart_options
->thin
= 1;
826 ret
->smart_options
->uploadpack
= "git-upload-pack";
827 if (remote
->uploadpack
)
828 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
829 ret
->smart_options
->receivepack
= "git-receive-pack";
830 if (remote
->receivepack
)
831 ret
->smart_options
->receivepack
= remote
->receivepack
;
837 int transport_set_option(struct transport
*transport
,
838 const char *name
, const char *value
)
840 int git_reports
= 1, protocol_reports
= 1;
842 if (transport
->smart_options
)
843 git_reports
= set_git_option(transport
->smart_options
,
846 if (transport
->set_option
)
847 protocol_reports
= transport
->set_option(transport
, name
,
850 /* If either report is 0, report 0 (success). */
851 if (!git_reports
|| !protocol_reports
)
853 /* If either reports -1 (invalid value), report -1. */
854 if ((git_reports
== -1) || (protocol_reports
== -1))
856 /* Otherwise if both report unknown, report unknown. */
860 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
864 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
866 transport
->verbose
= -1;
869 * Rules used to determine whether to report progress (processing aborts
870 * when a rule is satisfied):
872 * . Report progress, if force_progress is 1 (ie. --progress).
873 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
874 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
875 * . Report progress if isatty(2) is 1.
877 if (force_progress
>= 0)
878 transport
->progress
= !!force_progress
;
880 transport
->progress
= verbosity
>= 0 && isatty(2);
883 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
887 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
888 "not be found on any remote:\n"));
889 for (i
= 0; i
< needs_pushing
->nr
; i
++)
890 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
891 fprintf(stderr
, _("\nPlease try\n\n"
892 " git push --recurse-submodules=on-demand\n\n"
893 "or cd to the path and use\n\n"
895 "to push them to a remote.\n\n"));
897 string_list_clear(needs_pushing
, 0);
902 static int run_pre_push_hook(struct transport
*transport
,
903 struct ref
*remote_refs
)
907 struct child_process proc
= CHILD_PROCESS_INIT
;
911 if (!(argv
[0] = find_hook("pre-push")))
914 argv
[1] = transport
->remote
->name
;
915 argv
[2] = transport
->url
;
921 if (start_command(&proc
)) {
922 finish_command(&proc
);
926 sigchain_push(SIGPIPE
, SIG_IGN
);
928 strbuf_init(&buf
, 256);
930 for (r
= remote_refs
; r
; r
= r
->next
) {
931 if (!r
->peer_ref
) continue;
932 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
933 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
934 if (r
->status
== REF_STATUS_UPTODATE
) continue;
937 strbuf_addf( &buf
, "%s %s %s %s\n",
938 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
939 r
->name
, oid_to_hex(&r
->old_oid
));
941 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
942 /* We do not mind if a hook does not read all refs. */
949 strbuf_release(&buf
);
955 sigchain_pop(SIGPIPE
);
957 x
= finish_command(&proc
);
964 int transport_push(struct transport
*transport
,
965 int refspec_nr
, const char **refspec
, int flags
,
966 unsigned int *reject_reasons
)
969 transport_verify_remote_names(refspec_nr
, refspec
);
971 if (transport
->push_refs
) {
972 struct ref
*remote_refs
;
973 struct ref
*local_refs
= get_local_heads();
974 int match_flags
= MATCH_REFS_NONE
;
975 int verbose
= (transport
->verbose
> 0);
976 int quiet
= (transport
->verbose
< 0);
977 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
978 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
979 int push_ret
, ret
, err
;
981 if (check_push_refs(local_refs
, refspec_nr
, refspec
) < 0)
984 remote_refs
= transport
->get_refs_list(transport
, 1);
986 if (flags
& TRANSPORT_PUSH_ALL
)
987 match_flags
|= MATCH_REFS_ALL
;
988 if (flags
& TRANSPORT_PUSH_MIRROR
)
989 match_flags
|= MATCH_REFS_MIRROR
;
990 if (flags
& TRANSPORT_PUSH_PRUNE
)
991 match_flags
|= MATCH_REFS_PRUNE
;
992 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
993 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
995 if (match_push_refs(local_refs
, &remote_refs
,
996 refspec_nr
, refspec
, match_flags
)) {
1000 if (transport
->smart_options
&&
1001 transport
->smart_options
->cas
&&
1002 !is_empty_cas(transport
->smart_options
->cas
))
1003 apply_push_cas(transport
->smart_options
->cas
,
1004 transport
->remote
, remote_refs
);
1006 set_ref_status_for_push(remote_refs
,
1007 flags
& TRANSPORT_PUSH_MIRROR
,
1008 flags
& TRANSPORT_PUSH_FORCE
);
1010 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
1011 if (run_pre_push_hook(transport
, remote_refs
))
1014 if ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1015 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1016 !is_bare_repository()) {
1017 struct ref
*ref
= remote_refs
;
1018 struct oid_array commits
= OID_ARRAY_INIT
;
1020 for (; ref
; ref
= ref
->next
)
1021 if (!is_null_oid(&ref
->new_oid
))
1022 oid_array_append(&commits
,
1025 if (!push_unpushed_submodules(&commits
,
1027 refspec
, refspec_nr
,
1028 transport
->push_options
,
1030 oid_array_clear(&commits
);
1031 die("Failed to push all needed submodules!");
1033 oid_array_clear(&commits
);
1036 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
1037 ((flags
& (TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
|
1038 TRANSPORT_RECURSE_SUBMODULES_ONLY
)) &&
1039 !pretend
)) && !is_bare_repository()) {
1040 struct ref
*ref
= remote_refs
;
1041 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
1042 struct oid_array commits
= OID_ARRAY_INIT
;
1044 for (; ref
; ref
= ref
->next
)
1045 if (!is_null_oid(&ref
->new_oid
))
1046 oid_array_append(&commits
,
1049 if (find_unpushed_submodules(&commits
, transport
->remote
->name
,
1051 oid_array_clear(&commits
);
1052 die_with_unpushed_submodules(&needs_pushing
);
1054 string_list_clear(&needs_pushing
, 0);
1055 oid_array_clear(&commits
);
1058 if (!(flags
& TRANSPORT_RECURSE_SUBMODULES_ONLY
))
1059 push_ret
= transport
->push_refs(transport
, remote_refs
, flags
);
1062 err
= push_had_errors(remote_refs
);
1063 ret
= push_ret
| err
;
1066 transport_print_push_status(transport
->url
, remote_refs
,
1067 verbose
| porcelain
, porcelain
,
1070 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1071 set_upstreams(transport
, remote_refs
, pretend
);
1073 if (!(flags
& (TRANSPORT_PUSH_DRY_RUN
|
1074 TRANSPORT_RECURSE_SUBMODULES_ONLY
))) {
1076 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1077 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1080 if (porcelain
&& !push_ret
)
1082 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1083 fprintf(stderr
, "Everything up-to-date\n");
1090 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
1092 if (!transport
->got_remote_refs
) {
1093 transport
->remote_refs
= transport
->get_refs_list(transport
, 0);
1094 transport
->got_remote_refs
= 1;
1097 return transport
->remote_refs
;
1100 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1103 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1104 struct ref
**heads
= NULL
;
1107 for (rm
= refs
; rm
; rm
= rm
->next
) {
1110 !is_null_oid(&rm
->old_oid
) &&
1111 !oidcmp(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1113 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1114 heads
[nr_heads
++] = rm
;
1119 * When deepening of a shallow repository is requested,
1120 * then local and remote refs are likely to still be equal.
1121 * Just feed them all to the fetch method in that case.
1122 * This condition shouldn't be met in a non-deepening fetch
1123 * (see builtin/fetch.c:quickfetch()).
1125 ALLOC_ARRAY(heads
, nr_refs
);
1126 for (rm
= refs
; rm
; rm
= rm
->next
)
1127 heads
[nr_heads
++] = rm
;
1130 rc
= transport
->fetch(transport
, nr_heads
, heads
);
1136 void transport_unlock_pack(struct transport
*transport
)
1138 if (transport
->pack_lockfile
) {
1139 unlink_or_warn(transport
->pack_lockfile
);
1140 FREE_AND_NULL(transport
->pack_lockfile
);
1144 int transport_connect(struct transport
*transport
, const char *name
,
1145 const char *exec
, int fd
[2])
1147 if (transport
->connect
)
1148 return transport
->connect(transport
, name
, exec
, fd
);
1150 die("Operation not supported by protocol");
1153 int transport_disconnect(struct transport
*transport
)
1156 if (transport
->disconnect
)
1157 ret
= transport
->disconnect(transport
);
1163 * Strip username (and password) from a URL and return
1164 * it in a newly allocated string.
1166 char *transport_anonymize_url(const char *url
)
1168 char *scheme_prefix
, *anon_part
;
1169 size_t anon_len
, prefix_len
= 0;
1171 anon_part
= strchr(url
, '@');
1172 if (url_is_local_not_ssh(url
) || !anon_part
)
1175 anon_len
= strlen(++anon_part
);
1176 scheme_prefix
= strstr(url
, "://");
1177 if (!scheme_prefix
) {
1178 if (!strchr(anon_part
, ':'))
1179 /* cannot be "me@there:/path/name" */
1183 /* make sure scheme is reasonable */
1184 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1187 case '+': case '.': case '-':
1196 /* @ past the first slash does not count */
1197 cp
= strchr(scheme_prefix
+ 3, '/');
1198 if (cp
&& cp
< anon_part
)
1200 prefix_len
= scheme_prefix
- url
+ 3;
1202 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1203 (int)anon_len
, anon_part
);
1205 return xstrdup(url
);
1208 static void read_alternate_refs(const char *path
,
1209 alternate_ref_fn
*cb
,
1212 struct child_process cmd
= CHILD_PROCESS_INIT
;
1213 struct strbuf line
= STRBUF_INIT
;
1217 argv_array_pushf(&cmd
.args
, "--git-dir=%s", path
);
1218 argv_array_push(&cmd
.args
, "for-each-ref");
1219 argv_array_push(&cmd
.args
, "--format=%(objectname) %(refname)");
1220 cmd
.env
= local_repo_env
;
1223 if (start_command(&cmd
))
1226 fh
= xfdopen(cmd
.out
, "r");
1227 while (strbuf_getline_lf(&line
, fh
) != EOF
) {
1228 struct object_id oid
;
1230 if (get_oid_hex(line
.buf
, &oid
) ||
1231 line
.buf
[GIT_SHA1_HEXSZ
] != ' ') {
1232 warning("invalid line while parsing alternate refs: %s",
1237 cb(line
.buf
+ GIT_SHA1_HEXSZ
+ 1, &oid
, data
);
1241 finish_command(&cmd
);
1244 struct alternate_refs_data
{
1245 alternate_ref_fn
*fn
;
1249 static int refs_from_alternate_cb(struct alternate_object_database
*e
,
1252 struct strbuf path
= STRBUF_INIT
;
1254 struct alternate_refs_data
*cb
= data
;
1256 if (!strbuf_realpath(&path
, e
->path
, 0))
1258 if (!strbuf_strip_suffix(&path
, "/objects"))
1260 base_len
= path
.len
;
1262 /* Is this a git repository with refs? */
1263 strbuf_addstr(&path
, "/refs");
1264 if (!is_directory(path
.buf
))
1266 strbuf_setlen(&path
, base_len
);
1268 read_alternate_refs(path
.buf
, cb
->fn
, cb
->data
);
1271 strbuf_release(&path
);
1275 void for_each_alternate_ref(alternate_ref_fn fn
, void *data
)
1277 struct alternate_refs_data cb
;
1280 foreach_alt_odb(refs_from_alternate_cb
, &cb
);