2 * Copyright (C) 2005 Junio C Hamano
11 #include "xdiff-interface.h"
14 #include "run-command.h"
16 #include "object-store.h"
18 #include "submodule-config.h"
19 #include "submodule.h"
22 #include "string-list.h"
26 #include "parse-options.h"
28 #include "promisor-remote.h"
31 #ifdef NO_FAST_WORKING_DIRECTORY
32 #define FAST_WORKING_DIRECTORY 0
34 #define FAST_WORKING_DIRECTORY 1
37 static int diff_detect_rename_default
;
38 static int diff_indent_heuristic
= 1;
39 static int diff_rename_limit_default
= 1000;
40 static int diff_suppress_blank_empty
;
41 static int diff_use_color_default
= -1;
42 static int diff_color_moved_default
;
43 static int diff_color_moved_ws_default
;
44 static int diff_context_default
= 3;
45 static int diff_interhunk_context_default
;
46 static const char *diff_word_regex_cfg
;
47 static const char *external_diff_cmd_cfg
;
48 static const char *diff_order_file_cfg
;
49 int diff_auto_refresh_index
= 1;
50 static int diff_mnemonic_prefix
;
51 static int diff_no_prefix
;
52 static int diff_relative
;
53 static int diff_stat_graph_width
;
54 static int diff_dirstat_permille_default
= 30;
55 static struct diff_options default_diff_options
;
56 static long diff_algorithm
;
57 static unsigned ws_error_highlight_default
= WSEH_NEW
;
59 static char diff_colors
[][COLOR_MAXLEN
] = {
61 GIT_COLOR_NORMAL
, /* CONTEXT */
62 GIT_COLOR_BOLD
, /* METAINFO */
63 GIT_COLOR_CYAN
, /* FRAGINFO */
64 GIT_COLOR_RED
, /* OLD */
65 GIT_COLOR_GREEN
, /* NEW */
66 GIT_COLOR_YELLOW
, /* COMMIT */
67 GIT_COLOR_BG_RED
, /* WHITESPACE */
68 GIT_COLOR_NORMAL
, /* FUNCINFO */
69 GIT_COLOR_BOLD_MAGENTA
, /* OLD_MOVED */
70 GIT_COLOR_BOLD_BLUE
, /* OLD_MOVED ALTERNATIVE */
71 GIT_COLOR_FAINT
, /* OLD_MOVED_DIM */
72 GIT_COLOR_FAINT_ITALIC
, /* OLD_MOVED_ALTERNATIVE_DIM */
73 GIT_COLOR_BOLD_CYAN
, /* NEW_MOVED */
74 GIT_COLOR_BOLD_YELLOW
, /* NEW_MOVED ALTERNATIVE */
75 GIT_COLOR_FAINT
, /* NEW_MOVED_DIM */
76 GIT_COLOR_FAINT_ITALIC
, /* NEW_MOVED_ALTERNATIVE_DIM */
77 GIT_COLOR_FAINT
, /* CONTEXT_DIM */
78 GIT_COLOR_FAINT_RED
, /* OLD_DIM */
79 GIT_COLOR_FAINT_GREEN
, /* NEW_DIM */
80 GIT_COLOR_BOLD
, /* CONTEXT_BOLD */
81 GIT_COLOR_BOLD_RED
, /* OLD_BOLD */
82 GIT_COLOR_BOLD_GREEN
, /* NEW_BOLD */
85 static const char *color_diff_slots
[] = {
86 [DIFF_CONTEXT
] = "context",
87 [DIFF_METAINFO
] = "meta",
88 [DIFF_FRAGINFO
] = "frag",
89 [DIFF_FILE_OLD
] = "old",
90 [DIFF_FILE_NEW
] = "new",
91 [DIFF_COMMIT
] = "commit",
92 [DIFF_WHITESPACE
] = "whitespace",
93 [DIFF_FUNCINFO
] = "func",
94 [DIFF_FILE_OLD_MOVED
] = "oldMoved",
95 [DIFF_FILE_OLD_MOVED_ALT
] = "oldMovedAlternative",
96 [DIFF_FILE_OLD_MOVED_DIM
] = "oldMovedDimmed",
97 [DIFF_FILE_OLD_MOVED_ALT_DIM
] = "oldMovedAlternativeDimmed",
98 [DIFF_FILE_NEW_MOVED
] = "newMoved",
99 [DIFF_FILE_NEW_MOVED_ALT
] = "newMovedAlternative",
100 [DIFF_FILE_NEW_MOVED_DIM
] = "newMovedDimmed",
101 [DIFF_FILE_NEW_MOVED_ALT_DIM
] = "newMovedAlternativeDimmed",
102 [DIFF_CONTEXT_DIM
] = "contextDimmed",
103 [DIFF_FILE_OLD_DIM
] = "oldDimmed",
104 [DIFF_FILE_NEW_DIM
] = "newDimmed",
105 [DIFF_CONTEXT_BOLD
] = "contextBold",
106 [DIFF_FILE_OLD_BOLD
] = "oldBold",
107 [DIFF_FILE_NEW_BOLD
] = "newBold",
110 define_list_config_array_extra(color_diff_slots
, {"plain"});
112 static int parse_diff_color_slot(const char *var
)
114 if (!strcasecmp(var
, "plain"))
116 return LOOKUP_CONFIG(color_diff_slots
, var
);
119 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
120 struct strbuf
*errmsg
)
122 char *params_copy
= xstrdup(params_string
);
123 struct string_list params
= STRING_LIST_INIT_NODUP
;
128 string_list_split_in_place(¶ms
, params_copy
, ',', -1);
129 for (i
= 0; i
< params
.nr
; i
++) {
130 const char *p
= params
.items
[i
].string
;
131 if (!strcmp(p
, "changes")) {
132 options
->flags
.dirstat_by_line
= 0;
133 options
->flags
.dirstat_by_file
= 0;
134 } else if (!strcmp(p
, "lines")) {
135 options
->flags
.dirstat_by_line
= 1;
136 options
->flags
.dirstat_by_file
= 0;
137 } else if (!strcmp(p
, "files")) {
138 options
->flags
.dirstat_by_line
= 0;
139 options
->flags
.dirstat_by_file
= 1;
140 } else if (!strcmp(p
, "noncumulative")) {
141 options
->flags
.dirstat_cumulative
= 0;
142 } else if (!strcmp(p
, "cumulative")) {
143 options
->flags
.dirstat_cumulative
= 1;
144 } else if (isdigit(*p
)) {
146 int permille
= strtoul(p
, &end
, 10) * 10;
147 if (*end
== '.' && isdigit(*++end
)) {
148 /* only use first digit */
149 permille
+= *end
- '0';
150 /* .. and ignore any further digits */
151 while (isdigit(*++end
))
155 options
->dirstat_permille
= permille
;
157 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
162 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
167 string_list_clear(¶ms
, 0);
172 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
174 if (!strcmp(value
, "log"))
175 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
176 else if (!strcmp(value
, "short"))
177 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
178 else if (!strcmp(value
, "diff"))
179 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
181 * Please update $__git_diff_submodule_formats in
182 * git-completion.bash when you add new formats.
189 int git_config_rename(const char *var
, const char *value
)
192 return DIFF_DETECT_RENAME
;
193 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
194 return DIFF_DETECT_COPY
;
195 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
198 long parse_algorithm_value(const char *value
)
202 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
204 else if (!strcasecmp(value
, "minimal"))
205 return XDF_NEED_MINIMAL
;
206 else if (!strcasecmp(value
, "patience"))
207 return XDF_PATIENCE_DIFF
;
208 else if (!strcasecmp(value
, "histogram"))
209 return XDF_HISTOGRAM_DIFF
;
211 * Please update $__git_diff_algorithms in git-completion.bash
212 * when you add new algorithms.
217 static int parse_one_token(const char **arg
, const char *token
)
220 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
227 static int parse_ws_error_highlight(const char *arg
)
229 const char *orig_arg
= arg
;
233 if (parse_one_token(&arg
, "none"))
235 else if (parse_one_token(&arg
, "default"))
237 else if (parse_one_token(&arg
, "all"))
238 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
239 else if (parse_one_token(&arg
, "new"))
241 else if (parse_one_token(&arg
, "old"))
243 else if (parse_one_token(&arg
, "context"))
246 return -1 - (int)(arg
- orig_arg
);
255 * These are to give UI layer defaults.
256 * The core-level commands such as git-diff-files should
257 * never be affected by the setting of diff.renames
258 * the user happens to have in the configuration file.
260 void init_diff_ui_defaults(void)
262 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
265 int git_diff_heuristic_config(const char *var
, const char *value
, void *cb
)
267 if (!strcmp(var
, "diff.indentheuristic"))
268 diff_indent_heuristic
= git_config_bool(var
, value
);
272 static int parse_color_moved(const char *arg
)
274 switch (git_parse_maybe_bool(arg
)) {
276 return COLOR_MOVED_NO
;
278 return COLOR_MOVED_DEFAULT
;
283 if (!strcmp(arg
, "no"))
284 return COLOR_MOVED_NO
;
285 else if (!strcmp(arg
, "plain"))
286 return COLOR_MOVED_PLAIN
;
287 else if (!strcmp(arg
, "blocks"))
288 return COLOR_MOVED_BLOCKS
;
289 else if (!strcmp(arg
, "zebra"))
290 return COLOR_MOVED_ZEBRA
;
291 else if (!strcmp(arg
, "default"))
292 return COLOR_MOVED_DEFAULT
;
293 else if (!strcmp(arg
, "dimmed-zebra"))
294 return COLOR_MOVED_ZEBRA_DIM
;
295 else if (!strcmp(arg
, "dimmed_zebra"))
296 return COLOR_MOVED_ZEBRA_DIM
;
298 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
301 static unsigned parse_color_moved_ws(const char *arg
)
304 struct string_list l
= STRING_LIST_INIT_DUP
;
305 struct string_list_item
*i
;
307 string_list_split(&l
, arg
, ',', -1);
309 for_each_string_list_item(i
, &l
) {
310 struct strbuf sb
= STRBUF_INIT
;
311 strbuf_addstr(&sb
, i
->string
);
314 if (!strcmp(sb
.buf
, "no"))
316 else if (!strcmp(sb
.buf
, "ignore-space-change"))
317 ret
|= XDF_IGNORE_WHITESPACE_CHANGE
;
318 else if (!strcmp(sb
.buf
, "ignore-space-at-eol"))
319 ret
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
320 else if (!strcmp(sb
.buf
, "ignore-all-space"))
321 ret
|= XDF_IGNORE_WHITESPACE
;
322 else if (!strcmp(sb
.buf
, "allow-indentation-change"))
323 ret
|= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
;
325 ret
|= COLOR_MOVED_WS_ERROR
;
326 error(_("unknown color-moved-ws mode '%s', possible values are 'ignore-space-change', 'ignore-space-at-eol', 'ignore-all-space', 'allow-indentation-change'"), sb
.buf
);
332 if ((ret
& COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
) &&
333 (ret
& XDF_WHITESPACE_FLAGS
)) {
334 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
335 ret
|= COLOR_MOVED_WS_ERROR
;
338 string_list_clear(&l
, 0);
343 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
345 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
346 diff_use_color_default
= git_config_colorbool(var
, value
);
349 if (!strcmp(var
, "diff.colormoved")) {
350 int cm
= parse_color_moved(value
);
353 diff_color_moved_default
= cm
;
356 if (!strcmp(var
, "diff.colormovedws")) {
357 unsigned cm
= parse_color_moved_ws(value
);
358 if (cm
& COLOR_MOVED_WS_ERROR
)
360 diff_color_moved_ws_default
= cm
;
363 if (!strcmp(var
, "diff.context")) {
364 diff_context_default
= git_config_int(var
, value
);
365 if (diff_context_default
< 0)
369 if (!strcmp(var
, "diff.interhunkcontext")) {
370 diff_interhunk_context_default
= git_config_int(var
, value
);
371 if (diff_interhunk_context_default
< 0)
375 if (!strcmp(var
, "diff.renames")) {
376 diff_detect_rename_default
= git_config_rename(var
, value
);
379 if (!strcmp(var
, "diff.autorefreshindex")) {
380 diff_auto_refresh_index
= git_config_bool(var
, value
);
383 if (!strcmp(var
, "diff.mnemonicprefix")) {
384 diff_mnemonic_prefix
= git_config_bool(var
, value
);
387 if (!strcmp(var
, "diff.noprefix")) {
388 diff_no_prefix
= git_config_bool(var
, value
);
391 if (!strcmp(var
, "diff.relative")) {
392 diff_relative
= git_config_bool(var
, value
);
395 if (!strcmp(var
, "diff.statgraphwidth")) {
396 diff_stat_graph_width
= git_config_int(var
, value
);
399 if (!strcmp(var
, "diff.external"))
400 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
401 if (!strcmp(var
, "diff.wordregex"))
402 return git_config_string(&diff_word_regex_cfg
, var
, value
);
403 if (!strcmp(var
, "diff.orderfile"))
404 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
406 if (!strcmp(var
, "diff.ignoresubmodules"))
407 handle_ignore_submodules_arg(&default_diff_options
, value
);
409 if (!strcmp(var
, "diff.submodule")) {
410 if (parse_submodule_params(&default_diff_options
, value
))
411 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
416 if (!strcmp(var
, "diff.algorithm")) {
417 diff_algorithm
= parse_algorithm_value(value
);
418 if (diff_algorithm
< 0)
423 if (git_color_config(var
, value
, cb
) < 0)
426 return git_diff_basic_config(var
, value
, cb
);
429 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
433 if (!strcmp(var
, "diff.renamelimit")) {
434 diff_rename_limit_default
= git_config_int(var
, value
);
438 if (userdiff_config(var
, value
) < 0)
441 if (skip_prefix(var
, "diff.color.", &name
) ||
442 skip_prefix(var
, "color.diff.", &name
)) {
443 int slot
= parse_diff_color_slot(name
);
447 return config_error_nonbool(var
);
448 return color_parse(value
, diff_colors
[slot
]);
451 if (!strcmp(var
, "diff.wserrorhighlight")) {
452 int val
= parse_ws_error_highlight(value
);
455 ws_error_highlight_default
= val
;
459 /* like GNU diff's --suppress-blank-empty option */
460 if (!strcmp(var
, "diff.suppressblankempty") ||
461 /* for backwards compatibility */
462 !strcmp(var
, "diff.suppress-blank-empty")) {
463 diff_suppress_blank_empty
= git_config_bool(var
, value
);
467 if (!strcmp(var
, "diff.dirstat")) {
468 struct strbuf errmsg
= STRBUF_INIT
;
469 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
470 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
471 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
473 strbuf_release(&errmsg
);
474 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
478 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
481 return git_default_config(var
, value
, cb
);
484 static char *quote_two(const char *one
, const char *two
)
486 int need_one
= quote_c_style(one
, NULL
, NULL
, CQUOTE_NODQ
);
487 int need_two
= quote_c_style(two
, NULL
, NULL
, CQUOTE_NODQ
);
488 struct strbuf res
= STRBUF_INIT
;
490 if (need_one
+ need_two
) {
491 strbuf_addch(&res
, '"');
492 quote_c_style(one
, &res
, NULL
, CQUOTE_NODQ
);
493 quote_c_style(two
, &res
, NULL
, CQUOTE_NODQ
);
494 strbuf_addch(&res
, '"');
496 strbuf_addstr(&res
, one
);
497 strbuf_addstr(&res
, two
);
499 return strbuf_detach(&res
, NULL
);
502 static const char *external_diff(void)
504 static const char *external_diff_cmd
= NULL
;
505 static int done_preparing
= 0;
508 return external_diff_cmd
;
509 external_diff_cmd
= xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
510 if (!external_diff_cmd
)
511 external_diff_cmd
= external_diff_cmd_cfg
;
513 return external_diff_cmd
;
517 * Keep track of files used for diffing. Sometimes such an entry
518 * refers to a temporary file, sometimes to an existing file, and
519 * sometimes to "/dev/null".
521 static struct diff_tempfile
{
523 * filename external diff should read from, or NULL if this
524 * entry is currently not in use:
528 char hex
[GIT_MAX_HEXSZ
+ 1];
532 * If this diff_tempfile instance refers to a temporary file,
533 * this tempfile object is used to manage its lifetime.
535 struct tempfile
*tempfile
;
538 struct emit_callback
{
541 int blank_at_eof_in_preimage
;
542 int blank_at_eof_in_postimage
;
544 int lno_in_postimage
;
545 const char **label_path
;
546 struct diff_words_data
*diff_words
;
547 struct diff_options
*opt
;
548 struct strbuf
*header
;
551 static int count_lines(const char *data
, int size
)
553 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
560 completely_empty
= 0;
564 completely_empty
= 0;
567 if (completely_empty
)
570 count
++; /* no trailing newline */
574 static int fill_mmfile(struct repository
*r
, mmfile_t
*mf
,
575 struct diff_filespec
*one
)
577 if (!DIFF_FILE_VALID(one
)) {
578 mf
->ptr
= (char *)""; /* does not matter */
582 else if (diff_populate_filespec(r
, one
, NULL
))
586 mf
->size
= one
->size
;
590 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
591 static unsigned long diff_filespec_size(struct repository
*r
,
592 struct diff_filespec
*one
)
594 struct diff_populate_filespec_options dpf_options
= {
595 .check_size_only
= 1,
598 if (!DIFF_FILE_VALID(one
))
600 diff_populate_filespec(r
, one
, &dpf_options
);
604 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
607 long size
= mf
->size
;
612 ptr
+= size
- 1; /* pointing at the very end */
614 ; /* incomplete line */
616 ptr
--; /* skip the last LF */
617 while (mf
->ptr
< ptr
) {
619 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
620 if (*prev_eol
== '\n')
622 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
630 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
631 struct emit_callback
*ecbdata
)
634 unsigned ws_rule
= ecbdata
->ws_rule
;
635 l1
= count_trailing_blank(mf1
, ws_rule
);
636 l2
= count_trailing_blank(mf2
, ws_rule
);
638 ecbdata
->blank_at_eof_in_preimage
= 0;
639 ecbdata
->blank_at_eof_in_postimage
= 0;
642 at
= count_lines(mf1
->ptr
, mf1
->size
);
643 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
645 at
= count_lines(mf2
->ptr
, mf2
->size
);
646 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
649 static void emit_line_0(struct diff_options
*o
,
650 const char *set_sign
, const char *set
, unsigned reverse
, const char *reset
,
651 int first
, const char *line
, int len
)
653 int has_trailing_newline
, has_trailing_carriage_return
;
654 int needs_reset
= 0; /* at the end of the line */
655 FILE *file
= o
->file
;
657 fputs(diff_line_prefix(o
), file
);
659 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
660 if (has_trailing_newline
)
663 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
664 if (has_trailing_carriage_return
)
670 if (reverse
&& want_color(o
->use_color
)) {
671 fputs(GIT_COLOR_REVERSE
, file
);
676 fputs(set_sign
, file
);
687 if (set_sign
&& set
!= set_sign
)
692 fwrite(line
, len
, 1, file
);
693 needs_reset
= 1; /* 'line' may contain color codes. */
698 if (has_trailing_carriage_return
)
700 if (has_trailing_newline
)
704 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
705 const char *line
, int len
)
707 emit_line_0(o
, set
, NULL
, 0, reset
, 0, line
, len
);
711 DIFF_SYMBOL_BINARY_DIFF_HEADER
,
712 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
713 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
714 DIFF_SYMBOL_BINARY_DIFF_BODY
,
715 DIFF_SYMBOL_BINARY_DIFF_FOOTER
,
716 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
717 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
718 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
719 DIFF_SYMBOL_STATS_LINE
,
720 DIFF_SYMBOL_WORD_DIFF
,
721 DIFF_SYMBOL_STAT_SEP
,
723 DIFF_SYMBOL_SUBMODULE_ADD
,
724 DIFF_SYMBOL_SUBMODULE_DEL
,
725 DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
726 DIFF_SYMBOL_SUBMODULE_MODIFIED
,
727 DIFF_SYMBOL_SUBMODULE_HEADER
,
728 DIFF_SYMBOL_SUBMODULE_ERROR
,
729 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
,
730 DIFF_SYMBOL_REWRITE_DIFF
,
731 DIFF_SYMBOL_BINARY_FILES
,
733 DIFF_SYMBOL_FILEPAIR_PLUS
,
734 DIFF_SYMBOL_FILEPAIR_MINUS
,
735 DIFF_SYMBOL_WORDS_PORCELAIN
,
738 DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
741 DIFF_SYMBOL_NO_LF_EOF
,
742 DIFF_SYMBOL_CONTEXT_FRAGINFO
,
743 DIFF_SYMBOL_CONTEXT_MARKER
,
744 DIFF_SYMBOL_SEPARATOR
747 * Flags for content lines:
748 * 0..12 are whitespace rules
749 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
750 * 16 is marking if the line is blank at EOF
752 #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
753 #define DIFF_SYMBOL_MOVED_LINE (1<<17)
754 #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
755 #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
756 #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
759 * This struct is used when we need to buffer the output of the diff output.
761 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
762 * into the pre/post image file. This pointer could be a union with the
763 * line pointer. By storing an offset into the file instead of the literal line,
764 * we can decrease the memory footprint for the buffered output. At first we
765 * may want to only have indirection for the content lines, but we could also
766 * enhance the state for emitting prefabricated lines, e.g. the similarity
767 * score line or hunk/file headers would only need to store a number or path
768 * and then the output can be constructed later on depending on state.
770 struct emitted_diff_symbol
{
774 int indent_off
; /* Offset to first non-whitespace character */
775 int indent_width
; /* The visual width of the indentation */
778 #define EMITTED_DIFF_SYMBOL_INIT { 0 }
780 struct emitted_diff_symbols
{
781 struct emitted_diff_symbol
*buf
;
784 #define EMITTED_DIFF_SYMBOLS_INIT { 0 }
786 static void append_emitted_diff_symbol(struct diff_options
*o
,
787 struct emitted_diff_symbol
*e
)
789 struct emitted_diff_symbol
*f
;
791 ALLOC_GROW(o
->emitted_symbols
->buf
,
792 o
->emitted_symbols
->nr
+ 1,
793 o
->emitted_symbols
->alloc
);
794 f
= &o
->emitted_symbols
->buf
[o
->emitted_symbols
->nr
++];
796 memcpy(f
, e
, sizeof(struct emitted_diff_symbol
));
797 f
->line
= e
->line
? xmemdupz(e
->line
, e
->len
) : NULL
;
801 struct hashmap_entry ent
;
802 const struct emitted_diff_symbol
*es
;
803 struct moved_entry
*next_line
;
807 struct moved_entry
*match
;
808 int wsd
; /* The whitespace delta of this block */
811 static void moved_block_clear(struct moved_block
*b
)
813 memset(b
, 0, sizeof(*b
));
816 #define INDENT_BLANKLINE INT_MIN
818 static void fill_es_indent_data(struct emitted_diff_symbol
*es
)
820 unsigned int off
= 0, i
;
821 int width
= 0, tab_width
= es
->flags
& WS_TAB_WIDTH_MASK
;
822 const char *s
= es
->line
;
823 const int len
= es
->len
;
825 /* skip any \v \f \r at start of indentation */
826 while (s
[off
] == '\f' || s
[off
] == '\v' ||
827 (s
[off
] == '\r' && off
< len
- 1))
830 /* calculate the visual width of indentation */
835 } else if (s
[off
] == '\t') {
836 width
+= tab_width
- (width
% tab_width
);
837 while (s
[++off
] == '\t')
844 /* check if this line is blank */
845 for (i
= off
; i
< len
; i
++)
850 es
->indent_width
= INDENT_BLANKLINE
;
851 es
->indent_off
= len
;
853 es
->indent_off
= off
;
854 es
->indent_width
= width
;
858 static int compute_ws_delta(const struct emitted_diff_symbol
*a
,
859 const struct emitted_diff_symbol
*b
,
864 a_off
= a
->indent_off
,
865 a_width
= a
->indent_width
,
866 b_off
= b
->indent_off
,
867 b_width
= b
->indent_width
;
870 if (a_width
== INDENT_BLANKLINE
&& b_width
== INDENT_BLANKLINE
) {
871 *out
= INDENT_BLANKLINE
;
875 if (a
->s
== DIFF_SYMBOL_PLUS
)
876 delta
= a_width
- b_width
;
878 delta
= b_width
- a_width
;
880 if (a_len
- a_off
!= b_len
- b_off
||
881 memcmp(a
->line
+ a_off
, b
->line
+ b_off
, a_len
- a_off
))
889 static int cmp_in_block_with_wsd(const struct diff_options
*o
,
890 const struct moved_entry
*cur
,
891 const struct moved_entry
*match
,
892 struct moved_block
*pmb
,
895 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
896 int al
= cur
->es
->len
, bl
= match
->es
->len
, cl
= l
->len
;
897 const char *a
= cur
->es
->line
,
898 *b
= match
->es
->line
,
900 int a_off
= cur
->es
->indent_off
,
901 a_width
= cur
->es
->indent_width
,
902 c_off
= l
->indent_off
,
903 c_width
= l
->indent_width
;
907 * We need to check if 'cur' is equal to 'match'. As those
908 * are from the same (+/-) side, we do not need to adjust for
909 * indent changes. However these were found using fuzzy
910 * matching so we do have to check if they are equal. Here we
911 * just check the lengths. We delay calling memcmp() to check
912 * the contents until later as if the length comparison for a
913 * and c fails we can avoid the call all together.
918 /* If 'l' and 'cur' are both blank then they match. */
919 if (a_width
== INDENT_BLANKLINE
&& c_width
== INDENT_BLANKLINE
)
923 * The indent changes of the block are known and stored in pmb->wsd;
924 * however we need to check if the indent changes of the current line
925 * match those of the current block and that the text of 'l' and 'cur'
926 * after the indentation match.
928 if (cur
->es
->s
== DIFF_SYMBOL_PLUS
)
929 delta
= a_width
- c_width
;
931 delta
= c_width
- a_width
;
934 * If the previous lines of this block were all blank then set its
937 if (pmb
->wsd
== INDENT_BLANKLINE
)
940 return !(delta
== pmb
->wsd
&& al
- a_off
== cl
- c_off
&&
941 !memcmp(a
, b
, al
) && !
942 memcmp(a
+ a_off
, c
+ c_off
, al
- a_off
));
945 static int moved_entry_cmp(const void *hashmap_cmp_fn_data
,
946 const struct hashmap_entry
*eptr
,
947 const struct hashmap_entry
*entry_or_key
,
950 const struct diff_options
*diffopt
= hashmap_cmp_fn_data
;
951 const struct moved_entry
*a
, *b
;
952 unsigned flags
= diffopt
->color_moved_ws_handling
953 & XDF_WHITESPACE_FLAGS
;
955 a
= container_of(eptr
, const struct moved_entry
, ent
);
956 b
= container_of(entry_or_key
, const struct moved_entry
, ent
);
958 if (diffopt
->color_moved_ws_handling
&
959 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
961 * As there is not specific white space config given,
962 * we'd need to check for a new block, so ignore all
963 * white space. The setup of the white space
964 * configuration for the next block is done else where
966 flags
|= XDF_IGNORE_WHITESPACE
;
968 return !xdiff_compare_lines(a
->es
->line
, a
->es
->len
,
969 b
->es
->line
, b
->es
->len
,
973 static struct moved_entry
*prepare_entry(struct diff_options
*o
,
976 struct moved_entry
*ret
= xmalloc(sizeof(*ret
));
977 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[line_no
];
978 unsigned flags
= o
->color_moved_ws_handling
& XDF_WHITESPACE_FLAGS
;
979 unsigned int hash
= xdiff_hash_string(l
->line
, l
->len
, flags
);
981 hashmap_entry_init(&ret
->ent
, hash
);
983 ret
->next_line
= NULL
;
988 static void add_lines_to_move_detection(struct diff_options
*o
,
989 struct hashmap
*add_lines
,
990 struct hashmap
*del_lines
)
992 struct moved_entry
*prev_line
= NULL
;
995 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
997 struct moved_entry
*key
;
999 switch (o
->emitted_symbols
->buf
[n
].s
) {
1000 case DIFF_SYMBOL_PLUS
:
1003 case DIFF_SYMBOL_MINUS
:
1011 if (o
->color_moved_ws_handling
&
1012 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1013 fill_es_indent_data(&o
->emitted_symbols
->buf
[n
]);
1014 key
= prepare_entry(o
, n
);
1015 if (prev_line
&& prev_line
->es
->s
== o
->emitted_symbols
->buf
[n
].s
)
1016 prev_line
->next_line
= key
;
1018 hashmap_add(hm
, &key
->ent
);
1023 static void pmb_advance_or_null(struct diff_options
*o
,
1024 struct moved_entry
*match
,
1026 struct moved_block
*pmb
,
1030 for (i
= 0; i
< pmb_nr
; i
++) {
1031 struct moved_entry
*prev
= pmb
[i
].match
;
1032 struct moved_entry
*cur
= (prev
&& prev
->next_line
) ?
1033 prev
->next_line
: NULL
;
1034 if (cur
&& !hm
->cmpfn(o
, &cur
->ent
, &match
->ent
, NULL
)) {
1037 pmb
[i
].match
= NULL
;
1042 static void pmb_advance_or_null_multi_match(struct diff_options
*o
,
1043 struct moved_entry
*match
,
1045 struct moved_block
*pmb
,
1049 char *got_match
= xcalloc(1, pmb_nr
);
1051 hashmap_for_each_entry_from(hm
, match
, ent
) {
1052 for (i
= 0; i
< pmb_nr
; i
++) {
1053 struct moved_entry
*prev
= pmb
[i
].match
;
1054 struct moved_entry
*cur
= (prev
&& prev
->next_line
) ?
1055 prev
->next_line
: NULL
;
1058 if (!cmp_in_block_with_wsd(o
, cur
, match
, &pmb
[i
], n
))
1063 for (i
= 0; i
< pmb_nr
; i
++) {
1065 /* Advance to the next line */
1066 pmb
[i
].match
= pmb
[i
].match
->next_line
;
1068 moved_block_clear(&pmb
[i
]);
1075 static int shrink_potential_moved_blocks(struct moved_block
*pmb
,
1080 /* Shrink the set of potential block to the remaining running */
1081 for (lp
= 0, rp
= pmb_nr
- 1; lp
<= rp
;) {
1082 while (lp
< pmb_nr
&& pmb
[lp
].match
)
1084 /* lp points at the first NULL now */
1086 while (rp
> -1 && !pmb
[rp
].match
)
1088 /* rp points at the last non-NULL */
1090 if (lp
< pmb_nr
&& rp
> -1 && lp
< rp
) {
1092 memset(&pmb
[rp
], 0, sizeof(pmb
[rp
]));
1098 /* Remember the number of running sets */
1103 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1105 * Otherwise, if the last block has fewer alphanumeric characters than
1106 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
1109 * The last block consists of the (n - block_length)'th line up to but not
1110 * including the nth line.
1112 * Returns 0 if the last block is empty or is unset by this function, non zero
1115 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1116 * Think of a way to unify them.
1118 static int adjust_last_block(struct diff_options
*o
, int n
, int block_length
)
1120 int i
, alnum_count
= 0;
1121 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
1122 return block_length
;
1123 for (i
= 1; i
< block_length
+ 1; i
++) {
1124 const char *c
= o
->emitted_symbols
->buf
[n
- i
].line
;
1129 if (alnum_count
>= COLOR_MOVED_MIN_ALNUM_COUNT
)
1133 for (i
= 1; i
< block_length
+ 1; i
++)
1134 o
->emitted_symbols
->buf
[n
- i
].flags
&= ~DIFF_SYMBOL_MOVED_LINE
;
1138 /* Find blocks of moved code, delegate actual coloring decision to helper */
1139 static void mark_color_as_moved(struct diff_options
*o
,
1140 struct hashmap
*add_lines
,
1141 struct hashmap
*del_lines
)
1143 struct moved_block
*pmb
= NULL
; /* potentially moved blocks */
1144 int pmb_nr
= 0, pmb_alloc
= 0;
1145 int n
, flipped_block
= 0, block_length
= 0;
1148 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1149 struct hashmap
*hm
= NULL
;
1150 struct moved_entry
*key
;
1151 struct moved_entry
*match
= NULL
;
1152 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1153 enum diff_symbol last_symbol
= 0;
1156 case DIFF_SYMBOL_PLUS
:
1158 key
= prepare_entry(o
, n
);
1159 match
= hashmap_get_entry(hm
, key
, ent
, NULL
);
1162 case DIFF_SYMBOL_MINUS
:
1164 key
= prepare_entry(o
, n
);
1165 match
= hashmap_get_entry(hm
, key
, ent
, NULL
);
1175 adjust_last_block(o
, n
, block_length
);
1176 for(i
= 0; i
< pmb_nr
; i
++)
1177 moved_block_clear(&pmb
[i
]);
1185 if (o
->color_moved
== COLOR_MOVED_PLAIN
) {
1187 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1191 if (o
->color_moved_ws_handling
&
1192 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1193 pmb_advance_or_null_multi_match(o
, match
, hm
, pmb
, pmb_nr
, n
);
1195 pmb_advance_or_null(o
, match
, hm
, pmb
, pmb_nr
);
1197 pmb_nr
= shrink_potential_moved_blocks(pmb
, pmb_nr
);
1201 * The current line is the start of a new block.
1202 * Setup the set of potential blocks.
1204 hashmap_for_each_entry_from(hm
, match
, ent
) {
1205 ALLOC_GROW(pmb
, pmb_nr
+ 1, pmb_alloc
);
1206 if (o
->color_moved_ws_handling
&
1207 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
) {
1208 if (compute_ws_delta(l
, match
->es
,
1210 pmb
[pmb_nr
++].match
= match
;
1212 pmb
[pmb_nr
].wsd
= 0;
1213 pmb
[pmb_nr
++].match
= match
;
1217 if (adjust_last_block(o
, n
, block_length
) &&
1218 pmb_nr
&& last_symbol
!= l
->s
)
1219 flipped_block
= (flipped_block
+ 1) % 2;
1228 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1229 if (flipped_block
&& o
->color_moved
!= COLOR_MOVED_BLOCKS
)
1230 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_ALT
;
1234 adjust_last_block(o
, n
, block_length
);
1236 for(n
= 0; n
< pmb_nr
; n
++)
1237 moved_block_clear(&pmb
[n
]);
1241 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1242 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
1243 static void dim_moved_lines(struct diff_options
*o
)
1246 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1247 struct emitted_diff_symbol
*prev
= (n
!= 0) ?
1248 &o
->emitted_symbols
->buf
[n
- 1] : NULL
;
1249 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1250 struct emitted_diff_symbol
*next
=
1251 (n
< o
->emitted_symbols
->nr
- 1) ?
1252 &o
->emitted_symbols
->buf
[n
+ 1] : NULL
;
1254 /* Not a plus or minus line? */
1255 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
)
1258 /* Not a moved line? */
1259 if (!(l
->flags
& DIFF_SYMBOL_MOVED_LINE
))
1263 * If prev or next are not a plus or minus line,
1264 * pretend they don't exist
1266 if (prev
&& prev
->s
!= DIFF_SYMBOL_PLUS
&&
1267 prev
->s
!= DIFF_SYMBOL_MINUS
)
1269 if (next
&& next
->s
!= DIFF_SYMBOL_PLUS
&&
1270 next
->s
!= DIFF_SYMBOL_MINUS
)
1273 /* Inside a block? */
1275 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1276 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
)) &&
1278 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1279 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
))) {
1280 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1284 /* Check if we are at an interesting bound: */
1285 if (prev
&& (prev
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1286 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1287 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1289 if (next
&& (next
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1290 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1291 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1295 * The boundary to prev and next are not interesting,
1296 * so this line is not interesting as a whole
1298 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1302 static void emit_line_ws_markup(struct diff_options
*o
,
1303 const char *set_sign
, const char *set
,
1305 int sign_index
, const char *line
, int len
,
1306 unsigned ws_rule
, int blank_at_eof
)
1308 const char *ws
= NULL
;
1309 int sign
= o
->output_indicators
[sign_index
];
1311 if (o
->ws_error_highlight
& ws_rule
) {
1312 ws
= diff_get_color_opt(o
, DIFF_WHITESPACE
);
1317 if (!ws
&& !set_sign
)
1318 emit_line_0(o
, set
, NULL
, 0, reset
, sign
, line
, len
);
1320 emit_line_0(o
, set_sign
, set
, !!set_sign
, reset
, sign
, line
, len
);
1321 } else if (blank_at_eof
)
1322 /* Blank line at EOF - paint '+' as well */
1323 emit_line_0(o
, ws
, NULL
, 0, reset
, sign
, line
, len
);
1325 /* Emit just the prefix, then the rest. */
1326 emit_line_0(o
, set_sign
? set_sign
: set
, NULL
, !!set_sign
, reset
,
1328 ws_check_emit(line
, len
, ws_rule
,
1329 o
->file
, set
, reset
, ws
);
1333 static void emit_diff_symbol_from_struct(struct diff_options
*o
,
1334 struct emitted_diff_symbol
*eds
)
1336 static const char *nneof
= " No newline at end of file\n";
1337 const char *context
, *reset
, *set
, *set_sign
, *meta
, *fraginfo
;
1338 struct strbuf sb
= STRBUF_INIT
;
1340 enum diff_symbol s
= eds
->s
;
1341 const char *line
= eds
->line
;
1343 unsigned flags
= eds
->flags
;
1346 case DIFF_SYMBOL_NO_LF_EOF
:
1347 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1348 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1349 putc('\n', o
->file
);
1350 emit_line_0(o
, context
, NULL
, 0, reset
, '\\',
1351 nneof
, strlen(nneof
));
1353 case DIFF_SYMBOL_SUBMODULE_HEADER
:
1354 case DIFF_SYMBOL_SUBMODULE_ERROR
:
1355 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
:
1356 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
:
1357 case DIFF_SYMBOL_SUMMARY
:
1358 case DIFF_SYMBOL_STATS_LINE
:
1359 case DIFF_SYMBOL_BINARY_DIFF_BODY
:
1360 case DIFF_SYMBOL_CONTEXT_FRAGINFO
:
1361 emit_line(o
, "", "", line
, len
);
1363 case DIFF_SYMBOL_CONTEXT_INCOMPLETE
:
1364 case DIFF_SYMBOL_CONTEXT_MARKER
:
1365 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1366 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1367 emit_line(o
, context
, reset
, line
, len
);
1369 case DIFF_SYMBOL_SEPARATOR
:
1370 fprintf(o
->file
, "%s%c",
1371 diff_line_prefix(o
),
1372 o
->line_termination
);
1374 case DIFF_SYMBOL_CONTEXT
:
1375 set
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1376 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1378 if (o
->flags
.dual_color_diffed_diffs
) {
1379 char c
= !len
? 0 : line
[0];
1382 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1384 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1386 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1388 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1389 OUTPUT_INDICATOR_CONTEXT
, line
, len
,
1390 flags
& (DIFF_SYMBOL_CONTENT_WS_MASK
), 0);
1392 case DIFF_SYMBOL_PLUS
:
1393 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1394 DIFF_SYMBOL_MOVED_LINE_ALT
|
1395 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1396 case DIFF_SYMBOL_MOVED_LINE
|
1397 DIFF_SYMBOL_MOVED_LINE_ALT
|
1398 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1399 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT_DIM
);
1401 case DIFF_SYMBOL_MOVED_LINE
|
1402 DIFF_SYMBOL_MOVED_LINE_ALT
:
1403 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT
);
1405 case DIFF_SYMBOL_MOVED_LINE
|
1406 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1407 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_DIM
);
1409 case DIFF_SYMBOL_MOVED_LINE
:
1410 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED
);
1413 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1415 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1416 if (!o
->flags
.dual_color_diffed_diffs
)
1419 char c
= !len
? 0 : line
[0];
1423 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_BOLD
);
1425 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1427 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_BOLD
);
1429 set
= diff_get_color_opt(o
, DIFF_CONTEXT_BOLD
);
1430 flags
&= ~DIFF_SYMBOL_CONTENT_WS_MASK
;
1432 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1433 OUTPUT_INDICATOR_NEW
, line
, len
,
1434 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
,
1435 flags
& DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
);
1437 case DIFF_SYMBOL_MINUS
:
1438 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1439 DIFF_SYMBOL_MOVED_LINE_ALT
|
1440 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1441 case DIFF_SYMBOL_MOVED_LINE
|
1442 DIFF_SYMBOL_MOVED_LINE_ALT
|
1443 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1444 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT_DIM
);
1446 case DIFF_SYMBOL_MOVED_LINE
|
1447 DIFF_SYMBOL_MOVED_LINE_ALT
:
1448 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT
);
1450 case DIFF_SYMBOL_MOVED_LINE
|
1451 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1452 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_DIM
);
1454 case DIFF_SYMBOL_MOVED_LINE
:
1455 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED
);
1458 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1460 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1461 if (!o
->flags
.dual_color_diffed_diffs
)
1464 char c
= !len
? 0 : line
[0];
1468 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_DIM
);
1470 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1472 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_DIM
);
1474 set
= diff_get_color_opt(o
, DIFF_CONTEXT_DIM
);
1476 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1477 OUTPUT_INDICATOR_OLD
, line
, len
,
1478 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
, 0);
1480 case DIFF_SYMBOL_WORDS_PORCELAIN
:
1481 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1482 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1483 emit_line(o
, context
, reset
, line
, len
);
1484 fputs("~\n", o
->file
);
1486 case DIFF_SYMBOL_WORDS
:
1487 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1488 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1490 * Skip the prefix character, if any. With
1491 * diff_suppress_blank_empty, there may be
1494 if (line
[0] != '\n') {
1498 emit_line(o
, context
, reset
, line
, len
);
1500 case DIFF_SYMBOL_FILEPAIR_PLUS
:
1501 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1502 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1503 fprintf(o
->file
, "%s%s+++ %s%s%s\n", diff_line_prefix(o
), meta
,
1505 strchr(line
, ' ') ? "\t" : "");
1507 case DIFF_SYMBOL_FILEPAIR_MINUS
:
1508 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1509 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1510 fprintf(o
->file
, "%s%s--- %s%s%s\n", diff_line_prefix(o
), meta
,
1512 strchr(line
, ' ') ? "\t" : "");
1514 case DIFF_SYMBOL_BINARY_FILES
:
1515 case DIFF_SYMBOL_HEADER
:
1516 fprintf(o
->file
, "%s", line
);
1518 case DIFF_SYMBOL_BINARY_DIFF_HEADER
:
1519 fprintf(o
->file
, "%sGIT binary patch\n", diff_line_prefix(o
));
1521 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
:
1522 fprintf(o
->file
, "%sdelta %s\n", diff_line_prefix(o
), line
);
1524 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
:
1525 fprintf(o
->file
, "%sliteral %s\n", diff_line_prefix(o
), line
);
1527 case DIFF_SYMBOL_BINARY_DIFF_FOOTER
:
1528 fputs(diff_line_prefix(o
), o
->file
);
1529 fputc('\n', o
->file
);
1531 case DIFF_SYMBOL_REWRITE_DIFF
:
1532 fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
1533 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1534 emit_line(o
, fraginfo
, reset
, line
, len
);
1536 case DIFF_SYMBOL_SUBMODULE_ADD
:
1537 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1538 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1539 emit_line(o
, set
, reset
, line
, len
);
1541 case DIFF_SYMBOL_SUBMODULE_DEL
:
1542 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1543 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1544 emit_line(o
, set
, reset
, line
, len
);
1546 case DIFF_SYMBOL_SUBMODULE_UNTRACKED
:
1547 fprintf(o
->file
, "%sSubmodule %s contains untracked content\n",
1548 diff_line_prefix(o
), line
);
1550 case DIFF_SYMBOL_SUBMODULE_MODIFIED
:
1551 fprintf(o
->file
, "%sSubmodule %s contains modified content\n",
1552 diff_line_prefix(o
), line
);
1554 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
:
1555 emit_line(o
, "", "", " 0 files changed\n",
1556 strlen(" 0 files changed\n"));
1558 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV
:
1559 emit_line(o
, "", "", " ...\n", strlen(" ...\n"));
1561 case DIFF_SYMBOL_WORD_DIFF
:
1562 fprintf(o
->file
, "%.*s", len
, line
);
1564 case DIFF_SYMBOL_STAT_SEP
:
1565 fputs(o
->stat_sep
, o
->file
);
1568 BUG("unknown diff symbol");
1570 strbuf_release(&sb
);
1573 static void emit_diff_symbol(struct diff_options
*o
, enum diff_symbol s
,
1574 const char *line
, int len
, unsigned flags
)
1576 struct emitted_diff_symbol e
= {line
, len
, flags
, 0, 0, s
};
1578 if (o
->emitted_symbols
)
1579 append_emitted_diff_symbol(o
, &e
);
1581 emit_diff_symbol_from_struct(o
, &e
);
1584 void diff_emit_submodule_del(struct diff_options
*o
, const char *line
)
1586 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_DEL
, line
, strlen(line
), 0);
1589 void diff_emit_submodule_add(struct diff_options
*o
, const char *line
)
1591 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ADD
, line
, strlen(line
), 0);
1594 void diff_emit_submodule_untracked(struct diff_options
*o
, const char *path
)
1596 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
1597 path
, strlen(path
), 0);
1600 void diff_emit_submodule_modified(struct diff_options
*o
, const char *path
)
1602 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_MODIFIED
,
1603 path
, strlen(path
), 0);
1606 void diff_emit_submodule_header(struct diff_options
*o
, const char *header
)
1608 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_HEADER
,
1609 header
, strlen(header
), 0);
1612 void diff_emit_submodule_error(struct diff_options
*o
, const char *err
)
1614 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ERROR
, err
, strlen(err
), 0);
1617 void diff_emit_submodule_pipethrough(struct diff_options
*o
,
1618 const char *line
, int len
)
1620 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
, line
, len
, 0);
1623 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
1625 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
1626 ecbdata
->blank_at_eof_in_preimage
&&
1627 ecbdata
->blank_at_eof_in_postimage
&&
1628 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
1629 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
1631 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
1634 static void emit_add_line(struct emit_callback
*ecbdata
,
1635 const char *line
, int len
)
1637 unsigned flags
= WSEH_NEW
| ecbdata
->ws_rule
;
1638 if (new_blank_line_at_eof(ecbdata
, line
, len
))
1639 flags
|= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
;
1641 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_PLUS
, line
, len
, flags
);
1644 static void emit_del_line(struct emit_callback
*ecbdata
,
1645 const char *line
, int len
)
1647 unsigned flags
= WSEH_OLD
| ecbdata
->ws_rule
;
1648 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_MINUS
, line
, len
, flags
);
1651 static void emit_context_line(struct emit_callback
*ecbdata
,
1652 const char *line
, int len
)
1654 unsigned flags
= WSEH_CONTEXT
| ecbdata
->ws_rule
;
1655 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_CONTEXT
, line
, len
, flags
);
1658 static void emit_hunk_header(struct emit_callback
*ecbdata
,
1659 const char *line
, int len
)
1661 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1662 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
1663 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
1664 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1665 const char *reverse
= ecbdata
->color_diff
? GIT_COLOR_REVERSE
: "";
1666 static const char atat
[2] = { '@', '@' };
1667 const char *cp
, *ep
;
1668 struct strbuf msgbuf
= STRBUF_INIT
;
1673 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1674 * it always is at least 10 bytes long.
1677 memcmp(line
, atat
, 2) ||
1678 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
1679 emit_diff_symbol(ecbdata
->opt
,
1680 DIFF_SYMBOL_CONTEXT_MARKER
, line
, len
, 0);
1683 ep
+= 2; /* skip over @@ */
1685 /* The hunk header in fraginfo color */
1686 if (ecbdata
->opt
->flags
.dual_color_diffed_diffs
)
1687 strbuf_addstr(&msgbuf
, reverse
);
1688 strbuf_addstr(&msgbuf
, frag
);
1689 if (ecbdata
->opt
->flags
.suppress_hunk_header_line_count
)
1690 strbuf_add(&msgbuf
, atat
, sizeof(atat
));
1692 strbuf_add(&msgbuf
, line
, ep
- line
);
1693 strbuf_addstr(&msgbuf
, reset
);
1699 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
1702 /* blank before the func header */
1703 for (cp
= ep
; ep
- line
< len
; ep
++)
1704 if (*ep
!= ' ' && *ep
!= '\t')
1707 strbuf_addstr(&msgbuf
, context
);
1708 strbuf_add(&msgbuf
, cp
, ep
- cp
);
1709 strbuf_addstr(&msgbuf
, reset
);
1712 if (ep
< line
+ len
) {
1713 strbuf_addstr(&msgbuf
, func
);
1714 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
1715 strbuf_addstr(&msgbuf
, reset
);
1718 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
1719 strbuf_complete_line(&msgbuf
);
1720 emit_diff_symbol(ecbdata
->opt
,
1721 DIFF_SYMBOL_CONTEXT_FRAGINFO
, msgbuf
.buf
, msgbuf
.len
, 0);
1722 strbuf_release(&msgbuf
);
1725 static struct diff_tempfile
*claim_diff_tempfile(void)
1728 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
1729 if (!diff_temp
[i
].name
)
1730 return diff_temp
+ i
;
1731 BUG("diff is failing to clean up its tempfiles");
1734 static void remove_tempfile(void)
1737 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
1738 if (is_tempfile_active(diff_temp
[i
].tempfile
))
1739 delete_tempfile(&diff_temp
[i
].tempfile
);
1740 diff_temp
[i
].name
= NULL
;
1744 static void add_line_count(struct strbuf
*out
, int count
)
1748 strbuf_addstr(out
, "0,0");
1751 strbuf_addstr(out
, "1");
1754 strbuf_addf(out
, "1,%d", count
);
1759 static void emit_rewrite_lines(struct emit_callback
*ecb
,
1760 int prefix
, const char *data
, int size
)
1762 const char *endp
= NULL
;
1767 endp
= memchr(data
, '\n', size
);
1768 len
= endp
? (endp
- data
+ 1) : size
;
1769 if (prefix
!= '+') {
1770 ecb
->lno_in_preimage
++;
1771 emit_del_line(ecb
, data
, len
);
1773 ecb
->lno_in_postimage
++;
1774 emit_add_line(ecb
, data
, len
);
1780 emit_diff_symbol(ecb
->opt
, DIFF_SYMBOL_NO_LF_EOF
, NULL
, 0, 0);
1783 static void emit_rewrite_diff(const char *name_a
,
1785 struct diff_filespec
*one
,
1786 struct diff_filespec
*two
,
1787 struct userdiff_driver
*textconv_one
,
1788 struct userdiff_driver
*textconv_two
,
1789 struct diff_options
*o
)
1792 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
1793 const char *a_prefix
, *b_prefix
;
1794 char *data_one
, *data_two
;
1795 size_t size_one
, size_two
;
1796 struct emit_callback ecbdata
;
1797 struct strbuf out
= STRBUF_INIT
;
1799 if (diff_mnemonic_prefix
&& o
->flags
.reverse_diff
) {
1800 a_prefix
= o
->b_prefix
;
1801 b_prefix
= o
->a_prefix
;
1803 a_prefix
= o
->a_prefix
;
1804 b_prefix
= o
->b_prefix
;
1807 name_a
+= (*name_a
== '/');
1808 name_b
+= (*name_b
== '/');
1810 strbuf_reset(&a_name
);
1811 strbuf_reset(&b_name
);
1812 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
1813 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
1815 size_one
= fill_textconv(o
->repo
, textconv_one
, one
, &data_one
);
1816 size_two
= fill_textconv(o
->repo
, textconv_two
, two
, &data_two
);
1818 memset(&ecbdata
, 0, sizeof(ecbdata
));
1819 ecbdata
.color_diff
= want_color(o
->use_color
);
1820 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
1822 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
1824 mf1
.ptr
= (char *)data_one
;
1825 mf2
.ptr
= (char *)data_two
;
1826 mf1
.size
= size_one
;
1827 mf2
.size
= size_two
;
1828 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1830 ecbdata
.lno_in_preimage
= 1;
1831 ecbdata
.lno_in_postimage
= 1;
1833 lc_a
= count_lines(data_one
, size_one
);
1834 lc_b
= count_lines(data_two
, size_two
);
1836 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1837 a_name
.buf
, a_name
.len
, 0);
1838 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1839 b_name
.buf
, b_name
.len
, 0);
1841 strbuf_addstr(&out
, "@@ -");
1842 if (!o
->irreversible_delete
)
1843 add_line_count(&out
, lc_a
);
1845 strbuf_addstr(&out
, "?,?");
1846 strbuf_addstr(&out
, " +");
1847 add_line_count(&out
, lc_b
);
1848 strbuf_addstr(&out
, " @@\n");
1849 emit_diff_symbol(o
, DIFF_SYMBOL_REWRITE_DIFF
, out
.buf
, out
.len
, 0);
1850 strbuf_release(&out
);
1852 if (lc_a
&& !o
->irreversible_delete
)
1853 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
1855 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
1857 free((char *)data_one
);
1859 free((char *)data_two
);
1862 struct diff_words_buffer
{
1864 unsigned long alloc
;
1865 struct diff_words_orig
{
1866 const char *begin
, *end
;
1868 int orig_nr
, orig_alloc
;
1871 static void diff_words_append(char *line
, unsigned long len
,
1872 struct diff_words_buffer
*buffer
)
1874 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
1877 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
1878 buffer
->text
.size
+= len
;
1879 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
1882 struct diff_words_style_elem
{
1885 const char *color
; /* NULL; filled in by the setup code if
1886 * color is enabled */
1889 struct diff_words_style
{
1890 enum diff_words_type type
;
1891 struct diff_words_style_elem new_word
, old_word
, ctx
;
1892 const char *newline
;
1895 static struct diff_words_style diff_words_styles
[] = {
1896 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1897 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1898 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
1901 struct diff_words_data
{
1902 struct diff_words_buffer minus
, plus
;
1903 const char *current_plus
;
1905 struct diff_options
*opt
;
1906 regex_t
*word_regex
;
1907 enum diff_words_type type
;
1908 struct diff_words_style
*style
;
1911 static int fn_out_diff_words_write_helper(struct diff_options
*o
,
1912 struct diff_words_style_elem
*st_el
,
1913 const char *newline
,
1914 size_t count
, const char *buf
)
1917 struct strbuf sb
= STRBUF_INIT
;
1920 char *p
= memchr(buf
, '\n', count
);
1922 strbuf_addstr(&sb
, diff_line_prefix(o
));
1925 const char *reset
= st_el
->color
&& *st_el
->color
?
1926 GIT_COLOR_RESET
: NULL
;
1927 if (st_el
->color
&& *st_el
->color
)
1928 strbuf_addstr(&sb
, st_el
->color
);
1929 strbuf_addstr(&sb
, st_el
->prefix
);
1930 strbuf_add(&sb
, buf
, p
? p
- buf
: count
);
1931 strbuf_addstr(&sb
, st_el
->suffix
);
1933 strbuf_addstr(&sb
, reset
);
1938 strbuf_addstr(&sb
, newline
);
1939 count
-= p
+ 1 - buf
;
1943 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1951 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1953 strbuf_release(&sb
);
1958 * '--color-words' algorithm can be described as:
1960 * 1. collect the minus/plus lines of a diff hunk, divided into
1961 * minus-lines and plus-lines;
1963 * 2. break both minus-lines and plus-lines into words and
1964 * place them into two mmfile_t with one word for each line;
1966 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1968 * And for the common parts of the both file, we output the plus side text.
1969 * diff_words->current_plus is used to trace the current position of the plus file
1970 * which printed. diff_words->last_minus is used to trace the last minus word
1973 * For '--graph' to work with '--color-words', we need to output the graph prefix
1974 * on each line of color words output. Generally, there are two conditions on
1975 * which we should output the prefix.
1977 * 1. diff_words->last_minus == 0 &&
1978 * diff_words->current_plus == diff_words->plus.text.ptr
1980 * that is: the plus text must start as a new line, and if there is no minus
1981 * word printed, a graph prefix must be printed.
1983 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1984 * *(diff_words->current_plus - 1) == '\n'
1986 * that is: a graph prefix must be printed following a '\n'
1988 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
1990 if ((diff_words
->last_minus
== 0 &&
1991 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
1992 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
1993 *(diff_words
->current_plus
- 1) == '\n')) {
2000 static void fn_out_diff_words_aux(void *priv
,
2001 long minus_first
, long minus_len
,
2002 long plus_first
, long plus_len
,
2003 const char *func
, long funclen
)
2005 struct diff_words_data
*diff_words
= priv
;
2006 struct diff_words_style
*style
= diff_words
->style
;
2007 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
2008 struct diff_options
*opt
= diff_words
->opt
;
2009 const char *line_prefix
;
2012 line_prefix
= diff_line_prefix(opt
);
2014 /* POSIX requires that first be decremented by one if len == 0... */
2016 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
2018 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
2020 minus_begin
= minus_end
=
2021 diff_words
->minus
.orig
[minus_first
].end
;
2024 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
2025 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
2027 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
2029 if (color_words_output_graph_prefix(diff_words
)) {
2030 fputs(line_prefix
, diff_words
->opt
->file
);
2032 if (diff_words
->current_plus
!= plus_begin
) {
2033 fn_out_diff_words_write_helper(diff_words
->opt
,
2034 &style
->ctx
, style
->newline
,
2035 plus_begin
- diff_words
->current_plus
,
2036 diff_words
->current_plus
);
2038 if (minus_begin
!= minus_end
) {
2039 fn_out_diff_words_write_helper(diff_words
->opt
,
2040 &style
->old_word
, style
->newline
,
2041 minus_end
- minus_begin
, minus_begin
);
2043 if (plus_begin
!= plus_end
) {
2044 fn_out_diff_words_write_helper(diff_words
->opt
,
2045 &style
->new_word
, style
->newline
,
2046 plus_end
- plus_begin
, plus_begin
);
2049 diff_words
->current_plus
= plus_end
;
2050 diff_words
->last_minus
= minus_first
;
2053 /* This function starts looking at *begin, and returns 0 iff a word was found. */
2054 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
2055 int *begin
, int *end
)
2057 while (word_regex
&& *begin
< buffer
->size
) {
2058 regmatch_t match
[1];
2059 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
2060 buffer
->size
- *begin
, 1, match
, 0)) {
2061 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
2062 '\n', match
[0].rm_eo
- match
[0].rm_so
);
2063 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
2064 *begin
+= match
[0].rm_so
;
2068 return *begin
> *end
;
2074 /* find the next word */
2075 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
2077 if (*begin
>= buffer
->size
)
2080 /* find the end of the word */
2082 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
2089 * This function splits the words in buffer->text, stores the list with
2090 * newline separator into out, and saves the offsets of the original words
2093 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
2094 regex_t
*word_regex
)
2102 /* fake an empty "0th" word */
2103 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
2104 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
2105 buffer
->orig_nr
= 1;
2107 for (i
= 0; i
< buffer
->text
.size
; i
++) {
2108 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
2111 /* store original boundaries */
2112 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
2113 buffer
->orig_alloc
);
2114 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
2115 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
2118 /* store one word */
2119 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
2120 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
2121 out
->ptr
[out
->size
+ j
- i
] = '\n';
2122 out
->size
+= j
- i
+ 1;
2128 /* this executes the word diff on the accumulated buffers */
2129 static void diff_words_show(struct diff_words_data
*diff_words
)
2133 mmfile_t minus
, plus
;
2134 struct diff_words_style
*style
= diff_words
->style
;
2136 struct diff_options
*opt
= diff_words
->opt
;
2137 const char *line_prefix
;
2140 line_prefix
= diff_line_prefix(opt
);
2142 /* special case: only removal */
2143 if (!diff_words
->plus
.text
.size
) {
2144 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2145 line_prefix
, strlen(line_prefix
), 0);
2146 fn_out_diff_words_write_helper(diff_words
->opt
,
2147 &style
->old_word
, style
->newline
,
2148 diff_words
->minus
.text
.size
,
2149 diff_words
->minus
.text
.ptr
);
2150 diff_words
->minus
.text
.size
= 0;
2154 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
2155 diff_words
->last_minus
= 0;
2157 memset(&xpp
, 0, sizeof(xpp
));
2158 memset(&xecfg
, 0, sizeof(xecfg
));
2159 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
2160 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
2162 /* as only the hunk header will be parsed, we need a 0-context */
2164 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, NULL
,
2165 diff_words
, &xpp
, &xecfg
))
2166 die("unable to generate word diff");
2169 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
2170 diff_words
->plus
.text
.size
) {
2171 if (color_words_output_graph_prefix(diff_words
))
2172 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2173 line_prefix
, strlen(line_prefix
), 0);
2174 fn_out_diff_words_write_helper(diff_words
->opt
,
2175 &style
->ctx
, style
->newline
,
2176 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
2177 - diff_words
->current_plus
, diff_words
->current_plus
);
2179 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
2182 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
2183 static void diff_words_flush(struct emit_callback
*ecbdata
)
2185 struct diff_options
*wo
= ecbdata
->diff_words
->opt
;
2187 if (ecbdata
->diff_words
->minus
.text
.size
||
2188 ecbdata
->diff_words
->plus
.text
.size
)
2189 diff_words_show(ecbdata
->diff_words
);
2191 if (wo
->emitted_symbols
) {
2192 struct diff_options
*o
= ecbdata
->opt
;
2193 struct emitted_diff_symbols
*wol
= wo
->emitted_symbols
;
2198 * Instead of appending each, concat all words to a line?
2200 for (i
= 0; i
< wol
->nr
; i
++)
2201 append_emitted_diff_symbol(o
, &wol
->buf
[i
]);
2203 for (i
= 0; i
< wol
->nr
; i
++)
2204 free((void *)wol
->buf
[i
].line
);
2210 static void diff_filespec_load_driver(struct diff_filespec
*one
,
2211 struct index_state
*istate
)
2213 /* Use already-loaded driver */
2217 if (S_ISREG(one
->mode
))
2218 one
->driver
= userdiff_find_by_path(istate
, one
->path
);
2220 /* Fallback to default settings */
2222 one
->driver
= userdiff_find_by_name("default");
2225 static const char *userdiff_word_regex(struct diff_filespec
*one
,
2226 struct index_state
*istate
)
2228 diff_filespec_load_driver(one
, istate
);
2229 return one
->driver
->word_regex
;
2232 static void init_diff_words_data(struct emit_callback
*ecbdata
,
2233 struct diff_options
*orig_opts
,
2234 struct diff_filespec
*one
,
2235 struct diff_filespec
*two
)
2238 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
2239 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
2241 CALLOC_ARRAY(ecbdata
->diff_words
, 1);
2242 ecbdata
->diff_words
->type
= o
->word_diff
;
2243 ecbdata
->diff_words
->opt
= o
;
2245 if (orig_opts
->emitted_symbols
)
2246 CALLOC_ARRAY(o
->emitted_symbols
, 1);
2249 o
->word_regex
= userdiff_word_regex(one
, o
->repo
->index
);
2251 o
->word_regex
= userdiff_word_regex(two
, o
->repo
->index
);
2253 o
->word_regex
= diff_word_regex_cfg
;
2254 if (o
->word_regex
) {
2255 ecbdata
->diff_words
->word_regex
= (regex_t
*)
2256 xmalloc(sizeof(regex_t
));
2257 if (regcomp(ecbdata
->diff_words
->word_regex
,
2259 REG_EXTENDED
| REG_NEWLINE
))
2260 die("invalid regular expression: %s",
2263 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
2264 if (o
->word_diff
== diff_words_styles
[i
].type
) {
2265 ecbdata
->diff_words
->style
=
2266 &diff_words_styles
[i
];
2270 if (want_color(o
->use_color
)) {
2271 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
2272 st
->old_word
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2273 st
->new_word
.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2274 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
2278 static void free_diff_words_data(struct emit_callback
*ecbdata
)
2280 if (ecbdata
->diff_words
) {
2281 diff_words_flush(ecbdata
);
2282 free (ecbdata
->diff_words
->opt
->emitted_symbols
);
2283 free (ecbdata
->diff_words
->opt
);
2284 free (ecbdata
->diff_words
->minus
.text
.ptr
);
2285 free (ecbdata
->diff_words
->minus
.orig
);
2286 free (ecbdata
->diff_words
->plus
.text
.ptr
);
2287 free (ecbdata
->diff_words
->plus
.orig
);
2288 if (ecbdata
->diff_words
->word_regex
) {
2289 regfree(ecbdata
->diff_words
->word_regex
);
2290 free(ecbdata
->diff_words
->word_regex
);
2292 FREE_AND_NULL(ecbdata
->diff_words
);
2296 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
2298 if (want_color(diff_use_color
))
2299 return diff_colors
[ix
];
2303 const char *diff_line_prefix(struct diff_options
*opt
)
2305 struct strbuf
*msgbuf
;
2306 if (!opt
->output_prefix
)
2309 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
2313 static unsigned long sane_truncate_line(char *line
, unsigned long len
)
2316 unsigned long allot
;
2322 (void) utf8_width(&cp
, &l
);
2324 break; /* truncated in the middle? */
2329 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
2332 ecbdata
->lno_in_preimage
= 0;
2333 ecbdata
->lno_in_postimage
= 0;
2334 p
= strchr(line
, '-');
2336 return; /* cannot happen */
2337 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
2340 return; /* cannot happen */
2341 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
2344 static int fn_out_consume(void *priv
, char *line
, unsigned long len
)
2346 struct emit_callback
*ecbdata
= priv
;
2347 struct diff_options
*o
= ecbdata
->opt
;
2349 o
->found_changes
= 1;
2351 if (ecbdata
->header
) {
2352 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
2353 ecbdata
->header
->buf
, ecbdata
->header
->len
, 0);
2354 strbuf_reset(ecbdata
->header
);
2355 ecbdata
->header
= NULL
;
2358 if (ecbdata
->label_path
[0]) {
2359 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
2360 ecbdata
->label_path
[0],
2361 strlen(ecbdata
->label_path
[0]), 0);
2362 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
2363 ecbdata
->label_path
[1],
2364 strlen(ecbdata
->label_path
[1]), 0);
2365 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
2368 if (diff_suppress_blank_empty
2369 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
2374 if (line
[0] == '@') {
2375 if (ecbdata
->diff_words
)
2376 diff_words_flush(ecbdata
);
2377 len
= sane_truncate_line(line
, len
);
2378 find_lno(line
, ecbdata
);
2379 emit_hunk_header(ecbdata
, line
, len
);
2383 if (ecbdata
->diff_words
) {
2384 enum diff_symbol s
=
2385 ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
?
2386 DIFF_SYMBOL_WORDS_PORCELAIN
: DIFF_SYMBOL_WORDS
;
2387 if (line
[0] == '-') {
2388 diff_words_append(line
, len
,
2389 &ecbdata
->diff_words
->minus
);
2391 } else if (line
[0] == '+') {
2392 diff_words_append(line
, len
,
2393 &ecbdata
->diff_words
->plus
);
2395 } else if (starts_with(line
, "\\ ")) {
2397 * Eat the "no newline at eof" marker as if we
2398 * saw a "+" or "-" line with nothing on it,
2399 * and return without diff_words_flush() to
2400 * defer processing. If this is the end of
2401 * preimage, more "+" lines may come after it.
2405 diff_words_flush(ecbdata
);
2406 emit_diff_symbol(o
, s
, line
, len
, 0);
2412 ecbdata
->lno_in_postimage
++;
2413 emit_add_line(ecbdata
, line
+ 1, len
- 1);
2416 ecbdata
->lno_in_preimage
++;
2417 emit_del_line(ecbdata
, line
+ 1, len
- 1);
2420 ecbdata
->lno_in_postimage
++;
2421 ecbdata
->lno_in_preimage
++;
2422 emit_context_line(ecbdata
, line
+ 1, len
- 1);
2425 /* incomplete line at the end */
2426 ecbdata
->lno_in_preimage
++;
2427 emit_diff_symbol(o
, DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
2434 static void pprint_rename(struct strbuf
*name
, const char *a
, const char *b
)
2436 const char *old_name
= a
;
2437 const char *new_name
= b
;
2438 int pfx_length
, sfx_length
;
2439 int pfx_adjust_for_slash
;
2440 int len_a
= strlen(a
);
2441 int len_b
= strlen(b
);
2442 int a_midlen
, b_midlen
;
2443 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
2444 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
2446 if (qlen_a
|| qlen_b
) {
2447 quote_c_style(a
, name
, NULL
, 0);
2448 strbuf_addstr(name
, " => ");
2449 quote_c_style(b
, name
, NULL
, 0);
2453 /* Find common prefix */
2455 while (*old_name
&& *new_name
&& *old_name
== *new_name
) {
2456 if (*old_name
== '/')
2457 pfx_length
= old_name
- a
+ 1;
2462 /* Find common suffix */
2463 old_name
= a
+ len_a
;
2464 new_name
= b
+ len_b
;
2467 * If there is a common prefix, it must end in a slash. In
2468 * that case we let this loop run 1 into the prefix to see the
2471 * If there is no common prefix, we cannot do this as it would
2472 * underrun the input strings.
2474 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
2475 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old_name
&&
2476 b
+ pfx_length
- pfx_adjust_for_slash
<= new_name
&&
2477 *old_name
== *new_name
) {
2478 if (*old_name
== '/')
2479 sfx_length
= len_a
- (old_name
- a
);
2485 * pfx{mid-a => mid-b}sfx
2486 * {pfx-a => pfx-b}sfx
2487 * pfx{sfx-a => sfx-b}
2490 a_midlen
= len_a
- pfx_length
- sfx_length
;
2491 b_midlen
= len_b
- pfx_length
- sfx_length
;
2497 strbuf_grow(name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
2498 if (pfx_length
+ sfx_length
) {
2499 strbuf_add(name
, a
, pfx_length
);
2500 strbuf_addch(name
, '{');
2502 strbuf_add(name
, a
+ pfx_length
, a_midlen
);
2503 strbuf_addstr(name
, " => ");
2504 strbuf_add(name
, b
+ pfx_length
, b_midlen
);
2505 if (pfx_length
+ sfx_length
) {
2506 strbuf_addch(name
, '}');
2507 strbuf_add(name
, a
+ len_a
- sfx_length
, sfx_length
);
2511 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
2515 struct diffstat_file
*x
;
2517 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
2518 diffstat
->files
[diffstat
->nr
++] = x
;
2520 x
->from_name
= xstrdup(name_a
);
2521 x
->name
= xstrdup(name_b
);
2525 x
->from_name
= NULL
;
2526 x
->name
= xstrdup(name_a
);
2531 static int diffstat_consume(void *priv
, char *line
, unsigned long len
)
2533 struct diffstat_t
*diffstat
= priv
;
2534 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
2538 else if (line
[0] == '-')
2543 const char mime_boundary_leader
[] = "------------";
2545 static int scale_linear(int it
, int width
, int max_change
)
2550 * make sure that at least one '-' or '+' is printed if
2551 * there is any change to this path. The easiest way is to
2552 * scale linearly as if the allotted width is one column shorter
2553 * than it is, and then add 1 to the result.
2555 return 1 + (it
* (width
- 1) / max_change
);
2558 static void show_graph(struct strbuf
*out
, char ch
, int cnt
,
2559 const char *set
, const char *reset
)
2563 strbuf_addstr(out
, set
);
2564 strbuf_addchars(out
, ch
, cnt
);
2565 strbuf_addstr(out
, reset
);
2568 static void fill_print_name(struct diffstat_file
*file
)
2570 struct strbuf pname
= STRBUF_INIT
;
2572 if (file
->print_name
)
2575 if (file
->is_renamed
)
2576 pprint_rename(&pname
, file
->from_name
, file
->name
);
2578 quote_c_style(file
->name
, &pname
, NULL
, 0);
2581 strbuf_addf(&pname
, " (%s)", file
->comments
);
2583 file
->print_name
= strbuf_detach(&pname
, NULL
);
2586 static void print_stat_summary_inserts_deletes(struct diff_options
*options
,
2587 int files
, int insertions
, int deletions
)
2589 struct strbuf sb
= STRBUF_INIT
;
2592 assert(insertions
== 0 && deletions
== 0);
2593 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
2599 (files
== 1) ? " %d file changed" : " %d files changed",
2603 * For binary diff, the caller may want to print "x files
2604 * changed" with insertions == 0 && deletions == 0.
2606 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2607 * is probably less confusing (i.e skip over "2 files changed
2608 * but nothing about added/removed lines? Is this a bug in Git?").
2610 if (insertions
|| deletions
== 0) {
2612 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2616 if (deletions
|| insertions
== 0) {
2618 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2621 strbuf_addch(&sb
, '\n');
2622 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
2624 strbuf_release(&sb
);
2627 void print_stat_summary(FILE *fp
, int files
,
2628 int insertions
, int deletions
)
2630 struct diff_options o
;
2631 memset(&o
, 0, sizeof(o
));
2634 print_stat_summary_inserts_deletes(&o
, files
, insertions
, deletions
);
2637 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
2639 int i
, len
, add
, del
, adds
= 0, dels
= 0;
2640 uintmax_t max_change
= 0, max_len
= 0;
2641 int total_files
= data
->nr
, count
;
2642 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
2643 const char *reset
, *add_c
, *del_c
;
2644 int extra_shown
= 0;
2645 const char *line_prefix
= diff_line_prefix(options
);
2646 struct strbuf out
= STRBUF_INIT
;
2651 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
2653 reset
= diff_get_color_opt(options
, DIFF_RESET
);
2654 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
2655 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
2658 * Find the longest filename and max number of changes
2660 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
2661 struct diffstat_file
*file
= data
->files
[i
];
2662 uintmax_t change
= file
->added
+ file
->deleted
;
2664 if (!file
->is_interesting
&& (change
== 0)) {
2665 count
++; /* not shown == room for one more */
2668 fill_print_name(file
);
2669 len
= strlen(file
->print_name
);
2673 if (file
->is_unmerged
) {
2674 /* "Unmerged" is 8 characters */
2675 bin_width
= bin_width
< 8 ? 8 : bin_width
;
2678 if (file
->is_binary
) {
2679 /* "Bin XXX -> YYY bytes" */
2680 int w
= 14 + decimal_width(file
->added
)
2681 + decimal_width(file
->deleted
);
2682 bin_width
= bin_width
< w
? w
: bin_width
;
2683 /* Display change counts aligned with "Bin" */
2688 if (max_change
< change
)
2689 max_change
= change
;
2691 count
= i
; /* where we can stop scanning in data->files[] */
2694 * We have width = stat_width or term_columns() columns total.
2695 * We want a maximum of min(max_len, stat_name_width) for the name part.
2696 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2697 * We also need 1 for " " and 4 + decimal_width(max_change)
2698 * for " | NNNN " and one the empty column at the end, altogether
2699 * 6 + decimal_width(max_change).
2701 * If there's not enough space, we will use the smaller of
2702 * stat_name_width (if set) and 5/8*width for the filename,
2703 * and the rest for constant elements + graph part, but no more
2704 * than stat_graph_width for the graph part.
2705 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2706 * for the standard terminal size).
2708 * In other words: stat_width limits the maximum width, and
2709 * stat_name_width fixes the maximum width of the filename,
2710 * and is also used to divide available columns if there
2713 * Binary files are displayed with "Bin XXX -> YYY bytes"
2714 * instead of the change count and graph. This part is treated
2715 * similarly to the graph part, except that it is not
2716 * "scaled". If total width is too small to accommodate the
2717 * guaranteed minimum width of the filename part and the
2718 * separators and this message, this message will "overflow"
2719 * making the line longer than the maximum width.
2722 if (options
->stat_width
== -1)
2723 width
= term_columns() - strlen(line_prefix
);
2725 width
= options
->stat_width
? options
->stat_width
: 80;
2726 number_width
= decimal_width(max_change
) > number_width
?
2727 decimal_width(max_change
) : number_width
;
2729 if (options
->stat_graph_width
== -1)
2730 options
->stat_graph_width
= diff_stat_graph_width
;
2733 * Guarantee 3/8*16==6 for the graph part
2734 * and 5/8*16==10 for the filename part
2736 if (width
< 16 + 6 + number_width
)
2737 width
= 16 + 6 + number_width
;
2740 * First assign sizes that are wanted, ignoring available width.
2741 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2742 * starting from "XXX" should fit in graph_width.
2744 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
2745 if (options
->stat_graph_width
&&
2746 options
->stat_graph_width
< graph_width
)
2747 graph_width
= options
->stat_graph_width
;
2749 name_width
= (options
->stat_name_width
> 0 &&
2750 options
->stat_name_width
< max_len
) ?
2751 options
->stat_name_width
: max_len
;
2754 * Adjust adjustable widths not to exceed maximum width
2756 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
2757 if (graph_width
> width
* 3/8 - number_width
- 6) {
2758 graph_width
= width
* 3/8 - number_width
- 6;
2759 if (graph_width
< 6)
2763 if (options
->stat_graph_width
&&
2764 graph_width
> options
->stat_graph_width
)
2765 graph_width
= options
->stat_graph_width
;
2766 if (name_width
> width
- number_width
- 6 - graph_width
)
2767 name_width
= width
- number_width
- 6 - graph_width
;
2769 graph_width
= width
- number_width
- 6 - name_width
;
2773 * From here name_width is the width of the name area,
2774 * and graph_width is the width of the graph area.
2775 * max_change is used to scale graph properly.
2777 for (i
= 0; i
< count
; i
++) {
2778 const char *prefix
= "";
2779 struct diffstat_file
*file
= data
->files
[i
];
2780 char *name
= file
->print_name
;
2781 uintmax_t added
= file
->added
;
2782 uintmax_t deleted
= file
->deleted
;
2785 if (!file
->is_interesting
&& (added
+ deleted
== 0))
2789 * "scale" the filename
2792 name_len
= strlen(name
);
2793 if (name_width
< name_len
) {
2797 name
+= name_len
- len
;
2798 slash
= strchr(name
, '/');
2803 if (file
->is_binary
) {
2804 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2805 strbuf_addf(&out
, " %*s", number_width
, "Bin");
2806 if (!added
&& !deleted
) {
2807 strbuf_addch(&out
, '\n');
2808 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2809 out
.buf
, out
.len
, 0);
2813 strbuf_addf(&out
, " %s%"PRIuMAX
"%s",
2814 del_c
, deleted
, reset
);
2815 strbuf_addstr(&out
, " -> ");
2816 strbuf_addf(&out
, "%s%"PRIuMAX
"%s",
2817 add_c
, added
, reset
);
2818 strbuf_addstr(&out
, " bytes\n");
2819 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2820 out
.buf
, out
.len
, 0);
2824 else if (file
->is_unmerged
) {
2825 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2826 strbuf_addstr(&out
, " Unmerged\n");
2827 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2828 out
.buf
, out
.len
, 0);
2834 * scale the add/delete
2839 if (graph_width
<= max_change
) {
2840 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
2841 if (total
< 2 && add
&& del
)
2842 /* width >= 2 due to the sanity check */
2845 add
= scale_linear(add
, graph_width
, max_change
);
2848 del
= scale_linear(del
, graph_width
, max_change
);
2852 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2853 strbuf_addf(&out
, " %*"PRIuMAX
"%s",
2854 number_width
, added
+ deleted
,
2855 added
+ deleted
? " " : "");
2856 show_graph(&out
, '+', add
, add_c
, reset
);
2857 show_graph(&out
, '-', del
, del_c
, reset
);
2858 strbuf_addch(&out
, '\n');
2859 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2860 out
.buf
, out
.len
, 0);
2864 for (i
= 0; i
< data
->nr
; i
++) {
2865 struct diffstat_file
*file
= data
->files
[i
];
2866 uintmax_t added
= file
->added
;
2867 uintmax_t deleted
= file
->deleted
;
2869 if (file
->is_unmerged
||
2870 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
2875 if (!file
->is_binary
) {
2882 emit_diff_symbol(options
,
2883 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
2888 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2889 strbuf_release(&out
);
2892 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
2894 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
2899 for (i
= 0; i
< data
->nr
; i
++) {
2900 int added
= data
->files
[i
]->added
;
2901 int deleted
= data
->files
[i
]->deleted
;
2903 if (data
->files
[i
]->is_unmerged
||
2904 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
2906 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
2911 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2914 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
2921 for (i
= 0; i
< data
->nr
; i
++) {
2922 struct diffstat_file
*file
= data
->files
[i
];
2924 fprintf(options
->file
, "%s", diff_line_prefix(options
));
2926 if (file
->is_binary
)
2927 fprintf(options
->file
, "-\t-\t");
2929 fprintf(options
->file
,
2930 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
2931 file
->added
, file
->deleted
);
2932 if (options
->line_termination
) {
2933 fill_print_name(file
);
2934 if (!file
->is_renamed
)
2935 write_name_quoted(file
->name
, options
->file
,
2936 options
->line_termination
);
2938 fputs(file
->print_name
, options
->file
);
2939 putc(options
->line_termination
, options
->file
);
2942 if (file
->is_renamed
) {
2943 putc('\0', options
->file
);
2944 write_name_quoted(file
->from_name
, options
->file
, '\0');
2946 write_name_quoted(file
->name
, options
->file
, '\0');
2951 struct dirstat_file
{
2953 unsigned long changed
;
2956 struct dirstat_dir
{
2957 struct dirstat_file
*files
;
2958 int alloc
, nr
, permille
, cumulative
;
2961 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
2962 unsigned long changed
, const char *base
, int baselen
)
2964 unsigned long sum_changes
= 0;
2965 unsigned int sources
= 0;
2966 const char *line_prefix
= diff_line_prefix(opt
);
2969 struct dirstat_file
*f
= dir
->files
;
2970 int namelen
= strlen(f
->name
);
2971 unsigned long changes
;
2974 if (namelen
< baselen
)
2976 if (memcmp(f
->name
, base
, baselen
))
2978 slash
= strchr(f
->name
+ baselen
, '/');
2980 int newbaselen
= slash
+ 1 - f
->name
;
2981 changes
= gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
2984 changes
= f
->changed
;
2989 sum_changes
+= changes
;
2993 * We don't report dirstat's for
2995 * - or cases where everything came from a single directory
2996 * under this directory (sources == 1).
2998 if (baselen
&& sources
!= 1) {
3000 int permille
= sum_changes
* 1000 / changed
;
3001 if (permille
>= dir
->permille
) {
3002 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
3003 permille
/ 10, permille
% 10, baselen
, base
);
3004 if (!dir
->cumulative
)
3012 static int dirstat_compare(const void *_a
, const void *_b
)
3014 const struct dirstat_file
*a
= _a
;
3015 const struct dirstat_file
*b
= _b
;
3016 return strcmp(a
->name
, b
->name
);
3019 static void show_dirstat(struct diff_options
*options
)
3022 unsigned long changed
;
3023 struct dirstat_dir dir
;
3024 struct diff_queue_struct
*q
= &diff_queued_diff
;
3029 dir
.permille
= options
->dirstat_permille
;
3030 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3033 for (i
= 0; i
< q
->nr
; i
++) {
3034 struct diff_filepair
*p
= q
->queue
[i
];
3036 unsigned long copied
, added
, damage
;
3037 struct diff_populate_filespec_options dpf_options
= {
3038 .check_size_only
= 1,
3041 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
3043 if (p
->one
->oid_valid
&& p
->two
->oid_valid
&&
3044 oideq(&p
->one
->oid
, &p
->two
->oid
)) {
3046 * The SHA1 has not changed, so pre-/post-content is
3047 * identical. We can therefore skip looking at the
3048 * file contents altogether.
3054 if (options
->flags
.dirstat_by_file
) {
3056 * In --dirstat-by-file mode, we don't really need to
3057 * look at the actual file contents at all.
3058 * The fact that the SHA1 changed is enough for us to
3059 * add this file to the list of results
3060 * (with each file contributing equal damage).
3066 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
3067 diff_populate_filespec(options
->repo
, p
->one
, NULL
);
3068 diff_populate_filespec(options
->repo
, p
->two
, NULL
);
3069 diffcore_count_changes(options
->repo
,
3070 p
->one
, p
->two
, NULL
, NULL
,
3072 diff_free_filespec_data(p
->one
);
3073 diff_free_filespec_data(p
->two
);
3074 } else if (DIFF_FILE_VALID(p
->one
)) {
3075 diff_populate_filespec(options
->repo
, p
->one
, &dpf_options
);
3077 diff_free_filespec_data(p
->one
);
3078 } else if (DIFF_FILE_VALID(p
->two
)) {
3079 diff_populate_filespec(options
->repo
, p
->two
, &dpf_options
);
3081 added
= p
->two
->size
;
3082 diff_free_filespec_data(p
->two
);
3087 * Original minus copied is the removed material,
3088 * added is the new material. They are both damages
3089 * made to the preimage.
3090 * If the resulting damage is zero, we know that
3091 * diffcore_count_changes() considers the two entries to
3092 * be identical, but since the oid changed, we
3093 * know that there must have been _some_ kind of change,
3094 * so we force all entries to have damage > 0.
3096 damage
= (p
->one
->size
- copied
) + added
;
3101 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3102 dir
.files
[dir
.nr
].name
= name
;
3103 dir
.files
[dir
.nr
].changed
= damage
;
3108 /* This can happen even with many files, if everything was renames */
3112 /* Show all directories with more than x% of the changes */
3113 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
3114 gather_dirstat(options
, &dir
, changed
, "", 0);
3117 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
3120 unsigned long changed
;
3121 struct dirstat_dir dir
;
3129 dir
.permille
= options
->dirstat_permille
;
3130 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3133 for (i
= 0; i
< data
->nr
; i
++) {
3134 struct diffstat_file
*file
= data
->files
[i
];
3135 unsigned long damage
= file
->added
+ file
->deleted
;
3136 if (file
->is_binary
)
3138 * binary files counts bytes, not lines. Must find some
3139 * way to normalize binary bytes vs. textual lines.
3140 * The following heuristic assumes that there are 64
3142 * This is stupid and ugly, but very cheap...
3144 damage
= DIV_ROUND_UP(damage
, 64);
3145 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3146 dir
.files
[dir
.nr
].name
= file
->name
;
3147 dir
.files
[dir
.nr
].changed
= damage
;
3152 /* This can happen even with many files, if everything was renames */
3156 /* Show all directories with more than x% of the changes */
3157 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
3158 gather_dirstat(options
, &dir
, changed
, "", 0);
3161 static void free_diffstat_file(struct diffstat_file
*f
)
3163 free(f
->print_name
);
3169 void free_diffstat_info(struct diffstat_t
*diffstat
)
3172 for (i
= 0; i
< diffstat
->nr
; i
++)
3173 free_diffstat_file(diffstat
->files
[i
]);
3174 free(diffstat
->files
);
3177 struct checkdiff_t
{
3178 const char *filename
;
3180 int conflict_marker_size
;
3181 struct diff_options
*o
;
3186 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
3191 if (len
< marker_size
+ 1)
3193 firstchar
= line
[0];
3194 switch (firstchar
) {
3195 case '=': case '>': case '<': case '|':
3200 for (cnt
= 1; cnt
< marker_size
; cnt
++)
3201 if (line
[cnt
] != firstchar
)
3203 /* line[1] through line[marker_size-1] are same as firstchar */
3204 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
3209 static void checkdiff_consume_hunk(void *priv
,
3210 long ob
, long on
, long nb
, long nn
,
3211 const char *func
, long funclen
)
3214 struct checkdiff_t
*data
= priv
;
3215 data
->lineno
= nb
- 1;
3218 static int checkdiff_consume(void *priv
, char *line
, unsigned long len
)
3220 struct checkdiff_t
*data
= priv
;
3221 int marker_size
= data
->conflict_marker_size
;
3222 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
3223 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
3224 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
3226 const char *line_prefix
;
3229 line_prefix
= diff_line_prefix(data
->o
);
3231 if (line
[0] == '+') {
3234 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
3236 fprintf(data
->o
->file
,
3237 "%s%s:%d: leftover conflict marker\n",
3238 line_prefix
, data
->filename
, data
->lineno
);
3240 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
3243 data
->status
|= bad
;
3244 err
= whitespace_error_string(bad
);
3245 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
3246 line_prefix
, data
->filename
, data
->lineno
, err
);
3248 emit_line(data
->o
, set
, reset
, line
, 1);
3249 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
3250 data
->o
->file
, set
, reset
, ws
);
3251 } else if (line
[0] == ' ') {
3257 static unsigned char *deflate_it(char *data
,
3259 unsigned long *result_size
)
3262 unsigned char *deflated
;
3265 git_deflate_init(&stream
, zlib_compression_level
);
3266 bound
= git_deflate_bound(&stream
, size
);
3267 deflated
= xmalloc(bound
);
3268 stream
.next_out
= deflated
;
3269 stream
.avail_out
= bound
;
3271 stream
.next_in
= (unsigned char *)data
;
3272 stream
.avail_in
= size
;
3273 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
3275 git_deflate_end(&stream
);
3276 *result_size
= stream
.total_out
;
3280 static void emit_binary_diff_body(struct diff_options
*o
,
3281 mmfile_t
*one
, mmfile_t
*two
)
3287 unsigned long orig_size
;
3288 unsigned long delta_size
;
3289 unsigned long deflate_size
;
3290 unsigned long data_size
;
3292 /* We could do deflated delta, or we could do just deflated two,
3293 * whichever is smaller.
3296 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
3297 if (one
->size
&& two
->size
) {
3298 delta
= diff_delta(one
->ptr
, one
->size
,
3299 two
->ptr
, two
->size
,
3300 &delta_size
, deflate_size
);
3302 void *to_free
= delta
;
3303 orig_size
= delta_size
;
3304 delta
= deflate_it(delta
, delta_size
, &delta_size
);
3309 if (delta
&& delta_size
< deflate_size
) {
3310 char *s
= xstrfmt("%"PRIuMAX
, (uintmax_t)orig_size
);
3311 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
3316 data_size
= delta_size
;
3318 char *s
= xstrfmt("%lu", two
->size
);
3319 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
3324 data_size
= deflate_size
;
3327 /* emit data encoded in base85 */
3331 int bytes
= (52 < data_size
) ? 52 : data_size
;
3335 line
[0] = bytes
+ 'A' - 1;
3337 line
[0] = bytes
- 26 + 'a' - 1;
3338 encode_85(line
+ 1, cp
, bytes
);
3339 cp
= (char *) cp
+ bytes
;
3345 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_BODY
,
3348 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_FOOTER
, NULL
, 0, 0);
3352 static void emit_binary_diff(struct diff_options
*o
,
3353 mmfile_t
*one
, mmfile_t
*two
)
3355 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER
, NULL
, 0, 0);
3356 emit_binary_diff_body(o
, one
, two
);
3357 emit_binary_diff_body(o
, two
, one
);
3360 int diff_filespec_is_binary(struct repository
*r
,
3361 struct diff_filespec
*one
)
3363 struct diff_populate_filespec_options dpf_options
= {
3367 if (one
->is_binary
== -1) {
3368 diff_filespec_load_driver(one
, r
->index
);
3369 if (one
->driver
->binary
!= -1)
3370 one
->is_binary
= one
->driver
->binary
;
3372 if (!one
->data
&& DIFF_FILE_VALID(one
))
3373 diff_populate_filespec(r
, one
, &dpf_options
);
3374 if (one
->is_binary
== -1 && one
->data
)
3375 one
->is_binary
= buffer_is_binary(one
->data
,
3377 if (one
->is_binary
== -1)
3381 return one
->is_binary
;
3384 static const struct userdiff_funcname
*
3385 diff_funcname_pattern(struct diff_options
*o
, struct diff_filespec
*one
)
3387 diff_filespec_load_driver(one
, o
->repo
->index
);
3388 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
3391 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
3393 if (!options
->a_prefix
)
3394 options
->a_prefix
= a
;
3395 if (!options
->b_prefix
)
3396 options
->b_prefix
= b
;
3399 struct userdiff_driver
*get_textconv(struct repository
*r
,
3400 struct diff_filespec
*one
)
3402 if (!DIFF_FILE_VALID(one
))
3405 diff_filespec_load_driver(one
, r
->index
);
3406 return userdiff_get_textconv(r
, one
->driver
);
3409 static void builtin_diff(const char *name_a
,
3411 struct diff_filespec
*one
,
3412 struct diff_filespec
*two
,
3413 const char *xfrm_msg
,
3414 int must_show_header
,
3415 struct diff_options
*o
,
3416 int complete_rewrite
)
3420 char *a_one
, *b_two
;
3421 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
3422 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
3423 const char *a_prefix
, *b_prefix
;
3424 struct userdiff_driver
*textconv_one
= NULL
;
3425 struct userdiff_driver
*textconv_two
= NULL
;
3426 struct strbuf header
= STRBUF_INIT
;
3427 const char *line_prefix
= diff_line_prefix(o
);
3429 diff_set_mnemonic_prefix(o
, "a/", "b/");
3430 if (o
->flags
.reverse_diff
) {
3431 a_prefix
= o
->b_prefix
;
3432 b_prefix
= o
->a_prefix
;
3434 a_prefix
= o
->a_prefix
;
3435 b_prefix
= o
->b_prefix
;
3438 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
3439 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3440 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
3441 show_submodule_diff_summary(o
, one
->path
? one
->path
: two
->path
,
3442 &one
->oid
, &two
->oid
,
3443 two
->dirty_submodule
);
3445 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
3446 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3447 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
3448 show_submodule_inline_diff(o
, one
->path
? one
->path
: two
->path
,
3449 &one
->oid
, &two
->oid
,
3450 two
->dirty_submodule
);
3454 if (o
->flags
.allow_textconv
) {
3455 textconv_one
= get_textconv(o
->repo
, one
);
3456 textconv_two
= get_textconv(o
->repo
, two
);
3459 /* Never use a non-valid filename anywhere if at all possible */
3460 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
3461 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
3463 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
3464 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
3465 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
3466 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
3467 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3468 if (lbl
[0][0] == '/') {
3470 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3472 strbuf_addstr(&header
, xfrm_msg
);
3473 must_show_header
= 1;
3475 else if (lbl
[1][0] == '/') {
3476 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3478 strbuf_addstr(&header
, xfrm_msg
);
3479 must_show_header
= 1;
3482 if (one
->mode
!= two
->mode
) {
3483 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3484 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3485 must_show_header
= 1;
3488 strbuf_addstr(&header
, xfrm_msg
);
3491 * we do not run diff between different kind
3494 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3495 goto free_ab_and_return
;
3496 if (complete_rewrite
&&
3497 (textconv_one
|| !diff_filespec_is_binary(o
->repo
, one
)) &&
3498 (textconv_two
|| !diff_filespec_is_binary(o
->repo
, two
))) {
3499 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3500 header
.buf
, header
.len
, 0);
3501 strbuf_reset(&header
);
3502 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3503 textconv_one
, textconv_two
, o
);
3504 o
->found_changes
= 1;
3505 goto free_ab_and_return
;
3509 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3510 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3512 strbuf_reset(&header
);
3513 goto free_ab_and_return
;
3514 } else if (!o
->flags
.text
&&
3515 ( (!textconv_one
&& diff_filespec_is_binary(o
->repo
, one
)) ||
3516 (!textconv_two
&& diff_filespec_is_binary(o
->repo
, two
)) )) {
3517 struct strbuf sb
= STRBUF_INIT
;
3518 if (!one
->data
&& !two
->data
&&
3519 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3521 if (oideq(&one
->oid
, &two
->oid
)) {
3522 if (must_show_header
)
3523 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3524 header
.buf
, header
.len
,
3526 goto free_ab_and_return
;
3528 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3529 header
.buf
, header
.len
, 0);
3530 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3531 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3532 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3534 strbuf_release(&sb
);
3535 goto free_ab_and_return
;
3537 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3538 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3539 die("unable to read files to diff");
3540 /* Quite common confusing case */
3541 if (mf1
.size
== mf2
.size
&&
3542 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3543 if (must_show_header
)
3544 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3545 header
.buf
, header
.len
, 0);
3546 goto free_ab_and_return
;
3548 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3549 strbuf_reset(&header
);
3550 if (o
->flags
.binary
)
3551 emit_binary_diff(o
, &mf1
, &mf2
);
3553 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3554 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3555 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3557 strbuf_release(&sb
);
3559 o
->found_changes
= 1;
3561 /* Crazy xdl interfaces.. */
3562 const char *diffopts
;
3566 struct emit_callback ecbdata
;
3567 const struct userdiff_funcname
*pe
;
3569 if (must_show_header
) {
3570 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3571 header
.buf
, header
.len
, 0);
3572 strbuf_reset(&header
);
3575 mf1
.size
= fill_textconv(o
->repo
, textconv_one
, one
, &mf1
.ptr
);
3576 mf2
.size
= fill_textconv(o
->repo
, textconv_two
, two
, &mf2
.ptr
);
3578 pe
= diff_funcname_pattern(o
, one
);
3580 pe
= diff_funcname_pattern(o
, two
);
3582 memset(&xpp
, 0, sizeof(xpp
));
3583 memset(&xecfg
, 0, sizeof(xecfg
));
3584 memset(&ecbdata
, 0, sizeof(ecbdata
));
3585 if (o
->flags
.suppress_diff_headers
)
3587 ecbdata
.label_path
= lbl
;
3588 ecbdata
.color_diff
= want_color(o
->use_color
);
3589 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
3590 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3591 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3593 if (header
.len
&& !o
->flags
.suppress_diff_headers
)
3594 ecbdata
.header
= &header
;
3595 xpp
.flags
= o
->xdl_opts
;
3596 xpp
.ignore_regex
= o
->ignore_regex
;
3597 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3598 xpp
.anchors
= o
->anchors
;
3599 xpp
.anchors_nr
= o
->anchors_nr
;
3600 xecfg
.ctxlen
= o
->context
;
3601 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3602 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3603 if (o
->flags
.funccontext
)
3604 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3606 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3608 diffopts
= getenv("GIT_DIFF_OPTS");
3611 else if (skip_prefix(diffopts
, "--unified=", &v
))
3612 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3613 else if (skip_prefix(diffopts
, "-u", &v
))
3614 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3617 init_diff_words_data(&ecbdata
, o
, one
, two
);
3618 if (xdi_diff_outf(&mf1
, &mf2
, NULL
, fn_out_consume
,
3619 &ecbdata
, &xpp
, &xecfg
))
3620 die("unable to generate diff for %s", one
->path
);
3622 free_diff_words_data(&ecbdata
);
3627 xdiff_clear_find_func(&xecfg
);
3631 strbuf_release(&header
);
3632 diff_free_filespec_data(one
);
3633 diff_free_filespec_data(two
);
3639 static char *get_compact_summary(const struct diff_filepair
*p
, int is_renamed
)
3642 if (p
->status
== DIFF_STATUS_ADDED
) {
3643 if (S_ISLNK(p
->two
->mode
))
3645 else if ((p
->two
->mode
& 0777) == 0755)
3649 } else if (p
->status
== DIFF_STATUS_DELETED
)
3652 if (S_ISLNK(p
->one
->mode
) && !S_ISLNK(p
->two
->mode
))
3654 else if (!S_ISLNK(p
->one
->mode
) && S_ISLNK(p
->two
->mode
))
3656 else if ((p
->one
->mode
& 0777) == 0644 &&
3657 (p
->two
->mode
& 0777) == 0755)
3659 else if ((p
->one
->mode
& 0777) == 0755 &&
3660 (p
->two
->mode
& 0777) == 0644)
3665 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3666 struct diff_filespec
*one
,
3667 struct diff_filespec
*two
,
3668 struct diffstat_t
*diffstat
,
3669 struct diff_options
*o
,
3670 struct diff_filepair
*p
)
3673 struct diffstat_file
*data
;
3675 int complete_rewrite
= 0;
3677 if (!DIFF_PAIR_UNMERGED(p
)) {
3678 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3679 complete_rewrite
= 1;
3682 data
= diffstat_add(diffstat
, name_a
, name_b
);
3683 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3684 if (o
->flags
.stat_with_summary
)
3685 data
->comments
= get_compact_summary(p
, data
->is_renamed
);
3688 data
->is_unmerged
= 1;
3692 /* saves some reads if true, not a guarantee of diff outcome */
3693 may_differ
= !(one
->oid_valid
&& two
->oid_valid
&&
3694 oideq(&one
->oid
, &two
->oid
));
3696 if (diff_filespec_is_binary(o
->repo
, one
) ||
3697 diff_filespec_is_binary(o
->repo
, two
)) {
3698 data
->is_binary
= 1;
3703 data
->added
= diff_filespec_size(o
->repo
, two
);
3704 data
->deleted
= diff_filespec_size(o
->repo
, one
);
3708 else if (complete_rewrite
) {
3709 diff_populate_filespec(o
->repo
, one
, NULL
);
3710 diff_populate_filespec(o
->repo
, two
, NULL
);
3711 data
->deleted
= count_lines(one
->data
, one
->size
);
3712 data
->added
= count_lines(two
->data
, two
->size
);
3715 else if (may_differ
) {
3716 /* Crazy xdl interfaces.. */
3720 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3721 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3722 die("unable to read files to diff");
3724 memset(&xpp
, 0, sizeof(xpp
));
3725 memset(&xecfg
, 0, sizeof(xecfg
));
3726 xpp
.flags
= o
->xdl_opts
;
3727 xpp
.ignore_regex
= o
->ignore_regex
;
3728 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3729 xpp
.anchors
= o
->anchors
;
3730 xpp
.anchors_nr
= o
->anchors_nr
;
3731 xecfg
.ctxlen
= o
->context
;
3732 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3733 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
3734 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
3735 diffstat_consume
, diffstat
, &xpp
, &xecfg
))
3736 die("unable to generate diffstat for %s", one
->path
);
3738 if (DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
)) {
3739 struct diffstat_file
*file
=
3740 diffstat
->files
[diffstat
->nr
- 1];
3742 * Omit diffstats of modified files where nothing changed.
3743 * Even if may_differ, this might be the case due to
3744 * ignoring whitespace changes, etc.
3746 * But note that we special-case additions, deletions,
3747 * renames, and mode changes as adding an empty file,
3748 * for example is still of interest.
3750 if ((p
->status
== DIFF_STATUS_MODIFIED
)
3753 && one
->mode
== two
->mode
) {
3754 free_diffstat_file(file
);
3760 diff_free_filespec_data(one
);
3761 diff_free_filespec_data(two
);
3764 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3765 const char *attr_path
,
3766 struct diff_filespec
*one
,
3767 struct diff_filespec
*two
,
3768 struct diff_options
*o
)
3771 struct checkdiff_t data
;
3776 memset(&data
, 0, sizeof(data
));
3777 data
.filename
= name_b
? name_b
: name_a
;
3780 data
.ws_rule
= whitespace_rule(o
->repo
->index
, attr_path
);
3781 data
.conflict_marker_size
= ll_merge_marker_size(o
->repo
->index
, attr_path
);
3783 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3784 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3785 die("unable to read files to diff");
3788 * All the other codepaths check both sides, but not checking
3789 * the "old" side here is deliberate. We are checking the newly
3790 * introduced changes, and as long as the "new" side is text, we
3791 * can and should check what it introduces.
3793 if (diff_filespec_is_binary(o
->repo
, two
))
3794 goto free_and_return
;
3796 /* Crazy xdl interfaces.. */
3800 memset(&xpp
, 0, sizeof(xpp
));
3801 memset(&xecfg
, 0, sizeof(xecfg
));
3802 xecfg
.ctxlen
= 1; /* at least one context line */
3804 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume_hunk
,
3805 checkdiff_consume
, &data
,
3807 die("unable to generate checkdiff for %s", one
->path
);
3809 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3810 struct emit_callback ecbdata
;
3813 ecbdata
.ws_rule
= data
.ws_rule
;
3814 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3815 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3820 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3821 fprintf(o
->file
, "%s:%d: %s.\n",
3822 data
.filename
, blank_at_eof
, err
);
3823 data
.status
= 1; /* report errors */
3828 diff_free_filespec_data(one
);
3829 diff_free_filespec_data(two
);
3831 o
->flags
.check_failed
= 1;
3834 struct diff_filespec
*alloc_filespec(const char *path
)
3836 struct diff_filespec
*spec
;
3838 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3840 spec
->is_binary
= -1;
3844 void free_filespec(struct diff_filespec
*spec
)
3846 if (!--spec
->count
) {
3847 diff_free_filespec_data(spec
);
3852 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3853 int oid_valid
, unsigned short mode
)
3856 spec
->mode
= canon_mode(mode
);
3857 oidcpy(&spec
->oid
, oid
);
3858 spec
->oid_valid
= oid_valid
;
3863 * Given a name and sha1 pair, if the index tells us the file in
3864 * the work tree has that object contents, return true, so that
3865 * prepare_temp_file() does not have to inflate and extract.
3867 static int reuse_worktree_file(struct index_state
*istate
,
3869 const struct object_id
*oid
,
3872 const struct cache_entry
*ce
;
3877 * We do not read the cache ourselves here, because the
3878 * benchmark with my previous version that always reads cache
3879 * shows that it makes things worse for diff-tree comparing
3880 * two linux-2.6 kernel trees in an already checked out work
3881 * tree. This is because most diff-tree comparisons deal with
3882 * only a small number of files, while reading the cache is
3883 * expensive for a large project, and its cost outweighs the
3884 * savings we get by not inflating the object to a temporary
3885 * file. Practically, this code only helps when we are used
3886 * by diff-cache --cached, which does read the cache before
3892 /* We want to avoid the working directory if our caller
3893 * doesn't need the data in a normal file, this system
3894 * is rather slow with its stat/open/mmap/close syscalls,
3895 * and the object is contained in a pack file. The pack
3896 * is probably already open and will be faster to obtain
3897 * the data through than the working directory. Loose
3898 * objects however would tend to be slower as they need
3899 * to be individually opened and inflated.
3901 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_object_pack(oid
))
3905 * Similarly, if we'd have to convert the file contents anyway, that
3906 * makes the optimization not worthwhile.
3908 if (!want_file
&& would_convert_to_git(istate
, name
))
3912 * If this path does not match our sparse-checkout definition,
3913 * then the file will not be in the working directory.
3915 if (!path_in_sparse_checkout(name
, istate
))
3919 pos
= index_name_pos(istate
, name
, len
);
3922 ce
= istate
->cache
[pos
];
3925 * This is not the sha1 we are looking for, or
3926 * unreusable because it is not a regular file.
3928 if (!oideq(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
3932 * If ce is marked as "assume unchanged", there is no
3933 * guarantee that work tree matches what we are looking for.
3935 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
3939 * If ce matches the file in the work tree, we can reuse it.
3941 if (ce_uptodate(ce
) ||
3942 (!lstat(name
, &st
) && !ie_match_stat(istate
, ce
, &st
, 0)))
3948 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
3950 struct strbuf buf
= STRBUF_INIT
;
3953 /* Are we looking at the work tree? */
3954 if (s
->dirty_submodule
)
3957 strbuf_addf(&buf
, "Subproject commit %s%s\n",
3958 oid_to_hex(&s
->oid
), dirty
);
3962 strbuf_release(&buf
);
3964 s
->data
= strbuf_detach(&buf
, NULL
);
3971 * While doing rename detection and pickaxe operation, we may need to
3972 * grab the data for the blob (or file) for our own in-core comparison.
3973 * diff_filespec has data and size fields for this purpose.
3975 int diff_populate_filespec(struct repository
*r
,
3976 struct diff_filespec
*s
,
3977 const struct diff_populate_filespec_options
*options
)
3979 int size_only
= options
? options
->check_size_only
: 0;
3980 int check_binary
= options
? options
->check_binary
: 0;
3982 int conv_flags
= global_conv_flags_eol
;
3984 * demote FAIL to WARN to allow inspecting the situation
3985 * instead of refusing.
3987 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
3988 conv_flags
= CONV_EOL_RNDTRP_WARN
;
3990 if (!DIFF_FILE_VALID(s
))
3991 die("internal error: asking to populate invalid file.");
3992 if (S_ISDIR(s
->mode
))
3998 if (size_only
&& 0 < s
->size
)
4001 if (S_ISGITLINK(s
->mode
))
4002 return diff_populate_gitlink(s
, size_only
);
4004 if (!s
->oid_valid
||
4005 reuse_worktree_file(r
->index
, s
->path
, &s
->oid
, 0)) {
4006 struct strbuf buf
= STRBUF_INIT
;
4010 if (lstat(s
->path
, &st
) < 0) {
4014 s
->data
= (char *)"";
4018 s
->size
= xsize_t(st
.st_size
);
4021 if (S_ISLNK(st
.st_mode
)) {
4022 struct strbuf sb
= STRBUF_INIT
;
4024 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
4027 s
->data
= strbuf_detach(&sb
, NULL
);
4033 * Even if the caller would be happy with getting
4034 * only the size, we cannot return early at this
4035 * point if the path requires us to run the content
4038 if (size_only
&& !would_convert_to_git(r
->index
, s
->path
))
4042 * Note: this check uses xsize_t(st.st_size) that may
4043 * not be the true size of the blob after it goes
4044 * through convert_to_git(). This may not strictly be
4045 * correct, but the whole point of big_file_threshold
4046 * and is_binary check being that we want to avoid
4047 * opening the file and inspecting the contents, this
4051 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4055 fd
= open(s
->path
, O_RDONLY
);
4058 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
4060 s
->should_munmap
= 1;
4063 * Convert from working tree format to canonical git format
4065 if (convert_to_git(r
->index
, s
->path
, s
->data
, s
->size
, &buf
, conv_flags
)) {
4067 munmap(s
->data
, s
->size
);
4068 s
->should_munmap
= 0;
4069 s
->data
= strbuf_detach(&buf
, &size
);
4075 struct object_info info
= {
4079 if (!(size_only
|| check_binary
))
4081 * Set contentp, since there is no chance that merely
4082 * the size is sufficient.
4084 info
.contentp
= &s
->data
;
4086 if (options
&& options
->missing_object_cb
) {
4087 if (!oid_object_info_extended(r
, &s
->oid
, &info
,
4088 OBJECT_INFO_LOOKUP_REPLACE
|
4089 OBJECT_INFO_SKIP_FETCH_OBJECT
))
4091 options
->missing_object_cb(options
->missing_object_data
);
4093 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4094 OBJECT_INFO_LOOKUP_REPLACE
))
4095 die("unable to read %s", oid_to_hex(&s
->oid
));
4098 if (size_only
|| check_binary
) {
4101 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4106 if (!info
.contentp
) {
4107 info
.contentp
= &s
->data
;
4108 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4109 OBJECT_INFO_LOOKUP_REPLACE
))
4110 die("unable to read %s", oid_to_hex(&s
->oid
));
4117 void diff_free_filespec_blob(struct diff_filespec
*s
)
4121 else if (s
->should_munmap
)
4122 munmap(s
->data
, s
->size
);
4124 if (s
->should_free
|| s
->should_munmap
) {
4125 s
->should_free
= s
->should_munmap
= 0;
4130 void diff_free_filespec_data(struct diff_filespec
*s
)
4135 diff_free_filespec_blob(s
);
4136 FREE_AND_NULL(s
->cnt_data
);
4139 static void prep_temp_blob(struct index_state
*istate
,
4140 const char *path
, struct diff_tempfile
*temp
,
4143 const struct object_id
*oid
,
4146 struct strbuf buf
= STRBUF_INIT
;
4147 struct strbuf tempfile
= STRBUF_INIT
;
4148 char *path_dup
= xstrdup(path
);
4149 const char *base
= basename(path_dup
);
4150 struct checkout_metadata meta
;
4152 init_checkout_metadata(&meta
, NULL
, NULL
, oid
);
4154 /* Generate "XXXXXX_basename.ext" */
4155 strbuf_addstr(&tempfile
, "XXXXXX_");
4156 strbuf_addstr(&tempfile
, base
);
4158 temp
->tempfile
= mks_tempfile_ts(tempfile
.buf
, strlen(base
) + 1);
4159 if (!temp
->tempfile
)
4160 die_errno("unable to create temp-file");
4161 if (convert_to_working_tree(istate
, path
,
4162 (const char *)blob
, (size_t)size
, &buf
, &meta
)) {
4166 if (write_in_full(temp
->tempfile
->fd
, blob
, size
) < 0 ||
4167 close_tempfile_gently(temp
->tempfile
))
4168 die_errno("unable to write temp-file");
4169 temp
->name
= get_tempfile_path(temp
->tempfile
);
4170 oid_to_hex_r(temp
->hex
, oid
);
4171 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
4172 strbuf_release(&buf
);
4173 strbuf_release(&tempfile
);
4177 static struct diff_tempfile
*prepare_temp_file(struct repository
*r
,
4179 struct diff_filespec
*one
)
4181 struct diff_tempfile
*temp
= claim_diff_tempfile();
4183 if (!DIFF_FILE_VALID(one
)) {
4185 /* A '-' entry produces this for file-2, and
4186 * a '+' entry produces this for file-1.
4188 temp
->name
= "/dev/null";
4189 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
4190 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
4194 if (!S_ISGITLINK(one
->mode
) &&
4196 reuse_worktree_file(r
->index
, name
, &one
->oid
, 1))) {
4198 if (lstat(name
, &st
) < 0) {
4199 if (errno
== ENOENT
)
4200 goto not_a_valid_file
;
4201 die_errno("stat(%s)", name
);
4203 if (S_ISLNK(st
.st_mode
)) {
4204 struct strbuf sb
= STRBUF_INIT
;
4205 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
4206 die_errno("readlink(%s)", name
);
4207 prep_temp_blob(r
->index
, name
, temp
, sb
.buf
, sb
.len
,
4209 &one
->oid
: null_oid()),
4211 one
->mode
: S_IFLNK
));
4212 strbuf_release(&sb
);
4215 /* we can borrow from the file in the work tree */
4217 if (!one
->oid_valid
)
4218 oid_to_hex_r(temp
->hex
, null_oid());
4220 oid_to_hex_r(temp
->hex
, &one
->oid
);
4221 /* Even though we may sometimes borrow the
4222 * contents from the work tree, we always want
4223 * one->mode. mode is trustworthy even when
4224 * !(one->oid_valid), as long as
4225 * DIFF_FILE_VALID(one).
4227 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
4232 if (diff_populate_filespec(r
, one
, NULL
))
4233 die("cannot read data blob for %s", one
->path
);
4234 prep_temp_blob(r
->index
, name
, temp
,
4235 one
->data
, one
->size
,
4236 &one
->oid
, one
->mode
);
4241 static void add_external_diff_name(struct repository
*r
,
4242 struct strvec
*argv
,
4244 struct diff_filespec
*df
)
4246 struct diff_tempfile
*temp
= prepare_temp_file(r
, name
, df
);
4247 strvec_push(argv
, temp
->name
);
4248 strvec_push(argv
, temp
->hex
);
4249 strvec_push(argv
, temp
->mode
);
4252 /* An external diff command takes:
4254 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4255 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4258 static void run_external_diff(const char *pgm
,
4261 struct diff_filespec
*one
,
4262 struct diff_filespec
*two
,
4263 const char *xfrm_msg
,
4264 struct diff_options
*o
)
4266 struct strvec argv
= STRVEC_INIT
;
4267 struct strvec env
= STRVEC_INIT
;
4268 struct diff_queue_struct
*q
= &diff_queued_diff
;
4270 strvec_push(&argv
, pgm
);
4271 strvec_push(&argv
, name
);
4274 add_external_diff_name(o
->repo
, &argv
, name
, one
);
4276 add_external_diff_name(o
->repo
, &argv
, name
, two
);
4278 add_external_diff_name(o
->repo
, &argv
, other
, two
);
4279 strvec_push(&argv
, other
);
4280 strvec_push(&argv
, xfrm_msg
);
4284 strvec_pushf(&env
, "GIT_DIFF_PATH_COUNTER=%d", ++o
->diff_path_counter
);
4285 strvec_pushf(&env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
4287 diff_free_filespec_data(one
);
4288 diff_free_filespec_data(two
);
4289 if (run_command_v_opt_cd_env(argv
.v
, RUN_USING_SHELL
, NULL
, env
.v
))
4290 die(_("external diff died, stopping at %s"), name
);
4293 strvec_clear(&argv
);
4297 static int similarity_index(struct diff_filepair
*p
)
4299 return p
->score
* 100 / MAX_SCORE
;
4302 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
4304 if (startup_info
->have_repository
)
4305 return find_unique_abbrev(oid
, abbrev
);
4307 char *hex
= oid_to_hex(oid
);
4309 abbrev
= FALLBACK_DEFAULT_ABBREV
;
4310 if (abbrev
> the_hash_algo
->hexsz
)
4311 BUG("oid abbreviation out of range: %d", abbrev
);
4318 static void fill_metainfo(struct strbuf
*msg
,
4321 struct diff_filespec
*one
,
4322 struct diff_filespec
*two
,
4323 struct diff_options
*o
,
4324 struct diff_filepair
*p
,
4325 int *must_show_header
,
4328 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
4329 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
4330 const char *line_prefix
= diff_line_prefix(o
);
4332 *must_show_header
= 1;
4333 strbuf_init(msg
, PATH_MAX
* 2 + 300);
4334 switch (p
->status
) {
4335 case DIFF_STATUS_COPIED
:
4336 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4337 line_prefix
, set
, similarity_index(p
));
4338 strbuf_addf(msg
, "%s\n%s%scopy from ",
4339 reset
, line_prefix
, set
);
4340 quote_c_style(name
, msg
, NULL
, 0);
4341 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
4342 quote_c_style(other
, msg
, NULL
, 0);
4343 strbuf_addf(msg
, "%s\n", reset
);
4345 case DIFF_STATUS_RENAMED
:
4346 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4347 line_prefix
, set
, similarity_index(p
));
4348 strbuf_addf(msg
, "%s\n%s%srename from ",
4349 reset
, line_prefix
, set
);
4350 quote_c_style(name
, msg
, NULL
, 0);
4351 strbuf_addf(msg
, "%s\n%s%srename to ",
4352 reset
, line_prefix
, set
);
4353 quote_c_style(other
, msg
, NULL
, 0);
4354 strbuf_addf(msg
, "%s\n", reset
);
4356 case DIFF_STATUS_MODIFIED
:
4358 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
4360 set
, similarity_index(p
), reset
);
4365 *must_show_header
= 0;
4367 if (one
&& two
&& !oideq(&one
->oid
, &two
->oid
)) {
4368 const unsigned hexsz
= the_hash_algo
->hexsz
;
4369 int abbrev
= o
->abbrev
? o
->abbrev
: DEFAULT_ABBREV
;
4371 if (o
->flags
.full_index
)
4374 if (o
->flags
.binary
) {
4376 if ((!fill_mmfile(o
->repo
, &mf
, one
) &&
4377 diff_filespec_is_binary(o
->repo
, one
)) ||
4378 (!fill_mmfile(o
->repo
, &mf
, two
) &&
4379 diff_filespec_is_binary(o
->repo
, two
)))
4382 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
4383 diff_abbrev_oid(&one
->oid
, abbrev
),
4384 diff_abbrev_oid(&two
->oid
, abbrev
));
4385 if (one
->mode
== two
->mode
)
4386 strbuf_addf(msg
, " %06o", one
->mode
);
4387 strbuf_addf(msg
, "%s\n", reset
);
4391 static void run_diff_cmd(const char *pgm
,
4394 const char *attr_path
,
4395 struct diff_filespec
*one
,
4396 struct diff_filespec
*two
,
4398 struct diff_options
*o
,
4399 struct diff_filepair
*p
)
4401 const char *xfrm_msg
= NULL
;
4402 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
4403 int must_show_header
= 0;
4406 if (o
->flags
.allow_external
) {
4407 struct userdiff_driver
*drv
;
4409 drv
= userdiff_find_by_path(o
->repo
->index
, attr_path
);
4410 if (drv
&& drv
->external
)
4411 pgm
= drv
->external
;
4416 * don't use colors when the header is intended for an
4417 * external diff driver
4419 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
4421 want_color(o
->use_color
) && !pgm
);
4422 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
4426 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
, o
);
4430 builtin_diff(name
, other
? other
: name
,
4431 one
, two
, xfrm_msg
, must_show_header
,
4432 o
, complete_rewrite
);
4434 fprintf(o
->file
, "* Unmerged path %s\n", name
);
4437 static void diff_fill_oid_info(struct diff_filespec
*one
, struct index_state
*istate
)
4439 if (DIFF_FILE_VALID(one
)) {
4440 if (!one
->oid_valid
) {
4442 if (one
->is_stdin
) {
4446 if (lstat(one
->path
, &st
) < 0)
4447 die_errno("stat '%s'", one
->path
);
4448 if (index_path(istate
, &one
->oid
, one
->path
, &st
, 0))
4449 die("cannot hash %s", one
->path
);
4456 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
4458 /* Strip the prefix but do not molest /dev/null and absolute paths */
4459 if (*namep
&& !is_absolute_path(*namep
)) {
4460 *namep
+= prefix_length
;
4464 if (*otherp
&& !is_absolute_path(*otherp
)) {
4465 *otherp
+= prefix_length
;
4466 if (**otherp
== '/')
4471 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
4473 const char *pgm
= external_diff();
4475 struct diff_filespec
*one
= p
->one
;
4476 struct diff_filespec
*two
= p
->two
;
4479 const char *attr_path
;
4482 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
4484 if (o
->prefix_length
)
4485 strip_prefix(o
->prefix_length
, &name
, &other
);
4487 if (!o
->flags
.allow_external
)
4490 if (DIFF_PAIR_UNMERGED(p
)) {
4491 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
4492 NULL
, NULL
, NULL
, o
, p
);
4496 diff_fill_oid_info(one
, o
->repo
->index
);
4497 diff_fill_oid_info(two
, o
->repo
->index
);
4500 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4501 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4503 * a filepair that changes between file and symlink
4504 * needs to be split into deletion and creation.
4506 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4507 run_diff_cmd(NULL
, name
, other
, attr_path
,
4511 strbuf_release(&msg
);
4513 null
= alloc_filespec(one
->path
);
4514 run_diff_cmd(NULL
, name
, other
, attr_path
,
4515 null
, two
, &msg
, o
, p
);
4519 run_diff_cmd(pgm
, name
, other
, attr_path
,
4520 one
, two
, &msg
, o
, p
);
4522 strbuf_release(&msg
);
4525 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4526 struct diffstat_t
*diffstat
)
4531 if (DIFF_PAIR_UNMERGED(p
)) {
4533 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
,
4538 name
= p
->one
->path
;
4539 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4541 if (o
->prefix_length
)
4542 strip_prefix(o
->prefix_length
, &name
, &other
);
4544 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4545 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4547 builtin_diffstat(name
, other
, p
->one
, p
->two
,
4551 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4555 const char *attr_path
;
4557 if (DIFF_PAIR_UNMERGED(p
)) {
4562 name
= p
->one
->path
;
4563 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4564 attr_path
= other
? other
: name
;
4566 if (o
->prefix_length
)
4567 strip_prefix(o
->prefix_length
, &name
, &other
);
4569 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4570 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4572 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4575 static void prep_parse_options(struct diff_options
*options
);
4577 void repo_diff_setup(struct repository
*r
, struct diff_options
*options
)
4579 memcpy(options
, &default_diff_options
, sizeof(*options
));
4581 options
->file
= stdout
;
4584 options
->output_indicators
[OUTPUT_INDICATOR_NEW
] = '+';
4585 options
->output_indicators
[OUTPUT_INDICATOR_OLD
] = '-';
4586 options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
] = ' ';
4587 options
->abbrev
= DEFAULT_ABBREV
;
4588 options
->line_termination
= '\n';
4589 options
->break_opt
= -1;
4590 options
->rename_limit
= -1;
4591 options
->dirstat_permille
= diff_dirstat_permille_default
;
4592 options
->context
= diff_context_default
;
4593 options
->interhunkcontext
= diff_interhunk_context_default
;
4594 options
->ws_error_highlight
= ws_error_highlight_default
;
4595 options
->flags
.rename_empty
= 1;
4596 options
->flags
.relative_name
= diff_relative
;
4597 options
->objfind
= NULL
;
4599 /* pathchange left =NULL by default */
4600 options
->change
= diff_change
;
4601 options
->add_remove
= diff_addremove
;
4602 options
->use_color
= diff_use_color_default
;
4603 options
->detect_rename
= diff_detect_rename_default
;
4604 options
->xdl_opts
|= diff_algorithm
;
4605 if (diff_indent_heuristic
)
4606 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4608 options
->orderfile
= diff_order_file_cfg
;
4610 if (!options
->flags
.ignore_submodule_set
)
4611 options
->flags
.ignore_untracked_in_submodules
= 1;
4613 if (diff_no_prefix
) {
4614 options
->a_prefix
= options
->b_prefix
= "";
4615 } else if (!diff_mnemonic_prefix
) {
4616 options
->a_prefix
= "a/";
4617 options
->b_prefix
= "b/";
4620 options
->color_moved
= diff_color_moved_default
;
4621 options
->color_moved_ws_handling
= diff_color_moved_ws_default
;
4623 prep_parse_options(options
);
4626 void diff_setup_done(struct diff_options
*options
)
4628 unsigned check_mask
= DIFF_FORMAT_NAME
|
4629 DIFF_FORMAT_NAME_STATUS
|
4630 DIFF_FORMAT_CHECKDIFF
|
4631 DIFF_FORMAT_NO_OUTPUT
;
4633 * This must be signed because we're comparing against a potentially
4636 const int hexsz
= the_hash_algo
->hexsz
;
4638 if (options
->set_default
)
4639 options
->set_default(options
);
4641 if (HAS_MULTI_BITS(options
->output_format
& check_mask
))
4642 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
4643 "--name-only", "--name-status", "--check", "-s");
4645 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
4646 die(_("options '%s', '%s', and '%s' cannot be used together"),
4647 "-G", "-S", "--find-object");
4649 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_G_REGEX_MASK
))
4650 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"),
4651 "-G", "--pickaxe-regex", "--pickaxe-regex", "-S");
4653 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK
))
4654 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"),
4655 "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S");
4658 * Most of the time we can say "there are changes"
4659 * only by checking if there are changed paths, but
4660 * --ignore-whitespace* options force us to look
4664 if ((options
->xdl_opts
& XDF_WHITESPACE_FLAGS
) ||
4665 options
->ignore_regex_nr
)
4666 options
->flags
.diff_from_contents
= 1;
4668 options
->flags
.diff_from_contents
= 0;
4670 if (options
->flags
.find_copies_harder
)
4671 options
->detect_rename
= DIFF_DETECT_COPY
;
4673 if (!options
->flags
.relative_name
)
4674 options
->prefix
= NULL
;
4675 if (options
->prefix
)
4676 options
->prefix_length
= strlen(options
->prefix
);
4678 options
->prefix_length
= 0;
4680 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4681 DIFF_FORMAT_NAME_STATUS
|
4682 DIFF_FORMAT_CHECKDIFF
|
4683 DIFF_FORMAT_NO_OUTPUT
))
4684 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4685 DIFF_FORMAT_NUMSTAT
|
4686 DIFF_FORMAT_DIFFSTAT
|
4687 DIFF_FORMAT_SHORTSTAT
|
4688 DIFF_FORMAT_DIRSTAT
|
4689 DIFF_FORMAT_SUMMARY
|
4693 * These cases always need recursive; we do not drop caller-supplied
4694 * recursive bits for other formats here.
4696 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4697 DIFF_FORMAT_NUMSTAT
|
4698 DIFF_FORMAT_DIFFSTAT
|
4699 DIFF_FORMAT_SHORTSTAT
|
4700 DIFF_FORMAT_DIRSTAT
|
4701 DIFF_FORMAT_SUMMARY
|
4702 DIFF_FORMAT_CHECKDIFF
))
4703 options
->flags
.recursive
= 1;
4705 * Also pickaxe would not work very well if you do not say recursive
4707 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
4708 options
->flags
.recursive
= 1;
4710 * When patches are generated, submodules diffed against the work tree
4711 * must be checked for dirtiness too so it can be shown in the output
4713 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4714 options
->flags
.dirty_submodules
= 1;
4716 if (options
->detect_rename
&& options
->rename_limit
< 0)
4717 options
->rename_limit
= diff_rename_limit_default
;
4718 if (hexsz
< options
->abbrev
)
4719 options
->abbrev
= hexsz
; /* full */
4722 * It does not make sense to show the first hit we happened
4723 * to have found. It does not make sense not to return with
4724 * exit code in such a case either.
4726 if (options
->flags
.quick
) {
4727 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4728 options
->flags
.exit_with_status
= 1;
4731 options
->diff_path_counter
= 0;
4733 if (options
->flags
.follow_renames
&& options
->pathspec
.nr
!= 1)
4734 die(_("--follow requires exactly one pathspec"));
4736 if (!options
->use_color
|| external_diff())
4737 options
->color_moved
= 0;
4739 FREE_AND_NULL(options
->parseopts
);
4742 int parse_long_opt(const char *opt
, const char **argv
,
4743 const char **optarg
)
4745 const char *arg
= argv
[0];
4746 if (!skip_prefix(arg
, "--", &arg
))
4748 if (!skip_prefix(arg
, opt
, &arg
))
4750 if (*arg
== '=') { /* stuck form: --option=value */
4756 /* separate form: --option value */
4758 die("Option '--%s' requires a value", opt
);
4763 static int diff_opt_stat(const struct option
*opt
, const char *value
, int unset
)
4765 struct diff_options
*options
= opt
->value
;
4766 int width
= options
->stat_width
;
4767 int name_width
= options
->stat_name_width
;
4768 int graph_width
= options
->stat_graph_width
;
4769 int count
= options
->stat_count
;
4772 BUG_ON_OPT_NEG(unset
);
4774 if (!strcmp(opt
->long_name
, "stat")) {
4776 width
= strtoul(value
, &end
, 10);
4778 name_width
= strtoul(end
+1, &end
, 10);
4780 count
= strtoul(end
+1, &end
, 10);
4782 return error(_("invalid --stat value: %s"), value
);
4784 } else if (!strcmp(opt
->long_name
, "stat-width")) {
4785 width
= strtoul(value
, &end
, 10);
4787 return error(_("%s expects a numerical value"),
4789 } else if (!strcmp(opt
->long_name
, "stat-name-width")) {
4790 name_width
= strtoul(value
, &end
, 10);
4792 return error(_("%s expects a numerical value"),
4794 } else if (!strcmp(opt
->long_name
, "stat-graph-width")) {
4795 graph_width
= strtoul(value
, &end
, 10);
4797 return error(_("%s expects a numerical value"),
4799 } else if (!strcmp(opt
->long_name
, "stat-count")) {
4800 count
= strtoul(value
, &end
, 10);
4802 return error(_("%s expects a numerical value"),
4805 BUG("%s should not get here", opt
->long_name
);
4807 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4808 options
->stat_name_width
= name_width
;
4809 options
->stat_graph_width
= graph_width
;
4810 options
->stat_width
= width
;
4811 options
->stat_count
= count
;
4815 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
4817 struct strbuf errmsg
= STRBUF_INIT
;
4818 if (parse_dirstat_params(options
, params
, &errmsg
))
4819 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
4821 strbuf_release(&errmsg
);
4823 * The caller knows a dirstat-related option is given from the command
4824 * line; allow it to say "return this_function();"
4826 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
4830 static const char diff_status_letters
[] = {
4833 DIFF_STATUS_DELETED
,
4834 DIFF_STATUS_MODIFIED
,
4835 DIFF_STATUS_RENAMED
,
4836 DIFF_STATUS_TYPE_CHANGED
,
4837 DIFF_STATUS_UNKNOWN
,
4838 DIFF_STATUS_UNMERGED
,
4839 DIFF_STATUS_FILTER_AON
,
4840 DIFF_STATUS_FILTER_BROKEN
,
4844 static unsigned int filter_bit
['Z' + 1];
4846 static void prepare_filter_bits(void)
4850 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4851 for (i
= 0; diff_status_letters
[i
]; i
++)
4852 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4856 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4858 return opt
->filter
& filter_bit
[(int) status
];
4861 unsigned diff_filter_bit(char status
)
4863 prepare_filter_bits();
4864 return filter_bit
[(int) status
];
4867 static int diff_opt_diff_filter(const struct option
*option
,
4868 const char *optarg
, int unset
)
4870 struct diff_options
*opt
= option
->value
;
4873 BUG_ON_OPT_NEG(unset
);
4874 prepare_filter_bits();
4877 * If there is a negation e.g. 'd' in the input, and we haven't
4878 * initialized the filter field with another --diff-filter, start
4879 * from full set of bits, except for AON.
4882 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4883 if (optch
< 'a' || 'z' < optch
)
4885 opt
->filter
= (1 << (ARRAY_SIZE(diff_status_letters
) - 1)) - 1;
4886 opt
->filter
&= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4891 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4895 if ('a' <= optch
&& optch
<= 'z') {
4897 optch
= toupper(optch
);
4902 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
4904 return error(_("unknown change class '%c' in --diff-filter=%s"),
4907 opt
->filter
&= ~bit
;
4914 static void enable_patch_output(int *fmt
)
4916 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
4917 *fmt
|= DIFF_FORMAT_PATCH
;
4920 static int diff_opt_ws_error_highlight(const struct option
*option
,
4921 const char *arg
, int unset
)
4923 struct diff_options
*opt
= option
->value
;
4924 int val
= parse_ws_error_highlight(arg
);
4926 BUG_ON_OPT_NEG(unset
);
4928 return error(_("unknown value after ws-error-highlight=%.*s"),
4930 opt
->ws_error_highlight
= val
;
4934 static int diff_opt_find_object(const struct option
*option
,
4935 const char *arg
, int unset
)
4937 struct diff_options
*opt
= option
->value
;
4938 struct object_id oid
;
4940 BUG_ON_OPT_NEG(unset
);
4941 if (get_oid(arg
, &oid
))
4942 return error(_("unable to resolve '%s'"), arg
);
4945 CALLOC_ARRAY(opt
->objfind
, 1);
4947 opt
->pickaxe_opts
|= DIFF_PICKAXE_KIND_OBJFIND
;
4948 opt
->flags
.recursive
= 1;
4949 opt
->flags
.tree_in_recursive
= 1;
4950 oidset_insert(opt
->objfind
, &oid
);
4954 static int diff_opt_anchored(const struct option
*opt
,
4955 const char *arg
, int unset
)
4957 struct diff_options
*options
= opt
->value
;
4959 BUG_ON_OPT_NEG(unset
);
4960 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
4961 ALLOC_GROW(options
->anchors
, options
->anchors_nr
+ 1,
4962 options
->anchors_alloc
);
4963 options
->anchors
[options
->anchors_nr
++] = xstrdup(arg
);
4967 static int diff_opt_binary(const struct option
*opt
,
4968 const char *arg
, int unset
)
4970 struct diff_options
*options
= opt
->value
;
4972 BUG_ON_OPT_NEG(unset
);
4973 BUG_ON_OPT_ARG(arg
);
4974 enable_patch_output(&options
->output_format
);
4975 options
->flags
.binary
= 1;
4979 static int diff_opt_break_rewrites(const struct option
*opt
,
4980 const char *arg
, int unset
)
4982 int *break_opt
= opt
->value
;
4985 BUG_ON_OPT_NEG(unset
);
4988 opt1
= parse_rename_score(&arg
);
4991 else if (*arg
!= '/')
4992 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
4995 opt2
= parse_rename_score(&arg
);
4998 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
4999 *break_opt
= opt1
| (opt2
<< 16);
5003 static int diff_opt_char(const struct option
*opt
,
5004 const char *arg
, int unset
)
5006 char *value
= opt
->value
;
5008 BUG_ON_OPT_NEG(unset
);
5010 return error(_("%s expects a character, got '%s'"),
5011 opt
->long_name
, arg
);
5016 static int diff_opt_color_moved(const struct option
*opt
,
5017 const char *arg
, int unset
)
5019 struct diff_options
*options
= opt
->value
;
5022 options
->color_moved
= COLOR_MOVED_NO
;
5024 if (diff_color_moved_default
)
5025 options
->color_moved
= diff_color_moved_default
;
5026 if (options
->color_moved
== COLOR_MOVED_NO
)
5027 options
->color_moved
= COLOR_MOVED_DEFAULT
;
5029 int cm
= parse_color_moved(arg
);
5031 return error(_("bad --color-moved argument: %s"), arg
);
5032 options
->color_moved
= cm
;
5037 static int diff_opt_color_moved_ws(const struct option
*opt
,
5038 const char *arg
, int unset
)
5040 struct diff_options
*options
= opt
->value
;
5044 options
->color_moved_ws_handling
= 0;
5048 cm
= parse_color_moved_ws(arg
);
5049 if (cm
& COLOR_MOVED_WS_ERROR
)
5050 return error(_("invalid mode '%s' in --color-moved-ws"), arg
);
5051 options
->color_moved_ws_handling
= cm
;
5055 static int diff_opt_color_words(const struct option
*opt
,
5056 const char *arg
, int unset
)
5058 struct diff_options
*options
= opt
->value
;
5060 BUG_ON_OPT_NEG(unset
);
5061 options
->use_color
= 1;
5062 options
->word_diff
= DIFF_WORDS_COLOR
;
5063 options
->word_regex
= arg
;
5067 static int diff_opt_compact_summary(const struct option
*opt
,
5068 const char *arg
, int unset
)
5070 struct diff_options
*options
= opt
->value
;
5072 BUG_ON_OPT_ARG(arg
);
5074 options
->flags
.stat_with_summary
= 0;
5076 options
->flags
.stat_with_summary
= 1;
5077 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
5082 static int diff_opt_diff_algorithm(const struct option
*opt
,
5083 const char *arg
, int unset
)
5085 struct diff_options
*options
= opt
->value
;
5086 long value
= parse_algorithm_value(arg
);
5088 BUG_ON_OPT_NEG(unset
);
5090 return error(_("option diff-algorithm accepts \"myers\", "
5091 "\"minimal\", \"patience\" and \"histogram\""));
5093 /* clear out previous settings */
5094 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
5095 options
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
5096 options
->xdl_opts
|= value
;
5100 static int diff_opt_dirstat(const struct option
*opt
,
5101 const char *arg
, int unset
)
5103 struct diff_options
*options
= opt
->value
;
5105 BUG_ON_OPT_NEG(unset
);
5106 if (!strcmp(opt
->long_name
, "cumulative")) {
5108 BUG("how come --cumulative take a value?");
5110 } else if (!strcmp(opt
->long_name
, "dirstat-by-file"))
5111 parse_dirstat_opt(options
, "files");
5112 parse_dirstat_opt(options
, arg
? arg
: "");
5116 static int diff_opt_find_copies(const struct option
*opt
,
5117 const char *arg
, int unset
)
5119 struct diff_options
*options
= opt
->value
;
5121 BUG_ON_OPT_NEG(unset
);
5124 options
->rename_score
= parse_rename_score(&arg
);
5126 return error(_("invalid argument to %s"), opt
->long_name
);
5128 if (options
->detect_rename
== DIFF_DETECT_COPY
)
5129 options
->flags
.find_copies_harder
= 1;
5131 options
->detect_rename
= DIFF_DETECT_COPY
;
5136 static int diff_opt_find_renames(const struct option
*opt
,
5137 const char *arg
, int unset
)
5139 struct diff_options
*options
= opt
->value
;
5141 BUG_ON_OPT_NEG(unset
);
5144 options
->rename_score
= parse_rename_score(&arg
);
5146 return error(_("invalid argument to %s"), opt
->long_name
);
5148 options
->detect_rename
= DIFF_DETECT_RENAME
;
5152 static int diff_opt_follow(const struct option
*opt
,
5153 const char *arg
, int unset
)
5155 struct diff_options
*options
= opt
->value
;
5157 BUG_ON_OPT_ARG(arg
);
5159 options
->flags
.follow_renames
= 0;
5160 options
->flags
.default_follow_renames
= 0;
5162 options
->flags
.follow_renames
= 1;
5167 static int diff_opt_ignore_submodules(const struct option
*opt
,
5168 const char *arg
, int unset
)
5170 struct diff_options
*options
= opt
->value
;
5172 BUG_ON_OPT_NEG(unset
);
5175 options
->flags
.override_submodule_config
= 1;
5176 handle_ignore_submodules_arg(options
, arg
);
5180 static int diff_opt_line_prefix(const struct option
*opt
,
5181 const char *optarg
, int unset
)
5183 struct diff_options
*options
= opt
->value
;
5185 BUG_ON_OPT_NEG(unset
);
5186 options
->line_prefix
= optarg
;
5187 options
->line_prefix_length
= strlen(options
->line_prefix
);
5188 graph_setup_line_prefix(options
);
5192 static int diff_opt_no_prefix(const struct option
*opt
,
5193 const char *optarg
, int unset
)
5195 struct diff_options
*options
= opt
->value
;
5197 BUG_ON_OPT_NEG(unset
);
5198 BUG_ON_OPT_ARG(optarg
);
5199 options
->a_prefix
= "";
5200 options
->b_prefix
= "";
5204 static enum parse_opt_result
diff_opt_output(struct parse_opt_ctx_t
*ctx
,
5205 const struct option
*opt
,
5206 const char *arg
, int unset
)
5208 struct diff_options
*options
= opt
->value
;
5211 BUG_ON_OPT_NEG(unset
);
5212 path
= prefix_filename(ctx
->prefix
, arg
);
5213 options
->file
= xfopen(path
, "w");
5214 options
->close_file
= 1;
5215 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
5216 options
->use_color
= GIT_COLOR_NEVER
;
5221 static int diff_opt_patience(const struct option
*opt
,
5222 const char *arg
, int unset
)
5224 struct diff_options
*options
= opt
->value
;
5227 BUG_ON_OPT_NEG(unset
);
5228 BUG_ON_OPT_ARG(arg
);
5229 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
5231 * Both --patience and --anchored use PATIENCE_DIFF
5232 * internally, so remove any anchors previously
5235 for (i
= 0; i
< options
->anchors_nr
; i
++)
5236 free(options
->anchors
[i
]);
5237 options
->anchors_nr
= 0;
5241 static int diff_opt_ignore_regex(const struct option
*opt
,
5242 const char *arg
, int unset
)
5244 struct diff_options
*options
= opt
->value
;
5247 BUG_ON_OPT_NEG(unset
);
5248 regex
= xmalloc(sizeof(*regex
));
5249 if (regcomp(regex
, arg
, REG_EXTENDED
| REG_NEWLINE
))
5250 return error(_("invalid regex given to -I: '%s'"), arg
);
5251 ALLOC_GROW(options
->ignore_regex
, options
->ignore_regex_nr
+ 1,
5252 options
->ignore_regex_alloc
);
5253 options
->ignore_regex
[options
->ignore_regex_nr
++] = regex
;
5257 static int diff_opt_pickaxe_regex(const struct option
*opt
,
5258 const char *arg
, int unset
)
5260 struct diff_options
*options
= opt
->value
;
5262 BUG_ON_OPT_NEG(unset
);
5263 options
->pickaxe
= arg
;
5264 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
5268 static int diff_opt_pickaxe_string(const struct option
*opt
,
5269 const char *arg
, int unset
)
5271 struct diff_options
*options
= opt
->value
;
5273 BUG_ON_OPT_NEG(unset
);
5274 options
->pickaxe
= arg
;
5275 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
5279 static int diff_opt_relative(const struct option
*opt
,
5280 const char *arg
, int unset
)
5282 struct diff_options
*options
= opt
->value
;
5284 options
->flags
.relative_name
= !unset
;
5286 options
->prefix
= arg
;
5290 static int diff_opt_submodule(const struct option
*opt
,
5291 const char *arg
, int unset
)
5293 struct diff_options
*options
= opt
->value
;
5295 BUG_ON_OPT_NEG(unset
);
5298 if (parse_submodule_params(options
, arg
))
5299 return error(_("failed to parse --submodule option parameter: '%s'"),
5304 static int diff_opt_textconv(const struct option
*opt
,
5305 const char *arg
, int unset
)
5307 struct diff_options
*options
= opt
->value
;
5309 BUG_ON_OPT_ARG(arg
);
5311 options
->flags
.allow_textconv
= 0;
5313 options
->flags
.allow_textconv
= 1;
5314 options
->flags
.textconv_set_via_cmdline
= 1;
5319 static int diff_opt_unified(const struct option
*opt
,
5320 const char *arg
, int unset
)
5322 struct diff_options
*options
= opt
->value
;
5325 BUG_ON_OPT_NEG(unset
);
5328 options
->context
= strtol(arg
, &s
, 10);
5330 return error(_("%s expects a numerical value"), "--unified");
5332 enable_patch_output(&options
->output_format
);
5337 static int diff_opt_word_diff(const struct option
*opt
,
5338 const char *arg
, int unset
)
5340 struct diff_options
*options
= opt
->value
;
5342 BUG_ON_OPT_NEG(unset
);
5344 if (!strcmp(arg
, "plain"))
5345 options
->word_diff
= DIFF_WORDS_PLAIN
;
5346 else if (!strcmp(arg
, "color")) {
5347 options
->use_color
= 1;
5348 options
->word_diff
= DIFF_WORDS_COLOR
;
5350 else if (!strcmp(arg
, "porcelain"))
5351 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
5352 else if (!strcmp(arg
, "none"))
5353 options
->word_diff
= DIFF_WORDS_NONE
;
5355 return error(_("bad --word-diff argument: %s"), arg
);
5357 if (options
->word_diff
== DIFF_WORDS_NONE
)
5358 options
->word_diff
= DIFF_WORDS_PLAIN
;
5363 static int diff_opt_word_diff_regex(const struct option
*opt
,
5364 const char *arg
, int unset
)
5366 struct diff_options
*options
= opt
->value
;
5368 BUG_ON_OPT_NEG(unset
);
5369 if (options
->word_diff
== DIFF_WORDS_NONE
)
5370 options
->word_diff
= DIFF_WORDS_PLAIN
;
5371 options
->word_regex
= arg
;
5375 static int diff_opt_rotate_to(const struct option
*opt
, const char *arg
, int unset
)
5377 struct diff_options
*options
= opt
->value
;
5379 BUG_ON_OPT_NEG(unset
);
5380 if (!strcmp(opt
->long_name
, "skip-to"))
5381 options
->skip_instead_of_rotate
= 1;
5383 options
->skip_instead_of_rotate
= 0;
5384 options
->rotate_to
= arg
;
5388 static void prep_parse_options(struct diff_options
*options
)
5390 struct option parseopts
[] = {
5391 OPT_GROUP(N_("Diff output format options")),
5392 OPT_BITOP('p', "patch", &options
->output_format
,
5393 N_("generate patch"),
5394 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5395 OPT_BIT_F('s', "no-patch", &options
->output_format
,
5396 N_("suppress diff output"),
5397 DIFF_FORMAT_NO_OUTPUT
, PARSE_OPT_NONEG
),
5398 OPT_BITOP('u', NULL
, &options
->output_format
,
5399 N_("generate patch"),
5400 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5401 OPT_CALLBACK_F('U', "unified", options
, N_("<n>"),
5402 N_("generate diffs with <n> lines context"),
5403 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_unified
),
5404 OPT_BOOL('W', "function-context", &options
->flags
.funccontext
,
5405 N_("generate diffs with <n> lines context")),
5406 OPT_BIT_F(0, "raw", &options
->output_format
,
5407 N_("generate the diff in raw format"),
5408 DIFF_FORMAT_RAW
, PARSE_OPT_NONEG
),
5409 OPT_BITOP(0, "patch-with-raw", &options
->output_format
,
5410 N_("synonym for '-p --raw'"),
5411 DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
,
5412 DIFF_FORMAT_NO_OUTPUT
),
5413 OPT_BITOP(0, "patch-with-stat", &options
->output_format
,
5414 N_("synonym for '-p --stat'"),
5415 DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
,
5416 DIFF_FORMAT_NO_OUTPUT
),
5417 OPT_BIT_F(0, "numstat", &options
->output_format
,
5418 N_("machine friendly --stat"),
5419 DIFF_FORMAT_NUMSTAT
, PARSE_OPT_NONEG
),
5420 OPT_BIT_F(0, "shortstat", &options
->output_format
,
5421 N_("output only the last line of --stat"),
5422 DIFF_FORMAT_SHORTSTAT
, PARSE_OPT_NONEG
),
5423 OPT_CALLBACK_F('X', "dirstat", options
, N_("<param1,param2>..."),
5424 N_("output the distribution of relative amount of changes for each sub-directory"),
5425 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5427 OPT_CALLBACK_F(0, "cumulative", options
, NULL
,
5428 N_("synonym for --dirstat=cumulative"),
5429 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5431 OPT_CALLBACK_F(0, "dirstat-by-file", options
, N_("<param1,param2>..."),
5432 N_("synonym for --dirstat=files,param1,param2..."),
5433 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5435 OPT_BIT_F(0, "check", &options
->output_format
,
5436 N_("warn if changes introduce conflict markers or whitespace errors"),
5437 DIFF_FORMAT_CHECKDIFF
, PARSE_OPT_NONEG
),
5438 OPT_BIT_F(0, "summary", &options
->output_format
,
5439 N_("condensed summary such as creations, renames and mode changes"),
5440 DIFF_FORMAT_SUMMARY
, PARSE_OPT_NONEG
),
5441 OPT_BIT_F(0, "name-only", &options
->output_format
,
5442 N_("show only names of changed files"),
5443 DIFF_FORMAT_NAME
, PARSE_OPT_NONEG
),
5444 OPT_BIT_F(0, "name-status", &options
->output_format
,
5445 N_("show only names and status of changed files"),
5446 DIFF_FORMAT_NAME_STATUS
, PARSE_OPT_NONEG
),
5447 OPT_CALLBACK_F(0, "stat", options
, N_("<width>[,<name-width>[,<count>]]"),
5448 N_("generate diffstat"),
5449 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_stat
),
5450 OPT_CALLBACK_F(0, "stat-width", options
, N_("<width>"),
5451 N_("generate diffstat with a given width"),
5452 PARSE_OPT_NONEG
, diff_opt_stat
),
5453 OPT_CALLBACK_F(0, "stat-name-width", options
, N_("<width>"),
5454 N_("generate diffstat with a given name width"),
5455 PARSE_OPT_NONEG
, diff_opt_stat
),
5456 OPT_CALLBACK_F(0, "stat-graph-width", options
, N_("<width>"),
5457 N_("generate diffstat with a given graph width"),
5458 PARSE_OPT_NONEG
, diff_opt_stat
),
5459 OPT_CALLBACK_F(0, "stat-count", options
, N_("<count>"),
5460 N_("generate diffstat with limited lines"),
5461 PARSE_OPT_NONEG
, diff_opt_stat
),
5462 OPT_CALLBACK_F(0, "compact-summary", options
, NULL
,
5463 N_("generate compact summary in diffstat"),
5464 PARSE_OPT_NOARG
, diff_opt_compact_summary
),
5465 OPT_CALLBACK_F(0, "binary", options
, NULL
,
5466 N_("output a binary diff that can be applied"),
5467 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_binary
),
5468 OPT_BOOL(0, "full-index", &options
->flags
.full_index
,
5469 N_("show full pre- and post-image object names on the \"index\" lines")),
5470 OPT_COLOR_FLAG(0, "color", &options
->use_color
,
5471 N_("show colored diff")),
5472 OPT_CALLBACK_F(0, "ws-error-highlight", options
, N_("<kind>"),
5473 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5474 PARSE_OPT_NONEG
, diff_opt_ws_error_highlight
),
5475 OPT_SET_INT('z', NULL
, &options
->line_termination
,
5476 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5478 OPT__ABBREV(&options
->abbrev
),
5479 OPT_STRING_F(0, "src-prefix", &options
->a_prefix
, N_("<prefix>"),
5480 N_("show the given source prefix instead of \"a/\""),
5482 OPT_STRING_F(0, "dst-prefix", &options
->b_prefix
, N_("<prefix>"),
5483 N_("show the given destination prefix instead of \"b/\""),
5485 OPT_CALLBACK_F(0, "line-prefix", options
, N_("<prefix>"),
5486 N_("prepend an additional prefix to every line of output"),
5487 PARSE_OPT_NONEG
, diff_opt_line_prefix
),
5488 OPT_CALLBACK_F(0, "no-prefix", options
, NULL
,
5489 N_("do not show any source or destination prefix"),
5490 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_no_prefix
),
5491 OPT_INTEGER_F(0, "inter-hunk-context", &options
->interhunkcontext
,
5492 N_("show context between diff hunks up to the specified number of lines"),
5494 OPT_CALLBACK_F(0, "output-indicator-new",
5495 &options
->output_indicators
[OUTPUT_INDICATOR_NEW
],
5497 N_("specify the character to indicate a new line instead of '+'"),
5498 PARSE_OPT_NONEG
, diff_opt_char
),
5499 OPT_CALLBACK_F(0, "output-indicator-old",
5500 &options
->output_indicators
[OUTPUT_INDICATOR_OLD
],
5502 N_("specify the character to indicate an old line instead of '-'"),
5503 PARSE_OPT_NONEG
, diff_opt_char
),
5504 OPT_CALLBACK_F(0, "output-indicator-context",
5505 &options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
],
5507 N_("specify the character to indicate a context instead of ' '"),
5508 PARSE_OPT_NONEG
, diff_opt_char
),
5510 OPT_GROUP(N_("Diff rename options")),
5511 OPT_CALLBACK_F('B', "break-rewrites", &options
->break_opt
, N_("<n>[/<m>]"),
5512 N_("break complete rewrite changes into pairs of delete and create"),
5513 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5514 diff_opt_break_rewrites
),
5515 OPT_CALLBACK_F('M', "find-renames", options
, N_("<n>"),
5516 N_("detect renames"),
5517 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5518 diff_opt_find_renames
),
5519 OPT_SET_INT_F('D', "irreversible-delete", &options
->irreversible_delete
,
5520 N_("omit the preimage for deletes"),
5521 1, PARSE_OPT_NONEG
),
5522 OPT_CALLBACK_F('C', "find-copies", options
, N_("<n>"),
5523 N_("detect copies"),
5524 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5525 diff_opt_find_copies
),
5526 OPT_BOOL(0, "find-copies-harder", &options
->flags
.find_copies_harder
,
5527 N_("use unmodified files as source to find copies")),
5528 OPT_SET_INT_F(0, "no-renames", &options
->detect_rename
,
5529 N_("disable rename detection"),
5530 0, PARSE_OPT_NONEG
),
5531 OPT_BOOL(0, "rename-empty", &options
->flags
.rename_empty
,
5532 N_("use empty blobs as rename source")),
5533 OPT_CALLBACK_F(0, "follow", options
, NULL
,
5534 N_("continue listing the history of a file beyond renames"),
5535 PARSE_OPT_NOARG
, diff_opt_follow
),
5536 OPT_INTEGER('l', NULL
, &options
->rename_limit
,
5537 N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
5539 OPT_GROUP(N_("Diff algorithm options")),
5540 OPT_BIT(0, "minimal", &options
->xdl_opts
,
5541 N_("produce the smallest possible diff"),
5543 OPT_BIT_F('w', "ignore-all-space", &options
->xdl_opts
,
5544 N_("ignore whitespace when comparing lines"),
5545 XDF_IGNORE_WHITESPACE
, PARSE_OPT_NONEG
),
5546 OPT_BIT_F('b', "ignore-space-change", &options
->xdl_opts
,
5547 N_("ignore changes in amount of whitespace"),
5548 XDF_IGNORE_WHITESPACE_CHANGE
, PARSE_OPT_NONEG
),
5549 OPT_BIT_F(0, "ignore-space-at-eol", &options
->xdl_opts
,
5550 N_("ignore changes in whitespace at EOL"),
5551 XDF_IGNORE_WHITESPACE_AT_EOL
, PARSE_OPT_NONEG
),
5552 OPT_BIT_F(0, "ignore-cr-at-eol", &options
->xdl_opts
,
5553 N_("ignore carrier-return at the end of line"),
5554 XDF_IGNORE_CR_AT_EOL
, PARSE_OPT_NONEG
),
5555 OPT_BIT_F(0, "ignore-blank-lines", &options
->xdl_opts
,
5556 N_("ignore changes whose lines are all blank"),
5557 XDF_IGNORE_BLANK_LINES
, PARSE_OPT_NONEG
),
5558 OPT_CALLBACK_F('I', "ignore-matching-lines", options
, N_("<regex>"),
5559 N_("ignore changes whose all lines match <regex>"),
5560 0, diff_opt_ignore_regex
),
5561 OPT_BIT(0, "indent-heuristic", &options
->xdl_opts
,
5562 N_("heuristic to shift diff hunk boundaries for easy reading"),
5563 XDF_INDENT_HEURISTIC
),
5564 OPT_CALLBACK_F(0, "patience", options
, NULL
,
5565 N_("generate diff using the \"patience diff\" algorithm"),
5566 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5568 OPT_BITOP(0, "histogram", &options
->xdl_opts
,
5569 N_("generate diff using the \"histogram diff\" algorithm"),
5570 XDF_HISTOGRAM_DIFF
, XDF_DIFF_ALGORITHM_MASK
),
5571 OPT_CALLBACK_F(0, "diff-algorithm", options
, N_("<algorithm>"),
5572 N_("choose a diff algorithm"),
5573 PARSE_OPT_NONEG
, diff_opt_diff_algorithm
),
5574 OPT_CALLBACK_F(0, "anchored", options
, N_("<text>"),
5575 N_("generate diff using the \"anchored diff\" algorithm"),
5576 PARSE_OPT_NONEG
, diff_opt_anchored
),
5577 OPT_CALLBACK_F(0, "word-diff", options
, N_("<mode>"),
5578 N_("show word diff, using <mode> to delimit changed words"),
5579 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_word_diff
),
5580 OPT_CALLBACK_F(0, "word-diff-regex", options
, N_("<regex>"),
5581 N_("use <regex> to decide what a word is"),
5582 PARSE_OPT_NONEG
, diff_opt_word_diff_regex
),
5583 OPT_CALLBACK_F(0, "color-words", options
, N_("<regex>"),
5584 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5585 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_color_words
),
5586 OPT_CALLBACK_F(0, "color-moved", options
, N_("<mode>"),
5587 N_("moved lines of code are colored differently"),
5588 PARSE_OPT_OPTARG
, diff_opt_color_moved
),
5589 OPT_CALLBACK_F(0, "color-moved-ws", options
, N_("<mode>"),
5590 N_("how white spaces are ignored in --color-moved"),
5591 0, diff_opt_color_moved_ws
),
5593 OPT_GROUP(N_("Other diff options")),
5594 OPT_CALLBACK_F(0, "relative", options
, N_("<prefix>"),
5595 N_("when run from subdir, exclude changes outside and show relative paths"),
5598 OPT_BOOL('a', "text", &options
->flags
.text
,
5599 N_("treat all files as text")),
5600 OPT_BOOL('R', NULL
, &options
->flags
.reverse_diff
,
5601 N_("swap two inputs, reverse the diff")),
5602 OPT_BOOL(0, "exit-code", &options
->flags
.exit_with_status
,
5603 N_("exit with 1 if there were differences, 0 otherwise")),
5604 OPT_BOOL(0, "quiet", &options
->flags
.quick
,
5605 N_("disable all output of the program")),
5606 OPT_BOOL(0, "ext-diff", &options
->flags
.allow_external
,
5607 N_("allow an external diff helper to be executed")),
5608 OPT_CALLBACK_F(0, "textconv", options
, NULL
,
5609 N_("run external text conversion filters when comparing binary files"),
5610 PARSE_OPT_NOARG
, diff_opt_textconv
),
5611 OPT_CALLBACK_F(0, "ignore-submodules", options
, N_("<when>"),
5612 N_("ignore changes to submodules in the diff generation"),
5613 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5614 diff_opt_ignore_submodules
),
5615 OPT_CALLBACK_F(0, "submodule", options
, N_("<format>"),
5616 N_("specify how differences in submodules are shown"),
5617 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5618 diff_opt_submodule
),
5619 OPT_SET_INT_F(0, "ita-invisible-in-index", &options
->ita_invisible_in_index
,
5620 N_("hide 'git add -N' entries from the index"),
5621 1, PARSE_OPT_NONEG
),
5622 OPT_SET_INT_F(0, "ita-visible-in-index", &options
->ita_invisible_in_index
,
5623 N_("treat 'git add -N' entries as real in the index"),
5624 0, PARSE_OPT_NONEG
),
5625 OPT_CALLBACK_F('S', NULL
, options
, N_("<string>"),
5626 N_("look for differences that change the number of occurrences of the specified string"),
5627 0, diff_opt_pickaxe_string
),
5628 OPT_CALLBACK_F('G', NULL
, options
, N_("<regex>"),
5629 N_("look for differences that change the number of occurrences of the specified regex"),
5630 0, diff_opt_pickaxe_regex
),
5631 OPT_BIT_F(0, "pickaxe-all", &options
->pickaxe_opts
,
5632 N_("show all changes in the changeset with -S or -G"),
5633 DIFF_PICKAXE_ALL
, PARSE_OPT_NONEG
),
5634 OPT_BIT_F(0, "pickaxe-regex", &options
->pickaxe_opts
,
5635 N_("treat <string> in -S as extended POSIX regular expression"),
5636 DIFF_PICKAXE_REGEX
, PARSE_OPT_NONEG
),
5637 OPT_FILENAME('O', NULL
, &options
->orderfile
,
5638 N_("control the order in which files appear in the output")),
5639 OPT_CALLBACK_F(0, "rotate-to", options
, N_("<path>"),
5640 N_("show the change in the specified path first"),
5641 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5642 OPT_CALLBACK_F(0, "skip-to", options
, N_("<path>"),
5643 N_("skip the output to the specified path"),
5644 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5645 OPT_CALLBACK_F(0, "find-object", options
, N_("<object-id>"),
5646 N_("look for differences that change the number of occurrences of the specified object"),
5647 PARSE_OPT_NONEG
, diff_opt_find_object
),
5648 OPT_CALLBACK_F(0, "diff-filter", options
, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5649 N_("select files by diff type"),
5650 PARSE_OPT_NONEG
, diff_opt_diff_filter
),
5651 { OPTION_CALLBACK
, 0, "output", options
, N_("<file>"),
5652 N_("Output to a specific file"),
5653 PARSE_OPT_NONEG
, NULL
, 0, diff_opt_output
},
5658 ALLOC_ARRAY(options
->parseopts
, ARRAY_SIZE(parseopts
));
5659 memcpy(options
->parseopts
, parseopts
, sizeof(parseopts
));
5662 int diff_opt_parse(struct diff_options
*options
,
5663 const char **av
, int ac
, const char *prefix
)
5668 ac
= parse_options(ac
, av
, prefix
, options
->parseopts
, NULL
,
5669 PARSE_OPT_KEEP_DASHDASH
|
5670 PARSE_OPT_KEEP_UNKNOWN
|
5671 PARSE_OPT_NO_INTERNAL_HELP
|
5672 PARSE_OPT_ONE_SHOT
|
5673 PARSE_OPT_STOP_AT_NON_OPTION
);
5678 int parse_rename_score(const char **cp_p
)
5680 unsigned long num
, scale
;
5682 const char *cp
= *cp_p
;
5689 if ( !dot
&& ch
== '.' ) {
5692 } else if ( ch
== '%' ) {
5693 scale
= dot
? scale
*100 : 100;
5694 cp
++; /* % is always at the end */
5696 } else if ( ch
>= '0' && ch
<= '9' ) {
5697 if ( scale
< 100000 ) {
5699 num
= (num
*10) + (ch
-'0');
5708 /* user says num divided by scale and we say internally that
5709 * is MAX_SCORE * num / scale.
5711 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
5714 struct diff_queue_struct diff_queued_diff
;
5716 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
5718 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
5719 queue
->queue
[queue
->nr
++] = dp
;
5722 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
5723 struct diff_filespec
*one
,
5724 struct diff_filespec
*two
)
5726 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
5734 void diff_free_filepair(struct diff_filepair
*p
)
5736 free_filespec(p
->one
);
5737 free_filespec(p
->two
);
5741 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
5746 /* Do we want all 40 hex characters? */
5747 if (len
== the_hash_algo
->hexsz
)
5748 return oid_to_hex(oid
);
5750 /* An abbreviated value is fine, possibly followed by an ellipsis. */
5751 abbrev
= diff_abbrev_oid(oid
, len
);
5753 if (!print_sha1_ellipsis())
5756 abblen
= strlen(abbrev
);
5759 * In well-behaved cases, where the abbreviated result is the
5760 * same as the requested length, append three dots after the
5761 * abbreviation (hence the whole logic is limited to the case
5762 * where abblen < 37); when the actual abbreviated result is a
5763 * bit longer than the requested length, we reduce the number
5764 * of dots so that they match the well-behaved ones. However,
5765 * if the actual abbreviation is longer than the requested
5766 * length by more than three, we give up on aligning, and add
5767 * three dots anyway, to indicate that the output is not the
5768 * full object name. Yes, this may be suboptimal, but this
5769 * appears only in "diff --raw --abbrev" output and it is not
5770 * worth the effort to change it now. Note that this would
5771 * likely to work fine when the automatic sizing of default
5772 * abbreviation length is used--we would be fed -1 in "len" in
5773 * that case, and will end up always appending three-dots, but
5774 * the automatic sizing is supposed to give abblen that ensures
5775 * uniqueness across all objects (statistically speaking).
5777 if (abblen
< the_hash_algo
->hexsz
- 3) {
5778 static char hex
[GIT_MAX_HEXSZ
+ 1];
5779 if (len
< abblen
&& abblen
<= len
+ 2)
5780 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
5782 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
5786 return oid_to_hex(oid
);
5789 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
5791 int line_termination
= opt
->line_termination
;
5792 int inter_name_termination
= line_termination
? '\t' : '\0';
5794 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5795 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
5796 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
5797 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
5798 fprintf(opt
->file
, "%s ",
5799 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
5802 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
5803 inter_name_termination
);
5805 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
5808 if (p
->status
== DIFF_STATUS_COPIED
||
5809 p
->status
== DIFF_STATUS_RENAMED
) {
5810 const char *name_a
, *name_b
;
5811 name_a
= p
->one
->path
;
5812 name_b
= p
->two
->path
;
5813 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5814 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
5815 write_name_quoted(name_b
, opt
->file
, line_termination
);
5817 const char *name_a
, *name_b
;
5818 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
5820 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5821 write_name_quoted(name_a
, opt
->file
, line_termination
);
5825 int diff_unmodified_pair(struct diff_filepair
*p
)
5827 /* This function is written stricter than necessary to support
5828 * the currently implemented transformers, but the idea is to
5829 * let transformers to produce diff_filepairs any way they want,
5830 * and filter and clean them up here before producing the output.
5832 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
5834 if (DIFF_PAIR_UNMERGED(p
))
5835 return 0; /* unmerged is interesting */
5837 /* deletion, addition, mode or type change
5838 * and rename are all interesting.
5840 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
5841 DIFF_PAIR_MODE_CHANGED(p
) ||
5842 strcmp(one
->path
, two
->path
))
5845 /* both are valid and point at the same path. that is, we are
5846 * dealing with a change.
5848 if (one
->oid_valid
&& two
->oid_valid
&&
5849 oideq(&one
->oid
, &two
->oid
) &&
5850 !one
->dirty_submodule
&& !two
->dirty_submodule
)
5851 return 1; /* no change */
5852 if (!one
->oid_valid
&& !two
->oid_valid
)
5853 return 1; /* both look at the same file on the filesystem. */
5857 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
5859 if (diff_unmodified_pair(p
))
5862 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5863 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5864 return; /* no tree diffs in patch format */
5869 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
5870 struct diffstat_t
*diffstat
)
5872 if (diff_unmodified_pair(p
))
5875 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5876 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5877 return; /* no useful stat for tree diffs */
5879 run_diffstat(p
, o
, diffstat
);
5882 static void diff_flush_checkdiff(struct diff_filepair
*p
,
5883 struct diff_options
*o
)
5885 if (diff_unmodified_pair(p
))
5888 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5889 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5890 return; /* nothing to check in tree diffs */
5892 run_checkdiff(p
, o
);
5895 int diff_queue_is_empty(void)
5897 struct diff_queue_struct
*q
= &diff_queued_diff
;
5899 for (i
= 0; i
< q
->nr
; i
++)
5900 if (!diff_unmodified_pair(q
->queue
[i
]))
5906 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
5908 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
5911 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
5913 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
5914 fprintf(stderr
, "queue[%d] %s size %lu\n",
5919 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
5921 diff_debug_filespec(p
->one
, i
, "one");
5922 diff_debug_filespec(p
->two
, i
, "two");
5923 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
5924 p
->score
, p
->status
? p
->status
: '?',
5925 p
->one
->rename_used
, p
->broken_pair
);
5928 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
5932 fprintf(stderr
, "%s\n", msg
);
5933 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
5934 for (i
= 0; i
< q
->nr
; i
++) {
5935 struct diff_filepair
*p
= q
->queue
[i
];
5936 diff_debug_filepair(p
, i
);
5941 static void diff_resolve_rename_copy(void)
5944 struct diff_filepair
*p
;
5945 struct diff_queue_struct
*q
= &diff_queued_diff
;
5947 diff_debug_queue("resolve-rename-copy", q
);
5949 for (i
= 0; i
< q
->nr
; i
++) {
5951 p
->status
= 0; /* undecided */
5952 if (DIFF_PAIR_UNMERGED(p
))
5953 p
->status
= DIFF_STATUS_UNMERGED
;
5954 else if (!DIFF_FILE_VALID(p
->one
))
5955 p
->status
= DIFF_STATUS_ADDED
;
5956 else if (!DIFF_FILE_VALID(p
->two
))
5957 p
->status
= DIFF_STATUS_DELETED
;
5958 else if (DIFF_PAIR_TYPE_CHANGED(p
))
5959 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
5961 /* from this point on, we are dealing with a pair
5962 * whose both sides are valid and of the same type, i.e.
5963 * either in-place edit or rename/copy edit.
5965 else if (DIFF_PAIR_RENAME(p
)) {
5967 * A rename might have re-connected a broken
5968 * pair up, causing the pathnames to be the
5969 * same again. If so, that's not a rename at
5970 * all, just a modification..
5972 * Otherwise, see if this source was used for
5973 * multiple renames, in which case we decrement
5974 * the count, and call it a copy.
5976 if (!strcmp(p
->one
->path
, p
->two
->path
))
5977 p
->status
= DIFF_STATUS_MODIFIED
;
5978 else if (--p
->one
->rename_used
> 0)
5979 p
->status
= DIFF_STATUS_COPIED
;
5981 p
->status
= DIFF_STATUS_RENAMED
;
5983 else if (!oideq(&p
->one
->oid
, &p
->two
->oid
) ||
5984 p
->one
->mode
!= p
->two
->mode
||
5985 p
->one
->dirty_submodule
||
5986 p
->two
->dirty_submodule
||
5987 is_null_oid(&p
->one
->oid
))
5988 p
->status
= DIFF_STATUS_MODIFIED
;
5990 /* This is a "no-change" entry and should not
5991 * happen anymore, but prepare for broken callers.
5993 error("feeding unmodified %s to diffcore",
5995 p
->status
= DIFF_STATUS_UNKNOWN
;
5998 diff_debug_queue("resolve-rename-copy done", q
);
6001 static int check_pair_status(struct diff_filepair
*p
)
6003 switch (p
->status
) {
6004 case DIFF_STATUS_UNKNOWN
:
6007 die("internal error in diff-resolve-rename-copy");
6013 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
6015 int fmt
= opt
->output_format
;
6017 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
6018 diff_flush_checkdiff(p
, opt
);
6019 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
6020 diff_flush_raw(p
, opt
);
6021 else if (fmt
& DIFF_FORMAT_NAME
) {
6022 const char *name_a
, *name_b
;
6023 name_a
= p
->two
->path
;
6025 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6026 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
6027 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
6031 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
6033 struct strbuf sb
= STRBUF_INIT
;
6035 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
6037 strbuf_addf(&sb
, " %s ", newdelete
);
6039 quote_c_style(fs
->path
, &sb
, NULL
, 0);
6040 strbuf_addch(&sb
, '\n');
6041 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6043 strbuf_release(&sb
);
6046 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
6049 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
6050 struct strbuf sb
= STRBUF_INIT
;
6051 strbuf_addf(&sb
, " mode change %06o => %06o",
6052 p
->one
->mode
, p
->two
->mode
);
6054 strbuf_addch(&sb
, ' ');
6055 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6057 strbuf_addch(&sb
, '\n');
6058 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6060 strbuf_release(&sb
);
6064 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
6065 struct diff_filepair
*p
)
6067 struct strbuf sb
= STRBUF_INIT
;
6068 struct strbuf names
= STRBUF_INIT
;
6070 pprint_rename(&names
, p
->one
->path
, p
->two
->path
);
6071 strbuf_addf(&sb
, " %s %s (%d%%)\n",
6072 renamecopy
, names
.buf
, similarity_index(p
));
6073 strbuf_release(&names
);
6074 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6076 show_mode_change(opt
, p
, 0);
6077 strbuf_release(&sb
);
6080 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
6083 case DIFF_STATUS_DELETED
:
6084 show_file_mode_name(opt
, "delete", p
->one
);
6086 case DIFF_STATUS_ADDED
:
6087 show_file_mode_name(opt
, "create", p
->two
);
6089 case DIFF_STATUS_COPIED
:
6090 show_rename_copy(opt
, "copy", p
);
6092 case DIFF_STATUS_RENAMED
:
6093 show_rename_copy(opt
, "rename", p
);
6097 struct strbuf sb
= STRBUF_INIT
;
6098 strbuf_addstr(&sb
, " rewrite ");
6099 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6100 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
6101 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6103 strbuf_release(&sb
);
6105 show_mode_change(opt
, p
, !p
->score
);
6115 static int remove_space(char *line
, int len
)
6121 for (i
= 0; i
< len
; i
++)
6122 if (!isspace((c
= line
[i
])))
6128 void flush_one_hunk(struct object_id
*result
, git_hash_ctx
*ctx
)
6130 unsigned char hash
[GIT_MAX_RAWSZ
];
6131 unsigned short carry
= 0;
6134 the_hash_algo
->final_fn(hash
, ctx
);
6135 the_hash_algo
->init_fn(ctx
);
6136 /* 20-byte sum, with carry */
6137 for (i
= 0; i
< the_hash_algo
->rawsz
; ++i
) {
6138 carry
+= result
->hash
[i
] + hash
[i
];
6139 result
->hash
[i
] = carry
;
6144 static int patch_id_consume(void *priv
, char *line
, unsigned long len
)
6146 struct patch_id_t
*data
= priv
;
6149 if (len
> 12 && starts_with(line
, "\\ "))
6151 new_len
= remove_space(line
, len
);
6153 the_hash_algo
->update_fn(data
->ctx
, line
, new_len
);
6154 data
->patchlen
+= new_len
;
6158 static void patch_id_add_string(git_hash_ctx
*ctx
, const char *str
)
6160 the_hash_algo
->update_fn(ctx
, str
, strlen(str
));
6163 static void patch_id_add_mode(git_hash_ctx
*ctx
, unsigned mode
)
6165 /* large enough for 2^32 in octal */
6167 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
6168 the_hash_algo
->update_fn(ctx
, buf
, len
);
6171 /* returns 0 upon success, and writes result into oid */
6172 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
, int stable
)
6174 struct diff_queue_struct
*q
= &diff_queued_diff
;
6177 struct patch_id_t data
;
6179 the_hash_algo
->init_fn(&ctx
);
6180 memset(&data
, 0, sizeof(struct patch_id_t
));
6184 for (i
= 0; i
< q
->nr
; i
++) {
6188 struct diff_filepair
*p
= q
->queue
[i
];
6191 memset(&xpp
, 0, sizeof(xpp
));
6192 memset(&xecfg
, 0, sizeof(xecfg
));
6194 return error("internal diff status error");
6195 if (p
->status
== DIFF_STATUS_UNKNOWN
)
6197 if (diff_unmodified_pair(p
))
6199 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6200 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6202 if (DIFF_PAIR_UNMERGED(p
))
6205 diff_fill_oid_info(p
->one
, options
->repo
->index
);
6206 diff_fill_oid_info(p
->two
, options
->repo
->index
);
6208 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
6209 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
6210 patch_id_add_string(&ctx
, "diff--git");
6211 patch_id_add_string(&ctx
, "a/");
6212 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6213 patch_id_add_string(&ctx
, "b/");
6214 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6216 if (p
->one
->mode
== 0) {
6217 patch_id_add_string(&ctx
, "newfilemode");
6218 patch_id_add_mode(&ctx
, p
->two
->mode
);
6219 patch_id_add_string(&ctx
, "---/dev/null");
6220 patch_id_add_string(&ctx
, "+++b/");
6221 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6222 } else if (p
->two
->mode
== 0) {
6223 patch_id_add_string(&ctx
, "deletedfilemode");
6224 patch_id_add_mode(&ctx
, p
->one
->mode
);
6225 patch_id_add_string(&ctx
, "---a/");
6226 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6227 patch_id_add_string(&ctx
, "+++/dev/null");
6229 patch_id_add_string(&ctx
, "---a/");
6230 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6231 patch_id_add_string(&ctx
, "+++b/");
6232 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6235 if (diff_header_only
)
6238 if (fill_mmfile(options
->repo
, &mf1
, p
->one
) < 0 ||
6239 fill_mmfile(options
->repo
, &mf2
, p
->two
) < 0)
6240 return error("unable to read files to diff");
6242 if (diff_filespec_is_binary(options
->repo
, p
->one
) ||
6243 diff_filespec_is_binary(options
->repo
, p
->two
)) {
6244 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->one
->oid
),
6245 the_hash_algo
->hexsz
);
6246 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->two
->oid
),
6247 the_hash_algo
->hexsz
);
6253 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
6254 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
6255 patch_id_consume
, &data
, &xpp
, &xecfg
))
6256 return error("unable to generate patch-id diff for %s",
6260 flush_one_hunk(oid
, &ctx
);
6264 the_hash_algo
->final_oid_fn(oid
, &ctx
);
6269 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
, int stable
)
6271 struct diff_queue_struct
*q
= &diff_queued_diff
;
6273 int result
= diff_get_patch_id(options
, oid
, diff_header_only
, stable
);
6275 for (i
= 0; i
< q
->nr
; i
++)
6276 diff_free_filepair(q
->queue
[i
]);
6279 DIFF_QUEUE_CLEAR(q
);
6284 static int is_summary_empty(const struct diff_queue_struct
*q
)
6288 for (i
= 0; i
< q
->nr
; i
++) {
6289 const struct diff_filepair
*p
= q
->queue
[i
];
6291 switch (p
->status
) {
6292 case DIFF_STATUS_DELETED
:
6293 case DIFF_STATUS_ADDED
:
6294 case DIFF_STATUS_COPIED
:
6295 case DIFF_STATUS_RENAMED
:
6300 if (p
->one
->mode
&& p
->two
->mode
&&
6301 p
->one
->mode
!= p
->two
->mode
)
6309 static const char rename_limit_warning
[] =
6310 N_("exhaustive rename detection was skipped due to too many files.");
6312 static const char degrade_cc_to_c_warning
[] =
6313 N_("only found copies from modified paths due to too many files.");
6315 static const char rename_limit_advice
[] =
6316 N_("you may want to set your %s variable to at least "
6317 "%d and retry the command.");
6319 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
6323 warning(_(degrade_cc_to_c_warning
));
6325 warning(_(rename_limit_warning
));
6329 warning(_(rename_limit_advice
), varname
, needed
);
6332 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
6335 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
6336 struct diff_queue_struct
*q
= &diff_queued_diff
;
6338 if (WSEH_NEW
& WS_RULE_MASK
)
6339 BUG("WS rules bit mask overlaps with diff symbol flags");
6342 o
->emitted_symbols
= &esm
;
6344 for (i
= 0; i
< q
->nr
; i
++) {
6345 struct diff_filepair
*p
= q
->queue
[i
];
6346 if (check_pair_status(p
))
6347 diff_flush_patch(p
, o
);
6350 if (o
->emitted_symbols
) {
6351 if (o
->color_moved
) {
6352 struct hashmap add_lines
, del_lines
;
6354 if (o
->color_moved_ws_handling
&
6355 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
6356 o
->color_moved_ws_handling
|= XDF_IGNORE_WHITESPACE
;
6358 hashmap_init(&del_lines
, moved_entry_cmp
, o
, 0);
6359 hashmap_init(&add_lines
, moved_entry_cmp
, o
, 0);
6361 add_lines_to_move_detection(o
, &add_lines
, &del_lines
);
6362 mark_color_as_moved(o
, &add_lines
, &del_lines
);
6363 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
6366 hashmap_clear_and_free(&add_lines
, struct moved_entry
,
6368 hashmap_clear_and_free(&del_lines
, struct moved_entry
,
6372 for (i
= 0; i
< esm
.nr
; i
++)
6373 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
6375 for (i
= 0; i
< esm
.nr
; i
++)
6376 free((void *)esm
.buf
[i
].line
);
6379 o
->emitted_symbols
= NULL
;
6383 static void diff_free_file(struct diff_options
*options
)
6385 if (options
->close_file
)
6386 fclose(options
->file
);
6389 static void diff_free_ignore_regex(struct diff_options
*options
)
6393 for (i
= 0; i
< options
->ignore_regex_nr
; i
++) {
6394 regfree(options
->ignore_regex
[i
]);
6395 free(options
->ignore_regex
[i
]);
6397 free(options
->ignore_regex
);
6400 void diff_free(struct diff_options
*options
)
6402 if (options
->no_free
)
6405 diff_free_file(options
);
6406 diff_free_ignore_regex(options
);
6409 void diff_flush(struct diff_options
*options
)
6411 struct diff_queue_struct
*q
= &diff_queued_diff
;
6412 int i
, output_format
= options
->output_format
;
6414 int dirstat_by_line
= 0;
6417 * Order: raw, stat, summary, patch
6418 * or: name/name-status/checkdiff (other bits clear)
6423 if (output_format
& (DIFF_FORMAT_RAW
|
6425 DIFF_FORMAT_NAME_STATUS
|
6426 DIFF_FORMAT_CHECKDIFF
)) {
6427 for (i
= 0; i
< q
->nr
; i
++) {
6428 struct diff_filepair
*p
= q
->queue
[i
];
6429 if (check_pair_status(p
))
6430 flush_one_pair(p
, options
);
6435 if (output_format
& DIFF_FORMAT_DIRSTAT
&& options
->flags
.dirstat_by_line
)
6436 dirstat_by_line
= 1;
6438 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
6440 struct diffstat_t diffstat
;
6442 compute_diffstat(options
, &diffstat
, q
);
6443 if (output_format
& DIFF_FORMAT_NUMSTAT
)
6444 show_numstat(&diffstat
, options
);
6445 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
6446 show_stats(&diffstat
, options
);
6447 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
6448 show_shortstats(&diffstat
, options
);
6449 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
6450 show_dirstat_by_line(&diffstat
, options
);
6451 free_diffstat_info(&diffstat
);
6454 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
6455 show_dirstat(options
);
6457 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
6458 for (i
= 0; i
< q
->nr
; i
++) {
6459 diff_summary(options
, q
->queue
[i
]);
6464 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
6465 options
->flags
.exit_with_status
&&
6466 options
->flags
.diff_from_contents
) {
6468 * run diff_flush_patch for the exit status. setting
6469 * options->file to /dev/null should be safe, because we
6470 * aren't supposed to produce any output anyway.
6472 diff_free_file(options
);
6473 options
->file
= xfopen("/dev/null", "w");
6474 options
->close_file
= 1;
6475 options
->color_moved
= 0;
6476 for (i
= 0; i
< q
->nr
; i
++) {
6477 struct diff_filepair
*p
= q
->queue
[i
];
6478 if (check_pair_status(p
))
6479 diff_flush_patch(p
, options
);
6480 if (options
->found_changes
)
6485 if (output_format
& DIFF_FORMAT_PATCH
) {
6487 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
6488 if (options
->stat_sep
)
6489 /* attach patch instead of inline */
6490 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
6494 diff_flush_patch_all_file_pairs(options
);
6497 if (output_format
& DIFF_FORMAT_CALLBACK
)
6498 options
->format_callback(q
, options
, options
->format_callback_data
);
6500 for (i
= 0; i
< q
->nr
; i
++)
6501 diff_free_filepair(q
->queue
[i
]);
6504 DIFF_QUEUE_CLEAR(q
);
6508 * Report the content-level differences with HAS_CHANGES;
6509 * diff_addremove/diff_change does not set the bit when
6510 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6512 if (options
->flags
.diff_from_contents
) {
6513 if (options
->found_changes
)
6514 options
->flags
.has_changes
= 1;
6516 options
->flags
.has_changes
= 0;
6520 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
6522 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
6524 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
6526 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
6527 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
6528 filter_bit_tst(p
->status
, options
)));
6531 static void diffcore_apply_filter(struct diff_options
*options
)
6534 struct diff_queue_struct
*q
= &diff_queued_diff
;
6535 struct diff_queue_struct outq
;
6537 DIFF_QUEUE_CLEAR(&outq
);
6539 if (!options
->filter
)
6542 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
6544 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
6545 if (match_filter(options
, q
->queue
[i
]))
6551 /* otherwise we will clear the whole queue
6552 * by copying the empty outq at the end of this
6553 * function, but first clear the current entries
6556 for (i
= 0; i
< q
->nr
; i
++)
6557 diff_free_filepair(q
->queue
[i
]);
6560 /* Only the matching ones */
6561 for (i
= 0; i
< q
->nr
; i
++) {
6562 struct diff_filepair
*p
= q
->queue
[i
];
6563 if (match_filter(options
, p
))
6566 diff_free_filepair(p
);
6573 /* Check whether two filespecs with the same mode and size are identical */
6574 static int diff_filespec_is_identical(struct repository
*r
,
6575 struct diff_filespec
*one
,
6576 struct diff_filespec
*two
)
6578 if (S_ISGITLINK(one
->mode
))
6580 if (diff_populate_filespec(r
, one
, NULL
))
6582 if (diff_populate_filespec(r
, two
, NULL
))
6584 return !memcmp(one
->data
, two
->data
, one
->size
);
6587 static int diff_filespec_check_stat_unmatch(struct repository
*r
,
6588 struct diff_filepair
*p
)
6590 struct diff_populate_filespec_options dpf_options
= {
6591 .check_size_only
= 1,
6592 .missing_object_cb
= diff_queued_diff_prefetch
,
6593 .missing_object_data
= r
,
6596 if (p
->done_skip_stat_unmatch
)
6597 return p
->skip_stat_unmatch_result
;
6599 p
->done_skip_stat_unmatch
= 1;
6600 p
->skip_stat_unmatch_result
= 0;
6602 * 1. Entries that come from stat info dirtiness
6603 * always have both sides (iow, not create/delete),
6604 * one side of the object name is unknown, with
6605 * the same mode and size. Keep the ones that
6606 * do not match these criteria. They have real
6609 * 2. At this point, the file is known to be modified,
6610 * with the same mode and size, and the object
6611 * name of one side is unknown. Need to inspect
6612 * the identical contents.
6614 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
6615 !DIFF_FILE_VALID(p
->two
) ||
6616 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
6617 (p
->one
->mode
!= p
->two
->mode
) ||
6618 diff_populate_filespec(r
, p
->one
, &dpf_options
) ||
6619 diff_populate_filespec(r
, p
->two
, &dpf_options
) ||
6620 (p
->one
->size
!= p
->two
->size
) ||
6621 !diff_filespec_is_identical(r
, p
->one
, p
->two
)) /* (2) */
6622 p
->skip_stat_unmatch_result
= 1;
6623 return p
->skip_stat_unmatch_result
;
6626 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
6629 struct diff_queue_struct
*q
= &diff_queued_diff
;
6630 struct diff_queue_struct outq
;
6631 DIFF_QUEUE_CLEAR(&outq
);
6633 for (i
= 0; i
< q
->nr
; i
++) {
6634 struct diff_filepair
*p
= q
->queue
[i
];
6636 if (diff_filespec_check_stat_unmatch(diffopt
->repo
, p
))
6640 * The caller can subtract 1 from skip_stat_unmatch
6641 * to determine how many paths were dirty only
6642 * due to stat info mismatch.
6644 if (!diffopt
->flags
.no_index
)
6645 diffopt
->skip_stat_unmatch
++;
6646 diff_free_filepair(p
);
6653 static int diffnamecmp(const void *a_
, const void *b_
)
6655 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
6656 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
6657 const char *name_a
, *name_b
;
6659 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
6660 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
6661 return strcmp(name_a
, name_b
);
6664 void diffcore_fix_diff_index(void)
6666 struct diff_queue_struct
*q
= &diff_queued_diff
;
6667 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
6670 void diff_add_if_missing(struct repository
*r
,
6671 struct oid_array
*to_fetch
,
6672 const struct diff_filespec
*filespec
)
6674 if (filespec
&& filespec
->oid_valid
&&
6675 !S_ISGITLINK(filespec
->mode
) &&
6676 oid_object_info_extended(r
, &filespec
->oid
, NULL
,
6677 OBJECT_INFO_FOR_PREFETCH
))
6678 oid_array_append(to_fetch
, &filespec
->oid
);
6681 void diff_queued_diff_prefetch(void *repository
)
6683 struct repository
*repo
= repository
;
6685 struct diff_queue_struct
*q
= &diff_queued_diff
;
6686 struct oid_array to_fetch
= OID_ARRAY_INIT
;
6688 for (i
= 0; i
< q
->nr
; i
++) {
6689 struct diff_filepair
*p
= q
->queue
[i
];
6690 diff_add_if_missing(repo
, &to_fetch
, p
->one
);
6691 diff_add_if_missing(repo
, &to_fetch
, p
->two
);
6695 * NEEDSWORK: Consider deduplicating the OIDs sent.
6697 promisor_remote_get_direct(repo
, to_fetch
.oid
, to_fetch
.nr
);
6699 oid_array_clear(&to_fetch
);
6702 void diffcore_std(struct diff_options
*options
)
6704 int output_formats_to_prefetch
= DIFF_FORMAT_DIFFSTAT
|
6705 DIFF_FORMAT_NUMSTAT
|
6707 DIFF_FORMAT_SHORTSTAT
|
6708 DIFF_FORMAT_DIRSTAT
;
6711 * Check if the user requested a blob-data-requiring diff output and/or
6712 * break-rewrite detection (which requires blob data). If yes, prefetch
6715 * If no prefetching occurs, diffcore_rename() will prefetch if it
6716 * decides that it needs inexact rename detection.
6718 if (options
->repo
== the_repository
&& has_promisor_remote() &&
6719 (options
->output_format
& output_formats_to_prefetch
||
6720 options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
6721 diff_queued_diff_prefetch(options
->repo
);
6723 /* NOTE please keep the following in sync with diff_tree_combined() */
6724 if (options
->skip_stat_unmatch
)
6725 diffcore_skip_stat_unmatch(options
);
6726 if (!options
->found_follow
) {
6727 /* See try_to_follow_renames() in tree-diff.c */
6728 if (options
->break_opt
!= -1)
6729 diffcore_break(options
->repo
,
6730 options
->break_opt
);
6731 if (options
->detect_rename
)
6732 diffcore_rename(options
);
6733 if (options
->break_opt
!= -1)
6734 diffcore_merge_broken();
6736 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
6737 diffcore_pickaxe(options
);
6738 if (options
->orderfile
)
6739 diffcore_order(options
->orderfile
);
6740 if (options
->rotate_to
)
6741 diffcore_rotate(options
);
6742 if (!options
->found_follow
)
6743 /* See try_to_follow_renames() in tree-diff.c */
6744 diff_resolve_rename_copy();
6745 diffcore_apply_filter(options
);
6747 if (diff_queued_diff
.nr
&& !options
->flags
.diff_from_contents
)
6748 options
->flags
.has_changes
= 1;
6750 options
->flags
.has_changes
= 0;
6752 options
->found_follow
= 0;
6755 int diff_result_code(struct diff_options
*opt
, int status
)
6759 diff_warn_rename_limit("diff.renameLimit",
6760 opt
->needed_rename_limit
,
6761 opt
->degraded_cc_to_c
);
6762 if (!opt
->flags
.exit_with_status
&&
6763 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
6765 if (opt
->flags
.exit_with_status
&&
6766 opt
->flags
.has_changes
)
6768 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
6769 opt
->flags
.check_failed
)
6774 int diff_can_quit_early(struct diff_options
*opt
)
6776 return (opt
->flags
.quick
&&
6778 opt
->flags
.has_changes
);
6782 * Shall changes to this submodule be ignored?
6784 * Submodule changes can be configured to be ignored separately for each path,
6785 * but that configuration can be overridden from the command line.
6787 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
6790 struct diff_flags orig_flags
= options
->flags
;
6791 if (!options
->flags
.override_submodule_config
)
6792 set_diffopt_flags_from_submodule_config(options
, path
);
6793 if (options
->flags
.ignore_submodules
)
6795 options
->flags
= orig_flags
;
6799 void compute_diffstat(struct diff_options
*options
,
6800 struct diffstat_t
*diffstat
,
6801 struct diff_queue_struct
*q
)
6805 memset(diffstat
, 0, sizeof(struct diffstat_t
));
6806 for (i
= 0; i
< q
->nr
; i
++) {
6807 struct diff_filepair
*p
= q
->queue
[i
];
6808 if (check_pair_status(p
))
6809 diff_flush_stat(p
, options
, diffstat
);
6813 void diff_addremove(struct diff_options
*options
,
6814 int addremove
, unsigned mode
,
6815 const struct object_id
*oid
,
6817 const char *concatpath
, unsigned dirty_submodule
)
6819 struct diff_filespec
*one
, *two
;
6821 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
6824 /* This may look odd, but it is a preparation for
6825 * feeding "there are unchanged files which should
6826 * not produce diffs, but when you are doing copy
6827 * detection you would need them, so here they are"
6828 * entries to the diff-core. They will be prefixed
6829 * with something like '=' or '*' (I haven't decided
6830 * which but should not make any difference).
6831 * Feeding the same new and old to diff_change()
6832 * also has the same effect.
6833 * Before the final output happens, they are pruned after
6834 * merged into rename/copy pairs as appropriate.
6836 if (options
->flags
.reverse_diff
)
6837 addremove
= (addremove
== '+' ? '-' :
6838 addremove
== '-' ? '+' : addremove
);
6840 if (options
->prefix
&&
6841 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
6844 one
= alloc_filespec(concatpath
);
6845 two
= alloc_filespec(concatpath
);
6847 if (addremove
!= '+')
6848 fill_filespec(one
, oid
, oid_valid
, mode
);
6849 if (addremove
!= '-') {
6850 fill_filespec(two
, oid
, oid_valid
, mode
);
6851 two
->dirty_submodule
= dirty_submodule
;
6854 diff_queue(&diff_queued_diff
, one
, two
);
6855 if (!options
->flags
.diff_from_contents
)
6856 options
->flags
.has_changes
= 1;
6859 void diff_change(struct diff_options
*options
,
6860 unsigned old_mode
, unsigned new_mode
,
6861 const struct object_id
*old_oid
,
6862 const struct object_id
*new_oid
,
6863 int old_oid_valid
, int new_oid_valid
,
6864 const char *concatpath
,
6865 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
6867 struct diff_filespec
*one
, *two
;
6868 struct diff_filepair
*p
;
6870 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
6871 is_submodule_ignored(concatpath
, options
))
6874 if (options
->flags
.reverse_diff
) {
6875 SWAP(old_mode
, new_mode
);
6876 SWAP(old_oid
, new_oid
);
6877 SWAP(old_oid_valid
, new_oid_valid
);
6878 SWAP(old_dirty_submodule
, new_dirty_submodule
);
6881 if (options
->prefix
&&
6882 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
6885 one
= alloc_filespec(concatpath
);
6886 two
= alloc_filespec(concatpath
);
6887 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
6888 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
6889 one
->dirty_submodule
= old_dirty_submodule
;
6890 two
->dirty_submodule
= new_dirty_submodule
;
6891 p
= diff_queue(&diff_queued_diff
, one
, two
);
6893 if (options
->flags
.diff_from_contents
)
6896 if (options
->flags
.quick
&& options
->skip_stat_unmatch
&&
6897 !diff_filespec_check_stat_unmatch(options
->repo
, p
)) {
6898 diff_free_filespec_data(p
->one
);
6899 diff_free_filespec_data(p
->two
);
6903 options
->flags
.has_changes
= 1;
6906 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
6908 struct diff_filepair
*pair
;
6909 struct diff_filespec
*one
, *two
;
6911 if (options
->prefix
&&
6912 strncmp(path
, options
->prefix
, options
->prefix_length
))
6915 one
= alloc_filespec(path
);
6916 two
= alloc_filespec(path
);
6917 pair
= diff_queue(&diff_queued_diff
, one
, two
);
6918 pair
->is_unmerged
= 1;
6922 static char *run_textconv(struct repository
*r
,
6924 struct diff_filespec
*spec
,
6927 struct diff_tempfile
*temp
;
6928 struct child_process child
= CHILD_PROCESS_INIT
;
6929 struct strbuf buf
= STRBUF_INIT
;
6932 temp
= prepare_temp_file(r
, spec
->path
, spec
);
6933 strvec_push(&child
.args
, pgm
);
6934 strvec_push(&child
.args
, temp
->name
);
6936 child
.use_shell
= 1;
6938 if (start_command(&child
)) {
6943 if (strbuf_read(&buf
, child
.out
, 0) < 0)
6944 err
= error("error reading from textconv command '%s'", pgm
);
6947 if (finish_command(&child
) || err
) {
6948 strbuf_release(&buf
);
6954 return strbuf_detach(&buf
, outsize
);
6957 size_t fill_textconv(struct repository
*r
,
6958 struct userdiff_driver
*driver
,
6959 struct diff_filespec
*df
,
6965 if (!DIFF_FILE_VALID(df
)) {
6969 if (diff_populate_filespec(r
, df
, NULL
))
6970 die("unable to read files to diff");
6975 if (!driver
->textconv
)
6976 BUG("fill_textconv called with non-textconv driver");
6978 if (driver
->textconv_cache
&& df
->oid_valid
) {
6979 *outbuf
= notes_cache_get(driver
->textconv_cache
,
6986 *outbuf
= run_textconv(r
, driver
->textconv
, df
, &size
);
6988 die("unable to read files to diff");
6990 if (driver
->textconv_cache
&& df
->oid_valid
) {
6991 /* ignore errors, as we might be in a readonly repository */
6992 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
6995 * we could save up changes and flush them all at the end,
6996 * but we would need an extra call after all diffing is done.
6997 * Since generating a cache entry is the slow path anyway,
6998 * this extra overhead probably isn't a big deal.
7000 notes_cache_write(driver
->textconv_cache
);
7006 int textconv_object(struct repository
*r
,
7009 const struct object_id
*oid
,
7012 unsigned long *buf_size
)
7014 struct diff_filespec
*df
;
7015 struct userdiff_driver
*textconv
;
7017 df
= alloc_filespec(path
);
7018 fill_filespec(df
, oid
, oid_valid
, mode
);
7019 textconv
= get_textconv(r
, df
);
7025 *buf_size
= fill_textconv(r
, textconv
, df
, buf
);
7030 void setup_diff_pager(struct diff_options
*opt
)
7033 * If the user asked for our exit code, then either they want --quiet
7034 * or --exit-code. We should definitely not bother with a pager in the
7035 * former case, as we will generate no output. Since we still properly
7036 * report our exit code even when a pager is run, we _could_ run a
7037 * pager with --exit-code. But since we have not done so historically,
7038 * and because it is easy to find people oneline advising "git diff
7039 * --exit-code" in hooks and other scripts, we do not do so.
7041 if (!opt
->flags
.exit_with_status
&&
7042 check_pager_config("diff") != 0)