diff_helpers.c 4.92 KB
Newer Older
1 2
#include "clar_libgit2.h"
#include "diff_helpers.h"
3
#include "git2/sys/diff.h"
4 5 6 7 8

git_tree *resolve_commit_oid_to_tree(
	git_repository *repo,
	const char *partial_oid)
{
9
	size_t len = strlen(partial_oid);
10
	git_oid oid;
11 12
	git_object *obj = NULL;
	git_tree *tree = NULL;
13 14

	if (git_oid_fromstrn(&oid, partial_oid, len) == 0)
Carlos Martín Nieto committed
15 16 17
		cl_git_pass(git_object_lookup_prefix(&obj, repo, &oid, len, GIT_OBJ_ANY));

	cl_git_pass(git_object_peel((git_object **) &tree, obj, GIT_OBJ_TREE));
18 19 20
	git_object_free(obj);
	return tree;
}
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
static char diff_pick_suffix(int mode)
{
	if (S_ISDIR(mode))
		return '/';
	else if (GIT_PERMS_IS_EXEC(mode))
		return '*';
	else
		return ' ';
}

static void fprintf_delta(FILE *fp, const git_diff_delta *delta, float progress)
{
	char code = git_diff_status_char(delta->status);
	char old_suffix = diff_pick_suffix(delta->old_file.mode);
	char new_suffix = diff_pick_suffix(delta->new_file.mode);

	fprintf(fp, "%c\t%s", code, delta->old_file.path);

	if ((delta->old_file.path != delta->new_file.path &&
		 strcmp(delta->old_file.path, delta->new_file.path) != 0) ||
		(delta->old_file.mode != delta->new_file.mode &&
		 delta->old_file.mode != 0 && delta->new_file.mode != 0))
		fprintf(fp, "%c %s%c", old_suffix, delta->new_file.path, new_suffix);
	else if (old_suffix != ' ')
		fprintf(fp, "%c", old_suffix);

	fprintf(fp, "\t[%.2f]\n", progress);
}

Vicent Marti committed
51
int diff_file_cb(
52
	const git_diff_delta *delta,
53 54
	float progress,
	void *payload)
