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