/*
- * Copyright (C) 2009-2012 the libgit2 contributors
+ * Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
#include "git2/oid.h"
#include "git2/refs.h"
#include "git2/revwalk.h"
-#include "git2/indexer.h"
+#include "git2/transport.h"
#include "common.h"
-#include "transport.h"
#include "remote.h"
#include "refspec.h"
#include "pack.h"
#include "fetch.h"
#include "netops.h"
-#include "pkt.h"
+#include "repository.h"
+#include "refs.h"
-struct filter_payload {
- git_remote *remote;
- const git_refspec *spec, *tagspec;
- git_odb *odb;
- int found_head;
-};
-
-static int filter_ref__cb(git_remote_head *head, void *payload)
+static int maybe_want(git_remote *remote, git_remote_head *head, git_odb *odb, git_refspec *tagspec, git_remote_autotag_option_t tagopt)
{
- struct filter_payload *p = payload;
int match = 0;
if (!git_reference_is_valid_name(head->name))
return 0;
- if (!p->found_head && strcmp(head->name, GIT_HEAD_FILE) == 0)
- p->found_head = 1;
- else if (git_refspec_src_matches(p->spec, head->name))
- match = 1;
- else if (p->remote->download_tags == GIT_REMOTE_DOWNLOAD_TAGS_ALL &&
- git_refspec_src_matches(p->tagspec, head->name))
+ if (tagopt == GIT_REMOTE_DOWNLOAD_TAGS_ALL) {
+ /*
+ * If tagopt is --tags, always request tags
+ * in addition to the remote's refspecs
+ */
+ if (git_refspec_src_matches(tagspec, head->name))
match = 1;
+ }
+
+ if (!match && git_remote__matching_refspec(remote, head->name))
+ match = 1;
if (!match)
return 0;
/* If we have the object, mark it so we don't ask for it */
- if (git_odb_exists(p->odb, &head->oid))
+ if (git_odb_exists(odb, &head->oid)) {
head->local = 1;
+ }
else
- p->remote->need_pack = 1;
+ remote->need_pack = 1;
- return git_vector_insert(&p->remote->refs, head);
+ return git_vector_insert(&remote->refs, head);
}
-static int filter_wants(git_remote *remote)
+static int filter_wants(git_remote *remote, const git_fetch_options *opts)
{
- struct filter_payload p;
- git_refspec tagspec;
- int error = -1;
+ git_remote_head **heads;
+ git_refspec tagspec, head;
+ int error = 0;
+ git_odb *odb;
+ size_t i, heads_len;
+ git_remote_autotag_option_t tagopt = remote->download_tags;
+
+ if (opts && opts->download_tags != GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED)
+ tagopt = opts->download_tags;
git_vector_clear(&remote->refs);
- if (git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true) < 0)
+ if ((error = git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true)) < 0)
return error;
/*
* not interested in any particular branch but just the remote's
* HEAD, which will be stored in FETCH_HEAD after the fetch.
*/
- p.spec = git_remote_fetchspec(remote);
- p.tagspec = &tagspec;
- p.found_head = 0;
- p.remote = remote;
+ if (remote->active_refspecs.length == 0) {
+ if ((error = git_refspec__parse(&head, "HEAD", true)) < 0)
+ goto cleanup;
- if (git_repository_odb__weakptr(&p.odb, remote->repo) < 0)
+ error = git_refspec__dwim_one(&remote->active_refspecs, &head, &remote->refs);
+ git_refspec__free(&head);
+
+ if (error < 0)
+ goto cleanup;
+ }
+
+ if (git_repository_odb__weakptr(&odb, remote->repo) < 0)
goto cleanup;
- error = git_remote_ls(remote, filter_ref__cb, &p);
+ if (git_remote_ls((const git_remote_head ***)&heads, &heads_len, remote) < 0)
+ goto cleanup;
+
+ for (i = 0; i < heads_len; i++) {
+ if ((error = maybe_want(remote, heads[i], odb, &tagspec, tagopt)) < 0)
+ break;
+ }
cleanup:
git_refspec__free(&tagspec);
return error;
}
-/* Wait until we get an ack from the */
-static int recv_pkt(git_pkt **out, gitno_buffer *buf)
-{
- const char *ptr = buf->data, *line_end = ptr;
- git_pkt *pkt;
- int pkt_type, error = 0, ret;
-
- do {
- if (buf->offset > 0)
- error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->offset);
- else
- error = GIT_EBUFS;
-
- if (error == 0)
- break; /* return the pkt */
-
- if (error < 0 && error != GIT_EBUFS)
- return -1;
-
- if ((ret = gitno_recv(buf)) < 0)
- return -1;
- } while (error);
-
- gitno_consume(buf, line_end);
- pkt_type = pkt->type;
- if (out != NULL)
- *out = pkt;
- else
- git__free(pkt);
-
- return pkt_type;
-}
-
-static int store_common(git_transport *t)
-{
- git_pkt *pkt = NULL;
- gitno_buffer *buf = &t->buffer;
-
- do {
- if (recv_pkt(&pkt, buf) < 0)
- return -1;
-
- if (pkt->type == GIT_PKT_ACK) {
- if (git_vector_insert(&t->common, pkt) < 0)
- return -1;
- } else {
- git__free(pkt);
- return 0;
- }
-
- } while (1);
-
- return 0;
-}
-
/*
* In this first version, we push all our refs in and start sending
* them out. When we get an ACK we hide that commit and continue
* traversing until we're done
*/
-int git_fetch_negotiate(git_remote *remote)
+int git_fetch_negotiate(git_remote *remote, const git_fetch_options *opts)
{
git_transport *t = remote->transport;
- gitno_buffer *buf = &t->buffer;
- git_buf data = GIT_BUF_INIT;
- git_revwalk *walk = NULL;
- int error = -1, pkt_type;
- unsigned int i;
- git_oid oid;
- if (filter_wants(remote) < 0) {
+ remote->need_pack = 0;
+
+ if (filter_wants(remote, opts) < 0) {
giterr_set(GITERR_NET, "Failed to filter the reference list for wants");
return -1;
}
/* Don't try to negotiate when we don't want anything */
- if (remote->refs.length == 0 || !remote->need_pack)
+ if (!remote->need_pack)
return 0;
/*
* Now we have everything set up so we can start tell the
- * server what we want and what we have. Call the function if
- * the transport has its own logic. This is transitional and
- * will be removed once this function can support git and http.
- */
- if (t->own_logic)
- return t->negotiate_fetch(t, remote->repo, &remote->refs);
-
- /* No own logic, do our thing */
- if (git_pkt_buffer_wants(&remote->refs, &t->caps, &data) < 0)
- return -1;
-
- if (git_fetch_setup_walk(&walk, remote->repo) < 0)
- goto on_error;
- /*
- * We don't support any kind of ACK extensions, so the negotiation
- * boils down to sending what we have and listening for an ACK
- * every once in a while.
+ * server what we want and what we have.
*/
- i = 0;
- while ((error = git_revwalk_next(&oid, walk)) == 0) {
- git_pkt_buffer_have(&oid, &data);
- i++;
- if (i % 20 == 0) {
- if (t->cancel.val) {
- giterr_set(GITERR_NET, "The fetch was cancelled by the user");
- error = GIT_EUSER;
- goto on_error;
- }
-
- git_pkt_buffer_flush(&data);
- if (git_buf_oom(&data))
- goto on_error;
-
- if (t->negotiation_step(t, data.ptr, data.size) < 0)
- goto on_error;
-
- git_buf_clear(&data);
- if (t->caps.multi_ack) {
- if (store_common(t) < 0)
- goto on_error;
- } else {
- pkt_type = recv_pkt(NULL, buf);
-
- if (pkt_type == GIT_PKT_ACK) {
- break;
- } else if (pkt_type == GIT_PKT_NAK) {
- continue;
- } else {
- giterr_set(GITERR_NET, "Unexpected pkt type");
- goto on_error;
- }
- }
- }
-
- if (t->common.length > 0)
- break;
-
- if (i % 20 == 0 && t->rpc) {
- git_pkt_ack *pkt;
- unsigned int i;
-
- if (git_pkt_buffer_wants(&remote->refs, &t->caps, &data) < 0)
- goto on_error;
-
- git_vector_foreach(&t->common, i, pkt) {
- git_pkt_buffer_have(&pkt->oid, &data);
- }
-
- if (git_buf_oom(&data))
- goto on_error;
- }
- }
-
- if (error < 0 && error != GIT_ITEROVER)
- goto on_error;
-
- /* Tell the other end that we're done negotiating */
- if (t->rpc && t->common.length > 0) {
- git_pkt_ack *pkt;
- unsigned int i;
-
- if (git_pkt_buffer_wants(&remote->refs, &t->caps, &data) < 0)
- goto on_error;
-
- git_vector_foreach(&t->common, i, pkt) {
- git_pkt_buffer_have(&pkt->oid, &data);
- }
-
- if (git_buf_oom(&data))
- goto on_error;
- }
-
- git_pkt_buffer_done(&data);
- if (t->cancel.val) {
- giterr_set(GITERR_NET, "The fetch was cancelled by the user");
- error = GIT_EUSER;
- goto on_error;
- }
- if (t->negotiation_step(t, data.ptr, data.size) < 0)
- goto on_error;
-
- git_buf_free(&data);
- git_revwalk_free(walk);
-
- /* Now let's eat up whatever the server gives us */
- if (!t->caps.multi_ack) {
- pkt_type = recv_pkt(NULL, buf);
- if (pkt_type != GIT_PKT_ACK && pkt_type != GIT_PKT_NAK) {
- giterr_set(GITERR_NET, "Unexpected pkt type");
- return -1;
- }
- } else {
- git_pkt_ack *pkt;
- do {
- if (recv_pkt((git_pkt **)&pkt, buf) < 0)
- return -1;
-
- if (pkt->type == GIT_PKT_NAK ||
- (pkt->type == GIT_PKT_ACK && pkt->status != GIT_ACK_CONTINUE)) {
- git__free(pkt);
- break;
- }
-
- git__free(pkt);
- } while (1);
- }
-
- return 0;
-
-on_error:
- git_revwalk_free(walk);
- git_buf_free(&data);
- return error;
+ return t->negotiate_fetch(t,
+ remote->repo,
+ (const git_remote_head * const *)remote->refs.contents,
+ remote->refs.length);
}
-int git_fetch_download_pack(git_remote *remote, git_off_t *bytes, git_indexer_stats *stats)
+int git_fetch_download_pack(git_remote *remote, const git_remote_callbacks *callbacks)
{
git_transport *t = remote->transport;
+ git_transfer_progress_cb progress = NULL;
+ void *payload = NULL;
- if(!remote->need_pack)
+ if (!remote->need_pack)
return 0;
- if (t->own_logic)
- return t->download_pack(t, remote->repo, bytes, stats);
-
- return git_fetch__download_pack(t, remote->repo, bytes, stats);
-
-}
-
-static int no_sideband(git_transport *t, git_indexer_stream *idx, gitno_buffer *buf, git_off_t *bytes, git_indexer_stats *stats)
-{
- int recvd;
-
- do {
- if (t->cancel.val) {
- giterr_set(GITERR_NET, "The fetch was cancelled by the user");
- return GIT_EUSER;
- }
-
- if (git_indexer_stream_add(idx, buf->data, buf->offset, stats) < 0)
- return -1;
-
- gitno_consume_n(buf, buf->offset);
-
- if ((recvd = gitno_recv(buf)) < 0)
- return -1;
-
- *bytes += recvd;
- } while(recvd > 0);
-
- if (git_indexer_stream_finalize(idx, stats))
- return -1;
-
- return 0;
-}
-
-/* Receiving data from a socket and storing it is pretty much the same for git and HTTP */
-int git_fetch__download_pack(
- git_transport *t,
- git_repository *repo,
- git_off_t *bytes,
- git_indexer_stats *stats)
-{
- git_buf path = GIT_BUF_INIT;
- gitno_buffer *buf = &t->buffer;
- git_indexer_stream *idx = NULL;
- int error = -1;
-
- if (git_buf_joinpath(&path, git_repository_path(repo), "objects/pack") < 0)
- return -1;
-
- if (git_indexer_stream_new(&idx, git_buf_cstr(&path)) < 0)
- goto on_error;
-
- git_buf_free(&path);
- memset(stats, 0, sizeof(git_indexer_stats));
- *bytes = 0;
-
- /*
- * If the remote doesn't support the side-band, we can feed
- * the data directly to the indexer. Otherwise, we need to
- * check which one belongs there.
- */
- if (!t->caps.side_band && !t->caps.side_band_64k) {
- if (no_sideband(t, idx, buf, bytes, stats) < 0)
- goto on_error;
-
- git_indexer_stream_free(idx);
- return 0;
+ if (callbacks) {
+ progress = callbacks->transfer_progress;
+ payload = callbacks->payload;
}
- do {
- if (t->cancel.val) {
- giterr_set(GITERR_NET, "The fetch was cancelled by the user");
- error = GIT_EUSER;
- goto on_error;
- }
-
- git_pkt *pkt;
- if (recv_pkt(&pkt, buf) < 0)
- goto on_error;
-
- if (pkt->type == GIT_PKT_PROGRESS) {
- if (t->progress_cb) {
- git_pkt_progress *p = (git_pkt_progress *) pkt;
- t->progress_cb(p->data, p->len, t->cb_data);
- }
- git__free(pkt);
- } else if (pkt->type == GIT_PKT_DATA) {
- git_pkt_data *p = (git_pkt_data *) pkt;
- *bytes += p->len;
- if (git_indexer_stream_add(idx, p->data, p->len, stats) < 0)
- goto on_error;
-
- git__free(pkt);
- } else if (pkt->type == GIT_PKT_FLUSH) {
- /* A flush indicates the end of the packfile */
- git__free(pkt);
- break;
- }
- } while (1);
-
- if (git_indexer_stream_finalize(idx, stats) < 0)
- goto on_error;
-
- git_indexer_stream_free(idx);
- return 0;
-
-on_error:
- git_buf_free(&path);
- git_indexer_stream_free(idx);
- return error;
+ return t->download_pack(t, remote->repo, &remote->stats, progress, payload);
}
-int git_fetch_setup_walk(git_revwalk **out, git_repository *repo)
+int git_fetch_init_options(git_fetch_options *opts, unsigned int version)
{
- git_revwalk *walk;
- git_strarray refs;
- unsigned int i;
- git_reference *ref;
-
- if (git_reference_list(&refs, repo, GIT_REF_LISTALL) < 0)
- return -1;
-
- if (git_revwalk_new(&walk, repo) < 0)
- return -1;
-
- git_revwalk_sorting(walk, GIT_SORT_TIME);
-
- for (i = 0; i < refs.count; ++i) {
- /* No tags */
- if (!git__prefixcmp(refs.strings[i], GIT_REFS_TAGS_DIR))
- continue;
-
- if (git_reference_lookup(&ref, repo, refs.strings[i]) < 0)
- goto on_error;
-
- if (git_reference_type(ref) == GIT_REF_SYMBOLIC)
- continue;
- if (git_revwalk_push(walk, git_reference_oid(ref)) < 0)
- goto on_error;
-
- git_reference_free(ref);
- }
-
- git_strarray_free(&refs);
- *out = walk;
+ GIT_INIT_STRUCTURE_FROM_TEMPLATE(
+ opts, version, git_fetch_options, GIT_FETCH_OPTIONS_INIT);
return 0;
-
-on_error:
- git_reference_free(ref);
- git_strarray_free(&refs);
- return -1;
}