]>
Commit | Line | Data |
---|---|---|
6973dcae JH |
1 | /* |
2 | * Copyright (C) 2005 Junio C Hamano | |
3 | */ | |
4 | #include <sys/types.h> | |
5 | #include <sys/wait.h> | |
6 | #include <signal.h> | |
7 | #include "cache.h" | |
8 | #include "quote.h" | |
9 | #include "diff.h" | |
10 | #include "diffcore.h" | |
051308f6 | 11 | #include "delta.h" |
6973dcae JH |
12 | #include "xdiff-interface.h" |
13 | ||
14 | static int use_size_cache; | |
15 | ||
16 | int diff_rename_limit_default = -1; | |
17 | ||
18 | int git_diff_config(const char *var, const char *value) | |
19 | { | |
20 | if (!strcmp(var, "diff.renamelimit")) { | |
21 | diff_rename_limit_default = git_config_int(var, value); | |
22 | return 0; | |
23 | } | |
24 | ||
25 | return git_default_config(var, value); | |
26 | } | |
27 | ||
cd112cef | 28 | enum color_diff { |
50f575fc LT |
29 | DIFF_RESET = 0, |
30 | DIFF_PLAIN = 1, | |
31 | DIFF_METAINFO = 2, | |
32 | DIFF_FRAGINFO = 3, | |
33 | DIFF_FILE_OLD = 4, | |
34 | DIFF_FILE_NEW = 5, | |
cd112cef JS |
35 | }; |
36 | ||
50f575fc LT |
37 | #define COLOR_NORMAL "" |
38 | #define COLOR_BOLD "\033[1m" | |
39 | #define COLOR_DIM "\033[2m" | |
40 | #define COLOR_UL "\033[4m" | |
41 | #define COLOR_BLINK "\033[5m" | |
42 | #define COLOR_REVERSE "\033[7m" | |
43 | #define COLOR_RESET "\033[m" | |
44 | ||
45 | #define COLOR_BLACK "\033[30m" | |
46 | #define COLOR_RED "\033[31m" | |
47 | #define COLOR_GREEN "\033[32m" | |
48 | #define COLOR_YELLOW "\033[33m" | |
49 | #define COLOR_BLUE "\033[34m" | |
50 | #define COLOR_MAGENTA "\033[35m" | |
51 | #define COLOR_CYAN "\033[36m" | |
52 | #define COLOR_WHITE "\033[37m" | |
53 | ||
54 | #define COLOR_CYANBG "\033[46m" | |
55 | #define COLOR_GRAYBG "\033[47m" // Good for xterm | |
56 | ||
cd112cef | 57 | static const char *diff_colors[] = { |
50f575fc LT |
58 | [DIFF_RESET] = COLOR_RESET, |
59 | [DIFF_PLAIN] = COLOR_NORMAL, | |
60 | [DIFF_METAINFO] = COLOR_BOLD, | |
61 | [DIFF_FRAGINFO] = COLOR_CYAN, | |
62 | [DIFF_FILE_OLD] = COLOR_RED, | |
63 | [DIFF_FILE_NEW] = COLOR_GREEN, | |
cd112cef JS |
64 | }; |
65 | ||
6973dcae JH |
66 | static char *quote_one(const char *str) |
67 | { | |
68 | int needlen; | |
69 | char *xp; | |
70 | ||
71 | if (!str) | |
72 | return NULL; | |
73 | needlen = quote_c_style(str, NULL, NULL, 0); | |
74 | if (!needlen) | |
75 | return strdup(str); | |
76 | xp = xmalloc(needlen + 1); | |
77 | quote_c_style(str, xp, NULL, 0); | |
78 | return xp; | |
79 | } | |
80 | ||
81 | static char *quote_two(const char *one, const char *two) | |
82 | { | |
83 | int need_one = quote_c_style(one, NULL, NULL, 1); | |
84 | int need_two = quote_c_style(two, NULL, NULL, 1); | |
85 | char *xp; | |
86 | ||
87 | if (need_one + need_two) { | |
88 | if (!need_one) need_one = strlen(one); | |
89 | if (!need_two) need_one = strlen(two); | |
90 | ||
91 | xp = xmalloc(need_one + need_two + 3); | |
92 | xp[0] = '"'; | |
93 | quote_c_style(one, xp + 1, NULL, 1); | |
94 | quote_c_style(two, xp + need_one + 1, NULL, 1); | |
95 | strcpy(xp + need_one + need_two + 1, "\""); | |
96 | return xp; | |
97 | } | |
98 | need_one = strlen(one); | |
99 | need_two = strlen(two); | |
100 | xp = xmalloc(need_one + need_two + 1); | |
101 | strcpy(xp, one); | |
102 | strcpy(xp + need_one, two); | |
103 | return xp; | |
104 | } | |
105 | ||
106 | static const char *external_diff(void) | |
107 | { | |
108 | static const char *external_diff_cmd = NULL; | |
109 | static int done_preparing = 0; | |
110 | ||
111 | if (done_preparing) | |
112 | return external_diff_cmd; | |
113 | external_diff_cmd = getenv("GIT_EXTERNAL_DIFF"); | |
114 | done_preparing = 1; | |
115 | return external_diff_cmd; | |
116 | } | |
117 | ||
118 | #define TEMPFILE_PATH_LEN 50 | |
119 | ||
120 | static struct diff_tempfile { | |
121 | const char *name; /* filename external diff should read from */ | |
122 | char hex[41]; | |
123 | char mode[10]; | |
124 | char tmp_path[TEMPFILE_PATH_LEN]; | |
125 | } diff_temp[2]; | |
126 | ||
127 | static int count_lines(const char *data, int size) | |
128 | { | |
129 | int count, ch, completely_empty = 1, nl_just_seen = 0; | |
130 | count = 0; | |
131 | while (0 < size--) { | |
132 | ch = *data++; | |
133 | if (ch == '\n') { | |
134 | count++; | |
135 | nl_just_seen = 1; | |
136 | completely_empty = 0; | |
137 | } | |
138 | else { | |
139 | nl_just_seen = 0; | |
140 | completely_empty = 0; | |
141 | } | |
142 | } | |
143 | if (completely_empty) | |
144 | return 0; | |
145 | if (!nl_just_seen) | |
146 | count++; /* no trailing newline */ | |
147 | return count; | |
148 | } | |
149 | ||
150 | static void print_line_count(int count) | |
151 | { | |
152 | switch (count) { | |
153 | case 0: | |
154 | printf("0,0"); | |
155 | break; | |
156 | case 1: | |
157 | printf("1"); | |
158 | break; | |
159 | default: | |
160 | printf("1,%d", count); | |
161 | break; | |
162 | } | |
163 | } | |
164 | ||
165 | static void copy_file(int prefix, const char *data, int size) | |
166 | { | |
167 | int ch, nl_just_seen = 1; | |
168 | while (0 < size--) { | |
169 | ch = *data++; | |
170 | if (nl_just_seen) | |
171 | putchar(prefix); | |
172 | putchar(ch); | |
173 | if (ch == '\n') | |
174 | nl_just_seen = 1; | |
175 | else | |
176 | nl_just_seen = 0; | |
177 | } | |
178 | if (!nl_just_seen) | |
179 | printf("\n\\ No newline at end of file\n"); | |
180 | } | |
181 | ||
182 | static void emit_rewrite_diff(const char *name_a, | |
183 | const char *name_b, | |
184 | struct diff_filespec *one, | |
185 | struct diff_filespec *two) | |
186 | { | |
187 | int lc_a, lc_b; | |
188 | diff_populate_filespec(one, 0); | |
189 | diff_populate_filespec(two, 0); | |
190 | lc_a = count_lines(one->data, one->size); | |
191 | lc_b = count_lines(two->data, two->size); | |
192 | printf("--- %s\n+++ %s\n@@ -", name_a, name_b); | |
193 | print_line_count(lc_a); | |
194 | printf(" +"); | |
195 | print_line_count(lc_b); | |
196 | printf(" @@\n"); | |
197 | if (lc_a) | |
198 | copy_file('-', one->data, one->size); | |
199 | if (lc_b) | |
200 | copy_file('+', two->data, two->size); | |
201 | } | |
202 | ||
203 | static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one) | |
204 | { | |
205 | if (!DIFF_FILE_VALID(one)) { | |
d2543b8e | 206 | mf->ptr = (char *)""; /* does not matter */ |
6973dcae JH |
207 | mf->size = 0; |
208 | return 0; | |
209 | } | |
210 | else if (diff_populate_filespec(one, 0)) | |
211 | return -1; | |
212 | mf->ptr = one->data; | |
213 | mf->size = one->size; | |
214 | return 0; | |
215 | } | |
216 | ||
217 | struct emit_callback { | |
cd112cef JS |
218 | struct xdiff_emit_state xm; |
219 | int nparents, color_diff; | |
6973dcae JH |
220 | const char **label_path; |
221 | }; | |
222 | ||
50f575fc | 223 | static inline const char *get_color(int diff_use_color, enum color_diff ix) |
cd112cef JS |
224 | { |
225 | if (diff_use_color) | |
50f575fc LT |
226 | return diff_colors[ix]; |
227 | return ""; | |
cd112cef JS |
228 | } |
229 | ||
230 | static void fn_out_consume(void *priv, char *line, unsigned long len) | |
6973dcae JH |
231 | { |
232 | int i; | |
233 | struct emit_callback *ecbdata = priv; | |
50f575fc LT |
234 | const char *set = get_color(ecbdata->color_diff, DIFF_METAINFO); |
235 | const char *reset = get_color(ecbdata->color_diff, DIFF_RESET); | |
6973dcae JH |
236 | |
237 | if (ecbdata->label_path[0]) { | |
50f575fc LT |
238 | printf("%s--- %s%s\n", set, ecbdata->label_path[0], reset); |
239 | printf("%s+++ %s%s\n", set, ecbdata->label_path[1], reset); | |
6973dcae JH |
240 | ecbdata->label_path[0] = ecbdata->label_path[1] = NULL; |
241 | } | |
cd112cef JS |
242 | |
243 | /* This is not really necessary for now because | |
244 | * this codepath only deals with two-way diffs. | |
245 | */ | |
246 | for (i = 0; i < len && line[i] == '@'; i++) | |
247 | ; | |
248 | if (2 <= i && i < len && line[i] == ' ') { | |
249 | ecbdata->nparents = i - 1; | |
50f575fc | 250 | set = get_color(ecbdata->color_diff, DIFF_FRAGINFO); |
cd112cef JS |
251 | } |
252 | else if (len < ecbdata->nparents) | |
50f575fc | 253 | set = reset; |
cd112cef JS |
254 | else { |
255 | int nparents = ecbdata->nparents; | |
256 | int color = DIFF_PLAIN; | |
257 | for (i = 0; i < nparents && len; i++) { | |
258 | if (line[i] == '-') | |
259 | color = DIFF_FILE_OLD; | |
260 | else if (line[i] == '+') | |
261 | color = DIFF_FILE_NEW; | |
262 | } | |
50f575fc | 263 | set = get_color(ecbdata->color_diff, color); |
cd112cef | 264 | } |
50f575fc LT |
265 | if (len > 0 && line[len-1] == '\n') |
266 | len--; | |
267 | printf("%s%.*s%s\n", set, (int) len, line, reset); | |
6973dcae JH |
268 | } |
269 | ||
270 | static char *pprint_rename(const char *a, const char *b) | |
271 | { | |
272 | const char *old = a; | |
273 | const char *new = b; | |
274 | char *name = NULL; | |
275 | int pfx_length, sfx_length; | |
276 | int len_a = strlen(a); | |
277 | int len_b = strlen(b); | |
278 | ||
279 | /* Find common prefix */ | |
280 | pfx_length = 0; | |
281 | while (*old && *new && *old == *new) { | |
282 | if (*old == '/') | |
283 | pfx_length = old - a + 1; | |
284 | old++; | |
285 | new++; | |
286 | } | |
287 | ||
288 | /* Find common suffix */ | |
289 | old = a + len_a; | |
290 | new = b + len_b; | |
291 | sfx_length = 0; | |
292 | while (a <= old && b <= new && *old == *new) { | |
293 | if (*old == '/') | |
294 | sfx_length = len_a - (old - a); | |
295 | old--; | |
296 | new--; | |
297 | } | |
298 | ||
299 | /* | |
300 | * pfx{mid-a => mid-b}sfx | |
301 | * {pfx-a => pfx-b}sfx | |
302 | * pfx{sfx-a => sfx-b} | |
303 | * name-a => name-b | |
304 | */ | |
305 | if (pfx_length + sfx_length) { | |
cc908b82 JH |
306 | int a_midlen = len_a - pfx_length - sfx_length; |
307 | int b_midlen = len_b - pfx_length - sfx_length; | |
308 | if (a_midlen < 0) a_midlen = 0; | |
309 | if (b_midlen < 0) b_midlen = 0; | |
310 | ||
e3008464 | 311 | name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7); |
6973dcae JH |
312 | sprintf(name, "%.*s{%.*s => %.*s}%s", |
313 | pfx_length, a, | |
cc908b82 JH |
314 | a_midlen, a + pfx_length, |
315 | b_midlen, b + pfx_length, | |
6973dcae JH |
316 | a + len_a - sfx_length); |
317 | } | |
318 | else { | |
319 | name = xmalloc(len_a + len_b + 5); | |
320 | sprintf(name, "%s => %s", a, b); | |
321 | } | |
322 | return name; | |
323 | } | |
324 | ||
325 | struct diffstat_t { | |
326 | struct xdiff_emit_state xm; | |
327 | ||
328 | int nr; | |
329 | int alloc; | |
330 | struct diffstat_file { | |
331 | char *name; | |
332 | unsigned is_unmerged:1; | |
333 | unsigned is_binary:1; | |
334 | unsigned is_renamed:1; | |
335 | unsigned int added, deleted; | |
336 | } **files; | |
337 | }; | |
338 | ||
339 | static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat, | |
340 | const char *name_a, | |
341 | const char *name_b) | |
342 | { | |
343 | struct diffstat_file *x; | |
344 | x = xcalloc(sizeof (*x), 1); | |
345 | if (diffstat->nr == diffstat->alloc) { | |
346 | diffstat->alloc = alloc_nr(diffstat->alloc); | |
347 | diffstat->files = xrealloc(diffstat->files, | |
348 | diffstat->alloc * sizeof(x)); | |
349 | } | |
350 | diffstat->files[diffstat->nr++] = x; | |
351 | if (name_b) { | |
352 | x->name = pprint_rename(name_a, name_b); | |
353 | x->is_renamed = 1; | |
354 | } | |
355 | else | |
356 | x->name = strdup(name_a); | |
357 | return x; | |
358 | } | |
359 | ||
360 | static void diffstat_consume(void *priv, char *line, unsigned long len) | |
361 | { | |
362 | struct diffstat_t *diffstat = priv; | |
363 | struct diffstat_file *x = diffstat->files[diffstat->nr - 1]; | |
364 | ||
365 | if (line[0] == '+') | |
366 | x->added++; | |
367 | else if (line[0] == '-') | |
368 | x->deleted++; | |
369 | } | |
370 | ||
371 | static const char pluses[] = "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"; | |
372 | static const char minuses[]= "----------------------------------------------------------------------"; | |
698ce6f8 | 373 | const char mime_boundary_leader[] = "------------"; |
6973dcae JH |
374 | |
375 | static void show_stats(struct diffstat_t* data) | |
376 | { | |
6973dcae JH |
377 | int i, len, add, del, total, adds = 0, dels = 0; |
378 | int max, max_change = 0, max_len = 0; | |
379 | int total_files = data->nr; | |
380 | ||
381 | if (data->nr == 0) | |
382 | return; | |
383 | ||
384 | for (i = 0; i < data->nr; i++) { | |
385 | struct diffstat_file *file = data->files[i]; | |
386 | ||
387 | len = strlen(file->name); | |
388 | if (max_len < len) | |
389 | max_len = len; | |
390 | ||
391 | if (file->is_binary || file->is_unmerged) | |
392 | continue; | |
393 | if (max_change < file->added + file->deleted) | |
394 | max_change = file->added + file->deleted; | |
395 | } | |
396 | ||
397 | for (i = 0; i < data->nr; i++) { | |
d2543b8e | 398 | const char *prefix = ""; |
6973dcae JH |
399 | char *name = data->files[i]->name; |
400 | int added = data->files[i]->added; | |
401 | int deleted = data->files[i]->deleted; | |
402 | ||
403 | if (0 < (len = quote_c_style(name, NULL, NULL, 0))) { | |
404 | char *qname = xmalloc(len + 1); | |
405 | quote_c_style(name, qname, NULL, 0); | |
406 | free(name); | |
407 | data->files[i]->name = name = qname; | |
408 | } | |
409 | ||
410 | /* | |
411 | * "scale" the filename | |
412 | */ | |
413 | len = strlen(name); | |
414 | max = max_len; | |
415 | if (max > 50) | |
416 | max = 50; | |
417 | if (len > max) { | |
418 | char *slash; | |
419 | prefix = "..."; | |
420 | max -= 3; | |
421 | name += len - max; | |
422 | slash = strchr(name, '/'); | |
423 | if (slash) | |
424 | name = slash; | |
425 | } | |
426 | len = max; | |
427 | ||
428 | /* | |
429 | * scale the add/delete | |
430 | */ | |
431 | max = max_change; | |
432 | if (max + len > 70) | |
433 | max = 70 - len; | |
434 | ||
435 | if (data->files[i]->is_binary) { | |
436 | printf(" %s%-*s | Bin\n", prefix, len, name); | |
437 | goto free_diffstat_file; | |
438 | } | |
439 | else if (data->files[i]->is_unmerged) { | |
440 | printf(" %s%-*s | Unmerged\n", prefix, len, name); | |
441 | goto free_diffstat_file; | |
442 | } | |
443 | else if (!data->files[i]->is_renamed && | |
444 | (added + deleted == 0)) { | |
445 | total_files--; | |
446 | goto free_diffstat_file; | |
447 | } | |
448 | ||
449 | add = added; | |
450 | del = deleted; | |
451 | total = add + del; | |
452 | adds += add; | |
453 | dels += del; | |
454 | ||
455 | if (max_change > 0) { | |
456 | total = (total * max + max_change / 2) / max_change; | |
457 | add = (add * max + max_change / 2) / max_change; | |
458 | del = total - add; | |
459 | } | |
460 | printf(" %s%-*s |%5d %.*s%.*s\n", prefix, | |
461 | len, name, added + deleted, | |
462 | add, pluses, del, minuses); | |
463 | free_diffstat_file: | |
464 | free(data->files[i]->name); | |
465 | free(data->files[i]); | |
466 | } | |
467 | free(data->files); | |
468 | printf(" %d files changed, %d insertions(+), %d deletions(-)\n", | |
469 | total_files, adds, dels); | |
470 | } | |
471 | ||
88246898 JS |
472 | struct checkdiff_t { |
473 | struct xdiff_emit_state xm; | |
474 | const char *filename; | |
475 | int lineno; | |
476 | }; | |
477 | ||
478 | static void checkdiff_consume(void *priv, char *line, unsigned long len) | |
479 | { | |
480 | struct checkdiff_t *data = priv; | |
481 | ||
482 | if (line[0] == '+') { | |
483 | int i, spaces = 0; | |
484 | ||
485 | data->lineno++; | |
486 | ||
487 | /* check space before tab */ | |
488 | for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++) | |
489 | if (line[i] == ' ') | |
490 | spaces++; | |
491 | if (line[i - 1] == '\t' && spaces) | |
492 | printf("%s:%d: space before tab:%.*s\n", | |
493 | data->filename, data->lineno, (int)len, line); | |
494 | ||
495 | /* check white space at line end */ | |
496 | if (line[len - 1] == '\n') | |
497 | len--; | |
498 | if (isspace(line[len - 1])) | |
499 | printf("%s:%d: white space at end: %.*s\n", | |
500 | data->filename, data->lineno, (int)len, line); | |
501 | } else if (line[0] == ' ') | |
502 | data->lineno++; | |
503 | else if (line[0] == '@') { | |
504 | char *plus = strchr(line, '+'); | |
505 | if (plus) | |
9e848163 | 506 | data->lineno = strtol(plus, NULL, 10); |
88246898 JS |
507 | else |
508 | die("invalid diff"); | |
509 | } | |
510 | } | |
511 | ||
0660626c JH |
512 | static unsigned char *deflate_it(char *data, |
513 | unsigned long size, | |
514 | unsigned long *result_size) | |
051308f6 | 515 | { |
0660626c JH |
516 | int bound; |
517 | unsigned char *deflated; | |
518 | z_stream stream; | |
519 | ||
520 | memset(&stream, 0, sizeof(stream)); | |
521 | deflateInit(&stream, Z_BEST_COMPRESSION); | |
522 | bound = deflateBound(&stream, size); | |
523 | deflated = xmalloc(bound); | |
524 | stream.next_out = deflated; | |
525 | stream.avail_out = bound; | |
526 | ||
527 | stream.next_in = (unsigned char *)data; | |
528 | stream.avail_in = size; | |
529 | while (deflate(&stream, Z_FINISH) == Z_OK) | |
530 | ; /* nothing */ | |
531 | deflateEnd(&stream); | |
532 | *result_size = stream.total_out; | |
533 | return deflated; | |
051308f6 JH |
534 | } |
535 | ||
0660626c | 536 | static void emit_binary_diff(mmfile_t *one, mmfile_t *two) |
051308f6 | 537 | { |
0660626c JH |
538 | void *cp; |
539 | void *delta; | |
540 | void *deflated; | |
541 | void *data; | |
542 | unsigned long orig_size; | |
543 | unsigned long delta_size; | |
544 | unsigned long deflate_size; | |
545 | unsigned long data_size; | |
051308f6 | 546 | |
0660626c JH |
547 | printf("GIT binary patch\n"); |
548 | /* We could do deflated delta, or we could do just deflated two, | |
549 | * whichever is smaller. | |
051308f6 | 550 | */ |
0660626c JH |
551 | delta = NULL; |
552 | deflated = deflate_it(two->ptr, two->size, &deflate_size); | |
553 | if (one->size && two->size) { | |
554 | delta = diff_delta(one->ptr, one->size, | |
555 | two->ptr, two->size, | |
556 | &delta_size, deflate_size); | |
557 | if (delta) { | |
558 | void *to_free = delta; | |
559 | orig_size = delta_size; | |
560 | delta = deflate_it(delta, delta_size, &delta_size); | |
561 | free(to_free); | |
051308f6 JH |
562 | } |
563 | } | |
051308f6 | 564 | |
0660626c JH |
565 | if (delta && delta_size < deflate_size) { |
566 | printf("delta %lu\n", orig_size); | |
567 | free(deflated); | |
568 | data = delta; | |
569 | data_size = delta_size; | |
570 | } | |
571 | else { | |
572 | printf("literal %lu\n", two->size); | |
573 | free(delta); | |
574 | data = deflated; | |
575 | data_size = deflate_size; | |
576 | } | |
051308f6 | 577 | |
0660626c JH |
578 | /* emit data encoded in base85 */ |
579 | cp = data; | |
580 | while (data_size) { | |
581 | int bytes = (52 < data_size) ? 52 : data_size; | |
051308f6 | 582 | char line[70]; |
0660626c | 583 | data_size -= bytes; |
051308f6 JH |
584 | if (bytes <= 26) |
585 | line[0] = bytes + 'A' - 1; | |
586 | else | |
587 | line[0] = bytes - 26 + 'a' - 1; | |
588 | encode_85(line + 1, cp, bytes); | |
1d7f171c | 589 | cp = (char *) cp + bytes; |
051308f6 JH |
590 | puts(line); |
591 | } | |
592 | printf("\n"); | |
0660626c | 593 | free(data); |
051308f6 JH |
594 | } |
595 | ||
6973dcae JH |
596 | #define FIRST_FEW_BYTES 8000 |
597 | static int mmfile_is_binary(mmfile_t *mf) | |
598 | { | |
599 | long sz = mf->size; | |
600 | if (FIRST_FEW_BYTES < sz) | |
601 | sz = FIRST_FEW_BYTES; | |
602 | if (memchr(mf->ptr, 0, sz)) | |
603 | return 1; | |
604 | return 0; | |
605 | } | |
606 | ||
607 | static void builtin_diff(const char *name_a, | |
608 | const char *name_b, | |
609 | struct diff_filespec *one, | |
610 | struct diff_filespec *two, | |
611 | const char *xfrm_msg, | |
051308f6 | 612 | struct diff_options *o, |
6973dcae JH |
613 | int complete_rewrite) |
614 | { | |
615 | mmfile_t mf1, mf2; | |
616 | const char *lbl[2]; | |
617 | char *a_one, *b_two; | |
50f575fc | 618 | const char *set = get_color(o->color_diff, DIFF_METAINFO); |
0ec2f6b7 | 619 | const char *reset = get_color(o->color_diff, DIFF_RESET); |
6973dcae JH |
620 | |
621 | a_one = quote_two("a/", name_a); | |
622 | b_two = quote_two("b/", name_b); | |
623 | lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null"; | |
624 | lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null"; | |
50f575fc | 625 | printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset); |
6973dcae JH |
626 | if (lbl[0][0] == '/') { |
627 | /* /dev/null */ | |
50f575fc LT |
628 | printf("%snew file mode %06o%s\n", set, two->mode, reset); |
629 | if (xfrm_msg && xfrm_msg[0]) | |
630 | printf("%s%s%s\n", set, xfrm_msg, reset); | |
6973dcae JH |
631 | } |
632 | else if (lbl[1][0] == '/') { | |
50f575fc LT |
633 | printf("%sdeleted file mode %06o%s\n", set, one->mode, reset); |
634 | if (xfrm_msg && xfrm_msg[0]) | |
635 | printf("%s%s%s\n", set, xfrm_msg, reset); | |
6973dcae JH |
636 | } |
637 | else { | |
638 | if (one->mode != two->mode) { | |
50f575fc LT |
639 | printf("%sold mode %06o%s\n", set, one->mode, reset); |
640 | printf("%snew mode %06o%s\n", set, two->mode, reset); | |
cd112cef | 641 | } |
50f575fc LT |
642 | if (xfrm_msg && xfrm_msg[0]) |
643 | printf("%s%s%s\n", set, xfrm_msg, reset); | |
6973dcae JH |
644 | /* |
645 | * we do not run diff between different kind | |
646 | * of objects. | |
647 | */ | |
648 | if ((one->mode ^ two->mode) & S_IFMT) | |
649 | goto free_ab_and_return; | |
650 | if (complete_rewrite) { | |
651 | emit_rewrite_diff(name_a, name_b, one, two); | |
652 | goto free_ab_and_return; | |
653 | } | |
654 | } | |
655 | ||
656 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) | |
657 | die("unable to read files to diff"); | |
658 | ||
051308f6 | 659 | if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) { |
0660626c JH |
660 | /* Quite common confusing case */ |
661 | if (mf1.size == mf2.size && | |
662 | !memcmp(mf1.ptr, mf2.ptr, mf1.size)) | |
663 | goto free_ab_and_return; | |
664 | if (o->binary) | |
051308f6 JH |
665 | emit_binary_diff(&mf1, &mf2); |
666 | else | |
667 | printf("Binary files %s and %s differ\n", | |
668 | lbl[0], lbl[1]); | |
669 | } | |
6973dcae JH |
670 | else { |
671 | /* Crazy xdl interfaces.. */ | |
672 | const char *diffopts = getenv("GIT_DIFF_OPTS"); | |
673 | xpparam_t xpp; | |
674 | xdemitconf_t xecfg; | |
675 | xdemitcb_t ecb; | |
676 | struct emit_callback ecbdata; | |
677 | ||
cd112cef | 678 | memset(&ecbdata, 0, sizeof(ecbdata)); |
6973dcae | 679 | ecbdata.label_path = lbl; |
cd112cef | 680 | ecbdata.color_diff = o->color_diff; |
6973dcae | 681 | xpp.flags = XDF_NEED_MINIMAL; |
ee1e5412 | 682 | xecfg.ctxlen = o->context; |
6973dcae JH |
683 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
684 | if (!diffopts) | |
685 | ; | |
686 | else if (!strncmp(diffopts, "--unified=", 10)) | |
687 | xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10); | |
688 | else if (!strncmp(diffopts, "-u", 2)) | |
689 | xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10); | |
cd112cef | 690 | ecb.outf = xdiff_outf; |
6973dcae | 691 | ecb.priv = &ecbdata; |
cd112cef | 692 | ecbdata.xm.consume = fn_out_consume; |
6973dcae JH |
693 | xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb); |
694 | } | |
695 | ||
696 | free_ab_and_return: | |
697 | free(a_one); | |
698 | free(b_two); | |
699 | return; | |
700 | } | |
701 | ||
702 | static void builtin_diffstat(const char *name_a, const char *name_b, | |
703 | struct diff_filespec *one, | |
704 | struct diff_filespec *two, | |
710158e3 JH |
705 | struct diffstat_t *diffstat, |
706 | int complete_rewrite) | |
6973dcae JH |
707 | { |
708 | mmfile_t mf1, mf2; | |
709 | struct diffstat_file *data; | |
710 | ||
711 | data = diffstat_add(diffstat, name_a, name_b); | |
712 | ||
713 | if (!one || !two) { | |
714 | data->is_unmerged = 1; | |
715 | return; | |
716 | } | |
710158e3 JH |
717 | if (complete_rewrite) { |
718 | diff_populate_filespec(one, 0); | |
719 | diff_populate_filespec(two, 0); | |
720 | data->deleted = count_lines(one->data, one->size); | |
721 | data->added = count_lines(two->data, two->size); | |
722 | return; | |
723 | } | |
6973dcae JH |
724 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) |
725 | die("unable to read files to diff"); | |
726 | ||
727 | if (mmfile_is_binary(&mf1) || mmfile_is_binary(&mf2)) | |
728 | data->is_binary = 1; | |
729 | else { | |
730 | /* Crazy xdl interfaces.. */ | |
731 | xpparam_t xpp; | |
732 | xdemitconf_t xecfg; | |
733 | xdemitcb_t ecb; | |
734 | ||
735 | xpp.flags = XDF_NEED_MINIMAL; | |
736 | xecfg.ctxlen = 0; | |
737 | xecfg.flags = 0; | |
738 | ecb.outf = xdiff_outf; | |
739 | ecb.priv = diffstat; | |
740 | xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb); | |
741 | } | |
742 | } | |
743 | ||
88246898 JS |
744 | static void builtin_checkdiff(const char *name_a, const char *name_b, |
745 | struct diff_filespec *one, | |
746 | struct diff_filespec *two) | |
747 | { | |
748 | mmfile_t mf1, mf2; | |
749 | struct checkdiff_t data; | |
750 | ||
751 | if (!two) | |
752 | return; | |
753 | ||
754 | memset(&data, 0, sizeof(data)); | |
755 | data.xm.consume = checkdiff_consume; | |
756 | data.filename = name_b ? name_b : name_a; | |
757 | data.lineno = 0; | |
758 | ||
759 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) | |
760 | die("unable to read files to diff"); | |
761 | ||
762 | if (mmfile_is_binary(&mf2)) | |
763 | return; | |
764 | else { | |
765 | /* Crazy xdl interfaces.. */ | |
766 | xpparam_t xpp; | |
767 | xdemitconf_t xecfg; | |
768 | xdemitcb_t ecb; | |
769 | ||
770 | xpp.flags = XDF_NEED_MINIMAL; | |
771 | xecfg.ctxlen = 0; | |
772 | xecfg.flags = 0; | |
773 | ecb.outf = xdiff_outf; | |
774 | ecb.priv = &data; | |
775 | xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb); | |
776 | } | |
777 | } | |
778 | ||
6973dcae JH |
779 | struct diff_filespec *alloc_filespec(const char *path) |
780 | { | |
781 | int namelen = strlen(path); | |
782 | struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1); | |
783 | ||
784 | memset(spec, 0, sizeof(*spec)); | |
785 | spec->path = (char *)(spec + 1); | |
786 | memcpy(spec->path, path, namelen+1); | |
787 | return spec; | |
788 | } | |
789 | ||
790 | void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1, | |
791 | unsigned short mode) | |
792 | { | |
793 | if (mode) { | |
794 | spec->mode = canon_mode(mode); | |
795 | memcpy(spec->sha1, sha1, 20); | |
796 | spec->sha1_valid = !!memcmp(sha1, null_sha1, 20); | |
797 | } | |
798 | } | |
799 | ||
800 | /* | |
801 | * Given a name and sha1 pair, if the dircache tells us the file in | |
802 | * the work tree has that object contents, return true, so that | |
803 | * prepare_temp_file() does not have to inflate and extract. | |
804 | */ | |
805 | static int work_tree_matches(const char *name, const unsigned char *sha1) | |
806 | { | |
807 | struct cache_entry *ce; | |
808 | struct stat st; | |
809 | int pos, len; | |
810 | ||
811 | /* We do not read the cache ourselves here, because the | |
812 | * benchmark with my previous version that always reads cache | |
813 | * shows that it makes things worse for diff-tree comparing | |
814 | * two linux-2.6 kernel trees in an already checked out work | |
815 | * tree. This is because most diff-tree comparisons deal with | |
816 | * only a small number of files, while reading the cache is | |
817 | * expensive for a large project, and its cost outweighs the | |
818 | * savings we get by not inflating the object to a temporary | |
819 | * file. Practically, this code only helps when we are used | |
820 | * by diff-cache --cached, which does read the cache before | |
821 | * calling us. | |
822 | */ | |
823 | if (!active_cache) | |
824 | return 0; | |
825 | ||
826 | len = strlen(name); | |
827 | pos = cache_name_pos(name, len); | |
828 | if (pos < 0) | |
829 | return 0; | |
830 | ce = active_cache[pos]; | |
831 | if ((lstat(name, &st) < 0) || | |
832 | !S_ISREG(st.st_mode) || /* careful! */ | |
833 | ce_match_stat(ce, &st, 0) || | |
834 | memcmp(sha1, ce->sha1, 20)) | |
835 | return 0; | |
836 | /* we return 1 only when we can stat, it is a regular file, | |
837 | * stat information matches, and sha1 recorded in the cache | |
838 | * matches. I.e. we know the file in the work tree really is | |
839 | * the same as the <name, sha1> pair. | |
840 | */ | |
841 | return 1; | |
842 | } | |
843 | ||
844 | static struct sha1_size_cache { | |
845 | unsigned char sha1[20]; | |
846 | unsigned long size; | |
847 | } **sha1_size_cache; | |
848 | static int sha1_size_cache_nr, sha1_size_cache_alloc; | |
849 | ||
850 | static struct sha1_size_cache *locate_size_cache(unsigned char *sha1, | |
851 | int find_only, | |
852 | unsigned long size) | |
853 | { | |
854 | int first, last; | |
855 | struct sha1_size_cache *e; | |
856 | ||
857 | first = 0; | |
858 | last = sha1_size_cache_nr; | |
859 | while (last > first) { | |
860 | int cmp, next = (last + first) >> 1; | |
861 | e = sha1_size_cache[next]; | |
862 | cmp = memcmp(e->sha1, sha1, 20); | |
863 | if (!cmp) | |
864 | return e; | |
865 | if (cmp < 0) { | |
866 | last = next; | |
867 | continue; | |
868 | } | |
869 | first = next+1; | |
870 | } | |
871 | /* not found */ | |
872 | if (find_only) | |
873 | return NULL; | |
874 | /* insert to make it at "first" */ | |
875 | if (sha1_size_cache_alloc <= sha1_size_cache_nr) { | |
876 | sha1_size_cache_alloc = alloc_nr(sha1_size_cache_alloc); | |
877 | sha1_size_cache = xrealloc(sha1_size_cache, | |
878 | sha1_size_cache_alloc * | |
879 | sizeof(*sha1_size_cache)); | |
880 | } | |
881 | sha1_size_cache_nr++; | |
882 | if (first < sha1_size_cache_nr) | |
883 | memmove(sha1_size_cache + first + 1, sha1_size_cache + first, | |
884 | (sha1_size_cache_nr - first - 1) * | |
885 | sizeof(*sha1_size_cache)); | |
886 | e = xmalloc(sizeof(struct sha1_size_cache)); | |
887 | sha1_size_cache[first] = e; | |
888 | memcpy(e->sha1, sha1, 20); | |
889 | e->size = size; | |
890 | return e; | |
891 | } | |
892 | ||
893 | /* | |
894 | * While doing rename detection and pickaxe operation, we may need to | |
895 | * grab the data for the blob (or file) for our own in-core comparison. | |
896 | * diff_filespec has data and size fields for this purpose. | |
897 | */ | |
898 | int diff_populate_filespec(struct diff_filespec *s, int size_only) | |
899 | { | |
900 | int err = 0; | |
901 | if (!DIFF_FILE_VALID(s)) | |
902 | die("internal error: asking to populate invalid file."); | |
903 | if (S_ISDIR(s->mode)) | |
904 | return -1; | |
905 | ||
906 | if (!use_size_cache) | |
907 | size_only = 0; | |
908 | ||
909 | if (s->data) | |
910 | return err; | |
911 | if (!s->sha1_valid || | |
912 | work_tree_matches(s->path, s->sha1)) { | |
913 | struct stat st; | |
914 | int fd; | |
915 | if (lstat(s->path, &st) < 0) { | |
916 | if (errno == ENOENT) { | |
917 | err_empty: | |
918 | err = -1; | |
919 | empty: | |
d2543b8e | 920 | s->data = (char *)""; |
6973dcae JH |
921 | s->size = 0; |
922 | return err; | |
923 | } | |
924 | } | |
925 | s->size = st.st_size; | |
926 | if (!s->size) | |
927 | goto empty; | |
928 | if (size_only) | |
929 | return 0; | |
930 | if (S_ISLNK(st.st_mode)) { | |
931 | int ret; | |
932 | s->data = xmalloc(s->size); | |
933 | s->should_free = 1; | |
934 | ret = readlink(s->path, s->data, s->size); | |
935 | if (ret < 0) { | |
936 | free(s->data); | |
937 | goto err_empty; | |
938 | } | |
939 | return 0; | |
940 | } | |
941 | fd = open(s->path, O_RDONLY); | |
942 | if (fd < 0) | |
943 | goto err_empty; | |
944 | s->data = mmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); | |
945 | close(fd); | |
946 | if (s->data == MAP_FAILED) | |
947 | goto err_empty; | |
948 | s->should_munmap = 1; | |
949 | } | |
950 | else { | |
951 | char type[20]; | |
952 | struct sha1_size_cache *e; | |
953 | ||
954 | if (size_only) { | |
955 | e = locate_size_cache(s->sha1, 1, 0); | |
956 | if (e) { | |
957 | s->size = e->size; | |
958 | return 0; | |
959 | } | |
960 | if (!sha1_object_info(s->sha1, type, &s->size)) | |
961 | locate_size_cache(s->sha1, 0, s->size); | |
962 | } | |
963 | else { | |
964 | s->data = read_sha1_file(s->sha1, type, &s->size); | |
965 | s->should_free = 1; | |
966 | } | |
967 | } | |
968 | return 0; | |
969 | } | |
970 | ||
971 | void diff_free_filespec_data(struct diff_filespec *s) | |
972 | { | |
973 | if (s->should_free) | |
974 | free(s->data); | |
975 | else if (s->should_munmap) | |
976 | munmap(s->data, s->size); | |
977 | s->should_free = s->should_munmap = 0; | |
978 | s->data = NULL; | |
979 | free(s->cnt_data); | |
980 | s->cnt_data = NULL; | |
981 | } | |
982 | ||
983 | static void prep_temp_blob(struct diff_tempfile *temp, | |
984 | void *blob, | |
985 | unsigned long size, | |
986 | const unsigned char *sha1, | |
987 | int mode) | |
988 | { | |
989 | int fd; | |
990 | ||
991 | fd = git_mkstemp(temp->tmp_path, TEMPFILE_PATH_LEN, ".diff_XXXXXX"); | |
992 | if (fd < 0) | |
993 | die("unable to create temp-file"); | |
994 | if (write(fd, blob, size) != size) | |
995 | die("unable to write temp-file"); | |
996 | close(fd); | |
997 | temp->name = temp->tmp_path; | |
998 | strcpy(temp->hex, sha1_to_hex(sha1)); | |
999 | temp->hex[40] = 0; | |
1000 | sprintf(temp->mode, "%06o", mode); | |
1001 | } | |
1002 | ||
1003 | static void prepare_temp_file(const char *name, | |
1004 | struct diff_tempfile *temp, | |
1005 | struct diff_filespec *one) | |
1006 | { | |
1007 | if (!DIFF_FILE_VALID(one)) { | |
1008 | not_a_valid_file: | |
1009 | /* A '-' entry produces this for file-2, and | |
1010 | * a '+' entry produces this for file-1. | |
1011 | */ | |
1012 | temp->name = "/dev/null"; | |
1013 | strcpy(temp->hex, "."); | |
1014 | strcpy(temp->mode, "."); | |
1015 | return; | |
1016 | } | |
1017 | ||
1018 | if (!one->sha1_valid || | |
1019 | work_tree_matches(name, one->sha1)) { | |
1020 | struct stat st; | |
1021 | if (lstat(name, &st) < 0) { | |
1022 | if (errno == ENOENT) | |
1023 | goto not_a_valid_file; | |
1024 | die("stat(%s): %s", name, strerror(errno)); | |
1025 | } | |
1026 | if (S_ISLNK(st.st_mode)) { | |
1027 | int ret; | |
1028 | char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */ | |
1029 | if (sizeof(buf) <= st.st_size) | |
1030 | die("symlink too long: %s", name); | |
1031 | ret = readlink(name, buf, st.st_size); | |
1032 | if (ret < 0) | |
1033 | die("readlink(%s)", name); | |
1034 | prep_temp_blob(temp, buf, st.st_size, | |
1035 | (one->sha1_valid ? | |
1036 | one->sha1 : null_sha1), | |
1037 | (one->sha1_valid ? | |
1038 | one->mode : S_IFLNK)); | |
1039 | } | |
1040 | else { | |
1041 | /* we can borrow from the file in the work tree */ | |
1042 | temp->name = name; | |
1043 | if (!one->sha1_valid) | |
1044 | strcpy(temp->hex, sha1_to_hex(null_sha1)); | |
1045 | else | |
1046 | strcpy(temp->hex, sha1_to_hex(one->sha1)); | |
1047 | /* Even though we may sometimes borrow the | |
1048 | * contents from the work tree, we always want | |
1049 | * one->mode. mode is trustworthy even when | |
1050 | * !(one->sha1_valid), as long as | |
1051 | * DIFF_FILE_VALID(one). | |
1052 | */ | |
1053 | sprintf(temp->mode, "%06o", one->mode); | |
1054 | } | |
1055 | return; | |
1056 | } | |
1057 | else { | |
1058 | if (diff_populate_filespec(one, 0)) | |
1059 | die("cannot read data blob for %s", one->path); | |
1060 | prep_temp_blob(temp, one->data, one->size, | |
1061 | one->sha1, one->mode); | |
1062 | } | |
1063 | } | |
1064 | ||
1065 | static void remove_tempfile(void) | |
1066 | { | |
1067 | int i; | |
1068 | ||
1069 | for (i = 0; i < 2; i++) | |
1070 | if (diff_temp[i].name == diff_temp[i].tmp_path) { | |
1071 | unlink(diff_temp[i].name); | |
1072 | diff_temp[i].name = NULL; | |
1073 | } | |
1074 | } | |
1075 | ||
1076 | static void remove_tempfile_on_signal(int signo) | |
1077 | { | |
1078 | remove_tempfile(); | |
1079 | signal(SIGINT, SIG_DFL); | |
1080 | raise(signo); | |
1081 | } | |
1082 | ||
1083 | static int spawn_prog(const char *pgm, const char **arg) | |
1084 | { | |
1085 | pid_t pid; | |
1086 | int status; | |
1087 | ||
1088 | fflush(NULL); | |
1089 | pid = fork(); | |
1090 | if (pid < 0) | |
1091 | die("unable to fork"); | |
1092 | if (!pid) { | |
1093 | execvp(pgm, (char *const*) arg); | |
1094 | exit(255); | |
1095 | } | |
1096 | ||
1097 | while (waitpid(pid, &status, 0) < 0) { | |
1098 | if (errno == EINTR) | |
1099 | continue; | |
1100 | return -1; | |
1101 | } | |
1102 | ||
1103 | /* Earlier we did not check the exit status because | |
1104 | * diff exits non-zero if files are different, and | |
1105 | * we are not interested in knowing that. It was a | |
1106 | * mistake which made it harder to quit a diff-* | |
1107 | * session that uses the git-apply-patch-script as | |
1108 | * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF | |
1109 | * should also exit non-zero only when it wants to | |
1110 | * abort the entire diff-* session. | |
1111 | */ | |
1112 | if (WIFEXITED(status) && !WEXITSTATUS(status)) | |
1113 | return 0; | |
1114 | return -1; | |
1115 | } | |
1116 | ||
1117 | /* An external diff command takes: | |
1118 | * | |
1119 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ | |
1120 | * infile2 infile2-sha1 infile2-mode [ rename-to ] | |
1121 | * | |
1122 | */ | |
1123 | static void run_external_diff(const char *pgm, | |
1124 | const char *name, | |
1125 | const char *other, | |
1126 | struct diff_filespec *one, | |
1127 | struct diff_filespec *two, | |
1128 | const char *xfrm_msg, | |
1129 | int complete_rewrite) | |
1130 | { | |
1131 | const char *spawn_arg[10]; | |
1132 | struct diff_tempfile *temp = diff_temp; | |
1133 | int retval; | |
1134 | static int atexit_asked = 0; | |
1135 | const char *othername; | |
1136 | const char **arg = &spawn_arg[0]; | |
1137 | ||
1138 | othername = (other? other : name); | |
1139 | if (one && two) { | |
1140 | prepare_temp_file(name, &temp[0], one); | |
1141 | prepare_temp_file(othername, &temp[1], two); | |
1142 | if (! atexit_asked && | |
1143 | (temp[0].name == temp[0].tmp_path || | |
1144 | temp[1].name == temp[1].tmp_path)) { | |
1145 | atexit_asked = 1; | |
1146 | atexit(remove_tempfile); | |
1147 | } | |
1148 | signal(SIGINT, remove_tempfile_on_signal); | |
1149 | } | |
1150 | ||
1151 | if (one && two) { | |
1152 | *arg++ = pgm; | |
1153 | *arg++ = name; | |
1154 | *arg++ = temp[0].name; | |
1155 | *arg++ = temp[0].hex; | |
1156 | *arg++ = temp[0].mode; | |
1157 | *arg++ = temp[1].name; | |
1158 | *arg++ = temp[1].hex; | |
1159 | *arg++ = temp[1].mode; | |
1160 | if (other) { | |
1161 | *arg++ = other; | |
1162 | *arg++ = xfrm_msg; | |
1163 | } | |
1164 | } else { | |
1165 | *arg++ = pgm; | |
1166 | *arg++ = name; | |
1167 | } | |
1168 | *arg = NULL; | |
1169 | retval = spawn_prog(pgm, spawn_arg); | |
1170 | remove_tempfile(); | |
1171 | if (retval) { | |
1172 | fprintf(stderr, "external diff died, stopping at %s.\n", name); | |
1173 | exit(1); | |
1174 | } | |
1175 | } | |
1176 | ||
1177 | static void run_diff_cmd(const char *pgm, | |
1178 | const char *name, | |
1179 | const char *other, | |
1180 | struct diff_filespec *one, | |
1181 | struct diff_filespec *two, | |
1182 | const char *xfrm_msg, | |
051308f6 | 1183 | struct diff_options *o, |
6973dcae JH |
1184 | int complete_rewrite) |
1185 | { | |
1186 | if (pgm) { | |
1187 | run_external_diff(pgm, name, other, one, two, xfrm_msg, | |
1188 | complete_rewrite); | |
1189 | return; | |
1190 | } | |
1191 | if (one && two) | |
1192 | builtin_diff(name, other ? other : name, | |
051308f6 | 1193 | one, two, xfrm_msg, o, complete_rewrite); |
6973dcae JH |
1194 | else |
1195 | printf("* Unmerged path %s\n", name); | |
1196 | } | |
1197 | ||
1198 | static void diff_fill_sha1_info(struct diff_filespec *one) | |
1199 | { | |
1200 | if (DIFF_FILE_VALID(one)) { | |
1201 | if (!one->sha1_valid) { | |
1202 | struct stat st; | |
1203 | if (lstat(one->path, &st) < 0) | |
1204 | die("stat %s", one->path); | |
1205 | if (index_path(one->sha1, one->path, &st, 0)) | |
1206 | die("cannot hash %s\n", one->path); | |
1207 | } | |
1208 | } | |
1209 | else | |
1210 | memset(one->sha1, 0, 20); | |
1211 | } | |
1212 | ||
1213 | static void run_diff(struct diff_filepair *p, struct diff_options *o) | |
1214 | { | |
1215 | const char *pgm = external_diff(); | |
1216 | char msg[PATH_MAX*2+300], *xfrm_msg; | |
1217 | struct diff_filespec *one; | |
1218 | struct diff_filespec *two; | |
1219 | const char *name; | |
1220 | const char *other; | |
1221 | char *name_munged, *other_munged; | |
1222 | int complete_rewrite = 0; | |
1223 | int len; | |
1224 | ||
1225 | if (DIFF_PAIR_UNMERGED(p)) { | |
1226 | /* unmerged */ | |
051308f6 | 1227 | run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0); |
6973dcae JH |
1228 | return; |
1229 | } | |
1230 | ||
1231 | name = p->one->path; | |
1232 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
1233 | name_munged = quote_one(name); | |
1234 | other_munged = quote_one(other); | |
1235 | one = p->one; two = p->two; | |
1236 | ||
1237 | diff_fill_sha1_info(one); | |
1238 | diff_fill_sha1_info(two); | |
1239 | ||
1240 | len = 0; | |
1241 | switch (p->status) { | |
1242 | case DIFF_STATUS_COPIED: | |
1243 | len += snprintf(msg + len, sizeof(msg) - len, | |
1244 | "similarity index %d%%\n" | |
1245 | "copy from %s\n" | |
1246 | "copy to %s\n", | |
1247 | (int)(0.5 + p->score * 100.0/MAX_SCORE), | |
1248 | name_munged, other_munged); | |
1249 | break; | |
1250 | case DIFF_STATUS_RENAMED: | |
1251 | len += snprintf(msg + len, sizeof(msg) - len, | |
1252 | "similarity index %d%%\n" | |
1253 | "rename from %s\n" | |
1254 | "rename to %s\n", | |
1255 | (int)(0.5 + p->score * 100.0/MAX_SCORE), | |
1256 | name_munged, other_munged); | |
1257 | break; | |
1258 | case DIFF_STATUS_MODIFIED: | |
1259 | if (p->score) { | |
1260 | len += snprintf(msg + len, sizeof(msg) - len, | |
1261 | "dissimilarity index %d%%\n", | |
1262 | (int)(0.5 + p->score * | |
1263 | 100.0/MAX_SCORE)); | |
1264 | complete_rewrite = 1; | |
1265 | break; | |
1266 | } | |
1267 | /* fallthru */ | |
1268 | default: | |
1269 | /* nothing */ | |
1270 | ; | |
1271 | } | |
1272 | ||
1273 | if (memcmp(one->sha1, two->sha1, 20)) { | |
6973dcae | 1274 | int abbrev = o->full_index ? 40 : DEFAULT_ABBREV; |
6973dcae JH |
1275 | |
1276 | len += snprintf(msg + len, sizeof(msg) - len, | |
1277 | "index %.*s..%.*s", | |
dcb3450f LT |
1278 | abbrev, sha1_to_hex(one->sha1), |
1279 | abbrev, sha1_to_hex(two->sha1)); | |
6973dcae JH |
1280 | if (one->mode == two->mode) |
1281 | len += snprintf(msg + len, sizeof(msg) - len, | |
1282 | " %06o", one->mode); | |
1283 | len += snprintf(msg + len, sizeof(msg) - len, "\n"); | |
1284 | } | |
1285 | ||
1286 | if (len) | |
1287 | msg[--len] = 0; | |
1288 | xfrm_msg = len ? msg : NULL; | |
1289 | ||
1290 | if (!pgm && | |
1291 | DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) && | |
1292 | (S_IFMT & one->mode) != (S_IFMT & two->mode)) { | |
1293 | /* a filepair that changes between file and symlink | |
1294 | * needs to be split into deletion and creation. | |
1295 | */ | |
1296 | struct diff_filespec *null = alloc_filespec(two->path); | |
051308f6 | 1297 | run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0); |
6973dcae JH |
1298 | free(null); |
1299 | null = alloc_filespec(one->path); | |
051308f6 | 1300 | run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0); |
6973dcae JH |
1301 | free(null); |
1302 | } | |
1303 | else | |
051308f6 | 1304 | run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o, |
6973dcae JH |
1305 | complete_rewrite); |
1306 | ||
1307 | free(name_munged); | |
1308 | free(other_munged); | |
1309 | } | |
1310 | ||
1311 | static void run_diffstat(struct diff_filepair *p, struct diff_options *o, | |
1312 | struct diffstat_t *diffstat) | |
1313 | { | |
1314 | const char *name; | |
1315 | const char *other; | |
710158e3 | 1316 | int complete_rewrite = 0; |
6973dcae JH |
1317 | |
1318 | if (DIFF_PAIR_UNMERGED(p)) { | |
1319 | /* unmerged */ | |
710158e3 | 1320 | builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, 0); |
6973dcae JH |
1321 | return; |
1322 | } | |
1323 | ||
1324 | name = p->one->path; | |
1325 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
1326 | ||
1327 | diff_fill_sha1_info(p->one); | |
1328 | diff_fill_sha1_info(p->two); | |
1329 | ||
710158e3 JH |
1330 | if (p->status == DIFF_STATUS_MODIFIED && p->score) |
1331 | complete_rewrite = 1; | |
1332 | builtin_diffstat(name, other, p->one, p->two, diffstat, complete_rewrite); | |
6973dcae JH |
1333 | } |
1334 | ||
88246898 JS |
1335 | static void run_checkdiff(struct diff_filepair *p, struct diff_options *o) |
1336 | { | |
1337 | const char *name; | |
1338 | const char *other; | |
1339 | ||
1340 | if (DIFF_PAIR_UNMERGED(p)) { | |
1341 | /* unmerged */ | |
1342 | return; | |
1343 | } | |
1344 | ||
1345 | name = p->one->path; | |
1346 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
1347 | ||
1348 | diff_fill_sha1_info(p->one); | |
1349 | diff_fill_sha1_info(p->two); | |
1350 | ||
1351 | builtin_checkdiff(name, other, p->one, p->two); | |
1352 | } | |
1353 | ||
6973dcae JH |
1354 | void diff_setup(struct diff_options *options) |
1355 | { | |
1356 | memset(options, 0, sizeof(*options)); | |
1357 | options->output_format = DIFF_FORMAT_RAW; | |
1358 | options->line_termination = '\n'; | |
1359 | options->break_opt = -1; | |
1360 | options->rename_limit = -1; | |
ee1e5412 | 1361 | options->context = 3; |
6973dcae JH |
1362 | |
1363 | options->change = diff_change; | |
1364 | options->add_remove = diff_addremove; | |
1365 | } | |
1366 | ||
1367 | int diff_setup_done(struct diff_options *options) | |
1368 | { | |
1369 | if ((options->find_copies_harder && | |
1370 | options->detect_rename != DIFF_DETECT_COPY) || | |
1371 | (0 <= options->rename_limit && !options->detect_rename)) | |
1372 | return -1; | |
1373 | ||
1374 | /* | |
1375 | * These cases always need recursive; we do not drop caller-supplied | |
1376 | * recursive bits for other formats here. | |
1377 | */ | |
1378 | if ((options->output_format == DIFF_FORMAT_PATCH) || | |
88246898 JS |
1379 | (options->output_format == DIFF_FORMAT_DIFFSTAT) || |
1380 | (options->output_format == DIFF_FORMAT_CHECKDIFF)) | |
6973dcae JH |
1381 | options->recursive = 1; |
1382 | ||
5e363541 JH |
1383 | /* |
1384 | * These combinations do not make sense. | |
1385 | */ | |
1386 | if (options->output_format == DIFF_FORMAT_RAW) | |
1387 | options->with_raw = 0; | |
1388 | if (options->output_format == DIFF_FORMAT_DIFFSTAT) | |
1389 | options->with_stat = 0; | |
1390 | ||
6973dcae JH |
1391 | if (options->detect_rename && options->rename_limit < 0) |
1392 | options->rename_limit = diff_rename_limit_default; | |
1393 | if (options->setup & DIFF_SETUP_USE_CACHE) { | |
1394 | if (!active_cache) | |
1395 | /* read-cache does not die even when it fails | |
1396 | * so it is safe for us to do this here. Also | |
1397 | * it does not smudge active_cache or active_nr | |
1398 | * when it fails, so we do not have to worry about | |
1399 | * cleaning it up ourselves either. | |
1400 | */ | |
1401 | read_cache(); | |
1402 | } | |
1403 | if (options->setup & DIFF_SETUP_USE_SIZE_CACHE) | |
1404 | use_size_cache = 1; | |
1405 | if (options->abbrev <= 0 || 40 < options->abbrev) | |
1406 | options->abbrev = 40; /* full */ | |
1407 | ||
1408 | return 0; | |
1409 | } | |
1410 | ||
d2543b8e | 1411 | static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val) |
ee1e5412 LT |
1412 | { |
1413 | char c, *eq; | |
1414 | int len; | |
1415 | ||
1416 | if (*arg != '-') | |
1417 | return 0; | |
1418 | c = *++arg; | |
1419 | if (!c) | |
1420 | return 0; | |
1421 | if (c == arg_short) { | |
1422 | c = *++arg; | |
1423 | if (!c) | |
1424 | return 1; | |
1425 | if (val && isdigit(c)) { | |
1426 | char *end; | |
1427 | int n = strtoul(arg, &end, 10); | |
1428 | if (*end) | |
1429 | return 0; | |
1430 | *val = n; | |
1431 | return 1; | |
1432 | } | |
1433 | return 0; | |
1434 | } | |
1435 | if (c != '-') | |
1436 | return 0; | |
1437 | arg++; | |
1438 | eq = strchr(arg, '='); | |
1439 | if (eq) | |
1440 | len = eq - arg; | |
1441 | else | |
1442 | len = strlen(arg); | |
1443 | if (!len || strncmp(arg, arg_long, len)) | |
1444 | return 0; | |
1445 | if (eq) { | |
1446 | int n; | |
1447 | char *end; | |
1448 | if (!isdigit(*++eq)) | |
1449 | return 0; | |
1450 | n = strtoul(eq, &end, 10); | |
1451 | if (*end) | |
1452 | return 0; | |
1453 | *val = n; | |
1454 | } | |
1455 | return 1; | |
1456 | } | |
1457 | ||
6973dcae JH |
1458 | int diff_opt_parse(struct diff_options *options, const char **av, int ac) |
1459 | { | |
1460 | const char *arg = av[0]; | |
1461 | if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) | |
1462 | options->output_format = DIFF_FORMAT_PATCH; | |
ee1e5412 LT |
1463 | else if (opt_arg(arg, 'U', "unified", &options->context)) |
1464 | options->output_format = DIFF_FORMAT_PATCH; | |
6973dcae JH |
1465 | else if (!strcmp(arg, "--patch-with-raw")) { |
1466 | options->output_format = DIFF_FORMAT_PATCH; | |
1467 | options->with_raw = 1; | |
1468 | } | |
1469 | else if (!strcmp(arg, "--stat")) | |
1470 | options->output_format = DIFF_FORMAT_DIFFSTAT; | |
88246898 JS |
1471 | else if (!strcmp(arg, "--check")) |
1472 | options->output_format = DIFF_FORMAT_CHECKDIFF; | |
4bbd261b SE |
1473 | else if (!strcmp(arg, "--summary")) |
1474 | options->summary = 1; | |
6973dcae JH |
1475 | else if (!strcmp(arg, "--patch-with-stat")) { |
1476 | options->output_format = DIFF_FORMAT_PATCH; | |
1477 | options->with_stat = 1; | |
1478 | } | |
1479 | else if (!strcmp(arg, "-z")) | |
1480 | options->line_termination = 0; | |
1481 | else if (!strncmp(arg, "-l", 2)) | |
1482 | options->rename_limit = strtoul(arg+2, NULL, 10); | |
1483 | else if (!strcmp(arg, "--full-index")) | |
1484 | options->full_index = 1; | |
0660626c JH |
1485 | else if (!strcmp(arg, "--binary")) { |
1486 | options->output_format = DIFF_FORMAT_PATCH; | |
1487 | options->full_index = options->binary = 1; | |
1488 | } | |
6973dcae JH |
1489 | else if (!strcmp(arg, "--name-only")) |
1490 | options->output_format = DIFF_FORMAT_NAME; | |
1491 | else if (!strcmp(arg, "--name-status")) | |
1492 | options->output_format = DIFF_FORMAT_NAME_STATUS; | |
1493 | else if (!strcmp(arg, "-R")) | |
1494 | options->reverse_diff = 1; | |
1495 | else if (!strncmp(arg, "-S", 2)) | |
1496 | options->pickaxe = arg + 2; | |
1497 | else if (!strcmp(arg, "-s")) | |
1498 | options->output_format = DIFF_FORMAT_NO_OUTPUT; | |
1499 | else if (!strncmp(arg, "-O", 2)) | |
1500 | options->orderfile = arg + 2; | |
1501 | else if (!strncmp(arg, "--diff-filter=", 14)) | |
1502 | options->filter = arg + 14; | |
1503 | else if (!strcmp(arg, "--pickaxe-all")) | |
1504 | options->pickaxe_opts = DIFF_PICKAXE_ALL; | |
1505 | else if (!strcmp(arg, "--pickaxe-regex")) | |
1506 | options->pickaxe_opts = DIFF_PICKAXE_REGEX; | |
1507 | else if (!strncmp(arg, "-B", 2)) { | |
1508 | if ((options->break_opt = | |
1509 | diff_scoreopt_parse(arg)) == -1) | |
1510 | return -1; | |
1511 | } | |
1512 | else if (!strncmp(arg, "-M", 2)) { | |
1513 | if ((options->rename_score = | |
1514 | diff_scoreopt_parse(arg)) == -1) | |
1515 | return -1; | |
1516 | options->detect_rename = DIFF_DETECT_RENAME; | |
1517 | } | |
1518 | else if (!strncmp(arg, "-C", 2)) { | |
1519 | if ((options->rename_score = | |
1520 | diff_scoreopt_parse(arg)) == -1) | |
1521 | return -1; | |
1522 | options->detect_rename = DIFF_DETECT_COPY; | |
1523 | } | |
1524 | else if (!strcmp(arg, "--find-copies-harder")) | |
1525 | options->find_copies_harder = 1; | |
1526 | else if (!strcmp(arg, "--abbrev")) | |
1527 | options->abbrev = DEFAULT_ABBREV; | |
1528 | else if (!strncmp(arg, "--abbrev=", 9)) { | |
1529 | options->abbrev = strtoul(arg + 9, NULL, 10); | |
1530 | if (options->abbrev < MINIMUM_ABBREV) | |
1531 | options->abbrev = MINIMUM_ABBREV; | |
1532 | else if (40 < options->abbrev) | |
1533 | options->abbrev = 40; | |
1534 | } | |
cd112cef JS |
1535 | else if (!strcmp(arg, "--color")) |
1536 | options->color_diff = 1; | |
6973dcae JH |
1537 | else |
1538 | return 0; | |
1539 | return 1; | |
1540 | } | |
1541 | ||
1542 | static int parse_num(const char **cp_p) | |
1543 | { | |
1544 | unsigned long num, scale; | |
1545 | int ch, dot; | |
1546 | const char *cp = *cp_p; | |
1547 | ||
1548 | num = 0; | |
1549 | scale = 1; | |
1550 | dot = 0; | |
1551 | for(;;) { | |
1552 | ch = *cp; | |
1553 | if ( !dot && ch == '.' ) { | |
1554 | scale = 1; | |
1555 | dot = 1; | |
1556 | } else if ( ch == '%' ) { | |
1557 | scale = dot ? scale*100 : 100; | |
1558 | cp++; /* % is always at the end */ | |
1559 | break; | |
1560 | } else if ( ch >= '0' && ch <= '9' ) { | |
1561 | if ( scale < 100000 ) { | |
1562 | scale *= 10; | |
1563 | num = (num*10) + (ch-'0'); | |
1564 | } | |
1565 | } else { | |
1566 | break; | |
1567 | } | |
1568 | cp++; | |
1569 | } | |
1570 | *cp_p = cp; | |
1571 | ||
1572 | /* user says num divided by scale and we say internally that | |
1573 | * is MAX_SCORE * num / scale. | |
1574 | */ | |
1575 | return (num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale); | |
1576 | } | |
1577 | ||
1578 | int diff_scoreopt_parse(const char *opt) | |
1579 | { | |
1580 | int opt1, opt2, cmd; | |
1581 | ||
1582 | if (*opt++ != '-') | |
1583 | return -1; | |
1584 | cmd = *opt++; | |
1585 | if (cmd != 'M' && cmd != 'C' && cmd != 'B') | |
1586 | return -1; /* that is not a -M, -C nor -B option */ | |
1587 | ||
1588 | opt1 = parse_num(&opt); | |
1589 | if (cmd != 'B') | |
1590 | opt2 = 0; | |
1591 | else { | |
1592 | if (*opt == 0) | |
1593 | opt2 = 0; | |
1594 | else if (*opt != '/') | |
1595 | return -1; /* we expect -B80/99 or -B80 */ | |
1596 | else { | |
1597 | opt++; | |
1598 | opt2 = parse_num(&opt); | |
1599 | } | |
1600 | } | |
1601 | if (*opt != 0) | |
1602 | return -1; | |
1603 | return opt1 | (opt2 << 16); | |
1604 | } | |
1605 | ||
1606 | struct diff_queue_struct diff_queued_diff; | |
1607 | ||
1608 | void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp) | |
1609 | { | |
1610 | if (queue->alloc <= queue->nr) { | |
1611 | queue->alloc = alloc_nr(queue->alloc); | |
1612 | queue->queue = xrealloc(queue->queue, | |
1613 | sizeof(dp) * queue->alloc); | |
1614 | } | |
1615 | queue->queue[queue->nr++] = dp; | |
1616 | } | |
1617 | ||
1618 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, | |
1619 | struct diff_filespec *one, | |
1620 | struct diff_filespec *two) | |
1621 | { | |
1622 | struct diff_filepair *dp = xmalloc(sizeof(*dp)); | |
1623 | dp->one = one; | |
1624 | dp->two = two; | |
1625 | dp->score = 0; | |
1626 | dp->status = 0; | |
1627 | dp->source_stays = 0; | |
1628 | dp->broken_pair = 0; | |
1629 | if (queue) | |
1630 | diff_q(queue, dp); | |
1631 | return dp; | |
1632 | } | |
1633 | ||
1634 | void diff_free_filepair(struct diff_filepair *p) | |
1635 | { | |
1636 | diff_free_filespec_data(p->one); | |
1637 | diff_free_filespec_data(p->two); | |
1638 | free(p->one); | |
1639 | free(p->two); | |
1640 | free(p); | |
1641 | } | |
1642 | ||
1643 | /* This is different from find_unique_abbrev() in that | |
1644 | * it stuffs the result with dots for alignment. | |
1645 | */ | |
1646 | const char *diff_unique_abbrev(const unsigned char *sha1, int len) | |
1647 | { | |
1648 | int abblen; | |
1649 | const char *abbrev; | |
1650 | if (len == 40) | |
1651 | return sha1_to_hex(sha1); | |
1652 | ||
1653 | abbrev = find_unique_abbrev(sha1, len); | |
1654 | if (!abbrev) | |
1655 | return sha1_to_hex(sha1); | |
1656 | abblen = strlen(abbrev); | |
1657 | if (abblen < 37) { | |
1658 | static char hex[41]; | |
1659 | if (len < abblen && abblen <= len + 2) | |
1660 | sprintf(hex, "%s%.*s", abbrev, len+3-abblen, ".."); | |
1661 | else | |
1662 | sprintf(hex, "%s...", abbrev); | |
1663 | return hex; | |
1664 | } | |
1665 | return sha1_to_hex(sha1); | |
1666 | } | |
1667 | ||
1668 | static void diff_flush_raw(struct diff_filepair *p, | |
1669 | int line_termination, | |
1670 | int inter_name_termination, | |
1671 | struct diff_options *options, | |
1672 | int output_format) | |
1673 | { | |
1674 | int two_paths; | |
1675 | char status[10]; | |
1676 | int abbrev = options->abbrev; | |
1677 | const char *path_one, *path_two; | |
1678 | ||
1679 | path_one = p->one->path; | |
1680 | path_two = p->two->path; | |
1681 | if (line_termination) { | |
1682 | path_one = quote_one(path_one); | |
1683 | path_two = quote_one(path_two); | |
1684 | } | |
1685 | ||
1686 | if (p->score) | |
1687 | sprintf(status, "%c%03d", p->status, | |
1688 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); | |
1689 | else { | |
1690 | status[0] = p->status; | |
1691 | status[1] = 0; | |
1692 | } | |
1693 | switch (p->status) { | |
1694 | case DIFF_STATUS_COPIED: | |
1695 | case DIFF_STATUS_RENAMED: | |
1696 | two_paths = 1; | |
1697 | break; | |
1698 | case DIFF_STATUS_ADDED: | |
1699 | case DIFF_STATUS_DELETED: | |
1700 | two_paths = 0; | |
1701 | break; | |
1702 | default: | |
1703 | two_paths = 0; | |
1704 | break; | |
1705 | } | |
1706 | if (output_format != DIFF_FORMAT_NAME_STATUS) { | |
1707 | printf(":%06o %06o %s ", | |
1708 | p->one->mode, p->two->mode, | |
1709 | diff_unique_abbrev(p->one->sha1, abbrev)); | |
1710 | printf("%s ", | |
1711 | diff_unique_abbrev(p->two->sha1, abbrev)); | |
1712 | } | |
1713 | printf("%s%c%s", status, inter_name_termination, path_one); | |
1714 | if (two_paths) | |
1715 | printf("%c%s", inter_name_termination, path_two); | |
1716 | putchar(line_termination); | |
1717 | if (path_one != p->one->path) | |
1718 | free((void*)path_one); | |
1719 | if (path_two != p->two->path) | |
1720 | free((void*)path_two); | |
1721 | } | |
1722 | ||
d2543b8e | 1723 | static void diff_flush_name(struct diff_filepair *p, int line_termination) |
6973dcae JH |
1724 | { |
1725 | char *path = p->two->path; | |
1726 | ||
1727 | if (line_termination) | |
1728 | path = quote_one(p->two->path); | |
6973dcae JH |
1729 | printf("%s%c", path, line_termination); |
1730 | if (p->two->path != path) | |
1731 | free(path); | |
1732 | } | |
1733 | ||
1734 | int diff_unmodified_pair(struct diff_filepair *p) | |
1735 | { | |
1736 | /* This function is written stricter than necessary to support | |
1737 | * the currently implemented transformers, but the idea is to | |
1738 | * let transformers to produce diff_filepairs any way they want, | |
1739 | * and filter and clean them up here before producing the output. | |
1740 | */ | |
1741 | struct diff_filespec *one, *two; | |
1742 | ||
1743 | if (DIFF_PAIR_UNMERGED(p)) | |
1744 | return 0; /* unmerged is interesting */ | |
1745 | ||
1746 | one = p->one; | |
1747 | two = p->two; | |
1748 | ||
1749 | /* deletion, addition, mode or type change | |
1750 | * and rename are all interesting. | |
1751 | */ | |
1752 | if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) || | |
1753 | DIFF_PAIR_MODE_CHANGED(p) || | |
1754 | strcmp(one->path, two->path)) | |
1755 | return 0; | |
1756 | ||
1757 | /* both are valid and point at the same path. that is, we are | |
1758 | * dealing with a change. | |
1759 | */ | |
1760 | if (one->sha1_valid && two->sha1_valid && | |
1761 | !memcmp(one->sha1, two->sha1, sizeof(one->sha1))) | |
1762 | return 1; /* no change */ | |
1763 | if (!one->sha1_valid && !two->sha1_valid) | |
1764 | return 1; /* both look at the same file on the filesystem. */ | |
1765 | return 0; | |
1766 | } | |
1767 | ||
1768 | static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o) | |
1769 | { | |
1770 | if (diff_unmodified_pair(p)) | |
1771 | return; | |
1772 | ||
1773 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
1774 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
1775 | return; /* no tree diffs in patch format */ | |
1776 | ||
1777 | run_diff(p, o); | |
1778 | } | |
1779 | ||
1780 | static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o, | |
1781 | struct diffstat_t *diffstat) | |
1782 | { | |
1783 | if (diff_unmodified_pair(p)) | |
1784 | return; | |
1785 | ||
1786 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
1787 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
1788 | return; /* no tree diffs in patch format */ | |
1789 | ||
1790 | run_diffstat(p, o, diffstat); | |
1791 | } | |
1792 | ||
88246898 JS |
1793 | static void diff_flush_checkdiff(struct diff_filepair *p, |
1794 | struct diff_options *o) | |
1795 | { | |
1796 | if (diff_unmodified_pair(p)) | |
1797 | return; | |
1798 | ||
1799 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
1800 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
1801 | return; /* no tree diffs in patch format */ | |
1802 | ||
1803 | run_checkdiff(p, o); | |
1804 | } | |
1805 | ||
6973dcae JH |
1806 | int diff_queue_is_empty(void) |
1807 | { | |
1808 | struct diff_queue_struct *q = &diff_queued_diff; | |
1809 | int i; | |
1810 | for (i = 0; i < q->nr; i++) | |
1811 | if (!diff_unmodified_pair(q->queue[i])) | |
1812 | return 0; | |
1813 | return 1; | |
1814 | } | |
1815 | ||
1816 | #if DIFF_DEBUG | |
1817 | void diff_debug_filespec(struct diff_filespec *s, int x, const char *one) | |
1818 | { | |
1819 | fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", | |
1820 | x, one ? one : "", | |
1821 | s->path, | |
1822 | DIFF_FILE_VALID(s) ? "valid" : "invalid", | |
1823 | s->mode, | |
1824 | s->sha1_valid ? sha1_to_hex(s->sha1) : ""); | |
1825 | fprintf(stderr, "queue[%d] %s size %lu flags %d\n", | |
1826 | x, one ? one : "", | |
1827 | s->size, s->xfrm_flags); | |
1828 | } | |
1829 | ||
1830 | void diff_debug_filepair(const struct diff_filepair *p, int i) | |
1831 | { | |
1832 | diff_debug_filespec(p->one, i, "one"); | |
1833 | diff_debug_filespec(p->two, i, "two"); | |
1834 | fprintf(stderr, "score %d, status %c stays %d broken %d\n", | |
1835 | p->score, p->status ? p->status : '?', | |
1836 | p->source_stays, p->broken_pair); | |
1837 | } | |
1838 | ||
1839 | void diff_debug_queue(const char *msg, struct diff_queue_struct *q) | |
1840 | { | |
1841 | int i; | |
1842 | if (msg) | |
1843 | fprintf(stderr, "%s\n", msg); | |
1844 | fprintf(stderr, "q->nr = %d\n", q->nr); | |
1845 | for (i = 0; i < q->nr; i++) { | |
1846 | struct diff_filepair *p = q->queue[i]; | |
1847 | diff_debug_filepair(p, i); | |
1848 | } | |
1849 | } | |
1850 | #endif | |
1851 | ||
1852 | static void diff_resolve_rename_copy(void) | |
1853 | { | |
1854 | int i, j; | |
1855 | struct diff_filepair *p, *pp; | |
1856 | struct diff_queue_struct *q = &diff_queued_diff; | |
1857 | ||
1858 | diff_debug_queue("resolve-rename-copy", q); | |
1859 | ||
1860 | for (i = 0; i < q->nr; i++) { | |
1861 | p = q->queue[i]; | |
1862 | p->status = 0; /* undecided */ | |
1863 | if (DIFF_PAIR_UNMERGED(p)) | |
1864 | p->status = DIFF_STATUS_UNMERGED; | |
1865 | else if (!DIFF_FILE_VALID(p->one)) | |
1866 | p->status = DIFF_STATUS_ADDED; | |
1867 | else if (!DIFF_FILE_VALID(p->two)) | |
1868 | p->status = DIFF_STATUS_DELETED; | |
1869 | else if (DIFF_PAIR_TYPE_CHANGED(p)) | |
1870 | p->status = DIFF_STATUS_TYPE_CHANGED; | |
1871 | ||
1872 | /* from this point on, we are dealing with a pair | |
1873 | * whose both sides are valid and of the same type, i.e. | |
1874 | * either in-place edit or rename/copy edit. | |
1875 | */ | |
1876 | else if (DIFF_PAIR_RENAME(p)) { | |
1877 | if (p->source_stays) { | |
1878 | p->status = DIFF_STATUS_COPIED; | |
1879 | continue; | |
1880 | } | |
1881 | /* See if there is some other filepair that | |
1882 | * copies from the same source as us. If so | |
1883 | * we are a copy. Otherwise we are either a | |
1884 | * copy if the path stays, or a rename if it | |
1885 | * does not, but we already handled "stays" case. | |
1886 | */ | |
1887 | for (j = i + 1; j < q->nr; j++) { | |
1888 | pp = q->queue[j]; | |
1889 | if (strcmp(pp->one->path, p->one->path)) | |
1890 | continue; /* not us */ | |
1891 | if (!DIFF_PAIR_RENAME(pp)) | |
1892 | continue; /* not a rename/copy */ | |
1893 | /* pp is a rename/copy from the same source */ | |
1894 | p->status = DIFF_STATUS_COPIED; | |
1895 | break; | |
1896 | } | |
1897 | if (!p->status) | |
1898 | p->status = DIFF_STATUS_RENAMED; | |
1899 | } | |
1900 | else if (memcmp(p->one->sha1, p->two->sha1, 20) || | |
1901 | p->one->mode != p->two->mode) | |
1902 | p->status = DIFF_STATUS_MODIFIED; | |
1903 | else { | |
1904 | /* This is a "no-change" entry and should not | |
1905 | * happen anymore, but prepare for broken callers. | |
1906 | */ | |
1907 | error("feeding unmodified %s to diffcore", | |
1908 | p->one->path); | |
1909 | p->status = DIFF_STATUS_UNKNOWN; | |
1910 | } | |
1911 | } | |
1912 | diff_debug_queue("resolve-rename-copy done", q); | |
1913 | } | |
1914 | ||
1915 | static void flush_one_pair(struct diff_filepair *p, | |
1916 | int diff_output_format, | |
1917 | struct diff_options *options, | |
1918 | struct diffstat_t *diffstat) | |
1919 | { | |
1920 | int inter_name_termination = '\t'; | |
1921 | int line_termination = options->line_termination; | |
1922 | if (!line_termination) | |
1923 | inter_name_termination = 0; | |
1924 | ||
1925 | switch (p->status) { | |
1926 | case DIFF_STATUS_UNKNOWN: | |
1927 | break; | |
1928 | case 0: | |
1929 | die("internal error in diff-resolve-rename-copy"); | |
1930 | break; | |
1931 | default: | |
1932 | switch (diff_output_format) { | |
1933 | case DIFF_FORMAT_DIFFSTAT: | |
1934 | diff_flush_stat(p, options, diffstat); | |
1935 | break; | |
88246898 JS |
1936 | case DIFF_FORMAT_CHECKDIFF: |
1937 | diff_flush_checkdiff(p, options); | |
1938 | break; | |
6973dcae JH |
1939 | case DIFF_FORMAT_PATCH: |
1940 | diff_flush_patch(p, options); | |
1941 | break; | |
1942 | case DIFF_FORMAT_RAW: | |
1943 | case DIFF_FORMAT_NAME_STATUS: | |
1944 | diff_flush_raw(p, line_termination, | |
1945 | inter_name_termination, | |
1946 | options, diff_output_format); | |
1947 | break; | |
1948 | case DIFF_FORMAT_NAME: | |
d2543b8e | 1949 | diff_flush_name(p, line_termination); |
6973dcae JH |
1950 | break; |
1951 | case DIFF_FORMAT_NO_OUTPUT: | |
1952 | break; | |
1953 | } | |
1954 | } | |
1955 | } | |
1956 | ||
4bbd261b SE |
1957 | static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs) |
1958 | { | |
1959 | if (fs->mode) | |
1960 | printf(" %s mode %06o %s\n", newdelete, fs->mode, fs->path); | |
1961 | else | |
1962 | printf(" %s %s\n", newdelete, fs->path); | |
1963 | } | |
1964 | ||
1965 | ||
1966 | static void show_mode_change(struct diff_filepair *p, int show_name) | |
1967 | { | |
1968 | if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) { | |
1969 | if (show_name) | |
1970 | printf(" mode change %06o => %06o %s\n", | |
1971 | p->one->mode, p->two->mode, p->two->path); | |
1972 | else | |
1973 | printf(" mode change %06o => %06o\n", | |
1974 | p->one->mode, p->two->mode); | |
1975 | } | |
1976 | } | |
1977 | ||
1978 | static void show_rename_copy(const char *renamecopy, struct diff_filepair *p) | |
1979 | { | |
1980 | const char *old, *new; | |
1981 | ||
1982 | /* Find common prefix */ | |
1983 | old = p->one->path; | |
1984 | new = p->two->path; | |
1985 | while (1) { | |
1986 | const char *slash_old, *slash_new; | |
1987 | slash_old = strchr(old, '/'); | |
1988 | slash_new = strchr(new, '/'); | |
1989 | if (!slash_old || | |
1990 | !slash_new || | |
1991 | slash_old - old != slash_new - new || | |
1992 | memcmp(old, new, slash_new - new)) | |
1993 | break; | |
1994 | old = slash_old + 1; | |
1995 | new = slash_new + 1; | |
1996 | } | |
1997 | /* p->one->path thru old is the common prefix, and old and new | |
1998 | * through the end of names are renames | |
1999 | */ | |
2000 | if (old != p->one->path) | |
2001 | printf(" %s %.*s{%s => %s} (%d%%)\n", renamecopy, | |
2002 | (int)(old - p->one->path), p->one->path, | |
2003 | old, new, (int)(0.5 + p->score * 100.0/MAX_SCORE)); | |
2004 | else | |
2005 | printf(" %s %s => %s (%d%%)\n", renamecopy, | |
2006 | p->one->path, p->two->path, | |
2007 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); | |
2008 | show_mode_change(p, 0); | |
2009 | } | |
2010 | ||
2011 | static void diff_summary(struct diff_filepair *p) | |
2012 | { | |
2013 | switch(p->status) { | |
2014 | case DIFF_STATUS_DELETED: | |
2015 | show_file_mode_name("delete", p->one); | |
2016 | break; | |
2017 | case DIFF_STATUS_ADDED: | |
2018 | show_file_mode_name("create", p->two); | |
2019 | break; | |
2020 | case DIFF_STATUS_COPIED: | |
2021 | show_rename_copy("copy", p); | |
2022 | break; | |
2023 | case DIFF_STATUS_RENAMED: | |
2024 | show_rename_copy("rename", p); | |
2025 | break; | |
2026 | default: | |
2027 | if (p->score) { | |
2028 | printf(" rewrite %s (%d%%)\n", p->two->path, | |
2029 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); | |
2030 | show_mode_change(p, 0); | |
2031 | } else show_mode_change(p, 1); | |
2032 | break; | |
2033 | } | |
2034 | } | |
2035 | ||
6973dcae JH |
2036 | void diff_flush(struct diff_options *options) |
2037 | { | |
2038 | struct diff_queue_struct *q = &diff_queued_diff; | |
2039 | int i; | |
2040 | int diff_output_format = options->output_format; | |
2041 | struct diffstat_t *diffstat = NULL; | |
2042 | ||
2043 | if (diff_output_format == DIFF_FORMAT_DIFFSTAT || options->with_stat) { | |
2044 | diffstat = xcalloc(sizeof (struct diffstat_t), 1); | |
2045 | diffstat->xm.consume = diffstat_consume; | |
2046 | } | |
2047 | ||
2048 | if (options->with_raw) { | |
2049 | for (i = 0; i < q->nr; i++) { | |
2050 | struct diff_filepair *p = q->queue[i]; | |
2051 | flush_one_pair(p, DIFF_FORMAT_RAW, options, NULL); | |
2052 | } | |
2053 | putchar(options->line_termination); | |
2054 | } | |
2055 | if (options->with_stat) { | |
2056 | for (i = 0; i < q->nr; i++) { | |
2057 | struct diff_filepair *p = q->queue[i]; | |
2058 | flush_one_pair(p, DIFF_FORMAT_DIFFSTAT, options, | |
2059 | diffstat); | |
2060 | } | |
2061 | show_stats(diffstat); | |
2062 | free(diffstat); | |
2063 | diffstat = NULL; | |
fad70686 SE |
2064 | if (options->summary) |
2065 | for (i = 0; i < q->nr; i++) | |
2066 | diff_summary(q->queue[i]); | |
698ce6f8 JS |
2067 | if (options->stat_sep) |
2068 | fputs(options->stat_sep, stdout); | |
2069 | else | |
2070 | putchar(options->line_termination); | |
6973dcae JH |
2071 | } |
2072 | for (i = 0; i < q->nr; i++) { | |
2073 | struct diff_filepair *p = q->queue[i]; | |
2074 | flush_one_pair(p, diff_output_format, options, diffstat); | |
6973dcae JH |
2075 | } |
2076 | ||
2077 | if (diffstat) { | |
2078 | show_stats(diffstat); | |
2079 | free(diffstat); | |
2080 | } | |
2081 | ||
4bbd261b | 2082 | for (i = 0; i < q->nr; i++) { |
fad70686 | 2083 | if (diffstat && options->summary) |
4bbd261b SE |
2084 | diff_summary(q->queue[i]); |
2085 | diff_free_filepair(q->queue[i]); | |
2086 | } | |
2087 | ||
6973dcae JH |
2088 | free(q->queue); |
2089 | q->queue = NULL; | |
2090 | q->nr = q->alloc = 0; | |
2091 | } | |
2092 | ||
2093 | static void diffcore_apply_filter(const char *filter) | |
2094 | { | |
2095 | int i; | |
2096 | struct diff_queue_struct *q = &diff_queued_diff; | |
2097 | struct diff_queue_struct outq; | |
2098 | outq.queue = NULL; | |
2099 | outq.nr = outq.alloc = 0; | |
2100 | ||
2101 | if (!filter) | |
2102 | return; | |
2103 | ||
2104 | if (strchr(filter, DIFF_STATUS_FILTER_AON)) { | |
2105 | int found; | |
2106 | for (i = found = 0; !found && i < q->nr; i++) { | |
2107 | struct diff_filepair *p = q->queue[i]; | |
2108 | if (((p->status == DIFF_STATUS_MODIFIED) && | |
2109 | ((p->score && | |
2110 | strchr(filter, DIFF_STATUS_FILTER_BROKEN)) || | |
2111 | (!p->score && | |
2112 | strchr(filter, DIFF_STATUS_MODIFIED)))) || | |
2113 | ((p->status != DIFF_STATUS_MODIFIED) && | |
2114 | strchr(filter, p->status))) | |
2115 | found++; | |
2116 | } | |
2117 | if (found) | |
2118 | return; | |
2119 | ||
2120 | /* otherwise we will clear the whole queue | |
2121 | * by copying the empty outq at the end of this | |
2122 | * function, but first clear the current entries | |
2123 | * in the queue. | |
2124 | */ | |
2125 | for (i = 0; i < q->nr; i++) | |
2126 | diff_free_filepair(q->queue[i]); | |
2127 | } | |
2128 | else { | |
2129 | /* Only the matching ones */ | |
2130 | for (i = 0; i < q->nr; i++) { | |
2131 | struct diff_filepair *p = q->queue[i]; | |
2132 | ||
2133 | if (((p->status == DIFF_STATUS_MODIFIED) && | |
2134 | ((p->score && | |
2135 | strchr(filter, DIFF_STATUS_FILTER_BROKEN)) || | |
2136 | (!p->score && | |
2137 | strchr(filter, DIFF_STATUS_MODIFIED)))) || | |
2138 | ((p->status != DIFF_STATUS_MODIFIED) && | |
2139 | strchr(filter, p->status))) | |
2140 | diff_q(&outq, p); | |
2141 | else | |
2142 | diff_free_filepair(p); | |
2143 | } | |
2144 | } | |
2145 | free(q->queue); | |
2146 | *q = outq; | |
2147 | } | |
2148 | ||
2149 | void diffcore_std(struct diff_options *options) | |
2150 | { | |
2151 | if (options->break_opt != -1) | |
2152 | diffcore_break(options->break_opt); | |
2153 | if (options->detect_rename) | |
2154 | diffcore_rename(options); | |
2155 | if (options->break_opt != -1) | |
2156 | diffcore_merge_broken(); | |
2157 | if (options->pickaxe) | |
2158 | diffcore_pickaxe(options->pickaxe, options->pickaxe_opts); | |
2159 | if (options->orderfile) | |
2160 | diffcore_order(options->orderfile); | |
2161 | diff_resolve_rename_copy(); | |
2162 | diffcore_apply_filter(options->filter); | |
2163 | } | |
2164 | ||
2165 | ||
2166 | void diffcore_std_no_resolve(struct diff_options *options) | |
2167 | { | |
2168 | if (options->pickaxe) | |
2169 | diffcore_pickaxe(options->pickaxe, options->pickaxe_opts); | |
2170 | if (options->orderfile) | |
2171 | diffcore_order(options->orderfile); | |
2172 | diffcore_apply_filter(options->filter); | |
2173 | } | |
2174 | ||
2175 | void diff_addremove(struct diff_options *options, | |
2176 | int addremove, unsigned mode, | |
2177 | const unsigned char *sha1, | |
2178 | const char *base, const char *path) | |
2179 | { | |
2180 | char concatpath[PATH_MAX]; | |
2181 | struct diff_filespec *one, *two; | |
2182 | ||
2183 | /* This may look odd, but it is a preparation for | |
2184 | * feeding "there are unchanged files which should | |
2185 | * not produce diffs, but when you are doing copy | |
2186 | * detection you would need them, so here they are" | |
2187 | * entries to the diff-core. They will be prefixed | |
2188 | * with something like '=' or '*' (I haven't decided | |
2189 | * which but should not make any difference). | |
2190 | * Feeding the same new and old to diff_change() | |
2191 | * also has the same effect. | |
2192 | * Before the final output happens, they are pruned after | |
2193 | * merged into rename/copy pairs as appropriate. | |
2194 | */ | |
2195 | if (options->reverse_diff) | |
2196 | addremove = (addremove == '+' ? '-' : | |
2197 | addremove == '-' ? '+' : addremove); | |
2198 | ||
2199 | if (!path) path = ""; | |
2200 | sprintf(concatpath, "%s%s", base, path); | |
2201 | one = alloc_filespec(concatpath); | |
2202 | two = alloc_filespec(concatpath); | |
2203 | ||
2204 | if (addremove != '+') | |
2205 | fill_filespec(one, sha1, mode); | |
2206 | if (addremove != '-') | |
2207 | fill_filespec(two, sha1, mode); | |
2208 | ||
2209 | diff_queue(&diff_queued_diff, one, two); | |
2210 | } | |
2211 | ||
2212 | void diff_change(struct diff_options *options, | |
2213 | unsigned old_mode, unsigned new_mode, | |
2214 | const unsigned char *old_sha1, | |
2215 | const unsigned char *new_sha1, | |
2216 | const char *base, const char *path) | |
2217 | { | |
2218 | char concatpath[PATH_MAX]; | |
2219 | struct diff_filespec *one, *two; | |
2220 | ||
2221 | if (options->reverse_diff) { | |
2222 | unsigned tmp; | |
2223 | const unsigned char *tmp_c; | |
2224 | tmp = old_mode; old_mode = new_mode; new_mode = tmp; | |
2225 | tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c; | |
2226 | } | |
2227 | if (!path) path = ""; | |
2228 | sprintf(concatpath, "%s%s", base, path); | |
2229 | one = alloc_filespec(concatpath); | |
2230 | two = alloc_filespec(concatpath); | |
2231 | fill_filespec(one, old_sha1, old_mode); | |
2232 | fill_filespec(two, new_sha1, new_mode); | |
2233 | ||
2234 | diff_queue(&diff_queued_diff, one, two); | |
2235 | } | |
2236 | ||
2237 | void diff_unmerge(struct diff_options *options, | |
2238 | const char *path) | |
2239 | { | |
2240 | struct diff_filespec *one, *two; | |
2241 | one = alloc_filespec(path); | |
2242 | two = alloc_filespec(path); | |
2243 | diff_queue(&diff_queued_diff, one, two); | |
2244 | } |