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