diff.c 6.8 KB
Newer Older
1 2 3 4 5
#include <stdio.h>
#include <git2.h>
#include <stdlib.h>
#include <string.h>

6
static void check(int error, const char *message)
7 8 9 10 11 12 13
{
	if (error) {
		fprintf(stderr, "%s (%d)\n", message, error);
		exit(1);
	}
}

14 15
static int resolve_to_tree(
	git_repository *repo, const char *identifier, git_tree **tree)
16 17 18 19
{
	int err = 0;
	git_object *obj = NULL;

Ben Straub committed
20
	if ((err = git_revparse_single(&obj, repo, identifier)) < 0)
21
		return err;
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

	switch (git_object_type(obj)) {
	case GIT_OBJ_TREE:
		*tree = (git_tree *)obj;
		break;
	case GIT_OBJ_COMMIT:
		err = git_commit_tree(tree, (git_commit *)obj);
		git_object_free(obj);
		break;
	default:
		err = GIT_ENOTFOUND;
	}

	return err;
}

char *colors[] = {
	"\033[m", /* reset */
	"\033[1m", /* bold */
	"\033[31m", /* red */
	"\033[32m", /* green */
	"\033[36m" /* cyan */
};

46 47
static int printer(
	const git_diff_delta *delta,
48 49
	const git_diff_hunk *hunk,
	const git_diff_line *line,
50
	void *data)
51 52 53
{
	int *last_color = data, color = 0;

54
	(void)delta; (void)hunk;
55

56
	if (*last_color >= 0) {
57 58 59
		switch (line->origin) {
		case GIT_DIFF_LINE_ADDITION:  color = 3; break;
		case GIT_DIFF_LINE_DELETION:  color = 2; break;
60 61
		case GIT_DIFF_LINE_ADD_EOFNL: color = 3; break;
		case GIT_DIFF_LINE_DEL_EOFNL: color = 2; break;
62 63 64
		case GIT_DIFF_LINE_FILE_HDR:  color = 1; break;
		case GIT_DIFF_LINE_HUNK_HDR:  color = 4; break;
		default: break;
65 66 67 68 69 70 71 72 73
		}
		if (color != *last_color) {
			if (*last_color == 1 || color == 1)
				fputs(colors[0], stdout);
			fputs(colors[color], stdout);
			*last_color = color;
		}
	}

74 75 76 77 78 79 80
	if (line->origin == GIT_DIFF_LINE_CONTEXT ||
		line->origin == GIT_DIFF_LINE_ADDITION ||
		line->origin == GIT_DIFF_LINE_DELETION)
		fputc(line->origin, stdout);

	fwrite(line->content, 1, line->content_len, stdout);

81 82 83
	return 0;
}

84
static int check_uint16_param(const char *arg, const char *pattern, uint16_t *val)
85 86 87 88 89 90
{
	size_t len = strlen(pattern);
	uint16_t strval;
	char *endptr = NULL;
	if (strncmp(arg, pattern, len))
		return 0;
Russell Belfer committed
91 92 93 94
	if (arg[len] == '\0' && pattern[len - 1] != '=')
		return 1;
	if (arg[len] == '=')
		len++;
95 96 97 98 99 100 101
	strval = strtoul(arg + len, &endptr, 0);
	if (endptr == arg)
		return 0;
	*val = strval;
	return 1;
}

102
static int check_str_param(const char *arg, const char *pattern, const char **val)
103 104 105 106
{
	size_t len = strlen(pattern);
	if (strncmp(arg, pattern, len))
		return 0;
107
	*val = (const char *)(arg + len);
108 109 110
	return 1;
}

111
static void usage(const char *message, const char *arg)
112 113 114 115 116
{
	if (message && arg)
		fprintf(stderr, "%s: %s\n", message, arg);
	else if (message)
		fprintf(stderr, "%s\n", message);
117
	fprintf(stderr, "usage: diff [<tree-oid> [<tree-oid>]]\n");
118 119 120
	exit(1);
}

