diff.c 11.7 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7

8
#include "diff.h"
9 10

#include "git2/version.h"
11 12 13
#include "diff_generate.h"
#include "patch.h"
#include "commit.h"
14
#include "index.h"
15

16 17 18 19 20 21
struct patch_id_args {
	git_hash_ctx ctx;
	git_oid result;
	int first_file;
};

22 23 24 25 26 27 28 29 30 31 32 33 34
GIT_INLINE(const char *) diff_delta__path(const git_diff_delta *delta)
{
	const char *str = delta->old_file.path;

	if (!str ||
		delta->status == GIT_DELTA_ADDED ||
		delta->status == GIT_DELTA_RENAMED ||
		delta->status == GIT_DELTA_COPIED)
		str = delta->new_file.path;

	return str;
}

35 36 37 38 39
const char *git_diff_delta__path(const git_diff_delta *delta)
{
	return diff_delta__path(delta);
}

40
int git_diff_delta__cmp(const void *a, const void *b)
41 42
{
	const git_diff_delta *da = a, *db = b;
43
	int val = strcmp(diff_delta__path(da), diff_delta__path(db));
44 45 46
	return val ? val : ((int)da->status - (int)db->status);
}

47 48 49 50 51 52 53
int git_diff_delta__casecmp(const void *a, const void *b)
{
	const git_diff_delta *da = a, *db = b;
	int val = strcasecmp(diff_delta__path(da), diff_delta__path(db));
	return val ? val : ((int)da->status - (int)db->status);
}

54
int git_diff__entry_cmp(const void *a, const void *b)
55 56 57 58 59 60 61
{
	const git_index_entry *entry_a = a;
	const git_index_entry *entry_b = b;

	return strcmp(entry_a->path, entry_b->path);
}

62
int git_diff__entry_icmp(const void *a, const void *b)
63 64 65 66 67 68 69
{
	const git_index_entry *entry_a = a;
	const git_index_entry *entry_b = b;

	return strcasecmp(entry_a->path, entry_b->path);
}

70
void git_diff_free(git_diff *diff)
Russell Belfer committed
71 72 73 74
{
	if (!diff)
		return;

75
	GIT_REFCOUNT_DEC(diff, diff->free_fn);
Russell Belfer committed
76 77
}

78
void git_diff_addref(git_diff *diff)
79 80 81 82
{
	GIT_REFCOUNT_INC(diff);
}

Russell Belfer committed
83
size_t git_diff_num_deltas(const git_diff *diff)
84 85
{
	assert(diff);
Russell Belfer committed
86
	return diff->deltas.length;
87 88
}

Russell Belfer committed
89
size_t git_diff_num_deltas_of_type(const git_diff *diff, git_delta_t type)
90 91
{
	size_t i, count = 0;
Russell Belfer committed
92
	const git_diff_delta *delta;
93 94 95 96 97 98 99 100 101 102

	assert(diff);

	git_vector_foreach(&diff->deltas, i, delta) {
		count += (delta->status == type);
	}

	return count;
}

Russell Belfer committed
103 104 105 106 107 108
const git_diff_delta *git_diff_get_delta(const git_diff *diff, size_t idx)
{
	assert(diff);
	return git_vector_get(&diff->deltas, idx);
}

109
int git_diff_is_sorted_icase(const git_diff *diff)
110
{
Russell Belfer committed
111
	return (diff->opts.flags & GIT_DIFF_IGNORE_CASE) != 0;
112 113
}

