3 #include "run-command.h"
8 #include "fetch-pack.h"
18 * We copy packed-refs and refs/ into a temporary file, then read the
19 * loose refs recursively (sorting whenever possible), and then inserting
20 * those packed refs that are not yet in the list (not validating, but
21 * assuming that the file is sorted).
23 * Appears refactoring this from refs.c is too cumbersome.
26 static int str_cmp(const void *a
, const void *b
)
31 return strcmp(s1
, s2
);
34 /* path->buf + name_offset is expected to point to "refs/" */
36 static int read_loose_refs(struct strbuf
*path
, int name_offset
,
39 DIR *dir
= opendir(path
->buf
);
50 memset (&list
, 0, sizeof(list
));
52 while ((de
= readdir(dir
))) {
53 if (is_dot_or_dotdot(de
->d_name
))
55 ALLOC_GROW(list
.entries
, list
.nr
+ 1, list
.alloc
);
56 list
.entries
[list
.nr
++] = xstrdup(de
->d_name
);
62 qsort(list
.entries
, list
.nr
, sizeof(char *), str_cmp
);
65 strbuf_addch(path
, '/');
67 for (i
= 0; i
< list
.nr
; i
++, strbuf_setlen(path
, pathlen
+ 1)) {
68 strbuf_addstr(path
, list
.entries
[i
]);
69 if (read_loose_refs(path
, name_offset
, tail
)) {
70 int fd
= open(path
->buf
, O_RDONLY
);
76 next
= alloc_ref(path
->buf
+ name_offset
);
77 if (read_in_full(fd
, buffer
, 40) != 40 ||
78 get_sha1_hex(buffer
, next
->old_sha1
)) {
88 strbuf_setlen(path
, pathlen
);
90 for (i
= 0; i
< list
.nr
; i
++)
91 free(list
.entries
[i
]);
97 /* insert the packed refs for which no loose refs were found */
99 static void insert_packed_refs(const char *packed_refs
, struct ref
**list
)
101 FILE *f
= fopen(packed_refs
, "r");
102 static char buffer
[PATH_MAX
];
110 if (!fgets(buffer
, sizeof(buffer
), f
)) {
115 if (hexval(buffer
[0]) > 0xf)
117 len
= strlen(buffer
);
118 if (len
&& buffer
[len
- 1] == '\n')
119 buffer
[--len
] = '\0';
122 while ((*list
)->next
&&
123 (cmp
= strcmp(buffer
+ 41,
124 (*list
)->next
->name
)) > 0)
125 list
= &(*list
)->next
;
126 if (!(*list
)->next
|| cmp
< 0) {
127 struct ref
*next
= alloc_ref(buffer
+ 41);
129 if (get_sha1_hex(buffer
, next
->old_sha1
)) {
130 warning ("invalid SHA-1: %s", buffer
);
134 next
->next
= (*list
)->next
;
135 (*list
)->next
= next
;
136 list
= &(*list
)->next
;
141 static const char *rsync_url(const char *url
)
143 return prefixcmp(url
, "rsync://") ? skip_prefix(url
, "rsync:") : url
;
146 static struct ref
*get_refs_via_rsync(struct transport
*transport
, int for_push
)
148 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
149 struct ref dummy
, *tail
= &dummy
;
150 struct child_process rsync
;
157 /* copy the refs to the temporary directory */
159 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
160 if (!mkdtemp(temp_dir
.buf
))
161 die_errno ("Could not make temporary directory");
162 temp_dir_len
= temp_dir
.len
;
164 strbuf_addstr(&buf
, rsync_url(transport
->url
));
165 strbuf_addstr(&buf
, "/refs");
167 memset(&rsync
, 0, sizeof(rsync
));
169 rsync
.stdout_to_stderr
= 1;
171 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
173 args
[3] = temp_dir
.buf
;
176 if (run_command(&rsync
))
177 die ("Could not run rsync to get refs");
180 strbuf_addstr(&buf
, rsync_url(transport
->url
));
181 strbuf_addstr(&buf
, "/packed-refs");
185 if (run_command(&rsync
))
186 die ("Could not run rsync to get refs");
188 /* read the copied refs */
190 strbuf_addstr(&temp_dir
, "/refs");
191 read_loose_refs(&temp_dir
, temp_dir_len
+ 1, &tail
);
192 strbuf_setlen(&temp_dir
, temp_dir_len
);
195 strbuf_addstr(&temp_dir
, "/packed-refs");
196 insert_packed_refs(temp_dir
.buf
, &tail
);
197 strbuf_setlen(&temp_dir
, temp_dir_len
);
199 if (remove_dir_recursively(&temp_dir
, 0))
200 warning ("Error removing temporary directory %s.",
203 strbuf_release(&buf
);
204 strbuf_release(&temp_dir
);
209 static int fetch_objs_via_rsync(struct transport
*transport
,
210 int nr_objs
, const struct ref
**to_fetch
)
212 struct strbuf buf
= STRBUF_INIT
;
213 struct child_process rsync
;
217 strbuf_addstr(&buf
, rsync_url(transport
->url
));
218 strbuf_addstr(&buf
, "/objects/");
220 memset(&rsync
, 0, sizeof(rsync
));
222 rsync
.stdout_to_stderr
= 1;
224 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
225 args
[2] = "--ignore-existing";
226 args
[3] = "--exclude";
229 args
[6] = get_object_directory();
232 /* NEEDSWORK: handle one level of alternates */
233 result
= run_command(&rsync
);
235 strbuf_release(&buf
);
240 static int write_one_ref(const char *name
, const unsigned char *sha1
,
241 int flags
, void *data
)
243 struct strbuf
*buf
= data
;
247 /* when called via for_each_ref(), flags is non-zero */
248 if (flags
&& prefixcmp(name
, "refs/heads/") &&
249 prefixcmp(name
, "refs/tags/"))
252 strbuf_addstr(buf
, name
);
253 if (safe_create_leading_directories(buf
->buf
) ||
254 !(f
= fopen(buf
->buf
, "w")) ||
255 fprintf(f
, "%s\n", sha1_to_hex(sha1
)) < 0 ||
257 return error("problems writing temporary file %s", buf
->buf
);
258 strbuf_setlen(buf
, len
);
262 static int write_refs_to_temp_dir(struct strbuf
*temp_dir
,
263 int refspec_nr
, const char **refspec
)
267 for (i
= 0; i
< refspec_nr
; i
++) {
268 unsigned char sha1
[20];
271 if (dwim_ref(refspec
[i
], strlen(refspec
[i
]), sha1
, &ref
) != 1)
272 return error("Could not get ref %s", refspec
[i
]);
274 if (write_one_ref(ref
, sha1
, 0, temp_dir
)) {
283 static int rsync_transport_push(struct transport
*transport
,
284 int refspec_nr
, const char **refspec
, int flags
)
286 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
288 struct child_process rsync
;
289 const char *args
[10];
291 if (flags
& TRANSPORT_PUSH_MIRROR
)
292 return error("rsync transport does not support mirror mode");
294 /* first push the objects */
296 strbuf_addstr(&buf
, rsync_url(transport
->url
));
297 strbuf_addch(&buf
, '/');
299 memset(&rsync
, 0, sizeof(rsync
));
301 rsync
.stdout_to_stderr
= 1;
305 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
306 args
[i
++] = "--dry-run";
307 if (transport
->verbose
> 0)
309 args
[i
++] = "--ignore-existing";
310 args
[i
++] = "--exclude";
312 args
[i
++] = get_object_directory();
316 if (run_command(&rsync
))
317 return error("Could not push objects to %s",
318 rsync_url(transport
->url
));
320 /* copy the refs to the temporary directory; they could be packed. */
322 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
323 if (!mkdtemp(temp_dir
.buf
))
324 die_errno ("Could not make temporary directory");
325 strbuf_addch(&temp_dir
, '/');
327 if (flags
& TRANSPORT_PUSH_ALL
) {
328 if (for_each_ref(write_one_ref
, &temp_dir
))
330 } else if (write_refs_to_temp_dir(&temp_dir
, refspec_nr
, refspec
))
334 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
335 args
[i
++] = "--dry-run";
336 if (!(flags
& TRANSPORT_PUSH_FORCE
))
337 args
[i
++] = "--ignore-existing";
338 args
[i
++] = temp_dir
.buf
;
339 args
[i
++] = rsync_url(transport
->url
);
341 if (run_command(&rsync
))
342 result
= error("Could not push to %s",
343 rsync_url(transport
->url
));
345 if (remove_dir_recursively(&temp_dir
, 0))
346 warning ("Could not remove temporary directory %s.",
349 strbuf_release(&buf
);
350 strbuf_release(&temp_dir
);
355 /* Generic functions for using commit walkers */
357 #ifndef NO_CURL /* http fetch is the only user */
358 static int fetch_objs_via_walker(struct transport
*transport
,
359 int nr_objs
, const struct ref
**to_fetch
)
361 char *dest
= xstrdup(transport
->url
);
362 struct walker
*walker
= transport
->data
;
363 char **objs
= xmalloc(nr_objs
* sizeof(*objs
));
367 walker
->get_tree
= 1;
368 walker
->get_history
= 1;
369 walker
->get_verbosely
= transport
->verbose
>= 0;
370 walker
->get_recover
= 0;
372 for (i
= 0; i
< nr_objs
; i
++)
373 objs
[i
] = xstrdup(sha1_to_hex(to_fetch
[i
]->old_sha1
));
375 if (walker_fetch(walker
, nr_objs
, objs
, NULL
, NULL
))
376 die("Fetch failed.");
378 for (i
= 0; i
< nr_objs
; i
++)
386 static int disconnect_walker(struct transport
*transport
)
388 struct walker
*walker
= transport
->data
;
395 static int curl_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
)
401 if (flags
& TRANSPORT_PUSH_MIRROR
)
402 return error("http transport does not support mirror mode");
404 argv
= xmalloc((refspec_nr
+ 12) * sizeof(char *));
405 argv
[0] = "http-push";
407 if (flags
& TRANSPORT_PUSH_ALL
)
408 argv
[argc
++] = "--all";
409 if (flags
& TRANSPORT_PUSH_FORCE
)
410 argv
[argc
++] = "--force";
411 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
412 argv
[argc
++] = "--dry-run";
413 if (flags
& TRANSPORT_PUSH_VERBOSE
)
414 argv
[argc
++] = "--verbose";
415 argv
[argc
++] = transport
->url
;
417 argv
[argc
++] = *refspec
++;
419 err
= run_command_v_opt(argv
, RUN_GIT_CMD
);
421 case -ERR_RUN_COMMAND_FORK
:
422 error("unable to fork for %s", argv
[0]);
423 case -ERR_RUN_COMMAND_EXEC
:
424 error("unable to exec %s", argv
[0]);
426 case -ERR_RUN_COMMAND_WAITPID
:
427 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
428 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
429 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
430 error("%s died with strange error", argv
[0]);
435 static struct ref
*get_refs_via_curl(struct transport
*transport
, int for_push
)
437 struct strbuf buffer
= STRBUF_INIT
;
438 char *data
, *start
, *mid
;
444 struct ref
*refs
= NULL
;
445 struct ref
*ref
= NULL
;
446 struct ref
*last_ref
= NULL
;
448 struct walker
*walker
;
453 if (!transport
->data
)
454 transport
->data
= get_http_walker(transport
->url
,
457 walker
= transport
->data
;
459 refs_url
= xmalloc(strlen(transport
->url
) + 11);
460 sprintf(refs_url
, "%s/info/refs", transport
->url
);
462 http_ret
= http_get_strbuf(refs_url
, &buffer
, HTTP_NO_CACHE
);
466 case HTTP_MISSING_TARGET
:
467 die("%s not found: did you run git update-server-info on the"
468 " server?", refs_url
);
470 http_error(refs_url
, http_ret
);
471 die("HTTP request failed");
477 while (i
< buffer
.len
) {
482 if (data
[i
] == '\n') {
485 ref
= xmalloc(sizeof(struct ref
) +
486 strlen(ref_name
) + 1);
487 memset(ref
, 0, sizeof(struct ref
));
488 strcpy(ref
->name
, ref_name
);
489 get_sha1_hex(start
, ref
->old_sha1
);
493 last_ref
->next
= ref
;
500 strbuf_release(&buffer
);
502 ref
= alloc_ref("HEAD");
503 if (!walker
->fetch_ref(walker
, ref
) &&
504 !resolve_remote_symref(ref
, refs
)) {
511 strbuf_release(&buffer
);
516 static int fetch_objs_via_curl(struct transport
*transport
,
517 int nr_objs
, const struct ref
**to_fetch
)
519 if (!transport
->data
)
520 transport
->data
= get_http_walker(transport
->url
,
522 return fetch_objs_via_walker(transport
, nr_objs
, to_fetch
);
527 struct bundle_transport_data
{
529 struct bundle_header header
;
532 static struct ref
*get_refs_from_bundle(struct transport
*transport
, int for_push
)
534 struct bundle_transport_data
*data
= transport
->data
;
535 struct ref
*result
= NULL
;
543 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
545 die ("Could not read bundle '%s'.", transport
->url
);
546 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
547 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
548 struct ref
*ref
= alloc_ref(e
->name
);
549 hashcpy(ref
->old_sha1
, e
->sha1
);
556 static int fetch_refs_from_bundle(struct transport
*transport
,
557 int nr_heads
, const struct ref
**to_fetch
)
559 struct bundle_transport_data
*data
= transport
->data
;
560 return unbundle(&data
->header
, data
->fd
);
563 static int close_bundle(struct transport
*transport
)
565 struct bundle_transport_data
*data
= transport
->data
;
572 struct git_transport_data
{
575 unsigned followtags
: 1;
577 struct child_process
*conn
;
579 const char *uploadpack
;
580 const char *receivepack
;
581 struct extra_have_objects extra_have
;
584 static int set_git_option(struct transport
*connection
,
585 const char *name
, const char *value
)
587 struct git_transport_data
*data
= connection
->data
;
588 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
589 data
->uploadpack
= value
;
591 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
592 data
->receivepack
= value
;
594 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
595 data
->thin
= !!value
;
597 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
598 data
->followtags
= !!value
;
600 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
601 data
->keep
= !!value
;
603 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
607 data
->depth
= atoi(value
);
613 static int connect_setup(struct transport
*transport
, int for_push
, int verbose
)
615 struct git_transport_data
*data
= transport
->data
;
616 data
->conn
= git_connect(data
->fd
, transport
->url
,
617 for_push
? data
->receivepack
: data
->uploadpack
,
618 verbose
? CONNECT_VERBOSE
: 0);
622 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
)
624 struct git_transport_data
*data
= transport
->data
;
627 connect_setup(transport
, for_push
, 0);
628 get_remote_heads(data
->fd
[0], &refs
, 0, NULL
,
629 for_push
? REF_NORMAL
: 0, &data
->extra_have
);
634 static int fetch_refs_via_pack(struct transport
*transport
,
635 int nr_heads
, const struct ref
**to_fetch
)
637 struct git_transport_data
*data
= transport
->data
;
638 char **heads
= xmalloc(nr_heads
* sizeof(*heads
));
639 char **origh
= xmalloc(nr_heads
* sizeof(*origh
));
640 const struct ref
*refs
;
641 char *dest
= xstrdup(transport
->url
);
642 struct fetch_pack_args args
;
644 struct ref
*refs_tmp
= NULL
;
646 memset(&args
, 0, sizeof(args
));
647 args
.uploadpack
= data
->uploadpack
;
648 args
.keep_pack
= data
->keep
;
650 args
.use_thin_pack
= data
->thin
;
651 args
.include_tag
= data
->followtags
;
652 args
.verbose
= (transport
->verbose
> 0);
653 args
.quiet
= (transport
->verbose
< 0);
654 args
.no_progress
= args
.quiet
|| (!transport
->progress
&& !isatty(1));
655 args
.depth
= data
->depth
;
657 for (i
= 0; i
< nr_heads
; i
++)
658 origh
[i
] = heads
[i
] = xstrdup(to_fetch
[i
]->name
);
661 connect_setup(transport
, 0, 0);
662 get_remote_heads(data
->fd
[0], &refs_tmp
, 0, NULL
, 0, NULL
);
665 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
666 refs_tmp
? refs_tmp
: transport
->remote_refs
,
667 dest
, nr_heads
, heads
, &transport
->pack_lockfile
);
670 if (finish_connect(data
->conn
))
676 for (i
= 0; i
< nr_heads
; i
++)
681 return (refs
? 0 : -1);
684 static int push_had_errors(struct ref
*ref
)
686 for (; ref
; ref
= ref
->next
) {
687 switch (ref
->status
) {
688 case REF_STATUS_NONE
:
689 case REF_STATUS_UPTODATE
:
699 static int refs_pushed(struct ref
*ref
)
701 for (; ref
; ref
= ref
->next
) {
702 switch(ref
->status
) {
703 case REF_STATUS_NONE
:
704 case REF_STATUS_UPTODATE
:
713 static void update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
717 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
723 if (!remote_find_tracking(remote
, &rs
)) {
725 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
727 delete_ref(rs
.dst
, NULL
, 0);
729 update_ref("update by push", rs
.dst
,
730 ref
->new_sha1
, NULL
, 0, 0);
735 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
737 static void print_ref_status(char flag
, const char *summary
, struct ref
*to
, struct ref
*from
, const char *msg
, int porcelain
)
741 fprintf(stdout
, "%c\t%s:%s\t", flag
, from
->name
, to
->name
);
743 fprintf(stdout
, "%c\t:%s\t", flag
, to
->name
);
745 fprintf(stdout
, "%s (%s)\n", summary
, msg
);
747 fprintf(stdout
, "%s\n", summary
);
749 fprintf(stderr
, " %c %-*s ", flag
, SUMMARY_WIDTH
, summary
);
751 fprintf(stderr
, "%s -> %s", prettify_refname(from
->name
), prettify_refname(to
->name
));
753 fputs(prettify_refname(to
->name
), stderr
);
763 static const char *status_abbrev(unsigned char sha1
[20])
765 return find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
768 static void print_ok_ref_status(struct ref
*ref
, int porcelain
)
771 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
, porcelain
);
772 else if (is_null_sha1(ref
->old_sha1
))
773 print_ref_status('*',
774 (!prefixcmp(ref
->name
, "refs/tags/") ? "[new tag]" :
776 ref
, ref
->peer_ref
, NULL
, porcelain
);
782 strcpy(quickref
, status_abbrev(ref
->old_sha1
));
783 if (ref
->nonfastforward
) {
784 strcat(quickref
, "...");
786 msg
= "forced update";
788 strcat(quickref
, "..");
792 strcat(quickref
, status_abbrev(ref
->new_sha1
));
794 print_ref_status(type
, quickref
, ref
, ref
->peer_ref
, msg
, porcelain
);
798 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
, int porcelain
)
801 fprintf(stderr
, "To %s\n", dest
);
803 switch(ref
->status
) {
804 case REF_STATUS_NONE
:
805 print_ref_status('X', "[no match]", ref
, NULL
, NULL
, porcelain
);
807 case REF_STATUS_REJECT_NODELETE
:
808 print_ref_status('!', "[rejected]", ref
, NULL
,
809 "remote does not support deleting refs", porcelain
);
811 case REF_STATUS_UPTODATE
:
812 print_ref_status('=', "[up to date]", ref
,
813 ref
->peer_ref
, NULL
, porcelain
);
815 case REF_STATUS_REJECT_NONFASTFORWARD
:
816 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
817 "non-fast forward", porcelain
);
819 case REF_STATUS_REMOTE_REJECT
:
820 print_ref_status('!', "[remote rejected]", ref
,
821 ref
->deletion
? NULL
: ref
->peer_ref
,
822 ref
->remote_status
, porcelain
);
824 case REF_STATUS_EXPECTING_REPORT
:
825 print_ref_status('!', "[remote failure]", ref
,
826 ref
->deletion
? NULL
: ref
->peer_ref
,
827 "remote failed to report status", porcelain
);
830 print_ok_ref_status(ref
, porcelain
);
837 static void print_push_status(const char *dest
, struct ref
*refs
,
838 int verbose
, int porcelain
)
844 for (ref
= refs
; ref
; ref
= ref
->next
)
845 if (ref
->status
== REF_STATUS_UPTODATE
)
846 n
+= print_one_push_status(ref
, dest
, n
, porcelain
);
849 for (ref
= refs
; ref
; ref
= ref
->next
)
850 if (ref
->status
== REF_STATUS_OK
)
851 n
+= print_one_push_status(ref
, dest
, n
, porcelain
);
853 for (ref
= refs
; ref
; ref
= ref
->next
) {
854 if (ref
->status
!= REF_STATUS_NONE
&&
855 ref
->status
!= REF_STATUS_UPTODATE
&&
856 ref
->status
!= REF_STATUS_OK
)
857 n
+= print_one_push_status(ref
, dest
, n
, porcelain
);
861 static void verify_remote_names(int nr_heads
, const char **heads
)
865 for (i
= 0; i
< nr_heads
; i
++) {
866 const char *local
= heads
[i
];
867 const char *remote
= strrchr(heads
[i
], ':');
872 /* A matching refspec is okay. */
873 if (remote
== local
&& remote
[1] == '\0')
876 remote
= remote
? (remote
+ 1) : local
;
877 switch (check_ref_format(remote
)) {
879 case CHECK_REF_FORMAT_ONELEVEL
:
880 /* ok but a single level -- that is fine for
883 case CHECK_REF_FORMAT_WILDCARD
:
884 /* ok but ends with a pattern-match character */
887 die("remote part of refspec is not a valid name in %s",
892 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
894 struct git_transport_data
*data
= transport
->data
;
895 struct send_pack_args args
;
899 struct ref
*tmp_refs
;
900 connect_setup(transport
, 1, 0);
902 get_remote_heads(data
->fd
[0], &tmp_refs
, 0, NULL
, REF_NORMAL
,
906 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
907 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
908 args
.use_thin_pack
= data
->thin
;
909 args
.verbose
= !!(flags
& TRANSPORT_PUSH_VERBOSE
);
910 args
.quiet
= !!(flags
& TRANSPORT_PUSH_QUIET
);
911 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
913 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
918 ret
|= finish_connect(data
->conn
);
924 static int disconnect_git(struct transport
*transport
)
926 struct git_transport_data
*data
= transport
->data
;
928 packet_flush(data
->fd
[1]);
931 finish_connect(data
->conn
);
938 static int is_local(const char *url
)
940 const char *colon
= strchr(url
, ':');
941 const char *slash
= strchr(url
, '/');
942 return !colon
|| (slash
&& slash
< colon
) ||
943 has_dos_drive_prefix(url
);
946 static int is_file(const char *url
)
951 return S_ISREG(buf
.st_mode
);
954 struct transport
*transport_get(struct remote
*remote
, const char *url
)
956 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
958 ret
->remote
= remote
;
961 if (!prefixcmp(url
, "rsync:")) {
962 ret
->get_refs_list
= get_refs_via_rsync
;
963 ret
->fetch
= fetch_objs_via_rsync
;
964 ret
->push
= rsync_transport_push
;
966 } else if (!prefixcmp(url
, "http://")
967 || !prefixcmp(url
, "https://")
968 || !prefixcmp(url
, "ftp://")) {
970 error("git was compiled without libcurl support.");
972 ret
->get_refs_list
= get_refs_via_curl
;
973 ret
->fetch
= fetch_objs_via_curl
;
974 ret
->push
= curl_transport_push
;
976 ret
->disconnect
= disconnect_walker
;
978 } else if (is_local(url
) && is_file(url
)) {
979 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
981 ret
->get_refs_list
= get_refs_from_bundle
;
982 ret
->fetch
= fetch_refs_from_bundle
;
983 ret
->disconnect
= close_bundle
;
986 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
988 ret
->set_option
= set_git_option
;
989 ret
->get_refs_list
= get_refs_via_connect
;
990 ret
->fetch
= fetch_refs_via_pack
;
991 ret
->push_refs
= git_transport_push
;
992 ret
->disconnect
= disconnect_git
;
996 data
->uploadpack
= "git-upload-pack";
997 if (remote
&& remote
->uploadpack
)
998 data
->uploadpack
= remote
->uploadpack
;
999 data
->receivepack
= "git-receive-pack";
1000 if (remote
&& remote
->receivepack
)
1001 data
->receivepack
= remote
->receivepack
;
1007 int transport_set_option(struct transport
*transport
,
1008 const char *name
, const char *value
)
1010 if (transport
->set_option
)
1011 return transport
->set_option(transport
, name
, value
);
1015 int transport_push(struct transport
*transport
,
1016 int refspec_nr
, const char **refspec
, int flags
)
1018 verify_remote_names(refspec_nr
, refspec
);
1020 if (transport
->push
)
1021 return transport
->push(transport
, refspec_nr
, refspec
, flags
);
1022 if (transport
->push_refs
) {
1023 struct ref
*remote_refs
=
1024 transport
->get_refs_list(transport
, 1);
1025 struct ref
*local_refs
= get_local_heads();
1026 int match_flags
= MATCH_REFS_NONE
;
1027 int verbose
= flags
& TRANSPORT_PUSH_VERBOSE
;
1028 int quiet
= flags
& TRANSPORT_PUSH_QUIET
;
1029 int porcelain
= flags
& TRANSPORT_PUSH_PORCELAIN
;
1032 if (flags
& TRANSPORT_PUSH_ALL
)
1033 match_flags
|= MATCH_REFS_ALL
;
1034 if (flags
& TRANSPORT_PUSH_MIRROR
)
1035 match_flags
|= MATCH_REFS_MIRROR
;
1037 if (match_refs(local_refs
, &remote_refs
,
1038 refspec_nr
, refspec
, match_flags
)) {
1042 ret
= transport
->push_refs(transport
, remote_refs
, flags
);
1044 if (!quiet
|| push_had_errors(remote_refs
))
1045 print_push_status(transport
->url
, remote_refs
,
1046 verbose
| porcelain
, porcelain
);
1048 if (!(flags
& TRANSPORT_PUSH_DRY_RUN
)) {
1050 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1051 update_tracking_ref(transport
->remote
, ref
, verbose
);
1054 if (!ret
&& !refs_pushed(remote_refs
))
1055 fprintf(stderr
, "Everything up-to-date\n");
1061 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
1063 if (!transport
->remote_refs
)
1064 transport
->remote_refs
= transport
->get_refs_list(transport
, 0);
1065 return transport
->remote_refs
;
1068 int transport_fetch_refs(struct transport
*transport
, const struct ref
*refs
)
1071 int nr_heads
= 0, nr_alloc
= 0;
1072 const struct ref
**heads
= NULL
;
1073 const struct ref
*rm
;
1075 for (rm
= refs
; rm
; rm
= rm
->next
) {
1077 !hashcmp(rm
->peer_ref
->old_sha1
, rm
->old_sha1
))
1079 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1080 heads
[nr_heads
++] = rm
;
1083 rc
= transport
->fetch(transport
, nr_heads
, heads
);
1088 void transport_unlock_pack(struct transport
*transport
)
1090 if (transport
->pack_lockfile
) {
1091 unlink_or_warn(transport
->pack_lockfile
);
1092 free(transport
->pack_lockfile
);
1093 transport
->pack_lockfile
= NULL
;
1097 int transport_disconnect(struct transport
*transport
)
1100 if (transport
->disconnect
)
1101 ret
= transport
->disconnect(transport
);
1107 * Strip username (and password) from an url and return
1108 * it in a newly allocated string.
1110 char *transport_anonymize_url(const char *url
)
1112 char *anon_url
, *scheme_prefix
, *anon_part
;
1113 size_t anon_len
, prefix_len
= 0;
1115 anon_part
= strchr(url
, '@');
1116 if (is_local(url
) || !anon_part
)
1119 anon_len
= strlen(++anon_part
);
1120 scheme_prefix
= strstr(url
, "://");
1121 if (!scheme_prefix
) {
1122 if (!strchr(anon_part
, ':'))
1123 /* cannot be "me@there:/path/name" */
1127 /* make sure scheme is reasonable */
1128 for (cp
= url
; cp
< scheme_prefix
; cp
++) {
1131 case '+': case '.': case '-':
1140 /* @ past the first slash does not count */
1141 cp
= strchr(scheme_prefix
+ 3, '/');
1142 if (cp
&& cp
< anon_part
)
1144 prefix_len
= scheme_prefix
- url
+ 3;
1146 anon_url
= xcalloc(1, 1 + prefix_len
+ anon_len
);
1147 memcpy(anon_url
, url
, prefix_len
);
1148 memcpy(anon_url
+ prefix_len
, anon_part
, anon_len
);
1151 return xstrdup(url
);