diff_file.c 11.6 KB
Newer Older
1 2 3 4 5 6
/*
 * 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.
 */
7 8 9

#include "diff_file.h"

10 11 12
#include "git2/blob.h"
#include "git2/submodule.h"
#include "diff.h"
13
#include "diff_generate.h"
14 15 16 17 18 19 20 21 22
#include "odb.h"
#include "fileops.h"
#include "filter.h"

#define DIFF_MAX_FILESIZE 0x20000000

static bool diff_file_content_binary_by_size(git_diff_file_content *fc)
{
	/* if we have diff opts, check max_size vs file size */
23
	if ((fc->file->flags & DIFF_FLAGS_KNOWN_BINARY) == 0 &&
24
		fc->opts_max_size > 0 &&
25 26
		fc->file->size > fc->opts_max_size)
		fc->file->flags |= GIT_DIFF_FLAG_BINARY;
27

28
	return ((fc->file->flags & GIT_DIFF_FLAG_BINARY) != 0);
29 30 31 32
}

static void diff_file_content_binary_by_content(git_diff_file_content *fc)
{
33
	if ((fc->file->flags & DIFF_FLAGS_KNOWN_BINARY) != 0)
34 35 36 37
		return;

	switch (git_diff_driver_content_is_binary(
		fc->driver, fc->map.data, fc->map.len)) {
38 39
	case 0: fc->file->flags |= GIT_DIFF_FLAG_NOT_BINARY; break;
	case 1: fc->file->flags |= GIT_DIFF_FLAG_BINARY; break;
40 41 42 43
	default: break;
	}
}

44 45
static int diff_file_content_init_common(
	git_diff_file_content *fc, const git_diff_options *opts)
46
{
47 48 49 50 51
	fc->opts_flags = opts ? opts->flags : GIT_DIFF_NORMAL;

	if (opts && opts->max_size >= 0)
		fc->opts_max_size = opts->max_size ?
			opts->max_size : DIFF_MAX_FILESIZE;
52

53
	if (fc->src == GIT_ITERATOR_TYPE_EMPTY)
54
		fc->src = GIT_ITERATOR_TYPE_TREE;
55 56 57 58

	if (!fc->driver &&
		git_diff_driver_lookup(&fc->driver, fc->repo, fc->file->path) < 0)
		return -1;
59

60 61 62
	/* give driver a chance to modify options */
	git_diff_driver_update_options(&fc->opts_flags, fc->driver);

63
	/* make sure file is conceivable mmap-able */
64 65
	if ((git_off_t)((size_t)fc->file->size) != fc->file->size)
		fc->file->flags |= GIT_DIFF_FLAG_BINARY;
66 67
	/* check if user is forcing text diff the file */
	else if (fc->opts_flags & GIT_DIFF_FORCE_TEXT) {
68 69
		fc->file->flags &= ~GIT_DIFF_FLAG_BINARY;
		fc->file->flags |= GIT_DIFF_FLAG_NOT_BINARY;
70 71 72
	}
	/* check if user is forcing binary diff the file */
	else if (fc->opts_flags & GIT_DIFF_FORCE_BINARY) {
73 74
		fc->file->flags &= ~GIT_DIFF_FLAG_NOT_BINARY;
		fc->file->flags |= GIT_DIFF_FLAG_BINARY;
75
	}
76 77 78

	diff_file_content_binary_by_size(fc);

79 80
	if ((fc->flags & GIT_DIFF_FLAG__NO_DATA) != 0) {
		fc->flags |= GIT_DIFF_FLAG__LOADED;
81 82 83 84
		fc->map.len  = 0;
		fc->map.data = "";
	}

85
	if ((fc->flags & GIT_DIFF_FLAG__LOADED) != 0)
86 87 88 89 90
		diff_file_content_binary_by_content(fc);

	return 0;
}

