]>
Commit | Line | Data |
---|---|---|
0569e9b8 JH |
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" | |
17 | #include "path-list.h" | |
18 | ||
19 | static int read_directory(const char *path, struct path_list *list) | |
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)) | |
29 | path_list_insert(e->d_name, list); | |
30 | ||
31 | closedir(dir); | |
32 | return 0; | |
33 | } | |
34 | ||
35 | static int get_mode(const char *path, int *mode) | |
36 | { | |
37 | struct stat st; | |
38 | ||
39 | if (!path || !strcmp(path, "/dev/null")) | |
40 | *mode = 0; | |
41 | else if (!strcmp(path, "-")) | |
42 | *mode = create_ce_mode(0666); | |
43 | else if (stat(path, &st)) | |
44 | return error("Could not access '%s'", path); | |
45 | else | |
46 | *mode = st.st_mode; | |
47 | return 0; | |
48 | } | |
49 | ||
50 | static int queue_diff(struct diff_options *o, | |
51 | const char *name1, const char *name2) | |
52 | { | |
53 | int mode1 = 0, mode2 = 0; | |
54 | ||
55 | if (get_mode(name1, &mode1) || get_mode(name2, &mode2)) | |
56 | return -1; | |
57 | ||
58 | if (mode1 && mode2 && S_ISDIR(mode1) != S_ISDIR(mode2)) | |
59 | return error("file/directory conflict: %s, %s", name1, name2); | |
60 | ||
61 | if (S_ISDIR(mode1) || S_ISDIR(mode2)) { | |
62 | char buffer1[PATH_MAX], buffer2[PATH_MAX]; | |
63 | struct path_list p1 = {NULL, 0, 0, 1}, p2 = {NULL, 0, 0, 1}; | |
64 | int len1 = 0, len2 = 0, i1, i2, ret = 0; | |
65 | ||
66 | if (name1 && read_directory(name1, &p1)) | |
67 | return -1; | |
68 | if (name2 && read_directory(name2, &p2)) { | |
69 | path_list_clear(&p1, 0); | |
70 | return -1; | |
71 | } | |
72 | ||
73 | if (name1) { | |
74 | len1 = strlen(name1); | |
75 | if (len1 > 0 && name1[len1 - 1] == '/') | |
76 | len1--; | |
77 | memcpy(buffer1, name1, len1); | |
78 | buffer1[len1++] = '/'; | |
79 | } | |
80 | ||
81 | if (name2) { | |
82 | len2 = strlen(name2); | |
83 | if (len2 > 0 && name2[len2 - 1] == '/') | |
84 | len2--; | |
85 | memcpy(buffer2, name2, len2); | |
86 | buffer2[len2++] = '/'; | |
87 | } | |
88 | ||
89 | for (i1 = i2 = 0; !ret && (i1 < p1.nr || i2 < p2.nr); ) { | |
90 | const char *n1, *n2; | |
91 | int comp; | |
92 | ||
93 | if (i1 == p1.nr) | |
94 | comp = 1; | |
95 | else if (i2 == p2.nr) | |
96 | comp = -1; | |
97 | else | |
98 | comp = strcmp(p1.items[i1].path, | |
99 | p2.items[i2].path); | |
100 | ||
101 | if (comp > 0) | |
102 | n1 = NULL; | |
103 | else { | |
104 | n1 = buffer1; | |
105 | strncpy(buffer1 + len1, p1.items[i1++].path, | |
106 | PATH_MAX - len1); | |
107 | } | |
108 | ||
109 | if (comp < 0) | |
110 | n2 = NULL; | |
111 | else { | |
112 | n2 = buffer2; | |
113 | strncpy(buffer2 + len2, p2.items[i2++].path, | |
114 | PATH_MAX - len2); | |
115 | } | |
116 | ||
117 | ret = queue_diff(o, n1, n2); | |
118 | } | |
119 | path_list_clear(&p1, 0); | |
120 | path_list_clear(&p2, 0); | |
121 | ||
122 | return ret; | |
123 | } else { | |
124 | struct diff_filespec *d1, *d2; | |
125 | ||
126 | if (DIFF_OPT_TST(o, REVERSE_DIFF)) { | |
127 | unsigned tmp; | |
128 | const char *tmp_c; | |
129 | tmp = mode1; mode1 = mode2; mode2 = tmp; | |
130 | tmp_c = name1; name1 = name2; name2 = tmp_c; | |
131 | } | |
132 | ||
133 | if (!name1) | |
134 | name1 = "/dev/null"; | |
135 | if (!name2) | |
136 | name2 = "/dev/null"; | |
137 | d1 = alloc_filespec(name1); | |
138 | d2 = alloc_filespec(name2); | |
139 | fill_filespec(d1, null_sha1, mode1); | |
140 | fill_filespec(d2, null_sha1, mode2); | |
141 | ||
142 | diff_queue(&diff_queued_diff, d1, d2); | |
143 | return 0; | |
144 | } | |
145 | } | |
146 | ||
04036607 JH |
147 | static int path_outside_repo(const char *path) |
148 | { | |
149 | /* | |
150 | * We have already done setup_git_directory_gently() so we | |
151 | * know we are inside a git work tree already. | |
152 | */ | |
153 | const char *work_tree; | |
154 | size_t len; | |
155 | ||
156 | if (!is_absolute_path(path)) | |
157 | return 0; | |
158 | work_tree = get_git_work_tree(); | |
159 | len = strlen(work_tree); | |
160 | if (strncmp(path, work_tree, len) || | |
161 | (path[len] != '\0' && path[len] != '/')) | |
162 | return 1; | |
163 | return 0; | |
164 | } | |
165 | ||
0569e9b8 JH |
166 | void diff_no_index(struct rev_info *revs, |
167 | int argc, const char **argv, | |
168 | int nongit, const char *prefix) | |
169 | { | |
170 | int i; | |
171 | int no_index = 0; | |
172 | unsigned options = 0; | |
173 | ||
174 | /* Were we asked to do --no-index explicitly? */ | |
175 | for (i = 1; i < argc; i++) { | |
176 | if (!strcmp(argv[i], "--")) | |
177 | return; | |
178 | if (!strcmp(argv[i], "--no-index")) | |
179 | no_index = 1; | |
180 | if (argv[i][0] != '-') | |
181 | break; | |
182 | } | |
183 | ||
04036607 JH |
184 | if (!no_index && !nongit) { |
185 | /* | |
186 | * Inside a git repository, without --no-index. Only | |
187 | * when a path outside the repository is given, | |
188 | * e.g. "git diff /var/tmp/[12]", or "git diff | |
189 | * Makefile /var/tmp/Makefile", allow it to be used as | |
190 | * a colourful "diff" replacement. | |
191 | */ | |
192 | if ((argc != i + 2) || | |
193 | (!path_outside_repo(argv[i]) && | |
194 | !path_outside_repo(argv[i+1]))) | |
195 | return; | |
196 | } | |
0569e9b8 JH |
197 | if (argc != i + 2) |
198 | die("git diff %s takes two paths", | |
199 | no_index ? "--no-index" : "[--no-index]"); | |
200 | ||
b3fde6cc JH |
201 | /* |
202 | * If the user asked for our exit code then don't start a | |
203 | * pager or we would end up reporting its exit code instead. | |
204 | */ | |
205 | if (!DIFF_OPT_TST(&revs->diffopt, EXIT_WITH_STATUS)) | |
206 | setup_pager(); | |
207 | ||
0569e9b8 JH |
208 | diff_setup(&revs->diffopt); |
209 | if (!revs->diffopt.output_format) | |
210 | revs->diffopt.output_format = DIFF_FORMAT_PATCH; | |
211 | for (i = 1; i < argc - 2; ) { | |
212 | int j; | |
213 | if (!strcmp(argv[i], "--no-index")) | |
214 | i++; | |
215 | else if (!strcmp(argv[1], "-q")) | |
216 | options |= DIFF_SILENT_ON_REMOVED; | |
217 | else { | |
218 | j = diff_opt_parse(&revs->diffopt, argv + i, argc - i); | |
219 | if (!j) | |
220 | die("invalid diff option/value: %s", argv[i]); | |
221 | i += j; | |
222 | } | |
223 | } | |
224 | ||
225 | if (prefix) { | |
226 | int len = strlen(prefix); | |
227 | ||
228 | revs->diffopt.paths = xcalloc(2, sizeof(char*)); | |
229 | for (i = 0; i < 2; i++) { | |
230 | const char *p = argv[argc - 2 + i]; | |
231 | /* | |
232 | * stdin should be spelled as '-'; if you have | |
233 | * path that is '-', spell it as ./-. | |
234 | */ | |
235 | p = (strcmp(p, "-") | |
236 | ? xstrdup(prefix_filename(prefix, len, p)) | |
237 | : p); | |
238 | revs->diffopt.paths[i] = p; | |
239 | } | |
240 | } | |
241 | else | |
242 | revs->diffopt.paths = argv + argc - 2; | |
243 | revs->diffopt.nr_paths = 2; | |
244 | ||
245 | DIFF_OPT_SET(&revs->diffopt, EXIT_WITH_STATUS); | |
246 | DIFF_OPT_SET(&revs->diffopt, NO_INDEX); | |
247 | ||
248 | revs->max_count = -2; | |
249 | if (diff_setup_done(&revs->diffopt) < 0) | |
250 | die("diff_setup_done failed"); | |
251 | ||
252 | if (queue_diff(&revs->diffopt, revs->diffopt.paths[0], | |
253 | revs->diffopt.paths[1])) | |
254 | exit(1); | |
255 | diffcore_std(&revs->diffopt); | |
256 | diff_flush(&revs->diffopt); | |
257 | ||
258 | /* | |
259 | * The return code for --no-index imitates diff(1): | |
260 | * 0 = no changes, 1 = changes, else error | |
261 | */ | |
262 | exit(revs->diffopt.found_changes); | |
263 | } |