2 * "diff --no-index" support
3 * Copyright (c) 2007 by Johannes Schindelin
4 * Copyright (c) 2008 by Junio C Hamano
7 #define DISABLE_SIGN_COMPARE_WARNINGS
9 #include "git-compat-util.h"
17 #include "parse-options.h"
19 #include "string-list.h"
22 static int read_directory_contents(const char *path
, struct string_list
*list
,
23 const struct pathspec
*pathspec
,
26 struct strbuf match
= STRBUF_INIT
;
31 if (!(dir
= opendir(path
)))
32 return error("Could not open directory %s", path
);
35 strbuf_addstr(&match
, path
);
36 strbuf_complete(&match
, '/');
37 strbuf_remove(&match
, 0, skip
);
42 while ((e
= readdir_skip_dot_and_dotdot(dir
))) {
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
);
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
,
65 string_list_insert(list
, e
->d_name
);
68 strbuf_release(&match
);
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.
90 static int get_mode(const char *path
, int *mode
, enum special
*special
)
94 if (!path
|| !strcmp(path
, "/dev/null")) {
96 #ifdef GIT_WINDOWS_NATIVE
97 } else if (!strcasecmp(path
, "nul")) {
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
);
109 * For paths on the command-line treat named pipes and symbolic
110 * links that resolve to a named pipe specially.
114 (S_ISLNK(*mode
) && !stat(path
, &st
) && S_ISFIFO(st
.st_mode
)))) {
115 *mode
= create_ce_mode(0666);
116 *special
= SPECIAL_PIPE
;
122 static void populate_common(struct diff_filespec
*s
, struct strbuf
*buf
)
126 s
->should_munmap
= 0;
127 s
->data
= strbuf_detach(buf
, &size
);
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
);
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
;
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
);
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
))
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
);
192 /* 1 is file that is deleted */
193 d1
= noindex_filespec(algop
, name1
, mode1
, special1
);
194 d2
= noindex_filespec(algop
, NULL
, 0, SPECIAL_NONE
);
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
;
210 size_t len1
= 0, len2
= 0;
212 if (name1
&& read_directory_contents(name1
, &p1
, ps
, skip1
))
214 if (name2
&& read_directory_contents(name2
, &p2
, ps
, skip2
)) {
215 string_list_clear(&p1
, 0);
220 strbuf_addstr(&buffer1
, name1
);
221 strbuf_complete(&buffer1
, '/');
226 strbuf_addstr(&buffer2
, name2
);
227 strbuf_complete(&buffer2
, '/');
231 for (i1
= i2
= 0; !ret
&& (i1
< p1
.nr
|| i2
< p2
.nr
); ) {
235 strbuf_setlen(&buffer1
, len1
);
236 strbuf_setlen(&buffer2
, len2
);
240 else if (i2
== p2
.nr
)
243 comp
= strcmp(p1
.items
[i1
].string
, p2
.items
[i2
].string
);
248 strbuf_addstr(&buffer1
, p1
.items
[i1
++].string
);
255 strbuf_addstr(&buffer2
, p2
.items
[i2
++].string
);
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
);
268 struct diff_filespec
*d1
, *d2
;
270 if (o
->flags
.reverse_diff
) {
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
);
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] == '/')
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
)
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
)
330 append_basename(replacement
, path
[0], path
[1]);
331 path
[0] = replacement
->buf
;
333 append_basename(replacement
, path
[1], path
[0]);
334 path
[1] = replacement
->buf
;
340 static const char * const diff_no_index_usage
[] = {
341 N_("git diff --no-index [<options>] <path> <path> [<pathspec>...]"),
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;
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
),
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);
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
];
375 * stdin should be spelled as "-"; if you have
376 * path that is "-", spell it as "./-".
378 p
= file_from_standard_input
;
380 p
= to_free
[i
] = prefix_filename(prefix
, p
);
384 if (fixup_paths(paths
, &replacement
)) {
385 parse_pathspec(&pathspec
, PATHSPEC_FROMTOP
| PATHSPEC_ATTR
,
386 PATHSPEC_PREFER_FULL
| PATHSPEC_NO_REPOSITORY
,
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
,
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
);
431 for (i
= 0; i
< ARRAY_SIZE(to_free
); i
++)
433 strbuf_release(&replacement
);