diff_file.c 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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 "git2/blob.h"
#include "git2/submodule.h"
#include "diff.h"
#include "diff_file.h"
#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 */
21
	if ((fc->file->flags & DIFF_FLAGS_KNOWN_BINARY) == 0 &&
22
		fc->opts_max_size > 0 &&
23 24
		fc->file->size > fc->opts_max_size)
		fc->file->flags |= GIT_DIFF_FLAG_BINARY;
25

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

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

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

42 43
static int diff_file_content_init_common(
	git_diff_file_content *fc, const git_diff_options *opts)
44
{
45 46 47 48 49
	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;
50

51
	if (fc->src == GIT_ITERATOR_TYPE_EMPTY)
52
		fc->src = GIT_ITERATOR_TYPE_TREE;
53 54 55 56

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

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

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

	diff_file_content_binary_by_size(fc);

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

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

	return 0;
}

89
int git_diff_file_content__init_from_diff(
90
	git_diff_file_content *fc,
91
	git_diff *diff,
92 93 94 95 96 97 98 99
	size_t delta_index,
	bool use_old)
{
	git_diff_delta *delta = git_vector_get(&diff->deltas, delta_index);
	bool has_data = true;

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

103
	if (git_diff_driver_lookup(&fc->driver, fc->repo, fc->file->path) < 0)
104 105 106 107 108 109 110 111 112
		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
113
			(diff->opts.flags & GIT_DIFF_SHOW_UNTRACKED_CONTENT) != 0;
114 115 116 117 118 119 120 121 122 123 124
		break;
	case GIT_DELTA_MODIFIED:
	case GIT_DELTA_COPIED:
	case GIT_DELTA_RENAMED:
		break;
	default:
		has_data = false;
		break;
	}

	if (!has_data)
125
		fc->flags |= GIT_DIFF_FLAG__NO_DATA;
126

127
	return diff_file_content_init_common(fc, &diff->opts);
128 129
}

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

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

149 150 151
		if (src->blob) {
			fc->file->size = git_blob_rawsize(src->blob);
			git_oid_cpy(&fc->file->id, git_blob_id(src->blob));
152

153 154 155 156 157
			fc->map.len  = (size_t)fc->file->size;
			fc->map.data = (char *)git_blob_rawcontent(src->blob);
		} else {
			fc->file->size = src->buflen;
			git_odb_hash(&fc->file->id, src->buf, src->buflen, GIT_OBJ_BLOB);
158

159 160 161
			fc->map.len  = src->buflen;
			fc->map.data = (char *)src->buf;
		}
162 163
	}

164
	return diff_file_content_init_common(fc, opts);
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
}

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;

180
		if ((error = git_submodule_lookup(&sm, fc->repo, fc->file->path)) < 0 ||
181 182 183 184 185 186 187 188
			(error = git_submodule_status(&sm_status, sm)) < 0) {
			/* GIT_EEXISTS means a "submodule" that has not been git added */
			if (error == GIT_EEXISTS)
				error = 0;
			return error;
		}

		/* update OID if we didn't have it previously */
189
		if ((fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0 &&
190 191 192
			((sm_head = git_submodule_wd_id(sm)) != NULL ||
			 (sm_head = git_submodule_head_id(sm)) != NULL))
		{
193 194
			git_oid_cpy(&fc->file->id, sm_head);
			fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
195 196 197 198 199 200
		}

		if (GIT_SUBMODULE_STATUS_IS_WD_DIRTY(sm_status))
			status = "-dirty";
	}

201
	git_oid_tostr(oid, sizeof(oid), &fc->file->id);
202 203 204 205 206
	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);
207
	fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
208 209 210 211 212 213 214 215 216

	return 0;
}

static int diff_file_content_load_blob(git_diff_file_content *fc)
{
	int error = 0;
	git_odb_object *odb_obj = NULL;

217
	if (git_oid_iszero(&fc->file->id))
218 219
		return 0;

220
	if (fc->file->mode == GIT_FILEMODE_COMMIT)
221 222 223
		return diff_file_content_commit_to_str(fc, false);

	/* if we don't know size, try to peek at object header first */
224
	if (!fc->file->size) {
225 226
		if ((error = git_diff_file__resolve_zero_size(
				fc->file, &odb_obj, fc->repo)) < 0)
227 228 229 230 231 232 233 234 235 236 237 238
			return error;
	}

	if (diff_file_content_binary_by_size(fc))
		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(
239
			(git_blob **)&fc->blob, fc->repo, &fc->file->id);
240 241 242
	}

	if (!error) {
243
		fc->flags |= GIT_DIFF_FLAG__FREE_BLOB;
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
		fc->map.data = (void *)git_blob_rawcontent(fc->blob);
		fc->map.len  = (size_t)git_blob_rawsize(fc->blob);
	}

	return error;
}

