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 struct userdiff_driver
*get_textconv(struct repository
*r
,
3390 struct diff_filespec
*one
)
3392 if (!DIFF_FILE_VALID(one
))
3395 diff_filespec_load_driver(one
, r
->index
);
3396 return userdiff_get_textconv(r
, one
->driver
);
3399 static struct string_list
*additional_headers(struct diff_options
*o
,
3402 if (!o
->additional_path_headers
)
3404 return strmap_get(o
->additional_path_headers
, path
);
3407 static void add_formatted_header(struct strbuf
*msg
,
3409 const char *line_prefix
,
3413 const char *next
, *newline
;
3415 for (next
= header
; *next
; next
= newline
) {
3416 newline
= strchrnul(next
, '\n');
3417 strbuf_addf(msg
, "%s%s%.*s%s\n", line_prefix
, meta
,
3418 (int)(newline
- next
), next
, reset
);
3424 static void add_formatted_headers(struct strbuf
*msg
,
3425 struct string_list
*more_headers
,
3426 const char *line_prefix
,
3432 for (i
= 0; i
< more_headers
->nr
; i
++)
3433 add_formatted_header(msg
, more_headers
->items
[i
].string
,
3434 line_prefix
, meta
, reset
);
3437 static int diff_filepair_is_phoney(struct diff_filespec
*one
,
3438 struct diff_filespec
*two
)
3441 * This function specifically looks for pairs injected by
3442 * create_filepairs_for_header_only_notifications(). Such
3443 * pairs are "phoney" in that they do not represent any
3444 * content or even mode difference, but were inserted because
3445 * diff_queued_diff previously had no pair associated with
3446 * that path but we needed some pair to avoid losing the
3447 * "remerge CONFLICT" header associated with the path.
3449 return !DIFF_FILE_VALID(one
) && !DIFF_FILE_VALID(two
);
3452 static int set_diff_algorithm(struct diff_options
*opts
,
3455 long value
= parse_algorithm_value(alg
);
3460 /* clear out previous settings */
3461 DIFF_XDL_CLR(opts
, NEED_MINIMAL
);
3462 opts
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3463 opts
->xdl_opts
|= value
;
3468 static void builtin_diff(const char *name_a
,
3470 struct diff_filespec
*one
,
3471 struct diff_filespec
*two
,
3472 const char *xfrm_msg
,
3473 int must_show_header
,
3474 struct diff_options
*o
,
3475 int complete_rewrite
)
3479 char *a_one
, *b_two
;
3480 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
3481 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
3482 const char *a_prefix
, *b_prefix
;
3483 struct userdiff_driver
*textconv_one
= NULL
;
3484 struct userdiff_driver
*textconv_two
= NULL
;
3485 struct strbuf header
= STRBUF_INIT
;
3486 const char *line_prefix
= diff_line_prefix(o
);
3488 diff_set_mnemonic_prefix(o
, "a/", "b/");
3489 if (o
->flags
.reverse_diff
) {
3490 a_prefix
= o
->b_prefix
;
3491 b_prefix
= o
->a_prefix
;
3493 a_prefix
= o
->a_prefix
;
3494 b_prefix
= o
->b_prefix
;
3497 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
3498 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3499 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3500 (!diff_filepair_is_phoney(one
, two
))) {
3501 show_submodule_diff_summary(o
, one
->path
? one
->path
: two
->path
,
3502 &one
->oid
, &two
->oid
,
3503 two
->dirty_submodule
);
3505 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
3506 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3507 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3508 (!diff_filepair_is_phoney(one
, two
))) {
3509 show_submodule_inline_diff(o
, one
->path
? one
->path
: two
->path
,
3510 &one
->oid
, &two
->oid
,
3511 two
->dirty_submodule
);
3515 if (o
->flags
.allow_textconv
) {
3516 textconv_one
= get_textconv(o
->repo
, one
);
3517 textconv_two
= get_textconv(o
->repo
, two
);
3520 /* Never use a non-valid filename anywhere if at all possible */
3521 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
3522 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
3524 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
3525 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
3526 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
3527 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
3528 if (diff_filepair_is_phoney(one
, two
)) {
3530 * We should only reach this point for pairs generated from
3531 * create_filepairs_for_header_only_notifications(). For
3532 * these, we want to avoid the "/dev/null" special casing
3533 * above, because we do not want such pairs shown as either
3534 * "new file" or "deleted file" below.
3539 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3540 if (lbl
[0][0] == '/') {
3542 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3544 strbuf_addstr(&header
, xfrm_msg
);
3545 must_show_header
= 1;
3547 else if (lbl
[1][0] == '/') {
3548 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3550 strbuf_addstr(&header
, xfrm_msg
);
3551 must_show_header
= 1;
3554 if (one
->mode
!= two
->mode
) {
3555 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3556 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3557 must_show_header
= 1;
3560 strbuf_addstr(&header
, xfrm_msg
);
3563 * we do not run diff between different kind
3566 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3567 goto free_ab_and_return
;
3568 if (complete_rewrite
&&
3569 (textconv_one
|| !diff_filespec_is_binary(o
->repo
, one
)) &&
3570 (textconv_two
|| !diff_filespec_is_binary(o
->repo
, two
))) {
3571 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3572 header
.buf
, header
.len
, 0);
3573 strbuf_reset(&header
);
3574 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3575 textconv_one
, textconv_two
, o
);
3576 o
->found_changes
= 1;
3577 goto free_ab_and_return
;
3581 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3582 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3584 strbuf_reset(&header
);
3585 goto free_ab_and_return
;
3586 } else if (!o
->flags
.text
&&
3587 ( (!textconv_one
&& diff_filespec_is_binary(o
->repo
, one
)) ||
3588 (!textconv_two
&& diff_filespec_is_binary(o
->repo
, two
)) )) {
3589 struct strbuf sb
= STRBUF_INIT
;
3590 if (!one
->data
&& !two
->data
&&
3591 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3593 if (oideq(&one
->oid
, &two
->oid
)) {
3594 if (must_show_header
)
3595 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3596 header
.buf
, header
.len
,
3598 goto free_ab_and_return
;
3600 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3601 header
.buf
, header
.len
, 0);
3602 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3603 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3604 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3606 strbuf_release(&sb
);
3607 goto free_ab_and_return
;
3609 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3610 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3611 die("unable to read files to diff");
3612 /* Quite common confusing case */
3613 if (mf1
.size
== mf2
.size
&&
3614 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3615 if (must_show_header
)
3616 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3617 header
.buf
, header
.len
, 0);
3618 goto free_ab_and_return
;
3620 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3621 strbuf_reset(&header
);
3622 if (o
->flags
.binary
)
3623 emit_binary_diff(o
, &mf1
, &mf2
);
3625 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3626 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3627 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3629 strbuf_release(&sb
);
3631 o
->found_changes
= 1;
3633 /* Crazy xdl interfaces.. */
3634 const char *diffopts
;
3638 struct emit_callback ecbdata
;
3639 const struct userdiff_funcname
*pe
;
3641 if (must_show_header
) {
3642 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3643 header
.buf
, header
.len
, 0);
3644 strbuf_reset(&header
);
3647 mf1
.size
= fill_textconv(o
->repo
, textconv_one
, one
, &mf1
.ptr
);
3648 mf2
.size
= fill_textconv(o
->repo
, textconv_two
, two
, &mf2
.ptr
);
3650 pe
= diff_funcname_pattern(o
, one
);
3652 pe
= diff_funcname_pattern(o
, two
);
3654 memset(&xpp
, 0, sizeof(xpp
));
3655 memset(&xecfg
, 0, sizeof(xecfg
));
3656 memset(&ecbdata
, 0, sizeof(ecbdata
));
3657 if (o
->flags
.suppress_diff_headers
)
3659 ecbdata
.label_path
= lbl
;
3660 ecbdata
.color_diff
= want_color(o
->use_color
);
3661 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
3662 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3663 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3665 if (header
.len
&& !o
->flags
.suppress_diff_headers
)
3666 ecbdata
.header
= &header
;
3667 xpp
.flags
= o
->xdl_opts
;
3668 xpp
.ignore_regex
= o
->ignore_regex
;
3669 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3670 xpp
.anchors
= o
->anchors
;
3671 xpp
.anchors_nr
= o
->anchors_nr
;
3672 xecfg
.ctxlen
= o
->context
;
3673 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3674 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3675 if (o
->flags
.funccontext
)
3676 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3678 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3680 diffopts
= getenv("GIT_DIFF_OPTS");
3683 else if (skip_prefix(diffopts
, "--unified=", &v
))
3684 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3685 else if (skip_prefix(diffopts
, "-u", &v
))
3686 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3689 init_diff_words_data(&ecbdata
, o
, one
, two
);
3690 if (xdi_diff_outf(&mf1
, &mf2
, NULL
, fn_out_consume
,
3691 &ecbdata
, &xpp
, &xecfg
))
3692 die("unable to generate diff for %s", one
->path
);
3694 free_diff_words_data(&ecbdata
);
3699 xdiff_clear_find_func(&xecfg
);
3703 strbuf_release(&header
);
3704 diff_free_filespec_data(one
);
3705 diff_free_filespec_data(two
);
3711 static char *get_compact_summary(const struct diff_filepair
*p
, int is_renamed
)
3714 if (p
->status
== DIFF_STATUS_ADDED
) {
3715 if (S_ISLNK(p
->two
->mode
))
3717 else if ((p
->two
->mode
& 0777) == 0755)
3721 } else if (p
->status
== DIFF_STATUS_DELETED
)
3724 if (S_ISLNK(p
->one
->mode
) && !S_ISLNK(p
->two
->mode
))
3726 else if (!S_ISLNK(p
->one
->mode
) && S_ISLNK(p
->two
->mode
))
3728 else if ((p
->one
->mode
& 0777) == 0644 &&
3729 (p
->two
->mode
& 0777) == 0755)
3731 else if ((p
->one
->mode
& 0777) == 0755 &&
3732 (p
->two
->mode
& 0777) == 0644)
3737 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3738 struct diff_filespec
*one
,
3739 struct diff_filespec
*two
,
3740 struct diffstat_t
*diffstat
,
3741 struct diff_options
*o
,
3742 struct diff_filepair
*p
)
3745 struct diffstat_file
*data
;
3747 int complete_rewrite
= 0;
3749 if (!DIFF_PAIR_UNMERGED(p
)) {
3750 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3751 complete_rewrite
= 1;
3754 data
= diffstat_add(diffstat
, name_a
, name_b
);
3755 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3756 if (o
->flags
.stat_with_summary
)
3757 data
->comments
= get_compact_summary(p
, data
->is_renamed
);
3760 data
->is_unmerged
= 1;
3764 /* saves some reads if true, not a guarantee of diff outcome */
3765 may_differ
= !(one
->oid_valid
&& two
->oid_valid
&&
3766 oideq(&one
->oid
, &two
->oid
));
3768 if (diff_filespec_is_binary(o
->repo
, one
) ||
3769 diff_filespec_is_binary(o
->repo
, two
)) {
3770 data
->is_binary
= 1;
3775 data
->added
= diff_filespec_size(o
->repo
, two
);
3776 data
->deleted
= diff_filespec_size(o
->repo
, one
);
3780 else if (complete_rewrite
) {
3781 diff_populate_filespec(o
->repo
, one
, NULL
);
3782 diff_populate_filespec(o
->repo
, two
, NULL
);
3783 data
->deleted
= count_lines(one
->data
, one
->size
);
3784 data
->added
= count_lines(two
->data
, two
->size
);
3787 else if (may_differ
) {
3788 /* Crazy xdl interfaces.. */
3792 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3793 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3794 die("unable to read files to diff");
3796 memset(&xpp
, 0, sizeof(xpp
));
3797 memset(&xecfg
, 0, sizeof(xecfg
));
3798 xpp
.flags
= o
->xdl_opts
;
3799 xpp
.ignore_regex
= o
->ignore_regex
;
3800 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3801 xpp
.anchors
= o
->anchors
;
3802 xpp
.anchors_nr
= o
->anchors_nr
;
3803 xecfg
.ctxlen
= o
->context
;
3804 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3805 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
3806 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
3807 diffstat_consume
, diffstat
, &xpp
, &xecfg
))
3808 die("unable to generate diffstat for %s", one
->path
);
3810 if (DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
)) {
3811 struct diffstat_file
*file
=
3812 diffstat
->files
[diffstat
->nr
- 1];
3814 * Omit diffstats of modified files where nothing changed.
3815 * Even if may_differ, this might be the case due to
3816 * ignoring whitespace changes, etc.
3818 * But note that we special-case additions, deletions,
3819 * renames, and mode changes as adding an empty file,
3820 * for example is still of interest.
3822 if ((p
->status
== DIFF_STATUS_MODIFIED
)
3825 && one
->mode
== two
->mode
) {
3826 free_diffstat_file(file
);
3832 diff_free_filespec_data(one
);
3833 diff_free_filespec_data(two
);
3836 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3837 const char *attr_path
,
3838 struct diff_filespec
*one
,
3839 struct diff_filespec
*two
,
3840 struct diff_options
*o
)
3843 struct checkdiff_t data
;
3848 memset(&data
, 0, sizeof(data
));
3849 data
.filename
= name_b
? name_b
: name_a
;
3852 data
.ws_rule
= whitespace_rule(o
->repo
->index
, attr_path
);
3853 data
.conflict_marker_size
= ll_merge_marker_size(o
->repo
->index
, attr_path
);
3855 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3856 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3857 die("unable to read files to diff");
3860 * All the other codepaths check both sides, but not checking
3861 * the "old" side here is deliberate. We are checking the newly
3862 * introduced changes, and as long as the "new" side is text, we
3863 * can and should check what it introduces.
3865 if (diff_filespec_is_binary(o
->repo
, two
))
3866 goto free_and_return
;
3868 /* Crazy xdl interfaces.. */
3872 memset(&xpp
, 0, sizeof(xpp
));
3873 memset(&xecfg
, 0, sizeof(xecfg
));
3874 xecfg
.ctxlen
= 1; /* at least one context line */
3876 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume_hunk
,
3877 checkdiff_consume
, &data
,
3879 die("unable to generate checkdiff for %s", one
->path
);
3881 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3882 struct emit_callback ecbdata
;
3885 ecbdata
.ws_rule
= data
.ws_rule
;
3886 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3887 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3892 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3893 fprintf(o
->file
, "%s:%d: %s.\n",
3894 data
.filename
, blank_at_eof
, err
);
3895 data
.status
= 1; /* report errors */
3900 diff_free_filespec_data(one
);
3901 diff_free_filespec_data(two
);
3903 o
->flags
.check_failed
= 1;
3906 struct diff_filespec
*alloc_filespec(const char *path
)
3908 struct diff_filespec
*spec
;
3910 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3912 spec
->is_binary
= -1;
3916 void free_filespec(struct diff_filespec
*spec
)
3918 if (!--spec
->count
) {
3919 diff_free_filespec_data(spec
);
3924 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3925 int oid_valid
, unsigned short mode
)
3928 spec
->mode
= canon_mode(mode
);
3929 oidcpy(&spec
->oid
, oid
);
3930 spec
->oid_valid
= oid_valid
;
3935 * Given a name and sha1 pair, if the index tells us the file in
3936 * the work tree has that object contents, return true, so that
3937 * prepare_temp_file() does not have to inflate and extract.
3939 static int reuse_worktree_file(struct index_state
*istate
,
3941 const struct object_id
*oid
,
3944 const struct cache_entry
*ce
;
3949 * We do not read the cache ourselves here, because the
3950 * benchmark with my previous version that always reads cache
3951 * shows that it makes things worse for diff-tree comparing
3952 * two linux-2.6 kernel trees in an already checked out work
3953 * tree. This is because most diff-tree comparisons deal with
3954 * only a small number of files, while reading the cache is
3955 * expensive for a large project, and its cost outweighs the
3956 * savings we get by not inflating the object to a temporary
3957 * file. Practically, this code only helps when we are used
3958 * by diff-cache --cached, which does read the cache before
3964 /* We want to avoid the working directory if our caller
3965 * doesn't need the data in a normal file, this system
3966 * is rather slow with its stat/open/mmap/close syscalls,
3967 * and the object is contained in a pack file. The pack
3968 * is probably already open and will be faster to obtain
3969 * the data through than the working directory. Loose
3970 * objects however would tend to be slower as they need
3971 * to be individually opened and inflated.
3973 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_object_pack(oid
))
3977 * Similarly, if we'd have to convert the file contents anyway, that
3978 * makes the optimization not worthwhile.
3980 if (!want_file
&& would_convert_to_git(istate
, name
))
3984 * If this path does not match our sparse-checkout definition,
3985 * then the file will not be in the working directory.
3987 if (!path_in_sparse_checkout(name
, istate
))
3991 pos
= index_name_pos(istate
, name
, len
);
3994 ce
= istate
->cache
[pos
];
3997 * This is not the sha1 we are looking for, or
3998 * unreusable because it is not a regular file.
4000 if (!oideq(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
4004 * If ce is marked as "assume unchanged", there is no
4005 * guarantee that work tree matches what we are looking for.
4007 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
4011 * If ce matches the file in the work tree, we can reuse it.
4013 if (ce_uptodate(ce
) ||
4014 (!lstat(name
, &st
) && !ie_match_stat(istate
, ce
, &st
, 0)))
4020 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
4022 struct strbuf buf
= STRBUF_INIT
;
4025 /* Are we looking at the work tree? */
4026 if (s
->dirty_submodule
)
4029 strbuf_addf(&buf
, "Subproject commit %s%s\n",
4030 oid_to_hex(&s
->oid
), dirty
);
4034 strbuf_release(&buf
);
4036 s
->data
= strbuf_detach(&buf
, NULL
);
4043 * While doing rename detection and pickaxe operation, we may need to
4044 * grab the data for the blob (or file) for our own in-core comparison.
4045 * diff_filespec has data and size fields for this purpose.
4047 int diff_populate_filespec(struct repository
*r
,
4048 struct diff_filespec
*s
,
4049 const struct diff_populate_filespec_options
*options
)
4051 int size_only
= options
? options
->check_size_only
: 0;
4052 int check_binary
= options
? options
->check_binary
: 0;
4054 int conv_flags
= global_conv_flags_eol
;
4056 * demote FAIL to WARN to allow inspecting the situation
4057 * instead of refusing.
4059 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
4060 conv_flags
= CONV_EOL_RNDTRP_WARN
;
4062 if (!DIFF_FILE_VALID(s
))
4063 die("internal error: asking to populate invalid file.");
4064 if (S_ISDIR(s
->mode
))
4070 if (size_only
&& 0 < s
->size
)
4073 if (S_ISGITLINK(s
->mode
))
4074 return diff_populate_gitlink(s
, size_only
);
4076 if (!s
->oid_valid
||
4077 reuse_worktree_file(r
->index
, s
->path
, &s
->oid
, 0)) {
4078 struct strbuf buf
= STRBUF_INIT
;
4082 if (lstat(s
->path
, &st
) < 0) {
4086 s
->data
= (char *)"";
4090 s
->size
= xsize_t(st
.st_size
);
4093 if (S_ISLNK(st
.st_mode
)) {
4094 struct strbuf sb
= STRBUF_INIT
;
4096 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
4099 s
->data
= strbuf_detach(&sb
, NULL
);
4105 * Even if the caller would be happy with getting
4106 * only the size, we cannot return early at this
4107 * point if the path requires us to run the content
4110 if (size_only
&& !would_convert_to_git(r
->index
, s
->path
))
4114 * Note: this check uses xsize_t(st.st_size) that may
4115 * not be the true size of the blob after it goes
4116 * through convert_to_git(). This may not strictly be
4117 * correct, but the whole point of big_file_threshold
4118 * and is_binary check being that we want to avoid
4119 * opening the file and inspecting the contents, this
4123 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4127 fd
= open(s
->path
, O_RDONLY
);
4130 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
4132 s
->should_munmap
= 1;
4135 * Convert from working tree format to canonical git format
4137 if (convert_to_git(r
->index
, s
->path
, s
->data
, s
->size
, &buf
, conv_flags
)) {
4139 munmap(s
->data
, s
->size
);
4140 s
->should_munmap
= 0;
4141 s
->data
= strbuf_detach(&buf
, &size
);
4147 struct object_info info
= {
4151 if (!(size_only
|| check_binary
))
4153 * Set contentp, since there is no chance that merely
4154 * the size is sufficient.
4156 info
.contentp
= &s
->data
;
4158 if (options
&& options
->missing_object_cb
) {
4159 if (!oid_object_info_extended(r
, &s
->oid
, &info
,
4160 OBJECT_INFO_LOOKUP_REPLACE
|
4161 OBJECT_INFO_SKIP_FETCH_OBJECT
))
4163 options
->missing_object_cb(options
->missing_object_data
);
4165 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4166 OBJECT_INFO_LOOKUP_REPLACE
))
4167 die("unable to read %s", oid_to_hex(&s
->oid
));
4170 if (size_only
|| check_binary
) {
4173 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4178 if (!info
.contentp
) {
4179 info
.contentp
= &s
->data
;
4180 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4181 OBJECT_INFO_LOOKUP_REPLACE
))
4182 die("unable to read %s", oid_to_hex(&s
->oid
));
4189 void diff_free_filespec_blob(struct diff_filespec
*s
)
4193 else if (s
->should_munmap
)
4194 munmap(s
->data
, s
->size
);
4196 if (s
->should_free
|| s
->should_munmap
) {
4197 s
->should_free
= s
->should_munmap
= 0;
4202 void diff_free_filespec_data(struct diff_filespec
*s
)
4207 diff_free_filespec_blob(s
);
4208 FREE_AND_NULL(s
->cnt_data
);
4211 static void prep_temp_blob(struct index_state
*istate
,
4212 const char *path
, struct diff_tempfile
*temp
,
4215 const struct object_id
*oid
,
4218 struct strbuf buf
= STRBUF_INIT
;
4219 char *path_dup
= xstrdup(path
);
4220 const char *base
= basename(path_dup
);
4221 struct checkout_metadata meta
;
4223 init_checkout_metadata(&meta
, NULL
, NULL
, oid
);
4225 temp
->tempfile
= mks_tempfile_dt("git-blob-XXXXXX", base
);
4226 if (!temp
->tempfile
)
4227 die_errno("unable to create temp-file");
4228 if (convert_to_working_tree(istate
, path
,
4229 (const char *)blob
, (size_t)size
, &buf
, &meta
)) {
4233 if (write_in_full(temp
->tempfile
->fd
, blob
, size
) < 0 ||
4234 close_tempfile_gently(temp
->tempfile
))
4235 die_errno("unable to write temp-file");
4236 temp
->name
= get_tempfile_path(temp
->tempfile
);
4237 oid_to_hex_r(temp
->hex
, oid
);
4238 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
4239 strbuf_release(&buf
);
4243 static struct diff_tempfile
*prepare_temp_file(struct repository
*r
,
4244 struct diff_filespec
*one
)
4246 struct diff_tempfile
*temp
= claim_diff_tempfile();
4248 if (!DIFF_FILE_VALID(one
)) {
4250 /* A '-' entry produces this for file-2, and
4251 * a '+' entry produces this for file-1.
4253 temp
->name
= "/dev/null";
4254 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
4255 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
4259 if (!S_ISGITLINK(one
->mode
) &&
4261 reuse_worktree_file(r
->index
, one
->path
, &one
->oid
, 1))) {
4263 if (lstat(one
->path
, &st
) < 0) {
4264 if (errno
== ENOENT
)
4265 goto not_a_valid_file
;
4266 die_errno("stat(%s)", one
->path
);
4268 if (S_ISLNK(st
.st_mode
)) {
4269 struct strbuf sb
= STRBUF_INIT
;
4270 if (strbuf_readlink(&sb
, one
->path
, st
.st_size
) < 0)
4271 die_errno("readlink(%s)", one
->path
);
4272 prep_temp_blob(r
->index
, one
->path
, temp
, sb
.buf
, sb
.len
,
4274 &one
->oid
: null_oid()),
4276 one
->mode
: S_IFLNK
));
4277 strbuf_release(&sb
);
4280 /* we can borrow from the file in the work tree */
4281 temp
->name
= one
->path
;
4282 if (!one
->oid_valid
)
4283 oid_to_hex_r(temp
->hex
, null_oid());
4285 oid_to_hex_r(temp
->hex
, &one
->oid
);
4286 /* Even though we may sometimes borrow the
4287 * contents from the work tree, we always want
4288 * one->mode. mode is trustworthy even when
4289 * !(one->oid_valid), as long as
4290 * DIFF_FILE_VALID(one).
4292 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
4297 if (diff_populate_filespec(r
, one
, NULL
))
4298 die("cannot read data blob for %s", one
->path
);
4299 prep_temp_blob(r
->index
, one
->path
, temp
,
4300 one
->data
, one
->size
,
4301 &one
->oid
, one
->mode
);
4306 static void add_external_diff_name(struct repository
*r
,
4307 struct strvec
*argv
,
4308 struct diff_filespec
*df
)
4310 struct diff_tempfile
*temp
= prepare_temp_file(r
, df
);
4311 strvec_push(argv
, temp
->name
);
4312 strvec_push(argv
, temp
->hex
);
4313 strvec_push(argv
, temp
->mode
);
4316 /* An external diff command takes:
4318 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4319 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4322 static void run_external_diff(const char *pgm
,
4325 struct diff_filespec
*one
,
4326 struct diff_filespec
*two
,
4327 const char *xfrm_msg
,
4328 struct diff_options
*o
)
4330 struct child_process cmd
= CHILD_PROCESS_INIT
;
4331 struct diff_queue_struct
*q
= &diff_queued_diff
;
4333 strvec_push(&cmd
.args
, pgm
);
4334 strvec_push(&cmd
.args
, name
);
4337 add_external_diff_name(o
->repo
, &cmd
.args
, one
);
4338 add_external_diff_name(o
->repo
, &cmd
.args
, two
);
4340 strvec_push(&cmd
.args
, other
);
4341 strvec_push(&cmd
.args
, xfrm_msg
);
4345 strvec_pushf(&cmd
.env
, "GIT_DIFF_PATH_COUNTER=%d",
4346 ++o
->diff_path_counter
);
4347 strvec_pushf(&cmd
.env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
4349 diff_free_filespec_data(one
);
4350 diff_free_filespec_data(two
);
4352 if (run_command(&cmd
))
4353 die(_("external diff died, stopping at %s"), name
);
4358 static int similarity_index(struct diff_filepair
*p
)
4360 return p
->score
* 100 / MAX_SCORE
;
4363 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
4365 if (startup_info
->have_repository
)
4366 return repo_find_unique_abbrev(the_repository
, oid
, abbrev
);
4368 char *hex
= oid_to_hex(oid
);
4370 abbrev
= FALLBACK_DEFAULT_ABBREV
;
4371 if (abbrev
> the_hash_algo
->hexsz
)
4372 BUG("oid abbreviation out of range: %d", abbrev
);
4379 static void fill_metainfo(struct strbuf
*msg
,
4382 struct diff_filespec
*one
,
4383 struct diff_filespec
*two
,
4384 struct diff_options
*o
,
4385 struct diff_filepair
*p
,
4386 int *must_show_header
,
4389 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
4390 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
4391 const char *line_prefix
= diff_line_prefix(o
);
4392 struct string_list
*more_headers
= NULL
;
4394 *must_show_header
= 1;
4395 strbuf_init(msg
, PATH_MAX
* 2 + 300);
4396 switch (p
->status
) {
4397 case DIFF_STATUS_COPIED
:
4398 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4399 line_prefix
, set
, similarity_index(p
));
4400 strbuf_addf(msg
, "%s\n%s%scopy from ",
4401 reset
, line_prefix
, set
);
4402 quote_c_style(name
, msg
, NULL
, 0);
4403 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
4404 quote_c_style(other
, msg
, NULL
, 0);
4405 strbuf_addf(msg
, "%s\n", reset
);
4407 case DIFF_STATUS_RENAMED
:
4408 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4409 line_prefix
, set
, similarity_index(p
));
4410 strbuf_addf(msg
, "%s\n%s%srename from ",
4411 reset
, line_prefix
, set
);
4412 quote_c_style(name
, msg
, NULL
, 0);
4413 strbuf_addf(msg
, "%s\n%s%srename to ",
4414 reset
, line_prefix
, set
);
4415 quote_c_style(other
, msg
, NULL
, 0);
4416 strbuf_addf(msg
, "%s\n", reset
);
4418 case DIFF_STATUS_MODIFIED
:
4420 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
4422 set
, similarity_index(p
), reset
);
4427 *must_show_header
= 0;
4429 if ((more_headers
= additional_headers(o
, name
))) {
4430 add_formatted_headers(msg
, more_headers
,
4431 line_prefix
, set
, reset
);
4432 *must_show_header
= 1;
4434 if (one
&& two
&& !oideq(&one
->oid
, &two
->oid
)) {
4435 const unsigned hexsz
= the_hash_algo
->hexsz
;
4436 int abbrev
= o
->abbrev
? o
->abbrev
: DEFAULT_ABBREV
;
4438 if (o
->flags
.full_index
)
4441 if (o
->flags
.binary
) {
4443 if ((!fill_mmfile(o
->repo
, &mf
, one
) &&
4444 diff_filespec_is_binary(o
->repo
, one
)) ||
4445 (!fill_mmfile(o
->repo
, &mf
, two
) &&
4446 diff_filespec_is_binary(o
->repo
, two
)))
4449 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
4450 diff_abbrev_oid(&one
->oid
, abbrev
),
4451 diff_abbrev_oid(&two
->oid
, abbrev
));
4452 if (one
->mode
== two
->mode
)
4453 strbuf_addf(msg
, " %06o", one
->mode
);
4454 strbuf_addf(msg
, "%s\n", reset
);
4458 static void run_diff_cmd(const char *pgm
,
4461 const char *attr_path
,
4462 struct diff_filespec
*one
,
4463 struct diff_filespec
*two
,
4465 struct diff_options
*o
,
4466 struct diff_filepair
*p
)
4468 const char *xfrm_msg
= NULL
;
4469 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
4470 int must_show_header
= 0;
4471 struct userdiff_driver
*drv
= NULL
;
4473 if (o
->flags
.allow_external
|| !o
->ignore_driver_algorithm
)
4474 drv
= userdiff_find_by_path(o
->repo
->index
, attr_path
);
4476 if (o
->flags
.allow_external
&& drv
&& drv
->external
)
4477 pgm
= drv
->external
;
4481 * don't use colors when the header is intended for an
4482 * external diff driver
4484 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
4486 want_color(o
->use_color
) && !pgm
);
4487 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
4491 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
, o
);
4495 if (!o
->ignore_driver_algorithm
&& drv
&& drv
->algorithm
)
4496 set_diff_algorithm(o
, drv
->algorithm
);
4498 builtin_diff(name
, other
? other
: name
,
4499 one
, two
, xfrm_msg
, must_show_header
,
4500 o
, complete_rewrite
);
4502 fprintf(o
->file
, "* Unmerged path %s\n", name
);
4506 static void diff_fill_oid_info(struct diff_filespec
*one
, struct index_state
*istate
)
4508 if (DIFF_FILE_VALID(one
)) {
4509 if (!one
->oid_valid
) {
4511 if (one
->is_stdin
) {
4515 if (lstat(one
->path
, &st
) < 0)
4516 die_errno("stat '%s'", one
->path
);
4517 if (index_path(istate
, &one
->oid
, one
->path
, &st
, 0))
4518 die("cannot hash %s", one
->path
);
4525 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
4527 /* Strip the prefix but do not molest /dev/null and absolute paths */
4528 if (*namep
&& !is_absolute_path(*namep
)) {
4529 *namep
+= prefix_length
;
4533 if (*otherp
&& !is_absolute_path(*otherp
)) {
4534 *otherp
+= prefix_length
;
4535 if (**otherp
== '/')
4540 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
4542 const char *pgm
= external_diff();
4544 struct diff_filespec
*one
= p
->one
;
4545 struct diff_filespec
*two
= p
->two
;
4548 const char *attr_path
;
4551 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
4553 if (o
->prefix_length
)
4554 strip_prefix(o
->prefix_length
, &name
, &other
);
4556 if (!o
->flags
.allow_external
)
4559 if (DIFF_PAIR_UNMERGED(p
)) {
4560 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
4561 NULL
, NULL
, NULL
, o
, p
);
4565 diff_fill_oid_info(one
, o
->repo
->index
);
4566 diff_fill_oid_info(two
, o
->repo
->index
);
4569 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4570 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4572 * a filepair that changes between file and symlink
4573 * needs to be split into deletion and creation.
4575 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4576 run_diff_cmd(NULL
, name
, other
, attr_path
,
4580 strbuf_release(&msg
);
4582 null
= alloc_filespec(one
->path
);
4583 run_diff_cmd(NULL
, name
, other
, attr_path
,
4584 null
, two
, &msg
, o
, p
);
4588 run_diff_cmd(pgm
, name
, other
, attr_path
,
4589 one
, two
, &msg
, o
, p
);
4591 strbuf_release(&msg
);
4594 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4595 struct diffstat_t
*diffstat
)
4600 if (!o
->ignore_driver_algorithm
) {
4601 struct userdiff_driver
*drv
= userdiff_find_by_path(o
->repo
->index
,
4604 if (drv
&& drv
->algorithm
)
4605 set_diff_algorithm(o
, drv
->algorithm
);
4608 if (DIFF_PAIR_UNMERGED(p
)) {
4610 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
,
4615 name
= p
->one
->path
;
4616 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4618 if (o
->prefix_length
)
4619 strip_prefix(o
->prefix_length
, &name
, &other
);
4621 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4622 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4624 builtin_diffstat(name
, other
, p
->one
, p
->two
,
4628 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4632 const char *attr_path
;
4634 if (DIFF_PAIR_UNMERGED(p
)) {
4639 name
= p
->one
->path
;
4640 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4641 attr_path
= other
? other
: name
;
4643 if (o
->prefix_length
)
4644 strip_prefix(o
->prefix_length
, &name
, &other
);
4646 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4647 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4649 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4652 void repo_diff_setup(struct repository
*r
, struct diff_options
*options
)
4654 memcpy(options
, &default_diff_options
, sizeof(*options
));
4656 options
->file
= stdout
;
4659 options
->output_indicators
[OUTPUT_INDICATOR_NEW
] = '+';
4660 options
->output_indicators
[OUTPUT_INDICATOR_OLD
] = '-';
4661 options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
] = ' ';
4662 options
->abbrev
= DEFAULT_ABBREV
;
4663 options
->line_termination
= '\n';
4664 options
->break_opt
= -1;
4665 options
->rename_limit
= -1;
4666 options
->dirstat_permille
= diff_dirstat_permille_default
;
4667 options
->context
= diff_context_default
;
4668 options
->interhunkcontext
= diff_interhunk_context_default
;
4669 options
->ws_error_highlight
= ws_error_highlight_default
;
4670 options
->flags
.rename_empty
= 1;
4671 options
->flags
.relative_name
= diff_relative
;
4672 options
->objfind
= NULL
;
4674 /* pathchange left =NULL by default */
4675 options
->change
= diff_change
;
4676 options
->add_remove
= diff_addremove
;
4677 options
->use_color
= diff_use_color_default
;
4678 options
->detect_rename
= diff_detect_rename_default
;
4679 options
->xdl_opts
|= diff_algorithm
;
4680 if (diff_indent_heuristic
)
4681 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4683 options
->orderfile
= diff_order_file_cfg
;
4685 if (!options
->flags
.ignore_submodule_set
)
4686 options
->flags
.ignore_untracked_in_submodules
= 1;
4688 if (diff_no_prefix
) {
4689 options
->a_prefix
= options
->b_prefix
= "";
4690 } else if (!diff_mnemonic_prefix
) {
4691 options
->a_prefix
= "a/";
4692 options
->b_prefix
= "b/";
4695 options
->color_moved
= diff_color_moved_default
;
4696 options
->color_moved_ws_handling
= diff_color_moved_ws_default
;
4699 static const char diff_status_letters
[] = {
4702 DIFF_STATUS_DELETED
,
4703 DIFF_STATUS_MODIFIED
,
4704 DIFF_STATUS_RENAMED
,
4705 DIFF_STATUS_TYPE_CHANGED
,
4706 DIFF_STATUS_UNKNOWN
,
4707 DIFF_STATUS_UNMERGED
,
4708 DIFF_STATUS_FILTER_AON
,
4709 DIFF_STATUS_FILTER_BROKEN
,
4713 static unsigned int filter_bit
['Z' + 1];
4715 static void prepare_filter_bits(void)
4719 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4720 for (i
= 0; diff_status_letters
[i
]; i
++)
4721 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4725 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4727 return opt
->filter
& filter_bit
[(int) status
];
4730 unsigned diff_filter_bit(char status
)
4732 prepare_filter_bits();
4733 return filter_bit
[(int) status
];
4736 void diff_setup_done(struct diff_options
*options
)
4738 unsigned check_mask
= DIFF_FORMAT_NAME
|
4739 DIFF_FORMAT_NAME_STATUS
|
4740 DIFF_FORMAT_CHECKDIFF
|
4741 DIFF_FORMAT_NO_OUTPUT
;
4743 * This must be signed because we're comparing against a potentially
4746 const int hexsz
= the_hash_algo
->hexsz
;
4748 if (options
->set_default
)
4749 options
->set_default(options
);
4751 if (HAS_MULTI_BITS(options
->output_format
& check_mask
))
4752 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
4753 "--name-only", "--name-status", "--check", "-s");
4755 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
4756 die(_("options '%s', '%s', and '%s' cannot be used together"),
4757 "-G", "-S", "--find-object");
4759 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_G_REGEX_MASK
))
4760 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"),
4761 "-G", "--pickaxe-regex", "--pickaxe-regex", "-S");
4763 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK
))
4764 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"),
4765 "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S");
4768 * Most of the time we can say "there are changes"
4769 * only by checking if there are changed paths, but
4770 * --ignore-whitespace* options force us to look
4774 if ((options
->xdl_opts
& XDF_WHITESPACE_FLAGS
) ||
4775 options
->ignore_regex_nr
)
4776 options
->flags
.diff_from_contents
= 1;
4778 options
->flags
.diff_from_contents
= 0;
4780 if (options
->flags
.find_copies_harder
)
4781 options
->detect_rename
= DIFF_DETECT_COPY
;
4783 if (!options
->flags
.relative_name
)
4784 options
->prefix
= NULL
;
4785 if (options
->prefix
)
4786 options
->prefix_length
= strlen(options
->prefix
);
4788 options
->prefix_length
= 0;
4790 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4791 DIFF_FORMAT_NAME_STATUS
|
4792 DIFF_FORMAT_CHECKDIFF
|
4793 DIFF_FORMAT_NO_OUTPUT
))
4794 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4795 DIFF_FORMAT_NUMSTAT
|
4796 DIFF_FORMAT_DIFFSTAT
|
4797 DIFF_FORMAT_SHORTSTAT
|
4798 DIFF_FORMAT_DIRSTAT
|
4799 DIFF_FORMAT_SUMMARY
|
4803 * These cases always need recursive; we do not drop caller-supplied
4804 * recursive bits for other formats here.
4806 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4807 DIFF_FORMAT_NUMSTAT
|
4808 DIFF_FORMAT_DIFFSTAT
|
4809 DIFF_FORMAT_SHORTSTAT
|
4810 DIFF_FORMAT_DIRSTAT
|
4811 DIFF_FORMAT_SUMMARY
|
4812 DIFF_FORMAT_CHECKDIFF
))
4813 options
->flags
.recursive
= 1;
4815 * Also pickaxe would not work very well if you do not say recursive
4817 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
4818 options
->flags
.recursive
= 1;
4820 * When patches are generated, submodules diffed against the work tree
4821 * must be checked for dirtiness too so it can be shown in the output
4823 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4824 options
->flags
.dirty_submodules
= 1;
4826 if (options
->detect_rename
&& options
->rename_limit
< 0)
4827 options
->rename_limit
= diff_rename_limit_default
;
4828 if (hexsz
< options
->abbrev
)
4829 options
->abbrev
= hexsz
; /* full */
4832 * It does not make sense to show the first hit we happened
4833 * to have found. It does not make sense not to return with
4834 * exit code in such a case either.
4836 if (options
->flags
.quick
) {
4837 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4838 options
->flags
.exit_with_status
= 1;
4841 options
->diff_path_counter
= 0;
4843 if (options
->flags
.follow_renames
&& options
->pathspec
.nr
!= 1)
4844 die(_("--follow requires exactly one pathspec"));
4846 if (!options
->use_color
|| external_diff())
4847 options
->color_moved
= 0;
4849 if (options
->filter_not
) {
4850 if (!options
->filter
)
4851 options
->filter
= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4852 options
->filter
&= ~options
->filter_not
;
4856 int parse_long_opt(const char *opt
, const char **argv
,
4857 const char **optarg
)
4859 const char *arg
= argv
[0];
4860 if (!skip_prefix(arg
, "--", &arg
))
4862 if (!skip_prefix(arg
, opt
, &arg
))
4864 if (*arg
== '=') { /* stuck form: --option=value */
4870 /* separate form: --option value */
4872 die("Option '--%s' requires a value", opt
);
4877 static int diff_opt_stat(const struct option
*opt
, const char *value
, int unset
)
4879 struct diff_options
*options
= opt
->value
;
4880 int width
= options
->stat_width
;
4881 int name_width
= options
->stat_name_width
;
4882 int graph_width
= options
->stat_graph_width
;
4883 int count
= options
->stat_count
;
4886 BUG_ON_OPT_NEG(unset
);
4888 if (!strcmp(opt
->long_name
, "stat")) {
4890 width
= strtoul(value
, &end
, 10);
4892 name_width
= strtoul(end
+1, &end
, 10);
4894 count
= strtoul(end
+1, &end
, 10);
4896 return error(_("invalid --stat value: %s"), value
);
4898 } else if (!strcmp(opt
->long_name
, "stat-width")) {
4899 width
= strtoul(value
, &end
, 10);
4901 return error(_("%s expects a numerical value"),
4903 } else if (!strcmp(opt
->long_name
, "stat-name-width")) {
4904 name_width
= strtoul(value
, &end
, 10);
4906 return error(_("%s expects a numerical value"),
4908 } else if (!strcmp(opt
->long_name
, "stat-graph-width")) {
4909 graph_width
= strtoul(value
, &end
, 10);
4911 return error(_("%s expects a numerical value"),
4913 } else if (!strcmp(opt
->long_name
, "stat-count")) {
4914 count
= strtoul(value
, &end
, 10);
4916 return error(_("%s expects a numerical value"),
4919 BUG("%s should not get here", opt
->long_name
);
4921 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4922 options
->stat_name_width
= name_width
;
4923 options
->stat_graph_width
= graph_width
;
4924 options
->stat_width
= width
;
4925 options
->stat_count
= count
;
4929 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
4931 struct strbuf errmsg
= STRBUF_INIT
;
4932 if (parse_dirstat_params(options
, params
, &errmsg
))
4933 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
4935 strbuf_release(&errmsg
);
4937 * The caller knows a dirstat-related option is given from the command
4938 * line; allow it to say "return this_function();"
4940 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
4944 static int diff_opt_diff_filter(const struct option
*option
,
4945 const char *optarg
, int unset
)
4947 struct diff_options
*opt
= option
->value
;
4950 BUG_ON_OPT_NEG(unset
);
4951 prepare_filter_bits();
4953 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4957 if ('a' <= optch
&& optch
<= 'z') {
4959 optch
= toupper(optch
);
4964 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
4966 return error(_("unknown change class '%c' in --diff-filter=%s"),
4969 opt
->filter_not
|= bit
;
4976 static void enable_patch_output(int *fmt
)
4978 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
4979 *fmt
|= DIFF_FORMAT_PATCH
;
4982 static int diff_opt_ws_error_highlight(const struct option
*option
,
4983 const char *arg
, int unset
)
4985 struct diff_options
*opt
= option
->value
;
4986 int val
= parse_ws_error_highlight(arg
);
4988 BUG_ON_OPT_NEG(unset
);
4990 return error(_("unknown value after ws-error-highlight=%.*s"),
4992 opt
->ws_error_highlight
= val
;
4996 static int diff_opt_find_object(const struct option
*option
,
4997 const char *arg
, int unset
)
4999 struct diff_options
*opt
= option
->value
;
5000 struct object_id oid
;
5002 BUG_ON_OPT_NEG(unset
);
5003 if (repo_get_oid(the_repository
, arg
, &oid
))
5004 return error(_("unable to resolve '%s'"), arg
);
5007 CALLOC_ARRAY(opt
->objfind
, 1);
5009 opt
->pickaxe_opts
|= DIFF_PICKAXE_KIND_OBJFIND
;
5010 opt
->flags
.recursive
= 1;
5011 opt
->flags
.tree_in_recursive
= 1;
5012 oidset_insert(opt
->objfind
, &oid
);
5016 static int diff_opt_anchored(const struct option
*opt
,
5017 const char *arg
, int unset
)
5019 struct diff_options
*options
= opt
->value
;
5021 BUG_ON_OPT_NEG(unset
);
5022 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
5023 ALLOC_GROW(options
->anchors
, options
->anchors_nr
+ 1,
5024 options
->anchors_alloc
);
5025 options
->anchors
[options
->anchors_nr
++] = xstrdup(arg
);
5029 static int diff_opt_binary(const struct option
*opt
,
5030 const char *arg
, int unset
)
5032 struct diff_options
*options
= opt
->value
;
5034 BUG_ON_OPT_NEG(unset
);
5035 BUG_ON_OPT_ARG(arg
);
5036 enable_patch_output(&options
->output_format
);
5037 options
->flags
.binary
= 1;
5041 static int diff_opt_break_rewrites(const struct option
*opt
,
5042 const char *arg
, int unset
)
5044 int *break_opt
= opt
->value
;
5047 BUG_ON_OPT_NEG(unset
);
5050 opt1
= parse_rename_score(&arg
);
5053 else if (*arg
!= '/')
5054 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5057 opt2
= parse_rename_score(&arg
);
5060 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5061 *break_opt
= opt1
| (opt2
<< 16);
5065 static int diff_opt_char(const struct option
*opt
,
5066 const char *arg
, int unset
)
5068 char *value
= opt
->value
;
5070 BUG_ON_OPT_NEG(unset
);
5072 return error(_("%s expects a character, got '%s'"),
5073 opt
->long_name
, arg
);
5078 static int diff_opt_color_moved(const struct option
*opt
,
5079 const char *arg
, int unset
)
5081 struct diff_options
*options
= opt
->value
;
5084 options
->color_moved
= COLOR_MOVED_NO
;
5086 if (diff_color_moved_default
)
5087 options
->color_moved
= diff_color_moved_default
;
5088 if (options
->color_moved
== COLOR_MOVED_NO
)
5089 options
->color_moved
= COLOR_MOVED_DEFAULT
;
5091 int cm
= parse_color_moved(arg
);
5093 return error(_("bad --color-moved argument: %s"), arg
);
5094 options
->color_moved
= cm
;
5099 static int diff_opt_color_moved_ws(const struct option
*opt
,
5100 const char *arg
, int unset
)
5102 struct diff_options
*options
= opt
->value
;
5106 options
->color_moved_ws_handling
= 0;
5110 cm
= parse_color_moved_ws(arg
);
5111 if (cm
& COLOR_MOVED_WS_ERROR
)
5112 return error(_("invalid mode '%s' in --color-moved-ws"), arg
);
5113 options
->color_moved_ws_handling
= cm
;
5117 static int diff_opt_color_words(const struct option
*opt
,
5118 const char *arg
, int unset
)
5120 struct diff_options
*options
= opt
->value
;
5122 BUG_ON_OPT_NEG(unset
);
5123 options
->use_color
= 1;
5124 options
->word_diff
= DIFF_WORDS_COLOR
;
5125 options
->word_regex
= arg
;
5129 static int diff_opt_compact_summary(const struct option
*opt
,
5130 const char *arg
, int unset
)
5132 struct diff_options
*options
= opt
->value
;
5134 BUG_ON_OPT_ARG(arg
);
5136 options
->flags
.stat_with_summary
= 0;
5138 options
->flags
.stat_with_summary
= 1;
5139 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
5144 static int diff_opt_diff_algorithm(const struct option
*opt
,
5145 const char *arg
, int unset
)
5147 struct diff_options
*options
= opt
->value
;
5149 BUG_ON_OPT_NEG(unset
);
5151 if (set_diff_algorithm(options
, arg
))
5152 return error(_("option diff-algorithm accepts \"myers\", "
5153 "\"minimal\", \"patience\" and \"histogram\""));
5155 options
->ignore_driver_algorithm
= 1;
5160 static int diff_opt_diff_algorithm_no_arg(const struct option
*opt
,
5161 const char *arg
, int unset
)
5163 struct diff_options
*options
= opt
->value
;
5165 BUG_ON_OPT_NEG(unset
);
5166 BUG_ON_OPT_ARG(arg
);
5168 if (set_diff_algorithm(options
, opt
->long_name
))
5169 BUG("available diff algorithms include \"myers\", "
5170 "\"minimal\", \"patience\" and \"histogram\"");
5172 options
->ignore_driver_algorithm
= 1;
5177 static int diff_opt_dirstat(const struct option
*opt
,
5178 const char *arg
, int unset
)
5180 struct diff_options
*options
= opt
->value
;
5182 BUG_ON_OPT_NEG(unset
);
5183 if (!strcmp(opt
->long_name
, "cumulative")) {
5185 BUG("how come --cumulative take a value?");
5187 } else if (!strcmp(opt
->long_name
, "dirstat-by-file"))
5188 parse_dirstat_opt(options
, "files");
5189 parse_dirstat_opt(options
, arg
? arg
: "");
5193 static int diff_opt_find_copies(const struct option
*opt
,
5194 const char *arg
, int unset
)
5196 struct diff_options
*options
= opt
->value
;
5198 BUG_ON_OPT_NEG(unset
);
5201 options
->rename_score
= parse_rename_score(&arg
);
5203 return error(_("invalid argument to %s"), opt
->long_name
);
5205 if (options
->detect_rename
== DIFF_DETECT_COPY
)
5206 options
->flags
.find_copies_harder
= 1;
5208 options
->detect_rename
= DIFF_DETECT_COPY
;
5213 static int diff_opt_find_renames(const struct option
*opt
,
5214 const char *arg
, int unset
)
5216 struct diff_options
*options
= opt
->value
;
5218 BUG_ON_OPT_NEG(unset
);
5221 options
->rename_score
= parse_rename_score(&arg
);
5223 return error(_("invalid argument to %s"), opt
->long_name
);
5225 options
->detect_rename
= DIFF_DETECT_RENAME
;
5229 static int diff_opt_follow(const struct option
*opt
,
5230 const char *arg
, int unset
)
5232 struct diff_options
*options
= opt
->value
;
5234 BUG_ON_OPT_ARG(arg
);
5236 options
->flags
.follow_renames
= 0;
5237 options
->flags
.default_follow_renames
= 0;
5239 options
->flags
.follow_renames
= 1;
5244 static int diff_opt_ignore_submodules(const struct option
*opt
,
5245 const char *arg
, int unset
)
5247 struct diff_options
*options
= opt
->value
;
5249 BUG_ON_OPT_NEG(unset
);
5252 options
->flags
.override_submodule_config
= 1;
5253 handle_ignore_submodules_arg(options
, arg
);
5257 static int diff_opt_line_prefix(const struct option
*opt
,
5258 const char *optarg
, int unset
)
5260 struct diff_options
*options
= opt
->value
;
5262 BUG_ON_OPT_NEG(unset
);
5263 options
->line_prefix
= optarg
;
5264 options
->line_prefix_length
= strlen(options
->line_prefix
);
5265 graph_setup_line_prefix(options
);
5269 static int diff_opt_no_prefix(const struct option
*opt
,
5270 const char *optarg
, int unset
)
5272 struct diff_options
*options
= opt
->value
;
5274 BUG_ON_OPT_NEG(unset
);
5275 BUG_ON_OPT_ARG(optarg
);
5276 options
->a_prefix
= "";
5277 options
->b_prefix
= "";
5281 static enum parse_opt_result
diff_opt_output(struct parse_opt_ctx_t
*ctx
,
5282 const struct option
*opt
,
5283 const char *arg
, int unset
)
5285 struct diff_options
*options
= opt
->value
;
5288 BUG_ON_OPT_NEG(unset
);
5289 path
= prefix_filename(ctx
->prefix
, arg
);
5290 options
->file
= xfopen(path
, "w");
5291 options
->close_file
= 1;
5292 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
5293 options
->use_color
= GIT_COLOR_NEVER
;
5298 static int diff_opt_patience(const struct option
*opt
,
5299 const char *arg
, int unset
)
5301 struct diff_options
*options
= opt
->value
;
5304 BUG_ON_OPT_NEG(unset
);
5305 BUG_ON_OPT_ARG(arg
);
5307 * Both --patience and --anchored use PATIENCE_DIFF
5308 * internally, so remove any anchors previously
5311 for (i
= 0; i
< options
->anchors_nr
; i
++)
5312 free(options
->anchors
[i
]);
5313 options
->anchors_nr
= 0;
5314 options
->ignore_driver_algorithm
= 1;
5316 return set_diff_algorithm(options
, "patience");
5319 static int diff_opt_ignore_regex(const struct option
*opt
,
5320 const char *arg
, int unset
)
5322 struct diff_options
*options
= opt
->value
;
5325 BUG_ON_OPT_NEG(unset
);
5326 regex
= xmalloc(sizeof(*regex
));
5327 if (regcomp(regex
, arg
, REG_EXTENDED
| REG_NEWLINE
))
5328 return error(_("invalid regex given to -I: '%s'"), arg
);
5329 ALLOC_GROW(options
->ignore_regex
, options
->ignore_regex_nr
+ 1,
5330 options
->ignore_regex_alloc
);
5331 options
->ignore_regex
[options
->ignore_regex_nr
++] = regex
;
5335 static int diff_opt_pickaxe_regex(const struct option
*opt
,
5336 const char *arg
, int unset
)
5338 struct diff_options
*options
= opt
->value
;
5340 BUG_ON_OPT_NEG(unset
);
5341 options
->pickaxe
= arg
;
5342 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
5346 static int diff_opt_pickaxe_string(const struct option
*opt
,
5347 const char *arg
, int unset
)
5349 struct diff_options
*options
= opt
->value
;
5351 BUG_ON_OPT_NEG(unset
);
5352 options
->pickaxe
= arg
;
5353 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
5357 static int diff_opt_relative(const struct option
*opt
,
5358 const char *arg
, int unset
)
5360 struct diff_options
*options
= opt
->value
;
5362 options
->flags
.relative_name
= !unset
;
5364 options
->prefix
= arg
;
5368 static int diff_opt_submodule(const struct option
*opt
,
5369 const char *arg
, int unset
)
5371 struct diff_options
*options
= opt
->value
;
5373 BUG_ON_OPT_NEG(unset
);
5376 if (parse_submodule_params(options
, arg
))
5377 return error(_("failed to parse --submodule option parameter: '%s'"),
5382 static int diff_opt_textconv(const struct option
*opt
,
5383 const char *arg
, int unset
)
5385 struct diff_options
*options
= opt
->value
;
5387 BUG_ON_OPT_ARG(arg
);
5389 options
->flags
.allow_textconv
= 0;
5391 options
->flags
.allow_textconv
= 1;
5392 options
->flags
.textconv_set_via_cmdline
= 1;
5397 static int diff_opt_unified(const struct option
*opt
,
5398 const char *arg
, int unset
)
5400 struct diff_options
*options
= opt
->value
;
5403 BUG_ON_OPT_NEG(unset
);
5406 options
->context
= strtol(arg
, &s
, 10);
5408 return error(_("%s expects a numerical value"), "--unified");
5410 enable_patch_output(&options
->output_format
);
5415 static int diff_opt_word_diff(const struct option
*opt
,
5416 const char *arg
, int unset
)
5418 struct diff_options
*options
= opt
->value
;
5420 BUG_ON_OPT_NEG(unset
);
5422 if (!strcmp(arg
, "plain"))
5423 options
->word_diff
= DIFF_WORDS_PLAIN
;
5424 else if (!strcmp(arg
, "color")) {
5425 options
->use_color
= 1;
5426 options
->word_diff
= DIFF_WORDS_COLOR
;
5428 else if (!strcmp(arg
, "porcelain"))
5429 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
5430 else if (!strcmp(arg
, "none"))
5431 options
->word_diff
= DIFF_WORDS_NONE
;
5433 return error(_("bad --word-diff argument: %s"), arg
);
5435 if (options
->word_diff
== DIFF_WORDS_NONE
)
5436 options
->word_diff
= DIFF_WORDS_PLAIN
;
5441 static int diff_opt_word_diff_regex(const struct option
*opt
,
5442 const char *arg
, int unset
)
5444 struct diff_options
*options
= opt
->value
;
5446 BUG_ON_OPT_NEG(unset
);
5447 if (options
->word_diff
== DIFF_WORDS_NONE
)
5448 options
->word_diff
= DIFF_WORDS_PLAIN
;
5449 options
->word_regex
= arg
;
5453 static int diff_opt_rotate_to(const struct option
*opt
, const char *arg
, int unset
)
5455 struct diff_options
*options
= opt
->value
;
5457 BUG_ON_OPT_NEG(unset
);
5458 if (!strcmp(opt
->long_name
, "skip-to"))
5459 options
->skip_instead_of_rotate
= 1;
5461 options
->skip_instead_of_rotate
= 0;
5462 options
->rotate_to
= arg
;
5466 struct option
*add_diff_options(const struct option
*opts
,
5467 struct diff_options
*options
)
5469 struct option parseopts
[] = {
5470 OPT_GROUP(N_("Diff output format options")),
5471 OPT_BITOP('p', "patch", &options
->output_format
,
5472 N_("generate patch"),
5473 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5474 OPT_BIT_F('s', "no-patch", &options
->output_format
,
5475 N_("suppress diff output"),
5476 DIFF_FORMAT_NO_OUTPUT
, PARSE_OPT_NONEG
),
5477 OPT_BITOP('u', NULL
, &options
->output_format
,
5478 N_("generate patch"),
5479 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5480 OPT_CALLBACK_F('U', "unified", options
, N_("<n>"),
5481 N_("generate diffs with <n> lines context"),
5482 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_unified
),
5483 OPT_BOOL('W', "function-context", &options
->flags
.funccontext
,
5484 N_("generate diffs with <n> lines context")),
5485 OPT_BIT_F(0, "raw", &options
->output_format
,
5486 N_("generate the diff in raw format"),
5487 DIFF_FORMAT_RAW
, PARSE_OPT_NONEG
),
5488 OPT_BITOP(0, "patch-with-raw", &options
->output_format
,
5489 N_("synonym for '-p --raw'"),
5490 DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
,
5491 DIFF_FORMAT_NO_OUTPUT
),
5492 OPT_BITOP(0, "patch-with-stat", &options
->output_format
,
5493 N_("synonym for '-p --stat'"),
5494 DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
,
5495 DIFF_FORMAT_NO_OUTPUT
),
5496 OPT_BIT_F(0, "numstat", &options
->output_format
,
5497 N_("machine friendly --stat"),
5498 DIFF_FORMAT_NUMSTAT
, PARSE_OPT_NONEG
),
5499 OPT_BIT_F(0, "shortstat", &options
->output_format
,
5500 N_("output only the last line of --stat"),
5501 DIFF_FORMAT_SHORTSTAT
, PARSE_OPT_NONEG
),
5502 OPT_CALLBACK_F('X', "dirstat", options
, N_("<param1,param2>..."),
5503 N_("output the distribution of relative amount of changes for each sub-directory"),
5504 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5506 OPT_CALLBACK_F(0, "cumulative", options
, NULL
,
5507 N_("synonym for --dirstat=cumulative"),
5508 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5510 OPT_CALLBACK_F(0, "dirstat-by-file", options
, N_("<param1,param2>..."),
5511 N_("synonym for --dirstat=files,param1,param2..."),
5512 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5514 OPT_BIT_F(0, "check", &options
->output_format
,
5515 N_("warn if changes introduce conflict markers or whitespace errors"),
5516 DIFF_FORMAT_CHECKDIFF
, PARSE_OPT_NONEG
),
5517 OPT_BIT_F(0, "summary", &options
->output_format
,
5518 N_("condensed summary such as creations, renames and mode changes"),
5519 DIFF_FORMAT_SUMMARY
, PARSE_OPT_NONEG
),
5520 OPT_BIT_F(0, "name-only", &options
->output_format
,
5521 N_("show only names of changed files"),
5522 DIFF_FORMAT_NAME
, PARSE_OPT_NONEG
),
5523 OPT_BIT_F(0, "name-status", &options
->output_format
,
5524 N_("show only names and status of changed files"),
5525 DIFF_FORMAT_NAME_STATUS
, PARSE_OPT_NONEG
),
5526 OPT_CALLBACK_F(0, "stat", options
, N_("<width>[,<name-width>[,<count>]]"),
5527 N_("generate diffstat"),
5528 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_stat
),
5529 OPT_CALLBACK_F(0, "stat-width", options
, N_("<width>"),
5530 N_("generate diffstat with a given width"),
5531 PARSE_OPT_NONEG
, diff_opt_stat
),
5532 OPT_CALLBACK_F(0, "stat-name-width", options
, N_("<width>"),
5533 N_("generate diffstat with a given name width"),
5534 PARSE_OPT_NONEG
, diff_opt_stat
),
5535 OPT_CALLBACK_F(0, "stat-graph-width", options
, N_("<width>"),
5536 N_("generate diffstat with a given graph width"),
5537 PARSE_OPT_NONEG
, diff_opt_stat
),
5538 OPT_CALLBACK_F(0, "stat-count", options
, N_("<count>"),
5539 N_("generate diffstat with limited lines"),
5540 PARSE_OPT_NONEG
, diff_opt_stat
),
5541 OPT_CALLBACK_F(0, "compact-summary", options
, NULL
,
5542 N_("generate compact summary in diffstat"),
5543 PARSE_OPT_NOARG
, diff_opt_compact_summary
),
5544 OPT_CALLBACK_F(0, "binary", options
, NULL
,
5545 N_("output a binary diff that can be applied"),
5546 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_binary
),
5547 OPT_BOOL(0, "full-index", &options
->flags
.full_index
,
5548 N_("show full pre- and post-image object names on the \"index\" lines")),
5549 OPT_COLOR_FLAG(0, "color", &options
->use_color
,
5550 N_("show colored diff")),
5551 OPT_CALLBACK_F(0, "ws-error-highlight", options
, N_("<kind>"),
5552 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5553 PARSE_OPT_NONEG
, diff_opt_ws_error_highlight
),
5554 OPT_SET_INT('z', NULL
, &options
->line_termination
,
5555 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5557 OPT__ABBREV(&options
->abbrev
),
5558 OPT_STRING_F(0, "src-prefix", &options
->a_prefix
, N_("<prefix>"),
5559 N_("show the given source prefix instead of \"a/\""),
5561 OPT_STRING_F(0, "dst-prefix", &options
->b_prefix
, N_("<prefix>"),
5562 N_("show the given destination prefix instead of \"b/\""),
5564 OPT_CALLBACK_F(0, "line-prefix", options
, N_("<prefix>"),
5565 N_("prepend an additional prefix to every line of output"),
5566 PARSE_OPT_NONEG
, diff_opt_line_prefix
),
5567 OPT_CALLBACK_F(0, "no-prefix", options
, NULL
,
5568 N_("do not show any source or destination prefix"),
5569 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_no_prefix
),
5570 OPT_INTEGER_F(0, "inter-hunk-context", &options
->interhunkcontext
,
5571 N_("show context between diff hunks up to the specified number of lines"),
5573 OPT_CALLBACK_F(0, "output-indicator-new",
5574 &options
->output_indicators
[OUTPUT_INDICATOR_NEW
],
5576 N_("specify the character to indicate a new line instead of '+'"),
5577 PARSE_OPT_NONEG
, diff_opt_char
),
5578 OPT_CALLBACK_F(0, "output-indicator-old",
5579 &options
->output_indicators
[OUTPUT_INDICATOR_OLD
],
5581 N_("specify the character to indicate an old line instead of '-'"),
5582 PARSE_OPT_NONEG
, diff_opt_char
),
5583 OPT_CALLBACK_F(0, "output-indicator-context",
5584 &options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
],
5586 N_("specify the character to indicate a context instead of ' '"),
5587 PARSE_OPT_NONEG
, diff_opt_char
),
5589 OPT_GROUP(N_("Diff rename options")),
5590 OPT_CALLBACK_F('B', "break-rewrites", &options
->break_opt
, N_("<n>[/<m>]"),
5591 N_("break complete rewrite changes into pairs of delete and create"),
5592 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5593 diff_opt_break_rewrites
),
5594 OPT_CALLBACK_F('M', "find-renames", options
, N_("<n>"),
5595 N_("detect renames"),
5596 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5597 diff_opt_find_renames
),
5598 OPT_SET_INT_F('D', "irreversible-delete", &options
->irreversible_delete
,
5599 N_("omit the preimage for deletes"),
5600 1, PARSE_OPT_NONEG
),
5601 OPT_CALLBACK_F('C', "find-copies", options
, N_("<n>"),
5602 N_("detect copies"),
5603 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5604 diff_opt_find_copies
),
5605 OPT_BOOL(0, "find-copies-harder", &options
->flags
.find_copies_harder
,
5606 N_("use unmodified files as source to find copies")),
5607 OPT_SET_INT_F(0, "no-renames", &options
->detect_rename
,
5608 N_("disable rename detection"),
5609 0, PARSE_OPT_NONEG
),
5610 OPT_BOOL(0, "rename-empty", &options
->flags
.rename_empty
,
5611 N_("use empty blobs as rename source")),
5612 OPT_CALLBACK_F(0, "follow", options
, NULL
,
5613 N_("continue listing the history of a file beyond renames"),
5614 PARSE_OPT_NOARG
, diff_opt_follow
),
5615 OPT_INTEGER('l', NULL
, &options
->rename_limit
,
5616 N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
5618 OPT_GROUP(N_("Diff algorithm options")),
5619 OPT_CALLBACK_F(0, "minimal", options
, NULL
,
5620 N_("produce the smallest possible diff"),
5621 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5622 diff_opt_diff_algorithm_no_arg
),
5623 OPT_BIT_F('w', "ignore-all-space", &options
->xdl_opts
,
5624 N_("ignore whitespace when comparing lines"),
5625 XDF_IGNORE_WHITESPACE
, PARSE_OPT_NONEG
),
5626 OPT_BIT_F('b', "ignore-space-change", &options
->xdl_opts
,
5627 N_("ignore changes in amount of whitespace"),
5628 XDF_IGNORE_WHITESPACE_CHANGE
, PARSE_OPT_NONEG
),
5629 OPT_BIT_F(0, "ignore-space-at-eol", &options
->xdl_opts
,
5630 N_("ignore changes in whitespace at EOL"),
5631 XDF_IGNORE_WHITESPACE_AT_EOL
, PARSE_OPT_NONEG
),
5632 OPT_BIT_F(0, "ignore-cr-at-eol", &options
->xdl_opts
,
5633 N_("ignore carrier-return at the end of line"),
5634 XDF_IGNORE_CR_AT_EOL
, PARSE_OPT_NONEG
),
5635 OPT_BIT_F(0, "ignore-blank-lines", &options
->xdl_opts
,
5636 N_("ignore changes whose lines are all blank"),
5637 XDF_IGNORE_BLANK_LINES
, PARSE_OPT_NONEG
),
5638 OPT_CALLBACK_F('I', "ignore-matching-lines", options
, N_("<regex>"),
5639 N_("ignore changes whose all lines match <regex>"),
5640 0, diff_opt_ignore_regex
),
5641 OPT_BIT(0, "indent-heuristic", &options
->xdl_opts
,
5642 N_("heuristic to shift diff hunk boundaries for easy reading"),
5643 XDF_INDENT_HEURISTIC
),
5644 OPT_CALLBACK_F(0, "patience", options
, NULL
,
5645 N_("generate diff using the \"patience diff\" algorithm"),
5646 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5648 OPT_CALLBACK_F(0, "histogram", options
, NULL
,
5649 N_("generate diff using the \"histogram diff\" algorithm"),
5650 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5651 diff_opt_diff_algorithm_no_arg
),
5652 OPT_CALLBACK_F(0, "diff-algorithm", options
, N_("<algorithm>"),
5653 N_("choose a diff algorithm"),
5654 PARSE_OPT_NONEG
, diff_opt_diff_algorithm
),
5655 OPT_CALLBACK_F(0, "anchored", options
, N_("<text>"),
5656 N_("generate diff using the \"anchored diff\" algorithm"),
5657 PARSE_OPT_NONEG
, diff_opt_anchored
),
5658 OPT_CALLBACK_F(0, "word-diff", options
, N_("<mode>"),
5659 N_("show word diff, using <mode> to delimit changed words"),
5660 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_word_diff
),
5661 OPT_CALLBACK_F(0, "word-diff-regex", options
, N_("<regex>"),
5662 N_("use <regex> to decide what a word is"),
5663 PARSE_OPT_NONEG
, diff_opt_word_diff_regex
),
5664 OPT_CALLBACK_F(0, "color-words", options
, N_("<regex>"),
5665 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5666 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_color_words
),
5667 OPT_CALLBACK_F(0, "color-moved", options
, N_("<mode>"),
5668 N_("moved lines of code are colored differently"),
5669 PARSE_OPT_OPTARG
, diff_opt_color_moved
),
5670 OPT_CALLBACK_F(0, "color-moved-ws", options
, N_("<mode>"),
5671 N_("how white spaces are ignored in --color-moved"),
5672 0, diff_opt_color_moved_ws
),
5674 OPT_GROUP(N_("Other diff options")),
5675 OPT_CALLBACK_F(0, "relative", options
, N_("<prefix>"),
5676 N_("when run from subdir, exclude changes outside and show relative paths"),
5679 OPT_BOOL('a', "text", &options
->flags
.text
,
5680 N_("treat all files as text")),
5681 OPT_BOOL('R', NULL
, &options
->flags
.reverse_diff
,
5682 N_("swap two inputs, reverse the diff")),
5683 OPT_BOOL(0, "exit-code", &options
->flags
.exit_with_status
,
5684 N_("exit with 1 if there were differences, 0 otherwise")),
5685 OPT_BOOL(0, "quiet", &options
->flags
.quick
,
5686 N_("disable all output of the program")),
5687 OPT_BOOL(0, "ext-diff", &options
->flags
.allow_external
,
5688 N_("allow an external diff helper to be executed")),
5689 OPT_CALLBACK_F(0, "textconv", options
, NULL
,
5690 N_("run external text conversion filters when comparing binary files"),
5691 PARSE_OPT_NOARG
, diff_opt_textconv
),
5692 OPT_CALLBACK_F(0, "ignore-submodules", options
, N_("<when>"),
5693 N_("ignore changes to submodules in the diff generation"),
5694 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5695 diff_opt_ignore_submodules
),
5696 OPT_CALLBACK_F(0, "submodule", options
, N_("<format>"),
5697 N_("specify how differences in submodules are shown"),
5698 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5699 diff_opt_submodule
),
5700 OPT_SET_INT_F(0, "ita-invisible-in-index", &options
->ita_invisible_in_index
,
5701 N_("hide 'git add -N' entries from the index"),
5702 1, PARSE_OPT_NONEG
),
5703 OPT_SET_INT_F(0, "ita-visible-in-index", &options
->ita_invisible_in_index
,
5704 N_("treat 'git add -N' entries as real in the index"),
5705 0, PARSE_OPT_NONEG
),
5706 OPT_CALLBACK_F('S', NULL
, options
, N_("<string>"),
5707 N_("look for differences that change the number of occurrences of the specified string"),
5708 0, diff_opt_pickaxe_string
),
5709 OPT_CALLBACK_F('G', NULL
, options
, N_("<regex>"),
5710 N_("look for differences that change the number of occurrences of the specified regex"),
5711 0, diff_opt_pickaxe_regex
),
5712 OPT_BIT_F(0, "pickaxe-all", &options
->pickaxe_opts
,
5713 N_("show all changes in the changeset with -S or -G"),
5714 DIFF_PICKAXE_ALL
, PARSE_OPT_NONEG
),
5715 OPT_BIT_F(0, "pickaxe-regex", &options
->pickaxe_opts
,
5716 N_("treat <string> in -S as extended POSIX regular expression"),
5717 DIFF_PICKAXE_REGEX
, PARSE_OPT_NONEG
),
5718 OPT_FILENAME('O', NULL
, &options
->orderfile
,
5719 N_("control the order in which files appear in the output")),
5720 OPT_CALLBACK_F(0, "rotate-to", options
, N_("<path>"),
5721 N_("show the change in the specified path first"),
5722 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5723 OPT_CALLBACK_F(0, "skip-to", options
, N_("<path>"),
5724 N_("skip the output to the specified path"),
5725 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5726 OPT_CALLBACK_F(0, "find-object", options
, N_("<object-id>"),
5727 N_("look for differences that change the number of occurrences of the specified object"),
5728 PARSE_OPT_NONEG
, diff_opt_find_object
),
5729 OPT_CALLBACK_F(0, "diff-filter", options
, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5730 N_("select files by diff type"),
5731 PARSE_OPT_NONEG
, diff_opt_diff_filter
),
5732 { OPTION_CALLBACK
, 0, "output", options
, N_("<file>"),
5733 N_("output to a specific file"),
5734 PARSE_OPT_NONEG
, NULL
, 0, diff_opt_output
},
5739 return parse_options_concat(opts
, parseopts
);
5742 int diff_opt_parse(struct diff_options
*options
,
5743 const char **av
, int ac
, const char *prefix
)
5745 struct option no_options
[] = { OPT_END() };
5746 struct option
*parseopts
= add_diff_options(no_options
, options
);
5751 ac
= parse_options(ac
, av
, prefix
, parseopts
, NULL
,
5752 PARSE_OPT_KEEP_DASHDASH
|
5753 PARSE_OPT_KEEP_UNKNOWN_OPT
|
5754 PARSE_OPT_NO_INTERNAL_HELP
|
5755 PARSE_OPT_ONE_SHOT
|
5756 PARSE_OPT_STOP_AT_NON_OPTION
);
5762 int parse_rename_score(const char **cp_p
)
5764 unsigned long num
, scale
;
5766 const char *cp
= *cp_p
;
5773 if ( !dot
&& ch
== '.' ) {
5776 } else if ( ch
== '%' ) {
5777 scale
= dot
? scale
*100 : 100;
5778 cp
++; /* % is always at the end */
5780 } else if ( ch
>= '0' && ch
<= '9' ) {
5781 if ( scale
< 100000 ) {
5783 num
= (num
*10) + (ch
-'0');
5792 /* user says num divided by scale and we say internally that
5793 * is MAX_SCORE * num / scale.
5795 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
5798 struct diff_queue_struct diff_queued_diff
;
5800 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
5802 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
5803 queue
->queue
[queue
->nr
++] = dp
;
5806 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
5807 struct diff_filespec
*one
,
5808 struct diff_filespec
*two
)
5810 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
5818 void diff_free_filepair(struct diff_filepair
*p
)
5820 free_filespec(p
->one
);
5821 free_filespec(p
->two
);
5825 void diff_free_queue(struct diff_queue_struct
*q
)
5827 for (int i
= 0; i
< q
->nr
; i
++)
5828 diff_free_filepair(q
->queue
[i
]);
5832 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
5837 /* Do we want all 40 hex characters? */
5838 if (len
== the_hash_algo
->hexsz
)
5839 return oid_to_hex(oid
);
5841 /* An abbreviated value is fine, possibly followed by an ellipsis. */
5842 abbrev
= diff_abbrev_oid(oid
, len
);
5844 if (!print_sha1_ellipsis())
5847 abblen
= strlen(abbrev
);
5850 * In well-behaved cases, where the abbreviated result is the
5851 * same as the requested length, append three dots after the
5852 * abbreviation (hence the whole logic is limited to the case
5853 * where abblen < 37); when the actual abbreviated result is a
5854 * bit longer than the requested length, we reduce the number
5855 * of dots so that they match the well-behaved ones. However,
5856 * if the actual abbreviation is longer than the requested
5857 * length by more than three, we give up on aligning, and add
5858 * three dots anyway, to indicate that the output is not the
5859 * full object name. Yes, this may be suboptimal, but this
5860 * appears only in "diff --raw --abbrev" output and it is not
5861 * worth the effort to change it now. Note that this would
5862 * likely to work fine when the automatic sizing of default
5863 * abbreviation length is used--we would be fed -1 in "len" in
5864 * that case, and will end up always appending three-dots, but
5865 * the automatic sizing is supposed to give abblen that ensures
5866 * uniqueness across all objects (statistically speaking).
5868 if (abblen
< the_hash_algo
->hexsz
- 3) {
5869 static char hex
[GIT_MAX_HEXSZ
+ 1];
5870 if (len
< abblen
&& abblen
<= len
+ 2)
5871 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
5873 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
5877 return oid_to_hex(oid
);
5880 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
5882 int line_termination
= opt
->line_termination
;
5883 int inter_name_termination
= line_termination
? '\t' : '\0';
5885 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5886 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
5887 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
5888 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
5889 fprintf(opt
->file
, "%s ",
5890 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
5893 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
5894 inter_name_termination
);
5896 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
5899 if (p
->status
== DIFF_STATUS_COPIED
||
5900 p
->status
== DIFF_STATUS_RENAMED
) {
5901 const char *name_a
, *name_b
;
5902 name_a
= p
->one
->path
;
5903 name_b
= p
->two
->path
;
5904 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5905 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
5906 write_name_quoted(name_b
, opt
->file
, line_termination
);
5908 const char *name_a
, *name_b
;
5909 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
5911 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5912 write_name_quoted(name_a
, opt
->file
, line_termination
);
5916 int diff_unmodified_pair(struct diff_filepair
*p
)
5918 /* This function is written stricter than necessary to support
5919 * the currently implemented transformers, but the idea is to
5920 * let transformers to produce diff_filepairs any way they want,
5921 * and filter and clean them up here before producing the output.
5923 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
5925 if (DIFF_PAIR_UNMERGED(p
))
5926 return 0; /* unmerged is interesting */
5928 /* deletion, addition, mode or type change
5929 * and rename are all interesting.
5931 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
5932 DIFF_PAIR_MODE_CHANGED(p
) ||
5933 strcmp(one
->path
, two
->path
))
5936 /* both are valid and point at the same path. that is, we are
5937 * dealing with a change.
5939 if (one
->oid_valid
&& two
->oid_valid
&&
5940 oideq(&one
->oid
, &two
->oid
) &&
5941 !one
->dirty_submodule
&& !two
->dirty_submodule
)
5942 return 1; /* no change */
5943 if (!one
->oid_valid
&& !two
->oid_valid
)
5944 return 1; /* both look at the same file on the filesystem. */
5948 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
5950 int include_conflict_headers
=
5951 (additional_headers(o
, p
->one
->path
) &&
5953 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
5956 * Check if we can return early without showing a diff. Note that
5957 * diff_filepair only stores {oid, path, mode, is_valid}
5958 * information for each path, and thus diff_unmodified_pair() only
5959 * considers those bits of info. However, we do not want pairs
5960 * created by create_filepairs_for_header_only_notifications()
5961 * (which always look like unmodified pairs) to be ignored, so
5962 * return early if both p is unmodified AND we don't want to
5963 * include_conflict_headers.
5965 if (diff_unmodified_pair(p
) && !include_conflict_headers
)
5968 /* Actually, we can also return early to avoid showing tree diffs */
5969 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5970 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5976 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
5977 struct diffstat_t
*diffstat
)
5979 if (diff_unmodified_pair(p
))
5982 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5983 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5984 return; /* no useful stat for tree diffs */
5986 run_diffstat(p
, o
, diffstat
);
5989 static void diff_flush_checkdiff(struct diff_filepair
*p
,
5990 struct diff_options
*o
)
5992 if (diff_unmodified_pair(p
))
5995 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5996 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5997 return; /* nothing to check in tree diffs */
5999 run_checkdiff(p
, o
);
6002 int diff_queue_is_empty(struct diff_options
*o
)
6004 struct diff_queue_struct
*q
= &diff_queued_diff
;
6006 int include_conflict_headers
=
6007 (o
->additional_path_headers
&&
6008 strmap_get_size(o
->additional_path_headers
) &&
6010 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
6012 if (include_conflict_headers
)
6015 for (i
= 0; i
< q
->nr
; i
++)
6016 if (!diff_unmodified_pair(q
->queue
[i
]))
6022 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
6024 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
6027 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
6029 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
6030 fprintf(stderr
, "queue[%d] %s size %lu\n",
6035 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
6037 diff_debug_filespec(p
->one
, i
, "one");
6038 diff_debug_filespec(p
->two
, i
, "two");
6039 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
6040 p
->score
, p
->status
? p
->status
: '?',
6041 p
->one
->rename_used
, p
->broken_pair
);
6044 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
6048 fprintf(stderr
, "%s\n", msg
);
6049 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
6050 for (i
= 0; i
< q
->nr
; i
++) {
6051 struct diff_filepair
*p
= q
->queue
[i
];
6052 diff_debug_filepair(p
, i
);
6057 static void diff_resolve_rename_copy(void)
6060 struct diff_filepair
*p
;
6061 struct diff_queue_struct
*q
= &diff_queued_diff
;
6063 diff_debug_queue("resolve-rename-copy", q
);
6065 for (i
= 0; i
< q
->nr
; i
++) {
6067 p
->status
= 0; /* undecided */
6068 if (DIFF_PAIR_UNMERGED(p
))
6069 p
->status
= DIFF_STATUS_UNMERGED
;
6070 else if (!DIFF_FILE_VALID(p
->one
))
6071 p
->status
= DIFF_STATUS_ADDED
;
6072 else if (!DIFF_FILE_VALID(p
->two
))
6073 p
->status
= DIFF_STATUS_DELETED
;
6074 else if (DIFF_PAIR_TYPE_CHANGED(p
))
6075 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
6077 /* from this point on, we are dealing with a pair
6078 * whose both sides are valid and of the same type, i.e.
6079 * either in-place edit or rename/copy edit.
6081 else if (DIFF_PAIR_RENAME(p
)) {
6083 * A rename might have re-connected a broken
6084 * pair up, causing the pathnames to be the
6085 * same again. If so, that's not a rename at
6086 * all, just a modification..
6088 * Otherwise, see if this source was used for
6089 * multiple renames, in which case we decrement
6090 * the count, and call it a copy.
6092 if (!strcmp(p
->one
->path
, p
->two
->path
))
6093 p
->status
= DIFF_STATUS_MODIFIED
;
6094 else if (--p
->one
->rename_used
> 0)
6095 p
->status
= DIFF_STATUS_COPIED
;
6097 p
->status
= DIFF_STATUS_RENAMED
;
6099 else if (!oideq(&p
->one
->oid
, &p
->two
->oid
) ||
6100 p
->one
->mode
!= p
->two
->mode
||
6101 p
->one
->dirty_submodule
||
6102 p
->two
->dirty_submodule
||
6103 is_null_oid(&p
->one
->oid
))
6104 p
->status
= DIFF_STATUS_MODIFIED
;
6106 /* This is a "no-change" entry and should not
6107 * happen anymore, but prepare for broken callers.
6109 error("feeding unmodified %s to diffcore",
6111 p
->status
= DIFF_STATUS_UNKNOWN
;
6114 diff_debug_queue("resolve-rename-copy done", q
);
6117 static int check_pair_status(struct diff_filepair
*p
)
6119 switch (p
->status
) {
6120 case DIFF_STATUS_UNKNOWN
:
6123 die("internal error in diff-resolve-rename-copy");
6129 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
6131 int fmt
= opt
->output_format
;
6133 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
6134 diff_flush_checkdiff(p
, opt
);
6135 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
6136 diff_flush_raw(p
, opt
);
6137 else if (fmt
& DIFF_FORMAT_NAME
) {
6138 const char *name_a
, *name_b
;
6139 name_a
= p
->two
->path
;
6141 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6142 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
6143 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
6147 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
6149 struct strbuf sb
= STRBUF_INIT
;
6151 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
6153 strbuf_addf(&sb
, " %s ", newdelete
);
6155 quote_c_style(fs
->path
, &sb
, NULL
, 0);
6156 strbuf_addch(&sb
, '\n');
6157 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6159 strbuf_release(&sb
);
6162 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
6165 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
6166 struct strbuf sb
= STRBUF_INIT
;
6167 strbuf_addf(&sb
, " mode change %06o => %06o",
6168 p
->one
->mode
, p
->two
->mode
);
6170 strbuf_addch(&sb
, ' ');
6171 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6173 strbuf_addch(&sb
, '\n');
6174 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6176 strbuf_release(&sb
);
6180 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
6181 struct diff_filepair
*p
)
6183 struct strbuf sb
= STRBUF_INIT
;
6184 struct strbuf names
= STRBUF_INIT
;
6186 pprint_rename(&names
, p
->one
->path
, p
->two
->path
);
6187 strbuf_addf(&sb
, " %s %s (%d%%)\n",
6188 renamecopy
, names
.buf
, similarity_index(p
));
6189 strbuf_release(&names
);
6190 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6192 show_mode_change(opt
, p
, 0);
6193 strbuf_release(&sb
);
6196 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
6199 case DIFF_STATUS_DELETED
:
6200 show_file_mode_name(opt
, "delete", p
->one
);
6202 case DIFF_STATUS_ADDED
:
6203 show_file_mode_name(opt
, "create", p
->two
);
6205 case DIFF_STATUS_COPIED
:
6206 show_rename_copy(opt
, "copy", p
);
6208 case DIFF_STATUS_RENAMED
:
6209 show_rename_copy(opt
, "rename", p
);
6213 struct strbuf sb
= STRBUF_INIT
;
6214 strbuf_addstr(&sb
, " rewrite ");
6215 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6216 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
6217 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6219 strbuf_release(&sb
);
6221 show_mode_change(opt
, p
, !p
->score
);
6231 static int remove_space(char *line
, int len
)
6237 for (i
= 0; i
< len
; i
++)
6238 if (!isspace((c
= line
[i
])))
6244 void flush_one_hunk(struct object_id
*result
, git_hash_ctx
*ctx
)
6246 unsigned char hash
[GIT_MAX_RAWSZ
];
6247 unsigned short carry
= 0;
6250 the_hash_algo
->final_fn(hash
, ctx
);
6251 the_hash_algo
->init_fn(ctx
);
6252 /* 20-byte sum, with carry */
6253 for (i
= 0; i
< the_hash_algo
->rawsz
; ++i
) {
6254 carry
+= result
->hash
[i
] + hash
[i
];
6255 result
->hash
[i
] = carry
;
6260 static int patch_id_consume(void *priv
, char *line
, unsigned long len
)
6262 struct patch_id_t
*data
= priv
;
6265 if (len
> 12 && starts_with(line
, "\\ "))
6267 new_len
= remove_space(line
, len
);
6269 the_hash_algo
->update_fn(data
->ctx
, line
, new_len
);
6270 data
->patchlen
+= new_len
;
6274 static void patch_id_add_string(git_hash_ctx
*ctx
, const char *str
)
6276 the_hash_algo
->update_fn(ctx
, str
, strlen(str
));
6279 static void patch_id_add_mode(git_hash_ctx
*ctx
, unsigned mode
)
6281 /* large enough for 2^32 in octal */
6283 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
6284 the_hash_algo
->update_fn(ctx
, buf
, len
);
6287 /* returns 0 upon success, and writes result into oid */
6288 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
6290 struct diff_queue_struct
*q
= &diff_queued_diff
;
6293 struct patch_id_t data
;
6295 the_hash_algo
->init_fn(&ctx
);
6296 memset(&data
, 0, sizeof(struct patch_id_t
));
6300 for (i
= 0; i
< q
->nr
; i
++) {
6304 struct diff_filepair
*p
= q
->queue
[i
];
6307 memset(&xpp
, 0, sizeof(xpp
));
6308 memset(&xecfg
, 0, sizeof(xecfg
));
6310 return error("internal diff status error");
6311 if (p
->status
== DIFF_STATUS_UNKNOWN
)
6313 if (diff_unmodified_pair(p
))
6315 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6316 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6318 if (DIFF_PAIR_UNMERGED(p
))
6321 diff_fill_oid_info(p
->one
, options
->repo
->index
);
6322 diff_fill_oid_info(p
->two
, options
->repo
->index
);
6324 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
6325 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
6326 patch_id_add_string(&ctx
, "diff--git");
6327 patch_id_add_string(&ctx
, "a/");
6328 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6329 patch_id_add_string(&ctx
, "b/");
6330 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6332 if (p
->one
->mode
== 0) {
6333 patch_id_add_string(&ctx
, "newfilemode");
6334 patch_id_add_mode(&ctx
, p
->two
->mode
);
6335 } else if (p
->two
->mode
== 0) {
6336 patch_id_add_string(&ctx
, "deletedfilemode");
6337 patch_id_add_mode(&ctx
, p
->one
->mode
);
6338 } else if (p
->one
->mode
!= p
->two
->mode
) {
6339 patch_id_add_string(&ctx
, "oldmode");
6340 patch_id_add_mode(&ctx
, p
->one
->mode
);
6341 patch_id_add_string(&ctx
, "newmode");
6342 patch_id_add_mode(&ctx
, p
->two
->mode
);
6345 if (diff_header_only
) {
6346 /* don't do anything since we're only populating header info */
6347 } else if (diff_filespec_is_binary(options
->repo
, p
->one
) ||
6348 diff_filespec_is_binary(options
->repo
, p
->two
)) {
6349 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->one
->oid
),
6350 the_hash_algo
->hexsz
);
6351 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->two
->oid
),
6352 the_hash_algo
->hexsz
);
6354 if (p
->one
->mode
== 0) {
6355 patch_id_add_string(&ctx
, "---/dev/null");
6356 patch_id_add_string(&ctx
, "+++b/");
6357 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6358 } else if (p
->two
->mode
== 0) {
6359 patch_id_add_string(&ctx
, "---a/");
6360 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6361 patch_id_add_string(&ctx
, "+++/dev/null");
6363 patch_id_add_string(&ctx
, "---a/");
6364 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6365 patch_id_add_string(&ctx
, "+++b/");
6366 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6369 if (fill_mmfile(options
->repo
, &mf1
, p
->one
) < 0 ||
6370 fill_mmfile(options
->repo
, &mf2
, p
->two
) < 0)
6371 return error("unable to read files to diff");
6374 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
6375 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
6376 patch_id_consume
, &data
, &xpp
, &xecfg
))
6377 return error("unable to generate patch-id diff for %s",
6380 flush_one_hunk(oid
, &ctx
);
6386 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
6388 struct diff_queue_struct
*q
= &diff_queued_diff
;
6389 int result
= diff_get_patch_id(options
, oid
, diff_header_only
);
6392 DIFF_QUEUE_CLEAR(q
);
6397 static int is_summary_empty(const struct diff_queue_struct
*q
)
6401 for (i
= 0; i
< q
->nr
; i
++) {
6402 const struct diff_filepair
*p
= q
->queue
[i
];
6404 switch (p
->status
) {
6405 case DIFF_STATUS_DELETED
:
6406 case DIFF_STATUS_ADDED
:
6407 case DIFF_STATUS_COPIED
:
6408 case DIFF_STATUS_RENAMED
:
6413 if (p
->one
->mode
&& p
->two
->mode
&&
6414 p
->one
->mode
!= p
->two
->mode
)
6422 static const char rename_limit_warning
[] =
6423 N_("exhaustive rename detection was skipped due to too many files.");
6425 static const char degrade_cc_to_c_warning
[] =
6426 N_("only found copies from modified paths due to too many files.");
6428 static const char rename_limit_advice
[] =
6429 N_("you may want to set your %s variable to at least "
6430 "%d and retry the command.");
6432 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
6436 warning(_(degrade_cc_to_c_warning
));
6438 warning(_(rename_limit_warning
));
6442 warning(_(rename_limit_advice
), varname
, needed
);
6445 static void create_filepairs_for_header_only_notifications(struct diff_options
*o
)
6447 struct strset present
;
6448 struct diff_queue_struct
*q
= &diff_queued_diff
;
6449 struct hashmap_iter iter
;
6450 struct strmap_entry
*e
;
6453 strset_init_with_options(&present
, /*pool*/ NULL
, /*strdup*/ 0);
6456 * Find out which paths exist in diff_queued_diff, preferring
6457 * one->path for any pair that has multiple paths.
6459 for (i
= 0; i
< q
->nr
; i
++) {
6460 struct diff_filepair
*p
= q
->queue
[i
];
6461 char *path
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
6463 if (strmap_contains(o
->additional_path_headers
, path
))
6464 strset_add(&present
, path
);
6468 * Loop over paths in additional_path_headers; for each NOT already
6469 * in diff_queued_diff, create a synthetic filepair and insert that
6470 * into diff_queued_diff.
6472 strmap_for_each_entry(o
->additional_path_headers
, &iter
, e
) {
6473 if (!strset_contains(&present
, e
->key
)) {
6474 struct diff_filespec
*one
, *two
;
6475 struct diff_filepair
*p
;
6477 one
= alloc_filespec(e
->key
);
6478 two
= alloc_filespec(e
->key
);
6479 fill_filespec(one
, null_oid(), 0, 0);
6480 fill_filespec(two
, null_oid(), 0, 0);
6481 p
= diff_queue(q
, one
, two
);
6482 p
->status
= DIFF_STATUS_MODIFIED
;
6486 /* Re-sort the filepairs */
6487 diffcore_fix_diff_index();
6490 strset_clear(&present
);
6493 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
6496 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
6497 struct diff_queue_struct
*q
= &diff_queued_diff
;
6499 if (WSEH_NEW
& WS_RULE_MASK
)
6500 BUG("WS rules bit mask overlaps with diff symbol flags");
6503 o
->emitted_symbols
= &esm
;
6505 if (o
->additional_path_headers
)
6506 create_filepairs_for_header_only_notifications(o
);
6508 for (i
= 0; i
< q
->nr
; i
++) {
6509 struct diff_filepair
*p
= q
->queue
[i
];
6510 if (check_pair_status(p
))
6511 diff_flush_patch(p
, o
);
6514 if (o
->emitted_symbols
) {
6515 if (o
->color_moved
) {
6516 struct mem_pool entry_pool
;
6517 struct moved_entry_list
*entry_list
;
6519 mem_pool_init(&entry_pool
, 1024 * 1024);
6520 entry_list
= add_lines_to_move_detection(o
,
6522 mark_color_as_moved(o
, entry_list
);
6523 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
6526 mem_pool_discard(&entry_pool
, 0);
6530 for (i
= 0; i
< esm
.nr
; i
++)
6531 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
6533 for (i
= 0; i
< esm
.nr
; i
++)
6534 free((void *)esm
.buf
[i
].line
);
6537 o
->emitted_symbols
= NULL
;
6541 static void diff_free_file(struct diff_options
*options
)
6543 if (options
->close_file
)
6544 fclose(options
->file
);
6547 static void diff_free_ignore_regex(struct diff_options
*options
)
6551 for (i
= 0; i
< options
->ignore_regex_nr
; i
++) {
6552 regfree(options
->ignore_regex
[i
]);
6553 free(options
->ignore_regex
[i
]);
6555 free(options
->ignore_regex
);
6558 void diff_free(struct diff_options
*options
)
6560 if (options
->no_free
)
6563 diff_free_file(options
);
6564 diff_free_ignore_regex(options
);
6565 clear_pathspec(&options
->pathspec
);
6568 void diff_flush(struct diff_options
*options
)
6570 struct diff_queue_struct
*q
= &diff_queued_diff
;
6571 int i
, output_format
= options
->output_format
;
6573 int dirstat_by_line
= 0;
6576 * Order: raw, stat, summary, patch
6577 * or: name/name-status/checkdiff (other bits clear)
6579 if (!q
->nr
&& !options
->additional_path_headers
)
6582 if (output_format
& (DIFF_FORMAT_RAW
|
6584 DIFF_FORMAT_NAME_STATUS
|
6585 DIFF_FORMAT_CHECKDIFF
)) {
6586 for (i
= 0; i
< q
->nr
; i
++) {
6587 struct diff_filepair
*p
= q
->queue
[i
];
6588 if (check_pair_status(p
))
6589 flush_one_pair(p
, options
);
6594 if (output_format
& DIFF_FORMAT_DIRSTAT
&& options
->flags
.dirstat_by_line
)
6595 dirstat_by_line
= 1;
6597 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
6599 struct diffstat_t diffstat
;
6601 compute_diffstat(options
, &diffstat
, q
);
6602 if (output_format
& DIFF_FORMAT_NUMSTAT
)
6603 show_numstat(&diffstat
, options
);
6604 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
6605 show_stats(&diffstat
, options
);
6606 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
6607 show_shortstats(&diffstat
, options
);
6608 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
6609 show_dirstat_by_line(&diffstat
, options
);
6610 free_diffstat_info(&diffstat
);
6613 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
6614 show_dirstat(options
);
6616 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
6617 for (i
= 0; i
< q
->nr
; i
++) {
6618 diff_summary(options
, q
->queue
[i
]);
6623 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
6624 options
->flags
.exit_with_status
&&
6625 options
->flags
.diff_from_contents
) {
6627 * run diff_flush_patch for the exit status. setting
6628 * options->file to /dev/null should be safe, because we
6629 * aren't supposed to produce any output anyway.
6631 diff_free_file(options
);
6632 options
->file
= xfopen("/dev/null", "w");
6633 options
->close_file
= 1;
6634 options
->color_moved
= 0;
6635 for (i
= 0; i
< q
->nr
; i
++) {
6636 struct diff_filepair
*p
= q
->queue
[i
];
6637 if (check_pair_status(p
))
6638 diff_flush_patch(p
, options
);
6639 if (options
->found_changes
)
6644 if (output_format
& DIFF_FORMAT_PATCH
) {
6646 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
6647 if (options
->stat_sep
)
6648 /* attach patch instead of inline */
6649 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
6653 diff_flush_patch_all_file_pairs(options
);
6656 if (output_format
& DIFF_FORMAT_CALLBACK
)
6657 options
->format_callback(q
, options
, options
->format_callback_data
);
6661 DIFF_QUEUE_CLEAR(q
);
6665 * Report the content-level differences with HAS_CHANGES;
6666 * diff_addremove/diff_change does not set the bit when
6667 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6669 if (options
->flags
.diff_from_contents
) {
6670 if (options
->found_changes
)
6671 options
->flags
.has_changes
= 1;
6673 options
->flags
.has_changes
= 0;
6677 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
6679 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
6681 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
6683 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
6684 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
6685 filter_bit_tst(p
->status
, options
)));
6688 static void diffcore_apply_filter(struct diff_options
*options
)
6691 struct diff_queue_struct
*q
= &diff_queued_diff
;
6692 struct diff_queue_struct outq
;
6694 DIFF_QUEUE_CLEAR(&outq
);
6696 if (!options
->filter
)
6699 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
6701 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
6702 if (match_filter(options
, q
->queue
[i
]))
6708 /* otherwise we will clear the whole queue
6709 * by copying the empty outq at the end of this
6710 * function, but first clear the current entries
6713 for (i
= 0; i
< q
->nr
; i
++)
6714 diff_free_filepair(q
->queue
[i
]);
6717 /* Only the matching ones */
6718 for (i
= 0; i
< q
->nr
; i
++) {
6719 struct diff_filepair
*p
= q
->queue
[i
];
6720 if (match_filter(options
, p
))
6723 diff_free_filepair(p
);
6730 /* Check whether two filespecs with the same mode and size are identical */
6731 static int diff_filespec_is_identical(struct repository
*r
,
6732 struct diff_filespec
*one
,
6733 struct diff_filespec
*two
)
6735 if (S_ISGITLINK(one
->mode
))
6737 if (diff_populate_filespec(r
, one
, NULL
))
6739 if (diff_populate_filespec(r
, two
, NULL
))
6741 return !memcmp(one
->data
, two
->data
, one
->size
);
6744 static int diff_filespec_check_stat_unmatch(struct repository
*r
,
6745 struct diff_filepair
*p
)
6747 struct diff_populate_filespec_options dpf_options
= {
6748 .check_size_only
= 1,
6749 .missing_object_cb
= diff_queued_diff_prefetch
,
6750 .missing_object_data
= r
,
6753 if (p
->done_skip_stat_unmatch
)
6754 return p
->skip_stat_unmatch_result
;
6756 p
->done_skip_stat_unmatch
= 1;
6757 p
->skip_stat_unmatch_result
= 0;
6759 * 1. Entries that come from stat info dirtiness
6760 * always have both sides (iow, not create/delete),
6761 * one side of the object name is unknown, with
6762 * the same mode and size. Keep the ones that
6763 * do not match these criteria. They have real
6766 * 2. At this point, the file is known to be modified,
6767 * with the same mode and size, and the object
6768 * name of one side is unknown. Need to inspect
6769 * the identical contents.
6771 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
6772 !DIFF_FILE_VALID(p
->two
) ||
6773 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
6774 (p
->one
->mode
!= p
->two
->mode
) ||
6775 diff_populate_filespec(r
, p
->one
, &dpf_options
) ||
6776 diff_populate_filespec(r
, p
->two
, &dpf_options
) ||
6777 (p
->one
->size
!= p
->two
->size
) ||
6778 !diff_filespec_is_identical(r
, p
->one
, p
->two
)) /* (2) */
6779 p
->skip_stat_unmatch_result
= 1;
6780 return p
->skip_stat_unmatch_result
;
6783 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
6786 struct diff_queue_struct
*q
= &diff_queued_diff
;
6787 struct diff_queue_struct outq
;
6788 DIFF_QUEUE_CLEAR(&outq
);
6790 for (i
= 0; i
< q
->nr
; i
++) {
6791 struct diff_filepair
*p
= q
->queue
[i
];
6793 if (diff_filespec_check_stat_unmatch(diffopt
->repo
, p
))
6797 * The caller can subtract 1 from skip_stat_unmatch
6798 * to determine how many paths were dirty only
6799 * due to stat info mismatch.
6801 if (!diffopt
->flags
.no_index
)
6802 diffopt
->skip_stat_unmatch
++;
6803 diff_free_filepair(p
);
6810 static int diffnamecmp(const void *a_
, const void *b_
)
6812 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
6813 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
6814 const char *name_a
, *name_b
;
6816 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
6817 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
6818 return strcmp(name_a
, name_b
);
6821 void diffcore_fix_diff_index(void)
6823 struct diff_queue_struct
*q
= &diff_queued_diff
;
6824 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
6827 void diff_add_if_missing(struct repository
*r
,
6828 struct oid_array
*to_fetch
,
6829 const struct diff_filespec
*filespec
)
6831 if (filespec
&& filespec
->oid_valid
&&
6832 !S_ISGITLINK(filespec
->mode
) &&
6833 oid_object_info_extended(r
, &filespec
->oid
, NULL
,
6834 OBJECT_INFO_FOR_PREFETCH
))
6835 oid_array_append(to_fetch
, &filespec
->oid
);
6838 void diff_queued_diff_prefetch(void *repository
)
6840 struct repository
*repo
= repository
;
6842 struct diff_queue_struct
*q
= &diff_queued_diff
;
6843 struct oid_array to_fetch
= OID_ARRAY_INIT
;
6845 for (i
= 0; i
< q
->nr
; i
++) {
6846 struct diff_filepair
*p
= q
->queue
[i
];
6847 diff_add_if_missing(repo
, &to_fetch
, p
->one
);
6848 diff_add_if_missing(repo
, &to_fetch
, p
->two
);
6852 * NEEDSWORK: Consider deduplicating the OIDs sent.
6854 promisor_remote_get_direct(repo
, to_fetch
.oid
, to_fetch
.nr
);
6856 oid_array_clear(&to_fetch
);
6859 void diffcore_std(struct diff_options
*options
)
6861 int output_formats_to_prefetch
= DIFF_FORMAT_DIFFSTAT
|
6862 DIFF_FORMAT_NUMSTAT
|
6864 DIFF_FORMAT_SHORTSTAT
|
6865 DIFF_FORMAT_DIRSTAT
;
6868 * Check if the user requested a blob-data-requiring diff output and/or
6869 * break-rewrite detection (which requires blob data). If yes, prefetch
6872 * If no prefetching occurs, diffcore_rename() will prefetch if it
6873 * decides that it needs inexact rename detection.
6875 if (options
->repo
== the_repository
&& repo_has_promisor_remote(the_repository
) &&
6876 (options
->output_format
& output_formats_to_prefetch
||
6877 options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
6878 diff_queued_diff_prefetch(options
->repo
);
6880 /* NOTE please keep the following in sync with diff_tree_combined() */
6881 if (options
->skip_stat_unmatch
)
6882 diffcore_skip_stat_unmatch(options
);
6883 if (!options
->found_follow
) {
6884 /* See try_to_follow_renames() in tree-diff.c */
6885 if (options
->break_opt
!= -1)
6886 diffcore_break(options
->repo
,
6887 options
->break_opt
);
6888 if (options
->detect_rename
)
6889 diffcore_rename(options
);
6890 if (options
->break_opt
!= -1)
6891 diffcore_merge_broken();
6893 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
6894 diffcore_pickaxe(options
);
6895 if (options
->orderfile
)
6896 diffcore_order(options
->orderfile
);
6897 if (options
->rotate_to
)
6898 diffcore_rotate(options
);
6899 if (!options
->found_follow
)
6900 /* See try_to_follow_renames() in tree-diff.c */
6901 diff_resolve_rename_copy();
6902 diffcore_apply_filter(options
);
6904 if (diff_queued_diff
.nr
&& !options
->flags
.diff_from_contents
)
6905 options
->flags
.has_changes
= 1;
6907 options
->flags
.has_changes
= 0;
6909 options
->found_follow
= 0;
6912 int diff_result_code(struct diff_options
*opt
, int status
)
6916 diff_warn_rename_limit("diff.renameLimit",
6917 opt
->needed_rename_limit
,
6918 opt
->degraded_cc_to_c
);
6919 if (!opt
->flags
.exit_with_status
&&
6920 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
6922 if (opt
->flags
.exit_with_status
&&
6923 opt
->flags
.has_changes
)
6925 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
6926 opt
->flags
.check_failed
)
6931 int diff_can_quit_early(struct diff_options
*opt
)
6933 return (opt
->flags
.quick
&&
6935 opt
->flags
.has_changes
);
6939 * Shall changes to this submodule be ignored?
6941 * Submodule changes can be configured to be ignored separately for each path,
6942 * but that configuration can be overridden from the command line.
6944 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
6947 struct diff_flags orig_flags
= options
->flags
;
6948 if (!options
->flags
.override_submodule_config
)
6949 set_diffopt_flags_from_submodule_config(options
, path
);
6950 if (options
->flags
.ignore_submodules
)
6952 options
->flags
= orig_flags
;
6956 void compute_diffstat(struct diff_options
*options
,
6957 struct diffstat_t
*diffstat
,
6958 struct diff_queue_struct
*q
)
6962 memset(diffstat
, 0, sizeof(struct diffstat_t
));
6963 for (i
= 0; i
< q
->nr
; i
++) {
6964 struct diff_filepair
*p
= q
->queue
[i
];
6965 if (check_pair_status(p
))
6966 diff_flush_stat(p
, options
, diffstat
);
6970 void diff_addremove(struct diff_options
*options
,
6971 int addremove
, unsigned mode
,
6972 const struct object_id
*oid
,
6974 const char *concatpath
, unsigned dirty_submodule
)
6976 struct diff_filespec
*one
, *two
;
6978 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
6981 /* This may look odd, but it is a preparation for
6982 * feeding "there are unchanged files which should
6983 * not produce diffs, but when you are doing copy
6984 * detection you would need them, so here they are"
6985 * entries to the diff-core. They will be prefixed
6986 * with something like '=' or '*' (I haven't decided
6987 * which but should not make any difference).
6988 * Feeding the same new and old to diff_change()
6989 * also has the same effect.
6990 * Before the final output happens, they are pruned after
6991 * merged into rename/copy pairs as appropriate.
6993 if (options
->flags
.reverse_diff
)
6994 addremove
= (addremove
== '+' ? '-' :
6995 addremove
== '-' ? '+' : addremove
);
6997 if (options
->prefix
&&
6998 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
7001 one
= alloc_filespec(concatpath
);
7002 two
= alloc_filespec(concatpath
);
7004 if (addremove
!= '+')
7005 fill_filespec(one
, oid
, oid_valid
, mode
);
7006 if (addremove
!= '-') {
7007 fill_filespec(two
, oid
, oid_valid
, mode
);
7008 two
->dirty_submodule
= dirty_submodule
;
7011 diff_queue(&diff_queued_diff
, one
, two
);
7012 if (!options
->flags
.diff_from_contents
)
7013 options
->flags
.has_changes
= 1;
7016 void diff_change(struct diff_options
*options
,
7017 unsigned old_mode
, unsigned new_mode
,
7018 const struct object_id
*old_oid
,
7019 const struct object_id
*new_oid
,
7020 int old_oid_valid
, int new_oid_valid
,
7021 const char *concatpath
,
7022 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
7024 struct diff_filespec
*one
, *two
;
7025 struct diff_filepair
*p
;
7027 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
7028 is_submodule_ignored(concatpath
, options
))
7031 if (options
->flags
.reverse_diff
) {
7032 SWAP(old_mode
, new_mode
);
7033 SWAP(old_oid
, new_oid
);
7034 SWAP(old_oid_valid
, new_oid_valid
);
7035 SWAP(old_dirty_submodule
, new_dirty_submodule
);
7038 if (options
->prefix
&&
7039 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
7042 one
= alloc_filespec(concatpath
);
7043 two
= alloc_filespec(concatpath
);
7044 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
7045 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
7046 one
->dirty_submodule
= old_dirty_submodule
;
7047 two
->dirty_submodule
= new_dirty_submodule
;
7048 p
= diff_queue(&diff_queued_diff
, one
, two
);
7050 if (options
->flags
.diff_from_contents
)
7053 if (options
->flags
.quick
&& options
->skip_stat_unmatch
&&
7054 !diff_filespec_check_stat_unmatch(options
->repo
, p
)) {
7055 diff_free_filespec_data(p
->one
);
7056 diff_free_filespec_data(p
->two
);
7060 options
->flags
.has_changes
= 1;
7063 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
7065 struct diff_filepair
*pair
;
7066 struct diff_filespec
*one
, *two
;
7068 if (options
->prefix
&&
7069 strncmp(path
, options
->prefix
, options
->prefix_length
))
7072 one
= alloc_filespec(path
);
7073 two
= alloc_filespec(path
);
7074 pair
= diff_queue(&diff_queued_diff
, one
, two
);
7075 pair
->is_unmerged
= 1;
7079 static char *run_textconv(struct repository
*r
,
7081 struct diff_filespec
*spec
,
7084 struct diff_tempfile
*temp
;
7085 struct child_process child
= CHILD_PROCESS_INIT
;
7086 struct strbuf buf
= STRBUF_INIT
;
7089 temp
= prepare_temp_file(r
, spec
);
7090 strvec_push(&child
.args
, pgm
);
7091 strvec_push(&child
.args
, temp
->name
);
7093 child
.use_shell
= 1;
7095 if (start_command(&child
)) {
7100 if (strbuf_read(&buf
, child
.out
, 0) < 0)
7101 err
= error("error reading from textconv command '%s'", pgm
);
7104 if (finish_command(&child
) || err
) {
7105 strbuf_release(&buf
);
7111 return strbuf_detach(&buf
, outsize
);
7114 size_t fill_textconv(struct repository
*r
,
7115 struct userdiff_driver
*driver
,
7116 struct diff_filespec
*df
,
7122 if (!DIFF_FILE_VALID(df
)) {
7126 if (diff_populate_filespec(r
, df
, NULL
))
7127 die("unable to read files to diff");
7132 if (!driver
->textconv
)
7133 BUG("fill_textconv called with non-textconv driver");
7135 if (driver
->textconv_cache
&& df
->oid_valid
) {
7136 *outbuf
= notes_cache_get(driver
->textconv_cache
,
7143 *outbuf
= run_textconv(r
, driver
->textconv
, df
, &size
);
7145 die("unable to read files to diff");
7147 if (driver
->textconv_cache
&& df
->oid_valid
) {
7148 /* ignore errors, as we might be in a readonly repository */
7149 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
7152 * we could save up changes and flush them all at the end,
7153 * but we would need an extra call after all diffing is done.
7154 * Since generating a cache entry is the slow path anyway,
7155 * this extra overhead probably isn't a big deal.
7157 notes_cache_write(driver
->textconv_cache
);
7163 int textconv_object(struct repository
*r
,
7166 const struct object_id
*oid
,
7169 unsigned long *buf_size
)
7171 struct diff_filespec
*df
;
7172 struct userdiff_driver
*textconv
;
7174 df
= alloc_filespec(path
);
7175 fill_filespec(df
, oid
, oid_valid
, mode
);
7176 textconv
= get_textconv(r
, df
);
7182 *buf_size
= fill_textconv(r
, textconv
, df
, buf
);
7187 void setup_diff_pager(struct diff_options
*opt
)
7190 * If the user asked for our exit code, then either they want --quiet
7191 * or --exit-code. We should definitely not bother with a pager in the
7192 * former case, as we will generate no output. Since we still properly
7193 * report our exit code even when a pager is run, we _could_ run a
7194 * pager with --exit-code. But since we have not done so historically,
7195 * and because it is easy to find people oneline advising "git diff
7196 * --exit-code" in hooks and other scripts, we do not do so.
7198 if (!opt
->flags
.exit_with_status
&&
7199 check_pager_config("diff") != 0)