2 * Copyright (C) 2005 Junio C Hamano
11 #include "xdiff-interface.h"
14 #include "run-command.h"
17 #include "submodule-config.h"
18 #include "submodule.h"
21 #include "string-list.h"
22 #include "argv-array.h"
26 #ifdef NO_FAST_WORKING_DIRECTORY
27 #define FAST_WORKING_DIRECTORY 0
29 #define FAST_WORKING_DIRECTORY 1
32 static int diff_detect_rename_default
;
33 static int diff_indent_heuristic
= 1;
34 static int diff_rename_limit_default
= 400;
35 static int diff_suppress_blank_empty
;
36 static int diff_use_color_default
= -1;
37 static int diff_color_moved_default
;
38 static int diff_context_default
= 3;
39 static int diff_interhunk_context_default
;
40 static const char *diff_word_regex_cfg
;
41 static const char *external_diff_cmd_cfg
;
42 static const char *diff_order_file_cfg
;
43 int diff_auto_refresh_index
= 1;
44 static int diff_mnemonic_prefix
;
45 static int diff_no_prefix
;
46 static int diff_stat_graph_width
;
47 static int diff_dirstat_permille_default
= 30;
48 static struct diff_options default_diff_options
;
49 static long diff_algorithm
;
50 static unsigned ws_error_highlight_default
= WSEH_NEW
;
52 static char diff_colors
[][COLOR_MAXLEN
] = {
54 GIT_COLOR_NORMAL
, /* CONTEXT */
55 GIT_COLOR_BOLD
, /* METAINFO */
56 GIT_COLOR_CYAN
, /* FRAGINFO */
57 GIT_COLOR_RED
, /* OLD */
58 GIT_COLOR_GREEN
, /* NEW */
59 GIT_COLOR_YELLOW
, /* COMMIT */
60 GIT_COLOR_BG_RED
, /* WHITESPACE */
61 GIT_COLOR_NORMAL
, /* FUNCINFO */
62 GIT_COLOR_BOLD_MAGENTA
, /* OLD_MOVED */
63 GIT_COLOR_BOLD_BLUE
, /* OLD_MOVED ALTERNATIVE */
64 GIT_COLOR_FAINT
, /* OLD_MOVED_DIM */
65 GIT_COLOR_FAINT_ITALIC
, /* OLD_MOVED_ALTERNATIVE_DIM */
66 GIT_COLOR_BOLD_CYAN
, /* NEW_MOVED */
67 GIT_COLOR_BOLD_YELLOW
, /* NEW_MOVED ALTERNATIVE */
68 GIT_COLOR_FAINT
, /* NEW_MOVED_DIM */
69 GIT_COLOR_FAINT_ITALIC
, /* NEW_MOVED_ALTERNATIVE_DIM */
72 static NORETURN
void die_want_option(const char *option_name
)
74 die(_("option '%s' requires a value"), option_name
);
77 static int parse_diff_color_slot(const char *var
)
79 if (!strcasecmp(var
, "context") || !strcasecmp(var
, "plain"))
81 if (!strcasecmp(var
, "meta"))
83 if (!strcasecmp(var
, "frag"))
85 if (!strcasecmp(var
, "old"))
87 if (!strcasecmp(var
, "new"))
89 if (!strcasecmp(var
, "commit"))
91 if (!strcasecmp(var
, "whitespace"))
92 return DIFF_WHITESPACE
;
93 if (!strcasecmp(var
, "func"))
95 if (!strcasecmp(var
, "oldmoved"))
96 return DIFF_FILE_OLD_MOVED
;
97 if (!strcasecmp(var
, "oldmovedalternative"))
98 return DIFF_FILE_OLD_MOVED_ALT
;
99 if (!strcasecmp(var
, "oldmoveddimmed"))
100 return DIFF_FILE_OLD_MOVED_DIM
;
101 if (!strcasecmp(var
, "oldmovedalternativedimmed"))
102 return DIFF_FILE_OLD_MOVED_ALT_DIM
;
103 if (!strcasecmp(var
, "newmoved"))
104 return DIFF_FILE_NEW_MOVED
;
105 if (!strcasecmp(var
, "newmovedalternative"))
106 return DIFF_FILE_NEW_MOVED_ALT
;
107 if (!strcasecmp(var
, "newmoveddimmed"))
108 return DIFF_FILE_NEW_MOVED_DIM
;
109 if (!strcasecmp(var
, "newmovedalternativedimmed"))
110 return DIFF_FILE_NEW_MOVED_ALT_DIM
;
114 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
115 struct strbuf
*errmsg
)
117 char *params_copy
= xstrdup(params_string
);
118 struct string_list params
= STRING_LIST_INIT_NODUP
;
123 string_list_split_in_place(¶ms
, params_copy
, ',', -1);
124 for (i
= 0; i
< params
.nr
; i
++) {
125 const char *p
= params
.items
[i
].string
;
126 if (!strcmp(p
, "changes")) {
127 options
->flags
.dirstat_by_line
= 0;
128 options
->flags
.dirstat_by_file
= 0;
129 } else if (!strcmp(p
, "lines")) {
130 options
->flags
.dirstat_by_line
= 1;
131 options
->flags
.dirstat_by_file
= 0;
132 } else if (!strcmp(p
, "files")) {
133 options
->flags
.dirstat_by_line
= 0;
134 options
->flags
.dirstat_by_file
= 1;
135 } else if (!strcmp(p
, "noncumulative")) {
136 options
->flags
.dirstat_cumulative
= 0;
137 } else if (!strcmp(p
, "cumulative")) {
138 options
->flags
.dirstat_cumulative
= 1;
139 } else if (isdigit(*p
)) {
141 int permille
= strtoul(p
, &end
, 10) * 10;
142 if (*end
== '.' && isdigit(*++end
)) {
143 /* only use first digit */
144 permille
+= *end
- '0';
145 /* .. and ignore any further digits */
146 while (isdigit(*++end
))
150 options
->dirstat_permille
= permille
;
152 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
157 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
162 string_list_clear(¶ms
, 0);
167 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
169 if (!strcmp(value
, "log"))
170 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
171 else if (!strcmp(value
, "short"))
172 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
173 else if (!strcmp(value
, "diff"))
174 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
180 static int git_config_rename(const char *var
, const char *value
)
183 return DIFF_DETECT_RENAME
;
184 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
185 return DIFF_DETECT_COPY
;
186 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
189 long parse_algorithm_value(const char *value
)
193 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
195 else if (!strcasecmp(value
, "minimal"))
196 return XDF_NEED_MINIMAL
;
197 else if (!strcasecmp(value
, "patience"))
198 return XDF_PATIENCE_DIFF
;
199 else if (!strcasecmp(value
, "histogram"))
200 return XDF_HISTOGRAM_DIFF
;
204 static int parse_one_token(const char **arg
, const char *token
)
207 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
214 static int parse_ws_error_highlight(const char *arg
)
216 const char *orig_arg
= arg
;
220 if (parse_one_token(&arg
, "none"))
222 else if (parse_one_token(&arg
, "default"))
224 else if (parse_one_token(&arg
, "all"))
225 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
226 else if (parse_one_token(&arg
, "new"))
228 else if (parse_one_token(&arg
, "old"))
230 else if (parse_one_token(&arg
, "context"))
233 return -1 - (int)(arg
- orig_arg
);
242 * These are to give UI layer defaults.
243 * The core-level commands such as git-diff-files should
244 * never be affected by the setting of diff.renames
245 * the user happens to have in the configuration file.
247 void init_diff_ui_defaults(void)
249 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
252 int git_diff_heuristic_config(const char *var
, const char *value
, void *cb
)
254 if (!strcmp(var
, "diff.indentheuristic"))
255 diff_indent_heuristic
= git_config_bool(var
, value
);
259 static int parse_color_moved(const char *arg
)
261 switch (git_parse_maybe_bool(arg
)) {
263 return COLOR_MOVED_NO
;
265 return COLOR_MOVED_DEFAULT
;
270 if (!strcmp(arg
, "no"))
271 return COLOR_MOVED_NO
;
272 else if (!strcmp(arg
, "plain"))
273 return COLOR_MOVED_PLAIN
;
274 else if (!strcmp(arg
, "blocks"))
275 return COLOR_MOVED_BLOCKS
;
276 else if (!strcmp(arg
, "zebra"))
277 return COLOR_MOVED_ZEBRA
;
278 else if (!strcmp(arg
, "default"))
279 return COLOR_MOVED_DEFAULT
;
280 else if (!strcmp(arg
, "dimmed_zebra"))
281 return COLOR_MOVED_ZEBRA_DIM
;
283 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed_zebra', 'plain'"));
286 static int parse_color_moved_ws(const char *arg
)
289 struct string_list l
= STRING_LIST_INIT_DUP
;
290 struct string_list_item
*i
;
292 string_list_split(&l
, arg
, ',', -1);
294 for_each_string_list_item(i
, &l
) {
295 struct strbuf sb
= STRBUF_INIT
;
296 strbuf_addstr(&sb
, i
->string
);
299 if (!strcmp(sb
.buf
, "ignore-space-change"))
300 ret
|= XDF_IGNORE_WHITESPACE_CHANGE
;
301 else if (!strcmp(sb
.buf
, "ignore-space-at-eol"))
302 ret
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
303 else if (!strcmp(sb
.buf
, "ignore-all-space"))
304 ret
|= XDF_IGNORE_WHITESPACE
;
305 else if (!strcmp(sb
.buf
, "allow-indentation-change"))
306 ret
|= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
;
308 error(_("ignoring unknown color-moved-ws mode '%s'"), sb
.buf
);
313 if ((ret
& COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
) &&
314 (ret
& XDF_WHITESPACE_FLAGS
))
315 die(_("color-moved-ws: allow-indentation-change cannot be combined with other white space modes"));
317 string_list_clear(&l
, 0);
322 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
324 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
325 diff_use_color_default
= git_config_colorbool(var
, value
);
328 if (!strcmp(var
, "diff.colormoved")) {
329 int cm
= parse_color_moved(value
);
332 diff_color_moved_default
= cm
;
335 if (!strcmp(var
, "diff.context")) {
336 diff_context_default
= git_config_int(var
, value
);
337 if (diff_context_default
< 0)
341 if (!strcmp(var
, "diff.interhunkcontext")) {
342 diff_interhunk_context_default
= git_config_int(var
, value
);
343 if (diff_interhunk_context_default
< 0)
347 if (!strcmp(var
, "diff.renames")) {
348 diff_detect_rename_default
= git_config_rename(var
, value
);
351 if (!strcmp(var
, "diff.autorefreshindex")) {
352 diff_auto_refresh_index
= git_config_bool(var
, value
);
355 if (!strcmp(var
, "diff.mnemonicprefix")) {
356 diff_mnemonic_prefix
= git_config_bool(var
, value
);
359 if (!strcmp(var
, "diff.noprefix")) {
360 diff_no_prefix
= git_config_bool(var
, value
);
363 if (!strcmp(var
, "diff.statgraphwidth")) {
364 diff_stat_graph_width
= git_config_int(var
, value
);
367 if (!strcmp(var
, "diff.external"))
368 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
369 if (!strcmp(var
, "diff.wordregex"))
370 return git_config_string(&diff_word_regex_cfg
, var
, value
);
371 if (!strcmp(var
, "diff.orderfile"))
372 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
374 if (!strcmp(var
, "diff.ignoresubmodules"))
375 handle_ignore_submodules_arg(&default_diff_options
, value
);
377 if (!strcmp(var
, "diff.submodule")) {
378 if (parse_submodule_params(&default_diff_options
, value
))
379 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
384 if (!strcmp(var
, "diff.algorithm")) {
385 diff_algorithm
= parse_algorithm_value(value
);
386 if (diff_algorithm
< 0)
391 if (!strcmp(var
, "diff.wserrorhighlight")) {
392 int val
= parse_ws_error_highlight(value
);
395 ws_error_highlight_default
= val
;
399 if (git_color_config(var
, value
, cb
) < 0)
402 return git_diff_basic_config(var
, value
, cb
);
405 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
409 if (!strcmp(var
, "diff.renamelimit")) {
410 diff_rename_limit_default
= git_config_int(var
, value
);
414 if (userdiff_config(var
, value
) < 0)
417 if (skip_prefix(var
, "diff.color.", &name
) ||
418 skip_prefix(var
, "color.diff.", &name
)) {
419 int slot
= parse_diff_color_slot(name
);
423 return config_error_nonbool(var
);
424 return color_parse(value
, diff_colors
[slot
]);
427 /* like GNU diff's --suppress-blank-empty option */
428 if (!strcmp(var
, "diff.suppressblankempty") ||
429 /* for backwards compatibility */
430 !strcmp(var
, "diff.suppress-blank-empty")) {
431 diff_suppress_blank_empty
= git_config_bool(var
, value
);
435 if (!strcmp(var
, "diff.dirstat")) {
436 struct strbuf errmsg
= STRBUF_INIT
;
437 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
438 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
439 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
441 strbuf_release(&errmsg
);
442 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
446 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
449 return git_default_config(var
, value
, cb
);
452 static char *quote_two(const char *one
, const char *two
)
454 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
455 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
456 struct strbuf res
= STRBUF_INIT
;
458 if (need_one
+ need_two
) {
459 strbuf_addch(&res
, '"');
460 quote_c_style(one
, &res
, NULL
, 1);
461 quote_c_style(two
, &res
, NULL
, 1);
462 strbuf_addch(&res
, '"');
464 strbuf_addstr(&res
, one
);
465 strbuf_addstr(&res
, two
);
467 return strbuf_detach(&res
, NULL
);
470 static const char *external_diff(void)
472 static const char *external_diff_cmd
= NULL
;
473 static int done_preparing
= 0;
476 return external_diff_cmd
;
477 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
478 if (!external_diff_cmd
)
479 external_diff_cmd
= external_diff_cmd_cfg
;
481 return external_diff_cmd
;
485 * Keep track of files used for diffing. Sometimes such an entry
486 * refers to a temporary file, sometimes to an existing file, and
487 * sometimes to "/dev/null".
489 static struct diff_tempfile
{
491 * filename external diff should read from, or NULL if this
492 * entry is currently not in use:
496 char hex
[GIT_MAX_HEXSZ
+ 1];
500 * If this diff_tempfile instance refers to a temporary file,
501 * this tempfile object is used to manage its lifetime.
503 struct tempfile
*tempfile
;
506 struct emit_callback
{
509 int blank_at_eof_in_preimage
;
510 int blank_at_eof_in_postimage
;
512 int lno_in_postimage
;
513 const char **label_path
;
514 struct diff_words_data
*diff_words
;
515 struct diff_options
*opt
;
516 struct strbuf
*header
;
519 static int count_lines(const char *data
, int size
)
521 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
528 completely_empty
= 0;
532 completely_empty
= 0;
535 if (completely_empty
)
538 count
++; /* no trailing newline */
542 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
544 if (!DIFF_FILE_VALID(one
)) {
545 mf
->ptr
= (char *)""; /* does not matter */
549 else if (diff_populate_filespec(one
, 0))
553 mf
->size
= one
->size
;
557 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
558 static unsigned long diff_filespec_size(struct diff_filespec
*one
)
560 if (!DIFF_FILE_VALID(one
))
562 diff_populate_filespec(one
, CHECK_SIZE_ONLY
);
566 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
569 long size
= mf
->size
;
574 ptr
+= size
- 1; /* pointing at the very end */
576 ; /* incomplete line */
578 ptr
--; /* skip the last LF */
579 while (mf
->ptr
< ptr
) {
581 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
582 if (*prev_eol
== '\n')
584 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
592 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
593 struct emit_callback
*ecbdata
)
596 unsigned ws_rule
= ecbdata
->ws_rule
;
597 l1
= count_trailing_blank(mf1
, ws_rule
);
598 l2
= count_trailing_blank(mf2
, ws_rule
);
600 ecbdata
->blank_at_eof_in_preimage
= 0;
601 ecbdata
->blank_at_eof_in_postimage
= 0;
604 at
= count_lines(mf1
->ptr
, mf1
->size
);
605 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
607 at
= count_lines(mf2
->ptr
, mf2
->size
);
608 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
611 static void emit_line_0(struct diff_options
*o
, const char *set
, const char *reset
,
612 int first
, const char *line
, int len
)
614 int has_trailing_newline
, has_trailing_carriage_return
;
616 FILE *file
= o
->file
;
618 fputs(diff_line_prefix(o
), file
);
621 has_trailing_newline
= (first
== '\n');
622 has_trailing_carriage_return
= (!has_trailing_newline
&&
624 nofirst
= has_trailing_newline
|| has_trailing_carriage_return
;
626 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
627 if (has_trailing_newline
)
629 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
630 if (has_trailing_carriage_return
)
635 if (len
|| !nofirst
) {
639 fwrite(line
, len
, 1, file
);
642 if (has_trailing_carriage_return
)
644 if (has_trailing_newline
)
648 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
649 const char *line
, int len
)
651 emit_line_0(o
, set
, reset
, line
[0], line
+1, len
-1);
655 DIFF_SYMBOL_BINARY_DIFF_HEADER
,
656 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
657 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
658 DIFF_SYMBOL_BINARY_DIFF_BODY
,
659 DIFF_SYMBOL_BINARY_DIFF_FOOTER
,
660 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
661 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
662 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
663 DIFF_SYMBOL_STATS_LINE
,
664 DIFF_SYMBOL_WORD_DIFF
,
665 DIFF_SYMBOL_STAT_SEP
,
667 DIFF_SYMBOL_SUBMODULE_ADD
,
668 DIFF_SYMBOL_SUBMODULE_DEL
,
669 DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
670 DIFF_SYMBOL_SUBMODULE_MODIFIED
,
671 DIFF_SYMBOL_SUBMODULE_HEADER
,
672 DIFF_SYMBOL_SUBMODULE_ERROR
,
673 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
,
674 DIFF_SYMBOL_REWRITE_DIFF
,
675 DIFF_SYMBOL_BINARY_FILES
,
677 DIFF_SYMBOL_FILEPAIR_PLUS
,
678 DIFF_SYMBOL_FILEPAIR_MINUS
,
679 DIFF_SYMBOL_WORDS_PORCELAIN
,
682 DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
685 DIFF_SYMBOL_NO_LF_EOF
,
686 DIFF_SYMBOL_CONTEXT_FRAGINFO
,
687 DIFF_SYMBOL_CONTEXT_MARKER
,
688 DIFF_SYMBOL_SEPARATOR
691 * Flags for content lines:
692 * 0..12 are whitespace rules
693 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
694 * 16 is marking if the line is blank at EOF
696 #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
697 #define DIFF_SYMBOL_MOVED_LINE (1<<17)
698 #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
699 #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
700 #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
703 * This struct is used when we need to buffer the output of the diff output.
705 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
706 * into the pre/post image file. This pointer could be a union with the
707 * line pointer. By storing an offset into the file instead of the literal line,
708 * we can decrease the memory footprint for the buffered output. At first we
709 * may want to only have indirection for the content lines, but we could also
710 * enhance the state for emitting prefabricated lines, e.g. the similarity
711 * score line or hunk/file headers would only need to store a number or path
712 * and then the output can be constructed later on depending on state.
714 struct emitted_diff_symbol
{
720 #define EMITTED_DIFF_SYMBOL_INIT {NULL}
722 struct emitted_diff_symbols
{
723 struct emitted_diff_symbol
*buf
;
726 #define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
728 static void append_emitted_diff_symbol(struct diff_options
*o
,
729 struct emitted_diff_symbol
*e
)
731 struct emitted_diff_symbol
*f
;
733 ALLOC_GROW(o
->emitted_symbols
->buf
,
734 o
->emitted_symbols
->nr
+ 1,
735 o
->emitted_symbols
->alloc
);
736 f
= &o
->emitted_symbols
->buf
[o
->emitted_symbols
->nr
++];
738 memcpy(f
, e
, sizeof(struct emitted_diff_symbol
));
739 f
->line
= e
->line
? xmemdupz(e
->line
, e
->len
) : NULL
;
743 struct hashmap_entry ent
;
744 const struct emitted_diff_symbol
*es
;
745 struct moved_entry
*next_line
;
746 struct ws_delta
*wsd
;
750 * The struct ws_delta holds white space differences between moved lines, i.e.
751 * between '+' and '-' lines that have been detected to be a move.
752 * The string contains the difference in leading white spaces, before the
753 * rest of the line is compared using the white space config for move
754 * coloring. The current_longer indicates if the first string in the
755 * comparision is longer than the second.
759 unsigned int current_longer
: 1;
761 #define WS_DELTA_INIT { NULL, 0 }
763 static int compute_ws_delta(const struct emitted_diff_symbol
*a
,
764 const struct emitted_diff_symbol
*b
,
765 struct ws_delta
*out
)
767 const struct emitted_diff_symbol
*longer
= a
->len
> b
->len
? a
: b
;
768 const struct emitted_diff_symbol
*shorter
= a
->len
> b
->len
? b
: a
;
769 int d
= longer
->len
- shorter
->len
;
771 out
->string
= xmemdupz(longer
->line
, d
);
772 out
->current_longer
= (a
== longer
);
774 return !strncmp(longer
->line
+ d
, shorter
->line
, shorter
->len
);
777 static int cmp_in_block_with_wsd(const struct diff_options
*o
,
778 const struct moved_entry
*cur
,
779 const struct moved_entry
*match
,
780 struct moved_entry
*pmb
,
783 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
784 int al
= cur
->es
->len
, cl
= l
->len
;
785 const char *a
= cur
->es
->line
,
786 *b
= match
->es
->line
,
792 * We need to check if 'cur' is equal to 'match'.
793 * As those are from the same (+/-) side, we do not need to adjust for
794 * indent changes. However these were found using fuzzy matching
795 * so we do have to check if they are equal.
802 * No white space delta was carried forward? This can happen
803 * when we exit early in this function and do not carry
809 * The indent changes of the block are known and carried forward in
810 * pmb->wsd; however we need to check if the indent changes of the
811 * current line are still the same as before.
813 * To do so we need to compare 'l' to 'cur', adjusting the
814 * one of them for the white spaces, depending which was longer.
817 wslen
= strlen(pmb
->wsd
->string
);
818 if (pmb
->wsd
->current_longer
) {
832 static int moved_entry_cmp(const void *hashmap_cmp_fn_data
,
834 const void *entry_or_key
,
837 const struct diff_options
*diffopt
= hashmap_cmp_fn_data
;
838 const struct moved_entry
*a
= entry
;
839 const struct moved_entry
*b
= entry_or_key
;
840 unsigned flags
= diffopt
->color_moved_ws_handling
841 & XDF_WHITESPACE_FLAGS
;
843 if (diffopt
->color_moved_ws_handling
&
844 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
846 * As there is not specific white space config given,
847 * we'd need to check for a new block, so ignore all
848 * white space. The setup of the white space
849 * configuration for the next block is done else where
851 flags
|= XDF_IGNORE_WHITESPACE
;
853 return !xdiff_compare_lines(a
->es
->line
, a
->es
->len
,
854 b
->es
->line
, b
->es
->len
,
858 static struct moved_entry
*prepare_entry(struct diff_options
*o
,
861 struct moved_entry
*ret
= xmalloc(sizeof(*ret
));
862 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[line_no
];
863 unsigned flags
= o
->color_moved_ws_handling
& XDF_WHITESPACE_FLAGS
;
865 ret
->ent
.hash
= xdiff_hash_string(l
->line
, l
->len
, flags
);
867 ret
->next_line
= NULL
;
873 static void add_lines_to_move_detection(struct diff_options
*o
,
874 struct hashmap
*add_lines
,
875 struct hashmap
*del_lines
)
877 struct moved_entry
*prev_line
= NULL
;
880 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
882 struct moved_entry
*key
;
884 switch (o
->emitted_symbols
->buf
[n
].s
) {
885 case DIFF_SYMBOL_PLUS
:
888 case DIFF_SYMBOL_MINUS
:
896 key
= prepare_entry(o
, n
);
897 if (prev_line
&& prev_line
->es
->s
== o
->emitted_symbols
->buf
[n
].s
)
898 prev_line
->next_line
= key
;
900 hashmap_add(hm
, key
);
905 static void pmb_advance_or_null(struct diff_options
*o
,
906 struct moved_entry
*match
,
908 struct moved_entry
**pmb
,
912 for (i
= 0; i
< pmb_nr
; i
++) {
913 struct moved_entry
*prev
= pmb
[i
];
914 struct moved_entry
*cur
= (prev
&& prev
->next_line
) ?
915 prev
->next_line
: NULL
;
916 if (cur
&& !hm
->cmpfn(o
, cur
, match
, NULL
)) {
924 static void pmb_advance_or_null_multi_match(struct diff_options
*o
,
925 struct moved_entry
*match
,
927 struct moved_entry
**pmb
,
931 char *got_match
= xcalloc(1, pmb_nr
);
933 for (; match
; match
= hashmap_get_next(hm
, match
)) {
934 for (i
= 0; i
< pmb_nr
; i
++) {
935 struct moved_entry
*prev
= pmb
[i
];
936 struct moved_entry
*cur
= (prev
&& prev
->next_line
) ?
937 prev
->next_line
: NULL
;
940 if (!cmp_in_block_with_wsd(o
, cur
, match
, pmb
[i
], n
))
945 for (i
= 0; i
< pmb_nr
; i
++) {
947 /* Carry the white space delta forward */
948 pmb
[i
]->next_line
->wsd
= pmb
[i
]->wsd
;
949 pmb
[i
] = pmb
[i
]->next_line
;
955 static int shrink_potential_moved_blocks(struct moved_entry
**pmb
,
960 /* Shrink the set of potential block to the remaining running */
961 for (lp
= 0, rp
= pmb_nr
- 1; lp
<= rp
;) {
962 while (lp
< pmb_nr
&& pmb
[lp
])
964 /* lp points at the first NULL now */
966 while (rp
> -1 && !pmb
[rp
])
968 /* rp points at the last non-NULL */
970 if (lp
< pmb_nr
&& rp
> -1 && lp
< rp
) {
973 free(pmb
[rp
]->wsd
->string
);
974 FREE_AND_NULL(pmb
[rp
]->wsd
);
982 /* Remember the number of running sets */
987 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
989 * Otherwise, if the last block has fewer alphanumeric characters than
990 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
993 * The last block consists of the (n - block_length)'th line up to but not
994 * including the nth line.
996 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
997 * Think of a way to unify them.
999 static void adjust_last_block(struct diff_options
*o
, int n
, int block_length
)
1001 int i
, alnum_count
= 0;
1002 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
1004 for (i
= 1; i
< block_length
+ 1; i
++) {
1005 const char *c
= o
->emitted_symbols
->buf
[n
- i
].line
;
1010 if (alnum_count
>= COLOR_MOVED_MIN_ALNUM_COUNT
)
1014 for (i
= 1; i
< block_length
+ 1; i
++)
1015 o
->emitted_symbols
->buf
[n
- i
].flags
&= ~DIFF_SYMBOL_MOVED_LINE
;
1018 /* Find blocks of moved code, delegate actual coloring decision to helper */
1019 static void mark_color_as_moved(struct diff_options
*o
,
1020 struct hashmap
*add_lines
,
1021 struct hashmap
*del_lines
)
1023 struct moved_entry
**pmb
= NULL
; /* potentially moved blocks */
1024 int pmb_nr
= 0, pmb_alloc
= 0;
1025 int n
, flipped_block
= 1, block_length
= 0;
1028 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1029 struct hashmap
*hm
= NULL
;
1030 struct moved_entry
*key
;
1031 struct moved_entry
*match
= NULL
;
1032 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1035 case DIFF_SYMBOL_PLUS
:
1037 key
= prepare_entry(o
, n
);
1038 match
= hashmap_get(hm
, key
, NULL
);
1041 case DIFF_SYMBOL_MINUS
:
1043 key
= prepare_entry(o
, n
);
1044 match
= hashmap_get(hm
, key
, NULL
);
1052 adjust_last_block(o
, n
, block_length
);
1058 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1060 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
1063 if (o
->color_moved_ws_handling
&
1064 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1065 pmb_advance_or_null_multi_match(o
, match
, hm
, pmb
, pmb_nr
, n
);
1067 pmb_advance_or_null(o
, match
, hm
, pmb
, pmb_nr
);
1069 pmb_nr
= shrink_potential_moved_blocks(pmb
, pmb_nr
);
1073 * The current line is the start of a new block.
1074 * Setup the set of potential blocks.
1076 for (; match
; match
= hashmap_get_next(hm
, match
)) {
1077 ALLOC_GROW(pmb
, pmb_nr
+ 1, pmb_alloc
);
1078 if (o
->color_moved_ws_handling
&
1079 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
) {
1080 struct ws_delta
*wsd
= xmalloc(sizeof(*match
->wsd
));
1081 if (compute_ws_delta(l
, match
->es
, wsd
)) {
1083 pmb
[pmb_nr
++] = match
;
1087 pmb
[pmb_nr
++] = match
;
1091 flipped_block
= (flipped_block
+ 1) % 2;
1093 adjust_last_block(o
, n
, block_length
);
1099 if (flipped_block
&& o
->color_moved
!= COLOR_MOVED_BLOCKS
)
1100 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_ALT
;
1102 adjust_last_block(o
, n
, block_length
);
1107 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1108 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
1109 static void dim_moved_lines(struct diff_options
*o
)
1112 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1113 struct emitted_diff_symbol
*prev
= (n
!= 0) ?
1114 &o
->emitted_symbols
->buf
[n
- 1] : NULL
;
1115 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1116 struct emitted_diff_symbol
*next
=
1117 (n
< o
->emitted_symbols
->nr
- 1) ?
1118 &o
->emitted_symbols
->buf
[n
+ 1] : NULL
;
1120 /* Not a plus or minus line? */
1121 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
)
1124 /* Not a moved line? */
1125 if (!(l
->flags
& DIFF_SYMBOL_MOVED_LINE
))
1129 * If prev or next are not a plus or minus line,
1130 * pretend they don't exist
1132 if (prev
&& prev
->s
!= DIFF_SYMBOL_PLUS
&&
1133 prev
->s
!= DIFF_SYMBOL_MINUS
)
1135 if (next
&& next
->s
!= DIFF_SYMBOL_PLUS
&&
1136 next
->s
!= DIFF_SYMBOL_MINUS
)
1139 /* Inside a block? */
1141 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1142 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
)) &&
1144 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1145 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
))) {
1146 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1150 /* Check if we are at an interesting bound: */
1151 if (prev
&& (prev
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1152 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1153 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1155 if (next
&& (next
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1156 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1157 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1161 * The boundary to prev and next are not interesting,
1162 * so this line is not interesting as a whole
1164 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1168 static void emit_line_ws_markup(struct diff_options
*o
,
1169 const char *set
, const char *reset
,
1170 const char *line
, int len
, char sign
,
1171 unsigned ws_rule
, int blank_at_eof
)
1173 const char *ws
= NULL
;
1175 if (o
->ws_error_highlight
& ws_rule
) {
1176 ws
= diff_get_color_opt(o
, DIFF_WHITESPACE
);
1182 emit_line_0(o
, set
, reset
, sign
, line
, len
);
1183 else if (blank_at_eof
)
1184 /* Blank line at EOF - paint '+' as well */
1185 emit_line_0(o
, ws
, reset
, sign
, line
, len
);
1187 /* Emit just the prefix, then the rest. */
1188 emit_line_0(o
, set
, reset
, sign
, "", 0);
1189 ws_check_emit(line
, len
, ws_rule
,
1190 o
->file
, set
, reset
, ws
);
1194 static void emit_diff_symbol_from_struct(struct diff_options
*o
,
1195 struct emitted_diff_symbol
*eds
)
1197 static const char *nneof
= " No newline at end of file\n";
1198 const char *context
, *reset
, *set
, *meta
, *fraginfo
;
1199 struct strbuf sb
= STRBUF_INIT
;
1201 enum diff_symbol s
= eds
->s
;
1202 const char *line
= eds
->line
;
1204 unsigned flags
= eds
->flags
;
1207 case DIFF_SYMBOL_NO_LF_EOF
:
1208 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1209 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1210 putc('\n', o
->file
);
1211 emit_line_0(o
, context
, reset
, '\\',
1212 nneof
, strlen(nneof
));
1214 case DIFF_SYMBOL_SUBMODULE_HEADER
:
1215 case DIFF_SYMBOL_SUBMODULE_ERROR
:
1216 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
:
1217 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
:
1218 case DIFF_SYMBOL_SUMMARY
:
1219 case DIFF_SYMBOL_STATS_LINE
:
1220 case DIFF_SYMBOL_BINARY_DIFF_BODY
:
1221 case DIFF_SYMBOL_CONTEXT_FRAGINFO
:
1222 emit_line(o
, "", "", line
, len
);
1224 case DIFF_SYMBOL_CONTEXT_INCOMPLETE
:
1225 case DIFF_SYMBOL_CONTEXT_MARKER
:
1226 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1227 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1228 emit_line(o
, context
, reset
, line
, len
);
1230 case DIFF_SYMBOL_SEPARATOR
:
1231 fprintf(o
->file
, "%s%c",
1232 diff_line_prefix(o
),
1233 o
->line_termination
);
1235 case DIFF_SYMBOL_CONTEXT
:
1236 set
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1237 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1238 emit_line_ws_markup(o
, set
, reset
, line
, len
, ' ',
1239 flags
& (DIFF_SYMBOL_CONTENT_WS_MASK
), 0);
1241 case DIFF_SYMBOL_PLUS
:
1242 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1243 DIFF_SYMBOL_MOVED_LINE_ALT
|
1244 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1245 case DIFF_SYMBOL_MOVED_LINE
|
1246 DIFF_SYMBOL_MOVED_LINE_ALT
|
1247 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1248 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT_DIM
);
1250 case DIFF_SYMBOL_MOVED_LINE
|
1251 DIFF_SYMBOL_MOVED_LINE_ALT
:
1252 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT
);
1254 case DIFF_SYMBOL_MOVED_LINE
|
1255 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1256 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_DIM
);
1258 case DIFF_SYMBOL_MOVED_LINE
:
1259 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED
);
1262 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1264 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1265 emit_line_ws_markup(o
, set
, reset
, line
, len
, '+',
1266 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
,
1267 flags
& DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
);
1269 case DIFF_SYMBOL_MINUS
:
1270 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1271 DIFF_SYMBOL_MOVED_LINE_ALT
|
1272 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1273 case DIFF_SYMBOL_MOVED_LINE
|
1274 DIFF_SYMBOL_MOVED_LINE_ALT
|
1275 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1276 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT_DIM
);
1278 case DIFF_SYMBOL_MOVED_LINE
|
1279 DIFF_SYMBOL_MOVED_LINE_ALT
:
1280 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT
);
1282 case DIFF_SYMBOL_MOVED_LINE
|
1283 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1284 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_DIM
);
1286 case DIFF_SYMBOL_MOVED_LINE
:
1287 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED
);
1290 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1292 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1293 emit_line_ws_markup(o
, set
, reset
, line
, len
, '-',
1294 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
, 0);
1296 case DIFF_SYMBOL_WORDS_PORCELAIN
:
1297 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1298 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1299 emit_line(o
, context
, reset
, line
, len
);
1300 fputs("~\n", o
->file
);
1302 case DIFF_SYMBOL_WORDS
:
1303 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1304 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1306 * Skip the prefix character, if any. With
1307 * diff_suppress_blank_empty, there may be
1310 if (line
[0] != '\n') {
1314 emit_line(o
, context
, reset
, line
, len
);
1316 case DIFF_SYMBOL_FILEPAIR_PLUS
:
1317 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1318 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1319 fprintf(o
->file
, "%s%s+++ %s%s%s\n", diff_line_prefix(o
), meta
,
1321 strchr(line
, ' ') ? "\t" : "");
1323 case DIFF_SYMBOL_FILEPAIR_MINUS
:
1324 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1325 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1326 fprintf(o
->file
, "%s%s--- %s%s%s\n", diff_line_prefix(o
), meta
,
1328 strchr(line
, ' ') ? "\t" : "");
1330 case DIFF_SYMBOL_BINARY_FILES
:
1331 case DIFF_SYMBOL_HEADER
:
1332 fprintf(o
->file
, "%s", line
);
1334 case DIFF_SYMBOL_BINARY_DIFF_HEADER
:
1335 fprintf(o
->file
, "%sGIT binary patch\n", diff_line_prefix(o
));
1337 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
:
1338 fprintf(o
->file
, "%sdelta %s\n", diff_line_prefix(o
), line
);
1340 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
:
1341 fprintf(o
->file
, "%sliteral %s\n", diff_line_prefix(o
), line
);
1343 case DIFF_SYMBOL_BINARY_DIFF_FOOTER
:
1344 fputs(diff_line_prefix(o
), o
->file
);
1345 fputc('\n', o
->file
);
1347 case DIFF_SYMBOL_REWRITE_DIFF
:
1348 fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
1349 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1350 emit_line(o
, fraginfo
, reset
, line
, len
);
1352 case DIFF_SYMBOL_SUBMODULE_ADD
:
1353 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1354 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1355 emit_line(o
, set
, reset
, line
, len
);
1357 case DIFF_SYMBOL_SUBMODULE_DEL
:
1358 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1359 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1360 emit_line(o
, set
, reset
, line
, len
);
1362 case DIFF_SYMBOL_SUBMODULE_UNTRACKED
:
1363 fprintf(o
->file
, "%sSubmodule %s contains untracked content\n",
1364 diff_line_prefix(o
), line
);
1366 case DIFF_SYMBOL_SUBMODULE_MODIFIED
:
1367 fprintf(o
->file
, "%sSubmodule %s contains modified content\n",
1368 diff_line_prefix(o
), line
);
1370 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
:
1371 emit_line(o
, "", "", " 0 files changed\n",
1372 strlen(" 0 files changed\n"));
1374 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV
:
1375 emit_line(o
, "", "", " ...\n", strlen(" ...\n"));
1377 case DIFF_SYMBOL_WORD_DIFF
:
1378 fprintf(o
->file
, "%.*s", len
, line
);
1380 case DIFF_SYMBOL_STAT_SEP
:
1381 fputs(o
->stat_sep
, o
->file
);
1384 die("BUG: unknown diff symbol");
1386 strbuf_release(&sb
);
1389 static void emit_diff_symbol(struct diff_options
*o
, enum diff_symbol s
,
1390 const char *line
, int len
, unsigned flags
)
1392 struct emitted_diff_symbol e
= {line
, len
, flags
, s
};
1394 if (o
->emitted_symbols
)
1395 append_emitted_diff_symbol(o
, &e
);
1397 emit_diff_symbol_from_struct(o
, &e
);
1400 void diff_emit_submodule_del(struct diff_options
*o
, const char *line
)
1402 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_DEL
, line
, strlen(line
), 0);
1405 void diff_emit_submodule_add(struct diff_options
*o
, const char *line
)
1407 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ADD
, line
, strlen(line
), 0);
1410 void diff_emit_submodule_untracked(struct diff_options
*o
, const char *path
)
1412 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
1413 path
, strlen(path
), 0);
1416 void diff_emit_submodule_modified(struct diff_options
*o
, const char *path
)
1418 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_MODIFIED
,
1419 path
, strlen(path
), 0);
1422 void diff_emit_submodule_header(struct diff_options
*o
, const char *header
)
1424 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_HEADER
,
1425 header
, strlen(header
), 0);
1428 void diff_emit_submodule_error(struct diff_options
*o
, const char *err
)
1430 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ERROR
, err
, strlen(err
), 0);
1433 void diff_emit_submodule_pipethrough(struct diff_options
*o
,
1434 const char *line
, int len
)
1436 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
, line
, len
, 0);
1439 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
1441 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
1442 ecbdata
->blank_at_eof_in_preimage
&&
1443 ecbdata
->blank_at_eof_in_postimage
&&
1444 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
1445 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
1447 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
1450 static void emit_add_line(const char *reset
,
1451 struct emit_callback
*ecbdata
,
1452 const char *line
, int len
)
1454 unsigned flags
= WSEH_NEW
| ecbdata
->ws_rule
;
1455 if (new_blank_line_at_eof(ecbdata
, line
, len
))
1456 flags
|= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
;
1458 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_PLUS
, line
, len
, flags
);
1461 static void emit_del_line(const char *reset
,
1462 struct emit_callback
*ecbdata
,
1463 const char *line
, int len
)
1465 unsigned flags
= WSEH_OLD
| ecbdata
->ws_rule
;
1466 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_MINUS
, line
, len
, flags
);
1469 static void emit_context_line(const char *reset
,
1470 struct emit_callback
*ecbdata
,
1471 const char *line
, int len
)
1473 unsigned flags
= WSEH_CONTEXT
| ecbdata
->ws_rule
;
1474 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_CONTEXT
, line
, len
, flags
);
1477 static void emit_hunk_header(struct emit_callback
*ecbdata
,
1478 const char *line
, int len
)
1480 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1481 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
1482 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
1483 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1484 static const char atat
[2] = { '@', '@' };
1485 const char *cp
, *ep
;
1486 struct strbuf msgbuf
= STRBUF_INIT
;
1491 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1492 * it always is at least 10 bytes long.
1495 memcmp(line
, atat
, 2) ||
1496 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
1497 emit_diff_symbol(ecbdata
->opt
,
1498 DIFF_SYMBOL_CONTEXT_MARKER
, line
, len
, 0);
1501 ep
+= 2; /* skip over @@ */
1503 /* The hunk header in fraginfo color */
1504 strbuf_addstr(&msgbuf
, frag
);
1505 strbuf_add(&msgbuf
, line
, ep
- line
);
1506 strbuf_addstr(&msgbuf
, reset
);
1512 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
1515 /* blank before the func header */
1516 for (cp
= ep
; ep
- line
< len
; ep
++)
1517 if (*ep
!= ' ' && *ep
!= '\t')
1520 strbuf_addstr(&msgbuf
, context
);
1521 strbuf_add(&msgbuf
, cp
, ep
- cp
);
1522 strbuf_addstr(&msgbuf
, reset
);
1525 if (ep
< line
+ len
) {
1526 strbuf_addstr(&msgbuf
, func
);
1527 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
1528 strbuf_addstr(&msgbuf
, reset
);
1531 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
1532 strbuf_complete_line(&msgbuf
);
1533 emit_diff_symbol(ecbdata
->opt
,
1534 DIFF_SYMBOL_CONTEXT_FRAGINFO
, msgbuf
.buf
, msgbuf
.len
, 0);
1535 strbuf_release(&msgbuf
);
1538 static struct diff_tempfile
*claim_diff_tempfile(void) {
1540 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
1541 if (!diff_temp
[i
].name
)
1542 return diff_temp
+ i
;
1543 die("BUG: diff is failing to clean up its tempfiles");
1546 static void remove_tempfile(void)
1549 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
1550 if (is_tempfile_active(diff_temp
[i
].tempfile
))
1551 delete_tempfile(&diff_temp
[i
].tempfile
);
1552 diff_temp
[i
].name
= NULL
;
1556 static void add_line_count(struct strbuf
*out
, int count
)
1560 strbuf_addstr(out
, "0,0");
1563 strbuf_addstr(out
, "1");
1566 strbuf_addf(out
, "1,%d", count
);
1571 static void emit_rewrite_lines(struct emit_callback
*ecb
,
1572 int prefix
, const char *data
, int size
)
1574 const char *endp
= NULL
;
1575 const char *reset
= diff_get_color(ecb
->color_diff
, DIFF_RESET
);
1580 endp
= memchr(data
, '\n', size
);
1581 len
= endp
? (endp
- data
+ 1) : size
;
1582 if (prefix
!= '+') {
1583 ecb
->lno_in_preimage
++;
1584 emit_del_line(reset
, ecb
, data
, len
);
1586 ecb
->lno_in_postimage
++;
1587 emit_add_line(reset
, ecb
, data
, len
);
1593 emit_diff_symbol(ecb
->opt
, DIFF_SYMBOL_NO_LF_EOF
, NULL
, 0, 0);
1596 static void emit_rewrite_diff(const char *name_a
,
1598 struct diff_filespec
*one
,
1599 struct diff_filespec
*two
,
1600 struct userdiff_driver
*textconv_one
,
1601 struct userdiff_driver
*textconv_two
,
1602 struct diff_options
*o
)
1605 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
1606 const char *a_prefix
, *b_prefix
;
1607 char *data_one
, *data_two
;
1608 size_t size_one
, size_two
;
1609 struct emit_callback ecbdata
;
1610 struct strbuf out
= STRBUF_INIT
;
1612 if (diff_mnemonic_prefix
&& o
->flags
.reverse_diff
) {
1613 a_prefix
= o
->b_prefix
;
1614 b_prefix
= o
->a_prefix
;
1616 a_prefix
= o
->a_prefix
;
1617 b_prefix
= o
->b_prefix
;
1620 name_a
+= (*name_a
== '/');
1621 name_b
+= (*name_b
== '/');
1623 strbuf_reset(&a_name
);
1624 strbuf_reset(&b_name
);
1625 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
1626 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
1628 size_one
= fill_textconv(textconv_one
, one
, &data_one
);
1629 size_two
= fill_textconv(textconv_two
, two
, &data_two
);
1631 memset(&ecbdata
, 0, sizeof(ecbdata
));
1632 ecbdata
.color_diff
= want_color(o
->use_color
);
1633 ecbdata
.ws_rule
= whitespace_rule(name_b
);
1635 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
1637 mf1
.ptr
= (char *)data_one
;
1638 mf2
.ptr
= (char *)data_two
;
1639 mf1
.size
= size_one
;
1640 mf2
.size
= size_two
;
1641 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1643 ecbdata
.lno_in_preimage
= 1;
1644 ecbdata
.lno_in_postimage
= 1;
1646 lc_a
= count_lines(data_one
, size_one
);
1647 lc_b
= count_lines(data_two
, size_two
);
1649 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1650 a_name
.buf
, a_name
.len
, 0);
1651 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1652 b_name
.buf
, b_name
.len
, 0);
1654 strbuf_addstr(&out
, "@@ -");
1655 if (!o
->irreversible_delete
)
1656 add_line_count(&out
, lc_a
);
1658 strbuf_addstr(&out
, "?,?");
1659 strbuf_addstr(&out
, " +");
1660 add_line_count(&out
, lc_b
);
1661 strbuf_addstr(&out
, " @@\n");
1662 emit_diff_symbol(o
, DIFF_SYMBOL_REWRITE_DIFF
, out
.buf
, out
.len
, 0);
1663 strbuf_release(&out
);
1665 if (lc_a
&& !o
->irreversible_delete
)
1666 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
1668 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
1670 free((char *)data_one
);
1672 free((char *)data_two
);
1675 struct diff_words_buffer
{
1677 unsigned long alloc
;
1678 struct diff_words_orig
{
1679 const char *begin
, *end
;
1681 int orig_nr
, orig_alloc
;
1684 static void diff_words_append(char *line
, unsigned long len
,
1685 struct diff_words_buffer
*buffer
)
1687 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
1690 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
1691 buffer
->text
.size
+= len
;
1692 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
1695 struct diff_words_style_elem
{
1698 const char *color
; /* NULL; filled in by the setup code if
1699 * color is enabled */
1702 struct diff_words_style
{
1703 enum diff_words_type type
;
1704 struct diff_words_style_elem new_word
, old_word
, ctx
;
1705 const char *newline
;
1708 static struct diff_words_style diff_words_styles
[] = {
1709 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1710 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1711 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
1714 struct diff_words_data
{
1715 struct diff_words_buffer minus
, plus
;
1716 const char *current_plus
;
1718 struct diff_options
*opt
;
1719 regex_t
*word_regex
;
1720 enum diff_words_type type
;
1721 struct diff_words_style
*style
;
1724 static int fn_out_diff_words_write_helper(struct diff_options
*o
,
1725 struct diff_words_style_elem
*st_el
,
1726 const char *newline
,
1727 size_t count
, const char *buf
)
1730 struct strbuf sb
= STRBUF_INIT
;
1733 char *p
= memchr(buf
, '\n', count
);
1735 strbuf_addstr(&sb
, diff_line_prefix(o
));
1738 const char *reset
= st_el
->color
&& *st_el
->color
?
1739 GIT_COLOR_RESET
: NULL
;
1740 if (st_el
->color
&& *st_el
->color
)
1741 strbuf_addstr(&sb
, st_el
->color
);
1742 strbuf_addstr(&sb
, st_el
->prefix
);
1743 strbuf_add(&sb
, buf
, p
? p
- buf
: count
);
1744 strbuf_addstr(&sb
, st_el
->suffix
);
1746 strbuf_addstr(&sb
, reset
);
1751 strbuf_addstr(&sb
, newline
);
1752 count
-= p
+ 1 - buf
;
1756 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1764 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1766 strbuf_release(&sb
);
1771 * '--color-words' algorithm can be described as:
1773 * 1. collect the minus/plus lines of a diff hunk, divided into
1774 * minus-lines and plus-lines;
1776 * 2. break both minus-lines and plus-lines into words and
1777 * place them into two mmfile_t with one word for each line;
1779 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1781 * And for the common parts of the both file, we output the plus side text.
1782 * diff_words->current_plus is used to trace the current position of the plus file
1783 * which printed. diff_words->last_minus is used to trace the last minus word
1786 * For '--graph' to work with '--color-words', we need to output the graph prefix
1787 * on each line of color words output. Generally, there are two conditions on
1788 * which we should output the prefix.
1790 * 1. diff_words->last_minus == 0 &&
1791 * diff_words->current_plus == diff_words->plus.text.ptr
1793 * that is: the plus text must start as a new line, and if there is no minus
1794 * word printed, a graph prefix must be printed.
1796 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1797 * *(diff_words->current_plus - 1) == '\n'
1799 * that is: a graph prefix must be printed following a '\n'
1801 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
1803 if ((diff_words
->last_minus
== 0 &&
1804 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
1805 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
1806 *(diff_words
->current_plus
- 1) == '\n')) {
1813 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
1815 struct diff_words_data
*diff_words
= priv
;
1816 struct diff_words_style
*style
= diff_words
->style
;
1817 int minus_first
, minus_len
, plus_first
, plus_len
;
1818 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
1819 struct diff_options
*opt
= diff_words
->opt
;
1820 const char *line_prefix
;
1822 if (line
[0] != '@' || parse_hunk_header(line
, len
,
1823 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
1827 line_prefix
= diff_line_prefix(opt
);
1829 /* POSIX requires that first be decremented by one if len == 0... */
1831 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
1833 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
1835 minus_begin
= minus_end
=
1836 diff_words
->minus
.orig
[minus_first
].end
;
1839 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
1840 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
1842 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
1844 if (color_words_output_graph_prefix(diff_words
)) {
1845 fputs(line_prefix
, diff_words
->opt
->file
);
1847 if (diff_words
->current_plus
!= plus_begin
) {
1848 fn_out_diff_words_write_helper(diff_words
->opt
,
1849 &style
->ctx
, style
->newline
,
1850 plus_begin
- diff_words
->current_plus
,
1851 diff_words
->current_plus
);
1853 if (minus_begin
!= minus_end
) {
1854 fn_out_diff_words_write_helper(diff_words
->opt
,
1855 &style
->old_word
, style
->newline
,
1856 minus_end
- minus_begin
, minus_begin
);
1858 if (plus_begin
!= plus_end
) {
1859 fn_out_diff_words_write_helper(diff_words
->opt
,
1860 &style
->new_word
, style
->newline
,
1861 plus_end
- plus_begin
, plus_begin
);
1864 diff_words
->current_plus
= plus_end
;
1865 diff_words
->last_minus
= minus_first
;
1868 /* This function starts looking at *begin, and returns 0 iff a word was found. */
1869 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
1870 int *begin
, int *end
)
1872 if (word_regex
&& *begin
< buffer
->size
) {
1873 regmatch_t match
[1];
1874 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
1875 buffer
->size
- *begin
, 1, match
, 0)) {
1876 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
1877 '\n', match
[0].rm_eo
- match
[0].rm_so
);
1878 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
1879 *begin
+= match
[0].rm_so
;
1880 return *begin
>= *end
;
1885 /* find the next word */
1886 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
1888 if (*begin
>= buffer
->size
)
1891 /* find the end of the word */
1893 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
1900 * This function splits the words in buffer->text, stores the list with
1901 * newline separator into out, and saves the offsets of the original words
1904 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
1905 regex_t
*word_regex
)
1913 /* fake an empty "0th" word */
1914 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
1915 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
1916 buffer
->orig_nr
= 1;
1918 for (i
= 0; i
< buffer
->text
.size
; i
++) {
1919 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
1922 /* store original boundaries */
1923 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
1924 buffer
->orig_alloc
);
1925 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
1926 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
1929 /* store one word */
1930 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
1931 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
1932 out
->ptr
[out
->size
+ j
- i
] = '\n';
1933 out
->size
+= j
- i
+ 1;
1939 /* this executes the word diff on the accumulated buffers */
1940 static void diff_words_show(struct diff_words_data
*diff_words
)
1944 mmfile_t minus
, plus
;
1945 struct diff_words_style
*style
= diff_words
->style
;
1947 struct diff_options
*opt
= diff_words
->opt
;
1948 const char *line_prefix
;
1951 line_prefix
= diff_line_prefix(opt
);
1953 /* special case: only removal */
1954 if (!diff_words
->plus
.text
.size
) {
1955 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
1956 line_prefix
, strlen(line_prefix
), 0);
1957 fn_out_diff_words_write_helper(diff_words
->opt
,
1958 &style
->old_word
, style
->newline
,
1959 diff_words
->minus
.text
.size
,
1960 diff_words
->minus
.text
.ptr
);
1961 diff_words
->minus
.text
.size
= 0;
1965 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
1966 diff_words
->last_minus
= 0;
1968 memset(&xpp
, 0, sizeof(xpp
));
1969 memset(&xecfg
, 0, sizeof(xecfg
));
1970 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
1971 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
1973 /* as only the hunk header will be parsed, we need a 0-context */
1975 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
1977 die("unable to generate word diff");
1980 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
1981 diff_words
->plus
.text
.size
) {
1982 if (color_words_output_graph_prefix(diff_words
))
1983 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
1984 line_prefix
, strlen(line_prefix
), 0);
1985 fn_out_diff_words_write_helper(diff_words
->opt
,
1986 &style
->ctx
, style
->newline
,
1987 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
1988 - diff_words
->current_plus
, diff_words
->current_plus
);
1990 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
1993 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
1994 static void diff_words_flush(struct emit_callback
*ecbdata
)
1996 struct diff_options
*wo
= ecbdata
->diff_words
->opt
;
1998 if (ecbdata
->diff_words
->minus
.text
.size
||
1999 ecbdata
->diff_words
->plus
.text
.size
)
2000 diff_words_show(ecbdata
->diff_words
);
2002 if (wo
->emitted_symbols
) {
2003 struct diff_options
*o
= ecbdata
->opt
;
2004 struct emitted_diff_symbols
*wol
= wo
->emitted_symbols
;
2009 * Instead of appending each, concat all words to a line?
2011 for (i
= 0; i
< wol
->nr
; i
++)
2012 append_emitted_diff_symbol(o
, &wol
->buf
[i
]);
2014 for (i
= 0; i
< wol
->nr
; i
++)
2015 free((void *)wol
->buf
[i
].line
);
2021 static void diff_filespec_load_driver(struct diff_filespec
*one
)
2023 /* Use already-loaded driver */
2027 if (S_ISREG(one
->mode
))
2028 one
->driver
= userdiff_find_by_path(one
->path
);
2030 /* Fallback to default settings */
2032 one
->driver
= userdiff_find_by_name("default");
2035 static const char *userdiff_word_regex(struct diff_filespec
*one
)
2037 diff_filespec_load_driver(one
);
2038 return one
->driver
->word_regex
;
2041 static void init_diff_words_data(struct emit_callback
*ecbdata
,
2042 struct diff_options
*orig_opts
,
2043 struct diff_filespec
*one
,
2044 struct diff_filespec
*two
)
2047 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
2048 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
2050 ecbdata
->diff_words
=
2051 xcalloc(1, sizeof(struct diff_words_data
));
2052 ecbdata
->diff_words
->type
= o
->word_diff
;
2053 ecbdata
->diff_words
->opt
= o
;
2055 if (orig_opts
->emitted_symbols
)
2056 o
->emitted_symbols
=
2057 xcalloc(1, sizeof(struct emitted_diff_symbols
));
2060 o
->word_regex
= userdiff_word_regex(one
);
2062 o
->word_regex
= userdiff_word_regex(two
);
2064 o
->word_regex
= diff_word_regex_cfg
;
2065 if (o
->word_regex
) {
2066 ecbdata
->diff_words
->word_regex
= (regex_t
*)
2067 xmalloc(sizeof(regex_t
));
2068 if (regcomp(ecbdata
->diff_words
->word_regex
,
2070 REG_EXTENDED
| REG_NEWLINE
))
2071 die ("Invalid regular expression: %s",
2074 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
2075 if (o
->word_diff
== diff_words_styles
[i
].type
) {
2076 ecbdata
->diff_words
->style
=
2077 &diff_words_styles
[i
];
2081 if (want_color(o
->use_color
)) {
2082 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
2083 st
->old_word
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2084 st
->new_word
.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2085 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
2089 static void free_diff_words_data(struct emit_callback
*ecbdata
)
2091 if (ecbdata
->diff_words
) {
2092 diff_words_flush(ecbdata
);
2093 free (ecbdata
->diff_words
->opt
->emitted_symbols
);
2094 free (ecbdata
->diff_words
->opt
);
2095 free (ecbdata
->diff_words
->minus
.text
.ptr
);
2096 free (ecbdata
->diff_words
->minus
.orig
);
2097 free (ecbdata
->diff_words
->plus
.text
.ptr
);
2098 free (ecbdata
->diff_words
->plus
.orig
);
2099 if (ecbdata
->diff_words
->word_regex
) {
2100 regfree(ecbdata
->diff_words
->word_regex
);
2101 free(ecbdata
->diff_words
->word_regex
);
2103 FREE_AND_NULL(ecbdata
->diff_words
);
2107 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
2109 if (want_color(diff_use_color
))
2110 return diff_colors
[ix
];
2114 const char *diff_line_prefix(struct diff_options
*opt
)
2116 struct strbuf
*msgbuf
;
2117 if (!opt
->output_prefix
)
2120 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
2124 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
2127 unsigned long allot
;
2133 (void) utf8_width(&cp
, &l
);
2135 break; /* truncated in the middle? */
2140 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
2143 ecbdata
->lno_in_preimage
= 0;
2144 ecbdata
->lno_in_postimage
= 0;
2145 p
= strchr(line
, '-');
2147 return; /* cannot happen */
2148 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
2151 return; /* cannot happen */
2152 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
2155 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
2157 struct emit_callback
*ecbdata
= priv
;
2158 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
2159 struct diff_options
*o
= ecbdata
->opt
;
2161 o
->found_changes
= 1;
2163 if (ecbdata
->header
) {
2164 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
2165 ecbdata
->header
->buf
, ecbdata
->header
->len
, 0);
2166 strbuf_reset(ecbdata
->header
);
2167 ecbdata
->header
= NULL
;
2170 if (ecbdata
->label_path
[0]) {
2171 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
2172 ecbdata
->label_path
[0],
2173 strlen(ecbdata
->label_path
[0]), 0);
2174 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
2175 ecbdata
->label_path
[1],
2176 strlen(ecbdata
->label_path
[1]), 0);
2177 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
2180 if (diff_suppress_blank_empty
2181 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
2186 if (line
[0] == '@') {
2187 if (ecbdata
->diff_words
)
2188 diff_words_flush(ecbdata
);
2189 len
= sane_truncate_line(ecbdata
, line
, len
);
2190 find_lno(line
, ecbdata
);
2191 emit_hunk_header(ecbdata
, line
, len
);
2195 if (ecbdata
->diff_words
) {
2196 enum diff_symbol s
=
2197 ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
?
2198 DIFF_SYMBOL_WORDS_PORCELAIN
: DIFF_SYMBOL_WORDS
;
2199 if (line
[0] == '-') {
2200 diff_words_append(line
, len
,
2201 &ecbdata
->diff_words
->minus
);
2203 } else if (line
[0] == '+') {
2204 diff_words_append(line
, len
,
2205 &ecbdata
->diff_words
->plus
);
2207 } else if (starts_with(line
, "\\ ")) {
2209 * Eat the "no newline at eof" marker as if we
2210 * saw a "+" or "-" line with nothing on it,
2211 * and return without diff_words_flush() to
2212 * defer processing. If this is the end of
2213 * preimage, more "+" lines may come after it.
2217 diff_words_flush(ecbdata
);
2218 emit_diff_symbol(o
, s
, line
, len
, 0);
2224 ecbdata
->lno_in_postimage
++;
2225 emit_add_line(reset
, ecbdata
, line
+ 1, len
- 1);
2228 ecbdata
->lno_in_preimage
++;
2229 emit_del_line(reset
, ecbdata
, line
+ 1, len
- 1);
2232 ecbdata
->lno_in_postimage
++;
2233 ecbdata
->lno_in_preimage
++;
2234 emit_context_line(reset
, ecbdata
, line
+ 1, len
- 1);
2237 /* incomplete line at the end */
2238 ecbdata
->lno_in_preimage
++;
2239 emit_diff_symbol(o
, DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
2245 static void pprint_rename(struct strbuf
*name
, const char *a
, const char *b
)
2247 const char *old_name
= a
;
2248 const char *new_name
= b
;
2249 int pfx_length
, sfx_length
;
2250 int pfx_adjust_for_slash
;
2251 int len_a
= strlen(a
);
2252 int len_b
= strlen(b
);
2253 int a_midlen
, b_midlen
;
2254 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
2255 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
2257 if (qlen_a
|| qlen_b
) {
2258 quote_c_style(a
, name
, NULL
, 0);
2259 strbuf_addstr(name
, " => ");
2260 quote_c_style(b
, name
, NULL
, 0);
2264 /* Find common prefix */
2266 while (*old_name
&& *new_name
&& *old_name
== *new_name
) {
2267 if (*old_name
== '/')
2268 pfx_length
= old_name
- a
+ 1;
2273 /* Find common suffix */
2274 old_name
= a
+ len_a
;
2275 new_name
= b
+ len_b
;
2278 * If there is a common prefix, it must end in a slash. In
2279 * that case we let this loop run 1 into the prefix to see the
2282 * If there is no common prefix, we cannot do this as it would
2283 * underrun the input strings.
2285 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
2286 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old_name
&&
2287 b
+ pfx_length
- pfx_adjust_for_slash
<= new_name
&&
2288 *old_name
== *new_name
) {
2289 if (*old_name
== '/')
2290 sfx_length
= len_a
- (old_name
- a
);
2296 * pfx{mid-a => mid-b}sfx
2297 * {pfx-a => pfx-b}sfx
2298 * pfx{sfx-a => sfx-b}
2301 a_midlen
= len_a
- pfx_length
- sfx_length
;
2302 b_midlen
= len_b
- pfx_length
- sfx_length
;
2308 strbuf_grow(name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
2309 if (pfx_length
+ sfx_length
) {
2310 strbuf_add(name
, a
, pfx_length
);
2311 strbuf_addch(name
, '{');
2313 strbuf_add(name
, a
+ pfx_length
, a_midlen
);
2314 strbuf_addstr(name
, " => ");
2315 strbuf_add(name
, b
+ pfx_length
, b_midlen
);
2316 if (pfx_length
+ sfx_length
) {
2317 strbuf_addch(name
, '}');
2318 strbuf_add(name
, a
+ len_a
- sfx_length
, sfx_length
);
2325 struct diffstat_file
{
2329 const char *comments
;
2330 unsigned is_unmerged
:1;
2331 unsigned is_binary
:1;
2332 unsigned is_renamed
:1;
2333 unsigned is_interesting
:1;
2334 uintmax_t added
, deleted
;
2338 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
2342 struct diffstat_file
*x
;
2343 x
= xcalloc(1, sizeof(*x
));
2344 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
2345 diffstat
->files
[diffstat
->nr
++] = x
;
2347 x
->from_name
= xstrdup(name_a
);
2348 x
->name
= xstrdup(name_b
);
2352 x
->from_name
= NULL
;
2353 x
->name
= xstrdup(name_a
);
2358 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
2360 struct diffstat_t
*diffstat
= priv
;
2361 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
2365 else if (line
[0] == '-')
2369 const char mime_boundary_leader
[] = "------------";
2371 static int scale_linear(int it
, int width
, int max_change
)
2376 * make sure that at least one '-' or '+' is printed if
2377 * there is any change to this path. The easiest way is to
2378 * scale linearly as if the alloted width is one column shorter
2379 * than it is, and then add 1 to the result.
2381 return 1 + (it
* (width
- 1) / max_change
);
2384 static void show_graph(struct strbuf
*out
, char ch
, int cnt
,
2385 const char *set
, const char *reset
)
2389 strbuf_addstr(out
, set
);
2390 strbuf_addchars(out
, ch
, cnt
);
2391 strbuf_addstr(out
, reset
);
2394 static void fill_print_name(struct diffstat_file
*file
)
2396 struct strbuf pname
= STRBUF_INIT
;
2398 if (file
->print_name
)
2401 if (file
->is_renamed
)
2402 pprint_rename(&pname
, file
->from_name
, file
->name
);
2404 quote_c_style(file
->name
, &pname
, NULL
, 0);
2407 strbuf_addf(&pname
, " (%s)", file
->comments
);
2409 file
->print_name
= strbuf_detach(&pname
, NULL
);
2412 static void print_stat_summary_inserts_deletes(struct diff_options
*options
,
2413 int files
, int insertions
, int deletions
)
2415 struct strbuf sb
= STRBUF_INIT
;
2418 assert(insertions
== 0 && deletions
== 0);
2419 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
2425 (files
== 1) ? " %d file changed" : " %d files changed",
2429 * For binary diff, the caller may want to print "x files
2430 * changed" with insertions == 0 && deletions == 0.
2432 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2433 * is probably less confusing (i.e skip over "2 files changed
2434 * but nothing about added/removed lines? Is this a bug in Git?").
2436 if (insertions
|| deletions
== 0) {
2438 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2442 if (deletions
|| insertions
== 0) {
2444 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2447 strbuf_addch(&sb
, '\n');
2448 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
2450 strbuf_release(&sb
);
2453 void print_stat_summary(FILE *fp
, int files
,
2454 int insertions
, int deletions
)
2456 struct diff_options o
;
2457 memset(&o
, 0, sizeof(o
));
2460 print_stat_summary_inserts_deletes(&o
, files
, insertions
, deletions
);
2463 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
2465 int i
, len
, add
, del
, adds
= 0, dels
= 0;
2466 uintmax_t max_change
= 0, max_len
= 0;
2467 int total_files
= data
->nr
, count
;
2468 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
2469 const char *reset
, *add_c
, *del_c
;
2470 int extra_shown
= 0;
2471 const char *line_prefix
= diff_line_prefix(options
);
2472 struct strbuf out
= STRBUF_INIT
;
2477 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
2479 reset
= diff_get_color_opt(options
, DIFF_RESET
);
2480 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
2481 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
2484 * Find the longest filename and max number of changes
2486 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
2487 struct diffstat_file
*file
= data
->files
[i
];
2488 uintmax_t change
= file
->added
+ file
->deleted
;
2490 if (!file
->is_interesting
&& (change
== 0)) {
2491 count
++; /* not shown == room for one more */
2494 fill_print_name(file
);
2495 len
= strlen(file
->print_name
);
2499 if (file
->is_unmerged
) {
2500 /* "Unmerged" is 8 characters */
2501 bin_width
= bin_width
< 8 ? 8 : bin_width
;
2504 if (file
->is_binary
) {
2505 /* "Bin XXX -> YYY bytes" */
2506 int w
= 14 + decimal_width(file
->added
)
2507 + decimal_width(file
->deleted
);
2508 bin_width
= bin_width
< w
? w
: bin_width
;
2509 /* Display change counts aligned with "Bin" */
2514 if (max_change
< change
)
2515 max_change
= change
;
2517 count
= i
; /* where we can stop scanning in data->files[] */
2520 * We have width = stat_width or term_columns() columns total.
2521 * We want a maximum of min(max_len, stat_name_width) for the name part.
2522 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2523 * We also need 1 for " " and 4 + decimal_width(max_change)
2524 * for " | NNNN " and one the empty column at the end, altogether
2525 * 6 + decimal_width(max_change).
2527 * If there's not enough space, we will use the smaller of
2528 * stat_name_width (if set) and 5/8*width for the filename,
2529 * and the rest for constant elements + graph part, but no more
2530 * than stat_graph_width for the graph part.
2531 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2532 * for the standard terminal size).
2534 * In other words: stat_width limits the maximum width, and
2535 * stat_name_width fixes the maximum width of the filename,
2536 * and is also used to divide available columns if there
2539 * Binary files are displayed with "Bin XXX -> YYY bytes"
2540 * instead of the change count and graph. This part is treated
2541 * similarly to the graph part, except that it is not
2542 * "scaled". If total width is too small to accommodate the
2543 * guaranteed minimum width of the filename part and the
2544 * separators and this message, this message will "overflow"
2545 * making the line longer than the maximum width.
2548 if (options
->stat_width
== -1)
2549 width
= term_columns() - strlen(line_prefix
);
2551 width
= options
->stat_width
? options
->stat_width
: 80;
2552 number_width
= decimal_width(max_change
) > number_width
?
2553 decimal_width(max_change
) : number_width
;
2555 if (options
->stat_graph_width
== -1)
2556 options
->stat_graph_width
= diff_stat_graph_width
;
2559 * Guarantee 3/8*16==6 for the graph part
2560 * and 5/8*16==10 for the filename part
2562 if (width
< 16 + 6 + number_width
)
2563 width
= 16 + 6 + number_width
;
2566 * First assign sizes that are wanted, ignoring available width.
2567 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2568 * starting from "XXX" should fit in graph_width.
2570 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
2571 if (options
->stat_graph_width
&&
2572 options
->stat_graph_width
< graph_width
)
2573 graph_width
= options
->stat_graph_width
;
2575 name_width
= (options
->stat_name_width
> 0 &&
2576 options
->stat_name_width
< max_len
) ?
2577 options
->stat_name_width
: max_len
;
2580 * Adjust adjustable widths not to exceed maximum width
2582 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
2583 if (graph_width
> width
* 3/8 - number_width
- 6) {
2584 graph_width
= width
* 3/8 - number_width
- 6;
2585 if (graph_width
< 6)
2589 if (options
->stat_graph_width
&&
2590 graph_width
> options
->stat_graph_width
)
2591 graph_width
= options
->stat_graph_width
;
2592 if (name_width
> width
- number_width
- 6 - graph_width
)
2593 name_width
= width
- number_width
- 6 - graph_width
;
2595 graph_width
= width
- number_width
- 6 - name_width
;
2599 * From here name_width is the width of the name area,
2600 * and graph_width is the width of the graph area.
2601 * max_change is used to scale graph properly.
2603 for (i
= 0; i
< count
; i
++) {
2604 const char *prefix
= "";
2605 struct diffstat_file
*file
= data
->files
[i
];
2606 char *name
= file
->print_name
;
2607 uintmax_t added
= file
->added
;
2608 uintmax_t deleted
= file
->deleted
;
2611 if (!file
->is_interesting
&& (added
+ deleted
== 0))
2615 * "scale" the filename
2618 name_len
= strlen(name
);
2619 if (name_width
< name_len
) {
2623 name
+= name_len
- len
;
2624 slash
= strchr(name
, '/');
2629 if (file
->is_binary
) {
2630 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2631 strbuf_addf(&out
, " %*s", number_width
, "Bin");
2632 if (!added
&& !deleted
) {
2633 strbuf_addch(&out
, '\n');
2634 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2635 out
.buf
, out
.len
, 0);
2639 strbuf_addf(&out
, " %s%"PRIuMAX
"%s",
2640 del_c
, deleted
, reset
);
2641 strbuf_addstr(&out
, " -> ");
2642 strbuf_addf(&out
, "%s%"PRIuMAX
"%s",
2643 add_c
, added
, reset
);
2644 strbuf_addstr(&out
, " bytes\n");
2645 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2646 out
.buf
, out
.len
, 0);
2650 else if (file
->is_unmerged
) {
2651 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2652 strbuf_addstr(&out
, " Unmerged\n");
2653 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2654 out
.buf
, out
.len
, 0);
2660 * scale the add/delete
2665 if (graph_width
<= max_change
) {
2666 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
2667 if (total
< 2 && add
&& del
)
2668 /* width >= 2 due to the sanity check */
2671 add
= scale_linear(add
, graph_width
, max_change
);
2674 del
= scale_linear(del
, graph_width
, max_change
);
2678 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2679 strbuf_addf(&out
, " %*"PRIuMAX
"%s",
2680 number_width
, added
+ deleted
,
2681 added
+ deleted
? " " : "");
2682 show_graph(&out
, '+', add
, add_c
, reset
);
2683 show_graph(&out
, '-', del
, del_c
, reset
);
2684 strbuf_addch(&out
, '\n');
2685 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2686 out
.buf
, out
.len
, 0);
2690 for (i
= 0; i
< data
->nr
; i
++) {
2691 struct diffstat_file
*file
= data
->files
[i
];
2692 uintmax_t added
= file
->added
;
2693 uintmax_t deleted
= file
->deleted
;
2695 if (file
->is_unmerged
||
2696 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
2701 if (!file
->is_binary
) {
2708 emit_diff_symbol(options
,
2709 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
2714 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2715 strbuf_release(&out
);
2718 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
2720 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
2725 for (i
= 0; i
< data
->nr
; i
++) {
2726 int added
= data
->files
[i
]->added
;
2727 int deleted
= data
->files
[i
]->deleted
;
2729 if (data
->files
[i
]->is_unmerged
||
2730 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
2732 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
2737 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2740 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
2747 for (i
= 0; i
< data
->nr
; i
++) {
2748 struct diffstat_file
*file
= data
->files
[i
];
2750 fprintf(options
->file
, "%s", diff_line_prefix(options
));
2752 if (file
->is_binary
)
2753 fprintf(options
->file
, "-\t-\t");
2755 fprintf(options
->file
,
2756 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
2757 file
->added
, file
->deleted
);
2758 if (options
->line_termination
) {
2759 fill_print_name(file
);
2760 if (!file
->is_renamed
)
2761 write_name_quoted(file
->name
, options
->file
,
2762 options
->line_termination
);
2764 fputs(file
->print_name
, options
->file
);
2765 putc(options
->line_termination
, options
->file
);
2768 if (file
->is_renamed
) {
2769 putc('\0', options
->file
);
2770 write_name_quoted(file
->from_name
, options
->file
, '\0');
2772 write_name_quoted(file
->name
, options
->file
, '\0');
2777 struct dirstat_file
{
2779 unsigned long changed
;
2782 struct dirstat_dir
{
2783 struct dirstat_file
*files
;
2784 int alloc
, nr
, permille
, cumulative
;
2787 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
2788 unsigned long changed
, const char *base
, int baselen
)
2790 unsigned long sum_changes
= 0;
2791 unsigned int sources
= 0;
2792 const char *line_prefix
= diff_line_prefix(opt
);
2795 struct dirstat_file
*f
= dir
->files
;
2796 int namelen
= strlen(f
->name
);
2797 unsigned long changes
;
2800 if (namelen
< baselen
)
2802 if (memcmp(f
->name
, base
, baselen
))
2804 slash
= strchr(f
->name
+ baselen
, '/');
2806 int newbaselen
= slash
+ 1 - f
->name
;
2807 changes
= gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
2810 changes
= f
->changed
;
2815 sum_changes
+= changes
;
2819 * We don't report dirstat's for
2821 * - or cases where everything came from a single directory
2822 * under this directory (sources == 1).
2824 if (baselen
&& sources
!= 1) {
2826 int permille
= sum_changes
* 1000 / changed
;
2827 if (permille
>= dir
->permille
) {
2828 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
2829 permille
/ 10, permille
% 10, baselen
, base
);
2830 if (!dir
->cumulative
)
2838 static int dirstat_compare(const void *_a
, const void *_b
)
2840 const struct dirstat_file
*a
= _a
;
2841 const struct dirstat_file
*b
= _b
;
2842 return strcmp(a
->name
, b
->name
);
2845 static void show_dirstat(struct diff_options
*options
)
2848 unsigned long changed
;
2849 struct dirstat_dir dir
;
2850 struct diff_queue_struct
*q
= &diff_queued_diff
;
2855 dir
.permille
= options
->dirstat_permille
;
2856 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
2859 for (i
= 0; i
< q
->nr
; i
++) {
2860 struct diff_filepair
*p
= q
->queue
[i
];
2862 unsigned long copied
, added
, damage
;
2863 int content_changed
;
2865 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
2867 if (p
->one
->oid_valid
&& p
->two
->oid_valid
)
2868 content_changed
= oidcmp(&p
->one
->oid
, &p
->two
->oid
);
2870 content_changed
= 1;
2872 if (!content_changed
) {
2874 * The SHA1 has not changed, so pre-/post-content is
2875 * identical. We can therefore skip looking at the
2876 * file contents altogether.
2882 if (options
->flags
.dirstat_by_file
) {
2884 * In --dirstat-by-file mode, we don't really need to
2885 * look at the actual file contents at all.
2886 * The fact that the SHA1 changed is enough for us to
2887 * add this file to the list of results
2888 * (with each file contributing equal damage).
2894 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
2895 diff_populate_filespec(p
->one
, 0);
2896 diff_populate_filespec(p
->two
, 0);
2897 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
,
2899 diff_free_filespec_data(p
->one
);
2900 diff_free_filespec_data(p
->two
);
2901 } else if (DIFF_FILE_VALID(p
->one
)) {
2902 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
);
2904 diff_free_filespec_data(p
->one
);
2905 } else if (DIFF_FILE_VALID(p
->two
)) {
2906 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
);
2908 added
= p
->two
->size
;
2909 diff_free_filespec_data(p
->two
);
2914 * Original minus copied is the removed material,
2915 * added is the new material. They are both damages
2916 * made to the preimage.
2917 * If the resulting damage is zero, we know that
2918 * diffcore_count_changes() considers the two entries to
2919 * be identical, but since content_changed is true, we
2920 * know that there must have been _some_ kind of change,
2921 * so we force all entries to have damage > 0.
2923 damage
= (p
->one
->size
- copied
) + added
;
2928 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2929 dir
.files
[dir
.nr
].name
= name
;
2930 dir
.files
[dir
.nr
].changed
= damage
;
2935 /* This can happen even with many files, if everything was renames */
2939 /* Show all directories with more than x% of the changes */
2940 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
2941 gather_dirstat(options
, &dir
, changed
, "", 0);
2944 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
2947 unsigned long changed
;
2948 struct dirstat_dir dir
;
2956 dir
.permille
= options
->dirstat_permille
;
2957 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
2960 for (i
= 0; i
< data
->nr
; i
++) {
2961 struct diffstat_file
*file
= data
->files
[i
];
2962 unsigned long damage
= file
->added
+ file
->deleted
;
2963 if (file
->is_binary
)
2965 * binary files counts bytes, not lines. Must find some
2966 * way to normalize binary bytes vs. textual lines.
2967 * The following heuristic assumes that there are 64
2969 * This is stupid and ugly, but very cheap...
2971 damage
= DIV_ROUND_UP(damage
, 64);
2972 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2973 dir
.files
[dir
.nr
].name
= file
->name
;
2974 dir
.files
[dir
.nr
].changed
= damage
;
2979 /* This can happen even with many files, if everything was renames */
2983 /* Show all directories with more than x% of the changes */
2984 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
2985 gather_dirstat(options
, &dir
, changed
, "", 0);
2988 static void free_diffstat_info(struct diffstat_t
*diffstat
)
2991 for (i
= 0; i
< diffstat
->nr
; i
++) {
2992 struct diffstat_file
*f
= diffstat
->files
[i
];
2993 free(f
->print_name
);
2998 free(diffstat
->files
);
3001 struct checkdiff_t
{
3002 const char *filename
;
3004 int conflict_marker_size
;
3005 struct diff_options
*o
;
3010 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
3015 if (len
< marker_size
+ 1)
3017 firstchar
= line
[0];
3018 switch (firstchar
) {
3019 case '=': case '>': case '<': case '|':
3024 for (cnt
= 1; cnt
< marker_size
; cnt
++)
3025 if (line
[cnt
] != firstchar
)
3027 /* line[1] thru line[marker_size-1] are same as firstchar */
3028 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
3033 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
3035 struct checkdiff_t
*data
= priv
;
3036 int marker_size
= data
->conflict_marker_size
;
3037 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
3038 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
3039 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
3041 const char *line_prefix
;
3044 line_prefix
= diff_line_prefix(data
->o
);
3046 if (line
[0] == '+') {
3049 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
3051 fprintf(data
->o
->file
,
3052 "%s%s:%d: leftover conflict marker\n",
3053 line_prefix
, data
->filename
, data
->lineno
);
3055 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
3058 data
->status
|= bad
;
3059 err
= whitespace_error_string(bad
);
3060 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
3061 line_prefix
, data
->filename
, data
->lineno
, err
);
3063 emit_line(data
->o
, set
, reset
, line
, 1);
3064 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
3065 data
->o
->file
, set
, reset
, ws
);
3066 } else if (line
[0] == ' ') {
3068 } else if (line
[0] == '@') {
3069 char *plus
= strchr(line
, '+');
3071 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
3073 die("invalid diff");
3077 static unsigned char *deflate_it(char *data
,
3079 unsigned long *result_size
)
3082 unsigned char *deflated
;
3085 git_deflate_init(&stream
, zlib_compression_level
);
3086 bound
= git_deflate_bound(&stream
, size
);
3087 deflated
= xmalloc(bound
);
3088 stream
.next_out
= deflated
;
3089 stream
.avail_out
= bound
;
3091 stream
.next_in
= (unsigned char *)data
;
3092 stream
.avail_in
= size
;
3093 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
3095 git_deflate_end(&stream
);
3096 *result_size
= stream
.total_out
;
3100 static void emit_binary_diff_body(struct diff_options
*o
,
3101 mmfile_t
*one
, mmfile_t
*two
)
3107 unsigned long orig_size
;
3108 unsigned long delta_size
;
3109 unsigned long deflate_size
;
3110 unsigned long data_size
;
3112 /* We could do deflated delta, or we could do just deflated two,
3113 * whichever is smaller.
3116 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
3117 if (one
->size
&& two
->size
) {
3118 delta
= diff_delta(one
->ptr
, one
->size
,
3119 two
->ptr
, two
->size
,
3120 &delta_size
, deflate_size
);
3122 void *to_free
= delta
;
3123 orig_size
= delta_size
;
3124 delta
= deflate_it(delta
, delta_size
, &delta_size
);
3129 if (delta
&& delta_size
< deflate_size
) {
3130 char *s
= xstrfmt("%lu", orig_size
);
3131 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
3136 data_size
= delta_size
;
3138 char *s
= xstrfmt("%lu", two
->size
);
3139 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
3144 data_size
= deflate_size
;
3147 /* emit data encoded in base85 */
3151 int bytes
= (52 < data_size
) ? 52 : data_size
;
3155 line
[0] = bytes
+ 'A' - 1;
3157 line
[0] = bytes
- 26 + 'a' - 1;
3158 encode_85(line
+ 1, cp
, bytes
);
3159 cp
= (char *) cp
+ bytes
;
3165 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_BODY
,
3168 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_FOOTER
, NULL
, 0, 0);
3172 static void emit_binary_diff(struct diff_options
*o
,
3173 mmfile_t
*one
, mmfile_t
*two
)
3175 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER
, NULL
, 0, 0);
3176 emit_binary_diff_body(o
, one
, two
);
3177 emit_binary_diff_body(o
, two
, one
);
3180 int diff_filespec_is_binary(struct diff_filespec
*one
)
3182 if (one
->is_binary
== -1) {
3183 diff_filespec_load_driver(one
);
3184 if (one
->driver
->binary
!= -1)
3185 one
->is_binary
= one
->driver
->binary
;
3187 if (!one
->data
&& DIFF_FILE_VALID(one
))
3188 diff_populate_filespec(one
, CHECK_BINARY
);
3189 if (one
->is_binary
== -1 && one
->data
)
3190 one
->is_binary
= buffer_is_binary(one
->data
,
3192 if (one
->is_binary
== -1)
3196 return one
->is_binary
;
3199 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
3201 diff_filespec_load_driver(one
);
3202 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
3205 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
3207 if (!options
->a_prefix
)
3208 options
->a_prefix
= a
;
3209 if (!options
->b_prefix
)
3210 options
->b_prefix
= b
;
3213 struct userdiff_driver
*get_textconv(struct diff_filespec
*one
)
3215 if (!DIFF_FILE_VALID(one
))
3218 diff_filespec_load_driver(one
);
3219 return userdiff_get_textconv(one
->driver
);
3222 static void builtin_diff(const char *name_a
,
3224 struct diff_filespec
*one
,
3225 struct diff_filespec
*two
,
3226 const char *xfrm_msg
,
3227 int must_show_header
,
3228 struct diff_options
*o
,
3229 int complete_rewrite
)
3233 char *a_one
, *b_two
;
3234 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
3235 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
3236 const char *a_prefix
, *b_prefix
;
3237 struct userdiff_driver
*textconv_one
= NULL
;
3238 struct userdiff_driver
*textconv_two
= NULL
;
3239 struct strbuf header
= STRBUF_INIT
;
3240 const char *line_prefix
= diff_line_prefix(o
);
3242 diff_set_mnemonic_prefix(o
, "a/", "b/");
3243 if (o
->flags
.reverse_diff
) {
3244 a_prefix
= o
->b_prefix
;
3245 b_prefix
= o
->a_prefix
;
3247 a_prefix
= o
->a_prefix
;
3248 b_prefix
= o
->b_prefix
;
3251 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
3252 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3253 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
3254 show_submodule_summary(o
, one
->path
? one
->path
: two
->path
,
3255 &one
->oid
, &two
->oid
,
3256 two
->dirty_submodule
);
3258 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
3259 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3260 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
3261 show_submodule_inline_diff(o
, one
->path
? one
->path
: two
->path
,
3262 &one
->oid
, &two
->oid
,
3263 two
->dirty_submodule
);
3267 if (o
->flags
.allow_textconv
) {
3268 textconv_one
= get_textconv(one
);
3269 textconv_two
= get_textconv(two
);
3272 /* Never use a non-valid filename anywhere if at all possible */
3273 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
3274 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
3276 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
3277 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
3278 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
3279 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
3280 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3281 if (lbl
[0][0] == '/') {
3283 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3285 strbuf_addstr(&header
, xfrm_msg
);
3286 must_show_header
= 1;
3288 else if (lbl
[1][0] == '/') {
3289 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3291 strbuf_addstr(&header
, xfrm_msg
);
3292 must_show_header
= 1;
3295 if (one
->mode
!= two
->mode
) {
3296 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3297 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3298 must_show_header
= 1;
3301 strbuf_addstr(&header
, xfrm_msg
);
3304 * we do not run diff between different kind
3307 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3308 goto free_ab_and_return
;
3309 if (complete_rewrite
&&
3310 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
3311 (textconv_two
|| !diff_filespec_is_binary(two
))) {
3312 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3313 header
.buf
, header
.len
, 0);
3314 strbuf_reset(&header
);
3315 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3316 textconv_one
, textconv_two
, o
);
3317 o
->found_changes
= 1;
3318 goto free_ab_and_return
;
3322 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3323 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3325 strbuf_reset(&header
);
3326 goto free_ab_and_return
;
3327 } else if (!o
->flags
.text
&&
3328 ( (!textconv_one
&& diff_filespec_is_binary(one
)) ||
3329 (!textconv_two
&& diff_filespec_is_binary(two
)) )) {
3330 struct strbuf sb
= STRBUF_INIT
;
3331 if (!one
->data
&& !two
->data
&&
3332 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3334 if (!oidcmp(&one
->oid
, &two
->oid
)) {
3335 if (must_show_header
)
3336 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3337 header
.buf
, header
.len
,
3339 goto free_ab_and_return
;
3341 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3342 header
.buf
, header
.len
, 0);
3343 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3344 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3345 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3347 strbuf_release(&sb
);
3348 goto free_ab_and_return
;
3350 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
3351 die("unable to read files to diff");
3352 /* Quite common confusing case */
3353 if (mf1
.size
== mf2
.size
&&
3354 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3355 if (must_show_header
)
3356 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3357 header
.buf
, header
.len
, 0);
3358 goto free_ab_and_return
;
3360 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3361 strbuf_reset(&header
);
3362 if (o
->flags
.binary
)
3363 emit_binary_diff(o
, &mf1
, &mf2
);
3365 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3366 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3367 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3369 strbuf_release(&sb
);
3371 o
->found_changes
= 1;
3373 /* Crazy xdl interfaces.. */
3374 const char *diffopts
= getenv("GIT_DIFF_OPTS");
3378 struct emit_callback ecbdata
;
3379 const struct userdiff_funcname
*pe
;
3381 if (must_show_header
) {
3382 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3383 header
.buf
, header
.len
, 0);
3384 strbuf_reset(&header
);
3387 mf1
.size
= fill_textconv(textconv_one
, one
, &mf1
.ptr
);
3388 mf2
.size
= fill_textconv(textconv_two
, two
, &mf2
.ptr
);
3390 pe
= diff_funcname_pattern(one
);
3392 pe
= diff_funcname_pattern(two
);
3394 memset(&xpp
, 0, sizeof(xpp
));
3395 memset(&xecfg
, 0, sizeof(xecfg
));
3396 memset(&ecbdata
, 0, sizeof(ecbdata
));
3397 ecbdata
.label_path
= lbl
;
3398 ecbdata
.color_diff
= want_color(o
->use_color
);
3399 ecbdata
.ws_rule
= whitespace_rule(name_b
);
3400 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3401 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3403 ecbdata
.header
= header
.len
? &header
: NULL
;
3404 xpp
.flags
= o
->xdl_opts
;
3405 xpp
.anchors
= o
->anchors
;
3406 xpp
.anchors_nr
= o
->anchors_nr
;
3407 xecfg
.ctxlen
= o
->context
;
3408 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3409 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3410 if (o
->flags
.funccontext
)
3411 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3413 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3416 else if (skip_prefix(diffopts
, "--unified=", &v
))
3417 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3418 else if (skip_prefix(diffopts
, "-u", &v
))
3419 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3421 init_diff_words_data(&ecbdata
, o
, one
, two
);
3422 if (xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
3424 die("unable to generate diff for %s", one
->path
);
3426 free_diff_words_data(&ecbdata
);
3431 xdiff_clear_find_func(&xecfg
);
3435 strbuf_release(&header
);
3436 diff_free_filespec_data(one
);
3437 diff_free_filespec_data(two
);
3443 static char *get_compact_summary(const struct diff_filepair
*p
, int is_renamed
)
3446 if (p
->status
== DIFF_STATUS_ADDED
) {
3447 if (S_ISLNK(p
->two
->mode
))
3449 else if ((p
->two
->mode
& 0777) == 0755)
3453 } else if (p
->status
== DIFF_STATUS_DELETED
)
3456 if (S_ISLNK(p
->one
->mode
) && !S_ISLNK(p
->two
->mode
))
3458 else if (!S_ISLNK(p
->one
->mode
) && S_ISLNK(p
->two
->mode
))
3460 else if ((p
->one
->mode
& 0777) == 0644 &&
3461 (p
->two
->mode
& 0777) == 0755)
3463 else if ((p
->one
->mode
& 0777) == 0755 &&
3464 (p
->two
->mode
& 0777) == 0644)
3469 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3470 struct diff_filespec
*one
,
3471 struct diff_filespec
*two
,
3472 struct diffstat_t
*diffstat
,
3473 struct diff_options
*o
,
3474 struct diff_filepair
*p
)
3477 struct diffstat_file
*data
;
3479 int complete_rewrite
= 0;
3481 if (!DIFF_PAIR_UNMERGED(p
)) {
3482 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3483 complete_rewrite
= 1;
3486 data
= diffstat_add(diffstat
, name_a
, name_b
);
3487 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3488 if (o
->flags
.stat_with_summary
)
3489 data
->comments
= get_compact_summary(p
, data
->is_renamed
);
3492 data
->is_unmerged
= 1;
3496 same_contents
= !oidcmp(&one
->oid
, &two
->oid
);
3498 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
3499 data
->is_binary
= 1;
3500 if (same_contents
) {
3504 data
->added
= diff_filespec_size(two
);
3505 data
->deleted
= diff_filespec_size(one
);
3509 else if (complete_rewrite
) {
3510 diff_populate_filespec(one
, 0);
3511 diff_populate_filespec(two
, 0);
3512 data
->deleted
= count_lines(one
->data
, one
->size
);
3513 data
->added
= count_lines(two
->data
, two
->size
);
3516 else if (!same_contents
) {
3517 /* Crazy xdl interfaces.. */
3521 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
3522 die("unable to read files to diff");
3524 memset(&xpp
, 0, sizeof(xpp
));
3525 memset(&xecfg
, 0, sizeof(xecfg
));
3526 xpp
.flags
= o
->xdl_opts
;
3527 xpp
.anchors
= o
->anchors
;
3528 xpp
.anchors_nr
= o
->anchors_nr
;
3529 xecfg
.ctxlen
= o
->context
;
3530 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3531 if (xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
3533 die("unable to generate diffstat for %s", one
->path
);
3536 diff_free_filespec_data(one
);
3537 diff_free_filespec_data(two
);
3540 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3541 const char *attr_path
,
3542 struct diff_filespec
*one
,
3543 struct diff_filespec
*two
,
3544 struct diff_options
*o
)
3547 struct checkdiff_t data
;
3552 memset(&data
, 0, sizeof(data
));
3553 data
.filename
= name_b
? name_b
: name_a
;
3556 data
.ws_rule
= whitespace_rule(attr_path
);
3557 data
.conflict_marker_size
= ll_merge_marker_size(attr_path
);
3559 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
3560 die("unable to read files to diff");
3563 * All the other codepaths check both sides, but not checking
3564 * the "old" side here is deliberate. We are checking the newly
3565 * introduced changes, and as long as the "new" side is text, we
3566 * can and should check what it introduces.
3568 if (diff_filespec_is_binary(two
))
3569 goto free_and_return
;
3571 /* Crazy xdl interfaces.. */
3575 memset(&xpp
, 0, sizeof(xpp
));
3576 memset(&xecfg
, 0, sizeof(xecfg
));
3577 xecfg
.ctxlen
= 1; /* at least one context line */
3579 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
3581 die("unable to generate checkdiff for %s", one
->path
);
3583 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3584 struct emit_callback ecbdata
;
3587 ecbdata
.ws_rule
= data
.ws_rule
;
3588 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3589 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3594 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3595 fprintf(o
->file
, "%s:%d: %s.\n",
3596 data
.filename
, blank_at_eof
, err
);
3597 data
.status
= 1; /* report errors */
3602 diff_free_filespec_data(one
);
3603 diff_free_filespec_data(two
);
3605 o
->flags
.check_failed
= 1;
3608 struct diff_filespec
*alloc_filespec(const char *path
)
3610 struct diff_filespec
*spec
;
3612 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3614 spec
->is_binary
= -1;
3618 void free_filespec(struct diff_filespec
*spec
)
3620 if (!--spec
->count
) {
3621 diff_free_filespec_data(spec
);
3626 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3627 int oid_valid
, unsigned short mode
)
3630 spec
->mode
= canon_mode(mode
);
3631 oidcpy(&spec
->oid
, oid
);
3632 spec
->oid_valid
= oid_valid
;
3637 * Given a name and sha1 pair, if the index tells us the file in
3638 * the work tree has that object contents, return true, so that
3639 * prepare_temp_file() does not have to inflate and extract.
3641 static int reuse_worktree_file(const char *name
, const struct object_id
*oid
, int want_file
)
3643 const struct cache_entry
*ce
;
3648 * We do not read the cache ourselves here, because the
3649 * benchmark with my previous version that always reads cache
3650 * shows that it makes things worse for diff-tree comparing
3651 * two linux-2.6 kernel trees in an already checked out work
3652 * tree. This is because most diff-tree comparisons deal with
3653 * only a small number of files, while reading the cache is
3654 * expensive for a large project, and its cost outweighs the
3655 * savings we get by not inflating the object to a temporary
3656 * file. Practically, this code only helps when we are used
3657 * by diff-cache --cached, which does read the cache before
3663 /* We want to avoid the working directory if our caller
3664 * doesn't need the data in a normal file, this system
3665 * is rather slow with its stat/open/mmap/close syscalls,
3666 * and the object is contained in a pack file. The pack
3667 * is probably already open and will be faster to obtain
3668 * the data through than the working directory. Loose
3669 * objects however would tend to be slower as they need
3670 * to be individually opened and inflated.
3672 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(oid
->hash
))
3676 * Similarly, if we'd have to convert the file contents anyway, that
3677 * makes the optimization not worthwhile.
3679 if (!want_file
&& would_convert_to_git(&the_index
, name
))
3683 pos
= cache_name_pos(name
, len
);
3686 ce
= active_cache
[pos
];
3689 * This is not the sha1 we are looking for, or
3690 * unreusable because it is not a regular file.
3692 if (oidcmp(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
3696 * If ce is marked as "assume unchanged", there is no
3697 * guarantee that work tree matches what we are looking for.
3699 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
3703 * If ce matches the file in the work tree, we can reuse it.
3705 if (ce_uptodate(ce
) ||
3706 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
3712 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
3714 struct strbuf buf
= STRBUF_INIT
;
3717 /* Are we looking at the work tree? */
3718 if (s
->dirty_submodule
)
3721 strbuf_addf(&buf
, "Subproject commit %s%s\n",
3722 oid_to_hex(&s
->oid
), dirty
);
3726 strbuf_release(&buf
);
3728 s
->data
= strbuf_detach(&buf
, NULL
);
3735 * While doing rename detection and pickaxe operation, we may need to
3736 * grab the data for the blob (or file) for our own in-core comparison.
3737 * diff_filespec has data and size fields for this purpose.
3739 int diff_populate_filespec(struct diff_filespec
*s
, unsigned int flags
)
3741 int size_only
= flags
& CHECK_SIZE_ONLY
;
3743 int conv_flags
= global_conv_flags_eol
;
3745 * demote FAIL to WARN to allow inspecting the situation
3746 * instead of refusing.
3748 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
3749 conv_flags
= CONV_EOL_RNDTRP_WARN
;
3751 if (!DIFF_FILE_VALID(s
))
3752 die("internal error: asking to populate invalid file.");
3753 if (S_ISDIR(s
->mode
))
3759 if (size_only
&& 0 < s
->size
)
3762 if (S_ISGITLINK(s
->mode
))
3763 return diff_populate_gitlink(s
, size_only
);
3765 if (!s
->oid_valid
||
3766 reuse_worktree_file(s
->path
, &s
->oid
, 0)) {
3767 struct strbuf buf
= STRBUF_INIT
;
3771 if (lstat(s
->path
, &st
) < 0) {
3775 s
->data
= (char *)"";
3779 s
->size
= xsize_t(st
.st_size
);
3782 if (S_ISLNK(st
.st_mode
)) {
3783 struct strbuf sb
= STRBUF_INIT
;
3785 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
3788 s
->data
= strbuf_detach(&sb
, NULL
);
3794 * Even if the caller would be happy with getting
3795 * only the size, we cannot return early at this
3796 * point if the path requires us to run the content
3799 if (size_only
&& !would_convert_to_git(&the_index
, s
->path
))
3803 * Note: this check uses xsize_t(st.st_size) that may
3804 * not be the true size of the blob after it goes
3805 * through convert_to_git(). This may not strictly be
3806 * correct, but the whole point of big_file_threshold
3807 * and is_binary check being that we want to avoid
3808 * opening the file and inspecting the contents, this
3811 if ((flags
& CHECK_BINARY
) &&
3812 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
3816 fd
= open(s
->path
, O_RDONLY
);
3819 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
3821 s
->should_munmap
= 1;
3824 * Convert from working tree format to canonical git format
3826 if (convert_to_git(&the_index
, s
->path
, s
->data
, s
->size
, &buf
, conv_flags
)) {
3828 munmap(s
->data
, s
->size
);
3829 s
->should_munmap
= 0;
3830 s
->data
= strbuf_detach(&buf
, &size
);
3836 enum object_type type
;
3837 if (size_only
|| (flags
& CHECK_BINARY
)) {
3838 type
= oid_object_info(&s
->oid
, &s
->size
);
3840 die("unable to read %s",
3841 oid_to_hex(&s
->oid
));
3844 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
3849 s
->data
= read_object_file(&s
->oid
, &type
, &s
->size
);
3851 die("unable to read %s", oid_to_hex(&s
->oid
));
3857 void diff_free_filespec_blob(struct diff_filespec
*s
)
3861 else if (s
->should_munmap
)
3862 munmap(s
->data
, s
->size
);
3864 if (s
->should_free
|| s
->should_munmap
) {
3865 s
->should_free
= s
->should_munmap
= 0;
3870 void diff_free_filespec_data(struct diff_filespec
*s
)
3872 diff_free_filespec_blob(s
);
3873 FREE_AND_NULL(s
->cnt_data
);
3876 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
3879 const struct object_id
*oid
,
3882 struct strbuf buf
= STRBUF_INIT
;
3883 struct strbuf tempfile
= STRBUF_INIT
;
3884 char *path_dup
= xstrdup(path
);
3885 const char *base
= basename(path_dup
);
3887 /* Generate "XXXXXX_basename.ext" */
3888 strbuf_addstr(&tempfile
, "XXXXXX_");
3889 strbuf_addstr(&tempfile
, base
);
3891 temp
->tempfile
= mks_tempfile_ts(tempfile
.buf
, strlen(base
) + 1);
3892 if (!temp
->tempfile
)
3893 die_errno("unable to create temp-file");
3894 if (convert_to_working_tree(path
,
3895 (const char *)blob
, (size_t)size
, &buf
)) {
3899 if (write_in_full(temp
->tempfile
->fd
, blob
, size
) < 0 ||
3900 close_tempfile_gently(temp
->tempfile
))
3901 die_errno("unable to write temp-file");
3902 temp
->name
= get_tempfile_path(temp
->tempfile
);
3903 oid_to_hex_r(temp
->hex
, oid
);
3904 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
3905 strbuf_release(&buf
);
3906 strbuf_release(&tempfile
);
3910 static struct diff_tempfile
*prepare_temp_file(const char *name
,
3911 struct diff_filespec
*one
)
3913 struct diff_tempfile
*temp
= claim_diff_tempfile();
3915 if (!DIFF_FILE_VALID(one
)) {
3917 /* A '-' entry produces this for file-2, and
3918 * a '+' entry produces this for file-1.
3920 temp
->name
= "/dev/null";
3921 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
3922 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
3926 if (!S_ISGITLINK(one
->mode
) &&
3928 reuse_worktree_file(name
, &one
->oid
, 1))) {
3930 if (lstat(name
, &st
) < 0) {
3931 if (errno
== ENOENT
)
3932 goto not_a_valid_file
;
3933 die_errno("stat(%s)", name
);
3935 if (S_ISLNK(st
.st_mode
)) {
3936 struct strbuf sb
= STRBUF_INIT
;
3937 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
3938 die_errno("readlink(%s)", name
);
3939 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
3941 &one
->oid
: &null_oid
),
3943 one
->mode
: S_IFLNK
));
3944 strbuf_release(&sb
);
3947 /* we can borrow from the file in the work tree */
3949 if (!one
->oid_valid
)
3950 oid_to_hex_r(temp
->hex
, &null_oid
);
3952 oid_to_hex_r(temp
->hex
, &one
->oid
);
3953 /* Even though we may sometimes borrow the
3954 * contents from the work tree, we always want
3955 * one->mode. mode is trustworthy even when
3956 * !(one->oid_valid), as long as
3957 * DIFF_FILE_VALID(one).
3959 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
3964 if (diff_populate_filespec(one
, 0))
3965 die("cannot read data blob for %s", one
->path
);
3966 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
3967 &one
->oid
, one
->mode
);
3972 static void add_external_diff_name(struct argv_array
*argv
,
3974 struct diff_filespec
*df
)
3976 struct diff_tempfile
*temp
= prepare_temp_file(name
, df
);
3977 argv_array_push(argv
, temp
->name
);
3978 argv_array_push(argv
, temp
->hex
);
3979 argv_array_push(argv
, temp
->mode
);
3982 /* An external diff command takes:
3984 * diff-cmd name infile1 infile1-sha1 infile1-mode \
3985 * infile2 infile2-sha1 infile2-mode [ rename-to ]
3988 static void run_external_diff(const char *pgm
,
3991 struct diff_filespec
*one
,
3992 struct diff_filespec
*two
,
3993 const char *xfrm_msg
,
3994 int complete_rewrite
,
3995 struct diff_options
*o
)
3997 struct argv_array argv
= ARGV_ARRAY_INIT
;
3998 struct argv_array env
= ARGV_ARRAY_INIT
;
3999 struct diff_queue_struct
*q
= &diff_queued_diff
;
4001 argv_array_push(&argv
, pgm
);
4002 argv_array_push(&argv
, name
);
4005 add_external_diff_name(&argv
, name
, one
);
4007 add_external_diff_name(&argv
, name
, two
);
4009 add_external_diff_name(&argv
, other
, two
);
4010 argv_array_push(&argv
, other
);
4011 argv_array_push(&argv
, xfrm_msg
);
4015 argv_array_pushf(&env
, "GIT_DIFF_PATH_COUNTER=%d", ++o
->diff_path_counter
);
4016 argv_array_pushf(&env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
4018 if (run_command_v_opt_cd_env(argv
.argv
, RUN_USING_SHELL
, NULL
, env
.argv
))
4019 die(_("external diff died, stopping at %s"), name
);
4022 argv_array_clear(&argv
);
4023 argv_array_clear(&env
);
4026 static int similarity_index(struct diff_filepair
*p
)
4028 return p
->score
* 100 / MAX_SCORE
;
4031 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
4033 if (startup_info
->have_repository
)
4034 return find_unique_abbrev(oid
, abbrev
);
4036 char *hex
= oid_to_hex(oid
);
4038 abbrev
= FALLBACK_DEFAULT_ABBREV
;
4039 if (abbrev
> GIT_SHA1_HEXSZ
)
4040 die("BUG: oid abbreviation out of range: %d", abbrev
);
4047 static void fill_metainfo(struct strbuf
*msg
,
4050 struct diff_filespec
*one
,
4051 struct diff_filespec
*two
,
4052 struct diff_options
*o
,
4053 struct diff_filepair
*p
,
4054 int *must_show_header
,
4057 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
4058 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
4059 const char *line_prefix
= diff_line_prefix(o
);
4061 *must_show_header
= 1;
4062 strbuf_init(msg
, PATH_MAX
* 2 + 300);
4063 switch (p
->status
) {
4064 case DIFF_STATUS_COPIED
:
4065 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4066 line_prefix
, set
, similarity_index(p
));
4067 strbuf_addf(msg
, "%s\n%s%scopy from ",
4068 reset
, line_prefix
, set
);
4069 quote_c_style(name
, msg
, NULL
, 0);
4070 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
4071 quote_c_style(other
, msg
, NULL
, 0);
4072 strbuf_addf(msg
, "%s\n", reset
);
4074 case DIFF_STATUS_RENAMED
:
4075 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4076 line_prefix
, set
, similarity_index(p
));
4077 strbuf_addf(msg
, "%s\n%s%srename from ",
4078 reset
, line_prefix
, set
);
4079 quote_c_style(name
, msg
, NULL
, 0);
4080 strbuf_addf(msg
, "%s\n%s%srename to ",
4081 reset
, line_prefix
, set
);
4082 quote_c_style(other
, msg
, NULL
, 0);
4083 strbuf_addf(msg
, "%s\n", reset
);
4085 case DIFF_STATUS_MODIFIED
:
4087 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
4089 set
, similarity_index(p
), reset
);
4094 *must_show_header
= 0;
4096 if (one
&& two
&& oidcmp(&one
->oid
, &two
->oid
)) {
4097 int abbrev
= o
->flags
.full_index
? 40 : DEFAULT_ABBREV
;
4099 if (o
->flags
.binary
) {
4101 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
4102 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
4105 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
4106 diff_abbrev_oid(&one
->oid
, abbrev
),
4107 diff_abbrev_oid(&two
->oid
, abbrev
));
4108 if (one
->mode
== two
->mode
)
4109 strbuf_addf(msg
, " %06o", one
->mode
);
4110 strbuf_addf(msg
, "%s\n", reset
);
4114 static void run_diff_cmd(const char *pgm
,
4117 const char *attr_path
,
4118 struct diff_filespec
*one
,
4119 struct diff_filespec
*two
,
4121 struct diff_options
*o
,
4122 struct diff_filepair
*p
)
4124 const char *xfrm_msg
= NULL
;
4125 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
4126 int must_show_header
= 0;
4129 if (o
->flags
.allow_external
) {
4130 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
4131 if (drv
&& drv
->external
)
4132 pgm
= drv
->external
;
4137 * don't use colors when the header is intended for an
4138 * external diff driver
4140 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
4142 want_color(o
->use_color
) && !pgm
);
4143 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
4147 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
4148 complete_rewrite
, o
);
4152 builtin_diff(name
, other
? other
: name
,
4153 one
, two
, xfrm_msg
, must_show_header
,
4154 o
, complete_rewrite
);
4156 fprintf(o
->file
, "* Unmerged path %s\n", name
);
4159 static void diff_fill_oid_info(struct diff_filespec
*one
)
4161 if (DIFF_FILE_VALID(one
)) {
4162 if (!one
->oid_valid
) {
4164 if (one
->is_stdin
) {
4168 if (lstat(one
->path
, &st
) < 0)
4169 die_errno("stat '%s'", one
->path
);
4170 if (index_path(&one
->oid
, one
->path
, &st
, 0))
4171 die("cannot hash %s", one
->path
);
4178 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
4180 /* Strip the prefix but do not molest /dev/null and absolute paths */
4181 if (*namep
&& **namep
!= '/') {
4182 *namep
+= prefix_length
;
4186 if (*otherp
&& **otherp
!= '/') {
4187 *otherp
+= prefix_length
;
4188 if (**otherp
== '/')
4193 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
4195 const char *pgm
= external_diff();
4197 struct diff_filespec
*one
= p
->one
;
4198 struct diff_filespec
*two
= p
->two
;
4201 const char *attr_path
;
4204 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
4206 if (o
->prefix_length
)
4207 strip_prefix(o
->prefix_length
, &name
, &other
);
4209 if (!o
->flags
.allow_external
)
4212 if (DIFF_PAIR_UNMERGED(p
)) {
4213 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
4214 NULL
, NULL
, NULL
, o
, p
);
4218 diff_fill_oid_info(one
);
4219 diff_fill_oid_info(two
);
4222 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4223 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4225 * a filepair that changes between file and symlink
4226 * needs to be split into deletion and creation.
4228 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4229 run_diff_cmd(NULL
, name
, other
, attr_path
,
4230 one
, null
, &msg
, o
, p
);
4232 strbuf_release(&msg
);
4234 null
= alloc_filespec(one
->path
);
4235 run_diff_cmd(NULL
, name
, other
, attr_path
,
4236 null
, two
, &msg
, o
, p
);
4240 run_diff_cmd(pgm
, name
, other
, attr_path
,
4241 one
, two
, &msg
, o
, p
);
4243 strbuf_release(&msg
);
4246 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4247 struct diffstat_t
*diffstat
)
4252 if (DIFF_PAIR_UNMERGED(p
)) {
4254 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, p
);
4258 name
= p
->one
->path
;
4259 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4261 if (o
->prefix_length
)
4262 strip_prefix(o
->prefix_length
, &name
, &other
);
4264 diff_fill_oid_info(p
->one
);
4265 diff_fill_oid_info(p
->two
);
4267 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, p
);
4270 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4274 const char *attr_path
;
4276 if (DIFF_PAIR_UNMERGED(p
)) {
4281 name
= p
->one
->path
;
4282 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4283 attr_path
= other
? other
: name
;
4285 if (o
->prefix_length
)
4286 strip_prefix(o
->prefix_length
, &name
, &other
);
4288 diff_fill_oid_info(p
->one
);
4289 diff_fill_oid_info(p
->two
);
4291 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4294 void diff_setup(struct diff_options
*options
)
4296 memcpy(options
, &default_diff_options
, sizeof(*options
));
4298 options
->file
= stdout
;
4300 options
->abbrev
= DEFAULT_ABBREV
;
4301 options
->line_termination
= '\n';
4302 options
->break_opt
= -1;
4303 options
->rename_limit
= -1;
4304 options
->dirstat_permille
= diff_dirstat_permille_default
;
4305 options
->context
= diff_context_default
;
4306 options
->interhunkcontext
= diff_interhunk_context_default
;
4307 options
->ws_error_highlight
= ws_error_highlight_default
;
4308 options
->flags
.rename_empty
= 1;
4309 options
->objfind
= NULL
;
4311 /* pathchange left =NULL by default */
4312 options
->change
= diff_change
;
4313 options
->add_remove
= diff_addremove
;
4314 options
->use_color
= diff_use_color_default
;
4315 options
->detect_rename
= diff_detect_rename_default
;
4316 options
->xdl_opts
|= diff_algorithm
;
4317 if (diff_indent_heuristic
)
4318 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4320 options
->orderfile
= diff_order_file_cfg
;
4322 if (diff_no_prefix
) {
4323 options
->a_prefix
= options
->b_prefix
= "";
4324 } else if (!diff_mnemonic_prefix
) {
4325 options
->a_prefix
= "a/";
4326 options
->b_prefix
= "b/";
4329 options
->color_moved
= diff_color_moved_default
;
4332 void diff_setup_done(struct diff_options
*options
)
4334 unsigned check_mask
= DIFF_FORMAT_NAME
|
4335 DIFF_FORMAT_NAME_STATUS
|
4336 DIFF_FORMAT_CHECKDIFF
|
4337 DIFF_FORMAT_NO_OUTPUT
;
4339 if (options
->set_default
)
4340 options
->set_default(options
);
4342 if (HAS_MULTI_BITS(options
->output_format
& check_mask
))
4343 die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
4345 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
4346 die(_("-G, -S and --find-object are mutually exclusive"));
4349 * Most of the time we can say "there are changes"
4350 * only by checking if there are changed paths, but
4351 * --ignore-whitespace* options force us to look
4355 if ((options
->xdl_opts
& XDF_WHITESPACE_FLAGS
))
4356 options
->flags
.diff_from_contents
= 1;
4358 options
->flags
.diff_from_contents
= 0;
4360 if (options
->flags
.find_copies_harder
)
4361 options
->detect_rename
= DIFF_DETECT_COPY
;
4363 if (!options
->flags
.relative_name
)
4364 options
->prefix
= NULL
;
4365 if (options
->prefix
)
4366 options
->prefix_length
= strlen(options
->prefix
);
4368 options
->prefix_length
= 0;
4370 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4371 DIFF_FORMAT_NAME_STATUS
|
4372 DIFF_FORMAT_CHECKDIFF
|
4373 DIFF_FORMAT_NO_OUTPUT
))
4374 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4375 DIFF_FORMAT_NUMSTAT
|
4376 DIFF_FORMAT_DIFFSTAT
|
4377 DIFF_FORMAT_SHORTSTAT
|
4378 DIFF_FORMAT_DIRSTAT
|
4379 DIFF_FORMAT_SUMMARY
|
4383 * These cases always need recursive; we do not drop caller-supplied
4384 * recursive bits for other formats here.
4386 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4387 DIFF_FORMAT_NUMSTAT
|
4388 DIFF_FORMAT_DIFFSTAT
|
4389 DIFF_FORMAT_SHORTSTAT
|
4390 DIFF_FORMAT_DIRSTAT
|
4391 DIFF_FORMAT_SUMMARY
|
4392 DIFF_FORMAT_CHECKDIFF
))
4393 options
->flags
.recursive
= 1;
4395 * Also pickaxe would not work very well if you do not say recursive
4397 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
4398 options
->flags
.recursive
= 1;
4400 * When patches are generated, submodules diffed against the work tree
4401 * must be checked for dirtiness too so it can be shown in the output
4403 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4404 options
->flags
.dirty_submodules
= 1;
4406 if (options
->detect_rename
&& options
->rename_limit
< 0)
4407 options
->rename_limit
= diff_rename_limit_default
;
4408 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
4410 /* read-cache does not die even when it fails
4411 * so it is safe for us to do this here. Also
4412 * it does not smudge active_cache or active_nr
4413 * when it fails, so we do not have to worry about
4414 * cleaning it up ourselves either.
4418 if (40 < options
->abbrev
)
4419 options
->abbrev
= 40; /* full */
4422 * It does not make sense to show the first hit we happened
4423 * to have found. It does not make sense not to return with
4424 * exit code in such a case either.
4426 if (options
->flags
.quick
) {
4427 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4428 options
->flags
.exit_with_status
= 1;
4431 options
->diff_path_counter
= 0;
4433 if (options
->flags
.follow_renames
&& options
->pathspec
.nr
!= 1)
4434 die(_("--follow requires exactly one pathspec"));
4436 if (!options
->use_color
|| external_diff())
4437 options
->color_moved
= 0;
4440 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
4450 if (c
== arg_short
) {
4454 if (val
&& isdigit(c
)) {
4456 int n
= strtoul(arg
, &end
, 10);
4467 eq
= strchrnul(arg
, '=');
4469 if (!len
|| strncmp(arg
, arg_long
, len
))
4474 if (!isdigit(*++eq
))
4476 n
= strtoul(eq
, &end
, 10);
4484 static int diff_scoreopt_parse(const char *opt
);
4486 static inline int short_opt(char opt
, const char **argv
,
4487 const char **optarg
)
4489 const char *arg
= argv
[0];
4490 if (arg
[0] != '-' || arg
[1] != opt
)
4492 if (arg
[2] != '\0') {
4497 die("Option '%c' requires a value", opt
);
4502 int parse_long_opt(const char *opt
, const char **argv
,
4503 const char **optarg
)
4505 const char *arg
= argv
[0];
4506 if (!skip_prefix(arg
, "--", &arg
))
4508 if (!skip_prefix(arg
, opt
, &arg
))
4510 if (*arg
== '=') { /* stuck form: --option=value */
4516 /* separate form: --option value */
4518 die("Option '--%s' requires a value", opt
);
4523 static int stat_opt(struct diff_options
*options
, const char **av
)
4525 const char *arg
= av
[0];
4527 int width
= options
->stat_width
;
4528 int name_width
= options
->stat_name_width
;
4529 int graph_width
= options
->stat_graph_width
;
4530 int count
= options
->stat_count
;
4533 if (!skip_prefix(arg
, "--stat", &arg
))
4534 die("BUG: stat option does not begin with --stat: %s", arg
);
4539 if (skip_prefix(arg
, "-width", &arg
)) {
4541 width
= strtoul(arg
+ 1, &end
, 10);
4542 else if (!*arg
&& !av
[1])
4543 die_want_option("--stat-width");
4545 width
= strtoul(av
[1], &end
, 10);
4548 } else if (skip_prefix(arg
, "-name-width", &arg
)) {
4550 name_width
= strtoul(arg
+ 1, &end
, 10);
4551 else if (!*arg
&& !av
[1])
4552 die_want_option("--stat-name-width");
4554 name_width
= strtoul(av
[1], &end
, 10);
4557 } else if (skip_prefix(arg
, "-graph-width", &arg
)) {
4559 graph_width
= strtoul(arg
+ 1, &end
, 10);
4560 else if (!*arg
&& !av
[1])
4561 die_want_option("--stat-graph-width");
4563 graph_width
= strtoul(av
[1], &end
, 10);
4566 } else if (skip_prefix(arg
, "-count", &arg
)) {
4568 count
= strtoul(arg
+ 1, &end
, 10);
4569 else if (!*arg
&& !av
[1])
4570 die_want_option("--stat-count");
4572 count
= strtoul(av
[1], &end
, 10);
4578 width
= strtoul(arg
+1, &end
, 10);
4580 name_width
= strtoul(end
+1, &end
, 10);
4582 count
= strtoul(end
+1, &end
, 10);
4585 /* Important! This checks all the error cases! */
4588 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4589 options
->stat_name_width
= name_width
;
4590 options
->stat_graph_width
= graph_width
;
4591 options
->stat_width
= width
;
4592 options
->stat_count
= count
;
4596 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
4598 struct strbuf errmsg
= STRBUF_INIT
;
4599 if (parse_dirstat_params(options
, params
, &errmsg
))
4600 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
4602 strbuf_release(&errmsg
);
4604 * The caller knows a dirstat-related option is given from the command
4605 * line; allow it to say "return this_function();"
4607 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
4611 static int parse_submodule_opt(struct diff_options
*options
, const char *value
)
4613 if (parse_submodule_params(options
, value
))
4614 die(_("Failed to parse --submodule option parameter: '%s'"),
4619 static const char diff_status_letters
[] = {
4622 DIFF_STATUS_DELETED
,
4623 DIFF_STATUS_MODIFIED
,
4624 DIFF_STATUS_RENAMED
,
4625 DIFF_STATUS_TYPE_CHANGED
,
4626 DIFF_STATUS_UNKNOWN
,
4627 DIFF_STATUS_UNMERGED
,
4628 DIFF_STATUS_FILTER_AON
,
4629 DIFF_STATUS_FILTER_BROKEN
,
4633 static unsigned int filter_bit
['Z' + 1];
4635 static void prepare_filter_bits(void)
4639 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4640 for (i
= 0; diff_status_letters
[i
]; i
++)
4641 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4645 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4647 return opt
->filter
& filter_bit
[(int) status
];
4650 static int parse_diff_filter_opt(const char *optarg
, struct diff_options
*opt
)
4654 prepare_filter_bits();
4657 * If there is a negation e.g. 'd' in the input, and we haven't
4658 * initialized the filter field with another --diff-filter, start
4659 * from full set of bits, except for AON.
4662 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4663 if (optch
< 'a' || 'z' < optch
)
4665 opt
->filter
= (1 << (ARRAY_SIZE(diff_status_letters
) - 1)) - 1;
4666 opt
->filter
&= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4671 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4675 if ('a' <= optch
&& optch
<= 'z') {
4677 optch
= toupper(optch
);
4682 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
4686 opt
->filter
&= ~bit
;
4693 static void enable_patch_output(int *fmt
) {
4694 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
4695 *fmt
|= DIFF_FORMAT_PATCH
;
4698 static int parse_ws_error_highlight_opt(struct diff_options
*opt
, const char *arg
)
4700 int val
= parse_ws_error_highlight(arg
);
4703 error("unknown value after ws-error-highlight=%.*s",
4707 opt
->ws_error_highlight
= val
;
4711 static int parse_objfind_opt(struct diff_options
*opt
, const char *arg
)
4713 struct object_id oid
;
4715 if (get_oid(arg
, &oid
))
4716 return error("unable to resolve '%s'", arg
);
4719 opt
->objfind
= xcalloc(1, sizeof(*opt
->objfind
));
4721 opt
->pickaxe_opts
|= DIFF_PICKAXE_KIND_OBJFIND
;
4722 opt
->flags
.recursive
= 1;
4723 opt
->flags
.tree_in_recursive
= 1;
4724 oidset_insert(opt
->objfind
, &oid
);
4728 int diff_opt_parse(struct diff_options
*options
,
4729 const char **av
, int ac
, const char *prefix
)
4731 const char *arg
= av
[0];
4738 /* Output format options */
4739 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u") || !strcmp(arg
, "--patch")
4740 || opt_arg(arg
, 'U', "unified", &options
->context
))
4741 enable_patch_output(&options
->output_format
);
4742 else if (!strcmp(arg
, "--raw"))
4743 options
->output_format
|= DIFF_FORMAT_RAW
;
4744 else if (!strcmp(arg
, "--patch-with-raw")) {
4745 enable_patch_output(&options
->output_format
);
4746 options
->output_format
|= DIFF_FORMAT_RAW
;
4747 } else if (!strcmp(arg
, "--numstat"))
4748 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
4749 else if (!strcmp(arg
, "--shortstat"))
4750 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
4751 else if (skip_prefix(arg
, "-X", &arg
) ||
4752 skip_to_optional_arg(arg
, "--dirstat", &arg
))
4753 return parse_dirstat_opt(options
, arg
);
4754 else if (!strcmp(arg
, "--cumulative"))
4755 return parse_dirstat_opt(options
, "cumulative");
4756 else if (skip_to_optional_arg(arg
, "--dirstat-by-file", &arg
)) {
4757 parse_dirstat_opt(options
, "files");
4758 return parse_dirstat_opt(options
, arg
);
4760 else if (!strcmp(arg
, "--check"))
4761 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
4762 else if (!strcmp(arg
, "--summary"))
4763 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
4764 else if (!strcmp(arg
, "--patch-with-stat")) {
4765 enable_patch_output(&options
->output_format
);
4766 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4767 } else if (!strcmp(arg
, "--name-only"))
4768 options
->output_format
|= DIFF_FORMAT_NAME
;
4769 else if (!strcmp(arg
, "--name-status"))
4770 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
4771 else if (!strcmp(arg
, "-s") || !strcmp(arg
, "--no-patch"))
4772 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
4773 else if (starts_with(arg
, "--stat"))
4774 /* --stat, --stat-width, --stat-name-width, or --stat-count */
4775 return stat_opt(options
, av
);
4776 else if (!strcmp(arg
, "--compact-summary")) {
4777 options
->flags
.stat_with_summary
= 1;
4778 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4779 } else if (!strcmp(arg
, "--no-compact-summary"))
4780 options
->flags
.stat_with_summary
= 0;
4782 /* renames options */
4783 else if (starts_with(arg
, "-B") ||
4784 skip_to_optional_arg(arg
, "--break-rewrites", NULL
)) {
4785 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
4786 return error("invalid argument to -B: %s", arg
+2);
4788 else if (starts_with(arg
, "-M") ||
4789 skip_to_optional_arg(arg
, "--find-renames", NULL
)) {
4790 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
4791 return error("invalid argument to -M: %s", arg
+2);
4792 options
->detect_rename
= DIFF_DETECT_RENAME
;
4794 else if (!strcmp(arg
, "-D") || !strcmp(arg
, "--irreversible-delete")) {
4795 options
->irreversible_delete
= 1;
4797 else if (starts_with(arg
, "-C") ||
4798 skip_to_optional_arg(arg
, "--find-copies", NULL
)) {
4799 if (options
->detect_rename
== DIFF_DETECT_COPY
)
4800 options
->flags
.find_copies_harder
= 1;
4801 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
4802 return error("invalid argument to -C: %s", arg
+2);
4803 options
->detect_rename
= DIFF_DETECT_COPY
;
4805 else if (!strcmp(arg
, "--no-renames"))
4806 options
->detect_rename
= 0;
4807 else if (!strcmp(arg
, "--rename-empty"))
4808 options
->flags
.rename_empty
= 1;
4809 else if (!strcmp(arg
, "--no-rename-empty"))
4810 options
->flags
.rename_empty
= 0;
4811 else if (skip_to_optional_arg_default(arg
, "--relative", &arg
, NULL
)) {
4812 options
->flags
.relative_name
= 1;
4814 options
->prefix
= arg
;
4818 else if (!strcmp(arg
, "--minimal"))
4819 DIFF_XDL_SET(options
, NEED_MINIMAL
);
4820 else if (!strcmp(arg
, "--no-minimal"))
4821 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
4822 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
4823 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
4824 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
4825 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
4826 else if (!strcmp(arg
, "--ignore-space-at-eol"))
4827 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
4828 else if (!strcmp(arg
, "--ignore-cr-at-eol"))
4829 DIFF_XDL_SET(options
, IGNORE_CR_AT_EOL
);
4830 else if (!strcmp(arg
, "--ignore-blank-lines"))
4831 DIFF_XDL_SET(options
, IGNORE_BLANK_LINES
);
4832 else if (!strcmp(arg
, "--indent-heuristic"))
4833 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4834 else if (!strcmp(arg
, "--no-indent-heuristic"))
4835 DIFF_XDL_CLR(options
, INDENT_HEURISTIC
);
4836 else if (!strcmp(arg
, "--patience")) {
4838 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
4840 * Both --patience and --anchored use PATIENCE_DIFF
4841 * internally, so remove any anchors previously
4844 for (i
= 0; i
< options
->anchors_nr
; i
++)
4845 free(options
->anchors
[i
]);
4846 options
->anchors_nr
= 0;
4847 } else if (!strcmp(arg
, "--histogram"))
4848 options
->xdl_opts
= DIFF_WITH_ALG(options
, HISTOGRAM_DIFF
);
4849 else if ((argcount
= parse_long_opt("diff-algorithm", av
, &optarg
))) {
4850 long value
= parse_algorithm_value(optarg
);
4852 return error("option diff-algorithm accepts \"myers\", "
4853 "\"minimal\", \"patience\" and \"histogram\"");
4854 /* clear out previous settings */
4855 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
4856 options
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
4857 options
->xdl_opts
|= value
;
4859 } else if (skip_prefix(arg
, "--anchored=", &arg
)) {
4860 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
4861 ALLOC_GROW(options
->anchors
, options
->anchors_nr
+ 1,
4862 options
->anchors_alloc
);
4863 options
->anchors
[options
->anchors_nr
++] = xstrdup(arg
);
4867 else if (!strcmp(arg
, "--binary")) {
4868 enable_patch_output(&options
->output_format
);
4869 options
->flags
.binary
= 1;
4871 else if (!strcmp(arg
, "--full-index"))
4872 options
->flags
.full_index
= 1;
4873 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
4874 options
->flags
.text
= 1;
4875 else if (!strcmp(arg
, "-R"))
4876 options
->flags
.reverse_diff
= 1;
4877 else if (!strcmp(arg
, "--find-copies-harder"))
4878 options
->flags
.find_copies_harder
= 1;
4879 else if (!strcmp(arg
, "--follow"))
4880 options
->flags
.follow_renames
= 1;
4881 else if (!strcmp(arg
, "--no-follow")) {
4882 options
->flags
.follow_renames
= 0;
4883 options
->flags
.default_follow_renames
= 0;
4884 } else if (skip_to_optional_arg_default(arg
, "--color", &arg
, "always")) {
4885 int value
= git_config_colorbool(NULL
, arg
);
4887 return error("option `color' expects \"always\", \"auto\", or \"never\"");
4888 options
->use_color
= value
;
4890 else if (!strcmp(arg
, "--no-color"))
4891 options
->use_color
= 0;
4892 else if (!strcmp(arg
, "--color-moved")) {
4893 if (diff_color_moved_default
)
4894 options
->color_moved
= diff_color_moved_default
;
4895 if (options
->color_moved
== COLOR_MOVED_NO
)
4896 options
->color_moved
= COLOR_MOVED_DEFAULT
;
4897 } else if (!strcmp(arg
, "--no-color-moved"))
4898 options
->color_moved
= COLOR_MOVED_NO
;
4899 else if (skip_prefix(arg
, "--color-moved=", &arg
)) {
4900 int cm
= parse_color_moved(arg
);
4902 die("bad --color-moved argument: %s", arg
);
4903 options
->color_moved
= cm
;
4904 } else if (skip_prefix(arg
, "--color-moved-ws=", &arg
)) {
4905 options
->color_moved_ws_handling
= parse_color_moved_ws(arg
);
4906 } else if (skip_to_optional_arg_default(arg
, "--color-words", &options
->word_regex
, NULL
)) {
4907 options
->use_color
= 1;
4908 options
->word_diff
= DIFF_WORDS_COLOR
;
4910 else if (!strcmp(arg
, "--word-diff")) {
4911 if (options
->word_diff
== DIFF_WORDS_NONE
)
4912 options
->word_diff
= DIFF_WORDS_PLAIN
;
4914 else if (skip_prefix(arg
, "--word-diff=", &arg
)) {
4915 if (!strcmp(arg
, "plain"))
4916 options
->word_diff
= DIFF_WORDS_PLAIN
;
4917 else if (!strcmp(arg
, "color")) {
4918 options
->use_color
= 1;
4919 options
->word_diff
= DIFF_WORDS_COLOR
;
4921 else if (!strcmp(arg
, "porcelain"))
4922 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
4923 else if (!strcmp(arg
, "none"))
4924 options
->word_diff
= DIFF_WORDS_NONE
;
4926 die("bad --word-diff argument: %s", arg
);
4928 else if ((argcount
= parse_long_opt("word-diff-regex", av
, &optarg
))) {
4929 if (options
->word_diff
== DIFF_WORDS_NONE
)
4930 options
->word_diff
= DIFF_WORDS_PLAIN
;
4931 options
->word_regex
= optarg
;
4934 else if (!strcmp(arg
, "--exit-code"))
4935 options
->flags
.exit_with_status
= 1;
4936 else if (!strcmp(arg
, "--quiet"))
4937 options
->flags
.quick
= 1;
4938 else if (!strcmp(arg
, "--ext-diff"))
4939 options
->flags
.allow_external
= 1;
4940 else if (!strcmp(arg
, "--no-ext-diff"))
4941 options
->flags
.allow_external
= 0;
4942 else if (!strcmp(arg
, "--textconv")) {
4943 options
->flags
.allow_textconv
= 1;
4944 options
->flags
.textconv_set_via_cmdline
= 1;
4945 } else if (!strcmp(arg
, "--no-textconv"))
4946 options
->flags
.allow_textconv
= 0;
4947 else if (skip_to_optional_arg_default(arg
, "--ignore-submodules", &arg
, "all")) {
4948 options
->flags
.override_submodule_config
= 1;
4949 handle_ignore_submodules_arg(options
, arg
);
4950 } else if (skip_to_optional_arg_default(arg
, "--submodule", &arg
, "log"))
4951 return parse_submodule_opt(options
, arg
);
4952 else if (skip_prefix(arg
, "--ws-error-highlight=", &arg
))
4953 return parse_ws_error_highlight_opt(options
, arg
);
4954 else if (!strcmp(arg
, "--ita-invisible-in-index"))
4955 options
->ita_invisible_in_index
= 1;
4956 else if (!strcmp(arg
, "--ita-visible-in-index"))
4957 options
->ita_invisible_in_index
= 0;
4960 else if (!strcmp(arg
, "-z"))
4961 options
->line_termination
= 0;
4962 else if ((argcount
= short_opt('l', av
, &optarg
))) {
4963 options
->rename_limit
= strtoul(optarg
, NULL
, 10);
4966 else if ((argcount
= short_opt('S', av
, &optarg
))) {
4967 options
->pickaxe
= optarg
;
4968 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
4970 } else if ((argcount
= short_opt('G', av
, &optarg
))) {
4971 options
->pickaxe
= optarg
;
4972 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
4975 else if (!strcmp(arg
, "--pickaxe-all"))
4976 options
->pickaxe_opts
|= DIFF_PICKAXE_ALL
;
4977 else if (!strcmp(arg
, "--pickaxe-regex"))
4978 options
->pickaxe_opts
|= DIFF_PICKAXE_REGEX
;
4979 else if ((argcount
= short_opt('O', av
, &optarg
))) {
4980 options
->orderfile
= prefix_filename(prefix
, optarg
);
4982 } else if (skip_prefix(arg
, "--find-object=", &arg
))
4983 return parse_objfind_opt(options
, arg
);
4984 else if ((argcount
= parse_long_opt("diff-filter", av
, &optarg
))) {
4985 int offending
= parse_diff_filter_opt(optarg
, options
);
4987 die("unknown change class '%c' in --diff-filter=%s",
4991 else if (!strcmp(arg
, "--no-abbrev"))
4992 options
->abbrev
= 0;
4993 else if (!strcmp(arg
, "--abbrev"))
4994 options
->abbrev
= DEFAULT_ABBREV
;
4995 else if (skip_prefix(arg
, "--abbrev=", &arg
)) {
4996 options
->abbrev
= strtoul(arg
, NULL
, 10);
4997 if (options
->abbrev
< MINIMUM_ABBREV
)
4998 options
->abbrev
= MINIMUM_ABBREV
;
4999 else if (40 < options
->abbrev
)
5000 options
->abbrev
= 40;
5002 else if ((argcount
= parse_long_opt("src-prefix", av
, &optarg
))) {
5003 options
->a_prefix
= optarg
;
5006 else if ((argcount
= parse_long_opt("line-prefix", av
, &optarg
))) {
5007 options
->line_prefix
= optarg
;
5008 options
->line_prefix_length
= strlen(options
->line_prefix
);
5009 graph_setup_line_prefix(options
);
5012 else if ((argcount
= parse_long_opt("dst-prefix", av
, &optarg
))) {
5013 options
->b_prefix
= optarg
;
5016 else if (!strcmp(arg
, "--no-prefix"))
5017 options
->a_prefix
= options
->b_prefix
= "";
5018 else if (opt_arg(arg
, '\0', "inter-hunk-context",
5019 &options
->interhunkcontext
))
5021 else if (!strcmp(arg
, "-W"))
5022 options
->flags
.funccontext
= 1;
5023 else if (!strcmp(arg
, "--function-context"))
5024 options
->flags
.funccontext
= 1;
5025 else if (!strcmp(arg
, "--no-function-context"))
5026 options
->flags
.funccontext
= 0;
5027 else if ((argcount
= parse_long_opt("output", av
, &optarg
))) {
5028 char *path
= prefix_filename(prefix
, optarg
);
5029 options
->file
= xfopen(path
, "w");
5030 options
->close_file
= 1;
5031 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
5032 options
->use_color
= GIT_COLOR_NEVER
;
5040 int parse_rename_score(const char **cp_p
)
5042 unsigned long num
, scale
;
5044 const char *cp
= *cp_p
;
5051 if ( !dot
&& ch
== '.' ) {
5054 } else if ( ch
== '%' ) {
5055 scale
= dot
? scale
*100 : 100;
5056 cp
++; /* % is always at the end */
5058 } else if ( ch
>= '0' && ch
<= '9' ) {
5059 if ( scale
< 100000 ) {
5061 num
= (num
*10) + (ch
-'0');
5070 /* user says num divided by scale and we say internally that
5071 * is MAX_SCORE * num / scale.
5073 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
5076 static int diff_scoreopt_parse(const char *opt
)
5078 int opt1
, opt2
, cmd
;
5084 /* convert the long-form arguments into short-form versions */
5085 if (skip_prefix(opt
, "break-rewrites", &opt
)) {
5086 if (*opt
== 0 || *opt
++ == '=')
5088 } else if (skip_prefix(opt
, "find-copies", &opt
)) {
5089 if (*opt
== 0 || *opt
++ == '=')
5091 } else if (skip_prefix(opt
, "find-renames", &opt
)) {
5092 if (*opt
== 0 || *opt
++ == '=')
5096 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
5097 return -1; /* that is not a -M, -C, or -B option */
5099 opt1
= parse_rename_score(&opt
);
5105 else if (*opt
!= '/')
5106 return -1; /* we expect -B80/99 or -B80 */
5109 opt2
= parse_rename_score(&opt
);
5114 return opt1
| (opt2
<< 16);
5117 struct diff_queue_struct diff_queued_diff
;
5119 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
5121 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
5122 queue
->queue
[queue
->nr
++] = dp
;
5125 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
5126 struct diff_filespec
*one
,
5127 struct diff_filespec
*two
)
5129 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
5137 void diff_free_filepair(struct diff_filepair
*p
)
5139 free_filespec(p
->one
);
5140 free_filespec(p
->two
);
5144 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
5149 /* Do we want all 40 hex characters? */
5150 if (len
== GIT_SHA1_HEXSZ
)
5151 return oid_to_hex(oid
);
5153 /* An abbreviated value is fine, possibly followed by an ellipsis. */
5154 abbrev
= diff_abbrev_oid(oid
, len
);
5156 if (!print_sha1_ellipsis())
5159 abblen
= strlen(abbrev
);
5162 * In well-behaved cases, where the abbreviated result is the
5163 * same as the requested length, append three dots after the
5164 * abbreviation (hence the whole logic is limited to the case
5165 * where abblen < 37); when the actual abbreviated result is a
5166 * bit longer than the requested length, we reduce the number
5167 * of dots so that they match the well-behaved ones. However,
5168 * if the actual abbreviation is longer than the requested
5169 * length by more than three, we give up on aligning, and add
5170 * three dots anyway, to indicate that the output is not the
5171 * full object name. Yes, this may be suboptimal, but this
5172 * appears only in "diff --raw --abbrev" output and it is not
5173 * worth the effort to change it now. Note that this would
5174 * likely to work fine when the automatic sizing of default
5175 * abbreviation length is used--we would be fed -1 in "len" in
5176 * that case, and will end up always appending three-dots, but
5177 * the automatic sizing is supposed to give abblen that ensures
5178 * uniqueness across all objects (statistically speaking).
5180 if (abblen
< GIT_SHA1_HEXSZ
- 3) {
5181 static char hex
[GIT_MAX_HEXSZ
+ 1];
5182 if (len
< abblen
&& abblen
<= len
+ 2)
5183 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
5185 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
5189 return oid_to_hex(oid
);
5192 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
5194 int line_termination
= opt
->line_termination
;
5195 int inter_name_termination
= line_termination
? '\t' : '\0';
5197 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5198 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
5199 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
5200 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
5201 fprintf(opt
->file
, "%s ",
5202 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
5205 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
5206 inter_name_termination
);
5208 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
5211 if (p
->status
== DIFF_STATUS_COPIED
||
5212 p
->status
== DIFF_STATUS_RENAMED
) {
5213 const char *name_a
, *name_b
;
5214 name_a
= p
->one
->path
;
5215 name_b
= p
->two
->path
;
5216 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5217 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
5218 write_name_quoted(name_b
, opt
->file
, line_termination
);
5220 const char *name_a
, *name_b
;
5221 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
5223 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5224 write_name_quoted(name_a
, opt
->file
, line_termination
);
5228 int diff_unmodified_pair(struct diff_filepair
*p
)
5230 /* This function is written stricter than necessary to support
5231 * the currently implemented transformers, but the idea is to
5232 * let transformers to produce diff_filepairs any way they want,
5233 * and filter and clean them up here before producing the output.
5235 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
5237 if (DIFF_PAIR_UNMERGED(p
))
5238 return 0; /* unmerged is interesting */
5240 /* deletion, addition, mode or type change
5241 * and rename are all interesting.
5243 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
5244 DIFF_PAIR_MODE_CHANGED(p
) ||
5245 strcmp(one
->path
, two
->path
))
5248 /* both are valid and point at the same path. that is, we are
5249 * dealing with a change.
5251 if (one
->oid_valid
&& two
->oid_valid
&&
5252 !oidcmp(&one
->oid
, &two
->oid
) &&
5253 !one
->dirty_submodule
&& !two
->dirty_submodule
)
5254 return 1; /* no change */
5255 if (!one
->oid_valid
&& !two
->oid_valid
)
5256 return 1; /* both look at the same file on the filesystem. */
5260 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
5262 if (diff_unmodified_pair(p
))
5265 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5266 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5267 return; /* no tree diffs in patch format */
5272 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
5273 struct diffstat_t
*diffstat
)
5275 if (diff_unmodified_pair(p
))
5278 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5279 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5280 return; /* no useful stat for tree diffs */
5282 run_diffstat(p
, o
, diffstat
);
5285 static void diff_flush_checkdiff(struct diff_filepair
*p
,
5286 struct diff_options
*o
)
5288 if (diff_unmodified_pair(p
))
5291 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5292 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5293 return; /* nothing to check in tree diffs */
5295 run_checkdiff(p
, o
);
5298 int diff_queue_is_empty(void)
5300 struct diff_queue_struct
*q
= &diff_queued_diff
;
5302 for (i
= 0; i
< q
->nr
; i
++)
5303 if (!diff_unmodified_pair(q
->queue
[i
]))
5309 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
5311 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
5314 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
5316 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
5317 fprintf(stderr
, "queue[%d] %s size %lu\n",
5322 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
5324 diff_debug_filespec(p
->one
, i
, "one");
5325 diff_debug_filespec(p
->two
, i
, "two");
5326 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
5327 p
->score
, p
->status
? p
->status
: '?',
5328 p
->one
->rename_used
, p
->broken_pair
);
5331 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
5335 fprintf(stderr
, "%s\n", msg
);
5336 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
5337 for (i
= 0; i
< q
->nr
; i
++) {
5338 struct diff_filepair
*p
= q
->queue
[i
];
5339 diff_debug_filepair(p
, i
);
5344 static void diff_resolve_rename_copy(void)
5347 struct diff_filepair
*p
;
5348 struct diff_queue_struct
*q
= &diff_queued_diff
;
5350 diff_debug_queue("resolve-rename-copy", q
);
5352 for (i
= 0; i
< q
->nr
; i
++) {
5354 p
->status
= 0; /* undecided */
5355 if (DIFF_PAIR_UNMERGED(p
))
5356 p
->status
= DIFF_STATUS_UNMERGED
;
5357 else if (!DIFF_FILE_VALID(p
->one
))
5358 p
->status
= DIFF_STATUS_ADDED
;
5359 else if (!DIFF_FILE_VALID(p
->two
))
5360 p
->status
= DIFF_STATUS_DELETED
;
5361 else if (DIFF_PAIR_TYPE_CHANGED(p
))
5362 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
5364 /* from this point on, we are dealing with a pair
5365 * whose both sides are valid and of the same type, i.e.
5366 * either in-place edit or rename/copy edit.
5368 else if (DIFF_PAIR_RENAME(p
)) {
5370 * A rename might have re-connected a broken
5371 * pair up, causing the pathnames to be the
5372 * same again. If so, that's not a rename at
5373 * all, just a modification..
5375 * Otherwise, see if this source was used for
5376 * multiple renames, in which case we decrement
5377 * the count, and call it a copy.
5379 if (!strcmp(p
->one
->path
, p
->two
->path
))
5380 p
->status
= DIFF_STATUS_MODIFIED
;
5381 else if (--p
->one
->rename_used
> 0)
5382 p
->status
= DIFF_STATUS_COPIED
;
5384 p
->status
= DIFF_STATUS_RENAMED
;
5386 else if (oidcmp(&p
->one
->oid
, &p
->two
->oid
) ||
5387 p
->one
->mode
!= p
->two
->mode
||
5388 p
->one
->dirty_submodule
||
5389 p
->two
->dirty_submodule
||
5390 is_null_oid(&p
->one
->oid
))
5391 p
->status
= DIFF_STATUS_MODIFIED
;
5393 /* This is a "no-change" entry and should not
5394 * happen anymore, but prepare for broken callers.
5396 error("feeding unmodified %s to diffcore",
5398 p
->status
= DIFF_STATUS_UNKNOWN
;
5401 diff_debug_queue("resolve-rename-copy done", q
);
5404 static int check_pair_status(struct diff_filepair
*p
)
5406 switch (p
->status
) {
5407 case DIFF_STATUS_UNKNOWN
:
5410 die("internal error in diff-resolve-rename-copy");
5416 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
5418 int fmt
= opt
->output_format
;
5420 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
5421 diff_flush_checkdiff(p
, opt
);
5422 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
5423 diff_flush_raw(p
, opt
);
5424 else if (fmt
& DIFF_FORMAT_NAME
) {
5425 const char *name_a
, *name_b
;
5426 name_a
= p
->two
->path
;
5428 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5429 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5430 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
5434 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
5436 struct strbuf sb
= STRBUF_INIT
;
5438 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
5440 strbuf_addf(&sb
, " %s ", newdelete
);
5442 quote_c_style(fs
->path
, &sb
, NULL
, 0);
5443 strbuf_addch(&sb
, '\n');
5444 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5446 strbuf_release(&sb
);
5449 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
5452 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
5453 struct strbuf sb
= STRBUF_INIT
;
5454 strbuf_addf(&sb
, " mode change %06o => %06o",
5455 p
->one
->mode
, p
->two
->mode
);
5457 strbuf_addch(&sb
, ' ');
5458 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
5460 strbuf_addch(&sb
, '\n');
5461 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5463 strbuf_release(&sb
);
5467 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
5468 struct diff_filepair
*p
)
5470 struct strbuf sb
= STRBUF_INIT
;
5471 struct strbuf names
= STRBUF_INIT
;
5473 pprint_rename(&names
, p
->one
->path
, p
->two
->path
);
5474 strbuf_addf(&sb
, " %s %s (%d%%)\n",
5475 renamecopy
, names
.buf
, similarity_index(p
));
5476 strbuf_release(&names
);
5477 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5479 show_mode_change(opt
, p
, 0);
5480 strbuf_release(&sb
);
5483 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
5486 case DIFF_STATUS_DELETED
:
5487 show_file_mode_name(opt
, "delete", p
->one
);
5489 case DIFF_STATUS_ADDED
:
5490 show_file_mode_name(opt
, "create", p
->two
);
5492 case DIFF_STATUS_COPIED
:
5493 show_rename_copy(opt
, "copy", p
);
5495 case DIFF_STATUS_RENAMED
:
5496 show_rename_copy(opt
, "rename", p
);
5500 struct strbuf sb
= STRBUF_INIT
;
5501 strbuf_addstr(&sb
, " rewrite ");
5502 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
5503 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
5504 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5506 strbuf_release(&sb
);
5508 show_mode_change(opt
, p
, !p
->score
);
5518 static int remove_space(char *line
, int len
)
5524 for (i
= 0; i
< len
; i
++)
5525 if (!isspace((c
= line
[i
])))
5531 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
5533 struct patch_id_t
*data
= priv
;
5536 /* Ignore line numbers when computing the SHA1 of the patch */
5537 if (starts_with(line
, "@@ -"))
5540 new_len
= remove_space(line
, len
);
5542 git_SHA1_Update(data
->ctx
, line
, new_len
);
5543 data
->patchlen
+= new_len
;
5546 static void patch_id_add_string(git_SHA_CTX
*ctx
, const char *str
)
5548 git_SHA1_Update(ctx
, str
, strlen(str
));
5551 static void patch_id_add_mode(git_SHA_CTX
*ctx
, unsigned mode
)
5553 /* large enough for 2^32 in octal */
5555 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
5556 git_SHA1_Update(ctx
, buf
, len
);
5559 /* returns 0 upon success, and writes result into sha1 */
5560 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
5562 struct diff_queue_struct
*q
= &diff_queued_diff
;
5565 struct patch_id_t data
;
5567 git_SHA1_Init(&ctx
);
5568 memset(&data
, 0, sizeof(struct patch_id_t
));
5571 for (i
= 0; i
< q
->nr
; i
++) {
5575 struct diff_filepair
*p
= q
->queue
[i
];
5578 memset(&xpp
, 0, sizeof(xpp
));
5579 memset(&xecfg
, 0, sizeof(xecfg
));
5581 return error("internal diff status error");
5582 if (p
->status
== DIFF_STATUS_UNKNOWN
)
5584 if (diff_unmodified_pair(p
))
5586 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5587 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5589 if (DIFF_PAIR_UNMERGED(p
))
5592 diff_fill_oid_info(p
->one
);
5593 diff_fill_oid_info(p
->two
);
5595 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
5596 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
5597 patch_id_add_string(&ctx
, "diff--git");
5598 patch_id_add_string(&ctx
, "a/");
5599 git_SHA1_Update(&ctx
, p
->one
->path
, len1
);
5600 patch_id_add_string(&ctx
, "b/");
5601 git_SHA1_Update(&ctx
, p
->two
->path
, len2
);
5603 if (p
->one
->mode
== 0) {
5604 patch_id_add_string(&ctx
, "newfilemode");
5605 patch_id_add_mode(&ctx
, p
->two
->mode
);
5606 patch_id_add_string(&ctx
, "---/dev/null");
5607 patch_id_add_string(&ctx
, "+++b/");
5608 git_SHA1_Update(&ctx
, p
->two
->path
, len2
);
5609 } else if (p
->two
->mode
== 0) {
5610 patch_id_add_string(&ctx
, "deletedfilemode");
5611 patch_id_add_mode(&ctx
, p
->one
->mode
);
5612 patch_id_add_string(&ctx
, "---a/");
5613 git_SHA1_Update(&ctx
, p
->one
->path
, len1
);
5614 patch_id_add_string(&ctx
, "+++/dev/null");
5616 patch_id_add_string(&ctx
, "---a/");
5617 git_SHA1_Update(&ctx
, p
->one
->path
, len1
);
5618 patch_id_add_string(&ctx
, "+++b/");
5619 git_SHA1_Update(&ctx
, p
->two
->path
, len2
);
5622 if (diff_header_only
)
5625 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
5626 fill_mmfile(&mf2
, p
->two
) < 0)
5627 return error("unable to read files to diff");
5629 if (diff_filespec_is_binary(p
->one
) ||
5630 diff_filespec_is_binary(p
->two
)) {
5631 git_SHA1_Update(&ctx
, oid_to_hex(&p
->one
->oid
),
5633 git_SHA1_Update(&ctx
, oid_to_hex(&p
->two
->oid
),
5641 if (xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
5643 return error("unable to generate patch-id diff for %s",
5647 git_SHA1_Final(oid
->hash
, &ctx
);
5651 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
5653 struct diff_queue_struct
*q
= &diff_queued_diff
;
5655 int result
= diff_get_patch_id(options
, oid
, diff_header_only
);
5657 for (i
= 0; i
< q
->nr
; i
++)
5658 diff_free_filepair(q
->queue
[i
]);
5661 DIFF_QUEUE_CLEAR(q
);
5666 static int is_summary_empty(const struct diff_queue_struct
*q
)
5670 for (i
= 0; i
< q
->nr
; i
++) {
5671 const struct diff_filepair
*p
= q
->queue
[i
];
5673 switch (p
->status
) {
5674 case DIFF_STATUS_DELETED
:
5675 case DIFF_STATUS_ADDED
:
5676 case DIFF_STATUS_COPIED
:
5677 case DIFF_STATUS_RENAMED
:
5682 if (p
->one
->mode
&& p
->two
->mode
&&
5683 p
->one
->mode
!= p
->two
->mode
)
5691 static const char rename_limit_warning
[] =
5692 N_("inexact rename detection was skipped due to too many files.");
5694 static const char degrade_cc_to_c_warning
[] =
5695 N_("only found copies from modified paths due to too many files.");
5697 static const char rename_limit_advice
[] =
5698 N_("you may want to set your %s variable to at least "
5699 "%d and retry the command.");
5701 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
5705 warning(_(degrade_cc_to_c_warning
));
5707 warning(_(rename_limit_warning
));
5711 warning(_(rename_limit_advice
), varname
, needed
);
5714 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
5717 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
5718 struct diff_queue_struct
*q
= &diff_queued_diff
;
5720 if (WSEH_NEW
& WS_RULE_MASK
)
5721 die("BUG: WS rules bit mask overlaps with diff symbol flags");
5724 o
->emitted_symbols
= &esm
;
5726 for (i
= 0; i
< q
->nr
; i
++) {
5727 struct diff_filepair
*p
= q
->queue
[i
];
5728 if (check_pair_status(p
))
5729 diff_flush_patch(p
, o
);
5732 if (o
->emitted_symbols
) {
5733 if (o
->color_moved
) {
5734 struct hashmap add_lines
, del_lines
;
5736 if (o
->color_moved_ws_handling
&
5737 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
5738 o
->color_moved_ws_handling
|= XDF_IGNORE_WHITESPACE
;
5740 hashmap_init(&del_lines
, moved_entry_cmp
, o
, 0);
5741 hashmap_init(&add_lines
, moved_entry_cmp
, o
, 0);
5743 add_lines_to_move_detection(o
, &add_lines
, &del_lines
);
5744 mark_color_as_moved(o
, &add_lines
, &del_lines
);
5745 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
5748 hashmap_free(&add_lines
, 0);
5749 hashmap_free(&del_lines
, 0);
5752 for (i
= 0; i
< esm
.nr
; i
++)
5753 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
5755 for (i
= 0; i
< esm
.nr
; i
++)
5756 free((void *)esm
.buf
[i
].line
);
5761 void diff_flush(struct diff_options
*options
)
5763 struct diff_queue_struct
*q
= &diff_queued_diff
;
5764 int i
, output_format
= options
->output_format
;
5766 int dirstat_by_line
= 0;
5769 * Order: raw, stat, summary, patch
5770 * or: name/name-status/checkdiff (other bits clear)
5775 if (output_format
& (DIFF_FORMAT_RAW
|
5777 DIFF_FORMAT_NAME_STATUS
|
5778 DIFF_FORMAT_CHECKDIFF
)) {
5779 for (i
= 0; i
< q
->nr
; i
++) {
5780 struct diff_filepair
*p
= q
->queue
[i
];
5781 if (check_pair_status(p
))
5782 flush_one_pair(p
, options
);
5787 if (output_format
& DIFF_FORMAT_DIRSTAT
&& options
->flags
.dirstat_by_line
)
5788 dirstat_by_line
= 1;
5790 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
5792 struct diffstat_t diffstat
;
5794 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
5795 for (i
= 0; i
< q
->nr
; i
++) {
5796 struct diff_filepair
*p
= q
->queue
[i
];
5797 if (check_pair_status(p
))
5798 diff_flush_stat(p
, options
, &diffstat
);
5800 if (output_format
& DIFF_FORMAT_NUMSTAT
)
5801 show_numstat(&diffstat
, options
);
5802 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
5803 show_stats(&diffstat
, options
);
5804 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
5805 show_shortstats(&diffstat
, options
);
5806 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
5807 show_dirstat_by_line(&diffstat
, options
);
5808 free_diffstat_info(&diffstat
);
5811 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
5812 show_dirstat(options
);
5814 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
5815 for (i
= 0; i
< q
->nr
; i
++) {
5816 diff_summary(options
, q
->queue
[i
]);
5821 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
5822 options
->flags
.exit_with_status
&&
5823 options
->flags
.diff_from_contents
) {
5825 * run diff_flush_patch for the exit status. setting
5826 * options->file to /dev/null should be safe, because we
5827 * aren't supposed to produce any output anyway.
5829 if (options
->close_file
)
5830 fclose(options
->file
);
5831 options
->file
= xfopen("/dev/null", "w");
5832 options
->close_file
= 1;
5833 options
->color_moved
= 0;
5834 for (i
= 0; i
< q
->nr
; i
++) {
5835 struct diff_filepair
*p
= q
->queue
[i
];
5836 if (check_pair_status(p
))
5837 diff_flush_patch(p
, options
);
5838 if (options
->found_changes
)
5843 if (output_format
& DIFF_FORMAT_PATCH
) {
5845 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
5846 if (options
->stat_sep
)
5847 /* attach patch instead of inline */
5848 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
5852 diff_flush_patch_all_file_pairs(options
);
5855 if (output_format
& DIFF_FORMAT_CALLBACK
)
5856 options
->format_callback(q
, options
, options
->format_callback_data
);
5858 for (i
= 0; i
< q
->nr
; i
++)
5859 diff_free_filepair(q
->queue
[i
]);
5862 DIFF_QUEUE_CLEAR(q
);
5863 if (options
->close_file
)
5864 fclose(options
->file
);
5867 * Report the content-level differences with HAS_CHANGES;
5868 * diff_addremove/diff_change does not set the bit when
5869 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
5871 if (options
->flags
.diff_from_contents
) {
5872 if (options
->found_changes
)
5873 options
->flags
.has_changes
= 1;
5875 options
->flags
.has_changes
= 0;
5879 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
5881 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
5883 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
5885 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
5886 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
5887 filter_bit_tst(p
->status
, options
)));
5890 static void diffcore_apply_filter(struct diff_options
*options
)
5893 struct diff_queue_struct
*q
= &diff_queued_diff
;
5894 struct diff_queue_struct outq
;
5896 DIFF_QUEUE_CLEAR(&outq
);
5898 if (!options
->filter
)
5901 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
5903 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
5904 if (match_filter(options
, q
->queue
[i
]))
5910 /* otherwise we will clear the whole queue
5911 * by copying the empty outq at the end of this
5912 * function, but first clear the current entries
5915 for (i
= 0; i
< q
->nr
; i
++)
5916 diff_free_filepair(q
->queue
[i
]);
5919 /* Only the matching ones */
5920 for (i
= 0; i
< q
->nr
; i
++) {
5921 struct diff_filepair
*p
= q
->queue
[i
];
5922 if (match_filter(options
, p
))
5925 diff_free_filepair(p
);
5932 /* Check whether two filespecs with the same mode and size are identical */
5933 static int diff_filespec_is_identical(struct diff_filespec
*one
,
5934 struct diff_filespec
*two
)
5936 if (S_ISGITLINK(one
->mode
))
5938 if (diff_populate_filespec(one
, 0))
5940 if (diff_populate_filespec(two
, 0))
5942 return !memcmp(one
->data
, two
->data
, one
->size
);
5945 static int diff_filespec_check_stat_unmatch(struct diff_filepair
*p
)
5947 if (p
->done_skip_stat_unmatch
)
5948 return p
->skip_stat_unmatch_result
;
5950 p
->done_skip_stat_unmatch
= 1;
5951 p
->skip_stat_unmatch_result
= 0;
5953 * 1. Entries that come from stat info dirtiness
5954 * always have both sides (iow, not create/delete),
5955 * one side of the object name is unknown, with
5956 * the same mode and size. Keep the ones that
5957 * do not match these criteria. They have real
5960 * 2. At this point, the file is known to be modified,
5961 * with the same mode and size, and the object
5962 * name of one side is unknown. Need to inspect
5963 * the identical contents.
5965 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
5966 !DIFF_FILE_VALID(p
->two
) ||
5967 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
5968 (p
->one
->mode
!= p
->two
->mode
) ||
5969 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
) ||
5970 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
) ||
5971 (p
->one
->size
!= p
->two
->size
) ||
5972 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
5973 p
->skip_stat_unmatch_result
= 1;
5974 return p
->skip_stat_unmatch_result
;
5977 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
5980 struct diff_queue_struct
*q
= &diff_queued_diff
;
5981 struct diff_queue_struct outq
;
5982 DIFF_QUEUE_CLEAR(&outq
);
5984 for (i
= 0; i
< q
->nr
; i
++) {
5985 struct diff_filepair
*p
= q
->queue
[i
];
5987 if (diff_filespec_check_stat_unmatch(p
))
5991 * The caller can subtract 1 from skip_stat_unmatch
5992 * to determine how many paths were dirty only
5993 * due to stat info mismatch.
5995 if (!diffopt
->flags
.no_index
)
5996 diffopt
->skip_stat_unmatch
++;
5997 diff_free_filepair(p
);
6004 static int diffnamecmp(const void *a_
, const void *b_
)
6006 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
6007 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
6008 const char *name_a
, *name_b
;
6010 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
6011 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
6012 return strcmp(name_a
, name_b
);
6015 void diffcore_fix_diff_index(struct diff_options
*options
)
6017 struct diff_queue_struct
*q
= &diff_queued_diff
;
6018 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
6021 void diffcore_std(struct diff_options
*options
)
6023 /* NOTE please keep the following in sync with diff_tree_combined() */
6024 if (options
->skip_stat_unmatch
)
6025 diffcore_skip_stat_unmatch(options
);
6026 if (!options
->found_follow
) {
6027 /* See try_to_follow_renames() in tree-diff.c */
6028 if (options
->break_opt
!= -1)
6029 diffcore_break(options
->break_opt
);
6030 if (options
->detect_rename
)
6031 diffcore_rename(options
);
6032 if (options
->break_opt
!= -1)
6033 diffcore_merge_broken();
6035 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
6036 diffcore_pickaxe(options
);
6037 if (options
->orderfile
)
6038 diffcore_order(options
->orderfile
);
6039 if (!options
->found_follow
)
6040 /* See try_to_follow_renames() in tree-diff.c */
6041 diff_resolve_rename_copy();
6042 diffcore_apply_filter(options
);
6044 if (diff_queued_diff
.nr
&& !options
->flags
.diff_from_contents
)
6045 options
->flags
.has_changes
= 1;
6047 options
->flags
.has_changes
= 0;
6049 options
->found_follow
= 0;
6052 int diff_result_code(struct diff_options
*opt
, int status
)
6056 diff_warn_rename_limit("diff.renameLimit",
6057 opt
->needed_rename_limit
,
6058 opt
->degraded_cc_to_c
);
6059 if (!opt
->flags
.exit_with_status
&&
6060 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
6062 if (opt
->flags
.exit_with_status
&&
6063 opt
->flags
.has_changes
)
6065 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
6066 opt
->flags
.check_failed
)
6071 int diff_can_quit_early(struct diff_options
*opt
)
6073 return (opt
->flags
.quick
&&
6075 opt
->flags
.has_changes
);
6079 * Shall changes to this submodule be ignored?
6081 * Submodule changes can be configured to be ignored separately for each path,
6082 * but that configuration can be overridden from the command line.
6084 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
6087 struct diff_flags orig_flags
= options
->flags
;
6088 if (!options
->flags
.override_submodule_config
)
6089 set_diffopt_flags_from_submodule_config(options
, path
);
6090 if (options
->flags
.ignore_submodules
)
6092 options
->flags
= orig_flags
;
6096 void diff_addremove(struct diff_options
*options
,
6097 int addremove
, unsigned mode
,
6098 const struct object_id
*oid
,
6100 const char *concatpath
, unsigned dirty_submodule
)
6102 struct diff_filespec
*one
, *two
;
6104 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
6107 /* This may look odd, but it is a preparation for
6108 * feeding "there are unchanged files which should
6109 * not produce diffs, but when you are doing copy
6110 * detection you would need them, so here they are"
6111 * entries to the diff-core. They will be prefixed
6112 * with something like '=' or '*' (I haven't decided
6113 * which but should not make any difference).
6114 * Feeding the same new and old to diff_change()
6115 * also has the same effect.
6116 * Before the final output happens, they are pruned after
6117 * merged into rename/copy pairs as appropriate.
6119 if (options
->flags
.reverse_diff
)
6120 addremove
= (addremove
== '+' ? '-' :
6121 addremove
== '-' ? '+' : addremove
);
6123 if (options
->prefix
&&
6124 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
6127 one
= alloc_filespec(concatpath
);
6128 two
= alloc_filespec(concatpath
);
6130 if (addremove
!= '+')
6131 fill_filespec(one
, oid
, oid_valid
, mode
);
6132 if (addremove
!= '-') {
6133 fill_filespec(two
, oid
, oid_valid
, mode
);
6134 two
->dirty_submodule
= dirty_submodule
;
6137 diff_queue(&diff_queued_diff
, one
, two
);
6138 if (!options
->flags
.diff_from_contents
)
6139 options
->flags
.has_changes
= 1;
6142 void diff_change(struct diff_options
*options
,
6143 unsigned old_mode
, unsigned new_mode
,
6144 const struct object_id
*old_oid
,
6145 const struct object_id
*new_oid
,
6146 int old_oid_valid
, int new_oid_valid
,
6147 const char *concatpath
,
6148 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
6150 struct diff_filespec
*one
, *two
;
6151 struct diff_filepair
*p
;
6153 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
6154 is_submodule_ignored(concatpath
, options
))
6157 if (options
->flags
.reverse_diff
) {
6158 SWAP(old_mode
, new_mode
);
6159 SWAP(old_oid
, new_oid
);
6160 SWAP(old_oid_valid
, new_oid_valid
);
6161 SWAP(old_dirty_submodule
, new_dirty_submodule
);
6164 if (options
->prefix
&&
6165 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
6168 one
= alloc_filespec(concatpath
);
6169 two
= alloc_filespec(concatpath
);
6170 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
6171 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
6172 one
->dirty_submodule
= old_dirty_submodule
;
6173 two
->dirty_submodule
= new_dirty_submodule
;
6174 p
= diff_queue(&diff_queued_diff
, one
, two
);
6176 if (options
->flags
.diff_from_contents
)
6179 if (options
->flags
.quick
&& options
->skip_stat_unmatch
&&
6180 !diff_filespec_check_stat_unmatch(p
))
6183 options
->flags
.has_changes
= 1;
6186 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
6188 struct diff_filepair
*pair
;
6189 struct diff_filespec
*one
, *two
;
6191 if (options
->prefix
&&
6192 strncmp(path
, options
->prefix
, options
->prefix_length
))
6195 one
= alloc_filespec(path
);
6196 two
= alloc_filespec(path
);
6197 pair
= diff_queue(&diff_queued_diff
, one
, two
);
6198 pair
->is_unmerged
= 1;
6202 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
6205 struct diff_tempfile
*temp
;
6206 const char *argv
[3];
6207 const char **arg
= argv
;
6208 struct child_process child
= CHILD_PROCESS_INIT
;
6209 struct strbuf buf
= STRBUF_INIT
;
6212 temp
= prepare_temp_file(spec
->path
, spec
);
6214 *arg
++ = temp
->name
;
6217 child
.use_shell
= 1;
6220 if (start_command(&child
)) {
6225 if (strbuf_read(&buf
, child
.out
, 0) < 0)
6226 err
= error("error reading from textconv command '%s'", pgm
);
6229 if (finish_command(&child
) || err
) {
6230 strbuf_release(&buf
);
6236 return strbuf_detach(&buf
, outsize
);
6239 size_t fill_textconv(struct userdiff_driver
*driver
,
6240 struct diff_filespec
*df
,
6246 if (!DIFF_FILE_VALID(df
)) {
6250 if (diff_populate_filespec(df
, 0))
6251 die("unable to read files to diff");
6256 if (!driver
->textconv
)
6257 die("BUG: fill_textconv called with non-textconv driver");
6259 if (driver
->textconv_cache
&& df
->oid_valid
) {
6260 *outbuf
= notes_cache_get(driver
->textconv_cache
,
6267 *outbuf
= run_textconv(driver
->textconv
, df
, &size
);
6269 die("unable to read files to diff");
6271 if (driver
->textconv_cache
&& df
->oid_valid
) {
6272 /* ignore errors, as we might be in a readonly repository */
6273 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
6276 * we could save up changes and flush them all at the end,
6277 * but we would need an extra call after all diffing is done.
6278 * Since generating a cache entry is the slow path anyway,
6279 * this extra overhead probably isn't a big deal.
6281 notes_cache_write(driver
->textconv_cache
);
6287 int textconv_object(const char *path
,
6289 const struct object_id
*oid
,
6292 unsigned long *buf_size
)
6294 struct diff_filespec
*df
;
6295 struct userdiff_driver
*textconv
;
6297 df
= alloc_filespec(path
);
6298 fill_filespec(df
, oid
, oid_valid
, mode
);
6299 textconv
= get_textconv(df
);
6305 *buf_size
= fill_textconv(textconv
, df
, buf
);
6310 void setup_diff_pager(struct diff_options
*opt
)
6313 * If the user asked for our exit code, then either they want --quiet
6314 * or --exit-code. We should definitely not bother with a pager in the
6315 * former case, as we will generate no output. Since we still properly
6316 * report our exit code even when a pager is run, we _could_ run a
6317 * pager with --exit-code. But since we have not done so historically,
6318 * and because it is easy to find people oneline advising "git diff
6319 * --exit-code" in hooks and other scripts, we do not do so.
6321 if (!opt
->flags
.exit_with_status
&&
6322 check_pager_config("diff") != 0)