diff-index.c: "git diff" has no need to read blob from the standard input
[git/mjg.git] / diff-no-index.c
blob09656bca1d192f21f36ff56f2446c6bcc73442f4
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;
36 * This should be "(standard input)" or something, but it will
37 * probably expose many more breakages in the way no-index code
38 * is bolted onto the diff callchain.
40 static const char file_from_standard_input[] = "-";
42 static int get_mode(const char *path, int *mode)
44 struct stat st;
46 if (!path || !strcmp(path, "/dev/null"))
47 *mode = 0;
48 #ifdef _WIN32
49 else if (!strcasecmp(path, "nul"))
50 *mode = 0;
51 #endif
52 else if (path == file_from_standard_input)
53 *mode = create_ce_mode(0666);
54 else if (lstat(path, &st))
55 return error("Could not access '%s'", path);
56 else
57 *mode = st.st_mode;
58 return 0;
61 static int populate_from_stdin(struct diff_filespec *s)
63 struct strbuf buf = STRBUF_INIT;
64 size_t size = 0;
66 if (strbuf_read(&buf, 0, 0) < 0)
67 return error("error while reading from stdin %s",
68 strerror(errno));
70 s->should_munmap = 0;
71 s->data = strbuf_detach(&buf, &size);
72 s->size = size;
73 s->should_free = 1;
74 s->is_stdin = 1;
75 return 0;
78 static struct diff_filespec *noindex_filespec(const char *name, int mode)
80 struct diff_filespec *s;
82 if (!name)
83 name = "/dev/null";
84 s = alloc_filespec(name);
85 fill_filespec(s, null_sha1, mode);
86 if (name == file_from_standard_input)
87 populate_from_stdin(s);
88 return s;
91 static int queue_diff(struct diff_options *o,
92 const char *name1, const char *name2)
94 int mode1 = 0, mode2 = 0;
96 if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
97 return -1;
99 if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
100 return error("file/directory conflict: %s, %s", name1, name2);
102 if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
103 char buffer1[PATH_MAX], buffer2[PATH_MAX];
104 struct string_list p1 = STRING_LIST_INIT_DUP;
105 struct string_list p2 = STRING_LIST_INIT_DUP;
106 int len1 = 0, len2 = 0, i1, i2, ret = 0;
108 if (name1 && read_directory(name1, &p1))
109 return -1;
110 if (name2 && read_directory(name2, &p2)) {
111 string_list_clear(&p1, 0);
112 return -1;
115 if (name1) {
116 len1 = strlen(name1);
117 if (len1 > 0 && name1[len1 - 1] == '/')
118 len1--;
119 memcpy(buffer1, name1, len1);
120 buffer1[len1++] = '/';
123 if (name2) {
124 len2 = strlen(name2);
125 if (len2 > 0 && name2[len2 - 1] == '/')
126 len2--;
127 memcpy(buffer2, name2, len2);
128 buffer2[len2++] = '/';
131 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
132 const char *n1, *n2;
133 int comp;
135 if (i1 == p1.nr)
136 comp = 1;
137 else if (i2 == p2.nr)
138 comp = -1;
139 else
140 comp = strcmp(p1.items[i1].string,
141 p2.items[i2].string);
143 if (comp > 0)
144 n1 = NULL;
145 else {
146 n1 = buffer1;
147 strncpy(buffer1 + len1, p1.items[i1++].string,
148 PATH_MAX - len1);
151 if (comp < 0)
152 n2 = NULL;
153 else {
154 n2 = buffer2;
155 strncpy(buffer2 + len2, p2.items[i2++].string,
156 PATH_MAX - len2);
159 ret = queue_diff(o, n1, n2);
161 string_list_clear(&p1, 0);
162 string_list_clear(&p2, 0);
164 return ret;
165 } else {
166 struct diff_filespec *d1, *d2;
168 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
169 unsigned tmp;
170 const char *tmp_c;
171 tmp = mode1; mode1 = mode2; mode2 = tmp;
172 tmp_c = name1; name1 = name2; name2 = tmp_c;
175 d1 = noindex_filespec(name1, mode1);
176 d2 = noindex_filespec(name2, mode2);
177 diff_queue(&diff_queued_diff, d1, d2);
178 return 0;
182 static int path_outside_repo(const char *path)
184 const char *work_tree;
185 size_t len;
187 if (!is_absolute_path(path))
188 return 0;
189 work_tree = get_git_work_tree();
190 if (!work_tree)
191 return 1;
192 len = strlen(work_tree);
193 if (strncmp(path, work_tree, len) ||
194 (path[len] != '\0' && path[len] != '/'))
195 return 1;
196 return 0;
199 void diff_no_index(struct rev_info *revs,
200 int argc, const char **argv,
201 int nongit, const char *prefix)
203 int i, prefixlen;
204 int no_index = 0;
205 unsigned options = 0;
206 const char *paths[2];
208 /* Were we asked to do --no-index explicitly? */
209 for (i = 1; i < argc; i++) {
210 if (!strcmp(argv[i], "--")) {
211 i++;
212 break;
214 if (!strcmp(argv[i], "--no-index"))
215 no_index = 1;
216 if (argv[i][0] != '-')
217 break;
220 if (!no_index && !nongit) {
222 * Inside a git repository, without --no-index. Only
223 * when a path outside the repository is given,
224 * e.g. "git diff /var/tmp/[12]", or "git diff
225 * Makefile /var/tmp/Makefile", allow it to be used as
226 * a colourful "diff" replacement.
228 if ((argc != i + 2) ||
229 (!path_outside_repo(argv[i]) &&
230 !path_outside_repo(argv[i+1])))
231 return;
233 if (argc != i + 2)
234 usagef("git diff %s <path> <path>",
235 no_index ? "--no-index" : "[--no-index]");
237 diff_setup(&revs->diffopt);
238 for (i = 1; i < argc - 2; ) {
239 int j;
240 if (!strcmp(argv[i], "--no-index"))
241 i++;
242 else if (!strcmp(argv[i], "-q")) {
243 options |= DIFF_SILENT_ON_REMOVED;
244 i++;
246 else if (!strcmp(argv[i], "--"))
247 i++;
248 else {
249 j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
250 if (!j)
251 die("invalid diff option/value: %s", argv[i]);
252 i += j;
257 * If the user asked for our exit code then don't start a
258 * pager or we would end up reporting its exit code instead.
260 if (!DIFF_OPT_TST(&revs->diffopt, EXIT_WITH_STATUS))
261 setup_pager();
263 prefixlen = prefix ? strlen(prefix) : 0;
264 for (i = 0; i < 2; i++) {
265 const char *p = argv[argc - 2 + i];
266 if (!strcmp(p, "-"))
268 * stdin should be spelled as "-"; if you have
269 * path that is "-", spell it as "./-".
271 p = file_from_standard_input;
272 else if (prefixlen)
273 p = xstrdup(prefix_filename(prefix, prefixlen, p));
274 paths[i] = p;
276 revs->diffopt.skip_stat_unmatch = 1;
277 if (!revs->diffopt.output_format)
278 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
280 DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
281 DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
283 revs->max_count = -2;
284 if (diff_setup_done(&revs->diffopt) < 0)
285 die("diff_setup_done failed");
287 if (queue_diff(&revs->diffopt, paths[0], paths[1]))
288 exit(1);
289 diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
290 diffcore_std(&revs->diffopt);
291 diff_flush(&revs->diffopt);
294 * The return code for --no-index imitates diff(1):
295 * 0 = no changes, 1 = changes, else error
297 exit(revs->diffopt.found_changes);