diff-lib.c 14.6 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2005 Junio C Hamano
 */
4
#include "cache.h"
5
#include "quote.h"
Junio C Hamano's avatar
Junio C Hamano committed
6
#include "commit.h"
7
#include "diff.h"
8
#include "diffcore.h"
Junio C Hamano's avatar
Junio C Hamano committed
9
#include "revision.h"
10
#include "cache-tree.h"
11
#include "unpack-trees.h"
12
#include "refs.h"
13
#include "submodule.h"
14
#include "dir.h"
15

16
/*
Junio C Hamano's avatar
Junio C Hamano committed
17
 * diff-files
18
 */
19

20
/*
21 22 23 24 25 26 27
 * Has the work tree entity been removed?
 *
 * Return 1 if it was removed from the work tree, 0 if an entity to be
 * compared with the cache entry ce still exists (the latter includes
 * the case where a directory that is not a submodule repository
 * exists for ce that is a submodule -- it is a submodule that is not
 * checked out).  Return negative for an error.
28
 */
29
static int check_removed(const struct cache_entry *ce, struct stat *st)
30 31
{
	if (lstat(ce->name, st) < 0) {
32
		if (!is_missing_file_error(errno))
33 34 35
			return -1;
		return 1;
	}
36
	if (has_symlink_leading_path(ce->name, ce_namelen(ce)))
37 38 39
		return 1;
	if (S_ISDIR(st->st_mode)) {
		unsigned char sub[20];
40 41 42 43 44 45 46 47 48 49 50 51 52 53

		/*
		 * If ce is already a gitlink, we can have a plain
		 * directory (i.e. the submodule is not checked out),
		 * or a checked out submodule.  Either case this is not
		 * a case where something was removed from the work tree,
		 * so we will return 0.
		 *
		 * Otherwise, if the directory is not a submodule
		 * repository, that means ce which was a blob turned into
		 * a directory --- the blob was removed!
		 */
		if (!S_ISGITLINK(ce->ce_mode) &&
		    resolve_gitlink_ref(ce->name, "HEAD", sub))
54 55 56 57
			return 1;
	}
	return 0;
}
58

59 60 61 62 63 64 65 66 67
/*
 * Has a file changed or has a submodule new commits or a dirty work tree?
 *
 * Return 1 when changes are detected, 0 otherwise. If the DIRTY_SUBMODULES
 * option is set, the caller does not only want to know if a submodule is
 * modified at all but wants to know all the conditions that are met (new
 * commits, untracked content and/or modified content).
 */
static int match_stat_with_submodule(struct diff_options *diffopt,
68 69 70
				     const struct cache_entry *ce,
				     struct stat *st, unsigned ce_option,
				     unsigned *dirty_submodule)
71 72
{
	int changed = ce_match_stat(ce, st, ce_option);
73 74 75 76 77 78 79 80 81 82
	if (S_ISGITLINK(ce->ce_mode)) {
		unsigned orig_flags = diffopt->flags;
		if (!DIFF_OPT_TST(diffopt, OVERRIDE_SUBMODULE_CONFIG))
			set_diffopt_flags_from_submodule_config(diffopt, ce->name);
		if (DIFF_OPT_TST(diffopt, IGNORE_SUBMODULES))
			changed = 0;
		else if (!DIFF_OPT_TST(diffopt, IGNORE_DIRTY_SUBMODULES)
		    && (!changed || DIFF_OPT_TST(diffopt, DIRTY_SUBMODULES)))
			*dirty_submodule = is_submodule_modified(ce->name, DIFF_OPT_TST(diffopt, IGNORE_UNTRACKED_IN_SUBMODULES));
		diffopt->flags = orig_flags;
83 84 85 86
	}
	return changed;
}

