diff.c 8.95 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 "common.h"
11
#include "buf.h"
12
#include "patch.h"
13
#include "email.h"
14
#include "commit.h"
15
#include "index.h"
16 17 18 19
#include "diff_generate.h"

#include "git2/version.h"
#include "git2/email.h"
20

21
struct patch_id_args {
22
	git_diff *diff;
23 24
	git_hash_ctx ctx;
	git_oid result;
25
	git_oid_t oid_type;
26 27 28
	int first_file;
};

29 30 31 32 33 34 35 36 37 38 39 40 41
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;
}

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

49 50 51 52 53 54 55
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);
}

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

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

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

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

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

77
	GIT_REFCOUNT_DEC(diff, diff->free_fn);
Russell Belfer committed
78 79
}

80
void git_diff_addref(git_diff *diff)
81 82 83 84
{
	GIT_REFCOUNT_INC(diff);
}

Russell Belfer committed
85
size_t git_diff_num_deltas(const git_diff *diff)
86
{
Edward Thomson committed
87
	GIT_ASSERT_ARG(diff);
Russell Belfer committed
88
	return diff->deltas.length;
89 90
}

Russell Belfer committed
91
size_t git_diff_num_deltas_of_type(const git_diff *diff, git_delta_t type)
92 93
{
	size_t i, count = 0;
Russell Belfer committed
94
	const git_diff_delta *delta;
95

Edward Thomson committed
96
	GIT_ASSERT_ARG(diff);
97 98 99 100 101 102 103 104

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

	return count;
}

Russell Belfer committed
105 106
const git_diff_delta *git_diff_get_delta(const git_diff *diff, size_t idx)
{
Edward Thomson committed
107
	GIT_ASSERT_ARG_WITH_RETVAL(diff, NULL);
Russell Belfer committed
108 109 110
	return git_vector_get(&diff->deltas, idx);
}

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

116 117
int git_diff_get_perfdata(git_diff_perfdata *out, const git_diff *diff)
{
Edward Thomson committed
118
	GIT_ASSERT_ARG(out);
119
	GIT_ERROR_CHECK_VERSION(out, GIT_DIFF_PERFDATA_VERSION, "git_diff_perfdata");
120 121 122 123 124
	out->stat_calls = diff->perf.stat_calls;
	out->oid_calculations = diff->perf.oid_calculations;
	return 0;
}

125 126 127 128 129 130 131 132 133 134 135 136
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;

Edward Thomson committed
137
	GIT_ASSERT_ARG(diff);
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

	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;
}

160 161
#ifndef GIT_DEPRECATE_HARD

162 163 164 165 166
int git_diff_format_email(
	git_buf *out,
	git_diff *diff,
	const git_diff_format_email_options *opts)
{
167
	git_email_create_options email_create_opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
168
	git_str email = GIT_STR_INIT;
169 170
	int error;

Edward Thomson committed
171 172 173
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(diff);
	GIT_ASSERT_ARG(opts && opts->summary && opts->id && opts->author);
174

175
	GIT_ERROR_CHECK_VERSION(opts,
176 177 178
		GIT_DIFF_FORMAT_EMAIL_OPTIONS_VERSION,
		"git_format_email_options");

179 180 181 182 183 184 185 186 187
	/* This is a `git_buf` special case; subsequent calls append. */
	email.ptr = out->ptr;
	email.asize = out->reserved;
	email.size = out->size;

	out->ptr = git_str__initstr;
	out->reserved = 0;
	out->size = 0;

188 189
	if ((opts->flags & GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) != 0)
		email_create_opts.subject_prefix = "";
190

191
	error = git_email__append_from_diff(&email, diff, opts->patch_no,
192 193
		opts->total_patches, opts->id, opts->summary, opts->body,
		opts->author, &email_create_opts);
194

195 196 197 198 199 200 201
	if (error < 0)
		goto done;

	error = git_buf_fromstr(out, &email);

done:
	git_str_dispose(&email);
202 203 204 205 206 207 208 209 210
	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,
211
	uint32_t flags,
212 213 214
	const git_diff_options *diff_opts)
{
	git_diff *diff = NULL;
215 216 217 218
	git_email_create_options opts = GIT_EMAIL_CREATE_OPTIONS_INIT;
	const git_oid *commit_id;
	const char *summary, *body;
	const git_signature *author;
219 220
	int error;

Edward Thomson committed
221 222 223
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(commit);
224

225 226 227 228 229 230 231
	commit_id = git_commit_id(commit);
	summary = git_commit_summary(commit);
	body = git_commit_body(commit);
	author = git_commit_author(commit);

	if ((flags & GIT_DIFF_FORMAT_EMAIL_EXCLUDE_SUBJECT_PATCH_MARKER) != 0)
		opts.subject_prefix = "";
232 233 234 235

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

236
	error = git_email_create_from_diff(out, diff, patch_no, total_patches, commit_id, summary, body, author, &opts);
237 238 239 240 241

	git_diff_free(diff);
	return error;
}

