2 * "diff --no-index" support
3 * Copyright (c) 2007 by Johannes Schindelin
4 * Copyright (c) 2008 by Junio C Hamano
17 #include "path-list.h"
19 static int read_directory(const char *path, struct path_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 path_list_insert(e->d_name, list);
35 static int get_mode(const char *path, int *mode)
39 if (!path || !strcmp(path, "/dev/null"))
41 else if (!strcmp(path, "-"))
42 *mode = create_ce_mode(0666);
43 else if (stat(path, &st))
44 return error("Could not access '%s'", path);
50 static int queue_diff(struct diff_options *o,
51 const char *name1, const char *name2)
53 int mode1 = 0, mode2 = 0;
55 if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
58 if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
59 return error("file/directory conflict: %s, %s", name1, name2);
61 if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
62 char buffer1[PATH_MAX], buffer2[PATH_MAX];
63 struct path_list p1 = {NULL, 0, 0, 1}, p2 = {NULL, 0, 0, 1};
64 int len1 = 0, len2 = 0, i1, i2, ret = 0;
66 if (name1 && read_directory(name1, &p1))
68 if (name2 && read_directory(name2, &p2)) {
69 path_list_clear(&p1, 0);
75 if (len1 > 0 && name1[len1 - 1] == '/')
77 memcpy(buffer1, name1, len1);
78 buffer1[len1++] = '/';
83 if (len2 > 0 && name2[len2 - 1] == '/')
85 memcpy(buffer2, name2, len2);
86 buffer2[len2++] = '/';
89 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
98 comp = strcmp(p1.items[i1].path,
105 strncpy(buffer1 + len1, p1.items[i1++].path,
113 strncpy(buffer2 + len2, p2.items[i2++].path,
117 ret = queue_diff(o, n1, n2);
119 path_list_clear(&p1, 0);
120 path_list_clear(&p2, 0);
124 struct diff_filespec *d1, *d2;
126 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
129 tmp = mode1; mode1 = mode2; mode2 = tmp;
130 tmp_c = name1; name1 = name2; name2 = tmp_c;
137 d1 = alloc_filespec(name1);
138 d2 = alloc_filespec(name2);
139 fill_filespec(d1, null_sha1, mode1);
140 fill_filespec(d2, null_sha1, mode2);
142 diff_queue(&diff_queued_diff, d1, d2);
147 void diff_no_index(struct rev_info *revs,
148 int argc, const char **argv,
149 int nongit, const char *prefix)
153 unsigned options = 0;
155 /* Were we asked to do --no-index explicitly? */
156 for (i = 1; i < argc; i++) {
157 if (!strcmp(argv[i], "--"))
159 if (!strcmp(argv[i], "--no-index"))
161 if (argv[i][0] != '-')
166 * No explicit --no-index, but "git diff --opts A B" outside
167 * a git repository is a cute hack to support.
169 if (!no_index && !nongit)
173 die("git diff %s takes two paths",
174 no_index ? "--no-index" : "[--no-index]");
176 diff_setup(&revs->diffopt);
177 if (!revs->diffopt.output_format)
178 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
179 for (i = 1; i < argc - 2; ) {
181 if (!strcmp(argv[i], "--no-index"))
183 else if (!strcmp(argv[1], "-q"))
184 options |= DIFF_SILENT_ON_REMOVED;
186 j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
188 die("invalid diff option/value: %s", argv[i]);
194 int len = strlen(prefix);
196 revs->diffopt.paths = xcalloc(2, sizeof(char*));
197 for (i = 0; i < 2; i++) {
198 const char *p = argv[argc - 2 + i];
200 * stdin should be spelled as '-'; if you have
201 * path that is '-', spell it as ./-.
204 ? xstrdup(prefix_filename(prefix, len, p))
206 revs->diffopt.paths[i] = p;
210 revs->diffopt.paths = argv + argc - 2;
211 revs->diffopt.nr_paths = 2;
213 DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
214 DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
216 revs->max_count = -2;
217 if (diff_setup_done(&revs->diffopt) < 0)
218 die("diff_setup_done failed");
220 if (queue_diff(&revs->diffopt, revs->diffopt.paths[0],
221 revs->diffopt.paths[1]))
223 diffcore_std(&revs->diffopt);
224 diff_flush(&revs->diffopt);
227 * The return code for --no-index imitates diff(1):
228 * 0 = no changes, 1 = changes, else error
230 exit(revs->diffopt.found_changes);