114 115
int git_diff_get_perfdata(git_diff_perfdata *out, const git_diff *diff)
{
116
	assert(out);
117
	GIT_ERROR_CHECK_VERSION(out, GIT_DIFF_PERFDATA_VERSION, "git_diff_perfdata");
118 119 120 121 122
	out->stat_calls = diff->perf.stat_calls;
	out->oid_calculations = diff->perf.oid_calculations;
	return 0;
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
int git_diff_foreach(
	git_diff *diff,
	git_diff_file_cb file_cb,
	git_diff_binary_cb binary_cb,
	git_diff_hunk_cb hunk_cb,
	git_diff_line_cb data_cb,
	void *payload)
{
	int error = 0;
	git_diff_delta *delta;
	size_t idx;

	assert(diff);

	git_vector_foreach(&diff->deltas, idx, delta) {
		git_patch *patch;

		/* check flags against patch status */
		if (git_diff_delta__should_skip(&diff->opts, delta))
			continue;

		if ((error = git_patch_from_diff(&patch, diff, idx)) != 0)
			break;

		error = git_patch__invoke_callbacks(patch, file_cb, binary_cb,
						    hunk_cb, data_cb, payload);
		git_patch_free(patch);

		if (error)
			break;
	}

	return error;
}

158 159 160 161 162
int git_diff_format_email__append_header_tobuf(
	git_buf *out,
	const git_oid *id,
	const git_signature *author,
	const char *summary,
163
	const char *body,
164 165 166 167 168 169 170 171 172 173 174
	size_t patch_no,
	size_t total_patches,
	bool exclude_patchno_marker)
{
	char idstr[GIT_OID_HEXSZ + 1];
	char date_str[GIT_DATE_RFC2822_SZ];
	int error = 0;

	git_oid_fmt(idstr, id);
	idstr[GIT_OID_HEXSZ] = '\0';

175 176
	if ((error = git__date_rfc2822_fmt(date_str, sizeof(date_str),
		&author->when)) < 0)
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
		return error;

	error = git_buf_printf(out,
				"From %s Mon Sep 17 00:00:00 2001\n" \
				"From: %s <%s>\n" \
				"Date: %s\n" \
				"Subject: ",
				idstr,
				author->name, author->email,
				date_str);

	if (error < 0)
		return error;

	if (!exclude_patchno_marker) {
		if (total_patches == 1) {
			error = git_buf_puts(out, "[PATCH] ");
		} else {
195 196
			error = git_buf_printf(out, "[PATCH %"PRIuZ"/%"PRIuZ"] ",
				patch_no, total_patches);
197 198 199 200 201 202 203 204
		}

		if (error < 0)
			return error;
	}

	error = git_buf_printf(out, "%s\n\n", summary);

205 206 207 208 209 210 211
	if (body) {
		git_buf_puts(out, body);

		if (out->ptr[out->size - 1] != '\n')
			git_buf_putc(out, '\n');
	}

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 243 244
	return error;
}

int git_diff_format_email__append_patches_tobuf(
	git_buf *out,
	git_diff *diff)
{
	size_t i, deltas;
	int error = 0;

	deltas = git_diff_num_deltas(diff);

	for (i = 0; i < deltas; ++i) {
		git_patch *patch = NULL;

		if ((error = git_patch_from_diff(&patch, diff, i)) >= 0)
			error = git_patch_to_buf(out, patch);

		git_patch_free(patch);

		if (error < 0)
			break;
	}

	return error;
}

int git_diff_format_email(
	git_buf *out,
	git_diff *diff,
	const git_diff_format_email_options *opts)
{
	git_diff_stats *stats = NULL;
245
	char *summary = NULL, *loc = NULL;
246 247
	bool ignore_marker;
	unsigned int format_flags = 0;
248
	size_t allocsize;
249 250 251 252 253
	int error;

	assert(out && diff && opts);
	assert(opts->summary && opts->id && opts->author);

254
	GIT_ERROR_CHECK_VERSION(opts,
255 256 257 258 259
		GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION,
		"git_format_email_options");

	ignore_marker = (opts->flags &
		GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) != 0;
260

261
	if (!ignore_marker) {
262
		if (opts->patch_no > opts->total_patches) {
263
			git_error_set(GIT_ERROR_INVALID,
264 265
				"patch %"PRIuZ" out of range. max %"PRIuZ,
				opts->patch_no, opts->total_patches);
266 267 268 269
			return -1;
		}

		if (opts->patch_no == 0) {
270
			git_error_set(GIT_ERROR_INVALID,
271
				"invalid patch no %"PRIuZ". should be >0", opts->patch_no);
272 273 274 275
			return -1;
		}
	}

276 277 278 279 280 281 282
	/* the summary we receive may not be clean.
	 * it could potentially contain new line characters
	 * or not be set, sanitize, */
	if ((loc = strpbrk(opts->summary, "\r\n")) != NULL) {
		size_t offset = 0;

		if ((offset = (loc - opts->summary)) == 0) {
283
			git_error_set(GIT_ERROR_INVALID, "summary is empty");
284
			error = -1;
285
			goto on_error;
286 287
		}

288
		GIT_ERROR_CHECK_ALLOC_ADD(&allocsize, offset, 1);
289
		summary = git__calloc(allocsize, sizeof(char));
290
		GIT_ERROR_CHECK_ALLOC(summary);
291

292 293 294
		strncpy(summary, opts->summary, offset);
	}

295
	error = git_diff_format_email__append_header_tobuf(out,
296 297
		opts->id, opts->author, summary == NULL ? opts->summary : summary,
		opts->body, opts->patch_no, opts->total_patches, ignore_marker);
298 299 300 301 302 303 304 305

	if (error < 0)
		goto on_error;

	format_flags = GIT_DIFF_STATS_FULL | GIT_DIFF_STATS_INCLUDE_SUMMARY;

	if ((error = git_buf_puts(out, "---\n")) < 0 ||
		(error = git_diff_get_stats(&stats, diff)) < 0 ||
306 307
		(error = git_diff_stats_to_buf(out, stats, format_flags, 0)) < 0 ||
		(error = git_buf_putc(out, '\n')) < 0 ||
308 309 310 311 312 313
		(error = git_diff_format_email__append_patches_tobuf(out, diff)) < 0)
			goto on_error;

	error = git_buf_puts(out, "--\nlibgit2 " LIBGIT2_VERSION "\n\n");

on_error:
314
	git__free(summary);
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	git_diff_stats_free(stats);

	return error;
}

int git_diff_commit_as_email(
	git_buf *out,
	git_repository *repo,
	git_commit *commit,
	size_t patch_no,
	size_t total_patches,
	git_diff_format_email_flags_t flags,
	const git_diff_options *diff_opts)
{
	git_diff *diff = NULL;
330 331
	git_diff_format_email_options opts =
		GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT;
332 333 334 335 336 337 338 339 340
	int error;

	assert (out && repo && commit);

	opts.flags = flags;
	opts.patch_no = patch_no;
	opts.total_patches = total_patches;
	opts.id = git_commit_id(commit);
	opts.summary = git_commit_summary(commit);
341
	opts.body = git_commit_body(commit);
342 343 344 345 346 347 348 349 350 351 352
	opts.author = git_commit_author(commit);

	if ((error = git_diff__commit(&diff, repo, commit, diff_opts)) < 0)
		return error;

	error = git_diff_format_email(out, diff, &opts);

	git_diff_free(diff);
	return error;
}

353
int git_diff_options_init(git_diff_options *opts, unsigned int version)
354
{
355 356
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_diff_options, GIT_DIFF_OPTIONS_INIT);
357
	return 0;
358 359
}

