diff-index.c: do not pretend paths are pathspecs
[git/git.git] / diff-no-index.c
CommitLineData
0569e9b8
JH
1/*
2 * "diff --no-index" support
3 * Copyright (c) 2007 by Johannes Schindelin
4 * Copyright (c) 2008 by Junio C Hamano
5 */
6
7#include "cache.h"
8#include "color.h"
9#include "commit.h"
10#include "blob.h"
11#include "tag.h"
12#include "diff.h"
13#include "diffcore.h"
14#include "revision.h"
15#include "log-tree.h"
16#include "builtin.h"
c455c87c 17#include "string-list.h"
0569e9b8 18
c455c87c 19static int read_directory(const char *path, struct string_list *list)
0569e9b8
JH
20{
21 DIR *dir;
22 struct dirent *e;
23
24 if (!(dir = opendir(path)))
25 return error("Could not open directory %s", path);
26
27 while ((e = readdir(dir)))
28 if (strcmp(".", e->d_name) && strcmp("..", e->d_name))
78a395d3 29 string_list_insert(list, e->d_name);
0569e9b8
JH
30
31 closedir(dir);
32 return 0;
33}
34
35static int get_mode(const char *path, int *mode)
36{
37 struct stat st;
38
39 if (!path || !strcmp(path, "/dev/null"))
40 *mode = 0;
36adb4ab
JS
41#ifdef _WIN32
42 else if (!strcasecmp(path, "nul"))
43 *mode = 0;
44#endif
0569e9b8
JH
45 else if (!strcmp(path, "-"))
46 *mode = create_ce_mode(0666);
418566b6 47 else if (lstat(path, &st))
0569e9b8
JH
48 return error("Could not access '%s'", path);
49 else
50 *mode = st.st_mode;
51 return 0;
52}
53
54static int queue_diff(struct diff_options *o,
55 const char *name1, const char *name2)
56{
57 int mode1 = 0, mode2 = 0;
58
59 if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
60 return -1;
61
62 if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
63 return error("file/directory conflict: %s, %s", name1, name2);
64
65 if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
66 char buffer1[PATH_MAX], buffer2[PATH_MAX];
183113a5
TF
67 struct string_list p1 = STRING_LIST_INIT_DUP;
68 struct string_list p2 = STRING_LIST_INIT_DUP;
0569e9b8
JH
69 int len1 = 0, len2 = 0, i1, i2, ret = 0;
70
71 if (name1 && read_directory(name1, &p1))
72 return -1;
73 if (name2 && read_directory(name2, &p2)) {
c455c87c 74 string_list_clear(&p1, 0);
0569e9b8
JH
75 return -1;
76 }
77
78 if (name1) {
79 len1 = strlen(name1);
80 if (len1 > 0 && name1[len1 - 1] == '/')
81 len1--;
82 memcpy(buffer1, name1, len1);
83 buffer1[len1++] = '/';
84 }
85
86 if (name2) {
87 len2 = strlen(name2);
88 if (len2 > 0 && name2[len2 - 1] == '/')
89 len2--;
90 memcpy(buffer2, name2, len2);
91 buffer2[len2++] = '/';
92 }
93
94 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
95 const char *n1, *n2;
96 int comp;
97
98 if (i1 == p1.nr)
99 comp = 1;
100 else if (i2 == p2.nr)
101 comp = -1;
102 else
c455c87c
JS
103 comp = strcmp(p1.items[i1].string,
104 p2.items[i2].string);
0569e9b8
JH
105
106 if (comp > 0)
107 n1 = NULL;
108 else {
109 n1 = buffer1;
c455c87c 110 strncpy(buffer1 + len1, p1.items[i1++].string,
0569e9b8
JH
111 PATH_MAX - len1);
112 }
113
114 if (comp < 0)
115 n2 = NULL;
116 else {
117 n2 = buffer2;
c455c87c 118 strncpy(buffer2 + len2, p2.items[i2++].string,
0569e9b8
JH
119 PATH_MAX - len2);
120 }
121
122 ret = queue_diff(o, n1, n2);
123 }
c455c87c
JS
124 string_list_clear(&p1, 0);
125 string_list_clear(&p2, 0);
0569e9b8
JH
126
127 return ret;
128 } else {
129 struct diff_filespec *d1, *d2;
130
131 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
132 unsigned tmp;
133 const char *tmp_c;
134 tmp = mode1; mode1 = mode2; mode2 = tmp;
135 tmp_c = name1; name1 = name2; name2 = tmp_c;
136 }
137
138 if (!name1)
139 name1 = "/dev/null";
140 if (!name2)
141 name2 = "/dev/null";
142 d1 = alloc_filespec(name1);
143 d2 = alloc_filespec(name2);
144 fill_filespec(d1, null_sha1, mode1);
145 fill_filespec(d2, null_sha1, mode2);
146
147 diff_queue(&diff_queued_diff, d1, d2);
148 return 0;
149 }
150}
151
04036607
JH
152static int path_outside_repo(const char *path)
153{
04036607
JH
154 const char *work_tree;
155 size_t len;
156
157 if (!is_absolute_path(path))
158 return 0;
159 work_tree = get_git_work_tree();
4e1f8795
CB
160 if (!work_tree)
161 return 1;
04036607
JH
162 len = strlen(work_tree);
163 if (strncmp(path, work_tree, len) ||
164 (path[len] != '\0' && path[len] != '/'))
165 return 1;
166 return 0;
167}
168
0569e9b8
JH
169void diff_no_index(struct rev_info *revs,
170 int argc, const char **argv,
171 int nongit, const char *prefix)
172{
173 int i;
174 int no_index = 0;
175 unsigned options = 0;
c20f5926 176 const char *paths[2];
0569e9b8
JH
177
178 /* Were we asked to do --no-index explicitly? */
179 for (i = 1; i < argc; i++) {
e423ffd8
TR
180 if (!strcmp(argv[i], "--")) {
181 i++;
182 break;
183 }
0569e9b8
JH
184 if (!strcmp(argv[i], "--no-index"))
185 no_index = 1;
186 if (argv[i][0] != '-')
187 break;
188 }
189
04036607
JH
190 if (!no_index && !nongit) {
191 /*
192 * Inside a git repository, without --no-index. Only
193 * when a path outside the repository is given,
194 * e.g. "git diff /var/tmp/[12]", or "git diff
195 * Makefile /var/tmp/Makefile", allow it to be used as
196 * a colourful "diff" replacement.
197 */
198 if ((argc != i + 2) ||
199 (!path_outside_repo(argv[i]) &&
200 !path_outside_repo(argv[i+1])))
201 return;
202 }
0569e9b8 203 if (argc != i + 2)
d74bb308
JN
204 usagef("git diff %s <path> <path>",
205 no_index ? "--no-index" : "[--no-index]");
0569e9b8
JH
206
207 diff_setup(&revs->diffopt);
0569e9b8
JH
208 for (i = 1; i < argc - 2; ) {
209 int j;
210 if (!strcmp(argv[i], "--no-index"))
211 i++;
a324fc45 212 else if (!strcmp(argv[i], "-q")) {
0569e9b8 213 options |= DIFF_SILENT_ON_REMOVED;
a324fc45
TR
214 i++;
215 }
e423ffd8
TR
216 else if (!strcmp(argv[i], "--"))
217 i++;
0569e9b8
JH
218 else {
219 j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
220 if (!j)
221 die("invalid diff option/value: %s", argv[i]);
222 i += j;
223 }
224 }
225
c6dbca08
TR
226 /*
227 * If the user asked for our exit code then don't start a
228 * pager or we would end up reporting its exit code instead.
229 */
230 if (!DIFF_OPT_TST(&revs->diffopt, EXIT_WITH_STATUS))
231 setup_pager();
232
0569e9b8
JH
233 if (prefix) {
234 int len = strlen(prefix);
235
0569e9b8
JH
236 for (i = 0; i < 2; i++) {
237 const char *p = argv[argc - 2 + i];
238 /*
239 * stdin should be spelled as '-'; if you have
240 * path that is '-', spell it as ./-.
241 */
242 p = (strcmp(p, "-")
243 ? xstrdup(prefix_filename(prefix, len, p))
244 : p);
16dc36fe 245 paths[i] = p;
0569e9b8 246 }
c20f5926
JH
247 } else {
248 for (i = 0; i < 2; i++)
249 paths[i] = argv[argc - 2 + i];
0569e9b8 250 }
d61027b2 251 revs->diffopt.skip_stat_unmatch = 1;
5d83f9c1
JS
252 if (!revs->diffopt.output_format)
253 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
0569e9b8
JH
254
255 DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
256 DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
257
258 revs->max_count = -2;
259 if (diff_setup_done(&revs->diffopt) < 0)
260 die("diff_setup_done failed");
261
c20f5926 262 if (queue_diff(&revs->diffopt, paths[0], paths[1]))
0569e9b8 263 exit(1);
a5a818ee 264 diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
0569e9b8
JH
265 diffcore_std(&revs->diffopt);
266 diff_flush(&revs->diffopt);
267
268 /*
269 * The return code for --no-index imitates diff(1):
270 * 0 = no changes, 1 = changes, else error
271 */
272 exit(revs->diffopt.found_changes);
273}