diff_xdiff.c 6.57 KB
Newer Older
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
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
#include "common.h"
#include "diff.h"
#include "diff_driver.h"
#include "diff_patch.h"
#include "diff_xdiff.h"

static int git_xdiff_scan_int(const char **str, int *value)
{
	const char *scan = *str;
	int v = 0, digits = 0;
	/* find next digit */
	for (scan = *str; *scan && !git__isdigit(*scan); scan++);
	/* parse next number */
	for (; git__isdigit(*scan); scan++, digits++)
		v = (v * 10) + (*scan - '0');
	*str = scan;
	*value = v;
	return (digits > 0) ? 0 : -1;
}

27
static int git_xdiff_parse_hunk(git_diff_hunk *hunk, const char *header)
28 29 30
{
	/* expect something of the form "@@ -%d[,%d] +%d[,%d] @@" */
	if (*header != '@')
31
		goto fail;
32
	if (git_xdiff_scan_int(&header, &hunk->old_start) < 0)
33
		goto fail;
34
	if (*header == ',') {
35
		if (git_xdiff_scan_int(&header, &hunk->old_lines) < 0)
36
			goto fail;
37
	} else
38 39
		hunk->old_lines = 1;
	if (git_xdiff_scan_int(&header, &hunk->new_start) < 0)
40
		goto fail;
41
	if (*header == ',') {
42
		if (git_xdiff_scan_int(&header, &hunk->new_lines) < 0)
43
			goto fail;
44
	} else
45 46
		hunk->new_lines = 1;
	if (hunk->old_start < 0 || hunk->new_start < 0)
47
		goto fail;
48 49

	return 0;
50 51 52 53

fail:
	giterr_set(GITERR_INVALID, "Malformed hunk header from xdiff");
	return -1;
54 55 56 57
}

typedef struct {
	git_xdiff_output *xo;
58 59
	git_patch *patch;
	git_diff_hunk hunk;
Russell Belfer committed
60
	int old_lineno, new_lineno;
61
	mmfile_t xd_old_data, xd_new_data;
62 63
} git_xdiff_info;

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
static int diff_update_lines(
	git_xdiff_info *info,
	git_diff_line *line,
	const char *content,
	size_t content_len)
{
	const char *scan = content, *scan_end = content + content_len;

	for (line->num_lines = 0; scan < scan_end; ++scan)
		if (*scan == '\n')
			++line->num_lines;

	line->content     = content;
	line->content_len = content_len;

	/* expect " "/"-"/"+", then data */
	switch (line->origin) {
	case GIT_DIFF_LINE_ADDITION:
	case GIT_DIFF_LINE_DEL_EOFNL:
		line->old_lineno = -1;
		line->new_lineno = info->new_lineno;
Russell Belfer committed
85
		info->new_lineno += (int)line->num_lines;
86 87 88 89 90
		break;
	case GIT_DIFF_LINE_DELETION:
	case GIT_DIFF_LINE_ADD_EOFNL:
		line->old_lineno = info->old_lineno;
		line->new_lineno = -1;
Russell Belfer committed
91
		info->old_lineno += (int)line->num_lines;
92 93 94 95 96
		break;
	case GIT_DIFF_LINE_CONTEXT:
	case GIT_DIFF_LINE_CONTEXT_EOFNL:
		line->old_lineno = info->old_lineno;
		line->new_lineno = info->new_lineno;
Russell Belfer committed
97 98
		info->old_lineno += (int)line->num_lines;
		info->new_lineno += (int)line->num_lines;
99 100 101 102 103 104 105 106 107 108
		break;
	default:
		giterr_set(GITERR_INVALID, "Unknown diff line origin %02x",
			(unsigned int)line->origin);
		return -1;
	}

	return 0;
}

