2 * Copyright (C) 2005 Junio C Hamano
9 #include "environment.h"
17 #include "xdiff-interface.h"
20 #include "run-command.h"
22 #include "object-store.h"
24 #include "submodule-config.h"
25 #include "submodule.h"
29 #include "string-list.h"
32 #include "oid-array.h"
35 #include "parse-options.h"
37 #include "promisor-remote.h"
39 #include "object-file.h"
40 #include "object-name.h"
45 #ifdef NO_FAST_WORKING_DIRECTORY
46 #define FAST_WORKING_DIRECTORY 0
48 #define FAST_WORKING_DIRECTORY 1
51 static int diff_detect_rename_default
;
52 static int diff_indent_heuristic
= 1;
53 static int diff_rename_limit_default
= 1000;
54 static int diff_suppress_blank_empty
;
55 static int diff_use_color_default
= -1;
56 static int diff_color_moved_default
;
57 static int diff_color_moved_ws_default
;
58 static int diff_context_default
= 3;
59 static int diff_interhunk_context_default
;
60 static const char *diff_word_regex_cfg
;
61 static const char *external_diff_cmd_cfg
;
62 static const char *diff_order_file_cfg
;
63 int diff_auto_refresh_index
= 1;
64 static int diff_mnemonic_prefix
;
65 static int diff_no_prefix
;
66 static int diff_relative
;
67 static int diff_stat_graph_width
;
68 static int diff_dirstat_permille_default
= 30;
69 static struct diff_options default_diff_options
;
70 static long diff_algorithm
;
71 static unsigned ws_error_highlight_default
= WSEH_NEW
;
73 static char diff_colors
[][COLOR_MAXLEN
] = {
75 GIT_COLOR_NORMAL
, /* CONTEXT */
76 GIT_COLOR_BOLD
, /* METAINFO */
77 GIT_COLOR_CYAN
, /* FRAGINFO */
78 GIT_COLOR_RED
, /* OLD */
79 GIT_COLOR_GREEN
, /* NEW */
80 GIT_COLOR_YELLOW
, /* COMMIT */
81 GIT_COLOR_BG_RED
, /* WHITESPACE */
82 GIT_COLOR_NORMAL
, /* FUNCINFO */
83 GIT_COLOR_BOLD_MAGENTA
, /* OLD_MOVED */
84 GIT_COLOR_BOLD_BLUE
, /* OLD_MOVED ALTERNATIVE */
85 GIT_COLOR_FAINT
, /* OLD_MOVED_DIM */
86 GIT_COLOR_FAINT_ITALIC
, /* OLD_MOVED_ALTERNATIVE_DIM */
87 GIT_COLOR_BOLD_CYAN
, /* NEW_MOVED */
88 GIT_COLOR_BOLD_YELLOW
, /* NEW_MOVED ALTERNATIVE */
89 GIT_COLOR_FAINT
, /* NEW_MOVED_DIM */
90 GIT_COLOR_FAINT_ITALIC
, /* NEW_MOVED_ALTERNATIVE_DIM */
91 GIT_COLOR_FAINT
, /* CONTEXT_DIM */
92 GIT_COLOR_FAINT_RED
, /* OLD_DIM */
93 GIT_COLOR_FAINT_GREEN
, /* NEW_DIM */
94 GIT_COLOR_BOLD
, /* CONTEXT_BOLD */
95 GIT_COLOR_BOLD_RED
, /* OLD_BOLD */
96 GIT_COLOR_BOLD_GREEN
, /* NEW_BOLD */
99 static const char *color_diff_slots
[] = {
100 [DIFF_CONTEXT
] = "context",
101 [DIFF_METAINFO
] = "meta",
102 [DIFF_FRAGINFO
] = "frag",
103 [DIFF_FILE_OLD
] = "old",
104 [DIFF_FILE_NEW
] = "new",
105 [DIFF_COMMIT
] = "commit",
106 [DIFF_WHITESPACE
] = "whitespace",
107 [DIFF_FUNCINFO
] = "func",
108 [DIFF_FILE_OLD_MOVED
] = "oldMoved",
109 [DIFF_FILE_OLD_MOVED_ALT
] = "oldMovedAlternative",
110 [DIFF_FILE_OLD_MOVED_DIM
] = "oldMovedDimmed",
111 [DIFF_FILE_OLD_MOVED_ALT_DIM
] = "oldMovedAlternativeDimmed",
112 [DIFF_FILE_NEW_MOVED
] = "newMoved",
113 [DIFF_FILE_NEW_MOVED_ALT
] = "newMovedAlternative",
114 [DIFF_FILE_NEW_MOVED_DIM
] = "newMovedDimmed",
115 [DIFF_FILE_NEW_MOVED_ALT_DIM
] = "newMovedAlternativeDimmed",
116 [DIFF_CONTEXT_DIM
] = "contextDimmed",
117 [DIFF_FILE_OLD_DIM
] = "oldDimmed",
118 [DIFF_FILE_NEW_DIM
] = "newDimmed",
119 [DIFF_CONTEXT_BOLD
] = "contextBold",
120 [DIFF_FILE_OLD_BOLD
] = "oldBold",
121 [DIFF_FILE_NEW_BOLD
] = "newBold",
124 define_list_config_array_extra(color_diff_slots
, {"plain"});
126 static int parse_diff_color_slot(const char *var
)
128 if (!strcasecmp(var
, "plain"))
130 return LOOKUP_CONFIG(color_diff_slots
, var
);
133 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
134 struct strbuf
*errmsg
)
136 char *params_copy
= xstrdup(params_string
);
137 struct string_list params
= STRING_LIST_INIT_NODUP
;
142 string_list_split_in_place(¶ms
, params_copy
, ',', -1);
143 for (i
= 0; i
< params
.nr
; i
++) {
144 const char *p
= params
.items
[i
].string
;
145 if (!strcmp(p
, "changes")) {
146 options
->flags
.dirstat_by_line
= 0;
147 options
->flags
.dirstat_by_file
= 0;
148 } else if (!strcmp(p
, "lines")) {
149 options
->flags
.dirstat_by_line
= 1;
150 options
->flags
.dirstat_by_file
= 0;
151 } else if (!strcmp(p
, "files")) {
152 options
->flags
.dirstat_by_line
= 0;
153 options
->flags
.dirstat_by_file
= 1;
154 } else if (!strcmp(p
, "noncumulative")) {
155 options
->flags
.dirstat_cumulative
= 0;
156 } else if (!strcmp(p
, "cumulative")) {
157 options
->flags
.dirstat_cumulative
= 1;
158 } else if (isdigit(*p
)) {
160 int permille
= strtoul(p
, &end
, 10) * 10;
161 if (*end
== '.' && isdigit(*++end
)) {
162 /* only use first digit */
163 permille
+= *end
- '0';
164 /* .. and ignore any further digits */
165 while (isdigit(*++end
))
169 options
->dirstat_permille
= permille
;
171 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
176 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
181 string_list_clear(¶ms
, 0);
186 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
188 if (!strcmp(value
, "log"))
189 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
190 else if (!strcmp(value
, "short"))
191 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
192 else if (!strcmp(value
, "diff"))
193 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
195 * Please update $__git_diff_submodule_formats in
196 * git-completion.bash when you add new formats.
203 int git_config_rename(const char *var
, const char *value
)
206 return DIFF_DETECT_RENAME
;
207 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
208 return DIFF_DETECT_COPY
;
209 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
212 long parse_algorithm_value(const char *value
)
216 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
218 else if (!strcasecmp(value
, "minimal"))
219 return XDF_NEED_MINIMAL
;
220 else if (!strcasecmp(value
, "patience"))
221 return XDF_PATIENCE_DIFF
;
222 else if (!strcasecmp(value
, "histogram"))
223 return XDF_HISTOGRAM_DIFF
;
225 * Please update $__git_diff_algorithms in git-completion.bash
226 * when you add new algorithms.
231 static int parse_one_token(const char **arg
, const char *token
)
234 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
241 static int parse_ws_error_highlight(const char *arg
)
243 const char *orig_arg
= arg
;
247 if (parse_one_token(&arg
, "none"))
249 else if (parse_one_token(&arg
, "default"))
251 else if (parse_one_token(&arg
, "all"))
252 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
253 else if (parse_one_token(&arg
, "new"))
255 else if (parse_one_token(&arg
, "old"))
257 else if (parse_one_token(&arg
, "context"))
260 return -1 - (int)(arg
- orig_arg
);
269 * These are to give UI layer defaults.
270 * The core-level commands such as git-diff-files should
271 * never be affected by the setting of diff.renames
272 * the user happens to have in the configuration file.
274 void init_diff_ui_defaults(void)
276 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
279 int git_diff_heuristic_config(const char *var
, const char *value
,
282 if (!strcmp(var
, "diff.indentheuristic"))
283 diff_indent_heuristic
= git_config_bool(var
, value
);
287 static int parse_color_moved(const char *arg
)
289 switch (git_parse_maybe_bool(arg
)) {
291 return COLOR_MOVED_NO
;
293 return COLOR_MOVED_DEFAULT
;
298 if (!strcmp(arg
, "no"))
299 return COLOR_MOVED_NO
;
300 else if (!strcmp(arg
, "plain"))
301 return COLOR_MOVED_PLAIN
;
302 else if (!strcmp(arg
, "blocks"))
303 return COLOR_MOVED_BLOCKS
;
304 else if (!strcmp(arg
, "zebra"))
305 return COLOR_MOVED_ZEBRA
;
306 else if (!strcmp(arg
, "default"))
307 return COLOR_MOVED_DEFAULT
;
308 else if (!strcmp(arg
, "dimmed-zebra"))
309 return COLOR_MOVED_ZEBRA_DIM
;
310 else if (!strcmp(arg
, "dimmed_zebra"))
311 return COLOR_MOVED_ZEBRA_DIM
;
313 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
316 static unsigned parse_color_moved_ws(const char *arg
)
319 struct string_list l
= STRING_LIST_INIT_DUP
;
320 struct string_list_item
*i
;
322 string_list_split(&l
, arg
, ',', -1);
324 for_each_string_list_item(i
, &l
) {
325 struct strbuf sb
= STRBUF_INIT
;
326 strbuf_addstr(&sb
, i
->string
);
329 if (!strcmp(sb
.buf
, "no"))
331 else if (!strcmp(sb
.buf
, "ignore-space-change"))
332 ret
|= XDF_IGNORE_WHITESPACE_CHANGE
;
333 else if (!strcmp(sb
.buf
, "ignore-space-at-eol"))
334 ret
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
335 else if (!strcmp(sb
.buf
, "ignore-all-space"))
336 ret
|= XDF_IGNORE_WHITESPACE
;
337 else if (!strcmp(sb
.buf
, "allow-indentation-change"))
338 ret
|= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
;
340 ret
|= COLOR_MOVED_WS_ERROR
;
341 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
);
347 if ((ret
& COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
) &&
348 (ret
& XDF_WHITESPACE_FLAGS
)) {
349 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
350 ret
|= COLOR_MOVED_WS_ERROR
;
353 string_list_clear(&l
, 0);
358 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
360 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
361 diff_use_color_default
= git_config_colorbool(var
, value
);
364 if (!strcmp(var
, "diff.colormoved")) {
365 int cm
= parse_color_moved(value
);
368 diff_color_moved_default
= cm
;
371 if (!strcmp(var
, "diff.colormovedws")) {
372 unsigned cm
= parse_color_moved_ws(value
);
373 if (cm
& COLOR_MOVED_WS_ERROR
)
375 diff_color_moved_ws_default
= cm
;
378 if (!strcmp(var
, "diff.context")) {
379 diff_context_default
= git_config_int(var
, value
);
380 if (diff_context_default
< 0)
384 if (!strcmp(var
, "diff.interhunkcontext")) {
385 diff_interhunk_context_default
= git_config_int(var
, value
);
386 if (diff_interhunk_context_default
< 0)
390 if (!strcmp(var
, "diff.renames")) {
391 diff_detect_rename_default
= git_config_rename(var
, value
);
394 if (!strcmp(var
, "diff.autorefreshindex")) {
395 diff_auto_refresh_index
= git_config_bool(var
, value
);
398 if (!strcmp(var
, "diff.mnemonicprefix")) {
399 diff_mnemonic_prefix
= git_config_bool(var
, value
);
402 if (!strcmp(var
, "diff.noprefix")) {
403 diff_no_prefix
= git_config_bool(var
, value
);
406 if (!strcmp(var
, "diff.relative")) {
407 diff_relative
= git_config_bool(var
, value
);
410 if (!strcmp(var
, "diff.statgraphwidth")) {
411 diff_stat_graph_width
= git_config_int(var
, value
);
414 if (!strcmp(var
, "diff.external"))
415 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
416 if (!strcmp(var
, "diff.wordregex"))
417 return git_config_string(&diff_word_regex_cfg
, var
, value
);
418 if (!strcmp(var
, "diff.orderfile"))
419 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
421 if (!strcmp(var
, "diff.ignoresubmodules"))
422 handle_ignore_submodules_arg(&default_diff_options
, value
);
424 if (!strcmp(var
, "diff.submodule")) {
425 if (parse_submodule_params(&default_diff_options
, value
))
426 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
431 if (!strcmp(var
, "diff.algorithm")) {
432 diff_algorithm
= parse_algorithm_value(value
);
433 if (diff_algorithm
< 0)
438 if (git_color_config(var
, value
, cb
) < 0)
441 return git_diff_basic_config(var
, value
, cb
);
444 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
448 if (!strcmp(var
, "diff.renamelimit")) {
449 diff_rename_limit_default
= git_config_int(var
, value
);
453 if (userdiff_config(var
, value
) < 0)
456 if (skip_prefix(var
, "diff.color.", &name
) ||
457 skip_prefix(var
, "color.diff.", &name
)) {
458 int slot
= parse_diff_color_slot(name
);
462 return config_error_nonbool(var
);
463 return color_parse(value
, diff_colors
[slot
]);
466 if (!strcmp(var
, "diff.wserrorhighlight")) {
467 int val
= parse_ws_error_highlight(value
);
470 ws_error_highlight_default
= val
;
474 /* like GNU diff's --suppress-blank-empty option */
475 if (!strcmp(var
, "diff.suppressblankempty") ||
476 /* for backwards compatibility */
477 !strcmp(var
, "diff.suppress-blank-empty")) {
478 diff_suppress_blank_empty
= git_config_bool(var
, value
);
482 if (!strcmp(var
, "diff.dirstat")) {
483 struct strbuf errmsg
= STRBUF_INIT
;
484 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
485 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
486 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
488 strbuf_release(&errmsg
);
489 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
493 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
496 return git_default_config(var
, value
, cb
);
499 static char *quote_two(const char *one
, const char *two
)
501 int need_one
= quote_c_style(one
, NULL
, NULL
, CQUOTE_NODQ
);
502 int need_two
= quote_c_style(two
, NULL
, NULL
, CQUOTE_NODQ
);
503 struct strbuf res
= STRBUF_INIT
;
505 if (need_one
+ need_two
) {
506 strbuf_addch(&res
, '"');
507 quote_c_style(one
, &res
, NULL
, CQUOTE_NODQ
);
508 quote_c_style(two
, &res
, NULL
, CQUOTE_NODQ
);
509 strbuf_addch(&res
, '"');
511 strbuf_addstr(&res
, one
);
512 strbuf_addstr(&res
, two
);
514 return strbuf_detach(&res
, NULL
);
517 static const char *external_diff(void)
519 static const char *external_diff_cmd
= NULL
;
520 static int done_preparing
= 0;
523 return external_diff_cmd
;
524 external_diff_cmd
= xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
525 if (!external_diff_cmd
)
526 external_diff_cmd
= external_diff_cmd_cfg
;
528 return external_diff_cmd
;
532 * Keep track of files used for diffing. Sometimes such an entry
533 * refers to a temporary file, sometimes to an existing file, and
534 * sometimes to "/dev/null".
536 static struct diff_tempfile
{
538 * filename external diff should read from, or NULL if this
539 * entry is currently not in use:
543 char hex
[GIT_MAX_HEXSZ
+ 1];
547 * If this diff_tempfile instance refers to a temporary file,
548 * this tempfile object is used to manage its lifetime.
550 struct tempfile
*tempfile
;
553 struct emit_callback
{
556 int blank_at_eof_in_preimage
;
557 int blank_at_eof_in_postimage
;
559 int lno_in_postimage
;
560 const char **label_path
;
561 struct diff_words_data
*diff_words
;
562 struct diff_options
*opt
;
563 struct strbuf
*header
;
566 static int count_lines(const char *data
, int size
)
568 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
575 completely_empty
= 0;
579 completely_empty
= 0;
582 if (completely_empty
)
585 count
++; /* no trailing newline */
589 static int fill_mmfile(struct repository
*r
, mmfile_t
*mf
,
590 struct diff_filespec
*one
)
592 if (!DIFF_FILE_VALID(one
)) {
593 mf
->ptr
= (char *)""; /* does not matter */
597 else if (diff_populate_filespec(r
, one
, NULL
))
601 mf
->size
= one
->size
;
605 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
606 static unsigned long diff_filespec_size(struct repository
*r
,
607 struct diff_filespec
*one
)
609 struct diff_populate_filespec_options dpf_options
= {
610 .check_size_only
= 1,
613 if (!DIFF_FILE_VALID(one
))
615 diff_populate_filespec(r
, one
, &dpf_options
);
619 static int count_trailing_blank(mmfile_t
*mf
)
622 long size
= mf
->size
;
627 ptr
+= size
- 1; /* pointing at the very end */
629 ; /* incomplete line */
631 ptr
--; /* skip the last LF */
632 while (mf
->ptr
< ptr
) {
634 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
635 if (*prev_eol
== '\n')
637 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
))
645 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
646 struct emit_callback
*ecbdata
)
649 l1
= count_trailing_blank(mf1
);
650 l2
= count_trailing_blank(mf2
);
652 ecbdata
->blank_at_eof_in_preimage
= 0;
653 ecbdata
->blank_at_eof_in_postimage
= 0;
656 at
= count_lines(mf1
->ptr
, mf1
->size
);
657 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
659 at
= count_lines(mf2
->ptr
, mf2
->size
);
660 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
663 static void emit_line_0(struct diff_options
*o
,
664 const char *set_sign
, const char *set
, unsigned reverse
, const char *reset
,
665 int first
, const char *line
, int len
)
667 int has_trailing_newline
, has_trailing_carriage_return
;
668 int needs_reset
= 0; /* at the end of the line */
669 FILE *file
= o
->file
;
671 fputs(diff_line_prefix(o
), file
);
673 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
674 if (has_trailing_newline
)
677 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
678 if (has_trailing_carriage_return
)
684 if (reverse
&& want_color(o
->use_color
)) {
685 fputs(GIT_COLOR_REVERSE
, file
);
690 fputs(set_sign
, file
);
701 if (set_sign
&& set
!= set_sign
)
706 fwrite(line
, len
, 1, file
);
707 needs_reset
= 1; /* 'line' may contain color codes. */
712 if (has_trailing_carriage_return
)
714 if (has_trailing_newline
)
718 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
719 const char *line
, int len
)
721 emit_line_0(o
, set
, NULL
, 0, reset
, 0, line
, len
);
725 DIFF_SYMBOL_BINARY_DIFF_HEADER
,
726 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
727 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
728 DIFF_SYMBOL_BINARY_DIFF_BODY
,
729 DIFF_SYMBOL_BINARY_DIFF_FOOTER
,
730 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
731 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
732 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
733 DIFF_SYMBOL_STATS_LINE
,
734 DIFF_SYMBOL_WORD_DIFF
,
735 DIFF_SYMBOL_STAT_SEP
,
737 DIFF_SYMBOL_SUBMODULE_ADD
,
738 DIFF_SYMBOL_SUBMODULE_DEL
,
739 DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
740 DIFF_SYMBOL_SUBMODULE_MODIFIED
,
741 DIFF_SYMBOL_SUBMODULE_HEADER
,
742 DIFF_SYMBOL_SUBMODULE_ERROR
,
743 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
,
744 DIFF_SYMBOL_REWRITE_DIFF
,
745 DIFF_SYMBOL_BINARY_FILES
,
747 DIFF_SYMBOL_FILEPAIR_PLUS
,
748 DIFF_SYMBOL_FILEPAIR_MINUS
,
749 DIFF_SYMBOL_WORDS_PORCELAIN
,
752 DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
755 DIFF_SYMBOL_NO_LF_EOF
,
756 DIFF_SYMBOL_CONTEXT_FRAGINFO
,
757 DIFF_SYMBOL_CONTEXT_MARKER
,
758 DIFF_SYMBOL_SEPARATOR
761 * Flags for content lines:
762 * 0..12 are whitespace rules
763 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
764 * 16 is marking if the line is blank at EOF
766 #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
767 #define DIFF_SYMBOL_MOVED_LINE (1<<17)
768 #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
769 #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
770 #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
773 * This struct is used when we need to buffer the output of the diff output.
775 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
776 * into the pre/post image file. This pointer could be a union with the
777 * line pointer. By storing an offset into the file instead of the literal line,
778 * we can decrease the memory footprint for the buffered output. At first we
779 * may want to only have indirection for the content lines, but we could also
780 * enhance the state for emitting prefabricated lines, e.g. the similarity
781 * score line or hunk/file headers would only need to store a number or path
782 * and then the output can be constructed later on depending on state.
784 struct emitted_diff_symbol
{
788 int indent_off
; /* Offset to first non-whitespace character */
789 int indent_width
; /* The visual width of the indentation */
793 #define EMITTED_DIFF_SYMBOL_INIT { 0 }
795 struct emitted_diff_symbols
{
796 struct emitted_diff_symbol
*buf
;
799 #define EMITTED_DIFF_SYMBOLS_INIT { 0 }
801 static void append_emitted_diff_symbol(struct diff_options
*o
,
802 struct emitted_diff_symbol
*e
)
804 struct emitted_diff_symbol
*f
;
806 ALLOC_GROW(o
->emitted_symbols
->buf
,
807 o
->emitted_symbols
->nr
+ 1,
808 o
->emitted_symbols
->alloc
);
809 f
= &o
->emitted_symbols
->buf
[o
->emitted_symbols
->nr
++];
811 memcpy(f
, e
, sizeof(struct emitted_diff_symbol
));
812 f
->line
= e
->line
? xmemdupz(e
->line
, e
->len
) : NULL
;
815 static void free_emitted_diff_symbols(struct emitted_diff_symbols
*e
)
824 const struct emitted_diff_symbol
*es
;
825 struct moved_entry
*next_line
;
826 struct moved_entry
*next_match
;
830 struct moved_entry
*match
;
831 int wsd
; /* The whitespace delta of this block */
834 #define INDENT_BLANKLINE INT_MIN
836 static void fill_es_indent_data(struct emitted_diff_symbol
*es
)
838 unsigned int off
= 0, i
;
839 int width
= 0, tab_width
= es
->flags
& WS_TAB_WIDTH_MASK
;
840 const char *s
= es
->line
;
841 const int len
= es
->len
;
843 /* skip any \v \f \r at start of indentation */
844 while (s
[off
] == '\f' || s
[off
] == '\v' ||
845 (s
[off
] == '\r' && off
< len
- 1))
848 /* calculate the visual width of indentation */
853 } else if (s
[off
] == '\t') {
854 width
+= tab_width
- (width
% tab_width
);
855 while (s
[++off
] == '\t')
862 /* check if this line is blank */
863 for (i
= off
; i
< len
; i
++)
868 es
->indent_width
= INDENT_BLANKLINE
;
869 es
->indent_off
= len
;
871 es
->indent_off
= off
;
872 es
->indent_width
= width
;
876 static int compute_ws_delta(const struct emitted_diff_symbol
*a
,
877 const struct emitted_diff_symbol
*b
)
879 int a_width
= a
->indent_width
,
880 b_width
= b
->indent_width
;
882 if (a_width
== INDENT_BLANKLINE
&& b_width
== INDENT_BLANKLINE
)
883 return INDENT_BLANKLINE
;
885 return a_width
- b_width
;
888 static int cmp_in_block_with_wsd(const struct moved_entry
*cur
,
889 const struct emitted_diff_symbol
*l
,
890 struct moved_block
*pmb
)
892 int a_width
= cur
->es
->indent_width
, b_width
= l
->indent_width
;
895 /* The text of each line must match */
896 if (cur
->es
->id
!= l
->id
)
900 * If 'l' and 'cur' are both blank then we don't need to check the
901 * indent. We only need to check cur as we know the strings match.
903 if (a_width
== INDENT_BLANKLINE
)
907 * The indent changes of the block are known and stored in pmb->wsd;
908 * however we need to check if the indent changes of the current line
909 * match those of the current block.
911 delta
= b_width
- a_width
;
914 * If the previous lines of this block were all blank then set its
917 if (pmb
->wsd
== INDENT_BLANKLINE
)
920 return delta
!= pmb
->wsd
;
923 struct interned_diff_symbol
{
924 struct hashmap_entry ent
;
925 struct emitted_diff_symbol
*es
;
928 static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data
,
929 const struct hashmap_entry
*eptr
,
930 const struct hashmap_entry
*entry_or_key
,
931 const void *keydata UNUSED
)
933 const struct diff_options
*diffopt
= hashmap_cmp_fn_data
;
934 const struct emitted_diff_symbol
*a
, *b
;
935 unsigned flags
= diffopt
->color_moved_ws_handling
936 & XDF_WHITESPACE_FLAGS
;
938 a
= container_of(eptr
, const struct interned_diff_symbol
, ent
)->es
;
939 b
= container_of(entry_or_key
, const struct interned_diff_symbol
, ent
)->es
;
941 return !xdiff_compare_lines(a
->line
+ a
->indent_off
,
942 a
->len
- a
->indent_off
,
943 b
->line
+ b
->indent_off
,
944 b
->len
- b
->indent_off
, flags
);
947 static void prepare_entry(struct diff_options
*o
, struct emitted_diff_symbol
*l
,
948 struct interned_diff_symbol
*s
)
950 unsigned flags
= o
->color_moved_ws_handling
& XDF_WHITESPACE_FLAGS
;
951 unsigned int hash
= xdiff_hash_string(l
->line
+ l
->indent_off
,
952 l
->len
- l
->indent_off
, flags
);
954 hashmap_entry_init(&s
->ent
, hash
);
958 struct moved_entry_list
{
959 struct moved_entry
*add
, *del
;
962 static struct moved_entry_list
*add_lines_to_move_detection(struct diff_options
*o
,
963 struct mem_pool
*entry_mem_pool
)
965 struct moved_entry
*prev_line
= NULL
;
966 struct mem_pool interned_pool
;
967 struct hashmap interned_map
;
968 struct moved_entry_list
*entry_list
= NULL
;
969 size_t entry_list_alloc
= 0;
973 hashmap_init(&interned_map
, interned_diff_symbol_cmp
, o
, 8096);
974 mem_pool_init(&interned_pool
, 1024 * 1024);
976 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
977 struct interned_diff_symbol key
;
978 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
979 struct interned_diff_symbol
*s
;
980 struct moved_entry
*entry
;
982 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
) {
987 if (o
->color_moved_ws_handling
&
988 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
989 fill_es_indent_data(l
);
991 prepare_entry(o
, l
, &key
);
992 s
= hashmap_get_entry(&interned_map
, &key
, ent
, &key
.ent
);
997 ALLOC_GROW_BY(entry_list
, id
, 1, entry_list_alloc
);
998 hashmap_add(&interned_map
,
999 memcpy(mem_pool_alloc(&interned_pool
,
1001 &key
, sizeof(key
)));
1003 entry
= mem_pool_alloc(entry_mem_pool
, sizeof(*entry
));
1005 entry
->next_line
= NULL
;
1006 if (prev_line
&& prev_line
->es
->s
== l
->s
)
1007 prev_line
->next_line
= entry
;
1009 if (l
->s
== DIFF_SYMBOL_PLUS
) {
1010 entry
->next_match
= entry_list
[l
->id
].add
;
1011 entry_list
[l
->id
].add
= entry
;
1013 entry
->next_match
= entry_list
[l
->id
].del
;
1014 entry_list
[l
->id
].del
= entry
;
1018 hashmap_clear(&interned_map
);
1019 mem_pool_discard(&interned_pool
, 0);
1024 static void pmb_advance_or_null(struct diff_options
*o
,
1025 struct emitted_diff_symbol
*l
,
1026 struct moved_block
*pmb
,
1031 for (i
= 0, j
= 0; i
< *pmb_nr
; i
++) {
1033 struct moved_entry
*prev
= pmb
[i
].match
;
1034 struct moved_entry
*cur
= (prev
&& prev
->next_line
) ?
1035 prev
->next_line
: NULL
;
1037 if (o
->color_moved_ws_handling
&
1038 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1040 !cmp_in_block_with_wsd(cur
, l
, &pmb
[i
]);
1042 match
= cur
&& cur
->es
->id
== l
->id
;
1046 pmb
[j
++].match
= cur
;
1052 static void fill_potential_moved_blocks(struct diff_options
*o
,
1053 struct moved_entry
*match
,
1054 struct emitted_diff_symbol
*l
,
1055 struct moved_block
**pmb_p
,
1056 int *pmb_alloc_p
, int *pmb_nr_p
)
1059 struct moved_block
*pmb
= *pmb_p
;
1060 int pmb_alloc
= *pmb_alloc_p
, pmb_nr
= *pmb_nr_p
;
1063 * The current line is the start of a new block.
1064 * Setup the set of potential blocks.
1066 for (; match
; match
= match
->next_match
) {
1067 ALLOC_GROW(pmb
, pmb_nr
+ 1, pmb_alloc
);
1068 if (o
->color_moved_ws_handling
&
1069 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1070 pmb
[pmb_nr
].wsd
= compute_ws_delta(l
, match
->es
);
1072 pmb
[pmb_nr
].wsd
= 0;
1073 pmb
[pmb_nr
++].match
= match
;
1077 *pmb_alloc_p
= pmb_alloc
;
1082 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1084 * Otherwise, if the last block has fewer alphanumeric characters than
1085 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
1088 * The last block consists of the (n - block_length)'th line up to but not
1089 * including the nth line.
1091 * Returns 0 if the last block is empty or is unset by this function, non zero
1094 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1095 * Think of a way to unify them.
1097 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1098 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
1099 static int adjust_last_block(struct diff_options
*o
, int n
, int block_length
)
1101 int i
, alnum_count
= 0;
1102 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
1103 return block_length
;
1104 for (i
= 1; i
< block_length
+ 1; i
++) {
1105 const char *c
= o
->emitted_symbols
->buf
[n
- i
].line
;
1110 if (alnum_count
>= COLOR_MOVED_MIN_ALNUM_COUNT
)
1114 for (i
= 1; i
< block_length
+ 1; i
++)
1115 o
->emitted_symbols
->buf
[n
- i
].flags
&= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
;
1119 /* Find blocks of moved code, delegate actual coloring decision to helper */
1120 static void mark_color_as_moved(struct diff_options
*o
,
1121 struct moved_entry_list
*entry_list
)
1123 struct moved_block
*pmb
= NULL
; /* potentially moved blocks */
1124 int pmb_nr
= 0, pmb_alloc
= 0;
1125 int n
, flipped_block
= 0, block_length
= 0;
1126 enum diff_symbol moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1129 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1130 struct moved_entry
*match
= NULL
;
1131 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1134 case DIFF_SYMBOL_PLUS
:
1135 match
= entry_list
[l
->id
].del
;
1137 case DIFF_SYMBOL_MINUS
:
1138 match
= entry_list
[l
->id
].add
;
1144 if (pmb_nr
&& (!match
|| l
->s
!= moved_symbol
)) {
1145 if (!adjust_last_block(o
, n
, block_length
) &&
1148 * Rewind in case there is another match
1149 * starting at the second line of the block
1159 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1163 if (o
->color_moved
== COLOR_MOVED_PLAIN
) {
1164 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1168 pmb_advance_or_null(o
, l
, pmb
, &pmb_nr
);
1171 int contiguous
= adjust_last_block(o
, n
, block_length
);
1173 if (!contiguous
&& block_length
> 1)
1175 * Rewind in case there is another match
1176 * starting at the second line of the block
1180 fill_potential_moved_blocks(o
, match
, l
,
1184 if (contiguous
&& pmb_nr
&& moved_symbol
== l
->s
)
1185 flipped_block
= (flipped_block
+ 1) % 2;
1190 moved_symbol
= l
->s
;
1192 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1199 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1200 if (flipped_block
&& o
->color_moved
!= COLOR_MOVED_BLOCKS
)
1201 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_ALT
;
1204 adjust_last_block(o
, n
, block_length
);
1209 static void dim_moved_lines(struct diff_options
*o
)
1212 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1213 struct emitted_diff_symbol
*prev
= (n
!= 0) ?
1214 &o
->emitted_symbols
->buf
[n
- 1] : NULL
;
1215 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1216 struct emitted_diff_symbol
*next
=
1217 (n
< o
->emitted_symbols
->nr
- 1) ?
1218 &o
->emitted_symbols
->buf
[n
+ 1] : NULL
;
1220 /* Not a plus or minus line? */
1221 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
)
1224 /* Not a moved line? */
1225 if (!(l
->flags
& DIFF_SYMBOL_MOVED_LINE
))
1229 * If prev or next are not a plus or minus line,
1230 * pretend they don't exist
1232 if (prev
&& prev
->s
!= DIFF_SYMBOL_PLUS
&&
1233 prev
->s
!= DIFF_SYMBOL_MINUS
)
1235 if (next
&& next
->s
!= DIFF_SYMBOL_PLUS
&&
1236 next
->s
!= DIFF_SYMBOL_MINUS
)
1239 /* Inside a block? */
1241 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1242 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
)) &&
1244 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1245 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
))) {
1246 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1250 /* Check if we are at an interesting bound: */
1251 if (prev
&& (prev
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1252 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1253 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1255 if (next
&& (next
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1256 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1257 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1261 * The boundary to prev and next are not interesting,
1262 * so this line is not interesting as a whole
1264 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1268 static void emit_line_ws_markup(struct diff_options
*o
,
1269 const char *set_sign
, const char *set
,
1271 int sign_index
, const char *line
, int len
,
1272 unsigned ws_rule
, int blank_at_eof
)
1274 const char *ws
= NULL
;
1275 int sign
= o
->output_indicators
[sign_index
];
1277 if (o
->ws_error_highlight
& ws_rule
) {
1278 ws
= diff_get_color_opt(o
, DIFF_WHITESPACE
);
1283 if (!ws
&& !set_sign
)
1284 emit_line_0(o
, set
, NULL
, 0, reset
, sign
, line
, len
);
1286 emit_line_0(o
, set_sign
, set
, !!set_sign
, reset
, sign
, line
, len
);
1287 } else if (blank_at_eof
)
1288 /* Blank line at EOF - paint '+' as well */
1289 emit_line_0(o
, ws
, NULL
, 0, reset
, sign
, line
, len
);
1291 /* Emit just the prefix, then the rest. */
1292 emit_line_0(o
, set_sign
? set_sign
: set
, NULL
, !!set_sign
, reset
,
1294 ws_check_emit(line
, len
, ws_rule
,
1295 o
->file
, set
, reset
, ws
);
1299 static void emit_diff_symbol_from_struct(struct diff_options
*o
,
1300 struct emitted_diff_symbol
*eds
)
1302 static const char *nneof
= " No newline at end of file\n";
1303 const char *context
, *reset
, *set
, *set_sign
, *meta
, *fraginfo
;
1305 enum diff_symbol s
= eds
->s
;
1306 const char *line
= eds
->line
;
1308 unsigned flags
= eds
->flags
;
1311 case DIFF_SYMBOL_NO_LF_EOF
:
1312 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1313 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1314 putc('\n', o
->file
);
1315 emit_line_0(o
, context
, NULL
, 0, reset
, '\\',
1316 nneof
, strlen(nneof
));
1318 case DIFF_SYMBOL_SUBMODULE_HEADER
:
1319 case DIFF_SYMBOL_SUBMODULE_ERROR
:
1320 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
:
1321 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
:
1322 case DIFF_SYMBOL_SUMMARY
:
1323 case DIFF_SYMBOL_STATS_LINE
:
1324 case DIFF_SYMBOL_BINARY_DIFF_BODY
:
1325 case DIFF_SYMBOL_CONTEXT_FRAGINFO
:
1326 emit_line(o
, "", "", line
, len
);
1328 case DIFF_SYMBOL_CONTEXT_INCOMPLETE
:
1329 case DIFF_SYMBOL_CONTEXT_MARKER
:
1330 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1331 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1332 emit_line(o
, context
, reset
, line
, len
);
1334 case DIFF_SYMBOL_SEPARATOR
:
1335 fprintf(o
->file
, "%s%c",
1336 diff_line_prefix(o
),
1337 o
->line_termination
);
1339 case DIFF_SYMBOL_CONTEXT
:
1340 set
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1341 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1343 if (o
->flags
.dual_color_diffed_diffs
) {
1344 char c
= !len
? 0 : line
[0];
1347 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1349 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1351 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1353 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1354 OUTPUT_INDICATOR_CONTEXT
, line
, len
,
1355 flags
& (DIFF_SYMBOL_CONTENT_WS_MASK
), 0);
1357 case DIFF_SYMBOL_PLUS
:
1358 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1359 DIFF_SYMBOL_MOVED_LINE_ALT
|
1360 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1361 case DIFF_SYMBOL_MOVED_LINE
|
1362 DIFF_SYMBOL_MOVED_LINE_ALT
|
1363 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1364 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT_DIM
);
1366 case DIFF_SYMBOL_MOVED_LINE
|
1367 DIFF_SYMBOL_MOVED_LINE_ALT
:
1368 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT
);
1370 case DIFF_SYMBOL_MOVED_LINE
|
1371 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1372 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_DIM
);
1374 case DIFF_SYMBOL_MOVED_LINE
:
1375 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED
);
1378 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1380 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1381 if (!o
->flags
.dual_color_diffed_diffs
)
1384 char c
= !len
? 0 : line
[0];
1388 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_BOLD
);
1390 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1392 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_BOLD
);
1394 set
= diff_get_color_opt(o
, DIFF_CONTEXT_BOLD
);
1395 flags
&= ~DIFF_SYMBOL_CONTENT_WS_MASK
;
1397 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1398 OUTPUT_INDICATOR_NEW
, line
, len
,
1399 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
,
1400 flags
& DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
);
1402 case DIFF_SYMBOL_MINUS
:
1403 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1404 DIFF_SYMBOL_MOVED_LINE_ALT
|
1405 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1406 case DIFF_SYMBOL_MOVED_LINE
|
1407 DIFF_SYMBOL_MOVED_LINE_ALT
|
1408 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1409 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT_DIM
);
1411 case DIFF_SYMBOL_MOVED_LINE
|
1412 DIFF_SYMBOL_MOVED_LINE_ALT
:
1413 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT
);
1415 case DIFF_SYMBOL_MOVED_LINE
|
1416 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1417 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_DIM
);
1419 case DIFF_SYMBOL_MOVED_LINE
:
1420 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED
);
1423 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1425 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1426 if (!o
->flags
.dual_color_diffed_diffs
)
1429 char c
= !len
? 0 : line
[0];
1433 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_DIM
);
1435 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1437 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_DIM
);
1439 set
= diff_get_color_opt(o
, DIFF_CONTEXT_DIM
);
1441 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1442 OUTPUT_INDICATOR_OLD
, line
, len
,
1443 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
, 0);
1445 case DIFF_SYMBOL_WORDS_PORCELAIN
:
1446 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1447 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1448 emit_line(o
, context
, reset
, line
, len
);
1449 fputs("~\n", o
->file
);
1451 case DIFF_SYMBOL_WORDS
:
1452 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1453 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1455 * Skip the prefix character, if any. With
1456 * diff_suppress_blank_empty, there may be
1459 if (line
[0] != '\n') {
1463 emit_line(o
, context
, reset
, line
, len
);
1465 case DIFF_SYMBOL_FILEPAIR_PLUS
:
1466 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1467 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1468 fprintf(o
->file
, "%s%s+++ %s%s%s\n", diff_line_prefix(o
), meta
,
1470 strchr(line
, ' ') ? "\t" : "");
1472 case DIFF_SYMBOL_FILEPAIR_MINUS
:
1473 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1474 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1475 fprintf(o
->file
, "%s%s--- %s%s%s\n", diff_line_prefix(o
), meta
,
1477 strchr(line
, ' ') ? "\t" : "");
1479 case DIFF_SYMBOL_BINARY_FILES
:
1480 case DIFF_SYMBOL_HEADER
:
1481 fprintf(o
->file
, "%s", line
);
1483 case DIFF_SYMBOL_BINARY_DIFF_HEADER
:
1484 fprintf(o
->file
, "%sGIT binary patch\n", diff_line_prefix(o
));
1486 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
:
1487 fprintf(o
->file
, "%sdelta %s\n", diff_line_prefix(o
), line
);
1489 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
:
1490 fprintf(o
->file
, "%sliteral %s\n", diff_line_prefix(o
), line
);
1492 case DIFF_SYMBOL_BINARY_DIFF_FOOTER
:
1493 fputs(diff_line_prefix(o
), o
->file
);
1494 fputc('\n', o
->file
);
1496 case DIFF_SYMBOL_REWRITE_DIFF
:
1497 fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
1498 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1499 emit_line(o
, fraginfo
, reset
, line
, len
);
1501 case DIFF_SYMBOL_SUBMODULE_ADD
:
1502 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1503 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1504 emit_line(o
, set
, reset
, line
, len
);
1506 case DIFF_SYMBOL_SUBMODULE_DEL
:
1507 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1508 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1509 emit_line(o
, set
, reset
, line
, len
);
1511 case DIFF_SYMBOL_SUBMODULE_UNTRACKED
:
1512 fprintf(o
->file
, "%sSubmodule %s contains untracked content\n",
1513 diff_line_prefix(o
), line
);
1515 case DIFF_SYMBOL_SUBMODULE_MODIFIED
:
1516 fprintf(o
->file
, "%sSubmodule %s contains modified content\n",
1517 diff_line_prefix(o
), line
);
1519 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
:
1520 emit_line(o
, "", "", " 0 files changed\n",
1521 strlen(" 0 files changed\n"));
1523 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV
:
1524 emit_line(o
, "", "", " ...\n", strlen(" ...\n"));
1526 case DIFF_SYMBOL_WORD_DIFF
:
1527 fprintf(o
->file
, "%.*s", len
, line
);
1529 case DIFF_SYMBOL_STAT_SEP
:
1530 fputs(o
->stat_sep
, o
->file
);
1533 BUG("unknown diff symbol");
1537 static void emit_diff_symbol(struct diff_options
*o
, enum diff_symbol s
,
1538 const char *line
, int len
, unsigned flags
)
1540 struct emitted_diff_symbol e
= {
1541 .line
= line
, .len
= len
, .flags
= flags
, .s
= s
1544 if (o
->emitted_symbols
)
1545 append_emitted_diff_symbol(o
, &e
);
1547 emit_diff_symbol_from_struct(o
, &e
);
1550 void diff_emit_submodule_del(struct diff_options
*o
, const char *line
)
1552 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_DEL
, line
, strlen(line
), 0);
1555 void diff_emit_submodule_add(struct diff_options
*o
, const char *line
)
1557 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ADD
, line
, strlen(line
), 0);
1560 void diff_emit_submodule_untracked(struct diff_options
*o
, const char *path
)
1562 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
1563 path
, strlen(path
), 0);
1566 void diff_emit_submodule_modified(struct diff_options
*o
, const char *path
)
1568 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_MODIFIED
,
1569 path
, strlen(path
), 0);
1572 void diff_emit_submodule_header(struct diff_options
*o
, const char *header
)
1574 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_HEADER
,
1575 header
, strlen(header
), 0);
1578 void diff_emit_submodule_error(struct diff_options
*o
, const char *err
)
1580 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ERROR
, err
, strlen(err
), 0);
1583 void diff_emit_submodule_pipethrough(struct diff_options
*o
,
1584 const char *line
, int len
)
1586 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
, line
, len
, 0);
1589 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
1591 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
1592 ecbdata
->blank_at_eof_in_preimage
&&
1593 ecbdata
->blank_at_eof_in_postimage
&&
1594 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
1595 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
1597 return ws_blank_line(line
, len
);
1600 static void emit_add_line(struct emit_callback
*ecbdata
,
1601 const char *line
, int len
)
1603 unsigned flags
= WSEH_NEW
| ecbdata
->ws_rule
;
1604 if (new_blank_line_at_eof(ecbdata
, line
, len
))
1605 flags
|= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
;
1607 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_PLUS
, line
, len
, flags
);
1610 static void emit_del_line(struct emit_callback
*ecbdata
,
1611 const char *line
, int len
)
1613 unsigned flags
= WSEH_OLD
| ecbdata
->ws_rule
;
1614 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_MINUS
, line
, len
, flags
);
1617 static void emit_context_line(struct emit_callback
*ecbdata
,
1618 const char *line
, int len
)
1620 unsigned flags
= WSEH_CONTEXT
| ecbdata
->ws_rule
;
1621 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_CONTEXT
, line
, len
, flags
);
1624 static void emit_hunk_header(struct emit_callback
*ecbdata
,
1625 const char *line
, int len
)
1627 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1628 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
1629 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
1630 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1631 const char *reverse
= ecbdata
->color_diff
? GIT_COLOR_REVERSE
: "";
1632 static const char atat
[2] = { '@', '@' };
1633 const char *cp
, *ep
;
1634 struct strbuf msgbuf
= STRBUF_INIT
;
1639 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1640 * it always is at least 10 bytes long.
1643 memcmp(line
, atat
, 2) ||
1644 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
1645 emit_diff_symbol(ecbdata
->opt
,
1646 DIFF_SYMBOL_CONTEXT_MARKER
, line
, len
, 0);
1649 ep
+= 2; /* skip over @@ */
1651 /* The hunk header in fraginfo color */
1652 if (ecbdata
->opt
->flags
.dual_color_diffed_diffs
)
1653 strbuf_addstr(&msgbuf
, reverse
);
1654 strbuf_addstr(&msgbuf
, frag
);
1655 if (ecbdata
->opt
->flags
.suppress_hunk_header_line_count
)
1656 strbuf_add(&msgbuf
, atat
, sizeof(atat
));
1658 strbuf_add(&msgbuf
, line
, ep
- line
);
1659 strbuf_addstr(&msgbuf
, reset
);
1665 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
1668 /* blank before the func header */
1669 for (cp
= ep
; ep
- line
< len
; ep
++)
1670 if (*ep
!= ' ' && *ep
!= '\t')
1673 strbuf_addstr(&msgbuf
, context
);
1674 strbuf_add(&msgbuf
, cp
, ep
- cp
);
1675 strbuf_addstr(&msgbuf
, reset
);
1678 if (ep
< line
+ len
) {
1679 strbuf_addstr(&msgbuf
, func
);
1680 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
1681 strbuf_addstr(&msgbuf
, reset
);
1684 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
1685 strbuf_complete_line(&msgbuf
);
1686 emit_diff_symbol(ecbdata
->opt
,
1687 DIFF_SYMBOL_CONTEXT_FRAGINFO
, msgbuf
.buf
, msgbuf
.len
, 0);
1688 strbuf_release(&msgbuf
);
1691 static struct diff_tempfile
*claim_diff_tempfile(void)
1694 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
1695 if (!diff_temp
[i
].name
)
1696 return diff_temp
+ i
;
1697 BUG("diff is failing to clean up its tempfiles");
1700 static void remove_tempfile(void)
1703 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
1704 if (is_tempfile_active(diff_temp
[i
].tempfile
))
1705 delete_tempfile(&diff_temp
[i
].tempfile
);
1706 diff_temp
[i
].name
= NULL
;
1710 static void add_line_count(struct strbuf
*out
, int count
)
1714 strbuf_addstr(out
, "0,0");
1717 strbuf_addstr(out
, "1");
1720 strbuf_addf(out
, "1,%d", count
);
1725 static void emit_rewrite_lines(struct emit_callback
*ecb
,
1726 int prefix
, const char *data
, int size
)
1728 const char *endp
= NULL
;
1733 endp
= memchr(data
, '\n', size
);
1734 len
= endp
? (endp
- data
+ 1) : size
;
1735 if (prefix
!= '+') {
1736 ecb
->lno_in_preimage
++;
1737 emit_del_line(ecb
, data
, len
);
1739 ecb
->lno_in_postimage
++;
1740 emit_add_line(ecb
, data
, len
);
1746 emit_diff_symbol(ecb
->opt
, DIFF_SYMBOL_NO_LF_EOF
, NULL
, 0, 0);
1749 static void emit_rewrite_diff(const char *name_a
,
1751 struct diff_filespec
*one
,
1752 struct diff_filespec
*two
,
1753 struct userdiff_driver
*textconv_one
,
1754 struct userdiff_driver
*textconv_two
,
1755 struct diff_options
*o
)
1758 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
1759 const char *a_prefix
, *b_prefix
;
1760 char *data_one
, *data_two
;
1761 size_t size_one
, size_two
;
1762 struct emit_callback ecbdata
;
1763 struct strbuf out
= STRBUF_INIT
;
1765 if (diff_mnemonic_prefix
&& o
->flags
.reverse_diff
) {
1766 a_prefix
= o
->b_prefix
;
1767 b_prefix
= o
->a_prefix
;
1769 a_prefix
= o
->a_prefix
;
1770 b_prefix
= o
->b_prefix
;
1773 name_a
+= (*name_a
== '/');
1774 name_b
+= (*name_b
== '/');
1776 strbuf_reset(&a_name
);
1777 strbuf_reset(&b_name
);
1778 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
1779 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
1781 size_one
= fill_textconv(o
->repo
, textconv_one
, one
, &data_one
);
1782 size_two
= fill_textconv(o
->repo
, textconv_two
, two
, &data_two
);
1784 memset(&ecbdata
, 0, sizeof(ecbdata
));
1785 ecbdata
.color_diff
= want_color(o
->use_color
);
1786 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
1788 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
1790 mf1
.ptr
= (char *)data_one
;
1791 mf2
.ptr
= (char *)data_two
;
1792 mf1
.size
= size_one
;
1793 mf2
.size
= size_two
;
1794 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1796 ecbdata
.lno_in_preimage
= 1;
1797 ecbdata
.lno_in_postimage
= 1;
1799 lc_a
= count_lines(data_one
, size_one
);
1800 lc_b
= count_lines(data_two
, size_two
);
1802 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1803 a_name
.buf
, a_name
.len
, 0);
1804 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1805 b_name
.buf
, b_name
.len
, 0);
1807 strbuf_addstr(&out
, "@@ -");
1808 if (!o
->irreversible_delete
)
1809 add_line_count(&out
, lc_a
);
1811 strbuf_addstr(&out
, "?,?");
1812 strbuf_addstr(&out
, " +");
1813 add_line_count(&out
, lc_b
);
1814 strbuf_addstr(&out
, " @@\n");
1815 emit_diff_symbol(o
, DIFF_SYMBOL_REWRITE_DIFF
, out
.buf
, out
.len
, 0);
1816 strbuf_release(&out
);
1818 if (lc_a
&& !o
->irreversible_delete
)
1819 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
1821 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
1823 free((char *)data_one
);
1825 free((char *)data_two
);
1828 struct diff_words_buffer
{
1830 unsigned long alloc
;
1831 struct diff_words_orig
{
1832 const char *begin
, *end
;
1834 int orig_nr
, orig_alloc
;
1837 static void diff_words_append(char *line
, unsigned long len
,
1838 struct diff_words_buffer
*buffer
)
1840 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
1843 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
1844 buffer
->text
.size
+= len
;
1845 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
1848 struct diff_words_style_elem
{
1851 const char *color
; /* NULL; filled in by the setup code if
1852 * color is enabled */
1855 struct diff_words_style
{
1856 enum diff_words_type type
;
1857 struct diff_words_style_elem new_word
, old_word
, ctx
;
1858 const char *newline
;
1861 static struct diff_words_style diff_words_styles
[] = {
1862 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1863 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1864 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
1867 struct diff_words_data
{
1868 struct diff_words_buffer minus
, plus
;
1869 const char *current_plus
;
1871 struct diff_options
*opt
;
1872 regex_t
*word_regex
;
1873 enum diff_words_type type
;
1874 struct diff_words_style
*style
;
1877 static int fn_out_diff_words_write_helper(struct diff_options
*o
,
1878 struct diff_words_style_elem
*st_el
,
1879 const char *newline
,
1880 size_t count
, const char *buf
)
1883 struct strbuf sb
= STRBUF_INIT
;
1886 char *p
= memchr(buf
, '\n', count
);
1888 strbuf_addstr(&sb
, diff_line_prefix(o
));
1891 const char *reset
= st_el
->color
&& *st_el
->color
?
1892 GIT_COLOR_RESET
: NULL
;
1893 if (st_el
->color
&& *st_el
->color
)
1894 strbuf_addstr(&sb
, st_el
->color
);
1895 strbuf_addstr(&sb
, st_el
->prefix
);
1896 strbuf_add(&sb
, buf
, p
? p
- buf
: count
);
1897 strbuf_addstr(&sb
, st_el
->suffix
);
1899 strbuf_addstr(&sb
, reset
);
1904 strbuf_addstr(&sb
, newline
);
1905 count
-= p
+ 1 - buf
;
1909 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1917 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1919 strbuf_release(&sb
);
1924 * '--color-words' algorithm can be described as:
1926 * 1. collect the minus/plus lines of a diff hunk, divided into
1927 * minus-lines and plus-lines;
1929 * 2. break both minus-lines and plus-lines into words and
1930 * place them into two mmfile_t with one word for each line;
1932 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1934 * And for the common parts of the both file, we output the plus side text.
1935 * diff_words->current_plus is used to trace the current position of the plus file
1936 * which printed. diff_words->last_minus is used to trace the last minus word
1939 * For '--graph' to work with '--color-words', we need to output the graph prefix
1940 * on each line of color words output. Generally, there are two conditions on
1941 * which we should output the prefix.
1943 * 1. diff_words->last_minus == 0 &&
1944 * diff_words->current_plus == diff_words->plus.text.ptr
1946 * that is: the plus text must start as a new line, and if there is no minus
1947 * word printed, a graph prefix must be printed.
1949 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1950 * *(diff_words->current_plus - 1) == '\n'
1952 * that is: a graph prefix must be printed following a '\n'
1954 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
1956 if ((diff_words
->last_minus
== 0 &&
1957 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
1958 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
1959 *(diff_words
->current_plus
- 1) == '\n')) {
1966 static void fn_out_diff_words_aux(void *priv
,
1967 long minus_first
, long minus_len
,
1968 long plus_first
, long plus_len
,
1969 const char *func UNUSED
, long funclen UNUSED
)
1971 struct diff_words_data
*diff_words
= priv
;
1972 struct diff_words_style
*style
= diff_words
->style
;
1973 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
1974 struct diff_options
*opt
= diff_words
->opt
;
1975 const char *line_prefix
;
1978 line_prefix
= diff_line_prefix(opt
);
1980 /* POSIX requires that first be decremented by one if len == 0... */
1982 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
1984 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
1986 minus_begin
= minus_end
=
1987 diff_words
->minus
.orig
[minus_first
].end
;
1990 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
1991 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
1993 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
1995 if (color_words_output_graph_prefix(diff_words
)) {
1996 fputs(line_prefix
, diff_words
->opt
->file
);
1998 if (diff_words
->current_plus
!= plus_begin
) {
1999 fn_out_diff_words_write_helper(diff_words
->opt
,
2000 &style
->ctx
, style
->newline
,
2001 plus_begin
- diff_words
->current_plus
,
2002 diff_words
->current_plus
);
2004 if (minus_begin
!= minus_end
) {
2005 fn_out_diff_words_write_helper(diff_words
->opt
,
2006 &style
->old_word
, style
->newline
,
2007 minus_end
- minus_begin
, minus_begin
);
2009 if (plus_begin
!= plus_end
) {
2010 fn_out_diff_words_write_helper(diff_words
->opt
,
2011 &style
->new_word
, style
->newline
,
2012 plus_end
- plus_begin
, plus_begin
);
2015 diff_words
->current_plus
= plus_end
;
2016 diff_words
->last_minus
= minus_first
;
2019 /* This function starts looking at *begin, and returns 0 iff a word was found. */
2020 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
2021 int *begin
, int *end
)
2023 while (word_regex
&& *begin
< buffer
->size
) {
2024 regmatch_t match
[1];
2025 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
2026 buffer
->size
- *begin
, 1, match
, 0)) {
2027 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
2028 '\n', match
[0].rm_eo
- match
[0].rm_so
);
2029 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
2030 *begin
+= match
[0].rm_so
;
2034 return *begin
> *end
;
2040 /* find the next word */
2041 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
2043 if (*begin
>= buffer
->size
)
2046 /* find the end of the word */
2048 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
2055 * This function splits the words in buffer->text, stores the list with
2056 * newline separator into out, and saves the offsets of the original words
2059 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
2060 regex_t
*word_regex
)
2068 /* fake an empty "0th" word */
2069 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
2070 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
2071 buffer
->orig_nr
= 1;
2073 for (i
= 0; i
< buffer
->text
.size
; i
++) {
2074 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
2077 /* store original boundaries */
2078 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
2079 buffer
->orig_alloc
);
2080 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
2081 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
2084 /* store one word */
2085 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
2086 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
2087 out
->ptr
[out
->size
+ j
- i
] = '\n';
2088 out
->size
+= j
- i
+ 1;
2094 /* this executes the word diff on the accumulated buffers */
2095 static void diff_words_show(struct diff_words_data
*diff_words
)
2099 mmfile_t minus
, plus
;
2100 struct diff_words_style
*style
= diff_words
->style
;
2102 struct diff_options
*opt
= diff_words
->opt
;
2103 const char *line_prefix
;
2106 line_prefix
= diff_line_prefix(opt
);
2108 /* special case: only removal */
2109 if (!diff_words
->plus
.text
.size
) {
2110 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2111 line_prefix
, strlen(line_prefix
), 0);
2112 fn_out_diff_words_write_helper(diff_words
->opt
,
2113 &style
->old_word
, style
->newline
,
2114 diff_words
->minus
.text
.size
,
2115 diff_words
->minus
.text
.ptr
);
2116 diff_words
->minus
.text
.size
= 0;
2120 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
2121 diff_words
->last_minus
= 0;
2123 memset(&xpp
, 0, sizeof(xpp
));
2124 memset(&xecfg
, 0, sizeof(xecfg
));
2125 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
2126 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
2128 /* as only the hunk header will be parsed, we need a 0-context */
2130 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, NULL
,
2131 diff_words
, &xpp
, &xecfg
))
2132 die("unable to generate word diff");
2135 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
2136 diff_words
->plus
.text
.size
) {
2137 if (color_words_output_graph_prefix(diff_words
))
2138 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2139 line_prefix
, strlen(line_prefix
), 0);
2140 fn_out_diff_words_write_helper(diff_words
->opt
,
2141 &style
->ctx
, style
->newline
,
2142 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
2143 - diff_words
->current_plus
, diff_words
->current_plus
);
2145 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
2148 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
2149 static void diff_words_flush(struct emit_callback
*ecbdata
)
2151 struct diff_options
*wo
= ecbdata
->diff_words
->opt
;
2153 if (ecbdata
->diff_words
->minus
.text
.size
||
2154 ecbdata
->diff_words
->plus
.text
.size
)
2155 diff_words_show(ecbdata
->diff_words
);
2157 if (wo
->emitted_symbols
) {
2158 struct diff_options
*o
= ecbdata
->opt
;
2159 struct emitted_diff_symbols
*wol
= wo
->emitted_symbols
;
2164 * Instead of appending each, concat all words to a line?
2166 for (i
= 0; i
< wol
->nr
; i
++)
2167 append_emitted_diff_symbol(o
, &wol
->buf
[i
]);
2169 for (i
= 0; i
< wol
->nr
; i
++)
2170 free((void *)wol
->buf
[i
].line
);
2176 static void diff_filespec_load_driver(struct diff_filespec
*one
,
2177 struct index_state
*istate
)
2179 /* Use already-loaded driver */
2183 if (S_ISREG(one
->mode
))
2184 one
->driver
= userdiff_find_by_path(istate
, one
->path
);
2186 /* Fallback to default settings */
2188 one
->driver
= userdiff_find_by_name("default");
2191 static const char *userdiff_word_regex(struct diff_filespec
*one
,
2192 struct index_state
*istate
)
2194 diff_filespec_load_driver(one
, istate
);
2195 return one
->driver
->word_regex
;
2198 static void init_diff_words_data(struct emit_callback
*ecbdata
,
2199 struct diff_options
*orig_opts
,
2200 struct diff_filespec
*one
,
2201 struct diff_filespec
*two
)
2204 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
2205 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
2207 CALLOC_ARRAY(ecbdata
->diff_words
, 1);
2208 ecbdata
->diff_words
->type
= o
->word_diff
;
2209 ecbdata
->diff_words
->opt
= o
;
2211 if (orig_opts
->emitted_symbols
)
2212 CALLOC_ARRAY(o
->emitted_symbols
, 1);
2215 o
->word_regex
= userdiff_word_regex(one
, o
->repo
->index
);
2217 o
->word_regex
= userdiff_word_regex(two
, o
->repo
->index
);
2219 o
->word_regex
= diff_word_regex_cfg
;
2220 if (o
->word_regex
) {
2221 ecbdata
->diff_words
->word_regex
= (regex_t
*)
2222 xmalloc(sizeof(regex_t
));
2223 if (regcomp(ecbdata
->diff_words
->word_regex
,
2225 REG_EXTENDED
| REG_NEWLINE
))
2226 die("invalid regular expression: %s",
2229 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
2230 if (o
->word_diff
== diff_words_styles
[i
].type
) {
2231 ecbdata
->diff_words
->style
=
2232 &diff_words_styles
[i
];
2236 if (want_color(o
->use_color
)) {
2237 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
2238 st
->old_word
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2239 st
->new_word
.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2240 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
2244 static void free_diff_words_data(struct emit_callback
*ecbdata
)
2246 if (ecbdata
->diff_words
) {
2247 diff_words_flush(ecbdata
);
2248 free_emitted_diff_symbols(ecbdata
->diff_words
->opt
->emitted_symbols
);
2249 free (ecbdata
->diff_words
->opt
);
2250 free (ecbdata
->diff_words
->minus
.text
.ptr
);
2251 free (ecbdata
->diff_words
->minus
.orig
);
2252 free (ecbdata
->diff_words
->plus
.text
.ptr
);
2253 free (ecbdata
->diff_words
->plus
.orig
);
2254 if (ecbdata
->diff_words
->word_regex
) {
2255 regfree(ecbdata
->diff_words
->word_regex
);
2256 free(ecbdata
->diff_words
->word_regex
);
2258 FREE_AND_NULL(ecbdata
->diff_words
);
2262 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
2264 if (want_color(diff_use_color
))
2265 return diff_colors
[ix
];
2269 const char *diff_line_prefix(struct diff_options
*opt
)
2271 struct strbuf
*msgbuf
;
2272 if (!opt
->output_prefix
)
2275 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
2279 static unsigned long sane_truncate_line(char *line
, unsigned long len
)
2282 unsigned long allot
;
2288 (void) utf8_width(&cp
, &l
);
2290 break; /* truncated in the middle? */
2295 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
2298 ecbdata
->lno_in_preimage
= 0;
2299 ecbdata
->lno_in_postimage
= 0;
2300 p
= strchr(line
, '-');
2302 return; /* cannot happen */
2303 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
2306 return; /* cannot happen */
2307 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
2310 static int fn_out_consume(void *priv
, char *line
, unsigned long len
)
2312 struct emit_callback
*ecbdata
= priv
;
2313 struct diff_options
*o
= ecbdata
->opt
;
2315 o
->found_changes
= 1;
2317 if (ecbdata
->header
) {
2318 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
2319 ecbdata
->header
->buf
, ecbdata
->header
->len
, 0);
2320 strbuf_reset(ecbdata
->header
);
2321 ecbdata
->header
= NULL
;
2324 if (ecbdata
->label_path
[0]) {
2325 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
2326 ecbdata
->label_path
[0],
2327 strlen(ecbdata
->label_path
[0]), 0);
2328 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
2329 ecbdata
->label_path
[1],
2330 strlen(ecbdata
->label_path
[1]), 0);
2331 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
2334 if (diff_suppress_blank_empty
2335 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
2340 if (line
[0] == '@') {
2341 if (ecbdata
->diff_words
)
2342 diff_words_flush(ecbdata
);
2343 len
= sane_truncate_line(line
, len
);
2344 find_lno(line
, ecbdata
);
2345 emit_hunk_header(ecbdata
, line
, len
);
2349 if (ecbdata
->diff_words
) {
2350 enum diff_symbol s
=
2351 ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
?
2352 DIFF_SYMBOL_WORDS_PORCELAIN
: DIFF_SYMBOL_WORDS
;
2353 if (line
[0] == '-') {
2354 diff_words_append(line
, len
,
2355 &ecbdata
->diff_words
->minus
);
2357 } else if (line
[0] == '+') {
2358 diff_words_append(line
, len
,
2359 &ecbdata
->diff_words
->plus
);
2361 } else if (starts_with(line
, "\\ ")) {
2363 * Eat the "no newline at eof" marker as if we
2364 * saw a "+" or "-" line with nothing on it,
2365 * and return without diff_words_flush() to
2366 * defer processing. If this is the end of
2367 * preimage, more "+" lines may come after it.
2371 diff_words_flush(ecbdata
);
2372 emit_diff_symbol(o
, s
, line
, len
, 0);
2378 ecbdata
->lno_in_postimage
++;
2379 emit_add_line(ecbdata
, line
+ 1, len
- 1);
2382 ecbdata
->lno_in_preimage
++;
2383 emit_del_line(ecbdata
, line
+ 1, len
- 1);
2386 ecbdata
->lno_in_postimage
++;
2387 ecbdata
->lno_in_preimage
++;
2388 emit_context_line(ecbdata
, line
+ 1, len
- 1);
2391 /* incomplete line at the end */
2392 ecbdata
->lno_in_preimage
++;
2393 emit_diff_symbol(o
, DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
2400 static void pprint_rename(struct strbuf
*name
, const char *a
, const char *b
)
2402 const char *old_name
= a
;
2403 const char *new_name
= b
;
2404 int pfx_length
, sfx_length
;
2405 int pfx_adjust_for_slash
;
2406 int len_a
= strlen(a
);
2407 int len_b
= strlen(b
);
2408 int a_midlen
, b_midlen
;
2409 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
2410 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
2412 if (qlen_a
|| qlen_b
) {
2413 quote_c_style(a
, name
, NULL
, 0);
2414 strbuf_addstr(name
, " => ");
2415 quote_c_style(b
, name
, NULL
, 0);
2419 /* Find common prefix */
2421 while (*old_name
&& *new_name
&& *old_name
== *new_name
) {
2422 if (*old_name
== '/')
2423 pfx_length
= old_name
- a
+ 1;
2428 /* Find common suffix */
2429 old_name
= a
+ len_a
;
2430 new_name
= b
+ len_b
;
2433 * If there is a common prefix, it must end in a slash. In
2434 * that case we let this loop run 1 into the prefix to see the
2437 * If there is no common prefix, we cannot do this as it would
2438 * underrun the input strings.
2440 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
2441 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old_name
&&
2442 b
+ pfx_length
- pfx_adjust_for_slash
<= new_name
&&
2443 *old_name
== *new_name
) {
2444 if (*old_name
== '/')
2445 sfx_length
= len_a
- (old_name
- a
);
2451 * pfx{mid-a => mid-b}sfx
2452 * {pfx-a => pfx-b}sfx
2453 * pfx{sfx-a => sfx-b}
2456 a_midlen
= len_a
- pfx_length
- sfx_length
;
2457 b_midlen
= len_b
- pfx_length
- sfx_length
;
2463 strbuf_grow(name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
2464 if (pfx_length
+ sfx_length
) {
2465 strbuf_add(name
, a
, pfx_length
);
2466 strbuf_addch(name
, '{');
2468 strbuf_add(name
, a
+ pfx_length
, a_midlen
);
2469 strbuf_addstr(name
, " => ");
2470 strbuf_add(name
, b
+ pfx_length
, b_midlen
);
2471 if (pfx_length
+ sfx_length
) {
2472 strbuf_addch(name
, '}');
2473 strbuf_add(name
, a
+ len_a
- sfx_length
, sfx_length
);
2477 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
2481 struct diffstat_file
*x
;
2483 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
2484 diffstat
->files
[diffstat
->nr
++] = x
;
2486 x
->from_name
= xstrdup(name_a
);
2487 x
->name
= xstrdup(name_b
);
2491 x
->from_name
= NULL
;
2492 x
->name
= xstrdup(name_a
);
2497 static int diffstat_consume(void *priv
, char *line
, unsigned long len
)
2499 struct diffstat_t
*diffstat
= priv
;
2500 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
2503 BUG("xdiff fed us an empty line");
2507 else if (line
[0] == '-')
2512 const char mime_boundary_leader
[] = "------------";
2514 static int scale_linear(int it
, int width
, int max_change
)
2519 * make sure that at least one '-' or '+' is printed if
2520 * there is any change to this path. The easiest way is to
2521 * scale linearly as if the allotted width is one column shorter
2522 * than it is, and then add 1 to the result.
2524 return 1 + (it
* (width
- 1) / max_change
);
2527 static void show_graph(struct strbuf
*out
, char ch
, int cnt
,
2528 const char *set
, const char *reset
)
2532 strbuf_addstr(out
, set
);
2533 strbuf_addchars(out
, ch
, cnt
);
2534 strbuf_addstr(out
, reset
);
2537 static void fill_print_name(struct diffstat_file
*file
)
2539 struct strbuf pname
= STRBUF_INIT
;
2541 if (file
->print_name
)
2544 if (file
->is_renamed
)
2545 pprint_rename(&pname
, file
->from_name
, file
->name
);
2547 quote_c_style(file
->name
, &pname
, NULL
, 0);
2550 strbuf_addf(&pname
, " (%s)", file
->comments
);
2552 file
->print_name
= strbuf_detach(&pname
, NULL
);
2555 static void print_stat_summary_inserts_deletes(struct diff_options
*options
,
2556 int files
, int insertions
, int deletions
)
2558 struct strbuf sb
= STRBUF_INIT
;
2561 assert(insertions
== 0 && deletions
== 0);
2562 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
2568 (files
== 1) ? " %d file changed" : " %d files changed",
2572 * For binary diff, the caller may want to print "x files
2573 * changed" with insertions == 0 && deletions == 0.
2575 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2576 * is probably less confusing (i.e skip over "2 files changed
2577 * but nothing about added/removed lines? Is this a bug in Git?").
2579 if (insertions
|| deletions
== 0) {
2581 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2585 if (deletions
|| insertions
== 0) {
2587 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2590 strbuf_addch(&sb
, '\n');
2591 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
2593 strbuf_release(&sb
);
2596 void print_stat_summary(FILE *fp
, int files
,
2597 int insertions
, int deletions
)
2599 struct diff_options o
;
2600 memset(&o
, 0, sizeof(o
));
2603 print_stat_summary_inserts_deletes(&o
, files
, insertions
, deletions
);
2606 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
2608 int i
, len
, add
, del
, adds
= 0, dels
= 0;
2609 uintmax_t max_change
= 0, max_len
= 0;
2610 int total_files
= data
->nr
, count
;
2611 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
2612 const char *reset
, *add_c
, *del_c
;
2613 int extra_shown
= 0;
2614 const char *line_prefix
= diff_line_prefix(options
);
2615 struct strbuf out
= STRBUF_INIT
;
2620 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
2622 reset
= diff_get_color_opt(options
, DIFF_RESET
);
2623 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
2624 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
2627 * Find the longest filename and max number of changes
2629 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
2630 struct diffstat_file
*file
= data
->files
[i
];
2631 uintmax_t change
= file
->added
+ file
->deleted
;
2633 if (!file
->is_interesting
&& (change
== 0)) {
2634 count
++; /* not shown == room for one more */
2637 fill_print_name(file
);
2638 len
= utf8_strwidth(file
->print_name
);
2642 if (file
->is_unmerged
) {
2643 /* "Unmerged" is 8 characters */
2644 bin_width
= bin_width
< 8 ? 8 : bin_width
;
2647 if (file
->is_binary
) {
2648 /* "Bin XXX -> YYY bytes" */
2649 int w
= 14 + decimal_width(file
->added
)
2650 + decimal_width(file
->deleted
);
2651 bin_width
= bin_width
< w
? w
: bin_width
;
2652 /* Display change counts aligned with "Bin" */
2657 if (max_change
< change
)
2658 max_change
= change
;
2660 count
= i
; /* where we can stop scanning in data->files[] */
2663 * We have width = stat_width or term_columns() columns total.
2664 * We want a maximum of min(max_len, stat_name_width) for the name part.
2665 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2666 * We also need 1 for " " and 4 + decimal_width(max_change)
2667 * for " | NNNN " and one the empty column at the end, altogether
2668 * 6 + decimal_width(max_change).
2670 * If there's not enough space, we will use the smaller of
2671 * stat_name_width (if set) and 5/8*width for the filename,
2672 * and the rest for constant elements + graph part, but no more
2673 * than stat_graph_width for the graph part.
2674 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2675 * for the standard terminal size).
2677 * In other words: stat_width limits the maximum width, and
2678 * stat_name_width fixes the maximum width of the filename,
2679 * and is also used to divide available columns if there
2682 * Binary files are displayed with "Bin XXX -> YYY bytes"
2683 * instead of the change count and graph. This part is treated
2684 * similarly to the graph part, except that it is not
2685 * "scaled". If total width is too small to accommodate the
2686 * guaranteed minimum width of the filename part and the
2687 * separators and this message, this message will "overflow"
2688 * making the line longer than the maximum width.
2692 * NEEDSWORK: line_prefix is often used for "log --graph" output
2693 * and contains ANSI-colored string. utf8_strnwidth() should be
2694 * used to correctly count the display width instead of strlen().
2696 if (options
->stat_width
== -1)
2697 width
= term_columns() - strlen(line_prefix
);
2699 width
= options
->stat_width
? options
->stat_width
: 80;
2700 number_width
= decimal_width(max_change
) > number_width
?
2701 decimal_width(max_change
) : number_width
;
2703 if (options
->stat_graph_width
== -1)
2704 options
->stat_graph_width
= diff_stat_graph_width
;
2707 * Guarantee 3/8*16==6 for the graph part
2708 * and 5/8*16==10 for the filename part
2710 if (width
< 16 + 6 + number_width
)
2711 width
= 16 + 6 + number_width
;
2714 * First assign sizes that are wanted, ignoring available width.
2715 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2716 * starting from "XXX" should fit in graph_width.
2718 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
2719 if (options
->stat_graph_width
&&
2720 options
->stat_graph_width
< graph_width
)
2721 graph_width
= options
->stat_graph_width
;
2723 name_width
= (options
->stat_name_width
> 0 &&
2724 options
->stat_name_width
< max_len
) ?
2725 options
->stat_name_width
: max_len
;
2728 * Adjust adjustable widths not to exceed maximum width
2730 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
2731 if (graph_width
> width
* 3/8 - number_width
- 6) {
2732 graph_width
= width
* 3/8 - number_width
- 6;
2733 if (graph_width
< 6)
2737 if (options
->stat_graph_width
&&
2738 graph_width
> options
->stat_graph_width
)
2739 graph_width
= options
->stat_graph_width
;
2740 if (name_width
> width
- number_width
- 6 - graph_width
)
2741 name_width
= width
- number_width
- 6 - graph_width
;
2743 graph_width
= width
- number_width
- 6 - name_width
;
2747 * From here name_width is the width of the name area,
2748 * and graph_width is the width of the graph area.
2749 * max_change is used to scale graph properly.
2751 for (i
= 0; i
< count
; i
++) {
2752 const char *prefix
= "";
2753 struct diffstat_file
*file
= data
->files
[i
];
2754 char *name
= file
->print_name
;
2755 uintmax_t added
= file
->added
;
2756 uintmax_t deleted
= file
->deleted
;
2757 int name_len
, padding
;
2759 if (!file
->is_interesting
&& (added
+ deleted
== 0))
2763 * "scale" the filename
2766 name_len
= utf8_strwidth(name
);
2767 if (name_width
< name_len
) {
2772 * NEEDSWORK: (name_len - len) counts the display
2773 * width, which would be shorter than the byte
2774 * length of the corresponding substring.
2775 * Advancing "name" by that number of bytes does
2776 * *NOT* skip over that many columns, so it is
2777 * very likely that chomping the pathname at the
2778 * slash we will find starting from "name" will
2779 * leave the resulting string still too long.
2781 name
+= name_len
- len
;
2782 slash
= strchr(name
, '/');
2786 padding
= len
- utf8_strwidth(name
);
2790 if (file
->is_binary
) {
2791 strbuf_addf(&out
, " %s%s%*s | %*s",
2792 prefix
, name
, padding
, "",
2793 number_width
, "Bin");
2794 if (!added
&& !deleted
) {
2795 strbuf_addch(&out
, '\n');
2796 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2797 out
.buf
, out
.len
, 0);
2801 strbuf_addf(&out
, " %s%"PRIuMAX
"%s",
2802 del_c
, deleted
, reset
);
2803 strbuf_addstr(&out
, " -> ");
2804 strbuf_addf(&out
, "%s%"PRIuMAX
"%s",
2805 add_c
, added
, reset
);
2806 strbuf_addstr(&out
, " bytes\n");
2807 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2808 out
.buf
, out
.len
, 0);
2812 else if (file
->is_unmerged
) {
2813 strbuf_addf(&out
, " %s%s%*s | %*s",
2814 prefix
, name
, padding
, "",
2815 number_width
, "Unmerged\n");
2816 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2817 out
.buf
, out
.len
, 0);
2823 * scale the add/delete
2828 if (graph_width
<= max_change
) {
2829 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
2830 if (total
< 2 && add
&& del
)
2831 /* width >= 2 due to the sanity check */
2834 add
= scale_linear(add
, graph_width
, max_change
);
2837 del
= scale_linear(del
, graph_width
, max_change
);
2841 strbuf_addf(&out
, " %s%s%*s | %*"PRIuMAX
"%s",
2842 prefix
, name
, padding
, "",
2843 number_width
, added
+ deleted
,
2844 added
+ deleted
? " " : "");
2845 show_graph(&out
, '+', add
, add_c
, reset
);
2846 show_graph(&out
, '-', del
, del_c
, reset
);
2847 strbuf_addch(&out
, '\n');
2848 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2849 out
.buf
, out
.len
, 0);
2853 for (i
= 0; i
< data
->nr
; i
++) {
2854 struct diffstat_file
*file
= data
->files
[i
];
2855 uintmax_t added
= file
->added
;
2856 uintmax_t deleted
= file
->deleted
;
2858 if (file
->is_unmerged
||
2859 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
2864 if (!file
->is_binary
) {
2871 emit_diff_symbol(options
,
2872 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
2877 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2878 strbuf_release(&out
);
2881 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
2883 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
2888 for (i
= 0; i
< data
->nr
; i
++) {
2889 int added
= data
->files
[i
]->added
;
2890 int deleted
= data
->files
[i
]->deleted
;
2892 if (data
->files
[i
]->is_unmerged
||
2893 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
2895 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
2900 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2903 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
2910 for (i
= 0; i
< data
->nr
; i
++) {
2911 struct diffstat_file
*file
= data
->files
[i
];
2913 fprintf(options
->file
, "%s", diff_line_prefix(options
));
2915 if (file
->is_binary
)
2916 fprintf(options
->file
, "-\t-\t");
2918 fprintf(options
->file
,
2919 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
2920 file
->added
, file
->deleted
);
2921 if (options
->line_termination
) {
2922 fill_print_name(file
);
2923 if (!file
->is_renamed
)
2924 write_name_quoted(file
->name
, options
->file
,
2925 options
->line_termination
);
2927 fputs(file
->print_name
, options
->file
);
2928 putc(options
->line_termination
, options
->file
);
2931 if (file
->is_renamed
) {
2932 putc('\0', options
->file
);
2933 write_name_quoted(file
->from_name
, options
->file
, '\0');
2935 write_name_quoted(file
->name
, options
->file
, '\0');
2940 struct dirstat_file
{
2942 unsigned long changed
;
2945 struct dirstat_dir
{
2946 struct dirstat_file
*files
;
2947 int alloc
, nr
, permille
, cumulative
;
2950 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
2951 unsigned long changed
, const char *base
, int baselen
)
2953 unsigned long sum_changes
= 0;
2954 unsigned int sources
= 0;
2955 const char *line_prefix
= diff_line_prefix(opt
);
2958 struct dirstat_file
*f
= dir
->files
;
2959 int namelen
= strlen(f
->name
);
2960 unsigned long changes
;
2963 if (namelen
< baselen
)
2965 if (memcmp(f
->name
, base
, baselen
))
2967 slash
= strchr(f
->name
+ baselen
, '/');
2969 int newbaselen
= slash
+ 1 - f
->name
;
2970 changes
= gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
2973 changes
= f
->changed
;
2978 sum_changes
+= changes
;
2982 * We don't report dirstat's for
2984 * - or cases where everything came from a single directory
2985 * under this directory (sources == 1).
2987 if (baselen
&& sources
!= 1) {
2989 int permille
= sum_changes
* 1000 / changed
;
2990 if (permille
>= dir
->permille
) {
2991 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
2992 permille
/ 10, permille
% 10, baselen
, base
);
2993 if (!dir
->cumulative
)
3001 static int dirstat_compare(const void *_a
, const void *_b
)
3003 const struct dirstat_file
*a
= _a
;
3004 const struct dirstat_file
*b
= _b
;
3005 return strcmp(a
->name
, b
->name
);
3008 static void show_dirstat(struct diff_options
*options
)
3011 unsigned long changed
;
3012 struct dirstat_dir dir
;
3013 struct diff_queue_struct
*q
= &diff_queued_diff
;
3018 dir
.permille
= options
->dirstat_permille
;
3019 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3022 for (i
= 0; i
< q
->nr
; i
++) {
3023 struct diff_filepair
*p
= q
->queue
[i
];
3025 unsigned long copied
, added
, damage
;
3026 struct diff_populate_filespec_options dpf_options
= {
3027 .check_size_only
= 1,
3030 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
3032 if (p
->one
->oid_valid
&& p
->two
->oid_valid
&&
3033 oideq(&p
->one
->oid
, &p
->two
->oid
)) {
3035 * The SHA1 has not changed, so pre-/post-content is
3036 * identical. We can therefore skip looking at the
3037 * file contents altogether.
3043 if (options
->flags
.dirstat_by_file
) {
3045 * In --dirstat-by-file mode, we don't really need to
3046 * look at the actual file contents at all.
3047 * The fact that the SHA1 changed is enough for us to
3048 * add this file to the list of results
3049 * (with each file contributing equal damage).
3055 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
3056 diff_populate_filespec(options
->repo
, p
->one
, NULL
);
3057 diff_populate_filespec(options
->repo
, p
->two
, NULL
);
3058 diffcore_count_changes(options
->repo
,
3059 p
->one
, p
->two
, NULL
, NULL
,
3061 diff_free_filespec_data(p
->one
);
3062 diff_free_filespec_data(p
->two
);
3063 } else if (DIFF_FILE_VALID(p
->one
)) {
3064 diff_populate_filespec(options
->repo
, p
->one
, &dpf_options
);
3066 diff_free_filespec_data(p
->one
);
3067 } else if (DIFF_FILE_VALID(p
->two
)) {
3068 diff_populate_filespec(options
->repo
, p
->two
, &dpf_options
);
3070 added
= p
->two
->size
;
3071 diff_free_filespec_data(p
->two
);
3076 * Original minus copied is the removed material,
3077 * added is the new material. They are both damages
3078 * made to the preimage.
3079 * If the resulting damage is zero, we know that
3080 * diffcore_count_changes() considers the two entries to
3081 * be identical, but since the oid changed, we
3082 * know that there must have been _some_ kind of change,
3083 * so we force all entries to have damage > 0.
3085 damage
= (p
->one
->size
- copied
) + added
;
3090 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3091 dir
.files
[dir
.nr
].name
= name
;
3092 dir
.files
[dir
.nr
].changed
= damage
;
3097 /* This can happen even with many files, if everything was renames */
3101 /* Show all directories with more than x% of the changes */
3102 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
3103 gather_dirstat(options
, &dir
, changed
, "", 0);
3106 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
3109 unsigned long changed
;
3110 struct dirstat_dir dir
;
3118 dir
.permille
= options
->dirstat_permille
;
3119 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3122 for (i
= 0; i
< data
->nr
; i
++) {
3123 struct diffstat_file
*file
= data
->files
[i
];
3124 unsigned long damage
= file
->added
+ file
->deleted
;
3125 if (file
->is_binary
)
3127 * binary files counts bytes, not lines. Must find some
3128 * way to normalize binary bytes vs. textual lines.
3129 * The following heuristic assumes that there are 64
3131 * This is stupid and ugly, but very cheap...
3133 damage
= DIV_ROUND_UP(damage
, 64);
3134 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3135 dir
.files
[dir
.nr
].name
= file
->name
;
3136 dir
.files
[dir
.nr
].changed
= damage
;
3141 /* This can happen even with many files, if everything was renames */
3145 /* Show all directories with more than x% of the changes */
3146 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
3147 gather_dirstat(options
, &dir
, changed
, "", 0);
3150 static void free_diffstat_file(struct diffstat_file
*f
)
3152 free(f
->print_name
);
3158 void free_diffstat_info(struct diffstat_t
*diffstat
)
3161 for (i
= 0; i
< diffstat
->nr
; i
++)
3162 free_diffstat_file(diffstat
->files
[i
]);
3163 free(diffstat
->files
);
3166 struct checkdiff_t
{
3167 const char *filename
;
3169 int conflict_marker_size
;
3170 struct diff_options
*o
;
3175 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
3180 if (len
< marker_size
+ 1)
3182 firstchar
= line
[0];
3183 switch (firstchar
) {
3184 case '=': case '>': case '<': case '|':
3189 for (cnt
= 1; cnt
< marker_size
; cnt
++)
3190 if (line
[cnt
] != firstchar
)
3192 /* line[1] through line[marker_size-1] are same as firstchar */
3193 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
3198 static void checkdiff_consume_hunk(void *priv
,
3199 long ob UNUSED
, long on UNUSED
,
3200 long nb
, long nn UNUSED
,
3201 const char *func UNUSED
, long funclen UNUSED
)
3204 struct checkdiff_t
*data
= priv
;
3205 data
->lineno
= nb
- 1;
3208 static int checkdiff_consume(void *priv
, char *line
, unsigned long len
)
3210 struct checkdiff_t
*data
= priv
;
3211 int marker_size
= data
->conflict_marker_size
;
3212 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
3213 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
3214 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
3216 const char *line_prefix
;
3219 line_prefix
= diff_line_prefix(data
->o
);
3221 if (line
[0] == '+') {
3224 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
3226 fprintf(data
->o
->file
,
3227 "%s%s:%d: leftover conflict marker\n",
3228 line_prefix
, data
->filename
, data
->lineno
);
3230 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
3233 data
->status
|= bad
;
3234 err
= whitespace_error_string(bad
);
3235 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
3236 line_prefix
, data
->filename
, data
->lineno
, err
);
3238 emit_line(data
->o
, set
, reset
, line
, 1);
3239 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
3240 data
->o
->file
, set
, reset
, ws
);
3241 } else if (line
[0] == ' ') {
3247 static unsigned char *deflate_it(char *data
,
3249 unsigned long *result_size
)
3252 unsigned char *deflated
;
3255 git_deflate_init(&stream
, zlib_compression_level
);
3256 bound
= git_deflate_bound(&stream
, size
);
3257 deflated
= xmalloc(bound
);
3258 stream
.next_out
= deflated
;
3259 stream
.avail_out
= bound
;
3261 stream
.next_in
= (unsigned char *)data
;
3262 stream
.avail_in
= size
;
3263 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
3265 git_deflate_end(&stream
);
3266 *result_size
= stream
.total_out
;
3270 static void emit_binary_diff_body(struct diff_options
*o
,
3271 mmfile_t
*one
, mmfile_t
*two
)
3277 unsigned long orig_size
;
3278 unsigned long delta_size
;
3279 unsigned long deflate_size
;
3280 unsigned long data_size
;
3282 /* We could do deflated delta, or we could do just deflated two,
3283 * whichever is smaller.
3286 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
3287 if (one
->size
&& two
->size
) {
3288 delta
= diff_delta(one
->ptr
, one
->size
,
3289 two
->ptr
, two
->size
,
3290 &delta_size
, deflate_size
);
3292 void *to_free
= delta
;
3293 orig_size
= delta_size
;
3294 delta
= deflate_it(delta
, delta_size
, &delta_size
);
3299 if (delta
&& delta_size
< deflate_size
) {
3300 char *s
= xstrfmt("%"PRIuMAX
, (uintmax_t)orig_size
);
3301 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
3306 data_size
= delta_size
;
3308 char *s
= xstrfmt("%lu", two
->size
);
3309 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
3314 data_size
= deflate_size
;
3317 /* emit data encoded in base85 */
3321 int bytes
= (52 < data_size
) ? 52 : data_size
;
3325 line
[0] = bytes
+ 'A' - 1;
3327 line
[0] = bytes
- 26 + 'a' - 1;
3328 encode_85(line
+ 1, cp
, bytes
);
3329 cp
= (char *) cp
+ bytes
;
3335 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_BODY
,
3338 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_FOOTER
, NULL
, 0, 0);
3342 static void emit_binary_diff(struct diff_options
*o
,
3343 mmfile_t
*one
, mmfile_t
*two
)
3345 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER
, NULL
, 0, 0);
3346 emit_binary_diff_body(o
, one
, two
);
3347 emit_binary_diff_body(o
, two
, one
);
3350 int diff_filespec_is_binary(struct repository
*r
,
3351 struct diff_filespec
*one
)
3353 struct diff_populate_filespec_options dpf_options
= {
3357 if (one
->is_binary
== -1) {
3358 diff_filespec_load_driver(one
, r
->index
);
3359 if (one
->driver
->binary
!= -1)
3360 one
->is_binary
= one
->driver
->binary
;
3362 if (!one
->data
&& DIFF_FILE_VALID(one
))
3363 diff_populate_filespec(r
, one
, &dpf_options
);
3364 if (one
->is_binary
== -1 && one
->data
)
3365 one
->is_binary
= buffer_is_binary(one
->data
,
3367 if (one
->is_binary
== -1)
3371 return one
->is_binary
;
3374 static const struct userdiff_funcname
*
3375 diff_funcname_pattern(struct diff_options
*o
, struct diff_filespec
*one
)
3377 diff_filespec_load_driver(one
, o
->repo
->index
);
3378 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
3381 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
3383 if (!options
->a_prefix
)
3384 options
->a_prefix
= a
;
3385 if (!options
->b_prefix
)
3386 options
->b_prefix
= b
;
3389 void diff_set_noprefix(struct diff_options
*options
)
3391 options
->a_prefix
= options
->b_prefix
= "";
3394 void diff_set_default_prefix(struct diff_options
*options
)
3396 options
->a_prefix
= "a/";
3397 options
->b_prefix
= "b/";
3400 struct userdiff_driver
*get_textconv(struct repository
*r
,
3401 struct diff_filespec
*one
)
3403 if (!DIFF_FILE_VALID(one
))
3406 diff_filespec_load_driver(one
, r
->index
);
3407 return userdiff_get_textconv(r
, one
->driver
);
3410 static struct string_list
*additional_headers(struct diff_options
*o
,
3413 if (!o
->additional_path_headers
)
3415 return strmap_get(o
->additional_path_headers
, path
);
3418 static void add_formatted_header(struct strbuf
*msg
,
3420 const char *line_prefix
,
3424 const char *next
, *newline
;
3426 for (next
= header
; *next
; next
= newline
) {
3427 newline
= strchrnul(next
, '\n');
3428 strbuf_addf(msg
, "%s%s%.*s%s\n", line_prefix
, meta
,
3429 (int)(newline
- next
), next
, reset
);
3435 static void add_formatted_headers(struct strbuf
*msg
,
3436 struct string_list
*more_headers
,
3437 const char *line_prefix
,
3443 for (i
= 0; i
< more_headers
->nr
; i
++)
3444 add_formatted_header(msg
, more_headers
->items
[i
].string
,
3445 line_prefix
, meta
, reset
);
3448 static int diff_filepair_is_phoney(struct diff_filespec
*one
,
3449 struct diff_filespec
*two
)
3452 * This function specifically looks for pairs injected by
3453 * create_filepairs_for_header_only_notifications(). Such
3454 * pairs are "phoney" in that they do not represent any
3455 * content or even mode difference, but were inserted because
3456 * diff_queued_diff previously had no pair associated with
3457 * that path but we needed some pair to avoid losing the
3458 * "remerge CONFLICT" header associated with the path.
3460 return !DIFF_FILE_VALID(one
) && !DIFF_FILE_VALID(two
);
3463 static int set_diff_algorithm(struct diff_options
*opts
,
3466 long value
= parse_algorithm_value(alg
);
3471 /* clear out previous settings */
3472 DIFF_XDL_CLR(opts
, NEED_MINIMAL
);
3473 opts
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3474 opts
->xdl_opts
|= value
;
3479 static void builtin_diff(const char *name_a
,
3481 struct diff_filespec
*one
,
3482 struct diff_filespec
*two
,
3483 const char *xfrm_msg
,
3484 int must_show_header
,
3485 struct diff_options
*o
,
3486 int complete_rewrite
)
3490 char *a_one
, *b_two
;
3491 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
3492 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
3493 const char *a_prefix
, *b_prefix
;
3494 struct userdiff_driver
*textconv_one
= NULL
;
3495 struct userdiff_driver
*textconv_two
= NULL
;
3496 struct strbuf header
= STRBUF_INIT
;
3497 const char *line_prefix
= diff_line_prefix(o
);
3499 diff_set_mnemonic_prefix(o
, "a/", "b/");
3500 if (o
->flags
.reverse_diff
) {
3501 a_prefix
= o
->b_prefix
;
3502 b_prefix
= o
->a_prefix
;
3504 a_prefix
= o
->a_prefix
;
3505 b_prefix
= o
->b_prefix
;
3508 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
3509 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3510 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3511 (!diff_filepair_is_phoney(one
, two
))) {
3512 show_submodule_diff_summary(o
, one
->path
? one
->path
: two
->path
,
3513 &one
->oid
, &two
->oid
,
3514 two
->dirty_submodule
);
3516 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
3517 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3518 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3519 (!diff_filepair_is_phoney(one
, two
))) {
3520 show_submodule_inline_diff(o
, one
->path
? one
->path
: two
->path
,
3521 &one
->oid
, &two
->oid
,
3522 two
->dirty_submodule
);
3526 if (o
->flags
.allow_textconv
) {
3527 textconv_one
= get_textconv(o
->repo
, one
);
3528 textconv_two
= get_textconv(o
->repo
, two
);
3531 /* Never use a non-valid filename anywhere if at all possible */
3532 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
3533 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
3535 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
3536 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
3537 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
3538 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
3539 if (diff_filepair_is_phoney(one
, two
)) {
3541 * We should only reach this point for pairs generated from
3542 * create_filepairs_for_header_only_notifications(). For
3543 * these, we want to avoid the "/dev/null" special casing
3544 * above, because we do not want such pairs shown as either
3545 * "new file" or "deleted file" below.
3550 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3551 if (lbl
[0][0] == '/') {
3553 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3555 strbuf_addstr(&header
, xfrm_msg
);
3556 must_show_header
= 1;
3558 else if (lbl
[1][0] == '/') {
3559 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3561 strbuf_addstr(&header
, xfrm_msg
);
3562 must_show_header
= 1;
3565 if (one
->mode
!= two
->mode
) {
3566 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3567 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3568 must_show_header
= 1;
3571 strbuf_addstr(&header
, xfrm_msg
);
3574 * we do not run diff between different kind
3577 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3578 goto free_ab_and_return
;
3579 if (complete_rewrite
&&
3580 (textconv_one
|| !diff_filespec_is_binary(o
->repo
, one
)) &&
3581 (textconv_two
|| !diff_filespec_is_binary(o
->repo
, two
))) {
3582 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3583 header
.buf
, header
.len
, 0);
3584 strbuf_reset(&header
);
3585 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3586 textconv_one
, textconv_two
, o
);
3587 o
->found_changes
= 1;
3588 goto free_ab_and_return
;
3592 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3593 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3595 strbuf_reset(&header
);
3596 goto free_ab_and_return
;
3597 } else if (!o
->flags
.text
&&
3598 ( (!textconv_one
&& diff_filespec_is_binary(o
->repo
, one
)) ||
3599 (!textconv_two
&& diff_filespec_is_binary(o
->repo
, two
)) )) {
3600 struct strbuf sb
= STRBUF_INIT
;
3601 if (!one
->data
&& !two
->data
&&
3602 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3604 if (oideq(&one
->oid
, &two
->oid
)) {
3605 if (must_show_header
)
3606 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3607 header
.buf
, header
.len
,
3609 goto free_ab_and_return
;
3611 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3612 header
.buf
, header
.len
, 0);
3613 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3614 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3615 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3617 strbuf_release(&sb
);
3618 goto free_ab_and_return
;
3620 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3621 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3622 die("unable to read files to diff");
3623 /* Quite common confusing case */
3624 if (mf1
.size
== mf2
.size
&&
3625 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3626 if (must_show_header
)
3627 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3628 header
.buf
, header
.len
, 0);
3629 goto free_ab_and_return
;
3631 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3632 strbuf_reset(&header
);
3633 if (o
->flags
.binary
)
3634 emit_binary_diff(o
, &mf1
, &mf2
);
3636 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3637 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3638 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3640 strbuf_release(&sb
);
3642 o
->found_changes
= 1;
3644 /* Crazy xdl interfaces.. */
3645 const char *diffopts
;
3649 struct emit_callback ecbdata
;
3650 const struct userdiff_funcname
*pe
;
3652 if (must_show_header
) {
3653 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3654 header
.buf
, header
.len
, 0);
3655 strbuf_reset(&header
);
3658 mf1
.size
= fill_textconv(o
->repo
, textconv_one
, one
, &mf1
.ptr
);
3659 mf2
.size
= fill_textconv(o
->repo
, textconv_two
, two
, &mf2
.ptr
);
3661 pe
= diff_funcname_pattern(o
, one
);
3663 pe
= diff_funcname_pattern(o
, two
);
3665 memset(&xpp
, 0, sizeof(xpp
));
3666 memset(&xecfg
, 0, sizeof(xecfg
));
3667 memset(&ecbdata
, 0, sizeof(ecbdata
));
3668 if (o
->flags
.suppress_diff_headers
)
3670 ecbdata
.label_path
= lbl
;
3671 ecbdata
.color_diff
= want_color(o
->use_color
);
3672 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
3673 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3674 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3676 if (header
.len
&& !o
->flags
.suppress_diff_headers
)
3677 ecbdata
.header
= &header
;
3678 xpp
.flags
= o
->xdl_opts
;
3679 xpp
.ignore_regex
= o
->ignore_regex
;
3680 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3681 xpp
.anchors
= o
->anchors
;
3682 xpp
.anchors_nr
= o
->anchors_nr
;
3683 xecfg
.ctxlen
= o
->context
;
3684 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3685 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3686 if (o
->flags
.funccontext
)
3687 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3689 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3691 diffopts
= getenv("GIT_DIFF_OPTS");
3694 else if (skip_prefix(diffopts
, "--unified=", &v
))
3695 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3696 else if (skip_prefix(diffopts
, "-u", &v
))
3697 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3700 init_diff_words_data(&ecbdata
, o
, one
, two
);
3701 if (xdi_diff_outf(&mf1
, &mf2
, NULL
, fn_out_consume
,
3702 &ecbdata
, &xpp
, &xecfg
))
3703 die("unable to generate diff for %s", one
->path
);
3705 free_diff_words_data(&ecbdata
);
3710 xdiff_clear_find_func(&xecfg
);
3714 strbuf_release(&header
);
3715 diff_free_filespec_data(one
);
3716 diff_free_filespec_data(two
);
3722 static char *get_compact_summary(const struct diff_filepair
*p
, int is_renamed
)
3725 if (p
->status
== DIFF_STATUS_ADDED
) {
3726 if (S_ISLNK(p
->two
->mode
))
3728 else if ((p
->two
->mode
& 0777) == 0755)
3732 } else if (p
->status
== DIFF_STATUS_DELETED
)
3735 if (S_ISLNK(p
->one
->mode
) && !S_ISLNK(p
->two
->mode
))
3737 else if (!S_ISLNK(p
->one
->mode
) && S_ISLNK(p
->two
->mode
))
3739 else if ((p
->one
->mode
& 0777) == 0644 &&
3740 (p
->two
->mode
& 0777) == 0755)
3742 else if ((p
->one
->mode
& 0777) == 0755 &&
3743 (p
->two
->mode
& 0777) == 0644)
3748 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3749 struct diff_filespec
*one
,
3750 struct diff_filespec
*two
,
3751 struct diffstat_t
*diffstat
,
3752 struct diff_options
*o
,
3753 struct diff_filepair
*p
)
3756 struct diffstat_file
*data
;
3758 int complete_rewrite
= 0;
3760 if (!DIFF_PAIR_UNMERGED(p
)) {
3761 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3762 complete_rewrite
= 1;
3765 data
= diffstat_add(diffstat
, name_a
, name_b
);
3766 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3767 if (o
->flags
.stat_with_summary
)
3768 data
->comments
= get_compact_summary(p
, data
->is_renamed
);
3771 data
->is_unmerged
= 1;
3775 /* saves some reads if true, not a guarantee of diff outcome */
3776 may_differ
= !(one
->oid_valid
&& two
->oid_valid
&&
3777 oideq(&one
->oid
, &two
->oid
));
3779 if (diff_filespec_is_binary(o
->repo
, one
) ||
3780 diff_filespec_is_binary(o
->repo
, two
)) {
3781 data
->is_binary
= 1;
3786 data
->added
= diff_filespec_size(o
->repo
, two
);
3787 data
->deleted
= diff_filespec_size(o
->repo
, one
);
3791 else if (complete_rewrite
) {
3792 diff_populate_filespec(o
->repo
, one
, NULL
);
3793 diff_populate_filespec(o
->repo
, two
, NULL
);
3794 data
->deleted
= count_lines(one
->data
, one
->size
);
3795 data
->added
= count_lines(two
->data
, two
->size
);
3798 else if (may_differ
) {
3799 /* Crazy xdl interfaces.. */
3803 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3804 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3805 die("unable to read files to diff");
3807 memset(&xpp
, 0, sizeof(xpp
));
3808 memset(&xecfg
, 0, sizeof(xecfg
));
3809 xpp
.flags
= o
->xdl_opts
;
3810 xpp
.ignore_regex
= o
->ignore_regex
;
3811 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3812 xpp
.anchors
= o
->anchors
;
3813 xpp
.anchors_nr
= o
->anchors_nr
;
3814 xecfg
.ctxlen
= o
->context
;
3815 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3816 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
3817 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
3818 diffstat_consume
, diffstat
, &xpp
, &xecfg
))
3819 die("unable to generate diffstat for %s", one
->path
);
3821 if (DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
)) {
3822 struct diffstat_file
*file
=
3823 diffstat
->files
[diffstat
->nr
- 1];
3825 * Omit diffstats of modified files where nothing changed.
3826 * Even if may_differ, this might be the case due to
3827 * ignoring whitespace changes, etc.
3829 * But note that we special-case additions, deletions,
3830 * renames, and mode changes as adding an empty file,
3831 * for example is still of interest.
3833 if ((p
->status
== DIFF_STATUS_MODIFIED
)
3836 && one
->mode
== two
->mode
) {
3837 free_diffstat_file(file
);
3843 diff_free_filespec_data(one
);
3844 diff_free_filespec_data(two
);
3847 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3848 const char *attr_path
,
3849 struct diff_filespec
*one
,
3850 struct diff_filespec
*two
,
3851 struct diff_options
*o
)
3854 struct checkdiff_t data
;
3859 memset(&data
, 0, sizeof(data
));
3860 data
.filename
= name_b
? name_b
: name_a
;
3863 data
.ws_rule
= whitespace_rule(o
->repo
->index
, attr_path
);
3864 data
.conflict_marker_size
= ll_merge_marker_size(o
->repo
->index
, attr_path
);
3866 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3867 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3868 die("unable to read files to diff");
3871 * All the other codepaths check both sides, but not checking
3872 * the "old" side here is deliberate. We are checking the newly
3873 * introduced changes, and as long as the "new" side is text, we
3874 * can and should check what it introduces.
3876 if (diff_filespec_is_binary(o
->repo
, two
))
3877 goto free_and_return
;
3879 /* Crazy xdl interfaces.. */
3883 memset(&xpp
, 0, sizeof(xpp
));
3884 memset(&xecfg
, 0, sizeof(xecfg
));
3885 xecfg
.ctxlen
= 1; /* at least one context line */
3887 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume_hunk
,
3888 checkdiff_consume
, &data
,
3890 die("unable to generate checkdiff for %s", one
->path
);
3892 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3893 struct emit_callback ecbdata
;
3896 ecbdata
.ws_rule
= data
.ws_rule
;
3897 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3898 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3903 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3904 fprintf(o
->file
, "%s:%d: %s.\n",
3905 data
.filename
, blank_at_eof
, err
);
3906 data
.status
= 1; /* report errors */
3911 diff_free_filespec_data(one
);
3912 diff_free_filespec_data(two
);
3914 o
->flags
.check_failed
= 1;
3917 struct diff_filespec
*alloc_filespec(const char *path
)
3919 struct diff_filespec
*spec
;
3921 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3923 spec
->is_binary
= -1;
3927 void free_filespec(struct diff_filespec
*spec
)
3929 if (!--spec
->count
) {
3930 diff_free_filespec_data(spec
);
3935 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3936 int oid_valid
, unsigned short mode
)
3939 spec
->mode
= canon_mode(mode
);
3940 oidcpy(&spec
->oid
, oid
);
3941 spec
->oid_valid
= oid_valid
;
3946 * Given a name and sha1 pair, if the index tells us the file in
3947 * the work tree has that object contents, return true, so that
3948 * prepare_temp_file() does not have to inflate and extract.
3950 static int reuse_worktree_file(struct index_state
*istate
,
3952 const struct object_id
*oid
,
3955 const struct cache_entry
*ce
;
3960 * We do not read the cache ourselves here, because the
3961 * benchmark with my previous version that always reads cache
3962 * shows that it makes things worse for diff-tree comparing
3963 * two linux-2.6 kernel trees in an already checked out work
3964 * tree. This is because most diff-tree comparisons deal with
3965 * only a small number of files, while reading the cache is
3966 * expensive for a large project, and its cost outweighs the
3967 * savings we get by not inflating the object to a temporary
3968 * file. Practically, this code only helps when we are used
3969 * by diff-cache --cached, which does read the cache before
3975 /* We want to avoid the working directory if our caller
3976 * doesn't need the data in a normal file, this system
3977 * is rather slow with its stat/open/mmap/close syscalls,
3978 * and the object is contained in a pack file. The pack
3979 * is probably already open and will be faster to obtain
3980 * the data through than the working directory. Loose
3981 * objects however would tend to be slower as they need
3982 * to be individually opened and inflated.
3984 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_object_pack(oid
))
3988 * Similarly, if we'd have to convert the file contents anyway, that
3989 * makes the optimization not worthwhile.
3991 if (!want_file
&& would_convert_to_git(istate
, name
))
3995 * If this path does not match our sparse-checkout definition,
3996 * then the file will not be in the working directory.
3998 if (!path_in_sparse_checkout(name
, istate
))
4002 pos
= index_name_pos(istate
, name
, len
);
4005 ce
= istate
->cache
[pos
];
4008 * This is not the sha1 we are looking for, or
4009 * unreusable because it is not a regular file.
4011 if (!oideq(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
4015 * If ce is marked as "assume unchanged", there is no
4016 * guarantee that work tree matches what we are looking for.
4018 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
4022 * If ce matches the file in the work tree, we can reuse it.
4024 if (ce_uptodate(ce
) ||
4025 (!lstat(name
, &st
) && !ie_match_stat(istate
, ce
, &st
, 0)))
4031 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
4033 struct strbuf buf
= STRBUF_INIT
;
4036 /* Are we looking at the work tree? */
4037 if (s
->dirty_submodule
)
4040 strbuf_addf(&buf
, "Subproject commit %s%s\n",
4041 oid_to_hex(&s
->oid
), dirty
);
4045 strbuf_release(&buf
);
4047 s
->data
= strbuf_detach(&buf
, NULL
);
4054 * While doing rename detection and pickaxe operation, we may need to
4055 * grab the data for the blob (or file) for our own in-core comparison.
4056 * diff_filespec has data and size fields for this purpose.
4058 int diff_populate_filespec(struct repository
*r
,
4059 struct diff_filespec
*s
,
4060 const struct diff_populate_filespec_options
*options
)
4062 int size_only
= options
? options
->check_size_only
: 0;
4063 int check_binary
= options
? options
->check_binary
: 0;
4065 int conv_flags
= global_conv_flags_eol
;
4067 * demote FAIL to WARN to allow inspecting the situation
4068 * instead of refusing.
4070 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
4071 conv_flags
= CONV_EOL_RNDTRP_WARN
;
4073 if (!DIFF_FILE_VALID(s
))
4074 die("internal error: asking to populate invalid file.");
4075 if (S_ISDIR(s
->mode
))
4081 if (size_only
&& 0 < s
->size
)
4084 if (S_ISGITLINK(s
->mode
))
4085 return diff_populate_gitlink(s
, size_only
);
4087 if (!s
->oid_valid
||
4088 reuse_worktree_file(r
->index
, s
->path
, &s
->oid
, 0)) {
4089 struct strbuf buf
= STRBUF_INIT
;
4093 if (lstat(s
->path
, &st
) < 0) {
4097 s
->data
= (char *)"";
4101 s
->size
= xsize_t(st
.st_size
);
4104 if (S_ISLNK(st
.st_mode
)) {
4105 struct strbuf sb
= STRBUF_INIT
;
4107 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
4110 s
->data
= strbuf_detach(&sb
, NULL
);
4116 * Even if the caller would be happy with getting
4117 * only the size, we cannot return early at this
4118 * point if the path requires us to run the content
4121 if (size_only
&& !would_convert_to_git(r
->index
, s
->path
))
4125 * Note: this check uses xsize_t(st.st_size) that may
4126 * not be the true size of the blob after it goes
4127 * through convert_to_git(). This may not strictly be
4128 * correct, but the whole point of big_file_threshold
4129 * and is_binary check being that we want to avoid
4130 * opening the file and inspecting the contents, this
4134 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4138 fd
= open(s
->path
, O_RDONLY
);
4141 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
4143 s
->should_munmap
= 1;
4146 * Convert from working tree format to canonical git format
4148 if (convert_to_git(r
->index
, s
->path
, s
->data
, s
->size
, &buf
, conv_flags
)) {
4150 munmap(s
->data
, s
->size
);
4151 s
->should_munmap
= 0;
4152 s
->data
= strbuf_detach(&buf
, &size
);
4158 struct object_info info
= {
4162 if (!(size_only
|| check_binary
))
4164 * Set contentp, since there is no chance that merely
4165 * the size is sufficient.
4167 info
.contentp
= &s
->data
;
4169 if (options
&& options
->missing_object_cb
) {
4170 if (!oid_object_info_extended(r
, &s
->oid
, &info
,
4171 OBJECT_INFO_LOOKUP_REPLACE
|
4172 OBJECT_INFO_SKIP_FETCH_OBJECT
))
4174 options
->missing_object_cb(options
->missing_object_data
);
4176 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4177 OBJECT_INFO_LOOKUP_REPLACE
))
4178 die("unable to read %s", oid_to_hex(&s
->oid
));
4181 if (size_only
|| check_binary
) {
4184 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4189 if (!info
.contentp
) {
4190 info
.contentp
= &s
->data
;
4191 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4192 OBJECT_INFO_LOOKUP_REPLACE
))
4193 die("unable to read %s", oid_to_hex(&s
->oid
));
4200 void diff_free_filespec_blob(struct diff_filespec
*s
)
4204 else if (s
->should_munmap
)
4205 munmap(s
->data
, s
->size
);
4207 if (s
->should_free
|| s
->should_munmap
) {
4208 s
->should_free
= s
->should_munmap
= 0;
4213 void diff_free_filespec_data(struct diff_filespec
*s
)
4218 diff_free_filespec_blob(s
);
4219 FREE_AND_NULL(s
->cnt_data
);
4222 static void prep_temp_blob(struct index_state
*istate
,
4223 const char *path
, struct diff_tempfile
*temp
,
4226 const struct object_id
*oid
,
4229 struct strbuf buf
= STRBUF_INIT
;
4230 char *path_dup
= xstrdup(path
);
4231 const char *base
= basename(path_dup
);
4232 struct checkout_metadata meta
;
4234 init_checkout_metadata(&meta
, NULL
, NULL
, oid
);
4236 temp
->tempfile
= mks_tempfile_dt("git-blob-XXXXXX", base
);
4237 if (!temp
->tempfile
)
4238 die_errno("unable to create temp-file");
4239 if (convert_to_working_tree(istate
, path
,
4240 (const char *)blob
, (size_t)size
, &buf
, &meta
)) {
4244 if (write_in_full(temp
->tempfile
->fd
, blob
, size
) < 0 ||
4245 close_tempfile_gently(temp
->tempfile
))
4246 die_errno("unable to write temp-file");
4247 temp
->name
= get_tempfile_path(temp
->tempfile
);
4248 oid_to_hex_r(temp
->hex
, oid
);
4249 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
4250 strbuf_release(&buf
);
4254 static struct diff_tempfile
*prepare_temp_file(struct repository
*r
,
4255 struct diff_filespec
*one
)
4257 struct diff_tempfile
*temp
= claim_diff_tempfile();
4259 if (!DIFF_FILE_VALID(one
)) {
4261 /* A '-' entry produces this for file-2, and
4262 * a '+' entry produces this for file-1.
4264 temp
->name
= "/dev/null";
4265 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
4266 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
4270 if (!S_ISGITLINK(one
->mode
) &&
4272 reuse_worktree_file(r
->index
, one
->path
, &one
->oid
, 1))) {
4274 if (lstat(one
->path
, &st
) < 0) {
4275 if (errno
== ENOENT
)
4276 goto not_a_valid_file
;
4277 die_errno("stat(%s)", one
->path
);
4279 if (S_ISLNK(st
.st_mode
)) {
4280 struct strbuf sb
= STRBUF_INIT
;
4281 if (strbuf_readlink(&sb
, one
->path
, st
.st_size
) < 0)
4282 die_errno("readlink(%s)", one
->path
);
4283 prep_temp_blob(r
->index
, one
->path
, temp
, sb
.buf
, sb
.len
,
4285 &one
->oid
: null_oid()),
4287 one
->mode
: S_IFLNK
));
4288 strbuf_release(&sb
);
4291 /* we can borrow from the file in the work tree */
4292 temp
->name
= one
->path
;
4293 if (!one
->oid_valid
)
4294 oid_to_hex_r(temp
->hex
, null_oid());
4296 oid_to_hex_r(temp
->hex
, &one
->oid
);
4297 /* Even though we may sometimes borrow the
4298 * contents from the work tree, we always want
4299 * one->mode. mode is trustworthy even when
4300 * !(one->oid_valid), as long as
4301 * DIFF_FILE_VALID(one).
4303 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
4308 if (diff_populate_filespec(r
, one
, NULL
))
4309 die("cannot read data blob for %s", one
->path
);
4310 prep_temp_blob(r
->index
, one
->path
, temp
,
4311 one
->data
, one
->size
,
4312 &one
->oid
, one
->mode
);
4317 static void add_external_diff_name(struct repository
*r
,
4318 struct strvec
*argv
,
4319 struct diff_filespec
*df
)
4321 struct diff_tempfile
*temp
= prepare_temp_file(r
, df
);
4322 strvec_push(argv
, temp
->name
);
4323 strvec_push(argv
, temp
->hex
);
4324 strvec_push(argv
, temp
->mode
);
4327 /* An external diff command takes:
4329 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4330 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4333 static void run_external_diff(const char *pgm
,
4336 struct diff_filespec
*one
,
4337 struct diff_filespec
*two
,
4338 const char *xfrm_msg
,
4339 struct diff_options
*o
)
4341 struct child_process cmd
= CHILD_PROCESS_INIT
;
4342 struct diff_queue_struct
*q
= &diff_queued_diff
;
4344 strvec_push(&cmd
.args
, pgm
);
4345 strvec_push(&cmd
.args
, name
);
4348 add_external_diff_name(o
->repo
, &cmd
.args
, one
);
4349 add_external_diff_name(o
->repo
, &cmd
.args
, two
);
4351 strvec_push(&cmd
.args
, other
);
4352 strvec_push(&cmd
.args
, xfrm_msg
);
4356 strvec_pushf(&cmd
.env
, "GIT_DIFF_PATH_COUNTER=%d",
4357 ++o
->diff_path_counter
);
4358 strvec_pushf(&cmd
.env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
4360 diff_free_filespec_data(one
);
4361 diff_free_filespec_data(two
);
4363 if (run_command(&cmd
))
4364 die(_("external diff died, stopping at %s"), name
);
4369 static int similarity_index(struct diff_filepair
*p
)
4371 return p
->score
* 100 / MAX_SCORE
;
4374 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
4376 if (startup_info
->have_repository
)
4377 return repo_find_unique_abbrev(the_repository
, oid
, abbrev
);
4379 char *hex
= oid_to_hex(oid
);
4381 abbrev
= FALLBACK_DEFAULT_ABBREV
;
4382 if (abbrev
> the_hash_algo
->hexsz
)
4383 BUG("oid abbreviation out of range: %d", abbrev
);
4390 static void fill_metainfo(struct strbuf
*msg
,
4393 struct diff_filespec
*one
,
4394 struct diff_filespec
*two
,
4395 struct diff_options
*o
,
4396 struct diff_filepair
*p
,
4397 int *must_show_header
,
4400 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
4401 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
4402 const char *line_prefix
= diff_line_prefix(o
);
4403 struct string_list
*more_headers
= NULL
;
4405 *must_show_header
= 1;
4406 strbuf_init(msg
, PATH_MAX
* 2 + 300);
4407 switch (p
->status
) {
4408 case DIFF_STATUS_COPIED
:
4409 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4410 line_prefix
, set
, similarity_index(p
));
4411 strbuf_addf(msg
, "%s\n%s%scopy from ",
4412 reset
, line_prefix
, set
);
4413 quote_c_style(name
, msg
, NULL
, 0);
4414 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
4415 quote_c_style(other
, msg
, NULL
, 0);
4416 strbuf_addf(msg
, "%s\n", reset
);
4418 case DIFF_STATUS_RENAMED
:
4419 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4420 line_prefix
, set
, similarity_index(p
));
4421 strbuf_addf(msg
, "%s\n%s%srename from ",
4422 reset
, line_prefix
, set
);
4423 quote_c_style(name
, msg
, NULL
, 0);
4424 strbuf_addf(msg
, "%s\n%s%srename to ",
4425 reset
, line_prefix
, set
);
4426 quote_c_style(other
, msg
, NULL
, 0);
4427 strbuf_addf(msg
, "%s\n", reset
);
4429 case DIFF_STATUS_MODIFIED
:
4431 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
4433 set
, similarity_index(p
), reset
);
4438 *must_show_header
= 0;
4440 if ((more_headers
= additional_headers(o
, name
))) {
4441 add_formatted_headers(msg
, more_headers
,
4442 line_prefix
, set
, reset
);
4443 *must_show_header
= 1;
4445 if (one
&& two
&& !oideq(&one
->oid
, &two
->oid
)) {
4446 const unsigned hexsz
= the_hash_algo
->hexsz
;
4447 int abbrev
= o
->abbrev
? o
->abbrev
: DEFAULT_ABBREV
;
4449 if (o
->flags
.full_index
)
4452 if (o
->flags
.binary
) {
4454 if ((!fill_mmfile(o
->repo
, &mf
, one
) &&
4455 diff_filespec_is_binary(o
->repo
, one
)) ||
4456 (!fill_mmfile(o
->repo
, &mf
, two
) &&
4457 diff_filespec_is_binary(o
->repo
, two
)))
4460 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
4461 diff_abbrev_oid(&one
->oid
, abbrev
),
4462 diff_abbrev_oid(&two
->oid
, abbrev
));
4463 if (one
->mode
== two
->mode
)
4464 strbuf_addf(msg
, " %06o", one
->mode
);
4465 strbuf_addf(msg
, "%s\n", reset
);
4469 static void run_diff_cmd(const char *pgm
,
4472 const char *attr_path
,
4473 struct diff_filespec
*one
,
4474 struct diff_filespec
*two
,
4476 struct diff_options
*o
,
4477 struct diff_filepair
*p
)
4479 const char *xfrm_msg
= NULL
;
4480 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
4481 int must_show_header
= 0;
4482 struct userdiff_driver
*drv
= NULL
;
4484 if (o
->flags
.allow_external
|| !o
->ignore_driver_algorithm
)
4485 drv
= userdiff_find_by_path(o
->repo
->index
, attr_path
);
4487 if (o
->flags
.allow_external
&& drv
&& drv
->external
)
4488 pgm
= drv
->external
;
4492 * don't use colors when the header is intended for an
4493 * external diff driver
4495 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
4497 want_color(o
->use_color
) && !pgm
);
4498 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
4502 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
, o
);
4506 if (!o
->ignore_driver_algorithm
&& drv
&& drv
->algorithm
)
4507 set_diff_algorithm(o
, drv
->algorithm
);
4509 builtin_diff(name
, other
? other
: name
,
4510 one
, two
, xfrm_msg
, must_show_header
,
4511 o
, complete_rewrite
);
4513 fprintf(o
->file
, "* Unmerged path %s\n", name
);
4517 static void diff_fill_oid_info(struct diff_filespec
*one
, struct index_state
*istate
)
4519 if (DIFF_FILE_VALID(one
)) {
4520 if (!one
->oid_valid
) {
4522 if (one
->is_stdin
) {
4526 if (lstat(one
->path
, &st
) < 0)
4527 die_errno("stat '%s'", one
->path
);
4528 if (index_path(istate
, &one
->oid
, one
->path
, &st
, 0))
4529 die("cannot hash %s", one
->path
);
4536 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
4538 /* Strip the prefix but do not molest /dev/null and absolute paths */
4539 if (*namep
&& !is_absolute_path(*namep
)) {
4540 *namep
+= prefix_length
;
4544 if (*otherp
&& !is_absolute_path(*otherp
)) {
4545 *otherp
+= prefix_length
;
4546 if (**otherp
== '/')
4551 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
4553 const char *pgm
= external_diff();
4555 struct diff_filespec
*one
= p
->one
;
4556 struct diff_filespec
*two
= p
->two
;
4559 const char *attr_path
;
4562 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
4564 if (o
->prefix_length
)
4565 strip_prefix(o
->prefix_length
, &name
, &other
);
4567 if (!o
->flags
.allow_external
)
4570 if (DIFF_PAIR_UNMERGED(p
)) {
4571 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
4572 NULL
, NULL
, NULL
, o
, p
);
4576 diff_fill_oid_info(one
, o
->repo
->index
);
4577 diff_fill_oid_info(two
, o
->repo
->index
);
4580 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4581 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4583 * a filepair that changes between file and symlink
4584 * needs to be split into deletion and creation.
4586 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4587 run_diff_cmd(NULL
, name
, other
, attr_path
,
4591 strbuf_release(&msg
);
4593 null
= alloc_filespec(one
->path
);
4594 run_diff_cmd(NULL
, name
, other
, attr_path
,
4595 null
, two
, &msg
, o
, p
);
4599 run_diff_cmd(pgm
, name
, other
, attr_path
,
4600 one
, two
, &msg
, o
, p
);
4602 strbuf_release(&msg
);
4605 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4606 struct diffstat_t
*diffstat
)
4611 if (!o
->ignore_driver_algorithm
) {
4612 struct userdiff_driver
*drv
= userdiff_find_by_path(o
->repo
->index
,
4615 if (drv
&& drv
->algorithm
)
4616 set_diff_algorithm(o
, drv
->algorithm
);
4619 if (DIFF_PAIR_UNMERGED(p
)) {
4621 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
,
4626 name
= p
->one
->path
;
4627 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4629 if (o
->prefix_length
)
4630 strip_prefix(o
->prefix_length
, &name
, &other
);
4632 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4633 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4635 builtin_diffstat(name
, other
, p
->one
, p
->two
,
4639 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4643 const char *attr_path
;
4645 if (DIFF_PAIR_UNMERGED(p
)) {
4650 name
= p
->one
->path
;
4651 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4652 attr_path
= other
? other
: name
;
4654 if (o
->prefix_length
)
4655 strip_prefix(o
->prefix_length
, &name
, &other
);
4657 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4658 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4660 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4663 void repo_diff_setup(struct repository
*r
, struct diff_options
*options
)
4665 memcpy(options
, &default_diff_options
, sizeof(*options
));
4667 options
->file
= stdout
;
4670 options
->output_indicators
[OUTPUT_INDICATOR_NEW
] = '+';
4671 options
->output_indicators
[OUTPUT_INDICATOR_OLD
] = '-';
4672 options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
] = ' ';
4673 options
->abbrev
= DEFAULT_ABBREV
;
4674 options
->line_termination
= '\n';
4675 options
->break_opt
= -1;
4676 options
->rename_limit
= -1;
4677 options
->dirstat_permille
= diff_dirstat_permille_default
;
4678 options
->context
= diff_context_default
;
4679 options
->interhunkcontext
= diff_interhunk_context_default
;
4680 options
->ws_error_highlight
= ws_error_highlight_default
;
4681 options
->flags
.rename_empty
= 1;
4682 options
->flags
.relative_name
= diff_relative
;
4683 options
->objfind
= NULL
;
4685 /* pathchange left =NULL by default */
4686 options
->change
= diff_change
;
4687 options
->add_remove
= diff_addremove
;
4688 options
->use_color
= diff_use_color_default
;
4689 options
->detect_rename
= diff_detect_rename_default
;
4690 options
->xdl_opts
|= diff_algorithm
;
4691 if (diff_indent_heuristic
)
4692 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4694 options
->orderfile
= diff_order_file_cfg
;
4696 if (!options
->flags
.ignore_submodule_set
)
4697 options
->flags
.ignore_untracked_in_submodules
= 1;
4699 if (diff_no_prefix
) {
4700 diff_set_noprefix(options
);
4701 } else if (!diff_mnemonic_prefix
) {
4702 diff_set_default_prefix(options
);
4705 options
->color_moved
= diff_color_moved_default
;
4706 options
->color_moved_ws_handling
= diff_color_moved_ws_default
;
4709 static const char diff_status_letters
[] = {
4712 DIFF_STATUS_DELETED
,
4713 DIFF_STATUS_MODIFIED
,
4714 DIFF_STATUS_RENAMED
,
4715 DIFF_STATUS_TYPE_CHANGED
,
4716 DIFF_STATUS_UNKNOWN
,
4717 DIFF_STATUS_UNMERGED
,
4718 DIFF_STATUS_FILTER_AON
,
4719 DIFF_STATUS_FILTER_BROKEN
,
4723 static unsigned int filter_bit
['Z' + 1];
4725 static void prepare_filter_bits(void)
4729 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4730 for (i
= 0; diff_status_letters
[i
]; i
++)
4731 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4735 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4737 return opt
->filter
& filter_bit
[(int) status
];
4740 unsigned diff_filter_bit(char status
)
4742 prepare_filter_bits();
4743 return filter_bit
[(int) status
];
4746 void diff_setup_done(struct diff_options
*options
)
4748 unsigned check_mask
= DIFF_FORMAT_NAME
|
4749 DIFF_FORMAT_NAME_STATUS
|
4750 DIFF_FORMAT_CHECKDIFF
|
4751 DIFF_FORMAT_NO_OUTPUT
;
4753 * This must be signed because we're comparing against a potentially
4756 const int hexsz
= the_hash_algo
->hexsz
;
4758 if (options
->set_default
)
4759 options
->set_default(options
);
4761 if (HAS_MULTI_BITS(options
->output_format
& check_mask
))
4762 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
4763 "--name-only", "--name-status", "--check", "-s");
4765 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
4766 die(_("options '%s', '%s', and '%s' cannot be used together"),
4767 "-G", "-S", "--find-object");
4769 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_G_REGEX_MASK
))
4770 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"),
4771 "-G", "--pickaxe-regex", "--pickaxe-regex", "-S");
4773 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK
))
4774 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"),
4775 "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S");
4778 * Most of the time we can say "there are changes"
4779 * only by checking if there are changed paths, but
4780 * --ignore-whitespace* options force us to look
4784 if ((options
->xdl_opts
& XDF_WHITESPACE_FLAGS
) ||
4785 options
->ignore_regex_nr
)
4786 options
->flags
.diff_from_contents
= 1;
4788 options
->flags
.diff_from_contents
= 0;
4790 if (options
->flags
.find_copies_harder
)
4791 options
->detect_rename
= DIFF_DETECT_COPY
;
4793 if (!options
->flags
.relative_name
)
4794 options
->prefix
= NULL
;
4795 if (options
->prefix
)
4796 options
->prefix_length
= strlen(options
->prefix
);
4798 options
->prefix_length
= 0;
4800 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4801 DIFF_FORMAT_NAME_STATUS
|
4802 DIFF_FORMAT_CHECKDIFF
|
4803 DIFF_FORMAT_NO_OUTPUT
))
4804 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4805 DIFF_FORMAT_NUMSTAT
|
4806 DIFF_FORMAT_DIFFSTAT
|
4807 DIFF_FORMAT_SHORTSTAT
|
4808 DIFF_FORMAT_DIRSTAT
|
4809 DIFF_FORMAT_SUMMARY
|
4813 * These cases always need recursive; we do not drop caller-supplied
4814 * recursive bits for other formats here.
4816 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4817 DIFF_FORMAT_NUMSTAT
|
4818 DIFF_FORMAT_DIFFSTAT
|
4819 DIFF_FORMAT_SHORTSTAT
|
4820 DIFF_FORMAT_DIRSTAT
|
4821 DIFF_FORMAT_SUMMARY
|
4822 DIFF_FORMAT_CHECKDIFF
))
4823 options
->flags
.recursive
= 1;
4825 * Also pickaxe would not work very well if you do not say recursive
4827 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
4828 options
->flags
.recursive
= 1;
4830 * When patches are generated, submodules diffed against the work tree
4831 * must be checked for dirtiness too so it can be shown in the output
4833 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4834 options
->flags
.dirty_submodules
= 1;
4836 if (options
->detect_rename
&& options
->rename_limit
< 0)
4837 options
->rename_limit
= diff_rename_limit_default
;
4838 if (hexsz
< options
->abbrev
)
4839 options
->abbrev
= hexsz
; /* full */
4842 * It does not make sense to show the first hit we happened
4843 * to have found. It does not make sense not to return with
4844 * exit code in such a case either.
4846 if (options
->flags
.quick
) {
4847 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4848 options
->flags
.exit_with_status
= 1;
4851 options
->diff_path_counter
= 0;
4853 if (options
->flags
.follow_renames
&& options
->pathspec
.nr
!= 1)
4854 die(_("--follow requires exactly one pathspec"));
4856 if (!options
->use_color
|| external_diff())
4857 options
->color_moved
= 0;
4859 if (options
->filter_not
) {
4860 if (!options
->filter
)
4861 options
->filter
= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4862 options
->filter
&= ~options
->filter_not
;
4866 int parse_long_opt(const char *opt
, const char **argv
,
4867 const char **optarg
)
4869 const char *arg
= argv
[0];
4870 if (!skip_prefix(arg
, "--", &arg
))
4872 if (!skip_prefix(arg
, opt
, &arg
))
4874 if (*arg
== '=') { /* stuck form: --option=value */
4880 /* separate form: --option value */
4882 die("Option '--%s' requires a value", opt
);
4887 static int diff_opt_stat(const struct option
*opt
, const char *value
, int unset
)
4889 struct diff_options
*options
= opt
->value
;
4890 int width
= options
->stat_width
;
4891 int name_width
= options
->stat_name_width
;
4892 int graph_width
= options
->stat_graph_width
;
4893 int count
= options
->stat_count
;
4896 BUG_ON_OPT_NEG(unset
);
4898 if (!strcmp(opt
->long_name
, "stat")) {
4900 width
= strtoul(value
, &end
, 10);
4902 name_width
= strtoul(end
+1, &end
, 10);
4904 count
= strtoul(end
+1, &end
, 10);
4906 return error(_("invalid --stat value: %s"), value
);
4908 } else if (!strcmp(opt
->long_name
, "stat-width")) {
4909 width
= strtoul(value
, &end
, 10);
4911 return error(_("%s expects a numerical value"),
4913 } else if (!strcmp(opt
->long_name
, "stat-name-width")) {
4914 name_width
= strtoul(value
, &end
, 10);
4916 return error(_("%s expects a numerical value"),
4918 } else if (!strcmp(opt
->long_name
, "stat-graph-width")) {
4919 graph_width
= strtoul(value
, &end
, 10);
4921 return error(_("%s expects a numerical value"),
4923 } else if (!strcmp(opt
->long_name
, "stat-count")) {
4924 count
= strtoul(value
, &end
, 10);
4926 return error(_("%s expects a numerical value"),
4929 BUG("%s should not get here", opt
->long_name
);
4931 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4932 options
->stat_name_width
= name_width
;
4933 options
->stat_graph_width
= graph_width
;
4934 options
->stat_width
= width
;
4935 options
->stat_count
= count
;
4939 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
4941 struct strbuf errmsg
= STRBUF_INIT
;
4942 if (parse_dirstat_params(options
, params
, &errmsg
))
4943 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
4945 strbuf_release(&errmsg
);
4947 * The caller knows a dirstat-related option is given from the command
4948 * line; allow it to say "return this_function();"
4950 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
4954 static int diff_opt_diff_filter(const struct option
*option
,
4955 const char *optarg
, int unset
)
4957 struct diff_options
*opt
= option
->value
;
4960 BUG_ON_OPT_NEG(unset
);
4961 prepare_filter_bits();
4963 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4967 if ('a' <= optch
&& optch
<= 'z') {
4969 optch
= toupper(optch
);
4974 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
4976 return error(_("unknown change class '%c' in --diff-filter=%s"),
4979 opt
->filter_not
|= bit
;
4986 static void enable_patch_output(int *fmt
)
4988 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
4989 *fmt
|= DIFF_FORMAT_PATCH
;
4992 static int diff_opt_ws_error_highlight(const struct option
*option
,
4993 const char *arg
, int unset
)
4995 struct diff_options
*opt
= option
->value
;
4996 int val
= parse_ws_error_highlight(arg
);
4998 BUG_ON_OPT_NEG(unset
);
5000 return error(_("unknown value after ws-error-highlight=%.*s"),
5002 opt
->ws_error_highlight
= val
;
5006 static int diff_opt_find_object(const struct option
*option
,
5007 const char *arg
, int unset
)
5009 struct diff_options
*opt
= option
->value
;
5010 struct object_id oid
;
5012 BUG_ON_OPT_NEG(unset
);
5013 if (repo_get_oid(the_repository
, arg
, &oid
))
5014 return error(_("unable to resolve '%s'"), arg
);
5017 CALLOC_ARRAY(opt
->objfind
, 1);
5019 opt
->pickaxe_opts
|= DIFF_PICKAXE_KIND_OBJFIND
;
5020 opt
->flags
.recursive
= 1;
5021 opt
->flags
.tree_in_recursive
= 1;
5022 oidset_insert(opt
->objfind
, &oid
);
5026 static int diff_opt_anchored(const struct option
*opt
,
5027 const char *arg
, int unset
)
5029 struct diff_options
*options
= opt
->value
;
5031 BUG_ON_OPT_NEG(unset
);
5032 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
5033 ALLOC_GROW(options
->anchors
, options
->anchors_nr
+ 1,
5034 options
->anchors_alloc
);
5035 options
->anchors
[options
->anchors_nr
++] = xstrdup(arg
);
5039 static int diff_opt_binary(const struct option
*opt
,
5040 const char *arg
, int unset
)
5042 struct diff_options
*options
= opt
->value
;
5044 BUG_ON_OPT_NEG(unset
);
5045 BUG_ON_OPT_ARG(arg
);
5046 enable_patch_output(&options
->output_format
);
5047 options
->flags
.binary
= 1;
5051 static int diff_opt_break_rewrites(const struct option
*opt
,
5052 const char *arg
, int unset
)
5054 int *break_opt
= opt
->value
;
5057 BUG_ON_OPT_NEG(unset
);
5060 opt1
= parse_rename_score(&arg
);
5063 else if (*arg
!= '/')
5064 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5067 opt2
= parse_rename_score(&arg
);
5070 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5071 *break_opt
= opt1
| (opt2
<< 16);
5075 static int diff_opt_char(const struct option
*opt
,
5076 const char *arg
, int unset
)
5078 char *value
= opt
->value
;
5080 BUG_ON_OPT_NEG(unset
);
5082 return error(_("%s expects a character, got '%s'"),
5083 opt
->long_name
, arg
);
5088 static int diff_opt_color_moved(const struct option
*opt
,
5089 const char *arg
, int unset
)
5091 struct diff_options
*options
= opt
->value
;
5094 options
->color_moved
= COLOR_MOVED_NO
;
5096 if (diff_color_moved_default
)
5097 options
->color_moved
= diff_color_moved_default
;
5098 if (options
->color_moved
== COLOR_MOVED_NO
)
5099 options
->color_moved
= COLOR_MOVED_DEFAULT
;
5101 int cm
= parse_color_moved(arg
);
5103 return error(_("bad --color-moved argument: %s"), arg
);
5104 options
->color_moved
= cm
;
5109 static int diff_opt_color_moved_ws(const struct option
*opt
,
5110 const char *arg
, int unset
)
5112 struct diff_options
*options
= opt
->value
;
5116 options
->color_moved_ws_handling
= 0;
5120 cm
= parse_color_moved_ws(arg
);
5121 if (cm
& COLOR_MOVED_WS_ERROR
)
5122 return error(_("invalid mode '%s' in --color-moved-ws"), arg
);
5123 options
->color_moved_ws_handling
= cm
;
5127 static int diff_opt_color_words(const struct option
*opt
,
5128 const char *arg
, int unset
)
5130 struct diff_options
*options
= opt
->value
;
5132 BUG_ON_OPT_NEG(unset
);
5133 options
->use_color
= 1;
5134 options
->word_diff
= DIFF_WORDS_COLOR
;
5135 options
->word_regex
= arg
;
5139 static int diff_opt_compact_summary(const struct option
*opt
,
5140 const char *arg
, int unset
)
5142 struct diff_options
*options
= opt
->value
;
5144 BUG_ON_OPT_ARG(arg
);
5146 options
->flags
.stat_with_summary
= 0;
5148 options
->flags
.stat_with_summary
= 1;
5149 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
5154 static int diff_opt_diff_algorithm(const struct option
*opt
,
5155 const char *arg
, int unset
)
5157 struct diff_options
*options
= opt
->value
;
5159 BUG_ON_OPT_NEG(unset
);
5161 if (set_diff_algorithm(options
, arg
))
5162 return error(_("option diff-algorithm accepts \"myers\", "
5163 "\"minimal\", \"patience\" and \"histogram\""));
5165 options
->ignore_driver_algorithm
= 1;
5170 static int diff_opt_diff_algorithm_no_arg(const struct option
*opt
,
5171 const char *arg
, int unset
)
5173 struct diff_options
*options
= opt
->value
;
5175 BUG_ON_OPT_NEG(unset
);
5176 BUG_ON_OPT_ARG(arg
);
5178 if (set_diff_algorithm(options
, opt
->long_name
))
5179 BUG("available diff algorithms include \"myers\", "
5180 "\"minimal\", \"patience\" and \"histogram\"");
5182 options
->ignore_driver_algorithm
= 1;
5187 static int diff_opt_dirstat(const struct option
*opt
,
5188 const char *arg
, int unset
)
5190 struct diff_options
*options
= opt
->value
;
5192 BUG_ON_OPT_NEG(unset
);
5193 if (!strcmp(opt
->long_name
, "cumulative")) {
5195 BUG("how come --cumulative take a value?");
5197 } else if (!strcmp(opt
->long_name
, "dirstat-by-file"))
5198 parse_dirstat_opt(options
, "files");
5199 parse_dirstat_opt(options
, arg
? arg
: "");
5203 static int diff_opt_find_copies(const struct option
*opt
,
5204 const char *arg
, int unset
)
5206 struct diff_options
*options
= opt
->value
;
5208 BUG_ON_OPT_NEG(unset
);
5211 options
->rename_score
= parse_rename_score(&arg
);
5213 return error(_("invalid argument to %s"), opt
->long_name
);
5215 if (options
->detect_rename
== DIFF_DETECT_COPY
)
5216 options
->flags
.find_copies_harder
= 1;
5218 options
->detect_rename
= DIFF_DETECT_COPY
;
5223 static int diff_opt_find_renames(const struct option
*opt
,
5224 const char *arg
, int unset
)
5226 struct diff_options
*options
= opt
->value
;
5228 BUG_ON_OPT_NEG(unset
);
5231 options
->rename_score
= parse_rename_score(&arg
);
5233 return error(_("invalid argument to %s"), opt
->long_name
);
5235 options
->detect_rename
= DIFF_DETECT_RENAME
;
5239 static int diff_opt_follow(const struct option
*opt
,
5240 const char *arg
, int unset
)
5242 struct diff_options
*options
= opt
->value
;
5244 BUG_ON_OPT_ARG(arg
);
5246 options
->flags
.follow_renames
= 0;
5247 options
->flags
.default_follow_renames
= 0;
5249 options
->flags
.follow_renames
= 1;
5254 static int diff_opt_ignore_submodules(const struct option
*opt
,
5255 const char *arg
, int unset
)
5257 struct diff_options
*options
= opt
->value
;
5259 BUG_ON_OPT_NEG(unset
);
5262 options
->flags
.override_submodule_config
= 1;
5263 handle_ignore_submodules_arg(options
, arg
);
5267 static int diff_opt_line_prefix(const struct option
*opt
,
5268 const char *optarg
, int unset
)
5270 struct diff_options
*options
= opt
->value
;
5272 BUG_ON_OPT_NEG(unset
);
5273 options
->line_prefix
= optarg
;
5274 options
->line_prefix_length
= strlen(options
->line_prefix
);
5275 graph_setup_line_prefix(options
);
5279 static int diff_opt_no_prefix(const struct option
*opt
,
5280 const char *optarg
, int unset
)
5282 struct diff_options
*options
= opt
->value
;
5284 BUG_ON_OPT_NEG(unset
);
5285 BUG_ON_OPT_ARG(optarg
);
5286 diff_set_noprefix(options
);
5290 static int diff_opt_default_prefix(const struct option
*opt
,
5291 const char *optarg
, int unset
)
5293 struct diff_options
*options
= opt
->value
;
5295 BUG_ON_OPT_NEG(unset
);
5296 BUG_ON_OPT_ARG(optarg
);
5297 diff_set_default_prefix(options
);
5301 static enum parse_opt_result
diff_opt_output(struct parse_opt_ctx_t
*ctx
,
5302 const struct option
*opt
,
5303 const char *arg
, int unset
)
5305 struct diff_options
*options
= opt
->value
;
5308 BUG_ON_OPT_NEG(unset
);
5309 path
= prefix_filename(ctx
->prefix
, arg
);
5310 options
->file
= xfopen(path
, "w");
5311 options
->close_file
= 1;
5312 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
5313 options
->use_color
= GIT_COLOR_NEVER
;
5318 static int diff_opt_patience(const struct option
*opt
,
5319 const char *arg
, int unset
)
5321 struct diff_options
*options
= opt
->value
;
5324 BUG_ON_OPT_NEG(unset
);
5325 BUG_ON_OPT_ARG(arg
);
5327 * Both --patience and --anchored use PATIENCE_DIFF
5328 * internally, so remove any anchors previously
5331 for (i
= 0; i
< options
->anchors_nr
; i
++)
5332 free(options
->anchors
[i
]);
5333 options
->anchors_nr
= 0;
5334 options
->ignore_driver_algorithm
= 1;
5336 return set_diff_algorithm(options
, "patience");
5339 static int diff_opt_ignore_regex(const struct option
*opt
,
5340 const char *arg
, int unset
)
5342 struct diff_options
*options
= opt
->value
;
5345 BUG_ON_OPT_NEG(unset
);
5346 regex
= xmalloc(sizeof(*regex
));
5347 if (regcomp(regex
, arg
, REG_EXTENDED
| REG_NEWLINE
))
5348 return error(_("invalid regex given to -I: '%s'"), arg
);
5349 ALLOC_GROW(options
->ignore_regex
, options
->ignore_regex_nr
+ 1,
5350 options
->ignore_regex_alloc
);
5351 options
->ignore_regex
[options
->ignore_regex_nr
++] = regex
;
5355 static int diff_opt_pickaxe_regex(const struct option
*opt
,
5356 const char *arg
, int unset
)
5358 struct diff_options
*options
= opt
->value
;
5360 BUG_ON_OPT_NEG(unset
);
5361 options
->pickaxe
= arg
;
5362 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
5366 static int diff_opt_pickaxe_string(const struct option
*opt
,
5367 const char *arg
, int unset
)
5369 struct diff_options
*options
= opt
->value
;
5371 BUG_ON_OPT_NEG(unset
);
5372 options
->pickaxe
= arg
;
5373 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
5377 static int diff_opt_relative(const struct option
*opt
,
5378 const char *arg
, int unset
)
5380 struct diff_options
*options
= opt
->value
;
5382 options
->flags
.relative_name
= !unset
;
5384 options
->prefix
= arg
;
5388 static int diff_opt_submodule(const struct option
*opt
,
5389 const char *arg
, int unset
)
5391 struct diff_options
*options
= opt
->value
;
5393 BUG_ON_OPT_NEG(unset
);
5396 if (parse_submodule_params(options
, arg
))
5397 return error(_("failed to parse --submodule option parameter: '%s'"),
5402 static int diff_opt_textconv(const struct option
*opt
,
5403 const char *arg
, int unset
)
5405 struct diff_options
*options
= opt
->value
;
5407 BUG_ON_OPT_ARG(arg
);
5409 options
->flags
.allow_textconv
= 0;
5411 options
->flags
.allow_textconv
= 1;
5412 options
->flags
.textconv_set_via_cmdline
= 1;
5417 static int diff_opt_unified(const struct option
*opt
,
5418 const char *arg
, int unset
)
5420 struct diff_options
*options
= opt
->value
;
5423 BUG_ON_OPT_NEG(unset
);
5426 options
->context
= strtol(arg
, &s
, 10);
5428 return error(_("%s expects a numerical value"), "--unified");
5430 enable_patch_output(&options
->output_format
);
5435 static int diff_opt_word_diff(const struct option
*opt
,
5436 const char *arg
, int unset
)
5438 struct diff_options
*options
= opt
->value
;
5440 BUG_ON_OPT_NEG(unset
);
5442 if (!strcmp(arg
, "plain"))
5443 options
->word_diff
= DIFF_WORDS_PLAIN
;
5444 else if (!strcmp(arg
, "color")) {
5445 options
->use_color
= 1;
5446 options
->word_diff
= DIFF_WORDS_COLOR
;
5448 else if (!strcmp(arg
, "porcelain"))
5449 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
5450 else if (!strcmp(arg
, "none"))
5451 options
->word_diff
= DIFF_WORDS_NONE
;
5453 return error(_("bad --word-diff argument: %s"), arg
);
5455 if (options
->word_diff
== DIFF_WORDS_NONE
)
5456 options
->word_diff
= DIFF_WORDS_PLAIN
;
5461 static int diff_opt_word_diff_regex(const struct option
*opt
,
5462 const char *arg
, int unset
)
5464 struct diff_options
*options
= opt
->value
;
5466 BUG_ON_OPT_NEG(unset
);
5467 if (options
->word_diff
== DIFF_WORDS_NONE
)
5468 options
->word_diff
= DIFF_WORDS_PLAIN
;
5469 options
->word_regex
= arg
;
5473 static int diff_opt_rotate_to(const struct option
*opt
, const char *arg
, int unset
)
5475 struct diff_options
*options
= opt
->value
;
5477 BUG_ON_OPT_NEG(unset
);
5478 if (!strcmp(opt
->long_name
, "skip-to"))
5479 options
->skip_instead_of_rotate
= 1;
5481 options
->skip_instead_of_rotate
= 0;
5482 options
->rotate_to
= arg
;
5486 struct option
*add_diff_options(const struct option
*opts
,
5487 struct diff_options
*options
)
5489 struct option parseopts
[] = {
5490 OPT_GROUP(N_("Diff output format options")),
5491 OPT_BITOP('p', "patch", &options
->output_format
,
5492 N_("generate patch"),
5493 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5494 OPT_BIT_F('s', "no-patch", &options
->output_format
,
5495 N_("suppress diff output"),
5496 DIFF_FORMAT_NO_OUTPUT
, PARSE_OPT_NONEG
),
5497 OPT_BITOP('u', NULL
, &options
->output_format
,
5498 N_("generate patch"),
5499 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5500 OPT_CALLBACK_F('U', "unified", options
, N_("<n>"),
5501 N_("generate diffs with <n> lines context"),
5502 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_unified
),
5503 OPT_BOOL('W', "function-context", &options
->flags
.funccontext
,
5504 N_("generate diffs with <n> lines context")),
5505 OPT_BIT_F(0, "raw", &options
->output_format
,
5506 N_("generate the diff in raw format"),
5507 DIFF_FORMAT_RAW
, PARSE_OPT_NONEG
),
5508 OPT_BITOP(0, "patch-with-raw", &options
->output_format
,
5509 N_("synonym for '-p --raw'"),
5510 DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
,
5511 DIFF_FORMAT_NO_OUTPUT
),
5512 OPT_BITOP(0, "patch-with-stat", &options
->output_format
,
5513 N_("synonym for '-p --stat'"),
5514 DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
,
5515 DIFF_FORMAT_NO_OUTPUT
),
5516 OPT_BIT_F(0, "numstat", &options
->output_format
,
5517 N_("machine friendly --stat"),
5518 DIFF_FORMAT_NUMSTAT
, PARSE_OPT_NONEG
),
5519 OPT_BIT_F(0, "shortstat", &options
->output_format
,
5520 N_("output only the last line of --stat"),
5521 DIFF_FORMAT_SHORTSTAT
, PARSE_OPT_NONEG
),
5522 OPT_CALLBACK_F('X', "dirstat", options
, N_("<param1,param2>..."),
5523 N_("output the distribution of relative amount of changes for each sub-directory"),
5524 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5526 OPT_CALLBACK_F(0, "cumulative", options
, NULL
,
5527 N_("synonym for --dirstat=cumulative"),
5528 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5530 OPT_CALLBACK_F(0, "dirstat-by-file", options
, N_("<param1,param2>..."),
5531 N_("synonym for --dirstat=files,param1,param2..."),
5532 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5534 OPT_BIT_F(0, "check", &options
->output_format
,
5535 N_("warn if changes introduce conflict markers or whitespace errors"),
5536 DIFF_FORMAT_CHECKDIFF
, PARSE_OPT_NONEG
),
5537 OPT_BIT_F(0, "summary", &options
->output_format
,
5538 N_("condensed summary such as creations, renames and mode changes"),
5539 DIFF_FORMAT_SUMMARY
, PARSE_OPT_NONEG
),
5540 OPT_BIT_F(0, "name-only", &options
->output_format
,
5541 N_("show only names of changed files"),
5542 DIFF_FORMAT_NAME
, PARSE_OPT_NONEG
),
5543 OPT_BIT_F(0, "name-status", &options
->output_format
,
5544 N_("show only names and status of changed files"),
5545 DIFF_FORMAT_NAME_STATUS
, PARSE_OPT_NONEG
),
5546 OPT_CALLBACK_F(0, "stat", options
, N_("<width>[,<name-width>[,<count>]]"),
5547 N_("generate diffstat"),
5548 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_stat
),
5549 OPT_CALLBACK_F(0, "stat-width", options
, N_("<width>"),
5550 N_("generate diffstat with a given width"),
5551 PARSE_OPT_NONEG
, diff_opt_stat
),
5552 OPT_CALLBACK_F(0, "stat-name-width", options
, N_("<width>"),
5553 N_("generate diffstat with a given name width"),
5554 PARSE_OPT_NONEG
, diff_opt_stat
),
5555 OPT_CALLBACK_F(0, "stat-graph-width", options
, N_("<width>"),
5556 N_("generate diffstat with a given graph width"),
5557 PARSE_OPT_NONEG
, diff_opt_stat
),
5558 OPT_CALLBACK_F(0, "stat-count", options
, N_("<count>"),
5559 N_("generate diffstat with limited lines"),
5560 PARSE_OPT_NONEG
, diff_opt_stat
),
5561 OPT_CALLBACK_F(0, "compact-summary", options
, NULL
,
5562 N_("generate compact summary in diffstat"),
5563 PARSE_OPT_NOARG
, diff_opt_compact_summary
),
5564 OPT_CALLBACK_F(0, "binary", options
, NULL
,
5565 N_("output a binary diff that can be applied"),
5566 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_binary
),
5567 OPT_BOOL(0, "full-index", &options
->flags
.full_index
,
5568 N_("show full pre- and post-image object names on the \"index\" lines")),
5569 OPT_COLOR_FLAG(0, "color", &options
->use_color
,
5570 N_("show colored diff")),
5571 OPT_CALLBACK_F(0, "ws-error-highlight", options
, N_("<kind>"),
5572 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5573 PARSE_OPT_NONEG
, diff_opt_ws_error_highlight
),
5574 OPT_SET_INT('z', NULL
, &options
->line_termination
,
5575 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5577 OPT__ABBREV(&options
->abbrev
),
5578 OPT_STRING_F(0, "src-prefix", &options
->a_prefix
, N_("<prefix>"),
5579 N_("show the given source prefix instead of \"a/\""),
5581 OPT_STRING_F(0, "dst-prefix", &options
->b_prefix
, N_("<prefix>"),
5582 N_("show the given destination prefix instead of \"b/\""),
5584 OPT_CALLBACK_F(0, "line-prefix", options
, N_("<prefix>"),
5585 N_("prepend an additional prefix to every line of output"),
5586 PARSE_OPT_NONEG
, diff_opt_line_prefix
),
5587 OPT_CALLBACK_F(0, "no-prefix", options
, NULL
,
5588 N_("do not show any source or destination prefix"),
5589 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_no_prefix
),
5590 OPT_CALLBACK_F(0, "default-prefix", options
, NULL
,
5591 N_("use default prefixes a/ and b/"),
5592 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_default_prefix
),
5593 OPT_INTEGER_F(0, "inter-hunk-context", &options
->interhunkcontext
,
5594 N_("show context between diff hunks up to the specified number of lines"),
5596 OPT_CALLBACK_F(0, "output-indicator-new",
5597 &options
->output_indicators
[OUTPUT_INDICATOR_NEW
],
5599 N_("specify the character to indicate a new line instead of '+'"),
5600 PARSE_OPT_NONEG
, diff_opt_char
),
5601 OPT_CALLBACK_F(0, "output-indicator-old",
5602 &options
->output_indicators
[OUTPUT_INDICATOR_OLD
],
5604 N_("specify the character to indicate an old line instead of '-'"),
5605 PARSE_OPT_NONEG
, diff_opt_char
),
5606 OPT_CALLBACK_F(0, "output-indicator-context",
5607 &options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
],
5609 N_("specify the character to indicate a context instead of ' '"),
5610 PARSE_OPT_NONEG
, diff_opt_char
),
5612 OPT_GROUP(N_("Diff rename options")),
5613 OPT_CALLBACK_F('B', "break-rewrites", &options
->break_opt
, N_("<n>[/<m>]"),
5614 N_("break complete rewrite changes into pairs of delete and create"),
5615 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5616 diff_opt_break_rewrites
),
5617 OPT_CALLBACK_F('M', "find-renames", options
, N_("<n>"),
5618 N_("detect renames"),
5619 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5620 diff_opt_find_renames
),
5621 OPT_SET_INT_F('D', "irreversible-delete", &options
->irreversible_delete
,
5622 N_("omit the preimage for deletes"),
5623 1, PARSE_OPT_NONEG
),
5624 OPT_CALLBACK_F('C', "find-copies", options
, N_("<n>"),
5625 N_("detect copies"),
5626 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5627 diff_opt_find_copies
),
5628 OPT_BOOL(0, "find-copies-harder", &options
->flags
.find_copies_harder
,
5629 N_("use unmodified files as source to find copies")),
5630 OPT_SET_INT_F(0, "no-renames", &options
->detect_rename
,
5631 N_("disable rename detection"),
5632 0, PARSE_OPT_NONEG
),
5633 OPT_BOOL(0, "rename-empty", &options
->flags
.rename_empty
,
5634 N_("use empty blobs as rename source")),
5635 OPT_CALLBACK_F(0, "follow", options
, NULL
,
5636 N_("continue listing the history of a file beyond renames"),
5637 PARSE_OPT_NOARG
, diff_opt_follow
),
5638 OPT_INTEGER('l', NULL
, &options
->rename_limit
,
5639 N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
5641 OPT_GROUP(N_("Diff algorithm options")),
5642 OPT_CALLBACK_F(0, "minimal", options
, NULL
,
5643 N_("produce the smallest possible diff"),
5644 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5645 diff_opt_diff_algorithm_no_arg
),
5646 OPT_BIT_F('w', "ignore-all-space", &options
->xdl_opts
,
5647 N_("ignore whitespace when comparing lines"),
5648 XDF_IGNORE_WHITESPACE
, PARSE_OPT_NONEG
),
5649 OPT_BIT_F('b', "ignore-space-change", &options
->xdl_opts
,
5650 N_("ignore changes in amount of whitespace"),
5651 XDF_IGNORE_WHITESPACE_CHANGE
, PARSE_OPT_NONEG
),
5652 OPT_BIT_F(0, "ignore-space-at-eol", &options
->xdl_opts
,
5653 N_("ignore changes in whitespace at EOL"),
5654 XDF_IGNORE_WHITESPACE_AT_EOL
, PARSE_OPT_NONEG
),
5655 OPT_BIT_F(0, "ignore-cr-at-eol", &options
->xdl_opts
,
5656 N_("ignore carrier-return at the end of line"),
5657 XDF_IGNORE_CR_AT_EOL
, PARSE_OPT_NONEG
),
5658 OPT_BIT_F(0, "ignore-blank-lines", &options
->xdl_opts
,
5659 N_("ignore changes whose lines are all blank"),
5660 XDF_IGNORE_BLANK_LINES
, PARSE_OPT_NONEG
),
5661 OPT_CALLBACK_F('I', "ignore-matching-lines", options
, N_("<regex>"),
5662 N_("ignore changes whose all lines match <regex>"),
5663 0, diff_opt_ignore_regex
),
5664 OPT_BIT(0, "indent-heuristic", &options
->xdl_opts
,
5665 N_("heuristic to shift diff hunk boundaries for easy reading"),
5666 XDF_INDENT_HEURISTIC
),
5667 OPT_CALLBACK_F(0, "patience", options
, NULL
,
5668 N_("generate diff using the \"patience diff\" algorithm"),
5669 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5671 OPT_CALLBACK_F(0, "histogram", options
, NULL
,
5672 N_("generate diff using the \"histogram diff\" algorithm"),
5673 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5674 diff_opt_diff_algorithm_no_arg
),
5675 OPT_CALLBACK_F(0, "diff-algorithm", options
, N_("<algorithm>"),
5676 N_("choose a diff algorithm"),
5677 PARSE_OPT_NONEG
, diff_opt_diff_algorithm
),
5678 OPT_CALLBACK_F(0, "anchored", options
, N_("<text>"),
5679 N_("generate diff using the \"anchored diff\" algorithm"),
5680 PARSE_OPT_NONEG
, diff_opt_anchored
),
5681 OPT_CALLBACK_F(0, "word-diff", options
, N_("<mode>"),
5682 N_("show word diff, using <mode> to delimit changed words"),
5683 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_word_diff
),
5684 OPT_CALLBACK_F(0, "word-diff-regex", options
, N_("<regex>"),
5685 N_("use <regex> to decide what a word is"),
5686 PARSE_OPT_NONEG
, diff_opt_word_diff_regex
),
5687 OPT_CALLBACK_F(0, "color-words", options
, N_("<regex>"),
5688 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5689 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_color_words
),
5690 OPT_CALLBACK_F(0, "color-moved", options
, N_("<mode>"),
5691 N_("moved lines of code are colored differently"),
5692 PARSE_OPT_OPTARG
, diff_opt_color_moved
),
5693 OPT_CALLBACK_F(0, "color-moved-ws", options
, N_("<mode>"),
5694 N_("how white spaces are ignored in --color-moved"),
5695 0, diff_opt_color_moved_ws
),
5697 OPT_GROUP(N_("Other diff options")),
5698 OPT_CALLBACK_F(0, "relative", options
, N_("<prefix>"),
5699 N_("when run from subdir, exclude changes outside and show relative paths"),
5702 OPT_BOOL('a', "text", &options
->flags
.text
,
5703 N_("treat all files as text")),
5704 OPT_BOOL('R', NULL
, &options
->flags
.reverse_diff
,
5705 N_("swap two inputs, reverse the diff")),
5706 OPT_BOOL(0, "exit-code", &options
->flags
.exit_with_status
,
5707 N_("exit with 1 if there were differences, 0 otherwise")),
5708 OPT_BOOL(0, "quiet", &options
->flags
.quick
,
5709 N_("disable all output of the program")),
5710 OPT_BOOL(0, "ext-diff", &options
->flags
.allow_external
,
5711 N_("allow an external diff helper to be executed")),
5712 OPT_CALLBACK_F(0, "textconv", options
, NULL
,
5713 N_("run external text conversion filters when comparing binary files"),
5714 PARSE_OPT_NOARG
, diff_opt_textconv
),
5715 OPT_CALLBACK_F(0, "ignore-submodules", options
, N_("<when>"),
5716 N_("ignore changes to submodules in the diff generation"),
5717 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5718 diff_opt_ignore_submodules
),
5719 OPT_CALLBACK_F(0, "submodule", options
, N_("<format>"),
5720 N_("specify how differences in submodules are shown"),
5721 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5722 diff_opt_submodule
),
5723 OPT_SET_INT_F(0, "ita-invisible-in-index", &options
->ita_invisible_in_index
,
5724 N_("hide 'git add -N' entries from the index"),
5725 1, PARSE_OPT_NONEG
),
5726 OPT_SET_INT_F(0, "ita-visible-in-index", &options
->ita_invisible_in_index
,
5727 N_("treat 'git add -N' entries as real in the index"),
5728 0, PARSE_OPT_NONEG
),
5729 OPT_CALLBACK_F('S', NULL
, options
, N_("<string>"),
5730 N_("look for differences that change the number of occurrences of the specified string"),
5731 0, diff_opt_pickaxe_string
),
5732 OPT_CALLBACK_F('G', NULL
, options
, N_("<regex>"),
5733 N_("look for differences that change the number of occurrences of the specified regex"),
5734 0, diff_opt_pickaxe_regex
),
5735 OPT_BIT_F(0, "pickaxe-all", &options
->pickaxe_opts
,
5736 N_("show all changes in the changeset with -S or -G"),
5737 DIFF_PICKAXE_ALL
, PARSE_OPT_NONEG
),
5738 OPT_BIT_F(0, "pickaxe-regex", &options
->pickaxe_opts
,
5739 N_("treat <string> in -S as extended POSIX regular expression"),
5740 DIFF_PICKAXE_REGEX
, PARSE_OPT_NONEG
),
5741 OPT_FILENAME('O', NULL
, &options
->orderfile
,
5742 N_("control the order in which files appear in the output")),
5743 OPT_CALLBACK_F(0, "rotate-to", options
, N_("<path>"),
5744 N_("show the change in the specified path first"),
5745 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5746 OPT_CALLBACK_F(0, "skip-to", options
, N_("<path>"),
5747 N_("skip the output to the specified path"),
5748 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5749 OPT_CALLBACK_F(0, "find-object", options
, N_("<object-id>"),
5750 N_("look for differences that change the number of occurrences of the specified object"),
5751 PARSE_OPT_NONEG
, diff_opt_find_object
),
5752 OPT_CALLBACK_F(0, "diff-filter", options
, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5753 N_("select files by diff type"),
5754 PARSE_OPT_NONEG
, diff_opt_diff_filter
),
5755 { OPTION_CALLBACK
, 0, "output", options
, N_("<file>"),
5756 N_("output to a specific file"),
5757 PARSE_OPT_NONEG
, NULL
, 0, diff_opt_output
},
5762 return parse_options_concat(opts
, parseopts
);
5765 int diff_opt_parse(struct diff_options
*options
,
5766 const char **av
, int ac
, const char *prefix
)
5768 struct option no_options
[] = { OPT_END() };
5769 struct option
*parseopts
= add_diff_options(no_options
, options
);
5774 ac
= parse_options(ac
, av
, prefix
, parseopts
, NULL
,
5775 PARSE_OPT_KEEP_DASHDASH
|
5776 PARSE_OPT_KEEP_UNKNOWN_OPT
|
5777 PARSE_OPT_NO_INTERNAL_HELP
|
5778 PARSE_OPT_ONE_SHOT
|
5779 PARSE_OPT_STOP_AT_NON_OPTION
);
5785 int parse_rename_score(const char **cp_p
)
5787 unsigned long num
, scale
;
5789 const char *cp
= *cp_p
;
5796 if ( !dot
&& ch
== '.' ) {
5799 } else if ( ch
== '%' ) {
5800 scale
= dot
? scale
*100 : 100;
5801 cp
++; /* % is always at the end */
5803 } else if ( ch
>= '0' && ch
<= '9' ) {
5804 if ( scale
< 100000 ) {
5806 num
= (num
*10) + (ch
-'0');
5815 /* user says num divided by scale and we say internally that
5816 * is MAX_SCORE * num / scale.
5818 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
5821 struct diff_queue_struct diff_queued_diff
;
5823 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
5825 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
5826 queue
->queue
[queue
->nr
++] = dp
;
5829 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
5830 struct diff_filespec
*one
,
5831 struct diff_filespec
*two
)
5833 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
5841 void diff_free_filepair(struct diff_filepair
*p
)
5843 free_filespec(p
->one
);
5844 free_filespec(p
->two
);
5848 void diff_free_queue(struct diff_queue_struct
*q
)
5850 for (int i
= 0; i
< q
->nr
; i
++)
5851 diff_free_filepair(q
->queue
[i
]);
5855 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
5860 /* Do we want all 40 hex characters? */
5861 if (len
== the_hash_algo
->hexsz
)
5862 return oid_to_hex(oid
);
5864 /* An abbreviated value is fine, possibly followed by an ellipsis. */
5865 abbrev
= diff_abbrev_oid(oid
, len
);
5867 if (!print_sha1_ellipsis())
5870 abblen
= strlen(abbrev
);
5873 * In well-behaved cases, where the abbreviated result is the
5874 * same as the requested length, append three dots after the
5875 * abbreviation (hence the whole logic is limited to the case
5876 * where abblen < 37); when the actual abbreviated result is a
5877 * bit longer than the requested length, we reduce the number
5878 * of dots so that they match the well-behaved ones. However,
5879 * if the actual abbreviation is longer than the requested
5880 * length by more than three, we give up on aligning, and add
5881 * three dots anyway, to indicate that the output is not the
5882 * full object name. Yes, this may be suboptimal, but this
5883 * appears only in "diff --raw --abbrev" output and it is not
5884 * worth the effort to change it now. Note that this would
5885 * likely to work fine when the automatic sizing of default
5886 * abbreviation length is used--we would be fed -1 in "len" in
5887 * that case, and will end up always appending three-dots, but
5888 * the automatic sizing is supposed to give abblen that ensures
5889 * uniqueness across all objects (statistically speaking).
5891 if (abblen
< the_hash_algo
->hexsz
- 3) {
5892 static char hex
[GIT_MAX_HEXSZ
+ 1];
5893 if (len
< abblen
&& abblen
<= len
+ 2)
5894 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
5896 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
5900 return oid_to_hex(oid
);
5903 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
5905 int line_termination
= opt
->line_termination
;
5906 int inter_name_termination
= line_termination
? '\t' : '\0';
5908 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5909 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
5910 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
5911 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
5912 fprintf(opt
->file
, "%s ",
5913 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
5916 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
5917 inter_name_termination
);
5919 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
5922 if (p
->status
== DIFF_STATUS_COPIED
||
5923 p
->status
== DIFF_STATUS_RENAMED
) {
5924 const char *name_a
, *name_b
;
5925 name_a
= p
->one
->path
;
5926 name_b
= p
->two
->path
;
5927 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5928 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
5929 write_name_quoted(name_b
, opt
->file
, line_termination
);
5931 const char *name_a
, *name_b
;
5932 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
5934 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5935 write_name_quoted(name_a
, opt
->file
, line_termination
);
5939 int diff_unmodified_pair(struct diff_filepair
*p
)
5941 /* This function is written stricter than necessary to support
5942 * the currently implemented transformers, but the idea is to
5943 * let transformers to produce diff_filepairs any way they want,
5944 * and filter and clean them up here before producing the output.
5946 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
5948 if (DIFF_PAIR_UNMERGED(p
))
5949 return 0; /* unmerged is interesting */
5951 /* deletion, addition, mode or type change
5952 * and rename are all interesting.
5954 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
5955 DIFF_PAIR_MODE_CHANGED(p
) ||
5956 strcmp(one
->path
, two
->path
))
5959 /* both are valid and point at the same path. that is, we are
5960 * dealing with a change.
5962 if (one
->oid_valid
&& two
->oid_valid
&&
5963 oideq(&one
->oid
, &two
->oid
) &&
5964 !one
->dirty_submodule
&& !two
->dirty_submodule
)
5965 return 1; /* no change */
5966 if (!one
->oid_valid
&& !two
->oid_valid
)
5967 return 1; /* both look at the same file on the filesystem. */
5971 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
5973 int include_conflict_headers
=
5974 (additional_headers(o
, p
->one
->path
) &&
5976 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
5979 * Check if we can return early without showing a diff. Note that
5980 * diff_filepair only stores {oid, path, mode, is_valid}
5981 * information for each path, and thus diff_unmodified_pair() only
5982 * considers those bits of info. However, we do not want pairs
5983 * created by create_filepairs_for_header_only_notifications()
5984 * (which always look like unmodified pairs) to be ignored, so
5985 * return early if both p is unmodified AND we don't want to
5986 * include_conflict_headers.
5988 if (diff_unmodified_pair(p
) && !include_conflict_headers
)
5991 /* Actually, we can also return early to avoid showing tree diffs */
5992 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5993 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5999 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
6000 struct diffstat_t
*diffstat
)
6002 if (diff_unmodified_pair(p
))
6005 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6006 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6007 return; /* no useful stat for tree diffs */
6009 run_diffstat(p
, o
, diffstat
);
6012 static void diff_flush_checkdiff(struct diff_filepair
*p
,
6013 struct diff_options
*o
)
6015 if (diff_unmodified_pair(p
))
6018 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6019 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6020 return; /* nothing to check in tree diffs */
6022 run_checkdiff(p
, o
);
6025 int diff_queue_is_empty(struct diff_options
*o
)
6027 struct diff_queue_struct
*q
= &diff_queued_diff
;
6029 int include_conflict_headers
=
6030 (o
->additional_path_headers
&&
6031 strmap_get_size(o
->additional_path_headers
) &&
6033 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
6035 if (include_conflict_headers
)
6038 for (i
= 0; i
< q
->nr
; i
++)
6039 if (!diff_unmodified_pair(q
->queue
[i
]))
6045 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
6047 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
6050 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
6052 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
6053 fprintf(stderr
, "queue[%d] %s size %lu\n",
6058 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
6060 diff_debug_filespec(p
->one
, i
, "one");
6061 diff_debug_filespec(p
->two
, i
, "two");
6062 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
6063 p
->score
, p
->status
? p
->status
: '?',
6064 p
->one
->rename_used
, p
->broken_pair
);
6067 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
6071 fprintf(stderr
, "%s\n", msg
);
6072 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
6073 for (i
= 0; i
< q
->nr
; i
++) {
6074 struct diff_filepair
*p
= q
->queue
[i
];
6075 diff_debug_filepair(p
, i
);
6080 static void diff_resolve_rename_copy(void)
6083 struct diff_filepair
*p
;
6084 struct diff_queue_struct
*q
= &diff_queued_diff
;
6086 diff_debug_queue("resolve-rename-copy", q
);
6088 for (i
= 0; i
< q
->nr
; i
++) {
6090 p
->status
= 0; /* undecided */
6091 if (DIFF_PAIR_UNMERGED(p
))
6092 p
->status
= DIFF_STATUS_UNMERGED
;
6093 else if (!DIFF_FILE_VALID(p
->one
))
6094 p
->status
= DIFF_STATUS_ADDED
;
6095 else if (!DIFF_FILE_VALID(p
->two
))
6096 p
->status
= DIFF_STATUS_DELETED
;
6097 else if (DIFF_PAIR_TYPE_CHANGED(p
))
6098 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
6100 /* from this point on, we are dealing with a pair
6101 * whose both sides are valid and of the same type, i.e.
6102 * either in-place edit or rename/copy edit.
6104 else if (DIFF_PAIR_RENAME(p
)) {
6106 * A rename might have re-connected a broken
6107 * pair up, causing the pathnames to be the
6108 * same again. If so, that's not a rename at
6109 * all, just a modification..
6111 * Otherwise, see if this source was used for
6112 * multiple renames, in which case we decrement
6113 * the count, and call it a copy.
6115 if (!strcmp(p
->one
->path
, p
->two
->path
))
6116 p
->status
= DIFF_STATUS_MODIFIED
;
6117 else if (--p
->one
->rename_used
> 0)
6118 p
->status
= DIFF_STATUS_COPIED
;
6120 p
->status
= DIFF_STATUS_RENAMED
;
6122 else if (!oideq(&p
->one
->oid
, &p
->two
->oid
) ||
6123 p
->one
->mode
!= p
->two
->mode
||
6124 p
->one
->dirty_submodule
||
6125 p
->two
->dirty_submodule
||
6126 is_null_oid(&p
->one
->oid
))
6127 p
->status
= DIFF_STATUS_MODIFIED
;
6129 /* This is a "no-change" entry and should not
6130 * happen anymore, but prepare for broken callers.
6132 error("feeding unmodified %s to diffcore",
6134 p
->status
= DIFF_STATUS_UNKNOWN
;
6137 diff_debug_queue("resolve-rename-copy done", q
);
6140 static int check_pair_status(struct diff_filepair
*p
)
6142 switch (p
->status
) {
6143 case DIFF_STATUS_UNKNOWN
:
6146 die("internal error in diff-resolve-rename-copy");
6152 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
6154 int fmt
= opt
->output_format
;
6156 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
6157 diff_flush_checkdiff(p
, opt
);
6158 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
6159 diff_flush_raw(p
, opt
);
6160 else if (fmt
& DIFF_FORMAT_NAME
) {
6161 const char *name_a
, *name_b
;
6162 name_a
= p
->two
->path
;
6164 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6165 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
6166 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
6170 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
6172 struct strbuf sb
= STRBUF_INIT
;
6174 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
6176 strbuf_addf(&sb
, " %s ", newdelete
);
6178 quote_c_style(fs
->path
, &sb
, NULL
, 0);
6179 strbuf_addch(&sb
, '\n');
6180 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6182 strbuf_release(&sb
);
6185 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
6188 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
6189 struct strbuf sb
= STRBUF_INIT
;
6190 strbuf_addf(&sb
, " mode change %06o => %06o",
6191 p
->one
->mode
, p
->two
->mode
);
6193 strbuf_addch(&sb
, ' ');
6194 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6196 strbuf_addch(&sb
, '\n');
6197 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6199 strbuf_release(&sb
);
6203 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
6204 struct diff_filepair
*p
)
6206 struct strbuf sb
= STRBUF_INIT
;
6207 struct strbuf names
= STRBUF_INIT
;
6209 pprint_rename(&names
, p
->one
->path
, p
->two
->path
);
6210 strbuf_addf(&sb
, " %s %s (%d%%)\n",
6211 renamecopy
, names
.buf
, similarity_index(p
));
6212 strbuf_release(&names
);
6213 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6215 show_mode_change(opt
, p
, 0);
6216 strbuf_release(&sb
);
6219 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
6222 case DIFF_STATUS_DELETED
:
6223 show_file_mode_name(opt
, "delete", p
->one
);
6225 case DIFF_STATUS_ADDED
:
6226 show_file_mode_name(opt
, "create", p
->two
);
6228 case DIFF_STATUS_COPIED
:
6229 show_rename_copy(opt
, "copy", p
);
6231 case DIFF_STATUS_RENAMED
:
6232 show_rename_copy(opt
, "rename", p
);
6236 struct strbuf sb
= STRBUF_INIT
;
6237 strbuf_addstr(&sb
, " rewrite ");
6238 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6239 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
6240 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6242 strbuf_release(&sb
);
6244 show_mode_change(opt
, p
, !p
->score
);
6254 static int remove_space(char *line
, int len
)
6260 for (i
= 0; i
< len
; i
++)
6261 if (!isspace((c
= line
[i
])))
6267 void flush_one_hunk(struct object_id
*result
, git_hash_ctx
*ctx
)
6269 unsigned char hash
[GIT_MAX_RAWSZ
];
6270 unsigned short carry
= 0;
6273 the_hash_algo
->final_fn(hash
, ctx
);
6274 the_hash_algo
->init_fn(ctx
);
6275 /* 20-byte sum, with carry */
6276 for (i
= 0; i
< the_hash_algo
->rawsz
; ++i
) {
6277 carry
+= result
->hash
[i
] + hash
[i
];
6278 result
->hash
[i
] = carry
;
6283 static int patch_id_consume(void *priv
, char *line
, unsigned long len
)
6285 struct patch_id_t
*data
= priv
;
6288 if (len
> 12 && starts_with(line
, "\\ "))
6290 new_len
= remove_space(line
, len
);
6292 the_hash_algo
->update_fn(data
->ctx
, line
, new_len
);
6293 data
->patchlen
+= new_len
;
6297 static void patch_id_add_string(git_hash_ctx
*ctx
, const char *str
)
6299 the_hash_algo
->update_fn(ctx
, str
, strlen(str
));
6302 static void patch_id_add_mode(git_hash_ctx
*ctx
, unsigned mode
)
6304 /* large enough for 2^32 in octal */
6306 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
6307 the_hash_algo
->update_fn(ctx
, buf
, len
);
6310 /* returns 0 upon success, and writes result into oid */
6311 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
6313 struct diff_queue_struct
*q
= &diff_queued_diff
;
6316 struct patch_id_t data
;
6318 the_hash_algo
->init_fn(&ctx
);
6319 memset(&data
, 0, sizeof(struct patch_id_t
));
6323 for (i
= 0; i
< q
->nr
; i
++) {
6327 struct diff_filepair
*p
= q
->queue
[i
];
6330 memset(&xpp
, 0, sizeof(xpp
));
6331 memset(&xecfg
, 0, sizeof(xecfg
));
6333 return error("internal diff status error");
6334 if (p
->status
== DIFF_STATUS_UNKNOWN
)
6336 if (diff_unmodified_pair(p
))
6338 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6339 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6341 if (DIFF_PAIR_UNMERGED(p
))
6344 diff_fill_oid_info(p
->one
, options
->repo
->index
);
6345 diff_fill_oid_info(p
->two
, options
->repo
->index
);
6347 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
6348 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
6349 patch_id_add_string(&ctx
, "diff--git");
6350 patch_id_add_string(&ctx
, "a/");
6351 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6352 patch_id_add_string(&ctx
, "b/");
6353 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6355 if (p
->one
->mode
== 0) {
6356 patch_id_add_string(&ctx
, "newfilemode");
6357 patch_id_add_mode(&ctx
, p
->two
->mode
);
6358 } else if (p
->two
->mode
== 0) {
6359 patch_id_add_string(&ctx
, "deletedfilemode");
6360 patch_id_add_mode(&ctx
, p
->one
->mode
);
6361 } else if (p
->one
->mode
!= p
->two
->mode
) {
6362 patch_id_add_string(&ctx
, "oldmode");
6363 patch_id_add_mode(&ctx
, p
->one
->mode
);
6364 patch_id_add_string(&ctx
, "newmode");
6365 patch_id_add_mode(&ctx
, p
->two
->mode
);
6368 if (diff_header_only
) {
6369 /* don't do anything since we're only populating header info */
6370 } else if (diff_filespec_is_binary(options
->repo
, p
->one
) ||
6371 diff_filespec_is_binary(options
->repo
, p
->two
)) {
6372 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->one
->oid
),
6373 the_hash_algo
->hexsz
);
6374 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->two
->oid
),
6375 the_hash_algo
->hexsz
);
6377 if (p
->one
->mode
== 0) {
6378 patch_id_add_string(&ctx
, "---/dev/null");
6379 patch_id_add_string(&ctx
, "+++b/");
6380 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6381 } else if (p
->two
->mode
== 0) {
6382 patch_id_add_string(&ctx
, "---a/");
6383 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6384 patch_id_add_string(&ctx
, "+++/dev/null");
6386 patch_id_add_string(&ctx
, "---a/");
6387 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6388 patch_id_add_string(&ctx
, "+++b/");
6389 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6392 if (fill_mmfile(options
->repo
, &mf1
, p
->one
) < 0 ||
6393 fill_mmfile(options
->repo
, &mf2
, p
->two
) < 0)
6394 return error("unable to read files to diff");
6397 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
6398 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
6399 patch_id_consume
, &data
, &xpp
, &xecfg
))
6400 return error("unable to generate patch-id diff for %s",
6403 flush_one_hunk(oid
, &ctx
);
6409 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
6411 struct diff_queue_struct
*q
= &diff_queued_diff
;
6412 int result
= diff_get_patch_id(options
, oid
, diff_header_only
);
6415 DIFF_QUEUE_CLEAR(q
);
6420 static int is_summary_empty(const struct diff_queue_struct
*q
)
6424 for (i
= 0; i
< q
->nr
; i
++) {
6425 const struct diff_filepair
*p
= q
->queue
[i
];
6427 switch (p
->status
) {
6428 case DIFF_STATUS_DELETED
:
6429 case DIFF_STATUS_ADDED
:
6430 case DIFF_STATUS_COPIED
:
6431 case DIFF_STATUS_RENAMED
:
6436 if (p
->one
->mode
&& p
->two
->mode
&&
6437 p
->one
->mode
!= p
->two
->mode
)
6445 static const char rename_limit_warning
[] =
6446 N_("exhaustive rename detection was skipped due to too many files.");
6448 static const char degrade_cc_to_c_warning
[] =
6449 N_("only found copies from modified paths due to too many files.");
6451 static const char rename_limit_advice
[] =
6452 N_("you may want to set your %s variable to at least "
6453 "%d and retry the command.");
6455 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
6459 warning(_(degrade_cc_to_c_warning
));
6461 warning(_(rename_limit_warning
));
6465 warning(_(rename_limit_advice
), varname
, needed
);
6468 static void create_filepairs_for_header_only_notifications(struct diff_options
*o
)
6470 struct strset present
;
6471 struct diff_queue_struct
*q
= &diff_queued_diff
;
6472 struct hashmap_iter iter
;
6473 struct strmap_entry
*e
;
6476 strset_init_with_options(&present
, /*pool*/ NULL
, /*strdup*/ 0);
6479 * Find out which paths exist in diff_queued_diff, preferring
6480 * one->path for any pair that has multiple paths.
6482 for (i
= 0; i
< q
->nr
; i
++) {
6483 struct diff_filepair
*p
= q
->queue
[i
];
6484 char *path
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
6486 if (strmap_contains(o
->additional_path_headers
, path
))
6487 strset_add(&present
, path
);
6491 * Loop over paths in additional_path_headers; for each NOT already
6492 * in diff_queued_diff, create a synthetic filepair and insert that
6493 * into diff_queued_diff.
6495 strmap_for_each_entry(o
->additional_path_headers
, &iter
, e
) {
6496 if (!strset_contains(&present
, e
->key
)) {
6497 struct diff_filespec
*one
, *two
;
6498 struct diff_filepair
*p
;
6500 one
= alloc_filespec(e
->key
);
6501 two
= alloc_filespec(e
->key
);
6502 fill_filespec(one
, null_oid(), 0, 0);
6503 fill_filespec(two
, null_oid(), 0, 0);
6504 p
= diff_queue(q
, one
, two
);
6505 p
->status
= DIFF_STATUS_MODIFIED
;
6509 /* Re-sort the filepairs */
6510 diffcore_fix_diff_index();
6513 strset_clear(&present
);
6516 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
6519 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
6520 struct diff_queue_struct
*q
= &diff_queued_diff
;
6522 if (WSEH_NEW
& WS_RULE_MASK
)
6523 BUG("WS rules bit mask overlaps with diff symbol flags");
6526 o
->emitted_symbols
= &esm
;
6528 if (o
->additional_path_headers
)
6529 create_filepairs_for_header_only_notifications(o
);
6531 for (i
= 0; i
< q
->nr
; i
++) {
6532 struct diff_filepair
*p
= q
->queue
[i
];
6533 if (check_pair_status(p
))
6534 diff_flush_patch(p
, o
);
6537 if (o
->emitted_symbols
) {
6538 if (o
->color_moved
) {
6539 struct mem_pool entry_pool
;
6540 struct moved_entry_list
*entry_list
;
6542 mem_pool_init(&entry_pool
, 1024 * 1024);
6543 entry_list
= add_lines_to_move_detection(o
,
6545 mark_color_as_moved(o
, entry_list
);
6546 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
6549 mem_pool_discard(&entry_pool
, 0);
6553 for (i
= 0; i
< esm
.nr
; i
++)
6554 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
6556 for (i
= 0; i
< esm
.nr
; i
++)
6557 free((void *)esm
.buf
[i
].line
);
6560 o
->emitted_symbols
= NULL
;
6564 static void diff_free_file(struct diff_options
*options
)
6566 if (options
->close_file
)
6567 fclose(options
->file
);
6570 static void diff_free_ignore_regex(struct diff_options
*options
)
6574 for (i
= 0; i
< options
->ignore_regex_nr
; i
++) {
6575 regfree(options
->ignore_regex
[i
]);
6576 free(options
->ignore_regex
[i
]);
6578 free(options
->ignore_regex
);
6581 void diff_free(struct diff_options
*options
)
6583 if (options
->no_free
)
6586 diff_free_file(options
);
6587 diff_free_ignore_regex(options
);
6588 clear_pathspec(&options
->pathspec
);
6591 void diff_flush(struct diff_options
*options
)
6593 struct diff_queue_struct
*q
= &diff_queued_diff
;
6594 int i
, output_format
= options
->output_format
;
6596 int dirstat_by_line
= 0;
6599 * Order: raw, stat, summary, patch
6600 * or: name/name-status/checkdiff (other bits clear)
6602 if (!q
->nr
&& !options
->additional_path_headers
)
6605 if (output_format
& (DIFF_FORMAT_RAW
|
6607 DIFF_FORMAT_NAME_STATUS
|
6608 DIFF_FORMAT_CHECKDIFF
)) {
6609 for (i
= 0; i
< q
->nr
; i
++) {
6610 struct diff_filepair
*p
= q
->queue
[i
];
6611 if (check_pair_status(p
))
6612 flush_one_pair(p
, options
);
6617 if (output_format
& DIFF_FORMAT_DIRSTAT
&& options
->flags
.dirstat_by_line
)
6618 dirstat_by_line
= 1;
6620 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
6622 struct diffstat_t diffstat
;
6624 compute_diffstat(options
, &diffstat
, q
);
6625 if (output_format
& DIFF_FORMAT_NUMSTAT
)
6626 show_numstat(&diffstat
, options
);
6627 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
6628 show_stats(&diffstat
, options
);
6629 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
6630 show_shortstats(&diffstat
, options
);
6631 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
6632 show_dirstat_by_line(&diffstat
, options
);
6633 free_diffstat_info(&diffstat
);
6636 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
6637 show_dirstat(options
);
6639 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
6640 for (i
= 0; i
< q
->nr
; i
++) {
6641 diff_summary(options
, q
->queue
[i
]);
6646 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
6647 options
->flags
.exit_with_status
&&
6648 options
->flags
.diff_from_contents
) {
6650 * run diff_flush_patch for the exit status. setting
6651 * options->file to /dev/null should be safe, because we
6652 * aren't supposed to produce any output anyway.
6654 diff_free_file(options
);
6655 options
->file
= xfopen("/dev/null", "w");
6656 options
->close_file
= 1;
6657 options
->color_moved
= 0;
6658 for (i
= 0; i
< q
->nr
; i
++) {
6659 struct diff_filepair
*p
= q
->queue
[i
];
6660 if (check_pair_status(p
))
6661 diff_flush_patch(p
, options
);
6662 if (options
->found_changes
)
6667 if (output_format
& DIFF_FORMAT_PATCH
) {
6669 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
6670 if (options
->stat_sep
)
6671 /* attach patch instead of inline */
6672 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
6676 diff_flush_patch_all_file_pairs(options
);
6679 if (output_format
& DIFF_FORMAT_CALLBACK
)
6680 options
->format_callback(q
, options
, options
->format_callback_data
);
6684 DIFF_QUEUE_CLEAR(q
);
6688 * Report the content-level differences with HAS_CHANGES;
6689 * diff_addremove/diff_change does not set the bit when
6690 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6692 if (options
->flags
.diff_from_contents
) {
6693 if (options
->found_changes
)
6694 options
->flags
.has_changes
= 1;
6696 options
->flags
.has_changes
= 0;
6700 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
6702 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
6704 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
6706 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
6707 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
6708 filter_bit_tst(p
->status
, options
)));
6711 static void diffcore_apply_filter(struct diff_options
*options
)
6714 struct diff_queue_struct
*q
= &diff_queued_diff
;
6715 struct diff_queue_struct outq
;
6717 DIFF_QUEUE_CLEAR(&outq
);
6719 if (!options
->filter
)
6722 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
6724 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
6725 if (match_filter(options
, q
->queue
[i
]))
6731 /* otherwise we will clear the whole queue
6732 * by copying the empty outq at the end of this
6733 * function, but first clear the current entries
6736 for (i
= 0; i
< q
->nr
; i
++)
6737 diff_free_filepair(q
->queue
[i
]);
6740 /* Only the matching ones */
6741 for (i
= 0; i
< q
->nr
; i
++) {
6742 struct diff_filepair
*p
= q
->queue
[i
];
6743 if (match_filter(options
, p
))
6746 diff_free_filepair(p
);
6753 /* Check whether two filespecs with the same mode and size are identical */
6754 static int diff_filespec_is_identical(struct repository
*r
,
6755 struct diff_filespec
*one
,
6756 struct diff_filespec
*two
)
6758 if (S_ISGITLINK(one
->mode
))
6760 if (diff_populate_filespec(r
, one
, NULL
))
6762 if (diff_populate_filespec(r
, two
, NULL
))
6764 return !memcmp(one
->data
, two
->data
, one
->size
);
6767 static int diff_filespec_check_stat_unmatch(struct repository
*r
,
6768 struct diff_filepair
*p
)
6770 struct diff_populate_filespec_options dpf_options
= {
6771 .check_size_only
= 1,
6772 .missing_object_cb
= diff_queued_diff_prefetch
,
6773 .missing_object_data
= r
,
6776 if (p
->done_skip_stat_unmatch
)
6777 return p
->skip_stat_unmatch_result
;
6779 p
->done_skip_stat_unmatch
= 1;
6780 p
->skip_stat_unmatch_result
= 0;
6782 * 1. Entries that come from stat info dirtiness
6783 * always have both sides (iow, not create/delete),
6784 * one side of the object name is unknown, with
6785 * the same mode and size. Keep the ones that
6786 * do not match these criteria. They have real
6789 * 2. At this point, the file is known to be modified,
6790 * with the same mode and size, and the object
6791 * name of one side is unknown. Need to inspect
6792 * the identical contents.
6794 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
6795 !DIFF_FILE_VALID(p
->two
) ||
6796 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
6797 (p
->one
->mode
!= p
->two
->mode
) ||
6798 diff_populate_filespec(r
, p
->one
, &dpf_options
) ||
6799 diff_populate_filespec(r
, p
->two
, &dpf_options
) ||
6800 (p
->one
->size
!= p
->two
->size
) ||
6801 !diff_filespec_is_identical(r
, p
->one
, p
->two
)) /* (2) */
6802 p
->skip_stat_unmatch_result
= 1;
6803 return p
->skip_stat_unmatch_result
;
6806 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
6809 struct diff_queue_struct
*q
= &diff_queued_diff
;
6810 struct diff_queue_struct outq
;
6811 DIFF_QUEUE_CLEAR(&outq
);
6813 for (i
= 0; i
< q
->nr
; i
++) {
6814 struct diff_filepair
*p
= q
->queue
[i
];
6816 if (diff_filespec_check_stat_unmatch(diffopt
->repo
, p
))
6820 * The caller can subtract 1 from skip_stat_unmatch
6821 * to determine how many paths were dirty only
6822 * due to stat info mismatch.
6824 if (!diffopt
->flags
.no_index
)
6825 diffopt
->skip_stat_unmatch
++;
6826 diff_free_filepair(p
);
6833 static int diffnamecmp(const void *a_
, const void *b_
)
6835 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
6836 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
6837 const char *name_a
, *name_b
;
6839 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
6840 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
6841 return strcmp(name_a
, name_b
);
6844 void diffcore_fix_diff_index(void)
6846 struct diff_queue_struct
*q
= &diff_queued_diff
;
6847 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
6850 void diff_add_if_missing(struct repository
*r
,
6851 struct oid_array
*to_fetch
,
6852 const struct diff_filespec
*filespec
)
6854 if (filespec
&& filespec
->oid_valid
&&
6855 !S_ISGITLINK(filespec
->mode
) &&
6856 oid_object_info_extended(r
, &filespec
->oid
, NULL
,
6857 OBJECT_INFO_FOR_PREFETCH
))
6858 oid_array_append(to_fetch
, &filespec
->oid
);
6861 void diff_queued_diff_prefetch(void *repository
)
6863 struct repository
*repo
= repository
;
6865 struct diff_queue_struct
*q
= &diff_queued_diff
;
6866 struct oid_array to_fetch
= OID_ARRAY_INIT
;
6868 for (i
= 0; i
< q
->nr
; i
++) {
6869 struct diff_filepair
*p
= q
->queue
[i
];
6870 diff_add_if_missing(repo
, &to_fetch
, p
->one
);
6871 diff_add_if_missing(repo
, &to_fetch
, p
->two
);
6875 * NEEDSWORK: Consider deduplicating the OIDs sent.
6877 promisor_remote_get_direct(repo
, to_fetch
.oid
, to_fetch
.nr
);
6879 oid_array_clear(&to_fetch
);
6882 void diffcore_std(struct diff_options
*options
)
6884 int output_formats_to_prefetch
= DIFF_FORMAT_DIFFSTAT
|
6885 DIFF_FORMAT_NUMSTAT
|
6887 DIFF_FORMAT_SHORTSTAT
|
6888 DIFF_FORMAT_DIRSTAT
;
6891 * Check if the user requested a blob-data-requiring diff output and/or
6892 * break-rewrite detection (which requires blob data). If yes, prefetch
6895 * If no prefetching occurs, diffcore_rename() will prefetch if it
6896 * decides that it needs inexact rename detection.
6898 if (options
->repo
== the_repository
&& repo_has_promisor_remote(the_repository
) &&
6899 (options
->output_format
& output_formats_to_prefetch
||
6900 options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
6901 diff_queued_diff_prefetch(options
->repo
);
6903 /* NOTE please keep the following in sync with diff_tree_combined() */
6904 if (options
->skip_stat_unmatch
)
6905 diffcore_skip_stat_unmatch(options
);
6906 if (!options
->found_follow
) {
6907 /* See try_to_follow_renames() in tree-diff.c */
6908 if (options
->break_opt
!= -1)
6909 diffcore_break(options
->repo
,
6910 options
->break_opt
);
6911 if (options
->detect_rename
)
6912 diffcore_rename(options
);
6913 if (options
->break_opt
!= -1)
6914 diffcore_merge_broken();
6916 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
6917 diffcore_pickaxe(options
);
6918 if (options
->orderfile
)
6919 diffcore_order(options
->orderfile
);
6920 if (options
->rotate_to
)
6921 diffcore_rotate(options
);
6922 if (!options
->found_follow
)
6923 /* See try_to_follow_renames() in tree-diff.c */
6924 diff_resolve_rename_copy();
6925 diffcore_apply_filter(options
);
6927 if (diff_queued_diff
.nr
&& !options
->flags
.diff_from_contents
)
6928 options
->flags
.has_changes
= 1;
6930 options
->flags
.has_changes
= 0;
6932 options
->found_follow
= 0;
6935 int diff_result_code(struct diff_options
*opt
, int status
)
6939 diff_warn_rename_limit("diff.renameLimit",
6940 opt
->needed_rename_limit
,
6941 opt
->degraded_cc_to_c
);
6942 if (!opt
->flags
.exit_with_status
&&
6943 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
6945 if (opt
->flags
.exit_with_status
&&
6946 opt
->flags
.has_changes
)
6948 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
6949 opt
->flags
.check_failed
)
6954 int diff_can_quit_early(struct diff_options
*opt
)
6956 return (opt
->flags
.quick
&&
6958 opt
->flags
.has_changes
);
6962 * Shall changes to this submodule be ignored?
6964 * Submodule changes can be configured to be ignored separately for each path,
6965 * but that configuration can be overridden from the command line.
6967 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
6970 struct diff_flags orig_flags
= options
->flags
;
6971 if (!options
->flags
.override_submodule_config
)
6972 set_diffopt_flags_from_submodule_config(options
, path
);
6973 if (options
->flags
.ignore_submodules
)
6975 options
->flags
= orig_flags
;
6979 void compute_diffstat(struct diff_options
*options
,
6980 struct diffstat_t
*diffstat
,
6981 struct diff_queue_struct
*q
)
6985 memset(diffstat
, 0, sizeof(struct diffstat_t
));
6986 for (i
= 0; i
< q
->nr
; i
++) {
6987 struct diff_filepair
*p
= q
->queue
[i
];
6988 if (check_pair_status(p
))
6989 diff_flush_stat(p
, options
, diffstat
);
6993 void diff_addremove(struct diff_options
*options
,
6994 int addremove
, unsigned mode
,
6995 const struct object_id
*oid
,
6997 const char *concatpath
, unsigned dirty_submodule
)
6999 struct diff_filespec
*one
, *two
;
7001 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
7004 /* This may look odd, but it is a preparation for
7005 * feeding "there are unchanged files which should
7006 * not produce diffs, but when you are doing copy
7007 * detection you would need them, so here they are"
7008 * entries to the diff-core. They will be prefixed
7009 * with something like '=' or '*' (I haven't decided
7010 * which but should not make any difference).
7011 * Feeding the same new and old to diff_change()
7012 * also has the same effect.
7013 * Before the final output happens, they are pruned after
7014 * merged into rename/copy pairs as appropriate.
7016 if (options
->flags
.reverse_diff
)
7017 addremove
= (addremove
== '+' ? '-' :
7018 addremove
== '-' ? '+' : addremove
);
7020 if (options
->prefix
&&
7021 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
7024 one
= alloc_filespec(concatpath
);
7025 two
= alloc_filespec(concatpath
);
7027 if (addremove
!= '+')
7028 fill_filespec(one
, oid
, oid_valid
, mode
);
7029 if (addremove
!= '-') {
7030 fill_filespec(two
, oid
, oid_valid
, mode
);
7031 two
->dirty_submodule
= dirty_submodule
;
7034 diff_queue(&diff_queued_diff
, one
, two
);
7035 if (!options
->flags
.diff_from_contents
)
7036 options
->flags
.has_changes
= 1;
7039 void diff_change(struct diff_options
*options
,
7040 unsigned old_mode
, unsigned new_mode
,
7041 const struct object_id
*old_oid
,
7042 const struct object_id
*new_oid
,
7043 int old_oid_valid
, int new_oid_valid
,
7044 const char *concatpath
,
7045 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
7047 struct diff_filespec
*one
, *two
;
7048 struct diff_filepair
*p
;
7050 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
7051 is_submodule_ignored(concatpath
, options
))
7054 if (options
->flags
.reverse_diff
) {
7055 SWAP(old_mode
, new_mode
);
7056 SWAP(old_oid
, new_oid
);
7057 SWAP(old_oid_valid
, new_oid_valid
);
7058 SWAP(old_dirty_submodule
, new_dirty_submodule
);
7061 if (options
->prefix
&&
7062 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
7065 one
= alloc_filespec(concatpath
);
7066 two
= alloc_filespec(concatpath
);
7067 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
7068 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
7069 one
->dirty_submodule
= old_dirty_submodule
;
7070 two
->dirty_submodule
= new_dirty_submodule
;
7071 p
= diff_queue(&diff_queued_diff
, one
, two
);
7073 if (options
->flags
.diff_from_contents
)
7076 if (options
->flags
.quick
&& options
->skip_stat_unmatch
&&
7077 !diff_filespec_check_stat_unmatch(options
->repo
, p
)) {
7078 diff_free_filespec_data(p
->one
);
7079 diff_free_filespec_data(p
->two
);
7083 options
->flags
.has_changes
= 1;
7086 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
7088 struct diff_filepair
*pair
;
7089 struct diff_filespec
*one
, *two
;
7091 if (options
->prefix
&&
7092 strncmp(path
, options
->prefix
, options
->prefix_length
))
7095 one
= alloc_filespec(path
);
7096 two
= alloc_filespec(path
);
7097 pair
= diff_queue(&diff_queued_diff
, one
, two
);
7098 pair
->is_unmerged
= 1;
7102 static char *run_textconv(struct repository
*r
,
7104 struct diff_filespec
*spec
,
7107 struct diff_tempfile
*temp
;
7108 struct child_process child
= CHILD_PROCESS_INIT
;
7109 struct strbuf buf
= STRBUF_INIT
;
7112 temp
= prepare_temp_file(r
, spec
);
7113 strvec_push(&child
.args
, pgm
);
7114 strvec_push(&child
.args
, temp
->name
);
7116 child
.use_shell
= 1;
7118 if (start_command(&child
)) {
7123 if (strbuf_read(&buf
, child
.out
, 0) < 0)
7124 err
= error("error reading from textconv command '%s'", pgm
);
7127 if (finish_command(&child
) || err
) {
7128 strbuf_release(&buf
);
7134 return strbuf_detach(&buf
, outsize
);
7137 size_t fill_textconv(struct repository
*r
,
7138 struct userdiff_driver
*driver
,
7139 struct diff_filespec
*df
,
7145 if (!DIFF_FILE_VALID(df
)) {
7149 if (diff_populate_filespec(r
, df
, NULL
))
7150 die("unable to read files to diff");
7155 if (!driver
->textconv
)
7156 BUG("fill_textconv called with non-textconv driver");
7158 if (driver
->textconv_cache
&& df
->oid_valid
) {
7159 *outbuf
= notes_cache_get(driver
->textconv_cache
,
7166 *outbuf
= run_textconv(r
, driver
->textconv
, df
, &size
);
7168 die("unable to read files to diff");
7170 if (driver
->textconv_cache
&& df
->oid_valid
) {
7171 /* ignore errors, as we might be in a readonly repository */
7172 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
7175 * we could save up changes and flush them all at the end,
7176 * but we would need an extra call after all diffing is done.
7177 * Since generating a cache entry is the slow path anyway,
7178 * this extra overhead probably isn't a big deal.
7180 notes_cache_write(driver
->textconv_cache
);
7186 int textconv_object(struct repository
*r
,
7189 const struct object_id
*oid
,
7192 unsigned long *buf_size
)
7194 struct diff_filespec
*df
;
7195 struct userdiff_driver
*textconv
;
7197 df
= alloc_filespec(path
);
7198 fill_filespec(df
, oid
, oid_valid
, mode
);
7199 textconv
= get_textconv(r
, df
);
7205 *buf_size
= fill_textconv(r
, textconv
, df
, buf
);
7210 void setup_diff_pager(struct diff_options
*opt
)
7213 * If the user asked for our exit code, then either they want --quiet
7214 * or --exit-code. We should definitely not bother with a pager in the
7215 * former case, as we will generate no output. Since we still properly
7216 * report our exit code even when a pager is run, we _could_ run a
7217 * pager with --exit-code. But since we have not done so historically,
7218 * and because it is easy to find people oneline advising "git diff
7219 * --exit-code" in hooks and other scripts, we do not do so.
7221 if (!opt
->flags
.exit_with_status
&&
7222 check_pager_config("diff") != 0)