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