fetch.c 8.17 KB
Newer Older
1
/*
schu committed
2
 * Copyright (C) 2009-2012 the libgit2 contributors
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6 7 8 9
 */

#include "git2/oid.h"
#include "git2/refs.h"
Carlos Martín Nieto committed
10
#include "git2/revwalk.h"
11
#include "git2/indexer.h"
12 13 14 15 16

#include "common.h"
#include "transport.h"
#include "remote.h"
#include "refspec.h"
17
#include "pack.h"
18
#include "fetch.h"
19
#include "netops.h"
20
#include "pkt.h"
21

22 23
struct filter_payload {
	git_remote *remote;
24
	const git_refspec *spec;
25 26 27 28 29 30 31
	git_odb *odb;
	int found_head;
};

static int filter_ref__cb(git_remote_head *head, void *payload)
{
	struct filter_payload *p = payload;
32

33 34 35 36
	if (!p->found_head && strcmp(head->name, GIT_HEAD_FILE) == 0) {
		p->found_head = 1;
	} else {
		/* If it doesn't match the refpec, we don't want it */
37
		if (!git_refspec_src_matches(p->spec, head->name))
38
			return 0;
39 40 41 42

		/* Don't even try to ask for the annotation target */
		if (!git__suffixcmp(head->name, "^{}"))
			return 0;
43 44
	}

45 46 47 48 49 50 51 52 53 54 55 56 57 58
	/* If we have the object, mark it so we don't ask for it */
	if (git_odb_exists(p->odb, &head->oid))
		head->local = 1;
	else
		p->remote->need_pack = 1;

	return git_vector_insert(&p->remote->refs, head);
}

static int filter_wants(git_remote *remote)
{
	struct filter_payload p;

	git_vector_clear(&remote->refs);
59

60 61 62 63 64 65
	/*
	 * The fetch refspec can be NULL, and what this means is that the
	 * user didn't specify one. This is fine, as it means that we're
	 * not interested in any particular branch but just the remote's
	 * HEAD, which will be stored in FETCH_HEAD after the fetch.
	 */
66 67 68
	p.spec = git_remote_fetchspec(remote);
	p.found_head = 0;
	p.remote = remote;
69

70 71
	if (git_repository_odb__weakptr(&p.odb, remote->repo) < 0)
		return -1;
72

73
	return git_remote_ls(remote, filter_ref__cb, &p);
74
}
Carlos Martín Nieto committed
75

76
/* Wait until we get an ack from the */
77
static int recv_pkt(git_pkt **out, gitno_buffer *buf)
78
{
79
	const char *ptr = buf->data, *line_end = ptr;
80
	git_pkt *pkt;
81
	int pkt_type, error = 0, ret;
82 83

	do {
84 85 86 87 88 89 90 91 92 93 94
		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;

95
		if ((ret = gitno_recv(buf)) < 0)
96 97 98 99 100
			return -1;
	} while (error);

	gitno_consume(buf, line_end);
	pkt_type = pkt->type;
101 102 103 104
	if (out != NULL)
		*out = pkt;
	else
		git__free(pkt);
105 106 107 108

	return pkt_type;
}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
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;
}

Carlos Martín Nieto committed
131 132 133 134 135
/*
 * 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
 */
136
int git_fetch_negotiate(git_remote *remote)
Carlos Martín Nieto committed
137
{
138
	git_transport *t = remote->transport;
139 140 141 142 143 144
	gitno_buffer *buf = &t->buffer;
	git_buf data = GIT_BUF_INIT;
	git_revwalk *walk = NULL;
	int error, pkt_type;
	unsigned int i;
	git_oid oid;
Carlos Martín Nieto committed
145

146 147 148 149
	if (filter_wants(remote) < 0) {
		giterr_set(GITERR_NET, "Failed to filter the reference list for wants");
		return -1;
	}
150

Carlos Martín Nieto committed
151
	/* Don't try to negotiate when we don't want anything */
152 153
	if (remote->refs.length == 0 || !remote->need_pack)
		return 0;
154

Carlos Martín Nieto committed
155
	/*
156 157 158 159
	 * 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.
Carlos Martín Nieto committed
160
	 */
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	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.
	 */
	i = 0;
	while ((error = git_revwalk_next(&oid, walk)) == 0) {
		git_pkt_buffer_have(&oid, &data);
		i++;
		if (i % 20 == 0) {
			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);
188 189 190
			if (t->caps.multi_ack) {
				if (store_common(t) < 0)
					goto on_error;
191
			} else {
192 193 194 195 196 197 198 199 200 201
				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;
				}
202 203
			}
		}
204 205 206

		if (t->common.length > 0)
			break;
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221

		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;
		}
222 223 224 225 226 227
	}

	if (error < 0 && error != GIT_REVWALKOVER)
		goto on_error;

	/* Tell the other end that we're done negotiating */
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
	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;
	}

243 244 245 246 247 248 249 250
	git_pkt_buffer_done(&data);
	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 */
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	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);
271 272 273 274 275 276 277 278
	}

	return 0;

on_error:
	git_revwalk_free(walk);
	git_buf_free(&data);
	return -1;
Carlos Martín Nieto committed
279 280
}

281
int git_fetch_download_pack(git_remote *remote, git_off_t *bytes, git_indexer_stats *stats)
Carlos Martín Nieto committed
282
{
283 284
	git_transport *t = remote->transport;

285
	if(!remote->need_pack)
286
		return 0;
287

288 289 290
	if (t->own_logic)
		return t->download_pack(t, remote->repo, bytes, stats);

291
	return git_fetch__download_pack(t, remote->repo, bytes, stats);
292

Carlos Martín Nieto committed
293
}
294 295

/* Receiving data from a socket and storing it is pretty much the same for git and HTTP */
296
int git_fetch__download_pack(
297
	git_transport *t,
298 299 300
	git_repository *repo,
	git_off_t *bytes,
	git_indexer_stats *stats)
301
{
302
	int recvd;
303
	git_buf path = GIT_BUF_INIT;
304
	gitno_buffer *buf = &t->buffer;
305
	git_indexer_stream *idx = NULL;
306

307
	if (git_buf_joinpath(&path, git_repository_path(repo), "objects/pack") < 0)
308
		return -1;
309

310 311 312
	if (git_indexer_stream_new(&idx, git_buf_cstr(&path)) < 0)
		goto on_error;

313
	git_buf_free(&path);
314
	memset(stats, 0, sizeof(git_indexer_stats));
315
	*bytes = 0;
316

317
	do {
318
		if (git_indexer_stream_add(idx, buf->data, buf->offset, stats) < 0)
319
			goto on_error;
320

321 322 323
		gitno_consume_n(buf, buf->offset);

		if ((recvd = gitno_recv(buf)) < 0)
324
			goto on_error;
325

326 327
		*bytes += recvd;
	} while(recvd > 0);
328

329
	if (git_indexer_stream_finalize(idx, stats))
330 331
		goto on_error;

332
	git_indexer_stream_free(idx);
333
	return 0;
334

335
on_error:
336
	git_buf_free(&path);
337
	git_indexer_stream_free(idx);
338
	return -1;
339
}
340 341 342 343 344 345 346 347

int git_fetch_setup_walk(git_revwalk **out, git_repository *repo)
{
	git_revwalk *walk;
	git_strarray refs;
	unsigned int i;
	git_reference *ref;

348
	if (git_reference_list(&refs, repo, GIT_REF_LISTALL) < 0)
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
		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;
	return 0;

on_error:
	git_reference_free(ref);
	git_strarray_free(&refs);
	return -1;
}