87
int run_diff_files(struct rev_info *revs, unsigned int option)
88
{
Junio C Hamano's avatar
Junio C Hamano committed
89 90
	int entries, i;
	int diff_unmerged_stage = revs->max_count;
91 92
	unsigned ce_option = ((option & DIFF_RACY_IS_MODIFIED)
			      ? CE_MATCH_RACY_IS_DIRTY : 0);
93

94 95
	diff_set_mnemonic_prefix(&revs->diffopt, "i/", "w/");

Junio C Hamano's avatar
Junio C Hamano committed
96 97
	if (diff_unmerged_stage < 0)
		diff_unmerged_stage = 2;
98
	entries = active_nr;
Junio C Hamano's avatar
Junio C Hamano committed
99 100 101 102
	for (i = 0; i < entries; i++) {
		unsigned int oldmode, newmode;
		struct cache_entry *ce = active_cache[i];
		int changed;
103
		unsigned dirty_submodule = 0;
104
		const struct object_id *old_oid, *new_oid;
105

106
		if (diff_can_quit_early(&revs->diffopt))
107 108
			break;

109
		if (!ce_path_match(ce, &revs->prune_data, NULL))
110
			continue;
111

Junio C Hamano's avatar
Junio C Hamano committed
112
		if (ce_stage(ce)) {
113
			struct combine_diff_path *dpath;
114 115
			struct diff_filepair *pair;
			unsigned int wt_mode = 0;
Junio C Hamano's avatar
Junio C Hamano committed
116
			int num_compare_stages = 0;
117
			size_t path_len;
118
			struct stat st;
Junio C Hamano's avatar
Junio C Hamano committed
119

120 121
			path_len = ce_namelen(ce);

122
			dpath = xmalloc(combine_diff_path_size(5, path_len));
123 124 125 126 127
			dpath->path = (char *) &(dpath->parent[5]);

			dpath->next = NULL;
			memcpy(dpath->path, ce->name, path_len);
			dpath->path[path_len] = '\0';
128
			oidclr(&dpath->oid);
129
			memset(&(dpath->parent[0]), 0,
130 131
			       sizeof(struct combine_diff_parent)*5);

132
			changed = check_removed(ce, &st);
133
			if (!changed)
134
				wt_mode = ce_mode_from_stat(ce, st.st_mode);
135 136
			else {
				if (changed < 0) {
137 138 139
					perror(ce->name);
					continue;
				}
140
				wt_mode = 0;
141
			}
142
			dpath->mode = wt_mode;
Junio C Hamano's avatar
Junio C Hamano committed
143 144 145 146 147 148 149 150 151 152 153 154 155

			while (i < entries) {
				struct cache_entry *nce = active_cache[i];
				int stage;

				if (strcmp(ce->name, nce->name))
					break;

				/* Stage #2 (ours) is the first parent,
				 * stage #3 (theirs) is the second.
				 */
				stage = ce_stage(nce);
				if (2 <= stage) {
156
					int mode = nce->ce_mode;
Junio C Hamano's avatar
Junio C Hamano committed
157
					num_compare_stages++;
158 159
					oidcpy(&dpath->parent[stage - 2].oid,
					       &nce->oid);
160
					dpath->parent[stage-2].mode = ce_mode_from_stat(nce, mode);
161
					dpath->parent[stage-2].status =
Junio C Hamano's avatar
Junio C Hamano committed
162 163 164 165 166 167 168 169 170 171
						DIFF_STATUS_MODIFIED;
				}

				/* diff against the proper unmerged stage */
				if (stage == diff_unmerged_stage)
					ce = nce;
				i++;
			}
			/*
			 * Compensate for loop update
172
			 */
Junio C Hamano's avatar
Junio C Hamano committed
173
			i--;
Junio C Hamano's avatar
Junio C Hamano committed
174

Junio C Hamano's avatar
Junio C Hamano committed
175
			if (revs->combine_merges && num_compare_stages == 2) {
176
				show_combined_diff(dpath, 2,
Junio C Hamano's avatar
Junio C Hamano committed
177 178
						   revs->dense_combined_merges,
						   revs);
179
				free(dpath);
Junio C Hamano's avatar
Junio C Hamano committed
180
				continue;
181
			}
182
			FREE_AND_NULL(dpath);
183

Junio C Hamano's avatar
Junio C Hamano committed
184 185 186 187
			/*
			 * Show the diff for the 'ce' if we found the one
			 * from the desired stage.
			 */
188 189 190
			pair = diff_unmerge(&revs->diffopt, ce->name);
			if (wt_mode)
				pair->two->mode = wt_mode;
Junio C Hamano's avatar
Junio C Hamano committed
191 192
			if (ce_stage(ce) != diff_unmerged_stage)
				continue;
193
		}
194

195
		if (ce_uptodate(ce) || ce_skip_worktree(ce))
196
			continue;
197

198
		/* If CE_VALID is set, don't look at workdir for file removal */
199 200 201 202 203 204 205 206 207 208 209 210 211
		if (ce->ce_flags & CE_VALID) {
			changed = 0;
			newmode = ce->ce_mode;
		} else {
			struct stat st;

			changed = check_removed(ce, &st);
			if (changed) {
				if (changed < 0) {
					perror(ce->name);
					continue;
				}
				diff_addremove(&revs->diffopt, '-', ce->ce_mode,
212
					       &ce->oid,
213
					       !is_null_oid(&ce->oid),
214
					       ce->name, 0);
215
				continue;
216 217 218
			} else if (revs->diffopt.ita_invisible_in_index &&
				   ce_intent_to_add(ce)) {
				diff_addremove(&revs->diffopt, '+', ce->ce_mode,
219
					       &empty_tree_oid, 0,
220 221
					       ce->name, 0);
				continue;
222
			}
223 224 225 226

			changed = match_stat_with_submodule(&revs->diffopt, ce, &st,
							    ce_option, &dirty_submodule);
			newmode = ce_mode_from_stat(ce, st.st_mode);
227
		}
228

229
		if (!changed && !dirty_submodule) {
230 231 232 233
			ce_mark_uptodate(ce);
			if (!DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER))
				continue;
		}
234
		oldmode = ce->ce_mode;
235 236
		old_oid = &ce->oid;
		new_oid = changed ? &null_oid : &ce->oid;
Junio C Hamano's avatar
Junio C Hamano committed
237
		diff_change(&revs->diffopt, oldmode, newmode,
238
			    old_oid, new_oid,
239 240
			    !is_null_oid(old_oid),
			    !is_null_oid(new_oid),
241
			    ce->name, 0, dirty_submodule);
242

243
	}
