1/*
2* GIT - The information manager from hell
3*
4* Copyright (C) Linus Torvalds, 2005
5*/
6#include "cache.h"
7#include "diff.h"
89
static const char diff_files_usage[] =
10"git-diff-files [-q] [-0/-1/2/3] [<common diff options>] [<path>...]"
11COMMON_DIFF_OPTIONS_HELP;
1213
static struct diff_options diff_options;
14static int silent = 0;
15static int diff_unmerged_stage = -1;
1617
static void show_unmerge(const char *path)
18{
19diff_unmerge(&diff_options, path);
20}
2122
static void show_file(int pfx, struct cache_entry *ce)
23{
24diff_addremove(&diff_options, pfx, ntohl(ce->ce_mode),
25ce->sha1, ce->name, NULL);
26}
2728
static void show_modified(int oldmode, int mode,
29const unsigned char *old_sha1, const unsigned char *sha1,
30char *path)
31{
32diff_change(&diff_options, oldmode, mode, old_sha1, sha1, path, NULL);
33}
3435
int main(int argc, const char **argv)
36{
37const char **pathspec;
38const char *prefix = setup_git_directory();
39int entries, i;
4041
git_config(git_diff_config);
42diff_setup(&diff_options);
43while (1 < argc && argv[1][0] == '-') {
44if (!strcmp(argv[1], "--")) {
45argv++;
46argc--;
47break;
48}
49if (!strcmp(argv[1], "-0"))
50diff_unmerged_stage = 0;
51else if (!strcmp(argv[1], "-1"))
52diff_unmerged_stage = 1;
53else if (!strcmp(argv[1], "-2"))
54diff_unmerged_stage = 2;
55else if (!strcmp(argv[1], "-3"))
56diff_unmerged_stage = 3;
57else if (!strcmp(argv[1], "--base"))
58diff_unmerged_stage = 1;
59else if (!strcmp(argv[1], "--ours"))
60diff_unmerged_stage = 2;
61else if (!strcmp(argv[1], "--theirs"))
62diff_unmerged_stage = 3;
63else if (!strcmp(argv[1], "-q"))
64silent = 1;
65else if (!strcmp(argv[1], "-r"))
66; /* no-op */
67else if (!strcmp(argv[1], "-s"))
68; /* no-op */
69else {
70int diff_opt_cnt;
71diff_opt_cnt = diff_opt_parse(&diff_options,
72argv+1, argc-1);
73if (diff_opt_cnt < 0)
74usage(diff_files_usage);
75else if (diff_opt_cnt) {
76argv += diff_opt_cnt;
77argc -= diff_opt_cnt;
78continue;
79}
80else
81usage(diff_files_usage);
82}
83argv++; argc--;
84}
8586
/* Find the directory, and set up the pathspec */
87pathspec = get_pathspec(prefix, argv + 1);
88entries = read_cache();
8990
if (diff_unmerged_stage < 0) {
91/* default to "ours" if unmerged index, otherwise 0 */
92for (i = 0; i < entries; i++) {
93struct cache_entry *ce = active_cache[i];
94if (ce_stage(ce)) {
95diff_unmerged_stage = 2;
96break;
97}
98}
99if (diff_unmerged_stage < 0)
100diff_unmerged_stage = 0;
101}
102103
104
if (diff_setup_done(&diff_options) < 0)
105usage(diff_files_usage);
106107
/* At this point, if argc == 1, then we are doing everything.
108* Otherwise argv[1] .. argv[argc-1] have the explicit paths.
109*/
110if (entries < 0) {
111perror("read_cache");
112exit(1);
113}
114115
for (i = 0; i < entries; i++) {
116struct stat st;
117unsigned int oldmode, newmode;
118struct cache_entry *ce = active_cache[i];
119int changed;
120121
if (!ce_path_match(ce, pathspec))
122continue;
123124
if (ce_stage(ce)) {
125if (!diff_unmerged_stage)
126show_unmerge(ce->name);
127while (i < entries) {
128struct cache_entry *nce = active_cache[i];
129130
if (strcmp(ce->name, nce->name))
131break;
132/* diff against the proper unmerged stage */
133if (ce_stage(nce) == diff_unmerged_stage)
134ce = nce;
135i++;
136}
137/*
138* Compensate for loop update
139*/
140i--;
141/*
142* Show the diff for the 'ce' if we found the one
143* from the desired stage.
144*/
145if (ce_stage(ce) != diff_unmerged_stage)
146continue;
147}
148else if (diff_unmerged_stage)
149continue;
150151
if (lstat(ce->name, &st) < 0) {
152if (errno != ENOENT && errno != ENOTDIR) {
153perror(ce->name);
154continue;
155}
156if (silent)
157continue;
158show_file('-', ce);
159continue;
160}
161changed = ce_match_stat(ce, &st);
162if (!changed && !diff_options.find_copies_harder)
163continue;
164oldmode = ntohl(ce->ce_mode);
165166
newmode = DIFF_FILE_CANON_MODE(st.st_mode);
167if (!trust_executable_bit &&
168S_ISREG(newmode) && S_ISREG(oldmode) &&
169((newmode ^ oldmode) == 0111))
170newmode = oldmode;
171show_modified(oldmode, newmode,
172ce->sha1, (changed ? null_sha1 : ce->sha1),
173ce->name);
174}
175diffcore_std(&diff_options);
176diff_flush(&diff_options);
177return 0;
178}