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
static const char builtin_diff_usage[] =
24
"git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
J
Junio C Hamano 已提交
25

26 27 28 29 30
static const char *blob_path(struct object_array_entry *entry)
{
	return entry->path ? entry->path : entry->name;
}

J
Junio C Hamano 已提交
31 32
static void stuff_change(struct diff_options *opt,
			 unsigned old_mode, unsigned new_mode,
33 34 35 36
			 const struct object_id *old_oid,
			 const struct object_id *new_oid,
			 int old_oid_valid,
			 int new_oid_valid,
37 38
			 const char *old_path,
			 const char *new_path)
J
Junio C Hamano 已提交
39 40 41
{
	struct diff_filespec *one, *two;

42 43
	if (!is_null_oid(old_oid) && !is_null_oid(new_oid) &&
	    !oidcmp(old_oid, new_oid) && (old_mode == new_mode))
J
Junio C Hamano 已提交
44 45
		return;

46
	if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
R
René Scharfe 已提交
47
		SWAP(old_mode, new_mode);
48
		SWAP(old_oid, new_oid);
49
		SWAP(old_path, new_path);
J
Junio C Hamano 已提交
50
	}
51 52

	if (opt->prefix &&
53 54
	    (strncmp(old_path, opt->prefix, opt->prefix_length) ||
	     strncmp(new_path, opt->prefix, opt->prefix_length)))
55 56
		return;

57 58
	one = alloc_filespec(old_path);
	two = alloc_filespec(new_path);
59 60
	fill_filespec(one, old_oid->hash, old_oid_valid, old_mode);
	fill_filespec(two, new_oid->hash, new_oid_valid, new_mode);
J
Junio C Hamano 已提交
61 62 63 64 65 66

	diff_queue(&diff_queued_diff, one, two);
}

static int builtin_diff_b_f(struct rev_info *revs,
			    int argc, const char **argv,
67
			    struct object_array_entry **blob)
J
Junio C Hamano 已提交
68 69 70
{
	/* Blob vs file in the working tree*/
	struct stat st;
71
	const char *path;
J
Junio C Hamano 已提交
72

73 74 75
	if (argc > 1)
		usage(builtin_diff_usage);

76 77 78
	GUARD_PATHSPEC(&revs->prune_data, PATHSPEC_FROMTOP | PATHSPEC_LITERAL);
	path = revs->prune_data.items[0].match;

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

84 85
	diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");

86 87
	if (blob[0]->mode == S_IFINVALID)
		blob[0]->mode = canon_mode(st.st_mode);
88

J
Junio C Hamano 已提交
89
	stuff_change(&revs->diffopt,
90 91
		     blob[0]->mode, canon_mode(st.st_mode),
		     &blob[0]->item->oid, &null_oid,
92
		     1, 0,
93 94
		     blob[0]->path ? blob[0]->path : path,
		     path);
J
Junio C Hamano 已提交
95 96 97 98 99 100 101
	diffcore_std(&revs->diffopt);
	diff_flush(&revs->diffopt);
	return 0;
}

static int builtin_diff_blobs(struct rev_info *revs,
			      int argc, const char **argv,
102
			      struct object_array_entry **blob)
J
Junio C Hamano 已提交
103 104 105
{
	unsigned mode = canon_mode(S_IFREG | 0644);

106 107 108
	if (argc > 1)
		usage(builtin_diff_usage);

109 110
	if (blob[0]->mode == S_IFINVALID)
		blob[0]->mode = mode;
111

112 113
	if (blob[1]->mode == S_IFINVALID)
		blob[1]->mode = mode;
114

J
Junio C Hamano 已提交
115
	stuff_change(&revs->diffopt,
116 117
		     blob[0]->mode, blob[1]->mode,
		     &blob[0]->item->oid, &blob[1]->item->oid,
118
		     1, 1,
119
		     blob_path(blob[0]), blob_path(blob[1]));
J
Junio C Hamano 已提交
120 121 122 123 124 125 126 127 128 129 130
	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];
