git.c 9.85 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 10 11
 */

#include "git2/net.h"
#include "git2/common.h"
#include "git2/types.h"
#include "git2/errors.h"
12 13
#include "git2/net.h"
#include "git2/revwalk.h"
14 15 16

#include "vector.h"
#include "transport.h"
Vicent Marti committed
17
#include "pkt.h"
18
#include "common.h"
19
#include "netops.h"
Carlos Martín Nieto committed
20 21
#include "filebuf.h"
#include "repository.h"
22
#include "fetch.h"
23
#include "protocol.h"
24 25

typedef struct {
26
	git_transport parent;
27
	git_protocol proto;
28
	GIT_SOCKET socket;
29 30
	git_vector refs;
	git_remote_head **heads;
31
	git_transport_caps caps;
32 33
	char buff[1024];
	gitno_buffer buf;
34 35 36
#ifdef GIT_WIN32
	WSADATA wsd;
#endif
37 38
} transport_git;

39 40 41 42 43
/*
 * Create a git procol request.
 *
 * For example: 0035git-upload-pack /libgit2/libgit2\0host=github.com\0
 */
44
static int gen_proto(git_buf *request, const char *cmd, const char *url)
45
{
46
	char *delim, *repo;
47 48
	char default_command[] = "git-upload-pack";
	char host[] = "host=";
49
	size_t len;
50 51

	delim = strchr(url, '/');
52 53 54 55
	if (delim == NULL) {
		giterr_set(GITERR_NET, "Malformed URL");
		return -1;
	}
56 57 58 59 60 61 62 63 64 65

	repo = delim;

	delim = strchr(url, ':');
	if (delim == NULL)
		delim = strchr(url, '/');

	if (cmd == NULL)
		cmd = default_command;

66
	len = 4 + strlen(cmd) + 1 + strlen(repo) + 1 + strlen(host) + (delim - url) + 1;
67

68
	git_buf_grow(request, len);
69 70
	git_buf_printf(request, "%04x%s %s%c%s",
		(unsigned int)(len & 0x0FFFF), cmd, repo, 0, host);
71 72
	git_buf_put(request, url, delim - url);
	git_buf_putc(request, '\0');
73

74
	if (git_buf_oom(request))
75
		return -1;
76 77

	return 0;
78 79
}

80
static int send_request(GIT_SOCKET s, const char *cmd, const char *url)
81
{
82 83
	int error;
	git_buf request = GIT_BUF_INIT;
84

85
	error = gen_proto(&request, cmd, url);
86
	if (error < 0)
87 88
		goto cleanup;

89
	error = gitno_send(s, request.ptr, request.size, 0);
90 91

cleanup:
92
	git_buf_free(&request);
93 94
	return error;
}
95 96 97 98 99 100

/*
 * Parse the URL and connect to a server, storing the socket in
 * out. For convenience this also takes care of asking for the remote
 * refs
 */
101
static int do_connect(transport_git *t, const char *url)
102
{
103
	char *host, *port;
104
	const char prefix[] = "git://";
105 106 107
	int error;

	t->socket = INVALID_SOCKET;
108 109

	if (!git__prefixcmp(url, prefix))
110
		url += strlen(prefix);
111

112 113
	if (gitno_extract_host_and_port(&host, &port, url, GIT_DEFAULT_PORT) < 0)
		return -1;
114

115
	if ((error = gitno_connect(&t->socket, host, port)) == 0) {
116
		error = send_request(t->socket, NULL, url);
117
	}
118

119 120
	git__free(host);
	git__free(port);
121

122 123 124 125
	if (error < 0 && t->socket != INVALID_SOCKET) {
		gitno_close(t->socket);
		t->socket = INVALID_SOCKET;
	}
126

127
	if (t->socket == INVALID_SOCKET) {
128 129 130
		giterr_set(GITERR_NET, "Failed to connect to the host");
		return -1;
	}
131

132
	return 0;
133 134 135 136 137
}

/*
 * Read from the socket and store the references in the vector
 */
138
static int store_refs(transport_git *t)
139
{
140
	gitno_buffer *buf = &t->buf;
141
	int ret = 0;
142

143
	while (1) {
144 145 146 147 148 149 150
		if ((ret = gitno_recv(buf)) < 0)
			return -1;
		if (ret == 0) /* Orderly shutdown, so exit */
			return 0;

		ret = git_protocol_store_refs(&t->proto, buf->data, buf->offset);
		if (ret == GIT_ESHORTBUFFER) {
151 152 153
			gitno_consume_n(buf, buf->len);
			continue;
		}
154

155 156
		if (ret < 0)
			return ret;
157

158
		gitno_consume_n(buf, buf->offset);
159

160 161
		if (t->proto.flush) { /* No more refs */
			t->proto.flush = 0;
162
			return 0;
163 164 165 166
		}
	}
}

