fetch.c 5.6 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_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
39
	size_t bytes_received = 0;
40

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

Ben Straub committed
46
	cl_git_pass(git_remote_create(&remote, _repo, "test", url));
47 48
	git_remote_set_callbacks(remote, &callbacks);
	git_remote_set_autotag(remote, flag);
Ben Straub committed
49
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
50
	cl_git_pass(git_remote_download(remote, NULL));
51
	cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
52
	git_remote_disconnect(remote);
53
	cl_assert_equal_i(counter, n);
54
	cl_assert(bytes_received > 0);
nulltoken committed
55 56

	git_remote_free(remote);
57 58
}

59
void test_online_fetch__default_git(void)
60
{
61
	do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 5);
62 63
}

64
void test_online_fetch__default_http(void)
65
{
66
	do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 5);
67 68
}

69 70
void test_online_fetch__default_https(void)
{
71
	do_fetch("https://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_AUTO, 5);
72 73
}

74
void test_online_fetch__no_tags_git(void)
75 76 77 78
{
	do_fetch("git://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
}

79
void test_online_fetch__no_tags_http(void)
80 81 82
{
	do_fetch("http://github.com/libgit2/TestGitRepository.git", GIT_REMOTE_DOWNLOAD_TAGS_NONE, 3);
}
nulltoken committed
83

84 85 86 87 88
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"));
    	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
89
    	cl_git_pass(git_remote_download(remote, NULL));
90 91 92
    	git_remote_disconnect(remote);
    	
    	git_remote_connect(remote, GIT_DIRECTION_FETCH);
93
	cl_git_pass(git_remote_download(remote, NULL));
94 95 96 97 98
	git_remote_disconnect(remote);
	
	git_remote_free(remote);
}

99
static int transferProgressCallback(const git_transfer_progress *stats, void *payload)
nulltoken committed
100 101
{
	bool *invoked = (bool *)payload;
nulltoken committed
102 103

	GIT_UNUSED(stats);
nulltoken committed
104
	*invoked = true;
105
	return 0;
nulltoken committed
106 107
}

108
void test_online_fetch__doesnt_retrieve_a_pack_when_the_repository_is_up_to_date(void)
nulltoken committed
109 110 111
{
	git_repository *_repository;
	bool invoked = false;
112
	git_remote *remote;
113
	git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
114 115
	git_clone_options opts = GIT_CLONE_OPTIONS_INIT;
	opts.bare = true;
nulltoken committed
116

117 118
	cl_git_pass(git_clone(&_repository, "https://github.com/libgit2/TestGitRepository.git",
				"./fetch/lg2", &opts));
nulltoken committed
119 120 121 122 123 124 125 126 127
	git_repository_free(_repository);

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

	cl_git_pass(git_remote_load(&remote, _repository, "origin"));
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));

	cl_assert_equal_i(false, invoked);

128 129 130
	callbacks.transfer_progress = &transferProgressCallback;
	callbacks.payload = &invoked;
	git_remote_set_callbacks(remote, &callbacks);
131
	cl_git_pass(git_remote_download(remote, NULL));
nulltoken committed
132 133 134

	cl_assert_equal_i(false, invoked);

135
	cl_git_pass(git_remote_update_tips(remote, NULL, NULL));
nulltoken committed
136 137 138 139 140
	git_remote_disconnect(remote);

	git_remote_free(remote);
	git_repository_free(_repository);
}
141 142 143 144 145 146

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

	if (stats->received_objects > (stats->total_objects/2))
147
		return -4321;
148 149 150 151 152 153 154
	return 0;
}

void test_online_fetch__can_cancel(void)
{
	git_remote *remote;
	size_t bytes_received = 0;
155
	git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
156 157 158

	cl_git_pass(git_remote_create(&remote, _repo, "test",
				"http://github.com/libgit2/TestGitRepository.git"));
159 160 161 162 163

	callbacks.transfer_progress = cancel_at_half;
	callbacks.payload = &bytes_received;
	git_remote_set_callbacks(remote, &callbacks);

164
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
165
	cl_git_fail_with(git_remote_download(remote, NULL), -4321);
166 167 168
	git_remote_disconnect(remote);
	git_remote_free(remote);
}
169 170 171

void test_online_fetch__ls_disconnected(void)
{
172 173
	const git_remote_head **refs;
	size_t refs_len_before, refs_len_after;
174 175 176 177 178
	git_remote *remote;

	cl_git_pass(git_remote_create(&remote, _repo, "test",
				"http://github.com/libgit2/TestGitRepository.git"));
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
179
	cl_git_pass(git_remote_ls(&refs, &refs_len_before, remote));
180
	git_remote_disconnect(remote);
181
	cl_git_pass(git_remote_ls(&refs, &refs_len_after, remote));
182

183
	cl_assert_equal_i(refs_len_before, refs_len_after);
184 185 186

	git_remote_free(remote);
}
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

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"));
	cl_git_pass(git_remote_connect(remote, GIT_DIRECTION_FETCH));
	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);
}