Merge branch 'tr/maint-show-walk'
[git/git-svn.git] / diff-no-index.c
blobbeec49b5ac98c6d3ea895adcf66265f3a2fc7875
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 static int read_directory(const char *path, struct string_list *list)
21 DIR *dir;
22 struct dirent *e;
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(list, e->d_name);
31 closedir(dir);
32 return 0;
35 static int get_mode(const char *path, int *mode)
37 struct stat st;
39 if (!path || !strcmp(path, "/dev/null"))
40 *mode = 0;
41 #ifdef _WIN32
42 else if (!strcasecmp(path, "nul"))
43 *mode = 0;
44 #endif
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);
49 else
50 *mode = st.st_mode;
51 return 0;
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))
60 return -1;
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 struct strbuf buffer1 = STRBUF_INIT;
67 struct strbuf buffer2 = STRBUF_INIT;
68 struct string_list p1 = STRING_LIST_INIT_DUP;
69 struct string_list p2 = STRING_LIST_INIT_DUP;
70 int i1, i2, ret = 0;
71 size_t len1 = 0, len2 = 0;
73 if (name1 && read_directory(name1, &p1))
74 return -1;
75 if (name2 && read_directory(name2, &p2)) {
76 string_list_clear(&p1, 0);
77 return -1;
80 if (name1) {
81 strbuf_addstr(&buffer1, name1);
82 if (buffer1.len && buffer1.buf[buffer1.len - 1] != '/')
83 strbuf_addch(&buffer1, '/');
84 len1 = buffer1.len;
87 if (name2) {
88 strbuf_addstr(&buffer2, name2);
89 if (buffer2.len && buffer2.buf[buffer2.len - 1] != '/')
90 strbuf_addch(&buffer2, '/');
91 len2 = buffer2.len;
94 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
95 const char *n1, *n2;
96 int comp;
98 strbuf_setlen(&buffer1, len1);
99 strbuf_setlen(&buffer2, len2);
101 if (i1 == p1.nr)
102 comp = 1;
103 else if (i2 == p2.nr)
104 comp = -1;
105 else
106 comp = strcmp(p1.items[i1].string, p2.items[i2].string);
108 if (comp > 0)
109 n1 = NULL;
110 else {
111 strbuf_addstr(&buffer1, p1.items[i1++].string);
112 n1 = buffer1.buf;
115 if (comp < 0)
116 n2 = NULL;
117 else {
118 strbuf_addstr(&buffer2, p2.items[i2++].string);
119 n2 = buffer2.buf;
122 ret = queue_diff(o, n1, n2);
124 string_list_clear(&p1, 0);
125 string_list_clear(&p2, 0);
126 strbuf_release(&buffer1);
127 strbuf_release(&buffer2);
129 return ret;
130 } else {
131 struct diff_filespec *d1, *d2;
133 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
134 unsigned tmp;
135 const char *tmp_c;
136 tmp = mode1; mode1 = mode2; mode2 = tmp;
137 tmp_c = name1; name1 = name2; name2 = tmp_c;
140 if (!name1)
141 name1 = "/dev/null";
142 if (!name2)
143 name2 = "/dev/null";
144 d1 = alloc_filespec(name1);
145 d2 = alloc_filespec(name2);
146 fill_filespec(d1, null_sha1, mode1);
147 fill_filespec(d2, null_sha1, mode2);
149 diff_queue(&diff_queued_diff, d1, d2);
150 return 0;
154 void diff_no_index(struct rev_info *revs,
155 int argc, const char **argv,
156 int nongit, const char *prefix)
158 int i;
159 int no_index = 0;
160 unsigned options = 0;
162 /* Were we asked to do --no-index explicitly? */
163 for (i = 1; i < argc; i++) {
164 if (!strcmp(argv[i], "--")) {
165 i++;
166 break;
168 if (!strcmp(argv[i], "--no-index"))
169 no_index = 1;
170 if (argv[i][0] != '-')
171 break;
174 if (!no_index && !nongit) {
176 * Inside a git repository, without --no-index. Only
177 * when a path outside the repository is given,
178 * e.g. "git diff /var/tmp/[12]", or "git diff
179 * Makefile /var/tmp/Makefile", allow it to be used as
180 * a colourful "diff" replacement.
182 if ((argc != i + 2) ||
183 (path_inside_repo(prefix, argv[i]) &&
184 path_inside_repo(prefix, argv[i+1])))
185 return;
187 if (argc != i + 2)
188 usagef("git diff %s <path> <path>",
189 no_index ? "--no-index" : "[--no-index]");
191 diff_setup(&revs->diffopt);
192 for (i = 1; i < argc - 2; ) {
193 int j;
194 if (!strcmp(argv[i], "--no-index"))
195 i++;
196 else if (!strcmp(argv[i], "-q")) {
197 options |= DIFF_SILENT_ON_REMOVED;
198 i++;
200 else if (!strcmp(argv[i], "--"))
201 i++;
202 else {
203 j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
204 if (!j)
205 die("invalid diff option/value: %s", argv[i]);
206 i += j;
210 if (prefix) {
211 int len = strlen(prefix);
212 const char *paths[3];
213 memset(paths, 0, sizeof(paths));
215 for (i = 0; i < 2; i++) {
216 const char *p = argv[argc - 2 + i];
218 * stdin should be spelled as '-'; if you have
219 * path that is '-', spell it as ./-.
221 p = (strcmp(p, "-")
222 ? xstrdup(prefix_filename(prefix, len, p))
223 : p);
224 paths[i] = p;
226 diff_tree_setup_paths(paths, &revs->diffopt);
228 else
229 diff_tree_setup_paths(argv + argc - 2, &revs->diffopt);
230 revs->diffopt.skip_stat_unmatch = 1;
231 if (!revs->diffopt.output_format)
232 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
234 DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
236 revs->max_count = -2;
237 if (diff_setup_done(&revs->diffopt) < 0)
238 die("diff_setup_done failed");
240 setup_diff_pager(&revs->diffopt);
241 DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
243 if (queue_diff(&revs->diffopt, revs->diffopt.pathspec.raw[0],
244 revs->diffopt.pathspec.raw[1]))
245 exit(1);
246 diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
247 diffcore_std(&revs->diffopt);
248 diff_flush(&revs->diffopt);
251 * The return code for --no-index imitates diff(1):
252 * 0 = no changes, 1 = changes, else error
254 exit(diff_result_code(&revs->diffopt, 0));