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