diff_patch.c 22.4 KB
Newer Older
1 2 3 4 5 6 7
/*
 * 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"
8
#include "git2/blob.h"
9 10 11 12 13
#include "diff.h"
#include "diff_file.h"
#include "diff_driver.h"
#include "diff_patch.h"
#include "diff_xdiff.h"
14
#include "fileops.h"
15

16
/* cached information about a hunk in a diff */
17
typedef struct diff_patch_hunk {
18
	git_diff_hunk hunk;
19 20
	size_t line_start;
	size_t line_count;
21
} diff_patch_hunk;
22

23
struct git_patch {
24
	git_refcount rc;
25
	git_diff *diff; /* for refcount purposes, maybe NULL for blob diffs */
26 27 28 29 30 31
	git_diff_delta *delta;
	size_t delta_index;
	git_diff_file_content ofile;
	git_diff_file_content nfile;
	uint32_t flags;
	git_array_t(diff_patch_hunk) hunks;
32
	git_array_t(git_diff_line)   lines;
33
	size_t content_size, context_size, header_size;
34 35 36 37 38 39 40 41 42 43 44 45
	git_pool flattened;
};

enum {
	GIT_DIFF_PATCH_ALLOCATED   = (1 << 0),
	GIT_DIFF_PATCH_INITIALIZED = (1 << 1),
	GIT_DIFF_PATCH_LOADED      = (1 << 2),
	GIT_DIFF_PATCH_DIFFABLE    = (1 << 3),
	GIT_DIFF_PATCH_DIFFED      = (1 << 4),
	GIT_DIFF_PATCH_FLATTENED   = (1 << 5),
};

46 47
static void diff_output_init(
	git_diff_output*, const git_diff_options*,
48
	git_diff_file_cb, git_diff_hunk_cb, git_diff_line_cb, void*);
49

50
static void diff_output_to_patch(git_diff_output *, git_patch *);
51