360 361 362 363 364 365
int git_diff_init_options(git_diff_options *opts, unsigned int version)
{
	return git_diff_options_init(opts, version);
}

int git_diff_find_options_init(
366
	git_diff_find_options *opts, unsigned int version)
367
{
368 369
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_diff_find_options, GIT_DIFF_FIND_OPTIONS_INIT);
370
	return 0;
371
}
372

373 374 375 376 377 378 379
int git_diff_find_init_options(
	git_diff_find_options *opts, unsigned int version)
{
	return git_diff_find_options_init(opts, version);
}

int git_diff_format_email_options_init(
380
	git_diff_format_email_options *opts, unsigned int version)
381
{
382 383 384
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_diff_format_email_options,
		GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT);
385
	return 0;
386
}
387

388 389 390 391 392 393
int git_diff_format_email_init_options(
	git_diff_format_email_options *opts, unsigned int version)
{
	return git_diff_format_email_options_init(opts, version);
}

394 395 396 397 398 399 400 401 402 403 404 405
static int flush_hunk(git_oid *result, git_hash_ctx *ctx)
{
	git_oid hash;
	unsigned short carry = 0;
	int error, i;

	if ((error = git_hash_final(&hash, ctx)) < 0 ||
	    (error = git_hash_init(ctx)) < 0)
		return error;

	for (i = 0; i < GIT_OID_RAWSZ; i++) {
		carry += result->id[i] + hash.id[i];
406
		result->id[i] = (unsigned char)carry;
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
		carry >>= 8;
	}

	return 0;
}

