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 (de->d_name[0] == '.' && (de->d_name[1] == '\0' ||
54 (de->d_name[1] == '.' &&
55 de->d_name[2] == '\0')))
57 ALLOC_GROW(list.entries, list.nr + 1, list.alloc);
58 list.entries[list.nr++] = xstrdup(de->d_name);
64 qsort(list.entries, list.nr, sizeof(char *), str_cmp);
67 strbuf_addch(path, '/');
69 for (i = 0; i < list.nr; i++, strbuf_setlen(path, pathlen + 1)) {
70 strbuf_addstr(path, list.entries[i]);
71 if (read_loose_refs(path, name_offset, tail)) {
72 int fd = open(path->buf, O_RDONLY);
78 next = alloc_ref(path->len - name_offset + 1);
79 if (read_in_full(fd, buffer, 40) != 40 ||
80 get_sha1_hex(buffer, next->old_sha1)) {
86 strcpy(next->name, path->buf + name_offset);
91 strbuf_setlen(path, pathlen);
93 for (i = 0; i < list.nr; i++)
94 free(list.entries[i]);
100 /* insert the packed refs for which no loose refs were found */
102 static void insert_packed_refs(const char *packed_refs, struct ref **list)
104 FILE *f = fopen(packed_refs, "r");
105 static char buffer[PATH_MAX];
113 if (!fgets(buffer, sizeof(buffer), f)) {
118 if (hexval(buffer[0]) > 0xf)
120 len = strlen(buffer);
121 if (len && buffer[len - 1] == '\n')
122 buffer[--len] = '\0';
125 while ((*list)->next &&
126 (cmp = strcmp(buffer + 41,
127 (*list)->next->name)) > 0)
128 list = &(*list)->next;
129 if (!(*list)->next || cmp < 0) {
130 struct ref *next = alloc_ref(len - 40);
132 if (get_sha1_hex(buffer, next->old_sha1)) {
133 warning ("invalid SHA-1: %s", buffer);
137 strcpy(next->name, buffer + 41);
138 next->next = (*list)->next;
139 (*list)->next = next;
140 list = &(*list)->next;
145 static struct ref *get_refs_via_rsync(struct transport *transport)
147 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
148 struct ref dummy, *tail = &dummy;
149 struct child_process rsync;
153 /* copy the refs to the temporary directory */
155 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
156 if (!mkdtemp(temp_dir.buf))
157 die ("Could not make temporary directory");
158 temp_dir_len = temp_dir.len;
160 strbuf_addstr(&buf, transport->url);
161 strbuf_addstr(&buf, "/refs");
163 memset(&rsync, 0, sizeof(rsync));
165 rsync.stdout_to_stderr = 1;
167 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
169 args[3] = temp_dir.buf;
172 if (run_command(&rsync))
173 die ("Could not run rsync to get refs");
176 strbuf_addstr(&buf, transport->url);
177 strbuf_addstr(&buf, "/packed-refs");
181 if (run_command(&rsync))
182 die ("Could not run rsync to get refs");
184 /* read the copied refs */
186 strbuf_addstr(&temp_dir, "/refs");
187 read_loose_refs(&temp_dir, temp_dir_len + 1, &tail);
188 strbuf_setlen(&temp_dir, temp_dir_len);
191 strbuf_addstr(&temp_dir, "/packed-refs");
192 insert_packed_refs(temp_dir.buf, &tail);
193 strbuf_setlen(&temp_dir, temp_dir_len);
195 if (remove_dir_recursively(&temp_dir, 0))
196 warning ("Error removing temporary directory %s.",
199 strbuf_release(&buf);
200 strbuf_release(&temp_dir);
205 static int fetch_objs_via_rsync(struct transport *transport,
206 int nr_objs, const struct ref **to_fetch)
208 struct strbuf buf = STRBUF_INIT;
209 struct child_process rsync;
213 strbuf_addstr(&buf, transport->url);
214 strbuf_addstr(&buf, "/objects/");
216 memset(&rsync, 0, sizeof(rsync));
218 rsync.stdout_to_stderr = 1;
220 args[1] = (transport->verbose > 0) ? "-rv" : "-r";
221 args[2] = "--ignore-existing";
222 args[3] = "--exclude";
225 args[6] = get_object_directory();
228 /* NEEDSWORK: handle one level of alternates */
229 result = run_command(&rsync);
231 strbuf_release(&buf);
236 static int write_one_ref(const char *name, const unsigned char *sha1,
237 int flags, void *data)
239 struct strbuf *buf = data;
243 /* when called via for_each_ref(), flags is non-zero */
244 if (flags && prefixcmp(name, "refs/heads/") &&
245 prefixcmp(name, "refs/tags/"))
248 strbuf_addstr(buf, name);
249 if (safe_create_leading_directories(buf->buf) ||
250 !(f = fopen(buf->buf, "w")) ||
251 fprintf(f, "%s\n", sha1_to_hex(sha1)) < 0 ||
253 return error("problems writing temporary file %s", buf->buf);
254 strbuf_setlen(buf, len);
258 static int write_refs_to_temp_dir(struct strbuf *temp_dir,
259 int refspec_nr, const char **refspec)
263 for (i = 0; i < refspec_nr; i++) {
264 unsigned char sha1[20];
267 if (dwim_ref(refspec[i], strlen(refspec[i]), sha1, &ref) != 1)
268 return error("Could not get ref %s", refspec[i]);
270 if (write_one_ref(ref, sha1, 0, temp_dir)) {
279 static int rsync_transport_push(struct transport *transport,
280 int refspec_nr, const char **refspec, int flags)
282 struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT;
284 struct child_process rsync;
285 const char *args[10];
287 if (flags & TRANSPORT_PUSH_MIRROR)
288 return error("rsync transport does not support mirror mode");
290 /* first push the objects */
292 strbuf_addstr(&buf, transport->url);
293 strbuf_addch(&buf, '/');
295 memset(&rsync, 0, sizeof(rsync));
297 rsync.stdout_to_stderr = 1;
301 if (flags & TRANSPORT_PUSH_DRY_RUN)
302 args[i++] = "--dry-run";
303 if (transport->verbose > 0)
305 args[i++] = "--ignore-existing";
306 args[i++] = "--exclude";
308 args[i++] = get_object_directory();
312 if (run_command(&rsync))
313 return error("Could not push objects to %s", transport->url);
315 /* copy the refs to the temporary directory; they could be packed. */
317 strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX"));
318 if (!mkdtemp(temp_dir.buf))
319 die ("Could not make temporary directory");
320 strbuf_addch(&temp_dir, '/');
322 if (flags & TRANSPORT_PUSH_ALL) {
323 if (for_each_ref(write_one_ref, &temp_dir))
325 } else if (write_refs_to_temp_dir(&temp_dir, refspec_nr, refspec))
329 if (flags & TRANSPORT_PUSH_DRY_RUN)
330 args[i++] = "--dry-run";
331 if (!(flags & TRANSPORT_PUSH_FORCE))
332 args[i++] = "--ignore-existing";
333 args[i++] = temp_dir.buf;
334 args[i++] = transport->url;
336 if (run_command(&rsync))
337 result = error("Could not push to %s", transport->url);
339 if (remove_dir_recursively(&temp_dir, 0))
340 warning ("Could not remove temporary directory %s.",
343 strbuf_release(&buf);
344 strbuf_release(&temp_dir);
349 /* Generic functions for using commit walkers */
351 #ifndef NO_CURL /* http fetch is the only user */
352 static int fetch_objs_via_walker(struct transport *transport,
353 int nr_objs, const struct ref **to_fetch)
355 char *dest = xstrdup(transport->url);
356 struct walker *walker = transport->data;
357 char **objs = xmalloc(nr_objs * sizeof(*objs));
361 walker->get_tree = 1;
362 walker->get_history = 1;
363 walker->get_verbosely = transport->verbose >= 0;
364 walker->get_recover = 0;
366 for (i = 0; i < nr_objs; i++)
367 objs[i] = xstrdup(sha1_to_hex(to_fetch[i]->old_sha1));
369 if (walker_fetch(walker, nr_objs, objs, NULL, NULL))
370 die("Fetch failed.");
372 for (i = 0; i < nr_objs; i++)
380 static int disconnect_walker(struct transport *transport)
382 struct walker *walker = transport->data;
389 static int curl_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags)
395 if (flags & TRANSPORT_PUSH_MIRROR)
396 return error("http transport does not support mirror mode");
398 argv = xmalloc((refspec_nr + 12) * sizeof(char *));
399 argv[0] = "http-push";
401 if (flags & TRANSPORT_PUSH_ALL)
402 argv[argc++] = "--all";
403 if (flags & TRANSPORT_PUSH_FORCE)
404 argv[argc++] = "--force";
405 if (flags & TRANSPORT_PUSH_DRY_RUN)
406 argv[argc++] = "--dry-run";
407 if (flags & TRANSPORT_PUSH_VERBOSE)
408 argv[argc++] = "--verbose";
409 argv[argc++] = transport->url;
411 argv[argc++] = *refspec++;
413 err = run_command_v_opt(argv, RUN_GIT_CMD);
415 case -ERR_RUN_COMMAND_FORK:
416 error("unable to fork for %s", argv[0]);
417 case -ERR_RUN_COMMAND_EXEC:
418 error("unable to exec %s", argv[0]);
420 case -ERR_RUN_COMMAND_WAITPID:
421 case -ERR_RUN_COMMAND_WAITPID_WRONG_PID:
422 case -ERR_RUN_COMMAND_WAITPID_SIGNAL:
423 case -ERR_RUN_COMMAND_WAITPID_NOEXIT:
424 error("%s died with strange error", argv[0]);
429 static struct ref *get_refs_via_curl(struct transport *transport)
431 struct strbuf buffer = STRBUF_INIT;
432 char *data, *start, *mid;
437 struct active_request_slot *slot;
438 struct slot_results results;
440 struct ref *refs = NULL;
441 struct ref *ref = NULL;
442 struct ref *last_ref = NULL;
444 struct walker *walker;
446 if (!transport->data)
447 transport->data = get_http_walker(transport->url,
450 walker = transport->data;
452 refs_url = xmalloc(strlen(transport->url) + 11);
453 sprintf(refs_url, "%s/info/refs", transport->url);
455 slot = get_active_slot();
456 slot->results = &results;
457 curl_easy_setopt(slot->curl, CURLOPT_FILE, &buffer);
458 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_buffer);
459 curl_easy_setopt(slot->curl, CURLOPT_URL, refs_url);
460 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, NULL);
462 if (start_active_slot(slot)) {
463 run_active_slot(slot);
464 if (results.curl_result != CURLE_OK) {
465 strbuf_release(&buffer);
466 if (missing_target(&results))
467 die("%s not found: did you run git update-server-info on the server?", refs_url);
469 die("%s download error - %s", refs_url, curl_errorstr);
472 strbuf_release(&buffer);
473 die("Unable to start HTTP request");
479 while (i < buffer.len) {
484 if (data[i] == '\n') {
487 ref = xmalloc(sizeof(struct ref) +
488 strlen(ref_name) + 1);
489 memset(ref, 0, sizeof(struct ref));
490 strcpy(ref->name, ref_name);
491 get_sha1_hex(start, ref->old_sha1);
495 last_ref->next = ref;
502 strbuf_release(&buffer);
504 ref = alloc_ref_from_str("HEAD");
505 if (!walker->fetch_ref(walker, ref) &&
506 !resolve_remote_symref(ref, refs)) {
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)
534 struct bundle_transport_data *data = transport->data;
535 struct ref *result = NULL;
540 data->fd = read_bundle_header(transport->url, &data->header);
542 die ("Could not read bundle '%s'.", transport->url);
543 for (i = 0; i < data->header.references.nr; i++) {
544 struct ref_list_entry *e = data->header.references.list + i;
545 struct ref *ref = alloc_ref_from_str(e->name);
546 hashcpy(ref->old_sha1, e->sha1);
553 static int fetch_refs_from_bundle(struct transport *transport,
554 int nr_heads, const struct ref **to_fetch)
556 struct bundle_transport_data *data = transport->data;
557 return unbundle(&data->header, data->fd);
560 static int close_bundle(struct transport *transport)
562 struct bundle_transport_data *data = transport->data;
569 struct git_transport_data {
572 unsigned followtags : 1;
574 struct child_process *conn;
576 const char *uploadpack;
577 const char *receivepack;
580 static int set_git_option(struct transport *connection,
581 const char *name, const char *value)
583 struct git_transport_data *data = connection->data;
584 if (!strcmp(name, TRANS_OPT_UPLOADPACK)) {
585 data->uploadpack = value;
587 } else if (!strcmp(name, TRANS_OPT_RECEIVEPACK)) {
588 data->receivepack = value;
590 } else if (!strcmp(name, TRANS_OPT_THIN)) {
591 data->thin = !!value;
593 } else if (!strcmp(name, TRANS_OPT_FOLLOWTAGS)) {
594 data->followtags = !!value;
596 } else if (!strcmp(name, TRANS_OPT_KEEP)) {
597 data->keep = !!value;
599 } else if (!strcmp(name, TRANS_OPT_DEPTH)) {
603 data->depth = atoi(value);
609 static int connect_setup(struct transport *transport)
611 struct git_transport_data *data = transport->data;
612 data->conn = git_connect(data->fd, transport->url, data->uploadpack, 0);
616 static struct ref *get_refs_via_connect(struct transport *transport)
618 struct git_transport_data *data = transport->data;
621 connect_setup(transport);
622 get_remote_heads(data->fd[0], &refs, 0, NULL, 0);
627 static int fetch_refs_via_pack(struct transport *transport,
628 int nr_heads, const struct ref **to_fetch)
630 struct git_transport_data *data = transport->data;
631 char **heads = xmalloc(nr_heads * sizeof(*heads));
632 char **origh = xmalloc(nr_heads * sizeof(*origh));
633 const struct ref *refs;
634 char *dest = xstrdup(transport->url);
635 struct fetch_pack_args args;
637 struct ref *refs_tmp = NULL;
639 memset(&args, 0, sizeof(args));
640 args.uploadpack = data->uploadpack;
641 args.keep_pack = data->keep;
643 args.use_thin_pack = data->thin;
644 args.include_tag = data->followtags;
645 args.verbose = (transport->verbose > 0);
646 args.quiet = args.no_progress = (transport->verbose < 0);
647 args.no_progress = !isatty(1);
648 args.depth = data->depth;
650 for (i = 0; i < nr_heads; i++)
651 origh[i] = heads[i] = xstrdup(to_fetch[i]->name);
654 connect_setup(transport);
655 get_remote_heads(data->fd[0], &refs_tmp, 0, NULL, 0);
658 refs = fetch_pack(&args, data->fd, data->conn,
659 refs_tmp ? refs_tmp : transport->remote_refs,
660 dest, nr_heads, heads, &transport->pack_lockfile);
663 if (finish_connect(data->conn))
669 for (i = 0; i < nr_heads; i++)
674 return (refs ? 0 : -1);
677 static int git_transport_push(struct transport *transport, int refspec_nr, const char **refspec, int flags)
679 struct git_transport_data *data = transport->data;
680 struct send_pack_args args;
682 args.receivepack = data->receivepack;
683 args.send_all = !!(flags & TRANSPORT_PUSH_ALL);
684 args.send_mirror = !!(flags & TRANSPORT_PUSH_MIRROR);
685 args.force_update = !!(flags & TRANSPORT_PUSH_FORCE);
686 args.use_thin_pack = data->thin;
687 args.verbose = !!(flags & TRANSPORT_PUSH_VERBOSE);
688 args.dry_run = !!(flags & TRANSPORT_PUSH_DRY_RUN);
690 return send_pack(&args, transport->url, transport->remote, refspec_nr, refspec);
693 static int disconnect_git(struct transport *transport)
695 struct git_transport_data *data = transport->data;
697 packet_flush(data->fd[1]);
700 finish_connect(data->conn);
707 static int is_local(const char *url)
709 const char *colon = strchr(url, ':');
710 const char *slash = strchr(url, '/');
711 return !colon || (slash && slash < colon) ||
712 has_dos_drive_prefix(url);
715 static int is_file(const char *url)
720 return S_ISREG(buf.st_mode);
723 struct transport *transport_get(struct remote *remote, const char *url)
725 struct transport *ret = xcalloc(1, sizeof(*ret));
727 ret->remote = remote;
730 if (!prefixcmp(url, "rsync://")) {
731 ret->get_refs_list = get_refs_via_rsync;
732 ret->fetch = fetch_objs_via_rsync;
733 ret->push = rsync_transport_push;
735 } else if (!prefixcmp(url, "http://")
736 || !prefixcmp(url, "https://")
737 || !prefixcmp(url, "ftp://")) {
739 error("git was compiled without libcurl support.");
741 ret->get_refs_list = get_refs_via_curl;
742 ret->fetch = fetch_objs_via_curl;
743 ret->push = curl_transport_push;
745 ret->disconnect = disconnect_walker;
747 } else if (is_local(url) && is_file(url)) {
748 struct bundle_transport_data *data = xcalloc(1, sizeof(*data));
750 ret->get_refs_list = get_refs_from_bundle;
751 ret->fetch = fetch_refs_from_bundle;
752 ret->disconnect = close_bundle;
755 struct git_transport_data *data = xcalloc(1, sizeof(*data));
757 ret->set_option = set_git_option;
758 ret->get_refs_list = get_refs_via_connect;
759 ret->fetch = fetch_refs_via_pack;
760 ret->push = git_transport_push;
761 ret->disconnect = disconnect_git;
765 data->uploadpack = "git-upload-pack";
766 if (remote && remote->uploadpack)
767 data->uploadpack = remote->uploadpack;
768 data->receivepack = "git-receive-pack";
769 if (remote && remote->receivepack)
770 data->receivepack = remote->receivepack;
776 int transport_set_option(struct transport *transport,
777 const char *name, const char *value)
779 if (transport->set_option)
780 return transport->set_option(transport, name, value);
784 int transport_push(struct transport *transport,
785 int refspec_nr, const char **refspec, int flags)
787 if (!transport->push)
789 return transport->push(transport, refspec_nr, refspec, flags);
792 const struct ref *transport_get_remote_refs(struct transport *transport)
794 if (!transport->remote_refs)
795 transport->remote_refs = transport->get_refs_list(transport);
796 return transport->remote_refs;
799 int transport_fetch_refs(struct transport *transport, const struct ref *refs)
802 int nr_heads = 0, nr_alloc = 0;
803 const struct ref **heads = NULL;
804 const struct ref *rm;
806 for (rm = refs; rm; rm = rm->next) {
808 !hashcmp(rm->peer_ref->old_sha1, rm->old_sha1))
810 ALLOC_GROW(heads, nr_heads + 1, nr_alloc);
811 heads[nr_heads++] = rm;
814 rc = transport->fetch(transport, nr_heads, heads);
819 void transport_unlock_pack(struct transport *transport)
821 if (transport->pack_lockfile) {
822 unlink(transport->pack_lockfile);
823 free(transport->pack_lockfile);
824 transport->pack_lockfile = NULL;
828 int transport_disconnect(struct transport *transport)
831 if (transport->disconnect)
832 ret = transport->disconnect(transport);