167 168 169 170 171 172 173 174 175 176
static int detect_caps(transport_git *t)
{
	git_vector *refs = &t->refs;
	git_pkt_ref *pkt;
	git_transport_caps *caps = &t->caps;
	const char *ptr;

	pkt = git_vector_get(refs, 0);
	/* No refs or capabilites, odd but not a problem */
	if (pkt == NULL || pkt->capabilities == NULL)
177
		return 0;
178 179 180 181 182 183 184 185

	ptr = pkt->capabilities;
	while (ptr != NULL && *ptr != '\0') {
		if (*ptr == ' ')
			ptr++;

		if(!git__prefixcmp(ptr, GIT_CAP_OFS_DELTA)) {
			caps->common = caps->ofs_delta = 1;
186
			ptr += strlen(GIT_CAP_OFS_DELTA);
187 188 189 190 191 192 193
			continue;
		}

		/* We don't know this capability, so skip it */
		ptr = strchr(ptr, ' ');
	}

194
	return 0;
195 196
}

197 198 199 200
/*
 * Since this is a network connection, we need to parse and store the
 * pkt-lines at this stage and keep them there.
 */
201
static int git_connect(git_transport *transport, int direction)
202
{
203
	transport_git *t = (transport_git *) transport;
204

205 206 207 208
	if (direction == GIT_DIR_PUSH) {
		giterr_set(GITERR_NET, "Pushing over git:// is not supported");
		return -1;
	}
209

210
	t->parent.direction = direction;
211 212
	if (git_vector_init(&t->refs, 16, NULL) < 0)
		return -1;
213 214

	/* Connect and ask for the refs */
215 216
	if (do_connect(t, transport->url) < 0)
		goto cleanup;
217

218 219
	gitno_buffer_setup(&t->buf, t->buff, sizeof(t->buff), t->socket);

220
	t->parent.connected = 1;
221 222
	if (store_refs(t) < 0)
		goto cleanup;
223

224 225
	if (detect_caps(t) < 0)
		goto cleanup;
226

227
	return 0;
228
cleanup:
229 230
	git_vector_free(&t->refs);
	return -1;
231 232
}

233
static int git_ls(git_transport *transport, git_headlist_cb list_cb, void *opaque)
234
{
235 236
	transport_git *t = (transport_git *) transport;
	git_vector *refs = &t->refs;
237
	unsigned int i;
238
	git_pkt *p = NULL;
239

240 241
	git_vector_foreach(refs, i, p) {
		git_pkt_ref *pkt = NULL;
242 243 244 245

		if (p->type != GIT_PKT_REF)
			continue;

246 247
		pkt = (git_pkt_ref *)p;

248 249 250 251
		if (list_cb(&pkt->head, opaque) < 0) {
			giterr_set(GITERR_NET, "User callback returned error");
			return -1;
		}
252 253
	}

254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	return 0;
}

/* Wait until we get an ack from the */
static int recv_pkt(gitno_buffer *buf)
{
	const char *ptr = buf->data, *line_end;
	git_pkt *pkt;
	int pkt_type, error;

	do {
		/* Wait for max. 1 second */
		if ((error = gitno_select_in(buf, 1, 0)) < 0) {
			return -1;
		} else if (error == 0) {
			/*
			 * Some servers don't respond immediately, so if this
			 * happens, we keep sending information until it
			 * answers. Pretend we received a NAK to convince higher
			 * layers to do so.
			 */
			return GIT_PKT_NAK;
		}

		if ((error = gitno_recv(buf)) < 0)
			return -1;

		error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->offset);
		if (error == GIT_ESHORTBUFFER)
			continue;
		if (error < 0)
			return -1;
	} while (error);

	gitno_consume(buf, line_end);
	pkt_type = pkt->type;
	git__free(pkt);

	return pkt_type;
293 294
}

295
static int git_negotiate_fetch(git_transport *transport, git_repository *repo, const git_vector *wants)
296 297 298 299 300 301
{
	transport_git *t = (transport_git *) transport;
	git_revwalk *walk;
	git_oid oid;
	int error;
	unsigned int i;
302
	git_buf data = GIT_BUF_INIT;
303
	gitno_buffer *buf = &t->buf;
304

305
	if (git_pkt_buffer_wants(wants, &t->caps, &data) < 0)
306
		return -1;
307

308 309
	if (git_fetch_setup_walk(&walk, repo) < 0)
		goto on_error;
310

311 312
	if (gitno_send(t->socket, data.ptr, data.size, 0) < 0)
		goto on_error;
313

314
	git_buf_clear(&data);
315 316 317 318 319 320
	/*
	 * 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;
321
	while ((error = git_revwalk_next(&oid, walk)) == 0) {
322
		git_pkt_buffer_have(&oid, &data);
323
		i++;
324
		if (i % 20 == 0) {
325 326
			int pkt_type;

327 328 329 330 331 332 333
			git_pkt_buffer_flush(&data);
			if (git_buf_oom(&data))
				goto on_error;

			if (gitno_send(t->socket, data.ptr, data.size, 0) < 0)
				goto on_error;

334 335 336 337 338 339 340 341 342
			pkt_type = recv_pkt(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;
343
			}
344

345
		}
346
	}
347
	if (error < 0 && error != GIT_EREVWALKOVER)
348
		goto on_error;
349

350 351 352 353 354 355
	/* Tell the other end that we're done negotiating */
	git_buf_clear(&data);
	git_pkt_buffer_flush(&data);
	git_pkt_buffer_done(&data);
	if (gitno_send(t->socket, data.ptr, data.size, 0) < 0)
		goto on_error;
356

357
	git_buf_free(&data);
358
	git_revwalk_free(walk);
359
	return 0;
360

361
on_error:
362
	git_buf_free(&data);
363 364
	git_revwalk_free(walk);
	return -1;
365 366
}

