3 #include "run-command.h"
5 #include "fetch-pack.h"
16 * We copy packed-refs and refs/ into a temporary file, then read the
17 * loose refs recursively (sorting whenever possible), and then inserting
18 * those packed refs that are not yet in the list (not validating, but
19 * assuming that the file is sorted).
21 * Appears refactoring this from refs.c is too cumbersome.
24 static int str_cmp(const void *a
, const void *b
)
29 return strcmp(s1
, s2
);
32 /* path->buf + name_offset is expected to point to "refs/" */
34 static int read_loose_refs(struct strbuf
*path
, int name_offset
,
37 DIR *dir
= opendir(path
->buf
);
48 memset (&list
, 0, sizeof(list
));
50 while ((de
= readdir(dir
))) {
51 if (is_dot_or_dotdot(de
->d_name
))
53 ALLOC_GROW(list
.entries
, list
.nr
+ 1, list
.alloc
);
54 list
.entries
[list
.nr
++] = xstrdup(de
->d_name
);
60 qsort(list
.entries
, list
.nr
, sizeof(char *), str_cmp
);
63 strbuf_addch(path
, '/');
65 for (i
= 0; i
< list
.nr
; i
++, strbuf_setlen(path
, pathlen
+ 1)) {
66 strbuf_addstr(path
, list
.entries
[i
]);
67 if (read_loose_refs(path
, name_offset
, tail
)) {
68 int fd
= open(path
->buf
, O_RDONLY
);
74 next
= alloc_ref(path
->buf
+ name_offset
);
75 if (read_in_full(fd
, buffer
, 40) != 40 ||
76 get_sha1_hex(buffer
, next
->old_sha1
)) {
86 strbuf_setlen(path
, pathlen
);
88 for (i
= 0; i
< list
.nr
; i
++)
89 free(list
.entries
[i
]);
95 /* insert the packed refs for which no loose refs were found */
97 static void insert_packed_refs(const char *packed_refs
, struct ref
**list
)
99 FILE *f
= fopen(packed_refs
, "r");
100 static char buffer
[PATH_MAX
];
108 if (!fgets(buffer
, sizeof(buffer
), f
)) {
113 if (hexval(buffer
[0]) > 0xf)
115 len
= strlen(buffer
);
116 if (len
&& buffer
[len
- 1] == '\n')
117 buffer
[--len
] = '\0';
120 while ((*list
)->next
&&
121 (cmp
= strcmp(buffer
+ 41,
122 (*list
)->next
->name
)) > 0)
123 list
= &(*list
)->next
;
124 if (!(*list
)->next
|| cmp
< 0) {
125 struct ref
*next
= alloc_ref(buffer
+ 41);
127 if (get_sha1_hex(buffer
, next
->old_sha1
)) {
128 warning ("invalid SHA-1: %s", buffer
);
132 next
->next
= (*list
)->next
;
133 (*list
)->next
= next
;
134 list
= &(*list
)->next
;
139 static void set_upstreams(struct transport
*transport
, struct ref
*refs
,
143 for (ref
= refs
; ref
; ref
= ref
->next
) {
144 const char *localname
;
146 const char *remotename
;
147 unsigned char sha
[20];
150 * Check suitability for tracking. Must be successful /
151 * already up-to-date ref create/modify (not delete).
153 if (ref
->status
!= REF_STATUS_OK
&&
154 ref
->status
!= REF_STATUS_UPTODATE
)
158 if (!ref
->new_sha1
|| is_null_sha1(ref
->new_sha1
))
161 /* Follow symbolic refs (mainly for HEAD). */
162 localname
= ref
->peer_ref
->name
;
163 remotename
= ref
->name
;
164 tmp
= resolve_ref(localname
, sha
, 1, &flag
);
165 if (tmp
&& flag
& REF_ISSYMREF
&&
166 !prefixcmp(tmp
, "refs/heads/"))
169 /* Both source and destination must be local branches. */
170 if (!localname
|| prefixcmp(localname
, "refs/heads/"))
172 if (!remotename
|| prefixcmp(remotename
, "refs/heads/"))
176 install_branch_config(BRANCH_CONFIG_VERBOSE
,
177 localname
+ 11, transport
->remote
->name
,
180 printf("Would set upstream of '%s' to '%s' of '%s'\n",
181 localname
+ 11, remotename
+ 11,
182 transport
->remote
->name
);
186 static const char *rsync_url(const char *url
)
188 return prefixcmp(url
, "rsync://") ? skip_prefix(url
, "rsync:") : url
;
191 static struct ref
*get_refs_via_rsync(struct transport
*transport
, int for_push
)
193 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
194 struct ref dummy
= {0}, *tail
= &dummy
;
195 struct child_process rsync
;
202 /* copy the refs to the temporary directory */
204 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
205 if (!mkdtemp(temp_dir
.buf
))
206 die_errno ("Could not make temporary directory");
207 temp_dir_len
= temp_dir
.len
;
209 strbuf_addstr(&buf
, rsync_url(transport
->url
));
210 strbuf_addstr(&buf
, "/refs");
212 memset(&rsync
, 0, sizeof(rsync
));
214 rsync
.stdout_to_stderr
= 1;
216 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
218 args
[3] = temp_dir
.buf
;
221 if (run_command(&rsync
))
222 die ("Could not run rsync to get refs");
225 strbuf_addstr(&buf
, rsync_url(transport
->url
));
226 strbuf_addstr(&buf
, "/packed-refs");
230 if (run_command(&rsync
))
231 die ("Could not run rsync to get refs");
233 /* read the copied refs */
235 strbuf_addstr(&temp_dir
, "/refs");
236 read_loose_refs(&temp_dir
, temp_dir_len
+ 1, &tail
);
237 strbuf_setlen(&temp_dir
, temp_dir_len
);
240 strbuf_addstr(&temp_dir
, "/packed-refs");
241 insert_packed_refs(temp_dir
.buf
, &tail
);
242 strbuf_setlen(&temp_dir
, temp_dir_len
);
244 if (remove_dir_recursively(&temp_dir
, 0))
245 warning ("Error removing temporary directory %s.",
248 strbuf_release(&buf
);
249 strbuf_release(&temp_dir
);
254 static int fetch_objs_via_rsync(struct transport
*transport
,
255 int nr_objs
, struct ref
**to_fetch
)
257 struct strbuf buf
= STRBUF_INIT
;
258 struct child_process rsync
;
262 strbuf_addstr(&buf
, rsync_url(transport
->url
));
263 strbuf_addstr(&buf
, "/objects/");
265 memset(&rsync
, 0, sizeof(rsync
));
267 rsync
.stdout_to_stderr
= 1;
269 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
270 args
[2] = "--ignore-existing";
271 args
[3] = "--exclude";
274 args
[6] = get_object_directory();
277 /* NEEDSWORK: handle one level of alternates */
278 result
= run_command(&rsync
);
280 strbuf_release(&buf
);
285 static int write_one_ref(const char *name
, const unsigned char *sha1
,
286 int flags
, void *data
)
288 struct strbuf
*buf
= data
;
292 /* when called via for_each_ref(), flags is non-zero */
293 if (flags
&& prefixcmp(name
, "refs/heads/") &&
294 prefixcmp(name
, "refs/tags/"))
297 strbuf_addstr(buf
, name
);
298 if (safe_create_leading_directories(buf
->buf
) ||
299 !(f
= fopen(buf
->buf
, "w")) ||
300 fprintf(f
, "%s\n", sha1_to_hex(sha1
)) < 0 ||
302 return error("problems writing temporary file %s", buf
->buf
);
303 strbuf_setlen(buf
, len
);
307 static int write_refs_to_temp_dir(struct strbuf
*temp_dir
,
308 int refspec_nr
, const char **refspec
)
312 for (i
= 0; i
< refspec_nr
; i
++) {
313 unsigned char sha1
[20];
316 if (dwim_ref(refspec
[i
], strlen(refspec
[i
]), sha1
, &ref
) != 1)
317 return error("Could not get ref %s", refspec
[i
]);
319 if (write_one_ref(ref
, sha1
, 0, temp_dir
)) {
328 static int rsync_transport_push(struct transport
*transport
,
329 int refspec_nr
, const char **refspec
, int flags
)
331 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
333 struct child_process rsync
;
334 const char *args
[10];
336 if (flags
& TRANSPORT_PUSH_MIRROR
)
337 return error("rsync transport does not support mirror mode");
339 /* first push the objects */
341 strbuf_addstr(&buf
, rsync_url(transport
->url
));
342 strbuf_addch(&buf
, '/');
344 memset(&rsync
, 0, sizeof(rsync
));
346 rsync
.stdout_to_stderr
= 1;
350 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
351 args
[i
++] = "--dry-run";
352 if (transport
->verbose
> 0)
354 args
[i
++] = "--ignore-existing";
355 args
[i
++] = "--exclude";
357 args
[i
++] = get_object_directory();
361 if (run_command(&rsync
))
362 return error("Could not push objects to %s",
363 rsync_url(transport
->url
));
365 /* copy the refs to the temporary directory; they could be packed. */
367 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
368 if (!mkdtemp(temp_dir
.buf
))
369 die_errno ("Could not make temporary directory");
370 strbuf_addch(&temp_dir
, '/');
372 if (flags
& TRANSPORT_PUSH_ALL
) {
373 if (for_each_ref(write_one_ref
, &temp_dir
))
375 } else if (write_refs_to_temp_dir(&temp_dir
, refspec_nr
, refspec
))
379 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
380 args
[i
++] = "--dry-run";
381 if (!(flags
& TRANSPORT_PUSH_FORCE
))
382 args
[i
++] = "--ignore-existing";
383 args
[i
++] = temp_dir
.buf
;
384 args
[i
++] = rsync_url(transport
->url
);
386 if (run_command(&rsync
))
387 result
= error("Could not push to %s",
388 rsync_url(transport
->url
));
390 if (remove_dir_recursively(&temp_dir
, 0))
391 warning ("Could not remove temporary directory %s.",
394 strbuf_release(&buf
);
395 strbuf_release(&temp_dir
);
400 struct bundle_transport_data
{
402 struct bundle_header header
;
405 static struct ref
*get_refs_from_bundle(struct transport
*transport
, int for_push
)
407 struct bundle_transport_data
*data
= transport
->data
;
408 struct ref
*result
= NULL
;
416 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
418 die ("Could not read bundle '%s'.", transport
->url
);
419 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
420 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
421 struct ref
*ref
= alloc_ref(e
->name
);
422 hashcpy(ref
->old_sha1
, e
->sha1
);
429 static int fetch_refs_from_bundle(struct transport
*transport
,
430 int nr_heads
, struct ref
**to_fetch
)
432 struct bundle_transport_data
*data
= transport
->data
;
433 return unbundle(&data
->header
, data
->fd
);
436 static int close_bundle(struct transport
*transport
)
438 struct bundle_transport_data
*data
= transport
->data
;
445 struct git_transport_data
{
446 struct git_transport_options options
;
447 struct child_process
*conn
;
449 unsigned got_remote_heads
: 1;
450 struct extra_have_objects extra_have
;
453 static int set_git_option(struct git_transport_options
*opts
,
454 const char *name
, const char *value
)
456 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
457 opts
->uploadpack
= value
;
459 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
460 opts
->receivepack
= value
;
462 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
463 opts
->thin
= !!value
;
465 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
466 opts
->followtags
= !!value
;
468 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
469 opts
->keep
= !!value
;
471 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
475 opts
->depth
= atoi(value
);
481 static int connect_setup(struct transport
*transport
, int for_push
, int verbose
)
483 struct git_transport_data
*data
= transport
->data
;
488 data
->conn
= git_connect(data
->fd
, transport
->url
,
489 for_push
? data
->options
.receivepack
:
490 data
->options
.uploadpack
,
491 verbose
? CONNECT_VERBOSE
: 0);
496 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
)
498 struct git_transport_data
*data
= transport
->data
;
501 connect_setup(transport
, for_push
, 0);
502 get_remote_heads(data
->fd
[0], &refs
, 0, NULL
,
503 for_push
? REF_NORMAL
: 0, &data
->extra_have
);
504 data
->got_remote_heads
= 1;
509 static int fetch_refs_via_pack(struct transport
*transport
,
510 int nr_heads
, struct ref
**to_fetch
)
512 struct git_transport_data
*data
= transport
->data
;
513 char **heads
= xmalloc(nr_heads
* sizeof(*heads
));
514 char **origh
= xmalloc(nr_heads
* sizeof(*origh
));
515 const struct ref
*refs
;
516 char *dest
= xstrdup(transport
->url
);
517 struct fetch_pack_args args
;
519 struct ref
*refs_tmp
= NULL
;
521 memset(&args
, 0, sizeof(args
));
522 args
.uploadpack
= data
->options
.uploadpack
;
523 args
.keep_pack
= data
->options
.keep
;
525 args
.use_thin_pack
= data
->options
.thin
;
526 args
.include_tag
= data
->options
.followtags
;
527 args
.verbose
= (transport
->verbose
> 0);
528 args
.quiet
= (transport
->verbose
< 0);
529 args
.no_progress
= args
.quiet
|| (!transport
->progress
&& !isatty(2));
530 args
.depth
= data
->options
.depth
;
532 for (i
= 0; i
< nr_heads
; i
++)
533 origh
[i
] = heads
[i
] = xstrdup(to_fetch
[i
]->name
);
535 if (!data
->got_remote_heads
) {
536 connect_setup(transport
, 0, 0);
537 get_remote_heads(data
->fd
[0], &refs_tmp
, 0, NULL
, 0, NULL
);
538 data
->got_remote_heads
= 1;
541 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
542 refs_tmp
? refs_tmp
: transport
->remote_refs
,
543 dest
, nr_heads
, heads
, &transport
->pack_lockfile
);
546 if (finish_connect(data
->conn
))
549 data
->got_remote_heads
= 0;
553 for (i
= 0; i
< nr_heads
; i
++)
558 return (refs
? 0 : -1);
561 static int push_had_errors(struct ref
*ref
)
563 for (; ref
; ref
= ref
->next
) {
564 switch (ref
->status
) {
565 case REF_STATUS_NONE
:
566 case REF_STATUS_UPTODATE
:
576 int transport_refs_pushed(struct ref
*ref
)
578 for (; ref
; ref
= ref
->next
) {
579 switch(ref
->status
) {
580 case REF_STATUS_NONE
:
581 case REF_STATUS_UPTODATE
:
590 void transport_update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
594 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
600 if (!remote_find_tracking(remote
, &rs
)) {
602 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
604 delete_ref(rs
.dst
, NULL
, 0);
606 update_ref("update by push", rs
.dst
,
607 ref
->new_sha1
, NULL
, 0, 0);
612 static void print_ref_status(char flag
, const char *summary
, struct ref
*to
, struct ref
*from
, const char *msg
, int porcelain
)
616 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
618 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
620 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
622 fprintf(stdout
, "%s\n", summary
);
624 fprintf(stderr
, " %c %-*s ", flag
, TRANSPORT_SUMMARY_WIDTH
, summary
);
626 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
628 fputs(prettify_refname(to
->name
), stderr
);
638 static const char *status_abbrev(unsigned char sha1
[20])
640 return find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
643 static void print_ok_ref_status(struct ref
*ref
, int porcelain
)
646 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
, porcelain
);
647 else if (is_null_sha1(ref
->old_sha1
))
648 print_ref_status('*',
649 (!prefixcmp(ref
->name
, "refs/tags/") ? "[new tag]" :
651 ref
, ref
->peer_ref
, NULL
, porcelain
);
657 strcpy(quickref
, status_abbrev(ref
->old_sha1
));
658 if (ref
->nonfastforward
) {
659 strcat(quickref
, "...");
661 msg
= "forced update";
663 strcat(quickref
, "..");
667 strcat(quickref
, status_abbrev(ref
->new_sha1
));
669 print_ref_status(type
, quickref
, ref
, ref
->peer_ref
, msg
, porcelain
);
673 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
, int porcelain
)
676 fprintf(porcelain
? stdout
: stderr
, "To %s\n", dest
);
678 switch(ref
->status
) {
679 case REF_STATUS_NONE
:
680 print_ref_status('X', "[no match]", ref
, NULL
, NULL
, porcelain
);
682 case REF_STATUS_REJECT_NODELETE
:
683 print_ref_status('!', "[rejected]", ref
, NULL
,
684 "remote does not support deleting refs", porcelain
);
686 case REF_STATUS_UPTODATE
:
687 print_ref_status('=', "[up to date]", ref
,
688 ref
->peer_ref
, NULL
, porcelain
);
690 case REF_STATUS_REJECT_NONFASTFORWARD
:
691 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
692 "non-fast-forward", porcelain
);
694 case REF_STATUS_REMOTE_REJECT
:
695 print_ref_status('!', "[remote rejected]", ref
,
696 ref
->deletion
? NULL
: ref
->peer_ref
,
697 ref
->remote_status
, porcelain
);
699 case REF_STATUS_EXPECTING_REPORT
:
700 print_ref_status('!', "[remote failure]", ref
,
701 ref
->deletion
? NULL
: ref
->peer_ref
,
702 "remote failed to report status", porcelain
);
705 print_ok_ref_status(ref
, porcelain
);
712 void transport_print_push_status(const char *dest
, struct ref
*refs
,
713 int verbose
, int porcelain
, int *nonfastforward
)
719 for (ref
= refs
; ref
; ref
= ref
->next
)
720 if (ref
->status
== REF_STATUS_UPTODATE
)
721 n
+= print_one_push_status(ref
, dest
, n
, porcelain
);
724 for (ref
= refs
; ref
; ref
= ref
->next
)
725 if (ref
->status
== REF_STATUS_OK
)
726 n
+= print_one_push_status(ref
, dest
, n
, porcelain
);
729 for (ref
= refs
; ref
; ref
= ref
->next
) {
730 if (ref
->status
!= REF_STATUS_NONE
&&
731 ref
->status
!= REF_STATUS_UPTODATE
&&
732 ref
->status
!= REF_STATUS_OK
)
733 n
+= print_one_push_status(ref
, dest
, n
, porcelain
);
734 if (ref
->status
== REF_STATUS_REJECT_NONFASTFORWARD
)
739 void transport_verify_remote_names(int nr_heads
, const char **heads
)
743 for (i
= 0; i
< nr_heads
; i
++) {
744 const char *local
= heads
[i
];
745 const char *remote
= strrchr(heads
[i
], ':');
750 /* A matching refspec is okay. */
751 if (remote
== local
&& remote
[1] == '\0')
754 remote
= remote
? (remote
+ 1) : local
;
755 switch (check_ref_format(remote
)) {
757 case CHECK_REF_FORMAT_ONELEVEL
:
758 /* ok but a single level -- that is fine for
761 case CHECK_REF_FORMAT_WILDCARD
:
762 /* ok but ends with a pattern-match character */
765 die("remote part of refspec is not a valid name in %s",
770 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
772 struct git_transport_data
*data
= transport
->data
;
773 struct send_pack_args args
;
776 if (!data
->got_remote_heads
) {
777 struct ref
*tmp_refs
;
778 connect_setup(transport
, 1, 0);
780 get_remote_heads(data
->fd
[0], &tmp_refs
, 0, NULL
, REF_NORMAL
,
782 data
->got_remote_heads
= 1;
785 memset(&args
, 0, sizeof(args
));
786 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
787 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
788 args
.use_thin_pack
= data
->options
.thin
;
789 args
.verbose
= !!(flags
& TRANSPORT_PUSH_VERBOSE
);
790 args
.quiet
= !!(flags
& TRANSPORT_PUSH_QUIET
);
791 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
792 args
.porcelain
= !!(flags
& TRANSPORT_PUSH_PORCELAIN
);
794 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
799 ret
|= finish_connect(data
->conn
);
801 data
->got_remote_heads
= 0;
806 static int connect_git(struct transport
*transport
, const char *name
,
807 const char *executable
, int fd
[2])
809 struct git_transport_data
*data
= transport
->data
;
810 data
->conn
= git_connect(data
->fd
, transport
->url
,
817 static int disconnect_git(struct transport
*transport
)
819 struct git_transport_data
*data
= transport
->data
;
821 if (data
->got_remote_heads
)
822 packet_flush(data
->fd
[1]);
825 finish_connect(data
->conn
);
832 void transport_take_over(struct transport
*transport
,
833 struct child_process
*child
)
835 struct git_transport_data
*data
;
837 if (!transport
->smart_options
)
838 die("Bug detected: Taking over transport requires non-NULL "
839 "smart_options field.");
841 data
= xcalloc(1, sizeof(*data
));
842 data
->options
= *transport
->smart_options
;
844 data
->fd
[0] = data
->conn
->out
;
845 data
->fd
[1] = data
->conn
->in
;
846 data
->got_remote_heads
= 0;
847 transport
->data
= data
;
849 transport
->set_option
= NULL
;
850 transport
->get_refs_list
= get_refs_via_connect
;
851 transport
->fetch
= fetch_refs_via_pack
;
852 transport
->push
= NULL
;
853 transport
->push_refs
= git_transport_push
;
854 transport
->disconnect
= disconnect_git
;
855 transport
->smart_options
= &(data
->options
);
858 static int is_local(const char *url
)
860 const char *colon
= strchr(url
, ':');
861 const char *slash
= strchr(url
, '/');
862 return !colon
|| (slash
&& slash
< colon
) ||
863 has_dos_drive_prefix(url
);
866 static int is_file(const char *url
)
871 return S_ISREG(buf
.st_mode
);
874 static int isurlschemechar(int first_flag
, int ch
)
877 * The set of valid URL schemes, as per STD66 (RFC3986) is
878 * '[A-Za-z][A-Za-z0-9+.-]*'. But use sightly looser check
879 * of '[A-Za-z0-9][A-Za-z0-9+.-]*' because earlier version
880 * of check used '[A-Za-z0-9]+' so not to break any remote
883 int alphanumeric
, special
;
884 alphanumeric
= ch
> 0 && isalnum(ch
);
885 special
= ch
== '+' || ch
== '-' || ch
== '.';
886 return alphanumeric
|| (!first_flag
&& special
);
889 static int is_url(const char *url
)
891 const char *url2
, *first_slash
;
896 first_slash
= strchr(url
, '/');
898 /* Input with no slash at all or slash first can't be URL. */
899 if (!first_slash
|| first_slash
== url
)
901 /* Character before must be : and next must be /. */
902 if (first_slash
[-1] != ':' || first_slash
[1] != '/')
904 /* There must be something before the :// */
905 if (first_slash
== url
+ 1)
908 * Check all characters up to first slash - 1. Only alphanum
912 while (url2
< first_slash
- 1) {
913 if (!isurlschemechar(url2
== url
, (unsigned char)*url2
))
922 static int external_specification_len(const char *url
)
924 return strchr(url
, ':') - url
;
927 struct transport
*transport_get(struct remote
*remote
, const char *url
)
930 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
933 die("No remote provided to transport_get()");
935 ret
->got_remote_refs
= 0;
936 ret
->remote
= remote
;
937 helper
= remote
->foreign_vcs
;
939 if (!url
&& remote
->url
)
940 url
= remote
->url
[0];
943 /* maybe it is a foreign URL? */
947 while (isurlschemechar(p
== url
, *p
))
949 if (!prefixcmp(p
, "::"))
950 helper
= xstrndup(url
, p
- url
);
954 transport_helper_init(ret
, helper
);
955 } else if (!prefixcmp(url
, "rsync:")) {
956 ret
->get_refs_list
= get_refs_via_rsync
;
957 ret
->fetch
= fetch_objs_via_rsync
;
958 ret
->push
= rsync_transport_push
;
959 ret
->smart_options
= NULL
;
960 } else if (is_local(url
) && is_file(url
)) {
961 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
963 ret
->get_refs_list
= get_refs_from_bundle
;
964 ret
->fetch
= fetch_refs_from_bundle
;
965 ret
->disconnect
= close_bundle
;
966 ret
->smart_options
= NULL
;
967 } else if (!is_url(url
)
968 || !prefixcmp(url
, "file://")
969 || !prefixcmp(url
, "git://")
970 || !prefixcmp(url
, "ssh://")
971 || !prefixcmp(url
, "git+ssh://")
972 || !prefixcmp(url
, "ssh+git://")) {
973 /* These are builtin smart transports. */
974 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
976 ret
->set_option
= NULL
;
977 ret
->get_refs_list
= get_refs_via_connect
;
978 ret
->fetch
= fetch_refs_via_pack
;
979 ret
->push_refs
= git_transport_push
;
980 ret
->connect
= connect_git
;
981 ret
->disconnect
= disconnect_git
;
982 ret
->smart_options
= &(data
->options
);
985 data
->got_remote_heads
= 0;
987 /* Unknown protocol in URL. Pass to external handler. */
988 int len
= external_specification_len(url
);
989 char *handler
= xmalloc(len
+ 1);
991 strncpy(handler
, url
, len
);
992 transport_helper_init(ret
, handler
);
995 if (ret
->smart_options
) {
996 ret
->smart_options
->thin
= 1;
997 ret
->smart_options
->uploadpack
= "git-upload-pack";
998 if (remote
->uploadpack
)
999 ret
->smart_options
->uploadpack
= remote
->uploadpack
;
1000 ret
->smart_options
->receivepack
= "git-receive-pack";
1001 if (remote
->receivepack
)
1002 ret
->smart_options
->receivepack
= remote
->receivepack
;
1008 int transport_set_option(struct transport
*transport
,
1009 const char *name
, const char *value
)
1011 int git_reports
= 1, protocol_reports
= 1;
1013 if (transport
->smart_options
)
1014 git_reports
= set_git_option(transport
->smart_options
,
1017 if (transport
->set_option
)
1018 protocol_reports
= transport
->set_option(transport
, name
,
1021 /* If either report is 0, report 0 (success). */
1022 if (!git_reports
|| !protocol_reports
)
1024 /* If either reports -1 (invalid value), report -1. */
1025 if ((git_reports
== -1) || (protocol_reports
== -1))
1027 /* Otherwise if both report unknown, report unknown. */
1031 int transport_push(struct transport
*transport
,
1032 int refspec_nr
, const char **refspec
, int flags
,
1033 int *nonfastforward
)
1035 *nonfastforward
= 0;
1036 transport_verify_remote_names(refspec_nr
, refspec
);
1038 if (transport
->push
) {
1039 /* Maybe FIXME. But no important transport uses this case. */
1040 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1041 die("This transport does not support using --set-upstream");
1043 return transport
->push(transport
, refspec_nr
, refspec
, flags
);
1044 } else if (transport
->push_refs
) {
1045 struct ref
*remote_refs
=
1046 transport
->get_refs_list(transport
, 1);
1047 struct ref
*local_refs
= get_local_heads();
1048 int match_flags
= MATCH_REFS_NONE
;
1049 int verbose
= flags
& TRANSPORT_PUSH_VERBOSE
;
1050 int quiet
= flags
& TRANSPORT_PUSH_QUIET
;
1051 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1052 int pretend
= flags
& TRANSPORT_PUSH_DRY_RUN
;
1053 int push_ret
, ret
, err
;
1055 if (flags
& TRANSPORT_PUSH_ALL
)
1056 match_flags
|= MATCH_REFS_ALL
;
1057 if (flags
& TRANSPORT_PUSH_MIRROR
)
1058 match_flags
|= MATCH_REFS_MIRROR
;
1060 if (match_refs(local_refs
, &remote_refs
,
1061 refspec_nr
, refspec
, match_flags
)) {
1065 set_ref_status_for_push(remote_refs
,
1066 flags
& TRANSPORT_PUSH_MIRROR
,
1067 flags
& TRANSPORT_PUSH_FORCE
);
1069 push_ret
= transport
->push_refs(transport
, remote_refs
, flags
);
1070 err
= push_had_errors(remote_refs
);
1071 ret
= push_ret
| err
;
1074 transport_print_push_status(transport
->url
, remote_refs
,
1075 verbose
| porcelain
, porcelain
,
1078 if (flags
& TRANSPORT_PUSH_SET_UPSTREAM
)
1079 set_upstreams(transport
, remote_refs
, pretend
);
1081 if (!(flags
& TRANSPORT_PUSH_DRY_RUN
)) {
1083 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1084 transport_update_tracking_ref(transport
->remote
, ref
, verbose
);
1087 if (porcelain
&& !push_ret
)
1089 else if (!quiet
&& !ret
&& !transport_refs_pushed(remote_refs
))
1090 fprintf(stderr
, "Everything up-to-date\n");
1097 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
1099 if (!transport
->got_remote_refs
) {
1100 transport
->remote_refs
= transport
->get_refs_list(transport
, 0);
1101 transport
->got_remote_refs
= 1;
1104 return transport
->remote_refs
;
1107 int transport_fetch_refs(struct transport
*transport
, struct ref
*refs
)
1110 int nr_heads
= 0, nr_alloc
= 0, nr_refs
= 0;
1111 struct ref
**heads
= NULL
;
1114 for (rm
= refs
; rm
; rm
= rm
->next
) {
1117 !is_null_sha1(rm
->old_sha1
) &&
1118 !hashcmp(rm
->peer_ref
->old_sha1
, rm
->old_sha1
))
1120 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1121 heads
[nr_heads
++] = rm
;
1126 * When deepening of a shallow repository is requested,
1127 * then local and remote refs are likely to still be equal.
1128 * Just feed them all to the fetch method in that case.
1129 * This condition shouldn't be met in a non-deepening fetch
1130 * (see builtin-fetch.c:quickfetch()).
1132 heads
= xmalloc(nr_refs
* sizeof(*heads
));
1133 for (rm
= refs
; rm
; rm
= rm
->next
)
1134 heads
[nr_heads
++] = rm
;
1137 rc
= transport
->fetch(transport
, nr_heads
, heads
);
1143 void transport_unlock_pack(struct transport
*transport
)
1145 if (transport
->pack_lockfile
) {
1146 unlink_or_warn(transport
->pack_lockfile
);
1147 free(transport
->pack_lockfile
);
1148 transport
->pack_lockfile
= NULL
;
1152 int transport_connect(struct transport
*transport
, const char *name
,
1153 const char *exec
, int fd
[2])
1155 if (transport
->connect
)
1156 return transport
->connect(transport
, name
, exec
, fd
);
1158 die("Operation not supported by protocol");
1161 int transport_disconnect(struct transport
*transport
)
1164 if (transport
->disconnect
)
1165 ret
= transport
->disconnect(transport
);
1171 * Strip username (and password) from an url and return
1172 * it in a newly allocated string.
1174 char *transport_anonymize_url(const char *url
)
1176 char *anon_url
, *scheme_prefix
, *anon_part
;
1177 size_t anon_len
, prefix_len
= 0;
1179 anon_part
= strchr(url
, '@');
1180 if (is_local(url
) || !anon_part
)
1183 anon_len
= strlen(++anon_part
);
1184 scheme_prefix
= strstr(url
, "://");
1185 if (!scheme_prefix
) {
1186 if (!strchr(anon_part
, ':'))
1187 /* cannot be "me@there:/path/name" */
1191 /* make sure scheme is reasonable */
1192 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1195 case '+': case '.': case '-':
1204 /* @ past the first slash does not count */
1205 cp
= strchr(scheme_prefix
+ 3, '/');
1206 if (cp
&& cp
< anon_part
)
1208 prefix_len
= scheme_prefix
- url
+ 3;
1210 anon_url
= xcalloc(1, 1 + prefix_len
+ anon_len
);
1211 memcpy(anon_url
, url
, prefix_len
);
1212 memcpy(anon_url
+ prefix_len
, anon_part
, anon_len
);
1215 return xstrdup(url
);