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