91
int git_diff_file_content__init_from_diff(
92
	git_diff_file_content *fc,
93
	git_diff *diff,
94
	git_diff_delta *delta,
95 96 97 98 99 100
	bool use_old)
{
	bool has_data = true;

	memset(fc, 0, sizeof(*fc));
	fc->repo = diff->repo;
101
	fc->file = use_old ? &delta->old_file : &delta->new_file;
102 103
	fc->src  = use_old ? diff->old_src : diff->new_src;

104
	if (git_diff_driver_lookup(&fc->driver, fc->repo, fc->file->path) < 0)
105 106 107 108 109 110 111 112 113
		return -1;

	switch (delta->status) {
	case GIT_DELTA_ADDED:
		has_data = !use_old; break;
	case GIT_DELTA_DELETED:
		has_data = use_old; break;
	case GIT_DELTA_UNTRACKED:
		has_data = !use_old &&
Russell Belfer committed
114
			(diff->opts.flags & GIT_DIFF_SHOW_UNTRACKED_CONTENT) != 0;
115
		break;
116
	case GIT_DELTA_UNREADABLE:
117 118 119 120 121 122 123 124 125 126
	case GIT_DELTA_MODIFIED:
	case GIT_DELTA_COPIED:
	case GIT_DELTA_RENAMED:
		break;
	default:
		has_data = false;
		break;
	}

	if (!has_data)
127
		fc->flags |= GIT_DIFF_FLAG__NO_DATA;
128

129
	return diff_file_content_init_common(fc, &diff->opts);
130 131
}

132
int git_diff_file_content__init_from_src(
133 134 135
	git_diff_file_content *fc,
	git_repository *repo,
	const git_diff_options *opts,
136
	const git_diff_file_content_src *src,
137
	git_diff_file *as_file)
138 139 140
{
	memset(fc, 0, sizeof(*fc));
	fc->repo = repo;
141
	fc->file = as_file;
142

143
	if (!src->blob && !src->buf) {
144
		fc->flags |= GIT_DIFF_FLAG__NO_DATA;
145
	} else {
146
		fc->flags |= GIT_DIFF_FLAG__LOADED;
147
		fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
148
		fc->file->mode = GIT_FILEMODE_BLOB;
149

150
		if (src->blob) {
151
			git_blob_dup((git_blob **)&fc->blob, (git_blob *) src->blob);
152 153
			fc->file->size = git_blob_rawsize(src->blob);
			git_oid_cpy(&fc->file->id, git_blob_id(src->blob));
154
			fc->file->id_abbrev = GIT_OID_HEXSZ;
155

156 157
			fc->map.len  = (size_t)fc->file->size;
			fc->map.data = (char *)git_blob_rawcontent(src->blob);
158 159

			fc->flags |= GIT_DIFF_FLAG__FREE_BLOB;
160 161 162
		} else {
			fc->file->size = src->buflen;
			git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJ_BLOB);
163
			fc->file->id_abbrev = GIT_OID_HEXSZ;
164

165 166 167
			fc->map.len  = src->buflen;
			fc->map.data = (char *)src->buf;
		}
168 169
	}

170
	return diff_file_content_init_common(fc, opts);
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
}

static int diff_file_content_commit_to_str(
	git_diff_file_content *fc, bool check_status)
{
	char oid[GIT_OID_HEXSZ+1];
	git_buf content = GIT_BUF_INIT;
	const char *status = "";

	if (check_status) {
		int error = 0;
		git_submodule *sm = NULL;
		unsigned int sm_status = 0;
		const git_oid *sm_head;

186
		if ((error = git_submodule_lookup(&sm, fc->repo, fc->file->path)) < 0) {
187
			/* GIT_EEXISTS means a "submodule" that has not been git added */
188 189
			if (error == GIT_EEXISTS) {
				giterr_clear();
190
				error = 0;
191 192 193 194
			}
			return error;
		}

195
		if ((error = git_submodule_status(&sm_status, fc->repo, fc->file->path, GIT_SUBMODULE_IGNORE_UNSPECIFIED)) < 0) {
196
			git_submodule_free(sm);
197 198 199 200
			return error;
		}

		/* update OID if we didn't have it previously */
201
		if ((fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0 &&
202 203 204
			((sm_head = git_submodule_wd_id(sm)) != NULL ||
			 (sm_head = git_submodule_head_id(sm)) != NULL))
		{
205 206
			git_oid_cpy(&fc->file->id, sm_head);
			fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
207 208 209 210
		}

		if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status))
			status = "-dirty";
211 212

		git_submodule_free(sm);
213 214
	}

215
	git_oid_tostr(oid, sizeof(oid), &fc->file->id);
216 217 218 219 220
	if (git_buf_printf(&content, "Subproject commit %s%s\n", oid, status) < 0)
		return -1;

	fc->map.len  = git_buf_len(&content);
	fc->map.data = git_buf_detach(&content);
221
	fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
222 223 224 225

	return 0;
}

