fetchhead.c 4.61 KB
Newer Older
1 2
#include "clar_libgit2.h"

3
#include "futils.h"
4
#include "fetchhead.h"
5
#include "../fetchhead/fetchhead_data.h"
6 7
#include "git2/clone.h"

8
#define LIVE_REPO_URL "https://github.com/libgit2/TestGitRepository"
9 10

static git_repository *g_repo;
11
static git_clone_options g_options;
12

13
void test_online_fetchhead__initialize(void)
14
{
15
	git_fetch_options dummy_fetch = GIT_FETCH_OPTIONS_INIT;
16
	g_repo = NULL;
17 18 19

	memset(&g_options, 0, sizeof(git_clone_options));
	g_options.version = GIT_CLONE_OPTIONS_VERSION;
20
	g_options.fetch_opts = dummy_fetch;
21 22
}

23
void test_online_fetchhead__cleanup(void)
24
{
25
	if (g_repo) {
26
		git_repository_free(g_repo);
27 28 29
		g_repo = NULL;
	}

30
	cl_fixture_cleanup("./foo");
31 32 33 34
}

static void fetchhead_test_clone(void)
{
35
	cl_git_pass(git_clone(&g_repo, LIVE_REPO_URL, "./foo", &g_options));
36 37
}

38
static size_t count_references(void)
39 40
{
	git_strarray array;
41
	size_t refs;
42 43 44 45

	cl_git_pass(git_reference_list(&array, g_repo));
	refs = array.count;

46
	git_strarray_dispose(&array);
47 48 49 50

	return refs;
}

51 52 53
static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fetchhead)
{
	git_remote *remote;
54
	git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
55
	git_str fetchhead_buf = GIT_STR_INIT;
56
	git_strarray array, *active_refs = NULL;
57

58
	cl_git_pass(git_remote_lookup(&remote, g_repo, "origin"));
59
	fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO;
60

61
	if(fetchspec != NULL) {
62 63 64
		array.count = 1;
		array.strings = (char **) &fetchspec;
		active_refs = &array;
65
	}
66

67
	cl_git_pass(git_remote_fetch(remote, active_refs, &fetch_opts, NULL));
68 69
	git_remote_free(remote);

Ben Straub committed
70
	cl_git_pass(git_futils_readbuffer(&fetchhead_buf, "./foo/.git/FETCH_HEAD"));
71

72
	cl_assert_equal_s(fetchhead_buf.ptr, expected_fetchhead);
73
	git_str_dispose(&fetchhead_buf);
74 75
}

76
void test_online_fetchhead__wildcard_spec(void)
77 78
{
	fetchhead_test_clone();
79 80 81 82 83
	fetchhead_test_fetch(NULL, FETCH_HEAD_WILDCARD_DATA2);
	cl_git_pass(git_tag_delete(g_repo, "annotated_tag"));
	cl_git_pass(git_tag_delete(g_repo, "blob"));
	cl_git_pass(git_tag_delete(g_repo, "commit_tree"));
	cl_git_pass(git_tag_delete(g_repo, "nearly-dangling"));
84 85 86
	fetchhead_test_fetch(NULL, FETCH_HEAD_WILDCARD_DATA);
}

87
void test_online_fetchhead__explicit_spec(void)
88 89 90 91 92
{
	fetchhead_test_clone();
	fetchhead_test_fetch("refs/heads/first-merge:refs/remotes/origin/first-merge", FETCH_HEAD_EXPLICIT_DATA);
}

93
void test_online_fetchhead__no_merges(void)
94 95 96 97 98 99
{
	git_config *config;

	fetchhead_test_clone();

	cl_git_pass(git_repository_config(&config, g_repo));
100 101
	cl_git_pass(git_config_delete_entry(config, "branch.master.remote"));
	cl_git_pass(git_config_delete_entry(config, "branch.master.merge"));
102
	git_config_free(config);
103

104 105 106 107 108
	fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA2);
	cl_git_pass(git_tag_delete(g_repo, "annotated_tag"));
	cl_git_pass(git_tag_delete(g_repo, "blob"));
	cl_git_pass(git_tag_delete(g_repo, "commit_tree"));
	cl_git_pass(git_tag_delete(g_repo, "nearly-dangling"));
109
	fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA);
110 111
	cl_git_pass(git_tag_delete(g_repo, "commit_tree"));
	fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA3);
112
}
113 114 115 116

void test_online_fetchhead__explicit_dst_refspec_creates_branch(void)
{
	git_reference *ref;
117
	size_t refs;
118 119 120 121 122 123 124

	fetchhead_test_clone();
	refs = count_references();
	fetchhead_test_fetch("refs/heads/first-merge:refs/heads/explicit-refspec", FETCH_HEAD_EXPLICIT_DATA);

	cl_git_pass(git_branch_lookup(&ref, g_repo, "explicit-refspec", GIT_BRANCH_ALL));
	cl_assert_equal_i(refs + 1, count_references());
125 126

	git_reference_free(ref);
127 128 129 130 131
}

void test_online_fetchhead__empty_dst_refspec_creates_no_branch(void)
{
	git_reference *ref;
132
	size_t refs;
133 134 135 136 137 138 139 140 141 142 143 144

	fetchhead_test_clone();
	refs = count_references();

	fetchhead_test_fetch("refs/heads/first-merge", FETCH_HEAD_EXPLICIT_DATA);
	cl_git_fail(git_branch_lookup(&ref, g_repo, "first-merge", GIT_BRANCH_ALL));

	cl_assert_equal_i(refs, count_references());
}

void test_online_fetchhead__colon_only_dst_refspec_creates_no_branch(void)
{
145
	size_t refs;
146 147 148 149 150 151 152

	fetchhead_test_clone();
	refs = count_references();
	fetchhead_test_fetch("refs/heads/first-merge:", FETCH_HEAD_EXPLICIT_DATA);

	cl_assert_equal_i(refs, count_references());
}
153 154 155

void test_online_fetchhead__creds_get_stripped(void)
{
156
	git_str buf = GIT_STR_INIT;
157 158 159 160 161 162 163 164 165 166 167
	git_remote *remote;

	cl_git_pass(git_repository_init(&g_repo, "./foo", 0));
	cl_git_pass(git_remote_create_anonymous(&remote, g_repo, "https://foo:bar@github.com/libgit2/TestGitRepository"));
	cl_git_pass(git_remote_fetch(remote, NULL, NULL, NULL));

	cl_git_pass(git_futils_readbuffer(&buf, "./foo/.git/FETCH_HEAD"));
	cl_assert_equal_s(buf.ptr,
		"49322bb17d3acc9146f98c97d078513228bbf3c0\t\thttps://github.com/libgit2/TestGitRepository\n");

	git_remote_free(remote);
168
	git_str_dispose(&buf);
169
}