]>
Commit | Line | Data |
---|---|---|
be3cfa85 JH |
1 | /* |
2 | * Copyright (C) 2005 Junio C Hamano | |
3 | */ | |
4 | #include <sys/types.h> | |
5 | #include <sys/wait.h> | |
532149d7 | 6 | #include <signal.h> |
86436c28 JH |
7 | #include "cache.h" |
8 | #include "diff.h" | |
427dcb4b | 9 | #include "diffcore.h" |
86436c28 | 10 | |
d19938ab | 11 | static const char *diff_opts = "-pu"; |
5c97558c | 12 | static unsigned char null_sha1[20] = { 0, }; |
427dcb4b | 13 | |
427dcb4b | 14 | static int reverse_diff; |
f0c6b2a2 | 15 | static int use_size_cache; |
86436c28 | 16 | |
be3cfa85 | 17 | static const char *external_diff(void) |
86436c28 | 18 | { |
d19938ab | 19 | static const char *external_diff_cmd = NULL; |
be3cfa85 JH |
20 | static int done_preparing = 0; |
21 | ||
22 | if (done_preparing) | |
23 | return external_diff_cmd; | |
24 | ||
86436c28 JH |
25 | /* |
26 | * Default values above are meant to match the | |
27 | * Linux kernel development style. Examples of | |
28 | * alternative styles you can specify via environment | |
29 | * variables are: | |
30 | * | |
86436c28 JH |
31 | * GIT_DIFF_OPTS="-c"; |
32 | */ | |
d19938ab JH |
33 | if (gitenv("GIT_EXTERNAL_DIFF")) |
34 | external_diff_cmd = gitenv("GIT_EXTERNAL_DIFF"); | |
be3cfa85 JH |
35 | |
36 | /* In case external diff fails... */ | |
d19938ab | 37 | diff_opts = gitenv("GIT_DIFF_OPTS") ? : diff_opts; |
be3cfa85 JH |
38 | |
39 | done_preparing = 1; | |
40 | return external_diff_cmd; | |
86436c28 JH |
41 | } |
42 | ||
43 | /* Help to copy the thing properly quoted for the shell safety. | |
44 | * any single quote is replaced with '\'', and the caller is | |
45 | * expected to enclose the result within a single quote pair. | |
46 | * | |
47 | * E.g. | |
48 | * original sq_expand result | |
49 | * name ==> name ==> 'name' | |
50 | * a b ==> a b ==> 'a b' | |
51 | * a'b ==> a'\''b ==> 'a'\''b' | |
52 | */ | |
53 | static char *sq_expand(const char *src) | |
54 | { | |
55 | static char *buf = NULL; | |
56 | int cnt, c; | |
57 | const char *cp; | |
58 | char *bp; | |
59 | ||
57fe64a4 | 60 | /* count bytes needed to store the quoted string. */ |
86436c28 JH |
61 | for (cnt = 1, cp = src; *cp; cnt++, cp++) |
62 | if (*cp == '\'') | |
63 | cnt += 3; | |
64 | ||
812666c8 | 65 | buf = xmalloc(cnt); |
86436c28 JH |
66 | bp = buf; |
67 | while ((c = *src++)) { | |
68 | if (c != '\'') | |
69 | *bp++ = c; | |
70 | else { | |
71 | bp = strcpy(bp, "'\\''"); | |
72 | bp += 4; | |
73 | } | |
74 | } | |
75 | *bp = 0; | |
76 | return buf; | |
77 | } | |
78 | ||
be3cfa85 | 79 | static struct diff_tempfile { |
427dcb4b | 80 | const char *name; /* filename external diff should read from */ |
be3cfa85 JH |
81 | char hex[41]; |
82 | char mode[10]; | |
83 | char tmp_path[50]; | |
84 | } diff_temp[2]; | |
85 | ||
915838c3 JH |
86 | static void builtin_diff(const char *name_a, |
87 | const char *name_b, | |
57fe64a4 | 88 | struct diff_tempfile *temp, |
427dcb4b | 89 | const char *xfrm_msg) |
86436c28 | 90 | { |
9669e17a | 91 | int i, next_at, cmd_size; |
c983370e | 92 | const char *diff_cmd = "diff -L'%s%s' -L'%s%s'"; |
6fa28064 | 93 | const char *diff_arg = "'%s' '%s'||:"; /* "||:" is to return 0 */ |
2f978138 JH |
94 | const char *input_name_sq[2]; |
95 | const char *path0[2]; | |
96 | const char *path1[2]; | |
915838c3 | 97 | const char *name_sq[2]; |
2f978138 | 98 | char *cmd; |
915838c3 JH |
99 | |
100 | name_sq[0] = sq_expand(name_a); | |
101 | name_sq[1] = sq_expand(name_b); | |
102 | ||
c983370e JH |
103 | /* diff_cmd and diff_arg have 6 %s in total which makes |
104 | * the sum of these strings 12 bytes larger than required. | |
be3cfa85 | 105 | * we use 2 spaces around diff-opts, and we need to count |
c983370e | 106 | * terminating NUL, so we subtract 9 here. |
be3cfa85 | 107 | */ |
9669e17a | 108 | cmd_size = (strlen(diff_cmd) + strlen(diff_opts) + |
c983370e | 109 | strlen(diff_arg) - 9); |
2f978138 JH |
110 | for (i = 0; i < 2; i++) { |
111 | input_name_sq[i] = sq_expand(temp[i].name); | |
112 | if (!strcmp(temp[i].name, "/dev/null")) { | |
113 | path0[i] = "/dev/null"; | |
114 | path1[i] = ""; | |
2f978138 | 115 | } else { |
0980d9b3 | 116 | path0[i] = i ? "b/" : "a/"; |
915838c3 | 117 | path1[i] = name_sq[i]; |
2f978138 JH |
118 | } |
119 | cmd_size += (strlen(path0[i]) + strlen(path1[i]) + | |
c983370e | 120 | strlen(input_name_sq[i])); |
2f978138 | 121 | } |
be3cfa85 | 122 | |
2f978138 JH |
123 | cmd = xmalloc(cmd_size); |
124 | ||
125 | next_at = 0; | |
be3cfa85 | 126 | next_at += snprintf(cmd+next_at, cmd_size-next_at, |
2f978138 | 127 | diff_cmd, |
c983370e | 128 | path0[0], path1[0], path0[1], path1[1]); |
be3cfa85 JH |
129 | next_at += snprintf(cmd+next_at, cmd_size-next_at, |
130 | " %s ", diff_opts); | |
131 | next_at += snprintf(cmd+next_at, cmd_size-next_at, | |
2f978138 JH |
132 | diff_arg, input_name_sq[0], input_name_sq[1]); |
133 | ||
915838c3 | 134 | printf("diff --git a/%s b/%s\n", name_a, name_b); |
70aadac0 | 135 | if (!path1[0][0]) { |
b58f23b3 | 136 | printf("new file mode %s\n", temp[1].mode); |
70aadac0 JH |
137 | if (xfrm_msg && xfrm_msg[0]) |
138 | puts(xfrm_msg); | |
139 | } | |
140 | else if (!path1[1][0]) { | |
b58f23b3 | 141 | printf("deleted file mode %s\n", temp[0].mode); |
70aadac0 JH |
142 | if (xfrm_msg && xfrm_msg[0]) |
143 | puts(xfrm_msg); | |
144 | } | |
273b9834 | 145 | else { |
b58f23b3 JH |
146 | if (strcmp(temp[0].mode, temp[1].mode)) { |
147 | printf("old mode %s\n", temp[0].mode); | |
148 | printf("new mode %s\n", temp[1].mode); | |
149 | } | |
427dcb4b | 150 | if (xfrm_msg && xfrm_msg[0]) |
09d9d1a6 | 151 | puts(xfrm_msg); |
427dcb4b | 152 | |
273b9834 JH |
153 | if (strncmp(temp[0].mode, temp[1].mode, 3)) |
154 | /* we do not run diff between different kind | |
155 | * of objects. | |
156 | */ | |
b28858bf JH |
157 | exit(0); |
158 | } | |
c983370e | 159 | fflush(NULL); |
be3cfa85 | 160 | execlp("/bin/sh","sh", "-c", cmd, NULL); |
86436c28 JH |
161 | } |
162 | ||
427dcb4b JH |
163 | struct diff_filespec *alloc_filespec(const char *path) |
164 | { | |
165 | int namelen = strlen(path); | |
166 | struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1); | |
167 | spec->path = (char *)(spec + 1); | |
168 | strcpy(spec->path, path); | |
81e50eab | 169 | spec->should_free = spec->should_munmap = 0; |
427dcb4b JH |
170 | spec->xfrm_flags = 0; |
171 | spec->size = 0; | |
09d74b3b | 172 | spec->data = NULL; |
81e50eab JH |
173 | spec->mode = 0; |
174 | memset(spec->sha1, 0, 20); | |
427dcb4b JH |
175 | return spec; |
176 | } | |
177 | ||
178 | void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1, | |
179 | unsigned short mode) | |
180 | { | |
4130b995 JH |
181 | if (mode) { |
182 | spec->mode = DIFF_FILE_CANON_MODE(mode); | |
81e50eab JH |
183 | memcpy(spec->sha1, sha1, 20); |
184 | spec->sha1_valid = !!memcmp(sha1, null_sha1, 20); | |
185 | } | |
427dcb4b JH |
186 | } |
187 | ||
b46f0b6d JH |
188 | /* |
189 | * Given a name and sha1 pair, if the dircache tells us the file in | |
190 | * the work tree has that object contents, return true, so that | |
191 | * prepare_temp_file() does not have to inflate and extract. | |
192 | */ | |
193 | static int work_tree_matches(const char *name, const unsigned char *sha1) | |
194 | { | |
195 | struct cache_entry *ce; | |
196 | struct stat st; | |
197 | int pos, len; | |
5c97558c | 198 | |
b46f0b6d JH |
199 | /* We do not read the cache ourselves here, because the |
200 | * benchmark with my previous version that always reads cache | |
201 | * shows that it makes things worse for diff-tree comparing | |
202 | * two linux-2.6 kernel trees in an already checked out work | |
915838c3 | 203 | * tree. This is because most diff-tree comparisons deal with |
b46f0b6d JH |
204 | * only a small number of files, while reading the cache is |
205 | * expensive for a large project, and its cost outweighs the | |
206 | * savings we get by not inflating the object to a temporary | |
207 | * file. Practically, this code only helps when we are used | |
208 | * by diff-cache --cached, which does read the cache before | |
209 | * calling us. | |
57fe64a4 | 210 | */ |
b46f0b6d JH |
211 | if (!active_cache) |
212 | return 0; | |
213 | ||
214 | len = strlen(name); | |
215 | pos = cache_name_pos(name, len); | |
216 | if (pos < 0) | |
217 | return 0; | |
218 | ce = active_cache[pos]; | |
b28858bf | 219 | if ((lstat(name, &st) < 0) || |
427dcb4b | 220 | !S_ISREG(st.st_mode) || /* careful! */ |
5d728c84 | 221 | ce_match_stat(ce, &st) || |
b46f0b6d JH |
222 | memcmp(sha1, ce->sha1, 20)) |
223 | return 0; | |
427dcb4b JH |
224 | /* we return 1 only when we can stat, it is a regular file, |
225 | * stat information matches, and sha1 recorded in the cache | |
226 | * matches. I.e. we know the file in the work tree really is | |
227 | * the same as the <name, sha1> pair. | |
228 | */ | |
b46f0b6d JH |
229 | return 1; |
230 | } | |
231 | ||
f0c6b2a2 JH |
232 | static struct sha1_size_cache { |
233 | unsigned char sha1[20]; | |
234 | unsigned long size; | |
235 | } **sha1_size_cache; | |
236 | static int sha1_size_cache_nr, sha1_size_cache_alloc; | |
237 | ||
238 | static struct sha1_size_cache *locate_size_cache(unsigned char *sha1, | |
239 | unsigned long size) | |
240 | { | |
241 | int first, last; | |
242 | struct sha1_size_cache *e; | |
243 | ||
244 | first = 0; | |
245 | last = sha1_size_cache_nr; | |
246 | while (last > first) { | |
247 | int next = (last + first) >> 1; | |
248 | e = sha1_size_cache[next]; | |
249 | int cmp = memcmp(e->sha1, sha1, 20); | |
250 | if (!cmp) | |
251 | return e; | |
252 | if (cmp < 0) { | |
253 | last = next; | |
254 | continue; | |
255 | } | |
256 | first = next+1; | |
257 | } | |
258 | /* not found */ | |
259 | if (size == UINT_MAX) | |
260 | return NULL; | |
261 | /* insert to make it at "first" */ | |
262 | if (sha1_size_cache_alloc <= sha1_size_cache_nr) { | |
263 | sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc); | |
264 | sha1_size_cache = xrealloc(sha1_size_cache, | |
265 | sha1_size_cache_alloc * | |
266 | sizeof(*sha1_size_cache)); | |
267 | } | |
268 | sha1_size_cache_nr++; | |
269 | if (first < sha1_size_cache_nr) | |
270 | memmove(sha1_size_cache + first + 1, sha1_size_cache + first, | |
271 | (sha1_size_cache_nr - first - 1) * | |
272 | sizeof(*sha1_size_cache)); | |
273 | e = xmalloc(sizeof(struct sha1_size_cache)); | |
274 | sha1_size_cache[first] = e; | |
275 | memcpy(e->sha1, sha1, 20); | |
276 | e->size = size; | |
277 | return e; | |
278 | } | |
279 | ||
427dcb4b JH |
280 | /* |
281 | * While doing rename detection and pickaxe operation, we may need to | |
282 | * grab the data for the blob (or file) for our own in-core comparison. | |
283 | * diff_filespec has data and size fields for this purpose. | |
284 | */ | |
f0c6b2a2 | 285 | int diff_populate_filespec(struct diff_filespec *s, int size_only) |
427dcb4b JH |
286 | { |
287 | int err = 0; | |
81e50eab | 288 | if (!DIFF_FILE_VALID(s)) |
427dcb4b JH |
289 | die("internal error: asking to populate invalid file."); |
290 | if (S_ISDIR(s->mode)) | |
291 | return -1; | |
292 | ||
f0c6b2a2 JH |
293 | if (!use_size_cache) |
294 | size_only = 0; | |
295 | ||
427dcb4b JH |
296 | if (s->data) |
297 | return err; | |
298 | if (!s->sha1_valid || | |
299 | work_tree_matches(s->path, s->sha1)) { | |
300 | struct stat st; | |
301 | int fd; | |
302 | if (lstat(s->path, &st) < 0) { | |
303 | if (errno == ENOENT) { | |
304 | err_empty: | |
305 | err = -1; | |
306 | empty: | |
307 | s->data = ""; | |
308 | s->size = 0; | |
309 | return err; | |
310 | } | |
311 | } | |
312 | s->size = st.st_size; | |
313 | if (!s->size) | |
314 | goto empty; | |
f0c6b2a2 JH |
315 | if (size_only) |
316 | return 0; | |
427dcb4b JH |
317 | if (S_ISLNK(st.st_mode)) { |
318 | int ret; | |
319 | s->data = xmalloc(s->size); | |
320 | s->should_free = 1; | |
321 | ret = readlink(s->path, s->data, s->size); | |
322 | if (ret < 0) { | |
323 | free(s->data); | |
324 | goto err_empty; | |
325 | } | |
326 | return 0; | |
327 | } | |
328 | fd = open(s->path, O_RDONLY); | |
329 | if (fd < 0) | |
330 | goto err_empty; | |
331 | s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); | |
332 | s->should_munmap = 1; | |
333 | close(fd); | |
334 | } | |
335 | else { | |
336 | char type[20]; | |
f0c6b2a2 JH |
337 | struct sha1_size_cache *e; |
338 | ||
339 | if (size_only) { | |
340 | e = locate_size_cache(s->sha1, UINT_MAX); | |
341 | if (e) { | |
342 | s->size = e->size; | |
343 | return 0; | |
344 | } | |
65c2e0c3 JH |
345 | if (!sha1_file_size(s->sha1, &s->size)) |
346 | locate_size_cache(s->sha1, s->size); | |
347 | } | |
348 | else { | |
349 | s->data = read_sha1_file(s->sha1, type, &s->size); | |
350 | s->should_free = 1; | |
f0c6b2a2 | 351 | } |
427dcb4b JH |
352 | } |
353 | return 0; | |
354 | } | |
355 | ||
356 | void diff_free_filespec_data(struct diff_filespec *s) | |
357 | { | |
358 | if (s->should_free) | |
359 | free(s->data); | |
360 | else if (s->should_munmap) | |
361 | munmap(s->data, s->size); | |
362 | s->should_free = s->should_munmap = 0; | |
09d74b3b | 363 | s->data = NULL; |
427dcb4b JH |
364 | } |
365 | ||
b28858bf JH |
366 | static void prep_temp_blob(struct diff_tempfile *temp, |
367 | void *blob, | |
368 | unsigned long size, | |
369 | unsigned char *sha1, | |
370 | int mode) | |
371 | { | |
372 | int fd; | |
373 | ||
374 | strcpy(temp->tmp_path, ".diff_XXXXXX"); | |
375 | fd = mkstemp(temp->tmp_path); | |
376 | if (fd < 0) | |
377 | die("unable to create temp-file"); | |
378 | if (write(fd, blob, size) != size) | |
379 | die("unable to write temp-file"); | |
380 | close(fd); | |
381 | temp->name = temp->tmp_path; | |
382 | strcpy(temp->hex, sha1_to_hex(sha1)); | |
383 | temp->hex[40] = 0; | |
384 | sprintf(temp->mode, "%06o", mode); | |
385 | } | |
386 | ||
be3cfa85 JH |
387 | static void prepare_temp_file(const char *name, |
388 | struct diff_tempfile *temp, | |
427dcb4b | 389 | struct diff_filespec *one) |
86436c28 | 390 | { |
81e50eab | 391 | if (!DIFF_FILE_VALID(one)) { |
be3cfa85 | 392 | not_a_valid_file: |
532149d7 JH |
393 | /* A '-' entry produces this for file-2, and |
394 | * a '+' entry produces this for file-1. | |
395 | */ | |
be3cfa85 JH |
396 | temp->name = "/dev/null"; |
397 | strcpy(temp->hex, "."); | |
398 | strcpy(temp->mode, "."); | |
86436c28 JH |
399 | return; |
400 | } | |
be3cfa85 | 401 | |
5c97558c | 402 | if (!one->sha1_valid || |
427dcb4b | 403 | work_tree_matches(name, one->sha1)) { |
be3cfa85 | 404 | struct stat st; |
427dcb4b | 405 | if (lstat(name, &st) < 0) { |
be3cfa85 JH |
406 | if (errno == ENOENT) |
407 | goto not_a_valid_file; | |
427dcb4b | 408 | die("stat(%s): %s", name, strerror(errno)); |
be3cfa85 | 409 | } |
b28858bf JH |
410 | if (S_ISLNK(st.st_mode)) { |
411 | int ret; | |
412 | char *buf, buf_[1024]; | |
413 | buf = ((sizeof(buf_) < st.st_size) ? | |
414 | xmalloc(st.st_size) : buf_); | |
415 | ret = readlink(name, buf, st.st_size); | |
416 | if (ret < 0) | |
417 | die("readlink(%s)", name); | |
418 | prep_temp_blob(temp, buf, st.st_size, | |
419 | (one->sha1_valid ? | |
427dcb4b | 420 | one->sha1 : null_sha1), |
b28858bf JH |
421 | (one->sha1_valid ? |
422 | one->mode : S_IFLNK)); | |
423 | } | |
424 | else { | |
427dcb4b JH |
425 | /* we can borrow from the file in the work tree */ |
426 | temp->name = name; | |
b28858bf JH |
427 | if (!one->sha1_valid) |
428 | strcpy(temp->hex, sha1_to_hex(null_sha1)); | |
429 | else | |
427dcb4b | 430 | strcpy(temp->hex, sha1_to_hex(one->sha1)); |
9d429ff6 JH |
431 | /* Even though we may sometimes borrow the |
432 | * contents from the work tree, we always want | |
433 | * one->mode. mode is trustworthy even when | |
434 | * !(one->sha1_valid), as long as | |
435 | * DIFF_FILE_VALID(one). | |
436 | */ | |
437 | sprintf(temp->mode, "%06o", one->mode); | |
b28858bf JH |
438 | } |
439 | return; | |
be3cfa85 JH |
440 | } |
441 | else { | |
f0c6b2a2 | 442 | if (diff_populate_filespec(one, 0)) |
427dcb4b JH |
443 | die("cannot read data blob for %s", one->path); |
444 | prep_temp_blob(temp, one->data, one->size, | |
445 | one->sha1, one->mode); | |
be3cfa85 JH |
446 | } |
447 | } | |
448 | ||
449 | static void remove_tempfile(void) | |
450 | { | |
451 | int i; | |
452 | ||
453 | for (i = 0; i < 2; i++) | |
454 | if (diff_temp[i].name == diff_temp[i].tmp_path) { | |
455 | unlink(diff_temp[i].name); | |
456 | diff_temp[i].name = NULL; | |
457 | } | |
458 | } | |
459 | ||
532149d7 JH |
460 | static void remove_tempfile_on_signal(int signo) |
461 | { | |
462 | remove_tempfile(); | |
463 | } | |
464 | ||
be3cfa85 JH |
465 | /* An external diff command takes: |
466 | * | |
467 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ | |
5c97558c | 468 | * infile2 infile2-sha1 infile2-mode [ rename-to ] |
be3cfa85 JH |
469 | * |
470 | */ | |
4130b995 JH |
471 | static void run_external_diff(const char *pgm, |
472 | const char *name, | |
5c97558c | 473 | const char *other, |
427dcb4b JH |
474 | struct diff_filespec *one, |
475 | struct diff_filespec *two, | |
476 | const char *xfrm_msg) | |
be3cfa85 JH |
477 | { |
478 | struct diff_tempfile *temp = diff_temp; | |
532149d7 JH |
479 | pid_t pid; |
480 | int status; | |
be3cfa85 JH |
481 | static int atexit_asked = 0; |
482 | ||
77eb2720 JH |
483 | if (one && two) { |
484 | prepare_temp_file(name, &temp[0], one); | |
915838c3 | 485 | prepare_temp_file(other ? : name, &temp[1], two); |
77eb2720 JH |
486 | if (! atexit_asked && |
487 | (temp[0].name == temp[0].tmp_path || | |
488 | temp[1].name == temp[1].tmp_path)) { | |
489 | atexit_asked = 1; | |
490 | atexit(remove_tempfile); | |
491 | } | |
532149d7 | 492 | signal(SIGINT, remove_tempfile_on_signal); |
be3cfa85 JH |
493 | } |
494 | ||
495 | fflush(NULL); | |
496 | pid = fork(); | |
497 | if (pid < 0) | |
498 | die("unable to fork"); | |
499 | if (!pid) { | |
5c97558c JH |
500 | if (pgm) { |
501 | if (one && two) { | |
427dcb4b | 502 | const char *exec_arg[10]; |
5c97558c JH |
503 | const char **arg = &exec_arg[0]; |
504 | *arg++ = pgm; | |
505 | *arg++ = name; | |
506 | *arg++ = temp[0].name; | |
507 | *arg++ = temp[0].hex; | |
508 | *arg++ = temp[0].mode; | |
509 | *arg++ = temp[1].name; | |
510 | *arg++ = temp[1].hex; | |
511 | *arg++ = temp[1].mode; | |
427dcb4b | 512 | if (other) { |
5c97558c | 513 | *arg++ = other; |
427dcb4b JH |
514 | *arg++ = xfrm_msg; |
515 | } | |
09d74b3b | 516 | *arg = NULL; |
5c97558c JH |
517 | execvp(pgm, (char *const*) exec_arg); |
518 | } | |
77eb2720 JH |
519 | else |
520 | execlp(pgm, pgm, name, NULL); | |
521 | } | |
be3cfa85 JH |
522 | /* |
523 | * otherwise we use the built-in one. | |
524 | */ | |
77eb2720 | 525 | if (one && two) |
427dcb4b | 526 | builtin_diff(name, other ? : name, temp, xfrm_msg); |
77eb2720 JH |
527 | else |
528 | printf("* Unmerged path %s\n", name); | |
be3cfa85 JH |
529 | exit(0); |
530 | } | |
6fa28064 JH |
531 | if (waitpid(pid, &status, 0) < 0 || |
532 | !WIFEXITED(status) || WEXITSTATUS(status)) { | |
533 | /* Earlier we did not check the exit status because | |
532149d7 | 534 | * diff exits non-zero if files are different, and |
6fa28064 JH |
535 | * we are not interested in knowing that. It was a |
536 | * mistake which made it harder to quit a diff-* | |
537 | * session that uses the git-apply-patch-script as | |
538 | * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF | |
539 | * should also exit non-zero only when it wants to | |
540 | * abort the entire diff-* session. | |
532149d7 JH |
541 | */ |
542 | remove_tempfile(); | |
6fa28064 JH |
543 | fprintf(stderr, "external diff died, stopping at %s.\n", name); |
544 | exit(1); | |
532149d7 | 545 | } |
be3cfa85 JH |
546 | remove_tempfile(); |
547 | } | |
548 | ||
4130b995 JH |
549 | static void run_diff(const char *name, |
550 | const char *other, | |
551 | struct diff_filespec *one, | |
552 | struct diff_filespec *two, | |
553 | const char *xfrm_msg) | |
554 | { | |
555 | const char *pgm = external_diff(); | |
556 | if (!pgm && | |
557 | DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) && | |
558 | (S_IFMT & one->mode) != (S_IFMT & two->mode)) { | |
559 | /* a filepair that changes between file and symlink | |
560 | * needs to be split into deletion and creation. | |
561 | */ | |
562 | struct diff_filespec *null = alloc_filespec(two->path); | |
563 | run_external_diff(NULL, name, other, one, null, xfrm_msg); | |
564 | free(null); | |
565 | null = alloc_filespec(one->path); | |
566 | run_external_diff(NULL, name, other, null, two, xfrm_msg); | |
567 | free(null); | |
568 | } | |
569 | else | |
570 | run_external_diff(pgm, name, other, one, two, xfrm_msg); | |
571 | } | |
572 | ||
19feebc8 | 573 | void diff_setup(int flags) |
5c97558c | 574 | { |
19feebc8 JH |
575 | if (flags & DIFF_SETUP_REVERSE) |
576 | reverse_diff = 1; | |
f0c6b2a2 JH |
577 | if (flags & DIFF_SETUP_USE_CACHE) { |
578 | if (!active_cache) | |
579 | /* read-cache does not die even when it fails | |
580 | * so it is safe for us to do this here. Also | |
581 | * it does not smudge active_cache or active_nr | |
582 | * when it fails, so we do not have to worry about | |
583 | * cleaning it up oufselves either. | |
584 | */ | |
585 | read_cache(); | |
586 | } | |
587 | if (flags & DIFF_SETUP_USE_SIZE_CACHE) | |
588 | use_size_cache = 1; | |
589 | ||
5c97558c JH |
590 | } |
591 | ||
6b14d7fa JH |
592 | struct diff_queue_struct diff_queued_diff; |
593 | ||
594 | void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp) | |
5c97558c | 595 | { |
6b14d7fa JH |
596 | if (queue->alloc <= queue->nr) { |
597 | queue->alloc = alloc_nr(queue->alloc); | |
598 | queue->queue = xrealloc(queue->queue, | |
599 | sizeof(dp) * queue->alloc); | |
81e50eab | 600 | } |
6b14d7fa | 601 | queue->queue[queue->nr++] = dp; |
5c97558c JH |
602 | } |
603 | ||
52e95789 | 604 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, |
6b14d7fa JH |
605 | struct diff_filespec *one, |
606 | struct diff_filespec *two) | |
5c97558c | 607 | { |
52e95789 | 608 | struct diff_filepair *dp = xmalloc(sizeof(*dp)); |
427dcb4b JH |
609 | dp->one = one; |
610 | dp->two = two; | |
f7c1512a | 611 | dp->score = 0; |
15d061b4 | 612 | dp->source_stays = 0; |
f345b0a0 | 613 | dp->broken_pair = 0; |
6b14d7fa | 614 | diff_q(queue, dp); |
427dcb4b | 615 | return dp; |
5c97558c JH |
616 | } |
617 | ||
226406f6 JH |
618 | void diff_free_filepair(struct diff_filepair *p) |
619 | { | |
620 | diff_free_filespec_data(p->one); | |
621 | diff_free_filespec_data(p->two); | |
622 | free(p); | |
623 | } | |
624 | ||
bceafe75 JH |
625 | static void diff_flush_raw(struct diff_filepair *p, |
626 | int line_termination, | |
627 | int inter_name_termination) | |
5c97558c | 628 | { |
b6d8f309 JH |
629 | int two_paths; |
630 | char status[10]; | |
25d5ea41 JH |
631 | |
632 | if (line_termination) { | |
633 | const char *err = "path %s cannot be expressed without -z"; | |
634 | if (strchr(p->one->path, line_termination) || | |
635 | strchr(p->one->path, inter_name_termination)) | |
636 | die(err, p->one->path); | |
637 | if (strchr(p->two->path, line_termination) || | |
638 | strchr(p->two->path, inter_name_termination)) | |
639 | die(err, p->two->path); | |
640 | } | |
641 | ||
b6d8f309 JH |
642 | switch (p->status) { |
643 | case 'C': case 'R': | |
644 | two_paths = 1; | |
9fdade06 JH |
645 | sprintf(status, "%c%03d", p->status, |
646 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); | |
b6d8f309 | 647 | break; |
f345b0a0 JH |
648 | case 'N': case 'D': |
649 | two_paths = 0; | |
650 | if (p->score) | |
651 | sprintf(status, "%c%03d", p->status, | |
652 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); | |
653 | else { | |
654 | status[0] = p->status; | |
655 | status[1] = 0; | |
656 | } | |
657 | break; | |
b6d8f309 JH |
658 | default: |
659 | two_paths = 0; | |
660 | status[0] = p->status; | |
661 | status[1] = 0; | |
662 | break; | |
6b14d7fa | 663 | } |
81e50eab JH |
664 | printf(":%06o %06o %s ", |
665 | p->one->mode, p->two->mode, sha1_to_hex(p->one->sha1)); | |
b6d8f309 JH |
666 | printf("%s %s%c%s", |
667 | sha1_to_hex(p->two->sha1), | |
668 | status, | |
669 | inter_name_termination, | |
670 | p->one->path); | |
671 | if (two_paths) | |
672 | printf("%c%s", inter_name_termination, p->two->path); | |
673 | putchar(line_termination); | |
5c97558c JH |
674 | } |
675 | ||
f7c1512a | 676 | int diff_unmodified_pair(struct diff_filepair *p) |
5c97558c | 677 | { |
427dcb4b JH |
678 | /* This function is written stricter than necessary to support |
679 | * the currently implemented transformers, but the idea is to | |
52e95789 | 680 | * let transformers to produce diff_filepairs any way they want, |
427dcb4b | 681 | * and filter and clean them up here before producing the output. |
5c97558c | 682 | */ |
6b14d7fa JH |
683 | struct diff_filespec *one, *two; |
684 | ||
685 | if (DIFF_PAIR_UNMERGED(p)) | |
686 | return 0; /* unmerged is interesting */ | |
5c97558c | 687 | |
6b14d7fa JH |
688 | one = p->one; |
689 | two = p->two; | |
5c97558c | 690 | |
4130b995 JH |
691 | /* deletion, addition, mode or type change |
692 | * and rename are all interesting. | |
693 | */ | |
81e50eab | 694 | if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) || |
4130b995 | 695 | DIFF_PAIR_MODE_CHANGED(p) || |
427dcb4b JH |
696 | strcmp(one->path, two->path)) |
697 | return 0; | |
57fe64a4 | 698 | |
427dcb4b JH |
699 | /* both are valid and point at the same path. that is, we are |
700 | * dealing with a change. | |
57fe64a4 | 701 | */ |
427dcb4b JH |
702 | if (one->sha1_valid && two->sha1_valid && |
703 | !memcmp(one->sha1, two->sha1, sizeof(one->sha1))) | |
704 | return 1; /* no change */ | |
705 | if (!one->sha1_valid && !two->sha1_valid) | |
706 | return 1; /* both look at the same file on the filesystem. */ | |
707 | return 0; | |
57fe64a4 JH |
708 | } |
709 | ||
b6d8f309 | 710 | static void diff_flush_patch(struct diff_filepair *p) |
f7c1512a JH |
711 | { |
712 | const char *name, *other; | |
b6d8f309 | 713 | char msg_[PATH_MAX*2+200], *msg; |
f7c1512a | 714 | |
f7c1512a JH |
715 | if (diff_unmodified_pair(p)) |
716 | return; | |
717 | ||
718 | name = p->one->path; | |
719 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
720 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
721 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
722 | return; /* no tree diffs in patch format */ | |
723 | ||
b6d8f309 JH |
724 | switch (p->status) { |
725 | case 'C': | |
726 | sprintf(msg_, | |
727 | "similarity index %d%%\n" | |
728 | "copy from %s\n" | |
09d9d1a6 | 729 | "copy to %s", |
bceafe75 | 730 | (int)(0.5 + p->score * 100.0/MAX_SCORE), |
b6d8f309 JH |
731 | p->one->path, p->two->path); |
732 | msg = msg_; | |
733 | break; | |
734 | case 'R': | |
735 | sprintf(msg_, | |
736 | "similarity index %d%%\n" | |
737 | "rename old %s\n" | |
09d9d1a6 | 738 | "rename new %s", |
bceafe75 | 739 | (int)(0.5 + p->score * 100.0/MAX_SCORE), |
b6d8f309 JH |
740 | p->one->path, p->two->path); |
741 | msg = msg_; | |
742 | break; | |
70aadac0 JH |
743 | case 'D': case 'N': |
744 | if (DIFF_PAIR_BROKEN(p)) { | |
745 | sprintf(msg_, | |
746 | "dissimilarity index %d%%", | |
747 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); | |
748 | msg = msg_; | |
749 | } | |
750 | else | |
751 | msg = NULL; | |
752 | break; | |
b6d8f309 JH |
753 | default: |
754 | msg = NULL; | |
755 | } | |
756 | ||
f7c1512a | 757 | if (DIFF_PAIR_UNMERGED(p)) |
4130b995 | 758 | run_diff(name, NULL, NULL, NULL, NULL); |
f7c1512a | 759 | else |
4130b995 | 760 | run_diff(name, other, p->one, p->two, msg); |
f7c1512a JH |
761 | } |
762 | ||
bceafe75 | 763 | int diff_queue_is_empty(void) |
f7c1512a | 764 | { |
bceafe75 | 765 | struct diff_queue_struct *q = &diff_queued_diff; |
25d5ea41 JH |
766 | int i; |
767 | for (i = 0; i < q->nr; i++) | |
768 | if (!diff_unmodified_pair(q->queue[i])) | |
769 | return 0; | |
770 | return 1; | |
f7c1512a JH |
771 | } |
772 | ||
25d5ea41 JH |
773 | #if DIFF_DEBUG |
774 | void diff_debug_filespec(struct diff_filespec *s, int x, const char *one) | |
775 | { | |
776 | fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", | |
777 | x, one ? : "", | |
778 | s->path, | |
779 | DIFF_FILE_VALID(s) ? "valid" : "invalid", | |
780 | s->mode, | |
781 | s->sha1_valid ? sha1_to_hex(s->sha1) : ""); | |
782 | fprintf(stderr, "queue[%d] %s size %lu flags %d\n", | |
783 | x, one ? : "", | |
784 | s->size, s->xfrm_flags); | |
785 | } | |
786 | ||
787 | void diff_debug_filepair(const struct diff_filepair *p, int i) | |
788 | { | |
789 | diff_debug_filespec(p->one, i, "one"); | |
790 | diff_debug_filespec(p->two, i, "two"); | |
f345b0a0 JH |
791 | fprintf(stderr, "score %d, status %c stays %d broken %d\n", |
792 | p->score, p->status ? : '?', | |
793 | p->source_stays, p->broken_pair); | |
25d5ea41 JH |
794 | } |
795 | ||
796 | void diff_debug_queue(const char *msg, struct diff_queue_struct *q) | |
6b14d7fa | 797 | { |
6b14d7fa | 798 | int i; |
25d5ea41 JH |
799 | if (msg) |
800 | fprintf(stderr, "%s\n", msg); | |
801 | fprintf(stderr, "q->nr = %d\n", q->nr); | |
6b14d7fa JH |
802 | for (i = 0; i < q->nr; i++) { |
803 | struct diff_filepair *p = q->queue[i]; | |
25d5ea41 JH |
804 | diff_debug_filepair(p, i); |
805 | } | |
806 | } | |
807 | #endif | |
808 | ||
809 | static void diff_resolve_rename_copy(void) | |
810 | { | |
811 | int i, j; | |
812 | struct diff_filepair *p, *pp; | |
813 | struct diff_queue_struct *q = &diff_queued_diff; | |
814 | ||
25d5ea41 JH |
815 | diff_debug_queue("resolve-rename-copy", q); |
816 | ||
817 | for (i = 0; i < q->nr; i++) { | |
818 | p = q->queue[i]; | |
96716a19 | 819 | p->status = 0; /* undecided */ |
bceafe75 JH |
820 | if (DIFF_PAIR_UNMERGED(p)) |
821 | p->status = 'U'; | |
15d061b4 | 822 | else if (!DIFF_FILE_VALID(p->one)) |
bceafe75 | 823 | p->status = 'N'; |
2cd68882 JH |
824 | else if (!DIFF_FILE_VALID(p->two)) |
825 | p->status = 'D'; | |
96716a19 JH |
826 | else if (DIFF_PAIR_TYPE_CHANGED(p)) |
827 | p->status = 'T'; | |
828 | ||
829 | /* from this point on, we are dealing with a pair | |
830 | * whose both sides are valid and of the same type, i.e. | |
831 | * either in-place edit or rename/copy edit. | |
832 | */ | |
01c4e70f | 833 | else if (DIFF_PAIR_RENAME(p)) { |
15d061b4 JH |
834 | if (p->source_stays) { |
835 | p->status = 'C'; | |
836 | continue; | |
837 | } | |
838 | /* See if there is some other filepair that | |
839 | * copies from the same source as us. If so | |
840 | * we are a copy. Otherwise we are a rename. | |
25d5ea41 | 841 | */ |
15d061b4 | 842 | for (j = i + 1; j < q->nr; j++) { |
25d5ea41 JH |
843 | pp = q->queue[j]; |
844 | if (strcmp(pp->one->path, p->one->path)) | |
15d061b4 | 845 | continue; /* not us */ |
01c4e70f | 846 | if (!DIFF_PAIR_RENAME(pp)) |
15d061b4 JH |
847 | continue; /* not a rename/copy */ |
848 | /* pp is a rename/copy from the same source */ | |
849 | p->status = 'C'; | |
850 | break; | |
25d5ea41 JH |
851 | } |
852 | if (!p->status) | |
bceafe75 JH |
853 | p->status = 'R'; |
854 | } | |
9fdade06 JH |
855 | else if (memcmp(p->one->sha1, p->two->sha1, 20) || |
856 | p->one->mode != p->two->mode) | |
bceafe75 | 857 | p->status = 'M'; |
67574c40 JH |
858 | else { |
859 | /* This is a "no-change" entry and should not | |
860 | * happen anymore, but prepare for broken callers. | |
15d061b4 | 861 | */ |
67574c40 JH |
862 | error("feeding unmodified %s to diffcore", |
863 | p->one->path); | |
864 | p->status = 'X'; | |
865 | } | |
6b14d7fa | 866 | } |
25d5ea41 | 867 | diff_debug_queue("resolve-rename-copy done", q); |
38c6f780 JH |
868 | } |
869 | ||
b6d8f309 | 870 | void diff_flush(int diff_output_style, int resolve_rename_copy) |
38c6f780 JH |
871 | { |
872 | struct diff_queue_struct *q = &diff_queued_diff; | |
873 | int i; | |
bceafe75 JH |
874 | int line_termination = '\n'; |
875 | int inter_name_termination = '\t'; | |
38c6f780 | 876 | |
bceafe75 | 877 | if (diff_output_style == DIFF_FORMAT_MACHINE) |
6b14d7fa | 878 | line_termination = inter_name_termination = 0; |
bceafe75 JH |
879 | if (resolve_rename_copy) |
880 | diff_resolve_rename_copy(); | |
881 | ||
f7c1512a | 882 | for (i = 0; i < q->nr; i++) { |
f7c1512a | 883 | struct diff_filepair *p = q->queue[i]; |
4130b995 JH |
884 | if ((diff_output_style == DIFF_FORMAT_NO_OUTPUT) || |
885 | (p->status == 'X')) | |
96716a19 | 886 | continue; |
bceafe75 | 887 | if (p->status == 0) |
96716a19 | 888 | die("internal error in diff-resolve-rename-copy"); |
bceafe75 JH |
889 | switch (diff_output_style) { |
890 | case DIFF_FORMAT_PATCH: | |
b6d8f309 | 891 | diff_flush_patch(p); |
bceafe75 JH |
892 | break; |
893 | case DIFF_FORMAT_HUMAN: | |
894 | case DIFF_FORMAT_MACHINE: | |
895 | diff_flush_raw(p, line_termination, | |
896 | inter_name_termination); | |
897 | break; | |
898 | } | |
f7c1512a | 899 | } |
226406f6 JH |
900 | for (i = 0; i < q->nr; i++) |
901 | diff_free_filepair(q->queue[i]); | |
427dcb4b JH |
902 | free(q->queue); |
903 | q->queue = NULL; | |
904 | q->nr = q->alloc = 0; | |
5c97558c JH |
905 | } |
906 | ||
befe8639 JH |
907 | void diffcore_std(const char **paths, |
908 | int detect_rename, int rename_score, | |
f345b0a0 | 909 | const char *pickaxe, int pickaxe_opts, |
af5323e0 JH |
910 | int break_opt, |
911 | const char *orderfile) | |
befe8639 JH |
912 | { |
913 | if (paths && paths[0]) | |
914 | diffcore_pathspec(paths); | |
f345b0a0 JH |
915 | if (0 <= break_opt) |
916 | diffcore_break(break_opt); | |
befe8639 JH |
917 | if (detect_rename) |
918 | diffcore_rename(detect_rename, rename_score); | |
919 | if (pickaxe) | |
920 | diffcore_pickaxe(pickaxe, pickaxe_opts); | |
af5323e0 JH |
921 | if (orderfile) |
922 | diffcore_order(orderfile); | |
befe8639 JH |
923 | } |
924 | ||
77eb2720 JH |
925 | void diff_addremove(int addremove, unsigned mode, |
926 | const unsigned char *sha1, | |
927 | const char *base, const char *path) | |
be3cfa85 | 928 | { |
77eb2720 | 929 | char concatpath[PATH_MAX]; |
427dcb4b JH |
930 | struct diff_filespec *one, *two; |
931 | ||
932 | /* This may look odd, but it is a preparation for | |
933 | * feeding "there are unchanged files which should | |
934 | * not produce diffs, but when you are doing copy | |
935 | * detection you would need them, so here they are" | |
936 | * entries to the diff-core. They will be prefixed | |
937 | * with something like '=' or '*' (I haven't decided | |
938 | * which but should not make any difference). | |
f7c1512a | 939 | * Feeding the same new and old to diff_change() |
bceafe75 JH |
940 | * also has the same effect. |
941 | * Before the final output happens, they are pruned after | |
942 | * merged into rename/copy pairs as appropriate. | |
427dcb4b | 943 | */ |
7ca45252 | 944 | if (reverse_diff) |
427dcb4b JH |
945 | addremove = (addremove == '+' ? '-' : |
946 | addremove == '-' ? '+' : addremove); | |
7ca45252 | 947 | |
427dcb4b JH |
948 | if (!path) path = ""; |
949 | sprintf(concatpath, "%s%s", base, path); | |
950 | one = alloc_filespec(concatpath); | |
951 | two = alloc_filespec(concatpath); | |
be3cfa85 | 952 | |
427dcb4b JH |
953 | if (addremove != '+') |
954 | fill_filespec(one, sha1, mode); | |
955 | if (addremove != '-') | |
956 | fill_filespec(two, sha1, mode); | |
5c97558c | 957 | |
38c6f780 | 958 | diff_queue(&diff_queued_diff, one, two); |
be3cfa85 JH |
959 | } |
960 | ||
b6d8f309 JH |
961 | void diff_helper_input(unsigned old_mode, |
962 | unsigned new_mode, | |
963 | const unsigned char *old_sha1, | |
964 | const unsigned char *new_sha1, | |
965 | const char *old_path, | |
966 | int status, | |
967 | int score, | |
968 | const char *new_path) | |
81e50eab JH |
969 | { |
970 | struct diff_filespec *one, *two; | |
b6d8f309 | 971 | struct diff_filepair *dp; |
81e50eab | 972 | |
81e50eab JH |
973 | one = alloc_filespec(old_path); |
974 | two = alloc_filespec(new_path); | |
975 | if (old_mode) | |
976 | fill_filespec(one, old_sha1, old_mode); | |
977 | if (new_mode) | |
978 | fill_filespec(two, new_sha1, new_mode); | |
b6d8f309 | 979 | dp = diff_queue(&diff_queued_diff, one, two); |
903d475a | 980 | dp->score = score * MAX_SCORE / 100; |
b6d8f309 | 981 | dp->status = status; |
81e50eab JH |
982 | } |
983 | ||
77eb2720 JH |
984 | void diff_change(unsigned old_mode, unsigned new_mode, |
985 | const unsigned char *old_sha1, | |
986 | const unsigned char *new_sha1, | |
81e50eab JH |
987 | const char *base, const char *path) |
988 | { | |
77eb2720 | 989 | char concatpath[PATH_MAX]; |
427dcb4b | 990 | struct diff_filespec *one, *two; |
77eb2720 | 991 | |
7ca45252 JH |
992 | if (reverse_diff) { |
993 | unsigned tmp; | |
994 | const unsigned char *tmp_c; | |
995 | tmp = old_mode; old_mode = new_mode; new_mode = tmp; | |
996 | tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c; | |
997 | } | |
427dcb4b JH |
998 | if (!path) path = ""; |
999 | sprintf(concatpath, "%s%s", base, path); | |
1000 | one = alloc_filespec(concatpath); | |
1001 | two = alloc_filespec(concatpath); | |
1002 | fill_filespec(one, old_sha1, old_mode); | |
1003 | fill_filespec(two, new_sha1, new_mode); | |
1004 | ||
38c6f780 | 1005 | diff_queue(&diff_queued_diff, one, two); |
77eb2720 | 1006 | } |
be3cfa85 | 1007 | |
77eb2720 JH |
1008 | void diff_unmerge(const char *path) |
1009 | { | |
6b14d7fa JH |
1010 | struct diff_filespec *one, *two; |
1011 | one = alloc_filespec(path); | |
1012 | two = alloc_filespec(path); | |
1013 | diff_queue(&diff_queued_diff, one, two); | |
86436c28 | 1014 | } |