index.c 8.38 KB
Newer Older
1 2
#include "clar_libgit2.h"
#include "diff_helpers.h"
3
#include "index.h"
4 5 6 7 8

static git_repository *g_repo = NULL;

void test_diff_index__initialize(void)
{
9
	g_repo = cl_git_sandbox_init("status");
10 11 12 13
}

void test_diff_index__cleanup(void)
{
14
	cl_git_sandbox_cleanup();
15 16 17 18 19 20 21 22 23
}

void test_diff_index__0(void)
{
	/* grabbed a couple of commit oids from the history of the attr repo */
	const char *a_commit = "26a125ee1bf"; /* the current HEAD */
	const char *b_commit = "0017bd4ab1ec3"; /* the start */
	git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit);
	git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit);
24
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
25
	git_diff *diff = NULL;
26 27 28 29 30 31 32 33 34 35
	diff_expects exp;

	cl_assert(a);
	cl_assert(b);

	opts.context_lines = 1;
	opts.interhunk_lines = 1;

	memset(&exp, 0, sizeof(exp));

36
	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts));
37 38

	cl_git_pass(git_diff_foreach(
39
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
40 41 42 43 44 45 46 47

	/* to generate these values:
	 * - cd to tests/resources/status,
	 * - mv .gitted .git
	 * - git diff --name-status --cached 26a125ee1bf
	 * - git diff -U1 --cached 26a125ee1bf
	 * - mv .git .gitted
	 */
Russell Belfer committed
48
	cl_assert_equal_i(8, exp.files);
49 50 51
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]);
52

Russell Belfer committed
53
	cl_assert_equal_i(8, exp.hunks);
54

Russell Belfer committed
55 56 57 58
	cl_assert_equal_i(11, exp.lines);
	cl_assert_equal_i(3, exp.line_ctxt);
	cl_assert_equal_i(6, exp.line_adds);
	cl_assert_equal_i(2, exp.line_dels);
59

60
	git_diff_free(diff);
61 62 63
	diff = NULL;
	memset(&exp, 0, sizeof(exp));

64
	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, b, NULL, &opts));
65 66

	cl_git_pass(git_diff_foreach(
67
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
68 69 70 71 72 73 74 75

	/* to generate these values:
	 * - cd to tests/resources/status,
	 * - mv .gitted .git
	 * - git diff --name-status --cached 0017bd4ab1ec3
	 * - git diff -U1 --cached 0017bd4ab1ec3
	 * - mv .git .gitted
	 */
Russell Belfer committed
76
	cl_assert_equal_i(12, exp.files);
77 78 79
	cl_assert_equal_i(7, exp.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]);
80

Russell Belfer committed
81
	cl_assert_equal_i(12, exp.hunks);
82

Russell Belfer committed
83 84 85 86
	cl_assert_equal_i(16, exp.lines);
	cl_assert_equal_i(3, exp.line_ctxt);
	cl_assert_equal_i(11, exp.line_adds);
	cl_assert_equal_i(2, exp.line_dels);
87

88
	git_diff_free(diff);
89 90 91 92 93
	diff = NULL;

	git_tree_free(a);
	git_tree_free(b);
}
94 95

static int diff_stop_after_2_files(
96
	const git_diff_delta *delta,
97 98
	float progress,
	void *payload)
99
{
100
	diff_expects *e = payload;
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

	GIT_UNUSED(progress);
	GIT_UNUSED(delta);

	e->files++;

	return (e->files == 2);
}

void test_diff_index__1(void)
{
	/* grabbed a couple of commit oids from the history of the attr repo */
	const char *a_commit = "26a125ee1bf"; /* the current HEAD */
	const char *b_commit = "0017bd4ab1ec3"; /* the start */
	git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit);
	git_tree *b = resolve_commit_oid_to_tree(g_repo, b_commit);
117
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
118
	git_diff *diff = NULL;
119 120 121 122 123 124 125 126 127 128
	diff_expects exp;

	cl_assert(a);
	cl_assert(b);

	opts.context_lines = 1;
	opts.interhunk_lines = 1;

	memset(&exp, 0, sizeof(exp));

129
	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts));
130

131 132
	cl_assert_equal_i(1, git_diff_foreach(
		diff, diff_stop_after_2_files, NULL, NULL, NULL, &exp) );
133

Russell Belfer committed
134
	cl_assert_equal_i(2, exp.files);
135

136
	git_diff_free(diff);
137 138 139 140 141
	diff = NULL;

	git_tree_free(a);
	git_tree_free(b);
}
142 143 144 145 146 147

void test_diff_index__checks_options_version(void)
{
	const char *a_commit = "26a125ee1bf";
	git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit);
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
148
	git_diff *diff = NULL;
149 150 151
	const git_error *err;

	opts.version = 0;
152
	cl_git_fail(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts));
153 154
	err = git_error_last();
	cl_assert_equal_i(GIT_ERROR_INVALID, err->klass);
155
	cl_assert_equal_p(diff, NULL);
156

157
	git_error_clear();
158
	opts.version = 1024;
159
	cl_git_fail(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts));
160 161
	err = git_error_last();
	cl_assert_equal_i(GIT_ERROR_INVALID, err->klass);
162 163 164
	cl_assert_equal_p(diff, NULL);

	git_tree_free(a);
165 166
}

