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 struct ref
*get_refs_via_rsync(struct transport
*transport
, int for_push
)
143 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
144 struct ref dummy
, *tail
= &dummy
;
145 struct child_process rsync
;
152 /* copy the refs to the temporary directory */
154 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
155 if (!mkdtemp(temp_dir
.buf
))
156 die ("Could not make temporary directory");
157 temp_dir_len
= temp_dir
.len
;
159 strbuf_addstr(&buf
, transport
->url
);
160 strbuf_addstr(&buf
, "/refs");
162 memset(&rsync
, 0, sizeof(rsync
));
164 rsync
.stdout_to_stderr
= 1;
166 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
168 args
[3] = temp_dir
.buf
;
171 if (run_command(&rsync
))
172 die ("Could not run rsync to get refs");
175 strbuf_addstr(&buf
, transport
->url
);
176 strbuf_addstr(&buf
, "/packed-refs");
180 if (run_command(&rsync
))
181 die ("Could not run rsync to get refs");
183 /* read the copied refs */
185 strbuf_addstr(&temp_dir
, "/refs");
186 read_loose_refs(&temp_dir
, temp_dir_len
+ 1, &tail
);
187 strbuf_setlen(&temp_dir
, temp_dir_len
);
190 strbuf_addstr(&temp_dir
, "/packed-refs");
191 insert_packed_refs(temp_dir
.buf
, &tail
);
192 strbuf_setlen(&temp_dir
, temp_dir_len
);
194 if (remove_dir_recursively(&temp_dir
, 0))
195 warning ("Error removing temporary directory %s.",
198 strbuf_release(&buf
);
199 strbuf_release(&temp_dir
);
204 static int fetch_objs_via_rsync(struct transport
*transport
,
205 int nr_objs
, const struct ref
**to_fetch
)
207 struct strbuf buf
= STRBUF_INIT
;
208 struct child_process rsync
;
212 strbuf_addstr(&buf
, transport
->url
);
213 strbuf_addstr(&buf
, "/objects/");
215 memset(&rsync
, 0, sizeof(rsync
));
217 rsync
.stdout_to_stderr
= 1;
219 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
220 args
[2] = "--ignore-existing";
221 args
[3] = "--exclude";
224 args
[6] = get_object_directory();
227 /* NEEDSWORK: handle one level of alternates */
228 result
= run_command(&rsync
);
230 strbuf_release(&buf
);
235 static int write_one_ref(const char *name
, const unsigned char *sha1
,
236 int flags
, void *data
)
238 struct strbuf
*buf
= data
;
242 /* when called via for_each_ref(), flags is non-zero */
243 if (flags
&& prefixcmp(name
, "refs/heads/") &&
244 prefixcmp(name
, "refs/tags/"))
247 strbuf_addstr(buf
, name
);
248 if (safe_create_leading_directories(buf
->buf
) ||
249 !(f
= fopen(buf
->buf
, "w")) ||
250 fprintf(f
, "%s\n", sha1_to_hex(sha1
)) < 0 ||
252 return error("problems writing temporary file %s", buf
->buf
);
253 strbuf_setlen(buf
, len
);
257 static int write_refs_to_temp_dir(struct strbuf
*temp_dir
,
258 int refspec_nr
, const char **refspec
)
262 for (i
= 0; i
< refspec_nr
; i
++) {
263 unsigned char sha1
[20];
266 if (dwim_ref(refspec
[i
], strlen(refspec
[i
]), sha1
, &ref
) != 1)
267 return error("Could not get ref %s", refspec
[i
]);
269 if (write_one_ref(ref
, sha1
, 0, temp_dir
)) {
278 static int rsync_transport_push(struct transport
*transport
,
279 int refspec_nr
, const char **refspec
, int flags
)
281 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
283 struct child_process rsync
;
284 const char *args
[10];
286 if (flags
& TRANSPORT_PUSH_MIRROR
)
287 return error("rsync transport does not support mirror mode");
289 /* first push the objects */
291 strbuf_addstr(&buf
, transport
->url
);
292 strbuf_addch(&buf
, '/');
294 memset(&rsync
, 0, sizeof(rsync
));
296 rsync
.stdout_to_stderr
= 1;
300 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
301 args
[i
++] = "--dry-run";
302 if (transport
->verbose
> 0)
304 args
[i
++] = "--ignore-existing";
305 args
[i
++] = "--exclude";
307 args
[i
++] = get_object_directory();
311 if (run_command(&rsync
))
312 return error("Could not push objects to %s", transport
->url
);
314 /* copy the refs to the temporary directory; they could be packed. */
316 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
317 if (!mkdtemp(temp_dir
.buf
))
318 die ("Could not make temporary directory");
319 strbuf_addch(&temp_dir
, '/');
321 if (flags
& TRANSPORT_PUSH_ALL
) {
322 if (for_each_ref(write_one_ref
, &temp_dir
))
324 } else if (write_refs_to_temp_dir(&temp_dir
, refspec_nr
, refspec
))
328 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
329 args
[i
++] = "--dry-run";
330 if (!(flags
& TRANSPORT_PUSH_FORCE
))
331 args
[i
++] = "--ignore-existing";
332 args
[i
++] = temp_dir
.buf
;
333 args
[i
++] = transport
->url
;
335 if (run_command(&rsync
))
336 result
= error("Could not push to %s", transport
->url
);
338 if (remove_dir_recursively(&temp_dir
, 0))
339 warning ("Could not remove temporary directory %s.",
342 strbuf_release(&buf
);
343 strbuf_release(&temp_dir
);
348 /* Generic functions for using commit walkers */
350 #ifndef NO_CURL /* http fetch is the only user */
351 static int fetch_objs_via_walker(struct transport
*transport
,
352 int nr_objs
, const struct ref
**to_fetch
)
354 char *dest
= xstrdup(transport
->url
);
355 struct walker
*walker
= transport
->data
;
356 char **objs
= xmalloc(nr_objs
* sizeof(*objs
));
360 walker
->get_tree
= 1;
361 walker
->get_history
= 1;
362 walker
->get_verbosely
= transport
->verbose
>= 0;
363 walker
->get_recover
= 0;
365 for (i
= 0; i
< nr_objs
; i
++)
366 objs
[i
] = xstrdup(sha1_to_hex(to_fetch
[i
]->old_sha1
));
368 if (walker_fetch(walker
, nr_objs
, objs
, NULL
, NULL
))
369 die("Fetch failed.");
371 for (i
= 0; i
< nr_objs
; i
++)
379 static int disconnect_walker(struct transport
*transport
)
381 struct walker
*walker
= transport
->data
;
388 static int curl_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
)
394 if (flags
& TRANSPORT_PUSH_MIRROR
)
395 return error("http transport does not support mirror mode");
397 argv
= xmalloc((refspec_nr
+ 12) * sizeof(char *));
398 argv
[0] = "http-push";
400 if (flags
& TRANSPORT_PUSH_ALL
)
401 argv
[argc
++] = "--all";
402 if (flags
& TRANSPORT_PUSH_FORCE
)
403 argv
[argc
++] = "--force";
404 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
405 argv
[argc
++] = "--dry-run";
406 if (flags
& TRANSPORT_PUSH_VERBOSE
)
407 argv
[argc
++] = "--verbose";
408 argv
[argc
++] = transport
->url
;
410 argv
[argc
++] = *refspec
++;
412 err
= run_command_v_opt(argv
, RUN_GIT_CMD
);
414 case -ERR_RUN_COMMAND_FORK
:
415 error("unable to fork for %s", argv
[0]);
416 case -ERR_RUN_COMMAND_EXEC
:
417 error("unable to exec %s", argv
[0]);
419 case -ERR_RUN_COMMAND_WAITPID
:
420 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
421 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
422 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
423 error("%s died with strange error", argv
[0]);
428 static struct ref
*get_refs_via_curl(struct transport
*transport
, int for_push
)
430 struct strbuf buffer
= STRBUF_INIT
;
431 char *data
, *start
, *mid
;
436 struct active_request_slot
*slot
;
437 struct slot_results results
;
439 struct ref
*refs
= NULL
;
440 struct ref
*ref
= NULL
;
441 struct ref
*last_ref
= NULL
;
443 struct walker
*walker
;
448 if (!transport
->data
)
449 transport
->data
= get_http_walker(transport
->url
,
452 walker
= transport
->data
;
454 refs_url
= xmalloc(strlen(transport
->url
) + 11);
455 sprintf(refs_url
, "%s/info/refs", transport
->url
);
457 slot
= get_active_slot();
458 slot
->results
= &results
;
459 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
460 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
461 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, refs_url
);
462 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
464 if (start_active_slot(slot
)) {
465 run_active_slot(slot
);
466 if (results
.curl_result
!= CURLE_OK
) {
467 strbuf_release(&buffer
);
468 if (missing_target(&results
))
469 die("%s not found: did you run git update-server-info on the server?", refs_url
);
471 die("%s download error - %s", refs_url
, curl_errorstr
);
474 strbuf_release(&buffer
);
475 die("Unable to start HTTP request");
481 while (i
< buffer
.len
) {
486 if (data
[i
] == '\n') {
489 ref
= xmalloc(sizeof(struct ref
) +
490 strlen(ref_name
) + 1);
491 memset(ref
, 0, sizeof(struct ref
));
492 strcpy(ref
->name
, ref_name
);
493 get_sha1_hex(start
, ref
->old_sha1
);
497 last_ref
->next
= ref
;
504 strbuf_release(&buffer
);
506 ref
= alloc_ref("HEAD");
507 if (!walker
->fetch_ref(walker
, ref
) &&
508 !resolve_remote_symref(ref
, refs
)) {
518 static int fetch_objs_via_curl(struct transport
*transport
,
519 int nr_objs
, const struct ref
**to_fetch
)
521 if (!transport
->data
)
522 transport
->data
= get_http_walker(transport
->url
,
524 return fetch_objs_via_walker(transport
, nr_objs
, to_fetch
);
529 struct bundle_transport_data
{
531 struct bundle_header header
;
534 static struct ref
*get_refs_from_bundle(struct transport
*transport
, int for_push
)
536 struct bundle_transport_data
*data
= transport
->data
;
537 struct ref
*result
= NULL
;
545 data
->fd
= read_bundle_header(transport
->url
, &data
->header
);
547 die ("Could not read bundle '%s'.", transport
->url
);
548 for (i
= 0; i
< data
->header
.references
.nr
; i
++) {
549 struct ref_list_entry
*e
= data
->header
.references
.list
+ i
;
550 struct ref
*ref
= alloc_ref(e
->name
);
551 hashcpy(ref
->old_sha1
, e
->sha1
);
558 static int fetch_refs_from_bundle(struct transport
*transport
,
559 int nr_heads
, const struct ref
**to_fetch
)
561 struct bundle_transport_data
*data
= transport
->data
;
562 return unbundle(&data
->header
, data
->fd
);
565 static int close_bundle(struct transport
*transport
)
567 struct bundle_transport_data
*data
= transport
->data
;
574 struct git_transport_data
{
577 unsigned followtags
: 1;
579 struct child_process
*conn
;
581 const char *uploadpack
;
582 const char *receivepack
;
583 struct extra_have_objects extra_have
;
586 static int set_git_option(struct transport
*connection
,
587 const char *name
, const char *value
)
589 struct git_transport_data
*data
= connection
->data
;
590 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
591 data
->uploadpack
= value
;
593 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
594 data
->receivepack
= value
;
596 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
597 data
->thin
= !!value
;
599 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
600 data
->followtags
= !!value
;
602 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
603 data
->keep
= !!value
;
605 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
609 data
->depth
= atoi(value
);
615 static int connect_setup(struct transport
*transport
, int for_push
, int verbose
)
617 struct git_transport_data
*data
= transport
->data
;
618 data
->conn
= git_connect(data
->fd
, transport
->url
,
619 for_push
? data
->receivepack
: data
->uploadpack
,
620 verbose
? CONNECT_VERBOSE
: 0);
624 static struct ref
*get_refs_via_connect(struct transport
*transport
, int for_push
)
626 struct git_transport_data
*data
= transport
->data
;
629 connect_setup(transport
, for_push
, 0);
630 get_remote_heads(data
->fd
[0], &refs
, 0, NULL
,
631 for_push
? REF_NORMAL
: 0, &data
->extra_have
);
636 static int fetch_refs_via_pack(struct transport
*transport
,
637 int nr_heads
, const struct ref
**to_fetch
)
639 struct git_transport_data
*data
= transport
->data
;
640 char **heads
= xmalloc(nr_heads
* sizeof(*heads
));
641 char **origh
= xmalloc(nr_heads
* sizeof(*origh
));
642 const struct ref
*refs
;
643 char *dest
= xstrdup(transport
->url
);
644 struct fetch_pack_args args
;
646 struct ref
*refs_tmp
= NULL
;
648 memset(&args
, 0, sizeof(args
));
649 args
.uploadpack
= data
->uploadpack
;
650 args
.keep_pack
= data
->keep
;
652 args
.use_thin_pack
= data
->thin
;
653 args
.include_tag
= data
->followtags
;
654 args
.verbose
= (transport
->verbose
> 0);
655 args
.quiet
= (transport
->verbose
< 0);
656 args
.no_progress
= args
.quiet
|| (!transport
->progress
&& !isatty(1));
657 args
.depth
= data
->depth
;
659 for (i
= 0; i
< nr_heads
; i
++)
660 origh
[i
] = heads
[i
] = xstrdup(to_fetch
[i
]->name
);
663 connect_setup(transport
, 0, 0);
664 get_remote_heads(data
->fd
[0], &refs_tmp
, 0, NULL
, 0, NULL
);
667 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
668 refs_tmp
? refs_tmp
: transport
->remote_refs
,
669 dest
, nr_heads
, heads
, &transport
->pack_lockfile
);
672 if (finish_connect(data
->conn
))
678 for (i
= 0; i
< nr_heads
; i
++)
683 return (refs
? 0 : -1);
686 static int refs_pushed(struct ref
*ref
)
688 for (; ref
; ref
= ref
->next
) {
689 switch(ref
->status
) {
690 case REF_STATUS_NONE
:
691 case REF_STATUS_UPTODATE
:
700 static void update_tracking_ref(struct remote
*remote
, struct ref
*ref
, int verbose
)
704 if (ref
->status
!= REF_STATUS_OK
&& ref
->status
!= REF_STATUS_UPTODATE
)
710 if (!remote_find_tracking(remote
, &rs
)) {
712 fprintf(stderr
, "updating local tracking ref '%s'\n", rs
.dst
);
714 delete_ref(rs
.dst
, NULL
, 0);
716 update_ref("update by push", rs
.dst
,
717 ref
->new_sha1
, NULL
, 0, 0);
722 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
724 static void print_ref_status(char flag
, const char *summary
, struct ref
*to
, struct ref
*from
, const char *msg
)
726 fprintf(stderr
, " %c %-*s ", flag
, SUMMARY_WIDTH
, summary
);
728 fprintf(stderr
, "%s -> %s", prettify_ref(from
), prettify_ref(to
));
730 fputs(prettify_ref(to
), stderr
);
739 static const char *status_abbrev(unsigned char sha1
[20])
741 return find_unique_abbrev(sha1
, DEFAULT_ABBREV
);
744 static void print_ok_ref_status(struct ref
*ref
)
747 print_ref_status('-', "[deleted]", ref
, NULL
, NULL
);
748 else if (is_null_sha1(ref
->old_sha1
))
749 print_ref_status('*',
750 (!prefixcmp(ref
->name
, "refs/tags/") ? "[new tag]" :
752 ref
, ref
->peer_ref
, NULL
);
758 strcpy(quickref
, status_abbrev(ref
->old_sha1
));
759 if (ref
->nonfastforward
) {
760 strcat(quickref
, "...");
762 msg
= "forced update";
764 strcat(quickref
, "..");
768 strcat(quickref
, status_abbrev(ref
->new_sha1
));
770 print_ref_status(type
, quickref
, ref
, ref
->peer_ref
, msg
);
774 static int print_one_push_status(struct ref
*ref
, const char *dest
, int count
)
777 fprintf(stderr
, "To %s\n", dest
);
779 switch(ref
->status
) {
780 case REF_STATUS_NONE
:
781 print_ref_status('X', "[no match]", ref
, NULL
, NULL
);
783 case REF_STATUS_REJECT_NODELETE
:
784 print_ref_status('!', "[rejected]", ref
, NULL
,
785 "remote does not support deleting refs");
787 case REF_STATUS_UPTODATE
:
788 print_ref_status('=', "[up to date]", ref
,
789 ref
->peer_ref
, NULL
);
791 case REF_STATUS_REJECT_NONFASTFORWARD
:
792 print_ref_status('!', "[rejected]", ref
, ref
->peer_ref
,
795 case REF_STATUS_REMOTE_REJECT
:
796 print_ref_status('!', "[remote rejected]", ref
,
797 ref
->deletion
? NULL
: ref
->peer_ref
,
800 case REF_STATUS_EXPECTING_REPORT
:
801 print_ref_status('!', "[remote failure]", ref
,
802 ref
->deletion
? NULL
: ref
->peer_ref
,
803 "remote failed to report status");
806 print_ok_ref_status(ref
);
813 static void print_push_status(const char *dest
, struct ref
*refs
, int verbose
)
819 for (ref
= refs
; ref
; ref
= ref
->next
)
820 if (ref
->status
== REF_STATUS_UPTODATE
)
821 n
+= print_one_push_status(ref
, dest
, n
);
824 for (ref
= refs
; ref
; ref
= ref
->next
)
825 if (ref
->status
== REF_STATUS_OK
)
826 n
+= print_one_push_status(ref
, dest
, n
);
828 for (ref
= refs
; ref
; ref
= ref
->next
) {
829 if (ref
->status
!= REF_STATUS_NONE
&&
830 ref
->status
!= REF_STATUS_UPTODATE
&&
831 ref
->status
!= REF_STATUS_OK
)
832 n
+= print_one_push_status(ref
, dest
, n
);
836 static void verify_remote_names(int nr_heads
, const char **heads
)
840 for (i
= 0; i
< nr_heads
; i
++) {
841 const char *local
= heads
[i
];
842 const char *remote
= strrchr(heads
[i
], ':');
847 /* A matching refspec is okay. */
848 if (remote
== local
&& remote
[1] == '\0')
851 remote
= remote
? (remote
+ 1) : local
;
852 switch (check_ref_format(remote
)) {
854 case CHECK_REF_FORMAT_ONELEVEL
:
855 /* ok but a single level -- that is fine for
858 case CHECK_REF_FORMAT_WILDCARD
:
859 /* ok but ends with a pattern-match character */
862 die("remote part of refspec is not a valid name in %s",
867 static int git_transport_push(struct transport
*transport
, struct ref
*remote_refs
, int flags
)
869 struct git_transport_data
*data
= transport
->data
;
870 struct send_pack_args args
;
874 struct ref
*tmp_refs
;
875 connect_setup(transport
, 1, 0);
877 get_remote_heads(data
->fd
[0], &tmp_refs
, 0, NULL
, REF_NORMAL
,
881 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
882 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
883 args
.use_thin_pack
= data
->thin
;
884 args
.verbose
= !!(flags
& TRANSPORT_PUSH_VERBOSE
);
885 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
887 ret
= send_pack(&args
, data
->fd
, data
->conn
, remote_refs
,
892 ret
|= finish_connect(data
->conn
);
898 static int disconnect_git(struct transport
*transport
)
900 struct git_transport_data
*data
= transport
->data
;
902 packet_flush(data
->fd
[1]);
905 finish_connect(data
->conn
);
912 static int is_local(const char *url
)
914 const char *colon
= strchr(url
, ':');
915 const char *slash
= strchr(url
, '/');
916 return !colon
|| (slash
&& slash
< colon
) ||
917 has_dos_drive_prefix(url
);
920 static int is_file(const char *url
)
925 return S_ISREG(buf
.st_mode
);
928 struct transport
*transport_get(struct remote
*remote
, const char *url
)
930 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
932 ret
->remote
= remote
;
935 if (!prefixcmp(url
, "rsync://")) {
936 ret
->get_refs_list
= get_refs_via_rsync
;
937 ret
->fetch
= fetch_objs_via_rsync
;
938 ret
->push
= rsync_transport_push
;
940 } else if (!prefixcmp(url
, "http://")
941 || !prefixcmp(url
, "https://")
942 || !prefixcmp(url
, "ftp://")) {
944 error("git was compiled without libcurl support.");
946 ret
->get_refs_list
= get_refs_via_curl
;
947 ret
->fetch
= fetch_objs_via_curl
;
948 ret
->push
= curl_transport_push
;
950 ret
->disconnect
= disconnect_walker
;
952 } else if (is_local(url
) && is_file(url
)) {
953 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
955 ret
->get_refs_list
= get_refs_from_bundle
;
956 ret
->fetch
= fetch_refs_from_bundle
;
957 ret
->disconnect
= close_bundle
;
960 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
962 ret
->set_option
= set_git_option
;
963 ret
->get_refs_list
= get_refs_via_connect
;
964 ret
->fetch
= fetch_refs_via_pack
;
965 ret
->push_refs
= git_transport_push
;
966 ret
->disconnect
= disconnect_git
;
970 data
->uploadpack
= "git-upload-pack";
971 if (remote
&& remote
->uploadpack
)
972 data
->uploadpack
= remote
->uploadpack
;
973 data
->receivepack
= "git-receive-pack";
974 if (remote
&& remote
->receivepack
)
975 data
->receivepack
= remote
->receivepack
;
981 int transport_set_option(struct transport
*transport
,
982 const char *name
, const char *value
)
984 if (transport
->set_option
)
985 return transport
->set_option(transport
, name
, value
);
989 int transport_push(struct transport
*transport
,
990 int refspec_nr
, const char **refspec
, int flags
)
992 verify_remote_names(refspec_nr
, refspec
);
995 return transport
->push(transport
, refspec_nr
, refspec
, flags
);
996 if (transport
->push_refs
) {
997 struct ref
*remote_refs
=
998 transport
->get_refs_list(transport
, 1);
999 struct ref
**remote_tail
;
1000 struct ref
*local_refs
= get_local_heads();
1001 int match_flags
= MATCH_REFS_NONE
;
1002 int verbose
= flags
& TRANSPORT_PUSH_VERBOSE
;
1005 if (flags
& TRANSPORT_PUSH_ALL
)
1006 match_flags
|= MATCH_REFS_ALL
;
1007 if (flags
& TRANSPORT_PUSH_MIRROR
)
1008 match_flags
|= MATCH_REFS_MIRROR
;
1010 remote_tail
= &remote_refs
;
1011 while (*remote_tail
)
1012 remote_tail
= &((*remote_tail
)->next
);
1013 if (match_refs(local_refs
, remote_refs
, &remote_tail
,
1014 refspec_nr
, refspec
, match_flags
)) {
1018 ret
= transport
->push_refs(transport
, remote_refs
, flags
);
1020 print_push_status(transport
->url
, remote_refs
, verbose
);
1022 if (!(flags
& TRANSPORT_PUSH_DRY_RUN
)) {
1024 for (ref
= remote_refs
; ref
; ref
= ref
->next
)
1025 update_tracking_ref(transport
->remote
, ref
, verbose
);
1028 if (!ret
&& !refs_pushed(remote_refs
))
1029 fprintf(stderr
, "Everything up-to-date\n");
1035 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
1037 if (!transport
->remote_refs
)
1038 transport
->remote_refs
= transport
->get_refs_list(transport
, 0);
1039 return transport
->remote_refs
;
1042 int transport_fetch_refs(struct transport
*transport
, const struct ref
*refs
)
1045 int nr_heads
= 0, nr_alloc
= 0;
1046 const struct ref
**heads
= NULL
;
1047 const struct ref
*rm
;
1049 for (rm
= refs
; rm
; rm
= rm
->next
) {
1051 !hashcmp(rm
->peer_ref
->old_sha1
, rm
->old_sha1
))
1053 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
1054 heads
[nr_heads
++] = rm
;
1057 rc
= transport
->fetch(transport
, nr_heads
, heads
);
1062 void transport_unlock_pack(struct transport
*transport
)
1064 if (transport
->pack_lockfile
) {
1065 unlink(transport
->pack_lockfile
);
1066 free(transport
->pack_lockfile
);
1067 transport
->pack_lockfile
= NULL
;
1071 int transport_disconnect(struct transport
*transport
)
1074 if (transport
->disconnect
)
1075 ret
= transport
->disconnect(transport
);