3 #include "run-command.h"
5 #include "fetch-pack.h"
17 * We copy packed-refs and refs/ into a temporary file, then read the
18 * loose refs recursively (sorting whenever possible), and then inserting
19 * those packed refs that are not yet in the list (not validating, but
20 * assuming that the file is sorted).
22 * Appears refactoring this from refs.c is too cumbersome.
25 static int str_cmp(const void *a
, const void *b
)
30 return strcmp(s1
, s2
);
33 /* path->buf + name_offset is expected to point to "refs/" */
35 static int read_loose_refs(struct strbuf
*path
, int name_offset
,
38 DIR *dir
= opendir(path
->buf
);
49 memset (&list
, 0, sizeof(list
));
51 while ((de
= readdir(dir
))) {
52 if (is_dot_or_dotdot(de
->d_name
))
54 ALLOC_GROW(list
.entries
, list
.nr
+ 1, list
.alloc
);
55 list
.entries
[list
.nr
++] = xstrdup(de
->d_name
);
61 qsort(list
.entries
, list
.nr
, sizeof(char *), str_cmp
);
64 strbuf_addch(path
, '/');
66 for (i
= 0; i
< list
.nr
; i
++, strbuf_setlen(path
, pathlen
+ 1)) {
67 strbuf_addstr(path
, list
.entries
[i
]);
68 if (read_loose_refs(path
, name_offset
, tail
)) {
69 int fd
= open(path
->buf
, O_RDONLY
);
75 next
= alloc_ref(path
->buf
+ name_offset
);
76 if (read_in_full(fd
, buffer
, 40) != 40 ||
77 get_sha1_hex(buffer
, next
->old_sha1
)) {
87 strbuf_setlen(path
, pathlen
);
89 for (i
= 0; i
< list
.nr
; i
++)
90 free(list
.entries
[i
]);
96 /* insert the packed refs for which no loose refs were found */
98 static void insert_packed_refs(const char *packed_refs
, struct ref
**list
)
100 FILE *f
= fopen(packed_refs
, "r");
101 static char buffer
[PATH_MAX
];
109 if (!fgets(buffer
, sizeof(buffer
), f
)) {
114 if (hexval(buffer
[0]) > 0xf)
116 len
= strlen(buffer
);
117 if (len
&& buffer
[len
- 1] == '\n')
118 buffer
[--len
] = '\0';
121 while ((*list
)->next
&&
122 (cmp
= strcmp(buffer
+ 41,
123 (*list
)->next
->name
)) > 0)
124 list
= &(*list
)->next
;
125 if (!(*list
)->next
|| cmp
< 0) {
126 struct ref
*next
= alloc_ref(buffer
+ 41);
128 if (get_sha1_hex(buffer
, next
->old_sha1
)) {
129 warning ("invalid SHA-1: %s", buffer
);
133 next
->next
= (*list
)->next
;
134 (*list
)->next
= next
;
135 list
= &(*list
)->next
;
140 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
144 for (ref
= refs
; ref
; ref
= ref
->next
) {
145 const char *localname
;
147 const char *remotename
;
148 unsigned char sha
[20];
151 * Check suitability for tracking. Must be successful /
152 * already up-to-date ref create/modify (not delete).
154 if (ref
->status
!= REF_STATUS_OK
&&
155 ref
->status
!= REF_STATUS_UPTODATE
)
159 if (!ref
->new_sha1
|| is_null_sha1(ref
->new_sha1
))
162 /* Follow symbolic refs (mainly for HEAD). */
163 localname
= ref
->peer_ref
->name
;
164 remotename
= ref
->name
;
165 tmp
= resolve_ref(localname
, sha
, 1, &flag
);
166 if (tmp
&& flag
& REF_ISSYMREF
&&
167 !prefixcmp(tmp
, "refs/heads/"))
170 /* Both source and destination must be local branches. */
171 if (!localname
|| prefixcmp(localname
, "refs/heads/"))
173 if (!remotename
|| prefixcmp(remotename
, "refs/heads/"))
177 install_branch_config(BRANCH_CONFIG_VERBOSE
,
178 localname
+ 11, transport
->remote
->name
,
181 printf("Would set upstream of '%s' to '%s' of '%s'\n",
182 localname
+ 11, remotename
+ 11,
183 transport
->remote
->name
);
187 static const char *rsync_url(const char *url
)
189 return prefixcmp(url
, "rsync://") ? skip_prefix(url
, "rsync:") : url
;
192 static struct ref
*get_refs_via_rsync(struct transport
*transport
, int for_push
)
194 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
195 struct ref dummy
= {0}, *tail
= &dummy
;
196 struct child_process rsync
;
203 /* copy the refs to the temporary directory */
205 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
206 if (!mkdtemp(temp_dir
.buf
))
207 die_errno ("Could not make temporary directory");
208 temp_dir_len
= temp_dir
.len
;
210 strbuf_addstr(&buf
, rsync_url(transport
->url
));
211 strbuf_addstr(&buf
, "/refs");
213 memset(&rsync
, 0, sizeof(rsync
));
215 rsync
.stdout_to_stderr
= 1;
217 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
219 args
[3] = temp_dir
.buf
;
222 if (run_command(&rsync
))
223 die ("Could not run rsync to get refs");
226 strbuf_addstr(&buf
, rsync_url(transport
->url
));
227 strbuf_addstr(&buf
, "/packed-refs");
231 if (run_command(&rsync
))
232 die ("Could not run rsync to get refs");
234 /* read the copied refs */
236 strbuf_addstr(&temp_dir
, "/refs");
237 read_loose_refs(&temp_dir
, temp_dir_len
+ 1, &tail
);
238 strbuf_setlen(&temp_dir
, temp_dir_len
);
241 strbuf_addstr(&temp_dir
, "/packed-refs");
242 insert_packed_refs(temp_dir
.buf
, &tail
);
243 strbuf_setlen(&temp_dir
, temp_dir_len
);
245 if (remove_dir_recursively(&temp_dir
, 0))
246 warning ("Error removing temporary directory %s.",
249 strbuf_release(&buf
);
250 strbuf_release(&temp_dir
);
255 static int fetch_objs_via_rsync(struct transport
*transport
,
256 int nr_objs
, struct ref
**to_fetch
)
258 struct strbuf buf
= STRBUF_INIT
;
259 struct child_process rsync
;
263 strbuf_addstr(&buf
, rsync_url(transport
->url
));
264 strbuf_addstr(&buf
, "/objects/");
266 memset(&rsync
, 0, sizeof(rsync
));
268 rsync
.stdout_to_stderr
= 1;
270 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
271 args
[2] = "--ignore-existing";
272 args
[3] = "--exclude";
275 args
[6] = get_object_directory();
278 /* NEEDSWORK: handle one level of alternates */
279 result
= run_command(&rsync
);
281 strbuf_release(&buf
);
286 static int write_one_ref(const char *name
, const unsigned char *sha1
,
287 int flags
, void *data
)
289 struct strbuf
*buf
= data
;
293 /* when called via for_each_ref(), flags is non-zero */
294 if (flags
&& prefixcmp(name
, "refs/heads/") &&
295 prefixcmp(name
, "refs/tags/"))
298 strbuf_addstr(buf
, name
);
299 if (safe_create_leading_directories(buf
->buf
) ||
300 !(f
= fopen(buf
->buf
, "w")) ||
301 fprintf(f
, "%s\n", sha1_to_hex(sha1
)) < 0 ||
303 return error("problems writing temporary file %s", buf
->buf
);
304 strbuf_setlen(buf
, len
);
308 static int write_refs_to_temp_dir(struct strbuf
*temp_dir
,
309 int refspec_nr
, const char **refspec
)
313 for (i
= 0; i
< refspec_nr
; i
++) {
314 unsigned char sha1
[20];
317 if (dwim_ref(refspec
[i
], strlen(refspec
[i
]), sha1
, &ref
) != 1)
318 return error("Could not get ref %s", refspec
[i
]);
320 if (write_one_ref(ref
, sha1
, 0, temp_dir
)) {
329 static int rsync_transport_push(struct transport
*transport
,
330 int refspec_nr
, const char **refspec
, int flags
)
332 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
334 struct child_process rsync
;
335 const char *args
[10];
337 if (flags
& TRANSPORT_PUSH_MIRROR
)
338 return error("rsync transport does not support mirror mode");
340 /* first push the objects */
342 strbuf_addstr(&buf
, rsync_url(transport
->url
));
343 strbuf_addch(&buf
, '/');
345 memset(&rsync
, 0, sizeof(rsync
));
347 rsync
.stdout_to_stderr
= 1;
351 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
352 args
[i
++] = "--dry-run";
353 if (transport
->verbose
> 0)
355 args
[i
++] = "--ignore-existing";
356 args
[i
++] = "--exclude";
358 args
[i
++] = get_object_directory();
362 if (run_command(&rsync
))
363 return error("Could not push objects to %s",
364 rsync_url(transport
->url
));
366 /* copy the refs to the temporary directory; they could be packed. */
368 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
369 if (!mkdtemp(temp_dir
.buf
))
370 die_errno ("Could not make temporary directory");
371 strbuf_addch(&temp_dir
, '/');
373 if (flags
& TRANSPORT_PUSH_ALL
) {
374 if (for_each_ref(write_one_ref
, &temp_dir
))
376 } else if (write_refs_to_temp_dir(&temp_dir
, refspec_nr
, refspec
))
380 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
381 args
[i
++] = "--dry-run";
382 if (!(flags
& TRANSPORT_PUSH_FORCE
))
383 args
[i
++] = "--ignore-existing";
384 args
[i
++] = temp_dir
.buf
;
385 args
[i
++] = rsync_url(transport
->url
);
387 if (run_command(&rsync
))
388 result
= error("Could not push to %s",
389 rsync_url(transport
->url
));
391 if (remove_dir_recursively(&temp_dir
, 0))
392 warning ("Could not remove temporary directory %s.",
395 strbuf_release(&buf
);
396 strbuf_release(&temp_dir
);
401 struct bundle_transport_data
{
403 struct bundle_header header
;
406 static struct ref
*get_refs_from_bundle(struct transport
*transport
, int for_push
)
408 struct bundle_transport_data
*data
= transport
->data
;
409 struct ref
*result
= NULL
;
417 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
419 die ("Could not read bundle '%s'.", transport
->url
);
420 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
421 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
422 struct ref
*ref
= alloc_ref(e
->name
);
423 hashcpy(ref
->old_sha1
, e
->sha1
);
430 static int fetch_refs_from_bundle(struct transport
*transport
,
431 int nr_heads
, struct ref
**to_fetch
)
433 struct bundle_transport_data
*data
= transport
->data
;
434 return unbundle(&data
->header
, data
->fd
);
437 static int close_bundle(struct transport
*transport
)
439 struct bundle_transport_data
*data
= transport
->data
;
446 struct git_transport_data
{
447 struct git_transport_options options
;
448 struct child_process
*conn
;
450 unsigned got_remote_heads
: 1;
451 struct extra_have_objects extra_have
;
454 static int set_git_option(struct git_transport_options
*opts
,
455 const char *name
, const char *value
)
457 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
458 opts
->uploadpack
= value
;
460 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
461 opts
->receivepack
= value
;
463 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
464 opts
->thin
= !!value
;
466 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
467 opts
->followtags
= !!value
;
469 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
470 opts
->keep
= !!value
;
472 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
476 opts
->depth
= atoi(value
);
482 static int connect_setup(struct transport
*transport
, int for_push
, int verbose
)
484 struct git_transport_data
*data
= transport
->data
;
489 data
->conn
= git_connect(data
->fd
, transport
->url
,
490 for_push
? data
->options
.receivepack
:
491 data
->options
.uploadpack
,
492 verbose
? CONNECT_VERBOSE
: 0);
497 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
)
499 struct git_transport_data
*data
= transport
->data
;
502 connect_setup(transport
, for_push
, 0);
503 get_remote_heads(data
->fd
[0], &refs
, 0, NULL
,
504 for_push
? REF_NORMAL
: 0, &data
->extra_have
);
505 data
->got_remote_heads
= 1;
510 static int fetch_refs_via_pack(struct transport
*transport
,
511 int nr_heads
, struct ref
**to_fetch
)
513 struct git_transport_data
*data
= transport
->data
;
514 char **heads
= xmalloc(nr_heads
* sizeof(*heads
));
515 char **origh
= xmalloc(nr_heads
* sizeof(*origh
));
516 const struct ref
*refs
;
517 char *dest
= xstrdup(transport
->url
);
518 struct fetch_pack_args args
;
520 struct ref
*refs_tmp
= NULL
;
522 memset(&args
, 0, sizeof(args
));
523 args
.uploadpack
= data
->options
.uploadpack
;
524 args
.keep_pack
= data
->options
.keep
;
526 args
.use_thin_pack
= data
->options
.thin
;
527 args
.include_tag
= data
->options
.followtags
;
528 args
.verbose
= (transport
->verbose
> 0);
529 args
.quiet
= (transport
->verbose
< 0);
530 args
.no_progress
= !transport
->progress
;
531 args
.depth
= data
->options
.depth
;
533 for (i
= 0; i
< nr_heads
; i
++)
534 origh
[i
] = heads
[i
] = xstrdup(to_fetch
[i
]->name
);
536 if (!data
->got_remote_heads
) {
537 connect_setup(transport
, 0, 0);
538 get_remote_heads(data
->fd
[0], &refs_tmp
, 0, NULL
, 0, NULL
);
539 data
->got_remote_heads
= 1;
542 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
543 refs_tmp
? refs_tmp
: transport
->remote_refs
,
544 dest
, nr_heads
, heads
, &transport
->pack_lockfile
);
547 if (finish_connect(data
->conn
))
550 data
->got_remote_heads
= 0;
554 for (i
= 0; i
< nr_heads
; i
++)
559 return (refs
? 0 : -1);
562 static int push_had_errors(struct ref
*ref
)
564 for (; ref
; ref
= ref
->next
) {
565 switch (ref
->status
) {
566 case REF_STATUS_NONE
:
567 case REF_STATUS_UPTODATE
:
577 int transport_refs_pushed(struct ref
*ref
)
579 for (; ref
; ref
= ref
->next
) {
580 switch(ref
->status
) {
581 case REF_STATUS_NONE
:
582 case REF_STATUS_UPTODATE
:
591 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
595 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
601 if (!remote_find_tracking(remote
, &rs
)) {
603 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
605 delete_ref(rs
.dst
, NULL
, 0);
607 update_ref("update by push", rs
.dst
,
608 ref
->new_sha1
, NULL
, 0, 0);
613 static void print_ref_status(char flag
, const char *summary
, struct ref
*to
, struct ref
*from
, const char *msg
, int porcelain
)
617 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
619 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
621 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
623 fprintf(stdout
, "%s\n", summary
);
625 fprintf(stderr
, " %c %-*s ", flag
, TRANSPORT_SUMMARY_WIDTH
, summary
);
627 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
629 fputs(prettify_refname(to
->name
), stderr
);
639 static const char *status_abbrev(unsigned char sha1
[20])
641 return find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
644 static void print_ok_ref_status(struct ref
*ref
, int porcelain
)
647 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
, porcelain
);
648 else if (is_null_sha1(ref
->old_sha1
))
649 print_ref_status('*',
650 (!prefixcmp(ref
->name
, "refs/tags/") ? "[new tag]" :
652 ref
, ref
->peer_ref
, NULL
, porcelain
);
658 strcpy(quickref
, status_abbrev(ref
->old_sha1
));
659 if (ref
->nonfastforward
) {
660 strcat(quickref
, "...");
662 msg
= "forced update";
664 strcat(quickref
, "..");
668 strcat(quickref
, status_abbrev(ref
->new_sha1
));
670 print_ref_status(type
, quickref
, ref
, ref
->peer_ref
, msg
, porcelain
);
674 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
, int porcelain
)
677 fprintf(porcelain
? stdout
: stderr
, "To %s\n", dest
);
679 switch(ref
->status
) {
680 case REF_STATUS_NONE
:
681 print_ref_status('X', "[no match]", ref
, NULL
, NULL
, porcelain
);
683 case REF_STATUS_REJECT_NODELETE
:
684 print_ref_status('!', "[rejected]", ref
, NULL
,
685 "remote does not support deleting refs", porcelain
);
687 case REF_STATUS_UPTODATE
:
688 print_ref_status('=', "[up to date]", ref
,
689 ref
->peer_ref
, NULL
, porcelain
);
691 case REF_STATUS_REJECT_NONFASTFORWARD
:
692 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
693 "non-fast-forward", porcelain
);
695 case REF_STATUS_REMOTE_REJECT
:
696 print_ref_status('!', "[remote rejected]", ref
,
697 ref
->deletion
? NULL
: ref
->peer_ref
,
698 ref
->remote_status
, porcelain
);
700 case REF_STATUS_EXPECTING_REPORT
:
701 print_ref_status('!', "[remote failure]", ref
,
702 ref
->deletion
? NULL
: ref
->peer_ref
,
703 "remote failed to report status", porcelain
);
706 print_ok_ref_status(ref
, porcelain
);
713 void transport_print_push_status(const char *dest
, struct ref
*refs
,
714 int verbose
, int porcelain
, int *nonfastforward
)
720 for (ref
= refs
; ref
; ref
= ref
->next
)
721 if (ref
->status
== REF_STATUS_UPTODATE
)
722 n
+= print_one_push_status(ref
, dest
, n
, porcelain
);
725 for (ref
= refs
; ref
; ref
= ref
->next
)
726 if (ref
->status
== REF_STATUS_OK
)
727 n
+= print_one_push_status(ref
, dest
, n
, porcelain
);
730 for (ref
= refs
; ref
; ref
= ref
->next
) {
731 if (ref
->status
!= REF_STATUS_NONE
&&
732 ref
->status
!= REF_STATUS_UPTODATE
&&
733 ref
->status
!= REF_STATUS_OK
)
734 n
+= print_one_push_status(ref
, dest
, n
, porcelain
);
735 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
)
740 void transport_verify_remote_names(int nr_heads
, const char **heads
)
744 for (i
= 0; i
< nr_heads
; i
++) {
745 const char *local
= heads
[i
];
746 const char *remote
= strrchr(heads
[i
], ':');
751 /* A matching refspec is okay. */
752 if (remote
== local
&& remote
[1] == '\0')
755 remote
= remote
? (remote
+ 1) : local
;
756 switch (check_ref_format(remote
)) {
758 case CHECK_REF_FORMAT_ONELEVEL
:
759 /* ok but a single level -- that is fine for
762 case CHECK_REF_FORMAT_WILDCARD
:
763 /* ok but ends with a pattern-match character */
766 die("remote part of refspec is not a valid name in %s",
771 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
773 struct git_transport_data
*data
= transport
->data
;
774 struct send_pack_args args
;
777 if (!data
->got_remote_heads
) {
778 struct ref
*tmp_refs
;
779 connect_setup(transport
, 1, 0);
781 get_remote_heads(data
->fd
[0], &tmp_refs
, 0, NULL
, REF_NORMAL
,
783 data
->got_remote_heads
= 1;
786 memset(&args
, 0, sizeof(args
));
787 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
788 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
789 args
.use_thin_pack
= data
->options
.thin
;
790 args
.verbose
= (transport
->verbose
> 0);
791 args
.quiet
= (transport
->verbose
< 0);
792 args
.progress
= transport
->progress
;
793 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
794 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
796 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
801 ret
|= finish_connect(data
->conn
);
803 data
->got_remote_heads
= 0;
808 static int connect_git(struct transport
*transport
, const char *name
,
809 const char *executable
, int fd
[2])
811 struct git_transport_data
*data
= transport
->data
;
812 data
->conn
= git_connect(data
->fd
, transport
->url
,
819 static int disconnect_git(struct transport
*transport
)
821 struct git_transport_data
*data
= transport
->data
;
823 if (data
->got_remote_heads
)
824 packet_flush(data
->fd
[1]);
827 finish_connect(data
->conn
);
834 void transport_take_over(struct transport
*transport
,
835 struct child_process
*child
)
837 struct git_transport_data
*data
;
839 if (!transport
->smart_options
)
840 die("Bug detected: Taking over transport requires non-NULL "
841 "smart_options field.");
843 data
= xcalloc(1, sizeof(*data
));
844 data
->options
= *transport
->smart_options
;
846 data
->fd
[0] = data
->conn
->out
;
847 data
->fd
[1] = data
->conn
->in
;
848 data
->got_remote_heads
= 0;
849 transport
->data
= data
;
851 transport
->set_option
= NULL
;
852 transport
->get_refs_list
= get_refs_via_connect
;
853 transport
->fetch
= fetch_refs_via_pack
;
854 transport
->push
= NULL
;
855 transport
->push_refs
= git_transport_push
;
856 transport
->disconnect
= disconnect_git
;
857 transport
->smart_options
= &(data
->options
);
860 static int is_local(const char *url
)
862 const char *colon
= strchr(url
, ':');
863 const char *slash
= strchr(url
, '/');
864 return !colon
|| (slash
&& slash
< colon
) ||
865 has_dos_drive_prefix(url
);
868 static int is_file(const char *url
)
873 return S_ISREG(buf
.st_mode
);
876 static int external_specification_len(const char *url
)
878 return strchr(url
, ':') - url
;
881 struct transport
*transport_get(struct remote
*remote
, const char *url
)
884 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
886 ret
->progress
= isatty(2);
889 die("No remote provided to transport_get()");
891 ret
->got_remote_refs
= 0;
892 ret
->remote
= remote
;
893 helper
= remote
->foreign_vcs
;
895 if (!url
&& remote
->url
)
896 url
= remote
->url
[0];
899 /* maybe it is a foreign URL? */
903 while (is_urlschemechar(p
== url
, *p
))
905 if (!prefixcmp(p
, "::"))
906 helper
= xstrndup(url
, p
- url
);
910 transport_helper_init(ret
, helper
);
911 } else if (!prefixcmp(url
, "rsync:")) {
912 ret
->get_refs_list
= get_refs_via_rsync
;
913 ret
->fetch
= fetch_objs_via_rsync
;
914 ret
->push
= rsync_transport_push
;
915 ret
->smart_options
= NULL
;
916 } else if (is_local(url
) && is_file(url
)) {
917 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
919 ret
->get_refs_list
= get_refs_from_bundle
;
920 ret
->fetch
= fetch_refs_from_bundle
;
921 ret
->disconnect
= close_bundle
;
922 ret
->smart_options
= NULL
;
923 } else if (!is_url(url
)
924 || !prefixcmp(url
, "file://")
925 || !prefixcmp(url
, "git://")
926 || !prefixcmp(url
, "ssh://")
927 || !prefixcmp(url
, "git+ssh://")
928 || !prefixcmp(url
, "ssh+git://")) {
929 /* These are builtin smart transports. */
930 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
932 ret
->set_option
= NULL
;
933 ret
->get_refs_list
= get_refs_via_connect
;
934 ret
->fetch
= fetch_refs_via_pack
;
935 ret
->push_refs
= git_transport_push
;
936 ret
->connect
= connect_git
;
937 ret
->disconnect
= disconnect_git
;
938 ret
->smart_options
= &(data
->options
);
941 data
->got_remote_heads
= 0;
943 /* Unknown protocol in URL. Pass to external handler. */
944 int len
= external_specification_len(url
);
945 char *handler
= xmalloc(len
+ 1);
947 strncpy(handler
, url
, len
);
948 transport_helper_init(ret
, handler
);
951 if (ret
->smart_options
) {
952 ret
->smart_options
->thin
= 1;
953 ret
->smart_options
->uploadpack
= "git-upload-pack";
954 if (remote
->uploadpack
)
955 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
956 ret
->smart_options
->receivepack
= "git-receive-pack";
957 if (remote
->receivepack
)
958 ret
->smart_options
->receivepack
= remote
->receivepack
;
964 int transport_set_option(struct transport
*transport
,
965 const char *name
, const char *value
)
967 int git_reports
= 1, protocol_reports
= 1;
969 if (transport
->smart_options
)
970 git_reports
= set_git_option(transport
->smart_options
,
973 if (transport
->set_option
)
974 protocol_reports
= transport
->set_option(transport
, name
,
977 /* If either report is 0, report 0 (success). */
978 if (!git_reports
|| !protocol_reports
)
980 /* If either reports -1 (invalid value), report -1. */
981 if ((git_reports
== -1) || (protocol_reports
== -1))
983 /* Otherwise if both report unknown, report unknown. */
987 void transport_set_verbosity(struct transport
*transport
, int verbosity
,
991 transport
->verbose
= verbosity
<= 3 ? verbosity
: 3;
993 transport
->verbose
= -1;
996 * Rules used to determine whether to report progress (processing aborts
997 * when a rule is satisfied):
999 * 1. Report progress, if force_progress is 1 (ie. --progress).
1000 * 2. Don't report progress, if verbosity < 0 (ie. -q/--quiet ).
1001 * 3. Report progress if isatty(2) is 1.
1003 transport
->progress
= force_progress
|| (verbosity
>= 0 && isatty(2));
1006 int transport_push(struct transport
*transport
,
1007 int refspec_nr
, const char **refspec
, int flags
,
1008 int *nonfastforward
)
1010 *nonfastforward
= 0;
1011 transport_verify_remote_names(refspec_nr
, refspec
);
1013 if (transport
->push
) {
1014 /* Maybe FIXME. But no important transport uses this case. */
1015 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1016 die("This transport does not support using --set-upstream");
1018 return transport
->push(transport
, refspec_nr
, refspec
, flags
);
1019 } else if (transport
->push_refs
) {
1020 struct ref
*remote_refs
=
1021 transport
->get_refs_list(transport
, 1);
1022 struct ref
*local_refs
= get_local_heads();
1023 int match_flags
= MATCH_REFS_NONE
;
1024 int verbose
= (transport
->verbose
> 0);
1025 int quiet
= (transport
->verbose
< 0);
1026 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1027 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1028 int push_ret
, ret
, err
;
1030 if (flags
& TRANSPORT_PUSH_ALL
)
1031 match_flags
|= MATCH_REFS_ALL
;
1032 if (flags
& TRANSPORT_PUSH_MIRROR
)
1033 match_flags
|= MATCH_REFS_MIRROR
;
1035 if (match_refs(local_refs
, &remote_refs
,
1036 refspec_nr
, refspec
, match_flags
)) {
1040 set_ref_status_for_push(remote_refs
,
1041 flags
& TRANSPORT_PUSH_MIRROR
,
1042 flags
& TRANSPORT_PUSH_FORCE
);
1044 push_ret
= transport
->push_refs(transport
, remote_refs
, flags
);
1045 err
= push_had_errors(remote_refs
);
1046 ret
= push_ret
| err
;
1049 transport_print_push_status(transport
->url
, remote_refs
,
1050 verbose
| porcelain
, porcelain
,
1053 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1054 set_upstreams(transport
, remote_refs
, pretend
);
1056 if (!(flags
& TRANSPORT_PUSH_DRY_RUN
)) {
1058 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1059 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1062 if (porcelain
&& !push_ret
)
1064 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1065 fprintf(stderr
, "Everything up-to-date\n");
1072 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
1074 if (!transport
->got_remote_refs
) {
1075 transport
->remote_refs
= transport
->get_refs_list(transport
, 0);
1076 transport
->got_remote_refs
= 1;
1079 return transport
->remote_refs
;
1082 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1085 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1086 struct ref
**heads
= NULL
;
1089 for (rm
= refs
; rm
; rm
= rm
->next
) {
1092 !is_null_sha1(rm
->old_sha1
) &&
1093 !hashcmp(rm
->peer_ref
->old_sha1
, rm
->old_sha1
))
1095 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1096 heads
[nr_heads
++] = rm
;
1101 * When deepening of a shallow repository is requested,
1102 * then local and remote refs are likely to still be equal.
1103 * Just feed them all to the fetch method in that case.
1104 * This condition shouldn't be met in a non-deepening fetch
1105 * (see builtin-fetch.c:quickfetch()).
1107 heads
= xmalloc(nr_refs
* sizeof(*heads
));
1108 for (rm
= refs
; rm
; rm
= rm
->next
)
1109 heads
[nr_heads
++] = rm
;
1112 rc
= transport
->fetch(transport
, nr_heads
, heads
);
1118 void transport_unlock_pack(struct transport
*transport
)
1120 if (transport
->pack_lockfile
) {
1121 unlink_or_warn(transport
->pack_lockfile
);
1122 free(transport
->pack_lockfile
);
1123 transport
->pack_lockfile
= NULL
;
1127 int transport_connect(struct transport
*transport
, const char *name
,
1128 const char *exec
, int fd
[2])
1130 if (transport
->connect
)
1131 return transport
->connect(transport
, name
, exec
, fd
);
1133 die("Operation not supported by protocol");
1136 int transport_disconnect(struct transport
*transport
)
1139 if (transport
->disconnect
)
1140 ret
= transport
->disconnect(transport
);
1146 * Strip username (and password) from an url and return
1147 * it in a newly allocated string.
1149 char *transport_anonymize_url(const char *url
)
1151 char *anon_url
, *scheme_prefix
, *anon_part
;
1152 size_t anon_len
, prefix_len
= 0;
1154 anon_part
= strchr(url
, '@');
1155 if (is_local(url
) || !anon_part
)
1158 anon_len
= strlen(++anon_part
);
1159 scheme_prefix
= strstr(url
, "://");
1160 if (!scheme_prefix
) {
1161 if (!strchr(anon_part
, ':'))
1162 /* cannot be "me@there:/path/name" */
1166 /* make sure scheme is reasonable */
1167 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1170 case '+': case '.': case '-':
1179 /* @ past the first slash does not count */
1180 cp
= strchr(scheme_prefix
+ 3, '/');
1181 if (cp
&& cp
< anon_part
)
1183 prefix_len
= scheme_prefix
- url
+ 3;
1185 anon_url
= xcalloc(1, 1 + prefix_len
+ anon_len
);
1186 memcpy(anon_url
, url
, prefix_len
);
1187 memcpy(anon_url
+ prefix_len
, anon_part
, anon_len
);
1190 return xstrdup(url
);
1193 int refs_from_alternate_cb(struct alternate_object_database
*e
, void *cb
)
1197 struct remote
*remote
;
1198 struct transport
*transport
;
1199 const struct ref
*extra
;
1200 alternate_ref_fn
*ref_fn
= cb
;
1203 other
= xstrdup(real_path(e
->base
));
1205 len
= strlen(other
);
1207 while (other
[len
-1] == '/')
1208 other
[--len
] = '\0';
1209 if (len
< 8 || memcmp(other
+ len
- 8, "/objects", 8))
1211 /* Is this a git repository with refs? */
1212 memcpy(other
+ len
- 8, "/refs", 6);
1213 if (!is_directory(other
))
1215 other
[len
- 8] = '\0';
1216 remote
= remote_get(other
);
1217 transport
= transport_get(remote
, other
);
1218 for (extra
= transport_get_remote_refs(transport
);
1220 extra
= extra
->next
)
1221 ref_fn(extra
, NULL
);
1222 transport_disconnect(transport
);