Git 2.7
[git] / diff-no-index.c
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"
17 #include "string-list.h"
18 #include "dir.h"
19
20 static int read_directory_contents(const char *path, struct string_list *list)
21 {
22         DIR *dir;
23         struct dirent *e;
24
25         if (!(dir = opendir(path)))
26                 return error("Could not open directory %s", path);
27
28         while ((e = readdir(dir)))
29                 if (!is_dot_or_dotdot(e->d_name))
30                         string_list_insert(list, e->d_name);
31
32         closedir(dir);
33         return 0;
34 }
35
36 /*
37  * This should be "(standard input)" or something, but it will
38  * probably expose many more breakages in the way no-index code
39  * is bolted onto the diff callchain.
40  */
41 static const char file_from_standard_input[] = "-";
42
43 static int get_mode(const char *path, int *mode)
44 {
45         struct stat st;
46
47         if (!path || !strcmp(path, "/dev/null"))
48                 *mode = 0;
49 #ifdef GIT_WINDOWS_NATIVE
50         else if (!strcasecmp(path, "nul"))
51                 *mode = 0;
52 #endif
53         else if (path == file_from_standard_input)
54                 *mode = create_ce_mode(0666);
55         else if (lstat(path, &st))
56                 return error("Could not access '%s'", path);
57         else
58                 *mode = st.st_mode;
59         return 0;
60 }
61
62 static int populate_from_stdin(struct diff_filespec *s)
63 {
64         struct strbuf buf = STRBUF_INIT;
65         size_t size = 0;
66
67         if (strbuf_read(&buf, 0, 0) < 0)
68                 return error("error while reading from stdin %s",
69                                      strerror(errno));
70
71         s->should_munmap = 0;
72         s->data = strbuf_detach(&buf, &size);
73         s->size = size;
74         s->should_free = 1;
75         s->is_stdin = 1;
76         return 0;
77 }
78
79 static struct diff_filespec *noindex_filespec(const char *name, int mode)
80 {
81         struct diff_filespec *s;
82
83         if (!name)
84                 name = "/dev/null";
85         s = alloc_filespec(name);
86         fill_filespec(s, null_sha1, 0, mode);
87         if (name == file_from_standard_input)
88                 populate_from_stdin(s);
89         return s;
90 }
91
92 static int queue_diff(struct diff_options *o,
93                       const char *name1, const char *name2)
94 {
95         int mode1 = 0, mode2 = 0;
96
97         if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
98                 return -1;
99
100         if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2)) {
101                 struct diff_filespec *d1, *d2;
102
103                 if (S_ISDIR(mode1)) {
104                         /* 2 is file that is created */
105                         d1 = noindex_filespec(NULL, 0);
106                         d2 = noindex_filespec(name2, mode2);
107                         name2 = NULL;
108                         mode2 = 0;
109                 } else {
110                         /* 1 is file that is deleted */
111                         d1 = noindex_filespec(name1, mode1);
112                         d2 = noindex_filespec(NULL, 0);
113                         name1 = NULL;
114                         mode1 = 0;
115                 }
116                 /* emit that file */
117                 diff_queue(&diff_queued_diff, d1, d2);
118
119                 /* and then let the entire directory be created or deleted */
120         }
121
122         if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
123                 struct strbuf buffer1 = STRBUF_INIT;
124                 struct strbuf buffer2 = STRBUF_INIT;
125                 struct string_list p1 = STRING_LIST_INIT_DUP;
126                 struct string_list p2 = STRING_LIST_INIT_DUP;
127                 int i1, i2, ret = 0;
128                 size_t len1 = 0, len2 = 0;
129
130                 if (name1 && read_directory_contents(name1, &p1))
131                         return -1;
132                 if (name2 && read_directory_contents(name2, &p2)) {
133                         string_list_clear(&p1, 0);
134                         return -1;
135                 }
136
137                 if (name1) {
138                         strbuf_addstr(&buffer1, name1);
139                         strbuf_complete(&buffer1, '/');
140                         len1 = buffer1.len;
141                 }
142
143                 if (name2) {
144                         strbuf_addstr(&buffer2, name2);
145                         strbuf_complete(&buffer2, '/');
146                         len2 = buffer2.len;
147                 }
148
149                 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
150                         const char *n1, *n2;
151                         int comp;
152
153                         strbuf_setlen(&buffer1, len1);
154                         strbuf_setlen(&buffer2, len2);
155
156                         if (i1 == p1.nr)
157                                 comp = 1;
158                         else if (i2 == p2.nr)
159                                 comp = -1;
160                         else
161                                 comp = strcmp(p1.items[i1].string, p2.items[i2].string);
162
163                         if (comp > 0)
164                                 n1 = NULL;
165                         else {
166                                 strbuf_addstr(&buffer1, p1.items[i1++].string);
167                                 n1 = buffer1.buf;
168                         }
169
170                         if (comp < 0)
171                                 n2 = NULL;
172                         else {
173                                 strbuf_addstr(&buffer2, p2.items[i2++].string);
174                                 n2 = buffer2.buf;
175                         }
176
177                         ret = queue_diff(o, n1, n2);
178                 }
179                 string_list_clear(&p1, 0);
180                 string_list_clear(&p2, 0);
181                 strbuf_release(&buffer1);
182                 strbuf_release(&buffer2);
183
184                 return ret;
185         } else {
186                 struct diff_filespec *d1, *d2;
187
188                 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
189                         unsigned tmp;
190                         const char *tmp_c;
191                         tmp = mode1; mode1 = mode2; mode2 = tmp;
192                         tmp_c = name1; name1 = name2; name2 = tmp_c;
193                 }
194
195                 d1 = noindex_filespec(name1, mode1);
196                 d2 = noindex_filespec(name2, mode2);
197                 diff_queue(&diff_queued_diff, d1, d2);
198                 return 0;
199         }
200 }
201
202 /* append basename of F to D */
203 static void append_basename(struct strbuf *path, const char *dir, const char *file)
204 {
205         const char *tail = strrchr(file, '/');
206
207         strbuf_addstr(path, dir);
208         while (path->len && path->buf[path->len - 1] == '/')
209                 path->len--;
210         strbuf_addch(path, '/');
211         strbuf_addstr(path, tail ? tail + 1 : file);
212 }
213
214 /*
215  * DWIM "diff D F" into "diff D/F F" and "diff F D" into "diff F D/F"
216  * Note that we append the basename of F to D/, so "diff a/b/file D"
217  * becomes "diff a/b/file D/file", not "diff a/b/file D/a/b/file".
218  */
219 static void fixup_paths(const char **path, struct strbuf *replacement)
220 {
221         unsigned int isdir0, isdir1;
222
223         if (path[0] == file_from_standard_input ||
224             path[1] == file_from_standard_input)
225                 return;
226         isdir0 = is_directory(path[0]);
227         isdir1 = is_directory(path[1]);
228         if (isdir0 == isdir1)
229                 return;
230         if (isdir0) {
231                 append_basename(replacement, path[0], path[1]);
232                 path[0] = replacement->buf;
233         } else {
234                 append_basename(replacement, path[1], path[0]);
235                 path[1] = replacement->buf;
236         }
237 }
238
239 void diff_no_index(struct rev_info *revs,
240                    int argc, const char **argv,
241                    const char *prefix)
242 {
243         int i, prefixlen;
244         const char *paths[2];
245         struct strbuf replacement = STRBUF_INIT;
246
247         diff_setup(&revs->diffopt);
248         for (i = 1; i < argc - 2; ) {
249                 int j;
250                 if (!strcmp(argv[i], "--no-index"))
251                         i++;
252                 else if (!strcmp(argv[i], "--"))
253                         i++;
254                 else {
255                         j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
256                         if (j <= 0)
257                                 die("invalid diff option/value: %s", argv[i]);
258                         i += j;
259                 }
260         }
261
262         prefixlen = prefix ? strlen(prefix) : 0;
263         for (i = 0; i < 2; i++) {
264                 const char *p = argv[argc - 2 + i];
265                 if (!strcmp(p, "-"))
266                         /*
267                          * stdin should be spelled as "-"; if you have
268                          * path that is "-", spell it as "./-".
269                          */
270                         p = file_from_standard_input;
271                 else if (prefixlen)
272                         p = xstrdup(prefix_filename(prefix, prefixlen, p));
273                 paths[i] = p;
274         }
275
276         fixup_paths(paths, &replacement);
277
278         revs->diffopt.skip_stat_unmatch = 1;
279         if (!revs->diffopt.output_format)
280                 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
281
282         DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
283
284         revs->max_count = -2;
285         diff_setup_done(&revs->diffopt);
286
287         setup_diff_pager(&revs->diffopt);
288         DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
289
290         if (queue_diff(&revs->diffopt, paths[0], paths[1]))
291                 exit(1);
292         diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
293         diffcore_std(&revs->diffopt);
294         diff_flush(&revs->diffopt);
295
296         strbuf_release(&replacement);
297
298         /*
299          * The return code for --no-index imitates diff(1):
300          * 0 = no changes, 1 = changes, else error
301          */
302         exit(diff_result_code(&revs->diffopt, 0));
303 }