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

	diff_queue(&diff_queued_diff, one, two);
}

static int builtin_diff_b_f(struct rev_info *revs,
			    int argc, const char **argv,
67
			    struct blobinfo *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 88
	if (blob[0].mode == S_IFINVALID)
		blob[0].mode = canon_mode(st.st_mode);

J
Junio C Hamano 已提交
89
	stuff_change(&revs->diffopt,
90
		     blob[0].mode, canon_mode(st.st_mode),
J
Junio C Hamano 已提交
91
		     blob[0].sha1, null_sha1,
92
		     1, 0,
93
		     path, path);
J
Junio C Hamano 已提交
94 95 96 97 98 99 100 101 102 103 104
	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);

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

108 109 110 111 112 113
	if (blob[0].mode == S_IFINVALID)
		blob[0].mode = mode;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

288
	prefix = setup_git_directory_gently(&nongit);
289
	gitmodules_config();
290
	git_config(git_diff_ui_config, NULL);
291

292
	init_revisions(&rev, prefix);
293 294 295 296 297

	/* 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 */
298
	rev.diffopt.skip_stat_unmatch = !!diff_auto_refresh_index;
J
Junio C Hamano 已提交
299

300
	/* Scale to real terminal size and respect statGraphWidth config */
301
	rev.diffopt.stat_width = -1;
302
	rev.diffopt.stat_graph_width = -1;
303

J
Jeff King 已提交
304
	/* Default to let external and textconv be used */
305
	DIFF_OPT_SET(&rev.diffopt, ALLOW_EXTERNAL);
J
Jeff King 已提交
306
	DIFF_OPT_SET(&rev.diffopt, ALLOW_TEXTCONV);
307

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

316
	DIFF_OPT_SET(&rev.diffopt, RECURSIVE);
317

J
Jeff King 已提交
318
	setup_diff_pager(&rev.diffopt);
319

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

343
	for (i = 0; i < rev.pending.nr; i++) {
344 345 346
		struct object_array_entry *entry = &rev.pending.objects[i];
		struct object *obj = entry->item;
		const char *name = entry->name;
J
Junio C Hamano 已提交
347 348 349 350 351
		int flags = (obj->flags & UNINTERESTING);
		if (!obj->parsed)
			obj = parse_object(obj->sha1);
		obj = deref_tag(obj, NULL, 0);
		if (!obj)
352
			die(_("invalid object '%s' given."), name);
353
		if (obj->type == OBJ_COMMIT)
J
Junio C Hamano 已提交
354
			obj = &((struct commit *)obj)->tree->object;
355

356
		if (obj->type == OBJ_TREE) {
J
Junio C Hamano 已提交
357
			obj->flags |= flags;
358
			add_object_array(obj, name, &ent);
359
		} else if (obj->type == OBJ_BLOB) {
J
Junio C Hamano 已提交
360
			if (2 <= blobs)
361
				die(_("more than two blobs given: '%s'"), name);
362
			hashcpy(blob[blobs].sha1, obj->sha1);
J
Junio C Hamano 已提交
363
			blob[blobs].name = name;
364
			blob[blobs].mode = entry->mode;
J
Junio C Hamano 已提交
365
			blobs++;
J
Junio C Hamano 已提交
366

367 368
		} else {
			die(_("unhandled object '%s' given."), name);
J
Junio C Hamano 已提交
369 370
		}
	}
371
	if (rev.prune_data.nr)
372
		paths += rev.prune_data.nr;
J
Junio C Hamano 已提交
373 374 375 376

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