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