121 122 123
int main(int argc, char *argv[])
{
	git_repository *repo = NULL;
124
	git_tree *t1 = NULL, *t2 = NULL;
125
	git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
Russell Belfer committed
126
	git_diff_find_options findopts = GIT_DIFF_FIND_OPTIONS_INIT;
127
	git_diff *diff;
Russell Belfer committed
128 129
	int i, color = -1, cached = 0;
	git_diff_format_t format = GIT_DIFF_FORMAT_PATCH;
130 131 132 133
	char *a, *treeish1 = NULL, *treeish2 = NULL;
	const char *dir = ".";

	git_threads_init();
134

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
	/* parse arguments as copied from git-diff */

	for (i = 1; i < argc; ++i) {
		a = argv[i];

		if (a[0] != '-') {
			if (treeish1 == NULL)
				treeish1 = a;
			else if (treeish2 == NULL)
				treeish2 = a;
			else
				usage("Only one or two tree identifiers can be provided", NULL);
		}
		else if (!strcmp(a, "-p") || !strcmp(a, "-u") ||
			!strcmp(a, "--patch"))
Russell Belfer committed
150
			format = GIT_DIFF_FORMAT_PATCH;
151 152
		else if (!strcmp(a, "--cached"))
			cached = 1;
Russell Belfer committed
153 154
		else if (!strcmp(a, "--name-only"))
			format = GIT_DIFF_FORMAT_NAME_ONLY;
155
		else if (!strcmp(a, "--name-status"))
Russell Belfer committed
156
			format = GIT_DIFF_FORMAT_NAME_STATUS;
Russell Belfer committed
157
		else if (!strcmp(a, "--raw"))
Russell Belfer committed
158
			format = GIT_DIFF_FORMAT_RAW;
159 160 161 162 163 164 165 166 167 168 169 170 171 172
		else if (!strcmp(a, "--color"))
			color = 0;
		else if (!strcmp(a, "--no-color"))
			color = -1;
		else if (!strcmp(a, "-R"))
			opts.flags |= GIT_DIFF_REVERSE;
		else if (!strcmp(a, "-a") || !strcmp(a, "--text"))
			opts.flags |= GIT_DIFF_FORCE_TEXT;
		else if (!strcmp(a, "--ignore-space-at-eol"))
			opts.flags |= GIT_DIFF_IGNORE_WHITESPACE_EOL;
		else if (!strcmp(a, "-b") || !strcmp(a, "--ignore-space-change"))
			opts.flags |= GIT_DIFF_IGNORE_WHITESPACE_CHANGE;
		else if (!strcmp(a, "-w") || !strcmp(a, "--ignore-all-space"))
			opts.flags |= GIT_DIFF_IGNORE_WHITESPACE;
173 174 175 176
		else if (!strcmp(a, "--ignored"))
			opts.flags |= GIT_DIFF_INCLUDE_IGNORED;
		else if (!strcmp(a, "--untracked"))
			opts.flags |= GIT_DIFF_INCLUDE_UNTRACKED;
Russell Belfer committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190
		else if (check_uint16_param(a, "-M", &findopts.rename_threshold) ||
				 check_uint16_param(a, "--find-renames",
					&findopts.rename_threshold))
			findopts.flags |= GIT_DIFF_FIND_RENAMES;
		else if (check_uint16_param(a, "-C", &findopts.copy_threshold) ||
				 check_uint16_param(a, "--find-copies",
					&findopts.copy_threshold))
			findopts.flags |= GIT_DIFF_FIND_COPIES;
		else if (!strcmp(a, "--find-copies-harder"))
			findopts.flags |= GIT_DIFF_FIND_COPIES_FROM_UNMODIFIED;
		else if (!strncmp(a, "-B", 2) || !strncmp(a, "--break-rewrites", 16)) {
			/* TODO: parse thresholds */
			findopts.flags |= GIT_DIFF_FIND_REWRITES;
		}
191 192 193 194
		else if (!check_uint16_param(a, "-U", &opts.context_lines) &&
			!check_uint16_param(a, "--unified=", &opts.context_lines) &&
			!check_uint16_param(a, "--inter-hunk-context=",
				&opts.interhunk_lines) &&
195
			!check_str_param(a, "--src-prefix=", &opts.old_prefix) &&
196 197
			!check_str_param(a, "--dst-prefix=", &opts.new_prefix) &&
			!check_str_param(a, "--git-dir=", &dir))
198
			usage("Unknown arg", a);
199 200
	}

201 202
	/* open repo */

203
	check(git_repository_open_ext(&repo, dir, 0, NULL),
204 205
		"Could not open repository");

206 207
	if (treeish1)
		check(resolve_to_tree(repo, treeish1, &t1), "Looking up first tree");
208 209
	if (treeish2)
		check(resolve_to_tree(repo, treeish2, &t2), "Looking up second tree");
210

211 212 213 214 215 216 217
	/* <sha1> <sha2> */
	/* <sha1> --cached */
	/* <sha1> */
	/* --cached */
	/* nothing */

	if (t1 && t2)
218
		check(git_diff_tree_to_tree(&diff, repo, t1, t2, &opts), "Diff");
219
	else if (t1 && cached)
220
		check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff");
221
	else if (t1) {
222
		git_diff *diff2;
223 224
		check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff");
		check(git_diff_index_to_workdir(&diff2, repo, NULL, &opts), "Diff");
225
		check(git_diff_merge(diff, diff2), "Merge diffs");
226
		git_diff_free(diff2);
227 228 229
	}
	else if (cached) {
		check(resolve_to_tree(repo, "HEAD", &t1), "looking up HEAD");
230
		check(git_diff_tree_to_index(&diff, repo, t1, NULL, &opts), "Diff");
231
	}
232
	else
233
		check(git_diff_index_to_workdir(&diff, repo, NULL, &opts), "Diff");
234

Russell Belfer committed
235 236 237 238
	if ((findopts.flags & GIT_DIFF_FIND_ALL) != 0)
		check(git_diff_find_similar(diff, &findopts),
			"finding renames and copies ");

239 240
	if (color >= 0)
		fputs(colors[0], stdout);
241

Russell Belfer committed
242
	check(git_diff_print(diff, format, printer, &color), "Displaying diff");
243

244 245
	if (color >= 0)
		fputs(colors[0], stdout);
246

247
	git_diff_free(diff);
248 249
	git_tree_free(t1);
	git_tree_free(t2);
250 251
	git_repository_free(repo);

252 253
	git_threads_shutdown();

254 255 256
	return 0;
}