diff.c 12.0 KB
Newer Older
J
Junio C Hamano 已提交
1 2 3 4 5 6
/*
 * Builtin "git diff"
 *
 * Copyright (c) 2006 Junio C Hamano
 */
#include "cache.h"
7
#include "lockfile.h"
8
#include "color.h"
J
Junio C Hamano 已提交
9 10 11 12 13 14 15 16
#include "commit.h"
#include "blob.h"
#include "tag.h"
#include "diff.h"
#include "diffcore.h"
#include "revision.h"
#include "log-tree.h"
#include "builtin.h"
17
#include "submodule.h"
18
#include "sha1-array.h"
J
Junio C Hamano 已提交
19

20 21 22
#define DIFF_NO_INDEX_EXPLICIT 1
#define DIFF_NO_INDEX_IMPLICIT 2

J
Junio C Hamano 已提交
23 24 25
struct blobinfo {
	unsigned char sha1[20];
	const char *name;
26
	unsigned mode;
J
Junio C Hamano 已提交
27 28 29
};

static const char builtin_diff_usage[] =
30
"git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
J
Junio C Hamano 已提交
31 32 33 34 35

static void stuff_change(struct diff_options *opt,
			 unsigned old_mode, unsigned new_mode,
			 const unsigned char *old_sha1,
			 const unsigned char *new_sha1,
36 37
			 int old_sha1_valid,
			 int new_sha1_valid,
J
Junio C Hamano 已提交
38 39 40 41 42
			 const char *old_name,
			 const char *new_name)
{
	struct diff_filespec *one, *two;

D
David Rientjes 已提交
43
	if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
44
	    !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode))
J
Junio C Hamano 已提交
45 46
		return;

47
	if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
J
Junio C Hamano 已提交
48
		unsigned tmp;
P
Peter Hagervall 已提交
49
		const unsigned char *tmp_u;
J
Junio C Hamano 已提交
50 51 52 53 54
		const char *tmp_c;
		tmp = old_mode; old_mode = new_mode; new_mode = tmp;
		tmp_u = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_u;
		tmp_c = old_name; old_name = new_name; new_name = tmp_c;
	}
55 56 57 58 59 60

	if (opt->prefix &&
	    (strncmp(old_name, opt->prefix, opt->prefix_length) ||
	     strncmp(new_name, opt->prefix, opt->prefix_length)))
		return;

J
Junio C Hamano 已提交
61 62
	one = alloc_filespec(old_name);
	two = alloc_filespec(new_name);
63 64
	fill_filespec(one, old_sha1, old_sha1_valid, old_mode);
	fill_filespec(two, new_sha1, new_sha1_valid, new_mode);
J
Junio C Hamano 已提交
65 66 67 68 69 70

	diff_queue(&diff_queued_diff, one, two);
}

static int builtin_diff_b_f(struct rev_info *revs,
			    int argc, const char **argv,
71
			    struct blobinfo *blob)
J
Junio C Hamano 已提交
72 73 74
{
	/* Blob vs file in the working tree*/
	struct stat st;
75
	const char *path;
J
Junio C Hamano 已提交
76

77 78 79
	if (argc > 1)
		usage(builtin_diff_usage);

80 81 82
	GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
	path = revs->prune_data.items[0].match;

J
Junio C Hamano 已提交
83
	if (lstat(path, &st))
84
		die_errno(_("failed to stat '%s'"), path);
J
Junio C Hamano 已提交
85
	if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
86
		die(_("'%s': not a regular file or symlink"), path);
87

88 89
	diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");

90 91 92
	if (blob[0].mode == S_IFINVALID)
		blob[0].mode = canon_mode(st.st_mode);

J
Junio C Hamano 已提交
93
	stuff_change(&revs->diffopt,
94
		     blob[0].mode, canon_mode(st.st_mode),
J
Junio C Hamano 已提交
95
		     blob[0].sha1, null_sha1,
96
		     1, 0,
97
		     path, path);
J
Junio C Hamano 已提交
98 99 100 101 102 103 104 105 106 107 108
	diffcore_std(&revs->diffopt);
	diff_flush(&revs->diffopt);
	return 0;
}

static int builtin_diff_blobs(struct rev_info *revs,
			      int argc, const char **argv,
			      struct blobinfo *blob)
{
	unsigned mode = canon_mode(S_IFREG | 0644);

109 110 111
	if (argc > 1)
		usage(builtin_diff_usage);

112 113 114 115 116 117
	if (blob[0].mode == S_IFINVALID)
		blob[0].mode = mode;

