2 * "diff --no-index" support
3 * Copyright (c) 2007 by Johannes Schindelin
4 * Copyright (c) 2008 by Junio C Hamano
17 #include "string-list.h"
19 static int read_directory(const char *path, struct string_list *list)
24 if (!(dir = opendir(path)))
25 return error("Could not open directory %s", path);
27 while ((e = readdir(dir)))
28 if (strcmp(".", e->d_name) && strcmp("..", e->d_name))
29 string_list_insert(list, e->d_name);
36 * This should be "(standard input)" or something, but it will
37 * probably expose many more breakages in the way no-index code
38 * is bolted onto the diff callchain.
40 static const char file_from_standard_input[] = "-";
42 static int get_mode(const char *path, int *mode)
46 if (!path || !strcmp(path, "/dev/null"))
49 else if (!strcasecmp(path, "nul"))
52 else if (path == file_from_standard_input)
53 *mode = create_ce_mode(0666);
54 else if (lstat(path, &st))
55 return error("Could not access '%s'", path);
61 static int populate_from_stdin(struct diff_filespec *s)
63 struct strbuf buf = STRBUF_INIT;
66 if (strbuf_read(&buf, 0, 0) < 0)
67 return error("error while reading from stdin %s",
71 s->data = strbuf_detach(&buf, &size);
78 static struct diff_filespec *noindex_filespec(const char *name, int mode)
80 struct diff_filespec *s;
84 s = alloc_filespec(name);
85 fill_filespec(s, null_sha1, 0, mode);
86 if (name == file_from_standard_input)
87 populate_from_stdin(s);
91 static int queue_diff(struct diff_options *o,
92 const char *name1, const char *name2)
94 int mode1 = 0, mode2 = 0;
96 if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
99 if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
100 return error("file/directory conflict: %s, %s", name1, name2);
102 if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
103 struct strbuf buffer1 = STRBUF_INIT;
104 struct strbuf buffer2 = STRBUF_INIT;
105 struct string_list p1 = STRING_LIST_INIT_DUP;
106 struct string_list p2 = STRING_LIST_INIT_DUP;
108 size_t len1 = 0, len2 = 0;
110 if (name1 && read_directory(name1, &p1))
112 if (name2 && read_directory(name2, &p2)) {
113 string_list_clear(&p1, 0);
118 strbuf_addstr(&buffer1, name1);
119 if (buffer1.len && buffer1.buf[buffer1.len - 1] != '/')
120 strbuf_addch(&buffer1, '/');
125 strbuf_addstr(&buffer2, name2);
126 if (buffer2.len && buffer2.buf[buffer2.len - 1] != '/')
127 strbuf_addch(&buffer2, '/');
131 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
135 strbuf_setlen(&buffer1, len1);
136 strbuf_setlen(&buffer2, len2);
140 else if (i2 == p2.nr)
143 comp = strcmp(p1.items[i1].string, p2.items[i2].string);
148 strbuf_addstr(&buffer1, p1.items[i1++].string);
155 strbuf_addstr(&buffer2, p2.items[i2++].string);
159 ret = queue_diff(o, n1, n2);
161 string_list_clear(&p1, 0);
162 string_list_clear(&p2, 0);
163 strbuf_release(&buffer1);
164 strbuf_release(&buffer2);
168 struct diff_filespec *d1, *d2;
170 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
173 tmp = mode1; mode1 = mode2; mode2 = tmp;
174 tmp_c = name1; name1 = name2; name2 = tmp_c;
177 d1 = noindex_filespec(name1, mode1);
178 d2 = noindex_filespec(name2, mode2);
179 diff_queue(&diff_queued_diff, d1, d2);
184 void diff_no_index(struct rev_info *revs,
185 int argc, const char **argv,
186 int nongit, const char *prefix)
190 unsigned options = 0;
191 const char *paths[2];
193 /* Were we asked to do --no-index explicitly? */
194 for (i = 1; i < argc; i++) {
195 if (!strcmp(argv[i], "--")) {
199 if (!strcmp(argv[i], "--no-index"))
201 if (argv[i][0] != '-')
205 if (!no_index && !nongit) {
207 * Inside a git repository, without --no-index. Only
208 * when a path outside the repository is given,
209 * e.g. "git diff /var/tmp/[12]", or "git diff
210 * Makefile /var/tmp/Makefile", allow it to be used as
211 * a colourful "diff" replacement.
213 if ((argc != i + 2) ||
214 (path_inside_repo(prefix, argv[i]) &&
215 path_inside_repo(prefix, argv[i+1])))
219 usagef("git diff %s <path> <path>",
220 no_index ? "--no-index" : "[--no-index]");
222 diff_setup(&revs->diffopt);
223 for (i = 1; i < argc - 2; ) {
225 if (!strcmp(argv[i], "--no-index"))
227 else if (!strcmp(argv[i], "-q")) {
228 options |= DIFF_SILENT_ON_REMOVED;
231 else if (!strcmp(argv[i], "--"))
234 j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
236 die("invalid diff option/value: %s", argv[i]);
241 prefixlen = prefix ? strlen(prefix) : 0;
242 for (i = 0; i < 2; i++) {
243 const char *p = argv[argc - 2 + i];
246 * stdin should be spelled as "-"; if you have
247 * path that is "-", spell it as "./-".
249 p = file_from_standard_input;
251 p = xstrdup(prefix_filename(prefix, prefixlen, p));
254 revs->diffopt.skip_stat_unmatch = 1;
255 if (!revs->diffopt.output_format)
256 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
258 DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
260 revs->max_count = -2;
261 diff_setup_done(&revs->diffopt);
263 setup_diff_pager(&revs->diffopt);
264 DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
266 if (queue_diff(&revs->diffopt, paths[0], paths[1]))
268 diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
269 diffcore_std(&revs->diffopt);
270 diff_flush(&revs->diffopt);
273 * The return code for --no-index imitates diff(1):
274 * 0 = no changes, 1 = changes, else error
276 exit(diff_result_code(&revs->diffopt, 0));