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(e->d_name, list);
35 static int get_mode(const char *path, int *mode)
39 if (!path || !strcmp(path, "/dev/null"))
42 else if (!strcasecmp(path, "nul"))
45 else if (!strcmp(path, "-"))
46 *mode = create_ce_mode(0666);
47 else if (lstat(path, &st))
48 return error("Could not access '%s'", path);
54 static int queue_diff(struct diff_options *o,
55 const char *name1, const char *name2)
57 int mode1 = 0, mode2 = 0;
59 if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
62 if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
63 return error("file/directory conflict: %s, %s", name1, name2);
65 if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
66 char buffer1[PATH_MAX], buffer2[PATH_MAX];
67 struct string_list p1 = {NULL, 0, 0, 1}, p2 = {NULL, 0, 0, 1};
68 int len1 = 0, len2 = 0, i1, i2, ret = 0;
70 if (name1 && read_directory(name1, &p1))
72 if (name2 && read_directory(name2, &p2)) {
73 string_list_clear(&p1, 0);
79 if (len1 > 0 && name1[len1 - 1] == '/')
81 memcpy(buffer1, name1, len1);
82 buffer1[len1++] = '/';
87 if (len2 > 0 && name2[len2 - 1] == '/')
89 memcpy(buffer2, name2, len2);
90 buffer2[len2++] = '/';
93 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
102 comp = strcmp(p1.items[i1].string,
103 p2.items[i2].string);
109 strncpy(buffer1 + len1, p1.items[i1++].string,
117 strncpy(buffer2 + len2, p2.items[i2++].string,
121 ret = queue_diff(o, n1, n2);
123 string_list_clear(&p1, 0);
124 string_list_clear(&p2, 0);
128 struct diff_filespec *d1, *d2;
130 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
133 tmp = mode1; mode1 = mode2; mode2 = tmp;
134 tmp_c = name1; name1 = name2; name2 = tmp_c;
141 d1 = alloc_filespec(name1);
142 d2 = alloc_filespec(name2);
143 fill_filespec(d1, null_sha1, mode1);
144 fill_filespec(d2, null_sha1, mode2);
146 diff_queue(&diff_queued_diff, d1, d2);
151 static int path_outside_repo(const char *path)
154 * We have already done setup_git_directory_gently() so we
155 * know we are inside a git work tree already.
157 const char *work_tree;
160 if (!is_absolute_path(path))
162 work_tree = get_git_work_tree();
163 len = strlen(work_tree);
164 if (strncmp(path, work_tree, len) ||
165 (path[len] != '\0' && path[len] != '/'))
170 void diff_no_index(struct rev_info *revs,
171 int argc, const char **argv,
172 int nongit, const char *prefix)
176 unsigned options = 0;
178 /* Were we asked to do --no-index explicitly? */
179 for (i = 1; i < argc; i++) {
180 if (!strcmp(argv[i], "--")) {
184 if (!strcmp(argv[i], "--no-index"))
186 if (argv[i][0] != '-')
190 if (!no_index && !nongit) {
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.
198 if ((argc != i + 2) ||
199 (!path_outside_repo(argv[i]) &&
200 !path_outside_repo(argv[i+1])))
204 die("git diff %s takes two paths",
205 no_index ? "--no-index" : "[--no-index]");
207 diff_setup(&revs->diffopt);
208 for (i = 1; i < argc - 2; ) {
210 if (!strcmp(argv[i], "--no-index"))
212 else if (!strcmp(argv[i], "-q")) {
213 options |= DIFF_SILENT_ON_REMOVED;
216 else if (!strcmp(argv[i], "--"))
219 j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
221 die("invalid diff option/value: %s", argv[i]);
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.
230 if (!DIFF_OPT_TST(&revs->diffopt, EXIT_WITH_STATUS))
234 int len = strlen(prefix);
236 revs->diffopt.paths = xcalloc(2, sizeof(char*));
237 for (i = 0; i < 2; i++) {
238 const char *p = argv[argc - 2 + i];
240 * stdin should be spelled as '-'; if you have
241 * path that is '-', spell it as ./-.
244 ? xstrdup(prefix_filename(prefix, len, p))
246 revs->diffopt.paths[i] = p;
250 revs->diffopt.paths = argv + argc - 2;
251 revs->diffopt.nr_paths = 2;
252 revs->diffopt.skip_stat_unmatch = 1;
253 if (!revs->diffopt.output_format)
254 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
256 DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
257 DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
259 revs->max_count = -2;
260 if (diff_setup_done(&revs->diffopt) < 0)
261 die("diff_setup_done failed");
263 if (queue_diff(&revs->diffopt, revs->diffopt.paths[0],
264 revs->diffopt.paths[1]))
266 diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
267 diffcore_std(&revs->diffopt);
268 diff_flush(&revs->diffopt);
271 * The return code for --no-index imitates diff(1):
272 * 0 = no changes, 1 = changes, else error
274 exit(revs->diffopt.found_changes);