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"
25 #ifdef NO_FAST_WORKING_DIRECTORY
26 #define FAST_WORKING_DIRECTORY 0
28 #define FAST_WORKING_DIRECTORY 1
31 static int diff_detect_rename_default
;
32 static int diff_indent_heuristic
= 1;
33 static int diff_rename_limit_default
= 400;
34 static int diff_suppress_blank_empty
;
35 static int diff_use_color_default
= -1;
36 static int diff_color_moved_default
;
37 static int diff_context_default
= 3;
38 static int diff_interhunk_context_default
;
39 static const char *diff_word_regex_cfg
;
40 static const char *external_diff_cmd_cfg
;
41 static const char *diff_order_file_cfg
;
42 int diff_auto_refresh_index
= 1;
43 static int diff_mnemonic_prefix
;
44 static int diff_no_prefix
;
45 static int diff_stat_graph_width
;
46 static int diff_dirstat_permille_default
= 30;
47 static struct diff_options default_diff_options
;
48 static long diff_algorithm
;
49 static unsigned ws_error_highlight_default
= WSEH_NEW
;
51 static char diff_colors
[][COLOR_MAXLEN
] = {
53 GIT_COLOR_NORMAL
, /* CONTEXT */
54 GIT_COLOR_BOLD
, /* METAINFO */
55 GIT_COLOR_CYAN
, /* FRAGINFO */
56 GIT_COLOR_RED
, /* OLD */
57 GIT_COLOR_GREEN
, /* NEW */
58 GIT_COLOR_YELLOW
, /* COMMIT */
59 GIT_COLOR_BG_RED
, /* WHITESPACE */
60 GIT_COLOR_NORMAL
, /* FUNCINFO */
61 GIT_COLOR_BOLD_MAGENTA
, /* OLD_MOVED */
62 GIT_COLOR_BOLD_BLUE
, /* OLD_MOVED ALTERNATIVE */
63 GIT_COLOR_FAINT
, /* OLD_MOVED_DIM */
64 GIT_COLOR_FAINT_ITALIC
, /* OLD_MOVED_ALTERNATIVE_DIM */
65 GIT_COLOR_BOLD_CYAN
, /* NEW_MOVED */
66 GIT_COLOR_BOLD_YELLOW
, /* NEW_MOVED ALTERNATIVE */
67 GIT_COLOR_FAINT
, /* NEW_MOVED_DIM */
68 GIT_COLOR_FAINT_ITALIC
, /* NEW_MOVED_ALTERNATIVE_DIM */
71 static NORETURN
void die_want_option(const char *option_name
)
73 die(_("option '%s' requires a value"), option_name
);
76 static int parse_diff_color_slot(const char *var
)
78 if (!strcasecmp(var
, "context") || !strcasecmp(var
, "plain"))
80 if (!strcasecmp(var
, "meta"))
82 if (!strcasecmp(var
, "frag"))
84 if (!strcasecmp(var
, "old"))
86 if (!strcasecmp(var
, "new"))
88 if (!strcasecmp(var
, "commit"))
90 if (!strcasecmp(var
, "whitespace"))
91 return DIFF_WHITESPACE
;
92 if (!strcasecmp(var
, "func"))
94 if (!strcasecmp(var
, "oldmoved"))
95 return DIFF_FILE_OLD_MOVED
;
96 if (!strcasecmp(var
, "oldmovedalternative"))
97 return DIFF_FILE_OLD_MOVED_ALT
;
98 if (!strcasecmp(var
, "oldmoveddimmed"))
99 return DIFF_FILE_OLD_MOVED_DIM
;
100 if (!strcasecmp(var
, "oldmovedalternativedimmed"))
101 return DIFF_FILE_OLD_MOVED_ALT_DIM
;
102 if (!strcasecmp(var
, "newmoved"))
103 return DIFF_FILE_NEW_MOVED
;
104 if (!strcasecmp(var
, "newmovedalternative"))
105 return DIFF_FILE_NEW_MOVED_ALT
;
106 if (!strcasecmp(var
, "newmoveddimmed"))
107 return DIFF_FILE_NEW_MOVED_DIM
;
108 if (!strcasecmp(var
, "newmovedalternativedimmed"))
109 return DIFF_FILE_NEW_MOVED_ALT_DIM
;
113 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
114 struct strbuf
*errmsg
)
116 char *params_copy
= xstrdup(params_string
);
117 struct string_list params
= STRING_LIST_INIT_NODUP
;
122 string_list_split_in_place(¶ms
, params_copy
, ',', -1);
123 for (i
= 0; i
< params
.nr
; i
++) {
124 const char *p
= params
.items
[i
].string
;
125 if (!strcmp(p
, "changes")) {
126 DIFF_OPT_CLR(options
, DIRSTAT_BY_LINE
);
127 DIFF_OPT_CLR(options
, DIRSTAT_BY_FILE
);
128 } else if (!strcmp(p
, "lines")) {
129 DIFF_OPT_SET(options
, DIRSTAT_BY_LINE
);
130 DIFF_OPT_CLR(options
, DIRSTAT_BY_FILE
);
131 } else if (!strcmp(p
, "files")) {
132 DIFF_OPT_CLR(options
, DIRSTAT_BY_LINE
);
133 DIFF_OPT_SET(options
, DIRSTAT_BY_FILE
);
134 } else if (!strcmp(p
, "noncumulative")) {
135 DIFF_OPT_CLR(options
, DIRSTAT_CUMULATIVE
);
136 } else if (!strcmp(p
, "cumulative")) {
137 DIFF_OPT_SET(options
, DIRSTAT_CUMULATIVE
);
138 } else if (isdigit(*p
)) {
140 int permille
= strtoul(p
, &end
, 10) * 10;
141 if (*end
== '.' && isdigit(*++end
)) {
142 /* only use first digit */
143 permille
+= *end
- '0';
144 /* .. and ignore any further digits */
145 while (isdigit(*++end
))
149 options
->dirstat_permille
= permille
;
151 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
156 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
161 string_list_clear(¶ms
, 0);
166 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
168 if (!strcmp(value
, "log"))
169 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
170 else if (!strcmp(value
, "short"))
171 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
172 else if (!strcmp(value
, "diff"))
173 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
179 static int git_config_rename(const char *var
, const char *value
)
182 return DIFF_DETECT_RENAME
;
183 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
184 return DIFF_DETECT_COPY
;
185 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
188 long parse_algorithm_value(const char *value
)
192 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
194 else if (!strcasecmp(value
, "minimal"))
195 return XDF_NEED_MINIMAL
;
196 else if (!strcasecmp(value
, "patience"))
197 return XDF_PATIENCE_DIFF
;
198 else if (!strcasecmp(value
, "histogram"))
199 return XDF_HISTOGRAM_DIFF
;
203 static int parse_one_token(const char **arg
, const char *token
)
206 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
213 static int parse_ws_error_highlight(const char *arg
)
215 const char *orig_arg
= arg
;
219 if (parse_one_token(&arg
, "none"))
221 else if (parse_one_token(&arg
, "default"))
223 else if (parse_one_token(&arg
, "all"))
224 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
225 else if (parse_one_token(&arg
, "new"))
227 else if (parse_one_token(&arg
, "old"))
229 else if (parse_one_token(&arg
, "context"))
232 return -1 - (int)(arg
- orig_arg
);
241 * These are to give UI layer defaults.
242 * The core-level commands such as git-diff-files should
243 * never be affected by the setting of diff.renames
244 * the user happens to have in the configuration file.
246 void init_diff_ui_defaults(void)
248 diff_detect_rename_default
= 1;
251 int git_diff_heuristic_config(const char *var
, const char *value
, void *cb
)
253 if (!strcmp(var
, "diff.indentheuristic"))
254 diff_indent_heuristic
= git_config_bool(var
, value
);
258 static int parse_color_moved(const char *arg
)
260 switch (git_parse_maybe_bool(arg
)) {
262 return COLOR_MOVED_NO
;
264 return COLOR_MOVED_DEFAULT
;
269 if (!strcmp(arg
, "no"))
270 return COLOR_MOVED_NO
;
271 else if (!strcmp(arg
, "plain"))
272 return COLOR_MOVED_PLAIN
;
273 else if (!strcmp(arg
, "zebra"))
274 return COLOR_MOVED_ZEBRA
;
275 else if (!strcmp(arg
, "default"))
276 return COLOR_MOVED_DEFAULT
;
277 else if (!strcmp(arg
, "dimmed_zebra"))
278 return COLOR_MOVED_ZEBRA_DIM
;
280 return error(_("color moved setting must be one of 'no', 'default', 'zebra', 'dimmed_zebra', 'plain'"));
283 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
285 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
286 diff_use_color_default
= git_config_colorbool(var
, value
);
289 if (!strcmp(var
, "diff.colormoved")) {
290 int cm
= parse_color_moved(value
);
293 diff_color_moved_default
= cm
;
296 if (!strcmp(var
, "diff.context")) {
297 diff_context_default
= git_config_int(var
, value
);
298 if (diff_context_default
< 0)
302 if (!strcmp(var
, "diff.interhunkcontext")) {
303 diff_interhunk_context_default
= git_config_int(var
, value
);
304 if (diff_interhunk_context_default
< 0)
308 if (!strcmp(var
, "diff.renames")) {
309 diff_detect_rename_default
= git_config_rename(var
, value
);
312 if (!strcmp(var
, "diff.autorefreshindex")) {
313 diff_auto_refresh_index
= git_config_bool(var
, value
);
316 if (!strcmp(var
, "diff.mnemonicprefix")) {
317 diff_mnemonic_prefix
= git_config_bool(var
, value
);
320 if (!strcmp(var
, "diff.noprefix")) {
321 diff_no_prefix
= git_config_bool(var
, value
);
324 if (!strcmp(var
, "diff.statgraphwidth")) {
325 diff_stat_graph_width
= git_config_int(var
, value
);
328 if (!strcmp(var
, "diff.external"))
329 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
330 if (!strcmp(var
, "diff.wordregex"))
331 return git_config_string(&diff_word_regex_cfg
, var
, value
);
332 if (!strcmp(var
, "diff.orderfile"))
333 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
335 if (!strcmp(var
, "diff.ignoresubmodules"))
336 handle_ignore_submodules_arg(&default_diff_options
, value
);
338 if (!strcmp(var
, "diff.submodule")) {
339 if (parse_submodule_params(&default_diff_options
, value
))
340 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
345 if (!strcmp(var
, "diff.algorithm")) {
346 diff_algorithm
= parse_algorithm_value(value
);
347 if (diff_algorithm
< 0)
352 if (!strcmp(var
, "diff.wserrorhighlight")) {
353 int val
= parse_ws_error_highlight(value
);
356 ws_error_highlight_default
= val
;
360 if (git_color_config(var
, value
, cb
) < 0)
363 return git_diff_basic_config(var
, value
, cb
);
366 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
370 if (!strcmp(var
, "diff.renamelimit")) {
371 diff_rename_limit_default
= git_config_int(var
, value
);
375 if (userdiff_config(var
, value
) < 0)
378 if (skip_prefix(var
, "diff.color.", &name
) ||
379 skip_prefix(var
, "color.diff.", &name
)) {
380 int slot
= parse_diff_color_slot(name
);
384 return config_error_nonbool(var
);
385 return color_parse(value
, diff_colors
[slot
]);
388 /* like GNU diff's --suppress-blank-empty option */
389 if (!strcmp(var
, "diff.suppressblankempty") ||
390 /* for backwards compatibility */
391 !strcmp(var
, "diff.suppress-blank-empty")) {
392 diff_suppress_blank_empty
= git_config_bool(var
, value
);
396 if (!strcmp(var
, "diff.dirstat")) {
397 struct strbuf errmsg
= STRBUF_INIT
;
398 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
399 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
400 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
402 strbuf_release(&errmsg
);
403 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
407 if (starts_with(var
, "submodule."))
408 return parse_submodule_config_option(var
, value
);
410 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
413 return git_default_config(var
, value
, cb
);
416 static char *quote_two(const char *one
, const char *two
)
418 int need_one
= quote_c_style(one
, NULL
, NULL
, 1);
419 int need_two
= quote_c_style(two
, NULL
, NULL
, 1);
420 struct strbuf res
= STRBUF_INIT
;
422 if (need_one
+ need_two
) {
423 strbuf_addch(&res
, '"');
424 quote_c_style(one
, &res
, NULL
, 1);
425 quote_c_style(two
, &res
, NULL
, 1);
426 strbuf_addch(&res
, '"');
428 strbuf_addstr(&res
, one
);
429 strbuf_addstr(&res
, two
);
431 return strbuf_detach(&res
, NULL
);
434 static const char *external_diff(void)
436 static const char *external_diff_cmd
= NULL
;
437 static int done_preparing
= 0;
440 return external_diff_cmd
;
441 external_diff_cmd
= getenv("GIT_EXTERNAL_DIFF");
442 if (!external_diff_cmd
)
443 external_diff_cmd
= external_diff_cmd_cfg
;
445 return external_diff_cmd
;
449 * Keep track of files used for diffing. Sometimes such an entry
450 * refers to a temporary file, sometimes to an existing file, and
451 * sometimes to "/dev/null".
453 static struct diff_tempfile
{
455 * filename external diff should read from, or NULL if this
456 * entry is currently not in use:
460 char hex
[GIT_MAX_HEXSZ
+ 1];
464 * If this diff_tempfile instance refers to a temporary file,
465 * this tempfile object is used to manage its lifetime.
467 struct tempfile tempfile
;
470 typedef unsigned long (*sane_truncate_fn
)(char *line
, unsigned long len
);
472 struct emit_callback
{
475 int blank_at_eof_in_preimage
;
476 int blank_at_eof_in_postimage
;
478 int lno_in_postimage
;
479 sane_truncate_fn truncate
;
480 const char **label_path
;
481 struct diff_words_data
*diff_words
;
482 struct diff_options
*opt
;
483 struct strbuf
*header
;
486 static int count_lines(const char *data
, int size
)
488 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
495 completely_empty
= 0;
499 completely_empty
= 0;
502 if (completely_empty
)
505 count
++; /* no trailing newline */
509 static int fill_mmfile(mmfile_t
*mf
, struct diff_filespec
*one
)
511 if (!DIFF_FILE_VALID(one
)) {
512 mf
->ptr
= (char *)""; /* does not matter */
516 else if (diff_populate_filespec(one
, 0))
520 mf
->size
= one
->size
;
524 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
525 static unsigned long diff_filespec_size(struct diff_filespec
*one
)
527 if (!DIFF_FILE_VALID(one
))
529 diff_populate_filespec(one
, CHECK_SIZE_ONLY
);
533 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
536 long size
= mf
->size
;
541 ptr
+= size
- 1; /* pointing at the very end */
543 ; /* incomplete line */
545 ptr
--; /* skip the last LF */
546 while (mf
->ptr
< ptr
) {
548 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
549 if (*prev_eol
== '\n')
551 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
559 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
560 struct emit_callback
*ecbdata
)
563 unsigned ws_rule
= ecbdata
->ws_rule
;
564 l1
= count_trailing_blank(mf1
, ws_rule
);
565 l2
= count_trailing_blank(mf2
, ws_rule
);
567 ecbdata
->blank_at_eof_in_preimage
= 0;
568 ecbdata
->blank_at_eof_in_postimage
= 0;
571 at
= count_lines(mf1
->ptr
, mf1
->size
);
572 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
574 at
= count_lines(mf2
->ptr
, mf2
->size
);
575 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
578 static void emit_line_0(struct diff_options
*o
, const char *set
, const char *reset
,
579 int first
, const char *line
, int len
)
581 int has_trailing_newline
, has_trailing_carriage_return
;
583 FILE *file
= o
->file
;
585 fputs(diff_line_prefix(o
), file
);
588 has_trailing_newline
= (first
== '\n');
589 has_trailing_carriage_return
= (!has_trailing_newline
&&
591 nofirst
= has_trailing_newline
|| has_trailing_carriage_return
;
593 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
594 if (has_trailing_newline
)
596 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
597 if (has_trailing_carriage_return
)
602 if (len
|| !nofirst
) {
606 fwrite(line
, len
, 1, file
);
609 if (has_trailing_carriage_return
)
611 if (has_trailing_newline
)
615 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
616 const char *line
, int len
)
618 emit_line_0(o
, set
, reset
, line
[0], line
+1, len
-1);
622 DIFF_SYMBOL_BINARY_DIFF_HEADER
,
623 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
624 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
625 DIFF_SYMBOL_BINARY_DIFF_BODY
,
626 DIFF_SYMBOL_BINARY_DIFF_FOOTER
,
627 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
628 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
629 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
630 DIFF_SYMBOL_STATS_LINE
,
631 DIFF_SYMBOL_WORD_DIFF
,
632 DIFF_SYMBOL_STAT_SEP
,
634 DIFF_SYMBOL_SUBMODULE_ADD
,
635 DIFF_SYMBOL_SUBMODULE_DEL
,
636 DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
637 DIFF_SYMBOL_SUBMODULE_MODIFIED
,
638 DIFF_SYMBOL_SUBMODULE_HEADER
,
639 DIFF_SYMBOL_SUBMODULE_ERROR
,
640 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
,
641 DIFF_SYMBOL_REWRITE_DIFF
,
642 DIFF_SYMBOL_BINARY_FILES
,
644 DIFF_SYMBOL_FILEPAIR_PLUS
,
645 DIFF_SYMBOL_FILEPAIR_MINUS
,
646 DIFF_SYMBOL_WORDS_PORCELAIN
,
649 DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
652 DIFF_SYMBOL_NO_LF_EOF
,
653 DIFF_SYMBOL_CONTEXT_FRAGINFO
,
654 DIFF_SYMBOL_CONTEXT_MARKER
,
655 DIFF_SYMBOL_SEPARATOR
658 * Flags for content lines:
659 * 0..12 are whitespace rules
660 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
661 * 16 is marking if the line is blank at EOF
663 #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
664 #define DIFF_SYMBOL_MOVED_LINE (1<<17)
665 #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
666 #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
667 #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
670 * This struct is used when we need to buffer the output of the diff output.
672 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
673 * into the pre/post image file. This pointer could be a union with the
674 * line pointer. By storing an offset into the file instead of the literal line,
675 * we can decrease the memory footprint for the buffered output. At first we
676 * may want to only have indirection for the content lines, but we could also
677 * enhance the state for emitting prefabricated lines, e.g. the similarity
678 * score line or hunk/file headers would only need to store a number or path
679 * and then the output can be constructed later on depending on state.
681 struct emitted_diff_symbol
{
687 #define EMITTED_DIFF_SYMBOL_INIT {NULL}
689 struct emitted_diff_symbols
{
690 struct emitted_diff_symbol
*buf
;
693 #define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
695 static void append_emitted_diff_symbol(struct diff_options
*o
,
696 struct emitted_diff_symbol
*e
)
698 struct emitted_diff_symbol
*f
;
700 ALLOC_GROW(o
->emitted_symbols
->buf
,
701 o
->emitted_symbols
->nr
+ 1,
702 o
->emitted_symbols
->alloc
);
703 f
= &o
->emitted_symbols
->buf
[o
->emitted_symbols
->nr
++];
705 memcpy(f
, e
, sizeof(struct emitted_diff_symbol
));
706 f
->line
= e
->line
? xmemdupz(e
->line
, e
->len
) : NULL
;
710 struct hashmap_entry ent
;
711 const struct emitted_diff_symbol
*es
;
712 struct moved_entry
*next_line
;
715 static int next_byte(const char **cp
, const char **endp
,
716 const struct diff_options
*diffopt
)
723 if (DIFF_XDL_TST(diffopt
, IGNORE_WHITESPACE_CHANGE
)) {
724 while (*cp
< *endp
&& isspace(**cp
))
727 * After skipping a couple of whitespaces, we still have to
728 * account for one space.
733 if (DIFF_XDL_TST(diffopt
, IGNORE_WHITESPACE
)) {
734 while (*cp
< *endp
&& isspace(**cp
))
736 /* return the first non-ws character via the usual below */
739 retval
= (unsigned char)(**cp
);
744 static int moved_entry_cmp(const struct diff_options
*diffopt
,
745 const struct moved_entry
*a
,
746 const struct moved_entry
*b
,
749 const char *ap
= a
->es
->line
, *ae
= a
->es
->line
+ a
->es
->len
;
750 const char *bp
= b
->es
->line
, *be
= b
->es
->line
+ b
->es
->len
;
752 if (!(diffopt
->xdl_opts
& XDF_WHITESPACE_FLAGS
))
753 return a
->es
->len
!= b
->es
->len
|| memcmp(ap
, bp
, a
->es
->len
);
755 if (DIFF_XDL_TST(diffopt
, IGNORE_WHITESPACE_AT_EOL
)) {
756 while (ae
> ap
&& isspace(*ae
))
758 while (be
> bp
&& isspace(*be
))
764 ca
= next_byte(&ap
, &ae
, diffopt
);
765 cb
= next_byte(&bp
, &be
, diffopt
);
773 static unsigned get_string_hash(struct emitted_diff_symbol
*es
, struct diff_options
*o
)
775 if (o
->xdl_opts
& XDF_WHITESPACE_FLAGS
) {
776 static struct strbuf sb
= STRBUF_INIT
;
777 const char *ap
= es
->line
, *ae
= es
->line
+ es
->len
;
781 while (ae
> ap
&& isspace(*ae
))
783 while ((c
= next_byte(&ap
, &ae
, o
)) > 0)
784 strbuf_addch(&sb
, c
);
786 return memhash(sb
.buf
, sb
.len
);
788 return memhash(es
->line
, es
->len
);
792 static struct moved_entry
*prepare_entry(struct diff_options
*o
,
795 struct moved_entry
*ret
= xmalloc(sizeof(*ret
));
796 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[line_no
];
798 ret
->ent
.hash
= get_string_hash(l
, o
);
800 ret
->next_line
= NULL
;
805 static void add_lines_to_move_detection(struct diff_options
*o
,
806 struct hashmap
*add_lines
,
807 struct hashmap
*del_lines
)
809 struct moved_entry
*prev_line
= NULL
;
812 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
814 struct moved_entry
*key
;
816 switch (o
->emitted_symbols
->buf
[n
].s
) {
817 case DIFF_SYMBOL_PLUS
:
820 case DIFF_SYMBOL_MINUS
:
828 key
= prepare_entry(o
, n
);
829 if (prev_line
&& prev_line
->es
->s
== o
->emitted_symbols
->buf
[n
].s
)
830 prev_line
->next_line
= key
;
832 hashmap_add(hm
, key
);
837 static int shrink_potential_moved_blocks(struct moved_entry
**pmb
,
842 /* Shrink the set of potential block to the remaining running */
843 for (lp
= 0, rp
= pmb_nr
- 1; lp
<= rp
;) {
844 while (lp
< pmb_nr
&& pmb
[lp
])
846 /* lp points at the first NULL now */
848 while (rp
> -1 && !pmb
[rp
])
850 /* rp points at the last non-NULL */
852 if (lp
< pmb_nr
&& rp
> -1 && lp
< rp
) {
860 /* Remember the number of running sets */
865 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
867 * Otherwise, if the last block has fewer alphanumeric characters than
868 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
871 * The last block consists of the (n - block_length)'th line up to but not
872 * including the nth line.
874 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
875 * Think of a way to unify them.
877 static void adjust_last_block(struct diff_options
*o
, int n
, int block_length
)
879 int i
, alnum_count
= 0;
880 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
882 for (i
= 1; i
< block_length
+ 1; i
++) {
883 const char *c
= o
->emitted_symbols
->buf
[n
- i
].line
;
888 if (alnum_count
>= COLOR_MOVED_MIN_ALNUM_COUNT
)
892 for (i
= 1; i
< block_length
+ 1; i
++)
893 o
->emitted_symbols
->buf
[n
- i
].flags
&= ~DIFF_SYMBOL_MOVED_LINE
;
896 /* Find blocks of moved code, delegate actual coloring decision to helper */
897 static void mark_color_as_moved(struct diff_options
*o
,
898 struct hashmap
*add_lines
,
899 struct hashmap
*del_lines
)
901 struct moved_entry
**pmb
= NULL
; /* potentially moved blocks */
902 int pmb_nr
= 0, pmb_alloc
= 0;
903 int n
, flipped_block
= 1, block_length
= 0;
906 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
907 struct hashmap
*hm
= NULL
;
908 struct moved_entry
*key
;
909 struct moved_entry
*match
= NULL
;
910 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
914 case DIFF_SYMBOL_PLUS
:
916 key
= prepare_entry(o
, n
);
917 match
= hashmap_get(hm
, key
, o
);
920 case DIFF_SYMBOL_MINUS
:
922 key
= prepare_entry(o
, n
);
923 match
= hashmap_get(hm
, key
, o
);
931 adjust_last_block(o
, n
, block_length
);
937 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
939 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
942 /* Check any potential block runs, advance each or nullify */
943 for (i
= 0; i
< pmb_nr
; i
++) {
944 struct moved_entry
*p
= pmb
[i
];
945 struct moved_entry
*pnext
= (p
&& p
->next_line
) ?
947 if (pnext
&& !hm
->cmpfn(o
, pnext
, match
, NULL
)) {
948 pmb
[i
] = p
->next_line
;
954 pmb_nr
= shrink_potential_moved_blocks(pmb
, pmb_nr
);
958 * The current line is the start of a new block.
959 * Setup the set of potential blocks.
961 for (; match
; match
= hashmap_get_next(hm
, match
)) {
962 ALLOC_GROW(pmb
, pmb_nr
+ 1, pmb_alloc
);
963 pmb
[pmb_nr
++] = match
;
966 flipped_block
= (flipped_block
+ 1) % 2;
968 adjust_last_block(o
, n
, block_length
);
975 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_ALT
;
977 adjust_last_block(o
, n
, block_length
);
982 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
983 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
984 static void dim_moved_lines(struct diff_options
*o
)
987 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
988 struct emitted_diff_symbol
*prev
= (n
!= 0) ?
989 &o
->emitted_symbols
->buf
[n
- 1] : NULL
;
990 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
991 struct emitted_diff_symbol
*next
=
992 (n
< o
->emitted_symbols
->nr
- 1) ?
993 &o
->emitted_symbols
->buf
[n
+ 1] : NULL
;
995 /* Not a plus or minus line? */
996 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
)
999 /* Not a moved line? */
1000 if (!(l
->flags
& DIFF_SYMBOL_MOVED_LINE
))
1004 * If prev or next are not a plus or minus line,
1005 * pretend they don't exist
1007 if (prev
&& prev
->s
!= DIFF_SYMBOL_PLUS
&&
1008 prev
->s
!= DIFF_SYMBOL_MINUS
)
1010 if (next
&& next
->s
!= DIFF_SYMBOL_PLUS
&&
1011 next
->s
!= DIFF_SYMBOL_MINUS
)
1014 /* Inside a block? */
1016 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1017 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
)) &&
1019 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1020 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
))) {
1021 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1025 /* Check if we are at an interesting bound: */
1026 if (prev
&& (prev
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1027 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1028 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1030 if (next
&& (next
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1031 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1032 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1036 * The boundary to prev and next are not interesting,
1037 * so this line is not interesting as a whole
1039 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1043 static void emit_line_ws_markup(struct diff_options
*o
,
1044 const char *set
, const char *reset
,
1045 const char *line
, int len
, char sign
,
1046 unsigned ws_rule
, int blank_at_eof
)
1048 const char *ws
= NULL
;
1050 if (o
->ws_error_highlight
& ws_rule
) {
1051 ws
= diff_get_color_opt(o
, DIFF_WHITESPACE
);
1057 emit_line_0(o
, set
, reset
, sign
, line
, len
);
1058 else if (blank_at_eof
)
1059 /* Blank line at EOF - paint '+' as well */
1060 emit_line_0(o
, ws
, reset
, sign
, line
, len
);
1062 /* Emit just the prefix, then the rest. */
1063 emit_line_0(o
, set
, reset
, sign
, "", 0);
1064 ws_check_emit(line
, len
, ws_rule
,
1065 o
->file
, set
, reset
, ws
);
1069 static void emit_diff_symbol_from_struct(struct diff_options
*o
,
1070 struct emitted_diff_symbol
*eds
)
1072 static const char *nneof
= " No newline at end of file\n";
1073 const char *context
, *reset
, *set
, *meta
, *fraginfo
;
1074 struct strbuf sb
= STRBUF_INIT
;
1076 enum diff_symbol s
= eds
->s
;
1077 const char *line
= eds
->line
;
1079 unsigned flags
= eds
->flags
;
1082 case DIFF_SYMBOL_NO_LF_EOF
:
1083 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1084 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1085 putc('\n', o
->file
);
1086 emit_line_0(o
, context
, reset
, '\\',
1087 nneof
, strlen(nneof
));
1089 case DIFF_SYMBOL_SUBMODULE_HEADER
:
1090 case DIFF_SYMBOL_SUBMODULE_ERROR
:
1091 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
:
1092 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
:
1093 case DIFF_SYMBOL_SUMMARY
:
1094 case DIFF_SYMBOL_STATS_LINE
:
1095 case DIFF_SYMBOL_BINARY_DIFF_BODY
:
1096 case DIFF_SYMBOL_CONTEXT_FRAGINFO
:
1097 emit_line(o
, "", "", line
, len
);
1099 case DIFF_SYMBOL_CONTEXT_INCOMPLETE
:
1100 case DIFF_SYMBOL_CONTEXT_MARKER
:
1101 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1102 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1103 emit_line(o
, context
, reset
, line
, len
);
1105 case DIFF_SYMBOL_SEPARATOR
:
1106 fprintf(o
->file
, "%s%c",
1107 diff_line_prefix(o
),
1108 o
->line_termination
);
1110 case DIFF_SYMBOL_CONTEXT
:
1111 set
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1112 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1113 emit_line_ws_markup(o
, set
, reset
, line
, len
, ' ',
1114 flags
& (DIFF_SYMBOL_CONTENT_WS_MASK
), 0);
1116 case DIFF_SYMBOL_PLUS
:
1117 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1118 DIFF_SYMBOL_MOVED_LINE_ALT
|
1119 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1120 case DIFF_SYMBOL_MOVED_LINE
|
1121 DIFF_SYMBOL_MOVED_LINE_ALT
|
1122 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1123 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT_DIM
);
1125 case DIFF_SYMBOL_MOVED_LINE
|
1126 DIFF_SYMBOL_MOVED_LINE_ALT
:
1127 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT
);
1129 case DIFF_SYMBOL_MOVED_LINE
|
1130 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1131 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_DIM
);
1133 case DIFF_SYMBOL_MOVED_LINE
:
1134 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED
);
1137 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1139 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1140 emit_line_ws_markup(o
, set
, reset
, line
, len
, '+',
1141 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
,
1142 flags
& DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
);
1144 case DIFF_SYMBOL_MINUS
:
1145 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1146 DIFF_SYMBOL_MOVED_LINE_ALT
|
1147 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1148 case DIFF_SYMBOL_MOVED_LINE
|
1149 DIFF_SYMBOL_MOVED_LINE_ALT
|
1150 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1151 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT_DIM
);
1153 case DIFF_SYMBOL_MOVED_LINE
|
1154 DIFF_SYMBOL_MOVED_LINE_ALT
:
1155 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT
);
1157 case DIFF_SYMBOL_MOVED_LINE
|
1158 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1159 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_DIM
);
1161 case DIFF_SYMBOL_MOVED_LINE
:
1162 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED
);
1165 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1167 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1168 emit_line_ws_markup(o
, set
, reset
, line
, len
, '-',
1169 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
, 0);
1171 case DIFF_SYMBOL_WORDS_PORCELAIN
:
1172 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1173 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1174 emit_line(o
, context
, reset
, line
, len
);
1175 fputs("~\n", o
->file
);
1177 case DIFF_SYMBOL_WORDS
:
1178 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1179 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1181 * Skip the prefix character, if any. With
1182 * diff_suppress_blank_empty, there may be
1185 if (line
[0] != '\n') {
1189 emit_line(o
, context
, reset
, line
, len
);
1191 case DIFF_SYMBOL_FILEPAIR_PLUS
:
1192 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1193 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1194 fprintf(o
->file
, "%s%s+++ %s%s%s\n", diff_line_prefix(o
), meta
,
1196 strchr(line
, ' ') ? "\t" : "");
1198 case DIFF_SYMBOL_FILEPAIR_MINUS
:
1199 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1200 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1201 fprintf(o
->file
, "%s%s--- %s%s%s\n", diff_line_prefix(o
), meta
,
1203 strchr(line
, ' ') ? "\t" : "");
1205 case DIFF_SYMBOL_BINARY_FILES
:
1206 case DIFF_SYMBOL_HEADER
:
1207 fprintf(o
->file
, "%s", line
);
1209 case DIFF_SYMBOL_BINARY_DIFF_HEADER
:
1210 fprintf(o
->file
, "%sGIT binary patch\n", diff_line_prefix(o
));
1212 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
:
1213 fprintf(o
->file
, "%sdelta %s\n", diff_line_prefix(o
), line
);
1215 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
:
1216 fprintf(o
->file
, "%sliteral %s\n", diff_line_prefix(o
), line
);
1218 case DIFF_SYMBOL_BINARY_DIFF_FOOTER
:
1219 fputs(diff_line_prefix(o
), o
->file
);
1220 fputc('\n', o
->file
);
1222 case DIFF_SYMBOL_REWRITE_DIFF
:
1223 fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
1224 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1225 emit_line(o
, fraginfo
, reset
, line
, len
);
1227 case DIFF_SYMBOL_SUBMODULE_ADD
:
1228 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1229 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1230 emit_line(o
, set
, reset
, line
, len
);
1232 case DIFF_SYMBOL_SUBMODULE_DEL
:
1233 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1234 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1235 emit_line(o
, set
, reset
, line
, len
);
1237 case DIFF_SYMBOL_SUBMODULE_UNTRACKED
:
1238 fprintf(o
->file
, "%sSubmodule %s contains untracked content\n",
1239 diff_line_prefix(o
), line
);
1241 case DIFF_SYMBOL_SUBMODULE_MODIFIED
:
1242 fprintf(o
->file
, "%sSubmodule %s contains modified content\n",
1243 diff_line_prefix(o
), line
);
1245 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
:
1246 emit_line(o
, "", "", " 0 files changed\n",
1247 strlen(" 0 files changed\n"));
1249 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV
:
1250 emit_line(o
, "", "", " ...\n", strlen(" ...\n"));
1252 case DIFF_SYMBOL_WORD_DIFF
:
1253 fprintf(o
->file
, "%.*s", len
, line
);
1255 case DIFF_SYMBOL_STAT_SEP
:
1256 fputs(o
->stat_sep
, o
->file
);
1259 die("BUG: unknown diff symbol");
1261 strbuf_release(&sb
);
1264 static void emit_diff_symbol(struct diff_options
*o
, enum diff_symbol s
,
1265 const char *line
, int len
, unsigned flags
)
1267 struct emitted_diff_symbol e
= {line
, len
, flags
, s
};
1269 if (o
->emitted_symbols
)
1270 append_emitted_diff_symbol(o
, &e
);
1272 emit_diff_symbol_from_struct(o
, &e
);
1275 void diff_emit_submodule_del(struct diff_options
*o
, const char *line
)
1277 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_DEL
, line
, strlen(line
), 0);
1280 void diff_emit_submodule_add(struct diff_options
*o
, const char *line
)
1282 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ADD
, line
, strlen(line
), 0);
1285 void diff_emit_submodule_untracked(struct diff_options
*o
, const char *path
)
1287 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
1288 path
, strlen(path
), 0);
1291 void diff_emit_submodule_modified(struct diff_options
*o
, const char *path
)
1293 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_MODIFIED
,
1294 path
, strlen(path
), 0);
1297 void diff_emit_submodule_header(struct diff_options
*o
, const char *header
)
1299 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_HEADER
,
1300 header
, strlen(header
), 0);
1303 void diff_emit_submodule_error(struct diff_options
*o
, const char *err
)
1305 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ERROR
, err
, strlen(err
), 0);
1308 void diff_emit_submodule_pipethrough(struct diff_options
*o
,
1309 const char *line
, int len
)
1311 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
, line
, len
, 0);
1314 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
1316 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
1317 ecbdata
->blank_at_eof_in_preimage
&&
1318 ecbdata
->blank_at_eof_in_postimage
&&
1319 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
1320 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
1322 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
1325 static void emit_add_line(const char *reset
,
1326 struct emit_callback
*ecbdata
,
1327 const char *line
, int len
)
1329 unsigned flags
= WSEH_NEW
| ecbdata
->ws_rule
;
1330 if (new_blank_line_at_eof(ecbdata
, line
, len
))
1331 flags
|= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
;
1333 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_PLUS
, line
, len
, flags
);
1336 static void emit_del_line(const char *reset
,
1337 struct emit_callback
*ecbdata
,
1338 const char *line
, int len
)
1340 unsigned flags
= WSEH_OLD
| ecbdata
->ws_rule
;
1341 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_MINUS
, line
, len
, flags
);
1344 static void emit_context_line(const char *reset
,
1345 struct emit_callback
*ecbdata
,
1346 const char *line
, int len
)
1348 unsigned flags
= WSEH_CONTEXT
| ecbdata
->ws_rule
;
1349 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_CONTEXT
, line
, len
, flags
);
1352 static void emit_hunk_header(struct emit_callback
*ecbdata
,
1353 const char *line
, int len
)
1355 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1356 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
1357 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
1358 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1359 static const char atat
[2] = { '@', '@' };
1360 const char *cp
, *ep
;
1361 struct strbuf msgbuf
= STRBUF_INIT
;
1366 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1367 * it always is at least 10 bytes long.
1370 memcmp(line
, atat
, 2) ||
1371 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
1372 emit_diff_symbol(ecbdata
->opt
,
1373 DIFF_SYMBOL_CONTEXT_MARKER
, line
, len
, 0);
1376 ep
+= 2; /* skip over @@ */
1378 /* The hunk header in fraginfo color */
1379 strbuf_addstr(&msgbuf
, frag
);
1380 strbuf_add(&msgbuf
, line
, ep
- line
);
1381 strbuf_addstr(&msgbuf
, reset
);
1387 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
1390 /* blank before the func header */
1391 for (cp
= ep
; ep
- line
< len
; ep
++)
1392 if (*ep
!= ' ' && *ep
!= '\t')
1395 strbuf_addstr(&msgbuf
, context
);
1396 strbuf_add(&msgbuf
, cp
, ep
- cp
);
1397 strbuf_addstr(&msgbuf
, reset
);
1400 if (ep
< line
+ len
) {
1401 strbuf_addstr(&msgbuf
, func
);
1402 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
1403 strbuf_addstr(&msgbuf
, reset
);
1406 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
1407 strbuf_complete_line(&msgbuf
);
1408 emit_diff_symbol(ecbdata
->opt
,
1409 DIFF_SYMBOL_CONTEXT_FRAGINFO
, msgbuf
.buf
, msgbuf
.len
, 0);
1410 strbuf_release(&msgbuf
);
1413 static struct diff_tempfile
*claim_diff_tempfile(void) {
1415 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
1416 if (!diff_temp
[i
].name
)
1417 return diff_temp
+ i
;
1418 die("BUG: diff is failing to clean up its tempfiles");
1421 static void remove_tempfile(void)
1424 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
1425 if (is_tempfile_active(&diff_temp
[i
].tempfile
))
1426 delete_tempfile(&diff_temp
[i
].tempfile
);
1427 diff_temp
[i
].name
= NULL
;
1431 static void add_line_count(struct strbuf
*out
, int count
)
1435 strbuf_addstr(out
, "0,0");
1438 strbuf_addstr(out
, "1");
1441 strbuf_addf(out
, "1,%d", count
);
1446 static void emit_rewrite_lines(struct emit_callback
*ecb
,
1447 int prefix
, const char *data
, int size
)
1449 const char *endp
= NULL
;
1450 const char *reset
= diff_get_color(ecb
->color_diff
, DIFF_RESET
);
1455 endp
= memchr(data
, '\n', size
);
1456 len
= endp
? (endp
- data
+ 1) : size
;
1457 if (prefix
!= '+') {
1458 ecb
->lno_in_preimage
++;
1459 emit_del_line(reset
, ecb
, data
, len
);
1461 ecb
->lno_in_postimage
++;
1462 emit_add_line(reset
, ecb
, data
, len
);
1468 emit_diff_symbol(ecb
->opt
, DIFF_SYMBOL_NO_LF_EOF
, NULL
, 0, 0);
1471 static void emit_rewrite_diff(const char *name_a
,
1473 struct diff_filespec
*one
,
1474 struct diff_filespec
*two
,
1475 struct userdiff_driver
*textconv_one
,
1476 struct userdiff_driver
*textconv_two
,
1477 struct diff_options
*o
)
1480 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
1481 const char *a_prefix
, *b_prefix
;
1482 char *data_one
, *data_two
;
1483 size_t size_one
, size_two
;
1484 struct emit_callback ecbdata
;
1485 struct strbuf out
= STRBUF_INIT
;
1487 if (diff_mnemonic_prefix
&& DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
1488 a_prefix
= o
->b_prefix
;
1489 b_prefix
= o
->a_prefix
;
1491 a_prefix
= o
->a_prefix
;
1492 b_prefix
= o
->b_prefix
;
1495 name_a
+= (*name_a
== '/');
1496 name_b
+= (*name_b
== '/');
1498 strbuf_reset(&a_name
);
1499 strbuf_reset(&b_name
);
1500 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
1501 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
1503 size_one
= fill_textconv(textconv_one
, one
, &data_one
);
1504 size_two
= fill_textconv(textconv_two
, two
, &data_two
);
1506 memset(&ecbdata
, 0, sizeof(ecbdata
));
1507 ecbdata
.color_diff
= want_color(o
->use_color
);
1508 ecbdata
.ws_rule
= whitespace_rule(name_b
);
1510 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
1512 mf1
.ptr
= (char *)data_one
;
1513 mf2
.ptr
= (char *)data_two
;
1514 mf1
.size
= size_one
;
1515 mf2
.size
= size_two
;
1516 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1518 ecbdata
.lno_in_preimage
= 1;
1519 ecbdata
.lno_in_postimage
= 1;
1521 lc_a
= count_lines(data_one
, size_one
);
1522 lc_b
= count_lines(data_two
, size_two
);
1524 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1525 a_name
.buf
, a_name
.len
, 0);
1526 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1527 b_name
.buf
, b_name
.len
, 0);
1529 strbuf_addstr(&out
, "@@ -");
1530 if (!o
->irreversible_delete
)
1531 add_line_count(&out
, lc_a
);
1533 strbuf_addstr(&out
, "?,?");
1534 strbuf_addstr(&out
, " +");
1535 add_line_count(&out
, lc_b
);
1536 strbuf_addstr(&out
, " @@\n");
1537 emit_diff_symbol(o
, DIFF_SYMBOL_REWRITE_DIFF
, out
.buf
, out
.len
, 0);
1538 strbuf_release(&out
);
1540 if (lc_a
&& !o
->irreversible_delete
)
1541 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
1543 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
1545 free((char *)data_one
);
1547 free((char *)data_two
);
1550 struct diff_words_buffer
{
1553 struct diff_words_orig
{
1554 const char *begin
, *end
;
1556 int orig_nr
, orig_alloc
;
1559 static void diff_words_append(char *line
, unsigned long len
,
1560 struct diff_words_buffer
*buffer
)
1562 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
1565 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
1566 buffer
->text
.size
+= len
;
1567 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
1570 struct diff_words_style_elem
{
1573 const char *color
; /* NULL; filled in by the setup code if
1574 * color is enabled */
1577 struct diff_words_style
{
1578 enum diff_words_type type
;
1579 struct diff_words_style_elem
new, old
, ctx
;
1580 const char *newline
;
1583 static struct diff_words_style diff_words_styles
[] = {
1584 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1585 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1586 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
1589 struct diff_words_data
{
1590 struct diff_words_buffer minus
, plus
;
1591 const char *current_plus
;
1593 struct diff_options
*opt
;
1594 regex_t
*word_regex
;
1595 enum diff_words_type type
;
1596 struct diff_words_style
*style
;
1599 static int fn_out_diff_words_write_helper(struct diff_options
*o
,
1600 struct diff_words_style_elem
*st_el
,
1601 const char *newline
,
1602 size_t count
, const char *buf
)
1605 struct strbuf sb
= STRBUF_INIT
;
1608 char *p
= memchr(buf
, '\n', count
);
1610 strbuf_addstr(&sb
, diff_line_prefix(o
));
1613 const char *reset
= st_el
->color
&& *st_el
->color
?
1614 GIT_COLOR_RESET
: NULL
;
1615 if (st_el
->color
&& *st_el
->color
)
1616 strbuf_addstr(&sb
, st_el
->color
);
1617 strbuf_addstr(&sb
, st_el
->prefix
);
1618 strbuf_add(&sb
, buf
, p
? p
- buf
: count
);
1619 strbuf_addstr(&sb
, st_el
->suffix
);
1621 strbuf_addstr(&sb
, reset
);
1626 strbuf_addstr(&sb
, newline
);
1627 count
-= p
+ 1 - buf
;
1631 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1639 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1641 strbuf_release(&sb
);
1646 * '--color-words' algorithm can be described as:
1648 * 1. collect the minus/plus lines of a diff hunk, divided into
1649 * minus-lines and plus-lines;
1651 * 2. break both minus-lines and plus-lines into words and
1652 * place them into two mmfile_t with one word for each line;
1654 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1656 * And for the common parts of the both file, we output the plus side text.
1657 * diff_words->current_plus is used to trace the current position of the plus file
1658 * which printed. diff_words->last_minus is used to trace the last minus word
1661 * For '--graph' to work with '--color-words', we need to output the graph prefix
1662 * on each line of color words output. Generally, there are two conditions on
1663 * which we should output the prefix.
1665 * 1. diff_words->last_minus == 0 &&
1666 * diff_words->current_plus == diff_words->plus.text.ptr
1668 * that is: the plus text must start as a new line, and if there is no minus
1669 * word printed, a graph prefix must be printed.
1671 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1672 * *(diff_words->current_plus - 1) == '\n'
1674 * that is: a graph prefix must be printed following a '\n'
1676 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
1678 if ((diff_words
->last_minus
== 0 &&
1679 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
1680 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
1681 *(diff_words
->current_plus
- 1) == '\n')) {
1688 static void fn_out_diff_words_aux(void *priv
, char *line
, unsigned long len
)
1690 struct diff_words_data
*diff_words
= priv
;
1691 struct diff_words_style
*style
= diff_words
->style
;
1692 int minus_first
, minus_len
, plus_first
, plus_len
;
1693 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
1694 struct diff_options
*opt
= diff_words
->opt
;
1695 const char *line_prefix
;
1697 if (line
[0] != '@' || parse_hunk_header(line
, len
,
1698 &minus_first
, &minus_len
, &plus_first
, &plus_len
))
1702 line_prefix
= diff_line_prefix(opt
);
1704 /* POSIX requires that first be decremented by one if len == 0... */
1706 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
1708 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
1710 minus_begin
= minus_end
=
1711 diff_words
->minus
.orig
[minus_first
].end
;
1714 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
1715 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
1717 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
1719 if (color_words_output_graph_prefix(diff_words
)) {
1720 fputs(line_prefix
, diff_words
->opt
->file
);
1722 if (diff_words
->current_plus
!= plus_begin
) {
1723 fn_out_diff_words_write_helper(diff_words
->opt
,
1724 &style
->ctx
, style
->newline
,
1725 plus_begin
- diff_words
->current_plus
,
1726 diff_words
->current_plus
);
1728 if (minus_begin
!= minus_end
) {
1729 fn_out_diff_words_write_helper(diff_words
->opt
,
1730 &style
->old
, style
->newline
,
1731 minus_end
- minus_begin
, minus_begin
);
1733 if (plus_begin
!= plus_end
) {
1734 fn_out_diff_words_write_helper(diff_words
->opt
,
1735 &style
->new, style
->newline
,
1736 plus_end
- plus_begin
, plus_begin
);
1739 diff_words
->current_plus
= plus_end
;
1740 diff_words
->last_minus
= minus_first
;
1743 /* This function starts looking at *begin, and returns 0 iff a word was found. */
1744 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
1745 int *begin
, int *end
)
1747 if (word_regex
&& *begin
< buffer
->size
) {
1748 regmatch_t match
[1];
1749 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
1750 buffer
->size
- *begin
, 1, match
, 0)) {
1751 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
1752 '\n', match
[0].rm_eo
- match
[0].rm_so
);
1753 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
1754 *begin
+= match
[0].rm_so
;
1755 return *begin
>= *end
;
1760 /* find the next word */
1761 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
1763 if (*begin
>= buffer
->size
)
1766 /* find the end of the word */
1768 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
1775 * This function splits the words in buffer->text, stores the list with
1776 * newline separator into out, and saves the offsets of the original words
1779 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
1780 regex_t
*word_regex
)
1788 /* fake an empty "0th" word */
1789 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
1790 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
1791 buffer
->orig_nr
= 1;
1793 for (i
= 0; i
< buffer
->text
.size
; i
++) {
1794 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
1797 /* store original boundaries */
1798 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
1799 buffer
->orig_alloc
);
1800 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
1801 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
1804 /* store one word */
1805 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
1806 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
1807 out
->ptr
[out
->size
+ j
- i
] = '\n';
1808 out
->size
+= j
- i
+ 1;
1814 /* this executes the word diff on the accumulated buffers */
1815 static void diff_words_show(struct diff_words_data
*diff_words
)
1819 mmfile_t minus
, plus
;
1820 struct diff_words_style
*style
= diff_words
->style
;
1822 struct diff_options
*opt
= diff_words
->opt
;
1823 const char *line_prefix
;
1826 line_prefix
= diff_line_prefix(opt
);
1828 /* special case: only removal */
1829 if (!diff_words
->plus
.text
.size
) {
1830 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
1831 line_prefix
, strlen(line_prefix
), 0);
1832 fn_out_diff_words_write_helper(diff_words
->opt
,
1833 &style
->old
, style
->newline
,
1834 diff_words
->minus
.text
.size
,
1835 diff_words
->minus
.text
.ptr
);
1836 diff_words
->minus
.text
.size
= 0;
1840 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
1841 diff_words
->last_minus
= 0;
1843 memset(&xpp
, 0, sizeof(xpp
));
1844 memset(&xecfg
, 0, sizeof(xecfg
));
1845 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
1846 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
1848 /* as only the hunk header will be parsed, we need a 0-context */
1850 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, diff_words
,
1852 die("unable to generate word diff");
1855 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
1856 diff_words
->plus
.text
.size
) {
1857 if (color_words_output_graph_prefix(diff_words
))
1858 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
1859 line_prefix
, strlen(line_prefix
), 0);
1860 fn_out_diff_words_write_helper(diff_words
->opt
,
1861 &style
->ctx
, style
->newline
,
1862 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
1863 - diff_words
->current_plus
, diff_words
->current_plus
);
1865 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
1868 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
1869 static void diff_words_flush(struct emit_callback
*ecbdata
)
1871 struct diff_options
*wo
= ecbdata
->diff_words
->opt
;
1873 if (ecbdata
->diff_words
->minus
.text
.size
||
1874 ecbdata
->diff_words
->plus
.text
.size
)
1875 diff_words_show(ecbdata
->diff_words
);
1877 if (wo
->emitted_symbols
) {
1878 struct diff_options
*o
= ecbdata
->opt
;
1879 struct emitted_diff_symbols
*wol
= wo
->emitted_symbols
;
1884 * Instead of appending each, concat all words to a line?
1886 for (i
= 0; i
< wol
->nr
; i
++)
1887 append_emitted_diff_symbol(o
, &wol
->buf
[i
]);
1889 for (i
= 0; i
< wol
->nr
; i
++)
1890 free((void *)wol
->buf
[i
].line
);
1896 static void diff_filespec_load_driver(struct diff_filespec
*one
)
1898 /* Use already-loaded driver */
1902 if (S_ISREG(one
->mode
))
1903 one
->driver
= userdiff_find_by_path(one
->path
);
1905 /* Fallback to default settings */
1907 one
->driver
= userdiff_find_by_name("default");
1910 static const char *userdiff_word_regex(struct diff_filespec
*one
)
1912 diff_filespec_load_driver(one
);
1913 return one
->driver
->word_regex
;
1916 static void init_diff_words_data(struct emit_callback
*ecbdata
,
1917 struct diff_options
*orig_opts
,
1918 struct diff_filespec
*one
,
1919 struct diff_filespec
*two
)
1922 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
1923 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
1925 ecbdata
->diff_words
=
1926 xcalloc(1, sizeof(struct diff_words_data
));
1927 ecbdata
->diff_words
->type
= o
->word_diff
;
1928 ecbdata
->diff_words
->opt
= o
;
1930 if (orig_opts
->emitted_symbols
)
1931 o
->emitted_symbols
=
1932 xcalloc(1, sizeof(struct emitted_diff_symbols
));
1935 o
->word_regex
= userdiff_word_regex(one
);
1937 o
->word_regex
= userdiff_word_regex(two
);
1939 o
->word_regex
= diff_word_regex_cfg
;
1940 if (o
->word_regex
) {
1941 ecbdata
->diff_words
->word_regex
= (regex_t
*)
1942 xmalloc(sizeof(regex_t
));
1943 if (regcomp(ecbdata
->diff_words
->word_regex
,
1945 REG_EXTENDED
| REG_NEWLINE
))
1946 die ("Invalid regular expression: %s",
1949 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
1950 if (o
->word_diff
== diff_words_styles
[i
].type
) {
1951 ecbdata
->diff_words
->style
=
1952 &diff_words_styles
[i
];
1956 if (want_color(o
->use_color
)) {
1957 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
1958 st
->old
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1959 st
->new.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1960 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1964 static void free_diff_words_data(struct emit_callback
*ecbdata
)
1966 if (ecbdata
->diff_words
) {
1967 diff_words_flush(ecbdata
);
1968 free (ecbdata
->diff_words
->opt
->emitted_symbols
);
1969 free (ecbdata
->diff_words
->opt
);
1970 free (ecbdata
->diff_words
->minus
.text
.ptr
);
1971 free (ecbdata
->diff_words
->minus
.orig
);
1972 free (ecbdata
->diff_words
->plus
.text
.ptr
);
1973 free (ecbdata
->diff_words
->plus
.orig
);
1974 if (ecbdata
->diff_words
->word_regex
) {
1975 regfree(ecbdata
->diff_words
->word_regex
);
1976 free(ecbdata
->diff_words
->word_regex
);
1978 FREE_AND_NULL(ecbdata
->diff_words
);
1982 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
1984 if (want_color(diff_use_color
))
1985 return diff_colors
[ix
];
1989 const char *diff_line_prefix(struct diff_options
*opt
)
1991 struct strbuf
*msgbuf
;
1992 if (!opt
->output_prefix
)
1995 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
1999 static unsigned long sane_truncate_line(struct emit_callback
*ecb
, char *line
, unsigned long len
)
2002 unsigned long allot
;
2006 return ecb
->truncate(line
, len
);
2010 (void) utf8_width(&cp
, &l
);
2012 break; /* truncated in the middle? */
2017 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
2020 ecbdata
->lno_in_preimage
= 0;
2021 ecbdata
->lno_in_postimage
= 0;
2022 p
= strchr(line
, '-');
2024 return; /* cannot happen */
2025 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
2028 return; /* cannot happen */
2029 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
2032 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
2034 struct emit_callback
*ecbdata
= priv
;
2035 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
2036 struct diff_options
*o
= ecbdata
->opt
;
2038 o
->found_changes
= 1;
2040 if (ecbdata
->header
) {
2041 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
2042 ecbdata
->header
->buf
, ecbdata
->header
->len
, 0);
2043 strbuf_reset(ecbdata
->header
);
2044 ecbdata
->header
= NULL
;
2047 if (ecbdata
->label_path
[0]) {
2048 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
2049 ecbdata
->label_path
[0],
2050 strlen(ecbdata
->label_path
[0]), 0);
2051 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
2052 ecbdata
->label_path
[1],
2053 strlen(ecbdata
->label_path
[1]), 0);
2054 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
2057 if (diff_suppress_blank_empty
2058 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
2063 if (line
[0] == '@') {
2064 if (ecbdata
->diff_words
)
2065 diff_words_flush(ecbdata
);
2066 len
= sane_truncate_line(ecbdata
, line
, len
);
2067 find_lno(line
, ecbdata
);
2068 emit_hunk_header(ecbdata
, line
, len
);
2072 if (ecbdata
->diff_words
) {
2073 enum diff_symbol s
=
2074 ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
?
2075 DIFF_SYMBOL_WORDS_PORCELAIN
: DIFF_SYMBOL_WORDS
;
2076 if (line
[0] == '-') {
2077 diff_words_append(line
, len
,
2078 &ecbdata
->diff_words
->minus
);
2080 } else if (line
[0] == '+') {
2081 diff_words_append(line
, len
,
2082 &ecbdata
->diff_words
->plus
);
2084 } else if (starts_with(line
, "\\ ")) {
2086 * Eat the "no newline at eof" marker as if we
2087 * saw a "+" or "-" line with nothing on it,
2088 * and return without diff_words_flush() to
2089 * defer processing. If this is the end of
2090 * preimage, more "+" lines may come after it.
2094 diff_words_flush(ecbdata
);
2095 emit_diff_symbol(o
, s
, line
, len
, 0);
2101 ecbdata
->lno_in_postimage
++;
2102 emit_add_line(reset
, ecbdata
, line
+ 1, len
- 1);
2105 ecbdata
->lno_in_preimage
++;
2106 emit_del_line(reset
, ecbdata
, line
+ 1, len
- 1);
2109 ecbdata
->lno_in_postimage
++;
2110 ecbdata
->lno_in_preimage
++;
2111 emit_context_line(reset
, ecbdata
, line
+ 1, len
- 1);
2114 /* incomplete line at the end */
2115 ecbdata
->lno_in_preimage
++;
2116 emit_diff_symbol(o
, DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
2122 static char *pprint_rename(const char *a
, const char *b
)
2124 const char *old
= a
;
2125 const char *new = b
;
2126 struct strbuf name
= STRBUF_INIT
;
2127 int pfx_length
, sfx_length
;
2128 int pfx_adjust_for_slash
;
2129 int len_a
= strlen(a
);
2130 int len_b
= strlen(b
);
2131 int a_midlen
, b_midlen
;
2132 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
2133 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
2135 if (qlen_a
|| qlen_b
) {
2136 quote_c_style(a
, &name
, NULL
, 0);
2137 strbuf_addstr(&name
, " => ");
2138 quote_c_style(b
, &name
, NULL
, 0);
2139 return strbuf_detach(&name
, NULL
);
2142 /* Find common prefix */
2144 while (*old
&& *new && *old
== *new) {
2146 pfx_length
= old
- a
+ 1;
2151 /* Find common suffix */
2156 * If there is a common prefix, it must end in a slash. In
2157 * that case we let this loop run 1 into the prefix to see the
2160 * If there is no common prefix, we cannot do this as it would
2161 * underrun the input strings.
2163 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
2164 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old
&&
2165 b
+ pfx_length
- pfx_adjust_for_slash
<= new &&
2168 sfx_length
= len_a
- (old
- a
);
2174 * pfx{mid-a => mid-b}sfx
2175 * {pfx-a => pfx-b}sfx
2176 * pfx{sfx-a => sfx-b}
2179 a_midlen
= len_a
- pfx_length
- sfx_length
;
2180 b_midlen
= len_b
- pfx_length
- sfx_length
;
2186 strbuf_grow(&name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
2187 if (pfx_length
+ sfx_length
) {
2188 strbuf_add(&name
, a
, pfx_length
);
2189 strbuf_addch(&name
, '{');
2191 strbuf_add(&name
, a
+ pfx_length
, a_midlen
);
2192 strbuf_addstr(&name
, " => ");
2193 strbuf_add(&name
, b
+ pfx_length
, b_midlen
);
2194 if (pfx_length
+ sfx_length
) {
2195 strbuf_addch(&name
, '}');
2196 strbuf_add(&name
, a
+ len_a
- sfx_length
, sfx_length
);
2198 return strbuf_detach(&name
, NULL
);
2204 struct diffstat_file
{
2208 unsigned is_unmerged
:1;
2209 unsigned is_binary
:1;
2210 unsigned is_renamed
:1;
2211 unsigned is_interesting
:1;
2212 uintmax_t added
, deleted
;
2216 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
2220 struct diffstat_file
*x
;
2221 x
= xcalloc(1, sizeof(*x
));
2222 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
2223 diffstat
->files
[diffstat
->nr
++] = x
;
2225 x
->from_name
= xstrdup(name_a
);
2226 x
->name
= xstrdup(name_b
);
2230 x
->from_name
= NULL
;
2231 x
->name
= xstrdup(name_a
);
2236 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
2238 struct diffstat_t
*diffstat
= priv
;
2239 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
2243 else if (line
[0] == '-')
2247 const char mime_boundary_leader
[] = "------------";
2249 static int scale_linear(int it
, int width
, int max_change
)
2254 * make sure that at least one '-' or '+' is printed if
2255 * there is any change to this path. The easiest way is to
2256 * scale linearly as if the alloted width is one column shorter
2257 * than it is, and then add 1 to the result.
2259 return 1 + (it
* (width
- 1) / max_change
);
2262 static void show_graph(struct strbuf
*out
, char ch
, int cnt
,
2263 const char *set
, const char *reset
)
2267 strbuf_addstr(out
, set
);
2268 strbuf_addchars(out
, ch
, cnt
);
2269 strbuf_addstr(out
, reset
);
2272 static void fill_print_name(struct diffstat_file
*file
)
2276 if (file
->print_name
)
2279 if (!file
->is_renamed
) {
2280 struct strbuf buf
= STRBUF_INIT
;
2281 if (quote_c_style(file
->name
, &buf
, NULL
, 0)) {
2282 pname
= strbuf_detach(&buf
, NULL
);
2285 strbuf_release(&buf
);
2288 pname
= pprint_rename(file
->from_name
, file
->name
);
2290 file
->print_name
= pname
;
2293 static void print_stat_summary_inserts_deletes(struct diff_options
*options
,
2294 int files
, int insertions
, int deletions
)
2296 struct strbuf sb
= STRBUF_INIT
;
2299 assert(insertions
== 0 && deletions
== 0);
2300 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
2306 (files
== 1) ? " %d file changed" : " %d files changed",
2310 * For binary diff, the caller may want to print "x files
2311 * changed" with insertions == 0 && deletions == 0.
2313 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2314 * is probably less confusing (i.e skip over "2 files changed
2315 * but nothing about added/removed lines? Is this a bug in Git?").
2317 if (insertions
|| deletions
== 0) {
2319 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2323 if (deletions
|| insertions
== 0) {
2325 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2328 strbuf_addch(&sb
, '\n');
2329 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
2331 strbuf_release(&sb
);
2334 void print_stat_summary(FILE *fp
, int files
,
2335 int insertions
, int deletions
)
2337 struct diff_options o
;
2338 memset(&o
, 0, sizeof(o
));
2341 print_stat_summary_inserts_deletes(&o
, files
, insertions
, deletions
);
2344 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
2346 int i
, len
, add
, del
, adds
= 0, dels
= 0;
2347 uintmax_t max_change
= 0, max_len
= 0;
2348 int total_files
= data
->nr
, count
;
2349 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
2350 const char *reset
, *add_c
, *del_c
;
2351 int extra_shown
= 0;
2352 const char *line_prefix
= diff_line_prefix(options
);
2353 struct strbuf out
= STRBUF_INIT
;
2358 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
2360 reset
= diff_get_color_opt(options
, DIFF_RESET
);
2361 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
2362 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
2365 * Find the longest filename and max number of changes
2367 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
2368 struct diffstat_file
*file
= data
->files
[i
];
2369 uintmax_t change
= file
->added
+ file
->deleted
;
2371 if (!file
->is_interesting
&& (change
== 0)) {
2372 count
++; /* not shown == room for one more */
2375 fill_print_name(file
);
2376 len
= strlen(file
->print_name
);
2380 if (file
->is_unmerged
) {
2381 /* "Unmerged" is 8 characters */
2382 bin_width
= bin_width
< 8 ? 8 : bin_width
;
2385 if (file
->is_binary
) {
2386 /* "Bin XXX -> YYY bytes" */
2387 int w
= 14 + decimal_width(file
->added
)
2388 + decimal_width(file
->deleted
);
2389 bin_width
= bin_width
< w
? w
: bin_width
;
2390 /* Display change counts aligned with "Bin" */
2395 if (max_change
< change
)
2396 max_change
= change
;
2398 count
= i
; /* where we can stop scanning in data->files[] */
2401 * We have width = stat_width or term_columns() columns total.
2402 * We want a maximum of min(max_len, stat_name_width) for the name part.
2403 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2404 * We also need 1 for " " and 4 + decimal_width(max_change)
2405 * for " | NNNN " and one the empty column at the end, altogether
2406 * 6 + decimal_width(max_change).
2408 * If there's not enough space, we will use the smaller of
2409 * stat_name_width (if set) and 5/8*width for the filename,
2410 * and the rest for constant elements + graph part, but no more
2411 * than stat_graph_width for the graph part.
2412 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2413 * for the standard terminal size).
2415 * In other words: stat_width limits the maximum width, and
2416 * stat_name_width fixes the maximum width of the filename,
2417 * and is also used to divide available columns if there
2420 * Binary files are displayed with "Bin XXX -> YYY bytes"
2421 * instead of the change count and graph. This part is treated
2422 * similarly to the graph part, except that it is not
2423 * "scaled". If total width is too small to accommodate the
2424 * guaranteed minimum width of the filename part and the
2425 * separators and this message, this message will "overflow"
2426 * making the line longer than the maximum width.
2429 if (options
->stat_width
== -1)
2430 width
= term_columns() - strlen(line_prefix
);
2432 width
= options
->stat_width
? options
->stat_width
: 80;
2433 number_width
= decimal_width(max_change
) > number_width
?
2434 decimal_width(max_change
) : number_width
;
2436 if (options
->stat_graph_width
== -1)
2437 options
->stat_graph_width
= diff_stat_graph_width
;
2440 * Guarantee 3/8*16==6 for the graph part
2441 * and 5/8*16==10 for the filename part
2443 if (width
< 16 + 6 + number_width
)
2444 width
= 16 + 6 + number_width
;
2447 * First assign sizes that are wanted, ignoring available width.
2448 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2449 * starting from "XXX" should fit in graph_width.
2451 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
2452 if (options
->stat_graph_width
&&
2453 options
->stat_graph_width
< graph_width
)
2454 graph_width
= options
->stat_graph_width
;
2456 name_width
= (options
->stat_name_width
> 0 &&
2457 options
->stat_name_width
< max_len
) ?
2458 options
->stat_name_width
: max_len
;
2461 * Adjust adjustable widths not to exceed maximum width
2463 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
2464 if (graph_width
> width
* 3/8 - number_width
- 6) {
2465 graph_width
= width
* 3/8 - number_width
- 6;
2466 if (graph_width
< 6)
2470 if (options
->stat_graph_width
&&
2471 graph_width
> options
->stat_graph_width
)
2472 graph_width
= options
->stat_graph_width
;
2473 if (name_width
> width
- number_width
- 6 - graph_width
)
2474 name_width
= width
- number_width
- 6 - graph_width
;
2476 graph_width
= width
- number_width
- 6 - name_width
;
2480 * From here name_width is the width of the name area,
2481 * and graph_width is the width of the graph area.
2482 * max_change is used to scale graph properly.
2484 for (i
= 0; i
< count
; i
++) {
2485 const char *prefix
= "";
2486 struct diffstat_file
*file
= data
->files
[i
];
2487 char *name
= file
->print_name
;
2488 uintmax_t added
= file
->added
;
2489 uintmax_t deleted
= file
->deleted
;
2492 if (!file
->is_interesting
&& (added
+ deleted
== 0))
2496 * "scale" the filename
2499 name_len
= strlen(name
);
2500 if (name_width
< name_len
) {
2504 name
+= name_len
- len
;
2505 slash
= strchr(name
, '/');
2510 if (file
->is_binary
) {
2511 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2512 strbuf_addf(&out
, " %*s", number_width
, "Bin");
2513 if (!added
&& !deleted
) {
2514 strbuf_addch(&out
, '\n');
2515 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2516 out
.buf
, out
.len
, 0);
2520 strbuf_addf(&out
, " %s%"PRIuMAX
"%s",
2521 del_c
, deleted
, reset
);
2522 strbuf_addstr(&out
, " -> ");
2523 strbuf_addf(&out
, "%s%"PRIuMAX
"%s",
2524 add_c
, added
, reset
);
2525 strbuf_addstr(&out
, " bytes\n");
2526 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2527 out
.buf
, out
.len
, 0);
2531 else if (file
->is_unmerged
) {
2532 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2533 strbuf_addstr(&out
, " Unmerged\n");
2534 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2535 out
.buf
, out
.len
, 0);
2541 * scale the add/delete
2546 if (graph_width
<= max_change
) {
2547 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
2548 if (total
< 2 && add
&& del
)
2549 /* width >= 2 due to the sanity check */
2552 add
= scale_linear(add
, graph_width
, max_change
);
2555 del
= scale_linear(del
, graph_width
, max_change
);
2559 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2560 strbuf_addf(&out
, " %*"PRIuMAX
"%s",
2561 number_width
, added
+ deleted
,
2562 added
+ deleted
? " " : "");
2563 show_graph(&out
, '+', add
, add_c
, reset
);
2564 show_graph(&out
, '-', del
, del_c
, reset
);
2565 strbuf_addch(&out
, '\n');
2566 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2567 out
.buf
, out
.len
, 0);
2571 for (i
= 0; i
< data
->nr
; i
++) {
2572 struct diffstat_file
*file
= data
->files
[i
];
2573 uintmax_t added
= file
->added
;
2574 uintmax_t deleted
= file
->deleted
;
2576 if (file
->is_unmerged
||
2577 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
2582 if (!file
->is_binary
) {
2589 emit_diff_symbol(options
,
2590 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
2595 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2598 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
2600 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
2605 for (i
= 0; i
< data
->nr
; i
++) {
2606 int added
= data
->files
[i
]->added
;
2607 int deleted
= data
->files
[i
]->deleted
;
2609 if (data
->files
[i
]->is_unmerged
||
2610 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
2612 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
2617 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2620 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
2627 for (i
= 0; i
< data
->nr
; i
++) {
2628 struct diffstat_file
*file
= data
->files
[i
];
2630 fprintf(options
->file
, "%s", diff_line_prefix(options
));
2632 if (file
->is_binary
)
2633 fprintf(options
->file
, "-\t-\t");
2635 fprintf(options
->file
,
2636 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
2637 file
->added
, file
->deleted
);
2638 if (options
->line_termination
) {
2639 fill_print_name(file
);
2640 if (!file
->is_renamed
)
2641 write_name_quoted(file
->name
, options
->file
,
2642 options
->line_termination
);
2644 fputs(file
->print_name
, options
->file
);
2645 putc(options
->line_termination
, options
->file
);
2648 if (file
->is_renamed
) {
2649 putc('\0', options
->file
);
2650 write_name_quoted(file
->from_name
, options
->file
, '\0');
2652 write_name_quoted(file
->name
, options
->file
, '\0');
2657 struct dirstat_file
{
2659 unsigned long changed
;
2662 struct dirstat_dir
{
2663 struct dirstat_file
*files
;
2664 int alloc
, nr
, permille
, cumulative
;
2667 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
2668 unsigned long changed
, const char *base
, int baselen
)
2670 unsigned long this_dir
= 0;
2671 unsigned int sources
= 0;
2672 const char *line_prefix
= diff_line_prefix(opt
);
2675 struct dirstat_file
*f
= dir
->files
;
2676 int namelen
= strlen(f
->name
);
2680 if (namelen
< baselen
)
2682 if (memcmp(f
->name
, base
, baselen
))
2684 slash
= strchr(f
->name
+ baselen
, '/');
2686 int newbaselen
= slash
+ 1 - f
->name
;
2687 this = gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
2699 * We don't report dirstat's for
2701 * - or cases where everything came from a single directory
2702 * under this directory (sources == 1).
2704 if (baselen
&& sources
!= 1) {
2706 int permille
= this_dir
* 1000 / changed
;
2707 if (permille
>= dir
->permille
) {
2708 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
2709 permille
/ 10, permille
% 10, baselen
, base
);
2710 if (!dir
->cumulative
)
2718 static int dirstat_compare(const void *_a
, const void *_b
)
2720 const struct dirstat_file
*a
= _a
;
2721 const struct dirstat_file
*b
= _b
;
2722 return strcmp(a
->name
, b
->name
);
2725 static void show_dirstat(struct diff_options
*options
)
2728 unsigned long changed
;
2729 struct dirstat_dir dir
;
2730 struct diff_queue_struct
*q
= &diff_queued_diff
;
2735 dir
.permille
= options
->dirstat_permille
;
2736 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
2739 for (i
= 0; i
< q
->nr
; i
++) {
2740 struct diff_filepair
*p
= q
->queue
[i
];
2742 unsigned long copied
, added
, damage
;
2743 int content_changed
;
2745 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
2747 if (p
->one
->oid_valid
&& p
->two
->oid_valid
)
2748 content_changed
= oidcmp(&p
->one
->oid
, &p
->two
->oid
);
2750 content_changed
= 1;
2752 if (!content_changed
) {
2754 * The SHA1 has not changed, so pre-/post-content is
2755 * identical. We can therefore skip looking at the
2756 * file contents altogether.
2762 if (DIFF_OPT_TST(options
, DIRSTAT_BY_FILE
)) {
2764 * In --dirstat-by-file mode, we don't really need to
2765 * look at the actual file contents at all.
2766 * The fact that the SHA1 changed is enough for us to
2767 * add this file to the list of results
2768 * (with each file contributing equal damage).
2774 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
2775 diff_populate_filespec(p
->one
, 0);
2776 diff_populate_filespec(p
->two
, 0);
2777 diffcore_count_changes(p
->one
, p
->two
, NULL
, NULL
,
2779 diff_free_filespec_data(p
->one
);
2780 diff_free_filespec_data(p
->two
);
2781 } else if (DIFF_FILE_VALID(p
->one
)) {
2782 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
);
2784 diff_free_filespec_data(p
->one
);
2785 } else if (DIFF_FILE_VALID(p
->two
)) {
2786 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
);
2788 added
= p
->two
->size
;
2789 diff_free_filespec_data(p
->two
);
2794 * Original minus copied is the removed material,
2795 * added is the new material. They are both damages
2796 * made to the preimage.
2797 * If the resulting damage is zero, we know that
2798 * diffcore_count_changes() considers the two entries to
2799 * be identical, but since content_changed is true, we
2800 * know that there must have been _some_ kind of change,
2801 * so we force all entries to have damage > 0.
2803 damage
= (p
->one
->size
- copied
) + added
;
2808 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2809 dir
.files
[dir
.nr
].name
= name
;
2810 dir
.files
[dir
.nr
].changed
= damage
;
2815 /* This can happen even with many files, if everything was renames */
2819 /* Show all directories with more than x% of the changes */
2820 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
2821 gather_dirstat(options
, &dir
, changed
, "", 0);
2824 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
2827 unsigned long changed
;
2828 struct dirstat_dir dir
;
2836 dir
.permille
= options
->dirstat_permille
;
2837 dir
.cumulative
= DIFF_OPT_TST(options
, DIRSTAT_CUMULATIVE
);
2840 for (i
= 0; i
< data
->nr
; i
++) {
2841 struct diffstat_file
*file
= data
->files
[i
];
2842 unsigned long damage
= file
->added
+ file
->deleted
;
2843 if (file
->is_binary
)
2845 * binary files counts bytes, not lines. Must find some
2846 * way to normalize binary bytes vs. textual lines.
2847 * The following heuristic assumes that there are 64
2849 * This is stupid and ugly, but very cheap...
2851 damage
= (damage
+ 63) / 64;
2852 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
2853 dir
.files
[dir
.nr
].name
= file
->name
;
2854 dir
.files
[dir
.nr
].changed
= damage
;
2859 /* This can happen even with many files, if everything was renames */
2863 /* Show all directories with more than x% of the changes */
2864 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
2865 gather_dirstat(options
, &dir
, changed
, "", 0);
2868 static void free_diffstat_info(struct diffstat_t
*diffstat
)
2871 for (i
= 0; i
< diffstat
->nr
; i
++) {
2872 struct diffstat_file
*f
= diffstat
->files
[i
];
2873 if (f
->name
!= f
->print_name
)
2874 free(f
->print_name
);
2879 free(diffstat
->files
);
2882 struct checkdiff_t
{
2883 const char *filename
;
2885 int conflict_marker_size
;
2886 struct diff_options
*o
;
2891 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
2896 if (len
< marker_size
+ 1)
2898 firstchar
= line
[0];
2899 switch (firstchar
) {
2900 case '=': case '>': case '<': case '|':
2905 for (cnt
= 1; cnt
< marker_size
; cnt
++)
2906 if (line
[cnt
] != firstchar
)
2908 /* line[1] thru line[marker_size-1] are same as firstchar */
2909 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
2914 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
2916 struct checkdiff_t
*data
= priv
;
2917 int marker_size
= data
->conflict_marker_size
;
2918 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
2919 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
2920 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
2922 const char *line_prefix
;
2925 line_prefix
= diff_line_prefix(data
->o
);
2927 if (line
[0] == '+') {
2930 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
2932 fprintf(data
->o
->file
,
2933 "%s%s:%d: leftover conflict marker\n",
2934 line_prefix
, data
->filename
, data
->lineno
);
2936 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
2939 data
->status
|= bad
;
2940 err
= whitespace_error_string(bad
);
2941 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
2942 line_prefix
, data
->filename
, data
->lineno
, err
);
2944 emit_line(data
->o
, set
, reset
, line
, 1);
2945 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
2946 data
->o
->file
, set
, reset
, ws
);
2947 } else if (line
[0] == ' ') {
2949 } else if (line
[0] == '@') {
2950 char *plus
= strchr(line
, '+');
2952 data
->lineno
= strtol(plus
, NULL
, 10) - 1;
2954 die("invalid diff");
2958 static unsigned char *deflate_it(char *data
,
2960 unsigned long *result_size
)
2963 unsigned char *deflated
;
2966 git_deflate_init(&stream
, zlib_compression_level
);
2967 bound
= git_deflate_bound(&stream
, size
);
2968 deflated
= xmalloc(bound
);
2969 stream
.next_out
= deflated
;
2970 stream
.avail_out
= bound
;
2972 stream
.next_in
= (unsigned char *)data
;
2973 stream
.avail_in
= size
;
2974 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
2976 git_deflate_end(&stream
);
2977 *result_size
= stream
.total_out
;
2981 static void emit_binary_diff_body(struct diff_options
*o
,
2982 mmfile_t
*one
, mmfile_t
*two
)
2988 unsigned long orig_size
;
2989 unsigned long delta_size
;
2990 unsigned long deflate_size
;
2991 unsigned long data_size
;
2993 /* We could do deflated delta, or we could do just deflated two,
2994 * whichever is smaller.
2997 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
2998 if (one
->size
&& two
->size
) {
2999 delta
= diff_delta(one
->ptr
, one
->size
,
3000 two
->ptr
, two
->size
,
3001 &delta_size
, deflate_size
);
3003 void *to_free
= delta
;
3004 orig_size
= delta_size
;
3005 delta
= deflate_it(delta
, delta_size
, &delta_size
);
3010 if (delta
&& delta_size
< deflate_size
) {
3011 char *s
= xstrfmt("%lu", orig_size
);
3012 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
3017 data_size
= delta_size
;
3019 char *s
= xstrfmt("%lu", two
->size
);
3020 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
3025 data_size
= deflate_size
;
3028 /* emit data encoded in base85 */
3032 int bytes
= (52 < data_size
) ? 52 : data_size
;
3036 line
[0] = bytes
+ 'A' - 1;
3038 line
[0] = bytes
- 26 + 'a' - 1;
3039 encode_85(line
+ 1, cp
, bytes
);
3040 cp
= (char *) cp
+ bytes
;
3046 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_BODY
,
3049 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_FOOTER
, NULL
, 0, 0);
3053 static void emit_binary_diff(struct diff_options
*o
,
3054 mmfile_t
*one
, mmfile_t
*two
)
3056 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER
, NULL
, 0, 0);
3057 emit_binary_diff_body(o
, one
, two
);
3058 emit_binary_diff_body(o
, two
, one
);
3061 int diff_filespec_is_binary(struct diff_filespec
*one
)
3063 if (one
->is_binary
== -1) {
3064 diff_filespec_load_driver(one
);
3065 if (one
->driver
->binary
!= -1)
3066 one
->is_binary
= one
->driver
->binary
;
3068 if (!one
->data
&& DIFF_FILE_VALID(one
))
3069 diff_populate_filespec(one
, CHECK_BINARY
);
3070 if (one
->is_binary
== -1 && one
->data
)
3071 one
->is_binary
= buffer_is_binary(one
->data
,
3073 if (one
->is_binary
== -1)
3077 return one
->is_binary
;
3080 static const struct userdiff_funcname
*diff_funcname_pattern(struct diff_filespec
*one
)
3082 diff_filespec_load_driver(one
);
3083 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
3086 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
3088 if (!options
->a_prefix
)
3089 options
->a_prefix
= a
;
3090 if (!options
->b_prefix
)
3091 options
->b_prefix
= b
;
3094 struct userdiff_driver
*get_textconv(struct diff_filespec
*one
)
3096 if (!DIFF_FILE_VALID(one
))
3099 diff_filespec_load_driver(one
);
3100 return userdiff_get_textconv(one
->driver
);
3103 static void builtin_diff(const char *name_a
,
3105 struct diff_filespec
*one
,
3106 struct diff_filespec
*two
,
3107 const char *xfrm_msg
,
3108 int must_show_header
,
3109 struct diff_options
*o
,
3110 int complete_rewrite
)
3114 char *a_one
, *b_two
;
3115 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
3116 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
3117 const char *a_prefix
, *b_prefix
;
3118 struct userdiff_driver
*textconv_one
= NULL
;
3119 struct userdiff_driver
*textconv_two
= NULL
;
3120 struct strbuf header
= STRBUF_INIT
;
3121 const char *line_prefix
= diff_line_prefix(o
);
3123 diff_set_mnemonic_prefix(o
, "a/", "b/");
3124 if (DIFF_OPT_TST(o
, REVERSE_DIFF
)) {
3125 a_prefix
= o
->b_prefix
;
3126 b_prefix
= o
->a_prefix
;
3128 a_prefix
= o
->a_prefix
;
3129 b_prefix
= o
->b_prefix
;
3132 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
3133 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3134 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
3135 show_submodule_summary(o
, one
->path
? one
->path
: two
->path
,
3136 &one
->oid
, &two
->oid
,
3137 two
->dirty_submodule
);
3139 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
3140 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3141 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
3142 show_submodule_inline_diff(o
, one
->path
? one
->path
: two
->path
,
3143 &one
->oid
, &two
->oid
,
3144 two
->dirty_submodule
);
3148 if (DIFF_OPT_TST(o
, ALLOW_TEXTCONV
)) {
3149 textconv_one
= get_textconv(one
);
3150 textconv_two
= get_textconv(two
);
3153 /* Never use a non-valid filename anywhere if at all possible */
3154 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
3155 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
3157 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
3158 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
3159 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
3160 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
3161 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3162 if (lbl
[0][0] == '/') {
3164 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3166 strbuf_addstr(&header
, xfrm_msg
);
3167 must_show_header
= 1;
3169 else if (lbl
[1][0] == '/') {
3170 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3172 strbuf_addstr(&header
, xfrm_msg
);
3173 must_show_header
= 1;
3176 if (one
->mode
!= two
->mode
) {
3177 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3178 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3179 must_show_header
= 1;
3182 strbuf_addstr(&header
, xfrm_msg
);
3185 * we do not run diff between different kind
3188 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3189 goto free_ab_and_return
;
3190 if (complete_rewrite
&&
3191 (textconv_one
|| !diff_filespec_is_binary(one
)) &&
3192 (textconv_two
|| !diff_filespec_is_binary(two
))) {
3193 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3194 header
.buf
, header
.len
, 0);
3195 strbuf_reset(&header
);
3196 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3197 textconv_one
, textconv_two
, o
);
3198 o
->found_changes
= 1;
3199 goto free_ab_and_return
;
3203 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3204 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3206 strbuf_reset(&header
);
3207 goto free_ab_and_return
;
3208 } else if (!DIFF_OPT_TST(o
, TEXT
) &&
3209 ( (!textconv_one
&& diff_filespec_is_binary(one
)) ||
3210 (!textconv_two
&& diff_filespec_is_binary(two
)) )) {
3211 struct strbuf sb
= STRBUF_INIT
;
3212 if (!one
->data
&& !two
->data
&&
3213 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3214 !DIFF_OPT_TST(o
, BINARY
)) {
3215 if (!oidcmp(&one
->oid
, &two
->oid
)) {
3216 if (must_show_header
)
3217 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3218 header
.buf
, header
.len
,
3220 goto free_ab_and_return
;
3222 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3223 header
.buf
, header
.len
, 0);
3224 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3225 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3226 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3228 strbuf_release(&sb
);
3229 goto free_ab_and_return
;
3231 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
3232 die("unable to read files to diff");
3233 /* Quite common confusing case */
3234 if (mf1
.size
== mf2
.size
&&
3235 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3236 if (must_show_header
)
3237 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3238 header
.buf
, header
.len
, 0);
3239 goto free_ab_and_return
;
3241 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3242 strbuf_reset(&header
);
3243 if (DIFF_OPT_TST(o
, BINARY
))
3244 emit_binary_diff(o
, &mf1
, &mf2
);
3246 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3247 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3248 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3250 strbuf_release(&sb
);
3252 o
->found_changes
= 1;
3254 /* Crazy xdl interfaces.. */
3255 const char *diffopts
= getenv("GIT_DIFF_OPTS");
3259 struct emit_callback ecbdata
;
3260 const struct userdiff_funcname
*pe
;
3262 if (must_show_header
) {
3263 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3264 header
.buf
, header
.len
, 0);
3265 strbuf_reset(&header
);
3268 mf1
.size
= fill_textconv(textconv_one
, one
, &mf1
.ptr
);
3269 mf2
.size
= fill_textconv(textconv_two
, two
, &mf2
.ptr
);
3271 pe
= diff_funcname_pattern(one
);
3273 pe
= diff_funcname_pattern(two
);
3275 memset(&xpp
, 0, sizeof(xpp
));
3276 memset(&xecfg
, 0, sizeof(xecfg
));
3277 memset(&ecbdata
, 0, sizeof(ecbdata
));
3278 ecbdata
.label_path
= lbl
;
3279 ecbdata
.color_diff
= want_color(o
->use_color
);
3280 ecbdata
.ws_rule
= whitespace_rule(name_b
);
3281 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3282 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3284 ecbdata
.header
= header
.len
? &header
: NULL
;
3285 xpp
.flags
= o
->xdl_opts
;
3286 xecfg
.ctxlen
= o
->context
;
3287 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3288 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3289 if (DIFF_OPT_TST(o
, FUNCCONTEXT
))
3290 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3292 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3295 else if (skip_prefix(diffopts
, "--unified=", &v
))
3296 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3297 else if (skip_prefix(diffopts
, "-u", &v
))
3298 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3300 init_diff_words_data(&ecbdata
, o
, one
, two
);
3301 if (xdi_diff_outf(&mf1
, &mf2
, fn_out_consume
, &ecbdata
,
3303 die("unable to generate diff for %s", one
->path
);
3305 free_diff_words_data(&ecbdata
);
3310 xdiff_clear_find_func(&xecfg
);
3314 strbuf_release(&header
);
3315 diff_free_filespec_data(one
);
3316 diff_free_filespec_data(two
);
3322 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3323 struct diff_filespec
*one
,
3324 struct diff_filespec
*two
,
3325 struct diffstat_t
*diffstat
,
3326 struct diff_options
*o
,
3327 struct diff_filepair
*p
)
3330 struct diffstat_file
*data
;
3332 int complete_rewrite
= 0;
3334 if (!DIFF_PAIR_UNMERGED(p
)) {
3335 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3336 complete_rewrite
= 1;
3339 data
= diffstat_add(diffstat
, name_a
, name_b
);
3340 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3343 data
->is_unmerged
= 1;
3347 same_contents
= !oidcmp(&one
->oid
, &two
->oid
);
3349 if (diff_filespec_is_binary(one
) || diff_filespec_is_binary(two
)) {
3350 data
->is_binary
= 1;
3351 if (same_contents
) {
3355 data
->added
= diff_filespec_size(two
);
3356 data
->deleted
= diff_filespec_size(one
);
3360 else if (complete_rewrite
) {
3361 diff_populate_filespec(one
, 0);
3362 diff_populate_filespec(two
, 0);
3363 data
->deleted
= count_lines(one
->data
, one
->size
);
3364 data
->added
= count_lines(two
->data
, two
->size
);
3367 else if (!same_contents
) {
3368 /* Crazy xdl interfaces.. */
3372 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
3373 die("unable to read files to diff");
3375 memset(&xpp
, 0, sizeof(xpp
));
3376 memset(&xecfg
, 0, sizeof(xecfg
));
3377 xpp
.flags
= o
->xdl_opts
;
3378 xecfg
.ctxlen
= o
->context
;
3379 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3380 if (xdi_diff_outf(&mf1
, &mf2
, diffstat_consume
, diffstat
,
3382 die("unable to generate diffstat for %s", one
->path
);
3385 diff_free_filespec_data(one
);
3386 diff_free_filespec_data(two
);
3389 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3390 const char *attr_path
,
3391 struct diff_filespec
*one
,
3392 struct diff_filespec
*two
,
3393 struct diff_options
*o
)
3396 struct checkdiff_t data
;
3401 memset(&data
, 0, sizeof(data
));
3402 data
.filename
= name_b
? name_b
: name_a
;
3405 data
.ws_rule
= whitespace_rule(attr_path
);
3406 data
.conflict_marker_size
= ll_merge_marker_size(attr_path
);
3408 if (fill_mmfile(&mf1
, one
) < 0 || fill_mmfile(&mf2
, two
) < 0)
3409 die("unable to read files to diff");
3412 * All the other codepaths check both sides, but not checking
3413 * the "old" side here is deliberate. We are checking the newly
3414 * introduced changes, and as long as the "new" side is text, we
3415 * can and should check what it introduces.
3417 if (diff_filespec_is_binary(two
))
3418 goto free_and_return
;
3420 /* Crazy xdl interfaces.. */
3424 memset(&xpp
, 0, sizeof(xpp
));
3425 memset(&xecfg
, 0, sizeof(xecfg
));
3426 xecfg
.ctxlen
= 1; /* at least one context line */
3428 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume
, &data
,
3430 die("unable to generate checkdiff for %s", one
->path
);
3432 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3433 struct emit_callback ecbdata
;
3436 ecbdata
.ws_rule
= data
.ws_rule
;
3437 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3438 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3443 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3444 fprintf(o
->file
, "%s:%d: %s.\n",
3445 data
.filename
, blank_at_eof
, err
);
3446 data
.status
= 1; /* report errors */
3451 diff_free_filespec_data(one
);
3452 diff_free_filespec_data(two
);
3454 DIFF_OPT_SET(o
, CHECK_FAILED
);
3457 struct diff_filespec
*alloc_filespec(const char *path
)
3459 struct diff_filespec
*spec
;
3461 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3463 spec
->is_binary
= -1;
3467 void free_filespec(struct diff_filespec
*spec
)
3469 if (!--spec
->count
) {
3470 diff_free_filespec_data(spec
);
3475 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3476 int oid_valid
, unsigned short mode
)
3479 spec
->mode
= canon_mode(mode
);
3480 oidcpy(&spec
->oid
, oid
);
3481 spec
->oid_valid
= oid_valid
;
3486 * Given a name and sha1 pair, if the index tells us the file in
3487 * the work tree has that object contents, return true, so that
3488 * prepare_temp_file() does not have to inflate and extract.
3490 static int reuse_worktree_file(const char *name
, const struct object_id
*oid
, int want_file
)
3492 const struct cache_entry
*ce
;
3497 * We do not read the cache ourselves here, because the
3498 * benchmark with my previous version that always reads cache
3499 * shows that it makes things worse for diff-tree comparing
3500 * two linux-2.6 kernel trees in an already checked out work
3501 * tree. This is because most diff-tree comparisons deal with
3502 * only a small number of files, while reading the cache is
3503 * expensive for a large project, and its cost outweighs the
3504 * savings we get by not inflating the object to a temporary
3505 * file. Practically, this code only helps when we are used
3506 * by diff-cache --cached, which does read the cache before
3512 /* We want to avoid the working directory if our caller
3513 * doesn't need the data in a normal file, this system
3514 * is rather slow with its stat/open/mmap/close syscalls,
3515 * and the object is contained in a pack file. The pack
3516 * is probably already open and will be faster to obtain
3517 * the data through than the working directory. Loose
3518 * objects however would tend to be slower as they need
3519 * to be individually opened and inflated.
3521 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_sha1_pack(oid
->hash
))
3525 * Similarly, if we'd have to convert the file contents anyway, that
3526 * makes the optimization not worthwhile.
3528 if (!want_file
&& would_convert_to_git(&the_index
, name
))
3532 pos
= cache_name_pos(name
, len
);
3535 ce
= active_cache
[pos
];
3538 * This is not the sha1 we are looking for, or
3539 * unreusable because it is not a regular file.
3541 if (oidcmp(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
3545 * If ce is marked as "assume unchanged", there is no
3546 * guarantee that work tree matches what we are looking for.
3548 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
3552 * If ce matches the file in the work tree, we can reuse it.
3554 if (ce_uptodate(ce
) ||
3555 (!lstat(name
, &st
) && !ce_match_stat(ce
, &st
, 0)))
3561 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
3563 struct strbuf buf
= STRBUF_INIT
;
3566 /* Are we looking at the work tree? */
3567 if (s
->dirty_submodule
)
3570 strbuf_addf(&buf
, "Subproject commit %s%s\n",
3571 oid_to_hex(&s
->oid
), dirty
);
3575 strbuf_release(&buf
);
3577 s
->data
= strbuf_detach(&buf
, NULL
);
3584 * While doing rename detection and pickaxe operation, we may need to
3585 * grab the data for the blob (or file) for our own in-core comparison.
3586 * diff_filespec has data and size fields for this purpose.
3588 int diff_populate_filespec(struct diff_filespec
*s
, unsigned int flags
)
3590 int size_only
= flags
& CHECK_SIZE_ONLY
;
3593 * demote FAIL to WARN to allow inspecting the situation
3594 * instead of refusing.
3596 enum safe_crlf crlf_warn
= (safe_crlf
== SAFE_CRLF_FAIL
3600 if (!DIFF_FILE_VALID(s
))
3601 die("internal error: asking to populate invalid file.");
3602 if (S_ISDIR(s
->mode
))
3608 if (size_only
&& 0 < s
->size
)
3611 if (S_ISGITLINK(s
->mode
))
3612 return diff_populate_gitlink(s
, size_only
);
3614 if (!s
->oid_valid
||
3615 reuse_worktree_file(s
->path
, &s
->oid
, 0)) {
3616 struct strbuf buf
= STRBUF_INIT
;
3620 if (lstat(s
->path
, &st
) < 0) {
3621 if (errno
== ENOENT
) {
3625 s
->data
= (char *)"";
3630 s
->size
= xsize_t(st
.st_size
);
3633 if (S_ISLNK(st
.st_mode
)) {
3634 struct strbuf sb
= STRBUF_INIT
;
3636 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
3639 s
->data
= strbuf_detach(&sb
, NULL
);
3645 * Even if the caller would be happy with getting
3646 * only the size, we cannot return early at this
3647 * point if the path requires us to run the content
3650 if (size_only
&& !would_convert_to_git(&the_index
, s
->path
))
3654 * Note: this check uses xsize_t(st.st_size) that may
3655 * not be the true size of the blob after it goes
3656 * through convert_to_git(). This may not strictly be
3657 * correct, but the whole point of big_file_threshold
3658 * and is_binary check being that we want to avoid
3659 * opening the file and inspecting the contents, this
3662 if ((flags
& CHECK_BINARY
) &&
3663 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
3667 fd
= open(s
->path
, O_RDONLY
);
3670 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
3672 s
->should_munmap
= 1;
3675 * Convert from working tree format to canonical git format
3677 if (convert_to_git(&the_index
, s
->path
, s
->data
, s
->size
, &buf
, crlf_warn
)) {
3679 munmap(s
->data
, s
->size
);
3680 s
->should_munmap
= 0;
3681 s
->data
= strbuf_detach(&buf
, &size
);
3687 enum object_type type
;
3688 if (size_only
|| (flags
& CHECK_BINARY
)) {
3689 type
= sha1_object_info(s
->oid
.hash
, &s
->size
);
3691 die("unable to read %s",
3692 oid_to_hex(&s
->oid
));
3695 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
3700 s
->data
= read_sha1_file(s
->oid
.hash
, &type
, &s
->size
);
3702 die("unable to read %s", oid_to_hex(&s
->oid
));
3708 void diff_free_filespec_blob(struct diff_filespec
*s
)
3712 else if (s
->should_munmap
)
3713 munmap(s
->data
, s
->size
);
3715 if (s
->should_free
|| s
->should_munmap
) {
3716 s
->should_free
= s
->should_munmap
= 0;
3721 void diff_free_filespec_data(struct diff_filespec
*s
)
3723 diff_free_filespec_blob(s
);
3724 FREE_AND_NULL(s
->cnt_data
);
3727 static void prep_temp_blob(const char *path
, struct diff_tempfile
*temp
,
3730 const struct object_id
*oid
,
3734 struct strbuf buf
= STRBUF_INIT
;
3735 struct strbuf
template = STRBUF_INIT
;
3736 char *path_dup
= xstrdup(path
);
3737 const char *base
= basename(path_dup
);
3739 /* Generate "XXXXXX_basename.ext" */
3740 strbuf_addstr(&template, "XXXXXX_");
3741 strbuf_addstr(&template, base
);
3743 fd
= mks_tempfile_ts(&temp
->tempfile
, template.buf
, strlen(base
) + 1);
3745 die_errno("unable to create temp-file");
3746 if (convert_to_working_tree(path
,
3747 (const char *)blob
, (size_t)size
, &buf
)) {
3751 if (write_in_full(fd
, blob
, size
) != size
)
3752 die_errno("unable to write temp-file");
3753 close_tempfile(&temp
->tempfile
);
3754 temp
->name
= get_tempfile_path(&temp
->tempfile
);
3755 oid_to_hex_r(temp
->hex
, oid
);
3756 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
3757 strbuf_release(&buf
);
3758 strbuf_release(&template);
3762 static struct diff_tempfile
*prepare_temp_file(const char *name
,
3763 struct diff_filespec
*one
)
3765 struct diff_tempfile
*temp
= claim_diff_tempfile();
3767 if (!DIFF_FILE_VALID(one
)) {
3769 /* A '-' entry produces this for file-2, and
3770 * a '+' entry produces this for file-1.
3772 temp
->name
= "/dev/null";
3773 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
3774 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
3778 if (!S_ISGITLINK(one
->mode
) &&
3780 reuse_worktree_file(name
, &one
->oid
, 1))) {
3782 if (lstat(name
, &st
) < 0) {
3783 if (errno
== ENOENT
)
3784 goto not_a_valid_file
;
3785 die_errno("stat(%s)", name
);
3787 if (S_ISLNK(st
.st_mode
)) {
3788 struct strbuf sb
= STRBUF_INIT
;
3789 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
3790 die_errno("readlink(%s)", name
);
3791 prep_temp_blob(name
, temp
, sb
.buf
, sb
.len
,
3793 &one
->oid
: &null_oid
),
3795 one
->mode
: S_IFLNK
));
3796 strbuf_release(&sb
);
3799 /* we can borrow from the file in the work tree */
3801 if (!one
->oid_valid
)
3802 oid_to_hex_r(temp
->hex
, &null_oid
);
3804 oid_to_hex_r(temp
->hex
, &one
->oid
);
3805 /* Even though we may sometimes borrow the
3806 * contents from the work tree, we always want
3807 * one->mode. mode is trustworthy even when
3808 * !(one->oid_valid), as long as
3809 * DIFF_FILE_VALID(one).
3811 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
3816 if (diff_populate_filespec(one
, 0))
3817 die("cannot read data blob for %s", one
->path
);
3818 prep_temp_blob(name
, temp
, one
->data
, one
->size
,
3819 &one
->oid
, one
->mode
);
3824 static void add_external_diff_name(struct argv_array
*argv
,
3826 struct diff_filespec
*df
)
3828 struct diff_tempfile
*temp
= prepare_temp_file(name
, df
);
3829 argv_array_push(argv
, temp
->name
);
3830 argv_array_push(argv
, temp
->hex
);
3831 argv_array_push(argv
, temp
->mode
);
3834 /* An external diff command takes:
3836 * diff-cmd name infile1 infile1-sha1 infile1-mode \
3837 * infile2 infile2-sha1 infile2-mode [ rename-to ]
3840 static void run_external_diff(const char *pgm
,
3843 struct diff_filespec
*one
,
3844 struct diff_filespec
*two
,
3845 const char *xfrm_msg
,
3846 int complete_rewrite
,
3847 struct diff_options
*o
)
3849 struct argv_array argv
= ARGV_ARRAY_INIT
;
3850 struct argv_array env
= ARGV_ARRAY_INIT
;
3851 struct diff_queue_struct
*q
= &diff_queued_diff
;
3853 argv_array_push(&argv
, pgm
);
3854 argv_array_push(&argv
, name
);
3857 add_external_diff_name(&argv
, name
, one
);
3859 add_external_diff_name(&argv
, name
, two
);
3861 add_external_diff_name(&argv
, other
, two
);
3862 argv_array_push(&argv
, other
);
3863 argv_array_push(&argv
, xfrm_msg
);
3867 argv_array_pushf(&env
, "GIT_DIFF_PATH_COUNTER=%d", ++o
->diff_path_counter
);
3868 argv_array_pushf(&env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
3870 if (run_command_v_opt_cd_env(argv
.argv
, RUN_USING_SHELL
, NULL
, env
.argv
))
3871 die(_("external diff died, stopping at %s"), name
);
3874 argv_array_clear(&argv
);
3875 argv_array_clear(&env
);
3878 static int similarity_index(struct diff_filepair
*p
)
3880 return p
->score
* 100 / MAX_SCORE
;
3883 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
3885 if (startup_info
->have_repository
)
3886 return find_unique_abbrev(oid
->hash
, abbrev
);
3888 char *hex
= oid_to_hex(oid
);
3890 abbrev
= FALLBACK_DEFAULT_ABBREV
;
3891 if (abbrev
> GIT_SHA1_HEXSZ
)
3892 die("BUG: oid abbreviation out of range: %d", abbrev
);
3899 static void fill_metainfo(struct strbuf
*msg
,
3902 struct diff_filespec
*one
,
3903 struct diff_filespec
*two
,
3904 struct diff_options
*o
,
3905 struct diff_filepair
*p
,
3906 int *must_show_header
,
3909 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
3910 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
3911 const char *line_prefix
= diff_line_prefix(o
);
3913 *must_show_header
= 1;
3914 strbuf_init(msg
, PATH_MAX
* 2 + 300);
3915 switch (p
->status
) {
3916 case DIFF_STATUS_COPIED
:
3917 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
3918 line_prefix
, set
, similarity_index(p
));
3919 strbuf_addf(msg
, "%s\n%s%scopy from ",
3920 reset
, line_prefix
, set
);
3921 quote_c_style(name
, msg
, NULL
, 0);
3922 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
3923 quote_c_style(other
, msg
, NULL
, 0);
3924 strbuf_addf(msg
, "%s\n", reset
);
3926 case DIFF_STATUS_RENAMED
:
3927 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
3928 line_prefix
, set
, similarity_index(p
));
3929 strbuf_addf(msg
, "%s\n%s%srename from ",
3930 reset
, line_prefix
, set
);
3931 quote_c_style(name
, msg
, NULL
, 0);
3932 strbuf_addf(msg
, "%s\n%s%srename to ",
3933 reset
, line_prefix
, set
);
3934 quote_c_style(other
, msg
, NULL
, 0);
3935 strbuf_addf(msg
, "%s\n", reset
);
3937 case DIFF_STATUS_MODIFIED
:
3939 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
3941 set
, similarity_index(p
), reset
);
3946 *must_show_header
= 0;
3948 if (one
&& two
&& oidcmp(&one
->oid
, &two
->oid
)) {
3949 int abbrev
= DIFF_OPT_TST(o
, FULL_INDEX
) ? 40 : DEFAULT_ABBREV
;
3951 if (DIFF_OPT_TST(o
, BINARY
)) {
3953 if ((!fill_mmfile(&mf
, one
) && diff_filespec_is_binary(one
)) ||
3954 (!fill_mmfile(&mf
, two
) && diff_filespec_is_binary(two
)))
3957 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
3958 diff_abbrev_oid(&one
->oid
, abbrev
),
3959 diff_abbrev_oid(&two
->oid
, abbrev
));
3960 if (one
->mode
== two
->mode
)
3961 strbuf_addf(msg
, " %06o", one
->mode
);
3962 strbuf_addf(msg
, "%s\n", reset
);
3966 static void run_diff_cmd(const char *pgm
,
3969 const char *attr_path
,
3970 struct diff_filespec
*one
,
3971 struct diff_filespec
*two
,
3973 struct diff_options
*o
,
3974 struct diff_filepair
*p
)
3976 const char *xfrm_msg
= NULL
;
3977 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
3978 int must_show_header
= 0;
3981 if (DIFF_OPT_TST(o
, ALLOW_EXTERNAL
)) {
3982 struct userdiff_driver
*drv
= userdiff_find_by_path(attr_path
);
3983 if (drv
&& drv
->external
)
3984 pgm
= drv
->external
;
3989 * don't use colors when the header is intended for an
3990 * external diff driver
3992 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
3994 want_color(o
->use_color
) && !pgm
);
3995 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
3999 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
,
4000 complete_rewrite
, o
);
4004 builtin_diff(name
, other
? other
: name
,
4005 one
, two
, xfrm_msg
, must_show_header
,
4006 o
, complete_rewrite
);
4008 fprintf(o
->file
, "* Unmerged path %s\n", name
);
4011 static void diff_fill_oid_info(struct diff_filespec
*one
)
4013 if (DIFF_FILE_VALID(one
)) {
4014 if (!one
->oid_valid
) {
4016 if (one
->is_stdin
) {
4020 if (lstat(one
->path
, &st
) < 0)
4021 die_errno("stat '%s'", one
->path
);
4022 if (index_path(one
->oid
.hash
, one
->path
, &st
, 0))
4023 die("cannot hash %s", one
->path
);
4030 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
4032 /* Strip the prefix but do not molest /dev/null and absolute paths */
4033 if (*namep
&& **namep
!= '/') {
4034 *namep
+= prefix_length
;
4038 if (*otherp
&& **otherp
!= '/') {
4039 *otherp
+= prefix_length
;
4040 if (**otherp
== '/')
4045 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
4047 const char *pgm
= external_diff();
4049 struct diff_filespec
*one
= p
->one
;
4050 struct diff_filespec
*two
= p
->two
;
4053 const char *attr_path
;
4056 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
4058 if (o
->prefix_length
)
4059 strip_prefix(o
->prefix_length
, &name
, &other
);
4061 if (!DIFF_OPT_TST(o
, ALLOW_EXTERNAL
))
4064 if (DIFF_PAIR_UNMERGED(p
)) {
4065 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
4066 NULL
, NULL
, NULL
, o
, p
);
4070 diff_fill_oid_info(one
);
4071 diff_fill_oid_info(two
);
4074 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4075 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4077 * a filepair that changes between file and symlink
4078 * needs to be split into deletion and creation.
4080 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4081 run_diff_cmd(NULL
, name
, other
, attr_path
,
4082 one
, null
, &msg
, o
, p
);
4084 strbuf_release(&msg
);
4086 null
= alloc_filespec(one
->path
);
4087 run_diff_cmd(NULL
, name
, other
, attr_path
,
4088 null
, two
, &msg
, o
, p
);
4092 run_diff_cmd(pgm
, name
, other
, attr_path
,
4093 one
, two
, &msg
, o
, p
);
4095 strbuf_release(&msg
);
4098 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4099 struct diffstat_t
*diffstat
)
4104 if (DIFF_PAIR_UNMERGED(p
)) {
4106 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
, diffstat
, o
, p
);
4110 name
= p
->one
->path
;
4111 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4113 if (o
->prefix_length
)
4114 strip_prefix(o
->prefix_length
, &name
, &other
);
4116 diff_fill_oid_info(p
->one
);
4117 diff_fill_oid_info(p
->two
);
4119 builtin_diffstat(name
, other
, p
->one
, p
->two
, diffstat
, o
, p
);
4122 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4126 const char *attr_path
;
4128 if (DIFF_PAIR_UNMERGED(p
)) {
4133 name
= p
->one
->path
;
4134 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4135 attr_path
= other
? other
: name
;
4137 if (o
->prefix_length
)
4138 strip_prefix(o
->prefix_length
, &name
, &other
);
4140 diff_fill_oid_info(p
->one
);
4141 diff_fill_oid_info(p
->two
);
4143 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4146 void diff_setup(struct diff_options
*options
)
4148 memcpy(options
, &default_diff_options
, sizeof(*options
));
4150 options
->file
= stdout
;
4152 options
->abbrev
= DEFAULT_ABBREV
;
4153 options
->line_termination
= '\n';
4154 options
->break_opt
= -1;
4155 options
->rename_limit
= -1;
4156 options
->dirstat_permille
= diff_dirstat_permille_default
;
4157 options
->context
= diff_context_default
;
4158 options
->interhunkcontext
= diff_interhunk_context_default
;
4159 options
->ws_error_highlight
= ws_error_highlight_default
;
4160 DIFF_OPT_SET(options
, RENAME_EMPTY
);
4162 /* pathchange left =NULL by default */
4163 options
->change
= diff_change
;
4164 options
->add_remove
= diff_addremove
;
4165 options
->use_color
= diff_use_color_default
;
4166 options
->detect_rename
= diff_detect_rename_default
;
4167 options
->xdl_opts
|= diff_algorithm
;
4168 if (diff_indent_heuristic
)
4169 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4171 options
->orderfile
= diff_order_file_cfg
;
4173 if (diff_no_prefix
) {
4174 options
->a_prefix
= options
->b_prefix
= "";
4175 } else if (!diff_mnemonic_prefix
) {
4176 options
->a_prefix
= "a/";
4177 options
->b_prefix
= "b/";
4180 options
->color_moved
= diff_color_moved_default
;
4183 void diff_setup_done(struct diff_options
*options
)
4187 if (options
->set_default
)
4188 options
->set_default(options
);
4190 if (options
->output_format
& DIFF_FORMAT_NAME
)
4192 if (options
->output_format
& DIFF_FORMAT_NAME_STATUS
)
4194 if (options
->output_format
& DIFF_FORMAT_CHECKDIFF
)
4196 if (options
->output_format
& DIFF_FORMAT_NO_OUTPUT
)
4199 die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
4202 * Most of the time we can say "there are changes"
4203 * only by checking if there are changed paths, but
4204 * --ignore-whitespace* options force us to look
4208 if (DIFF_XDL_TST(options
, IGNORE_WHITESPACE
) ||
4209 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_CHANGE
) ||
4210 DIFF_XDL_TST(options
, IGNORE_WHITESPACE_AT_EOL
))
4211 DIFF_OPT_SET(options
, DIFF_FROM_CONTENTS
);
4213 DIFF_OPT_CLR(options
, DIFF_FROM_CONTENTS
);
4215 if (DIFF_OPT_TST(options
, FIND_COPIES_HARDER
))
4216 options
->detect_rename
= DIFF_DETECT_COPY
;
4218 if (!DIFF_OPT_TST(options
, RELATIVE_NAME
))
4219 options
->prefix
= NULL
;
4220 if (options
->prefix
)
4221 options
->prefix_length
= strlen(options
->prefix
);
4223 options
->prefix_length
= 0;
4225 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4226 DIFF_FORMAT_NAME_STATUS
|
4227 DIFF_FORMAT_CHECKDIFF
|
4228 DIFF_FORMAT_NO_OUTPUT
))
4229 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4230 DIFF_FORMAT_NUMSTAT
|
4231 DIFF_FORMAT_DIFFSTAT
|
4232 DIFF_FORMAT_SHORTSTAT
|
4233 DIFF_FORMAT_DIRSTAT
|
4234 DIFF_FORMAT_SUMMARY
|
4238 * These cases always need recursive; we do not drop caller-supplied
4239 * recursive bits for other formats here.
4241 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4242 DIFF_FORMAT_NUMSTAT
|
4243 DIFF_FORMAT_DIFFSTAT
|
4244 DIFF_FORMAT_SHORTSTAT
|
4245 DIFF_FORMAT_DIRSTAT
|
4246 DIFF_FORMAT_SUMMARY
|
4247 DIFF_FORMAT_CHECKDIFF
))
4248 DIFF_OPT_SET(options
, RECURSIVE
);
4250 * Also pickaxe would not work very well if you do not say recursive
4252 if (options
->pickaxe
)
4253 DIFF_OPT_SET(options
, RECURSIVE
);
4255 * When patches are generated, submodules diffed against the work tree
4256 * must be checked for dirtiness too so it can be shown in the output
4258 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4259 DIFF_OPT_SET(options
, DIRTY_SUBMODULES
);
4261 if (options
->detect_rename
&& options
->rename_limit
< 0)
4262 options
->rename_limit
= diff_rename_limit_default
;
4263 if (options
->setup
& DIFF_SETUP_USE_CACHE
) {
4265 /* read-cache does not die even when it fails
4266 * so it is safe for us to do this here. Also
4267 * it does not smudge active_cache or active_nr
4268 * when it fails, so we do not have to worry about
4269 * cleaning it up ourselves either.
4273 if (40 < options
->abbrev
)
4274 options
->abbrev
= 40; /* full */
4277 * It does not make sense to show the first hit we happened
4278 * to have found. It does not make sense not to return with
4279 * exit code in such a case either.
4281 if (DIFF_OPT_TST(options
, QUICK
)) {
4282 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4283 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
4286 options
->diff_path_counter
= 0;
4288 if (DIFF_OPT_TST(options
, FOLLOW_RENAMES
) && options
->pathspec
.nr
!= 1)
4289 die(_("--follow requires exactly one pathspec"));
4291 if (!options
->use_color
|| external_diff())
4292 options
->color_moved
= 0;
4295 static int opt_arg(const char *arg
, int arg_short
, const char *arg_long
, int *val
)
4305 if (c
== arg_short
) {
4309 if (val
&& isdigit(c
)) {
4311 int n
= strtoul(arg
, &end
, 10);
4322 eq
= strchrnul(arg
, '=');
4324 if (!len
|| strncmp(arg
, arg_long
, len
))
4329 if (!isdigit(*++eq
))
4331 n
= strtoul(eq
, &end
, 10);
4339 static int diff_scoreopt_parse(const char *opt
);
4341 static inline int short_opt(char opt
, const char **argv
,
4342 const char **optarg
)
4344 const char *arg
= argv
[0];
4345 if (arg
[0] != '-' || arg
[1] != opt
)
4347 if (arg
[2] != '\0') {
4352 die("Option '%c' requires a value", opt
);
4357 int parse_long_opt(const char *opt
, const char **argv
,
4358 const char **optarg
)
4360 const char *arg
= argv
[0];
4361 if (!skip_prefix(arg
, "--", &arg
))
4363 if (!skip_prefix(arg
, opt
, &arg
))
4365 if (*arg
== '=') { /* stuck form: --option=value */
4371 /* separate form: --option value */
4373 die("Option '--%s' requires a value", opt
);
4378 static int stat_opt(struct diff_options
*options
, const char **av
)
4380 const char *arg
= av
[0];
4382 int width
= options
->stat_width
;
4383 int name_width
= options
->stat_name_width
;
4384 int graph_width
= options
->stat_graph_width
;
4385 int count
= options
->stat_count
;
4388 if (!skip_prefix(arg
, "--stat", &arg
))
4389 die("BUG: stat option does not begin with --stat: %s", arg
);
4394 if (skip_prefix(arg
, "-width", &arg
)) {
4396 width
= strtoul(arg
+ 1, &end
, 10);
4397 else if (!*arg
&& !av
[1])
4398 die_want_option("--stat-width");
4400 width
= strtoul(av
[1], &end
, 10);
4403 } else if (skip_prefix(arg
, "-name-width", &arg
)) {
4405 name_width
= strtoul(arg
+ 1, &end
, 10);
4406 else if (!*arg
&& !av
[1])
4407 die_want_option("--stat-name-width");
4409 name_width
= strtoul(av
[1], &end
, 10);
4412 } else if (skip_prefix(arg
, "-graph-width", &arg
)) {
4414 graph_width
= strtoul(arg
+ 1, &end
, 10);
4415 else if (!*arg
&& !av
[1])
4416 die_want_option("--stat-graph-width");
4418 graph_width
= strtoul(av
[1], &end
, 10);
4421 } else if (skip_prefix(arg
, "-count", &arg
)) {
4423 count
= strtoul(arg
+ 1, &end
, 10);
4424 else if (!*arg
&& !av
[1])
4425 die_want_option("--stat-count");
4427 count
= strtoul(av
[1], &end
, 10);
4433 width
= strtoul(arg
+1, &end
, 10);
4435 name_width
= strtoul(end
+1, &end
, 10);
4437 count
= strtoul(end
+1, &end
, 10);
4440 /* Important! This checks all the error cases! */
4443 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4444 options
->stat_name_width
= name_width
;
4445 options
->stat_graph_width
= graph_width
;
4446 options
->stat_width
= width
;
4447 options
->stat_count
= count
;
4451 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
4453 struct strbuf errmsg
= STRBUF_INIT
;
4454 if (parse_dirstat_params(options
, params
, &errmsg
))
4455 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
4457 strbuf_release(&errmsg
);
4459 * The caller knows a dirstat-related option is given from the command
4460 * line; allow it to say "return this_function();"
4462 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
4466 static int parse_submodule_opt(struct diff_options
*options
, const char *value
)
4468 if (parse_submodule_params(options
, value
))
4469 die(_("Failed to parse --submodule option parameter: '%s'"),
4474 static const char diff_status_letters
[] = {
4477 DIFF_STATUS_DELETED
,
4478 DIFF_STATUS_MODIFIED
,
4479 DIFF_STATUS_RENAMED
,
4480 DIFF_STATUS_TYPE_CHANGED
,
4481 DIFF_STATUS_UNKNOWN
,
4482 DIFF_STATUS_UNMERGED
,
4483 DIFF_STATUS_FILTER_AON
,
4484 DIFF_STATUS_FILTER_BROKEN
,
4488 static unsigned int filter_bit
['Z' + 1];
4490 static void prepare_filter_bits(void)
4494 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4495 for (i
= 0; diff_status_letters
[i
]; i
++)
4496 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4500 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4502 return opt
->filter
& filter_bit
[(int) status
];
4505 static int parse_diff_filter_opt(const char *optarg
, struct diff_options
*opt
)
4509 prepare_filter_bits();
4512 * If there is a negation e.g. 'd' in the input, and we haven't
4513 * initialized the filter field with another --diff-filter, start
4514 * from full set of bits, except for AON.
4517 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4518 if (optch
< 'a' || 'z' < optch
)
4520 opt
->filter
= (1 << (ARRAY_SIZE(diff_status_letters
) - 1)) - 1;
4521 opt
->filter
&= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4526 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4530 if ('a' <= optch
&& optch
<= 'z') {
4532 optch
= toupper(optch
);
4537 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
4541 opt
->filter
&= ~bit
;
4548 static void enable_patch_output(int *fmt
) {
4549 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
4550 *fmt
|= DIFF_FORMAT_PATCH
;
4553 static int parse_ws_error_highlight_opt(struct diff_options
*opt
, const char *arg
)
4555 int val
= parse_ws_error_highlight(arg
);
4558 error("unknown value after ws-error-highlight=%.*s",
4562 opt
->ws_error_highlight
= val
;
4566 int diff_opt_parse(struct diff_options
*options
,
4567 const char **av
, int ac
, const char *prefix
)
4569 const char *arg
= av
[0];
4576 /* Output format options */
4577 if (!strcmp(arg
, "-p") || !strcmp(arg
, "-u") || !strcmp(arg
, "--patch")
4578 || opt_arg(arg
, 'U', "unified", &options
->context
))
4579 enable_patch_output(&options
->output_format
);
4580 else if (!strcmp(arg
, "--raw"))
4581 options
->output_format
|= DIFF_FORMAT_RAW
;
4582 else if (!strcmp(arg
, "--patch-with-raw")) {
4583 enable_patch_output(&options
->output_format
);
4584 options
->output_format
|= DIFF_FORMAT_RAW
;
4585 } else if (!strcmp(arg
, "--numstat"))
4586 options
->output_format
|= DIFF_FORMAT_NUMSTAT
;
4587 else if (!strcmp(arg
, "--shortstat"))
4588 options
->output_format
|= DIFF_FORMAT_SHORTSTAT
;
4589 else if (!strcmp(arg
, "-X") || !strcmp(arg
, "--dirstat"))
4590 return parse_dirstat_opt(options
, "");
4591 else if (skip_prefix(arg
, "-X", &arg
))
4592 return parse_dirstat_opt(options
, arg
);
4593 else if (skip_prefix(arg
, "--dirstat=", &arg
))
4594 return parse_dirstat_opt(options
, arg
);
4595 else if (!strcmp(arg
, "--cumulative"))
4596 return parse_dirstat_opt(options
, "cumulative");
4597 else if (!strcmp(arg
, "--dirstat-by-file"))
4598 return parse_dirstat_opt(options
, "files");
4599 else if (skip_prefix(arg
, "--dirstat-by-file=", &arg
)) {
4600 parse_dirstat_opt(options
, "files");
4601 return parse_dirstat_opt(options
, arg
);
4603 else if (!strcmp(arg
, "--check"))
4604 options
->output_format
|= DIFF_FORMAT_CHECKDIFF
;
4605 else if (!strcmp(arg
, "--summary"))
4606 options
->output_format
|= DIFF_FORMAT_SUMMARY
;
4607 else if (!strcmp(arg
, "--patch-with-stat")) {
4608 enable_patch_output(&options
->output_format
);
4609 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4610 } else if (!strcmp(arg
, "--name-only"))
4611 options
->output_format
|= DIFF_FORMAT_NAME
;
4612 else if (!strcmp(arg
, "--name-status"))
4613 options
->output_format
|= DIFF_FORMAT_NAME_STATUS
;
4614 else if (!strcmp(arg
, "-s") || !strcmp(arg
, "--no-patch"))
4615 options
->output_format
|= DIFF_FORMAT_NO_OUTPUT
;
4616 else if (starts_with(arg
, "--stat"))
4617 /* --stat, --stat-width, --stat-name-width, or --stat-count */
4618 return stat_opt(options
, av
);
4620 /* renames options */
4621 else if (starts_with(arg
, "-B") || starts_with(arg
, "--break-rewrites=") ||
4622 !strcmp(arg
, "--break-rewrites")) {
4623 if ((options
->break_opt
= diff_scoreopt_parse(arg
)) == -1)
4624 return error("invalid argument to -B: %s", arg
+2);
4626 else if (starts_with(arg
, "-M") || starts_with(arg
, "--find-renames=") ||
4627 !strcmp(arg
, "--find-renames")) {
4628 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
4629 return error("invalid argument to -M: %s", arg
+2);
4630 options
->detect_rename
= DIFF_DETECT_RENAME
;
4632 else if (!strcmp(arg
, "-D") || !strcmp(arg
, "--irreversible-delete")) {
4633 options
->irreversible_delete
= 1;
4635 else if (starts_with(arg
, "-C") || starts_with(arg
, "--find-copies=") ||
4636 !strcmp(arg
, "--find-copies")) {
4637 if (options
->detect_rename
== DIFF_DETECT_COPY
)
4638 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
4639 if ((options
->rename_score
= diff_scoreopt_parse(arg
)) == -1)
4640 return error("invalid argument to -C: %s", arg
+2);
4641 options
->detect_rename
= DIFF_DETECT_COPY
;
4643 else if (!strcmp(arg
, "--no-renames"))
4644 options
->detect_rename
= 0;
4645 else if (!strcmp(arg
, "--rename-empty"))
4646 DIFF_OPT_SET(options
, RENAME_EMPTY
);
4647 else if (!strcmp(arg
, "--no-rename-empty"))
4648 DIFF_OPT_CLR(options
, RENAME_EMPTY
);
4649 else if (!strcmp(arg
, "--relative"))
4650 DIFF_OPT_SET(options
, RELATIVE_NAME
);
4651 else if (skip_prefix(arg
, "--relative=", &arg
)) {
4652 DIFF_OPT_SET(options
, RELATIVE_NAME
);
4653 options
->prefix
= arg
;
4657 else if (!strcmp(arg
, "--minimal"))
4658 DIFF_XDL_SET(options
, NEED_MINIMAL
);
4659 else if (!strcmp(arg
, "--no-minimal"))
4660 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
4661 else if (!strcmp(arg
, "-w") || !strcmp(arg
, "--ignore-all-space"))
4662 DIFF_XDL_SET(options
, IGNORE_WHITESPACE
);
4663 else if (!strcmp(arg
, "-b") || !strcmp(arg
, "--ignore-space-change"))
4664 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_CHANGE
);
4665 else if (!strcmp(arg
, "--ignore-space-at-eol"))
4666 DIFF_XDL_SET(options
, IGNORE_WHITESPACE_AT_EOL
);
4667 else if (!strcmp(arg
, "--ignore-blank-lines"))
4668 DIFF_XDL_SET(options
, IGNORE_BLANK_LINES
);
4669 else if (!strcmp(arg
, "--indent-heuristic"))
4670 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4671 else if (!strcmp(arg
, "--no-indent-heuristic"))
4672 DIFF_XDL_CLR(options
, INDENT_HEURISTIC
);
4673 else if (!strcmp(arg
, "--patience"))
4674 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
4675 else if (!strcmp(arg
, "--histogram"))
4676 options
->xdl_opts
= DIFF_WITH_ALG(options
, HISTOGRAM_DIFF
);
4677 else if ((argcount
= parse_long_opt("diff-algorithm", av
, &optarg
))) {
4678 long value
= parse_algorithm_value(optarg
);
4680 return error("option diff-algorithm accepts \"myers\", "
4681 "\"minimal\", \"patience\" and \"histogram\"");
4682 /* clear out previous settings */
4683 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
4684 options
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
4685 options
->xdl_opts
|= value
;
4690 else if (!strcmp(arg
, "--binary")) {
4691 enable_patch_output(&options
->output_format
);
4692 DIFF_OPT_SET(options
, BINARY
);
4694 else if (!strcmp(arg
, "--full-index"))
4695 DIFF_OPT_SET(options
, FULL_INDEX
);
4696 else if (!strcmp(arg
, "-a") || !strcmp(arg
, "--text"))
4697 DIFF_OPT_SET(options
, TEXT
);
4698 else if (!strcmp(arg
, "-R"))
4699 DIFF_OPT_SET(options
, REVERSE_DIFF
);
4700 else if (!strcmp(arg
, "--find-copies-harder"))
4701 DIFF_OPT_SET(options
, FIND_COPIES_HARDER
);
4702 else if (!strcmp(arg
, "--follow"))
4703 DIFF_OPT_SET(options
, FOLLOW_RENAMES
);
4704 else if (!strcmp(arg
, "--no-follow")) {
4705 DIFF_OPT_CLR(options
, FOLLOW_RENAMES
);
4706 DIFF_OPT_CLR(options
, DEFAULT_FOLLOW_RENAMES
);
4707 } else if (!strcmp(arg
, "--color"))
4708 options
->use_color
= 1;
4709 else if (skip_prefix(arg
, "--color=", &arg
)) {
4710 int value
= git_config_colorbool(NULL
, arg
);
4712 return error("option `color' expects \"always\", \"auto\", or \"never\"");
4713 options
->use_color
= value
;
4715 else if (!strcmp(arg
, "--no-color"))
4716 options
->use_color
= 0;
4717 else if (!strcmp(arg
, "--color-moved")) {
4718 if (diff_color_moved_default
)
4719 options
->color_moved
= diff_color_moved_default
;
4720 if (options
->color_moved
== COLOR_MOVED_NO
)
4721 options
->color_moved
= COLOR_MOVED_DEFAULT
;
4722 } else if (!strcmp(arg
, "--no-color-moved"))
4723 options
->color_moved
= COLOR_MOVED_NO
;
4724 else if (skip_prefix(arg
, "--color-moved=", &arg
)) {
4725 int cm
= parse_color_moved(arg
);
4727 die("bad --color-moved argument: %s", arg
);
4728 options
->color_moved
= cm
;
4729 } else if (!strcmp(arg
, "--color-words")) {
4730 options
->use_color
= 1;
4731 options
->word_diff
= DIFF_WORDS_COLOR
;
4733 else if (skip_prefix(arg
, "--color-words=", &arg
)) {
4734 options
->use_color
= 1;
4735 options
->word_diff
= DIFF_WORDS_COLOR
;
4736 options
->word_regex
= arg
;
4738 else if (!strcmp(arg
, "--word-diff")) {
4739 if (options
->word_diff
== DIFF_WORDS_NONE
)
4740 options
->word_diff
= DIFF_WORDS_PLAIN
;
4742 else if (skip_prefix(arg
, "--word-diff=", &arg
)) {
4743 if (!strcmp(arg
, "plain"))
4744 options
->word_diff
= DIFF_WORDS_PLAIN
;
4745 else if (!strcmp(arg
, "color")) {
4746 options
->use_color
= 1;
4747 options
->word_diff
= DIFF_WORDS_COLOR
;
4749 else if (!strcmp(arg
, "porcelain"))
4750 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
4751 else if (!strcmp(arg
, "none"))
4752 options
->word_diff
= DIFF_WORDS_NONE
;
4754 die("bad --word-diff argument: %s", arg
);
4756 else if ((argcount
= parse_long_opt("word-diff-regex", av
, &optarg
))) {
4757 if (options
->word_diff
== DIFF_WORDS_NONE
)
4758 options
->word_diff
= DIFF_WORDS_PLAIN
;
4759 options
->word_regex
= optarg
;
4762 else if (!strcmp(arg
, "--exit-code"))
4763 DIFF_OPT_SET(options
, EXIT_WITH_STATUS
);
4764 else if (!strcmp(arg
, "--quiet"))
4765 DIFF_OPT_SET(options
, QUICK
);
4766 else if (!strcmp(arg
, "--ext-diff"))
4767 DIFF_OPT_SET(options
, ALLOW_EXTERNAL
);
4768 else if (!strcmp(arg
, "--no-ext-diff"))
4769 DIFF_OPT_CLR(options
, ALLOW_EXTERNAL
);
4770 else if (!strcmp(arg
, "--textconv"))
4771 DIFF_OPT_SET(options
, ALLOW_TEXTCONV
);
4772 else if (!strcmp(arg
, "--no-textconv"))
4773 DIFF_OPT_CLR(options
, ALLOW_TEXTCONV
);
4774 else if (!strcmp(arg
, "--ignore-submodules")) {
4775 DIFF_OPT_SET(options
, OVERRIDE_SUBMODULE_CONFIG
);
4776 handle_ignore_submodules_arg(options
, "all");
4777 } else if (skip_prefix(arg
, "--ignore-submodules=", &arg
)) {
4778 DIFF_OPT_SET(options
, OVERRIDE_SUBMODULE_CONFIG
);
4779 handle_ignore_submodules_arg(options
, arg
);
4780 } else if (!strcmp(arg
, "--submodule"))
4781 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
4782 else if (skip_prefix(arg
, "--submodule=", &arg
))
4783 return parse_submodule_opt(options
, arg
);
4784 else if (skip_prefix(arg
, "--ws-error-highlight=", &arg
))
4785 return parse_ws_error_highlight_opt(options
, arg
);
4786 else if (!strcmp(arg
, "--ita-invisible-in-index"))
4787 options
->ita_invisible_in_index
= 1;
4788 else if (!strcmp(arg
, "--ita-visible-in-index"))
4789 options
->ita_invisible_in_index
= 0;
4792 else if (!strcmp(arg
, "-z"))
4793 options
->line_termination
= 0;
4794 else if ((argcount
= short_opt('l', av
, &optarg
))) {
4795 options
->rename_limit
= strtoul(optarg
, NULL
, 10);
4798 else if ((argcount
= short_opt('S', av
, &optarg
))) {
4799 options
->pickaxe
= optarg
;
4800 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
4802 } else if ((argcount
= short_opt('G', av
, &optarg
))) {
4803 options
->pickaxe
= optarg
;
4804 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
4807 else if (!strcmp(arg
, "--pickaxe-all"))
4808 options
->pickaxe_opts
|= DIFF_PICKAXE_ALL
;
4809 else if (!strcmp(arg
, "--pickaxe-regex"))
4810 options
->pickaxe_opts
|= DIFF_PICKAXE_REGEX
;
4811 else if ((argcount
= short_opt('O', av
, &optarg
))) {
4812 options
->orderfile
= prefix_filename(prefix
, optarg
);
4815 else if ((argcount
= parse_long_opt("diff-filter", av
, &optarg
))) {
4816 int offending
= parse_diff_filter_opt(optarg
, options
);
4818 die("unknown change class '%c' in --diff-filter=%s",
4822 else if (!strcmp(arg
, "--no-abbrev"))
4823 options
->abbrev
= 0;
4824 else if (!strcmp(arg
, "--abbrev"))
4825 options
->abbrev
= DEFAULT_ABBREV
;
4826 else if (skip_prefix(arg
, "--abbrev=", &arg
)) {
4827 options
->abbrev
= strtoul(arg
, NULL
, 10);
4828 if (options
->abbrev
< MINIMUM_ABBREV
)
4829 options
->abbrev
= MINIMUM_ABBREV
;
4830 else if (40 < options
->abbrev
)
4831 options
->abbrev
= 40;
4833 else if ((argcount
= parse_long_opt("src-prefix", av
, &optarg
))) {
4834 options
->a_prefix
= optarg
;
4837 else if ((argcount
= parse_long_opt("line-prefix", av
, &optarg
))) {
4838 options
->line_prefix
= optarg
;
4839 options
->line_prefix_length
= strlen(options
->line_prefix
);
4840 graph_setup_line_prefix(options
);
4843 else if ((argcount
= parse_long_opt("dst-prefix", av
, &optarg
))) {
4844 options
->b_prefix
= optarg
;
4847 else if (!strcmp(arg
, "--no-prefix"))
4848 options
->a_prefix
= options
->b_prefix
= "";
4849 else if (opt_arg(arg
, '\0', "inter-hunk-context",
4850 &options
->interhunkcontext
))
4852 else if (!strcmp(arg
, "-W"))
4853 DIFF_OPT_SET(options
, FUNCCONTEXT
);
4854 else if (!strcmp(arg
, "--function-context"))
4855 DIFF_OPT_SET(options
, FUNCCONTEXT
);
4856 else if (!strcmp(arg
, "--no-function-context"))
4857 DIFF_OPT_CLR(options
, FUNCCONTEXT
);
4858 else if ((argcount
= parse_long_opt("output", av
, &optarg
))) {
4859 char *path
= prefix_filename(prefix
, optarg
);
4860 options
->file
= xfopen(path
, "w");
4861 options
->close_file
= 1;
4862 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
4863 options
->use_color
= GIT_COLOR_NEVER
;
4871 int parse_rename_score(const char **cp_p
)
4873 unsigned long num
, scale
;
4875 const char *cp
= *cp_p
;
4882 if ( !dot
&& ch
== '.' ) {
4885 } else if ( ch
== '%' ) {
4886 scale
= dot
? scale
*100 : 100;
4887 cp
++; /* % is always at the end */
4889 } else if ( ch
>= '0' && ch
<= '9' ) {
4890 if ( scale
< 100000 ) {
4892 num
= (num
*10) + (ch
-'0');
4901 /* user says num divided by scale and we say internally that
4902 * is MAX_SCORE * num / scale.
4904 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
4907 static int diff_scoreopt_parse(const char *opt
)
4909 int opt1
, opt2
, cmd
;
4915 /* convert the long-form arguments into short-form versions */
4916 if (skip_prefix(opt
, "break-rewrites", &opt
)) {
4917 if (*opt
== 0 || *opt
++ == '=')
4919 } else if (skip_prefix(opt
, "find-copies", &opt
)) {
4920 if (*opt
== 0 || *opt
++ == '=')
4922 } else if (skip_prefix(opt
, "find-renames", &opt
)) {
4923 if (*opt
== 0 || *opt
++ == '=')
4927 if (cmd
!= 'M' && cmd
!= 'C' && cmd
!= 'B')
4928 return -1; /* that is not a -M, -C, or -B option */
4930 opt1
= parse_rename_score(&opt
);
4936 else if (*opt
!= '/')
4937 return -1; /* we expect -B80/99 or -B80 */
4940 opt2
= parse_rename_score(&opt
);
4945 return opt1
| (opt2
<< 16);
4948 struct diff_queue_struct diff_queued_diff
;
4950 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
4952 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
4953 queue
->queue
[queue
->nr
++] = dp
;
4956 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
4957 struct diff_filespec
*one
,
4958 struct diff_filespec
*two
)
4960 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
4968 void diff_free_filepair(struct diff_filepair
*p
)
4970 free_filespec(p
->one
);
4971 free_filespec(p
->two
);
4975 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
4980 if (len
== GIT_SHA1_HEXSZ
)
4981 return oid_to_hex(oid
);
4983 abbrev
= diff_abbrev_oid(oid
, len
);
4984 abblen
= strlen(abbrev
);
4987 * In well-behaved cases, where the abbbreviated result is the
4988 * same as the requested length, append three dots after the
4989 * abbreviation (hence the whole logic is limited to the case
4990 * where abblen < 37); when the actual abbreviated result is a
4991 * bit longer than the requested length, we reduce the number
4992 * of dots so that they match the well-behaved ones. However,
4993 * if the actual abbreviation is longer than the requested
4994 * length by more than three, we give up on aligning, and add
4995 * three dots anyway, to indicate that the output is not the
4996 * full object name. Yes, this may be suboptimal, but this
4997 * appears only in "diff --raw --abbrev" output and it is not
4998 * worth the effort to change it now. Note that this would
4999 * likely to work fine when the automatic sizing of default
5000 * abbreviation length is used--we would be fed -1 in "len" in
5001 * that case, and will end up always appending three-dots, but
5002 * the automatic sizing is supposed to give abblen that ensures
5003 * uniqueness across all objects (statistically speaking).
5005 if (abblen
< GIT_SHA1_HEXSZ
- 3) {
5006 static char hex
[GIT_MAX_HEXSZ
+ 1];
5007 if (len
< abblen
&& abblen
<= len
+ 2)
5008 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
5010 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
5014 return oid_to_hex(oid
);
5017 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
5019 int line_termination
= opt
->line_termination
;
5020 int inter_name_termination
= line_termination
? '\t' : '\0';
5022 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5023 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
5024 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
5025 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
5026 fprintf(opt
->file
, "%s ",
5027 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
5030 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
5031 inter_name_termination
);
5033 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
5036 if (p
->status
== DIFF_STATUS_COPIED
||
5037 p
->status
== DIFF_STATUS_RENAMED
) {
5038 const char *name_a
, *name_b
;
5039 name_a
= p
->one
->path
;
5040 name_b
= p
->two
->path
;
5041 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5042 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
5043 write_name_quoted(name_b
, opt
->file
, line_termination
);
5045 const char *name_a
, *name_b
;
5046 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
5048 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5049 write_name_quoted(name_a
, opt
->file
, line_termination
);
5053 int diff_unmodified_pair(struct diff_filepair
*p
)
5055 /* This function is written stricter than necessary to support
5056 * the currently implemented transformers, but the idea is to
5057 * let transformers to produce diff_filepairs any way they want,
5058 * and filter and clean them up here before producing the output.
5060 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
5062 if (DIFF_PAIR_UNMERGED(p
))
5063 return 0; /* unmerged is interesting */
5065 /* deletion, addition, mode or type change
5066 * and rename are all interesting.
5068 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
5069 DIFF_PAIR_MODE_CHANGED(p
) ||
5070 strcmp(one
->path
, two
->path
))
5073 /* both are valid and point at the same path. that is, we are
5074 * dealing with a change.
5076 if (one
->oid_valid
&& two
->oid_valid
&&
5077 !oidcmp(&one
->oid
, &two
->oid
) &&
5078 !one
->dirty_submodule
&& !two
->dirty_submodule
)
5079 return 1; /* no change */
5080 if (!one
->oid_valid
&& !two
->oid_valid
)
5081 return 1; /* both look at the same file on the filesystem. */
5085 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
5087 if (diff_unmodified_pair(p
))
5090 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5091 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5092 return; /* no tree diffs in patch format */
5097 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
5098 struct diffstat_t
*diffstat
)
5100 if (diff_unmodified_pair(p
))
5103 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5104 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5105 return; /* no useful stat for tree diffs */
5107 run_diffstat(p
, o
, diffstat
);
5110 static void diff_flush_checkdiff(struct diff_filepair
*p
,
5111 struct diff_options
*o
)
5113 if (diff_unmodified_pair(p
))
5116 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5117 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5118 return; /* nothing to check in tree diffs */
5120 run_checkdiff(p
, o
);
5123 int diff_queue_is_empty(void)
5125 struct diff_queue_struct
*q
= &diff_queued_diff
;
5127 for (i
= 0; i
< q
->nr
; i
++)
5128 if (!diff_unmodified_pair(q
->queue
[i
]))
5134 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
5136 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
5139 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
5141 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
5142 fprintf(stderr
, "queue[%d] %s size %lu\n",
5147 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
5149 diff_debug_filespec(p
->one
, i
, "one");
5150 diff_debug_filespec(p
->two
, i
, "two");
5151 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
5152 p
->score
, p
->status
? p
->status
: '?',
5153 p
->one
->rename_used
, p
->broken_pair
);
5156 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
5160 fprintf(stderr
, "%s\n", msg
);
5161 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
5162 for (i
= 0; i
< q
->nr
; i
++) {
5163 struct diff_filepair
*p
= q
->queue
[i
];
5164 diff_debug_filepair(p
, i
);
5169 static void diff_resolve_rename_copy(void)
5172 struct diff_filepair
*p
;
5173 struct diff_queue_struct
*q
= &diff_queued_diff
;
5175 diff_debug_queue("resolve-rename-copy", q
);
5177 for (i
= 0; i
< q
->nr
; i
++) {
5179 p
->status
= 0; /* undecided */
5180 if (DIFF_PAIR_UNMERGED(p
))
5181 p
->status
= DIFF_STATUS_UNMERGED
;
5182 else if (!DIFF_FILE_VALID(p
->one
))
5183 p
->status
= DIFF_STATUS_ADDED
;
5184 else if (!DIFF_FILE_VALID(p
->two
))
5185 p
->status
= DIFF_STATUS_DELETED
;
5186 else if (DIFF_PAIR_TYPE_CHANGED(p
))
5187 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
5189 /* from this point on, we are dealing with a pair
5190 * whose both sides are valid and of the same type, i.e.
5191 * either in-place edit or rename/copy edit.
5193 else if (DIFF_PAIR_RENAME(p
)) {
5195 * A rename might have re-connected a broken
5196 * pair up, causing the pathnames to be the
5197 * same again. If so, that's not a rename at
5198 * all, just a modification..
5200 * Otherwise, see if this source was used for
5201 * multiple renames, in which case we decrement
5202 * the count, and call it a copy.
5204 if (!strcmp(p
->one
->path
, p
->two
->path
))
5205 p
->status
= DIFF_STATUS_MODIFIED
;
5206 else if (--p
->one
->rename_used
> 0)
5207 p
->status
= DIFF_STATUS_COPIED
;
5209 p
->status
= DIFF_STATUS_RENAMED
;
5211 else if (oidcmp(&p
->one
->oid
, &p
->two
->oid
) ||
5212 p
->one
->mode
!= p
->two
->mode
||
5213 p
->one
->dirty_submodule
||
5214 p
->two
->dirty_submodule
||
5215 is_null_oid(&p
->one
->oid
))
5216 p
->status
= DIFF_STATUS_MODIFIED
;
5218 /* This is a "no-change" entry and should not
5219 * happen anymore, but prepare for broken callers.
5221 error("feeding unmodified %s to diffcore",
5223 p
->status
= DIFF_STATUS_UNKNOWN
;
5226 diff_debug_queue("resolve-rename-copy done", q
);
5229 static int check_pair_status(struct diff_filepair
*p
)
5231 switch (p
->status
) {
5232 case DIFF_STATUS_UNKNOWN
:
5235 die("internal error in diff-resolve-rename-copy");
5241 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
5243 int fmt
= opt
->output_format
;
5245 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
5246 diff_flush_checkdiff(p
, opt
);
5247 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
5248 diff_flush_raw(p
, opt
);
5249 else if (fmt
& DIFF_FORMAT_NAME
) {
5250 const char *name_a
, *name_b
;
5251 name_a
= p
->two
->path
;
5253 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5254 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5255 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
5259 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
5261 struct strbuf sb
= STRBUF_INIT
;
5263 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
5265 strbuf_addf(&sb
, " %s ", newdelete
);
5267 quote_c_style(fs
->path
, &sb
, NULL
, 0);
5268 strbuf_addch(&sb
, '\n');
5269 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5271 strbuf_release(&sb
);
5274 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
5277 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
5278 struct strbuf sb
= STRBUF_INIT
;
5279 strbuf_addf(&sb
, " mode change %06o => %06o",
5280 p
->one
->mode
, p
->two
->mode
);
5282 strbuf_addch(&sb
, ' ');
5283 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
5285 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5287 strbuf_release(&sb
);
5291 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
5292 struct diff_filepair
*p
)
5294 struct strbuf sb
= STRBUF_INIT
;
5295 char *names
= pprint_rename(p
->one
->path
, p
->two
->path
);
5296 strbuf_addf(&sb
, " %s %s (%d%%)\n",
5297 renamecopy
, names
, similarity_index(p
));
5299 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5301 show_mode_change(opt
, p
, 0);
5304 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
5307 case DIFF_STATUS_DELETED
:
5308 show_file_mode_name(opt
, "delete", p
->one
);
5310 case DIFF_STATUS_ADDED
:
5311 show_file_mode_name(opt
, "create", p
->two
);
5313 case DIFF_STATUS_COPIED
:
5314 show_rename_copy(opt
, "copy", p
);
5316 case DIFF_STATUS_RENAMED
:
5317 show_rename_copy(opt
, "rename", p
);
5321 struct strbuf sb
= STRBUF_INIT
;
5322 strbuf_addstr(&sb
, " rewrite ");
5323 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
5324 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
5325 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
5328 show_mode_change(opt
, p
, !p
->score
);
5338 static int remove_space(char *line
, int len
)
5344 for (i
= 0; i
< len
; i
++)
5345 if (!isspace((c
= line
[i
])))
5351 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
5353 struct patch_id_t
*data
= priv
;
5356 /* Ignore line numbers when computing the SHA1 of the patch */
5357 if (starts_with(line
, "@@ -"))
5360 new_len
= remove_space(line
, len
);
5362 git_SHA1_Update(data
->ctx
, line
, new_len
);
5363 data
->patchlen
+= new_len
;
5366 static void patch_id_add_string(git_SHA_CTX
*ctx
, const char *str
)
5368 git_SHA1_Update(ctx
, str
, strlen(str
));
5371 static void patch_id_add_mode(git_SHA_CTX
*ctx
, unsigned mode
)
5373 /* large enough for 2^32 in octal */
5375 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
5376 git_SHA1_Update(ctx
, buf
, len
);
5379 /* returns 0 upon success, and writes result into sha1 */
5380 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
5382 struct diff_queue_struct
*q
= &diff_queued_diff
;
5385 struct patch_id_t data
;
5387 git_SHA1_Init(&ctx
);
5388 memset(&data
, 0, sizeof(struct patch_id_t
));
5391 for (i
= 0; i
< q
->nr
; i
++) {
5395 struct diff_filepair
*p
= q
->queue
[i
];
5398 memset(&xpp
, 0, sizeof(xpp
));
5399 memset(&xecfg
, 0, sizeof(xecfg
));
5401 return error("internal diff status error");
5402 if (p
->status
== DIFF_STATUS_UNKNOWN
)
5404 if (diff_unmodified_pair(p
))
5406 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5407 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5409 if (DIFF_PAIR_UNMERGED(p
))
5412 diff_fill_oid_info(p
->one
);
5413 diff_fill_oid_info(p
->two
);
5415 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
5416 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
5417 patch_id_add_string(&ctx
, "diff--git");
5418 patch_id_add_string(&ctx
, "a/");
5419 git_SHA1_Update(&ctx
, p
->one
->path
, len1
);
5420 patch_id_add_string(&ctx
, "b/");
5421 git_SHA1_Update(&ctx
, p
->two
->path
, len2
);
5423 if (p
->one
->mode
== 0) {
5424 patch_id_add_string(&ctx
, "newfilemode");
5425 patch_id_add_mode(&ctx
, p
->two
->mode
);
5426 patch_id_add_string(&ctx
, "---/dev/null");
5427 patch_id_add_string(&ctx
, "+++b/");
5428 git_SHA1_Update(&ctx
, p
->two
->path
, len2
);
5429 } else if (p
->two
->mode
== 0) {
5430 patch_id_add_string(&ctx
, "deletedfilemode");
5431 patch_id_add_mode(&ctx
, p
->one
->mode
);
5432 patch_id_add_string(&ctx
, "---a/");
5433 git_SHA1_Update(&ctx
, p
->one
->path
, len1
);
5434 patch_id_add_string(&ctx
, "+++/dev/null");
5436 patch_id_add_string(&ctx
, "---a/");
5437 git_SHA1_Update(&ctx
, p
->one
->path
, len1
);
5438 patch_id_add_string(&ctx
, "+++b/");
5439 git_SHA1_Update(&ctx
, p
->two
->path
, len2
);
5442 if (diff_header_only
)
5445 if (fill_mmfile(&mf1
, p
->one
) < 0 ||
5446 fill_mmfile(&mf2
, p
->two
) < 0)
5447 return error("unable to read files to diff");
5449 if (diff_filespec_is_binary(p
->one
) ||
5450 diff_filespec_is_binary(p
->two
)) {
5451 git_SHA1_Update(&ctx
, oid_to_hex(&p
->one
->oid
),
5453 git_SHA1_Update(&ctx
, oid_to_hex(&p
->two
->oid
),
5461 if (xdi_diff_outf(&mf1
, &mf2
, patch_id_consume
, &data
,
5463 return error("unable to generate patch-id diff for %s",
5467 git_SHA1_Final(oid
->hash
, &ctx
);
5471 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
5473 struct diff_queue_struct
*q
= &diff_queued_diff
;
5475 int result
= diff_get_patch_id(options
, oid
, diff_header_only
);
5477 for (i
= 0; i
< q
->nr
; i
++)
5478 diff_free_filepair(q
->queue
[i
]);
5481 DIFF_QUEUE_CLEAR(q
);
5486 static int is_summary_empty(const struct diff_queue_struct
*q
)
5490 for (i
= 0; i
< q
->nr
; i
++) {
5491 const struct diff_filepair
*p
= q
->queue
[i
];
5493 switch (p
->status
) {
5494 case DIFF_STATUS_DELETED
:
5495 case DIFF_STATUS_ADDED
:
5496 case DIFF_STATUS_COPIED
:
5497 case DIFF_STATUS_RENAMED
:
5502 if (p
->one
->mode
&& p
->two
->mode
&&
5503 p
->one
->mode
!= p
->two
->mode
)
5511 static const char rename_limit_warning
[] =
5512 N_("inexact rename detection was skipped due to too many files.");
5514 static const char degrade_cc_to_c_warning
[] =
5515 N_("only found copies from modified paths due to too many files.");
5517 static const char rename_limit_advice
[] =
5518 N_("you may want to set your %s variable to at least "
5519 "%d and retry the command.");
5521 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
5524 warning(_(degrade_cc_to_c_warning
));
5526 warning(_(rename_limit_warning
));
5529 if (0 < needed
&& needed
< 32767)
5530 warning(_(rename_limit_advice
), varname
, needed
);
5533 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
5536 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
5537 struct diff_queue_struct
*q
= &diff_queued_diff
;
5539 if (WSEH_NEW
& WS_RULE_MASK
)
5540 die("BUG: WS rules bit mask overlaps with diff symbol flags");
5543 o
->emitted_symbols
= &esm
;
5545 for (i
= 0; i
< q
->nr
; i
++) {
5546 struct diff_filepair
*p
= q
->queue
[i
];
5547 if (check_pair_status(p
))
5548 diff_flush_patch(p
, o
);
5551 if (o
->emitted_symbols
) {
5552 if (o
->color_moved
) {
5553 struct hashmap add_lines
, del_lines
;
5555 hashmap_init(&del_lines
,
5556 (hashmap_cmp_fn
)moved_entry_cmp
, o
, 0);
5557 hashmap_init(&add_lines
,
5558 (hashmap_cmp_fn
)moved_entry_cmp
, o
, 0);
5560 add_lines_to_move_detection(o
, &add_lines
, &del_lines
);
5561 mark_color_as_moved(o
, &add_lines
, &del_lines
);
5562 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
5565 hashmap_free(&add_lines
, 0);
5566 hashmap_free(&del_lines
, 0);
5569 for (i
= 0; i
< esm
.nr
; i
++)
5570 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
5572 for (i
= 0; i
< esm
.nr
; i
++)
5573 free((void *)esm
.buf
[i
].line
);
5578 void diff_flush(struct diff_options
*options
)
5580 struct diff_queue_struct
*q
= &diff_queued_diff
;
5581 int i
, output_format
= options
->output_format
;
5583 int dirstat_by_line
= 0;
5586 * Order: raw, stat, summary, patch
5587 * or: name/name-status/checkdiff (other bits clear)
5592 if (output_format
& (DIFF_FORMAT_RAW
|
5594 DIFF_FORMAT_NAME_STATUS
|
5595 DIFF_FORMAT_CHECKDIFF
)) {
5596 for (i
= 0; i
< q
->nr
; i
++) {
5597 struct diff_filepair
*p
= q
->queue
[i
];
5598 if (check_pair_status(p
))
5599 flush_one_pair(p
, options
);
5604 if (output_format
& DIFF_FORMAT_DIRSTAT
&& DIFF_OPT_TST(options
, DIRSTAT_BY_LINE
))
5605 dirstat_by_line
= 1;
5607 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
5609 struct diffstat_t diffstat
;
5611 memset(&diffstat
, 0, sizeof(struct diffstat_t
));
5612 for (i
= 0; i
< q
->nr
; i
++) {
5613 struct diff_filepair
*p
= q
->queue
[i
];
5614 if (check_pair_status(p
))
5615 diff_flush_stat(p
, options
, &diffstat
);
5617 if (output_format
& DIFF_FORMAT_NUMSTAT
)
5618 show_numstat(&diffstat
, options
);
5619 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
5620 show_stats(&diffstat
, options
);
5621 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
5622 show_shortstats(&diffstat
, options
);
5623 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
5624 show_dirstat_by_line(&diffstat
, options
);
5625 free_diffstat_info(&diffstat
);
5628 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
5629 show_dirstat(options
);
5631 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
5632 for (i
= 0; i
< q
->nr
; i
++) {
5633 diff_summary(options
, q
->queue
[i
]);
5638 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
5639 DIFF_OPT_TST(options
, EXIT_WITH_STATUS
) &&
5640 DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
5642 * run diff_flush_patch for the exit status. setting
5643 * options->file to /dev/null should be safe, because we
5644 * aren't supposed to produce any output anyway.
5646 if (options
->close_file
)
5647 fclose(options
->file
);
5648 options
->file
= xfopen("/dev/null", "w");
5649 options
->close_file
= 1;
5650 options
->color_moved
= 0;
5651 for (i
= 0; i
< q
->nr
; i
++) {
5652 struct diff_filepair
*p
= q
->queue
[i
];
5653 if (check_pair_status(p
))
5654 diff_flush_patch(p
, options
);
5655 if (options
->found_changes
)
5660 if (output_format
& DIFF_FORMAT_PATCH
) {
5662 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
5663 if (options
->stat_sep
)
5664 /* attach patch instead of inline */
5665 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
5669 diff_flush_patch_all_file_pairs(options
);
5672 if (output_format
& DIFF_FORMAT_CALLBACK
)
5673 options
->format_callback(q
, options
, options
->format_callback_data
);
5675 for (i
= 0; i
< q
->nr
; i
++)
5676 diff_free_filepair(q
->queue
[i
]);
5679 DIFF_QUEUE_CLEAR(q
);
5680 if (options
->close_file
)
5681 fclose(options
->file
);
5684 * Report the content-level differences with HAS_CHANGES;
5685 * diff_addremove/diff_change does not set the bit when
5686 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
5688 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
)) {
5689 if (options
->found_changes
)
5690 DIFF_OPT_SET(options
, HAS_CHANGES
);
5692 DIFF_OPT_CLR(options
, HAS_CHANGES
);
5696 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
5698 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
5700 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
5702 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
5703 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
5704 filter_bit_tst(p
->status
, options
)));
5707 static void diffcore_apply_filter(struct diff_options
*options
)
5710 struct diff_queue_struct
*q
= &diff_queued_diff
;
5711 struct diff_queue_struct outq
;
5713 DIFF_QUEUE_CLEAR(&outq
);
5715 if (!options
->filter
)
5718 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
5720 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
5721 if (match_filter(options
, q
->queue
[i
]))
5727 /* otherwise we will clear the whole queue
5728 * by copying the empty outq at the end of this
5729 * function, but first clear the current entries
5732 for (i
= 0; i
< q
->nr
; i
++)
5733 diff_free_filepair(q
->queue
[i
]);
5736 /* Only the matching ones */
5737 for (i
= 0; i
< q
->nr
; i
++) {
5738 struct diff_filepair
*p
= q
->queue
[i
];
5739 if (match_filter(options
, p
))
5742 diff_free_filepair(p
);
5749 /* Check whether two filespecs with the same mode and size are identical */
5750 static int diff_filespec_is_identical(struct diff_filespec
*one
,
5751 struct diff_filespec
*two
)
5753 if (S_ISGITLINK(one
->mode
))
5755 if (diff_populate_filespec(one
, 0))
5757 if (diff_populate_filespec(two
, 0))
5759 return !memcmp(one
->data
, two
->data
, one
->size
);
5762 static int diff_filespec_check_stat_unmatch(struct diff_filepair
*p
)
5764 if (p
->done_skip_stat_unmatch
)
5765 return p
->skip_stat_unmatch_result
;
5767 p
->done_skip_stat_unmatch
= 1;
5768 p
->skip_stat_unmatch_result
= 0;
5770 * 1. Entries that come from stat info dirtiness
5771 * always have both sides (iow, not create/delete),
5772 * one side of the object name is unknown, with
5773 * the same mode and size. Keep the ones that
5774 * do not match these criteria. They have real
5777 * 2. At this point, the file is known to be modified,
5778 * with the same mode and size, and the object
5779 * name of one side is unknown. Need to inspect
5780 * the identical contents.
5782 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
5783 !DIFF_FILE_VALID(p
->two
) ||
5784 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
5785 (p
->one
->mode
!= p
->two
->mode
) ||
5786 diff_populate_filespec(p
->one
, CHECK_SIZE_ONLY
) ||
5787 diff_populate_filespec(p
->two
, CHECK_SIZE_ONLY
) ||
5788 (p
->one
->size
!= p
->two
->size
) ||
5789 !diff_filespec_is_identical(p
->one
, p
->two
)) /* (2) */
5790 p
->skip_stat_unmatch_result
= 1;
5791 return p
->skip_stat_unmatch_result
;
5794 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
5797 struct diff_queue_struct
*q
= &diff_queued_diff
;
5798 struct diff_queue_struct outq
;
5799 DIFF_QUEUE_CLEAR(&outq
);
5801 for (i
= 0; i
< q
->nr
; i
++) {
5802 struct diff_filepair
*p
= q
->queue
[i
];
5804 if (diff_filespec_check_stat_unmatch(p
))
5808 * The caller can subtract 1 from skip_stat_unmatch
5809 * to determine how many paths were dirty only
5810 * due to stat info mismatch.
5812 if (!DIFF_OPT_TST(diffopt
, NO_INDEX
))
5813 diffopt
->skip_stat_unmatch
++;
5814 diff_free_filepair(p
);
5821 static int diffnamecmp(const void *a_
, const void *b_
)
5823 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
5824 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
5825 const char *name_a
, *name_b
;
5827 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
5828 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
5829 return strcmp(name_a
, name_b
);
5832 void diffcore_fix_diff_index(struct diff_options
*options
)
5834 struct diff_queue_struct
*q
= &diff_queued_diff
;
5835 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
5838 void diffcore_std(struct diff_options
*options
)
5840 /* NOTE please keep the following in sync with diff_tree_combined() */
5841 if (options
->skip_stat_unmatch
)
5842 diffcore_skip_stat_unmatch(options
);
5843 if (!options
->found_follow
) {
5844 /* See try_to_follow_renames() in tree-diff.c */
5845 if (options
->break_opt
!= -1)
5846 diffcore_break(options
->break_opt
);
5847 if (options
->detect_rename
)
5848 diffcore_rename(options
);
5849 if (options
->break_opt
!= -1)
5850 diffcore_merge_broken();
5852 if (options
->pickaxe
)
5853 diffcore_pickaxe(options
);
5854 if (options
->orderfile
)
5855 diffcore_order(options
->orderfile
);
5856 if (!options
->found_follow
)
5857 /* See try_to_follow_renames() in tree-diff.c */
5858 diff_resolve_rename_copy();
5859 diffcore_apply_filter(options
);
5861 if (diff_queued_diff
.nr
&& !DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
5862 DIFF_OPT_SET(options
, HAS_CHANGES
);
5864 DIFF_OPT_CLR(options
, HAS_CHANGES
);
5866 options
->found_follow
= 0;
5869 int diff_result_code(struct diff_options
*opt
, int status
)
5873 diff_warn_rename_limit("diff.renameLimit",
5874 opt
->needed_rename_limit
,
5875 opt
->degraded_cc_to_c
);
5876 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
5877 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
5879 if (DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
5880 DIFF_OPT_TST(opt
, HAS_CHANGES
))
5882 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
5883 DIFF_OPT_TST(opt
, CHECK_FAILED
))
5888 int diff_can_quit_early(struct diff_options
*opt
)
5890 return (DIFF_OPT_TST(opt
, QUICK
) &&
5892 DIFF_OPT_TST(opt
, HAS_CHANGES
));
5896 * Shall changes to this submodule be ignored?
5898 * Submodule changes can be configured to be ignored separately for each path,
5899 * but that configuration can be overridden from the command line.
5901 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
5904 unsigned orig_flags
= options
->flags
;
5905 if (!DIFF_OPT_TST(options
, OVERRIDE_SUBMODULE_CONFIG
))
5906 set_diffopt_flags_from_submodule_config(options
, path
);
5907 if (DIFF_OPT_TST(options
, IGNORE_SUBMODULES
))
5909 options
->flags
= orig_flags
;
5913 void diff_addremove(struct diff_options
*options
,
5914 int addremove
, unsigned mode
,
5915 const struct object_id
*oid
,
5917 const char *concatpath
, unsigned dirty_submodule
)
5919 struct diff_filespec
*one
, *two
;
5921 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
5924 /* This may look odd, but it is a preparation for
5925 * feeding "there are unchanged files which should
5926 * not produce diffs, but when you are doing copy
5927 * detection you would need them, so here they are"
5928 * entries to the diff-core. They will be prefixed
5929 * with something like '=' or '*' (I haven't decided
5930 * which but should not make any difference).
5931 * Feeding the same new and old to diff_change()
5932 * also has the same effect.
5933 * Before the final output happens, they are pruned after
5934 * merged into rename/copy pairs as appropriate.
5936 if (DIFF_OPT_TST(options
, REVERSE_DIFF
))
5937 addremove
= (addremove
== '+' ? '-' :
5938 addremove
== '-' ? '+' : addremove
);
5940 if (options
->prefix
&&
5941 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
5944 one
= alloc_filespec(concatpath
);
5945 two
= alloc_filespec(concatpath
);
5947 if (addremove
!= '+')
5948 fill_filespec(one
, oid
, oid_valid
, mode
);
5949 if (addremove
!= '-') {
5950 fill_filespec(two
, oid
, oid_valid
, mode
);
5951 two
->dirty_submodule
= dirty_submodule
;
5954 diff_queue(&diff_queued_diff
, one
, two
);
5955 if (!DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
5956 DIFF_OPT_SET(options
, HAS_CHANGES
);
5959 void diff_change(struct diff_options
*options
,
5960 unsigned old_mode
, unsigned new_mode
,
5961 const struct object_id
*old_oid
,
5962 const struct object_id
*new_oid
,
5963 int old_oid_valid
, int new_oid_valid
,
5964 const char *concatpath
,
5965 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
5967 struct diff_filespec
*one
, *two
;
5968 struct diff_filepair
*p
;
5970 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
5971 is_submodule_ignored(concatpath
, options
))
5974 if (DIFF_OPT_TST(options
, REVERSE_DIFF
)) {
5975 SWAP(old_mode
, new_mode
);
5976 SWAP(old_oid
, new_oid
);
5977 SWAP(old_oid_valid
, new_oid_valid
);
5978 SWAP(old_dirty_submodule
, new_dirty_submodule
);
5981 if (options
->prefix
&&
5982 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
5985 one
= alloc_filespec(concatpath
);
5986 two
= alloc_filespec(concatpath
);
5987 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
5988 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
5989 one
->dirty_submodule
= old_dirty_submodule
;
5990 two
->dirty_submodule
= new_dirty_submodule
;
5991 p
= diff_queue(&diff_queued_diff
, one
, two
);
5993 if (DIFF_OPT_TST(options
, DIFF_FROM_CONTENTS
))
5996 if (DIFF_OPT_TST(options
, QUICK
) && options
->skip_stat_unmatch
&&
5997 !diff_filespec_check_stat_unmatch(p
))
6000 DIFF_OPT_SET(options
, HAS_CHANGES
);
6003 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
6005 struct diff_filepair
*pair
;
6006 struct diff_filespec
*one
, *two
;
6008 if (options
->prefix
&&
6009 strncmp(path
, options
->prefix
, options
->prefix_length
))
6012 one
= alloc_filespec(path
);
6013 two
= alloc_filespec(path
);
6014 pair
= diff_queue(&diff_queued_diff
, one
, two
);
6015 pair
->is_unmerged
= 1;
6019 static char *run_textconv(const char *pgm
, struct diff_filespec
*spec
,
6022 struct diff_tempfile
*temp
;
6023 const char *argv
[3];
6024 const char **arg
= argv
;
6025 struct child_process child
= CHILD_PROCESS_INIT
;
6026 struct strbuf buf
= STRBUF_INIT
;
6029 temp
= prepare_temp_file(spec
->path
, spec
);
6031 *arg
++ = temp
->name
;
6034 child
.use_shell
= 1;
6037 if (start_command(&child
)) {
6042 if (strbuf_read(&buf
, child
.out
, 0) < 0)
6043 err
= error("error reading from textconv command '%s'", pgm
);
6046 if (finish_command(&child
) || err
) {
6047 strbuf_release(&buf
);
6053 return strbuf_detach(&buf
, outsize
);
6056 size_t fill_textconv(struct userdiff_driver
*driver
,
6057 struct diff_filespec
*df
,
6063 if (!DIFF_FILE_VALID(df
)) {
6067 if (diff_populate_filespec(df
, 0))
6068 die("unable to read files to diff");
6073 if (!driver
->textconv
)
6074 die("BUG: fill_textconv called with non-textconv driver");
6076 if (driver
->textconv_cache
&& df
->oid_valid
) {
6077 *outbuf
= notes_cache_get(driver
->textconv_cache
,
6084 *outbuf
= run_textconv(driver
->textconv
, df
, &size
);
6086 die("unable to read files to diff");
6088 if (driver
->textconv_cache
&& df
->oid_valid
) {
6089 /* ignore errors, as we might be in a readonly repository */
6090 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
6093 * we could save up changes and flush them all at the end,
6094 * but we would need an extra call after all diffing is done.
6095 * Since generating a cache entry is the slow path anyway,
6096 * this extra overhead probably isn't a big deal.
6098 notes_cache_write(driver
->textconv_cache
);
6104 int textconv_object(const char *path
,
6106 const struct object_id
*oid
,
6109 unsigned long *buf_size
)
6111 struct diff_filespec
*df
;
6112 struct userdiff_driver
*textconv
;
6114 df
= alloc_filespec(path
);
6115 fill_filespec(df
, oid
, oid_valid
, mode
);
6116 textconv
= get_textconv(df
);
6122 *buf_size
= fill_textconv(textconv
, df
, buf
);
6127 void setup_diff_pager(struct diff_options
*opt
)
6130 * If the user asked for our exit code, then either they want --quiet
6131 * or --exit-code. We should definitely not bother with a pager in the
6132 * former case, as we will generate no output. Since we still properly
6133 * report our exit code even when a pager is run, we _could_ run a
6134 * pager with --exit-code. But since we have not done so historically,
6135 * and because it is easy to find people oneline advising "git diff
6136 * --exit-code" in hooks and other scripts, we do not do so.
6138 if (!DIFF_OPT_TST(opt
, EXIT_WITH_STATUS
) &&
6139 check_pager_config("diff") != 0)