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 void add_basename_exclude(const char *exclude
, struct diff_options
*opts
)
21 if (!opts
->basename_excludes
) {
22 opts
->basename_excludes
=
23 xcalloc(sizeof(struct string_list
), 1);
24 opts
->basename_excludes
->strdup_strings
= 1;
27 string_list_append(exclude
, opts
->basename_excludes
);
30 int basename_is_excluded(const char *basename
, struct diff_options
*options
)
34 if (!options
->basename_excludes
)
37 for (i
= 0; i
< options
->basename_excludes
->nr
; i
++)
38 if (!fnmatch(options
->basename_excludes
->items
[i
].string
,
44 void free_basename_excludes(struct diff_options
*options
)
46 if (!options
->basename_excludes
)
48 string_list_clear(options
->basename_excludes
, 0);
49 free(options
->basename_excludes
);
50 options
->basename_excludes
= NULL
;
53 static int read_directory(const char *path
, struct string_list
*list
,
54 struct diff_options
*options
)
59 if (!(dir
= opendir(path
)))
60 return error("Could not open directory %s", path
);
62 while ((e
= readdir(dir
)))
63 if (strcmp(".", e
->d_name
) && strcmp("..", e
->d_name
) &&
64 !basename_is_excluded(e
->d_name
, options
))
65 string_list_insert(e
->d_name
, list
);
71 static int get_mode(const char *path
, int *mode
)
75 if (!path
|| !strcmp(path
, "/dev/null"))
78 else if (!strcasecmp(path
, "nul"))
81 else if (!strcmp(path
, "-"))
82 *mode
= create_ce_mode(0666);
83 else if (lstat(path
, &st
))
84 return error("Could not access '%s'", path
);
90 static int queue_diff(struct diff_options
*o
,
91 const char *name1
, const char *name2
)
93 int mode1
= 0, mode2
= 0;
95 if (get_mode(name1
, &mode1
) || get_mode(name2
, &mode2
))
98 if (mode1
&& mode2
&& S_ISDIR(mode1
) != S_ISDIR(mode2
))
99 return error("file/directory conflict: %s, %s", name1
, name2
);
101 if (S_ISDIR(mode1
) || S_ISDIR(mode2
)) {
102 char buffer1
[PATH_MAX
], buffer2
[PATH_MAX
];
103 struct string_list p1
= {NULL
, 0, 0, 1}, p2
= {NULL
, 0, 0, 1};
104 int len1
= 0, len2
= 0, i1
, i2
, ret
= 0;
106 if (name1
&& read_directory(name1
, &p1
, o
))
108 if (name2
&& read_directory(name2
, &p2
, o
)) {
109 string_list_clear(&p1
, 0);
114 len1
= strlen(name1
);
115 if (len1
> 0 && name1
[len1
- 1] == '/')
117 memcpy(buffer1
, name1
, len1
);
118 buffer1
[len1
++] = '/';
122 len2
= strlen(name2
);
123 if (len2
> 0 && name2
[len2
- 1] == '/')
125 memcpy(buffer2
, name2
, len2
);
126 buffer2
[len2
++] = '/';
129 for (i1
= i2
= 0; !ret
&& (i1
< p1
.nr
|| i2
< p2
.nr
); ) {
135 else if (i2
== p2
.nr
)
138 comp
= strcmp(p1
.items
[i1
].string
,
139 p2
.items
[i2
].string
);
145 strncpy(buffer1
+ len1
, p1
.items
[i1
++].string
,
153 strncpy(buffer2
+ len2
, p2
.items
[i2
++].string
,
157 ret
= queue_diff(o
, n1
, n2
);
159 string_list_clear(&p1
, 0);
160 string_list_clear(&p2
, 0);
164 struct diff_filespec
*d1
, *d2
;
166 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
169 tmp
= mode1
; mode1
= mode2
; mode2
= tmp
;
170 tmp_c
= name1
; name1
= name2
; name2
= tmp_c
;
177 d1
= alloc_filespec(name1
);
178 d2
= alloc_filespec(name2
);
179 fill_filespec(d1
, null_sha1
, mode1
);
180 fill_filespec(d2
, null_sha1
, mode2
);
182 diff_queue(&diff_queued_diff
, d1
, d2
);
187 static int path_outside_repo(const char *path
)
190 * We have already done setup_git_directory_gently() so we
191 * know we are inside a git work tree already.
193 const char *work_tree
;
196 if (!is_absolute_path(path
))
198 work_tree
= get_git_work_tree();
199 len
= strlen(work_tree
);
200 if (strncmp(path
, work_tree
, len
) ||
201 (path
[len
] != '\0' && path
[len
] != '/'))
206 void diff_no_index(struct rev_info
*revs
,
207 int argc
, const char **argv
,
208 int nongit
, const char *prefix
)
212 unsigned options
= 0;
214 /* Were we asked to do --no-index explicitly? */
215 for (i
= 1; i
< argc
; i
++) {
216 if (!strcmp(argv
[i
], "--")) {
220 if (argv
[i
][0] != '-')
222 if (!strcmp(argv
[i
], "--no-index"))
224 if (!strcmp(argv
[i
], "-x"))
228 if (!no_index
&& !nongit
) {
230 * Inside a git repository, without --no-index. Only
231 * when a path outside the repository is given,
232 * e.g. "git diff /var/tmp/[12]", or "git diff
233 * Makefile /var/tmp/Makefile", allow it to be used as
234 * a colourful "diff" replacement.
236 if ((argc
!= i
+ 2) ||
237 (!path_outside_repo(argv
[i
]) &&
238 !path_outside_repo(argv
[i
+1])))
242 usagef("git diff %s <path> <path>",
243 no_index
? "--no-index" : "[--no-index]");
245 diff_setup(&revs
->diffopt
);
246 for (i
= 1; i
< argc
- 2; ) {
248 if (!strcmp(argv
[i
], "--no-index"))
250 else if (!strcmp(argv
[i
], "-q")) {
251 options
|= DIFF_SILENT_ON_REMOVED
;
254 else if (!strcmp(argv
[i
], "--"))
257 j
= diff_opt_parse(&revs
->diffopt
, argv
+ i
, argc
- i
);
259 die("invalid diff option/value: %s", argv
[i
]);
265 * If the user asked for our exit code then don't start a
266 * pager or we would end up reporting its exit code instead.
268 if (!DIFF_OPT_TST(&revs
->diffopt
, EXIT_WITH_STATUS
))
272 int len
= strlen(prefix
);
274 revs
->diffopt
.paths
= xcalloc(2, sizeof(char *));
275 for (i
= 0; i
< 2; i
++) {
276 const char *p
= argv
[argc
- 2 + i
];
278 * stdin should be spelled as '-'; if you have
279 * path that is '-', spell it as ./-.
282 ? xstrdup(prefix_filename(prefix
, len
, p
))
284 revs
->diffopt
.paths
[i
] = p
;
288 revs
->diffopt
.paths
= argv
+ argc
- 2;
289 revs
->diffopt
.nr_paths
= 2;
290 revs
->diffopt
.skip_stat_unmatch
= 1;
291 if (!revs
->diffopt
.output_format
)
292 revs
->diffopt
.output_format
= DIFF_FORMAT_PATCH
;
294 DIFF_OPT_SET(&revs
->diffopt
, EXIT_WITH_STATUS
);
295 DIFF_OPT_SET(&revs
->diffopt
, NO_INDEX
);
297 revs
->max_count
= -2;
298 if (diff_setup_done(&revs
->diffopt
) < 0)
299 die("diff_setup_done failed");
301 if (queue_diff(&revs
->diffopt
, revs
->diffopt
.paths
[0],
302 revs
->diffopt
.paths
[1]))
304 diff_set_mnemonic_prefix(&revs
->diffopt
, "1/", "2/");
305 diffcore_std(&revs
->diffopt
);
306 diff_flush(&revs
->diffopt
);
309 * The return code for --no-index imitates diff(1):
310 * 0 = no changes, 1 = changes, else error
312 exit(revs
->diffopt
.found_changes
);