diff-files.c 4.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * GIT - The information manager from hell
 *
 * Copyright (C) Linus Torvalds, 2005
 */
6
#include "cache.h"
7
#include "diff.h"
8

9
static const char *diff_files_usage =
10 11 12
"git-diff-files [-q] "
"[<common diff options>] [<path>...]"
COMMON_DIFF_OPTIONS_HELP;
13

14
static int diff_output_format = DIFF_FORMAT_HUMAN;
15
static int detect_rename = 0;
J
Junio C Hamano 已提交
16
static int find_copies_harder = 0;
17
static int diff_setup_opt = 0;
J
Junio C Hamano 已提交
18
static int diff_score_opt = 0;
19
static const char *pickaxe = NULL;
20
static int pickaxe_opts = 0;
21
static int diff_break_opt = -1;
22
static const char *orderfile = NULL;
23
static const char *diff_filter = NULL;
24 25
static int silent = 0;

26
static void show_unmerge(const char *path)
27
{
J
Junio C Hamano 已提交
28
	diff_unmerge(path);
29 30 31 32
}

static void show_file(int pfx, struct cache_entry *ce)
{
J
Junio C Hamano 已提交
33
	diff_addremove(pfx, ntohl(ce->ce_mode), ce->sha1, ce->name, NULL);
34 35 36
}

static void show_modified(int oldmode, int mode,
37
			  const unsigned char *old_sha1, const unsigned char *sha1,
38 39
			  char *path)
{
J
Junio C Hamano 已提交
40
	diff_change(oldmode, mode, old_sha1, sha1, path, NULL);
41 42
}

J
Junio C Hamano 已提交
43
int main(int argc, const char **argv)
44
{
45
	static const unsigned char null_sha1[20] = { 0, };
46
	const char **pathspec;
47 48 49
	int entries = read_cache();
	int i;

50
	while (1 < argc && argv[1][0] == '-') {
51
		if (!strcmp(argv[1], "-p") || !strcmp(argv[1], "-u"))
52
			diff_output_format = DIFF_FORMAT_PATCH;
53
		else if (!strcmp(argv[1], "-q"))
54
			silent = 1;
55
		else if (!strcmp(argv[1], "-r"))
56
			; /* no-op */
57 58 59
		else if (!strcmp(argv[1], "-s"))
			; /* no-op */
		else if (!strcmp(argv[1], "-z"))
60
			diff_output_format = DIFF_FORMAT_MACHINE;
61 62 63 64
		else if (!strcmp(argv[1], "--name-only"))
			diff_output_format = DIFF_FORMAT_NAME;
		else if (!strcmp(argv[1], "--name-only-z"))
			diff_output_format = DIFF_FORMAT_NAME_Z;
J
Junio C Hamano 已提交
65
		else if (!strcmp(argv[1], "-R"))
66
			diff_setup_opt |= DIFF_SETUP_REVERSE;
J
Junio C Hamano 已提交
67
		else if (!strncmp(argv[1], "-S", 2))
68
			pickaxe = argv[1] + 2;
69 70
		else if (!strncmp(argv[1], "-O", 2))
			orderfile = argv[1] + 2;
71 72
		else if (!strncmp(argv[1], "--diff-filter=", 14))
			diff_filter = argv[1] + 14;
73 74
		else if (!strcmp(argv[1], "--pickaxe-all"))
			pickaxe_opts = DIFF_PICKAXE_ALL;
75 76 77 78 79
		else if (!strncmp(argv[1], "-B", 2)) {
			if ((diff_break_opt =
			     diff_scoreopt_parse(argv[1])) == -1)
				usage(diff_files_usage);
		}
J
Junio C Hamano 已提交
80
		else if (!strncmp(argv[1], "-M", 2)) {
81 82 83
			if ((diff_score_opt =
			     diff_scoreopt_parse(argv[1])) == -1)
				usage(diff_files_usage);
J
Junio C Hamano 已提交
84
			detect_rename = DIFF_DETECT_RENAME;
85
		}
86
		else if (!strncmp(argv[1], "-C", 2)) {
87 88 89
			if ((diff_score_opt =
			     diff_scoreopt_parse(argv[1])) == -1)
				usage(diff_files_usage);
J
Junio C Hamano 已提交
90
			detect_rename = DIFF_DETECT_COPY;
91
		}
J
Junio C Hamano 已提交
92 93
		else if (!strcmp(argv[1], "--find-copies-harder"))
			find_copies_harder = 1;
94
		else
95
			usage(diff_files_usage);
96
		argv++; argc--;
P
Petr Baudis 已提交
97 98
	}

99 100 101
	/* Do we have a pathspec? */
	pathspec = (argc > 1) ? argv + 1 : NULL;

J
Junio C Hamano 已提交
102 103 104
	if (find_copies_harder && detect_rename != DIFF_DETECT_COPY)
		usage(diff_files_usage);

105 106 107
	/* At this point, if argc == 1, then we are doing everything.
	 * Otherwise argv[1] .. argv[argc-1] have the explicit paths.
	 */
108 109 110 111
	if (entries < 0) {
		perror("read_cache");
		exit(1);
	}
112

113
	diff_setup(diff_setup_opt);
114

115 116
	for (i = 0; i < entries; i++) {
		struct stat st;
J
Junio C Hamano 已提交
117
		unsigned int oldmode;
118
		struct cache_entry *ce = active_cache[i];
119
		int changed;
120

121 122 123
		if (!ce_path_match(ce, pathspec))
			continue;

124
		if (ce_stage(ce)) {
125
			show_unmerge(ce->name);
126 127 128 129 130 131
			while (i < entries &&
			       !strcmp(ce->name, active_cache[i]->name))
				i++;
			i--; /* compensate for loop control increments */
			continue;
		}
J
Junio C Hamano 已提交
132

133
		if (lstat(ce->name, &st) < 0) {
134
			if (errno != ENOENT && errno != ENOTDIR) {
135
				perror(ce->name);
136
				continue;
J
Junio C Hamano 已提交
137
			}
138
			if (silent)
139
				continue;
140
			show_file('-', ce);
141 142
			continue;
		}
143
		changed = ce_match_stat(ce, &st);
J
Junio C Hamano 已提交
144
		if (!changed && !find_copies_harder)
145
			continue;
146
		oldmode = ntohl(ce->ce_mode);
J
Junio C Hamano 已提交
147
		show_modified(oldmode, DIFF_FILE_CANON_MODE(st.st_mode),
J
Junio C Hamano 已提交
148
			      ce->sha1, (changed ? null_sha1 : ce->sha1),
149
			      ce->name);
150
	}
151
	diffcore_std(pathspec, 
152
		     detect_rename, diff_score_opt,
153
		     pickaxe, pickaxe_opts,
154
		     diff_break_opt,
155 156
		     orderfile, diff_filter);
	diff_flush(diff_output_format);
157 158
	return 0;
}