226 227 228
static int diff_file_content_load_blob(
	git_diff_file_content *fc,
	git_diff_options *opts)
229 230 231 232
{
	int error = 0;
	git_odb_object *odb_obj = NULL;

233
	if (git_oid_iszero(&fc->file->id))
234 235
		return 0;

236
	if (fc->file->mode == GIT_FILEMODE_COMMIT)
237 238 239
		return diff_file_content_commit_to_str(fc, false);

	/* if we don't know size, try to peek at object header first */
240
	if (!fc->file->size) {
241 242
		if ((error = git_diff_file__resolve_zero_size(
				fc->file, &odb_obj, fc->repo)) < 0)
243 244 245
			return error;
	}

246 247
	if ((opts->flags & GIT_DIFF_SHOW_BINARY) == 0 &&
		diff_file_content_binary_by_size(fc))
248 249 250 251 252 253 254 255
		return 0;

	if (odb_obj != NULL) {
		error = git_object__from_odb_object(
			(git_object **)&fc->blob, fc->repo, odb_obj, GIT_OBJ_BLOB);
		git_odb_object_free(odb_obj);
	} else {
		error = git_blob_lookup(
256
			(git_blob **)&fc->blob, fc->repo, &fc->file->id);
257 258 259
	}

	if (!error) {
260
		fc->flags |= GIT_DIFF_FLAG__FREE_BLOB;
261 262 263 264 265 266 267
		fc->map.data = (void *)git_blob_rawcontent(fc->blob);
		fc->map.len  = (size_t)git_blob_rawsize(fc->blob);
	}

	return error;
}

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
static int diff_file_content_load_workdir_symlink_fake(
	git_diff_file_content *fc, git_buf *path)
{
	git_buf target = GIT_BUF_INIT;
	int error;

	if ((error = git_futils_readbuffer(&target, path->ptr)) < 0)
		return error;

	fc->map.len = git_buf_len(&target);
	fc->map.data = git_buf_detach(&target);
	fc->flags |= GIT_DIFF_FLAG__FREE_DATA;

	git_buf_free(&target);
	return error;
}

285 286 287 288
static int diff_file_content_load_workdir_symlink(
	git_diff_file_content *fc, git_buf *path)
{
	ssize_t alloc_len, read_len;
289 290 291 292 293 294 295 296
	int symlink_supported, error;

	if ((error = git_repository__cvar(
		&symlink_supported, fc->repo, GIT_CVAR_SYMLINKS)) < 0)
		return -1;

	if (!symlink_supported)
		return diff_file_content_load_workdir_symlink_fake(fc, path);
297 298 299 300

	/* link path on disk could be UTF-16, so prepare a buffer that is
	 * big enough to handle some UTF-8 data expansion
	 */
301
	alloc_len = (ssize_t)(fc->file->size * 2) + 1;
302 303 304 305

	fc->map.data = git__calloc(alloc_len, sizeof(char));
	GITERR_CHECK_ALLOC(fc->map.data);

306
	fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
307 308 309

	read_len = p_readlink(git_buf_cstr(path), fc->map.data, alloc_len);
	if (read_len < 0) {
310
		giterr_set(GITERR_OS, "failed to read symlink '%s'", fc->file->path);
311 312 313 314 315 316 317 318
		return -1;
	}

	fc->map.len = read_len;
	return 0;
}

static int diff_file_content_load_workdir_file(
319 320 321
	git_diff_file_content *fc,
	git_buf *path,
	git_diff_options *diff_opts)
322 323
{
	int error = 0;
324
	git_filter_list *fl = NULL;
325
	git_file fd = git_futils_open_ro(git_buf_cstr(path));
326
	git_buf raw = GIT_BUF_INIT;
327 328 329 330

	if (fd < 0)
		return fd;

331 332
	if (!fc->file->size &&
		!(fc->file->size = git_futils_filesize(fd)))
333 334
		goto cleanup;

335 336
	if ((diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0 &&
		diff_file_content_binary_by_size(fc))
337 338
		goto cleanup;

339
	if ((error = git_filter_list_load(
340
			&fl, fc->repo, NULL, fc->file->path,
341
			GIT_FILTER_TO_ODB, GIT_FILTER_ALLOW_UNSAFE)) < 0)
342 343
		goto cleanup;

344 345
	/* if there are no filters, try to mmap the file */
	if (fl == NULL) {
346
		if (!(error = git_futils_mmap_ro(
347
				&fc->map, fd, 0, (size_t)fc->file->size))) {
348
			fc->flags |= GIT_DIFF_FLAG__UNMAP_DATA;
349 350 351
			goto cleanup;
		}

352 353
		/* if mmap failed, fall through to try readbuffer below */
		giterr_clear();
354 355
	}

356
	if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size))) {
357
		git_buf out = GIT_BUF_INIT;
358

359
		error = git_filter_list_apply_to_data(&out, fl, &raw);
360

361 362
		if (out.ptr != raw.ptr)
			git_buf_free(&raw);
363

364 365 366 367 368 369
		if (!error) {
			fc->map.len  = out.size;
			fc->map.data = out.ptr;
			fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
		}
	}