	if (blob[1].mode == S_IFINVALID)
		blob[1].mode = mode;

J
Junio C Hamano 已提交
118
	stuff_change(&revs->diffopt,
119
		     blob[0].mode, blob[1].mode,
120
		     blob[0].sha1, blob[1].sha1,
121
		     1, 1,
122
		     blob[0].name, blob[1].name);
J
Junio C Hamano 已提交
123 124 125 126 127 128 129 130 131 132 133
	diffcore_std(&revs->diffopt);
	diff_flush(&revs->diffopt);
	return 0;
}

static int builtin_diff_index(struct rev_info *revs,
			      int argc, const char **argv)
{
	int cached = 0;
	while (1 < argc) {
		const char *arg = argv[1];
134
		if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
J
Junio C Hamano 已提交
135 136 137 138 139 140 141 142 143
			cached = 1;
		else
			usage(builtin_diff_usage);
		argv++; argc--;
	}
	/*
	 * Make sure there is one revision (i.e. pending object),
	 * and there is no revision filtering parameters.
	 */
144
	if (revs->pending.nr != 1 ||
J
Junio C Hamano 已提交
145 146 147
	    revs->max_count != -1 || revs->min_age != -1 ||
	    revs->max_age != -1)
		usage(builtin_diff_usage);
148 149
	if (!cached) {
		setup_work_tree();
150
		if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
151 152 153 154 155
			perror("read_cache_preload");
			return -1;
		}
	} else if (read_cache() < 0) {
		perror("read_cache");
156 157
		return -1;
	}
J
Junio C Hamano 已提交
158 159 160 161 162
	return run_diff_index(revs, cached);
}

static int builtin_diff_tree(struct rev_info *revs,
			     int argc, const char **argv,
163 164
			     struct object_array_entry *ent0,
			     struct object_array_entry *ent1)
J
Junio C Hamano 已提交
165 166
{
	const unsigned char *(sha1[2]);
167
	int swap = 0;
168 169 170

	if (argc > 1)
		usage(builtin_diff_usage);
171

172 173 174
	/*
	 * We saw two trees, ent0 and ent1.  If ent1 is uninteresting,
	 * swap them.
175
	 */
176
	if (ent1->item->flags & UNINTERESTING)
177
		swap = 1;
178
	sha1[swap] = ent0->item->sha1;
F
Felipe Contreras 已提交
179
	sha1[1 - swap] = ent1->item->sha1;
J
Junio C Hamano 已提交
180 181 182 183 184
	diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
	log_tree_diff_flush(revs);
	return 0;
}

185 186
static int builtin_diff_combined(struct rev_info *revs,
				 int argc, const char **argv,
187
				 struct object_array_entry *ent,
188 189
				 int ents)
{
190
	struct sha1_array parents = SHA1_ARRAY_INIT;
191 192
	int i;

193 194 195
	if (argc > 1)
		usage(builtin_diff_usage);

196 197
	if (!revs->dense_combined_merges && !revs->combine_merges)
		revs->dense_combined_merges = revs->combine_merges = 1;
198 199 200
	for (i = 1; i < ents; i++)
		sha1_array_append(&parents, ent[i].item->sha1);
	diff_tree_combined(ent[0].item->sha1, &parents,
201
			   revs->dense_combined_merges, revs);
202
	sha1_array_clear(&parents);
203 204 205
	return 0;
}

206 207 208 209 210 211 212 213 214 215 216 217
static void refresh_index_quietly(void)
{
	struct lock_file *lock_file;
	int fd;

	lock_file = xcalloc(1, sizeof(struct lock_file));
	fd = hold_locked_index(lock_file, 0);
	if (fd < 0)
		return;
	discard_cache();
	read_cache();
	refresh_cache(REFRESH_QUIET|REFRESH_UNMERGED);
218
	update_index_if_able(&the_index, lock_file);
219 220
}

