3 #include "run-command.h"
5 #include "fetch-pack.h"
15 #include "submodule.h"
16 #include "string-list.h"
17 #include "sha1-array.h"
20 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
24 for (ref
= refs
; ref
; ref
= ref
->next
) {
25 const char *localname
;
27 const char *remotename
;
28 unsigned char sha
[20];
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 hashcpy(ref
->old_oid
.hash
, e
->sha1
);
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 sha1_array extra_have
;
120 struct sha1_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
)
207 struct git_transport_data
*data
= transport
->data
;
209 char *dest
= xstrdup(transport
->url
);
210 struct fetch_pack_args args
;
211 struct ref
*refs_tmp
= NULL
;
213 memset(&args
, 0, sizeof(args
));
214 args
.uploadpack
= data
->options
.uploadpack
;
215 args
.keep_pack
= data
->options
.keep
;
217 args
.use_thin_pack
= data
->options
.thin
;
218 args
.include_tag
= data
->options
.followtags
;
219 args
.verbose
= (transport
->verbose
> 1);
220 args
.quiet
= (transport
->verbose
< 0);
221 args
.no_progress
= !transport
->progress
;
222 args
.depth
= data
->options
.depth
;
223 args
.deepen_since
= data
->options
.deepen_since
;
224 args
.deepen_not
= data
->options
.deepen_not
;
225 args
.deepen_relative
= data
->options
.deepen_relative
;
226 args
.check_self_contained_and_connected
=
227 data
->options
.check_self_contained_and_connected
;
228 args
.cloning
= transport
->cloning
;
229 args
.update_shallow
= data
->options
.update_shallow
;
231 if (!data
->got_remote_heads
) {
232 connect_setup(transport
, 0);
233 get_remote_heads(data
->fd
[0], NULL
, 0, &refs_tmp
, 0,
234 NULL
, &data
->shallow
);
235 data
->got_remote_heads
= 1;
238 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
239 refs_tmp
? refs_tmp
: transport
->remote_refs
,
240 dest
, to_fetch
, nr_heads
, &data
->shallow
,
241 &transport
->pack_lockfile
);
244 if (finish_connect(data
->conn
)) {
249 data
->got_remote_heads
= 0;
250 data
->options
.self_contained_and_connected
=
251 args
.self_contained_and_connected
;
256 return (refs
? 0 : -1);
259 static int push_had_errors(struct ref
*ref
)
261 for (; ref
; ref
= ref
->next
) {
262 switch (ref
->status
) {
263 case REF_STATUS_NONE
:
264 case REF_STATUS_UPTODATE
:
274 int transport_refs_pushed(struct ref
*ref
)
276 for (; ref
; ref
= ref
->next
) {
277 switch(ref
->status
) {
278 case REF_STATUS_NONE
:
279 case REF_STATUS_UPTODATE
:
288 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
292 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
298 if (!remote_find_tracking(remote
, &rs
)) {
300 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
302 delete_ref(rs
.dst
, NULL
, 0);
304 update_ref("update by push", rs
.dst
,
305 ref
->new_oid
.hash
, NULL
, 0, 0);
310 static void print_ref_status(char flag
, const char *summary
,
311 struct ref
*to
, struct ref
*from
, const char *msg
,
312 int porcelain
, int summary_width
)
316 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
318 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
320 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
322 fprintf(stdout
, "%s\n", summary
);
324 fprintf(stderr
, " %c %-*s ", flag
, summary_width
, summary
);
326 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
328 fputs(prettify_refname(to
->name
), stderr
);
338 static void print_ok_ref_status(struct ref
*ref
, int porcelain
, int summary_width
)
341 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
,
342 porcelain
, summary_width
);
343 else if (is_null_oid(&ref
->old_oid
))
344 print_ref_status('*',
345 (starts_with(ref
->name
, "refs/tags/") ? "[new tag]" :
347 ref
, ref
->peer_ref
, NULL
, porcelain
, summary_width
);
349 struct strbuf quickref
= STRBUF_INIT
;
353 strbuf_add_unique_abbrev(&quickref
, ref
->old_oid
.hash
,
355 if (ref
->forced_update
) {
356 strbuf_addstr(&quickref
, "...");
358 msg
= "forced update";
360 strbuf_addstr(&quickref
, "..");
364 strbuf_add_unique_abbrev(&quickref
, ref
->new_oid
.hash
,
367 print_ref_status(type
, quickref
.buf
, ref
, ref
->peer_ref
, msg
,
368 porcelain
, summary_width
);
369 strbuf_release(&quickref
);
373 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
,
374 int porcelain
, int summary_width
)
377 char *url
= transport_anonymize_url(dest
);
378 fprintf(porcelain
? stdout
: stderr
, "To %s\n", url
);
382 switch(ref
->status
) {
383 case REF_STATUS_NONE
:
384 print_ref_status('X', "[no match]", ref
, NULL
, NULL
,
385 porcelain
, summary_width
);
387 case REF_STATUS_REJECT_NODELETE
:
388 print_ref_status('!', "[rejected]", ref
, NULL
,
389 "remote does not support deleting refs",
390 porcelain
, summary_width
);
392 case REF_STATUS_UPTODATE
:
393 print_ref_status('=', "[up to date]", ref
,
394 ref
->peer_ref
, NULL
, porcelain
, summary_width
);
396 case REF_STATUS_REJECT_NONFASTFORWARD
:
397 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
398 "non-fast-forward", porcelain
, summary_width
);
400 case REF_STATUS_REJECT_ALREADY_EXISTS
:
401 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
402 "already exists", porcelain
, summary_width
);
404 case REF_STATUS_REJECT_FETCH_FIRST
:
405 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
406 "fetch first", porcelain
, summary_width
);
408 case REF_STATUS_REJECT_NEEDS_FORCE
:
409 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
410 "needs force", porcelain
, summary_width
);
412 case REF_STATUS_REJECT_STALE
:
413 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
414 "stale info", porcelain
, summary_width
);
416 case REF_STATUS_REJECT_SHALLOW
:
417 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
418 "new shallow roots not allowed",
419 porcelain
, summary_width
);
421 case REF_STATUS_REMOTE_REJECT
:
422 print_ref_status('!', "[remote rejected]", ref
,
423 ref
->deletion
? NULL
: ref
->peer_ref
,
424 ref
->remote_status
, porcelain
, summary_width
);
426 case REF_STATUS_EXPECTING_REPORT
:
427 print_ref_status('!', "[remote failure]", ref
,
428 ref
->deletion
? NULL
: ref
->peer_ref
,
429 "remote failed to report status",
430 porcelain
, summary_width
);
432 case REF_STATUS_ATOMIC_PUSH_FAILED
:
433 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
434 "atomic push failed", porcelain
, summary_width
);
437 print_ok_ref_status(ref
, porcelain
, summary_width
);
444 static int measure_abbrev(const struct object_id
*oid
, int sofar
)
446 char hex
[GIT_SHA1_HEXSZ
+ 1];
447 int w
= find_unique_abbrev_r(hex
, oid
->hash
, DEFAULT_ABBREV
);
449 return (w
< sofar
) ? sofar
: w
;
452 int transport_summary_width(const struct ref
*refs
)
456 for (; refs
; refs
= refs
->next
) {
457 maxw
= measure_abbrev(&refs
->old_oid
, maxw
);
458 maxw
= measure_abbrev(&refs
->new_oid
, maxw
);
461 maxw
= FALLBACK_DEFAULT_ABBREV
;
462 return (2 * maxw
+ 3);
465 void transport_print_push_status(const char *dest
, struct ref
*refs
,
466 int verbose
, int porcelain
, unsigned int *reject_reasons
)
470 unsigned char head_sha1
[20];
472 int summary_width
= transport_summary_width(refs
);
474 head
= resolve_refdup("HEAD", RESOLVE_REF_READING
, head_sha1
, NULL
);
477 for (ref
= refs
; ref
; ref
= ref
->next
)
478 if (ref
->status
== REF_STATUS_UPTODATE
)
479 n
+= print_one_push_status(ref
, dest
, n
,
480 porcelain
, summary_width
);
483 for (ref
= refs
; ref
; ref
= ref
->next
)
484 if (ref
->status
== REF_STATUS_OK
)
485 n
+= print_one_push_status(ref
, dest
, n
,
486 porcelain
, summary_width
);
489 for (ref
= refs
; ref
; ref
= ref
->next
) {
490 if (ref
->status
!= REF_STATUS_NONE
&&
491 ref
->status
!= REF_STATUS_UPTODATE
&&
492 ref
->status
!= REF_STATUS_OK
)
493 n
+= print_one_push_status(ref
, dest
, n
,
494 porcelain
, summary_width
);
495 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) {
496 if (head
!= NULL
&& !strcmp(head
, ref
->name
))
497 *reject_reasons
|= REJECT_NON_FF_HEAD
;
499 *reject_reasons
|= REJECT_NON_FF_OTHER
;
500 } else if (ref
->status
== REF_STATUS_REJECT_ALREADY_EXISTS
) {
501 *reject_reasons
|= REJECT_ALREADY_EXISTS
;
502 } else if (ref
->status
== REF_STATUS_REJECT_FETCH_FIRST
) {
503 *reject_reasons
|= REJECT_FETCH_FIRST
;
504 } else if (ref
->status
== REF_STATUS_REJECT_NEEDS_FORCE
) {
505 *reject_reasons
|= REJECT_NEEDS_FORCE
;
511 void transport_verify_remote_names(int nr_heads
, const char **heads
)
515 for (i
= 0; i
< nr_heads
; i
++) {
516 const char *local
= heads
[i
];
517 const char *remote
= strrchr(heads
[i
], ':');
522 /* A matching refspec is okay. */
523 if (remote
== local
&& remote
[1] == '\0')
526 remote
= remote
? (remote
+ 1) : local
;
527 if (check_refname_format(remote
,
528 REFNAME_ALLOW_ONELEVEL
|REFNAME_REFSPEC_PATTERN
))
529 die("remote part of refspec is not a valid name in %s",
534 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
536 struct git_transport_data
*data
= transport
->data
;
537 struct send_pack_args args
;
540 if (!data
->got_remote_heads
) {
541 struct ref
*tmp_refs
;
542 connect_setup(transport
, 1);
544 get_remote_heads(data
->fd
[0], NULL
, 0, &tmp_refs
, REF_NORMAL
,
545 NULL
, &data
->shallow
);
546 data
->got_remote_heads
= 1;
549 memset(&args
, 0, sizeof(args
));
550 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
551 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
552 args
.use_thin_pack
= data
->options
.thin
;
553 args
.verbose
= (transport
->verbose
> 0);
554 args
.quiet
= (transport
->verbose
< 0);
555 args
.progress
= transport
->progress
;
556 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
557 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
558 args
.atomic
= !!(flags
& TRANSPORT_PUSH_ATOMIC
);
559 args
.push_options
= transport
->push_options
;
560 args
.url
= transport
->url
;
562 if (flags
& TRANSPORT_PUSH_CERT_ALWAYS
)
563 args
.push_cert
= SEND_PACK_PUSH_CERT_ALWAYS
;
564 else if (flags
& TRANSPORT_PUSH_CERT_IF_ASKED
)
565 args
.push_cert
= SEND_PACK_PUSH_CERT_IF_ASKED
;
567 args
.push_cert
= SEND_PACK_PUSH_CERT_NEVER
;
569 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
574 ret
|= finish_connect(data
->conn
);
576 data
->got_remote_heads
= 0;
581 static int connect_git(struct transport
*transport
, const char *name
,
582 const char *executable
, int fd
[2])
584 struct git_transport_data
*data
= transport
->data
;
585 data
->conn
= git_connect(data
->fd
, transport
->url
,
592 static int disconnect_git(struct transport
*transport
)
594 struct git_transport_data
*data
= transport
->data
;
596 if (data
->got_remote_heads
)
597 packet_flush(data
->fd
[1]);
600 finish_connect(data
->conn
);
607 void transport_take_over(struct transport
*transport
,
608 struct child_process
*child
)
610 struct git_transport_data
*data
;
612 if (!transport
->smart_options
)
613 die("BUG: taking over transport requires non-NULL "
614 "smart_options field.");
616 data
= xcalloc(1, sizeof(*data
));
617 data
->options
= *transport
->smart_options
;
619 data
->fd
[0] = data
->conn
->out
;
620 data
->fd
[1] = data
->conn
->in
;
621 data
->got_remote_heads
= 0;
622 transport
->data
= data
;
624 transport
->set_option
= NULL
;
625 transport
->get_refs_list
= get_refs_via_connect
;
626 transport
->fetch
= fetch_refs_via_pack
;
627 transport
->push
= NULL
;
628 transport
->push_refs
= git_transport_push
;
629 transport
->disconnect
= disconnect_git
;
630 transport
->smart_options
= &(data
->options
);
632 transport
->cannot_reuse
= 1;
635 static int is_file(const char *url
)
640 return S_ISREG(buf
.st_mode
);
643 static int external_specification_len(const char *url
)
645 return strchr(url
, ':') - url
;
648 static const struct string_list
*protocol_whitelist(void)
650 static int enabled
= -1;
651 static struct string_list allowed
= STRING_LIST_INIT_DUP
;
654 const char *v
= getenv("GIT_ALLOW_PROTOCOL");
656 string_list_split(&allowed
, v
, ':', -1);
657 string_list_sort(&allowed
);
664 return enabled
? &allowed
: NULL
;
667 int is_transport_allowed(const char *type
)
669 const struct string_list
*allowed
= protocol_whitelist();
670 return !allowed
|| string_list_has_string(allowed
, type
);
673 void transport_check_allowed(const char *type
)
675 if (!is_transport_allowed(type
))
676 die("transport '%s' not allowed", type
);
679 int transport_restrict_protocols(void)
681 return !!protocol_whitelist();
684 struct transport
*transport_get(struct remote
*remote
, const char *url
)
687 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
689 ret
->progress
= isatty(2);
692 die("No remote provided to transport_get()");
694 ret
->got_remote_refs
= 0;
695 ret
->remote
= remote
;
696 helper
= remote
->foreign_vcs
;
698 if (!url
&& remote
->url
)
699 url
= remote
->url
[0];
702 /* maybe it is a foreign URL? */
706 while (is_urlschemechar(p
== url
, *p
))
708 if (starts_with(p
, "::"))
709 helper
= xstrndup(url
, p
- url
);
713 transport_helper_init(ret
, helper
);
714 } else if (starts_with(url
, "rsync:")) {
715 die("git-over-rsync is no longer supported");
716 } else if (url_is_local_not_ssh(url
) && is_file(url
) && is_bundle(url
, 1)) {
717 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
718 transport_check_allowed("file");
720 ret
->get_refs_list
= get_refs_from_bundle
;
721 ret
->fetch
= fetch_refs_from_bundle
;
722 ret
->disconnect
= close_bundle
;
723 ret
->smart_options
= NULL
;
724 } else if (!is_url(url
)
725 || starts_with(url
, "file://")
726 || starts_with(url
, "git://")
727 || starts_with(url
, "ssh://")
728 || starts_with(url
, "git+ssh://") /* deprecated - do not use */
729 || starts_with(url
, "ssh+git://") /* deprecated - do not use */
732 * These are builtin smart transports; "allowed" transports
733 * will be checked individually in git_connect.
735 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
737 ret
->set_option
= NULL
;
738 ret
->get_refs_list
= get_refs_via_connect
;
739 ret
->fetch
= fetch_refs_via_pack
;
740 ret
->push_refs
= git_transport_push
;
741 ret
->connect
= connect_git
;
742 ret
->disconnect
= disconnect_git
;
743 ret
->smart_options
= &(data
->options
);
746 data
->got_remote_heads
= 0;
748 /* Unknown protocol in URL. Pass to external handler. */
749 int len
= external_specification_len(url
);
750 char *handler
= xmemdupz(url
, len
);
751 transport_helper_init(ret
, handler
);
754 if (ret
->smart_options
) {
755 ret
->smart_options
->thin
= 1;
756 ret
->smart_options
->uploadpack
= "git-upload-pack";
757 if (remote
->uploadpack
)
758 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
759 ret
->smart_options
->receivepack
= "git-receive-pack";
760 if (remote
->receivepack
)
761 ret
->smart_options
->receivepack
= remote
->receivepack
;
767 int transport_set_option(struct transport
*transport
,
768 const char *name
, const char *value
)
770 int git_reports
= 1, protocol_reports
= 1;
772 if (transport
->smart_options
)
773 git_reports
= set_git_option(transport
->smart_options
,
776 if (transport
->set_option
)
777 protocol_reports
= transport
->set_option(transport
, name
,
780 /* If either report is 0, report 0 (success). */
781 if (!git_reports
|| !protocol_reports
)
783 /* If either reports -1 (invalid value), report -1. */
784 if ((git_reports
== -1) || (protocol_reports
== -1))
786 /* Otherwise if both report unknown, report unknown. */
790 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
794 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
796 transport
->verbose
= -1;
799 * Rules used to determine whether to report progress (processing aborts
800 * when a rule is satisfied):
802 * . Report progress, if force_progress is 1 (ie. --progress).
803 * . Don't report progress, if force_progress is 0 (ie. --no-progress).
804 * . Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
805 * . Report progress if isatty(2) is 1.
807 if (force_progress
>= 0)
808 transport
->progress
= !!force_progress
;
810 transport
->progress
= verbosity
>= 0 && isatty(2);
813 static void die_with_unpushed_submodules(struct string_list
*needs_pushing
)
817 fprintf(stderr
, _("The following submodule paths contain changes that can\n"
818 "not be found on any remote:\n"));
819 for (i
= 0; i
< needs_pushing
->nr
; i
++)
820 fprintf(stderr
, " %s\n", needs_pushing
->items
[i
].string
);
821 fprintf(stderr
, _("\nPlease try\n\n"
822 " git push --recurse-submodules=on-demand\n\n"
823 "or cd to the path and use\n\n"
825 "to push them to a remote.\n\n"));
827 string_list_clear(needs_pushing
, 0);
832 static int run_pre_push_hook(struct transport
*transport
,
833 struct ref
*remote_refs
)
837 struct child_process proc
= CHILD_PROCESS_INIT
;
841 if (!(argv
[0] = find_hook("pre-push")))
844 argv
[1] = transport
->remote
->name
;
845 argv
[2] = transport
->url
;
851 if (start_command(&proc
)) {
852 finish_command(&proc
);
856 sigchain_push(SIGPIPE
, SIG_IGN
);
858 strbuf_init(&buf
, 256);
860 for (r
= remote_refs
; r
; r
= r
->next
) {
861 if (!r
->peer_ref
) continue;
862 if (r
->status
== REF_STATUS_REJECT_NONFASTFORWARD
) continue;
863 if (r
->status
== REF_STATUS_REJECT_STALE
) continue;
864 if (r
->status
== REF_STATUS_UPTODATE
) continue;
867 strbuf_addf( &buf
, "%s %s %s %s\n",
868 r
->peer_ref
->name
, oid_to_hex(&r
->new_oid
),
869 r
->name
, oid_to_hex(&r
->old_oid
));
871 if (write_in_full(proc
.in
, buf
.buf
, buf
.len
) < 0) {
872 /* We do not mind if a hook does not read all refs. */
879 strbuf_release(&buf
);
885 sigchain_pop(SIGPIPE
);
887 x
= finish_command(&proc
);
894 int transport_push(struct transport
*transport
,
895 int refspec_nr
, const char **refspec
, int flags
,
896 unsigned int *reject_reasons
)
899 transport_verify_remote_names(refspec_nr
, refspec
);
901 if (transport
->push
) {
902 /* Maybe FIXME. But no important transport uses this case. */
903 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
904 die("This transport does not support using --set-upstream");
906 return transport
->push(transport
, refspec_nr
, refspec
, flags
);
907 } else if (transport
->push_refs
) {
908 struct ref
*remote_refs
;
909 struct ref
*local_refs
= get_local_heads();
910 int match_flags
= MATCH_REFS_NONE
;
911 int verbose
= (transport
->verbose
> 0);
912 int quiet
= (transport
->verbose
< 0);
913 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
914 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
915 int push_ret
, ret
, err
;
917 if (check_push_refs(local_refs
, refspec_nr
, refspec
) < 0)
920 remote_refs
= transport
->get_refs_list(transport
, 1);
922 if (flags
& TRANSPORT_PUSH_ALL
)
923 match_flags
|= MATCH_REFS_ALL
;
924 if (flags
& TRANSPORT_PUSH_MIRROR
)
925 match_flags
|= MATCH_REFS_MIRROR
;
926 if (flags
& TRANSPORT_PUSH_PRUNE
)
927 match_flags
|= MATCH_REFS_PRUNE
;
928 if (flags
& TRANSPORT_PUSH_FOLLOW_TAGS
)
929 match_flags
|= MATCH_REFS_FOLLOW_TAGS
;
931 if (match_push_refs(local_refs
, &remote_refs
,
932 refspec_nr
, refspec
, match_flags
)) {
936 if (transport
->smart_options
&&
937 transport
->smart_options
->cas
&&
938 !is_empty_cas(transport
->smart_options
->cas
))
939 apply_push_cas(transport
->smart_options
->cas
,
940 transport
->remote
, remote_refs
);
942 set_ref_status_for_push(remote_refs
,
943 flags
& TRANSPORT_PUSH_MIRROR
,
944 flags
& TRANSPORT_PUSH_FORCE
);
946 if (!(flags
& TRANSPORT_PUSH_NO_HOOK
))
947 if (run_pre_push_hook(transport
, remote_refs
))
950 if ((flags
& TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
) && !is_bare_repository()) {
951 struct ref
*ref
= remote_refs
;
952 struct sha1_array commits
= SHA1_ARRAY_INIT
;
954 for (; ref
; ref
= ref
->next
)
955 if (!is_null_oid(&ref
->new_oid
))
956 sha1_array_append(&commits
, ref
->new_oid
.hash
);
958 if (!push_unpushed_submodules(&commits
,
959 transport
->remote
->name
,
961 sha1_array_clear(&commits
);
962 die("Failed to push all needed submodules!");
964 sha1_array_clear(&commits
);
967 if (((flags
& TRANSPORT_RECURSE_SUBMODULES_CHECK
) ||
968 ((flags
& TRANSPORT_RECURSE_SUBMODULES_ON_DEMAND
) &&
969 !pretend
)) && !is_bare_repository()) {
970 struct ref
*ref
= remote_refs
;
971 struct string_list needs_pushing
= STRING_LIST_INIT_DUP
;
972 struct sha1_array commits
= SHA1_ARRAY_INIT
;
974 for (; ref
; ref
= ref
->next
)
975 if (!is_null_oid(&ref
->new_oid
))
976 sha1_array_append(&commits
, ref
->new_oid
.hash
);
978 if (find_unpushed_submodules(&commits
, transport
->remote
->name
,
980 sha1_array_clear(&commits
);
981 die_with_unpushed_submodules(&needs_pushing
);
983 string_list_clear(&needs_pushing
, 0);
984 sha1_array_clear(&commits
);
987 push_ret
= transport
->push_refs(transport
, remote_refs
, flags
);
988 err
= push_had_errors(remote_refs
);
989 ret
= push_ret
| err
;
992 transport_print_push_status(transport
->url
, remote_refs
,
993 verbose
| porcelain
, porcelain
,
996 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
997 set_upstreams(transport
, remote_refs
, pretend
);
999 if (!(flags
& TRANSPORT_PUSH_DRY_RUN
)) {
1001 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1002 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1005 if (porcelain
&& !push_ret
)
1007 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1008 fprintf(stderr
, "Everything up-to-date\n");
1015 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
1017 if (!transport
->got_remote_refs
) {
1018 transport
->remote_refs
= transport
->get_refs_list(transport
, 0);
1019 transport
->got_remote_refs
= 1;
1022 return transport
->remote_refs
;
1025 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1028 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1029 struct ref
**heads
= NULL
;
1032 for (rm
= refs
; rm
; rm
= rm
->next
) {
1035 !is_null_oid(&rm
->old_oid
) &&
1036 !oidcmp(&rm
->peer_ref
->old_oid
, &rm
->old_oid
))
1038 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1039 heads
[nr_heads
++] = rm
;
1044 * When deepening of a shallow repository is requested,
1045 * then local and remote refs are likely to still be equal.
1046 * Just feed them all to the fetch method in that case.
1047 * This condition shouldn't be met in a non-deepening fetch
1048 * (see builtin/fetch.c:quickfetch()).
1050 ALLOC_ARRAY(heads
, nr_refs
);
1051 for (rm
= refs
; rm
; rm
= rm
->next
)
1052 heads
[nr_heads
++] = rm
;
1055 rc
= transport
->fetch(transport
, nr_heads
, heads
);
1061 void transport_unlock_pack(struct transport
*transport
)
1063 if (transport
->pack_lockfile
) {
1064 unlink_or_warn(transport
->pack_lockfile
);
1065 free(transport
->pack_lockfile
);
1066 transport
->pack_lockfile
= NULL
;
1070 int transport_connect(struct transport
*transport
, const char *name
,
1071 const char *exec
, int fd
[2])
1073 if (transport
->connect
)
1074 return transport
->connect(transport
, name
, exec
, fd
);
1076 die("Operation not supported by protocol");
1079 int transport_disconnect(struct transport
*transport
)
1082 if (transport
->disconnect
)
1083 ret
= transport
->disconnect(transport
);
1089 * Strip username (and password) from a URL and return
1090 * it in a newly allocated string.
1092 char *transport_anonymize_url(const char *url
)
1094 char *scheme_prefix
, *anon_part
;
1095 size_t anon_len
, prefix_len
= 0;
1097 anon_part
= strchr(url
, '@');
1098 if (url_is_local_not_ssh(url
) || !anon_part
)
1101 anon_len
= strlen(++anon_part
);
1102 scheme_prefix
= strstr(url
, "://");
1103 if (!scheme_prefix
) {
1104 if (!strchr(anon_part
, ':'))
1105 /* cannot be "me@there:/path/name" */
1109 /* make sure scheme is reasonable */
1110 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1113 case '+': case '.': case '-':
1122 /* @ past the first slash does not count */
1123 cp
= strchr(scheme_prefix
+ 3, '/');
1124 if (cp
&& cp
< anon_part
)
1126 prefix_len
= scheme_prefix
- url
+ 3;
1128 return xstrfmt("%.*s%.*s", (int)prefix_len
, url
,
1129 (int)anon_len
, anon_part
);
1131 return xstrdup(url
);
1134 struct alternate_refs_data
{
1135 alternate_ref_fn
*fn
;
1139 static int refs_from_alternate_cb(struct alternate_object_database
*e
,
1144 struct remote
*remote
;
1145 struct transport
*transport
;
1146 const struct ref
*extra
;
1147 struct alternate_refs_data
*cb
= data
;
1149 other
= xstrdup(real_path(e
->path
));
1150 len
= strlen(other
);
1152 while (other
[len
-1] == '/')
1153 other
[--len
] = '\0';
1154 if (len
< 8 || memcmp(other
+ len
- 8, "/objects", 8))
1156 /* Is this a git repository with refs? */
1157 memcpy(other
+ len
- 8, "/refs", 6);
1158 if (!is_directory(other
))
1160 other
[len
- 8] = '\0';
1161 remote
= remote_get(other
);
1162 transport
= transport_get(remote
, other
);
1163 for (extra
= transport_get_remote_refs(transport
);
1165 extra
= extra
->next
)
1166 cb
->fn(extra
, cb
->data
);
1167 transport_disconnect(transport
);
1173 void for_each_alternate_ref(alternate_ref_fn fn
, void *data
)
1175 struct alternate_refs_data cb
;
1178 foreach_alt_odb(refs_from_alternate_cb
, &cb
);