131
		if (!strcmp(arg, "--cached") || !strcmp(arg, "--staged"))
J
Junio C Hamano 已提交
132 133 134 135 136 137 138 139 140
			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.
	 */
141
	if (revs->pending.nr != 1 ||
J
Junio C Hamano 已提交
142 143 144
	    revs->max_count != -1 || revs->min_age != -1 ||
	    revs->max_age != -1)
		usage(builtin_diff_usage);
145 146
	if (!cached) {
		setup_work_tree();
147
		if (read_cache_preload(&revs->diffopt.pathspec) < 0) {
148 149 150 151 152
			perror("read_cache_preload");
			return -1;
		}
	} else if (read_cache() < 0) {
		perror("read_cache");
153 154
		return -1;
	}
J
Junio C Hamano 已提交
155 156 157 158 159
	return run_diff_index(revs, cached);
}

static int builtin_diff_tree(struct rev_info *revs,
			     int argc, const char **argv,
160 161
			     struct object_array_entry *ent0,
			     struct object_array_entry *ent1)
J
Junio C Hamano 已提交
162
{
163
	const struct object_id *(oid[2]);
164
	int swap = 0;
165 166 167

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

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

182 183
static int builtin_diff_combined(struct rev_info *revs,
				 int argc, const char **argv,
184
				 struct object_array_entry *ent,
185 186
				 int ents)
{
187
	struct oid_array parents = OID_ARRAY_INIT;
188 189
	int i;

190 191 192
	if (argc > 1)
		usage(builtin_diff_usage);

193 194
	if (!revs->dense_combined_merges && !revs->combine_merges)
		revs->dense_combined_merges = revs->combine_merges = 1;
195
	for (i = 1; i < ents; i++)
196
		oid_array_append(&parents, &ent[i].item->oid);
B
brian m. carlson 已提交
197
	diff_tree_combined(ent[0].item->oid.hash, &parents,
198
			   revs->dense_combined_merges, revs);
199
	oid_array_clear(&parents);
200 201 202
	return 0;
}

203 204 205 206 207 208 209 210 211 212 213 214
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);
215
	update_index_if_able(&the_index, lock_file);
216 217
}

218 219 220 221 222 223 224 225 226 227 228 229 230
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;
231 232
		else if (!strcmp(argv[1], "-h"))
			usage(builtin_diff_usage);
233
		else
234
			return error(_("invalid option: %s"), argv[1]);
235 236 237
		argv++; argc--;
	}

238 239 240 241 242 243 244
	/*
	 * "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 &&
245 246 247
	    (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
		revs->combine_merges = revs->dense_combined_merges = 1;

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

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

	/*
	 * 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)
	 *
283 284 285
	 * N=0, M=0, P=2:
	 *      compare two filesystem entities (aka --no-index).
	 *
J
Junio C Hamano 已提交
286 287
	 * Other cases are errors.
	 */
J
Junio C Hamano 已提交
288

289 290 291 292 293 294 295 296 297 298 299 300
	/* 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;
	}

301
	prefix = setup_git_directory_gently(&nongit);
302

303
	if (!no_index) {
304 305 306 307 308 309 310 311 312 313 314 315
		/*
		 * 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;
	}
316

317 318
	if (!no_index)
		gitmodules_config();
319
	init_diff_ui_defaults();
320
	git_config(git_diff_ui_config, NULL);
321
	precompose_argv(argc, argv);
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
		/* If this is a no-index diff, just run it and exit there. */
344
		diff_no_index(&rev, argc, argv);
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
		int flags = (obj->flags & UNINTERESTING);
		if (!obj->parsed)
B
brian m. carlson 已提交
398
			obj = parse_object(obj->oid.hash);
J
Junio C Hamano 已提交
399 400
		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
			blob[blobs] = entry;
J
Junio C Hamano 已提交
412
			blobs++;
J
Junio C Hamano 已提交
413

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

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