Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 277 lines (238 sloc) 6.447 kb
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
1 /*
2 * "diff --no-index" support
3 * Copyright (c) 2007 by Johannes Schindelin
4 * Copyright (c) 2008 by Junio C Hamano
5 */
6
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"
c455c87c » dscho
2008-07-21 Rename path_list to string_list
17 #include "string-list.h"
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
18
c455c87c » dscho
2008-07-21 Rename path_list to string_list
19 static int read_directory(const char *path, struct string_list *list)
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
20 {
21 DIR *dir;
22 struct dirent *e;
23
24 if (!(dir = opendir(path)))
25 return error("Could not open directory %s", path);
26
27 while ((e = readdir(dir)))
28 if (strcmp(".", e->d_name) && strcmp("..", e->d_name))
78a395d3 » qur
2010-06-26 string_list: Fix argument order for string_list_insert
29 string_list_insert(list, e->d_name);
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
30
31 closedir(dir);
32 return 0;
33 }
34
4682d852 » gitster
2012-06-27 diff-index.c: "git diff" has no need to read blob from the standard i…
35 /*
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.
39 */
40 static const char file_from_standard_input[] = "-";
41
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
42 static int get_mode(const char *path, int *mode)
43 {
44 struct stat st;
45
46 if (!path || !strcmp(path, "/dev/null"))
47 *mode = 0;
36adb4ab » dscho
2009-03-07 MinGW: fix diff --no-index /dev/null ...
48 #ifdef _WIN32
49 else if (!strcasecmp(path, "nul"))
50 *mode = 0;
51 #endif
4682d852 » gitster
2012-06-27 diff-index.c: "git diff" has no need to read blob from the standard i…
52 else if (path == file_from_standard_input)
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
53 *mode = create_ce_mode(0666);
418566b6 » dscho
2009-01-29 Fix 'git diff --no-index' with a non-existing symlink target
54 else if (lstat(path, &st))
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
55 return error("Could not access '%s'", path);
56 else
57 *mode = st.st_mode;
58 return 0;
59 }
60
4682d852 » gitster
2012-06-27 diff-index.c: "git diff" has no need to read blob from the standard i…
61 static int populate_from_stdin(struct diff_filespec *s)
62 {
63 struct strbuf buf = STRBUF_INIT;
64 size_t size = 0;
65
66 if (strbuf_read(&buf, 0, 0) < 0)
67 return error("error while reading from stdin %s",
68 strerror(errno));
69
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;
76 }
77
78 static struct diff_filespec *noindex_filespec(const char *name, int mode)
79 {
80 struct diff_filespec *s;
81
82 if (!name)
83 name = "/dev/null";
84 s = alloc_filespec(name);
3b753148 » gitster
2012-08-27 Merge branch 'jk/maint-null-in-trees'
85 fill_filespec(s, null_sha1, 0, mode);
4682d852 » gitster
2012-06-27 diff-index.c: "git diff" has no need to read blob from the standard i…
86 if (name == file_from_standard_input)
87 populate_from_stdin(s);
88 return s;
89 }
90
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
91 static int queue_diff(struct diff_options *o,
875b91b3 » gitster
2012-04-25 diff --no-index: use strbuf for temporary pathnames
92 const char *name1, const char *name2)
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
93 {
94 int mode1 = 0, mode2 = 0;
95
96 if (get_mode(name1, &mode1) || get_mode(name2, &mode2))
97 return -1;
98
99 if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2))
100 return error("file/directory conflict: %s, %s", name1, name2);
101
102 if (S_ISDIR(mode1) || S_ISDIR(mode2)) {
875b91b3 » gitster
2012-04-25 diff --no-index: use strbuf for temporary pathnames
103 struct strbuf buffer1 = STRBUF_INIT;
104 struct strbuf buffer2 = STRBUF_INIT;
183113a5 » Thiago Farina
2010-07-04 string_list: Add STRING_LIST_INIT macro and make use of it.
105 struct string_list p1 = STRING_LIST_INIT_DUP;
106 struct string_list p2 = STRING_LIST_INIT_DUP;
875b91b3 » gitster
2012-04-25 diff --no-index: use strbuf for temporary pathnames
107 int i1, i2, ret = 0;
f3999e03 » bpowers
2012-05-16 diff --no-index: reset temporary buffer lengths on directory iteration
108 size_t len1 = 0, len2 = 0;
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
109
110 if (name1 && read_directory(name1, &p1))
111 return -1;
112 if (name2 && read_directory(name2, &p2)) {
c455c87c » dscho
2008-07-21 Rename path_list to string_list
113 string_list_clear(&p1, 0);
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
114 return -1;
115 }
116
117 if (name1) {
875b91b3 » gitster
2012-04-25 diff --no-index: use strbuf for temporary pathnames
118 strbuf_addstr(&buffer1, name1);
119 if (buffer1.len && buffer1.buf[buffer1.len - 1] != '/')
120 strbuf_addch(&buffer1, '/');
f3999e03 » bpowers
2012-05-16 diff --no-index: reset temporary buffer lengths on directory iteration
121 len1 = buffer1.len;
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
122 }
123
124 if (name2) {
875b91b3 » gitster
2012-04-25 diff --no-index: use strbuf for temporary pathnames
125 strbuf_addstr(&buffer2, name2);
126 if (buffer2.len && buffer2.buf[buffer2.len - 1] != '/')
127 strbuf_addch(&buffer2, '/');
f3999e03 » bpowers
2012-05-16 diff --no-index: reset temporary buffer lengths on directory iteration
128 len2 = buffer2.len;
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
129 }
130
131 for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) {
132 const char *n1, *n2;
133 int comp;
134
f3999e03 » bpowers
2012-05-16 diff --no-index: reset temporary buffer lengths on directory iteration
135 strbuf_setlen(&buffer1, len1);
136 strbuf_setlen(&buffer2, len2);
137
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
138 if (i1 == p1.nr)
139 comp = 1;
140 else if (i2 == p2.nr)
141 comp = -1;
142 else
875b91b3 » gitster
2012-04-25 diff --no-index: use strbuf for temporary pathnames
143 comp = strcmp(p1.items[i1].string, p2.items[i2].string);
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
144
145 if (comp > 0)
146 n1 = NULL;
147 else {
875b91b3 » gitster
2012-04-25 diff --no-index: use strbuf for temporary pathnames
148 strbuf_addstr(&buffer1, p1.items[i1++].string);
149 n1 = buffer1.buf;
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
150 }
151
152 if (comp < 0)
153 n2 = NULL;
154 else {
875b91b3 » gitster
2012-04-25 diff --no-index: use strbuf for temporary pathnames
155 strbuf_addstr(&buffer2, p2.items[i2++].string);
156 n2 = buffer2.buf;
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
157 }
158
159 ret = queue_diff(o, n1, n2);
160 }
c455c87c » dscho
2008-07-21 Rename path_list to string_list
161 string_list_clear(&p1, 0);
162 string_list_clear(&p2, 0);
176a3354 » bpowers
2012-05-16 diff --no-index: don't leak buffers in queue_diff
163 strbuf_release(&buffer1);
164 strbuf_release(&buffer2);
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
165
166 return ret;
167 } else {
168 struct diff_filespec *d1, *d2;
169
170 if (DIFF_OPT_TST(o, REVERSE_DIFF)) {
171 unsigned tmp;
172 const char *tmp_c;
173 tmp = mode1; mode1 = mode2; mode2 = tmp;
174 tmp_c = name1; name1 = name2; name2 = tmp_c;
175 }
176
4682d852 » gitster
2012-06-27 diff-index.c: "git diff" has no need to read blob from the standard i…
177 d1 = noindex_filespec(name1, mode1);
178 d2 = noindex_filespec(name2, mode2);
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
179 diff_queue(&diff_queued_diff, d1, d2);
180 return 0;
181 }
182 }
183
184 void diff_no_index(struct rev_info *revs,
185 int argc, const char **argv,
186 int nongit, const char *prefix)
187 {
3b069b1b » gitster
2012-06-27 diff-index.c: unify handling of command line paths
188 int i, prefixlen;
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
189 int no_index = 0;
190 unsigned options = 0;
c20f5926 » gitster
2012-06-27 diff-index.c: do not pretend paths are pathspecs
191 const char *paths[2];
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
192
193 /* Were we asked to do --no-index explicitly? */
194 for (i = 1; i < argc; i++) {
e423ffd8 » trast
2009-01-06 diff: accept -- when using --no-index
195 if (!strcmp(argv[i], "--")) {
196 i++;
197 break;
198 }
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
199 if (!strcmp(argv[i], "--no-index"))
200 no_index = 1;
201 if (argv[i][0] != '-')
202 break;
203 }
204
04036607 » gitster
2008-05-26 git-diff: allow --no-index semantics a bit more
205 if (!no_index && !nongit) {
206 /*
207 * Inside a git repository, without --no-index. Only
208 * when a path outside the repository is given,
209 * e.g. "git diff /var/tmp/[12]", or "git diff
210 * Makefile /var/tmp/Makefile", allow it to be used as
211 * a colourful "diff" replacement.
212 */
213 if ((argc != i + 2) ||
546e0fd9 » peff
2012-06-21 diff: handle relative paths in no-index
214 (path_inside_repo(prefix, argv[i]) &&
215 path_inside_repo(prefix, argv[i+1])))
04036607 » gitster
2008-05-26 git-diff: allow --no-index semantics a bit more
216 return;
217 }
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
218 if (argc != i + 2)
d74bb308 » jrn
2009-11-09 diff --no-index: make the usage string less scary
219 usagef("git diff %s <path> <path>",
220 no_index ? "--no-index" : "[--no-index]");
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
221
222 diff_setup(&revs->diffopt);
223 for (i = 1; i < argc - 2; ) {
224 int j;
225 if (!strcmp(argv[i], "--no-index"))
226 i++;
a324fc45 » trast
2009-01-07 diff --no-index -q: fix endless loop
227 else if (!strcmp(argv[i], "-q")) {
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
228 options |= DIFF_SILENT_ON_REMOVED;
a324fc45 » trast
2009-01-07 diff --no-index -q: fix endless loop
229 i++;
230 }
e423ffd8 » trast
2009-01-06 diff: accept -- when using --no-index
231 else if (!strcmp(argv[i], "--"))
232 i++;
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
233 else {
234 j = diff_opt_parse(&revs->diffopt, argv + i, argc - i);
235 if (!j)
236 die("invalid diff option/value: %s", argv[i]);
237 i += j;
238 }
239 }
240
3b069b1b » gitster
2012-06-27 diff-index.c: unify handling of command line paths
241 prefixlen = prefix ? strlen(prefix) : 0;
242 for (i = 0; i < 2; i++) {
243 const char *p = argv[argc - 2 + i];
244 if (!strcmp(p, "-"))
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
245 /*
3b069b1b » gitster
2012-06-27 diff-index.c: unify handling of command line paths
246 * stdin should be spelled as "-"; if you have
247 * path that is "-", spell it as "./-".
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
248 */
4682d852 » gitster
2012-06-27 diff-index.c: "git diff" has no need to read blob from the standard i…
249 p = file_from_standard_input;
3b069b1b » gitster
2012-06-27 diff-index.c: unify handling of command line paths
250 else if (prefixlen)
251 p = xstrdup(prefix_filename(prefix, prefixlen, p));
252 paths[i] = p;
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
253 }
d61027b2 » Michael Spang
2009-02-18 Skip timestamp differences for diff --no-index
254 revs->diffopt.skip_stat_unmatch = 1;
5d83f9c1 » Johannes Sixt
2009-03-25 diff --no-index: Do not generate patch output if other output is requ…
255 if (!revs->diffopt.output_format)
256 revs->diffopt.output_format = DIFF_FORMAT_PATCH;
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
257
258 DIFF_OPT_SET(&revs->diffopt, NO_INDEX);
259
260 revs->max_count = -2;
28452655 » trast
2012-08-03 diff_setup_done(): return void
261 diff_setup_done(&revs->diffopt);
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
262
af63b543 » peff
2012-06-15 do not run pager with diff --no-index --quiet
263 setup_diff_pager(&revs->diffopt);
264 DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS);
265
c20f5926 » gitster
2012-06-27 diff-index.c: do not pretend paths are pathspecs
266 if (queue_diff(&revs->diffopt, paths[0], paths[1]))
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
267 exit(1);
a5a818ee » gitster
2008-08-18 diff: vary default prefix depending on what are compared
268 diff_set_mnemonic_prefix(&revs->diffopt, "1/", "2/");
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
269 diffcore_std(&revs->diffopt);
270 diff_flush(&revs->diffopt);
271
272 /*
273 * The return code for --no-index imitates diff(1):
274 * 0 = no changes, 1 = changes, else error
275 */
304970dd » thenigan
2012-06-21 diff-no-index: exit(1) if 'diff --quiet <repo file> <external file>' …
276 exit(diff_result_code(&revs->diffopt, 0));
0569e9b8 » gitster
2008-05-23 "git diff": do not ignore index without --no-index
277 }
Something went wrong with that request. Please try again.