52
static void diff_patch_update_binary(git_patch *patch)
53 54 55 56
{
	if ((patch->delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0)
		return;

57 58
	if ((patch->ofile.file->flags & GIT_DIFF_FLAG_BINARY) != 0 ||
		(patch->nfile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
59 60
		patch->delta->flags |= GIT_DIFF_FLAG_BINARY;

61 62
	else if ((patch->ofile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0 &&
			 (patch->nfile.file->flags & DIFF_FLAGS_NOT_BINARY) != 0)
63 64 65
		patch->delta->flags |= GIT_DIFF_FLAG_NOT_BINARY;
}

66
static void diff_patch_init_common(git_patch *patch)
67 68 69 70
{
	diff_patch_update_binary(patch);

	if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
71
		patch->flags |= GIT_DIFF_PATCH_LOADED; /* LOADED but not DIFFABLE */
72 73 74 75

	patch->flags |= GIT_DIFF_PATCH_INITIALIZED;

	if (patch->diff)
76
		git_diff_addref(patch->diff);
77 78 79
}

static int diff_patch_init_from_diff(
80
	git_patch *patch, git_diff *diff, size_t delta_index)
81 82 83 84 85 86 87 88
{
	int error = 0;

	memset(patch, 0, sizeof(*patch));
	patch->diff  = diff;
	patch->delta = git_vector_get(&diff->deltas, delta_index);
	patch->delta_index = delta_index;

89
	if ((error = git_diff_file_content__init_from_diff(
90
			&patch->ofile, diff, delta_index, true)) < 0 ||
91
		(error = git_diff_file_content__init_from_diff(
92 93 94 95 96 97 98 99 100
			&patch->nfile, diff, delta_index, false)) < 0)
		return error;

	diff_patch_init_common(patch);

	return 0;
}

static int diff_patch_alloc_from_diff(
Russell Belfer committed
101
	git_patch **out, git_diff *diff, size_t delta_index)
102 103
{
	int error;
104
	git_patch *patch = git__calloc(1, sizeof(git_patch));
105 106 107 108 109 110 111 112 113 114 115 116 117 118
	GITERR_CHECK_ALLOC(patch);

	if (!(error = diff_patch_init_from_diff(patch, diff, delta_index))) {
		patch->flags |= GIT_DIFF_PATCH_ALLOCATED;
		GIT_REFCOUNT_INC(patch);
	} else {
		git__free(patch);
		patch = NULL;
	}

	*out = patch;
	return error;
}

119
static int diff_patch_load(git_patch *patch, git_diff_output *output)
120 121 122 123 124 125 126 127 128 129
{
	int error = 0;
	bool incomplete_data;

	if ((patch->flags & GIT_DIFF_PATCH_LOADED) != 0)
		return 0;

	/* if no hunk and data callbacks and user doesn't care if data looks
	 * binary, then there is no need to actually load the data
	 */
130
	if ((patch->ofile.opts_flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0 &&
131 132 133 134
		output && !output->hunk_cb && !output->data_cb)
		return 0;

	incomplete_data =
135
		(((patch->ofile.flags & GIT_DIFF_FLAG__NO_DATA) != 0 ||
136
		  (patch->ofile.file->flags & GIT_DIFF_FLAG_VALID_ID) != 0) &&
137
		 ((patch->nfile.flags & GIT_DIFF_FLAG__NO_DATA) != 0 ||
138
		  (patch->nfile.file->flags & GIT_DIFF_FLAG_VALID_ID) != 0));
139 140 141 142 143

	/* always try to load workdir content first because filtering may
	 * need 2x data size and this minimizes peak memory footprint
	 */
	if (patch->ofile.src == GIT_ITERATOR_TYPE_WORKDIR) {
144
		if ((error = git_diff_file_content__load(&patch->ofile)) < 0 ||
145
			(patch->ofile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
146 147 148
			goto cleanup;
	}
	if (patch->nfile.src == GIT_ITERATOR_TYPE_WORKDIR) {
149
		if ((error = git_diff_file_content__load(&patch->nfile)) < 0 ||
150
			(patch->nfile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
151 152 153 154 155
			goto cleanup;
	}

	/* once workdir has been tried, load other data as needed */
	if (patch->ofile.src != GIT_ITERATOR_TYPE_WORKDIR) {
156
		if ((error = git_diff_file_content__load(&patch->ofile)) < 0 ||
157
			(patch->ofile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
158 159 160
			goto cleanup;
	}
	if (patch->nfile.src != GIT_ITERATOR_TYPE_WORKDIR) {
161
		if ((error = git_diff_file_content__load(&patch->nfile)) < 0 ||
162
			(patch->nfile.file->flags & GIT_DIFF_FLAG_BINARY) != 0)
163 164 165
			goto cleanup;
	}

166 167 168
	/* if previously missing an oid, and now that we have it the two sides
	 * are the same (and not submodules), update MODIFIED -> UNMODIFIED
	 */
169
	if (incomplete_data &&
170
		patch->ofile.file->mode == patch->nfile.file->mode &&
171
		patch->ofile.file->mode != GIT_FILEMODE_COMMIT &&
172
		git_oid_equal(&patch->ofile.file->id, &patch->nfile.file->id) &&
173
		patch->delta->status == GIT_DELTA_MODIFIED) /* not RENAMED/COPIED! */
174 175 176 177 178 179 180 181 182 183 184 185 186
		patch->delta->status = GIT_DELTA_UNMODIFIED;

cleanup:
	diff_patch_update_binary(patch);

	if (!error) {
		/* patch is diffable only for non-binary, modified files where
		 * at least one side has data and the data actually changed
		 */
		if ((patch->delta->flags & GIT_DIFF_FLAG_BINARY) == 0 &&
			patch->delta->status != GIT_DELTA_UNMODIFIED &&
			(patch->ofile.map.len || patch->nfile.map.len) &&
			(patch->ofile.map.len != patch->nfile.map.len ||
187
			 !git_oid_equal(&patch->ofile.file->id, &patch->nfile.file->id)))
188 189 190 191 192 193 194 195
			patch->flags |= GIT_DIFF_PATCH_DIFFABLE;

		patch->flags |= GIT_DIFF_PATCH_LOADED;
	}

	return error;
}

196
static int diff_patch_invoke_file_callback(
197
	git_patch *patch, git_diff_output *output)
198
{
199
	float progress = patch->diff ?
200 201
		((float)patch->delta_index / patch->diff->deltas.length) : 1.0f;

202 203
	if (!output->file_cb)
		return 0;
204

205
	return giterr_set_after_callback_function(
206 207
		output->file_cb(patch->delta, progress, output->payload),
		"git_patch");
208 209
}

210
static int diff_patch_generate(git_patch *patch, git_diff_output *output)
211 212 213 214 215 216
{
	int error = 0;

	if ((patch->flags & GIT_DIFF_PATCH_DIFFED) != 0)
		return 0;

217 218 219 220
	/* if we are not looking at the hunks and lines, don't do the diff */
	if (!output->hunk_cb && !output->data_cb)
		return 0;

221 222 223 224 225 226 227 228
	if ((patch->flags & GIT_DIFF_PATCH_LOADED) == 0 &&
		(error = diff_patch_load(patch, output)) < 0)
		return error;

	if ((patch->flags & GIT_DIFF_PATCH_DIFFABLE) == 0)
		return 0;

	if (output->diff_cb != NULL &&
229
		(error = output->diff_cb(output, patch)) < 0)
230 231 232 233 234
		patch->flags |= GIT_DIFF_PATCH_DIFFED;

	return error;
}

235
static void diff_patch_free(git_patch *patch)
236
{
237 238
	git_diff_file_content__clear(&patch->ofile);
	git_diff_file_content__clear(&patch->nfile);
239 240 241 242

	git_array_clear(patch->lines);
	git_array_clear(patch->hunks);

243
	git_diff_free(patch->diff); /* decrements refcount */
244 245 246 247 248 249 250 251
	patch->diff = NULL;

	git_pool_clear(&patch->flattened);

	if (patch->flags & GIT_DIFF_PATCH_ALLOCATED)
		git__free(patch);
}

252
static int diff_required(git_diff *diff, const char *action)
253 254 255 256 257 258 259 260
{
	if (diff)
		return 0;
	giterr_set(GITERR_INVALID, "Must provide valid diff to %s", action);
	return -1;
}

int git_diff_foreach(
261
	git_diff *diff,
262 263
	git_diff_file_cb file_cb,
	git_diff_hunk_cb hunk_cb,
264
	git_diff_line_cb data_cb,
265 266 267 268 269
	void *payload)
{
	int error = 0;
	git_xdiff_output xo;
	size_t idx;
270
	git_patch patch;
271

272 273
	if ((error = diff_required(diff, "git_diff_foreach")) < 0)
		return error;
274

275
	memset(&xo, 0, sizeof(xo));
276
	memset(&patch, 0, sizeof(patch));
Russell Belfer committed
277 278
	diff_output_init(
		&xo.output, &diff->opts, file_cb, hunk_cb, data_cb, payload);
279 280 281
	git_xdiff_init(&xo, &diff->opts);

	git_vector_foreach(&diff->deltas, idx, patch.delta) {
282

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

287 288
		if ((error = diff_patch_init_from_diff(&patch, diff, idx)) < 0)
			break;
289

290 291
		if (!(error = diff_patch_invoke_file_callback(&patch, &xo.output)))
			error = diff_patch_generate(&patch, &xo.output);
292

293
		git_patch_free(&patch);
294

295
		if (error)
296 297 298 299 300 301 302
			break;
	}

	return error;
}

typedef struct {
303
	git_patch patch;
304
	git_diff_delta delta;
305
	char paths[GIT_FLEX_ARRAY];
306
} diff_patch_with_delta;
307

308
static int diff_single_generate(diff_patch_with_delta *pd, git_xdiff_output *xo)
309 310
{
	int error = 0;
311
	git_patch *patch = &pd->patch;
312 313
	bool has_old = ((patch->ofile.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
	bool has_new = ((patch->nfile.flags & GIT_DIFF_FLAG__NO_DATA) == 0);
314

315
	pd->delta.status = has_new ?
316 317 318
		(has_old ? GIT_DELTA_MODIFIED : GIT_DELTA_ADDED) :
		(has_old ? GIT_DELTA_DELETED : GIT_DELTA_UNTRACKED);

319
	if (git_oid_equal(&patch->nfile.file->id, &patch->ofile.file->id))
320
		pd->delta.status = GIT_DELTA_UNMODIFIED;
321

322
	patch->delta = &pd->delta;
323 324 325

	diff_patch_init_common(patch);

326 327 328 329
	if (pd->delta.status == GIT_DELTA_UNMODIFIED &&
		!(patch->ofile.opts_flags & GIT_DIFF_INCLUDE_UNMODIFIED))
		return error;

330
	error = diff_patch_invoke_file_callback(patch, (git_diff_output *)xo);
331 332

	if (!error)
333
		error = diff_patch_generate(patch, (git_diff_output *)xo);
334 335 336 337

	return error;
}

338
static int diff_patch_from_sources(
339 340
	diff_patch_with_delta *pd,
	git_xdiff_output *xo,
341 342
	git_diff_file_content_src *oldsrc,
	git_diff_file_content_src *newsrc,
343
	const git_diff_options *opts)
344 345 346
{
	int error = 0;
	git_repository *repo =
347 348 349 350
		oldsrc->blob ? git_blob_owner(oldsrc->blob) :
		newsrc->blob ? git_blob_owner(newsrc->blob) : NULL;
	git_diff_file *lfile = &pd->delta.old_file, *rfile = &pd->delta.new_file;
	git_diff_file_content *ldata = &pd->patch.ofile, *rdata = &pd->patch.nfile;
351 352 353 354

	GITERR_CHECK_VERSION(opts, GIT_DIFF_OPTIONS_VERSION, "git_diff_options");

	if (opts && (opts->flags & GIT_DIFF_REVERSE) != 0) {
355 356
		void *tmp = lfile; lfile = rfile; rfile = tmp;
		tmp = ldata; ldata = rdata; rdata = tmp;
357 358
	}

359 360
	pd->patch.delta = &pd->delta;

361 362 363 364 365 366 367 368 369 370 371
	if (!oldsrc->as_path) {
		if (newsrc->as_path)
			oldsrc->as_path = newsrc->as_path;
		else
			oldsrc->as_path = newsrc->as_path = "file";
	}
	else if (!newsrc->as_path)
		newsrc->as_path = oldsrc->as_path;

	lfile->path = oldsrc->as_path;
	rfile->path = newsrc->as_path;
372

373 374 375 376
	if ((error = git_diff_file_content__init_from_src(
			ldata, repo, opts, oldsrc, lfile)) < 0 ||
		(error = git_diff_file_content__init_from_src(
			rdata, repo, opts, newsrc, rfile)) < 0)
377 378 379 380 381
		return error;

	return diff_single_generate(pd, xo);
}

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
static int diff_patch_with_delta_alloc(
	diff_patch_with_delta **out,
	const char **old_path,
	const char **new_path)
{
	diff_patch_with_delta *pd;
	size_t old_len = *old_path ? strlen(*old_path) : 0;
	size_t new_len = *new_path ? strlen(*new_path) : 0;

	*out = pd = git__calloc(1, sizeof(*pd) + old_len + new_len + 2);
	GITERR_CHECK_ALLOC(pd);

	pd->patch.flags = GIT_DIFF_PATCH_ALLOCATED;

	if (*old_path) {
		memcpy(&pd->paths[0], *old_path, old_len);
		*old_path = &pd->paths[0];
	} else if (*new_path)
		*old_path = &pd->paths[old_len + 1];

	if (*new_path) {
		memcpy(&pd->paths[old_len + 1], *new_path, new_len);
		*new_path = &pd->paths[old_len + 1];
	} else if (*old_path)
		*new_path = &pd->paths[0];

	return 0;
}

411 412 413
static int diff_from_sources(
	git_diff_file_content_src *oldsrc,
	git_diff_file_content_src *newsrc,
414 415 416
	const git_diff_options *opts,
	git_diff_file_cb file_cb,
	git_diff_hunk_cb hunk_cb,
417
	git_diff_line_cb data_cb,
418 419 420 421 422 423 424 425
	void *payload)
{
	int error = 0;
	diff_patch_with_delta pd;
	git_xdiff_output xo;

	memset(&xo, 0, sizeof(xo));
	diff_output_init(
Russell Belfer committed
426
		&xo.output, opts, file_cb, hunk_cb, data_cb, payload);
427
	git_xdiff_init(&xo, opts);
428

429
	memset(&pd, 0, sizeof(pd));
430 431

	error = diff_patch_from_sources(&pd, &xo, oldsrc, newsrc, opts);
432

433
	git_patch_free(&pd.patch);
434 435 436 437

	return error;
}

438
static int patch_from_sources(
439
	git_patch **out,
440 441
	git_diff_file_content_src *oldsrc,
	git_diff_file_content_src *newsrc,
442 443 444 445 446 447 448 449 450
	const git_diff_options *opts)
{
	int error = 0;
	diff_patch_with_delta *pd;
	git_xdiff_output xo;

	assert(out);
	*out = NULL;

451 452 453
	if ((error = diff_patch_with_delta_alloc(
			&pd, &oldsrc->as_path, &newsrc->as_path)) < 0)
		return error;
454 455

	memset(&xo, 0, sizeof(xo));
Russell Belfer committed
456
	diff_output_to_patch(&xo.output, &pd->patch);
457 458
	git_xdiff_init(&xo, opts);

459
	if (!(error = diff_patch_from_sources(pd, &xo, oldsrc, newsrc, opts)))
460
		*out = (git_patch *)pd;
461
	else
462
		git_patch_free((git_patch *)pd);
463 464 465 466

	return error;
}

467
int git_diff_blobs(
468
	const git_blob *old_blob,
469
	const char *old_path,
470 471 472 473 474 475 476
	const git_blob *new_blob,
	const char *new_path,
	const git_diff_options *opts,
	git_diff_file_cb file_cb,
	git_diff_hunk_cb hunk_cb,
	git_diff_line_cb data_cb,
	void *payload)
477
{
478 479 480 481 482 483 484
	git_diff_file_content_src osrc =
		GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
	git_diff_file_content_src nsrc =
		GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path);
	return diff_from_sources(
		&osrc, &nsrc, opts, file_cb, hunk_cb, data_cb, payload);
}
485

486 487 488 489 490 491 492 493 494 495 496 497 498
int git_patch_from_blobs(
	git_patch **out,
	const git_blob *old_blob,
	const char *old_path,
	const git_blob *new_blob,
	const char *new_path,
	const git_diff_options *opts)
{
	git_diff_file_content_src osrc =
		GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
	git_diff_file_content_src nsrc =
		GIT_DIFF_FILE_CONTENT_SRC__BLOB(new_blob, new_path);
	return patch_from_sources(out, &osrc, &nsrc, opts);
499 500 501 502
}

int git_diff_blob_to_buffer(
	const git_blob *old_blob,
503
	const char *old_path,
504 505
	const char *buf,
	size_t buflen,
506
	const char *buf_path,
507 508 509
	const git_diff_options *opts,
	git_diff_file_cb file_cb,
	git_diff_hunk_cb hunk_cb,
510
	git_diff_line_cb data_cb,
511 512
	void *payload)
{
513 514 515 516 517 518
	git_diff_file_content_src osrc =
		GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
	git_diff_file_content_src nsrc =
		GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path);
	return diff_from_sources(
		&osrc, &nsrc, opts, file_cb, hunk_cb, data_cb, payload);
519 520
}

521 522
int git_patch_from_blob_and_buffer(
	git_patch **out,
523
	const git_blob *old_blob,
524
	const char *old_path,
525 526
	const char *buf,
	size_t buflen,
527
	const char *buf_path,
528 529
	const git_diff_options *opts)
{
530 531 532 533 534 535
	git_diff_file_content_src osrc =
		GIT_DIFF_FILE_CONTENT_SRC__BLOB(old_blob, old_path);
	git_diff_file_content_src nsrc =
		GIT_DIFF_FILE_CONTENT_SRC__BUF(buf, buflen, buf_path);
	return patch_from_sources(out, &osrc, &nsrc, opts);
}
536

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
int git_diff_buffers(
	const void *old_buf,
	size_t old_len,
	const char *old_path,
	const void *new_buf,
	size_t new_len,
	const char *new_path,
	const git_diff_options *opts,
	git_diff_file_cb file_cb,
	git_diff_hunk_cb hunk_cb,
	git_diff_line_cb data_cb,
	void *payload)
{
	git_diff_file_content_src osrc =
		GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path);
	git_diff_file_content_src nsrc =
		GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path);
	return diff_from_sources(
		&osrc, &nsrc, opts, file_cb, hunk_cb, data_cb, payload);
}
557

558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
int git_patch_from_buffers(
	git_patch **out,
	const void *old_buf,
	size_t old_len,
	const char *old_path,
	const char *new_buf,
	size_t new_len,
	const char *new_path,
	const git_diff_options *opts)
{
	git_diff_file_content_src osrc =
		GIT_DIFF_FILE_CONTENT_SRC__BUF(old_buf, old_len, old_path);
	git_diff_file_content_src nsrc =
		GIT_DIFF_FILE_CONTENT_SRC__BUF(new_buf, new_len, new_path);
	return patch_from_sources(out, &osrc, &nsrc, opts);
573 574
}

575
int git_patch_from_diff(
Russell Belfer committed
576
	git_patch **patch_ptr, git_diff *diff, size_t idx)
577 578 579 580
{
	int error = 0;
	git_xdiff_output xo;
	git_diff_delta *delta = NULL;
581
	git_patch *patch = NULL;
582 583 584

	if (patch_ptr) *patch_ptr = NULL;

585
	if (diff_required(diff, "git_patch_from_diff") < 0)
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605
		return -1;

	delta = git_vector_get(&diff->deltas, idx);
	if (!delta) {
		giterr_set(GITERR_INVALID, "Index out of range for delta in diff");
		return GIT_ENOTFOUND;
	}

	if (git_diff_delta__should_skip(&diff->opts, delta))
		return 0;

	/* don't load the patch data unless we need it for binary check */
	if (!patch_ptr &&
		((delta->flags & DIFF_FLAGS_KNOWN_BINARY) != 0 ||
		 (diff->opts.flags & GIT_DIFF_SKIP_BINARY_CHECK) != 0))
		return 0;

	if ((error = diff_patch_alloc_from_diff(&patch, diff, idx)) < 0)
		return error;

606
	memset(&xo, 0, sizeof(xo));
Russell Belfer committed
607
	diff_output_to_patch(&xo.output, patch);
608 609
	git_xdiff_init(&xo, &diff->opts);

610
	error = diff_patch_invoke_file_callback(patch, &xo.output);
611 612

	if (!error)
Russell Belfer committed
613
		error = diff_patch_generate(patch, &xo.output);
614 615

	if (!error) {
616 617
		/* TODO: if cumulative diff size is < 0.5 total size, flatten patch */
		/* TODO: and unload the file content */
618 619 620
	}

	if (error || !patch_ptr)
621
		git_patch_free(patch);
622 623 624 625 626 627
	else
		*patch_ptr = patch;

	return error;
}

628
void git_patch_free(git_patch *patch)
629 630 631 632 633
{
	if (patch)
		GIT_REFCOUNT_DEC(patch, diff_patch_free);
}

634
const git_diff_delta *git_patch_get_delta(const git_patch *patch)
635 636 637 638 639
{
	assert(patch);
	return patch->delta;
}

640
size_t git_patch_num_hunks(const git_patch *patch)
641 642 643 644 645
{
	assert(patch);
	return git_array_size(patch->hunks);
}

646
int git_patch_line_stats(
647 648 649
	size_t *total_ctxt,
	size_t *total_adds,
	size_t *total_dels,
650
	const git_patch *patch)
651 652 653 654 655 656
{
	size_t totals[3], idx;

	memset(totals, 0, sizeof(totals));

	for (idx = 0; idx < git_array_size(patch->lines); ++idx) {
657
		git_diff_line *line = git_array_get(patch->lines, idx);
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
		if (!line)
			continue;

		switch (line->origin) {
		case GIT_DIFF_LINE_CONTEXT:  totals[0]++; break;
		case GIT_DIFF_LINE_ADDITION: totals[1]++; break;
		case GIT_DIFF_LINE_DELETION: totals[2]++; break;
		default:
			/* diff --stat and --numstat don't count EOFNL marks because
			 * they will always be paired with a ADDITION or DELETION line.
			 */
			break;
		}
	}

	if (total_ctxt)
		*total_ctxt = totals[0];
	if (total_adds)
		*total_adds = totals[1];
	if (total_dels)
		*total_dels = totals[2];

	return 0;
}

static int diff_error_outofrange(const char *thing)
{
	giterr_set(GITERR_INVALID, "Diff patch %s index out of range", thing);
	return GIT_ENOTFOUND;
}

689 690
int git_patch_get_hunk(
	const git_diff_hunk **out,
691
	size_t *lines_in_hunk,
692
	git_patch *patch,
693 694 695 696 697 698 699 700
	size_t hunk_idx)
{
	diff_patch_hunk *hunk;
	assert(patch);

	hunk = git_array_get(patch->hunks, hunk_idx);

	if (!hunk) {
701
		if (out) *out = NULL;
702 703 704 705
		if (lines_in_hunk) *lines_in_hunk = 0;
		return diff_error_outofrange("hunk");
	}

706
	if (out) *out = &hunk->hunk;
707 708 709 710
	if (lines_in_hunk) *lines_in_hunk = hunk->line_count;
	return 0;
}

711
int git_patch_num_lines_in_hunk(const git_patch *patch, size_t hunk_idx)
712 713 714 715 716 717 718 719 720
{
	diff_patch_hunk *hunk;
	assert(patch);

	if (!(hunk = git_array_get(patch->hunks, hunk_idx)))
		return diff_error_outofrange("hunk");
	return (int)hunk->line_count;
}

721
int git_patch_get_line_in_hunk(
722
	const git_diff_line **out,
723
	git_patch *patch,
724 725 726 727
	size_t hunk_idx,
	size_t line_of_hunk)
{
	diff_patch_hunk *hunk;
728
	git_diff_line *line;
729 730 731 732

	assert(patch);

	if (!(hunk = git_array_get(patch->hunks, hunk_idx))) {
733 734
		if (out) *out = NULL;
		return diff_error_outofrange("hunk");
735 736 737 738 739
	}

	if (line_of_hunk >= hunk->line_count ||
		!(line = git_array_get(
			patch->lines, hunk->line_start + line_of_hunk))) {
740 741
		if (out) *out = NULL;
		return diff_error_outofrange("line");
742 743
	}

744
	if (out) *out = line;
745 746 747
	return 0;
}

748 749
size_t git_patch_size(
	git_patch *patch,
750 751 752
	int include_context,
	int include_hunk_headers,
	int include_file_headers)
753 754 755 756 757 758 759 760 761 762
{
	size_t out;

	assert(patch);

	out = patch->content_size;

	if (!include_context)
		out -= patch->context_size;

763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
	if (include_hunk_headers)
		out += patch->header_size;

	if (include_file_headers) {
		git_buf file_header = GIT_BUF_INIT;

		if (git_diff_delta__format_file_header(
				&file_header, patch->delta, NULL, NULL, 0) < 0)
			giterr_clear();
		else
			out += git_buf_len(&file_header);

		git_buf_free(&file_header);
	}

778 779 780
	return out;
}

781
git_diff *git_patch__diff(git_patch *patch)
782 783 784 785
{
	return patch->diff;
}

786
git_diff_driver *git_patch__driver(git_patch *patch)
787 788 789 790 791
{
	/* ofile driver is representative for whole patch */
	return patch->ofile.driver;
}

792 793
void git_patch__old_data(
	char **ptr, size_t *len, git_patch *patch)
794 795 796 797 798
{
	*ptr = patch->ofile.map.data;
	*len = patch->ofile.map.len;
}

799 800
void git_patch__new_data(
	char **ptr, size_t *len, git_patch *patch)
801 802 803 804 805
{
	*ptr = patch->nfile.map.data;
	*len = patch->nfile.map.len;
}

806 807
int git_patch__invoke_callbacks(
	git_patch *patch,
808 809
	git_diff_file_cb file_cb,
	git_diff_hunk_cb hunk_cb,
810
	git_diff_line_cb line_cb,
811 812 813 814 815 816 817 818 819 820 821 822 823 824
	void *payload)
{
	int error = 0;
	uint32_t i, j;

	if (file_cb)
		error = file_cb(patch->delta, 0, payload);

	if (!hunk_cb && !line_cb)
		return error;

	for (i = 0; !error && i < git_array_size(patch->hunks); ++i) {
		diff_patch_hunk *h = git_array_get(patch->hunks, i);

825
		error = hunk_cb(patch->delta, &h->hunk, payload);
826 827 828 829 830

		if (!line_cb)
			continue;

		for (j = 0; !error && j < h->line_count; ++j) {
831
			git_diff_line *l =
832 833
				git_array_get(patch->lines, h->line_start + j);

834
			error = line_cb(patch->delta, &h->hunk, l, payload);
835 836 837 838 839 840
		}
	}

	return error;
}

841 842 843 844 845 846

static int diff_patch_file_cb(
	const git_diff_delta *delta,
	float progress,
	void *payload)
{
847
	GIT_UNUSED(delta); GIT_UNUSED(progress); GIT_UNUSED(payload);
848 849 850 851 852
	return 0;
}

static int diff_patch_hunk_cb(
	const git_diff_delta *delta,
853
	const git_diff_hunk *hunk_,
854 855
	void *payload)
{
856
	git_patch *patch = payload;
857 858 859 860
	diff_patch_hunk *hunk;

	GIT_UNUSED(delta);

861
	hunk = git_array_alloc(patch->hunks);
862
	GITERR_CHECK_ALLOC(hunk);
863

864
	memcpy(&hunk->hunk, hunk_, sizeof(hunk->hunk));
865

866
	patch->header_size += hunk_->header_len;
867

868 869 870 871 872 873 874 875
	hunk->line_start = git_array_size(patch->lines);
	hunk->line_count = 0;

	return 0;
}

static int diff_patch_line_cb(
	const git_diff_delta *delta,
876
	const git_diff_hunk *hunk_,
877
	const git_diff_line *line_,
878 879
	void *payload)
{
880
	git_patch *patch = payload;
881
	diff_patch_hunk *hunk;
882
	git_diff_line   *line;
883 884

	GIT_UNUSED(delta);
885
	GIT_UNUSED(hunk_);
886 887

	hunk = git_array_last(patch->hunks);
888
	assert(hunk); /* programmer error if no hunk is available */
889

890
	line = git_array_alloc(patch->lines);
891
	GITERR_CHECK_ALLOC(line);
892

893
	memcpy(line, line_, sizeof(*line));
894 895 896

	/* do some bookkeeping so we can provide old/new line numbers */

897
	patch->content_size += line->content_len;
898

899 900
	if (line->origin == GIT_DIFF_LINE_ADDITION ||
		line->origin == GIT_DIFF_LINE_DELETION)
901
		patch->content_size += 1;
902
	else if (line->origin == GIT_DIFF_LINE_CONTEXT) {
903
		patch->content_size += 1;
904 905 906
		patch->context_size += line->content_len + 1;
	} else if (line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL)
		patch->context_size += line->content_len;
907 908 909 910 911 912 913 914 915 916 917

	hunk->line_count++;

	return 0;
}

static void diff_output_init(
	git_diff_output *out,
	const git_diff_options *opts,
	git_diff_file_cb file_cb,
	git_diff_hunk_cb hunk_cb,
918
	git_diff_line_cb data_cb,
919 920 921 922 923 924 925 926 927 928 929 930
	void *payload)
{
	GIT_UNUSED(opts);

	memset(out, 0, sizeof(*out));

	out->file_cb = file_cb;
	out->hunk_cb = hunk_cb;
	out->data_cb = data_cb;
	out->payload = payload;
}

931
static void diff_output_to_patch(git_diff_output *out, git_patch *patch)
932 933
{
	diff_output_init(
934
		out, NULL,
935 936
		diff_patch_file_cb, diff_patch_hunk_cb, diff_patch_line_cb, patch);
}