242 243 244 245 246 247 248 249 250 251 252 253
int git_diff_init_options(git_diff_options *opts, unsigned int version)
{
	return git_diff_options_init(opts, version);
}

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(
254
	git_diff_format_email_options *opts, unsigned int version)
255
{
256 257 258
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_diff_format_email_options,
		GIT_DIFF_FORMAT_EMAIL_OPTIONS_INIT);
259
	return 0;
260
}
261

262 263 264 265 266
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);
}
267

268
#endif
269

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
int git_diff_options_init(git_diff_options *opts, unsigned int version)
{
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_diff_options, GIT_DIFF_OPTIONS_INIT);
	return 0;
}

int git_diff_find_options_init(
	git_diff_find_options *opts, unsigned int version)
{
	GIT_INIT_STRUCTURE_FROM_TEMPLATE(
		opts, version, git_diff_find_options, GIT_DIFF_FIND_OPTIONS_INIT);
	return 0;
}

285
static int flush_hunk(git_oid *result, struct patch_id_args *args)
286
{
287
	git_hash_ctx *ctx = &args->ctx;
288 289
	git_oid hash;
	unsigned short carry = 0;
290 291
	size_t i;
	int error;
292

293
	if ((error = git_hash_final(hash.id, ctx)) < 0 ||
294 295 296
	    (error = git_hash_init(ctx)) < 0)
		return error;

297
	for (i = 0; i < git_oid_size(args->oid_type); i++) {
298
		carry += result->id[i] + hash.id[i];
299
		result->id[i] = (unsigned char)carry;
300 301 302 303 304 305
		carry >>= 8;
	}

	return 0;
}

306
static void strip_spaces(git_str *buf)
307 308 309 310 311 312 313 314 315 316 317 318
{
	char *src = buf->ptr, *dst = buf->ptr;
	char c;
	size_t len = 0;

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

319
	git_str_truncate(buf, len);
320 321
}

322
static int diff_patchid_print_callback_to_buf(
323
	const git_diff_delta *delta,
324 325
	const git_diff_hunk *hunk,
	const git_diff_line *line,
326 327 328
	void *payload)
{
	struct patch_id_args *args = (struct patch_id_args *) payload;
329
	git_str buf = GIT_STR_INIT;
330
	int error = 0;
331

332 333 334
	if (line->origin == GIT_DIFF_LINE_CONTEXT_EOFNL ||
	    line->origin == GIT_DIFF_LINE_ADD_EOFNL ||
	    line->origin == GIT_DIFF_LINE_DEL_EOFNL)
335 336
		goto out;

337 338
	if ((error = git_diff_print_callback__to_buf(delta, hunk,
						     line, &buf)) < 0)
339 340 341 342
		goto out;

	strip_spaces(&buf);

343 344
	if (line->origin == GIT_DIFF_LINE_FILE_HDR &&
	    !args->first_file &&
345
	    (error = flush_hunk(&args->result, args) < 0))
346 347
			goto out;

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

351 352 353
	if (line->origin == GIT_DIFF_LINE_FILE_HDR && args->first_file)
		args->first_file = 0;

354
out:
355
	git_str_dispose(&buf);
356 357 358
	return error;
}

359
int git_diff_patchid_options_init(git_diff_patchid_options *opts, unsigned int version)
360 361 362 363 364 365 366 367 368
{
	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;
369
	git_hash_algorithm_t algorithm;
370 371
	int error;

372
	GIT_ERROR_CHECK_VERSION(
373 374
		opts, GIT_DIFF_PATCHID_OPTIONS_VERSION, "git_diff_patchid_options");

375 376
	algorithm = git_oid_algorithm(diff->opts.oid_type);

377
	memset(&args, 0, sizeof(args));
378
	args.diff = diff;
379
	args.first_file = 1;
380 381
	args.oid_type = diff->opts.oid_type;
	if ((error = git_hash_ctx_init(&args.ctx, algorithm)) < 0)
382 383
		goto out;

384 385
	if ((error = git_diff_print(diff,
				    GIT_DIFF_FORMAT_PATCH_ID,
386
				    diff_patchid_print_callback_to_buf,
387
				    &args)) < 0)
388 389
		goto out;

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

393
#ifdef GIT_EXPERIMENTAL_SHA256
394
	args.result.type = diff->opts.oid_type;
395 396
#endif

397 398 399
	git_oid_cpy(out, &args.result);

out:
400
	git_hash_ctx_cleanup(&args.ctx);
401 402
	return error;
}