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