fetch.c 5.82 KB
Newer Older
1 2 3 4 5
#include "clar_libgit2.h"

static git_repository *_repo;
static int counter;

6
void test_online_fetch__initialize(void)
7 8 9 10
{
	cl_git_pass(git_repository_init(&_repo, "./fetch", 0));
}

11
void test_online_fetch__cleanup(void)
12 13
{
	git_repository_free(_repo);
14 15
	_repo = NULL;

16 17 18 19 20
	cl_fixture_cleanup("./fetch");
}

static int update_tips(const char *refname, const git_oid *a, const git_oid *b, void *data)
{
21
	GIT_UNUSED(refname); GIT_UNUSED(a); GIT_UNUSED(b); GIT_UNUSED(data);
22 23 24 25 26 27

	++counter;

	return 0;
}

28
static int progress(const git_transfer_progress *stats, void *payload)
29
{
30
	size_t *bytes_received = (size_t *)payload;
31
	*bytes_received = stats->received_bytes;
32
	return 0;
33 34
}

nulltoken committed
35
static void do_fetch(const char *url, git_remote_autotag_option_t flag, int n)
36 37
{
	git_remote *remote;
38
	git_fetch_options options = GIT_FETCH_OPTIONS_INIT;
39
	size_t bytes_received = 0;
40

41 42 43
	options.callbacks.transfer_progress = progress;
	options.callbacks.update_tips = update_tips;
	options.callbacks.payload = &bytes_received;
44
	options.download_tags = flag;
45 46
	counter = 0;

Ben Straub committed
47
	cl_git_pass(git_remote_create(&remote, _repo, "test", url));
48
	cl_git_pass(git_remote_fetch(remote, NULL, &options, NULL));
49
	cl_assert_equal_i(counter, n);
50
	cl_assert(bytes_received > 0);
nulltoken committed
51 52

	git_remote_free(remote);
53 54
}

55
void test_online_fetch__default_git(void)
56
{
57
	do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6);
58 59
}

60
void test_online_fetch__default_http(void)
61
{
62
	do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6);
63 64
}

65 66
void test_online_fetch__default_https(void)
{
67
	do_fetch("https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 6);
68 69
}

70
void test_online_fetch__no_tags_git(void)
71 72 73 74
{
	do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
}

75
void test_online_fetch__no_tags_http(void)
76 77 78
{
	do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
}
nulltoken committed
79

80 81 82 83
void test_online_fetch__fetch_twice(void)
{
	git_remote *remote;
	cl_git_pass(git_remote_create(&remote, _repo, "test", "git://github.com/libgit2/TestGitRepository.git"));
84
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
85
	cl_git_pass(git_remote_download(remote, NULL, NULL));
86 87
    	git_remote_disconnect(remote);
    	
88
	git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL);
89
	cl_git_pass(git_remote_download(remote, NULL, NULL));
90 91 92 93 94
	git_remote_disconnect(remote);
	
	git_remote_free(remote);
}

95
static int transferProgressCallback(const git_transfer_progress *stats, void *payload)
nulltoken committed
96 97
{
	bool *invoked = (bool *)payload;
nulltoken committed
98 99

	GIT_UNUSED(stats);
nulltoken committed
100
	*invoked = true;
101
	return 0;
nulltoken committed
102 103
}

104
void test_online_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date(void)
nulltoken committed
105 106 107
{
	git_repository *_repository;
	bool invoked = false;
108
	git_remote *remote;
109
	git_fetch_options options = GIT_FETCH_OPTIONS_INIT;
110 111
	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
	opts.bare = true;
nulltoken committed
112

113 114
	cl_git_pass(git_clone(&_repository, "https://github.com/libgit2/TestGitRepository.git",
				"./fetch/lg2", &opts));
nulltoken committed
115 116 117 118
	git_repository_free(_repository);

	cl_git_pass(git_repository_open(&_repository, "./fetch/lg2"));

119
	cl_git_pass(git_remote_lookup(&remote, _repository, "origin"));
120
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
nulltoken committed
121 122 123

	cl_assert_equal_i(false, invoked);

124 125 126
	options.callbacks.transfer_progress = &transferProgressCallback;
	options.callbacks.payload = &invoked;
	cl_git_pass(git_remote_download(remote, NULL, &options));
nulltoken committed
127 128 129

	cl_assert_equal_i(false, invoked);

130
	cl_git_pass(git_remote_update_tips(remote, &options.callbacks, 1, options.download_tags, NULL));
nulltoken committed
131 132 133 134 135
	git_remote_disconnect(remote);

	git_remote_free(remote);
	git_repository_free(_repository);
}
136 137 138 139 140 141

static int cancel_at_half(const git_transfer_progress *stats, void *payload)
{
	GIT_UNUSED(payload);

	if (stats->received_objects > (stats->total_objects/2))
142
		return -4321;
143 144 145 146 147 148 149
	return 0;
}

void test_online_fetch__can_cancel(void)
{
	git_remote *remote;
	size_t bytes_received = 0;
150
	git_fetch_options options = GIT_FETCH_OPTIONS_INIT;
151 152 153

	cl_git_pass(git_remote_create(&remote, _repo, "test",
				"http://github.com/libgit2/TestGitRepository.git"));
154

155 156
	options.callbacks.transfer_progress = cancel_at_half;
	options.callbacks.payload = &bytes_received;
157

158
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
159
	cl_git_fail_with(git_remote_download(remote, NULL, &options), -4321);
160 161 162
	git_remote_disconnect(remote);
	git_remote_free(remote);
}
163 164 165

void test_online_fetch__ls_disconnected(void)
{
166 167
	const git_remote_head **refs;
	size_t refs_len_before, refs_len_after;
168 169 170 171
	git_remote *remote;

	cl_git_pass(git_remote_create(&remote, _repo, "test",
				"http://github.com/libgit2/TestGitRepository.git"));
172
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
173
	cl_git_pass(git_remote_ls(&refs, &refs_len_before, remote));
174
	git_remote_disconnect(remote);
175
	cl_git_pass(git_remote_ls(&refs, &refs_len_after, remote));
176

177
	cl_assert_equal_i(refs_len_before, refs_len_after);
178 179 180

	git_remote_free(remote);
}
181 182 183 184 185 186 187 188 189

void test_online_fetch__remote_symrefs(void)
{
	const git_remote_head **refs;
	size_t refs_len;
	git_remote *remote;

	cl_git_pass(git_remote_create(&remote, _repo, "test",
				"http://github.com/libgit2/TestGitRepository.git"));
190
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH, NULL, NULL, NULL));
191 192 193 194 195 196 197 198
	git_remote_disconnect(remote);
	cl_git_pass(git_remote_ls(&refs, &refs_len, remote));

	cl_assert_equal_s("HEAD", refs[0]->name);
	cl_assert_equal_s("refs/heads/master", refs[0]->symref_target);

	git_remote_free(remote);
}
199 200 201 202 203 204

void test_online_fetch__twice(void)
{
	git_remote *remote;

	cl_git_pass(git_remote_create(&remote, _repo, "test", "http://github.com/libgit2/TestGitRepository.git"));
205 206
	cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
	cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));
207 208 209

	git_remote_free(remote);
}