221 222 223 224 225 226 227 228 229 230 231 232 233
static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv)
{
	unsigned int options = 0;

	while (1 < argc && argv[1][0] == '-') {
		if (!strcmp(argv[1], "--base"))
			revs->max_count = 1;
		else if (!strcmp(argv[1], "--ours"))
			revs->max_count = 2;
		else if (!strcmp(argv[1], "--theirs"))
			revs->max_count = 3;
		else if (!strcmp(argv[1], "-q"))
			options |= DIFF_SILENT_ON_REMOVED;
234 235
		else if (!strcmp(argv[1], "-h"))
			usage(builtin_diff_usage);
236
		else
237
			return error(_("invalid option: %s"), argv[1]);
238 239 240
		argv++; argc--;
	}

241 242 243 244 245 246 247
	/*
	 * "diff --base" should not combine merges because it was not
	 * asked to.  "diff -c" should not densify (if the user wants
	 * dense one, --cc can be explicitly asked for, or just rely
	 * on the default).
	 */
	if (revs->max_count == -1 && !revs->combine_merges &&
248 249 250
	    (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
		revs->combine_merges = revs->dense_combined_merges = 1;

251
	setup_work_tree();
252
	if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
L
Linus Torvalds 已提交
253
		perror("read_cache_preload");
254 255
		return -1;
	}
256
	return run_diff_files(revs, options);
257 258
}

259
int cmd_diff(int argc, const char **argv, const char *prefix)
J
Junio C Hamano 已提交
260
{
261
	int i;
J
Junio C Hamano 已提交
262
	struct rev_info rev;
263 264
	struct object_array ent = OBJECT_ARRAY_INIT;
	int blobs = 0, paths = 0;
J
Junio C Hamano 已提交
265
	struct blobinfo blob[2];
266
	int nongit = 0, no_index = 0;
267
	int result = 0;
J
Junio C Hamano 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

	/*
	 * We could get N tree-ish in the rev.pending_objects list.
	 * Also there could be M blobs there, and P pathspecs.
	 *
	 * N=0, M=0:
	 *	cache vs files (diff-files)
	 * N=0, M=2:
	 *      compare two random blobs.  P must be zero.
	 * N=0, M=1, P=1:
	 *	compare a blob with a working tree file.
	 *
	 * N=1, M=0:
	 *      tree vs cache (diff-index --cached)
	 *
	 * N=2, M=0:
	 *      tree vs tree (diff-tree)
	 *
286 287 288
	 * N=0, M=0, P=2:
	 *      compare two filesystem entities (aka --no-index).
	 *
J
Junio C Hamano 已提交
289 290
	 * Other cases are errors.
	 */
J
Junio C Hamano 已提交
291

292 293 294 295 296 297 298 299 300 301 302 303
	/* Were we asked to do --no-index explicitly? */
	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "--")) {
			i++;
			break;
		}
		if (!strcmp(argv[i], "--no-index"))
			no_index = DIFF_NO_INDEX_EXPLICIT;
		if (argv[i][0] != '-')
			break;
	}

304 305 306
	if (!no_index)
		prefix = setup_git_directory_gently(&nongit);

307 308 309 310 311 312 313 314 315 316 317 318
	/*
	 * Treat git diff with at least one path outside of the
	 * repo the same as if the command would have been executed
	 * outside of a git repository.  In this case it behaves
	 * the same way as "git diff --no-index <a> <b>", which acts
	 * as a colourful "diff" replacement.
	 */
	if (nongit || ((argc == i + 2) &&
		       (!path_inside_repo(prefix, argv[i]) ||
			!path_inside_repo(prefix, argv[i + 1]))))
		no_index = DIFF_NO_INDEX_IMPLICIT;

319 320
	if (!no_index)
		gitmodules_config();
321
	git_config(git_diff_ui_config, NULL);
322

323
	init_revisions(&rev, prefix);
324

T
Thomas Gummerer 已提交
325 326 327 328 329 330 331 332 333 334
	if (no_index && argc != i + 2) {
		if (no_index == DIFF_NO_INDEX_IMPLICIT) {
			/*
			 * There was no --no-index and there were not two
			 * paths. It is possible that the user intended
			 * to do an inside-repository operation.
			 */
			fprintf(stderr, "Not a git repository\n");
			fprintf(stderr,
				"To compare two paths outside a working tree:\n");
335
		}
T
Thomas Gummerer 已提交
336 337 338 339 340 341 342
		/* Give the usage message for non-repository usage and exit. */
		usagef("git diff %s <path> <path>",
		       no_index == DIFF_NO_INDEX_EXPLICIT ?
		       "--no-index" : "[--no-index]");

	}
	if (no_index)
343 344
		/* If this is a no-index diff, just run it and exit there. */
		diff_no_index(&rev, argc, argv, prefix);
345 346

	/* Otherwise, we are doing the usual "git" diff */
347
	rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
J
Junio C Hamano 已提交
348

349
	/* Scale to real terminal size and respect statGraphWidth config */
350
	rev.diffopt.stat_width = -1;
351
	rev.diffopt.stat_graph_width = -1;
352

J
Jeff King 已提交
353
	/* Default to let external and textconv be used */
354
	DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
J
Jeff King 已提交
355
	DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
356

357
	if (nongit)
358
		die(_("Not a git repository"));
359
	argc = setup_revisions(argc, argv, &rev, NULL);
360
	if (!rev.diffopt.output_format) {
361
		rev.diffopt.output_format = DIFF_FORMAT_PATCH;
T
Thomas Rast 已提交
362
		diff_setup_done(&rev.diffopt);
363
	}
364

365
	DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
366

