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
)
148 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
149 struct ref dummy
, *tail
= &dummy
;
150 struct child_process rsync
;
154 /* copy the refs to the temporary directory */
156 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
157 if (!mkdtemp(temp_dir
.buf
))
158 die ("Could not make temporary directory");
159 temp_dir_len
= temp_dir
.len
;
161 strbuf_addstr(&buf
, rsync_url(transport
->url
));
162 strbuf_addstr(&buf
, "/refs");
164 memset(&rsync
, 0, sizeof(rsync
));
166 rsync
.stdout_to_stderr
= 1;
168 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
170 args
[3] = temp_dir
.buf
;
173 if (run_command(&rsync
))
174 die ("Could not run rsync to get refs");
177 strbuf_addstr(&buf
, rsync_url(transport
->url
));
178 strbuf_addstr(&buf
, "/packed-refs");
182 if (run_command(&rsync
))
183 die ("Could not run rsync to get refs");
185 /* read the copied refs */
187 strbuf_addstr(&temp_dir
, "/refs");
188 read_loose_refs(&temp_dir
, temp_dir_len
+ 1, &tail
);
189 strbuf_setlen(&temp_dir
, temp_dir_len
);
192 strbuf_addstr(&temp_dir
, "/packed-refs");
193 insert_packed_refs(temp_dir
.buf
, &tail
);
194 strbuf_setlen(&temp_dir
, temp_dir_len
);
196 if (remove_dir_recursively(&temp_dir
, 0))
197 warning ("Error removing temporary directory %s.",
200 strbuf_release(&buf
);
201 strbuf_release(&temp_dir
);
206 static int fetch_objs_via_rsync(struct transport
*transport
,
207 int nr_objs
, const struct ref
**to_fetch
)
209 struct strbuf buf
= STRBUF_INIT
;
210 struct child_process rsync
;
214 strbuf_addstr(&buf
, rsync_url(transport
->url
));
215 strbuf_addstr(&buf
, "/objects/");
217 memset(&rsync
, 0, sizeof(rsync
));
219 rsync
.stdout_to_stderr
= 1;
221 args
[1] = (transport
->verbose
> 0) ? "-rv" : "-r";
222 args
[2] = "--ignore-existing";
223 args
[3] = "--exclude";
226 args
[6] = get_object_directory();
229 /* NEEDSWORK: handle one level of alternates */
230 result
= run_command(&rsync
);
232 strbuf_release(&buf
);
237 static int write_one_ref(const char *name
, const unsigned char *sha1
,
238 int flags
, void *data
)
240 struct strbuf
*buf
= data
;
244 /* when called via for_each_ref(), flags is non-zero */
245 if (flags
&& prefixcmp(name
, "refs/heads/") &&
246 prefixcmp(name
, "refs/tags/"))
249 strbuf_addstr(buf
, name
);
250 if (safe_create_leading_directories(buf
->buf
) ||
251 !(f
= fopen(buf
->buf
, "w")) ||
252 fprintf(f
, "%s\n", sha1_to_hex(sha1
)) < 0 ||
254 return error("problems writing temporary file %s", buf
->buf
);
255 strbuf_setlen(buf
, len
);
259 static int write_refs_to_temp_dir(struct strbuf
*temp_dir
,
260 int refspec_nr
, const char **refspec
)
264 for (i
= 0; i
< refspec_nr
; i
++) {
265 unsigned char sha1
[20];
268 if (dwim_ref(refspec
[i
], strlen(refspec
[i
]), sha1
, &ref
) != 1)
269 return error("Could not get ref %s", refspec
[i
]);
271 if (write_one_ref(ref
, sha1
, 0, temp_dir
)) {
280 static int rsync_transport_push(struct transport
*transport
,
281 int refspec_nr
, const char **refspec
, int flags
)
283 struct strbuf buf
= STRBUF_INIT
, temp_dir
= STRBUF_INIT
;
285 struct child_process rsync
;
286 const char *args
[10];
288 if (flags
& TRANSPORT_PUSH_MIRROR
)
289 return error("rsync transport does not support mirror mode");
291 /* first push the objects */
293 strbuf_addstr(&buf
, rsync_url(transport
->url
));
294 strbuf_addch(&buf
, '/');
296 memset(&rsync
, 0, sizeof(rsync
));
298 rsync
.stdout_to_stderr
= 1;
302 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
303 args
[i
++] = "--dry-run";
304 if (transport
->verbose
> 0)
306 args
[i
++] = "--ignore-existing";
307 args
[i
++] = "--exclude";
309 args
[i
++] = get_object_directory();
313 if (run_command(&rsync
))
314 return error("Could not push objects to %s",
315 rsync_url(transport
->url
));
317 /* copy the refs to the temporary directory; they could be packed. */
319 strbuf_addstr(&temp_dir
, git_path("rsync-refs-XXXXXX"));
320 if (!mkdtemp(temp_dir
.buf
))
321 die ("Could not make temporary directory");
322 strbuf_addch(&temp_dir
, '/');
324 if (flags
& TRANSPORT_PUSH_ALL
) {
325 if (for_each_ref(write_one_ref
, &temp_dir
))
327 } else if (write_refs_to_temp_dir(&temp_dir
, refspec_nr
, refspec
))
331 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
332 args
[i
++] = "--dry-run";
333 if (!(flags
& TRANSPORT_PUSH_FORCE
))
334 args
[i
++] = "--ignore-existing";
335 args
[i
++] = temp_dir
.buf
;
336 args
[i
++] = rsync_url(transport
->url
);
338 if (run_command(&rsync
))
339 result
= error("Could not push to %s",
340 rsync_url(transport
->url
));
342 if (remove_dir_recursively(&temp_dir
, 0))
343 warning ("Could not remove temporary directory %s.",
346 strbuf_release(&buf
);
347 strbuf_release(&temp_dir
);
352 /* Generic functions for using commit walkers */
354 #ifndef NO_CURL /* http fetch is the only user */
355 static int fetch_objs_via_walker(struct transport
*transport
,
356 int nr_objs
, const struct ref
**to_fetch
)
358 char *dest
= xstrdup(transport
->url
);
359 struct walker
*walker
= transport
->data
;
360 char **objs
= xmalloc(nr_objs
* sizeof(*objs
));
364 walker
->get_tree
= 1;
365 walker
->get_history
= 1;
366 walker
->get_verbosely
= transport
->verbose
>= 0;
367 walker
->get_recover
= 0;
369 for (i
= 0; i
< nr_objs
; i
++)
370 objs
[i
] = xstrdup(sha1_to_hex(to_fetch
[i
]->old_sha1
));
372 if (walker_fetch(walker
, nr_objs
, objs
, NULL
, NULL
))
373 die("Fetch failed.");
375 for (i
= 0; i
< nr_objs
; i
++)
383 static int disconnect_walker(struct transport
*transport
)
385 struct walker
*walker
= transport
->data
;
392 static int curl_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
)
398 if (flags
& TRANSPORT_PUSH_MIRROR
)
399 return error("http transport does not support mirror mode");
401 argv
= xmalloc((refspec_nr
+ 12) * sizeof(char *));
402 argv
[0] = "http-push";
404 if (flags
& TRANSPORT_PUSH_ALL
)
405 argv
[argc
++] = "--all";
406 if (flags
& TRANSPORT_PUSH_FORCE
)
407 argv
[argc
++] = "--force";
408 if (flags
& TRANSPORT_PUSH_DRY_RUN
)
409 argv
[argc
++] = "--dry-run";
410 if (flags
& TRANSPORT_PUSH_VERBOSE
)
411 argv
[argc
++] = "--verbose";
412 argv
[argc
++] = transport
->url
;
414 argv
[argc
++] = *refspec
++;
416 err
= run_command_v_opt(argv
, RUN_GIT_CMD
);
418 case -ERR_RUN_COMMAND_FORK
:
419 error("unable to fork for %s", argv
[0]);
420 case -ERR_RUN_COMMAND_EXEC
:
421 error("unable to exec %s", argv
[0]);
423 case -ERR_RUN_COMMAND_WAITPID
:
424 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID
:
425 case -ERR_RUN_COMMAND_WAITPID_SIGNAL
:
426 case -ERR_RUN_COMMAND_WAITPID_NOEXIT
:
427 error("%s died with strange error", argv
[0]);
432 static struct ref
*get_refs_via_curl(struct transport
*transport
)
434 struct strbuf buffer
= STRBUF_INIT
;
435 char *data
, *start
, *mid
;
440 struct active_request_slot
*slot
;
441 struct slot_results results
;
443 struct ref
*refs
= NULL
;
444 struct ref
*ref
= NULL
;
445 struct ref
*last_ref
= NULL
;
447 struct walker
*walker
;
449 if (!transport
->data
)
450 transport
->data
= get_http_walker(transport
->url
,
453 walker
= transport
->data
;
455 refs_url
= xmalloc(strlen(transport
->url
) + 11);
456 sprintf(refs_url
, "%s/info/refs", transport
->url
);
458 slot
= get_active_slot();
459 slot
->results
= &results
;
460 curl_easy_setopt(slot
->curl
, CURLOPT_FILE
, &buffer
);
461 curl_easy_setopt(slot
->curl
, CURLOPT_WRITEFUNCTION
, fwrite_buffer
);
462 curl_easy_setopt(slot
->curl
, CURLOPT_URL
, refs_url
);
463 curl_easy_setopt(slot
->curl
, CURLOPT_HTTPHEADER
, NULL
);
465 if (start_active_slot(slot
)) {
466 run_active_slot(slot
);
467 if (results
.curl_result
!= CURLE_OK
) {
468 strbuf_release(&buffer
);
469 if (missing_target(&results
))
470 die("%s not found: did you run git update-server-info on the server?", refs_url
);
472 die("%s download error - %s", refs_url
, curl_errorstr
);
475 strbuf_release(&buffer
);
476 die("Unable to start HTTP request");
482 while (i
< buffer
.len
) {
487 if (data
[i
] == '\n') {
490 ref
= xmalloc(sizeof(struct ref
) +
491 strlen(ref_name
) + 1);
492 memset(ref
, 0, sizeof(struct ref
));
493 strcpy(ref
->name
, ref_name
);
494 get_sha1_hex(start
, ref
->old_sha1
);
498 last_ref
->next
= ref
;
505 strbuf_release(&buffer
);
507 ref
= alloc_ref("HEAD");
508 if (!walker
->fetch_ref(walker
, ref
) &&
509 !resolve_remote_symref(ref
, refs
)) {
519 static int fetch_objs_via_curl(struct transport
*transport
,
520 int nr_objs
, const struct ref
**to_fetch
)
522 if (!transport
->data
)
523 transport
->data
= get_http_walker(transport
->url
,
525 return fetch_objs_via_walker(transport
, nr_objs
, to_fetch
);
530 struct bundle_transport_data
{
532 struct bundle_header header
;
535 static struct ref
*get_refs_from_bundle(struct transport
*transport
)
537 struct bundle_transport_data
*data
= transport
->data
;
538 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
;
583 static int set_git_option(struct transport
*connection
,
584 const char *name
, const char *value
)
586 struct git_transport_data
*data
= connection
->data
;
587 if (!strcmp(name
, TRANS_OPT_UPLOADPACK
)) {
588 data
->uploadpack
= value
;
590 } else if (!strcmp(name
, TRANS_OPT_RECEIVEPACK
)) {
591 data
->receivepack
= value
;
593 } else if (!strcmp(name
, TRANS_OPT_THIN
)) {
594 data
->thin
= !!value
;
596 } else if (!strcmp(name
, TRANS_OPT_FOLLOWTAGS
)) {
597 data
->followtags
= !!value
;
599 } else if (!strcmp(name
, TRANS_OPT_KEEP
)) {
600 data
->keep
= !!value
;
602 } else if (!strcmp(name
, TRANS_OPT_DEPTH
)) {
606 data
->depth
= atoi(value
);
612 static int connect_setup(struct transport
*transport
)
614 struct git_transport_data
*data
= transport
->data
;
615 data
->conn
= git_connect(data
->fd
, transport
->url
, data
->uploadpack
, 0);
619 static struct ref
*get_refs_via_connect(struct transport
*transport
)
621 struct git_transport_data
*data
= transport
->data
;
624 connect_setup(transport
);
625 get_remote_heads(data
->fd
[0], &refs
, 0, NULL
, 0, NULL
);
630 static int fetch_refs_via_pack(struct transport
*transport
,
631 int nr_heads
, const struct ref
**to_fetch
)
633 struct git_transport_data
*data
= transport
->data
;
634 char **heads
= xmalloc(nr_heads
* sizeof(*heads
));
635 char **origh
= xmalloc(nr_heads
* sizeof(*origh
));
636 const struct ref
*refs
;
637 char *dest
= xstrdup(transport
->url
);
638 struct fetch_pack_args args
;
640 struct ref
*refs_tmp
= NULL
;
642 memset(&args
, 0, sizeof(args
));
643 args
.uploadpack
= data
->uploadpack
;
644 args
.keep_pack
= data
->keep
;
646 args
.use_thin_pack
= data
->thin
;
647 args
.include_tag
= data
->followtags
;
648 args
.verbose
= (transport
->verbose
> 0);
649 args
.quiet
= (transport
->verbose
< 0);
650 args
.no_progress
= args
.quiet
|| (!transport
->progress
&& !isatty(1));
651 args
.depth
= data
->depth
;
653 for (i
= 0; i
< nr_heads
; i
++)
654 origh
[i
] = heads
[i
] = xstrdup(to_fetch
[i
]->name
);
657 connect_setup(transport
);
658 get_remote_heads(data
->fd
[0], &refs_tmp
, 0, NULL
, 0, NULL
);
661 refs
= fetch_pack(&args
, data
->fd
, data
->conn
,
662 refs_tmp
? refs_tmp
: transport
->remote_refs
,
663 dest
, nr_heads
, heads
, &transport
->pack_lockfile
);
666 if (finish_connect(data
->conn
))
672 for (i
= 0; i
< nr_heads
; i
++)
677 return (refs
? 0 : -1);
680 static int git_transport_push(struct transport
*transport
, int refspec_nr
, const char **refspec
, int flags
)
682 struct git_transport_data
*data
= transport
->data
;
683 struct send_pack_args args
;
685 args
.receivepack
= data
->receivepack
;
686 args
.send_all
= !!(flags
& TRANSPORT_PUSH_ALL
);
687 args
.send_mirror
= !!(flags
& TRANSPORT_PUSH_MIRROR
);
688 args
.force_update
= !!(flags
& TRANSPORT_PUSH_FORCE
);
689 args
.use_thin_pack
= data
->thin
;
690 args
.verbose
= !!(flags
& TRANSPORT_PUSH_VERBOSE
);
691 args
.dry_run
= !!(flags
& TRANSPORT_PUSH_DRY_RUN
);
693 return send_pack(&args
, transport
->url
, transport
->remote
, refspec_nr
, refspec
);
696 static int disconnect_git(struct transport
*transport
)
698 struct git_transport_data
*data
= transport
->data
;
700 packet_flush(data
->fd
[1]);
703 finish_connect(data
->conn
);
710 static int is_local(const char *url
)
712 const char *colon
= strchr(url
, ':');
713 const char *slash
= strchr(url
, '/');
714 return !colon
|| (slash
&& slash
< colon
) ||
715 has_dos_drive_prefix(url
);
718 static int is_file(const char *url
)
723 return S_ISREG(buf
.st_mode
);
726 struct transport
*transport_get(struct remote
*remote
, const char *url
)
728 struct transport
*ret
= xcalloc(1, sizeof(*ret
));
730 ret
->remote
= remote
;
733 if (!prefixcmp(url
, "rsync:")) {
734 ret
->get_refs_list
= get_refs_via_rsync
;
735 ret
->fetch
= fetch_objs_via_rsync
;
736 ret
->push
= rsync_transport_push
;
738 } else if (!prefixcmp(url
, "http://")
739 || !prefixcmp(url
, "https://")
740 || !prefixcmp(url
, "ftp://")) {
742 error("git was compiled without libcurl support.");
744 ret
->get_refs_list
= get_refs_via_curl
;
745 ret
->fetch
= fetch_objs_via_curl
;
746 ret
->push
= curl_transport_push
;
748 ret
->disconnect
= disconnect_walker
;
750 } else if (is_local(url
) && is_file(url
)) {
751 struct bundle_transport_data
*data
= xcalloc(1, sizeof(*data
));
753 ret
->get_refs_list
= get_refs_from_bundle
;
754 ret
->fetch
= fetch_refs_from_bundle
;
755 ret
->disconnect
= close_bundle
;
758 struct git_transport_data
*data
= xcalloc(1, sizeof(*data
));
760 ret
->set_option
= set_git_option
;
761 ret
->get_refs_list
= get_refs_via_connect
;
762 ret
->fetch
= fetch_refs_via_pack
;
763 ret
->push
= git_transport_push
;
764 ret
->disconnect
= disconnect_git
;
768 data
->uploadpack
= "git-upload-pack";
769 if (remote
&& remote
->uploadpack
)
770 data
->uploadpack
= remote
->uploadpack
;
771 data
->receivepack
= "git-receive-pack";
772 if (remote
&& remote
->receivepack
)
773 data
->receivepack
= remote
->receivepack
;
779 int transport_set_option(struct transport
*transport
,
780 const char *name
, const char *value
)
782 if (transport
->set_option
)
783 return transport
->set_option(transport
, name
, value
);
787 int transport_push(struct transport
*transport
,
788 int refspec_nr
, const char **refspec
, int flags
)
790 if (!transport
->push
)
792 return transport
->push(transport
, refspec_nr
, refspec
, flags
);
795 const struct ref
*transport_get_remote_refs(struct transport
*transport
)
797 if (!transport
->remote_refs
)
798 transport
->remote_refs
= transport
->get_refs_list(transport
);
799 return transport
->remote_refs
;
802 int transport_fetch_refs(struct transport
*transport
, const struct ref
*refs
)
805 int nr_heads
= 0, nr_alloc
= 0;
806 const struct ref
**heads
= NULL
;
807 const struct ref
*rm
;
809 for (rm
= refs
; rm
; rm
= rm
->next
) {
811 !hashcmp(rm
->peer_ref
->old_sha1
, rm
->old_sha1
))
813 ALLOC_GROW(heads
, nr_heads
+ 1, nr_alloc
);
814 heads
[nr_heads
++] = rm
;
817 rc
= transport
->fetch(transport
, nr_heads
, heads
);
822 void transport_unlock_pack(struct transport
*transport
)
824 if (transport
->pack_lockfile
) {
825 unlink(transport
->pack_lockfile
);
826 free(transport
->pack_lockfile
);
827 transport
->pack_lockfile
= NULL
;
831 int transport_disconnect(struct transport
*transport
)
834 if (transport
->disconnect
)
835 ret
= transport
->disconnect(transport
);