fetch.c 4.07 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 "fetch.h"

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

#include "remote.h"
#include "refspec.h"
17
#include "pack.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, valid;
25

26 27 28 29
	if (git_reference_name_is_valid(&valid, head->name) < 0)
		return -1;

	if (!valid)
30 31
		return 0;

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

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

44 45
	if (!match)
		return 0;
46

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

54
	return git_vector_insert(&remote->refs, head);
55 56
}

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

66
	if (opts && opts->download_tags != GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED)
67
		tagopt = opts->download_tags;
68

69 70
	git_vector_clear(&remote->refs);
	if ((error = git_refspec__parse(&tagspec, GIT_REFSPEC_TAGS, true)) < 0)
nulltoken committed
71
		return error;
72

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

83
		error = git_refspec__dwim_one(&remote->active_refspecs, &head, &remote->refs);
84
		git_refspec__dispose(&head);
85 86

		if (error < 0)
87 88 89
			goto cleanup;
	}

90 91
	if (git_repository_odb__weakptr(&odb, remote->repo) < 0)
		goto cleanup;
92

93
	if (git_remote_ls((const git_remote_head ***)&heads, &heads_len, remote) < 0)
nulltoken committed
94
		goto cleanup;
95

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

cleanup:
102
	git_refspec__dispose(&tagspec);
nulltoken committed
103 104

	return error;
105
}
Carlos Martín Nieto committed
106 107 108 109 110 111

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

116
	remote->need_pack = 0;
Etienne Samson committed
117

118
	if (filter_wants(remote, opts) < 0) {
119
		git_error_set(GIT_ERROR_NET, "failed to filter the reference list for wants");
120 121
		return -1;
	}
122

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

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

137
int git_fetch_download_pack(git_remote *remote, const git_remote_callbacks *callbacks)
138
{
139
	git_transport *t = remote->transport;
140
	git_indexer_progress_cb progress = NULL;
141
	void *payload = NULL;
142

143
	if (!remote->need_pack)
144
		return 0;
145

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

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

154
int git_fetch_options_init(git_fetch_options *opts, unsigned int version)
155 156 157 158
{
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_fetch_options, GIT_FETCH_OPTIONS_INIT);
	return 0;
159
}
160

161
#ifndef GIT_DEPRECATE_HARD
162 163 164 165
int git_fetch_init_options(git_fetch_options *opts, unsigned int version)
{
	return git_fetch_options_init(opts, version);
}
166
#endif