109 110 111
static int git_xdiff_cb(void *priv, mmbuffer_t *bufs, int len)
{
	git_xdiff_info *info = priv;
112
	git_patch *patch = info->patch;
Russell Belfer committed
113
	const git_diff_delta *delta = git_patch_get_delta(patch);
114
	git_diff_output *output = &info->xo->output;
115
	git_diff_line line;
116 117

	if (len == 1) {
118
		output->error = git_xdiff_parse_hunk(&info->hunk, bufs[0].ptr);
119 120 121
		if (output->error < 0)
			return output->error;

122 123 124 125 126 127
		info->hunk.header_len = bufs[0].size;
		if (info->hunk.header_len >= sizeof(info->hunk.header))
			info->hunk.header_len = sizeof(info->hunk.header) - 1;
		memcpy(info->hunk.header, bufs[0].ptr, info->hunk.header_len);
		info->hunk.header[info->hunk.header_len] = '\0';

128
		if (output->hunk_cb != NULL &&
129 130 131
			(output->error = output->hunk_cb(
				delta, &info->hunk, output->payload)))
			return output->error;
132 133 134

		info->old_lineno = info->hunk.old_start;
		info->new_lineno = info->hunk.new_start;
135 136 137 138
	}

	if (len == 2 || len == 3) {
		/* expect " "/"-"/"+", then data */
139
		line.origin =
140 141 142 143
			(*bufs[0].ptr == '+') ? GIT_DIFF_LINE_ADDITION :
			(*bufs[0].ptr == '-') ? GIT_DIFF_LINE_DELETION :
			GIT_DIFF_LINE_CONTEXT;

144 145 146 147 148 149 150
		if (line.origin == GIT_DIFF_LINE_ADDITION)
			line.content_offset = bufs[1].ptr - info->xd_new_data.ptr;
		else if (line.origin == GIT_DIFF_LINE_DELETION)
			line.content_offset = bufs[1].ptr - info->xd_old_data.ptr;
		else
			line.content_offset = -1;

151 152 153
		output->error = diff_update_lines(
			info, &line, bufs[1].ptr, bufs[1].size);

154 155 156
		if (!output->error && output->data_cb != NULL)
			output->error = output->data_cb(
				delta, &info->hunk, &line, output->payload);
157 158 159 160 161 162 163 164
	}

	if (len == 3 && !output->error) {
		/* If we have a '+' and a third buf, then we have added a line
		 * without a newline and the old code had one, so DEL_EOFNL.
		 * If we have a '-' and a third buf, then we have removed a line
		 * with out a newline but added a blank line, so ADD_EOFNL.
		 */
165
		line.origin =
166 167 168 169
			(*bufs[0].ptr == '+') ? GIT_DIFF_LINE_DEL_EOFNL :
			(*bufs[0].ptr == '-') ? GIT_DIFF_LINE_ADD_EOFNL :
			GIT_DIFF_LINE_CONTEXT_EOFNL;

170 171
		line.content_offset = -1;

172 173 174
		output->error = diff_update_lines(
			info, &line, bufs[2].ptr, bufs[2].size);

175 176 177
		if (!output->error && output->data_cb != NULL)
			output->error = output->data_cb(
				delta, &info->hunk, &line, output->payload);
178 179 180 181 182
	}

	return output->error;
}

183
static int git_xdiff(git_diff_output *output, git_patch *patch)
184 185 186
{
	git_xdiff_output *xo = (git_xdiff_output *)output;
	git_xdiff_info info;
187
	git_diff_find_context_payload findctxt;
188 189 190 191 192 193 194

	memset(&info, 0, sizeof(info));
	info.patch = patch;
	info.xo    = xo;

	xo->callback.priv = &info;

195
	git_diff_find_context_init(
196
		&xo->config.find_func, &findctxt, git_patch__driver(patch));
197
	xo->config.find_func_priv = &findctxt;
198 199 200 201 202 203

	if (xo->config.find_func != NULL)
		xo->config.flags |= XDL_EMIT_FUNCNAMES;
	else
		xo->config.flags &= ~XDL_EMIT_FUNCNAMES;

204 205 206
	/* TODO: check ofile.opts_flags to see if driver-specific per-file
	 * updates are needed to xo->params.flags
	 */
207

208 209
	git_patch__old_data(&info.xd_old_data.ptr, &info.xd_old_data.size, patch);
	git_patch__new_data(&info.xd_new_data.ptr, &info.xd_new_data.size, patch);
210

211
	xdl_diff(&info.xd_old_data, &info.xd_new_data,
212 213
		&xo->params, &xo->config, &xo->callback);

214 215
	git_diff_find_context_clear(&findctxt);

216 217 218 219 220
	return xo->output.error;
}

void git_xdiff_init(git_xdiff_output *xo, const git_diff_options *opts)
{
Russell Belfer committed
221
	uint32_t flags = opts ? opts->flags : 0;
222 223 224 225 226 227 228 229 230 231 232 233 234

	xo->output.diff_cb = git_xdiff;

	xo->config.ctxlen = opts ? opts->context_lines : 3;
	xo->config.interhunkctxlen = opts ? opts->interhunk_lines : 0;

	if (flags & GIT_DIFF_IGNORE_WHITESPACE)
		xo->params.flags |= XDF_WHITESPACE_FLAGS;
	if (flags & GIT_DIFF_IGNORE_WHITESPACE_CHANGE)
		xo->params.flags |= XDF_IGNORE_WHITESPACE_CHANGE;
	if (flags & GIT_DIFF_IGNORE_WHITESPACE_EOL)
		xo->params.flags |= XDF_IGNORE_WHITESPACE_AT_EOL;

235 236 237 238 239
	if (flags & GIT_DIFF_PATIENCE)
		xo->params.flags |= XDF_PATIENCE_DIFF;
	if (flags & GIT_DIFF_MINIMAL)
		xo->params.flags |= XDF_NEED_MINIMAL;

240 241
	xo->callback.outf = git_xdiff_cb;
}