Junio C Hamano's avatar
Junio C Hamano committed
244 245 246
	diffcore_std(&revs->diffopt);
	diff_flush(&revs->diffopt);
	return 0;
247
}
248

Junio C Hamano's avatar
Junio C Hamano committed
249 250 251 252 253 254 255
/*
 * diff-index
 */

/* A file entry went away or appeared */
static void diff_index_show_file(struct rev_info *revs,
				 const char *prefix,
256
				 const struct cache_entry *ce,
257
				 const struct object_id *oid, int oid_valid,
258
				 unsigned int mode,
259
				 unsigned dirty_submodule)
Junio C Hamano's avatar
Junio C Hamano committed
260
{
261
	diff_addremove(&revs->diffopt, prefix[0], mode,
262
		       oid, oid_valid, ce->name, dirty_submodule);
Junio C Hamano's avatar
Junio C Hamano committed
263 264
}

265
static int get_stat_data(const struct cache_entry *ce,
266
			 const struct object_id **oidp,
Junio C Hamano's avatar
Junio C Hamano committed
267
			 unsigned int *modep,
268
			 int cached, int match_missing,
269
			 unsigned *dirty_submodule, struct diff_options *diffopt)
Junio C Hamano's avatar
Junio C Hamano committed
270
{
271
	const struct object_id *oid = &ce->oid;
Junio C Hamano's avatar
Junio C Hamano committed
272 273
	unsigned int mode = ce->ce_mode;

274
	if (!cached && !ce_uptodate(ce)) {
Junio C Hamano's avatar
Junio C Hamano committed
275 276
		int changed;
		struct stat st;
277
		changed = check_removed(ce, &st);
278 279 280 281
		if (changed < 0)
			return -1;
		else if (changed) {
			if (match_missing) {
282
				*oidp = oid;
Junio C Hamano's avatar
Junio C Hamano committed
283 284 285 286 287
				*modep = mode;
				return 0;
			}
			return -1;
		}
288 289
		changed = match_stat_with_submodule(diffopt, ce, &st,
						    0, dirty_submodule);
Junio C Hamano's avatar
Junio C Hamano committed
290
		if (changed) {
291
			mode = ce_mode_from_stat(ce, st.st_mode);
292
			oid = &null_oid;
Junio C Hamano's avatar
Junio C Hamano committed
293 294 295
		}
	}

296
	*oidp = oid;
Junio C Hamano's avatar
Junio C Hamano committed
297 298 299 300
	*modep = mode;
	return 0;
}

