fetch.c 3.87 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
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/transport.h"
12 13 14 15

#include "common.h"
#include "remote.h"
#include "refspec.h"
16
#include "pack.h"
17
#include "fetch.h"
18
#include "netops.h"
19 20
#include "repository.h"
#include "refs.h"
21

22
static int maybe_want(git_remote *remote, git_remote_head *head, git_odb *odb, git_refspec *tagspec, git_remote_autotag_option_t tagopt)
23
{
24
	int match = 0;
25

26 27 28
	if (!git_reference_is_valid_name(head->name))
		return 0;

29
	if (tagopt == GIT_REMOTE_DOWNLOAD_TAGS_ALL) {
30
		/*
31 32
		 * If tagopt is --tags, always request tags
		 * in addition to the remote's refspecs
33
		 */
34
		if (git_refspec_src_matches(tagspec, head->name))
35
			match = 1;
36 37 38 39
	}

	if (!match && git_remote__matching_refspec(remote, head->name))
		match = 1;
40

41 42
	if (!match)
		return 0;
43

44
	/* If we have the object, mark it so we don't ask for it */
45
	if (git_odb_exists(odb, &head->oid)) {
46
		head->local = 1;
47
	}
48
	else
49
		remote->need_pack = 1;
50

51
	return git_vector_insert(&remote->refs, head);
52 53
}

54
static int filter_wants(git_remote *remote, const git_fetch_options *opts)
55
{
56
	git_remote_head **heads;
57
	git_refspec tagspec, head;
58 59 60
	int error = 0;
	git_odb *odb;
	size_t i, heads_len;
61 62
	git_remote_autotag_option_t tagopt = remote->download_tags;

63
	if (opts && opts->download_tags != GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED)
64
		tagopt = opts->download_tags;
65

66 67
	git_vector_clear(&remote->refs);
	if ((error = git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true)) < 0)
nulltoken committed
68
		return error;
69

70 71 72 73 74 75
	/*
	 * 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.
	 */
76 77 78 79
	if (remote->active_refspecs.length == 0) {
		if ((error = git_refspec__parse(&head, "HEAD", true)) < 0)
			goto cleanup;

80 81 82 83
		error = git_refspec__dwim_one(&remote->active_refspecs, &head, &remote->refs);
		git_refspec__free(&head);

		if (error < 0)
84 85 86
			goto cleanup;
	}

87 88
	if (git_repository_odb__weakptr(&odb, remote->repo) < 0)
		goto cleanup;
89

90
	if (git_remote_ls((const git_remote_head ***)&heads, &heads_len, remote) < 0)
nulltoken committed
91
		goto cleanup;
92

93
	for (i = 0; i < heads_len; i++) {
94
		if ((error = maybe_want(remote, heads[i], odb, &tagspec, tagopt)) < 0)
95 96
			break;
	}
nulltoken committed
97 98 99 100 101

cleanup:
	git_refspec__free(&tagspec);

	return error;
102
}
Carlos Martín Nieto committed
103 104 105 106 107 108

/*
 * 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
 */
109
int git_fetch_negotiate(git_remote *remote, const git_fetch_options *opts)
Carlos Martín Nieto committed
110
{
111
	git_transport *t = remote->transport;
112

113
	remote->need_pack = 0;
Etienne Samson committed
114

115
	if (filter_wants(remote, opts) < 0) {
116
		giterr_set(GITERR_NET, "failed to filter the reference list for wants");
117 118
		return -1;
	}
119

Carlos Martín Nieto committed
120
	/* Don't try to negotiate when we don't want anything */
121
	if (!remote->need_pack)
122
		return 0;
123

Carlos Martín Nieto committed
124
	/*
125
	 * Now we have everything set up so we can start tell the
126
	 * server what we want and what we have.
127
	 */
128 129 130 131
	return t->negotiate_fetch(t,
		remote->repo,
		(const git_remote_head * const *)remote->refs.contents,
		remote->refs.length);
Carlos Martín Nieto committed
132 133
}

134
int git_fetch_download_pack(git_remote *remote, const git_remote_callbacks *callbacks)
135
{
136
	git_transport *t = remote->transport;
137 138
	git_transfer_progress_cb progress = NULL;
	void *payload = NULL;
139

140
	if (!remote->need_pack)
141
		return 0;
142

143 144 145 146 147 148 149 150 151 152 153 154 155
	if (callbacks) {
		progress = callbacks->transfer_progress;
		payload  = callbacks->payload;
	}

	return t->download_pack(t, remote->repo, &remote->stats, progress, payload);
}

int git_fetch_init_options(git_fetch_options *opts, unsigned int version)
{
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_fetch_options, GIT_FETCH_OPTIONS_INIT);
	return 0;
156
}