diff.c 10.6 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 "color.h"
J
Junio C Hamano 已提交
8 9 10 11 12 13 14 15
#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"
16
#include "submodule.h"
17
#include "sha1-array.h"
J
Junio C Hamano 已提交
18 19 20 21

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

static const char builtin_diff_usage[] =
26
"git diff [<options>] [<commit> [<commit>]] [--] [<path>...]";
J
Junio C Hamano 已提交
27 28 29 30 31

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

D
David Rientjes 已提交
39
	if (!is_null_sha1(old_sha1) && !is_null_sha1(new_sha1) &&
40
	    !hashcmp(old_sha1, new_sha1) && (old_mode == new_mode))
J
Junio C Hamano 已提交
41 42
		return;

43
	if (DIFF_OPT_TST(opt, REVERSE_DIFF)) {
J
Junio C Hamano 已提交
44
		unsigned tmp;
P
Peter Hagervall 已提交
45
		const unsigned char *tmp_u;
J
Junio C Hamano 已提交
46 47 48 49 50
		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;
	}
51 52 53 54 55 56

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

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

	diff_queue(&diff_queued_diff, one, two);
}

static int builtin_diff_b_f(struct rev_info *revs,
			    int argc, const char **argv,
			    struct blobinfo *blob,
			    const char *path)
{
	/* Blob vs file in the working tree*/
	struct stat st;

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

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

81 82
	diff_set_mnemonic_prefix(&revs->diffopt, "o/", "w/");

83 84 85
	if (blob[0].mode == S_IFINVALID)
		blob[0].mode = canon_mode(st.st_mode);

J
Junio C Hamano 已提交
86
	stuff_change(&revs->diffopt,
87
		     blob[0].mode, canon_mode(st.st_mode),
J
Junio C Hamano 已提交
88
		     blob[0].sha1, null_sha1,
89
		     1, 0,
90
		     path, path);
J
Junio C Hamano 已提交
91 92 93 94 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,
			      struct blobinfo *blob)
{
	unsigned mode = canon_mode(S_IFREG | 0644);

102 103 104
	if (argc > 1)
		usage(builtin_diff_usage);

105 106 107 108 109 110
	if (blob[0].mode == S_IFINVALID)
		blob[0].mode = mode;

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

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

static int builtin_diff_tree(struct rev_info *revs,
			     int argc, const char **argv,
156
			     struct object_array_entry *ent)
J
Junio C Hamano 已提交
157 158
{
	const unsigned char *(sha1[2]);
159
	int swap = 0;
160 161 162

	if (argc > 1)
		usage(builtin_diff_usage);
163 164

	/* We saw two trees, ent[0] and ent[1].
165
	 * if ent[1] is uninteresting, they are swapped
166
	 */
167 168
	if (ent[1].item->flags & UNINTERESTING)
		swap = 1;
J
Junio C Hamano 已提交
169 170 171 172 173 174 175
	sha1[swap] = ent[0].item->sha1;
	sha1[1-swap] = ent[1].item->sha1;
	diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
	log_tree_diff_flush(revs);
	return 0;
}

176 177
static int builtin_diff_combined(struct rev_info *revs,
				 int argc, const char **argv,
178
				 struct object_array_entry *ent,
179 180
				 int ents)
{
181
	struct sha1_array parents = SHA1_ARRAY_INIT;
182 183
	int i;

184 185 186
	if (argc > 1)
		usage(builtin_diff_usage);

187 188
	if (!revs->dense_combined_merges && !revs->combine_merges)
		revs->dense_combined_merges = revs->combine_merges = 1;
189 190 191
	for (i = 1; i < ents; i++)
		sha1_array_append(&parents, ent[i].item->sha1);
	diff_tree_combined(ent[0].item->sha1, &parents,
192
			   revs->dense_combined_merges, revs);
193
	sha1_array_clear(&parents);
194 195 196
	return 0;
}

197 198 199 200 201 202 203 204 205 206 207 208
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);
209
	update_index_if_able(&the_index, lock_file);
210 211
}

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

232 233 234 235 236 237 238
	/*
	 * "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 &&
239 240 241
	    (revs->diffopt.output_format & DIFF_FORMAT_PATCH))
		revs->combine_merges = revs->dense_combined_merges = 1;

242
	setup_work_tree();
243
	if (read_cache_preload(revs->diffopt.pathspec.raw) < 0) {
L
Linus Torvalds 已提交
244
		perror("read_cache_preload");
245 246
		return -1;
	}
247
	return run_diff_files(revs, options);
248 249
}

250
int cmd_diff(int argc, const char **argv, const char *prefix)
J
Junio C Hamano 已提交
251
{
252
	int i;
J
Junio C Hamano 已提交
253
	struct rev_info rev;
254
	struct object_array_entry ent[100];
J
Junio C Hamano 已提交
255
	int ents = 0, blobs = 0, paths = 0;
256
	const char *path = NULL;
J
Junio C Hamano 已提交
257
	struct blobinfo blob[2];
258
	int nongit;
259
	int result = 0;
J
Junio C Hamano 已提交
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

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

284
	prefix = setup_git_directory_gently(&nongit);
285
	gitmodules_config();
286
	git_config(git_diff_ui_config, NULL);
287

288
	init_revisions(&rev, prefix);
289 290 291 292 293

	/* If this is a no-index diff, just run it and exit there. */
	diff_no_index(&rev, argc, argv, nongit, prefix);

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

296
	/* Scale to real terminal size and respect statGraphWidth config */