301
static void show_new_file(struct rev_info *revs,
302
			  const struct cache_entry *new,
Junio C Hamano's avatar
Junio C Hamano committed
303 304
			  int cached, int match_missing)
{
305
	const struct object_id *oid;
Junio C Hamano's avatar
Junio C Hamano committed
306
	unsigned int mode;
307
	unsigned dirty_submodule = 0;
Junio C Hamano's avatar
Junio C Hamano committed
308

309 310
	/*
	 * New file in the index: it might actually be different in
311
	 * the working tree.
Junio C Hamano's avatar
Junio C Hamano committed
312
	 */
313
	if (get_stat_data(new, &oid, &mode, cached, match_missing,
314
	    &dirty_submodule, &revs->diffopt) < 0)
Junio C Hamano's avatar
Junio C Hamano committed
315 316
		return;

317
	diff_index_show_file(revs, "+", new, oid, !is_null_oid(oid), mode, dirty_submodule);
Junio C Hamano's avatar
Junio C Hamano committed
318 319
}

320
static int show_modified(struct rev_info *revs,
321 322
			 const struct cache_entry *old,
			 const struct cache_entry *new,
Junio C Hamano's avatar
Junio C Hamano committed
323 324 325 326
			 int report_missing,
			 int cached, int match_missing)
{
	unsigned int mode, oldmode;
327
	const struct object_id *oid;
328
	unsigned dirty_submodule = 0;
Junio C Hamano's avatar
Junio C Hamano committed
329

330
	if (get_stat_data(new, &oid, &mode, cached, match_missing,
331
			  &dirty_submodule, &revs->diffopt) < 0) {
Junio C Hamano's avatar
Junio C Hamano committed
332 333
		if (report_missing)
			diff_index_show_file(revs, "-", old,
334
					     &old->oid, 1, old->ce_mode,
335
					     0);
Junio C Hamano's avatar
Junio C Hamano committed
336 337 338
		return -1;
	}

339
	if (revs->combine_merges && !cached &&
340
	    (oidcmp(oid, &old->oid) || oidcmp(&old->oid, &new->oid))) {
341 342 343 344 345 346 347 348
		struct combine_diff_path *p;
		int pathlen = ce_namelen(new);

		p = xmalloc(combine_diff_path_size(2, pathlen));
		p->path = (char *) &p->parent[2];
		p->next = NULL;
		memcpy(p->path, new->name, pathlen);
		p->path[pathlen] = 0;
349
		p->mode = mode;
350
		oidclr(&p->oid);
351 352
		memset(p->parent, 0, 2 * sizeof(struct combine_diff_parent));
		p->parent[0].status = DIFF_STATUS_MODIFIED;
353
		p->parent[0].mode = new->ce_mode;
354
		oidcpy(&p->parent[0].oid, &new->oid);
355
		p->parent[1].status = DIFF_STATUS_MODIFIED;
356
		p->parent[1].mode = old->ce_mode;
357
		oidcpy(&p->parent[1].oid, &old->oid);
358 359 360 361 362
		show_combined_diff(p, 2, revs->dense_combined_merges, revs);
		free(p);
		return 0;
	}

Junio C Hamano's avatar
Junio C Hamano committed
363
	oldmode = old->ce_mode;
364
	if (mode == oldmode && !oidcmp(oid, &old->oid) && !dirty_submodule &&
365
	    !DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER))
