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