fixup.cc5711424b7ae36276a40c06ede5d95f87ca20f0
[git/dscho.git] / diff-no-index.c
blob7b05face5167ee1c41c0bf4f097a53826f945e38
1 /*
2 * "diff --no-index" support
3 * Copyright (c) 2007 by Johannes Schindelin
4 * Copyright (c) 2008 by Junio C Hamano
5 */
7 #include "cache.h"
8 #include "color.h"
9 #include "commit.h"
10 #include "blob.h"
11 #include "tag.h"
12 #include "diff.h"
13 #include "diffcore.h"
14 #include "revision.h"
15 #include "log-tree.h"
16 #include "builtin.h"
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)
32 int i;
34 if (!options->basename_excludes)
35 return 0;
37 for (i = 0; i < options->basename_excludes->nr; i++)
38 if (!fnmatch(options->basename_excludes->items[i].string,
39 basename, 0))
40 return 1;
41 return 0;
44 void free_basename_excludes(struct diff_options *options)
46 if (!options->basename_excludes)
47 return;
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)
56 DIR *dir;
57 struct dirent *e;
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);
67 closedir(dir);
68 return 0;
71 static int get_mode(const char *path, int *mode)
73 struct stat st;
75 if (!path || !strcmp(path, "/dev/null"))
76 *mode = 0;
77 #ifdef _WIN32
78 else if (!strcasecmp(path, "nul"))
79 *mode = 0;
80 #endif
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);
85 else
86 *mode = st.st_mode;
87 return 0;
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))
96 return -1;
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))
107 return -1;
108 if (name2 && read_directory(name2, &p2, o)) {
109 string_list_clear(&p1, 0);
110 return -1;
113 if (name1) {
114 len1 = strlen(name1);
115 if (len1 > 0 && name1[len1 - 1] == '/')
116 len1--;
117 memcpy(buffer1, name1, len1);
118 buffer1[len1++] = '/';
121 if (name2) {
122 len2 = strlen(name2);
123 if (len2 > 0 && name2[len2 - 1] == '/')
124 len2--;
125 memcpy(buffer2, name2, len2);
126 buffer2[len2++] = '/';
129 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
130 const char *n1, *n2;
131 int comp;
133 if (i1 == p1.nr)
134 comp = 1;
135 else if (i2 == p2.nr)
136 comp = -1;
137 else
138 comp = strcmp(p1.items[i1].string,
139 p2.items[i2].string);
141 if (comp > 0)
142 n1 = NULL;
143 else {
144 n1 = buffer1;
145 strncpy(buffer1 + len1, p1.items[i1++].string,
146 PATH_MAX - len1);
149 if (comp < 0)
150 n2 = NULL;
151 else {
152 n2 = buffer2;
153 strncpy(buffer2 + len2, p2.items[i2++].string,
154 PATH_MAX - len2);
157 ret = queue_diff(o, n1, n2);
159 string_list_clear(&p1, 0);
160 string_list_clear(&p2, 0);
162 return ret;
163 } else {
164 struct diff_filespec *d1, *d2;
166 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
167 unsigned tmp;
168 const char *tmp_c;
169 tmp = mode1; mode1 = mode2; mode2 = tmp;
170 tmp_c = name1; name1 = name2; name2 = tmp_c;
173 if (!name1)
174 name1 = "/dev/null";
175 if (!name2)
176 name2 = "/dev/null";
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);
183 return 0;
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;
194 size_t len;
196 if (!is_absolute_path(path))
197 return 0;
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] != '/'))
202 return 1;
203 return 0;
206 void diff_no_index(struct rev_info *revs,
207 int argc, const char **argv,
208 int nongit, const char *prefix)
210 int i;
211 int no_index = 0;
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], "--")) {
217 i++;
218 break;
220 if (argv[i][0] != '-')
221 break;
222 if (!strcmp(argv[i], "--no-index"))
223 no_index = 1;
224 if (!strcmp(argv[i], "-x"))
225 i++;
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])))
239 return;
241 if (argc != i + 2)
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; ) {
247 int j;
248 if (!strcmp(argv[i], "--no-index"))
249 i++;
250 else if (!strcmp(argv[i], "-q")) {
251 options |= DIFF_SILENT_ON_REMOVED;
252 i++;
254 else if (!strcmp(argv[i], "--"))
255 i++;
256 else {
257 j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
258 if (!j)
259 die("invalid diff option/value: %s", argv[i]);
260 i += j;
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))
269 setup_pager();
271 if (prefix) {
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 ./-.
281 p = (strcmp(p, "-")
282 ? xstrdup(prefix_filename(prefix, len, p))
283 : p);
284 revs->diffopt.paths[i] = p;
287 else
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]))
303 exit(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);