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

3
#include "fileops.h"
4
#include "fetchhead.h"
5
#include "../fetchhead/fetchhead_data.h"
6 7 8 9 10
#include "git2/clone.h"

#define LIVE_REPO_URL "git://github.com/libgit2/TestGitRepository"

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 39 40
}

static void fetchhead_test_fetch(const char *fetchspec, const char *expected_fetchhead)
{
	git_remote *remote;
41
	git_fetch_options fetch_opts = GIT_FETCH_OPTIONS_INIT;
42 43
	git_buf fetchhead_buf = GIT_BUF_INIT;
	int equals = 0;
44
	git_strarray array, *active_refs = NULL;
45

46
	cl_git_pass(git_remote_lookup(&remote, g_repo, "origin"));
47
	fetch_opts.download_tags = GIT_REMOTE_DOWNLOAD_TAGS_AUTO;
48

49
	if(fetchspec != NULL) {
50 51 52
		array.count = 1;
		array.strings = (char **) &fetchspec;
		active_refs = &array;
53
	}
54

55
	cl_git_pass(git_remote_fetch(remote, active_refs, &fetch_opts, NULL));
56 57
	git_remote_free(remote);

Ben Straub committed
58
	cl_git_pass(git_futils_readbuffer(&fetchhead_buf, "./foo/.git/FETCH_HEAD"));
59 60 61 62 63 64 65 66

	equals = (strcmp(fetchhead_buf.ptr, expected_fetchhead) == 0);

	git_buf_free(&fetchhead_buf);

	cl_assert(equals);
}

67
void test_online_fetchhead__wildcard_spec(void)
68 69
{
	fetchhead_test_clone();
70 71 72 73 74
	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"));
75 76 77
	fetchhead_test_fetch(NULL, FETCH_HEAD_WILDCARD_DATA);
}

78
void test_online_fetchhead__explicit_spec(void)
79 80 81 82 83
{
	fetchhead_test_clone();
	fetchhead_test_fetch("refs/heads/first-merge:refs/remotes/origin/first-merge", FETCH_HEAD_EXPLICIT_DATA);
}

84
void test_online_fetchhead__no_merges(void)
85 86 87 88 89 90
{
	git_config *config;

	fetchhead_test_clone();

	cl_git_pass(git_repository_config(&config, g_repo));
91 92
	cl_git_pass(git_config_delete_entry(config, "branch.master.remote"));
	cl_git_pass(git_config_delete_entry(config, "branch.master.merge"));
93
	git_config_free(config);
94

95 96 97 98 99
	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"));
100
	fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA);
101 102
	cl_git_pass(git_tag_delete(g_repo, "commit_tree"));
	fetchhead_test_fetch(NULL, FETCH_HEAD_NO_MERGE_DATA3);
103
}