]>
Commit | Line | Data |
---|---|---|
6973dcae JH |
1 | /* |
2 | * Copyright (C) 2005 Junio C Hamano | |
3 | */ | |
6973dcae JH |
4 | #include "cache.h" |
5 | #include "quote.h" | |
6 | #include "diff.h" | |
7 | #include "diffcore.h" | |
051308f6 | 8 | #include "delta.h" |
6973dcae | 9 | #include "xdiff-interface.h" |
7c92fe0e | 10 | #include "color.h" |
8c701249 | 11 | #include "attr.h" |
6973dcae | 12 | |
1510fea7 SP |
13 | #ifdef NO_FAST_WORKING_DIRECTORY |
14 | #define FAST_WORKING_DIRECTORY 0 | |
15 | #else | |
16 | #define FAST_WORKING_DIRECTORY 1 | |
17 | #endif | |
18 | ||
96f1e58f | 19 | static int diff_detect_rename_default; |
801235c5 | 20 | static int diff_rename_limit_default = -1; |
96f1e58f | 21 | static int diff_use_color_default; |
6973dcae | 22 | |
7c92fe0e | 23 | static char diff_colors[][COLOR_MAXLEN] = { |
f37399e6 | 24 | "\033[m", /* reset */ |
448c3ef1 JH |
25 | "", /* PLAIN (normal) */ |
26 | "\033[1m", /* METAINFO (bold) */ | |
27 | "\033[36m", /* FRAGINFO (cyan) */ | |
28 | "\033[31m", /* OLD (red) */ | |
29 | "\033[32m", /* NEW (green) */ | |
30 | "\033[33m", /* COMMIT (yellow) */ | |
31 | "\033[41m", /* WHITESPACE (red background) */ | |
cd112cef JS |
32 | }; |
33 | ||
801235c5 JH |
34 | static int parse_diff_color_slot(const char *var, int ofs) |
35 | { | |
36 | if (!strcasecmp(var+ofs, "plain")) | |
37 | return DIFF_PLAIN; | |
38 | if (!strcasecmp(var+ofs, "meta")) | |
39 | return DIFF_METAINFO; | |
40 | if (!strcasecmp(var+ofs, "frag")) | |
41 | return DIFF_FRAGINFO; | |
42 | if (!strcasecmp(var+ofs, "old")) | |
43 | return DIFF_FILE_OLD; | |
44 | if (!strcasecmp(var+ofs, "new")) | |
45 | return DIFF_FILE_NEW; | |
ce436973 JK |
46 | if (!strcasecmp(var+ofs, "commit")) |
47 | return DIFF_COMMIT; | |
448c3ef1 JH |
48 | if (!strcasecmp(var+ofs, "whitespace")) |
49 | return DIFF_WHITESPACE; | |
801235c5 JH |
50 | die("bad config variable '%s'", var); |
51 | } | |
52 | ||
f1af60bd JH |
53 | static struct ll_diff_driver { |
54 | const char *name; | |
55 | struct ll_diff_driver *next; | |
56 | char *cmd; | |
57 | } *user_diff, **user_diff_tail; | |
58 | ||
59 | /* | |
60 | * Currently there is only "diff.<drivername>.command" variable; | |
61 | * because there are "diff.color.<slot>" variables, we are parsing | |
62 | * this in a bit convoluted way to allow low level diff driver | |
63 | * called "color". | |
64 | */ | |
65 | static int parse_lldiff_command(const char *var, const char *ep, const char *value) | |
66 | { | |
67 | const char *name; | |
68 | int namelen; | |
69 | struct ll_diff_driver *drv; | |
70 | ||
71 | name = var + 5; | |
72 | namelen = ep - name; | |
73 | for (drv = user_diff; drv; drv = drv->next) | |
74 | if (!strncmp(drv->name, name, namelen) && !drv->name[namelen]) | |
75 | break; | |
76 | if (!drv) { | |
77 | char *namebuf; | |
78 | drv = xcalloc(1, sizeof(struct ll_diff_driver)); | |
79 | namebuf = xmalloc(namelen + 1); | |
80 | memcpy(namebuf, name, namelen); | |
81 | namebuf[namelen] = 0; | |
82 | drv->name = namebuf; | |
83 | drv->next = NULL; | |
84 | if (!user_diff_tail) | |
85 | user_diff_tail = &user_diff; | |
86 | *user_diff_tail = drv; | |
87 | user_diff_tail = &(drv->next); | |
88 | } | |
89 | ||
90 | if (!value) | |
91 | return error("%s: lacks value", var); | |
92 | drv->cmd = strdup(value); | |
93 | return 0; | |
94 | } | |
95 | ||
83ad63cf JH |
96 | /* |
97 | * These are to give UI layer defaults. | |
98 | * The core-level commands such as git-diff-files should | |
99 | * never be affected by the setting of diff.renames | |
100 | * the user happens to have in the configuration file. | |
101 | */ | |
102 | int git_diff_ui_config(const char *var, const char *value) | |
801235c5 JH |
103 | { |
104 | if (!strcmp(var, "diff.renamelimit")) { | |
105 | diff_rename_limit_default = git_config_int(var, value); | |
106 | return 0; | |
107 | } | |
a159ca0c | 108 | if (!strcmp(var, "diff.color") || !strcmp(var, "color.diff")) { |
7c92fe0e | 109 | diff_use_color_default = git_config_colorbool(var, value); |
801235c5 JH |
110 | return 0; |
111 | } | |
b68ea12e EW |
112 | if (!strcmp(var, "diff.renames")) { |
113 | if (!value) | |
114 | diff_detect_rename_default = DIFF_DETECT_RENAME; | |
115 | else if (!strcasecmp(value, "copies") || | |
116 | !strcasecmp(value, "copy")) | |
117 | diff_detect_rename_default = DIFF_DETECT_COPY; | |
118 | else if (git_config_bool(var,value)) | |
119 | diff_detect_rename_default = DIFF_DETECT_RENAME; | |
120 | return 0; | |
121 | } | |
f1af60bd JH |
122 | if (!prefixcmp(var, "diff.")) { |
123 | const char *ep = strrchr(var, '.'); | |
124 | ||
125 | if (ep != var + 4 && !strcmp(ep, ".command")) | |
126 | return parse_lldiff_command(var, ep, value); | |
127 | } | |
1968d77d | 128 | if (!prefixcmp(var, "diff.color.") || !prefixcmp(var, "color.diff.")) { |
801235c5 | 129 | int slot = parse_diff_color_slot(var, 11); |
7c92fe0e | 130 | color_parse(value, var, diff_colors[slot]); |
801235c5 JH |
131 | return 0; |
132 | } | |
f1af60bd | 133 | |
801235c5 JH |
134 | return git_default_config(var, value); |
135 | } | |
136 | ||
6973dcae JH |
137 | static char *quote_one(const char *str) |
138 | { | |
139 | int needlen; | |
140 | char *xp; | |
141 | ||
142 | if (!str) | |
143 | return NULL; | |
144 | needlen = quote_c_style(str, NULL, NULL, 0); | |
145 | if (!needlen) | |
9befac47 | 146 | return xstrdup(str); |
6973dcae JH |
147 | xp = xmalloc(needlen + 1); |
148 | quote_c_style(str, xp, NULL, 0); | |
149 | return xp; | |
150 | } | |
151 | ||
152 | static char *quote_two(const char *one, const char *two) | |
153 | { | |
154 | int need_one = quote_c_style(one, NULL, NULL, 1); | |
155 | int need_two = quote_c_style(two, NULL, NULL, 1); | |
156 | char *xp; | |
157 | ||
158 | if (need_one + need_two) { | |
159 | if (!need_one) need_one = strlen(one); | |
160 | if (!need_two) need_one = strlen(two); | |
161 | ||
162 | xp = xmalloc(need_one + need_two + 3); | |
163 | xp[0] = '"'; | |
164 | quote_c_style(one, xp + 1, NULL, 1); | |
165 | quote_c_style(two, xp + need_one + 1, NULL, 1); | |
166 | strcpy(xp + need_one + need_two + 1, "\""); | |
167 | return xp; | |
168 | } | |
169 | need_one = strlen(one); | |
170 | need_two = strlen(two); | |
171 | xp = xmalloc(need_one + need_two + 1); | |
172 | strcpy(xp, one); | |
173 | strcpy(xp + need_one, two); | |
174 | return xp; | |
175 | } | |
176 | ||
177 | static const char *external_diff(void) | |
178 | { | |
179 | static const char *external_diff_cmd = NULL; | |
180 | static int done_preparing = 0; | |
181 | ||
182 | if (done_preparing) | |
183 | return external_diff_cmd; | |
184 | external_diff_cmd = getenv("GIT_EXTERNAL_DIFF"); | |
185 | done_preparing = 1; | |
186 | return external_diff_cmd; | |
187 | } | |
188 | ||
6973dcae JH |
189 | static struct diff_tempfile { |
190 | const char *name; /* filename external diff should read from */ | |
191 | char hex[41]; | |
192 | char mode[10]; | |
1472966c | 193 | char tmp_path[PATH_MAX]; |
6973dcae JH |
194 | } diff_temp[2]; |
195 | ||
196 | static int count_lines(const char *data, int size) | |
197 | { | |
198 | int count, ch, completely_empty = 1, nl_just_seen = 0; | |
199 | count = 0; | |
200 | while (0 < size--) { | |
201 | ch = *data++; | |
202 | if (ch == '\n') { | |
203 | count++; | |
204 | nl_just_seen = 1; | |
205 | completely_empty = 0; | |
206 | } | |
207 | else { | |
208 | nl_just_seen = 0; | |
209 | completely_empty = 0; | |
210 | } | |
211 | } | |
212 | if (completely_empty) | |
213 | return 0; | |
214 | if (!nl_just_seen) | |
215 | count++; /* no trailing newline */ | |
216 | return count; | |
217 | } | |
218 | ||
219 | static void print_line_count(int count) | |
220 | { | |
221 | switch (count) { | |
222 | case 0: | |
223 | printf("0,0"); | |
224 | break; | |
225 | case 1: | |
226 | printf("1"); | |
227 | break; | |
228 | default: | |
229 | printf("1,%d", count); | |
230 | break; | |
231 | } | |
232 | } | |
233 | ||
13e36ec5 JS |
234 | static void copy_file(int prefix, const char *data, int size, |
235 | const char *set, const char *reset) | |
6973dcae JH |
236 | { |
237 | int ch, nl_just_seen = 1; | |
238 | while (0 < size--) { | |
239 | ch = *data++; | |
13e36ec5 JS |
240 | if (nl_just_seen) { |
241 | fputs(set, stdout); | |
6973dcae | 242 | putchar(prefix); |
13e36ec5 JS |
243 | } |
244 | if (ch == '\n') { | |
6973dcae | 245 | nl_just_seen = 1; |
13e36ec5 JS |
246 | fputs(reset, stdout); |
247 | } else | |
6973dcae | 248 | nl_just_seen = 0; |
13e36ec5 | 249 | putchar(ch); |
6973dcae JH |
250 | } |
251 | if (!nl_just_seen) | |
13e36ec5 | 252 | printf("%s\n\\ No newline at end of file\n", reset); |
6973dcae JH |
253 | } |
254 | ||
255 | static void emit_rewrite_diff(const char *name_a, | |
256 | const char *name_b, | |
257 | struct diff_filespec *one, | |
13e36ec5 JS |
258 | struct diff_filespec *two, |
259 | int color_diff) | |
6973dcae JH |
260 | { |
261 | int lc_a, lc_b; | |
1a9eb3b9 | 262 | const char *name_a_tab, *name_b_tab; |
13e36ec5 JS |
263 | const char *metainfo = diff_get_color(color_diff, DIFF_METAINFO); |
264 | const char *fraginfo = diff_get_color(color_diff, DIFF_FRAGINFO); | |
265 | const char *old = diff_get_color(color_diff, DIFF_FILE_OLD); | |
266 | const char *new = diff_get_color(color_diff, DIFF_FILE_NEW); | |
267 | const char *reset = diff_get_color(color_diff, DIFF_RESET); | |
1a9eb3b9 | 268 | |
8a13becc JH |
269 | name_a += (*name_a == '/'); |
270 | name_b += (*name_b == '/'); | |
1a9eb3b9 JH |
271 | name_a_tab = strchr(name_a, ' ') ? "\t" : ""; |
272 | name_b_tab = strchr(name_b, ' ') ? "\t" : ""; | |
273 | ||
6973dcae JH |
274 | diff_populate_filespec(one, 0); |
275 | diff_populate_filespec(two, 0); | |
276 | lc_a = count_lines(one->data, one->size); | |
277 | lc_b = count_lines(two->data, two->size); | |
13e36ec5 JS |
278 | printf("%s--- a/%s%s%s\n%s+++ b/%s%s%s\n%s@@ -", |
279 | metainfo, name_a, name_a_tab, reset, | |
280 | metainfo, name_b, name_b_tab, reset, fraginfo); | |
6973dcae JH |
281 | print_line_count(lc_a); |
282 | printf(" +"); | |
283 | print_line_count(lc_b); | |
13e36ec5 | 284 | printf(" @@%s\n", reset); |
6973dcae | 285 | if (lc_a) |
13e36ec5 | 286 | copy_file('-', one->data, one->size, old, reset); |
6973dcae | 287 | if (lc_b) |
13e36ec5 | 288 | copy_file('+', two->data, two->size, new, reset); |
6973dcae JH |
289 | } |
290 | ||
291 | static int fill_mmfile(mmfile_t *mf, struct diff_filespec *one) | |
292 | { | |
293 | if (!DIFF_FILE_VALID(one)) { | |
d2543b8e | 294 | mf->ptr = (char *)""; /* does not matter */ |
6973dcae JH |
295 | mf->size = 0; |
296 | return 0; | |
297 | } | |
298 | else if (diff_populate_filespec(one, 0)) | |
299 | return -1; | |
300 | mf->ptr = one->data; | |
301 | mf->size = one->size; | |
302 | return 0; | |
303 | } | |
304 | ||
f59a59e2 JS |
305 | struct diff_words_buffer { |
306 | mmfile_t text; | |
307 | long alloc; | |
308 | long current; /* output pointer */ | |
309 | int suppressed_newline; | |
310 | }; | |
311 | ||
312 | static void diff_words_append(char *line, unsigned long len, | |
313 | struct diff_words_buffer *buffer) | |
314 | { | |
315 | if (buffer->text.size + len > buffer->alloc) { | |
316 | buffer->alloc = (buffer->text.size + len) * 3 / 2; | |
317 | buffer->text.ptr = xrealloc(buffer->text.ptr, buffer->alloc); | |
318 | } | |
319 | line++; | |
320 | len--; | |
321 | memcpy(buffer->text.ptr + buffer->text.size, line, len); | |
322 | buffer->text.size += len; | |
323 | } | |
324 | ||
325 | struct diff_words_data { | |
326 | struct xdiff_emit_state xm; | |
327 | struct diff_words_buffer minus, plus; | |
328 | }; | |
329 | ||
330 | static void print_word(struct diff_words_buffer *buffer, int len, int color, | |
331 | int suppress_newline) | |
332 | { | |
333 | const char *ptr; | |
334 | int eol = 0; | |
335 | ||
336 | if (len == 0) | |
337 | return; | |
338 | ||
339 | ptr = buffer->text.ptr + buffer->current; | |
340 | buffer->current += len; | |
341 | ||
342 | if (ptr[len - 1] == '\n') { | |
343 | eol = 1; | |
344 | len--; | |
345 | } | |
346 | ||
347 | fputs(diff_get_color(1, color), stdout); | |
348 | fwrite(ptr, len, 1, stdout); | |
349 | fputs(diff_get_color(1, DIFF_RESET), stdout); | |
350 | ||
351 | if (eol) { | |
352 | if (suppress_newline) | |
353 | buffer->suppressed_newline = 1; | |
354 | else | |
355 | putchar('\n'); | |
356 | } | |
357 | } | |
358 | ||
359 | static void fn_out_diff_words_aux(void *priv, char *line, unsigned long len) | |
360 | { | |
361 | struct diff_words_data *diff_words = priv; | |
362 | ||
363 | if (diff_words->minus.suppressed_newline) { | |
364 | if (line[0] != '+') | |
365 | putchar('\n'); | |
366 | diff_words->minus.suppressed_newline = 0; | |
367 | } | |
368 | ||
369 | len--; | |
370 | switch (line[0]) { | |
371 | case '-': | |
372 | print_word(&diff_words->minus, len, DIFF_FILE_OLD, 1); | |
373 | break; | |
374 | case '+': | |
375 | print_word(&diff_words->plus, len, DIFF_FILE_NEW, 0); | |
376 | break; | |
377 | case ' ': | |
378 | print_word(&diff_words->plus, len, DIFF_PLAIN, 0); | |
379 | diff_words->minus.current += len; | |
380 | break; | |
381 | } | |
382 | } | |
383 | ||
384 | /* this executes the word diff on the accumulated buffers */ | |
385 | static void diff_words_show(struct diff_words_data *diff_words) | |
386 | { | |
387 | xpparam_t xpp; | |
388 | xdemitconf_t xecfg; | |
389 | xdemitcb_t ecb; | |
390 | mmfile_t minus, plus; | |
391 | int i; | |
392 | ||
393 | minus.size = diff_words->minus.text.size; | |
394 | minus.ptr = xmalloc(minus.size); | |
395 | memcpy(minus.ptr, diff_words->minus.text.ptr, minus.size); | |
396 | for (i = 0; i < minus.size; i++) | |
397 | if (isspace(minus.ptr[i])) | |
398 | minus.ptr[i] = '\n'; | |
399 | diff_words->minus.current = 0; | |
400 | ||
401 | plus.size = diff_words->plus.text.size; | |
402 | plus.ptr = xmalloc(plus.size); | |
403 | memcpy(plus.ptr, diff_words->plus.text.ptr, plus.size); | |
404 | for (i = 0; i < plus.size; i++) | |
405 | if (isspace(plus.ptr[i])) | |
406 | plus.ptr[i] = '\n'; | |
407 | diff_words->plus.current = 0; | |
408 | ||
409 | xpp.flags = XDF_NEED_MINIMAL; | |
410 | xecfg.ctxlen = diff_words->minus.alloc + diff_words->plus.alloc; | |
411 | xecfg.flags = 0; | |
412 | ecb.outf = xdiff_outf; | |
413 | ecb.priv = diff_words; | |
414 | diff_words->xm.consume = fn_out_diff_words_aux; | |
415 | xdl_diff(&minus, &plus, &xpp, &xecfg, &ecb); | |
416 | ||
417 | free(minus.ptr); | |
418 | free(plus.ptr); | |
419 | diff_words->minus.text.size = diff_words->plus.text.size = 0; | |
420 | ||
421 | if (diff_words->minus.suppressed_newline) { | |
422 | putchar('\n'); | |
423 | diff_words->minus.suppressed_newline = 0; | |
424 | } | |
425 | } | |
426 | ||
6973dcae | 427 | struct emit_callback { |
cd112cef JS |
428 | struct xdiff_emit_state xm; |
429 | int nparents, color_diff; | |
6973dcae | 430 | const char **label_path; |
f59a59e2 | 431 | struct diff_words_data *diff_words; |
34a5e1a2 | 432 | int *found_changesp; |
6973dcae JH |
433 | }; |
434 | ||
f59a59e2 JS |
435 | static void free_diff_words_data(struct emit_callback *ecbdata) |
436 | { | |
437 | if (ecbdata->diff_words) { | |
438 | /* flush buffers */ | |
439 | if (ecbdata->diff_words->minus.text.size || | |
440 | ecbdata->diff_words->plus.text.size) | |
441 | diff_words_show(ecbdata->diff_words); | |
442 | ||
443 | if (ecbdata->diff_words->minus.text.ptr) | |
444 | free (ecbdata->diff_words->minus.text.ptr); | |
445 | if (ecbdata->diff_words->plus.text.ptr) | |
446 | free (ecbdata->diff_words->plus.text.ptr); | |
447 | free(ecbdata->diff_words); | |
448 | ecbdata->diff_words = NULL; | |
449 | } | |
450 | } | |
451 | ||
ce436973 | 452 | const char *diff_get_color(int diff_use_color, enum color_diff ix) |
cd112cef JS |
453 | { |
454 | if (diff_use_color) | |
50f575fc LT |
455 | return diff_colors[ix]; |
456 | return ""; | |
cd112cef JS |
457 | } |
458 | ||
448c3ef1 JH |
459 | static void emit_line(const char *set, const char *reset, const char *line, int len) |
460 | { | |
461 | if (len > 0 && line[len-1] == '\n') | |
462 | len--; | |
463 | fputs(set, stdout); | |
464 | fwrite(line, len, 1, stdout); | |
465 | puts(reset); | |
466 | } | |
467 | ||
c5a8c3ec JS |
468 | static void emit_line_with_ws(int nparents, |
469 | const char *set, const char *reset, const char *ws, | |
470 | const char *line, int len) | |
448c3ef1 | 471 | { |
c5a8c3ec | 472 | int col0 = nparents; |
448c3ef1 JH |
473 | int last_tab_in_indent = -1; |
474 | int last_space_in_indent = -1; | |
475 | int i; | |
476 | int tail = len; | |
477 | int need_highlight_leading_space = 0; | |
448c3ef1 JH |
478 | /* The line is a newly added line. Does it have funny leading |
479 | * whitespaces? In indent, SP should never precede a TAB. | |
480 | */ | |
481 | for (i = col0; i < len; i++) { | |
482 | if (line[i] == '\t') { | |
483 | last_tab_in_indent = i; | |
484 | if (0 <= last_space_in_indent) | |
485 | need_highlight_leading_space = 1; | |
486 | } | |
487 | else if (line[i] == ' ') | |
488 | last_space_in_indent = i; | |
489 | else | |
490 | break; | |
491 | } | |
492 | fputs(set, stdout); | |
493 | fwrite(line, col0, 1, stdout); | |
494 | fputs(reset, stdout); | |
495 | if (((i == len) || line[i] == '\n') && i != col0) { | |
496 | /* The whole line was indent */ | |
497 | emit_line(ws, reset, line + col0, len - col0); | |
498 | return; | |
499 | } | |
500 | i = col0; | |
501 | if (need_highlight_leading_space) { | |
502 | while (i < last_tab_in_indent) { | |
503 | if (line[i] == ' ') { | |
504 | fputs(ws, stdout); | |
505 | putchar(' '); | |
506 | fputs(reset, stdout); | |
507 | } | |
508 | else | |
509 | putchar(line[i]); | |
510 | i++; | |
511 | } | |
512 | } | |
513 | tail = len - 1; | |
514 | if (line[tail] == '\n' && i < tail) | |
515 | tail--; | |
516 | while (i < tail) { | |
517 | if (!isspace(line[tail])) | |
518 | break; | |
519 | tail--; | |
520 | } | |
521 | if ((i < tail && line[tail + 1] != '\n')) { | |
522 | /* This has whitespace between tail+1..len */ | |
523 | fputs(set, stdout); | |
524 | fwrite(line + i, tail - i + 1, 1, stdout); | |
525 | fputs(reset, stdout); | |
526 | emit_line(ws, reset, line + tail + 1, len - tail - 1); | |
527 | } | |
528 | else | |
529 | emit_line(set, reset, line + i, len - i); | |
530 | } | |
531 | ||
c5a8c3ec JS |
532 | static void emit_add_line(const char *reset, struct emit_callback *ecbdata, const char *line, int len) |
533 | { | |
534 | const char *ws = diff_get_color(ecbdata->color_diff, DIFF_WHITESPACE); | |
535 | const char *set = diff_get_color(ecbdata->color_diff, DIFF_FILE_NEW); | |
536 | ||
537 | if (!*ws) | |
538 | emit_line(set, reset, line, len); | |
539 | else | |
540 | emit_line_with_ws(ecbdata->nparents, set, reset, ws, | |
541 | line, len); | |
542 | } | |
543 | ||
cd112cef | 544 | static void fn_out_consume(void *priv, char *line, unsigned long len) |
6973dcae JH |
545 | { |
546 | int i; | |
448c3ef1 | 547 | int color; |
6973dcae | 548 | struct emit_callback *ecbdata = priv; |
ce436973 JK |
549 | const char *set = diff_get_color(ecbdata->color_diff, DIFF_METAINFO); |
550 | const char *reset = diff_get_color(ecbdata->color_diff, DIFF_RESET); | |
6973dcae | 551 | |
34a5e1a2 JS |
552 | *(ecbdata->found_changesp) = 1; |
553 | ||
6973dcae | 554 | if (ecbdata->label_path[0]) { |
1a9eb3b9 JH |
555 | const char *name_a_tab, *name_b_tab; |
556 | ||
557 | name_a_tab = strchr(ecbdata->label_path[0], ' ') ? "\t" : ""; | |
558 | name_b_tab = strchr(ecbdata->label_path[1], ' ') ? "\t" : ""; | |
559 | ||
560 | printf("%s--- %s%s%s\n", | |
561 | set, ecbdata->label_path[0], reset, name_a_tab); | |
562 | printf("%s+++ %s%s%s\n", | |
563 | set, ecbdata->label_path[1], reset, name_b_tab); | |
6973dcae JH |
564 | ecbdata->label_path[0] = ecbdata->label_path[1] = NULL; |
565 | } | |
cd112cef JS |
566 | |
567 | /* This is not really necessary for now because | |
568 | * this codepath only deals with two-way diffs. | |
569 | */ | |
570 | for (i = 0; i < len && line[i] == '@'; i++) | |
571 | ; | |
572 | if (2 <= i && i < len && line[i] == ' ') { | |
573 | ecbdata->nparents = i - 1; | |
448c3ef1 JH |
574 | emit_line(diff_get_color(ecbdata->color_diff, DIFF_FRAGINFO), |
575 | reset, line, len); | |
576 | return; | |
cd112cef | 577 | } |
448c3ef1 JH |
578 | |
579 | if (len < ecbdata->nparents) { | |
50f575fc | 580 | set = reset; |
448c3ef1 JH |
581 | emit_line(reset, reset, line, len); |
582 | return; | |
cd112cef | 583 | } |
448c3ef1 JH |
584 | |
585 | color = DIFF_PLAIN; | |
586 | if (ecbdata->diff_words && ecbdata->nparents != 1) | |
587 | /* fall back to normal diff */ | |
588 | free_diff_words_data(ecbdata); | |
589 | if (ecbdata->diff_words) { | |
590 | if (line[0] == '-') { | |
591 | diff_words_append(line, len, | |
592 | &ecbdata->diff_words->minus); | |
593 | return; | |
594 | } else if (line[0] == '+') { | |
595 | diff_words_append(line, len, | |
596 | &ecbdata->diff_words->plus); | |
597 | return; | |
598 | } | |
599 | if (ecbdata->diff_words->minus.text.size || | |
600 | ecbdata->diff_words->plus.text.size) | |
601 | diff_words_show(ecbdata->diff_words); | |
602 | line++; | |
50f575fc | 603 | len--; |
448c3ef1 JH |
604 | emit_line(set, reset, line, len); |
605 | return; | |
606 | } | |
607 | for (i = 0; i < ecbdata->nparents && len; i++) { | |
608 | if (line[i] == '-') | |
609 | color = DIFF_FILE_OLD; | |
610 | else if (line[i] == '+') | |
611 | color = DIFF_FILE_NEW; | |
612 | } | |
613 | ||
614 | if (color != DIFF_FILE_NEW) { | |
615 | emit_line(diff_get_color(ecbdata->color_diff, color), | |
616 | reset, line, len); | |
617 | return; | |
618 | } | |
619 | emit_add_line(reset, ecbdata, line, len); | |
6973dcae JH |
620 | } |
621 | ||
622 | static char *pprint_rename(const char *a, const char *b) | |
623 | { | |
624 | const char *old = a; | |
625 | const char *new = b; | |
626 | char *name = NULL; | |
627 | int pfx_length, sfx_length; | |
628 | int len_a = strlen(a); | |
629 | int len_b = strlen(b); | |
e5bfbf9b AJ |
630 | int qlen_a = quote_c_style(a, NULL, NULL, 0); |
631 | int qlen_b = quote_c_style(b, NULL, NULL, 0); | |
632 | ||
633 | if (qlen_a || qlen_b) { | |
634 | if (qlen_a) len_a = qlen_a; | |
635 | if (qlen_b) len_b = qlen_b; | |
636 | name = xmalloc( len_a + len_b + 5 ); | |
637 | if (qlen_a) | |
638 | quote_c_style(a, name, NULL, 0); | |
639 | else | |
640 | memcpy(name, a, len_a); | |
641 | memcpy(name + len_a, " => ", 4); | |
642 | if (qlen_b) | |
643 | quote_c_style(b, name + len_a + 4, NULL, 0); | |
644 | else | |
645 | memcpy(name + len_a + 4, b, len_b + 1); | |
646 | return name; | |
647 | } | |
6973dcae JH |
648 | |
649 | /* Find common prefix */ | |
650 | pfx_length = 0; | |
651 | while (*old && *new && *old == *new) { | |
652 | if (*old == '/') | |
653 | pfx_length = old - a + 1; | |
654 | old++; | |
655 | new++; | |
656 | } | |
657 | ||
658 | /* Find common suffix */ | |
659 | old = a + len_a; | |
660 | new = b + len_b; | |
661 | sfx_length = 0; | |
662 | while (a <= old && b <= new && *old == *new) { | |
663 | if (*old == '/') | |
664 | sfx_length = len_a - (old - a); | |
665 | old--; | |
666 | new--; | |
667 | } | |
668 | ||
669 | /* | |
670 | * pfx{mid-a => mid-b}sfx | |
671 | * {pfx-a => pfx-b}sfx | |
672 | * pfx{sfx-a => sfx-b} | |
673 | * name-a => name-b | |
674 | */ | |
675 | if (pfx_length + sfx_length) { | |
cc908b82 JH |
676 | int a_midlen = len_a - pfx_length - sfx_length; |
677 | int b_midlen = len_b - pfx_length - sfx_length; | |
678 | if (a_midlen < 0) a_midlen = 0; | |
679 | if (b_midlen < 0) b_midlen = 0; | |
680 | ||
e3008464 | 681 | name = xmalloc(pfx_length + a_midlen + b_midlen + sfx_length + 7); |
6973dcae JH |
682 | sprintf(name, "%.*s{%.*s => %.*s}%s", |
683 | pfx_length, a, | |
cc908b82 JH |
684 | a_midlen, a + pfx_length, |
685 | b_midlen, b + pfx_length, | |
6973dcae JH |
686 | a + len_a - sfx_length); |
687 | } | |
688 | else { | |
689 | name = xmalloc(len_a + len_b + 5); | |
690 | sprintf(name, "%s => %s", a, b); | |
691 | } | |
692 | return name; | |
693 | } | |
694 | ||
695 | struct diffstat_t { | |
696 | struct xdiff_emit_state xm; | |
697 | ||
698 | int nr; | |
699 | int alloc; | |
700 | struct diffstat_file { | |
701 | char *name; | |
702 | unsigned is_unmerged:1; | |
703 | unsigned is_binary:1; | |
704 | unsigned is_renamed:1; | |
705 | unsigned int added, deleted; | |
706 | } **files; | |
707 | }; | |
708 | ||
709 | static struct diffstat_file *diffstat_add(struct diffstat_t *diffstat, | |
710 | const char *name_a, | |
711 | const char *name_b) | |
712 | { | |
713 | struct diffstat_file *x; | |
714 | x = xcalloc(sizeof (*x), 1); | |
715 | if (diffstat->nr == diffstat->alloc) { | |
716 | diffstat->alloc = alloc_nr(diffstat->alloc); | |
717 | diffstat->files = xrealloc(diffstat->files, | |
718 | diffstat->alloc * sizeof(x)); | |
719 | } | |
720 | diffstat->files[diffstat->nr++] = x; | |
721 | if (name_b) { | |
722 | x->name = pprint_rename(name_a, name_b); | |
723 | x->is_renamed = 1; | |
724 | } | |
725 | else | |
9befac47 | 726 | x->name = xstrdup(name_a); |
6973dcae JH |
727 | return x; |
728 | } | |
729 | ||
730 | static void diffstat_consume(void *priv, char *line, unsigned long len) | |
731 | { | |
732 | struct diffstat_t *diffstat = priv; | |
733 | struct diffstat_file *x = diffstat->files[diffstat->nr - 1]; | |
734 | ||
735 | if (line[0] == '+') | |
736 | x->added++; | |
737 | else if (line[0] == '-') | |
738 | x->deleted++; | |
739 | } | |
740 | ||
698ce6f8 | 741 | const char mime_boundary_leader[] = "------------"; |
6973dcae | 742 | |
a2540023 JH |
743 | static int scale_linear(int it, int width, int max_change) |
744 | { | |
745 | /* | |
3ed74e60 JS |
746 | * make sure that at least one '-' is printed if there were deletions, |
747 | * and likewise for '+'. | |
a2540023 | 748 | */ |
3ed74e60 JS |
749 | if (max_change < 2) |
750 | return it; | |
751 | return ((it - 1) * (width - 1) + max_change - 1) / (max_change - 1); | |
a2540023 JH |
752 | } |
753 | ||
785f7432 JH |
754 | static void show_name(const char *prefix, const char *name, int len, |
755 | const char *reset, const char *set) | |
a2540023 | 756 | { |
785f7432 | 757 | printf(" %s%s%-*s%s |", set, prefix, len, name, reset); |
a2540023 JH |
758 | } |
759 | ||
785f7432 | 760 | static void show_graph(char ch, int cnt, const char *set, const char *reset) |
a2540023 JH |
761 | { |
762 | if (cnt <= 0) | |
763 | return; | |
785f7432 | 764 | printf("%s", set); |
a2540023 JH |
765 | while (cnt--) |
766 | putchar(ch); | |
785f7432 | 767 | printf("%s", reset); |
a2540023 JH |
768 | } |
769 | ||
770 | static void show_stats(struct diffstat_t* data, struct diff_options *options) | |
6973dcae | 771 | { |
6973dcae | 772 | int i, len, add, del, total, adds = 0, dels = 0; |
a2540023 | 773 | int max_change = 0, max_len = 0; |
6973dcae | 774 | int total_files = data->nr; |
a2540023 | 775 | int width, name_width; |
785f7432 | 776 | const char *reset, *set, *add_c, *del_c; |
6973dcae JH |
777 | |
778 | if (data->nr == 0) | |
779 | return; | |
780 | ||
a2540023 JH |
781 | width = options->stat_width ? options->stat_width : 80; |
782 | name_width = options->stat_name_width ? options->stat_name_width : 50; | |
783 | ||
784 | /* Sanity: give at least 5 columns to the graph, | |
785 | * but leave at least 10 columns for the name. | |
786 | */ | |
787 | if (width < name_width + 15) { | |
788 | if (name_width <= 25) | |
789 | width = name_width + 15; | |
790 | else | |
791 | name_width = width - 15; | |
792 | } | |
793 | ||
794 | /* Find the longest filename and max number of changes */ | |
785f7432 JH |
795 | reset = diff_get_color(options->color_diff, DIFF_RESET); |
796 | set = diff_get_color(options->color_diff, DIFF_PLAIN); | |
797 | add_c = diff_get_color(options->color_diff, DIFF_FILE_NEW); | |
798 | del_c = diff_get_color(options->color_diff, DIFF_FILE_OLD); | |
799 | ||
6973dcae JH |
800 | for (i = 0; i < data->nr; i++) { |
801 | struct diffstat_file *file = data->files[i]; | |
a2540023 JH |
802 | int change = file->added + file->deleted; |
803 | ||
e5bfbf9b AJ |
804 | if (!file->is_renamed) { /* renames are already quoted by pprint_rename */ |
805 | len = quote_c_style(file->name, NULL, NULL, 0); | |
806 | if (len) { | |
807 | char *qname = xmalloc(len + 1); | |
808 | quote_c_style(file->name, qname, NULL, 0); | |
809 | free(file->name); | |
810 | file->name = qname; | |
811 | } | |
a2540023 | 812 | } |
6973dcae JH |
813 | |
814 | len = strlen(file->name); | |
815 | if (max_len < len) | |
816 | max_len = len; | |
817 | ||
818 | if (file->is_binary || file->is_unmerged) | |
819 | continue; | |
a2540023 JH |
820 | if (max_change < change) |
821 | max_change = change; | |
6973dcae JH |
822 | } |
823 | ||
a2540023 JH |
824 | /* Compute the width of the graph part; |
825 | * 10 is for one blank at the beginning of the line plus | |
826 | * " | count " between the name and the graph. | |
827 | * | |
828 | * From here on, name_width is the width of the name area, | |
829 | * and width is the width of the graph area. | |
830 | */ | |
831 | name_width = (name_width < max_len) ? name_width : max_len; | |
832 | if (width < (name_width + 10) + max_change) | |
833 | width = width - (name_width + 10); | |
834 | else | |
835 | width = max_change; | |
836 | ||
6973dcae | 837 | for (i = 0; i < data->nr; i++) { |
d2543b8e | 838 | const char *prefix = ""; |
6973dcae JH |
839 | char *name = data->files[i]->name; |
840 | int added = data->files[i]->added; | |
841 | int deleted = data->files[i]->deleted; | |
a2540023 | 842 | int name_len; |
6973dcae JH |
843 | |
844 | /* | |
845 | * "scale" the filename | |
846 | */ | |
a2540023 JH |
847 | len = name_width; |
848 | name_len = strlen(name); | |
849 | if (name_width < name_len) { | |
6973dcae JH |
850 | char *slash; |
851 | prefix = "..."; | |
a2540023 JH |
852 | len -= 3; |
853 | name += name_len - len; | |
6973dcae JH |
854 | slash = strchr(name, '/'); |
855 | if (slash) | |
856 | name = slash; | |
857 | } | |
6973dcae JH |
858 | |
859 | if (data->files[i]->is_binary) { | |
785f7432 | 860 | show_name(prefix, name, len, reset, set); |
b1882587 AP |
861 | printf(" Bin "); |
862 | printf("%s%d%s", del_c, deleted, reset); | |
863 | printf(" -> "); | |
864 | printf("%s%d%s", add_c, added, reset); | |
865 | printf(" bytes"); | |
866 | printf("\n"); | |
6973dcae JH |
867 | goto free_diffstat_file; |
868 | } | |
869 | else if (data->files[i]->is_unmerged) { | |
785f7432 | 870 | show_name(prefix, name, len, reset, set); |
a2540023 | 871 | printf(" Unmerged\n"); |
6973dcae JH |
872 | goto free_diffstat_file; |
873 | } | |
874 | else if (!data->files[i]->is_renamed && | |
875 | (added + deleted == 0)) { | |
876 | total_files--; | |
877 | goto free_diffstat_file; | |
878 | } | |
879 | ||
a2540023 JH |
880 | /* |
881 | * scale the add/delete | |
882 | */ | |
6973dcae JH |
883 | add = added; |
884 | del = deleted; | |
885 | total = add + del; | |
886 | adds += add; | |
887 | dels += del; | |
888 | ||
a2540023 | 889 | if (width <= max_change) { |
a2540023 | 890 | add = scale_linear(add, width, max_change); |
3ed74e60 JS |
891 | del = scale_linear(del, width, max_change); |
892 | total = add + del; | |
6973dcae | 893 | } |
785f7432 | 894 | show_name(prefix, name, len, reset, set); |
a2540023 | 895 | printf("%5d ", added + deleted); |
785f7432 JH |
896 | show_graph('+', add, add_c, reset); |
897 | show_graph('-', del, del_c, reset); | |
a2540023 | 898 | putchar('\n'); |
6973dcae JH |
899 | free_diffstat_file: |
900 | free(data->files[i]->name); | |
901 | free(data->files[i]); | |
902 | } | |
903 | free(data->files); | |
785f7432 JH |
904 | printf("%s %d files changed, %d insertions(+), %d deletions(-)%s\n", |
905 | set, total_files, adds, dels, reset); | |
6973dcae JH |
906 | } |
907 | ||
ebd124c6 NP |
908 | static void show_shortstats(struct diffstat_t* data) |
909 | { | |
910 | int i, adds = 0, dels = 0, total_files = data->nr; | |
911 | ||
912 | if (data->nr == 0) | |
913 | return; | |
914 | ||
915 | for (i = 0; i < data->nr; i++) { | |
916 | if (!data->files[i]->is_binary && | |
917 | !data->files[i]->is_unmerged) { | |
918 | int added = data->files[i]->added; | |
919 | int deleted= data->files[i]->deleted; | |
920 | if (!data->files[i]->is_renamed && | |
921 | (added + deleted == 0)) { | |
922 | total_files--; | |
923 | } else { | |
924 | adds += added; | |
925 | dels += deleted; | |
926 | } | |
927 | } | |
928 | free(data->files[i]->name); | |
929 | free(data->files[i]); | |
930 | } | |
931 | free(data->files); | |
932 | ||
933 | printf(" %d files changed, %d insertions(+), %d deletions(-)\n", | |
934 | total_files, adds, dels); | |
935 | } | |
936 | ||
74e2abe5 JH |
937 | static void show_numstat(struct diffstat_t* data, struct diff_options *options) |
938 | { | |
939 | int i; | |
940 | ||
941 | for (i = 0; i < data->nr; i++) { | |
942 | struct diffstat_file *file = data->files[i]; | |
943 | ||
bfddbc5e JH |
944 | if (file->is_binary) |
945 | printf("-\t-\t"); | |
946 | else | |
947 | printf("%d\t%d\t", file->added, file->deleted); | |
e5bfbf9b | 948 | if (options->line_termination && !file->is_renamed && |
74e2abe5 JH |
949 | quote_c_style(file->name, NULL, NULL, 0)) |
950 | quote_c_style(file->name, NULL, stdout, 0); | |
951 | else | |
952 | fputs(file->name, stdout); | |
953 | putchar(options->line_termination); | |
954 | } | |
955 | } | |
956 | ||
88246898 JS |
957 | struct checkdiff_t { |
958 | struct xdiff_emit_state xm; | |
959 | const char *filename; | |
c5a8c3ec | 960 | int lineno, color_diff; |
88246898 JS |
961 | }; |
962 | ||
963 | static void checkdiff_consume(void *priv, char *line, unsigned long len) | |
964 | { | |
965 | struct checkdiff_t *data = priv; | |
c5a8c3ec JS |
966 | const char *ws = diff_get_color(data->color_diff, DIFF_WHITESPACE); |
967 | const char *reset = diff_get_color(data->color_diff, DIFF_RESET); | |
968 | const char *set = diff_get_color(data->color_diff, DIFF_FILE_NEW); | |
88246898 JS |
969 | |
970 | if (line[0] == '+') { | |
c5a8c3ec | 971 | int i, spaces = 0, space_before_tab = 0, white_space_at_end = 0; |
88246898 | 972 | |
88246898 JS |
973 | /* check space before tab */ |
974 | for (i = 1; i < len && (line[i] == ' ' || line[i] == '\t'); i++) | |
975 | if (line[i] == ' ') | |
976 | spaces++; | |
977 | if (line[i - 1] == '\t' && spaces) | |
c5a8c3ec | 978 | space_before_tab = 1; |
88246898 JS |
979 | |
980 | /* check white space at line end */ | |
981 | if (line[len - 1] == '\n') | |
982 | len--; | |
983 | if (isspace(line[len - 1])) | |
c5a8c3ec JS |
984 | white_space_at_end = 1; |
985 | ||
986 | if (space_before_tab || white_space_at_end) { | |
987 | printf("%s:%d: %s", data->filename, data->lineno, ws); | |
988 | if (space_before_tab) { | |
989 | printf("space before tab"); | |
990 | if (white_space_at_end) | |
991 | putchar(','); | |
992 | } | |
993 | if (white_space_at_end) | |
994 | printf("white space at end"); | |
995 | printf(":%s ", reset); | |
996 | emit_line_with_ws(1, set, reset, ws, line, len); | |
997 | } | |
e6d40d65 JS |
998 | |
999 | data->lineno++; | |
88246898 JS |
1000 | } else if (line[0] == ' ') |
1001 | data->lineno++; | |
1002 | else if (line[0] == '@') { | |
1003 | char *plus = strchr(line, '+'); | |
1004 | if (plus) | |
9e848163 | 1005 | data->lineno = strtol(plus, NULL, 10); |
88246898 JS |
1006 | else |
1007 | die("invalid diff"); | |
1008 | } | |
1009 | } | |
1010 | ||
0660626c JH |
1011 | static unsigned char *deflate_it(char *data, |
1012 | unsigned long size, | |
1013 | unsigned long *result_size) | |
051308f6 | 1014 | { |
0660626c JH |
1015 | int bound; |
1016 | unsigned char *deflated; | |
1017 | z_stream stream; | |
1018 | ||
1019 | memset(&stream, 0, sizeof(stream)); | |
12f6c308 | 1020 | deflateInit(&stream, zlib_compression_level); |
0660626c JH |
1021 | bound = deflateBound(&stream, size); |
1022 | deflated = xmalloc(bound); | |
1023 | stream.next_out = deflated; | |
1024 | stream.avail_out = bound; | |
1025 | ||
1026 | stream.next_in = (unsigned char *)data; | |
1027 | stream.avail_in = size; | |
1028 | while (deflate(&stream, Z_FINISH) == Z_OK) | |
1029 | ; /* nothing */ | |
1030 | deflateEnd(&stream); | |
1031 | *result_size = stream.total_out; | |
1032 | return deflated; | |
051308f6 JH |
1033 | } |
1034 | ||
d4c452f0 | 1035 | static void emit_binary_diff_body(mmfile_t *one, mmfile_t *two) |
051308f6 | 1036 | { |
0660626c JH |
1037 | void *cp; |
1038 | void *delta; | |
1039 | void *deflated; | |
1040 | void *data; | |
1041 | unsigned long orig_size; | |
1042 | unsigned long delta_size; | |
1043 | unsigned long deflate_size; | |
1044 | unsigned long data_size; | |
051308f6 | 1045 | |
0660626c JH |
1046 | /* We could do deflated delta, or we could do just deflated two, |
1047 | * whichever is smaller. | |
051308f6 | 1048 | */ |
0660626c JH |
1049 | delta = NULL; |
1050 | deflated = deflate_it(two->ptr, two->size, &deflate_size); | |
1051 | if (one->size && two->size) { | |
1052 | delta = diff_delta(one->ptr, one->size, | |
1053 | two->ptr, two->size, | |
1054 | &delta_size, deflate_size); | |
1055 | if (delta) { | |
1056 | void *to_free = delta; | |
1057 | orig_size = delta_size; | |
1058 | delta = deflate_it(delta, delta_size, &delta_size); | |
1059 | free(to_free); | |
051308f6 JH |
1060 | } |
1061 | } | |
051308f6 | 1062 | |
0660626c JH |
1063 | if (delta && delta_size < deflate_size) { |
1064 | printf("delta %lu\n", orig_size); | |
1065 | free(deflated); | |
1066 | data = delta; | |
1067 | data_size = delta_size; | |
1068 | } | |
1069 | else { | |
1070 | printf("literal %lu\n", two->size); | |
1071 | free(delta); | |
1072 | data = deflated; | |
1073 | data_size = deflate_size; | |
1074 | } | |
051308f6 | 1075 | |
0660626c JH |
1076 | /* emit data encoded in base85 */ |
1077 | cp = data; | |
1078 | while (data_size) { | |
1079 | int bytes = (52 < data_size) ? 52 : data_size; | |
051308f6 | 1080 | char line[70]; |
0660626c | 1081 | data_size -= bytes; |
051308f6 JH |
1082 | if (bytes <= 26) |
1083 | line[0] = bytes + 'A' - 1; | |
1084 | else | |
1085 | line[0] = bytes - 26 + 'a' - 1; | |
1086 | encode_85(line + 1, cp, bytes); | |
1d7f171c | 1087 | cp = (char *) cp + bytes; |
051308f6 JH |
1088 | puts(line); |
1089 | } | |
1090 | printf("\n"); | |
0660626c | 1091 | free(data); |
051308f6 JH |
1092 | } |
1093 | ||
d4c452f0 JH |
1094 | static void emit_binary_diff(mmfile_t *one, mmfile_t *two) |
1095 | { | |
1096 | printf("GIT binary patch\n"); | |
1097 | emit_binary_diff_body(one, two); | |
1098 | emit_binary_diff_body(two, one); | |
1099 | } | |
1100 | ||
8c701249 JH |
1101 | static void setup_diff_attr_check(struct git_attr_check *check) |
1102 | { | |
1103 | static struct git_attr *attr_diff; | |
1104 | ||
1105 | if (!attr_diff) | |
1106 | attr_diff = git_attr("diff", 4); | |
1107 | check->attr = attr_diff; | |
1108 | } | |
1109 | ||
8c701249 | 1110 | static int file_is_binary(struct diff_filespec *one) |
6973dcae | 1111 | { |
8c701249 JH |
1112 | struct git_attr_check attr_diff_check; |
1113 | ||
1114 | setup_diff_attr_check(&attr_diff_check); | |
515106fa | 1115 | if (!git_checkattr(one->path, 1, &attr_diff_check)) { |
a5e92abd | 1116 | const char *value = attr_diff_check.value; |
515106fa JH |
1117 | if (ATTR_TRUE(value)) |
1118 | return 0; | |
1119 | else if (ATTR_FALSE(value)) | |
1120 | return 1; | |
515106fa | 1121 | } |
40250af4 | 1122 | |
8c701249 JH |
1123 | if (!one->data) { |
1124 | if (!DIFF_FILE_VALID(one)) | |
1125 | return 0; | |
1126 | diff_populate_filespec(one, 0); | |
1127 | } | |
6bfce93e | 1128 | return buffer_is_binary(one->data, one->size); |
6973dcae JH |
1129 | } |
1130 | ||
1131 | static void builtin_diff(const char *name_a, | |
1132 | const char *name_b, | |
1133 | struct diff_filespec *one, | |
1134 | struct diff_filespec *two, | |
1135 | const char *xfrm_msg, | |
051308f6 | 1136 | struct diff_options *o, |
6973dcae JH |
1137 | int complete_rewrite) |
1138 | { | |
1139 | mmfile_t mf1, mf2; | |
1140 | const char *lbl[2]; | |
1141 | char *a_one, *b_two; | |
ce436973 JK |
1142 | const char *set = diff_get_color(o->color_diff, DIFF_METAINFO); |
1143 | const char *reset = diff_get_color(o->color_diff, DIFF_RESET); | |
6973dcae | 1144 | |
50892777 JH |
1145 | a_one = quote_two("a/", name_a + (*name_a == '/')); |
1146 | b_two = quote_two("b/", name_b + (*name_b == '/')); | |
6973dcae JH |
1147 | lbl[0] = DIFF_FILE_VALID(one) ? a_one : "/dev/null"; |
1148 | lbl[1] = DIFF_FILE_VALID(two) ? b_two : "/dev/null"; | |
50f575fc | 1149 | printf("%sdiff --git %s %s%s\n", set, a_one, b_two, reset); |
6973dcae JH |
1150 | if (lbl[0][0] == '/') { |
1151 | /* /dev/null */ | |
50f575fc LT |
1152 | printf("%snew file mode %06o%s\n", set, two->mode, reset); |
1153 | if (xfrm_msg && xfrm_msg[0]) | |
1154 | printf("%s%s%s\n", set, xfrm_msg, reset); | |
6973dcae JH |
1155 | } |
1156 | else if (lbl[1][0] == '/') { | |
50f575fc LT |
1157 | printf("%sdeleted file mode %06o%s\n", set, one->mode, reset); |
1158 | if (xfrm_msg && xfrm_msg[0]) | |
1159 | printf("%s%s%s\n", set, xfrm_msg, reset); | |
6973dcae JH |
1160 | } |
1161 | else { | |
1162 | if (one->mode != two->mode) { | |
50f575fc LT |
1163 | printf("%sold mode %06o%s\n", set, one->mode, reset); |
1164 | printf("%snew mode %06o%s\n", set, two->mode, reset); | |
cd112cef | 1165 | } |
50f575fc LT |
1166 | if (xfrm_msg && xfrm_msg[0]) |
1167 | printf("%s%s%s\n", set, xfrm_msg, reset); | |
6973dcae JH |
1168 | /* |
1169 | * we do not run diff between different kind | |
1170 | * of objects. | |
1171 | */ | |
1172 | if ((one->mode ^ two->mode) & S_IFMT) | |
1173 | goto free_ab_and_return; | |
1174 | if (complete_rewrite) { | |
13e36ec5 JS |
1175 | emit_rewrite_diff(name_a, name_b, one, two, |
1176 | o->color_diff); | |
34a5e1a2 | 1177 | o->found_changes = 1; |
6973dcae JH |
1178 | goto free_ab_and_return; |
1179 | } | |
1180 | } | |
1181 | ||
1182 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) | |
1183 | die("unable to read files to diff"); | |
1184 | ||
8c701249 | 1185 | if (!o->text && (file_is_binary(one) || file_is_binary(two))) { |
0660626c JH |
1186 | /* Quite common confusing case */ |
1187 | if (mf1.size == mf2.size && | |
1188 | !memcmp(mf1.ptr, mf2.ptr, mf1.size)) | |
1189 | goto free_ab_and_return; | |
1190 | if (o->binary) | |
051308f6 JH |
1191 | emit_binary_diff(&mf1, &mf2); |
1192 | else | |
1193 | printf("Binary files %s and %s differ\n", | |
1194 | lbl[0], lbl[1]); | |
34a5e1a2 | 1195 | o->found_changes = 1; |
051308f6 | 1196 | } |
6973dcae JH |
1197 | else { |
1198 | /* Crazy xdl interfaces.. */ | |
1199 | const char *diffopts = getenv("GIT_DIFF_OPTS"); | |
1200 | xpparam_t xpp; | |
1201 | xdemitconf_t xecfg; | |
1202 | xdemitcb_t ecb; | |
1203 | struct emit_callback ecbdata; | |
1204 | ||
cd112cef | 1205 | memset(&ecbdata, 0, sizeof(ecbdata)); |
6973dcae | 1206 | ecbdata.label_path = lbl; |
cd112cef | 1207 | ecbdata.color_diff = o->color_diff; |
34a5e1a2 | 1208 | ecbdata.found_changesp = &o->found_changes; |
0d21efa5 | 1209 | xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts; |
ee1e5412 | 1210 | xecfg.ctxlen = o->context; |
6973dcae JH |
1211 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
1212 | if (!diffopts) | |
1213 | ; | |
cc44c765 | 1214 | else if (!prefixcmp(diffopts, "--unified=")) |
6973dcae | 1215 | xecfg.ctxlen = strtoul(diffopts + 10, NULL, 10); |
cc44c765 | 1216 | else if (!prefixcmp(diffopts, "-u")) |
6973dcae | 1217 | xecfg.ctxlen = strtoul(diffopts + 2, NULL, 10); |
cd112cef | 1218 | ecb.outf = xdiff_outf; |
6973dcae | 1219 | ecb.priv = &ecbdata; |
cd112cef | 1220 | ecbdata.xm.consume = fn_out_consume; |
f59a59e2 JS |
1221 | if (o->color_diff_words) |
1222 | ecbdata.diff_words = | |
1223 | xcalloc(1, sizeof(struct diff_words_data)); | |
6973dcae | 1224 | xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb); |
f59a59e2 JS |
1225 | if (o->color_diff_words) |
1226 | free_diff_words_data(&ecbdata); | |
6973dcae JH |
1227 | } |
1228 | ||
1229 | free_ab_and_return: | |
fc3abdf5 JH |
1230 | diff_free_filespec_data(one); |
1231 | diff_free_filespec_data(two); | |
6973dcae JH |
1232 | free(a_one); |
1233 | free(b_two); | |
1234 | return; | |
1235 | } | |
1236 | ||
1237 | static void builtin_diffstat(const char *name_a, const char *name_b, | |
1238 | struct diff_filespec *one, | |
1239 | struct diff_filespec *two, | |
710158e3 | 1240 | struct diffstat_t *diffstat, |
0d21efa5 | 1241 | struct diff_options *o, |
710158e3 | 1242 | int complete_rewrite) |
6973dcae JH |
1243 | { |
1244 | mmfile_t mf1, mf2; | |
1245 | struct diffstat_file *data; | |
1246 | ||
1247 | data = diffstat_add(diffstat, name_a, name_b); | |
1248 | ||
1249 | if (!one || !two) { | |
1250 | data->is_unmerged = 1; | |
1251 | return; | |
1252 | } | |
710158e3 JH |
1253 | if (complete_rewrite) { |
1254 | diff_populate_filespec(one, 0); | |
1255 | diff_populate_filespec(two, 0); | |
1256 | data->deleted = count_lines(one->data, one->size); | |
1257 | data->added = count_lines(two->data, two->size); | |
fc3abdf5 | 1258 | goto free_and_return; |
710158e3 | 1259 | } |
6973dcae JH |
1260 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) |
1261 | die("unable to read files to diff"); | |
1262 | ||
8c701249 | 1263 | if (file_is_binary(one) || file_is_binary(two)) { |
6973dcae | 1264 | data->is_binary = 1; |
b1882587 AP |
1265 | data->added = mf2.size; |
1266 | data->deleted = mf1.size; | |
1267 | } else { | |
6973dcae JH |
1268 | /* Crazy xdl interfaces.. */ |
1269 | xpparam_t xpp; | |
1270 | xdemitconf_t xecfg; | |
1271 | xdemitcb_t ecb; | |
1272 | ||
0d21efa5 | 1273 | xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts; |
6973dcae JH |
1274 | xecfg.ctxlen = 0; |
1275 | xecfg.flags = 0; | |
1276 | ecb.outf = xdiff_outf; | |
1277 | ecb.priv = diffstat; | |
1278 | xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb); | |
1279 | } | |
fc3abdf5 JH |
1280 | |
1281 | free_and_return: | |
1282 | diff_free_filespec_data(one); | |
1283 | diff_free_filespec_data(two); | |
6973dcae JH |
1284 | } |
1285 | ||
88246898 JS |
1286 | static void builtin_checkdiff(const char *name_a, const char *name_b, |
1287 | struct diff_filespec *one, | |
c5a8c3ec | 1288 | struct diff_filespec *two, struct diff_options *o) |
88246898 JS |
1289 | { |
1290 | mmfile_t mf1, mf2; | |
1291 | struct checkdiff_t data; | |
1292 | ||
1293 | if (!two) | |
1294 | return; | |
1295 | ||
1296 | memset(&data, 0, sizeof(data)); | |
1297 | data.xm.consume = checkdiff_consume; | |
1298 | data.filename = name_b ? name_b : name_a; | |
1299 | data.lineno = 0; | |
c5a8c3ec | 1300 | data.color_diff = o->color_diff; |
88246898 JS |
1301 | |
1302 | if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0) | |
1303 | die("unable to read files to diff"); | |
1304 | ||
8c701249 | 1305 | if (file_is_binary(two)) |
fc3abdf5 | 1306 | goto free_and_return; |
88246898 JS |
1307 | else { |
1308 | /* Crazy xdl interfaces.. */ | |
1309 | xpparam_t xpp; | |
1310 | xdemitconf_t xecfg; | |
1311 | xdemitcb_t ecb; | |
1312 | ||
1313 | xpp.flags = XDF_NEED_MINIMAL; | |
1314 | xecfg.ctxlen = 0; | |
1315 | xecfg.flags = 0; | |
1316 | ecb.outf = xdiff_outf; | |
1317 | ecb.priv = &data; | |
1318 | xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb); | |
1319 | } | |
fc3abdf5 JH |
1320 | free_and_return: |
1321 | diff_free_filespec_data(one); | |
1322 | diff_free_filespec_data(two); | |
88246898 JS |
1323 | } |
1324 | ||
6973dcae JH |
1325 | struct diff_filespec *alloc_filespec(const char *path) |
1326 | { | |
1327 | int namelen = strlen(path); | |
1328 | struct diff_filespec *spec = xmalloc(sizeof(*spec) + namelen + 1); | |
1329 | ||
1330 | memset(spec, 0, sizeof(*spec)); | |
1331 | spec->path = (char *)(spec + 1); | |
1332 | memcpy(spec->path, path, namelen+1); | |
1333 | return spec; | |
1334 | } | |
1335 | ||
1336 | void fill_filespec(struct diff_filespec *spec, const unsigned char *sha1, | |
1337 | unsigned short mode) | |
1338 | { | |
1339 | if (mode) { | |
1340 | spec->mode = canon_mode(mode); | |
e702496e | 1341 | hashcpy(spec->sha1, sha1); |
0bef57ee | 1342 | spec->sha1_valid = !is_null_sha1(sha1); |
6973dcae JH |
1343 | } |
1344 | } | |
1345 | ||
1346 | /* | |
5adf317b | 1347 | * Given a name and sha1 pair, if the index tells us the file in |
6973dcae JH |
1348 | * the work tree has that object contents, return true, so that |
1349 | * prepare_temp_file() does not have to inflate and extract. | |
1350 | */ | |
1510fea7 | 1351 | static int reuse_worktree_file(const char *name, const unsigned char *sha1, int want_file) |
6973dcae JH |
1352 | { |
1353 | struct cache_entry *ce; | |
1354 | struct stat st; | |
1355 | int pos, len; | |
1356 | ||
1357 | /* We do not read the cache ourselves here, because the | |
1358 | * benchmark with my previous version that always reads cache | |
1359 | * shows that it makes things worse for diff-tree comparing | |
1360 | * two linux-2.6 kernel trees in an already checked out work | |
1361 | * tree. This is because most diff-tree comparisons deal with | |
1362 | * only a small number of files, while reading the cache is | |
1363 | * expensive for a large project, and its cost outweighs the | |
1364 | * savings we get by not inflating the object to a temporary | |
1365 | * file. Practically, this code only helps when we are used | |
1366 | * by diff-cache --cached, which does read the cache before | |
1367 | * calling us. | |
1368 | */ | |
1369 | if (!active_cache) | |
1370 | return 0; | |
1371 | ||
1510fea7 SP |
1372 | /* We want to avoid the working directory if our caller |
1373 | * doesn't need the data in a normal file, this system | |
1374 | * is rather slow with its stat/open/mmap/close syscalls, | |
1375 | * and the object is contained in a pack file. The pack | |
1376 | * is probably already open and will be faster to obtain | |
1377 | * the data through than the working directory. Loose | |
1378 | * objects however would tend to be slower as they need | |
1379 | * to be individually opened and inflated. | |
1380 | */ | |
5caf9232 | 1381 | if (!FAST_WORKING_DIRECTORY && !want_file && has_sha1_pack(sha1, NULL)) |
1510fea7 SP |
1382 | return 0; |
1383 | ||
6973dcae JH |
1384 | len = strlen(name); |
1385 | pos = cache_name_pos(name, len); | |
1386 | if (pos < 0) | |
1387 | return 0; | |
1388 | ce = active_cache[pos]; | |
1389 | if ((lstat(name, &st) < 0) || | |
1390 | !S_ISREG(st.st_mode) || /* careful! */ | |
1391 | ce_match_stat(ce, &st, 0) || | |
a89fccd2 | 1392 | hashcmp(sha1, ce->sha1)) |
6973dcae JH |
1393 | return 0; |
1394 | /* we return 1 only when we can stat, it is a regular file, | |
1395 | * stat information matches, and sha1 recorded in the cache | |
1396 | * matches. I.e. we know the file in the work tree really is | |
1397 | * the same as the <name, sha1> pair. | |
1398 | */ | |
1399 | return 1; | |
1400 | } | |
1401 | ||
3afaa72d JH |
1402 | static int populate_from_stdin(struct diff_filespec *s) |
1403 | { | |
1404 | #define INCREMENT 1024 | |
1405 | char *buf; | |
1406 | unsigned long size; | |
8a912bcb | 1407 | ssize_t got; |
3afaa72d JH |
1408 | |
1409 | size = 0; | |
1410 | buf = NULL; | |
1411 | while (1) { | |
1412 | buf = xrealloc(buf, size + INCREMENT); | |
1413 | got = xread(0, buf + size, INCREMENT); | |
1414 | if (!got) | |
1415 | break; /* EOF */ | |
1416 | if (got < 0) | |
1417 | return error("error while reading from stdin %s", | |
1418 | strerror(errno)); | |
1419 | size += got; | |
1420 | } | |
1421 | s->should_munmap = 0; | |
1422 | s->data = buf; | |
1423 | s->size = size; | |
1424 | s->should_free = 1; | |
1425 | return 0; | |
1426 | } | |
1427 | ||
04786756 LT |
1428 | static int diff_populate_gitlink(struct diff_filespec *s, int size_only) |
1429 | { | |
1430 | int len; | |
1431 | char *data = xmalloc(100); | |
1432 | len = snprintf(data, 100, | |
1433 | "Subproject commit %s\n", sha1_to_hex(s->sha1)); | |
1434 | s->data = data; | |
1435 | s->size = len; | |
1436 | s->should_free = 1; | |
1437 | if (size_only) { | |
1438 | s->data = NULL; | |
1439 | free(data); | |
1440 | } | |
1441 | return 0; | |
1442 | } | |
1443 | ||
6973dcae JH |
1444 | /* |
1445 | * While doing rename detection and pickaxe operation, we may need to | |
1446 | * grab the data for the blob (or file) for our own in-core comparison. | |
1447 | * diff_filespec has data and size fields for this purpose. | |
1448 | */ | |
1449 | int diff_populate_filespec(struct diff_filespec *s, int size_only) | |
1450 | { | |
1451 | int err = 0; | |
1452 | if (!DIFF_FILE_VALID(s)) | |
1453 | die("internal error: asking to populate invalid file."); | |
1454 | if (S_ISDIR(s->mode)) | |
1455 | return -1; | |
1456 | ||
6973dcae | 1457 | if (s->data) |
fc3abdf5 | 1458 | return 0; |
04786756 | 1459 | |
6e0b8ed6 JH |
1460 | if (size_only && 0 < s->size) |
1461 | return 0; | |
1462 | ||
302b9282 | 1463 | if (S_ISGITLINK(s->mode)) |
04786756 LT |
1464 | return diff_populate_gitlink(s, size_only); |
1465 | ||
6973dcae | 1466 | if (!s->sha1_valid || |
1510fea7 | 1467 | reuse_worktree_file(s->path, s->sha1, 0)) { |
6973dcae JH |
1468 | struct stat st; |
1469 | int fd; | |
6c510bee LT |
1470 | char *buf; |
1471 | unsigned long size; | |
1472 | ||
3afaa72d JH |
1473 | if (!strcmp(s->path, "-")) |
1474 | return populate_from_stdin(s); | |
1475 | ||
6973dcae JH |
1476 | if (lstat(s->path, &st) < 0) { |
1477 | if (errno == ENOENT) { | |
1478 | err_empty: | |
1479 | err = -1; | |
1480 | empty: | |
d2543b8e | 1481 | s->data = (char *)""; |
6973dcae JH |
1482 | s->size = 0; |
1483 | return err; | |
1484 | } | |
1485 | } | |
dc49cd76 | 1486 | s->size = xsize_t(st.st_size); |
6973dcae JH |
1487 | if (!s->size) |
1488 | goto empty; | |
1489 | if (size_only) | |
1490 | return 0; | |
1491 | if (S_ISLNK(st.st_mode)) { | |
1492 | int ret; | |
1493 | s->data = xmalloc(s->size); | |
1494 | s->should_free = 1; | |
1495 | ret = readlink(s->path, s->data, s->size); | |
1496 | if (ret < 0) { | |
1497 | free(s->data); | |
1498 | goto err_empty; | |
1499 | } | |
1500 | return 0; | |
1501 | } | |
1502 | fd = open(s->path, O_RDONLY); | |
1503 | if (fd < 0) | |
1504 | goto err_empty; | |
c4712e45 | 1505 | s->data = xmmap(NULL, s->size, PROT_READ, MAP_PRIVATE, fd, 0); |
6973dcae | 1506 | close(fd); |
6973dcae | 1507 | s->should_munmap = 1; |
6c510bee LT |
1508 | |
1509 | /* | |
1510 | * Convert from working tree format to canonical git format | |
1511 | */ | |
6c510bee | 1512 | size = s->size; |
ac78e548 AR |
1513 | buf = convert_to_git(s->path, s->data, &size); |
1514 | if (buf) { | |
6c510bee LT |
1515 | munmap(s->data, s->size); |
1516 | s->should_munmap = 0; | |
1517 | s->data = buf; | |
1518 | s->size = size; | |
1519 | s->should_free = 1; | |
1520 | } | |
6973dcae JH |
1521 | } |
1522 | else { | |
21666f1a | 1523 | enum object_type type; |
6e0b8ed6 | 1524 | if (size_only) |
21666f1a | 1525 | type = sha1_object_info(s->sha1, &s->size); |
6973dcae | 1526 | else { |
21666f1a | 1527 | s->data = read_sha1_file(s->sha1, &type, &s->size); |
6973dcae JH |
1528 | s->should_free = 1; |
1529 | } | |
1530 | } | |
1531 | return 0; | |
1532 | } | |
1533 | ||
1534 | void diff_free_filespec_data(struct diff_filespec *s) | |
1535 | { | |
1536 | if (s->should_free) | |
1537 | free(s->data); | |
1538 | else if (s->should_munmap) | |
1539 | munmap(s->data, s->size); | |
fc3abdf5 JH |
1540 | |
1541 | if (s->should_free || s->should_munmap) { | |
1542 | s->should_free = s->should_munmap = 0; | |
1543 | s->data = NULL; | |
1544 | } | |
6973dcae JH |
1545 | free(s->cnt_data); |
1546 | s->cnt_data = NULL; | |
1547 | } | |
1548 | ||
1549 | static void prep_temp_blob(struct diff_tempfile *temp, | |
1550 | void *blob, | |
1551 | unsigned long size, | |
1552 | const unsigned char *sha1, | |
1553 | int mode) | |
1554 | { | |
1555 | int fd; | |
1556 | ||
1472966c | 1557 | fd = git_mkstemp(temp->tmp_path, PATH_MAX, ".diff_XXXXXX"); |
6973dcae JH |
1558 | if (fd < 0) |
1559 | die("unable to create temp-file"); | |
93822c22 | 1560 | if (write_in_full(fd, blob, size) != size) |
6973dcae JH |
1561 | die("unable to write temp-file"); |
1562 | close(fd); | |
1563 | temp->name = temp->tmp_path; | |
1564 | strcpy(temp->hex, sha1_to_hex(sha1)); | |
1565 | temp->hex[40] = 0; | |
1566 | sprintf(temp->mode, "%06o", mode); | |
1567 | } | |
1568 | ||
1569 | static void prepare_temp_file(const char *name, | |
1570 | struct diff_tempfile *temp, | |
1571 | struct diff_filespec *one) | |
1572 | { | |
1573 | if (!DIFF_FILE_VALID(one)) { | |
1574 | not_a_valid_file: | |
1575 | /* A '-' entry produces this for file-2, and | |
1576 | * a '+' entry produces this for file-1. | |
1577 | */ | |
1578 | temp->name = "/dev/null"; | |
1579 | strcpy(temp->hex, "."); | |
1580 | strcpy(temp->mode, "."); | |
1581 | return; | |
1582 | } | |
1583 | ||
1584 | if (!one->sha1_valid || | |
1510fea7 | 1585 | reuse_worktree_file(name, one->sha1, 1)) { |
6973dcae JH |
1586 | struct stat st; |
1587 | if (lstat(name, &st) < 0) { | |
1588 | if (errno == ENOENT) | |
1589 | goto not_a_valid_file; | |
1590 | die("stat(%s): %s", name, strerror(errno)); | |
1591 | } | |
1592 | if (S_ISLNK(st.st_mode)) { | |
1593 | int ret; | |
1594 | char buf[PATH_MAX + 1]; /* ought to be SYMLINK_MAX */ | |
dc49cd76 | 1595 | size_t sz = xsize_t(st.st_size); |
6973dcae JH |
1596 | if (sizeof(buf) <= st.st_size) |
1597 | die("symlink too long: %s", name); | |
dc49cd76 | 1598 | ret = readlink(name, buf, sz); |
6973dcae JH |
1599 | if (ret < 0) |
1600 | die("readlink(%s)", name); | |
dc49cd76 | 1601 | prep_temp_blob(temp, buf, sz, |
6973dcae JH |
1602 | (one->sha1_valid ? |
1603 | one->sha1 : null_sha1), | |
1604 | (one->sha1_valid ? | |
1605 | one->mode : S_IFLNK)); | |
1606 | } | |
1607 | else { | |
1608 | /* we can borrow from the file in the work tree */ | |
1609 | temp->name = name; | |
1610 | if (!one->sha1_valid) | |
1611 | strcpy(temp->hex, sha1_to_hex(null_sha1)); | |
1612 | else | |
1613 | strcpy(temp->hex, sha1_to_hex(one->sha1)); | |
1614 | /* Even though we may sometimes borrow the | |
1615 | * contents from the work tree, we always want | |
1616 | * one->mode. mode is trustworthy even when | |
1617 | * !(one->sha1_valid), as long as | |
1618 | * DIFF_FILE_VALID(one). | |
1619 | */ | |
1620 | sprintf(temp->mode, "%06o", one->mode); | |
1621 | } | |
1622 | return; | |
1623 | } | |
1624 | else { | |
1625 | if (diff_populate_filespec(one, 0)) | |
1626 | die("cannot read data blob for %s", one->path); | |
1627 | prep_temp_blob(temp, one->data, one->size, | |
1628 | one->sha1, one->mode); | |
1629 | } | |
1630 | } | |
1631 | ||
1632 | static void remove_tempfile(void) | |
1633 | { | |
1634 | int i; | |
1635 | ||
1636 | for (i = 0; i < 2; i++) | |
1637 | if (diff_temp[i].name == diff_temp[i].tmp_path) { | |
1638 | unlink(diff_temp[i].name); | |
1639 | diff_temp[i].name = NULL; | |
1640 | } | |
1641 | } | |
1642 | ||
1643 | static void remove_tempfile_on_signal(int signo) | |
1644 | { | |
1645 | remove_tempfile(); | |
1646 | signal(SIGINT, SIG_DFL); | |
1647 | raise(signo); | |
1648 | } | |
1649 | ||
1650 | static int spawn_prog(const char *pgm, const char **arg) | |
1651 | { | |
1652 | pid_t pid; | |
1653 | int status; | |
1654 | ||
1655 | fflush(NULL); | |
1656 | pid = fork(); | |
1657 | if (pid < 0) | |
1658 | die("unable to fork"); | |
1659 | if (!pid) { | |
1660 | execvp(pgm, (char *const*) arg); | |
1661 | exit(255); | |
1662 | } | |
1663 | ||
1664 | while (waitpid(pid, &status, 0) < 0) { | |
1665 | if (errno == EINTR) | |
1666 | continue; | |
1667 | return -1; | |
1668 | } | |
1669 | ||
1670 | /* Earlier we did not check the exit status because | |
1671 | * diff exits non-zero if files are different, and | |
1672 | * we are not interested in knowing that. It was a | |
1673 | * mistake which made it harder to quit a diff-* | |
1674 | * session that uses the git-apply-patch-script as | |
1675 | * the GIT_EXTERNAL_DIFF. A custom GIT_EXTERNAL_DIFF | |
1676 | * should also exit non-zero only when it wants to | |
1677 | * abort the entire diff-* session. | |
1678 | */ | |
1679 | if (WIFEXITED(status) && !WEXITSTATUS(status)) | |
1680 | return 0; | |
1681 | return -1; | |
1682 | } | |
1683 | ||
1684 | /* An external diff command takes: | |
1685 | * | |
1686 | * diff-cmd name infile1 infile1-sha1 infile1-mode \ | |
1687 | * infile2 infile2-sha1 infile2-mode [ rename-to ] | |
1688 | * | |
1689 | */ | |
1690 | static void run_external_diff(const char *pgm, | |
1691 | const char *name, | |
1692 | const char *other, | |
1693 | struct diff_filespec *one, | |
1694 | struct diff_filespec *two, | |
1695 | const char *xfrm_msg, | |
1696 | int complete_rewrite) | |
1697 | { | |
1698 | const char *spawn_arg[10]; | |
1699 | struct diff_tempfile *temp = diff_temp; | |
1700 | int retval; | |
1701 | static int atexit_asked = 0; | |
1702 | const char *othername; | |
1703 | const char **arg = &spawn_arg[0]; | |
1704 | ||
1705 | othername = (other? other : name); | |
1706 | if (one && two) { | |
1707 | prepare_temp_file(name, &temp[0], one); | |
1708 | prepare_temp_file(othername, &temp[1], two); | |
1709 | if (! atexit_asked && | |
1710 | (temp[0].name == temp[0].tmp_path || | |
1711 | temp[1].name == temp[1].tmp_path)) { | |
1712 | atexit_asked = 1; | |
1713 | atexit(remove_tempfile); | |
1714 | } | |
1715 | signal(SIGINT, remove_tempfile_on_signal); | |
1716 | } | |
1717 | ||
1718 | if (one && two) { | |
1719 | *arg++ = pgm; | |
1720 | *arg++ = name; | |
1721 | *arg++ = temp[0].name; | |
1722 | *arg++ = temp[0].hex; | |
1723 | *arg++ = temp[0].mode; | |
1724 | *arg++ = temp[1].name; | |
1725 | *arg++ = temp[1].hex; | |
1726 | *arg++ = temp[1].mode; | |
1727 | if (other) { | |
1728 | *arg++ = other; | |
1729 | *arg++ = xfrm_msg; | |
1730 | } | |
1731 | } else { | |
1732 | *arg++ = pgm; | |
1733 | *arg++ = name; | |
1734 | } | |
1735 | *arg = NULL; | |
1736 | retval = spawn_prog(pgm, spawn_arg); | |
1737 | remove_tempfile(); | |
1738 | if (retval) { | |
1739 | fprintf(stderr, "external diff died, stopping at %s.\n", name); | |
1740 | exit(1); | |
1741 | } | |
1742 | } | |
1743 | ||
f1af60bd JH |
1744 | static const char *external_diff_attr(const char *name) |
1745 | { | |
1746 | struct git_attr_check attr_diff_check; | |
1747 | ||
1748 | setup_diff_attr_check(&attr_diff_check); | |
1749 | if (!git_checkattr(name, 1, &attr_diff_check)) { | |
1750 | const char *value = attr_diff_check.value; | |
1751 | if (!ATTR_TRUE(value) && | |
1752 | !ATTR_FALSE(value) && | |
1753 | !ATTR_UNSET(value)) { | |
1754 | struct ll_diff_driver *drv; | |
1755 | ||
1756 | if (!user_diff_tail) { | |
1757 | user_diff_tail = &user_diff; | |
1758 | git_config(git_diff_ui_config); | |
1759 | } | |
1760 | for (drv = user_diff; drv; drv = drv->next) | |
1761 | if (!strcmp(drv->name, value)) | |
1762 | return drv->cmd; | |
1763 | } | |
1764 | } | |
1765 | return NULL; | |
1766 | } | |
1767 | ||
6973dcae JH |
1768 | static void run_diff_cmd(const char *pgm, |
1769 | const char *name, | |
1770 | const char *other, | |
1771 | struct diff_filespec *one, | |
1772 | struct diff_filespec *two, | |
1773 | const char *xfrm_msg, | |
051308f6 | 1774 | struct diff_options *o, |
6973dcae JH |
1775 | int complete_rewrite) |
1776 | { | |
f1af60bd JH |
1777 | if (!o->allow_external) |
1778 | pgm = NULL; | |
1779 | else { | |
1780 | const char *cmd = external_diff_attr(name); | |
1781 | if (cmd) | |
1782 | pgm = cmd; | |
1783 | } | |
1784 | ||
6973dcae JH |
1785 | if (pgm) { |
1786 | run_external_diff(pgm, name, other, one, two, xfrm_msg, | |
1787 | complete_rewrite); | |
1788 | return; | |
1789 | } | |
1790 | if (one && two) | |
1791 | builtin_diff(name, other ? other : name, | |
051308f6 | 1792 | one, two, xfrm_msg, o, complete_rewrite); |
6973dcae JH |
1793 | else |
1794 | printf("* Unmerged path %s\n", name); | |
1795 | } | |
1796 | ||
1797 | static void diff_fill_sha1_info(struct diff_filespec *one) | |
1798 | { | |
1799 | if (DIFF_FILE_VALID(one)) { | |
1800 | if (!one->sha1_valid) { | |
1801 | struct stat st; | |
5332b2af JS |
1802 | if (!strcmp(one->path, "-")) { |
1803 | hashcpy(one->sha1, null_sha1); | |
1804 | return; | |
1805 | } | |
6973dcae JH |
1806 | if (lstat(one->path, &st) < 0) |
1807 | die("stat %s", one->path); | |
1808 | if (index_path(one->sha1, one->path, &st, 0)) | |
1809 | die("cannot hash %s\n", one->path); | |
1810 | } | |
1811 | } | |
1812 | else | |
e702496e | 1813 | hashclr(one->sha1); |
6973dcae JH |
1814 | } |
1815 | ||
1816 | static void run_diff(struct diff_filepair *p, struct diff_options *o) | |
1817 | { | |
1818 | const char *pgm = external_diff(); | |
1819 | char msg[PATH_MAX*2+300], *xfrm_msg; | |
1820 | struct diff_filespec *one; | |
1821 | struct diff_filespec *two; | |
1822 | const char *name; | |
1823 | const char *other; | |
1824 | char *name_munged, *other_munged; | |
1825 | int complete_rewrite = 0; | |
1826 | int len; | |
1827 | ||
1828 | if (DIFF_PAIR_UNMERGED(p)) { | |
1829 | /* unmerged */ | |
051308f6 | 1830 | run_diff_cmd(pgm, p->one->path, NULL, NULL, NULL, NULL, o, 0); |
6973dcae JH |
1831 | return; |
1832 | } | |
1833 | ||
1834 | name = p->one->path; | |
1835 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
1836 | name_munged = quote_one(name); | |
1837 | other_munged = quote_one(other); | |
1838 | one = p->one; two = p->two; | |
1839 | ||
1840 | diff_fill_sha1_info(one); | |
1841 | diff_fill_sha1_info(two); | |
1842 | ||
1843 | len = 0; | |
1844 | switch (p->status) { | |
1845 | case DIFF_STATUS_COPIED: | |
1846 | len += snprintf(msg + len, sizeof(msg) - len, | |
1847 | "similarity index %d%%\n" | |
1848 | "copy from %s\n" | |
1849 | "copy to %s\n", | |
1850 | (int)(0.5 + p->score * 100.0/MAX_SCORE), | |
1851 | name_munged, other_munged); | |
1852 | break; | |
1853 | case DIFF_STATUS_RENAMED: | |
1854 | len += snprintf(msg + len, sizeof(msg) - len, | |
1855 | "similarity index %d%%\n" | |
1856 | "rename from %s\n" | |
1857 | "rename to %s\n", | |
1858 | (int)(0.5 + p->score * 100.0/MAX_SCORE), | |
1859 | name_munged, other_munged); | |
1860 | break; | |
1861 | case DIFF_STATUS_MODIFIED: | |
1862 | if (p->score) { | |
1863 | len += snprintf(msg + len, sizeof(msg) - len, | |
1864 | "dissimilarity index %d%%\n", | |
1865 | (int)(0.5 + p->score * | |
1866 | 100.0/MAX_SCORE)); | |
1867 | complete_rewrite = 1; | |
1868 | break; | |
1869 | } | |
1870 | /* fallthru */ | |
1871 | default: | |
1872 | /* nothing */ | |
1873 | ; | |
1874 | } | |
1875 | ||
a89fccd2 | 1876 | if (hashcmp(one->sha1, two->sha1)) { |
6973dcae | 1877 | int abbrev = o->full_index ? 40 : DEFAULT_ABBREV; |
6973dcae | 1878 | |
82793c55 JH |
1879 | if (o->binary) { |
1880 | mmfile_t mf; | |
8c701249 JH |
1881 | if ((!fill_mmfile(&mf, one) && file_is_binary(one)) || |
1882 | (!fill_mmfile(&mf, two) && file_is_binary(two))) | |
82793c55 JH |
1883 | abbrev = 40; |
1884 | } | |
6973dcae JH |
1885 | len += snprintf(msg + len, sizeof(msg) - len, |
1886 | "index %.*s..%.*s", | |
dcb3450f LT |
1887 | abbrev, sha1_to_hex(one->sha1), |
1888 | abbrev, sha1_to_hex(two->sha1)); | |
6973dcae JH |
1889 | if (one->mode == two->mode) |
1890 | len += snprintf(msg + len, sizeof(msg) - len, | |
1891 | " %06o", one->mode); | |
1892 | len += snprintf(msg + len, sizeof(msg) - len, "\n"); | |
1893 | } | |
1894 | ||
1895 | if (len) | |
1896 | msg[--len] = 0; | |
1897 | xfrm_msg = len ? msg : NULL; | |
1898 | ||
1899 | if (!pgm && | |
1900 | DIFF_FILE_VALID(one) && DIFF_FILE_VALID(two) && | |
1901 | (S_IFMT & one->mode) != (S_IFMT & two->mode)) { | |
1902 | /* a filepair that changes between file and symlink | |
1903 | * needs to be split into deletion and creation. | |
1904 | */ | |
1905 | struct diff_filespec *null = alloc_filespec(two->path); | |
051308f6 | 1906 | run_diff_cmd(NULL, name, other, one, null, xfrm_msg, o, 0); |
6973dcae JH |
1907 | free(null); |
1908 | null = alloc_filespec(one->path); | |
051308f6 | 1909 | run_diff_cmd(NULL, name, other, null, two, xfrm_msg, o, 0); |
6973dcae JH |
1910 | free(null); |
1911 | } | |
1912 | else | |
051308f6 | 1913 | run_diff_cmd(pgm, name, other, one, two, xfrm_msg, o, |
6973dcae JH |
1914 | complete_rewrite); |
1915 | ||
1916 | free(name_munged); | |
1917 | free(other_munged); | |
1918 | } | |
1919 | ||
1920 | static void run_diffstat(struct diff_filepair *p, struct diff_options *o, | |
1921 | struct diffstat_t *diffstat) | |
1922 | { | |
1923 | const char *name; | |
1924 | const char *other; | |
710158e3 | 1925 | int complete_rewrite = 0; |
6973dcae JH |
1926 | |
1927 | if (DIFF_PAIR_UNMERGED(p)) { | |
1928 | /* unmerged */ | |
0d21efa5 | 1929 | builtin_diffstat(p->one->path, NULL, NULL, NULL, diffstat, o, 0); |
6973dcae JH |
1930 | return; |
1931 | } | |
1932 | ||
1933 | name = p->one->path; | |
1934 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
1935 | ||
1936 | diff_fill_sha1_info(p->one); | |
1937 | diff_fill_sha1_info(p->two); | |
1938 | ||
710158e3 JH |
1939 | if (p->status == DIFF_STATUS_MODIFIED && p->score) |
1940 | complete_rewrite = 1; | |
0d21efa5 | 1941 | builtin_diffstat(name, other, p->one, p->two, diffstat, o, complete_rewrite); |
6973dcae JH |
1942 | } |
1943 | ||
88246898 JS |
1944 | static void run_checkdiff(struct diff_filepair *p, struct diff_options *o) |
1945 | { | |
1946 | const char *name; | |
1947 | const char *other; | |
1948 | ||
1949 | if (DIFF_PAIR_UNMERGED(p)) { | |
1950 | /* unmerged */ | |
1951 | return; | |
1952 | } | |
1953 | ||
1954 | name = p->one->path; | |
1955 | other = (strcmp(name, p->two->path) ? p->two->path : NULL); | |
1956 | ||
1957 | diff_fill_sha1_info(p->one); | |
1958 | diff_fill_sha1_info(p->two); | |
1959 | ||
c5a8c3ec | 1960 | builtin_checkdiff(name, other, p->one, p->two, o); |
88246898 JS |
1961 | } |
1962 | ||
6973dcae JH |
1963 | void diff_setup(struct diff_options *options) |
1964 | { | |
1965 | memset(options, 0, sizeof(*options)); | |
6973dcae JH |
1966 | options->line_termination = '\n'; |
1967 | options->break_opt = -1; | |
1968 | options->rename_limit = -1; | |
ee1e5412 | 1969 | options->context = 3; |
3969cf7d | 1970 | options->msg_sep = ""; |
6973dcae JH |
1971 | |
1972 | options->change = diff_change; | |
1973 | options->add_remove = diff_addremove; | |
801235c5 | 1974 | options->color_diff = diff_use_color_default; |
b68ea12e | 1975 | options->detect_rename = diff_detect_rename_default; |
6973dcae JH |
1976 | } |
1977 | ||
1978 | int diff_setup_done(struct diff_options *options) | |
1979 | { | |
d7de00f7 TH |
1980 | int count = 0; |
1981 | ||
1982 | if (options->output_format & DIFF_FORMAT_NAME) | |
1983 | count++; | |
1984 | if (options->output_format & DIFF_FORMAT_NAME_STATUS) | |
1985 | count++; | |
1986 | if (options->output_format & DIFF_FORMAT_CHECKDIFF) | |
1987 | count++; | |
1988 | if (options->output_format & DIFF_FORMAT_NO_OUTPUT) | |
1989 | count++; | |
1990 | if (count > 1) | |
1991 | die("--name-only, --name-status, --check and -s are mutually exclusive"); | |
1992 | ||
03b9d560 JH |
1993 | if (options->find_copies_harder) |
1994 | options->detect_rename = DIFF_DETECT_COPY; | |
1995 | ||
c6744349 TH |
1996 | if (options->output_format & (DIFF_FORMAT_NAME | |
1997 | DIFF_FORMAT_NAME_STATUS | | |
1998 | DIFF_FORMAT_CHECKDIFF | | |
1999 | DIFF_FORMAT_NO_OUTPUT)) | |
2000 | options->output_format &= ~(DIFF_FORMAT_RAW | | |
74e2abe5 | 2001 | DIFF_FORMAT_NUMSTAT | |
c6744349 | 2002 | DIFF_FORMAT_DIFFSTAT | |
ebd124c6 | 2003 | DIFF_FORMAT_SHORTSTAT | |
c6744349 TH |
2004 | DIFF_FORMAT_SUMMARY | |
2005 | DIFF_FORMAT_PATCH); | |
2006 | ||
6973dcae JH |
2007 | /* |
2008 | * These cases always need recursive; we do not drop caller-supplied | |
2009 | * recursive bits for other formats here. | |
2010 | */ | |
c6744349 | 2011 | if (options->output_format & (DIFF_FORMAT_PATCH | |
74e2abe5 | 2012 | DIFF_FORMAT_NUMSTAT | |
c6744349 | 2013 | DIFF_FORMAT_DIFFSTAT | |
ebd124c6 | 2014 | DIFF_FORMAT_SHORTSTAT | |
d7014dc0 | 2015 | DIFF_FORMAT_SUMMARY | |
c6744349 | 2016 | DIFF_FORMAT_CHECKDIFF)) |
6973dcae | 2017 | options->recursive = 1; |
5e363541 | 2018 | /* |
3969cf7d | 2019 | * Also pickaxe would not work very well if you do not say recursive |
5e363541 | 2020 | */ |
3969cf7d JH |
2021 | if (options->pickaxe) |
2022 | options->recursive = 1; | |
5e363541 | 2023 | |
6973dcae JH |
2024 | if (options->detect_rename && options->rename_limit < 0) |
2025 | options->rename_limit = diff_rename_limit_default; | |
2026 | if (options->setup & DIFF_SETUP_USE_CACHE) { | |
2027 | if (!active_cache) | |
2028 | /* read-cache does not die even when it fails | |
2029 | * so it is safe for us to do this here. Also | |
2030 | * it does not smudge active_cache or active_nr | |
2031 | * when it fails, so we do not have to worry about | |
2032 | * cleaning it up ourselves either. | |
2033 | */ | |
2034 | read_cache(); | |
2035 | } | |
6973dcae JH |
2036 | if (options->abbrev <= 0 || 40 < options->abbrev) |
2037 | options->abbrev = 40; /* full */ | |
2038 | ||
68aacb2f JH |
2039 | /* |
2040 | * It does not make sense to show the first hit we happened | |
2041 | * to have found. It does not make sense not to return with | |
2042 | * exit code in such a case either. | |
2043 | */ | |
2044 | if (options->quiet) { | |
2045 | options->output_format = DIFF_FORMAT_NO_OUTPUT; | |
2046 | options->exit_with_status = 1; | |
2047 | } | |
2048 | ||
2049 | /* | |
2050 | * If we postprocess in diffcore, we cannot simply return | |
2051 | * upon the first hit. We need to run diff as usual. | |
2052 | */ | |
2053 | if (options->pickaxe || options->filter) | |
2054 | options->quiet = 0; | |
2055 | ||
6973dcae JH |
2056 | return 0; |
2057 | } | |
2058 | ||
d2543b8e | 2059 | static int opt_arg(const char *arg, int arg_short, const char *arg_long, int *val) |
ee1e5412 LT |
2060 | { |
2061 | char c, *eq; | |
2062 | int len; | |
2063 | ||
2064 | if (*arg != '-') | |
2065 | return 0; | |
2066 | c = *++arg; | |
2067 | if (!c) | |
2068 | return 0; | |
2069 | if (c == arg_short) { | |
2070 | c = *++arg; | |
2071 | if (!c) | |
2072 | return 1; | |
2073 | if (val && isdigit(c)) { | |
2074 | char *end; | |
2075 | int n = strtoul(arg, &end, 10); | |
2076 | if (*end) | |
2077 | return 0; | |
2078 | *val = n; | |
2079 | return 1; | |
2080 | } | |
2081 | return 0; | |
2082 | } | |
2083 | if (c != '-') | |
2084 | return 0; | |
2085 | arg++; | |
2086 | eq = strchr(arg, '='); | |
2087 | if (eq) | |
2088 | len = eq - arg; | |
2089 | else | |
2090 | len = strlen(arg); | |
2091 | if (!len || strncmp(arg, arg_long, len)) | |
2092 | return 0; | |
2093 | if (eq) { | |
2094 | int n; | |
2095 | char *end; | |
2096 | if (!isdigit(*++eq)) | |
2097 | return 0; | |
2098 | n = strtoul(eq, &end, 10); | |
2099 | if (*end) | |
2100 | return 0; | |
2101 | *val = n; | |
2102 | } | |
2103 | return 1; | |
2104 | } | |
2105 | ||
16befb8b JH |
2106 | static int diff_scoreopt_parse(const char *opt); |
2107 | ||
6973dcae JH |
2108 | int diff_opt_parse(struct diff_options *options, const char **av, int ac) |
2109 | { | |
2110 | const char *arg = av[0]; | |
2111 | if (!strcmp(arg, "-p") || !strcmp(arg, "-u")) | |
c6744349 | 2112 | options->output_format |= DIFF_FORMAT_PATCH; |
ee1e5412 | 2113 | else if (opt_arg(arg, 'U', "unified", &options->context)) |
c6744349 | 2114 | options->output_format |= DIFF_FORMAT_PATCH; |
a610786f TH |
2115 | else if (!strcmp(arg, "--raw")) |
2116 | options->output_format |= DIFF_FORMAT_RAW; | |
6973dcae | 2117 | else if (!strcmp(arg, "--patch-with-raw")) { |
c6744349 | 2118 | options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_RAW; |
6973dcae | 2119 | } |
74e2abe5 JH |
2120 | else if (!strcmp(arg, "--numstat")) { |
2121 | options->output_format |= DIFF_FORMAT_NUMSTAT; | |
2122 | } | |
ebd124c6 NP |
2123 | else if (!strcmp(arg, "--shortstat")) { |
2124 | options->output_format |= DIFF_FORMAT_SHORTSTAT; | |
2125 | } | |
cc44c765 | 2126 | else if (!prefixcmp(arg, "--stat")) { |
5c5b2ea9 LT |
2127 | char *end; |
2128 | int width = options->stat_width; | |
2129 | int name_width = options->stat_name_width; | |
2130 | arg += 6; | |
2131 | end = (char *)arg; | |
2132 | ||
2133 | switch (*arg) { | |
2134 | case '-': | |
cc44c765 | 2135 | if (!prefixcmp(arg, "-width=")) |
5c5b2ea9 | 2136 | width = strtoul(arg + 7, &end, 10); |
cc44c765 | 2137 | else if (!prefixcmp(arg, "-name-width=")) |
5c5b2ea9 LT |
2138 | name_width = strtoul(arg + 12, &end, 10); |
2139 | break; | |
2140 | case '=': | |
2141 | width = strtoul(arg+1, &end, 10); | |
2142 | if (*end == ',') | |
2143 | name_width = strtoul(end+1, &end, 10); | |
2144 | } | |
2145 | ||
2146 | /* Important! This checks all the error cases! */ | |
2147 | if (*end) | |
2148 | return 0; | |
c6744349 | 2149 | options->output_format |= DIFF_FORMAT_DIFFSTAT; |
5c5b2ea9 LT |
2150 | options->stat_name_width = name_width; |
2151 | options->stat_width = width; | |
a2540023 | 2152 | } |
88246898 | 2153 | else if (!strcmp(arg, "--check")) |
c6744349 | 2154 | options->output_format |= DIFF_FORMAT_CHECKDIFF; |
4bbd261b | 2155 | else if (!strcmp(arg, "--summary")) |
c6744349 | 2156 | options->output_format |= DIFF_FORMAT_SUMMARY; |
6973dcae | 2157 | else if (!strcmp(arg, "--patch-with-stat")) { |
c6744349 | 2158 | options->output_format |= DIFF_FORMAT_PATCH | DIFF_FORMAT_DIFFSTAT; |
6973dcae JH |
2159 | } |
2160 | else if (!strcmp(arg, "-z")) | |
2161 | options->line_termination = 0; | |
cc44c765 | 2162 | else if (!prefixcmp(arg, "-l")) |
6973dcae JH |
2163 | options->rename_limit = strtoul(arg+2, NULL, 10); |
2164 | else if (!strcmp(arg, "--full-index")) | |
2165 | options->full_index = 1; | |
0660626c | 2166 | else if (!strcmp(arg, "--binary")) { |
c6744349 | 2167 | options->output_format |= DIFF_FORMAT_PATCH; |
82793c55 | 2168 | options->binary = 1; |
0660626c | 2169 | } |
63ac4501 | 2170 | else if (!strcmp(arg, "-a") || !strcmp(arg, "--text")) { |
6d64ea96 SF |
2171 | options->text = 1; |
2172 | } | |
6973dcae | 2173 | else if (!strcmp(arg, "--name-only")) |
c6744349 | 2174 | options->output_format |= DIFF_FORMAT_NAME; |
6973dcae | 2175 | else if (!strcmp(arg, "--name-status")) |
c6744349 | 2176 | options->output_format |= DIFF_FORMAT_NAME_STATUS; |
6973dcae JH |
2177 | else if (!strcmp(arg, "-R")) |
2178 | options->reverse_diff = 1; | |
cc44c765 | 2179 | else if (!prefixcmp(arg, "-S")) |
6973dcae | 2180 | options->pickaxe = arg + 2; |
c6744349 TH |
2181 | else if (!strcmp(arg, "-s")) { |
2182 | options->output_format |= DIFF_FORMAT_NO_OUTPUT; | |
2183 | } | |
cc44c765 | 2184 | else if (!prefixcmp(arg, "-O")) |
6973dcae | 2185 | options->orderfile = arg + 2; |
cc44c765 | 2186 | else if (!prefixcmp(arg, "--diff-filter=")) |
6973dcae JH |
2187 | options->filter = arg + 14; |
2188 | else if (!strcmp(arg, "--pickaxe-all")) | |
2189 | options->pickaxe_opts = DIFF_PICKAXE_ALL; | |
2190 | else if (!strcmp(arg, "--pickaxe-regex")) | |
2191 | options->pickaxe_opts = DIFF_PICKAXE_REGEX; | |
cc44c765 | 2192 | else if (!prefixcmp(arg, "-B")) { |
6973dcae JH |
2193 | if ((options->break_opt = |
2194 | diff_scoreopt_parse(arg)) == -1) | |
2195 | return -1; | |
2196 | } | |
cc44c765 | 2197 | else if (!prefixcmp(arg, "-M")) { |
6973dcae JH |
2198 | if ((options->rename_score = |
2199 | diff_scoreopt_parse(arg)) == -1) | |
2200 | return -1; | |
2201 | options->detect_rename = DIFF_DETECT_RENAME; | |
2202 | } | |
cc44c765 | 2203 | else if (!prefixcmp(arg, "-C")) { |
ca6c0970 JS |
2204 | if (options->detect_rename == DIFF_DETECT_COPY) |
2205 | options->find_copies_harder = 1; | |
6973dcae JH |
2206 | if ((options->rename_score = |
2207 | diff_scoreopt_parse(arg)) == -1) | |
2208 | return -1; | |
2209 | options->detect_rename = DIFF_DETECT_COPY; | |
2210 | } | |
2211 | else if (!strcmp(arg, "--find-copies-harder")) | |
2212 | options->find_copies_harder = 1; | |
750f7b66 LT |
2213 | else if (!strcmp(arg, "--follow")) |
2214 | options->follow_renames = 1; | |
6973dcae JH |
2215 | else if (!strcmp(arg, "--abbrev")) |
2216 | options->abbrev = DEFAULT_ABBREV; | |
cc44c765 | 2217 | else if (!prefixcmp(arg, "--abbrev=")) { |
6973dcae JH |
2218 | options->abbrev = strtoul(arg + 9, NULL, 10); |
2219 | if (options->abbrev < MINIMUM_ABBREV) | |
2220 | options->abbrev = MINIMUM_ABBREV; | |
2221 | else if (40 < options->abbrev) | |
2222 | options->abbrev = 40; | |
2223 | } | |
cd112cef JS |
2224 | else if (!strcmp(arg, "--color")) |
2225 | options->color_diff = 1; | |
fef88bb0 JH |
2226 | else if (!strcmp(arg, "--no-color")) |
2227 | options->color_diff = 0; | |
0d21efa5 JS |
2228 | else if (!strcmp(arg, "-w") || !strcmp(arg, "--ignore-all-space")) |
2229 | options->xdl_opts |= XDF_IGNORE_WHITESPACE; | |
2230 | else if (!strcmp(arg, "-b") || !strcmp(arg, "--ignore-space-change")) | |
2231 | options->xdl_opts |= XDF_IGNORE_WHITESPACE_CHANGE; | |
859f9c45 JS |
2232 | else if (!strcmp(arg, "--ignore-space-at-eol")) |
2233 | options->xdl_opts |= XDF_IGNORE_WHITESPACE_AT_EOL; | |
f59a59e2 JS |
2234 | else if (!strcmp(arg, "--color-words")) |
2235 | options->color_diff = options->color_diff_words = 1; | |
b68ea12e EW |
2236 | else if (!strcmp(arg, "--no-renames")) |
2237 | options->detect_rename = 0; | |
41bbf9d5 AR |
2238 | else if (!strcmp(arg, "--exit-code")) |
2239 | options->exit_with_status = 1; | |
68aacb2f JH |
2240 | else if (!strcmp(arg, "--quiet")) |
2241 | options->quiet = 1; | |
6973dcae JH |
2242 | else |
2243 | return 0; | |
2244 | return 1; | |
2245 | } | |
2246 | ||
2247 | static int parse_num(const char **cp_p) | |
2248 | { | |
2249 | unsigned long num, scale; | |
2250 | int ch, dot; | |
2251 | const char *cp = *cp_p; | |
2252 | ||
2253 | num = 0; | |
2254 | scale = 1; | |
2255 | dot = 0; | |
2256 | for(;;) { | |
2257 | ch = *cp; | |
2258 | if ( !dot && ch == '.' ) { | |
2259 | scale = 1; | |
2260 | dot = 1; | |
2261 | } else if ( ch == '%' ) { | |
2262 | scale = dot ? scale*100 : 100; | |
2263 | cp++; /* % is always at the end */ | |
2264 | break; | |
2265 | } else if ( ch >= '0' && ch <= '9' ) { | |
2266 | if ( scale < 100000 ) { | |
2267 | scale *= 10; | |
2268 | num = (num*10) + (ch-'0'); | |
2269 | } | |
2270 | } else { | |
2271 | break; | |
2272 | } | |
2273 | cp++; | |
2274 | } | |
2275 | *cp_p = cp; | |
2276 | ||
2277 | /* user says num divided by scale and we say internally that | |
2278 | * is MAX_SCORE * num / scale. | |
2279 | */ | |
dc49cd76 | 2280 | return (int)((num >= scale) ? MAX_SCORE : (MAX_SCORE * num / scale)); |
6973dcae JH |
2281 | } |
2282 | ||
16befb8b | 2283 | static int diff_scoreopt_parse(const char *opt) |
6973dcae JH |
2284 | { |
2285 | int opt1, opt2, cmd; | |
2286 | ||
2287 | if (*opt++ != '-') | |
2288 | return -1; | |
2289 | cmd = *opt++; | |
2290 | if (cmd != 'M' && cmd != 'C' && cmd != 'B') | |
2291 | return -1; /* that is not a -M, -C nor -B option */ | |
2292 | ||
2293 | opt1 = parse_num(&opt); | |
2294 | if (cmd != 'B') | |
2295 | opt2 = 0; | |
2296 | else { | |
2297 | if (*opt == 0) | |
2298 | opt2 = 0; | |
2299 | else if (*opt != '/') | |
2300 | return -1; /* we expect -B80/99 or -B80 */ | |
2301 | else { | |
2302 | opt++; | |
2303 | opt2 = parse_num(&opt); | |
2304 | } | |
2305 | } | |
2306 | if (*opt != 0) | |
2307 | return -1; | |
2308 | return opt1 | (opt2 << 16); | |
2309 | } | |
2310 | ||
2311 | struct diff_queue_struct diff_queued_diff; | |
2312 | ||
2313 | void diff_q(struct diff_queue_struct *queue, struct diff_filepair *dp) | |
2314 | { | |
2315 | if (queue->alloc <= queue->nr) { | |
2316 | queue->alloc = alloc_nr(queue->alloc); | |
2317 | queue->queue = xrealloc(queue->queue, | |
2318 | sizeof(dp) * queue->alloc); | |
2319 | } | |
2320 | queue->queue[queue->nr++] = dp; | |
2321 | } | |
2322 | ||
2323 | struct diff_filepair *diff_queue(struct diff_queue_struct *queue, | |
2324 | struct diff_filespec *one, | |
2325 | struct diff_filespec *two) | |
2326 | { | |
ef677686 | 2327 | struct diff_filepair *dp = xcalloc(1, sizeof(*dp)); |
6973dcae JH |
2328 | dp->one = one; |
2329 | dp->two = two; | |
6973dcae JH |
2330 | if (queue) |
2331 | diff_q(queue, dp); | |
2332 | return dp; | |
2333 | } | |
2334 | ||
2335 | void diff_free_filepair(struct diff_filepair *p) | |
2336 | { | |
2337 | diff_free_filespec_data(p->one); | |
2338 | diff_free_filespec_data(p->two); | |
2339 | free(p->one); | |
2340 | free(p->two); | |
2341 | free(p); | |
2342 | } | |
2343 | ||
2344 | /* This is different from find_unique_abbrev() in that | |
2345 | * it stuffs the result with dots for alignment. | |
2346 | */ | |
2347 | const char *diff_unique_abbrev(const unsigned char *sha1, int len) | |
2348 | { | |
2349 | int abblen; | |
2350 | const char *abbrev; | |
2351 | if (len == 40) | |
2352 | return sha1_to_hex(sha1); | |
2353 | ||
2354 | abbrev = find_unique_abbrev(sha1, len); | |
2355 | if (!abbrev) | |
2356 | return sha1_to_hex(sha1); | |
2357 | abblen = strlen(abbrev); | |
2358 | if (abblen < 37) { | |
2359 | static char hex[41]; | |
2360 | if (len < abblen && abblen <= len + 2) | |
2361 | sprintf(hex, "%s%.*s", abbrev, len+3-abblen, ".."); | |
2362 | else | |
2363 | sprintf(hex, "%s...", abbrev); | |
2364 | return hex; | |
2365 | } | |
2366 | return sha1_to_hex(sha1); | |
2367 | } | |
2368 | ||
2369 | static void diff_flush_raw(struct diff_filepair *p, | |
c6744349 | 2370 | struct diff_options *options) |
6973dcae JH |
2371 | { |
2372 | int two_paths; | |
2373 | char status[10]; | |
2374 | int abbrev = options->abbrev; | |
2375 | const char *path_one, *path_two; | |
c6744349 TH |
2376 | int inter_name_termination = '\t'; |
2377 | int line_termination = options->line_termination; | |
2378 | ||
2379 | if (!line_termination) | |
2380 | inter_name_termination = 0; | |
6973dcae JH |
2381 | |
2382 | path_one = p->one->path; | |
2383 | path_two = p->two->path; | |
2384 | if (line_termination) { | |
2385 | path_one = quote_one(path_one); | |
2386 | path_two = quote_one(path_two); | |
2387 | } | |
2388 | ||
2389 | if (p->score) | |
2390 | sprintf(status, "%c%03d", p->status, | |
2391 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); | |
2392 | else { | |
2393 | status[0] = p->status; | |
2394 | status[1] = 0; | |
2395 | } | |
2396 | switch (p->status) { | |
2397 | case DIFF_STATUS_COPIED: | |
2398 | case DIFF_STATUS_RENAMED: | |
2399 | two_paths = 1; | |
2400 | break; | |
2401 | case DIFF_STATUS_ADDED: | |
2402 | case DIFF_STATUS_DELETED: | |
2403 | two_paths = 0; | |
2404 | break; | |
2405 | default: | |
2406 | two_paths = 0; | |
2407 | break; | |
2408 | } | |
c6744349 | 2409 | if (!(options->output_format & DIFF_FORMAT_NAME_STATUS)) { |
6973dcae JH |
2410 | printf(":%06o %06o %s ", |
2411 | p->one->mode, p->two->mode, | |
2412 | diff_unique_abbrev(p->one->sha1, abbrev)); | |
2413 | printf("%s ", | |
2414 | diff_unique_abbrev(p->two->sha1, abbrev)); | |
2415 | } | |
2416 | printf("%s%c%s", status, inter_name_termination, path_one); | |
2417 | if (two_paths) | |
2418 | printf("%c%s", inter_name_termination, path_two); | |
2419 | putchar(line_termination); | |
2420 | if (path_one != p->one->path) | |
2421 | free((void*)path_one); | |
2422 | if (path_two != p->two->path) | |
2423 | free((void*)path_two); | |
2424 | } | |
2425 | ||
471efb09 | 2426 | static void diff_flush_name(struct diff_filepair *p, struct diff_options *opt) |
6973dcae JH |
2427 | { |
2428 | char *path = p->two->path; | |
2429 | ||
471efb09 | 2430 | if (opt->line_termination) |
6973dcae | 2431 | path = quote_one(p->two->path); |
471efb09 | 2432 | printf("%s%c", path, opt->line_termination); |
6973dcae JH |
2433 | if (p->two->path != path) |
2434 | free(path); | |
2435 | } | |
2436 | ||
2437 | int diff_unmodified_pair(struct diff_filepair *p) | |
2438 | { | |
2439 | /* This function is written stricter than necessary to support | |
2440 | * the currently implemented transformers, but the idea is to | |
2441 | * let transformers to produce diff_filepairs any way they want, | |
2442 | * and filter and clean them up here before producing the output. | |
2443 | */ | |
2444 | struct diff_filespec *one, *two; | |
2445 | ||
2446 | if (DIFF_PAIR_UNMERGED(p)) | |
2447 | return 0; /* unmerged is interesting */ | |
2448 | ||
2449 | one = p->one; | |
2450 | two = p->two; | |
2451 | ||
2452 | /* deletion, addition, mode or type change | |
2453 | * and rename are all interesting. | |
2454 | */ | |
2455 | if (DIFF_FILE_VALID(one) != DIFF_FILE_VALID(two) || | |
2456 | DIFF_PAIR_MODE_CHANGED(p) || | |
2457 | strcmp(one->path, two->path)) | |
2458 | return 0; | |
2459 | ||
2460 | /* both are valid and point at the same path. that is, we are | |
2461 | * dealing with a change. | |
2462 | */ | |
2463 | if (one->sha1_valid && two->sha1_valid && | |
a89fccd2 | 2464 | !hashcmp(one->sha1, two->sha1)) |
6973dcae JH |
2465 | return 1; /* no change */ |
2466 | if (!one->sha1_valid && !two->sha1_valid) | |
2467 | return 1; /* both look at the same file on the filesystem. */ | |
2468 | return 0; | |
2469 | } | |
2470 | ||
2471 | static void diff_flush_patch(struct diff_filepair *p, struct diff_options *o) | |
2472 | { | |
2473 | if (diff_unmodified_pair(p)) | |
2474 | return; | |
2475 | ||
2476 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
2477 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
2478 | return; /* no tree diffs in patch format */ | |
2479 | ||
2480 | run_diff(p, o); | |
2481 | } | |
2482 | ||
2483 | static void diff_flush_stat(struct diff_filepair *p, struct diff_options *o, | |
2484 | struct diffstat_t *diffstat) | |
2485 | { | |
2486 | if (diff_unmodified_pair(p)) | |
2487 | return; | |
2488 | ||
2489 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
2490 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
2491 | return; /* no tree diffs in patch format */ | |
2492 | ||
2493 | run_diffstat(p, o, diffstat); | |
2494 | } | |
2495 | ||
88246898 JS |
2496 | static void diff_flush_checkdiff(struct diff_filepair *p, |
2497 | struct diff_options *o) | |
2498 | { | |
2499 | if (diff_unmodified_pair(p)) | |
2500 | return; | |
2501 | ||
2502 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
2503 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
2504 | return; /* no tree diffs in patch format */ | |
2505 | ||
2506 | run_checkdiff(p, o); | |
2507 | } | |
2508 | ||
6973dcae JH |
2509 | int diff_queue_is_empty(void) |
2510 | { | |
2511 | struct diff_queue_struct *q = &diff_queued_diff; | |
2512 | int i; | |
2513 | for (i = 0; i < q->nr; i++) | |
2514 | if (!diff_unmodified_pair(q->queue[i])) | |
2515 | return 0; | |
2516 | return 1; | |
2517 | } | |
2518 | ||
2519 | #if DIFF_DEBUG | |
2520 | void diff_debug_filespec(struct diff_filespec *s, int x, const char *one) | |
2521 | { | |
2522 | fprintf(stderr, "queue[%d] %s (%s) %s %06o %s\n", | |
2523 | x, one ? one : "", | |
2524 | s->path, | |
2525 | DIFF_FILE_VALID(s) ? "valid" : "invalid", | |
2526 | s->mode, | |
2527 | s->sha1_valid ? sha1_to_hex(s->sha1) : ""); | |
2528 | fprintf(stderr, "queue[%d] %s size %lu flags %d\n", | |
2529 | x, one ? one : "", | |
2530 | s->size, s->xfrm_flags); | |
2531 | } | |
2532 | ||
2533 | void diff_debug_filepair(const struct diff_filepair *p, int i) | |
2534 | { | |
2535 | diff_debug_filespec(p->one, i, "one"); | |
2536 | diff_debug_filespec(p->two, i, "two"); | |
2537 | fprintf(stderr, "score %d, status %c stays %d broken %d\n", | |
2538 | p->score, p->status ? p->status : '?', | |
2539 | p->source_stays, p->broken_pair); | |
2540 | } | |
2541 | ||
2542 | void diff_debug_queue(const char *msg, struct diff_queue_struct *q) | |
2543 | { | |
2544 | int i; | |
2545 | if (msg) | |
2546 | fprintf(stderr, "%s\n", msg); | |
2547 | fprintf(stderr, "q->nr = %d\n", q->nr); | |
2548 | for (i = 0; i < q->nr; i++) { | |
2549 | struct diff_filepair *p = q->queue[i]; | |
2550 | diff_debug_filepair(p, i); | |
2551 | } | |
2552 | } | |
2553 | #endif | |
2554 | ||
2555 | static void diff_resolve_rename_copy(void) | |
2556 | { | |
2557 | int i, j; | |
2558 | struct diff_filepair *p, *pp; | |
2559 | struct diff_queue_struct *q = &diff_queued_diff; | |
2560 | ||
2561 | diff_debug_queue("resolve-rename-copy", q); | |
2562 | ||
2563 | for (i = 0; i < q->nr; i++) { | |
2564 | p = q->queue[i]; | |
2565 | p->status = 0; /* undecided */ | |
2566 | if (DIFF_PAIR_UNMERGED(p)) | |
2567 | p->status = DIFF_STATUS_UNMERGED; | |
2568 | else if (!DIFF_FILE_VALID(p->one)) | |
2569 | p->status = DIFF_STATUS_ADDED; | |
2570 | else if (!DIFF_FILE_VALID(p->two)) | |
2571 | p->status = DIFF_STATUS_DELETED; | |
2572 | else if (DIFF_PAIR_TYPE_CHANGED(p)) | |
2573 | p->status = DIFF_STATUS_TYPE_CHANGED; | |
2574 | ||
2575 | /* from this point on, we are dealing with a pair | |
2576 | * whose both sides are valid and of the same type, i.e. | |
2577 | * either in-place edit or rename/copy edit. | |
2578 | */ | |
2579 | else if (DIFF_PAIR_RENAME(p)) { | |
2580 | if (p->source_stays) { | |
2581 | p->status = DIFF_STATUS_COPIED; | |
2582 | continue; | |
2583 | } | |
2584 | /* See if there is some other filepair that | |
2585 | * copies from the same source as us. If so | |
2586 | * we are a copy. Otherwise we are either a | |
2587 | * copy if the path stays, or a rename if it | |
2588 | * does not, but we already handled "stays" case. | |
2589 | */ | |
2590 | for (j = i + 1; j < q->nr; j++) { | |
2591 | pp = q->queue[j]; | |
2592 | if (strcmp(pp->one->path, p->one->path)) | |
2593 | continue; /* not us */ | |
2594 | if (!DIFF_PAIR_RENAME(pp)) | |
2595 | continue; /* not a rename/copy */ | |
2596 | /* pp is a rename/copy from the same source */ | |
2597 | p->status = DIFF_STATUS_COPIED; | |
2598 | break; | |
2599 | } | |
2600 | if (!p->status) | |
2601 | p->status = DIFF_STATUS_RENAMED; | |
2602 | } | |
a89fccd2 | 2603 | else if (hashcmp(p->one->sha1, p->two->sha1) || |
d516c2d1 JS |
2604 | p->one->mode != p->two->mode || |
2605 | is_null_sha1(p->one->sha1)) | |
6973dcae JH |
2606 | p->status = DIFF_STATUS_MODIFIED; |
2607 | else { | |
2608 | /* This is a "no-change" entry and should not | |
2609 | * happen anymore, but prepare for broken callers. | |
2610 | */ | |
2611 | error("feeding unmodified %s to diffcore", | |
2612 | p->one->path); | |
2613 | p->status = DIFF_STATUS_UNKNOWN; | |
2614 | } | |
2615 | } | |
2616 | diff_debug_queue("resolve-rename-copy done", q); | |
2617 | } | |
2618 | ||
c6744349 | 2619 | static int check_pair_status(struct diff_filepair *p) |
6973dcae | 2620 | { |
6973dcae JH |
2621 | switch (p->status) { |
2622 | case DIFF_STATUS_UNKNOWN: | |
c6744349 | 2623 | return 0; |
6973dcae JH |
2624 | case 0: |
2625 | die("internal error in diff-resolve-rename-copy"); | |
6973dcae | 2626 | default: |
c6744349 | 2627 | return 1; |
6973dcae JH |
2628 | } |
2629 | } | |
2630 | ||
c6744349 TH |
2631 | static void flush_one_pair(struct diff_filepair *p, struct diff_options *opt) |
2632 | { | |
2633 | int fmt = opt->output_format; | |
2634 | ||
2635 | if (fmt & DIFF_FORMAT_CHECKDIFF) | |
2636 | diff_flush_checkdiff(p, opt); | |
2637 | else if (fmt & (DIFF_FORMAT_RAW | DIFF_FORMAT_NAME_STATUS)) | |
2638 | diff_flush_raw(p, opt); | |
2639 | else if (fmt & DIFF_FORMAT_NAME) | |
471efb09 | 2640 | diff_flush_name(p, opt); |
c6744349 TH |
2641 | } |
2642 | ||
4bbd261b SE |
2643 | static void show_file_mode_name(const char *newdelete, struct diff_filespec *fs) |
2644 | { | |
0d26a64e | 2645 | char *name = quote_one(fs->path); |
4bbd261b | 2646 | if (fs->mode) |
0d26a64e | 2647 | printf(" %s mode %06o %s\n", newdelete, fs->mode, name); |
4bbd261b | 2648 | else |
0d26a64e AJ |
2649 | printf(" %s %s\n", newdelete, name); |
2650 | free(name); | |
4bbd261b SE |
2651 | } |
2652 | ||
2653 | ||
2654 | static void show_mode_change(struct diff_filepair *p, int show_name) | |
2655 | { | |
2656 | if (p->one->mode && p->two->mode && p->one->mode != p->two->mode) { | |
0d26a64e AJ |
2657 | if (show_name) { |
2658 | char *name = quote_one(p->two->path); | |
4bbd261b | 2659 | printf(" mode change %06o => %06o %s\n", |
0d26a64e AJ |
2660 | p->one->mode, p->two->mode, name); |
2661 | free(name); | |
2662 | } | |
4bbd261b SE |
2663 | else |
2664 | printf(" mode change %06o => %06o\n", | |
2665 | p->one->mode, p->two->mode); | |
2666 | } | |
2667 | } | |
2668 | ||
2669 | static void show_rename_copy(const char *renamecopy, struct diff_filepair *p) | |
2670 | { | |
b9f44164 | 2671 | char *names = pprint_rename(p->one->path, p->two->path); |
4bbd261b | 2672 | |
b9f44164 AJ |
2673 | printf(" %s %s (%d%%)\n", renamecopy, names, |
2674 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); | |
2675 | free(names); | |
4bbd261b SE |
2676 | show_mode_change(p, 0); |
2677 | } | |
2678 | ||
2679 | static void diff_summary(struct diff_filepair *p) | |
2680 | { | |
2681 | switch(p->status) { | |
2682 | case DIFF_STATUS_DELETED: | |
2683 | show_file_mode_name("delete", p->one); | |
2684 | break; | |
2685 | case DIFF_STATUS_ADDED: | |
2686 | show_file_mode_name("create", p->two); | |
2687 | break; | |
2688 | case DIFF_STATUS_COPIED: | |
2689 | show_rename_copy("copy", p); | |
2690 | break; | |
2691 | case DIFF_STATUS_RENAMED: | |
2692 | show_rename_copy("rename", p); | |
2693 | break; | |
2694 | default: | |
2695 | if (p->score) { | |
0d26a64e AJ |
2696 | char *name = quote_one(p->two->path); |
2697 | printf(" rewrite %s (%d%%)\n", name, | |
4bbd261b | 2698 | (int)(0.5 + p->score * 100.0/MAX_SCORE)); |
0d26a64e | 2699 | free(name); |
4bbd261b SE |
2700 | show_mode_change(p, 0); |
2701 | } else show_mode_change(p, 1); | |
2702 | break; | |
2703 | } | |
2704 | } | |
2705 | ||
fcb3d0ad JS |
2706 | struct patch_id_t { |
2707 | struct xdiff_emit_state xm; | |
2708 | SHA_CTX *ctx; | |
2709 | int patchlen; | |
2710 | }; | |
2711 | ||
2712 | static int remove_space(char *line, int len) | |
2713 | { | |
2714 | int i; | |
2715 | char *dst = line; | |
2716 | unsigned char c; | |
2717 | ||
2718 | for (i = 0; i < len; i++) | |
2719 | if (!isspace((c = line[i]))) | |
2720 | *dst++ = c; | |
2721 | ||
2722 | return dst - line; | |
2723 | } | |
2724 | ||
2725 | static void patch_id_consume(void *priv, char *line, unsigned long len) | |
2726 | { | |
2727 | struct patch_id_t *data = priv; | |
2728 | int new_len; | |
2729 | ||
2730 | /* Ignore line numbers when computing the SHA1 of the patch */ | |
cc44c765 | 2731 | if (!prefixcmp(line, "@@ -")) |
fcb3d0ad JS |
2732 | return; |
2733 | ||
2734 | new_len = remove_space(line, len); | |
2735 | ||
2736 | SHA1_Update(data->ctx, line, new_len); | |
2737 | data->patchlen += new_len; | |
2738 | } | |
2739 | ||
2740 | /* returns 0 upon success, and writes result into sha1 */ | |
2741 | static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1) | |
2742 | { | |
2743 | struct diff_queue_struct *q = &diff_queued_diff; | |
2744 | int i; | |
2745 | SHA_CTX ctx; | |
2746 | struct patch_id_t data; | |
2747 | char buffer[PATH_MAX * 4 + 20]; | |
2748 | ||
2749 | SHA1_Init(&ctx); | |
2750 | memset(&data, 0, sizeof(struct patch_id_t)); | |
2751 | data.ctx = &ctx; | |
2752 | data.xm.consume = patch_id_consume; | |
2753 | ||
2754 | for (i = 0; i < q->nr; i++) { | |
2755 | xpparam_t xpp; | |
2756 | xdemitconf_t xecfg; | |
2757 | xdemitcb_t ecb; | |
2758 | mmfile_t mf1, mf2; | |
2759 | struct diff_filepair *p = q->queue[i]; | |
2760 | int len1, len2; | |
2761 | ||
2762 | if (p->status == 0) | |
2763 | return error("internal diff status error"); | |
2764 | if (p->status == DIFF_STATUS_UNKNOWN) | |
2765 | continue; | |
2766 | if (diff_unmodified_pair(p)) | |
2767 | continue; | |
2768 | if ((DIFF_FILE_VALID(p->one) && S_ISDIR(p->one->mode)) || | |
2769 | (DIFF_FILE_VALID(p->two) && S_ISDIR(p->two->mode))) | |
2770 | continue; | |
2771 | if (DIFF_PAIR_UNMERGED(p)) | |
2772 | continue; | |
2773 | ||
2774 | diff_fill_sha1_info(p->one); | |
2775 | diff_fill_sha1_info(p->two); | |
2776 | if (fill_mmfile(&mf1, p->one) < 0 || | |
2777 | fill_mmfile(&mf2, p->two) < 0) | |
2778 | return error("unable to read files to diff"); | |
2779 | ||
2780 | /* Maybe hash p->two? into the patch id? */ | |
8c701249 | 2781 | if (file_is_binary(p->two)) |
fcb3d0ad JS |
2782 | continue; |
2783 | ||
2784 | len1 = remove_space(p->one->path, strlen(p->one->path)); | |
2785 | len2 = remove_space(p->two->path, strlen(p->two->path)); | |
2786 | if (p->one->mode == 0) | |
2787 | len1 = snprintf(buffer, sizeof(buffer), | |
2788 | "diff--gita/%.*sb/%.*s" | |
2789 | "newfilemode%06o" | |
2790 | "---/dev/null" | |
2791 | "+++b/%.*s", | |
2792 | len1, p->one->path, | |
2793 | len2, p->two->path, | |
2794 | p->two->mode, | |
2795 | len2, p->two->path); | |
2796 | else if (p->two->mode == 0) | |
2797 | len1 = snprintf(buffer, sizeof(buffer), | |
2798 | "diff--gita/%.*sb/%.*s" | |
2799 | "deletedfilemode%06o" | |
2800 | "---a/%.*s" | |
2801 | "+++/dev/null", | |
2802 | len1, p->one->path, | |
2803 | len2, p->two->path, | |
2804 | p->one->mode, | |
2805 | len1, p->one->path); | |
2806 | else | |
2807 | len1 = snprintf(buffer, sizeof(buffer), | |
2808 | "diff--gita/%.*sb/%.*s" | |
2809 | "---a/%.*s" | |
2810 | "+++b/%.*s", | |
2811 | len1, p->one->path, | |
2812 | len2, p->two->path, | |
2813 | len1, p->one->path, | |
2814 | len2, p->two->path); | |
2815 | SHA1_Update(&ctx, buffer, len1); | |
2816 | ||
2817 | xpp.flags = XDF_NEED_MINIMAL; | |
2818 | xecfg.ctxlen = 3; | |
9fdc3bb5 | 2819 | xecfg.flags = XDL_EMIT_FUNCNAMES; |
fcb3d0ad JS |
2820 | ecb.outf = xdiff_outf; |
2821 | ecb.priv = &data; | |
2822 | xdl_diff(&mf1, &mf2, &xpp, &xecfg, &ecb); | |
2823 | } | |
2824 | ||
2825 | SHA1_Final(sha1, &ctx); | |
2826 | return 0; | |
2827 | } | |
2828 | ||
2829 | int diff_flush_patch_id(struct diff_options *options, unsigned char *sha1) | |
2830 | { | |
2831 | struct diff_queue_struct *q = &diff_queued_diff; | |
2832 | int i; | |
2833 | int result = diff_get_patch_id(options, sha1); | |
2834 | ||
2835 | for (i = 0; i < q->nr; i++) | |
2836 | diff_free_filepair(q->queue[i]); | |
2837 | ||
2838 | free(q->queue); | |
2839 | q->queue = NULL; | |
2840 | q->nr = q->alloc = 0; | |
2841 | ||
2842 | return result; | |
2843 | } | |
2844 | ||
946c3784 | 2845 | static int is_summary_empty(const struct diff_queue_struct *q) |
6973dcae | 2846 | { |
6973dcae | 2847 | int i; |
6973dcae | 2848 | |
946c3784 TH |
2849 | for (i = 0; i < q->nr; i++) { |
2850 | const struct diff_filepair *p = q->queue[i]; | |
2851 | ||
2852 | switch (p->status) { | |
2853 | case DIFF_STATUS_DELETED: | |
2854 | case DIFF_STATUS_ADDED: | |
2855 | case DIFF_STATUS_COPIED: | |
2856 | case DIFF_STATUS_RENAMED: | |
2857 | return 0; | |
2858 | default: | |
2859 | if (p->score) | |
2860 | return 0; | |
2861 | if (p->one->mode && p->two->mode && | |
2862 | p->one->mode != p->two->mode) | |
2863 | return 0; | |
2864 | break; | |
2865 | } | |
6973dcae | 2866 | } |
946c3784 TH |
2867 | return 1; |
2868 | } | |
2869 | ||
6973dcae JH |
2870 | void diff_flush(struct diff_options *options) |
2871 | { | |
2872 | struct diff_queue_struct *q = &diff_queued_diff; | |
c6744349 | 2873 | int i, output_format = options->output_format; |
946c3784 | 2874 | int separator = 0; |
6973dcae | 2875 | |
c6744349 TH |
2876 | /* |
2877 | * Order: raw, stat, summary, patch | |
2878 | * or: name/name-status/checkdiff (other bits clear) | |
2879 | */ | |
946c3784 TH |
2880 | if (!q->nr) |
2881 | goto free_queue; | |
6973dcae | 2882 | |
c6744349 TH |
2883 | if (output_format & (DIFF_FORMAT_RAW | |
2884 | DIFF_FORMAT_NAME | | |
2885 | DIFF_FORMAT_NAME_STATUS | | |
2886 | DIFF_FORMAT_CHECKDIFF)) { | |
6973dcae JH |
2887 | for (i = 0; i < q->nr; i++) { |
2888 | struct diff_filepair *p = q->queue[i]; | |
c6744349 TH |
2889 | if (check_pair_status(p)) |
2890 | flush_one_pair(p, options); | |
6973dcae | 2891 | } |
946c3784 | 2892 | separator++; |
6973dcae | 2893 | } |
c6744349 | 2894 | |
ebd124c6 | 2895 | if (output_format & (DIFF_FORMAT_DIFFSTAT|DIFF_FORMAT_SHORTSTAT|DIFF_FORMAT_NUMSTAT)) { |
5e2b0636 | 2896 | struct diffstat_t diffstat; |
c6744349 | 2897 | |
5e2b0636 TH |
2898 | memset(&diffstat, 0, sizeof(struct diffstat_t)); |
2899 | diffstat.xm.consume = diffstat_consume; | |
6973dcae JH |
2900 | for (i = 0; i < q->nr; i++) { |
2901 | struct diff_filepair *p = q->queue[i]; | |
c6744349 | 2902 | if (check_pair_status(p)) |
5e2b0636 | 2903 | diff_flush_stat(p, options, &diffstat); |
6973dcae | 2904 | } |
74e2abe5 JH |
2905 | if (output_format & DIFF_FORMAT_NUMSTAT) |
2906 | show_numstat(&diffstat, options); | |
2907 | if (output_format & DIFF_FORMAT_DIFFSTAT) | |
2908 | show_stats(&diffstat, options); | |
ebd124c6 NP |
2909 | else if (output_format & DIFF_FORMAT_SHORTSTAT) |
2910 | show_shortstats(&diffstat); | |
3969cf7d | 2911 | separator++; |
6973dcae JH |
2912 | } |
2913 | ||
946c3784 | 2914 | if (output_format & DIFF_FORMAT_SUMMARY && !is_summary_empty(q)) { |
c6744349 TH |
2915 | for (i = 0; i < q->nr; i++) |
2916 | diff_summary(q->queue[i]); | |
3969cf7d | 2917 | separator++; |
6973dcae JH |
2918 | } |
2919 | ||
c6744349 | 2920 | if (output_format & DIFF_FORMAT_PATCH) { |
946c3784 TH |
2921 | if (separator) { |
2922 | if (options->stat_sep) { | |
2923 | /* attach patch instead of inline */ | |
c6744349 | 2924 | fputs(options->stat_sep, stdout); |
946c3784 | 2925 | } else { |
c6744349 | 2926 | putchar(options->line_termination); |
946c3784 | 2927 | } |
c6744349 TH |
2928 | } |
2929 | ||
2930 | for (i = 0; i < q->nr; i++) { | |
2931 | struct diff_filepair *p = q->queue[i]; | |
2932 | if (check_pair_status(p)) | |
2933 | diff_flush_patch(p, options); | |
2934 | } | |
4bbd261b SE |
2935 | } |
2936 | ||
04245581 JK |
2937 | if (output_format & DIFF_FORMAT_CALLBACK) |
2938 | options->format_callback(q, options, options->format_callback_data); | |
2939 | ||
c6744349 TH |
2940 | for (i = 0; i < q->nr; i++) |
2941 | diff_free_filepair(q->queue[i]); | |
946c3784 | 2942 | free_queue: |
6973dcae JH |
2943 | free(q->queue); |
2944 | q->queue = NULL; | |
2945 | q->nr = q->alloc = 0; | |
2946 | } | |
2947 | ||
2948 | static void diffcore_apply_filter(const char *filter) | |
2949 | { | |
2950 | int i; | |
2951 | struct diff_queue_struct *q = &diff_queued_diff; | |
2952 | struct diff_queue_struct outq; | |
2953 | outq.queue = NULL; | |
2954 | outq.nr = outq.alloc = 0; | |
2955 | ||
2956 | if (!filter) | |
2957 | return; | |
2958 | ||
2959 | if (strchr(filter, DIFF_STATUS_FILTER_AON)) { | |
2960 | int found; | |
2961 | for (i = found = 0; !found && i < q->nr; i++) { | |
2962 | struct diff_filepair *p = q->queue[i]; | |
2963 | if (((p->status == DIFF_STATUS_MODIFIED) && | |
2964 | ((p->score && | |
2965 | strchr(filter, DIFF_STATUS_FILTER_BROKEN)) || | |
2966 | (!p->score && | |
2967 | strchr(filter, DIFF_STATUS_MODIFIED)))) || | |
2968 | ((p->status != DIFF_STATUS_MODIFIED) && | |
2969 | strchr(filter, p->status))) | |
2970 | found++; | |
2971 | } | |
2972 | if (found) | |
2973 | return; | |
2974 | ||
2975 | /* otherwise we will clear the whole queue | |
2976 | * by copying the empty outq at the end of this | |
2977 | * function, but first clear the current entries | |
2978 | * in the queue. | |
2979 | */ | |
2980 | for (i = 0; i < q->nr; i++) | |
2981 | diff_free_filepair(q->queue[i]); | |
2982 | } | |
2983 | else { | |
2984 | /* Only the matching ones */ | |
2985 | for (i = 0; i < q->nr; i++) { | |
2986 | struct diff_filepair *p = q->queue[i]; | |
2987 | ||
2988 | if (((p->status == DIFF_STATUS_MODIFIED) && | |
2989 | ((p->score && | |
2990 | strchr(filter, DIFF_STATUS_FILTER_BROKEN)) || | |
2991 | (!p->score && | |
2992 | strchr(filter, DIFF_STATUS_MODIFIED)))) || | |
2993 | ((p->status != DIFF_STATUS_MODIFIED) && | |
2994 | strchr(filter, p->status))) | |
2995 | diff_q(&outq, p); | |
2996 | else | |
2997 | diff_free_filepair(p); | |
2998 | } | |
2999 | } | |
3000 | free(q->queue); | |
3001 | *q = outq; | |
3002 | } | |
3003 | ||
3004 | void diffcore_std(struct diff_options *options) | |
3005 | { | |
68aacb2f JH |
3006 | if (options->quiet) |
3007 | return; | |
6973dcae JH |
3008 | if (options->break_opt != -1) |
3009 | diffcore_break(options->break_opt); | |
3010 | if (options->detect_rename) | |
3011 | diffcore_rename(options); | |
3012 | if (options->break_opt != -1) | |
3013 | diffcore_merge_broken(); | |
3014 | if (options->pickaxe) | |
3015 | diffcore_pickaxe(options->pickaxe, options->pickaxe_opts); | |
3016 | if (options->orderfile) | |
3017 | diffcore_order(options->orderfile); | |
3018 | diff_resolve_rename_copy(); | |
3019 | diffcore_apply_filter(options->filter); | |
68aacb2f JH |
3020 | |
3021 | options->has_changes = !!diff_queued_diff.nr; | |
6973dcae JH |
3022 | } |
3023 | ||
3024 | ||
6973dcae JH |
3025 | void diff_addremove(struct diff_options *options, |
3026 | int addremove, unsigned mode, | |
3027 | const unsigned char *sha1, | |
3028 | const char *base, const char *path) | |
3029 | { | |
3030 | char concatpath[PATH_MAX]; | |
3031 | struct diff_filespec *one, *two; | |
3032 | ||
3033 | /* This may look odd, but it is a preparation for | |
3034 | * feeding "there are unchanged files which should | |
3035 | * not produce diffs, but when you are doing copy | |
3036 | * detection you would need them, so here they are" | |
3037 | * entries to the diff-core. They will be prefixed | |
3038 | * with something like '=' or '*' (I haven't decided | |
3039 | * which but should not make any difference). | |
a6080a0a | 3040 | * Feeding the same new and old to diff_change() |
6973dcae JH |
3041 | * also has the same effect. |
3042 | * Before the final output happens, they are pruned after | |
3043 | * merged into rename/copy pairs as appropriate. | |
3044 | */ | |
3045 | if (options->reverse_diff) | |
3046 | addremove = (addremove == '+' ? '-' : | |
3047 | addremove == '-' ? '+' : addremove); | |
3048 | ||
3049 | if (!path) path = ""; | |
3050 | sprintf(concatpath, "%s%s", base, path); | |
3051 | one = alloc_filespec(concatpath); | |
3052 | two = alloc_filespec(concatpath); | |
3053 | ||
3054 | if (addremove != '+') | |
3055 | fill_filespec(one, sha1, mode); | |
3056 | if (addremove != '-') | |
3057 | fill_filespec(two, sha1, mode); | |
3058 | ||
3059 | diff_queue(&diff_queued_diff, one, two); | |
68aacb2f | 3060 | options->has_changes = 1; |
6973dcae JH |
3061 | } |
3062 | ||
3063 | void diff_change(struct diff_options *options, | |
3064 | unsigned old_mode, unsigned new_mode, | |
3065 | const unsigned char *old_sha1, | |
3066 | const unsigned char *new_sha1, | |
a6080a0a | 3067 | const char *base, const char *path) |
6973dcae JH |
3068 | { |
3069 | char concatpath[PATH_MAX]; | |
3070 | struct diff_filespec *one, *two; | |
3071 | ||
3072 | if (options->reverse_diff) { | |
3073 | unsigned tmp; | |
3074 | const unsigned char *tmp_c; | |
3075 | tmp = old_mode; old_mode = new_mode; new_mode = tmp; | |
3076 | tmp_c = old_sha1; old_sha1 = new_sha1; new_sha1 = tmp_c; | |
3077 | } | |
3078 | if (!path) path = ""; | |
3079 | sprintf(concatpath, "%s%s", base, path); | |
3080 | one = alloc_filespec(concatpath); | |
3081 | two = alloc_filespec(concatpath); | |
3082 | fill_filespec(one, old_sha1, old_mode); | |
3083 | fill_filespec(two, new_sha1, new_mode); | |
3084 | ||
3085 | diff_queue(&diff_queued_diff, one, two); | |
68aacb2f | 3086 | options->has_changes = 1; |
6973dcae JH |
3087 | } |
3088 | ||
3089 | void diff_unmerge(struct diff_options *options, | |
e9c84099 JH |
3090 | const char *path, |
3091 | unsigned mode, const unsigned char *sha1) | |
6973dcae JH |
3092 | { |
3093 | struct diff_filespec *one, *two; | |
3094 | one = alloc_filespec(path); | |
3095 | two = alloc_filespec(path); | |
e9c84099 JH |
3096 | fill_filespec(one, sha1, mode); |
3097 | diff_queue(&diff_queued_diff, one, two)->is_unmerged = 1; | |
6973dcae | 3098 | } |