static int diff_file_content_load_workdir_symlink(
	git_diff_file_content *fc, git_buf *path)
{
	ssize_t alloc_len, read_len;

	/* link path on disk could be UTF-16, so prepare a buffer that is
	 * big enough to handle some UTF-8 data expansion
	 */
259
	alloc_len = (ssize_t)(fc->file->size * 2) + 1;
260 261 262 263

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

264
	fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
265 266 267

	read_len = p_readlink(git_buf_cstr(path), fc->map.data, alloc_len);
	if (read_len < 0) {
268
		giterr_set(GITERR_OS, "Failed to read symlink '%s'", fc->file->path);
269 270 271 272 273 274 275 276 277 278 279
		return -1;
	}

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

static int diff_file_content_load_workdir_file(
	git_diff_file_content *fc, git_buf *path)
{
	int error = 0;
280
	git_filter_list *fl = NULL;
281
	git_file fd = git_futils_open_ro(git_buf_cstr(path));
282
	git_buf raw = GIT_BUF_INIT;
283 284 285 286

	if (fd < 0)
		return fd;

287 288
	if (!fc->file->size &&
		!(fc->file->size = git_futils_filesize(fd)))
289 290 291 292 293
		goto cleanup;

	if (diff_file_content_binary_by_size(fc))
		goto cleanup;

294
	if ((error = git_filter_list_load(
Russell Belfer committed
295
			&fl, fc->repo, NULL, fc->file->path, GIT_FILTER_TO_ODB)) < 0)
296 297
		goto cleanup;

298 299
	/* if there are no filters, try to mmap the file */
	if (fl == NULL) {
300
		if (!(error = git_futils_mmap_ro(
301
				&fc->map, fd, 0, (size_t)fc->file->size))) {
302
			fc->flags |= GIT_DIFF_FLAG__UNMAP_DATA;
303 304 305
			goto cleanup;
		}

306 307
		/* if mmap failed, fall through to try readbuffer below */
		giterr_clear();
308 309
	}

310
	if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)fc->file->size))) {
311
		git_buf out = GIT_BUF_INIT;
312

313
		error = git_filter_list_apply_to_data(&out, fl, &raw);
314

315
		git_buf_free(&raw);
316

317 318 319 320 321 322
		if (!error) {
			fc->map.len  = out.size;
			fc->map.data = out.ptr;
			fc->flags |= GIT_DIFF_FLAG__FREE_DATA;
		}
	}
323

324
cleanup:
325
	git_filter_list_free(fl);
326 327 328 329 330 331 332 333 334 335
	p_close(fd);

	return error;
}

static int diff_file_content_load_workdir(git_diff_file_content *fc)
{
	int error = 0;
	git_buf path = GIT_BUF_INIT;

336
	if (fc->file->mode == GIT_FILEMODE_COMMIT)
337 338
		return diff_file_content_commit_to_str(fc, true);

339
	if (fc->file->mode == GIT_FILEMODE_TREE)
340 341 342
		return 0;

	if (git_buf_joinpath(
343
			&path, git_repository_workdir(fc->repo), fc->file->path) < 0)
344 345
		return -1;

346
	if (S_ISLNK(fc->file->mode))
347 348 349 350 351
		error = diff_file_content_load_workdir_symlink(fc, &path);
	else
		error = diff_file_content_load_workdir_file(fc, &path);

	/* once data is loaded, update OID if we didn't have it previously */
352
	if (!error && (fc->file->flags & GIT_DIFF_FLAG_VALID_ID) == 0) {
353
		error = git_odb_hash(
354 355
			&fc->file->id, fc->map.data, fc->map.len, GIT_OBJ_BLOB);
		fc->file->flags |= GIT_DIFF_FLAG_VALID_ID;
356 357 358 359 360 361
	}

	git_buf_free(&path);
	return error;
}

362
int git_diff_file_content__load(git_diff_file_content *fc)
363 364 365
{
	int error = 0;

366
	if ((fc->flags & GIT_DIFF_FLAG__LOADED) != 0)
367 368
		return 0;

369
	if ((fc->file->flags & GIT_DIFF_FLAG_BINARY) != 0)
370 371 372 373 374 375 376 377 378
		return 0;

	if (fc->src == GIT_ITERATOR_TYPE_WORKDIR)
		error = diff_file_content_load_workdir(fc);
	else
		error = diff_file_content_load_blob(fc);
	if (error)
		return error;

379
	fc->flags |= GIT_DIFF_FLAG__LOADED;
380 381 382 383 384 385

	diff_file_content_binary_by_content(fc);

	return 0;
}

386
void git_diff_file_content__unload(git_diff_file_content *fc)
387
{
388 389 390
	if ((fc->flags & GIT_DIFF_FLAG__LOADED) == 0)
		return;

391
	if (fc->flags & GIT_DIFF_FLAG__FREE_DATA) {
392 393 394
		git__free(fc->map.data);
		fc->map.data = "";
		fc->map.len  = 0;
395
		fc->flags &= ~GIT_DIFF_FLAG__FREE_DATA;
396
	}
397
	else if (fc->flags & GIT_DIFF_FLAG__UNMAP_DATA) {
398 399 400
		git_futils_mmap_free(&fc->map);
		fc->map.data = "";
		fc->map.len  = 0;
401
		fc->flags &= ~GIT_DIFF_FLAG__UNMAP_DATA;
402 403
	}

404
	if (fc->flags & GIT_DIFF_FLAG__FREE_BLOB) {
405 406
		git_blob_free((git_blob *)fc->blob);
		fc->blob = NULL;
407
		fc->flags &= ~GIT_DIFF_FLAG__FREE_BLOB;
408 409
	}

410
	fc->flags &= ~GIT_DIFF_FLAG__LOADED;
411 412
}

413
void git_diff_file_content__clear(git_diff_file_content *fc)
414
{
415
	git_diff_file_content__unload(fc);
416 417 418

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