297
	rev.diffopt.stat_width = -1;
298
	rev.diffopt.stat_graph_width = -1;
299

J
Jeff King 已提交
300
	/* Default to let external and textconv be used */
301
	DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
J
Jeff King 已提交
302
	DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
303

304
	if (nongit)
305
		die(_("Not a git repository"));
306
	argc = setup_revisions(argc, argv, &rev, NULL);
307
	if (!rev.diffopt.output_format) {
308
		rev.diffopt.output_format = DIFF_FORMAT_PATCH;
T
Thomas Rast 已提交
309
		diff_setup_done(&rev.diffopt);
310
	}
311

312
	DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
313

J
Jeff King 已提交
314
	setup_diff_pager(&rev.diffopt);
315

316 317
	/*
	 * Do we have --cached and not have a pending object, then
J
Junio C Hamano 已提交
318 319
	 * default to HEAD by hand.  Eek.
	 */
320
	if (!rev.pending.nr) {
J
Junio C Hamano 已提交
321 322 323 324 325
		int i;
		for (i = 1; i < argc; i++) {
			const char *arg = argv[i];
			if (!strcmp(arg, "--"))
				break;
326 327
			else if (!strcmp(arg, "--cached") ||
				 !strcmp(arg, "--staged")) {
328
				add_head_to_pending(&rev);
329 330
				if (!rev.pending.nr) {
					struct tree *tree;
331
					tree = lookup_tree(EMPTY_TREE_SHA1_BIN);
332 333
					add_pending_object(&rev, &tree->object, "HEAD");
				}
J
Junio C Hamano 已提交
334 335 336 337 338
				break;
			}
		}
	}

339 340
	for (i = 0; i < rev.pending.nr; i++) {
		struct object_array_entry *list = rev.pending.objects+i;
J
Junio C Hamano 已提交
341 342 343 344 345 346 347
		struct object *obj = list->item;
		const char *name = list->name;
		int flags = (obj->flags & UNINTERESTING);
		if (!obj->parsed)
			obj = parse_object(obj->sha1);
		obj = deref_tag(obj, NULL, 0);
		if (!obj)
348
			die(_("invalid object '%s' given."), name);
349
		if (obj->type == OBJ_COMMIT)
J
Junio C Hamano 已提交
350
			obj = &((struct commit *)obj)->tree->object;
351
		if (obj->type == OBJ_TREE) {
352
			if (ARRAY_SIZE(ent) <= ents)
353
				die(_("more than %d trees given: '%s'"),
354
				    (int) ARRAY_SIZE(ent), name);
J
Junio C Hamano 已提交
355 356 357 358 359 360
			obj->flags |= flags;
			ent[ents].item = obj;
			ent[ents].name = name;
			ents++;
			continue;
		}
361
		if (obj->type == OBJ_BLOB) {
J
Junio C Hamano 已提交
362
			if (2 <= blobs)
363
				die(_("more than two blobs given: '%s'"), name);
364
			hashcpy(blob[blobs].sha1, obj->sha1);
J
Junio C Hamano 已提交
365
			blob[blobs].name = name;
366
			blob[blobs].mode = list->mode;
J
Junio C Hamano 已提交
367 368
			blobs++;
			continue;
J
Junio C Hamano 已提交
369

J
Junio C Hamano 已提交
370
		}
371
		die(_("unhandled object '%s' given."), name);
J
Junio C Hamano 已提交
372
	}
373 374 375 376
	if (rev.prune_data.nr) {
		if (!path)
			path = rev.prune_data.items[0].match;
		paths += rev.prune_data.nr;
J
Junio C Hamano 已提交
377 378 379 380 381 382 383 384
	}

	/*
	 * Now, do the arguments look reasonable?
	 */
	if (!ents) {
		switch (blobs) {
		case 0:
385
			result = builtin_diff_files(&rev, argc, argv);
J
Junio C Hamano 已提交
386 387 388 389
			break;
		case 1:
			if (paths != 1)
				usage(builtin_diff_usage);
390
			result = builtin_diff_b_f(&rev, argc, argv, blob, path);
J
Junio C Hamano 已提交
391 392
			break;
		case 2:
393 394
			if (paths)
				usage(builtin_diff_usage);
395
			result = builtin_diff_blobs(&rev, argc, argv, blob);
J
Junio C Hamano 已提交
396 397 398 399 400 401 402 403
			break;
		default:
			usage(builtin_diff_usage);
		}
	}
	else if (blobs)
		usage(builtin_diff_usage);
	else if (ents == 1)
404
		result = builtin_diff_index(&rev, argc, argv);
J
Junio C Hamano 已提交
405
	else if (ents == 2)
406
		result = builtin_diff_tree(&rev, argc, argv, ent);
407 408 409 410 411 412 413 414 415
	else if (ent[0].item->flags & UNINTERESTING) {
		/*
		 * diff A...B where there is at least one merge base
		 * between A and B.  We have ent[0] == merge-base,
		 * ent[ents-2] == A, and ent[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.
		 */
		ent[1] = ent[ents-1];
416
		result = builtin_diff_tree(&rev, argc, argv, ent);
417
	} else
418
		result = builtin_diff_combined(&rev, argc, argv,
419
					       ent, ents);
J
Junio C Hamano 已提交
420
	result = diff_result_code(&rev.diffopt, result);
421 422
	if (1 < rev.diffopt.skip_stat_unmatch)
		refresh_index_quietly();
423
	return result;
J
Junio C Hamano 已提交
424
}