J
Jeff King 已提交
367
	setup_diff_pager(&rev.diffopt);
368

369 370
	/*
	 * Do we have --cached and not have a pending object, then
J
Junio C Hamano 已提交
371 372
	 * default to HEAD by hand.  Eek.
	 */
373
	if (!rev.pending.nr) {
J
Junio C Hamano 已提交
374 375 376 377 378
		int i;
		for (i = 1; i < argc; i++) {
			const char *arg = argv[i];
			if (!strcmp(arg, "--"))
				break;
379 380
			else if (!strcmp(arg, "--cached") ||
				 !strcmp(arg, "--staged")) {
381
				add_head_to_pending(&rev);
382 383
				if (!rev.pending.nr) {
					struct tree *tree;
384
					tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
385 386
					add_pending_object(&rev, &tree->object, "HEAD");
				}
J
Junio C Hamano 已提交
387 388 389 390 391
				break;
			}
		}
	}

392
	for (i = 0; i < rev.pending.nr; i++) {
393 394 395
		struct object_array_entry *entry = &rev.pending.objects[i];
		struct object *obj = entry->item;
		const char *name = entry->name;
J
Junio C Hamano 已提交
396 397 398 399 400
		int flags = (obj->flags & UNINTERESTING);
		if (!obj->parsed)
			obj = parse_object(obj->sha1);
		obj = deref_tag(obj, NULL, 0);
		if (!obj)
401
			die(_("invalid object '%s' given."), name);
402
		if (obj->type == OBJ_COMMIT)
J
Junio C Hamano 已提交
403
			obj = &((struct commit *)obj)->tree->object;
404

405
		if (obj->type == OBJ_TREE) {
J
Junio C Hamano 已提交
406
			obj->flags |= flags;
407
			add_object_array(obj, name, &ent);
408
		} else if (obj->type == OBJ_BLOB) {
J
Junio C Hamano 已提交
409
			if (2 <= blobs)
410
				die(_("more than two blobs given: '%s'"), name);
411
			hashcpy(blob[blobs].sha1, obj->sha1);
J
Junio C Hamano 已提交
412
			blob[blobs].name = name;
413
			blob[blobs].mode = entry->mode;
J
Junio C Hamano 已提交
414
			blobs++;
J
Junio C Hamano 已提交
415

416 417
		} else {
			die(_("unhandled object '%s' given."), name);
J
Junio C Hamano 已提交
418 419
		}
	}
420
	if (rev.prune_data.nr)
421
		paths += rev.prune_data.nr;
J
Junio C Hamano 已提交
422 423 424 425

	/*
	 * Now, do the arguments look reasonable?
	 */
426
	if (!ent.nr) {
J
Junio C Hamano 已提交
427 428
		switch (blobs) {
		case 0:
429
			result = builtin_diff_files(&rev, argc, argv);
J
Junio C Hamano 已提交
430 431 432 433
			break;
		case 1:
			if (paths != 1)
				usage(builtin_diff_usage);
434
			result = builtin_diff_b_f(&rev, argc, argv, blob);
J
Junio C Hamano 已提交
435 436
			break;
		case 2:
437 438
			if (paths)
				usage(builtin_diff_usage);
439
			result = builtin_diff_blobs(&rev, argc, argv, blob);
J
Junio C Hamano 已提交
440 441 442 443 444 445 446
			break;
		default:
			usage(builtin_diff_usage);
		}
	}
	else if (blobs)
		usage(builtin_diff_usage);
447
	else if (ent.nr == 1)
448
		result = builtin_diff_index(&rev, argc, argv);
449 450 451 452
	else if (ent.nr == 2)
		result = builtin_diff_tree(&rev, argc, argv,
					   &ent.objects[0], &ent.objects[1]);
	else if (ent.objects[0].item->flags & UNINTERESTING) {
453 454
		/*
		 * diff A...B where there is at least one merge base
455 456 457 458 459
		 * between A and B.  We have ent.objects[0] ==
		 * merge-base, ent.objects[ents-2] == A, and
		 * ent.objects[ents-1] == B.  Show diff between the
		 * base and B.  Note that we pick one merge base at
		 * random if there are more than one.
460
		 */
461 462 463
		result = builtin_diff_tree(&rev, argc, argv,
					   &ent.objects[0],
					   &ent.objects[ent.nr-1]);
464
	} else
465
		result = builtin_diff_combined(&rev, argc, argv,
466
					       ent.objects, ent.nr);
J
Junio C Hamano 已提交
467
	result = diff_result_code(&rev.diffopt, result);
468 469
	if (1 < rev.diffopt.skip_stat_unmatch)
		refresh_index_quietly();
470
	return result;
J
Junio C Hamano 已提交
471
}