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

static git_repository *g_repo = NULL;

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

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

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);
23
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
24
	git_diff *diff = NULL;
25 26 27 28 29 30 31 32 33 34
	diff_expects exp;

	cl_assert(a);
	cl_assert(b);

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

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

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

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

	/* 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
47
	cl_assert_equal_i(8, exp.files);
48 49 50
	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]);
51

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

Russell Belfer committed
54 55 56 57
	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);
58

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

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

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

	/* 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
75
	cl_assert_equal_i(12, exp.files);
76 77 78
	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]);
79

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

Russell Belfer committed
82 83 84 85
	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);
86

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

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

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

	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);
116
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
117
	git_diff *diff = NULL;
118 119 120 121 122 123 124 125 126 127
	diff_expects exp;

	cl_assert(a);
	cl_assert(b);

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

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

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

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

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

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

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

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;
147
	git_diff *diff = NULL;
148 149 150
	const git_error *err;

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

	giterr_clear();
	opts.version = 1024;
158
	cl_git_fail(git_diff_tree_to_index(&diff, g_repo, a, NULL, &opts));
159 160
	err = giterr_last();
	cl_assert_equal_i(GITERR_INVALID, err->klass);
161 162 163
	cl_assert_equal_p(diff, NULL);

	git_tree_free(a);
164 165
}