167 168 169 170 171
static void do_conflicted_diff(diff_expects *exp, unsigned long flags)
{
	const char *a_commit = "26a125ee1bf"; /* the current HEAD */
	git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit);
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
172
	git_index_entry ancestor = {{0}}, ours = {{0}}, theirs = {{0}};
173 174 175 176 177 178 179 180 181 182 183 184 185 186
	git_diff *diff = NULL;
	git_index *index;

	cl_assert(a);

	opts.context_lines = 1;
	opts.interhunk_lines = 1;
	opts.flags |= flags;

	memset(exp, 0, sizeof(diff_expects));

	cl_git_pass(git_repository_index(&index, g_repo));

	ancestor.path = ours.path = theirs.path = "staged_changes";
187
	ancestor.mode = ours.mode = theirs.mode = GIT_FILEMODE_BLOB;
188

189 190 191
	git_oid__fromstr(&ancestor.id, "d427e0b2e138501a3d15cc376077a3631e15bd46", GIT_OID_SHA1);
	git_oid__fromstr(&ours.id, "ee3fa1b8c00aff7fe02065fdb50864bb0d932ccf", GIT_OID_SHA1);
	git_oid__fromstr(&theirs.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", GIT_OID_SHA1);
192 193 194 195 196

	cl_git_pass(git_index_conflict_add(index, &ancestor, &ours, &theirs));
	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, index, &opts));

	cl_git_pass(git_diff_foreach(
197
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, exp));
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242

	git_diff_free(diff);
	git_tree_free(a);
	git_index_free(index);
}

void test_diff_index__reports_conflicts(void)
{
	diff_expects exp;

	do_conflicted_diff(&exp, 0);

	cl_assert_equal_i(8, exp.files);
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_CONFLICTED]);

	cl_assert_equal_i(7, exp.hunks);

	cl_assert_equal_i(9, exp.lines);
	cl_assert_equal_i(2, exp.line_ctxt);
	cl_assert_equal_i(5, exp.line_adds);
	cl_assert_equal_i(2, exp.line_dels);
}

void test_diff_index__reports_conflicts_when_reversed(void)
{
	diff_expects exp;

	do_conflicted_diff(&exp, GIT_DIFF_REVERSE);

	cl_assert_equal_i(8, exp.files);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(1, exp.file_status[GIT_DELTA_CONFLICTED]);

	cl_assert_equal_i(7, exp.hunks);

	cl_assert_equal_i(9, exp.lines);
	cl_assert_equal_i(2, exp.line_ctxt);
	cl_assert_equal_i(2, exp.line_adds);
	cl_assert_equal_i(5, exp.line_dels);
}
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

void test_diff_index__not_in_head_conflicted(void)
{
	const char *a_commit = "26a125ee1bf"; /* the current HEAD */
	git_index_entry theirs = {{0}};
	git_index *index;
	git_diff *diff;
	const git_diff_delta *delta;

	git_tree *a = resolve_commit_oid_to_tree(g_repo, a_commit);

	cl_git_pass(git_repository_index(&index, g_repo));
	cl_git_pass(git_index_read_tree(index, a));

	theirs.path = "file_not_in_head";
	theirs.mode = GIT_FILEMODE_BLOB;
259
	git_oid__fromstr(&theirs.id, "2bd0a343aeef7a2cf0d158478966a6e587ff3863", GIT_OID_SHA1);
260 261 262 263 264 265 266 267 268 269 270 271
	cl_git_pass(git_index_conflict_add(index, NULL, NULL, &theirs));

	cl_git_pass(git_diff_tree_to_index(&diff, g_repo, a, index, NULL));

	cl_assert_equal_i(git_diff_num_deltas(diff), 1);
	delta = git_diff_get_delta(diff, 0);
	cl_assert_equal_i(delta->status, GIT_DELTA_CONFLICTED);

	git_diff_free(diff);
	git_index_free(index);
	git_tree_free(a);
}
272 273 274 275 276 277 278 279 280 281

void test_diff_index__to_index(void)
{
	const char *a_commit = "26a125ee1bf"; /* the current HEAD */
	git_tree *old_tree;
	git_index *old_index;
	git_index *new_index;
	git_diff *diff;
	diff_expects exp;

282
	cl_git_pass(git_index__new(&old_index, GIT_OID_SHA1));
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
	old_tree = resolve_commit_oid_to_tree(g_repo, a_commit);
	cl_git_pass(git_index_read_tree(old_index, old_tree));

	cl_git_pass(git_repository_index(&new_index, g_repo));

	cl_git_pass(git_diff_index_to_index(&diff, g_repo, old_index, new_index, NULL));

	memset(&exp, 0, sizeof(diff_expects));
	cl_git_pass(git_diff_foreach(
		diff, diff_file_cb, diff_binary_cb, diff_hunk_cb, diff_line_cb, &exp));
	cl_assert_equal_i(8, exp.files);
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_ADDED]);
	cl_assert_equal_i(2, exp.file_status[GIT_DELTA_DELETED]);
	cl_assert_equal_i(3, exp.file_status[GIT_DELTA_MODIFIED]);
	cl_assert_equal_i(0, exp.file_status[GIT_DELTA_CONFLICTED]);

	git_diff_free(diff);
	git_index_free(new_index);
	git_index_free(old_index);
	git_tree_free(old_tree);
}