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, 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 char buffer1[PATH_MAX], buffer2[PATH_MAX];
104 struct string_list p1 = STRING_LIST_INIT_DUP;
105 struct string_list p2 = STRING_LIST_INIT_DUP;
106 int len1 = 0, len2 = 0, i1, i2, ret = 0;
108 if (name1 && read_directory(name1, &p1))
110 if (name2 && read_directory(name2, &p2)) {
111 string_list_clear(&p1, 0);
116 len1 = strlen(name1);
117 if (len1 > 0 && name1[len1 - 1] == '/')
119 memcpy(buffer1, name1, len1);
120 buffer1[len1++] = '/';
124 len2 = strlen(name2);
125 if (len2 > 0 && name2[len2 - 1] == '/')
127 memcpy(buffer2, name2, len2);
128 buffer2[len2++] = '/';
131 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
137 else if (i2 == p2.nr)
140 comp = strcmp(p1.items[i1].string,
141 p2.items[i2].string);
147 strncpy(buffer1 + len1, p1.items[i1++].string,
155 strncpy(buffer2 + len2, p2.items[i2++].string,
159 ret = queue_diff(o, n1, n2);
161 string_list_clear(&p1, 0);
162 string_list_clear(&p2, 0);
166 struct diff_filespec *d1, *d2;
168 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
171 tmp = mode1; mode1 = mode2; mode2 = tmp;
172 tmp_c = name1; name1 = name2; name2 = tmp_c;
175 d1 = noindex_filespec(name1, mode1);
176 d2 = noindex_filespec(name2, mode2);
177 diff_queue(&diff_queued_diff, d1, d2);
182 static int path_outside_repo(const char *path)
184 const char *work_tree;
187 if (!is_absolute_path(path))
189 work_tree = get_git_work_tree();
192 len = strlen(work_tree);
193 if (strncmp(path, work_tree, len) ||
194 (path[len] != '\0' && path[len] != '/'))
199 void diff_no_index(struct rev_info *revs,
200 int argc, const char **argv,
201 int nongit, const char *prefix)
205 unsigned options = 0;
206 const char *paths[2];
208 /* Were we asked to do --no-index explicitly? */
209 for (i = 1; i < argc; i++) {
210 if (!strcmp(argv[i], "--")) {
214 if (!strcmp(argv[i], "--no-index"))
216 if (argv[i][0] != '-')
220 if (!no_index && !nongit) {
222 * Inside a git repository, without --no-index. Only
223 * when a path outside the repository is given,
224 * e.g. "git diff /var/tmp/[12]", or "git diff
225 * Makefile /var/tmp/Makefile", allow it to be used as
226 * a colourful "diff" replacement.
228 if ((argc != i + 2) ||
229 (!path_outside_repo(argv[i]) &&
230 !path_outside_repo(argv[i+1])))
234 usagef("git diff %s <path> <path>",
235 no_index ? "--no-index" : "[--no-index]");
237 diff_setup(&revs->diffopt);
238 for (i = 1; i < argc - 2; ) {
240 if (!strcmp(argv[i], "--no-index"))
242 else if (!strcmp(argv[i], "-q")) {
243 options |= DIFF_SILENT_ON_REMOVED;
246 else if (!strcmp(argv[i], "--"))
249 j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
251 die("invalid diff option/value: %s", argv[i]);
257 * If the user asked for our exit code then don't start a
258 * pager or we would end up reporting its exit code instead.
260 if (!DIFF_OPT_TST(&revs->diffopt, EXIT_WITH_STATUS))
263 prefixlen = prefix ? strlen(prefix) : 0;
264 for (i = 0; i < 2; i++) {
265 const char *p = argv[argc - 2 + i];
268 * stdin should be spelled as "-"; if you have
269 * path that is "-", spell it as "./-".
271 p = file_from_standard_input;
273 p = xstrdup(prefix_filename(prefix, prefixlen, p));
276 revs->diffopt.skip_stat_unmatch = 1;
277 if (!revs->diffopt.output_format)
278 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
280 DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
281 DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
283 revs->max_count = -2;
284 if (diff_setup_done(&revs->diffopt) < 0)
285 die("diff_setup_done failed");
287 if (queue_diff(&revs->diffopt, paths[0], paths[1]))
289 diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
290 diffcore_std(&revs->diffopt);
291 diff_flush(&revs->diffopt);
294 * The return code for --no-index imitates diff(1):
295 * 0 = no changes, 1 = changes, else error
297 exit(revs->diffopt.found_changes);