Junio C Hamano's avatar
Junio C Hamano committed
366 367 368
		return 0;

	diff_change(&revs->diffopt, oldmode, mode,
369
		    &old->oid, oid, 1, !is_null_oid(oid),
370
		    old->name, 0, dirty_submodule);
Junio C Hamano's avatar
Junio C Hamano committed
371 372 373
	return 0;
}

374 375 376 377 378 379 380
/*
 * This gets a mix of an existing index and a tree, one pathname entry
 * at a time. The index entry may be a single stage-0 one, but it could
 * also be multiple unmerged entries (in which case idx_pos/idx_nr will
 * give you the position and number of entries in the index).
 */
static void do_oneway_diff(struct unpack_trees_options *o,
381 382
			   const struct cache_entry *idx,
			   const struct cache_entry *tree)
Junio C Hamano's avatar
Junio C Hamano committed
383
{
384
	struct rev_info *revs = o->unpack_data;
385
	int match_missing, cached;
386

387 388 389 390 391 392 393 394
	/* i-t-a entries do not actually exist in the index */
	if (revs->diffopt.ita_invisible_in_index &&
	    idx && ce_intent_to_add(idx)) {
		idx = NULL;
		if (!tree)
			return;	/* nothing to diff.. */
	}

395
	/* if the entry is not checked out, don't examine work tree */
396 397
	cached = o->index_only ||
		(idx && ((idx->ce_flags & CE_VALID) || ce_skip_worktree(idx)));
Junio C Hamano's avatar
Junio C Hamano committed
398
	/*
399
	 * Backward compatibility wart - "diff-index -m" does
400 401 402 403 404 405 406 407
	 * not mean "do not ignore merges", but "match_missing".
	 *
	 * But with the revision flag parsing, that's found in
	 * "!revs->ignore_merges".
	 */
	match_missing = !revs->ignore_merges;

	if (cached && idx && ce_stage(idx)) {
408 409
		struct diff_filepair *pair;
		pair = diff_unmerge(&revs->diffopt, idx->name);
410
		if (tree)
411
			fill_filespec(pair->one, &tree->oid, 1,
412
				      tree->ce_mode);
413 414 415 416 417 418 419
		return;
	}

	/*
	 * Something added to the tree?
	 */
	if (!tree) {
420
		show_new_file(revs, idx, cached, match_missing);
421 422 423 424 425
		return;
	}

	/*
	 * Something removed from the tree?
426
	 */
427
	if (!idx) {
428
		diff_index_show_file(revs, "-", tree, &tree->oid, 1,
429
				     tree->ce_mode, 0);
430 431 432 433
		return;
	}

	/* Show difference between old and new */
434
	show_modified(revs, tree, idx, 1, cached, match_missing);
435 436 437 438 439 440 441 442 443 444 445
}

/*
 * The unpack_trees() interface is designed for merging, so
 * the different source entries are designed primarily for
 * the source trees, with the old index being really mainly
 * used for being replaced by the result.
 *
 * For diffing, the index is more important, and we only have a
 * single tree.
 *
446
 * We're supposed to advance o->pos to skip what we have already processed.
447 448 449 450 451
 *
 * This wrapper makes it all more readable, and takes care of all
 * the fairly complex unpack_trees() semantic requirements, including
 * the skipping, the path matching, the type conflict cases etc.
 */
452 453
static int oneway_diff(const struct cache_entry * const *src,
		       struct unpack_trees_options *o)