static void strip_spaces(git_buf *buf)
{
	char *src = buf->ptr, *dst = buf->ptr;
	char c;
	size_t len = 0;

	while ((c = *src++) != '\0') {
		if (!git__isspace(c)) {
			*dst++ = c;
			len++;
		}
	}

	git_buf_truncate(buf, len);
}

static int file_cb(
	const git_diff_delta *delta,
	float progress,
	void *payload)
{
	struct patch_id_args *args = (struct patch_id_args *) payload;
	git_buf buf = GIT_BUF_INIT;
	int error;

	GIT_UNUSED(progress);

	if (!args->first_file &&
	    (error = flush_hunk(&args->result, &args->ctx)) < 0)
		goto out;
	args->first_file = 0;

	if ((error = git_buf_printf(&buf,
				    "diff--gita/%sb/%s---a/%s+++b/%s",
				    delta->old_file.path,
				    delta->new_file.path,
				    delta->old_file.path,
				    delta->new_file.path)) < 0)
		goto out;

	strip_spaces(&buf);

	if ((error = git_hash_update(&args->ctx, buf.ptr, buf.size)) < 0)
		goto out;

out:
459
	git_buf_dispose(&buf);
460 461 462
	return error;
}

463
static int patchid_line_cb(
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
	const git_diff_delta *delta,
	const git_diff_hunk *hunk,
	const git_diff_line *line,
	void *payload)
{
	struct patch_id_args *args = (struct patch_id_args *) payload;
	git_buf buf = GIT_BUF_INIT;
	int error;

	GIT_UNUSED(delta);
	GIT_UNUSED(hunk);

	switch (line->origin) {
	    case GIT_DIFF_LINE_ADDITION:
		git_buf_putc(&buf, '+');
		break;
	    case GIT_DIFF_LINE_DELETION:
		git_buf_putc(&buf, '-');
		break;
	    case GIT_DIFF_LINE_CONTEXT:
		break;
485 486 487 488 489 490 491 492
	    case GIT_DIFF_LINE_CONTEXT_EOFNL:
	    case GIT_DIFF_LINE_ADD_EOFNL:
	    case GIT_DIFF_LINE_DEL_EOFNL:
		/*
		 * Ignore EOF without newlines for patch IDs as whitespace is
		 * not supposed to be significant.
		 */
		return 0;
493
	    default:
494
		git_error_set(GIT_ERROR_PATCH, "invalid line origin for patch");
495 496 497 498 499 500 501 502 503 504
		return -1;
	}

	git_buf_put(&buf, line->content, line->content_len);
	strip_spaces(&buf);

	if ((error = git_hash_update(&args->ctx, buf.ptr, buf.size)) < 0)
		goto out;

out:
505
	git_buf_dispose(&buf);
506 507 508
	return error;
}

509
int git_diff_patchid_options_init(git_diff_patchid_options *opts, unsigned int version)
510 511 512 513 514 515 516 517 518 519 520
{
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_diff_patchid_options, GIT_DIFF_PATCHID_OPTIONS_INIT);
	return 0;
}

int git_diff_patchid(git_oid *out, git_diff *diff, git_diff_patchid_options *opts)
{
	struct patch_id_args args;
	int error;

521
	GIT_ERROR_CHECK_VERSION(
522 523 524 525 526 527 528
		opts, GIT_DIFF_PATCHID_OPTIONS_VERSION, "git_diff_patchid_options");

	memset(&args, 0, sizeof(args));
	args.first_file = 1;
	if ((error = git_hash_ctx_init(&args.ctx)) < 0)
		goto out;

529
	if ((error = git_diff_foreach(diff, file_cb, NULL, NULL, patchid_line_cb, &args)) < 0)
530 531 532 533 534 535 536 537
		goto out;

	if ((error = (flush_hunk(&args.result, &args.ctx))) < 0)
		goto out;

	git_oid_cpy(out, &args.result);

out:
538
	git_hash_ctx_cleanup(&args.ctx);
539 540
	return error;
}