55
{
56
	diff_expects *e = payload;
57

58
	if (e->debug)
59
		fprintf_delta(stderr, delta, progress);
60 61 62 63 64

	if (e->names)
		cl_assert_equal_s(e->names[e->files], delta->old_file.path);
	if (e->statuses)
		cl_assert_equal_i(e->statuses[e->files], (int)delta->status);
65

66 67
	e->files++;

68
	if ((delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
69
		e->files_binary++;
70 71 72 73 74

	cl_assert(delta->status <= GIT_DELTA_TYPECHANGE);

	e->file_status[delta->status] += 1;

75 76 77
	return 0;
}

78 79 80 81 82
int diff_print_file_cb(
	const git_diff_delta *delta,
	float progress,
	void *payload)
{
83 84 85 86 87 88 89 90
	if (!payload) {
		fprintf_delta(stderr, delta, progress);
		return 0;
	}

	if (!((diff_expects *)payload)->debug)
		fprintf_delta(stderr, delta, progress);

91 92 93
	return diff_file_cb(delta, progress, payload);
}

Vicent Marti committed
94
int diff_hunk_cb(
95
	const git_diff_delta *delta,
96
	const git_diff_hunk *hunk,
97
	void *payload)
98
{
99
	diff_expects *e = payload;
100
	const char *scan = hunk->header, *scan_end = scan + hunk->header_len;
101 102

	GIT_UNUSED(delta);
103 104

	/* confirm no NUL bytes in header text */
105 106
	while (scan < scan_end)
		cl_assert('\0' != *scan++);
107

108
	e->hunks++;
109 110
	e->hunk_old_lines += hunk->old_lines;
	e->hunk_new_lines += hunk->new_lines;
111 112 113
	return 0;
}

Vicent Marti committed
114
int diff_line_cb(
115
	const git_diff_delta *delta,
116 117
	const git_diff_hunk *hunk,
	const git_diff_line *line,
118
	void *payload)
119
{
120
	diff_expects *e = payload;
121 122

	GIT_UNUSED(delta);
123
	GIT_UNUSED(hunk);
124

125
	e->lines++;
126
	switch (line->origin) {
127
	case GIT_DIFF_LINE_CONTEXT:
128
	case GIT_DIFF_LINE_CONTEXT_EOFNL: /* techically not a line */
129 130 131
		e->line_ctxt++;
		break;
	case GIT_DIFF_LINE_ADDITION:
132
	case GIT_DIFF_LINE_ADD_EOFNL: /* technically not a line add */
133
		e->line_adds++;
134
		break;
135
	case GIT_DIFF_LINE_DELETION:
136
	case GIT_DIFF_LINE_DEL_EOFNL: /* technically not a line delete */
137 138 139 140 141 142 143
		e->line_dels++;
		break;
	default:
		break;
	}
	return 0;
}
Russell Belfer committed
144 145

int diff_foreach_via_iterator(
146
	git_diff *diff,
Vicent Marti committed
147 148
	git_diff_file_cb file_cb,
	git_diff_hunk_cb hunk_cb,
149
	git_diff_line_cb line_cb,
150
	void *data)
Russell Belfer committed
151
{
152
	size_t d, num_d = git_diff_num_deltas(diff);
Russell Belfer committed
153

154
	for (d = 0; d < num_d; ++d) {
155
		git_patch *patch;
156
		const git_diff_delta *delta;
157
		size_t h, num_h;
Russell Belfer committed
158

Russell Belfer committed
159 160
		cl_git_pass(git_patch_from_diff(&patch, diff, d));
		cl_assert((delta = git_patch_get_delta(patch)) != NULL);
Russell Belfer committed
161 162

		/* call file_cb for this file */
163
		if (file_cb != NULL && file_cb(delta, (float)d / num_d, data) != 0) {
164
			git_patch_free(patch);
Russell Belfer committed
165
			goto abort;
166
		}
Russell Belfer committed
167

168 169
		/* if there are no changes, then the patch will be NULL */
		if (!patch) {
170 171
			cl_assert(delta->status == GIT_DELTA_UNMODIFIED ||
					  (delta->flags & GIT_DIFF_FLAG_BINARY) != 0);
172 173 174
			continue;
		}

175
		if (!hunk_cb && !line_cb) {
176
			git_patch_free(patch);
Russell Belfer committed
177
			continue;
178 179
		}

180
		num_h = git_patch_num_hunks(patch);
Russell Belfer committed
181

182
		for (h = 0; h < num_h; h++) {
183 184
			const git_diff_hunk *hunk;
			size_t l, num_l;
Russell Belfer committed
185

186
			cl_git_pass(git_patch_get_hunk(&hunk, &num_l, patch, h));
187

188
			if (hunk_cb && hunk_cb(delta, hunk, data) != 0) {
189
				git_patch_free(patch);
Russell Belfer committed
190
				goto abort;
191
			}
Russell Belfer committed
192

193
			for (l = 0; l < num_l; ++l) {
194
				const git_diff_line *line;
Russell Belfer committed
195

196
				cl_git_pass(git_patch_get_line_in_hunk(&line, patch, h, l));
Russell Belfer committed
197

198
				if (line_cb &&
199
					line_cb(delta, hunk, line, data) != 0) {
200
					git_patch_free(patch);
Russell Belfer committed
201
					goto abort;
202
				}
Russell Belfer committed
203 204 205
			}
		}

206
		git_patch_free(patch);
Russell Belfer committed
207 208
	}

209
	return 0;
Russell Belfer committed
210 211 212 213 214

abort:
	giterr_clear();
	return GIT_EUSER;
}
215

216
void diff_print(FILE *fp, git_diff *diff)
217
{
218 219 220
	cl_git_pass(
		git_diff_print(diff, GIT_DIFF_FORMAT_PATCH,
			git_diff_print_callback__to_file_handle, fp ? fp : stderr));
221
}
222

223
void diff_print_raw(FILE *fp, git_diff *diff)
224
{
225 226 227
	cl_git_pass(
		git_diff_print(diff, GIT_DIFF_FORMAT_RAW,
			git_diff_print_callback__to_file_handle, fp ? fp : stderr));
228
}