The sixteenth batch
[git/gitster.git] / diff-no-index.c
blob88ae4cee56ba41819cb7d6c332feb59b6e0fe5a8
1 /*
2 * "diff --no-index" support
3 * Copyright (c) 2007 by Johannes Schindelin
4 * Copyright (c) 2008 by Junio C Hamano
5 */
7 #define DISABLE_SIGN_COMPARE_WARNINGS
9 #include "git-compat-util.h"
10 #include "abspath.h"
11 #include "color.h"
12 #include "commit.h"
13 #include "diff.h"
14 #include "diffcore.h"
15 #include "gettext.h"
16 #include "revision.h"
17 #include "parse-options.h"
18 #include "pathspec.h"
19 #include "string-list.h"
20 #include "dir.h"
22 static int read_directory_contents(const char *path, struct string_list *list,
23 const struct pathspec *pathspec,
24 int skip)
26 struct strbuf match = STRBUF_INIT;
27 int len;
28 DIR *dir;
29 struct dirent *e;
31 if (!(dir = opendir(path)))
32 return error("Could not open directory %s", path);
34 if (pathspec) {
35 strbuf_addstr(&match, path);
36 strbuf_complete(&match, '/');
37 strbuf_remove(&match, 0, skip);
39 len = match.len;
42 while ((e = readdir_skip_dot_and_dotdot(dir))) {
43 if (pathspec) {
44 int is_dir = 0;
46 strbuf_setlen(&match, len);
47 strbuf_addstr(&match, e->d_name);
48 if (NOT_CONSTANT(DTYPE(e)) != DT_UNKNOWN) {
49 is_dir = (DTYPE(e) == DT_DIR);
50 } else {
51 struct strbuf pathbuf = STRBUF_INIT;
53 strbuf_addstr(&pathbuf, path);
54 strbuf_complete(&pathbuf, '/');
55 is_dir = get_dtype(e, &pathbuf, 0) == DT_DIR;
56 strbuf_release(&pathbuf);
59 if (!match_leading_pathspec(NULL, pathspec,
60 match.buf, match.len,
61 0, NULL, is_dir))
62 continue;
65 string_list_insert(list, e->d_name);
68 strbuf_release(&match);
69 closedir(dir);
70 return 0;
74 * This should be "(standard input)" or something, but it will
75 * probably expose many more breakages in the way no-index code
76 * is bolted onto the diff callchain.
78 static const char file_from_standard_input[] = "-";
81 * For paths given on the command-line we treat "-" as stdin and named
82 * pipes and symbolic links to named pipes specially.
84 enum special {
85 SPECIAL_NONE,
86 SPECIAL_STDIN,
87 SPECIAL_PIPE,
90 static int get_mode(const char *path, int *mode, enum special *special)
92 struct stat st;
94 if (!path || !strcmp(path, "/dev/null")) {
95 *mode = 0;
96 #ifdef GIT_WINDOWS_NATIVE
97 } else if (!strcasecmp(path, "nul")) {
98 *mode = 0;
99 #endif
100 } else if (path == file_from_standard_input) {
101 *mode = create_ce_mode(0666);
102 *special = SPECIAL_STDIN;
103 } else if (lstat(path, &st)) {
104 return error("Could not access '%s'", path);
105 } else {
106 *mode = st.st_mode;
109 * For paths on the command-line treat named pipes and symbolic
110 * links that resolve to a named pipe specially.
112 if (special &&
113 (S_ISFIFO(*mode) ||
114 (S_ISLNK(*mode) && !stat(path, &st) && S_ISFIFO(st.st_mode)))) {
115 *mode = create_ce_mode(0666);
116 *special = SPECIAL_PIPE;
119 return 0;
122 static void populate_common(struct diff_filespec *s, struct strbuf *buf)
124 size_t size = 0;
126 s->should_munmap = 0;
127 s->data = strbuf_detach(buf, &size);
128 s->size = size;
129 s->should_free = 1;
130 s->is_stdin = 1;
133 static void populate_from_pipe(struct diff_filespec *s)
135 struct strbuf buf = STRBUF_INIT;
136 int fd = xopen(s->path, O_RDONLY);
138 if (strbuf_read(&buf, fd, 0) < 0)
139 die_errno("error while reading from '%s'", s->path);
140 close(fd);
141 populate_common(s, &buf);
144 static void populate_from_stdin(struct diff_filespec *s)
146 struct strbuf buf = STRBUF_INIT;
148 if (strbuf_read(&buf, 0, 0) < 0)
149 die_errno("error while reading from stdin");
150 populate_common(s, &buf);
153 static struct diff_filespec *noindex_filespec(const struct git_hash_algo *algop,
154 const char *name, int mode,
155 enum special special)
157 struct diff_filespec *s;
159 if (!name)
160 name = "/dev/null";
161 s = alloc_filespec(name);
162 fill_filespec(s, null_oid(algop), 0, mode);
163 if (special == SPECIAL_STDIN)
164 populate_from_stdin(s);
165 else if (special == SPECIAL_PIPE)
166 populate_from_pipe(s);
167 return s;
170 static int queue_diff(struct diff_options *o, const struct git_hash_algo *algop,
171 const char *name1, const char *name2, int recursing,
172 const struct pathspec *ps, int skip1, int skip2)
174 int mode1 = 0, mode2 = 0;
175 enum special special1 = SPECIAL_NONE, special2 = SPECIAL_NONE;
177 /* Paths can only be special if we're not recursing. */
178 if (get_mode(name1, &mode1, recursing ? NULL : &special1) ||
179 get_mode(name2, &mode2, recursing ? NULL : &special2))
180 return -1;
182 if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2)) {
183 struct diff_filespec *d1, *d2;
185 if (S_ISDIR(mode1)) {
186 /* 2 is file that is created */
187 d1 = noindex_filespec(algop, NULL, 0, SPECIAL_NONE);
188 d2 = noindex_filespec(algop, name2, mode2, special2);
189 name2 = NULL;
190 mode2 = 0;
191 } else {
192 /* 1 is file that is deleted */
193 d1 = noindex_filespec(algop, name1, mode1, special1);
194 d2 = noindex_filespec(algop, NULL, 0, SPECIAL_NONE);
195 name1 = NULL;
196 mode1 = 0;
198 /* emit that file */
199 diff_queue(&diff_queued_diff, d1, d2);
201 /* and then let the entire directory be created or deleted */
204 if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
205 struct strbuf buffer1 = STRBUF_INIT;
206 struct strbuf buffer2 = STRBUF_INIT;
207 struct string_list p1 = STRING_LIST_INIT_DUP;
208 struct string_list p2 = STRING_LIST_INIT_DUP;
209 int i1, i2, ret = 0;
210 size_t len1 = 0, len2 = 0;
212 if (name1 && read_directory_contents(name1, &p1, ps, skip1))
213 return -1;
214 if (name2 && read_directory_contents(name2, &p2, ps, skip2)) {
215 string_list_clear(&p1, 0);
216 return -1;
219 if (name1) {
220 strbuf_addstr(&buffer1, name1);
221 strbuf_complete(&buffer1, '/');
222 len1 = buffer1.len;
225 if (name2) {
226 strbuf_addstr(&buffer2, name2);
227 strbuf_complete(&buffer2, '/');
228 len2 = buffer2.len;
231 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
232 const char *n1, *n2;
233 int comp;
235 strbuf_setlen(&buffer1, len1);
236 strbuf_setlen(&buffer2, len2);
238 if (i1 == p1.nr)
239 comp = 1;
240 else if (i2 == p2.nr)
241 comp = -1;
242 else
243 comp = strcmp(p1.items[i1].string, p2.items[i2].string);
245 if (comp > 0)
246 n1 = NULL;
247 else {
248 strbuf_addstr(&buffer1, p1.items[i1++].string);
249 n1 = buffer1.buf;
252 if (comp < 0)
253 n2 = NULL;
254 else {
255 strbuf_addstr(&buffer2, p2.items[i2++].string);
256 n2 = buffer2.buf;
259 ret = queue_diff(o, algop, n1, n2, 1, ps, skip1, skip2);
261 string_list_clear(&p1, 0);
262 string_list_clear(&p2, 0);
263 strbuf_release(&buffer1);
264 strbuf_release(&buffer2);
266 return ret;
267 } else {
268 struct diff_filespec *d1, *d2;
270 if (o->flags.reverse_diff) {
271 SWAP(mode1, mode2);
272 SWAP(name1, name2);
273 SWAP(special1, special2);
276 d1 = noindex_filespec(algop, name1, mode1, special1);
277 d2 = noindex_filespec(algop, name2, mode2, special2);
278 diff_queue(&diff_queued_diff, d1, d2);
279 return 0;
283 /* append basename of F to D */
284 static void append_basename(struct strbuf *path, const char *dir, const char *file)
286 const char *tail = strrchr(file, '/');
288 strbuf_addstr(path, dir);
289 while (path->len && path->buf[path->len - 1] == '/')
290 path->len--;
291 strbuf_addch(path, '/');
292 strbuf_addstr(path, tail ? tail + 1 : file);
296 * DWIM "diff D F" into "diff D/F F" and "diff F D" into "diff F D/F"
297 * Note that we append the basename of F to D/, so "diff a/b/file D"
298 * becomes "diff a/b/file D/file", not "diff a/b/file D/a/b/file".
300 * Return 1 if both paths are directories, 0 otherwise.
302 static int fixup_paths(const char **path, struct strbuf *replacement)
304 struct stat st;
305 unsigned int isdir0 = 0, isdir1 = 0;
306 unsigned int ispipe0 = 0, ispipe1 = 0;
308 if (path[0] != file_from_standard_input && !stat(path[0], &st)) {
309 isdir0 = S_ISDIR(st.st_mode);
310 ispipe0 = S_ISFIFO(st.st_mode);
313 if (path[1] != file_from_standard_input && !stat(path[1], &st)) {
314 isdir1 = S_ISDIR(st.st_mode);
315 ispipe1 = S_ISFIFO(st.st_mode);
318 if ((path[0] == file_from_standard_input && isdir1) ||
319 (isdir0 && path[1] == file_from_standard_input))
320 die(_("cannot compare stdin to a directory"));
322 if ((isdir0 && ispipe1) || (ispipe0 && isdir1))
323 die(_("cannot compare a named pipe to a directory"));
325 /* if both paths are directories, we will enable pathspecs */
326 if (isdir0 && isdir1)
327 return 1;
329 if (isdir0) {
330 append_basename(replacement, path[0], path[1]);
331 path[0] = replacement->buf;
332 } else if (isdir1) {
333 append_basename(replacement, path[1], path[0]);
334 path[1] = replacement->buf;
337 return 0;
340 static const char * const diff_no_index_usage[] = {
341 N_("git diff --no-index [<options>] <path> <path> [<pathspec>...]"),
342 NULL
345 int diff_no_index(struct rev_info *revs, const struct git_hash_algo *algop,
346 int implicit_no_index, int argc, const char **argv)
348 struct pathspec pathspec, *ps = NULL;
349 int i, no_index, skip1 = 0, skip2 = 0;
350 int ret = 1;
351 const char *paths[2];
352 char *to_free[ARRAY_SIZE(paths)] = { 0 };
353 struct strbuf replacement = STRBUF_INIT;
354 const char *prefix = revs->prefix;
355 struct option no_index_options[] = {
356 OPT_BOOL_F(0, "no-index", &no_index, "",
357 PARSE_OPT_NONEG | PARSE_OPT_HIDDEN),
358 OPT_END(),
360 struct option *options;
362 options = add_diff_options(no_index_options, &revs->diffopt);
363 argc = parse_options(argc, argv, revs->prefix, options,
364 diff_no_index_usage, 0);
365 if (argc < 2) {
366 if (implicit_no_index)
367 warning(_("Not a git repository. Use --no-index to "
368 "compare two paths outside a working tree"));
369 usage_with_options(diff_no_index_usage, options);
371 for (i = 0; i < 2; i++) {
372 const char *p = argv[i];
373 if (!strcmp(p, "-"))
375 * stdin should be spelled as "-"; if you have
376 * path that is "-", spell it as "./-".
378 p = file_from_standard_input;
379 else if (prefix)
380 p = to_free[i] = prefix_filename(prefix, p);
381 paths[i] = p;
384 if (fixup_paths(paths, &replacement)) {
385 parse_pathspec(&pathspec, PATHSPEC_FROMTOP | PATHSPEC_ATTR,
386 PATHSPEC_PREFER_FULL | PATHSPEC_NO_REPOSITORY,
387 NULL, &argv[2]);
388 if (pathspec.nr)
389 ps = &pathspec;
391 skip1 = strlen(paths[0]);
392 skip1 += paths[0][skip1] == '/' ? 0 : 1;
393 skip2 = strlen(paths[1]);
394 skip2 += paths[1][skip2] == '/' ? 0 : 1;
395 } else if (argc > 2) {
396 warning(_("Limiting comparison with pathspecs is only "
397 "supported if both paths are directories."));
398 usage_with_options(diff_no_index_usage, options);
400 FREE_AND_NULL(options);
402 revs->diffopt.skip_stat_unmatch = 1;
403 if (!revs->diffopt.output_format)
404 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
406 revs->diffopt.flags.no_index = 1;
408 revs->diffopt.flags.relative_name = 1;
409 revs->diffopt.prefix = prefix;
411 revs->max_count = -2;
412 diff_setup_done(&revs->diffopt);
414 setup_diff_pager(&revs->diffopt);
415 revs->diffopt.flags.exit_with_status = 1;
417 if (queue_diff(&revs->diffopt, algop, paths[0], paths[1], 0, ps,
418 skip1, skip2))
419 goto out;
420 diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
421 diffcore_std(&revs->diffopt);
422 diff_flush(&revs->diffopt);
425 * The return code for --no-index imitates diff(1):
426 * 0 = no changes, 1 = changes, else error
428 ret = diff_result_code(revs);
430 out:
431 for (i = 0; i < ARRAY_SIZE(to_free); i++)
432 free(to_free[i]);
433 strbuf_release(&replacement);
434 if (ps)
435 clear_pathspec(ps);
436 return ret;