367
static int git_download_pack(git_transport *transport, git_repository *repo, git_off_t *bytes, git_indexer_stats *stats)
Carlos Martín Nieto committed
368 369
{
	transport_git *t = (transport_git *) transport;
370
	int error = 0, read_bytes;
371
	gitno_buffer *buf = &t->buf;
Carlos Martín Nieto committed
372 373 374 375
	git_pkt *pkt;
	const char *line_end, *ptr;

	/*
376
	 * For now, we ignore everything and wait for the pack
Carlos Martín Nieto committed
377
	 */
378
	do {
379
		ptr = buf->data;
Carlos Martín Nieto committed
380
		/* Whilst we're searching for the pack */
381
		while (1) {
382
			if (buf->offset == 0) {
Carlos Martín Nieto committed
383
				break;
384 385 386
			}

			error = git_pkt_parse_line(&pkt, ptr, &line_end, buf->offset);
Carlos Martín Nieto committed
387 388
			if (error == GIT_ESHORTBUFFER)
				break;
389

Carlos Martín Nieto committed
390 391 392
			if (error < GIT_SUCCESS)
				return error;

393
			if (pkt->type == GIT_PKT_PACK) {
394
				git__free(pkt);
395
				return git_fetch__download_pack(buf->data, buf->offset, t->socket, repo, bytes, stats);
396
			}
397

Carlos Martín Nieto committed
398
			/* For now we don't care about anything */
399
			git__free(pkt);
400 401 402
			gitno_consume(buf, line_end);
		}

403 404
		read_bytes = gitno_recv(buf);
	} while (read_bytes);
405

406
	return read_bytes;
Carlos Martín Nieto committed
407 408
}

409 410
static int git_close(git_transport *transport)
{
411
	transport_git *t = (transport_git*) transport;
412

413
	/* Can't do anything if there's an error, so don't bother checking  */
414
	git_pkt_send_flush(t->socket);
415 416 417 418
	if (gitno_close(t->socket) < 0) {
		giterr_set(GITERR_NET, "Failed to close socket");
		return -1;
	}
419

420 421 422 423
#ifdef GIT_WIN32
	WSACleanup();
#endif

424
	return 0;
425 426 427 428
}

static void git_free(git_transport *transport)
{
429 430
	transport_git *t = (transport_git *) transport;
	git_vector *refs = &t->refs;
431 432 433 434
	unsigned int i;

	for (i = 0; i < refs->length; ++i) {
		git_pkt *p = git_vector_get(refs, i);
435
		git_pkt_free(p);
436 437 438
	}

	git_vector_free(refs);
439
	git__free(t->heads);
440
	git_buf_free(&t->proto.buf);
441 442
	git__free(t->parent.url);
	git__free(t);
443 444
}

445
int git_transport_git(git_transport **out)
446
{
447
	transport_git *t;
448 449 450
#ifdef GIT_WIN32
	int ret;
#endif
451 452

	t = git__malloc(sizeof(transport_git));
453
	GITERR_CHECK_ALLOC(t);
454

455 456
	memset(t, 0x0, sizeof(transport_git));

457 458
	t->parent.connect = git_connect;
	t->parent.ls = git_ls;
459
	t->parent.negotiate_fetch = git_negotiate_fetch;
Carlos Martín Nieto committed
460
	t->parent.download_pack = git_download_pack;
461 462
	t->parent.close = git_close;
	t->parent.free = git_free;
463 464
	t->proto.refs = &t->refs;
	t->proto.transport = (git_transport *) t;
465 466

	*out = (git_transport *) t;
467

468 469 470 471
#ifdef GIT_WIN32
	ret = WSAStartup(MAKEWORD(2,2), &t->wsd);
	if (ret != 0) {
		git_free(*out);
472 473
		giterr_set(GITERR_NET, "Winsock init failed");
		return -1;
474 475 476
	}
#endif

477
	return 0;
478
}