Welcome to mirror list, hosted at ThFree Co, Russian Federation.

setup.c « merge « tests-clar - github.com/mono/libgit2.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 946c67e7bc19bcac3fe3cf2acb0521501e739548 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "clar_libgit2.h"
#include "git2/repository.h"
#include "git2/merge.h"
#include "merge.h"
#include "refs.h"
#include "fileops.h"

static git_repository *repo;
static git_index *repo_index;

#define TEST_REPO_PATH "testrepo"
#define TEST_INDEX_PATH TEST_REPO_PATH "/.git/index"

#define ORIG_HEAD                   "bd593285fc7fe4ca18ccdbabf027f5d689101452"

#define THEIRS_SIMPLE_BRANCH        "branch"
#define THEIRS_SIMPLE_OID           "7cb63eed597130ba4abb87b3e544b85021905520"

#define OCTO1_BRANCH                "octo1"
#define OCTO1_OID                   "16f825815cfd20a07a75c71554e82d8eede0b061"

#define OCTO2_BRANCH                "octo2"
#define OCTO2_OID                   "158dc7bedb202f5b26502bf3574faa7f4238d56c"

#define OCTO3_BRANCH                "octo3"
#define OCTO3_OID                   "50ce7d7d01217679e26c55939eef119e0c93e272"

#define OCTO4_BRANCH                "octo4"
#define OCTO4_OID                   "54269b3f6ec3d7d4ede24dd350dd5d605495c3ae"

#define OCTO5_BRANCH                "octo5"
#define OCTO5_OID                   "e4f618a2c3ed0669308735727df5ebf2447f022f"

// Fixture setup and teardown
void test_merge_setup__initialize(void)
{
	repo = cl_git_sandbox_init(TEST_REPO_PATH);
    git_repository_index(&repo_index, repo);
}

void test_merge_setup__cleanup(void)
{
    git_index_free(repo_index);
	cl_git_sandbox_cleanup();
}

static void write_file_contents(const char *filename, const char *output)
{
	git_buf file_path_buf = GIT_BUF_INIT;

    git_buf_printf(&file_path_buf, "%s/%s", git_repository_path(repo), filename);
	cl_git_rewritefile(file_path_buf.ptr, output);

	git_buf_free(&file_path_buf);
}

struct merge_head_cb_data {
	const char **oid_str;
	unsigned int len;

	unsigned int i;
};

static int merge_head_foreach_cb(const git_oid *oid, void *payload)
{
	git_oid expected_oid;
	struct merge_head_cb_data *cb_data = payload;

	git_oid_fromstr(&expected_oid, cb_data->oid_str[cb_data->i]);
	cl_assert(git_oid_cmp(&expected_oid, oid) == 0);
	cb_data->i++;
	return 0;
}

void test_merge_setup__head_notfound(void)
{
	int error;

	cl_git_fail((error = git_repository_mergehead_foreach(repo,
		merge_head_foreach_cb, NULL)));
	cl_assert(error == GIT_ENOTFOUND);
}

void test_merge_setup__head_invalid_oid(void)
{
	int error;

	write_file_contents(GIT_MERGE_HEAD_FILE, "invalid-oid\n");

	cl_git_fail((error = git_repository_mergehead_foreach(repo,
		merge_head_foreach_cb, NULL)));
	cl_assert(error == -1);
}

void test_merge_setup__head_foreach_nonewline(void)
{
	int error;

	write_file_contents(GIT_MERGE_HEAD_FILE, THEIRS_SIMPLE_OID);

	cl_git_fail((error = git_repository_mergehead_foreach(repo,
		merge_head_foreach_cb, NULL)));
	cl_assert(error == -1);
}

void test_merge_setup__head_foreach_one(void)
{
	const char *expected = THEIRS_SIMPLE_OID;

	struct merge_head_cb_data cb_data = { &expected, 1 };

	write_file_contents(GIT_MERGE_HEAD_FILE, THEIRS_SIMPLE_OID "\n");

	cl_git_pass(git_repository_mergehead_foreach(repo,
		merge_head_foreach_cb, &cb_data));

	cl_assert(cb_data.i == cb_data.len);
}

void test_merge_setup__head_foreach_octopus(void)
{
	const char *expected[] = { THEIRS_SIMPLE_OID,
		OCTO1_OID, OCTO2_OID, OCTO3_OID, OCTO4_OID, OCTO5_OID };

	struct merge_head_cb_data cb_data = { expected, 6 };

	write_file_contents(GIT_MERGE_HEAD_FILE,
		THEIRS_SIMPLE_OID "\n"
		OCTO1_OID "\n"
		OCTO2_OID "\n"
		OCTO3_OID "\n"
		OCTO4_OID "\n"
		OCTO5_OID "\n");

	cl_git_pass(git_repository_mergehead_foreach(repo,
		merge_head_foreach_cb, &cb_data));

	cl_assert(cb_data.i == cb_data.len);
}