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