370

371
cleanup:
372
	git_filter_list_free(fl);
373 374 375 376 377
	p_close(fd);

	return error;
}

378 379 380
static int diff_file_content_load_workdir(
	git_diff_file_content *fc,
	git_diff_options *diff_opts)
381 382 383 384
{
	int error = 0;
	git_buf path = GIT_BUF_INIT;

385
	if (fc->file->mode == GIT_FILEMODE_COMMIT)
386 387
		return diff_file_content_commit_to_str(fc, true);

388
	if (fc->file->mode == GIT_FILEMODE_TREE)
389 390 391
		return 0;

	if (git_buf_joinpath(
392
			&path, git_repository_workdir(fc->repo), fc->file->path) < 0)
393 394
		return -1;

395
	if (S_ISLNK(fc->file->mode))
396 397
		error = diff_file_content_load_workdir_symlink(fc, &path);
	else
398
		error = diff_file_content_load_workdir_file(fc, &path, diff_opts);
399 400

	/* once data is loaded, update OID if we didn't have it previously */
401
	if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0) {
402
		error = git_odb_hash(
403 404
			&fc->file->id, fc->map.data, fc->map.len, GIT_OBJ_BLOB);
		fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
405 406 407 408 409 410
	}

	git_buf_free(&path);
	return error;
}

411 412 413
int git_diff_file_content__load(
	git_diff_file_content *fc,
	git_diff_options *diff_opts)
414 415 416
{
	int error = 0;

417
	if ((fc->flags & GIT_DIFF_FLAG__LOADED) != 0)
418 419
		return 0;

420 421
	if ((fc->file->flags & GIT_DIFF_FLAG_BINARY) != 0 &&
		(diff_opts->flags & GIT_DIFF_SHOW_BINARY) == 0)
422 423 424
		return 0;

	if (fc->src == GIT_ITERATOR_TYPE_WORKDIR)
425
		error = diff_file_content_load_workdir(fc, diff_opts);
426
	else
427
		error = diff_file_content_load_blob(fc, diff_opts);
428 429 430
	if (error)
		return error;

431
	fc->flags |= GIT_DIFF_FLAG__LOADED;
432 433 434 435 436 437

	diff_file_content_binary_by_content(fc);

	return 0;
}

438
void git_diff_file_content__unload(git_diff_file_content *fc)
439
{
440 441 442
	if ((fc->flags & GIT_DIFF_FLAG__LOADED) == 0)
		return;

443
	if (fc->flags & GIT_DIFF_FLAG__FREE_DATA) {
444 445 446
		git__free(fc->map.data);
		fc->map.data = "";
		fc->map.len  = 0;
447
		fc->flags &= ~GIT_DIFF_FLAG__FREE_DATA;
448
	}
449
	else if (fc->flags & GIT_DIFF_FLAG__UNMAP_DATA) {
450 451 452
		git_futils_mmap_free(&fc->map);
		fc->map.data = "";
		fc->map.len  = 0;
453
		fc->flags &= ~GIT_DIFF_FLAG__UNMAP_DATA;
454 455
	}

456
	if (fc->flags & GIT_DIFF_FLAG__FREE_BLOB) {
457 458
		git_blob_free((git_blob *)fc->blob);
		fc->blob = NULL;
459
		fc->flags &= ~GIT_DIFF_FLAG__FREE_BLOB;
460 461
	}

462
	fc->flags &= ~GIT_DIFF_FLAG__LOADED;
463 464
}

465
void git_diff_file_content__clear(git_diff_file_content *fc)
466
{
467
	git_diff_file_content__unload(fc);
468 469 470

	/* for now, nothing else to do */
}