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