454
{
455 456
	const struct cache_entry *idx = src[0];
	const struct cache_entry *tree = src[1];
457
	struct rev_info *revs = o->unpack_data;
458 459 460 461 462 463 464 465 466 467

	/*
	 * Unpack-trees generates a DF/conflict entry if
	 * there was a directory in the index and a tree
	 * in the tree. From a diff standpoint, that's a
	 * delete of the tree and a create of the file.
	 */
	if (tree == o->df_conflict_entry)
		tree = NULL;

468
	if (ce_path_match(idx ? idx : tree, &revs->prune_data, NULL)) {
469
		do_oneway_diff(o, idx, tree);
470 471 472 473 474
		if (diff_can_quit_early(&revs->diffopt)) {
			o->exiting_early = 1;
			return -1;
		}
	}
475

476
	return 0;
477 478
}

479
static int diff_cache(struct rev_info *revs,
480
		      const struct object_id *tree_oid,
481 482
		      const char *tree_name,
		      int cached)
483 484 485
{
	struct tree *tree;
	struct tree_desc t;
486
	struct unpack_trees_options opts;
Junio C Hamano's avatar
Junio C Hamano committed
487

488
	tree = parse_tree_indirect(tree_oid);
Junio C Hamano's avatar
Junio C Hamano committed
489
	if (!tree)
490
		return error("bad tree object %s",
491
			     tree_name ? tree_name : oid_to_hex(tree_oid));
492 493 494
	memset(&opts, 0, sizeof(opts));
	opts.head_idx = 1;
	opts.index_only = cached;
495 496
	opts.diff_index_cached = (cached &&
				  !DIFF_OPT_TST(&revs->diffopt, FIND_COPIES_HARDER));
497 498
	opts.merge = 1;
	opts.fn = oneway_diff;
499
	opts.unpack_data = revs;
500 501
	opts.src_index = &the_index;
	opts.dst_index = NULL;
502
	opts.pathspec = &revs->diffopt.pathspec;
503
	opts.pathspec->recursive = 1;
504 505

	init_tree_desc(&t, tree->buffer, tree->size);
506 507 508 509 510 511 512 513
	return unpack_trees(1, &t, &opts);
}

int run_diff_index(struct rev_info *revs, int cached)
{
	struct object_array_entry *ent;

	ent = revs->pending.objects;
514
	if (diff_cache(revs, &ent->item->oid, ent->name, cached))
515
		exit(128);
516

517
	diff_set_mnemonic_prefix(&revs->diffopt, "c/", cached ? "i/" : "w/");
518
	diffcore_fix_diff_index(&revs->diffopt);
Junio C Hamano's avatar
Junio C Hamano committed
519 520
	diffcore_std(&revs->diffopt);
	diff_flush(&revs->diffopt);
521
	return 0;
Junio C Hamano's avatar
Junio C Hamano committed
522
}
523

524
int do_diff_cache(const struct object_id *tree_oid, struct diff_options *opt)
525 526 527 528
{
	struct rev_info revs;

	init_revisions(&revs, NULL);
529
	copy_pathspec(&revs.prune_data, &opt->pathspec);
530
	revs.diffopt = *opt;
531

532
	if (diff_cache(&revs, tree_oid, NULL, 1))
533
		exit(128);
534
	return 0;
535
}
536

537 538
int index_differs_from(const char *def, int diff_flags,
		       int ita_invisible_in_index)
539 540
{
	struct rev_info rev;
541
	struct setup_revision_opt opt;
542 543

	init_revisions(&rev, NULL);
544 545 546
	memset(&opt, 0, sizeof(opt));
	opt.def = def;
	setup_revisions(0, NULL, &rev, &opt);
547
	DIFF_OPT_SET(&rev.diffopt, QUICK);
548 549
	DIFF_OPT_SET(&rev.diffopt, EXIT_WITH_STATUS);
	rev.diffopt.flags |= diff_flags;
550
	rev.diffopt.ita_invisible_in_index = ita_invisible_in_index;
551 552 553 554 555
	run_diff_index(&rev, 1);
	if (rev.pending.alloc)
		free(rev.pending.objects);
	return (DIFF_OPT_TST(&rev.diffopt, HAS_CHANGES) != 0);
}