2 * Copyright (C) 2005 Junio C Hamano
11 #include "xdiff-interface.h"
14 #include "run-command.h"
16 #include "object-store.h"
18 #include "submodule-config.h"
19 #include "submodule.h"
22 #include "string-list.h"
26 #include "parse-options.h"
28 #include "promisor-remote.h"
30 #ifdef NO_FAST_WORKING_DIRECTORY
31 #define FAST_WORKING_DIRECTORY 0
33 #define FAST_WORKING_DIRECTORY 1
36 static int diff_detect_rename_default
;
37 static int diff_indent_heuristic
= 1;
38 static int diff_rename_limit_default
= 400;
39 static int diff_suppress_blank_empty
;
40 static int diff_use_color_default
= -1;
41 static int diff_color_moved_default
;
42 static int diff_color_moved_ws_default
;
43 static int diff_context_default
= 3;
44 static int diff_interhunk_context_default
;
45 static const char *diff_word_regex_cfg
;
46 static const char *external_diff_cmd_cfg
;
47 static const char *diff_order_file_cfg
;
48 int diff_auto_refresh_index
= 1;
49 static int diff_mnemonic_prefix
;
50 static int diff_no_prefix
;
51 static int diff_relative
;
52 static int diff_stat_graph_width
;
53 static int diff_dirstat_permille_default
= 30;
54 static struct diff_options default_diff_options
;
55 static long diff_algorithm
;
56 static unsigned ws_error_highlight_default
= WSEH_NEW
;
58 static char diff_colors
[][COLOR_MAXLEN
] = {
60 GIT_COLOR_NORMAL
, /* CONTEXT */
61 GIT_COLOR_BOLD
, /* METAINFO */
62 GIT_COLOR_CYAN
, /* FRAGINFO */
63 GIT_COLOR_RED
, /* OLD */
64 GIT_COLOR_GREEN
, /* NEW */
65 GIT_COLOR_YELLOW
, /* COMMIT */
66 GIT_COLOR_BG_RED
, /* WHITESPACE */
67 GIT_COLOR_NORMAL
, /* FUNCINFO */
68 GIT_COLOR_BOLD_MAGENTA
, /* OLD_MOVED */
69 GIT_COLOR_BOLD_BLUE
, /* OLD_MOVED ALTERNATIVE */
70 GIT_COLOR_FAINT
, /* OLD_MOVED_DIM */
71 GIT_COLOR_FAINT_ITALIC
, /* OLD_MOVED_ALTERNATIVE_DIM */
72 GIT_COLOR_BOLD_CYAN
, /* NEW_MOVED */
73 GIT_COLOR_BOLD_YELLOW
, /* NEW_MOVED ALTERNATIVE */
74 GIT_COLOR_FAINT
, /* NEW_MOVED_DIM */
75 GIT_COLOR_FAINT_ITALIC
, /* NEW_MOVED_ALTERNATIVE_DIM */
76 GIT_COLOR_FAINT
, /* CONTEXT_DIM */
77 GIT_COLOR_FAINT_RED
, /* OLD_DIM */
78 GIT_COLOR_FAINT_GREEN
, /* NEW_DIM */
79 GIT_COLOR_BOLD
, /* CONTEXT_BOLD */
80 GIT_COLOR_BOLD_RED
, /* OLD_BOLD */
81 GIT_COLOR_BOLD_GREEN
, /* NEW_BOLD */
84 static const char *color_diff_slots
[] = {
85 [DIFF_CONTEXT
] = "context",
86 [DIFF_METAINFO
] = "meta",
87 [DIFF_FRAGINFO
] = "frag",
88 [DIFF_FILE_OLD
] = "old",
89 [DIFF_FILE_NEW
] = "new",
90 [DIFF_COMMIT
] = "commit",
91 [DIFF_WHITESPACE
] = "whitespace",
92 [DIFF_FUNCINFO
] = "func",
93 [DIFF_FILE_OLD_MOVED
] = "oldMoved",
94 [DIFF_FILE_OLD_MOVED_ALT
] = "oldMovedAlternative",
95 [DIFF_FILE_OLD_MOVED_DIM
] = "oldMovedDimmed",
96 [DIFF_FILE_OLD_MOVED_ALT_DIM
] = "oldMovedAlternativeDimmed",
97 [DIFF_FILE_NEW_MOVED
] = "newMoved",
98 [DIFF_FILE_NEW_MOVED_ALT
] = "newMovedAlternative",
99 [DIFF_FILE_NEW_MOVED_DIM
] = "newMovedDimmed",
100 [DIFF_FILE_NEW_MOVED_ALT_DIM
] = "newMovedAlternativeDimmed",
101 [DIFF_CONTEXT_DIM
] = "contextDimmed",
102 [DIFF_FILE_OLD_DIM
] = "oldDimmed",
103 [DIFF_FILE_NEW_DIM
] = "newDimmed",
104 [DIFF_CONTEXT_BOLD
] = "contextBold",
105 [DIFF_FILE_OLD_BOLD
] = "oldBold",
106 [DIFF_FILE_NEW_BOLD
] = "newBold",
109 define_list_config_array_extra(color_diff_slots
, {"plain"});
111 static int parse_diff_color_slot(const char *var
)
113 if (!strcasecmp(var
, "plain"))
115 return LOOKUP_CONFIG(color_diff_slots
, var
);
118 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
119 struct strbuf
*errmsg
)
121 char *params_copy
= xstrdup(params_string
);
122 struct string_list params
= STRING_LIST_INIT_NODUP
;
127 string_list_split_in_place(¶ms
, params_copy
, ',', -1);
128 for (i
= 0; i
< params
.nr
; i
++) {
129 const char *p
= params
.items
[i
].string
;
130 if (!strcmp(p
, "changes")) {
131 options
->flags
.dirstat_by_line
= 0;
132 options
->flags
.dirstat_by_file
= 0;
133 } else if (!strcmp(p
, "lines")) {
134 options
->flags
.dirstat_by_line
= 1;
135 options
->flags
.dirstat_by_file
= 0;
136 } else if (!strcmp(p
, "files")) {
137 options
->flags
.dirstat_by_line
= 0;
138 options
->flags
.dirstat_by_file
= 1;
139 } else if (!strcmp(p
, "noncumulative")) {
140 options
->flags
.dirstat_cumulative
= 0;
141 } else if (!strcmp(p
, "cumulative")) {
142 options
->flags
.dirstat_cumulative
= 1;
143 } else if (isdigit(*p
)) {
145 int permille
= strtoul(p
, &end
, 10) * 10;
146 if (*end
== '.' && isdigit(*++end
)) {
147 /* only use first digit */
148 permille
+= *end
- '0';
149 /* .. and ignore any further digits */
150 while (isdigit(*++end
))
154 options
->dirstat_permille
= permille
;
156 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
161 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
166 string_list_clear(¶ms
, 0);
171 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
173 if (!strcmp(value
, "log"))
174 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
175 else if (!strcmp(value
, "short"))
176 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
177 else if (!strcmp(value
, "diff"))
178 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
180 * Please update $__git_diff_submodule_formats in
181 * git-completion.bash when you add new formats.
188 int git_config_rename(const char *var
, const char *value
)
191 return DIFF_DETECT_RENAME
;
192 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
193 return DIFF_DETECT_COPY
;
194 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
197 long parse_algorithm_value(const char *value
)
201 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
203 else if (!strcasecmp(value
, "minimal"))
204 return XDF_NEED_MINIMAL
;
205 else if (!strcasecmp(value
, "patience"))
206 return XDF_PATIENCE_DIFF
;
207 else if (!strcasecmp(value
, "histogram"))
208 return XDF_HISTOGRAM_DIFF
;
210 * Please update $__git_diff_algorithms in git-completion.bash
211 * when you add new algorithms.
216 static int parse_one_token(const char **arg
, const char *token
)
219 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
226 static int parse_ws_error_highlight(const char *arg
)
228 const char *orig_arg
= arg
;
232 if (parse_one_token(&arg
, "none"))
234 else if (parse_one_token(&arg
, "default"))
236 else if (parse_one_token(&arg
, "all"))
237 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
238 else if (parse_one_token(&arg
, "new"))
240 else if (parse_one_token(&arg
, "old"))
242 else if (parse_one_token(&arg
, "context"))
245 return -1 - (int)(arg
- orig_arg
);
254 * These are to give UI layer defaults.
255 * The core-level commands such as git-diff-files should
256 * never be affected by the setting of diff.renames
257 * the user happens to have in the configuration file.
259 void init_diff_ui_defaults(void)
261 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
264 int git_diff_heuristic_config(const char *var
, const char *value
, void *cb
)
266 if (!strcmp(var
, "diff.indentheuristic"))
267 diff_indent_heuristic
= git_config_bool(var
, value
);
271 static int parse_color_moved(const char *arg
)
273 switch (git_parse_maybe_bool(arg
)) {
275 return COLOR_MOVED_NO
;
277 return COLOR_MOVED_DEFAULT
;
282 if (!strcmp(arg
, "no"))
283 return COLOR_MOVED_NO
;
284 else if (!strcmp(arg
, "plain"))
285 return COLOR_MOVED_PLAIN
;
286 else if (!strcmp(arg
, "blocks"))
287 return COLOR_MOVED_BLOCKS
;
288 else if (!strcmp(arg
, "zebra"))
289 return COLOR_MOVED_ZEBRA
;
290 else if (!strcmp(arg
, "default"))
291 return COLOR_MOVED_DEFAULT
;
292 else if (!strcmp(arg
, "dimmed-zebra"))
293 return COLOR_MOVED_ZEBRA_DIM
;
294 else if (!strcmp(arg
, "dimmed_zebra"))
295 return COLOR_MOVED_ZEBRA_DIM
;
297 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
300 static unsigned parse_color_moved_ws(const char *arg
)
303 struct string_list l
= STRING_LIST_INIT_DUP
;
304 struct string_list_item
*i
;
306 string_list_split(&l
, arg
, ',', -1);
308 for_each_string_list_item(i
, &l
) {
309 struct strbuf sb
= STRBUF_INIT
;
310 strbuf_addstr(&sb
, i
->string
);
313 if (!strcmp(sb
.buf
, "no"))
315 else if (!strcmp(sb
.buf
, "ignore-space-change"))
316 ret
|= XDF_IGNORE_WHITESPACE_CHANGE
;
317 else if (!strcmp(sb
.buf
, "ignore-space-at-eol"))
318 ret
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
319 else if (!strcmp(sb
.buf
, "ignore-all-space"))
320 ret
|= XDF_IGNORE_WHITESPACE
;
321 else if (!strcmp(sb
.buf
, "allow-indentation-change"))
322 ret
|= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
;
324 ret
|= COLOR_MOVED_WS_ERROR
;
325 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
);
331 if ((ret
& COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
) &&
332 (ret
& XDF_WHITESPACE_FLAGS
)) {
333 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
334 ret
|= COLOR_MOVED_WS_ERROR
;
337 string_list_clear(&l
, 0);
342 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
344 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
345 diff_use_color_default
= git_config_colorbool(var
, value
);
348 if (!strcmp(var
, "diff.colormoved")) {
349 int cm
= parse_color_moved(value
);
352 diff_color_moved_default
= cm
;
355 if (!strcmp(var
, "diff.colormovedws")) {
356 unsigned cm
= parse_color_moved_ws(value
);
357 if (cm
& COLOR_MOVED_WS_ERROR
)
359 diff_color_moved_ws_default
= cm
;
362 if (!strcmp(var
, "diff.context")) {
363 diff_context_default
= git_config_int(var
, value
);
364 if (diff_context_default
< 0)
368 if (!strcmp(var
, "diff.interhunkcontext")) {
369 diff_interhunk_context_default
= git_config_int(var
, value
);
370 if (diff_interhunk_context_default
< 0)
374 if (!strcmp(var
, "diff.renames")) {
375 diff_detect_rename_default
= git_config_rename(var
, value
);
378 if (!strcmp(var
, "diff.autorefreshindex")) {
379 diff_auto_refresh_index
= git_config_bool(var
, value
);
382 if (!strcmp(var
, "diff.mnemonicprefix")) {
383 diff_mnemonic_prefix
= git_config_bool(var
, value
);
386 if (!strcmp(var
, "diff.noprefix")) {
387 diff_no_prefix
= git_config_bool(var
, value
);
390 if (!strcmp(var
, "diff.relative")) {
391 diff_relative
= git_config_bool(var
, value
);
394 if (!strcmp(var
, "diff.statgraphwidth")) {
395 diff_stat_graph_width
= git_config_int(var
, value
);
398 if (!strcmp(var
, "diff.external"))
399 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
400 if (!strcmp(var
, "diff.wordregex"))
401 return git_config_string(&diff_word_regex_cfg
, var
, value
);
402 if (!strcmp(var
, "diff.orderfile"))
403 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
405 if (!strcmp(var
, "diff.ignoresubmodules"))
406 handle_ignore_submodules_arg(&default_diff_options
, value
);
408 if (!strcmp(var
, "diff.submodule")) {
409 if (parse_submodule_params(&default_diff_options
, value
))
410 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
415 if (!strcmp(var
, "diff.algorithm")) {
416 diff_algorithm
= parse_algorithm_value(value
);
417 if (diff_algorithm
< 0)
422 if (git_color_config(var
, value
, cb
) < 0)
425 return git_diff_basic_config(var
, value
, cb
);
428 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
432 if (!strcmp(var
, "diff.renamelimit")) {
433 diff_rename_limit_default
= git_config_int(var
, value
);
437 if (userdiff_config(var
, value
) < 0)
440 if (skip_prefix(var
, "diff.color.", &name
) ||
441 skip_prefix(var
, "color.diff.", &name
)) {
442 int slot
= parse_diff_color_slot(name
);
446 return config_error_nonbool(var
);
447 return color_parse(value
, diff_colors
[slot
]);
450 if (!strcmp(var
, "diff.wserrorhighlight")) {
451 int val
= parse_ws_error_highlight(value
);
454 ws_error_highlight_default
= val
;
458 /* like GNU diff's --suppress-blank-empty option */
459 if (!strcmp(var
, "diff.suppressblankempty") ||
460 /* for backwards compatibility */
461 !strcmp(var
, "diff.suppress-blank-empty")) {
462 diff_suppress_blank_empty
= git_config_bool(var
, value
);
466 if (!strcmp(var
, "diff.dirstat")) {
467 struct strbuf errmsg
= STRBUF_INIT
;
468 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
469 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
470 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
472 strbuf_release(&errmsg
);
473 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
477 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
480 return git_default_config(var
, value
, cb
);
483 static char *quote_two(const char *one
, const char *two
)
485 int need_one
= quote_c_style(one
, NULL
, NULL
, CQUOTE_NODQ
);
486 int need_two
= quote_c_style(two
, NULL
, NULL
, CQUOTE_NODQ
);
487 struct strbuf res
= STRBUF_INIT
;
489 if (need_one
+ need_two
) {
490 strbuf_addch(&res
, '"');
491 quote_c_style(one
, &res
, NULL
, CQUOTE_NODQ
);
492 quote_c_style(two
, &res
, NULL
, CQUOTE_NODQ
);
493 strbuf_addch(&res
, '"');
495 strbuf_addstr(&res
, one
);
496 strbuf_addstr(&res
, two
);
498 return strbuf_detach(&res
, NULL
);
501 static const char *external_diff(void)
503 static const char *external_diff_cmd
= NULL
;
504 static int done_preparing
= 0;
507 return external_diff_cmd
;
508 external_diff_cmd
= xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
509 if (!external_diff_cmd
)
510 external_diff_cmd
= external_diff_cmd_cfg
;
512 return external_diff_cmd
;
516 * Keep track of files used for diffing. Sometimes such an entry
517 * refers to a temporary file, sometimes to an existing file, and
518 * sometimes to "/dev/null".
520 static struct diff_tempfile
{
522 * filename external diff should read from, or NULL if this
523 * entry is currently not in use:
527 char hex
[GIT_MAX_HEXSZ
+ 1];
531 * If this diff_tempfile instance refers to a temporary file,
532 * this tempfile object is used to manage its lifetime.
534 struct tempfile
*tempfile
;
537 struct emit_callback
{
540 int blank_at_eof_in_preimage
;
541 int blank_at_eof_in_postimage
;
543 int lno_in_postimage
;
544 const char **label_path
;
545 struct diff_words_data
*diff_words
;
546 struct diff_options
*opt
;
547 struct strbuf
*header
;
550 static int count_lines(const char *data
, int size
)
552 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
559 completely_empty
= 0;
563 completely_empty
= 0;
566 if (completely_empty
)
569 count
++; /* no trailing newline */
573 static int fill_mmfile(struct repository
*r
, mmfile_t
*mf
,
574 struct diff_filespec
*one
)
576 if (!DIFF_FILE_VALID(one
)) {
577 mf
->ptr
= (char *)""; /* does not matter */
581 else if (diff_populate_filespec(r
, one
, NULL
))
585 mf
->size
= one
->size
;
589 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
590 static unsigned long diff_filespec_size(struct repository
*r
,
591 struct diff_filespec
*one
)
593 struct diff_populate_filespec_options dpf_options
= {
594 .check_size_only
= 1,
597 if (!DIFF_FILE_VALID(one
))
599 diff_populate_filespec(r
, one
, &dpf_options
);
603 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
606 long size
= mf
->size
;
611 ptr
+= size
- 1; /* pointing at the very end */
613 ; /* incomplete line */
615 ptr
--; /* skip the last LF */
616 while (mf
->ptr
< ptr
) {
618 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
619 if (*prev_eol
== '\n')
621 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
629 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
630 struct emit_callback
*ecbdata
)
633 unsigned ws_rule
= ecbdata
->ws_rule
;
634 l1
= count_trailing_blank(mf1
, ws_rule
);
635 l2
= count_trailing_blank(mf2
, ws_rule
);
637 ecbdata
->blank_at_eof_in_preimage
= 0;
638 ecbdata
->blank_at_eof_in_postimage
= 0;
641 at
= count_lines(mf1
->ptr
, mf1
->size
);
642 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
644 at
= count_lines(mf2
->ptr
, mf2
->size
);
645 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
648 static void emit_line_0(struct diff_options
*o
,
649 const char *set_sign
, const char *set
, unsigned reverse
, const char *reset
,
650 int first
, const char *line
, int len
)
652 int has_trailing_newline
, has_trailing_carriage_return
;
653 int needs_reset
= 0; /* at the end of the line */
654 FILE *file
= o
->file
;
656 fputs(diff_line_prefix(o
), file
);
658 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
659 if (has_trailing_newline
)
662 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
663 if (has_trailing_carriage_return
)
669 if (reverse
&& want_color(o
->use_color
)) {
670 fputs(GIT_COLOR_REVERSE
, file
);
675 fputs(set_sign
, file
);
686 if (set_sign
&& set
!= set_sign
)
691 fwrite(line
, len
, 1, file
);
692 needs_reset
= 1; /* 'line' may contain color codes. */
697 if (has_trailing_carriage_return
)
699 if (has_trailing_newline
)
703 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
704 const char *line
, int len
)
706 emit_line_0(o
, set
, NULL
, 0, reset
, 0, line
, len
);
710 DIFF_SYMBOL_BINARY_DIFF_HEADER
,
711 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
712 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
713 DIFF_SYMBOL_BINARY_DIFF_BODY
,
714 DIFF_SYMBOL_BINARY_DIFF_FOOTER
,
715 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
716 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
717 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
718 DIFF_SYMBOL_STATS_LINE
,
719 DIFF_SYMBOL_WORD_DIFF
,
720 DIFF_SYMBOL_STAT_SEP
,
722 DIFF_SYMBOL_SUBMODULE_ADD
,
723 DIFF_SYMBOL_SUBMODULE_DEL
,
724 DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
725 DIFF_SYMBOL_SUBMODULE_MODIFIED
,
726 DIFF_SYMBOL_SUBMODULE_HEADER
,
727 DIFF_SYMBOL_SUBMODULE_ERROR
,
728 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
,
729 DIFF_SYMBOL_REWRITE_DIFF
,
730 DIFF_SYMBOL_BINARY_FILES
,
732 DIFF_SYMBOL_FILEPAIR_PLUS
,
733 DIFF_SYMBOL_FILEPAIR_MINUS
,
734 DIFF_SYMBOL_WORDS_PORCELAIN
,
737 DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
740 DIFF_SYMBOL_NO_LF_EOF
,
741 DIFF_SYMBOL_CONTEXT_FRAGINFO
,
742 DIFF_SYMBOL_CONTEXT_MARKER
,
743 DIFF_SYMBOL_SEPARATOR
746 * Flags for content lines:
747 * 0..12 are whitespace rules
748 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
749 * 16 is marking if the line is blank at EOF
751 #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
752 #define DIFF_SYMBOL_MOVED_LINE (1<<17)
753 #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
754 #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
755 #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
758 * This struct is used when we need to buffer the output of the diff output.
760 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
761 * into the pre/post image file. This pointer could be a union with the
762 * line pointer. By storing an offset into the file instead of the literal line,
763 * we can decrease the memory footprint for the buffered output. At first we
764 * may want to only have indirection for the content lines, but we could also
765 * enhance the state for emitting prefabricated lines, e.g. the similarity
766 * score line or hunk/file headers would only need to store a number or path
767 * and then the output can be constructed later on depending on state.
769 struct emitted_diff_symbol
{
773 int indent_off
; /* Offset to first non-whitespace character */
774 int indent_width
; /* The visual width of the indentation */
777 #define EMITTED_DIFF_SYMBOL_INIT {NULL}
779 struct emitted_diff_symbols
{
780 struct emitted_diff_symbol
*buf
;
783 #define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
785 static void append_emitted_diff_symbol(struct diff_options
*o
,
786 struct emitted_diff_symbol
*e
)
788 struct emitted_diff_symbol
*f
;
790 ALLOC_GROW(o
->emitted_symbols
->buf
,
791 o
->emitted_symbols
->nr
+ 1,
792 o
->emitted_symbols
->alloc
);
793 f
= &o
->emitted_symbols
->buf
[o
->emitted_symbols
->nr
++];
795 memcpy(f
, e
, sizeof(struct emitted_diff_symbol
));
796 f
->line
= e
->line
? xmemdupz(e
->line
, e
->len
) : NULL
;
800 struct hashmap_entry ent
;
801 const struct emitted_diff_symbol
*es
;
802 struct moved_entry
*next_line
;
806 struct moved_entry
*match
;
807 int wsd
; /* The whitespace delta of this block */
810 static void moved_block_clear(struct moved_block
*b
)
812 memset(b
, 0, sizeof(*b
));
815 #define INDENT_BLANKLINE INT_MIN
817 static void fill_es_indent_data(struct emitted_diff_symbol
*es
)
819 unsigned int off
= 0, i
;
820 int width
= 0, tab_width
= es
->flags
& WS_TAB_WIDTH_MASK
;
821 const char *s
= es
->line
;
822 const int len
= es
->len
;
824 /* skip any \v \f \r at start of indentation */
825 while (s
[off
] == '\f' || s
[off
] == '\v' ||
826 (s
[off
] == '\r' && off
< len
- 1))
829 /* calculate the visual width of indentation */
834 } else if (s
[off
] == '\t') {
835 width
+= tab_width
- (width
% tab_width
);
836 while (s
[++off
] == '\t')
843 /* check if this line is blank */
844 for (i
= off
; i
< len
; i
++)
849 es
->indent_width
= INDENT_BLANKLINE
;
850 es
->indent_off
= len
;
852 es
->indent_off
= off
;
853 es
->indent_width
= width
;
857 static int compute_ws_delta(const struct emitted_diff_symbol
*a
,
858 const struct emitted_diff_symbol
*b
,
863 a_off
= a
->indent_off
,
864 a_width
= a
->indent_width
,
865 b_off
= b
->indent_off
,
866 b_width
= b
->indent_width
;
869 if (a_width
== INDENT_BLANKLINE
&& b_width
== INDENT_BLANKLINE
) {
870 *out
= INDENT_BLANKLINE
;
874 if (a
->s
== DIFF_SYMBOL_PLUS
)
875 delta
= a_width
- b_width
;
877 delta
= b_width
- a_width
;
879 if (a_len
- a_off
!= b_len
- b_off
||
880 memcmp(a
->line
+ a_off
, b
->line
+ b_off
, a_len
- a_off
))
888 static int cmp_in_block_with_wsd(const struct diff_options
*o
,
889 const struct moved_entry
*cur
,
890 const struct moved_entry
*match
,
891 struct moved_block
*pmb
,
894 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
895 int al
= cur
->es
->len
, bl
= match
->es
->len
, cl
= l
->len
;
896 const char *a
= cur
->es
->line
,
897 *b
= match
->es
->line
,
899 int a_off
= cur
->es
->indent_off
,
900 a_width
= cur
->es
->indent_width
,
901 c_off
= l
->indent_off
,
902 c_width
= l
->indent_width
;
906 * We need to check if 'cur' is equal to 'match'. As those
907 * are from the same (+/-) side, we do not need to adjust for
908 * indent changes. However these were found using fuzzy
909 * matching so we do have to check if they are equal. Here we
910 * just check the lengths. We delay calling memcmp() to check
911 * the contents until later as if the length comparison for a
912 * and c fails we can avoid the call all together.
917 /* If 'l' and 'cur' are both blank then they match. */
918 if (a_width
== INDENT_BLANKLINE
&& c_width
== INDENT_BLANKLINE
)
922 * The indent changes of the block are known and stored in pmb->wsd;
923 * however we need to check if the indent changes of the current line
924 * match those of the current block and that the text of 'l' and 'cur'
925 * after the indentation match.
927 if (cur
->es
->s
== DIFF_SYMBOL_PLUS
)
928 delta
= a_width
- c_width
;
930 delta
= c_width
- a_width
;
933 * If the previous lines of this block were all blank then set its
936 if (pmb
->wsd
== INDENT_BLANKLINE
)
939 return !(delta
== pmb
->wsd
&& al
- a_off
== cl
- c_off
&&
940 !memcmp(a
, b
, al
) && !
941 memcmp(a
+ a_off
, c
+ c_off
, al
- a_off
));
944 static int moved_entry_cmp(const void *hashmap_cmp_fn_data
,
945 const struct hashmap_entry
*eptr
,
946 const struct hashmap_entry
*entry_or_key
,
949 const struct diff_options
*diffopt
= hashmap_cmp_fn_data
;
950 const struct moved_entry
*a
, *b
;
951 unsigned flags
= diffopt
->color_moved_ws_handling
952 & XDF_WHITESPACE_FLAGS
;
954 a
= container_of(eptr
, const struct moved_entry
, ent
);
955 b
= container_of(entry_or_key
, const struct moved_entry
, ent
);
957 if (diffopt
->color_moved_ws_handling
&
958 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
960 * As there is not specific white space config given,
961 * we'd need to check for a new block, so ignore all
962 * white space. The setup of the white space
963 * configuration for the next block is done else where
965 flags
|= XDF_IGNORE_WHITESPACE
;
967 return !xdiff_compare_lines(a
->es
->line
, a
->es
->len
,
968 b
->es
->line
, b
->es
->len
,
972 static struct moved_entry
*prepare_entry(struct diff_options
*o
,
975 struct moved_entry
*ret
= xmalloc(sizeof(*ret
));
976 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[line_no
];
977 unsigned flags
= o
->color_moved_ws_handling
& XDF_WHITESPACE_FLAGS
;
978 unsigned int hash
= xdiff_hash_string(l
->line
, l
->len
, flags
);
980 hashmap_entry_init(&ret
->ent
, hash
);
982 ret
->next_line
= NULL
;
987 static void add_lines_to_move_detection(struct diff_options
*o
,
988 struct hashmap
*add_lines
,
989 struct hashmap
*del_lines
)
991 struct moved_entry
*prev_line
= NULL
;
994 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
996 struct moved_entry
*key
;
998 switch (o
->emitted_symbols
->buf
[n
].s
) {
999 case DIFF_SYMBOL_PLUS
:
1002 case DIFF_SYMBOL_MINUS
:
1010 if (o
->color_moved_ws_handling
&
1011 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1012 fill_es_indent_data(&o
->emitted_symbols
->buf
[n
]);
1013 key
= prepare_entry(o
, n
);
1014 if (prev_line
&& prev_line
->es
->s
== o
->emitted_symbols
->buf
[n
].s
)
1015 prev_line
->next_line
= key
;
1017 hashmap_add(hm
, &key
->ent
);
1022 static void pmb_advance_or_null(struct diff_options
*o
,
1023 struct moved_entry
*match
,
1025 struct moved_block
*pmb
,
1029 for (i
= 0; i
< pmb_nr
; i
++) {
1030 struct moved_entry
*prev
= pmb
[i
].match
;
1031 struct moved_entry
*cur
= (prev
&& prev
->next_line
) ?
1032 prev
->next_line
: NULL
;
1033 if (cur
&& !hm
->cmpfn(o
, &cur
->ent
, &match
->ent
, NULL
)) {
1036 pmb
[i
].match
= NULL
;
1041 static void pmb_advance_or_null_multi_match(struct diff_options
*o
,
1042 struct moved_entry
*match
,
1044 struct moved_block
*pmb
,
1048 char *got_match
= xcalloc(1, pmb_nr
);
1050 hashmap_for_each_entry_from(hm
, match
, ent
) {
1051 for (i
= 0; i
< pmb_nr
; i
++) {
1052 struct moved_entry
*prev
= pmb
[i
].match
;
1053 struct moved_entry
*cur
= (prev
&& prev
->next_line
) ?
1054 prev
->next_line
: NULL
;
1057 if (!cmp_in_block_with_wsd(o
, cur
, match
, &pmb
[i
], n
))
1062 for (i
= 0; i
< pmb_nr
; i
++) {
1064 /* Advance to the next line */
1065 pmb
[i
].match
= pmb
[i
].match
->next_line
;
1067 moved_block_clear(&pmb
[i
]);
1074 static int shrink_potential_moved_blocks(struct moved_block
*pmb
,
1079 /* Shrink the set of potential block to the remaining running */
1080 for (lp
= 0, rp
= pmb_nr
- 1; lp
<= rp
;) {
1081 while (lp
< pmb_nr
&& pmb
[lp
].match
)
1083 /* lp points at the first NULL now */
1085 while (rp
> -1 && !pmb
[rp
].match
)
1087 /* rp points at the last non-NULL */
1089 if (lp
< pmb_nr
&& rp
> -1 && lp
< rp
) {
1091 memset(&pmb
[rp
], 0, sizeof(pmb
[rp
]));
1097 /* Remember the number of running sets */
1102 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1104 * Otherwise, if the last block has fewer alphanumeric characters than
1105 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
1108 * The last block consists of the (n - block_length)'th line up to but not
1109 * including the nth line.
1111 * Returns 0 if the last block is empty or is unset by this function, non zero
1114 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1115 * Think of a way to unify them.
1117 static int adjust_last_block(struct diff_options
*o
, int n
, int block_length
)
1119 int i
, alnum_count
= 0;
1120 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
1121 return block_length
;
1122 for (i
= 1; i
< block_length
+ 1; i
++) {
1123 const char *c
= o
->emitted_symbols
->buf
[n
- i
].line
;
1128 if (alnum_count
>= COLOR_MOVED_MIN_ALNUM_COUNT
)
1132 for (i
= 1; i
< block_length
+ 1; i
++)
1133 o
->emitted_symbols
->buf
[n
- i
].flags
&= ~DIFF_SYMBOL_MOVED_LINE
;
1137 /* Find blocks of moved code, delegate actual coloring decision to helper */
1138 static void mark_color_as_moved(struct diff_options
*o
,
1139 struct hashmap
*add_lines
,
1140 struct hashmap
*del_lines
)
1142 struct moved_block
*pmb
= NULL
; /* potentially moved blocks */
1143 int pmb_nr
= 0, pmb_alloc
= 0;
1144 int n
, flipped_block
= 0, block_length
= 0;
1147 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1148 struct hashmap
*hm
= NULL
;
1149 struct moved_entry
*key
;
1150 struct moved_entry
*match
= NULL
;
1151 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1152 enum diff_symbol last_symbol
= 0;
1155 case DIFF_SYMBOL_PLUS
:
1157 key
= prepare_entry(o
, n
);
1158 match
= hashmap_get_entry(hm
, key
, ent
, NULL
);
1161 case DIFF_SYMBOL_MINUS
:
1163 key
= prepare_entry(o
, n
);
1164 match
= hashmap_get_entry(hm
, key
, ent
, NULL
);
1174 adjust_last_block(o
, n
, block_length
);
1175 for(i
= 0; i
< pmb_nr
; i
++)
1176 moved_block_clear(&pmb
[i
]);
1184 if (o
->color_moved
== COLOR_MOVED_PLAIN
) {
1186 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1190 if (o
->color_moved_ws_handling
&
1191 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1192 pmb_advance_or_null_multi_match(o
, match
, hm
, pmb
, pmb_nr
, n
);
1194 pmb_advance_or_null(o
, match
, hm
, pmb
, pmb_nr
);
1196 pmb_nr
= shrink_potential_moved_blocks(pmb
, pmb_nr
);
1200 * The current line is the start of a new block.
1201 * Setup the set of potential blocks.
1203 hashmap_for_each_entry_from(hm
, match
, ent
) {
1204 ALLOC_GROW(pmb
, pmb_nr
+ 1, pmb_alloc
);
1205 if (o
->color_moved_ws_handling
&
1206 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
) {
1207 if (compute_ws_delta(l
, match
->es
,
1209 pmb
[pmb_nr
++].match
= match
;
1211 pmb
[pmb_nr
].wsd
= 0;
1212 pmb
[pmb_nr
++].match
= match
;
1216 if (adjust_last_block(o
, n
, block_length
) &&
1217 pmb_nr
&& last_symbol
!= l
->s
)
1218 flipped_block
= (flipped_block
+ 1) % 2;
1227 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1228 if (flipped_block
&& o
->color_moved
!= COLOR_MOVED_BLOCKS
)
1229 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_ALT
;
1233 adjust_last_block(o
, n
, block_length
);
1235 for(n
= 0; n
< pmb_nr
; n
++)
1236 moved_block_clear(&pmb
[n
]);
1240 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1241 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
1242 static void dim_moved_lines(struct diff_options
*o
)
1245 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1246 struct emitted_diff_symbol
*prev
= (n
!= 0) ?
1247 &o
->emitted_symbols
->buf
[n
- 1] : NULL
;
1248 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1249 struct emitted_diff_symbol
*next
=
1250 (n
< o
->emitted_symbols
->nr
- 1) ?
1251 &o
->emitted_symbols
->buf
[n
+ 1] : NULL
;
1253 /* Not a plus or minus line? */
1254 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
)
1257 /* Not a moved line? */
1258 if (!(l
->flags
& DIFF_SYMBOL_MOVED_LINE
))
1262 * If prev or next are not a plus or minus line,
1263 * pretend they don't exist
1265 if (prev
&& prev
->s
!= DIFF_SYMBOL_PLUS
&&
1266 prev
->s
!= DIFF_SYMBOL_MINUS
)
1268 if (next
&& next
->s
!= DIFF_SYMBOL_PLUS
&&
1269 next
->s
!= DIFF_SYMBOL_MINUS
)
1272 /* Inside a block? */
1274 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1275 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
)) &&
1277 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1278 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
))) {
1279 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1283 /* Check if we are at an interesting bound: */
1284 if (prev
&& (prev
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1285 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1286 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1288 if (next
&& (next
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1289 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1290 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1294 * The boundary to prev and next are not interesting,
1295 * so this line is not interesting as a whole
1297 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1301 static void emit_line_ws_markup(struct diff_options
*o
,
1302 const char *set_sign
, const char *set
,
1304 int sign_index
, const char *line
, int len
,
1305 unsigned ws_rule
, int blank_at_eof
)
1307 const char *ws
= NULL
;
1308 int sign
= o
->output_indicators
[sign_index
];
1310 if (o
->ws_error_highlight
& ws_rule
) {
1311 ws
= diff_get_color_opt(o
, DIFF_WHITESPACE
);
1316 if (!ws
&& !set_sign
)
1317 emit_line_0(o
, set
, NULL
, 0, reset
, sign
, line
, len
);
1319 emit_line_0(o
, set_sign
, set
, !!set_sign
, reset
, sign
, line
, len
);
1320 } else if (blank_at_eof
)
1321 /* Blank line at EOF - paint '+' as well */
1322 emit_line_0(o
, ws
, NULL
, 0, reset
, sign
, line
, len
);
1324 /* Emit just the prefix, then the rest. */
1325 emit_line_0(o
, set_sign
? set_sign
: set
, NULL
, !!set_sign
, reset
,
1327 ws_check_emit(line
, len
, ws_rule
,
1328 o
->file
, set
, reset
, ws
);
1332 static void emit_diff_symbol_from_struct(struct diff_options
*o
,
1333 struct emitted_diff_symbol
*eds
)
1335 static const char *nneof
= " No newline at end of file\n";
1336 const char *context
, *reset
, *set
, *set_sign
, *meta
, *fraginfo
;
1337 struct strbuf sb
= STRBUF_INIT
;
1339 enum diff_symbol s
= eds
->s
;
1340 const char *line
= eds
->line
;
1342 unsigned flags
= eds
->flags
;
1345 case DIFF_SYMBOL_NO_LF_EOF
:
1346 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1347 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1348 putc('\n', o
->file
);
1349 emit_line_0(o
, context
, NULL
, 0, reset
, '\\',
1350 nneof
, strlen(nneof
));
1352 case DIFF_SYMBOL_SUBMODULE_HEADER
:
1353 case DIFF_SYMBOL_SUBMODULE_ERROR
:
1354 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
:
1355 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
:
1356 case DIFF_SYMBOL_SUMMARY
:
1357 case DIFF_SYMBOL_STATS_LINE
:
1358 case DIFF_SYMBOL_BINARY_DIFF_BODY
:
1359 case DIFF_SYMBOL_CONTEXT_FRAGINFO
:
1360 emit_line(o
, "", "", line
, len
);
1362 case DIFF_SYMBOL_CONTEXT_INCOMPLETE
:
1363 case DIFF_SYMBOL_CONTEXT_MARKER
:
1364 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1365 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1366 emit_line(o
, context
, reset
, line
, len
);
1368 case DIFF_SYMBOL_SEPARATOR
:
1369 fprintf(o
->file
, "%s%c",
1370 diff_line_prefix(o
),
1371 o
->line_termination
);
1373 case DIFF_SYMBOL_CONTEXT
:
1374 set
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1375 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1377 if (o
->flags
.dual_color_diffed_diffs
) {
1378 char c
= !len
? 0 : line
[0];
1381 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1383 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1385 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1387 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1388 OUTPUT_INDICATOR_CONTEXT
, line
, len
,
1389 flags
& (DIFF_SYMBOL_CONTENT_WS_MASK
), 0);
1391 case DIFF_SYMBOL_PLUS
:
1392 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1393 DIFF_SYMBOL_MOVED_LINE_ALT
|
1394 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1395 case DIFF_SYMBOL_MOVED_LINE
|
1396 DIFF_SYMBOL_MOVED_LINE_ALT
|
1397 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1398 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT_DIM
);
1400 case DIFF_SYMBOL_MOVED_LINE
|
1401 DIFF_SYMBOL_MOVED_LINE_ALT
:
1402 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT
);
1404 case DIFF_SYMBOL_MOVED_LINE
|
1405 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1406 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_DIM
);
1408 case DIFF_SYMBOL_MOVED_LINE
:
1409 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED
);
1412 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1414 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1415 if (!o
->flags
.dual_color_diffed_diffs
)
1418 char c
= !len
? 0 : line
[0];
1422 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_BOLD
);
1424 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1426 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_BOLD
);
1428 set
= diff_get_color_opt(o
, DIFF_CONTEXT_BOLD
);
1429 flags
&= ~DIFF_SYMBOL_CONTENT_WS_MASK
;
1431 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1432 OUTPUT_INDICATOR_NEW
, line
, len
,
1433 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
,
1434 flags
& DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
);
1436 case DIFF_SYMBOL_MINUS
:
1437 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1438 DIFF_SYMBOL_MOVED_LINE_ALT
|
1439 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1440 case DIFF_SYMBOL_MOVED_LINE
|
1441 DIFF_SYMBOL_MOVED_LINE_ALT
|
1442 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1443 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT_DIM
);
1445 case DIFF_SYMBOL_MOVED_LINE
|
1446 DIFF_SYMBOL_MOVED_LINE_ALT
:
1447 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT
);
1449 case DIFF_SYMBOL_MOVED_LINE
|
1450 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1451 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_DIM
);
1453 case DIFF_SYMBOL_MOVED_LINE
:
1454 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED
);
1457 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1459 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1460 if (!o
->flags
.dual_color_diffed_diffs
)
1463 char c
= !len
? 0 : line
[0];
1467 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_DIM
);
1469 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1471 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_DIM
);
1473 set
= diff_get_color_opt(o
, DIFF_CONTEXT_DIM
);
1475 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1476 OUTPUT_INDICATOR_OLD
, line
, len
,
1477 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
, 0);
1479 case DIFF_SYMBOL_WORDS_PORCELAIN
:
1480 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1481 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1482 emit_line(o
, context
, reset
, line
, len
);
1483 fputs("~\n", o
->file
);
1485 case DIFF_SYMBOL_WORDS
:
1486 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1487 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1489 * Skip the prefix character, if any. With
1490 * diff_suppress_blank_empty, there may be
1493 if (line
[0] != '\n') {
1497 emit_line(o
, context
, reset
, line
, len
);
1499 case DIFF_SYMBOL_FILEPAIR_PLUS
:
1500 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1501 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1502 fprintf(o
->file
, "%s%s+++ %s%s%s\n", diff_line_prefix(o
), meta
,
1504 strchr(line
, ' ') ? "\t" : "");
1506 case DIFF_SYMBOL_FILEPAIR_MINUS
:
1507 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1508 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1509 fprintf(o
->file
, "%s%s--- %s%s%s\n", diff_line_prefix(o
), meta
,
1511 strchr(line
, ' ') ? "\t" : "");
1513 case DIFF_SYMBOL_BINARY_FILES
:
1514 case DIFF_SYMBOL_HEADER
:
1515 fprintf(o
->file
, "%s", line
);
1517 case DIFF_SYMBOL_BINARY_DIFF_HEADER
:
1518 fprintf(o
->file
, "%sGIT binary patch\n", diff_line_prefix(o
));
1520 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
:
1521 fprintf(o
->file
, "%sdelta %s\n", diff_line_prefix(o
), line
);
1523 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
:
1524 fprintf(o
->file
, "%sliteral %s\n", diff_line_prefix(o
), line
);
1526 case DIFF_SYMBOL_BINARY_DIFF_FOOTER
:
1527 fputs(diff_line_prefix(o
), o
->file
);
1528 fputc('\n', o
->file
);
1530 case DIFF_SYMBOL_REWRITE_DIFF
:
1531 fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
1532 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1533 emit_line(o
, fraginfo
, reset
, line
, len
);
1535 case DIFF_SYMBOL_SUBMODULE_ADD
:
1536 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1537 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1538 emit_line(o
, set
, reset
, line
, len
);
1540 case DIFF_SYMBOL_SUBMODULE_DEL
:
1541 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1542 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1543 emit_line(o
, set
, reset
, line
, len
);
1545 case DIFF_SYMBOL_SUBMODULE_UNTRACKED
:
1546 fprintf(o
->file
, "%sSubmodule %s contains untracked content\n",
1547 diff_line_prefix(o
), line
);
1549 case DIFF_SYMBOL_SUBMODULE_MODIFIED
:
1550 fprintf(o
->file
, "%sSubmodule %s contains modified content\n",
1551 diff_line_prefix(o
), line
);
1553 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
:
1554 emit_line(o
, "", "", " 0 files changed\n",
1555 strlen(" 0 files changed\n"));
1557 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV
:
1558 emit_line(o
, "", "", " ...\n", strlen(" ...\n"));
1560 case DIFF_SYMBOL_WORD_DIFF
:
1561 fprintf(o
->file
, "%.*s", len
, line
);
1563 case DIFF_SYMBOL_STAT_SEP
:
1564 fputs(o
->stat_sep
, o
->file
);
1567 BUG("unknown diff symbol");
1569 strbuf_release(&sb
);
1572 static void emit_diff_symbol(struct diff_options
*o
, enum diff_symbol s
,
1573 const char *line
, int len
, unsigned flags
)
1575 struct emitted_diff_symbol e
= {line
, len
, flags
, 0, 0, s
};
1577 if (o
->emitted_symbols
)
1578 append_emitted_diff_symbol(o
, &e
);
1580 emit_diff_symbol_from_struct(o
, &e
);
1583 void diff_emit_submodule_del(struct diff_options
*o
, const char *line
)
1585 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_DEL
, line
, strlen(line
), 0);
1588 void diff_emit_submodule_add(struct diff_options
*o
, const char *line
)
1590 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ADD
, line
, strlen(line
), 0);
1593 void diff_emit_submodule_untracked(struct diff_options
*o
, const char *path
)
1595 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
1596 path
, strlen(path
), 0);
1599 void diff_emit_submodule_modified(struct diff_options
*o
, const char *path
)
1601 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_MODIFIED
,
1602 path
, strlen(path
), 0);
1605 void diff_emit_submodule_header(struct diff_options
*o
, const char *header
)
1607 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_HEADER
,
1608 header
, strlen(header
), 0);
1611 void diff_emit_submodule_error(struct diff_options
*o
, const char *err
)
1613 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ERROR
, err
, strlen(err
), 0);
1616 void diff_emit_submodule_pipethrough(struct diff_options
*o
,
1617 const char *line
, int len
)
1619 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
, line
, len
, 0);
1622 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
1624 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
1625 ecbdata
->blank_at_eof_in_preimage
&&
1626 ecbdata
->blank_at_eof_in_postimage
&&
1627 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
1628 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
1630 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
1633 static void emit_add_line(struct emit_callback
*ecbdata
,
1634 const char *line
, int len
)
1636 unsigned flags
= WSEH_NEW
| ecbdata
->ws_rule
;
1637 if (new_blank_line_at_eof(ecbdata
, line
, len
))
1638 flags
|= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
;
1640 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_PLUS
, line
, len
, flags
);
1643 static void emit_del_line(struct emit_callback
*ecbdata
,
1644 const char *line
, int len
)
1646 unsigned flags
= WSEH_OLD
| ecbdata
->ws_rule
;
1647 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_MINUS
, line
, len
, flags
);
1650 static void emit_context_line(struct emit_callback
*ecbdata
,
1651 const char *line
, int len
)
1653 unsigned flags
= WSEH_CONTEXT
| ecbdata
->ws_rule
;
1654 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_CONTEXT
, line
, len
, flags
);
1657 static void emit_hunk_header(struct emit_callback
*ecbdata
,
1658 const char *line
, int len
)
1660 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1661 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
1662 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
1663 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1664 const char *reverse
= ecbdata
->color_diff
? GIT_COLOR_REVERSE
: "";
1665 static const char atat
[2] = { '@', '@' };
1666 const char *cp
, *ep
;
1667 struct strbuf msgbuf
= STRBUF_INIT
;
1672 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1673 * it always is at least 10 bytes long.
1676 memcmp(line
, atat
, 2) ||
1677 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
1678 emit_diff_symbol(ecbdata
->opt
,
1679 DIFF_SYMBOL_CONTEXT_MARKER
, line
, len
, 0);
1682 ep
+= 2; /* skip over @@ */
1684 /* The hunk header in fraginfo color */
1685 if (ecbdata
->opt
->flags
.dual_color_diffed_diffs
)
1686 strbuf_addstr(&msgbuf
, reverse
);
1687 strbuf_addstr(&msgbuf
, frag
);
1688 if (ecbdata
->opt
->flags
.suppress_hunk_header_line_count
)
1689 strbuf_add(&msgbuf
, atat
, sizeof(atat
));
1691 strbuf_add(&msgbuf
, line
, ep
- line
);
1692 strbuf_addstr(&msgbuf
, reset
);
1698 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
1701 /* blank before the func header */
1702 for (cp
= ep
; ep
- line
< len
; ep
++)
1703 if (*ep
!= ' ' && *ep
!= '\t')
1706 strbuf_addstr(&msgbuf
, context
);
1707 strbuf_add(&msgbuf
, cp
, ep
- cp
);
1708 strbuf_addstr(&msgbuf
, reset
);
1711 if (ep
< line
+ len
) {
1712 strbuf_addstr(&msgbuf
, func
);
1713 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
1714 strbuf_addstr(&msgbuf
, reset
);
1717 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
1718 strbuf_complete_line(&msgbuf
);
1719 emit_diff_symbol(ecbdata
->opt
,
1720 DIFF_SYMBOL_CONTEXT_FRAGINFO
, msgbuf
.buf
, msgbuf
.len
, 0);
1721 strbuf_release(&msgbuf
);
1724 static struct diff_tempfile
*claim_diff_tempfile(void)
1727 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
1728 if (!diff_temp
[i
].name
)
1729 return diff_temp
+ i
;
1730 BUG("diff is failing to clean up its tempfiles");
1733 static void remove_tempfile(void)
1736 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
1737 if (is_tempfile_active(diff_temp
[i
].tempfile
))
1738 delete_tempfile(&diff_temp
[i
].tempfile
);
1739 diff_temp
[i
].name
= NULL
;
1743 static void add_line_count(struct strbuf
*out
, int count
)
1747 strbuf_addstr(out
, "0,0");
1750 strbuf_addstr(out
, "1");
1753 strbuf_addf(out
, "1,%d", count
);
1758 static void emit_rewrite_lines(struct emit_callback
*ecb
,
1759 int prefix
, const char *data
, int size
)
1761 const char *endp
= NULL
;
1766 endp
= memchr(data
, '\n', size
);
1767 len
= endp
? (endp
- data
+ 1) : size
;
1768 if (prefix
!= '+') {
1769 ecb
->lno_in_preimage
++;
1770 emit_del_line(ecb
, data
, len
);
1772 ecb
->lno_in_postimage
++;
1773 emit_add_line(ecb
, data
, len
);
1779 emit_diff_symbol(ecb
->opt
, DIFF_SYMBOL_NO_LF_EOF
, NULL
, 0, 0);
1782 static void emit_rewrite_diff(const char *name_a
,
1784 struct diff_filespec
*one
,
1785 struct diff_filespec
*two
,
1786 struct userdiff_driver
*textconv_one
,
1787 struct userdiff_driver
*textconv_two
,
1788 struct diff_options
*o
)
1791 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
1792 const char *a_prefix
, *b_prefix
;
1793 char *data_one
, *data_two
;
1794 size_t size_one
, size_two
;
1795 struct emit_callback ecbdata
;
1796 struct strbuf out
= STRBUF_INIT
;
1798 if (diff_mnemonic_prefix
&& o
->flags
.reverse_diff
) {
1799 a_prefix
= o
->b_prefix
;
1800 b_prefix
= o
->a_prefix
;
1802 a_prefix
= o
->a_prefix
;
1803 b_prefix
= o
->b_prefix
;
1806 name_a
+= (*name_a
== '/');
1807 name_b
+= (*name_b
== '/');
1809 strbuf_reset(&a_name
);
1810 strbuf_reset(&b_name
);
1811 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
1812 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
1814 size_one
= fill_textconv(o
->repo
, textconv_one
, one
, &data_one
);
1815 size_two
= fill_textconv(o
->repo
, textconv_two
, two
, &data_two
);
1817 memset(&ecbdata
, 0, sizeof(ecbdata
));
1818 ecbdata
.color_diff
= want_color(o
->use_color
);
1819 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
1821 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
1823 mf1
.ptr
= (char *)data_one
;
1824 mf2
.ptr
= (char *)data_two
;
1825 mf1
.size
= size_one
;
1826 mf2
.size
= size_two
;
1827 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1829 ecbdata
.lno_in_preimage
= 1;
1830 ecbdata
.lno_in_postimage
= 1;
1832 lc_a
= count_lines(data_one
, size_one
);
1833 lc_b
= count_lines(data_two
, size_two
);
1835 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1836 a_name
.buf
, a_name
.len
, 0);
1837 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1838 b_name
.buf
, b_name
.len
, 0);
1840 strbuf_addstr(&out
, "@@ -");
1841 if (!o
->irreversible_delete
)
1842 add_line_count(&out
, lc_a
);
1844 strbuf_addstr(&out
, "?,?");
1845 strbuf_addstr(&out
, " +");
1846 add_line_count(&out
, lc_b
);
1847 strbuf_addstr(&out
, " @@\n");
1848 emit_diff_symbol(o
, DIFF_SYMBOL_REWRITE_DIFF
, out
.buf
, out
.len
, 0);
1849 strbuf_release(&out
);
1851 if (lc_a
&& !o
->irreversible_delete
)
1852 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
1854 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
1856 free((char *)data_one
);
1858 free((char *)data_two
);
1861 struct diff_words_buffer
{
1863 unsigned long alloc
;
1864 struct diff_words_orig
{
1865 const char *begin
, *end
;
1867 int orig_nr
, orig_alloc
;
1870 static void diff_words_append(char *line
, unsigned long len
,
1871 struct diff_words_buffer
*buffer
)
1873 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
1876 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
1877 buffer
->text
.size
+= len
;
1878 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
1881 struct diff_words_style_elem
{
1884 const char *color
; /* NULL; filled in by the setup code if
1885 * color is enabled */
1888 struct diff_words_style
{
1889 enum diff_words_type type
;
1890 struct diff_words_style_elem new_word
, old_word
, ctx
;
1891 const char *newline
;
1894 static struct diff_words_style diff_words_styles
[] = {
1895 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1896 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1897 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
1900 struct diff_words_data
{
1901 struct diff_words_buffer minus
, plus
;
1902 const char *current_plus
;
1904 struct diff_options
*opt
;
1905 regex_t
*word_regex
;
1906 enum diff_words_type type
;
1907 struct diff_words_style
*style
;
1910 static int fn_out_diff_words_write_helper(struct diff_options
*o
,
1911 struct diff_words_style_elem
*st_el
,
1912 const char *newline
,
1913 size_t count
, const char *buf
)
1916 struct strbuf sb
= STRBUF_INIT
;
1919 char *p
= memchr(buf
, '\n', count
);
1921 strbuf_addstr(&sb
, diff_line_prefix(o
));
1924 const char *reset
= st_el
->color
&& *st_el
->color
?
1925 GIT_COLOR_RESET
: NULL
;
1926 if (st_el
->color
&& *st_el
->color
)
1927 strbuf_addstr(&sb
, st_el
->color
);
1928 strbuf_addstr(&sb
, st_el
->prefix
);
1929 strbuf_add(&sb
, buf
, p
? p
- buf
: count
);
1930 strbuf_addstr(&sb
, st_el
->suffix
);
1932 strbuf_addstr(&sb
, reset
);
1937 strbuf_addstr(&sb
, newline
);
1938 count
-= p
+ 1 - buf
;
1942 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1950 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1952 strbuf_release(&sb
);
1957 * '--color-words' algorithm can be described as:
1959 * 1. collect the minus/plus lines of a diff hunk, divided into
1960 * minus-lines and plus-lines;
1962 * 2. break both minus-lines and plus-lines into words and
1963 * place them into two mmfile_t with one word for each line;
1965 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1967 * And for the common parts of the both file, we output the plus side text.
1968 * diff_words->current_plus is used to trace the current position of the plus file
1969 * which printed. diff_words->last_minus is used to trace the last minus word
1972 * For '--graph' to work with '--color-words', we need to output the graph prefix
1973 * on each line of color words output. Generally, there are two conditions on
1974 * which we should output the prefix.
1976 * 1. diff_words->last_minus == 0 &&
1977 * diff_words->current_plus == diff_words->plus.text.ptr
1979 * that is: the plus text must start as a new line, and if there is no minus
1980 * word printed, a graph prefix must be printed.
1982 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1983 * *(diff_words->current_plus - 1) == '\n'
1985 * that is: a graph prefix must be printed following a '\n'
1987 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
1989 if ((diff_words
->last_minus
== 0 &&
1990 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
1991 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
1992 *(diff_words
->current_plus
- 1) == '\n')) {
1999 static void fn_out_diff_words_aux(void *priv
,
2000 long minus_first
, long minus_len
,
2001 long plus_first
, long plus_len
,
2002 const char *func
, long funclen
)
2004 struct diff_words_data
*diff_words
= priv
;
2005 struct diff_words_style
*style
= diff_words
->style
;
2006 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
2007 struct diff_options
*opt
= diff_words
->opt
;
2008 const char *line_prefix
;
2011 line_prefix
= diff_line_prefix(opt
);
2013 /* POSIX requires that first be decremented by one if len == 0... */
2015 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
2017 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
2019 minus_begin
= minus_end
=
2020 diff_words
->minus
.orig
[minus_first
].end
;
2023 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
2024 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
2026 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
2028 if (color_words_output_graph_prefix(diff_words
)) {
2029 fputs(line_prefix
, diff_words
->opt
->file
);
2031 if (diff_words
->current_plus
!= plus_begin
) {
2032 fn_out_diff_words_write_helper(diff_words
->opt
,
2033 &style
->ctx
, style
->newline
,
2034 plus_begin
- diff_words
->current_plus
,
2035 diff_words
->current_plus
);
2037 if (minus_begin
!= minus_end
) {
2038 fn_out_diff_words_write_helper(diff_words
->opt
,
2039 &style
->old_word
, style
->newline
,
2040 minus_end
- minus_begin
, minus_begin
);
2042 if (plus_begin
!= plus_end
) {
2043 fn_out_diff_words_write_helper(diff_words
->opt
,
2044 &style
->new_word
, style
->newline
,
2045 plus_end
- plus_begin
, plus_begin
);
2048 diff_words
->current_plus
= plus_end
;
2049 diff_words
->last_minus
= minus_first
;
2052 /* This function starts looking at *begin, and returns 0 iff a word was found. */
2053 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
2054 int *begin
, int *end
)
2056 if (word_regex
&& *begin
< buffer
->size
) {
2057 regmatch_t match
[1];
2058 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
2059 buffer
->size
- *begin
, 1, match
, 0)) {
2060 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
2061 '\n', match
[0].rm_eo
- match
[0].rm_so
);
2062 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
2063 *begin
+= match
[0].rm_so
;
2064 return *begin
>= *end
;
2069 /* find the next word */
2070 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
2072 if (*begin
>= buffer
->size
)
2075 /* find the end of the word */
2077 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
2084 * This function splits the words in buffer->text, stores the list with
2085 * newline separator into out, and saves the offsets of the original words
2088 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
2089 regex_t
*word_regex
)
2097 /* fake an empty "0th" word */
2098 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
2099 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
2100 buffer
->orig_nr
= 1;
2102 for (i
= 0; i
< buffer
->text
.size
; i
++) {
2103 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
2106 /* store original boundaries */
2107 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
2108 buffer
->orig_alloc
);
2109 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
2110 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
2113 /* store one word */
2114 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
2115 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
2116 out
->ptr
[out
->size
+ j
- i
] = '\n';
2117 out
->size
+= j
- i
+ 1;
2123 /* this executes the word diff on the accumulated buffers */
2124 static void diff_words_show(struct diff_words_data
*diff_words
)
2128 mmfile_t minus
, plus
;
2129 struct diff_words_style
*style
= diff_words
->style
;
2131 struct diff_options
*opt
= diff_words
->opt
;
2132 const char *line_prefix
;
2135 line_prefix
= diff_line_prefix(opt
);
2137 /* special case: only removal */
2138 if (!diff_words
->plus
.text
.size
) {
2139 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2140 line_prefix
, strlen(line_prefix
), 0);
2141 fn_out_diff_words_write_helper(diff_words
->opt
,
2142 &style
->old_word
, style
->newline
,
2143 diff_words
->minus
.text
.size
,
2144 diff_words
->minus
.text
.ptr
);
2145 diff_words
->minus
.text
.size
= 0;
2149 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
2150 diff_words
->last_minus
= 0;
2152 memset(&xpp
, 0, sizeof(xpp
));
2153 memset(&xecfg
, 0, sizeof(xecfg
));
2154 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
2155 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
2157 /* as only the hunk header will be parsed, we need a 0-context */
2159 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, NULL
,
2160 diff_words
, &xpp
, &xecfg
))
2161 die("unable to generate word diff");
2164 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
2165 diff_words
->plus
.text
.size
) {
2166 if (color_words_output_graph_prefix(diff_words
))
2167 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2168 line_prefix
, strlen(line_prefix
), 0);
2169 fn_out_diff_words_write_helper(diff_words
->opt
,
2170 &style
->ctx
, style
->newline
,
2171 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
2172 - diff_words
->current_plus
, diff_words
->current_plus
);
2174 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
2177 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
2178 static void diff_words_flush(struct emit_callback
*ecbdata
)
2180 struct diff_options
*wo
= ecbdata
->diff_words
->opt
;
2182 if (ecbdata
->diff_words
->minus
.text
.size
||
2183 ecbdata
->diff_words
->plus
.text
.size
)
2184 diff_words_show(ecbdata
->diff_words
);
2186 if (wo
->emitted_symbols
) {
2187 struct diff_options
*o
= ecbdata
->opt
;
2188 struct emitted_diff_symbols
*wol
= wo
->emitted_symbols
;
2193 * Instead of appending each, concat all words to a line?
2195 for (i
= 0; i
< wol
->nr
; i
++)
2196 append_emitted_diff_symbol(o
, &wol
->buf
[i
]);
2198 for (i
= 0; i
< wol
->nr
; i
++)
2199 free((void *)wol
->buf
[i
].line
);
2205 static void diff_filespec_load_driver(struct diff_filespec
*one
,
2206 struct index_state
*istate
)
2208 /* Use already-loaded driver */
2212 if (S_ISREG(one
->mode
))
2213 one
->driver
= userdiff_find_by_path(istate
, one
->path
);
2215 /* Fallback to default settings */
2217 one
->driver
= userdiff_find_by_name("default");
2220 static const char *userdiff_word_regex(struct diff_filespec
*one
,
2221 struct index_state
*istate
)
2223 diff_filespec_load_driver(one
, istate
);
2224 return one
->driver
->word_regex
;
2227 static void init_diff_words_data(struct emit_callback
*ecbdata
,
2228 struct diff_options
*orig_opts
,
2229 struct diff_filespec
*one
,
2230 struct diff_filespec
*two
)
2233 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
2234 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
2236 CALLOC_ARRAY(ecbdata
->diff_words
, 1);
2237 ecbdata
->diff_words
->type
= o
->word_diff
;
2238 ecbdata
->diff_words
->opt
= o
;
2240 if (orig_opts
->emitted_symbols
)
2241 CALLOC_ARRAY(o
->emitted_symbols
, 1);
2244 o
->word_regex
= userdiff_word_regex(one
, o
->repo
->index
);
2246 o
->word_regex
= userdiff_word_regex(two
, o
->repo
->index
);
2248 o
->word_regex
= diff_word_regex_cfg
;
2249 if (o
->word_regex
) {
2250 ecbdata
->diff_words
->word_regex
= (regex_t
*)
2251 xmalloc(sizeof(regex_t
));
2252 if (regcomp(ecbdata
->diff_words
->word_regex
,
2254 REG_EXTENDED
| REG_NEWLINE
))
2255 die("invalid regular expression: %s",
2258 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
2259 if (o
->word_diff
== diff_words_styles
[i
].type
) {
2260 ecbdata
->diff_words
->style
=
2261 &diff_words_styles
[i
];
2265 if (want_color(o
->use_color
)) {
2266 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
2267 st
->old_word
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2268 st
->new_word
.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2269 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
2273 static void free_diff_words_data(struct emit_callback
*ecbdata
)
2275 if (ecbdata
->diff_words
) {
2276 diff_words_flush(ecbdata
);
2277 free (ecbdata
->diff_words
->opt
->emitted_symbols
);
2278 free (ecbdata
->diff_words
->opt
);
2279 free (ecbdata
->diff_words
->minus
.text
.ptr
);
2280 free (ecbdata
->diff_words
->minus
.orig
);
2281 free (ecbdata
->diff_words
->plus
.text
.ptr
);
2282 free (ecbdata
->diff_words
->plus
.orig
);
2283 if (ecbdata
->diff_words
->word_regex
) {
2284 regfree(ecbdata
->diff_words
->word_regex
);
2285 free(ecbdata
->diff_words
->word_regex
);
2287 FREE_AND_NULL(ecbdata
->diff_words
);
2291 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
2293 if (want_color(diff_use_color
))
2294 return diff_colors
[ix
];
2298 const char *diff_line_prefix(struct diff_options
*opt
)
2300 struct strbuf
*msgbuf
;
2301 if (!opt
->output_prefix
)
2304 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
2308 static unsigned long sane_truncate_line(char *line
, unsigned long len
)
2311 unsigned long allot
;
2317 (void) utf8_width(&cp
, &l
);
2319 break; /* truncated in the middle? */
2324 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
2327 ecbdata
->lno_in_preimage
= 0;
2328 ecbdata
->lno_in_postimage
= 0;
2329 p
= strchr(line
, '-');
2331 return; /* cannot happen */
2332 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
2335 return; /* cannot happen */
2336 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
2339 static void fn_out_consume(void *priv
, char *line
, unsigned long len
)
2341 struct emit_callback
*ecbdata
= priv
;
2342 struct diff_options
*o
= ecbdata
->opt
;
2344 o
->found_changes
= 1;
2346 if (ecbdata
->header
) {
2347 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
2348 ecbdata
->header
->buf
, ecbdata
->header
->len
, 0);
2349 strbuf_reset(ecbdata
->header
);
2350 ecbdata
->header
= NULL
;
2353 if (ecbdata
->label_path
[0]) {
2354 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
2355 ecbdata
->label_path
[0],
2356 strlen(ecbdata
->label_path
[0]), 0);
2357 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
2358 ecbdata
->label_path
[1],
2359 strlen(ecbdata
->label_path
[1]), 0);
2360 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
2363 if (diff_suppress_blank_empty
2364 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
2369 if (line
[0] == '@') {
2370 if (ecbdata
->diff_words
)
2371 diff_words_flush(ecbdata
);
2372 len
= sane_truncate_line(line
, len
);
2373 find_lno(line
, ecbdata
);
2374 emit_hunk_header(ecbdata
, line
, len
);
2378 if (ecbdata
->diff_words
) {
2379 enum diff_symbol s
=
2380 ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
?
2381 DIFF_SYMBOL_WORDS_PORCELAIN
: DIFF_SYMBOL_WORDS
;
2382 if (line
[0] == '-') {
2383 diff_words_append(line
, len
,
2384 &ecbdata
->diff_words
->minus
);
2386 } else if (line
[0] == '+') {
2387 diff_words_append(line
, len
,
2388 &ecbdata
->diff_words
->plus
);
2390 } else if (starts_with(line
, "\\ ")) {
2392 * Eat the "no newline at eof" marker as if we
2393 * saw a "+" or "-" line with nothing on it,
2394 * and return without diff_words_flush() to
2395 * defer processing. If this is the end of
2396 * preimage, more "+" lines may come after it.
2400 diff_words_flush(ecbdata
);
2401 emit_diff_symbol(o
, s
, line
, len
, 0);
2407 ecbdata
->lno_in_postimage
++;
2408 emit_add_line(ecbdata
, line
+ 1, len
- 1);
2411 ecbdata
->lno_in_preimage
++;
2412 emit_del_line(ecbdata
, line
+ 1, len
- 1);
2415 ecbdata
->lno_in_postimage
++;
2416 ecbdata
->lno_in_preimage
++;
2417 emit_context_line(ecbdata
, line
+ 1, len
- 1);
2420 /* incomplete line at the end */
2421 ecbdata
->lno_in_preimage
++;
2422 emit_diff_symbol(o
, DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
2428 static void pprint_rename(struct strbuf
*name
, const char *a
, const char *b
)
2430 const char *old_name
= a
;
2431 const char *new_name
= b
;
2432 int pfx_length
, sfx_length
;
2433 int pfx_adjust_for_slash
;
2434 int len_a
= strlen(a
);
2435 int len_b
= strlen(b
);
2436 int a_midlen
, b_midlen
;
2437 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
2438 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
2440 if (qlen_a
|| qlen_b
) {
2441 quote_c_style(a
, name
, NULL
, 0);
2442 strbuf_addstr(name
, " => ");
2443 quote_c_style(b
, name
, NULL
, 0);
2447 /* Find common prefix */
2449 while (*old_name
&& *new_name
&& *old_name
== *new_name
) {
2450 if (*old_name
== '/')
2451 pfx_length
= old_name
- a
+ 1;
2456 /* Find common suffix */
2457 old_name
= a
+ len_a
;
2458 new_name
= b
+ len_b
;
2461 * If there is a common prefix, it must end in a slash. In
2462 * that case we let this loop run 1 into the prefix to see the
2465 * If there is no common prefix, we cannot do this as it would
2466 * underrun the input strings.
2468 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
2469 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old_name
&&
2470 b
+ pfx_length
- pfx_adjust_for_slash
<= new_name
&&
2471 *old_name
== *new_name
) {
2472 if (*old_name
== '/')
2473 sfx_length
= len_a
- (old_name
- a
);
2479 * pfx{mid-a => mid-b}sfx
2480 * {pfx-a => pfx-b}sfx
2481 * pfx{sfx-a => sfx-b}
2484 a_midlen
= len_a
- pfx_length
- sfx_length
;
2485 b_midlen
= len_b
- pfx_length
- sfx_length
;
2491 strbuf_grow(name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
2492 if (pfx_length
+ sfx_length
) {
2493 strbuf_add(name
, a
, pfx_length
);
2494 strbuf_addch(name
, '{');
2496 strbuf_add(name
, a
+ pfx_length
, a_midlen
);
2497 strbuf_addstr(name
, " => ");
2498 strbuf_add(name
, b
+ pfx_length
, b_midlen
);
2499 if (pfx_length
+ sfx_length
) {
2500 strbuf_addch(name
, '}');
2501 strbuf_add(name
, a
+ len_a
- sfx_length
, sfx_length
);
2505 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
2509 struct diffstat_file
*x
;
2511 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
2512 diffstat
->files
[diffstat
->nr
++] = x
;
2514 x
->from_name
= xstrdup(name_a
);
2515 x
->name
= xstrdup(name_b
);
2519 x
->from_name
= NULL
;
2520 x
->name
= xstrdup(name_a
);
2525 static void diffstat_consume(void *priv
, char *line
, unsigned long len
)
2527 struct diffstat_t
*diffstat
= priv
;
2528 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
2532 else if (line
[0] == '-')
2536 const char mime_boundary_leader
[] = "------------";
2538 static int scale_linear(int it
, int width
, int max_change
)
2543 * make sure that at least one '-' or '+' is printed if
2544 * there is any change to this path. The easiest way is to
2545 * scale linearly as if the allotted width is one column shorter
2546 * than it is, and then add 1 to the result.
2548 return 1 + (it
* (width
- 1) / max_change
);
2551 static void show_graph(struct strbuf
*out
, char ch
, int cnt
,
2552 const char *set
, const char *reset
)
2556 strbuf_addstr(out
, set
);
2557 strbuf_addchars(out
, ch
, cnt
);
2558 strbuf_addstr(out
, reset
);
2561 static void fill_print_name(struct diffstat_file
*file
)
2563 struct strbuf pname
= STRBUF_INIT
;
2565 if (file
->print_name
)
2568 if (file
->is_renamed
)
2569 pprint_rename(&pname
, file
->from_name
, file
->name
);
2571 quote_c_style(file
->name
, &pname
, NULL
, 0);
2574 strbuf_addf(&pname
, " (%s)", file
->comments
);
2576 file
->print_name
= strbuf_detach(&pname
, NULL
);
2579 static void print_stat_summary_inserts_deletes(struct diff_options
*options
,
2580 int files
, int insertions
, int deletions
)
2582 struct strbuf sb
= STRBUF_INIT
;
2585 assert(insertions
== 0 && deletions
== 0);
2586 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
2592 (files
== 1) ? " %d file changed" : " %d files changed",
2596 * For binary diff, the caller may want to print "x files
2597 * changed" with insertions == 0 && deletions == 0.
2599 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2600 * is probably less confusing (i.e skip over "2 files changed
2601 * but nothing about added/removed lines? Is this a bug in Git?").
2603 if (insertions
|| deletions
== 0) {
2605 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2609 if (deletions
|| insertions
== 0) {
2611 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2614 strbuf_addch(&sb
, '\n');
2615 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
2617 strbuf_release(&sb
);
2620 void print_stat_summary(FILE *fp
, int files
,
2621 int insertions
, int deletions
)
2623 struct diff_options o
;
2624 memset(&o
, 0, sizeof(o
));
2627 print_stat_summary_inserts_deletes(&o
, files
, insertions
, deletions
);
2630 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
2632 int i
, len
, add
, del
, adds
= 0, dels
= 0;
2633 uintmax_t max_change
= 0, max_len
= 0;
2634 int total_files
= data
->nr
, count
;
2635 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
2636 const char *reset
, *add_c
, *del_c
;
2637 int extra_shown
= 0;
2638 const char *line_prefix
= diff_line_prefix(options
);
2639 struct strbuf out
= STRBUF_INIT
;
2644 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
2646 reset
= diff_get_color_opt(options
, DIFF_RESET
);
2647 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
2648 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
2651 * Find the longest filename and max number of changes
2653 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
2654 struct diffstat_file
*file
= data
->files
[i
];
2655 uintmax_t change
= file
->added
+ file
->deleted
;
2657 if (!file
->is_interesting
&& (change
== 0)) {
2658 count
++; /* not shown == room for one more */
2661 fill_print_name(file
);
2662 len
= strlen(file
->print_name
);
2666 if (file
->is_unmerged
) {
2667 /* "Unmerged" is 8 characters */
2668 bin_width
= bin_width
< 8 ? 8 : bin_width
;
2671 if (file
->is_binary
) {
2672 /* "Bin XXX -> YYY bytes" */
2673 int w
= 14 + decimal_width(file
->added
)
2674 + decimal_width(file
->deleted
);
2675 bin_width
= bin_width
< w
? w
: bin_width
;
2676 /* Display change counts aligned with "Bin" */
2681 if (max_change
< change
)
2682 max_change
= change
;
2684 count
= i
; /* where we can stop scanning in data->files[] */
2687 * We have width = stat_width or term_columns() columns total.
2688 * We want a maximum of min(max_len, stat_name_width) for the name part.
2689 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2690 * We also need 1 for " " and 4 + decimal_width(max_change)
2691 * for " | NNNN " and one the empty column at the end, altogether
2692 * 6 + decimal_width(max_change).
2694 * If there's not enough space, we will use the smaller of
2695 * stat_name_width (if set) and 5/8*width for the filename,
2696 * and the rest for constant elements + graph part, but no more
2697 * than stat_graph_width for the graph part.
2698 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2699 * for the standard terminal size).
2701 * In other words: stat_width limits the maximum width, and
2702 * stat_name_width fixes the maximum width of the filename,
2703 * and is also used to divide available columns if there
2706 * Binary files are displayed with "Bin XXX -> YYY bytes"
2707 * instead of the change count and graph. This part is treated
2708 * similarly to the graph part, except that it is not
2709 * "scaled". If total width is too small to accommodate the
2710 * guaranteed minimum width of the filename part and the
2711 * separators and this message, this message will "overflow"
2712 * making the line longer than the maximum width.
2715 if (options
->stat_width
== -1)
2716 width
= term_columns() - strlen(line_prefix
);
2718 width
= options
->stat_width
? options
->stat_width
: 80;
2719 number_width
= decimal_width(max_change
) > number_width
?
2720 decimal_width(max_change
) : number_width
;
2722 if (options
->stat_graph_width
== -1)
2723 options
->stat_graph_width
= diff_stat_graph_width
;
2726 * Guarantee 3/8*16==6 for the graph part
2727 * and 5/8*16==10 for the filename part
2729 if (width
< 16 + 6 + number_width
)
2730 width
= 16 + 6 + number_width
;
2733 * First assign sizes that are wanted, ignoring available width.
2734 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2735 * starting from "XXX" should fit in graph_width.
2737 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
2738 if (options
->stat_graph_width
&&
2739 options
->stat_graph_width
< graph_width
)
2740 graph_width
= options
->stat_graph_width
;
2742 name_width
= (options
->stat_name_width
> 0 &&
2743 options
->stat_name_width
< max_len
) ?
2744 options
->stat_name_width
: max_len
;
2747 * Adjust adjustable widths not to exceed maximum width
2749 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
2750 if (graph_width
> width
* 3/8 - number_width
- 6) {
2751 graph_width
= width
* 3/8 - number_width
- 6;
2752 if (graph_width
< 6)
2756 if (options
->stat_graph_width
&&
2757 graph_width
> options
->stat_graph_width
)
2758 graph_width
= options
->stat_graph_width
;
2759 if (name_width
> width
- number_width
- 6 - graph_width
)
2760 name_width
= width
- number_width
- 6 - graph_width
;
2762 graph_width
= width
- number_width
- 6 - name_width
;
2766 * From here name_width is the width of the name area,
2767 * and graph_width is the width of the graph area.
2768 * max_change is used to scale graph properly.
2770 for (i
= 0; i
< count
; i
++) {
2771 const char *prefix
= "";
2772 struct diffstat_file
*file
= data
->files
[i
];
2773 char *name
= file
->print_name
;
2774 uintmax_t added
= file
->added
;
2775 uintmax_t deleted
= file
->deleted
;
2778 if (!file
->is_interesting
&& (added
+ deleted
== 0))
2782 * "scale" the filename
2785 name_len
= strlen(name
);
2786 if (name_width
< name_len
) {
2790 name
+= name_len
- len
;
2791 slash
= strchr(name
, '/');
2796 if (file
->is_binary
) {
2797 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2798 strbuf_addf(&out
, " %*s", number_width
, "Bin");
2799 if (!added
&& !deleted
) {
2800 strbuf_addch(&out
, '\n');
2801 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2802 out
.buf
, out
.len
, 0);
2806 strbuf_addf(&out
, " %s%"PRIuMAX
"%s",
2807 del_c
, deleted
, reset
);
2808 strbuf_addstr(&out
, " -> ");
2809 strbuf_addf(&out
, "%s%"PRIuMAX
"%s",
2810 add_c
, added
, reset
);
2811 strbuf_addstr(&out
, " bytes\n");
2812 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2813 out
.buf
, out
.len
, 0);
2817 else if (file
->is_unmerged
) {
2818 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2819 strbuf_addstr(&out
, " Unmerged\n");
2820 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2821 out
.buf
, out
.len
, 0);
2827 * scale the add/delete
2832 if (graph_width
<= max_change
) {
2833 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
2834 if (total
< 2 && add
&& del
)
2835 /* width >= 2 due to the sanity check */
2838 add
= scale_linear(add
, graph_width
, max_change
);
2841 del
= scale_linear(del
, graph_width
, max_change
);
2845 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2846 strbuf_addf(&out
, " %*"PRIuMAX
"%s",
2847 number_width
, added
+ deleted
,
2848 added
+ deleted
? " " : "");
2849 show_graph(&out
, '+', add
, add_c
, reset
);
2850 show_graph(&out
, '-', del
, del_c
, reset
);
2851 strbuf_addch(&out
, '\n');
2852 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2853 out
.buf
, out
.len
, 0);
2857 for (i
= 0; i
< data
->nr
; i
++) {
2858 struct diffstat_file
*file
= data
->files
[i
];
2859 uintmax_t added
= file
->added
;
2860 uintmax_t deleted
= file
->deleted
;
2862 if (file
->is_unmerged
||
2863 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
2868 if (!file
->is_binary
) {
2875 emit_diff_symbol(options
,
2876 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
2881 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2882 strbuf_release(&out
);
2885 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
2887 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
2892 for (i
= 0; i
< data
->nr
; i
++) {
2893 int added
= data
->files
[i
]->added
;
2894 int deleted
= data
->files
[i
]->deleted
;
2896 if (data
->files
[i
]->is_unmerged
||
2897 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
2899 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
2904 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2907 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
2914 for (i
= 0; i
< data
->nr
; i
++) {
2915 struct diffstat_file
*file
= data
->files
[i
];
2917 fprintf(options
->file
, "%s", diff_line_prefix(options
));
2919 if (file
->is_binary
)
2920 fprintf(options
->file
, "-\t-\t");
2922 fprintf(options
->file
,
2923 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
2924 file
->added
, file
->deleted
);
2925 if (options
->line_termination
) {
2926 fill_print_name(file
);
2927 if (!file
->is_renamed
)
2928 write_name_quoted(file
->name
, options
->file
,
2929 options
->line_termination
);
2931 fputs(file
->print_name
, options
->file
);
2932 putc(options
->line_termination
, options
->file
);
2935 if (file
->is_renamed
) {
2936 putc('\0', options
->file
);
2937 write_name_quoted(file
->from_name
, options
->file
, '\0');
2939 write_name_quoted(file
->name
, options
->file
, '\0');
2944 struct dirstat_file
{
2946 unsigned long changed
;
2949 struct dirstat_dir
{
2950 struct dirstat_file
*files
;
2951 int alloc
, nr
, permille
, cumulative
;
2954 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
2955 unsigned long changed
, const char *base
, int baselen
)
2957 unsigned long sum_changes
= 0;
2958 unsigned int sources
= 0;
2959 const char *line_prefix
= diff_line_prefix(opt
);
2962 struct dirstat_file
*f
= dir
->files
;
2963 int namelen
= strlen(f
->name
);
2964 unsigned long changes
;
2967 if (namelen
< baselen
)
2969 if (memcmp(f
->name
, base
, baselen
))
2971 slash
= strchr(f
->name
+ baselen
, '/');
2973 int newbaselen
= slash
+ 1 - f
->name
;
2974 changes
= gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
2977 changes
= f
->changed
;
2982 sum_changes
+= changes
;
2986 * We don't report dirstat's for
2988 * - or cases where everything came from a single directory
2989 * under this directory (sources == 1).
2991 if (baselen
&& sources
!= 1) {
2993 int permille
= sum_changes
* 1000 / changed
;
2994 if (permille
>= dir
->permille
) {
2995 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
2996 permille
/ 10, permille
% 10, baselen
, base
);
2997 if (!dir
->cumulative
)
3005 static int dirstat_compare(const void *_a
, const void *_b
)
3007 const struct dirstat_file
*a
= _a
;
3008 const struct dirstat_file
*b
= _b
;
3009 return strcmp(a
->name
, b
->name
);
3012 static void show_dirstat(struct diff_options
*options
)
3015 unsigned long changed
;
3016 struct dirstat_dir dir
;
3017 struct diff_queue_struct
*q
= &diff_queued_diff
;
3022 dir
.permille
= options
->dirstat_permille
;
3023 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3026 for (i
= 0; i
< q
->nr
; i
++) {
3027 struct diff_filepair
*p
= q
->queue
[i
];
3029 unsigned long copied
, added
, damage
;
3030 struct diff_populate_filespec_options dpf_options
= {
3031 .check_size_only
= 1,
3034 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
3036 if (p
->one
->oid_valid
&& p
->two
->oid_valid
&&
3037 oideq(&p
->one
->oid
, &p
->two
->oid
)) {
3039 * The SHA1 has not changed, so pre-/post-content is
3040 * identical. We can therefore skip looking at the
3041 * file contents altogether.
3047 if (options
->flags
.dirstat_by_file
) {
3049 * In --dirstat-by-file mode, we don't really need to
3050 * look at the actual file contents at all.
3051 * The fact that the SHA1 changed is enough for us to
3052 * add this file to the list of results
3053 * (with each file contributing equal damage).
3059 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
3060 diff_populate_filespec(options
->repo
, p
->one
, NULL
);
3061 diff_populate_filespec(options
->repo
, p
->two
, NULL
);
3062 diffcore_count_changes(options
->repo
,
3063 p
->one
, p
->two
, NULL
, NULL
,
3065 diff_free_filespec_data(p
->one
);
3066 diff_free_filespec_data(p
->two
);
3067 } else if (DIFF_FILE_VALID(p
->one
)) {
3068 diff_populate_filespec(options
->repo
, p
->one
, &dpf_options
);
3070 diff_free_filespec_data(p
->one
);
3071 } else if (DIFF_FILE_VALID(p
->two
)) {
3072 diff_populate_filespec(options
->repo
, p
->two
, &dpf_options
);
3074 added
= p
->two
->size
;
3075 diff_free_filespec_data(p
->two
);
3080 * Original minus copied is the removed material,
3081 * added is the new material. They are both damages
3082 * made to the preimage.
3083 * If the resulting damage is zero, we know that
3084 * diffcore_count_changes() considers the two entries to
3085 * be identical, but since the oid changed, we
3086 * know that there must have been _some_ kind of change,
3087 * so we force all entries to have damage > 0.
3089 damage
= (p
->one
->size
- copied
) + added
;
3094 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3095 dir
.files
[dir
.nr
].name
= name
;
3096 dir
.files
[dir
.nr
].changed
= damage
;
3101 /* This can happen even with many files, if everything was renames */
3105 /* Show all directories with more than x% of the changes */
3106 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
3107 gather_dirstat(options
, &dir
, changed
, "", 0);
3110 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
3113 unsigned long changed
;
3114 struct dirstat_dir dir
;
3122 dir
.permille
= options
->dirstat_permille
;
3123 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3126 for (i
= 0; i
< data
->nr
; i
++) {
3127 struct diffstat_file
*file
= data
->files
[i
];
3128 unsigned long damage
= file
->added
+ file
->deleted
;
3129 if (file
->is_binary
)
3131 * binary files counts bytes, not lines. Must find some
3132 * way to normalize binary bytes vs. textual lines.
3133 * The following heuristic assumes that there are 64
3135 * This is stupid and ugly, but very cheap...
3137 damage
= DIV_ROUND_UP(damage
, 64);
3138 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3139 dir
.files
[dir
.nr
].name
= file
->name
;
3140 dir
.files
[dir
.nr
].changed
= damage
;
3145 /* This can happen even with many files, if everything was renames */
3149 /* Show all directories with more than x% of the changes */
3150 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
3151 gather_dirstat(options
, &dir
, changed
, "", 0);
3154 static void free_diffstat_file(struct diffstat_file
*f
)
3156 free(f
->print_name
);
3162 void free_diffstat_info(struct diffstat_t
*diffstat
)
3165 for (i
= 0; i
< diffstat
->nr
; i
++)
3166 free_diffstat_file(diffstat
->files
[i
]);
3167 free(diffstat
->files
);
3170 struct checkdiff_t
{
3171 const char *filename
;
3173 int conflict_marker_size
;
3174 struct diff_options
*o
;
3179 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
3184 if (len
< marker_size
+ 1)
3186 firstchar
= line
[0];
3187 switch (firstchar
) {
3188 case '=': case '>': case '<': case '|':
3193 for (cnt
= 1; cnt
< marker_size
; cnt
++)
3194 if (line
[cnt
] != firstchar
)
3196 /* line[1] through line[marker_size-1] are same as firstchar */
3197 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
3202 static void checkdiff_consume_hunk(void *priv
,
3203 long ob
, long on
, long nb
, long nn
,
3204 const char *func
, long funclen
)
3207 struct checkdiff_t
*data
= priv
;
3208 data
->lineno
= nb
- 1;
3211 static void checkdiff_consume(void *priv
, char *line
, unsigned long len
)
3213 struct checkdiff_t
*data
= priv
;
3214 int marker_size
= data
->conflict_marker_size
;
3215 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
3216 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
3217 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
3219 const char *line_prefix
;
3222 line_prefix
= diff_line_prefix(data
->o
);
3224 if (line
[0] == '+') {
3227 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
3229 fprintf(data
->o
->file
,
3230 "%s%s:%d: leftover conflict marker\n",
3231 line_prefix
, data
->filename
, data
->lineno
);
3233 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
3236 data
->status
|= bad
;
3237 err
= whitespace_error_string(bad
);
3238 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
3239 line_prefix
, data
->filename
, data
->lineno
, err
);
3241 emit_line(data
->o
, set
, reset
, line
, 1);
3242 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
3243 data
->o
->file
, set
, reset
, ws
);
3244 } else if (line
[0] == ' ') {
3249 static unsigned char *deflate_it(char *data
,
3251 unsigned long *result_size
)
3254 unsigned char *deflated
;
3257 git_deflate_init(&stream
, zlib_compression_level
);
3258 bound
= git_deflate_bound(&stream
, size
);
3259 deflated
= xmalloc(bound
);
3260 stream
.next_out
= deflated
;
3261 stream
.avail_out
= bound
;
3263 stream
.next_in
= (unsigned char *)data
;
3264 stream
.avail_in
= size
;
3265 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
3267 git_deflate_end(&stream
);
3268 *result_size
= stream
.total_out
;
3272 static void emit_binary_diff_body(struct diff_options
*o
,
3273 mmfile_t
*one
, mmfile_t
*two
)
3279 unsigned long orig_size
;
3280 unsigned long delta_size
;
3281 unsigned long deflate_size
;
3282 unsigned long data_size
;
3284 /* We could do deflated delta, or we could do just deflated two,
3285 * whichever is smaller.
3288 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
3289 if (one
->size
&& two
->size
) {
3290 delta
= diff_delta(one
->ptr
, one
->size
,
3291 two
->ptr
, two
->size
,
3292 &delta_size
, deflate_size
);
3294 void *to_free
= delta
;
3295 orig_size
= delta_size
;
3296 delta
= deflate_it(delta
, delta_size
, &delta_size
);
3301 if (delta
&& delta_size
< deflate_size
) {
3302 char *s
= xstrfmt("%"PRIuMAX
, (uintmax_t)orig_size
);
3303 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
3308 data_size
= delta_size
;
3310 char *s
= xstrfmt("%lu", two
->size
);
3311 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
3316 data_size
= deflate_size
;
3319 /* emit data encoded in base85 */
3323 int bytes
= (52 < data_size
) ? 52 : data_size
;
3327 line
[0] = bytes
+ 'A' - 1;
3329 line
[0] = bytes
- 26 + 'a' - 1;
3330 encode_85(line
+ 1, cp
, bytes
);
3331 cp
= (char *) cp
+ bytes
;
3337 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_BODY
,
3340 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_FOOTER
, NULL
, 0, 0);
3344 static void emit_binary_diff(struct diff_options
*o
,
3345 mmfile_t
*one
, mmfile_t
*two
)
3347 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER
, NULL
, 0, 0);
3348 emit_binary_diff_body(o
, one
, two
);
3349 emit_binary_diff_body(o
, two
, one
);
3352 int diff_filespec_is_binary(struct repository
*r
,
3353 struct diff_filespec
*one
)
3355 struct diff_populate_filespec_options dpf_options
= {
3359 if (one
->is_binary
== -1) {
3360 diff_filespec_load_driver(one
, r
->index
);
3361 if (one
->driver
->binary
!= -1)
3362 one
->is_binary
= one
->driver
->binary
;
3364 if (!one
->data
&& DIFF_FILE_VALID(one
))
3365 diff_populate_filespec(r
, one
, &dpf_options
);
3366 if (one
->is_binary
== -1 && one
->data
)
3367 one
->is_binary
= buffer_is_binary(one
->data
,
3369 if (one
->is_binary
== -1)
3373 return one
->is_binary
;
3376 static const struct userdiff_funcname
*
3377 diff_funcname_pattern(struct diff_options
*o
, struct diff_filespec
*one
)
3379 diff_filespec_load_driver(one
, o
->repo
->index
);
3380 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
3383 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
3385 if (!options
->a_prefix
)
3386 options
->a_prefix
= a
;
3387 if (!options
->b_prefix
)
3388 options
->b_prefix
= b
;
3391 struct userdiff_driver
*get_textconv(struct repository
*r
,
3392 struct diff_filespec
*one
)
3394 if (!DIFF_FILE_VALID(one
))
3397 diff_filespec_load_driver(one
, r
->index
);
3398 return userdiff_get_textconv(r
, one
->driver
);
3401 static void builtin_diff(const char *name_a
,
3403 struct diff_filespec
*one
,
3404 struct diff_filespec
*two
,
3405 const char *xfrm_msg
,
3406 int must_show_header
,
3407 struct diff_options
*o
,
3408 int complete_rewrite
)
3412 char *a_one
, *b_two
;
3413 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
3414 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
3415 const char *a_prefix
, *b_prefix
;
3416 struct userdiff_driver
*textconv_one
= NULL
;
3417 struct userdiff_driver
*textconv_two
= NULL
;
3418 struct strbuf header
= STRBUF_INIT
;
3419 const char *line_prefix
= diff_line_prefix(o
);
3421 diff_set_mnemonic_prefix(o
, "a/", "b/");
3422 if (o
->flags
.reverse_diff
) {
3423 a_prefix
= o
->b_prefix
;
3424 b_prefix
= o
->a_prefix
;
3426 a_prefix
= o
->a_prefix
;
3427 b_prefix
= o
->b_prefix
;
3430 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
3431 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3432 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
3433 show_submodule_diff_summary(o
, one
->path
? one
->path
: two
->path
,
3434 &one
->oid
, &two
->oid
,
3435 two
->dirty_submodule
);
3437 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
3438 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3439 (!two
->mode
|| S_ISGITLINK(two
->mode
))) {
3440 show_submodule_inline_diff(o
, one
->path
? one
->path
: two
->path
,
3441 &one
->oid
, &two
->oid
,
3442 two
->dirty_submodule
);
3446 if (o
->flags
.allow_textconv
) {
3447 textconv_one
= get_textconv(o
->repo
, one
);
3448 textconv_two
= get_textconv(o
->repo
, two
);
3451 /* Never use a non-valid filename anywhere if at all possible */
3452 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
3453 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
3455 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
3456 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
3457 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
3458 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
3459 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3460 if (lbl
[0][0] == '/') {
3462 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3464 strbuf_addstr(&header
, xfrm_msg
);
3465 must_show_header
= 1;
3467 else if (lbl
[1][0] == '/') {
3468 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3470 strbuf_addstr(&header
, xfrm_msg
);
3471 must_show_header
= 1;
3474 if (one
->mode
!= two
->mode
) {
3475 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3476 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3477 must_show_header
= 1;
3480 strbuf_addstr(&header
, xfrm_msg
);
3483 * we do not run diff between different kind
3486 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3487 goto free_ab_and_return
;
3488 if (complete_rewrite
&&
3489 (textconv_one
|| !diff_filespec_is_binary(o
->repo
, one
)) &&
3490 (textconv_two
|| !diff_filespec_is_binary(o
->repo
, two
))) {
3491 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3492 header
.buf
, header
.len
, 0);
3493 strbuf_reset(&header
);
3494 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3495 textconv_one
, textconv_two
, o
);
3496 o
->found_changes
= 1;
3497 goto free_ab_and_return
;
3501 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3502 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3504 strbuf_reset(&header
);
3505 goto free_ab_and_return
;
3506 } else if (!o
->flags
.text
&&
3507 ( (!textconv_one
&& diff_filespec_is_binary(o
->repo
, one
)) ||
3508 (!textconv_two
&& diff_filespec_is_binary(o
->repo
, two
)) )) {
3509 struct strbuf sb
= STRBUF_INIT
;
3510 if (!one
->data
&& !two
->data
&&
3511 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3513 if (oideq(&one
->oid
, &two
->oid
)) {
3514 if (must_show_header
)
3515 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3516 header
.buf
, header
.len
,
3518 goto free_ab_and_return
;
3520 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3521 header
.buf
, header
.len
, 0);
3522 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3523 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3524 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3526 strbuf_release(&sb
);
3527 goto free_ab_and_return
;
3529 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3530 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3531 die("unable to read files to diff");
3532 /* Quite common confusing case */
3533 if (mf1
.size
== mf2
.size
&&
3534 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3535 if (must_show_header
)
3536 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3537 header
.buf
, header
.len
, 0);
3538 goto free_ab_and_return
;
3540 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3541 strbuf_reset(&header
);
3542 if (o
->flags
.binary
)
3543 emit_binary_diff(o
, &mf1
, &mf2
);
3545 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3546 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3547 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3549 strbuf_release(&sb
);
3551 o
->found_changes
= 1;
3553 /* Crazy xdl interfaces.. */
3554 const char *diffopts
;
3558 struct emit_callback ecbdata
;
3559 const struct userdiff_funcname
*pe
;
3561 if (must_show_header
) {
3562 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3563 header
.buf
, header
.len
, 0);
3564 strbuf_reset(&header
);
3567 mf1
.size
= fill_textconv(o
->repo
, textconv_one
, one
, &mf1
.ptr
);
3568 mf2
.size
= fill_textconv(o
->repo
, textconv_two
, two
, &mf2
.ptr
);
3570 pe
= diff_funcname_pattern(o
, one
);
3572 pe
= diff_funcname_pattern(o
, two
);
3574 memset(&xpp
, 0, sizeof(xpp
));
3575 memset(&xecfg
, 0, sizeof(xecfg
));
3576 memset(&ecbdata
, 0, sizeof(ecbdata
));
3577 if (o
->flags
.suppress_diff_headers
)
3579 ecbdata
.label_path
= lbl
;
3580 ecbdata
.color_diff
= want_color(o
->use_color
);
3581 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
3582 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3583 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3585 if (header
.len
&& !o
->flags
.suppress_diff_headers
)
3586 ecbdata
.header
= &header
;
3587 xpp
.flags
= o
->xdl_opts
;
3588 xpp
.ignore_regex
= o
->ignore_regex
;
3589 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3590 xpp
.anchors
= o
->anchors
;
3591 xpp
.anchors_nr
= o
->anchors_nr
;
3592 xecfg
.ctxlen
= o
->context
;
3593 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3594 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3595 if (o
->flags
.funccontext
)
3596 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3598 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3600 diffopts
= getenv("GIT_DIFF_OPTS");
3603 else if (skip_prefix(diffopts
, "--unified=", &v
))
3604 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3605 else if (skip_prefix(diffopts
, "-u", &v
))
3606 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3609 init_diff_words_data(&ecbdata
, o
, one
, two
);
3610 if (xdi_diff_outf(&mf1
, &mf2
, NULL
, fn_out_consume
,
3611 &ecbdata
, &xpp
, &xecfg
))
3612 die("unable to generate diff for %s", one
->path
);
3614 free_diff_words_data(&ecbdata
);
3619 xdiff_clear_find_func(&xecfg
);
3623 strbuf_release(&header
);
3624 diff_free_filespec_data(one
);
3625 diff_free_filespec_data(two
);
3631 static char *get_compact_summary(const struct diff_filepair
*p
, int is_renamed
)
3634 if (p
->status
== DIFF_STATUS_ADDED
) {
3635 if (S_ISLNK(p
->two
->mode
))
3637 else if ((p
->two
->mode
& 0777) == 0755)
3641 } else if (p
->status
== DIFF_STATUS_DELETED
)
3644 if (S_ISLNK(p
->one
->mode
) && !S_ISLNK(p
->two
->mode
))
3646 else if (!S_ISLNK(p
->one
->mode
) && S_ISLNK(p
->two
->mode
))
3648 else if ((p
->one
->mode
& 0777) == 0644 &&
3649 (p
->two
->mode
& 0777) == 0755)
3651 else if ((p
->one
->mode
& 0777) == 0755 &&
3652 (p
->two
->mode
& 0777) == 0644)
3657 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3658 struct diff_filespec
*one
,
3659 struct diff_filespec
*two
,
3660 struct diffstat_t
*diffstat
,
3661 struct diff_options
*o
,
3662 struct diff_filepair
*p
)
3665 struct diffstat_file
*data
;
3667 int complete_rewrite
= 0;
3669 if (!DIFF_PAIR_UNMERGED(p
)) {
3670 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3671 complete_rewrite
= 1;
3674 data
= diffstat_add(diffstat
, name_a
, name_b
);
3675 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3676 if (o
->flags
.stat_with_summary
)
3677 data
->comments
= get_compact_summary(p
, data
->is_renamed
);
3680 data
->is_unmerged
= 1;
3684 /* saves some reads if true, not a guarantee of diff outcome */
3685 may_differ
= !(one
->oid_valid
&& two
->oid_valid
&&
3686 oideq(&one
->oid
, &two
->oid
));
3688 if (diff_filespec_is_binary(o
->repo
, one
) ||
3689 diff_filespec_is_binary(o
->repo
, two
)) {
3690 data
->is_binary
= 1;
3695 data
->added
= diff_filespec_size(o
->repo
, two
);
3696 data
->deleted
= diff_filespec_size(o
->repo
, one
);
3700 else if (complete_rewrite
) {
3701 diff_populate_filespec(o
->repo
, one
, NULL
);
3702 diff_populate_filespec(o
->repo
, two
, NULL
);
3703 data
->deleted
= count_lines(one
->data
, one
->size
);
3704 data
->added
= count_lines(two
->data
, two
->size
);
3707 else if (may_differ
) {
3708 /* Crazy xdl interfaces.. */
3712 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3713 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3714 die("unable to read files to diff");
3716 memset(&xpp
, 0, sizeof(xpp
));
3717 memset(&xecfg
, 0, sizeof(xecfg
));
3718 xpp
.flags
= o
->xdl_opts
;
3719 xpp
.ignore_regex
= o
->ignore_regex
;
3720 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3721 xpp
.anchors
= o
->anchors
;
3722 xpp
.anchors_nr
= o
->anchors_nr
;
3723 xecfg
.ctxlen
= o
->context
;
3724 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3725 if (xdi_diff_outf(&mf1
, &mf2
, discard_hunk_line
,
3726 diffstat_consume
, diffstat
, &xpp
, &xecfg
))
3727 die("unable to generate diffstat for %s", one
->path
);
3729 if (DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
)) {
3730 struct diffstat_file
*file
=
3731 diffstat
->files
[diffstat
->nr
- 1];
3733 * Omit diffstats of modified files where nothing changed.
3734 * Even if may_differ, this might be the case due to
3735 * ignoring whitespace changes, etc.
3737 * But note that we special-case additions, deletions,
3738 * renames, and mode changes as adding an empty file,
3739 * for example is still of interest.
3741 if ((p
->status
== DIFF_STATUS_MODIFIED
)
3744 && one
->mode
== two
->mode
) {
3745 free_diffstat_file(file
);
3751 diff_free_filespec_data(one
);
3752 diff_free_filespec_data(two
);
3755 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3756 const char *attr_path
,
3757 struct diff_filespec
*one
,
3758 struct diff_filespec
*two
,
3759 struct diff_options
*o
)
3762 struct checkdiff_t data
;
3767 memset(&data
, 0, sizeof(data
));
3768 data
.filename
= name_b
? name_b
: name_a
;
3771 data
.ws_rule
= whitespace_rule(o
->repo
->index
, attr_path
);
3772 data
.conflict_marker_size
= ll_merge_marker_size(o
->repo
->index
, attr_path
);
3774 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3775 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3776 die("unable to read files to diff");
3779 * All the other codepaths check both sides, but not checking
3780 * the "old" side here is deliberate. We are checking the newly
3781 * introduced changes, and as long as the "new" side is text, we
3782 * can and should check what it introduces.
3784 if (diff_filespec_is_binary(o
->repo
, two
))
3785 goto free_and_return
;
3787 /* Crazy xdl interfaces.. */
3791 memset(&xpp
, 0, sizeof(xpp
));
3792 memset(&xecfg
, 0, sizeof(xecfg
));
3793 xecfg
.ctxlen
= 1; /* at least one context line */
3795 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume_hunk
,
3796 checkdiff_consume
, &data
,
3798 die("unable to generate checkdiff for %s", one
->path
);
3800 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3801 struct emit_callback ecbdata
;
3804 ecbdata
.ws_rule
= data
.ws_rule
;
3805 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3806 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3811 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3812 fprintf(o
->file
, "%s:%d: %s.\n",
3813 data
.filename
, blank_at_eof
, err
);
3814 data
.status
= 1; /* report errors */
3819 diff_free_filespec_data(one
);
3820 diff_free_filespec_data(two
);
3822 o
->flags
.check_failed
= 1;
3825 struct diff_filespec
*alloc_filespec(const char *path
)
3827 struct diff_filespec
*spec
;
3829 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3831 spec
->is_binary
= -1;
3835 void free_filespec(struct diff_filespec
*spec
)
3837 if (!--spec
->count
) {
3838 diff_free_filespec_data(spec
);
3843 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3844 int oid_valid
, unsigned short mode
)
3847 spec
->mode
= canon_mode(mode
);
3848 oidcpy(&spec
->oid
, oid
);
3849 spec
->oid_valid
= oid_valid
;
3854 * Given a name and sha1 pair, if the index tells us the file in
3855 * the work tree has that object contents, return true, so that
3856 * prepare_temp_file() does not have to inflate and extract.
3858 static int reuse_worktree_file(struct index_state
*istate
,
3860 const struct object_id
*oid
,
3863 const struct cache_entry
*ce
;
3868 * We do not read the cache ourselves here, because the
3869 * benchmark with my previous version that always reads cache
3870 * shows that it makes things worse for diff-tree comparing
3871 * two linux-2.6 kernel trees in an already checked out work
3872 * tree. This is because most diff-tree comparisons deal with
3873 * only a small number of files, while reading the cache is
3874 * expensive for a large project, and its cost outweighs the
3875 * savings we get by not inflating the object to a temporary
3876 * file. Practically, this code only helps when we are used
3877 * by diff-cache --cached, which does read the cache before
3883 /* We want to avoid the working directory if our caller
3884 * doesn't need the data in a normal file, this system
3885 * is rather slow with its stat/open/mmap/close syscalls,
3886 * and the object is contained in a pack file. The pack
3887 * is probably already open and will be faster to obtain
3888 * the data through than the working directory. Loose
3889 * objects however would tend to be slower as they need
3890 * to be individually opened and inflated.
3892 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_object_pack(oid
))
3896 * Similarly, if we'd have to convert the file contents anyway, that
3897 * makes the optimization not worthwhile.
3899 if (!want_file
&& would_convert_to_git(istate
, name
))
3903 pos
= index_name_pos(istate
, name
, len
);
3906 ce
= istate
->cache
[pos
];
3909 * This is not the sha1 we are looking for, or
3910 * unreusable because it is not a regular file.
3912 if (!oideq(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
3916 * If ce is marked as "assume unchanged", there is no
3917 * guarantee that work tree matches what we are looking for.
3919 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
3923 * If ce matches the file in the work tree, we can reuse it.
3925 if (ce_uptodate(ce
) ||
3926 (!lstat(name
, &st
) && !ie_match_stat(istate
, ce
, &st
, 0)))
3932 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
3934 struct strbuf buf
= STRBUF_INIT
;
3937 /* Are we looking at the work tree? */
3938 if (s
->dirty_submodule
)
3941 strbuf_addf(&buf
, "Subproject commit %s%s\n",
3942 oid_to_hex(&s
->oid
), dirty
);
3946 strbuf_release(&buf
);
3948 s
->data
= strbuf_detach(&buf
, NULL
);
3955 * While doing rename detection and pickaxe operation, we may need to
3956 * grab the data for the blob (or file) for our own in-core comparison.
3957 * diff_filespec has data and size fields for this purpose.
3959 int diff_populate_filespec(struct repository
*r
,
3960 struct diff_filespec
*s
,
3961 const struct diff_populate_filespec_options
*options
)
3963 int size_only
= options
? options
->check_size_only
: 0;
3964 int check_binary
= options
? options
->check_binary
: 0;
3966 int conv_flags
= global_conv_flags_eol
;
3968 * demote FAIL to WARN to allow inspecting the situation
3969 * instead of refusing.
3971 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
3972 conv_flags
= CONV_EOL_RNDTRP_WARN
;
3974 if (!DIFF_FILE_VALID(s
))
3975 die("internal error: asking to populate invalid file.");
3976 if (S_ISDIR(s
->mode
))
3982 if (size_only
&& 0 < s
->size
)
3985 if (S_ISGITLINK(s
->mode
))
3986 return diff_populate_gitlink(s
, size_only
);
3988 if (!s
->oid_valid
||
3989 reuse_worktree_file(r
->index
, s
->path
, &s
->oid
, 0)) {
3990 struct strbuf buf
= STRBUF_INIT
;
3994 if (lstat(s
->path
, &st
) < 0) {
3998 s
->data
= (char *)"";
4002 s
->size
= xsize_t(st
.st_size
);
4005 if (S_ISLNK(st
.st_mode
)) {
4006 struct strbuf sb
= STRBUF_INIT
;
4008 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
4011 s
->data
= strbuf_detach(&sb
, NULL
);
4017 * Even if the caller would be happy with getting
4018 * only the size, we cannot return early at this
4019 * point if the path requires us to run the content
4022 if (size_only
&& !would_convert_to_git(r
->index
, s
->path
))
4026 * Note: this check uses xsize_t(st.st_size) that may
4027 * not be the true size of the blob after it goes
4028 * through convert_to_git(). This may not strictly be
4029 * correct, but the whole point of big_file_threshold
4030 * and is_binary check being that we want to avoid
4031 * opening the file and inspecting the contents, this
4035 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4039 fd
= open(s
->path
, O_RDONLY
);
4042 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
4044 s
->should_munmap
= 1;
4047 * Convert from working tree format to canonical git format
4049 if (convert_to_git(r
->index
, s
->path
, s
->data
, s
->size
, &buf
, conv_flags
)) {
4051 munmap(s
->data
, s
->size
);
4052 s
->should_munmap
= 0;
4053 s
->data
= strbuf_detach(&buf
, &size
);
4059 struct object_info info
= {
4063 if (!(size_only
|| check_binary
))
4065 * Set contentp, since there is no chance that merely
4066 * the size is sufficient.
4068 info
.contentp
= &s
->data
;
4070 if (options
&& options
->missing_object_cb
) {
4071 if (!oid_object_info_extended(r
, &s
->oid
, &info
,
4072 OBJECT_INFO_LOOKUP_REPLACE
|
4073 OBJECT_INFO_SKIP_FETCH_OBJECT
))
4075 options
->missing_object_cb(options
->missing_object_data
);
4077 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4078 OBJECT_INFO_LOOKUP_REPLACE
))
4079 die("unable to read %s", oid_to_hex(&s
->oid
));
4082 if (size_only
|| check_binary
) {
4085 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4090 if (!info
.contentp
) {
4091 info
.contentp
= &s
->data
;
4092 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4093 OBJECT_INFO_LOOKUP_REPLACE
))
4094 die("unable to read %s", oid_to_hex(&s
->oid
));
4101 void diff_free_filespec_blob(struct diff_filespec
*s
)
4105 else if (s
->should_munmap
)
4106 munmap(s
->data
, s
->size
);
4108 if (s
->should_free
|| s
->should_munmap
) {
4109 s
->should_free
= s
->should_munmap
= 0;
4114 void diff_free_filespec_data(struct diff_filespec
*s
)
4119 diff_free_filespec_blob(s
);
4120 FREE_AND_NULL(s
->cnt_data
);
4123 static void prep_temp_blob(struct index_state
*istate
,
4124 const char *path
, struct diff_tempfile
*temp
,
4127 const struct object_id
*oid
,
4130 struct strbuf buf
= STRBUF_INIT
;
4131 struct strbuf tempfile
= STRBUF_INIT
;
4132 char *path_dup
= xstrdup(path
);
4133 const char *base
= basename(path_dup
);
4134 struct checkout_metadata meta
;
4136 init_checkout_metadata(&meta
, NULL
, NULL
, oid
);
4138 /* Generate "XXXXXX_basename.ext" */
4139 strbuf_addstr(&tempfile
, "XXXXXX_");
4140 strbuf_addstr(&tempfile
, base
);
4142 temp
->tempfile
= mks_tempfile_ts(tempfile
.buf
, strlen(base
) + 1);
4143 if (!temp
->tempfile
)
4144 die_errno("unable to create temp-file");
4145 if (convert_to_working_tree(istate
, path
,
4146 (const char *)blob
, (size_t)size
, &buf
, &meta
)) {
4150 if (write_in_full(temp
->tempfile
->fd
, blob
, size
) < 0 ||
4151 close_tempfile_gently(temp
->tempfile
))
4152 die_errno("unable to write temp-file");
4153 temp
->name
= get_tempfile_path(temp
->tempfile
);
4154 oid_to_hex_r(temp
->hex
, oid
);
4155 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
4156 strbuf_release(&buf
);
4157 strbuf_release(&tempfile
);
4161 static struct diff_tempfile
*prepare_temp_file(struct repository
*r
,
4163 struct diff_filespec
*one
)
4165 struct diff_tempfile
*temp
= claim_diff_tempfile();
4167 if (!DIFF_FILE_VALID(one
)) {
4169 /* A '-' entry produces this for file-2, and
4170 * a '+' entry produces this for file-1.
4172 temp
->name
= "/dev/null";
4173 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
4174 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
4178 if (!S_ISGITLINK(one
->mode
) &&
4180 reuse_worktree_file(r
->index
, name
, &one
->oid
, 1))) {
4182 if (lstat(name
, &st
) < 0) {
4183 if (errno
== ENOENT
)
4184 goto not_a_valid_file
;
4185 die_errno("stat(%s)", name
);
4187 if (S_ISLNK(st
.st_mode
)) {
4188 struct strbuf sb
= STRBUF_INIT
;
4189 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
4190 die_errno("readlink(%s)", name
);
4191 prep_temp_blob(r
->index
, name
, temp
, sb
.buf
, sb
.len
,
4193 &one
->oid
: &null_oid
),
4195 one
->mode
: S_IFLNK
));
4196 strbuf_release(&sb
);
4199 /* we can borrow from the file in the work tree */
4201 if (!one
->oid_valid
)
4202 oid_to_hex_r(temp
->hex
, &null_oid
);
4204 oid_to_hex_r(temp
->hex
, &one
->oid
);
4205 /* Even though we may sometimes borrow the
4206 * contents from the work tree, we always want
4207 * one->mode. mode is trustworthy even when
4208 * !(one->oid_valid), as long as
4209 * DIFF_FILE_VALID(one).
4211 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
4216 if (diff_populate_filespec(r
, one
, NULL
))
4217 die("cannot read data blob for %s", one
->path
);
4218 prep_temp_blob(r
->index
, name
, temp
,
4219 one
->data
, one
->size
,
4220 &one
->oid
, one
->mode
);
4225 static void add_external_diff_name(struct repository
*r
,
4226 struct strvec
*argv
,
4228 struct diff_filespec
*df
)
4230 struct diff_tempfile
*temp
= prepare_temp_file(r
, name
, df
);
4231 strvec_push(argv
, temp
->name
);
4232 strvec_push(argv
, temp
->hex
);
4233 strvec_push(argv
, temp
->mode
);
4236 /* An external diff command takes:
4238 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4239 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4242 static void run_external_diff(const char *pgm
,
4245 struct diff_filespec
*one
,
4246 struct diff_filespec
*two
,
4247 const char *xfrm_msg
,
4248 struct diff_options
*o
)
4250 struct strvec argv
= STRVEC_INIT
;
4251 struct strvec env
= STRVEC_INIT
;
4252 struct diff_queue_struct
*q
= &diff_queued_diff
;
4254 strvec_push(&argv
, pgm
);
4255 strvec_push(&argv
, name
);
4258 add_external_diff_name(o
->repo
, &argv
, name
, one
);
4260 add_external_diff_name(o
->repo
, &argv
, name
, two
);
4262 add_external_diff_name(o
->repo
, &argv
, other
, two
);
4263 strvec_push(&argv
, other
);
4264 strvec_push(&argv
, xfrm_msg
);
4268 strvec_pushf(&env
, "GIT_DIFF_PATH_COUNTER=%d", ++o
->diff_path_counter
);
4269 strvec_pushf(&env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
4271 diff_free_filespec_data(one
);
4272 diff_free_filespec_data(two
);
4273 if (run_command_v_opt_cd_env(argv
.v
, RUN_USING_SHELL
, NULL
, env
.v
))
4274 die(_("external diff died, stopping at %s"), name
);
4277 strvec_clear(&argv
);
4281 static int similarity_index(struct diff_filepair
*p
)
4283 return p
->score
* 100 / MAX_SCORE
;
4286 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
4288 if (startup_info
->have_repository
)
4289 return find_unique_abbrev(oid
, abbrev
);
4291 char *hex
= oid_to_hex(oid
);
4293 abbrev
= FALLBACK_DEFAULT_ABBREV
;
4294 if (abbrev
> the_hash_algo
->hexsz
)
4295 BUG("oid abbreviation out of range: %d", abbrev
);
4302 static void fill_metainfo(struct strbuf
*msg
,
4305 struct diff_filespec
*one
,
4306 struct diff_filespec
*two
,
4307 struct diff_options
*o
,
4308 struct diff_filepair
*p
,
4309 int *must_show_header
,
4312 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
4313 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
4314 const char *line_prefix
= diff_line_prefix(o
);
4316 *must_show_header
= 1;
4317 strbuf_init(msg
, PATH_MAX
* 2 + 300);
4318 switch (p
->status
) {
4319 case DIFF_STATUS_COPIED
:
4320 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4321 line_prefix
, set
, similarity_index(p
));
4322 strbuf_addf(msg
, "%s\n%s%scopy from ",
4323 reset
, line_prefix
, set
);
4324 quote_c_style(name
, msg
, NULL
, 0);
4325 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
4326 quote_c_style(other
, msg
, NULL
, 0);
4327 strbuf_addf(msg
, "%s\n", reset
);
4329 case DIFF_STATUS_RENAMED
:
4330 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4331 line_prefix
, set
, similarity_index(p
));
4332 strbuf_addf(msg
, "%s\n%s%srename from ",
4333 reset
, line_prefix
, set
);
4334 quote_c_style(name
, msg
, NULL
, 0);
4335 strbuf_addf(msg
, "%s\n%s%srename to ",
4336 reset
, line_prefix
, set
);
4337 quote_c_style(other
, msg
, NULL
, 0);
4338 strbuf_addf(msg
, "%s\n", reset
);
4340 case DIFF_STATUS_MODIFIED
:
4342 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
4344 set
, similarity_index(p
), reset
);
4349 *must_show_header
= 0;
4351 if (one
&& two
&& !oideq(&one
->oid
, &two
->oid
)) {
4352 const unsigned hexsz
= the_hash_algo
->hexsz
;
4353 int abbrev
= o
->abbrev
? o
->abbrev
: DEFAULT_ABBREV
;
4355 if (o
->flags
.full_index
)
4358 if (o
->flags
.binary
) {
4360 if ((!fill_mmfile(o
->repo
, &mf
, one
) &&
4361 diff_filespec_is_binary(o
->repo
, one
)) ||
4362 (!fill_mmfile(o
->repo
, &mf
, two
) &&
4363 diff_filespec_is_binary(o
->repo
, two
)))
4366 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
4367 diff_abbrev_oid(&one
->oid
, abbrev
),
4368 diff_abbrev_oid(&two
->oid
, abbrev
));
4369 if (one
->mode
== two
->mode
)
4370 strbuf_addf(msg
, " %06o", one
->mode
);
4371 strbuf_addf(msg
, "%s\n", reset
);
4375 static void run_diff_cmd(const char *pgm
,
4378 const char *attr_path
,
4379 struct diff_filespec
*one
,
4380 struct diff_filespec
*two
,
4382 struct diff_options
*o
,
4383 struct diff_filepair
*p
)
4385 const char *xfrm_msg
= NULL
;
4386 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
4387 int must_show_header
= 0;
4390 if (o
->flags
.allow_external
) {
4391 struct userdiff_driver
*drv
;
4393 drv
= userdiff_find_by_path(o
->repo
->index
, attr_path
);
4394 if (drv
&& drv
->external
)
4395 pgm
= drv
->external
;
4400 * don't use colors when the header is intended for an
4401 * external diff driver
4403 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
4405 want_color(o
->use_color
) && !pgm
);
4406 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
4410 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
, o
);
4414 builtin_diff(name
, other
? other
: name
,
4415 one
, two
, xfrm_msg
, must_show_header
,
4416 o
, complete_rewrite
);
4418 fprintf(o
->file
, "* Unmerged path %s\n", name
);
4421 static void diff_fill_oid_info(struct diff_filespec
*one
, struct index_state
*istate
)
4423 if (DIFF_FILE_VALID(one
)) {
4424 if (!one
->oid_valid
) {
4426 if (one
->is_stdin
) {
4430 if (lstat(one
->path
, &st
) < 0)
4431 die_errno("stat '%s'", one
->path
);
4432 if (index_path(istate
, &one
->oid
, one
->path
, &st
, 0))
4433 die("cannot hash %s", one
->path
);
4440 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
4442 /* Strip the prefix but do not molest /dev/null and absolute paths */
4443 if (*namep
&& !is_absolute_path(*namep
)) {
4444 *namep
+= prefix_length
;
4448 if (*otherp
&& !is_absolute_path(*otherp
)) {
4449 *otherp
+= prefix_length
;
4450 if (**otherp
== '/')
4455 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
4457 const char *pgm
= external_diff();
4459 struct diff_filespec
*one
= p
->one
;
4460 struct diff_filespec
*two
= p
->two
;
4463 const char *attr_path
;
4466 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
4468 if (o
->prefix_length
)
4469 strip_prefix(o
->prefix_length
, &name
, &other
);
4471 if (!o
->flags
.allow_external
)
4474 if (DIFF_PAIR_UNMERGED(p
)) {
4475 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
4476 NULL
, NULL
, NULL
, o
, p
);
4480 diff_fill_oid_info(one
, o
->repo
->index
);
4481 diff_fill_oid_info(two
, o
->repo
->index
);
4484 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4485 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4487 * a filepair that changes between file and symlink
4488 * needs to be split into deletion and creation.
4490 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4491 run_diff_cmd(NULL
, name
, other
, attr_path
,
4495 strbuf_release(&msg
);
4497 null
= alloc_filespec(one
->path
);
4498 run_diff_cmd(NULL
, name
, other
, attr_path
,
4499 null
, two
, &msg
, o
, p
);
4503 run_diff_cmd(pgm
, name
, other
, attr_path
,
4504 one
, two
, &msg
, o
, p
);
4506 strbuf_release(&msg
);
4509 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4510 struct diffstat_t
*diffstat
)
4515 if (DIFF_PAIR_UNMERGED(p
)) {
4517 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
,
4522 name
= p
->one
->path
;
4523 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4525 if (o
->prefix_length
)
4526 strip_prefix(o
->prefix_length
, &name
, &other
);
4528 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4529 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4531 builtin_diffstat(name
, other
, p
->one
, p
->two
,
4535 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4539 const char *attr_path
;
4541 if (DIFF_PAIR_UNMERGED(p
)) {
4546 name
= p
->one
->path
;
4547 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4548 attr_path
= other
? other
: name
;
4550 if (o
->prefix_length
)
4551 strip_prefix(o
->prefix_length
, &name
, &other
);
4553 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4554 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4556 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4559 static void prep_parse_options(struct diff_options
*options
);
4561 void repo_diff_setup(struct repository
*r
, struct diff_options
*options
)
4563 memcpy(options
, &default_diff_options
, sizeof(*options
));
4565 options
->file
= stdout
;
4568 options
->output_indicators
[OUTPUT_INDICATOR_NEW
] = '+';
4569 options
->output_indicators
[OUTPUT_INDICATOR_OLD
] = '-';
4570 options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
] = ' ';
4571 options
->abbrev
= DEFAULT_ABBREV
;
4572 options
->line_termination
= '\n';
4573 options
->break_opt
= -1;
4574 options
->rename_limit
= -1;
4575 options
->dirstat_permille
= diff_dirstat_permille_default
;
4576 options
->context
= diff_context_default
;
4577 options
->interhunkcontext
= diff_interhunk_context_default
;
4578 options
->ws_error_highlight
= ws_error_highlight_default
;
4579 options
->flags
.rename_empty
= 1;
4580 options
->flags
.relative_name
= diff_relative
;
4581 options
->objfind
= NULL
;
4583 /* pathchange left =NULL by default */
4584 options
->change
= diff_change
;
4585 options
->add_remove
= diff_addremove
;
4586 options
->use_color
= diff_use_color_default
;
4587 options
->detect_rename
= diff_detect_rename_default
;
4588 options
->xdl_opts
|= diff_algorithm
;
4589 if (diff_indent_heuristic
)
4590 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4592 options
->orderfile
= diff_order_file_cfg
;
4594 if (!options
->flags
.ignore_submodule_set
)
4595 options
->flags
.ignore_untracked_in_submodules
= 1;
4597 if (diff_no_prefix
) {
4598 options
->a_prefix
= options
->b_prefix
= "";
4599 } else if (!diff_mnemonic_prefix
) {
4600 options
->a_prefix
= "a/";
4601 options
->b_prefix
= "b/";
4604 options
->color_moved
= diff_color_moved_default
;
4605 options
->color_moved_ws_handling
= diff_color_moved_ws_default
;
4607 prep_parse_options(options
);
4610 void diff_setup_done(struct diff_options
*options
)
4612 unsigned check_mask
= DIFF_FORMAT_NAME
|
4613 DIFF_FORMAT_NAME_STATUS
|
4614 DIFF_FORMAT_CHECKDIFF
|
4615 DIFF_FORMAT_NO_OUTPUT
;
4617 * This must be signed because we're comparing against a potentially
4620 const int hexsz
= the_hash_algo
->hexsz
;
4622 if (options
->set_default
)
4623 options
->set_default(options
);
4625 if (HAS_MULTI_BITS(options
->output_format
& check_mask
))
4626 die(_("--name-only, --name-status, --check and -s are mutually exclusive"));
4628 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
4629 die(_("-G, -S and --find-object are mutually exclusive"));
4631 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_G_REGEX_MASK
))
4632 die(_("-G and --pickaxe-regex are mutually exclusive, use --pickaxe-regex with -S"));
4634 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK
))
4635 die(_("---pickaxe-all and --find-object are mutually exclusive, use --pickaxe-all with -G and -S"));
4638 * Most of the time we can say "there are changes"
4639 * only by checking if there are changed paths, but
4640 * --ignore-whitespace* options force us to look
4644 if ((options
->xdl_opts
& XDF_WHITESPACE_FLAGS
) ||
4645 options
->ignore_regex_nr
)
4646 options
->flags
.diff_from_contents
= 1;
4648 options
->flags
.diff_from_contents
= 0;
4650 if (options
->flags
.find_copies_harder
)
4651 options
->detect_rename
= DIFF_DETECT_COPY
;
4653 if (!options
->flags
.relative_name
)
4654 options
->prefix
= NULL
;
4655 if (options
->prefix
)
4656 options
->prefix_length
= strlen(options
->prefix
);
4658 options
->prefix_length
= 0;
4660 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4661 DIFF_FORMAT_NAME_STATUS
|
4662 DIFF_FORMAT_CHECKDIFF
|
4663 DIFF_FORMAT_NO_OUTPUT
))
4664 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4665 DIFF_FORMAT_NUMSTAT
|
4666 DIFF_FORMAT_DIFFSTAT
|
4667 DIFF_FORMAT_SHORTSTAT
|
4668 DIFF_FORMAT_DIRSTAT
|
4669 DIFF_FORMAT_SUMMARY
|
4673 * These cases always need recursive; we do not drop caller-supplied
4674 * recursive bits for other formats here.
4676 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4677 DIFF_FORMAT_NUMSTAT
|
4678 DIFF_FORMAT_DIFFSTAT
|
4679 DIFF_FORMAT_SHORTSTAT
|
4680 DIFF_FORMAT_DIRSTAT
|
4681 DIFF_FORMAT_SUMMARY
|
4682 DIFF_FORMAT_CHECKDIFF
))
4683 options
->flags
.recursive
= 1;
4685 * Also pickaxe would not work very well if you do not say recursive
4687 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
4688 options
->flags
.recursive
= 1;
4690 * When patches are generated, submodules diffed against the work tree
4691 * must be checked for dirtiness too so it can be shown in the output
4693 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4694 options
->flags
.dirty_submodules
= 1;
4696 if (options
->detect_rename
&& options
->rename_limit
< 0)
4697 options
->rename_limit
= diff_rename_limit_default
;
4698 if (hexsz
< options
->abbrev
)
4699 options
->abbrev
= hexsz
; /* full */
4702 * It does not make sense to show the first hit we happened
4703 * to have found. It does not make sense not to return with
4704 * exit code in such a case either.
4706 if (options
->flags
.quick
) {
4707 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4708 options
->flags
.exit_with_status
= 1;
4711 options
->diff_path_counter
= 0;
4713 if (options
->flags
.follow_renames
&& options
->pathspec
.nr
!= 1)
4714 die(_("--follow requires exactly one pathspec"));
4716 if (!options
->use_color
|| external_diff())
4717 options
->color_moved
= 0;
4719 FREE_AND_NULL(options
->parseopts
);
4722 int parse_long_opt(const char *opt
, const char **argv
,
4723 const char **optarg
)
4725 const char *arg
= argv
[0];
4726 if (!skip_prefix(arg
, "--", &arg
))
4728 if (!skip_prefix(arg
, opt
, &arg
))
4730 if (*arg
== '=') { /* stuck form: --option=value */
4736 /* separate form: --option value */
4738 die("Option '--%s' requires a value", opt
);
4743 static int diff_opt_stat(const struct option
*opt
, const char *value
, int unset
)
4745 struct diff_options
*options
= opt
->value
;
4746 int width
= options
->stat_width
;
4747 int name_width
= options
->stat_name_width
;
4748 int graph_width
= options
->stat_graph_width
;
4749 int count
= options
->stat_count
;
4752 BUG_ON_OPT_NEG(unset
);
4754 if (!strcmp(opt
->long_name
, "stat")) {
4756 width
= strtoul(value
, &end
, 10);
4758 name_width
= strtoul(end
+1, &end
, 10);
4760 count
= strtoul(end
+1, &end
, 10);
4762 return error(_("invalid --stat value: %s"), value
);
4764 } else if (!strcmp(opt
->long_name
, "stat-width")) {
4765 width
= strtoul(value
, &end
, 10);
4767 return error(_("%s expects a numerical value"),
4769 } else if (!strcmp(opt
->long_name
, "stat-name-width")) {
4770 name_width
= strtoul(value
, &end
, 10);
4772 return error(_("%s expects a numerical value"),
4774 } else if (!strcmp(opt
->long_name
, "stat-graph-width")) {
4775 graph_width
= strtoul(value
, &end
, 10);
4777 return error(_("%s expects a numerical value"),
4779 } else if (!strcmp(opt
->long_name
, "stat-count")) {
4780 count
= strtoul(value
, &end
, 10);
4782 return error(_("%s expects a numerical value"),
4785 BUG("%s should not get here", opt
->long_name
);
4787 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4788 options
->stat_name_width
= name_width
;
4789 options
->stat_graph_width
= graph_width
;
4790 options
->stat_width
= width
;
4791 options
->stat_count
= count
;
4795 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
4797 struct strbuf errmsg
= STRBUF_INIT
;
4798 if (parse_dirstat_params(options
, params
, &errmsg
))
4799 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
4801 strbuf_release(&errmsg
);
4803 * The caller knows a dirstat-related option is given from the command
4804 * line; allow it to say "return this_function();"
4806 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
4810 static const char diff_status_letters
[] = {
4813 DIFF_STATUS_DELETED
,
4814 DIFF_STATUS_MODIFIED
,
4815 DIFF_STATUS_RENAMED
,
4816 DIFF_STATUS_TYPE_CHANGED
,
4817 DIFF_STATUS_UNKNOWN
,
4818 DIFF_STATUS_UNMERGED
,
4819 DIFF_STATUS_FILTER_AON
,
4820 DIFF_STATUS_FILTER_BROKEN
,
4824 static unsigned int filter_bit
['Z' + 1];
4826 static void prepare_filter_bits(void)
4830 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4831 for (i
= 0; diff_status_letters
[i
]; i
++)
4832 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4836 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4838 return opt
->filter
& filter_bit
[(int) status
];
4841 unsigned diff_filter_bit(char status
)
4843 prepare_filter_bits();
4844 return filter_bit
[(int) status
];
4847 static int diff_opt_diff_filter(const struct option
*option
,
4848 const char *optarg
, int unset
)
4850 struct diff_options
*opt
= option
->value
;
4853 BUG_ON_OPT_NEG(unset
);
4854 prepare_filter_bits();
4857 * If there is a negation e.g. 'd' in the input, and we haven't
4858 * initialized the filter field with another --diff-filter, start
4859 * from full set of bits, except for AON.
4862 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4863 if (optch
< 'a' || 'z' < optch
)
4865 opt
->filter
= (1 << (ARRAY_SIZE(diff_status_letters
) - 1)) - 1;
4866 opt
->filter
&= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4871 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4875 if ('a' <= optch
&& optch
<= 'z') {
4877 optch
= toupper(optch
);
4882 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
4884 return error(_("unknown change class '%c' in --diff-filter=%s"),
4887 opt
->filter
&= ~bit
;
4894 static void enable_patch_output(int *fmt
)
4896 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
4897 *fmt
|= DIFF_FORMAT_PATCH
;
4900 static int diff_opt_ws_error_highlight(const struct option
*option
,
4901 const char *arg
, int unset
)
4903 struct diff_options
*opt
= option
->value
;
4904 int val
= parse_ws_error_highlight(arg
);
4906 BUG_ON_OPT_NEG(unset
);
4908 return error(_("unknown value after ws-error-highlight=%.*s"),
4910 opt
->ws_error_highlight
= val
;
4914 static int diff_opt_find_object(const struct option
*option
,
4915 const char *arg
, int unset
)
4917 struct diff_options
*opt
= option
->value
;
4918 struct object_id oid
;
4920 BUG_ON_OPT_NEG(unset
);
4921 if (get_oid(arg
, &oid
))
4922 return error(_("unable to resolve '%s'"), arg
);
4925 CALLOC_ARRAY(opt
->objfind
, 1);
4927 opt
->pickaxe_opts
|= DIFF_PICKAXE_KIND_OBJFIND
;
4928 opt
->flags
.recursive
= 1;
4929 opt
->flags
.tree_in_recursive
= 1;
4930 oidset_insert(opt
->objfind
, &oid
);
4934 static int diff_opt_anchored(const struct option
*opt
,
4935 const char *arg
, int unset
)
4937 struct diff_options
*options
= opt
->value
;
4939 BUG_ON_OPT_NEG(unset
);
4940 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
4941 ALLOC_GROW(options
->anchors
, options
->anchors_nr
+ 1,
4942 options
->anchors_alloc
);
4943 options
->anchors
[options
->anchors_nr
++] = xstrdup(arg
);
4947 static int diff_opt_binary(const struct option
*opt
,
4948 const char *arg
, int unset
)
4950 struct diff_options
*options
= opt
->value
;
4952 BUG_ON_OPT_NEG(unset
);
4953 BUG_ON_OPT_ARG(arg
);
4954 enable_patch_output(&options
->output_format
);
4955 options
->flags
.binary
= 1;
4959 static int diff_opt_break_rewrites(const struct option
*opt
,
4960 const char *arg
, int unset
)
4962 int *break_opt
= opt
->value
;
4965 BUG_ON_OPT_NEG(unset
);
4968 opt1
= parse_rename_score(&arg
);
4971 else if (*arg
!= '/')
4972 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
4975 opt2
= parse_rename_score(&arg
);
4978 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
4979 *break_opt
= opt1
| (opt2
<< 16);
4983 static int diff_opt_char(const struct option
*opt
,
4984 const char *arg
, int unset
)
4986 char *value
= opt
->value
;
4988 BUG_ON_OPT_NEG(unset
);
4990 return error(_("%s expects a character, got '%s'"),
4991 opt
->long_name
, arg
);
4996 static int diff_opt_color_moved(const struct option
*opt
,
4997 const char *arg
, int unset
)
4999 struct diff_options
*options
= opt
->value
;
5002 options
->color_moved
= COLOR_MOVED_NO
;
5004 if (diff_color_moved_default
)
5005 options
->color_moved
= diff_color_moved_default
;
5006 if (options
->color_moved
== COLOR_MOVED_NO
)
5007 options
->color_moved
= COLOR_MOVED_DEFAULT
;
5009 int cm
= parse_color_moved(arg
);
5011 return error(_("bad --color-moved argument: %s"), arg
);
5012 options
->color_moved
= cm
;
5017 static int diff_opt_color_moved_ws(const struct option
*opt
,
5018 const char *arg
, int unset
)
5020 struct diff_options
*options
= opt
->value
;
5024 options
->color_moved_ws_handling
= 0;
5028 cm
= parse_color_moved_ws(arg
);
5029 if (cm
& COLOR_MOVED_WS_ERROR
)
5030 return error(_("invalid mode '%s' in --color-moved-ws"), arg
);
5031 options
->color_moved_ws_handling
= cm
;
5035 static int diff_opt_color_words(const struct option
*opt
,
5036 const char *arg
, int unset
)
5038 struct diff_options
*options
= opt
->value
;
5040 BUG_ON_OPT_NEG(unset
);
5041 options
->use_color
= 1;
5042 options
->word_diff
= DIFF_WORDS_COLOR
;
5043 options
->word_regex
= arg
;
5047 static int diff_opt_compact_summary(const struct option
*opt
,
5048 const char *arg
, int unset
)
5050 struct diff_options
*options
= opt
->value
;
5052 BUG_ON_OPT_ARG(arg
);
5054 options
->flags
.stat_with_summary
= 0;
5056 options
->flags
.stat_with_summary
= 1;
5057 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
5062 static int diff_opt_diff_algorithm(const struct option
*opt
,
5063 const char *arg
, int unset
)
5065 struct diff_options
*options
= opt
->value
;
5066 long value
= parse_algorithm_value(arg
);
5068 BUG_ON_OPT_NEG(unset
);
5070 return error(_("option diff-algorithm accepts \"myers\", "
5071 "\"minimal\", \"patience\" and \"histogram\""));
5073 /* clear out previous settings */
5074 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
5075 options
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
5076 options
->xdl_opts
|= value
;
5080 static int diff_opt_dirstat(const struct option
*opt
,
5081 const char *arg
, int unset
)
5083 struct diff_options
*options
= opt
->value
;
5085 BUG_ON_OPT_NEG(unset
);
5086 if (!strcmp(opt
->long_name
, "cumulative")) {
5088 BUG("how come --cumulative take a value?");
5090 } else if (!strcmp(opt
->long_name
, "dirstat-by-file"))
5091 parse_dirstat_opt(options
, "files");
5092 parse_dirstat_opt(options
, arg
? arg
: "");
5096 static int diff_opt_find_copies(const struct option
*opt
,
5097 const char *arg
, int unset
)
5099 struct diff_options
*options
= opt
->value
;
5101 BUG_ON_OPT_NEG(unset
);
5104 options
->rename_score
= parse_rename_score(&arg
);
5106 return error(_("invalid argument to %s"), opt
->long_name
);
5108 if (options
->detect_rename
== DIFF_DETECT_COPY
)
5109 options
->flags
.find_copies_harder
= 1;
5111 options
->detect_rename
= DIFF_DETECT_COPY
;
5116 static int diff_opt_find_renames(const struct option
*opt
,
5117 const char *arg
, int unset
)
5119 struct diff_options
*options
= opt
->value
;
5121 BUG_ON_OPT_NEG(unset
);
5124 options
->rename_score
= parse_rename_score(&arg
);
5126 return error(_("invalid argument to %s"), opt
->long_name
);
5128 options
->detect_rename
= DIFF_DETECT_RENAME
;
5132 static int diff_opt_follow(const struct option
*opt
,
5133 const char *arg
, int unset
)
5135 struct diff_options
*options
= opt
->value
;
5137 BUG_ON_OPT_ARG(arg
);
5139 options
->flags
.follow_renames
= 0;
5140 options
->flags
.default_follow_renames
= 0;
5142 options
->flags
.follow_renames
= 1;
5147 static int diff_opt_ignore_submodules(const struct option
*opt
,
5148 const char *arg
, int unset
)
5150 struct diff_options
*options
= opt
->value
;
5152 BUG_ON_OPT_NEG(unset
);
5155 options
->flags
.override_submodule_config
= 1;
5156 handle_ignore_submodules_arg(options
, arg
);
5160 static int diff_opt_line_prefix(const struct option
*opt
,
5161 const char *optarg
, int unset
)
5163 struct diff_options
*options
= opt
->value
;
5165 BUG_ON_OPT_NEG(unset
);
5166 options
->line_prefix
= optarg
;
5167 options
->line_prefix_length
= strlen(options
->line_prefix
);
5168 graph_setup_line_prefix(options
);
5172 static int diff_opt_no_prefix(const struct option
*opt
,
5173 const char *optarg
, int unset
)
5175 struct diff_options
*options
= opt
->value
;
5177 BUG_ON_OPT_NEG(unset
);
5178 BUG_ON_OPT_ARG(optarg
);
5179 options
->a_prefix
= "";
5180 options
->b_prefix
= "";
5184 static enum parse_opt_result
diff_opt_output(struct parse_opt_ctx_t
*ctx
,
5185 const struct option
*opt
,
5186 const char *arg
, int unset
)
5188 struct diff_options
*options
= opt
->value
;
5191 BUG_ON_OPT_NEG(unset
);
5192 path
= prefix_filename(ctx
->prefix
, arg
);
5193 options
->file
= xfopen(path
, "w");
5194 options
->close_file
= 1;
5195 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
5196 options
->use_color
= GIT_COLOR_NEVER
;
5201 static int diff_opt_patience(const struct option
*opt
,
5202 const char *arg
, int unset
)
5204 struct diff_options
*options
= opt
->value
;
5207 BUG_ON_OPT_NEG(unset
);
5208 BUG_ON_OPT_ARG(arg
);
5209 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
5211 * Both --patience and --anchored use PATIENCE_DIFF
5212 * internally, so remove any anchors previously
5215 for (i
= 0; i
< options
->anchors_nr
; i
++)
5216 free(options
->anchors
[i
]);
5217 options
->anchors_nr
= 0;
5221 static int diff_opt_ignore_regex(const struct option
*opt
,
5222 const char *arg
, int unset
)
5224 struct diff_options
*options
= opt
->value
;
5227 BUG_ON_OPT_NEG(unset
);
5228 regex
= xmalloc(sizeof(*regex
));
5229 if (regcomp(regex
, arg
, REG_EXTENDED
| REG_NEWLINE
))
5230 return error(_("invalid regex given to -I: '%s'"), arg
);
5231 ALLOC_GROW(options
->ignore_regex
, options
->ignore_regex_nr
+ 1,
5232 options
->ignore_regex_alloc
);
5233 options
->ignore_regex
[options
->ignore_regex_nr
++] = regex
;
5237 static int diff_opt_pickaxe_regex(const struct option
*opt
,
5238 const char *arg
, int unset
)
5240 struct diff_options
*options
= opt
->value
;
5242 BUG_ON_OPT_NEG(unset
);
5243 options
->pickaxe
= arg
;
5244 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
5248 static int diff_opt_pickaxe_string(const struct option
*opt
,
5249 const char *arg
, int unset
)
5251 struct diff_options
*options
= opt
->value
;
5253 BUG_ON_OPT_NEG(unset
);
5254 options
->pickaxe
= arg
;
5255 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
5259 static int diff_opt_relative(const struct option
*opt
,
5260 const char *arg
, int unset
)
5262 struct diff_options
*options
= opt
->value
;
5264 options
->flags
.relative_name
= !unset
;
5266 options
->prefix
= arg
;
5270 static int diff_opt_submodule(const struct option
*opt
,
5271 const char *arg
, int unset
)
5273 struct diff_options
*options
= opt
->value
;
5275 BUG_ON_OPT_NEG(unset
);
5278 if (parse_submodule_params(options
, arg
))
5279 return error(_("failed to parse --submodule option parameter: '%s'"),
5284 static int diff_opt_textconv(const struct option
*opt
,
5285 const char *arg
, int unset
)
5287 struct diff_options
*options
= opt
->value
;
5289 BUG_ON_OPT_ARG(arg
);
5291 options
->flags
.allow_textconv
= 0;
5293 options
->flags
.allow_textconv
= 1;
5294 options
->flags
.textconv_set_via_cmdline
= 1;
5299 static int diff_opt_unified(const struct option
*opt
,
5300 const char *arg
, int unset
)
5302 struct diff_options
*options
= opt
->value
;
5305 BUG_ON_OPT_NEG(unset
);
5308 options
->context
= strtol(arg
, &s
, 10);
5310 return error(_("%s expects a numerical value"), "--unified");
5312 enable_patch_output(&options
->output_format
);
5317 static int diff_opt_word_diff(const struct option
*opt
,
5318 const char *arg
, int unset
)
5320 struct diff_options
*options
= opt
->value
;
5322 BUG_ON_OPT_NEG(unset
);
5324 if (!strcmp(arg
, "plain"))
5325 options
->word_diff
= DIFF_WORDS_PLAIN
;
5326 else if (!strcmp(arg
, "color")) {
5327 options
->use_color
= 1;
5328 options
->word_diff
= DIFF_WORDS_COLOR
;
5330 else if (!strcmp(arg
, "porcelain"))
5331 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
5332 else if (!strcmp(arg
, "none"))
5333 options
->word_diff
= DIFF_WORDS_NONE
;
5335 return error(_("bad --word-diff argument: %s"), arg
);
5337 if (options
->word_diff
== DIFF_WORDS_NONE
)
5338 options
->word_diff
= DIFF_WORDS_PLAIN
;
5343 static int diff_opt_word_diff_regex(const struct option
*opt
,
5344 const char *arg
, int unset
)
5346 struct diff_options
*options
= opt
->value
;
5348 BUG_ON_OPT_NEG(unset
);
5349 if (options
->word_diff
== DIFF_WORDS_NONE
)
5350 options
->word_diff
= DIFF_WORDS_PLAIN
;
5351 options
->word_regex
= arg
;
5355 static int diff_opt_rotate_to(const struct option
*opt
, const char *arg
, int unset
)
5357 struct diff_options
*options
= opt
->value
;
5359 BUG_ON_OPT_NEG(unset
);
5360 if (!strcmp(opt
->long_name
, "skip-to"))
5361 options
->skip_instead_of_rotate
= 1;
5363 options
->skip_instead_of_rotate
= 0;
5364 options
->rotate_to
= arg
;
5368 static void prep_parse_options(struct diff_options
*options
)
5370 struct option parseopts
[] = {
5371 OPT_GROUP(N_("Diff output format options")),
5372 OPT_BITOP('p', "patch", &options
->output_format
,
5373 N_("generate patch"),
5374 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5375 OPT_BIT_F('s', "no-patch", &options
->output_format
,
5376 N_("suppress diff output"),
5377 DIFF_FORMAT_NO_OUTPUT
, PARSE_OPT_NONEG
),
5378 OPT_BITOP('u', NULL
, &options
->output_format
,
5379 N_("generate patch"),
5380 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5381 OPT_CALLBACK_F('U', "unified", options
, N_("<n>"),
5382 N_("generate diffs with <n> lines context"),
5383 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_unified
),
5384 OPT_BOOL('W', "function-context", &options
->flags
.funccontext
,
5385 N_("generate diffs with <n> lines context")),
5386 OPT_BIT_F(0, "raw", &options
->output_format
,
5387 N_("generate the diff in raw format"),
5388 DIFF_FORMAT_RAW
, PARSE_OPT_NONEG
),
5389 OPT_BITOP(0, "patch-with-raw", &options
->output_format
,
5390 N_("synonym for '-p --raw'"),
5391 DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
,
5392 DIFF_FORMAT_NO_OUTPUT
),
5393 OPT_BITOP(0, "patch-with-stat", &options
->output_format
,
5394 N_("synonym for '-p --stat'"),
5395 DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
,
5396 DIFF_FORMAT_NO_OUTPUT
),
5397 OPT_BIT_F(0, "numstat", &options
->output_format
,
5398 N_("machine friendly --stat"),
5399 DIFF_FORMAT_NUMSTAT
, PARSE_OPT_NONEG
),
5400 OPT_BIT_F(0, "shortstat", &options
->output_format
,
5401 N_("output only the last line of --stat"),
5402 DIFF_FORMAT_SHORTSTAT
, PARSE_OPT_NONEG
),
5403 OPT_CALLBACK_F('X', "dirstat", options
, N_("<param1,param2>..."),
5404 N_("output the distribution of relative amount of changes for each sub-directory"),
5405 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5407 OPT_CALLBACK_F(0, "cumulative", options
, NULL
,
5408 N_("synonym for --dirstat=cumulative"),
5409 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5411 OPT_CALLBACK_F(0, "dirstat-by-file", options
, N_("<param1,param2>..."),
5412 N_("synonym for --dirstat=files,param1,param2..."),
5413 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5415 OPT_BIT_F(0, "check", &options
->output_format
,
5416 N_("warn if changes introduce conflict markers or whitespace errors"),
5417 DIFF_FORMAT_CHECKDIFF
, PARSE_OPT_NONEG
),
5418 OPT_BIT_F(0, "summary", &options
->output_format
,
5419 N_("condensed summary such as creations, renames and mode changes"),
5420 DIFF_FORMAT_SUMMARY
, PARSE_OPT_NONEG
),
5421 OPT_BIT_F(0, "name-only", &options
->output_format
,
5422 N_("show only names of changed files"),
5423 DIFF_FORMAT_NAME
, PARSE_OPT_NONEG
),
5424 OPT_BIT_F(0, "name-status", &options
->output_format
,
5425 N_("show only names and status of changed files"),
5426 DIFF_FORMAT_NAME_STATUS
, PARSE_OPT_NONEG
),
5427 OPT_CALLBACK_F(0, "stat", options
, N_("<width>[,<name-width>[,<count>]]"),
5428 N_("generate diffstat"),
5429 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_stat
),
5430 OPT_CALLBACK_F(0, "stat-width", options
, N_("<width>"),
5431 N_("generate diffstat with a given width"),
5432 PARSE_OPT_NONEG
, diff_opt_stat
),
5433 OPT_CALLBACK_F(0, "stat-name-width", options
, N_("<width>"),
5434 N_("generate diffstat with a given name width"),
5435 PARSE_OPT_NONEG
, diff_opt_stat
),
5436 OPT_CALLBACK_F(0, "stat-graph-width", options
, N_("<width>"),
5437 N_("generate diffstat with a given graph width"),
5438 PARSE_OPT_NONEG
, diff_opt_stat
),
5439 OPT_CALLBACK_F(0, "stat-count", options
, N_("<count>"),
5440 N_("generate diffstat with limited lines"),
5441 PARSE_OPT_NONEG
, diff_opt_stat
),
5442 OPT_CALLBACK_F(0, "compact-summary", options
, NULL
,
5443 N_("generate compact summary in diffstat"),
5444 PARSE_OPT_NOARG
, diff_opt_compact_summary
),
5445 OPT_CALLBACK_F(0, "binary", options
, NULL
,
5446 N_("output a binary diff that can be applied"),
5447 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_binary
),
5448 OPT_BOOL(0, "full-index", &options
->flags
.full_index
,
5449 N_("show full pre- and post-image object names on the \"index\" lines")),
5450 OPT_COLOR_FLAG(0, "color", &options
->use_color
,
5451 N_("show colored diff")),
5452 OPT_CALLBACK_F(0, "ws-error-highlight", options
, N_("<kind>"),
5453 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5454 PARSE_OPT_NONEG
, diff_opt_ws_error_highlight
),
5455 OPT_SET_INT('z', NULL
, &options
->line_termination
,
5456 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5458 OPT__ABBREV(&options
->abbrev
),
5459 OPT_STRING_F(0, "src-prefix", &options
->a_prefix
, N_("<prefix>"),
5460 N_("show the given source prefix instead of \"a/\""),
5462 OPT_STRING_F(0, "dst-prefix", &options
->b_prefix
, N_("<prefix>"),
5463 N_("show the given destination prefix instead of \"b/\""),
5465 OPT_CALLBACK_F(0, "line-prefix", options
, N_("<prefix>"),
5466 N_("prepend an additional prefix to every line of output"),
5467 PARSE_OPT_NONEG
, diff_opt_line_prefix
),
5468 OPT_CALLBACK_F(0, "no-prefix", options
, NULL
,
5469 N_("do not show any source or destination prefix"),
5470 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_no_prefix
),
5471 OPT_INTEGER_F(0, "inter-hunk-context", &options
->interhunkcontext
,
5472 N_("show context between diff hunks up to the specified number of lines"),
5474 OPT_CALLBACK_F(0, "output-indicator-new",
5475 &options
->output_indicators
[OUTPUT_INDICATOR_NEW
],
5477 N_("specify the character to indicate a new line instead of '+'"),
5478 PARSE_OPT_NONEG
, diff_opt_char
),
5479 OPT_CALLBACK_F(0, "output-indicator-old",
5480 &options
->output_indicators
[OUTPUT_INDICATOR_OLD
],
5482 N_("specify the character to indicate an old line instead of '-'"),
5483 PARSE_OPT_NONEG
, diff_opt_char
),
5484 OPT_CALLBACK_F(0, "output-indicator-context",
5485 &options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
],
5487 N_("specify the character to indicate a context instead of ' '"),
5488 PARSE_OPT_NONEG
, diff_opt_char
),
5490 OPT_GROUP(N_("Diff rename options")),
5491 OPT_CALLBACK_F('B', "break-rewrites", &options
->break_opt
, N_("<n>[/<m>]"),
5492 N_("break complete rewrite changes into pairs of delete and create"),
5493 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5494 diff_opt_break_rewrites
),
5495 OPT_CALLBACK_F('M', "find-renames", options
, N_("<n>"),
5496 N_("detect renames"),
5497 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5498 diff_opt_find_renames
),
5499 OPT_SET_INT_F('D', "irreversible-delete", &options
->irreversible_delete
,
5500 N_("omit the preimage for deletes"),
5501 1, PARSE_OPT_NONEG
),
5502 OPT_CALLBACK_F('C', "find-copies", options
, N_("<n>"),
5503 N_("detect copies"),
5504 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5505 diff_opt_find_copies
),
5506 OPT_BOOL(0, "find-copies-harder", &options
->flags
.find_copies_harder
,
5507 N_("use unmodified files as source to find copies")),
5508 OPT_SET_INT_F(0, "no-renames", &options
->detect_rename
,
5509 N_("disable rename detection"),
5510 0, PARSE_OPT_NONEG
),
5511 OPT_BOOL(0, "rename-empty", &options
->flags
.rename_empty
,
5512 N_("use empty blobs as rename source")),
5513 OPT_CALLBACK_F(0, "follow", options
, NULL
,
5514 N_("continue listing the history of a file beyond renames"),
5515 PARSE_OPT_NOARG
, diff_opt_follow
),
5516 OPT_INTEGER('l', NULL
, &options
->rename_limit
,
5517 N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
5519 OPT_GROUP(N_("Diff algorithm options")),
5520 OPT_BIT(0, "minimal", &options
->xdl_opts
,
5521 N_("produce the smallest possible diff"),
5523 OPT_BIT_F('w', "ignore-all-space", &options
->xdl_opts
,
5524 N_("ignore whitespace when comparing lines"),
5525 XDF_IGNORE_WHITESPACE
, PARSE_OPT_NONEG
),
5526 OPT_BIT_F('b', "ignore-space-change", &options
->xdl_opts
,
5527 N_("ignore changes in amount of whitespace"),
5528 XDF_IGNORE_WHITESPACE_CHANGE
, PARSE_OPT_NONEG
),
5529 OPT_BIT_F(0, "ignore-space-at-eol", &options
->xdl_opts
,
5530 N_("ignore changes in whitespace at EOL"),
5531 XDF_IGNORE_WHITESPACE_AT_EOL
, PARSE_OPT_NONEG
),
5532 OPT_BIT_F(0, "ignore-cr-at-eol", &options
->xdl_opts
,
5533 N_("ignore carrier-return at the end of line"),
5534 XDF_IGNORE_CR_AT_EOL
, PARSE_OPT_NONEG
),
5535 OPT_BIT_F(0, "ignore-blank-lines", &options
->xdl_opts
,
5536 N_("ignore changes whose lines are all blank"),
5537 XDF_IGNORE_BLANK_LINES
, PARSE_OPT_NONEG
),
5538 OPT_CALLBACK_F('I', "ignore-matching-lines", options
, N_("<regex>"),
5539 N_("ignore changes whose all lines match <regex>"),
5540 0, diff_opt_ignore_regex
),
5541 OPT_BIT(0, "indent-heuristic", &options
->xdl_opts
,
5542 N_("heuristic to shift diff hunk boundaries for easy reading"),
5543 XDF_INDENT_HEURISTIC
),
5544 OPT_CALLBACK_F(0, "patience", options
, NULL
,
5545 N_("generate diff using the \"patience diff\" algorithm"),
5546 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5548 OPT_BITOP(0, "histogram", &options
->xdl_opts
,
5549 N_("generate diff using the \"histogram diff\" algorithm"),
5550 XDF_HISTOGRAM_DIFF
, XDF_DIFF_ALGORITHM_MASK
),
5551 OPT_CALLBACK_F(0, "diff-algorithm", options
, N_("<algorithm>"),
5552 N_("choose a diff algorithm"),
5553 PARSE_OPT_NONEG
, diff_opt_diff_algorithm
),
5554 OPT_CALLBACK_F(0, "anchored", options
, N_("<text>"),
5555 N_("generate diff using the \"anchored diff\" algorithm"),
5556 PARSE_OPT_NONEG
, diff_opt_anchored
),
5557 OPT_CALLBACK_F(0, "word-diff", options
, N_("<mode>"),
5558 N_("show word diff, using <mode> to delimit changed words"),
5559 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_word_diff
),
5560 OPT_CALLBACK_F(0, "word-diff-regex", options
, N_("<regex>"),
5561 N_("use <regex> to decide what a word is"),
5562 PARSE_OPT_NONEG
, diff_opt_word_diff_regex
),
5563 OPT_CALLBACK_F(0, "color-words", options
, N_("<regex>"),
5564 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5565 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_color_words
),
5566 OPT_CALLBACK_F(0, "color-moved", options
, N_("<mode>"),
5567 N_("moved lines of code are colored differently"),
5568 PARSE_OPT_OPTARG
, diff_opt_color_moved
),
5569 OPT_CALLBACK_F(0, "color-moved-ws", options
, N_("<mode>"),
5570 N_("how white spaces are ignored in --color-moved"),
5571 0, diff_opt_color_moved_ws
),
5573 OPT_GROUP(N_("Other diff options")),
5574 OPT_CALLBACK_F(0, "relative", options
, N_("<prefix>"),
5575 N_("when run from subdir, exclude changes outside and show relative paths"),
5578 OPT_BOOL('a', "text", &options
->flags
.text
,
5579 N_("treat all files as text")),
5580 OPT_BOOL('R', NULL
, &options
->flags
.reverse_diff
,
5581 N_("swap two inputs, reverse the diff")),
5582 OPT_BOOL(0, "exit-code", &options
->flags
.exit_with_status
,
5583 N_("exit with 1 if there were differences, 0 otherwise")),
5584 OPT_BOOL(0, "quiet", &options
->flags
.quick
,
5585 N_("disable all output of the program")),
5586 OPT_BOOL(0, "ext-diff", &options
->flags
.allow_external
,
5587 N_("allow an external diff helper to be executed")),
5588 OPT_CALLBACK_F(0, "textconv", options
, NULL
,
5589 N_("run external text conversion filters when comparing binary files"),
5590 PARSE_OPT_NOARG
, diff_opt_textconv
),
5591 OPT_CALLBACK_F(0, "ignore-submodules", options
, N_("<when>"),
5592 N_("ignore changes to submodules in the diff generation"),
5593 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5594 diff_opt_ignore_submodules
),
5595 OPT_CALLBACK_F(0, "submodule", options
, N_("<format>"),
5596 N_("specify how differences in submodules are shown"),
5597 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5598 diff_opt_submodule
),
5599 OPT_SET_INT_F(0, "ita-invisible-in-index", &options
->ita_invisible_in_index
,
5600 N_("hide 'git add -N' entries from the index"),
5601 1, PARSE_OPT_NONEG
),
5602 OPT_SET_INT_F(0, "ita-visible-in-index", &options
->ita_invisible_in_index
,
5603 N_("treat 'git add -N' entries as real in the index"),
5604 0, PARSE_OPT_NONEG
),
5605 OPT_CALLBACK_F('S', NULL
, options
, N_("<string>"),
5606 N_("look for differences that change the number of occurrences of the specified string"),
5607 0, diff_opt_pickaxe_string
),
5608 OPT_CALLBACK_F('G', NULL
, options
, N_("<regex>"),
5609 N_("look for differences that change the number of occurrences of the specified regex"),
5610 0, diff_opt_pickaxe_regex
),
5611 OPT_BIT_F(0, "pickaxe-all", &options
->pickaxe_opts
,
5612 N_("show all changes in the changeset with -S or -G"),
5613 DIFF_PICKAXE_ALL
, PARSE_OPT_NONEG
),
5614 OPT_BIT_F(0, "pickaxe-regex", &options
->pickaxe_opts
,
5615 N_("treat <string> in -S as extended POSIX regular expression"),
5616 DIFF_PICKAXE_REGEX
, PARSE_OPT_NONEG
),
5617 OPT_FILENAME('O', NULL
, &options
->orderfile
,
5618 N_("control the order in which files appear in the output")),
5619 OPT_CALLBACK_F(0, "rotate-to", options
, N_("<path>"),
5620 N_("show the change in the specified path first"),
5621 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5622 OPT_CALLBACK_F(0, "skip-to", options
, N_("<path>"),
5623 N_("skip the output to the specified path"),
5624 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5625 OPT_CALLBACK_F(0, "find-object", options
, N_("<object-id>"),
5626 N_("look for differences that change the number of occurrences of the specified object"),
5627 PARSE_OPT_NONEG
, diff_opt_find_object
),
5628 OPT_CALLBACK_F(0, "diff-filter", options
, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5629 N_("select files by diff type"),
5630 PARSE_OPT_NONEG
, diff_opt_diff_filter
),
5631 { OPTION_CALLBACK
, 0, "output", options
, N_("<file>"),
5632 N_("Output to a specific file"),
5633 PARSE_OPT_NONEG
, NULL
, 0, diff_opt_output
},
5638 ALLOC_ARRAY(options
->parseopts
, ARRAY_SIZE(parseopts
));
5639 memcpy(options
->parseopts
, parseopts
, sizeof(parseopts
));
5642 int diff_opt_parse(struct diff_options
*options
,
5643 const char **av
, int ac
, const char *prefix
)
5648 ac
= parse_options(ac
, av
, prefix
, options
->parseopts
, NULL
,
5649 PARSE_OPT_KEEP_DASHDASH
|
5650 PARSE_OPT_KEEP_UNKNOWN
|
5651 PARSE_OPT_NO_INTERNAL_HELP
|
5652 PARSE_OPT_ONE_SHOT
|
5653 PARSE_OPT_STOP_AT_NON_OPTION
);
5658 int parse_rename_score(const char **cp_p
)
5660 unsigned long num
, scale
;
5662 const char *cp
= *cp_p
;
5669 if ( !dot
&& ch
== '.' ) {
5672 } else if ( ch
== '%' ) {
5673 scale
= dot
? scale
*100 : 100;
5674 cp
++; /* % is always at the end */
5676 } else if ( ch
>= '0' && ch
<= '9' ) {
5677 if ( scale
< 100000 ) {
5679 num
= (num
*10) + (ch
-'0');
5688 /* user says num divided by scale and we say internally that
5689 * is MAX_SCORE * num / scale.
5691 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
5694 struct diff_queue_struct diff_queued_diff
;
5696 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
5698 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
5699 queue
->queue
[queue
->nr
++] = dp
;
5702 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
5703 struct diff_filespec
*one
,
5704 struct diff_filespec
*two
)
5706 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
5714 void diff_free_filepair(struct diff_filepair
*p
)
5716 free_filespec(p
->one
);
5717 free_filespec(p
->two
);
5721 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
5726 /* Do we want all 40 hex characters? */
5727 if (len
== the_hash_algo
->hexsz
)
5728 return oid_to_hex(oid
);
5730 /* An abbreviated value is fine, possibly followed by an ellipsis. */
5731 abbrev
= diff_abbrev_oid(oid
, len
);
5733 if (!print_sha1_ellipsis())
5736 abblen
= strlen(abbrev
);
5739 * In well-behaved cases, where the abbreviated result is the
5740 * same as the requested length, append three dots after the
5741 * abbreviation (hence the whole logic is limited to the case
5742 * where abblen < 37); when the actual abbreviated result is a
5743 * bit longer than the requested length, we reduce the number
5744 * of dots so that they match the well-behaved ones. However,
5745 * if the actual abbreviation is longer than the requested
5746 * length by more than three, we give up on aligning, and add
5747 * three dots anyway, to indicate that the output is not the
5748 * full object name. Yes, this may be suboptimal, but this
5749 * appears only in "diff --raw --abbrev" output and it is not
5750 * worth the effort to change it now. Note that this would
5751 * likely to work fine when the automatic sizing of default
5752 * abbreviation length is used--we would be fed -1 in "len" in
5753 * that case, and will end up always appending three-dots, but
5754 * the automatic sizing is supposed to give abblen that ensures
5755 * uniqueness across all objects (statistically speaking).
5757 if (abblen
< the_hash_algo
->hexsz
- 3) {
5758 static char hex
[GIT_MAX_HEXSZ
+ 1];
5759 if (len
< abblen
&& abblen
<= len
+ 2)
5760 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
5762 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
5766 return oid_to_hex(oid
);
5769 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
5771 int line_termination
= opt
->line_termination
;
5772 int inter_name_termination
= line_termination
? '\t' : '\0';
5774 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5775 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
5776 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
5777 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
5778 fprintf(opt
->file
, "%s ",
5779 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
5782 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
5783 inter_name_termination
);
5785 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
5788 if (p
->status
== DIFF_STATUS_COPIED
||
5789 p
->status
== DIFF_STATUS_RENAMED
) {
5790 const char *name_a
, *name_b
;
5791 name_a
= p
->one
->path
;
5792 name_b
= p
->two
->path
;
5793 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5794 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
5795 write_name_quoted(name_b
, opt
->file
, line_termination
);
5797 const char *name_a
, *name_b
;
5798 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
5800 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5801 write_name_quoted(name_a
, opt
->file
, line_termination
);
5805 int diff_unmodified_pair(struct diff_filepair
*p
)
5807 /* This function is written stricter than necessary to support
5808 * the currently implemented transformers, but the idea is to
5809 * let transformers to produce diff_filepairs any way they want,
5810 * and filter and clean them up here before producing the output.
5812 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
5814 if (DIFF_PAIR_UNMERGED(p
))
5815 return 0; /* unmerged is interesting */
5817 /* deletion, addition, mode or type change
5818 * and rename are all interesting.
5820 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
5821 DIFF_PAIR_MODE_CHANGED(p
) ||
5822 strcmp(one
->path
, two
->path
))
5825 /* both are valid and point at the same path. that is, we are
5826 * dealing with a change.
5828 if (one
->oid_valid
&& two
->oid_valid
&&
5829 oideq(&one
->oid
, &two
->oid
) &&
5830 !one
->dirty_submodule
&& !two
->dirty_submodule
)
5831 return 1; /* no change */
5832 if (!one
->oid_valid
&& !two
->oid_valid
)
5833 return 1; /* both look at the same file on the filesystem. */
5837 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
5839 if (diff_unmodified_pair(p
))
5842 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5843 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5844 return; /* no tree diffs in patch format */
5849 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
5850 struct diffstat_t
*diffstat
)
5852 if (diff_unmodified_pair(p
))
5855 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5856 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5857 return; /* no useful stat for tree diffs */
5859 run_diffstat(p
, o
, diffstat
);
5862 static void diff_flush_checkdiff(struct diff_filepair
*p
,
5863 struct diff_options
*o
)
5865 if (diff_unmodified_pair(p
))
5868 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5869 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5870 return; /* nothing to check in tree diffs */
5872 run_checkdiff(p
, o
);
5875 int diff_queue_is_empty(void)
5877 struct diff_queue_struct
*q
= &diff_queued_diff
;
5879 for (i
= 0; i
< q
->nr
; i
++)
5880 if (!diff_unmodified_pair(q
->queue
[i
]))
5886 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
5888 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
5891 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
5893 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
5894 fprintf(stderr
, "queue[%d] %s size %lu\n",
5899 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
5901 diff_debug_filespec(p
->one
, i
, "one");
5902 diff_debug_filespec(p
->two
, i
, "two");
5903 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
5904 p
->score
, p
->status
? p
->status
: '?',
5905 p
->one
->rename_used
, p
->broken_pair
);
5908 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
5912 fprintf(stderr
, "%s\n", msg
);
5913 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
5914 for (i
= 0; i
< q
->nr
; i
++) {
5915 struct diff_filepair
*p
= q
->queue
[i
];
5916 diff_debug_filepair(p
, i
);
5921 static void diff_resolve_rename_copy(void)
5924 struct diff_filepair
*p
;
5925 struct diff_queue_struct
*q
= &diff_queued_diff
;
5927 diff_debug_queue("resolve-rename-copy", q
);
5929 for (i
= 0; i
< q
->nr
; i
++) {
5931 p
->status
= 0; /* undecided */
5932 if (DIFF_PAIR_UNMERGED(p
))
5933 p
->status
= DIFF_STATUS_UNMERGED
;
5934 else if (!DIFF_FILE_VALID(p
->one
))
5935 p
->status
= DIFF_STATUS_ADDED
;
5936 else if (!DIFF_FILE_VALID(p
->two
))
5937 p
->status
= DIFF_STATUS_DELETED
;
5938 else if (DIFF_PAIR_TYPE_CHANGED(p
))
5939 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
5941 /* from this point on, we are dealing with a pair
5942 * whose both sides are valid and of the same type, i.e.
5943 * either in-place edit or rename/copy edit.
5945 else if (DIFF_PAIR_RENAME(p
)) {
5947 * A rename might have re-connected a broken
5948 * pair up, causing the pathnames to be the
5949 * same again. If so, that's not a rename at
5950 * all, just a modification..
5952 * Otherwise, see if this source was used for
5953 * multiple renames, in which case we decrement
5954 * the count, and call it a copy.
5956 if (!strcmp(p
->one
->path
, p
->two
->path
))
5957 p
->status
= DIFF_STATUS_MODIFIED
;
5958 else if (--p
->one
->rename_used
> 0)
5959 p
->status
= DIFF_STATUS_COPIED
;
5961 p
->status
= DIFF_STATUS_RENAMED
;
5963 else if (!oideq(&p
->one
->oid
, &p
->two
->oid
) ||
5964 p
->one
->mode
!= p
->two
->mode
||
5965 p
->one
->dirty_submodule
||
5966 p
->two
->dirty_submodule
||
5967 is_null_oid(&p
->one
->oid
))
5968 p
->status
= DIFF_STATUS_MODIFIED
;
5970 /* This is a "no-change" entry and should not
5971 * happen anymore, but prepare for broken callers.
5973 error("feeding unmodified %s to diffcore",
5975 p
->status
= DIFF_STATUS_UNKNOWN
;
5978 diff_debug_queue("resolve-rename-copy done", q
);
5981 static int check_pair_status(struct diff_filepair
*p
)
5983 switch (p
->status
) {
5984 case DIFF_STATUS_UNKNOWN
:
5987 die("internal error in diff-resolve-rename-copy");
5993 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
5995 int fmt
= opt
->output_format
;
5997 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
5998 diff_flush_checkdiff(p
, opt
);
5999 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
6000 diff_flush_raw(p
, opt
);
6001 else if (fmt
& DIFF_FORMAT_NAME
) {
6002 const char *name_a
, *name_b
;
6003 name_a
= p
->two
->path
;
6005 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6006 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
6007 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
6011 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
6013 struct strbuf sb
= STRBUF_INIT
;
6015 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
6017 strbuf_addf(&sb
, " %s ", newdelete
);
6019 quote_c_style(fs
->path
, &sb
, NULL
, 0);
6020 strbuf_addch(&sb
, '\n');
6021 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6023 strbuf_release(&sb
);
6026 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
6029 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
6030 struct strbuf sb
= STRBUF_INIT
;
6031 strbuf_addf(&sb
, " mode change %06o => %06o",
6032 p
->one
->mode
, p
->two
->mode
);
6034 strbuf_addch(&sb
, ' ');
6035 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6037 strbuf_addch(&sb
, '\n');
6038 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6040 strbuf_release(&sb
);
6044 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
6045 struct diff_filepair
*p
)
6047 struct strbuf sb
= STRBUF_INIT
;
6048 struct strbuf names
= STRBUF_INIT
;
6050 pprint_rename(&names
, p
->one
->path
, p
->two
->path
);
6051 strbuf_addf(&sb
, " %s %s (%d%%)\n",
6052 renamecopy
, names
.buf
, similarity_index(p
));
6053 strbuf_release(&names
);
6054 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6056 show_mode_change(opt
, p
, 0);
6057 strbuf_release(&sb
);
6060 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
6063 case DIFF_STATUS_DELETED
:
6064 show_file_mode_name(opt
, "delete", p
->one
);
6066 case DIFF_STATUS_ADDED
:
6067 show_file_mode_name(opt
, "create", p
->two
);
6069 case DIFF_STATUS_COPIED
:
6070 show_rename_copy(opt
, "copy", p
);
6072 case DIFF_STATUS_RENAMED
:
6073 show_rename_copy(opt
, "rename", p
);
6077 struct strbuf sb
= STRBUF_INIT
;
6078 strbuf_addstr(&sb
, " rewrite ");
6079 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6080 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
6081 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6083 strbuf_release(&sb
);
6085 show_mode_change(opt
, p
, !p
->score
);
6095 static int remove_space(char *line
, int len
)
6101 for (i
= 0; i
< len
; i
++)
6102 if (!isspace((c
= line
[i
])))
6108 void flush_one_hunk(struct object_id
*result
, git_hash_ctx
*ctx
)
6110 unsigned char hash
[GIT_MAX_RAWSZ
];
6111 unsigned short carry
= 0;
6114 the_hash_algo
->final_fn(hash
, ctx
);
6115 the_hash_algo
->init_fn(ctx
);
6116 /* 20-byte sum, with carry */
6117 for (i
= 0; i
< the_hash_algo
->rawsz
; ++i
) {
6118 carry
+= result
->hash
[i
] + hash
[i
];
6119 result
->hash
[i
] = carry
;
6124 static void patch_id_consume(void *priv
, char *line
, unsigned long len
)
6126 struct patch_id_t
*data
= priv
;
6129 if (len
> 12 && starts_with(line
, "\\ "))
6131 new_len
= remove_space(line
, len
);
6133 the_hash_algo
->update_fn(data
->ctx
, line
, new_len
);
6134 data
->patchlen
+= new_len
;
6137 static void patch_id_add_string(git_hash_ctx
*ctx
, const char *str
)
6139 the_hash_algo
->update_fn(ctx
, str
, strlen(str
));
6142 static void patch_id_add_mode(git_hash_ctx
*ctx
, unsigned mode
)
6144 /* large enough for 2^32 in octal */
6146 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
6147 the_hash_algo
->update_fn(ctx
, buf
, len
);
6150 /* returns 0 upon success, and writes result into oid */
6151 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
, int stable
)
6153 struct diff_queue_struct
*q
= &diff_queued_diff
;
6156 struct patch_id_t data
;
6158 the_hash_algo
->init_fn(&ctx
);
6159 memset(&data
, 0, sizeof(struct patch_id_t
));
6163 for (i
= 0; i
< q
->nr
; i
++) {
6167 struct diff_filepair
*p
= q
->queue
[i
];
6170 memset(&xpp
, 0, sizeof(xpp
));
6171 memset(&xecfg
, 0, sizeof(xecfg
));
6173 return error("internal diff status error");
6174 if (p
->status
== DIFF_STATUS_UNKNOWN
)
6176 if (diff_unmodified_pair(p
))
6178 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6179 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6181 if (DIFF_PAIR_UNMERGED(p
))
6184 diff_fill_oid_info(p
->one
, options
->repo
->index
);
6185 diff_fill_oid_info(p
->two
, options
->repo
->index
);
6187 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
6188 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
6189 patch_id_add_string(&ctx
, "diff--git");
6190 patch_id_add_string(&ctx
, "a/");
6191 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6192 patch_id_add_string(&ctx
, "b/");
6193 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6195 if (p
->one
->mode
== 0) {
6196 patch_id_add_string(&ctx
, "newfilemode");
6197 patch_id_add_mode(&ctx
, p
->two
->mode
);
6198 patch_id_add_string(&ctx
, "---/dev/null");
6199 patch_id_add_string(&ctx
, "+++b/");
6200 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6201 } else if (p
->two
->mode
== 0) {
6202 patch_id_add_string(&ctx
, "deletedfilemode");
6203 patch_id_add_mode(&ctx
, p
->one
->mode
);
6204 patch_id_add_string(&ctx
, "---a/");
6205 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6206 patch_id_add_string(&ctx
, "+++/dev/null");
6208 patch_id_add_string(&ctx
, "---a/");
6209 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6210 patch_id_add_string(&ctx
, "+++b/");
6211 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6214 if (diff_header_only
)
6217 if (fill_mmfile(options
->repo
, &mf1
, p
->one
) < 0 ||
6218 fill_mmfile(options
->repo
, &mf2
, p
->two
) < 0)
6219 return error("unable to read files to diff");
6221 if (diff_filespec_is_binary(options
->repo
, p
->one
) ||
6222 diff_filespec_is_binary(options
->repo
, p
->two
)) {
6223 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->one
->oid
),
6224 the_hash_algo
->hexsz
);
6225 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->two
->oid
),
6226 the_hash_algo
->hexsz
);
6233 if (xdi_diff_outf(&mf1
, &mf2
, discard_hunk_line
,
6234 patch_id_consume
, &data
, &xpp
, &xecfg
))
6235 return error("unable to generate patch-id diff for %s",
6239 flush_one_hunk(oid
, &ctx
);
6243 the_hash_algo
->final_fn(oid
->hash
, &ctx
);
6248 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
, int stable
)
6250 struct diff_queue_struct
*q
= &diff_queued_diff
;
6252 int result
= diff_get_patch_id(options
, oid
, diff_header_only
, stable
);
6254 for (i
= 0; i
< q
->nr
; i
++)
6255 diff_free_filepair(q
->queue
[i
]);
6258 DIFF_QUEUE_CLEAR(q
);
6263 static int is_summary_empty(const struct diff_queue_struct
*q
)
6267 for (i
= 0; i
< q
->nr
; i
++) {
6268 const struct diff_filepair
*p
= q
->queue
[i
];
6270 switch (p
->status
) {
6271 case DIFF_STATUS_DELETED
:
6272 case DIFF_STATUS_ADDED
:
6273 case DIFF_STATUS_COPIED
:
6274 case DIFF_STATUS_RENAMED
:
6279 if (p
->one
->mode
&& p
->two
->mode
&&
6280 p
->one
->mode
!= p
->two
->mode
)
6288 static const char rename_limit_warning
[] =
6289 N_("inexact rename detection was skipped due to too many files.");
6291 static const char degrade_cc_to_c_warning
[] =
6292 N_("only found copies from modified paths due to too many files.");
6294 static const char rename_limit_advice
[] =
6295 N_("you may want to set your %s variable to at least "
6296 "%d and retry the command.");
6298 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
6302 warning(_(degrade_cc_to_c_warning
));
6304 warning(_(rename_limit_warning
));
6308 warning(_(rename_limit_advice
), varname
, needed
);
6311 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
6314 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
6315 struct diff_queue_struct
*q
= &diff_queued_diff
;
6317 if (WSEH_NEW
& WS_RULE_MASK
)
6318 BUG("WS rules bit mask overlaps with diff symbol flags");
6321 o
->emitted_symbols
= &esm
;
6323 for (i
= 0; i
< q
->nr
; i
++) {
6324 struct diff_filepair
*p
= q
->queue
[i
];
6325 if (check_pair_status(p
))
6326 diff_flush_patch(p
, o
);
6329 if (o
->emitted_symbols
) {
6330 if (o
->color_moved
) {
6331 struct hashmap add_lines
, del_lines
;
6333 if (o
->color_moved_ws_handling
&
6334 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
6335 o
->color_moved_ws_handling
|= XDF_IGNORE_WHITESPACE
;
6337 hashmap_init(&del_lines
, moved_entry_cmp
, o
, 0);
6338 hashmap_init(&add_lines
, moved_entry_cmp
, o
, 0);
6340 add_lines_to_move_detection(o
, &add_lines
, &del_lines
);
6341 mark_color_as_moved(o
, &add_lines
, &del_lines
);
6342 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
6345 hashmap_clear_and_free(&add_lines
, struct moved_entry
,
6347 hashmap_clear_and_free(&del_lines
, struct moved_entry
,
6351 for (i
= 0; i
< esm
.nr
; i
++)
6352 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
6354 for (i
= 0; i
< esm
.nr
; i
++)
6355 free((void *)esm
.buf
[i
].line
);
6358 o
->emitted_symbols
= NULL
;
6362 static void diff_free_file(struct diff_options
*options
)
6364 if (options
->close_file
)
6365 fclose(options
->file
);
6368 static void diff_free_ignore_regex(struct diff_options
*options
)
6372 for (i
= 0; i
< options
->ignore_regex_nr
; i
++) {
6373 regfree(options
->ignore_regex
[i
]);
6374 free(options
->ignore_regex
[i
]);
6376 free(options
->ignore_regex
);
6379 void diff_free(struct diff_options
*options
)
6381 if (options
->no_free
)
6384 diff_free_file(options
);
6385 diff_free_ignore_regex(options
);
6388 void diff_flush(struct diff_options
*options
)
6390 struct diff_queue_struct
*q
= &diff_queued_diff
;
6391 int i
, output_format
= options
->output_format
;
6393 int dirstat_by_line
= 0;
6396 * Order: raw, stat, summary, patch
6397 * or: name/name-status/checkdiff (other bits clear)
6402 if (output_format
& (DIFF_FORMAT_RAW
|
6404 DIFF_FORMAT_NAME_STATUS
|
6405 DIFF_FORMAT_CHECKDIFF
)) {
6406 for (i
= 0; i
< q
->nr
; i
++) {
6407 struct diff_filepair
*p
= q
->queue
[i
];
6408 if (check_pair_status(p
))
6409 flush_one_pair(p
, options
);
6414 if (output_format
& DIFF_FORMAT_DIRSTAT
&& options
->flags
.dirstat_by_line
)
6415 dirstat_by_line
= 1;
6417 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
6419 struct diffstat_t diffstat
;
6421 compute_diffstat(options
, &diffstat
, q
);
6422 if (output_format
& DIFF_FORMAT_NUMSTAT
)
6423 show_numstat(&diffstat
, options
);
6424 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
6425 show_stats(&diffstat
, options
);
6426 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
6427 show_shortstats(&diffstat
, options
);
6428 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
6429 show_dirstat_by_line(&diffstat
, options
);
6430 free_diffstat_info(&diffstat
);
6433 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
6434 show_dirstat(options
);
6436 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
6437 for (i
= 0; i
< q
->nr
; i
++) {
6438 diff_summary(options
, q
->queue
[i
]);
6443 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
6444 options
->flags
.exit_with_status
&&
6445 options
->flags
.diff_from_contents
) {
6447 * run diff_flush_patch for the exit status. setting
6448 * options->file to /dev/null should be safe, because we
6449 * aren't supposed to produce any output anyway.
6451 diff_free_file(options
);
6452 options
->file
= xfopen("/dev/null", "w");
6453 options
->close_file
= 1;
6454 options
->color_moved
= 0;
6455 for (i
= 0; i
< q
->nr
; i
++) {
6456 struct diff_filepair
*p
= q
->queue
[i
];
6457 if (check_pair_status(p
))
6458 diff_flush_patch(p
, options
);
6459 if (options
->found_changes
)
6464 if (output_format
& DIFF_FORMAT_PATCH
) {
6466 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
6467 if (options
->stat_sep
)
6468 /* attach patch instead of inline */
6469 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
6473 diff_flush_patch_all_file_pairs(options
);
6476 if (output_format
& DIFF_FORMAT_CALLBACK
)
6477 options
->format_callback(q
, options
, options
->format_callback_data
);
6479 for (i
= 0; i
< q
->nr
; i
++)
6480 diff_free_filepair(q
->queue
[i
]);
6483 DIFF_QUEUE_CLEAR(q
);
6487 * Report the content-level differences with HAS_CHANGES;
6488 * diff_addremove/diff_change does not set the bit when
6489 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6491 if (options
->flags
.diff_from_contents
) {
6492 if (options
->found_changes
)
6493 options
->flags
.has_changes
= 1;
6495 options
->flags
.has_changes
= 0;
6499 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
6501 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
6503 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
6505 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
6506 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
6507 filter_bit_tst(p
->status
, options
)));
6510 static void diffcore_apply_filter(struct diff_options
*options
)
6513 struct diff_queue_struct
*q
= &diff_queued_diff
;
6514 struct diff_queue_struct outq
;
6516 DIFF_QUEUE_CLEAR(&outq
);
6518 if (!options
->filter
)
6521 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
6523 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
6524 if (match_filter(options
, q
->queue
[i
]))
6530 /* otherwise we will clear the whole queue
6531 * by copying the empty outq at the end of this
6532 * function, but first clear the current entries
6535 for (i
= 0; i
< q
->nr
; i
++)
6536 diff_free_filepair(q
->queue
[i
]);
6539 /* Only the matching ones */
6540 for (i
= 0; i
< q
->nr
; i
++) {
6541 struct diff_filepair
*p
= q
->queue
[i
];
6542 if (match_filter(options
, p
))
6545 diff_free_filepair(p
);
6552 /* Check whether two filespecs with the same mode and size are identical */
6553 static int diff_filespec_is_identical(struct repository
*r
,
6554 struct diff_filespec
*one
,
6555 struct diff_filespec
*two
)
6557 if (S_ISGITLINK(one
->mode
))
6559 if (diff_populate_filespec(r
, one
, NULL
))
6561 if (diff_populate_filespec(r
, two
, NULL
))
6563 return !memcmp(one
->data
, two
->data
, one
->size
);
6566 static int diff_filespec_check_stat_unmatch(struct repository
*r
,
6567 struct diff_filepair
*p
)
6569 struct diff_populate_filespec_options dpf_options
= {
6570 .check_size_only
= 1,
6571 .missing_object_cb
= diff_queued_diff_prefetch
,
6572 .missing_object_data
= r
,
6575 if (p
->done_skip_stat_unmatch
)
6576 return p
->skip_stat_unmatch_result
;
6578 p
->done_skip_stat_unmatch
= 1;
6579 p
->skip_stat_unmatch_result
= 0;
6581 * 1. Entries that come from stat info dirtiness
6582 * always have both sides (iow, not create/delete),
6583 * one side of the object name is unknown, with
6584 * the same mode and size. Keep the ones that
6585 * do not match these criteria. They have real
6588 * 2. At this point, the file is known to be modified,
6589 * with the same mode and size, and the object
6590 * name of one side is unknown. Need to inspect
6591 * the identical contents.
6593 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
6594 !DIFF_FILE_VALID(p
->two
) ||
6595 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
6596 (p
->one
->mode
!= p
->two
->mode
) ||
6597 diff_populate_filespec(r
, p
->one
, &dpf_options
) ||
6598 diff_populate_filespec(r
, p
->two
, &dpf_options
) ||
6599 (p
->one
->size
!= p
->two
->size
) ||
6600 !diff_filespec_is_identical(r
, p
->one
, p
->two
)) /* (2) */
6601 p
->skip_stat_unmatch_result
= 1;
6602 return p
->skip_stat_unmatch_result
;
6605 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
6608 struct diff_queue_struct
*q
= &diff_queued_diff
;
6609 struct diff_queue_struct outq
;
6610 DIFF_QUEUE_CLEAR(&outq
);
6612 for (i
= 0; i
< q
->nr
; i
++) {
6613 struct diff_filepair
*p
= q
->queue
[i
];
6615 if (diff_filespec_check_stat_unmatch(diffopt
->repo
, p
))
6619 * The caller can subtract 1 from skip_stat_unmatch
6620 * to determine how many paths were dirty only
6621 * due to stat info mismatch.
6623 if (!diffopt
->flags
.no_index
)
6624 diffopt
->skip_stat_unmatch
++;
6625 diff_free_filepair(p
);
6632 static int diffnamecmp(const void *a_
, const void *b_
)
6634 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
6635 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
6636 const char *name_a
, *name_b
;
6638 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
6639 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
6640 return strcmp(name_a
, name_b
);
6643 void diffcore_fix_diff_index(void)
6645 struct diff_queue_struct
*q
= &diff_queued_diff
;
6646 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
6649 void diff_add_if_missing(struct repository
*r
,
6650 struct oid_array
*to_fetch
,
6651 const struct diff_filespec
*filespec
)
6653 if (filespec
&& filespec
->oid_valid
&&
6654 !S_ISGITLINK(filespec
->mode
) &&
6655 oid_object_info_extended(r
, &filespec
->oid
, NULL
,
6656 OBJECT_INFO_FOR_PREFETCH
))
6657 oid_array_append(to_fetch
, &filespec
->oid
);
6660 void diff_queued_diff_prefetch(void *repository
)
6662 struct repository
*repo
= repository
;
6664 struct diff_queue_struct
*q
= &diff_queued_diff
;
6665 struct oid_array to_fetch
= OID_ARRAY_INIT
;
6667 for (i
= 0; i
< q
->nr
; i
++) {
6668 struct diff_filepair
*p
= q
->queue
[i
];
6669 diff_add_if_missing(repo
, &to_fetch
, p
->one
);
6670 diff_add_if_missing(repo
, &to_fetch
, p
->two
);
6674 * NEEDSWORK: Consider deduplicating the OIDs sent.
6676 promisor_remote_get_direct(repo
, to_fetch
.oid
, to_fetch
.nr
);
6678 oid_array_clear(&to_fetch
);
6681 void diffcore_std(struct diff_options
*options
)
6683 int output_formats_to_prefetch
= DIFF_FORMAT_DIFFSTAT
|
6684 DIFF_FORMAT_NUMSTAT
|
6686 DIFF_FORMAT_SHORTSTAT
|
6687 DIFF_FORMAT_DIRSTAT
;
6690 * Check if the user requested a blob-data-requiring diff output and/or
6691 * break-rewrite detection (which requires blob data). If yes, prefetch
6694 * If no prefetching occurs, diffcore_rename() will prefetch if it
6695 * decides that it needs inexact rename detection.
6697 if (options
->repo
== the_repository
&& has_promisor_remote() &&
6698 (options
->output_format
& output_formats_to_prefetch
||
6699 options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
6700 diff_queued_diff_prefetch(options
->repo
);
6702 /* NOTE please keep the following in sync with diff_tree_combined() */
6703 if (options
->skip_stat_unmatch
)
6704 diffcore_skip_stat_unmatch(options
);
6705 if (!options
->found_follow
) {
6706 /* See try_to_follow_renames() in tree-diff.c */
6707 if (options
->break_opt
!= -1)
6708 diffcore_break(options
->repo
,
6709 options
->break_opt
);
6710 if (options
->detect_rename
)
6711 diffcore_rename(options
);
6712 if (options
->break_opt
!= -1)
6713 diffcore_merge_broken();
6715 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
6716 diffcore_pickaxe(options
);
6717 if (options
->orderfile
)
6718 diffcore_order(options
->orderfile
);
6719 if (options
->rotate_to
)
6720 diffcore_rotate(options
);
6721 if (!options
->found_follow
)
6722 /* See try_to_follow_renames() in tree-diff.c */
6723 diff_resolve_rename_copy();
6724 diffcore_apply_filter(options
);
6726 if (diff_queued_diff
.nr
&& !options
->flags
.diff_from_contents
)
6727 options
->flags
.has_changes
= 1;
6729 options
->flags
.has_changes
= 0;
6731 options
->found_follow
= 0;
6734 int diff_result_code(struct diff_options
*opt
, int status
)
6738 diff_warn_rename_limit("diff.renameLimit",
6739 opt
->needed_rename_limit
,
6740 opt
->degraded_cc_to_c
);
6741 if (!opt
->flags
.exit_with_status
&&
6742 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
6744 if (opt
->flags
.exit_with_status
&&
6745 opt
->flags
.has_changes
)
6747 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
6748 opt
->flags
.check_failed
)
6753 int diff_can_quit_early(struct diff_options
*opt
)
6755 return (opt
->flags
.quick
&&
6757 opt
->flags
.has_changes
);
6761 * Shall changes to this submodule be ignored?
6763 * Submodule changes can be configured to be ignored separately for each path,
6764 * but that configuration can be overridden from the command line.
6766 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
6769 struct diff_flags orig_flags
= options
->flags
;
6770 if (!options
->flags
.override_submodule_config
)
6771 set_diffopt_flags_from_submodule_config(options
, path
);
6772 if (options
->flags
.ignore_submodules
)
6774 options
->flags
= orig_flags
;
6778 void compute_diffstat(struct diff_options
*options
,
6779 struct diffstat_t
*diffstat
,
6780 struct diff_queue_struct
*q
)
6784 memset(diffstat
, 0, sizeof(struct diffstat_t
));
6785 for (i
= 0; i
< q
->nr
; i
++) {
6786 struct diff_filepair
*p
= q
->queue
[i
];
6787 if (check_pair_status(p
))
6788 diff_flush_stat(p
, options
, diffstat
);
6792 void diff_addremove(struct diff_options
*options
,
6793 int addremove
, unsigned mode
,
6794 const struct object_id
*oid
,
6796 const char *concatpath
, unsigned dirty_submodule
)
6798 struct diff_filespec
*one
, *two
;
6800 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
6803 /* This may look odd, but it is a preparation for
6804 * feeding "there are unchanged files which should
6805 * not produce diffs, but when you are doing copy
6806 * detection you would need them, so here they are"
6807 * entries to the diff-core. They will be prefixed
6808 * with something like '=' or '*' (I haven't decided
6809 * which but should not make any difference).
6810 * Feeding the same new and old to diff_change()
6811 * also has the same effect.
6812 * Before the final output happens, they are pruned after
6813 * merged into rename/copy pairs as appropriate.
6815 if (options
->flags
.reverse_diff
)
6816 addremove
= (addremove
== '+' ? '-' :
6817 addremove
== '-' ? '+' : addremove
);
6819 if (options
->prefix
&&
6820 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
6823 one
= alloc_filespec(concatpath
);
6824 two
= alloc_filespec(concatpath
);
6826 if (addremove
!= '+')
6827 fill_filespec(one
, oid
, oid_valid
, mode
);
6828 if (addremove
!= '-') {
6829 fill_filespec(two
, oid
, oid_valid
, mode
);
6830 two
->dirty_submodule
= dirty_submodule
;
6833 diff_queue(&diff_queued_diff
, one
, two
);
6834 if (!options
->flags
.diff_from_contents
)
6835 options
->flags
.has_changes
= 1;
6838 void diff_change(struct diff_options
*options
,
6839 unsigned old_mode
, unsigned new_mode
,
6840 const struct object_id
*old_oid
,
6841 const struct object_id
*new_oid
,
6842 int old_oid_valid
, int new_oid_valid
,
6843 const char *concatpath
,
6844 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
6846 struct diff_filespec
*one
, *two
;
6847 struct diff_filepair
*p
;
6849 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
6850 is_submodule_ignored(concatpath
, options
))
6853 if (options
->flags
.reverse_diff
) {
6854 SWAP(old_mode
, new_mode
);
6855 SWAP(old_oid
, new_oid
);
6856 SWAP(old_oid_valid
, new_oid_valid
);
6857 SWAP(old_dirty_submodule
, new_dirty_submodule
);
6860 if (options
->prefix
&&
6861 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
6864 one
= alloc_filespec(concatpath
);
6865 two
= alloc_filespec(concatpath
);
6866 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
6867 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
6868 one
->dirty_submodule
= old_dirty_submodule
;
6869 two
->dirty_submodule
= new_dirty_submodule
;
6870 p
= diff_queue(&diff_queued_diff
, one
, two
);
6872 if (options
->flags
.diff_from_contents
)
6875 if (options
->flags
.quick
&& options
->skip_stat_unmatch
&&
6876 !diff_filespec_check_stat_unmatch(options
->repo
, p
)) {
6877 diff_free_filespec_data(p
->one
);
6878 diff_free_filespec_data(p
->two
);
6882 options
->flags
.has_changes
= 1;
6885 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
6887 struct diff_filepair
*pair
;
6888 struct diff_filespec
*one
, *two
;
6890 if (options
->prefix
&&
6891 strncmp(path
, options
->prefix
, options
->prefix_length
))
6894 one
= alloc_filespec(path
);
6895 two
= alloc_filespec(path
);
6896 pair
= diff_queue(&diff_queued_diff
, one
, two
);
6897 pair
->is_unmerged
= 1;
6901 static char *run_textconv(struct repository
*r
,
6903 struct diff_filespec
*spec
,
6906 struct diff_tempfile
*temp
;
6907 const char *argv
[3];
6908 const char **arg
= argv
;
6909 struct child_process child
= CHILD_PROCESS_INIT
;
6910 struct strbuf buf
= STRBUF_INIT
;
6913 temp
= prepare_temp_file(r
, spec
->path
, spec
);
6915 *arg
++ = temp
->name
;
6918 child
.use_shell
= 1;
6921 if (start_command(&child
)) {
6926 if (strbuf_read(&buf
, child
.out
, 0) < 0)
6927 err
= error("error reading from textconv command '%s'", pgm
);
6930 if (finish_command(&child
) || err
) {
6931 strbuf_release(&buf
);
6937 return strbuf_detach(&buf
, outsize
);
6940 size_t fill_textconv(struct repository
*r
,
6941 struct userdiff_driver
*driver
,
6942 struct diff_filespec
*df
,
6948 if (!DIFF_FILE_VALID(df
)) {
6952 if (diff_populate_filespec(r
, df
, NULL
))
6953 die("unable to read files to diff");
6958 if (!driver
->textconv
)
6959 BUG("fill_textconv called with non-textconv driver");
6961 if (driver
->textconv_cache
&& df
->oid_valid
) {
6962 *outbuf
= notes_cache_get(driver
->textconv_cache
,
6969 *outbuf
= run_textconv(r
, driver
->textconv
, df
, &size
);
6971 die("unable to read files to diff");
6973 if (driver
->textconv_cache
&& df
->oid_valid
) {
6974 /* ignore errors, as we might be in a readonly repository */
6975 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
6978 * we could save up changes and flush them all at the end,
6979 * but we would need an extra call after all diffing is done.
6980 * Since generating a cache entry is the slow path anyway,
6981 * this extra overhead probably isn't a big deal.
6983 notes_cache_write(driver
->textconv_cache
);
6989 int textconv_object(struct repository
*r
,
6992 const struct object_id
*oid
,
6995 unsigned long *buf_size
)
6997 struct diff_filespec
*df
;
6998 struct userdiff_driver
*textconv
;
7000 df
= alloc_filespec(path
);
7001 fill_filespec(df
, oid
, oid_valid
, mode
);
7002 textconv
= get_textconv(r
, df
);
7008 *buf_size
= fill_textconv(r
, textconv
, df
, buf
);
7013 void setup_diff_pager(struct diff_options
*opt
)
7016 * If the user asked for our exit code, then either they want --quiet
7017 * or --exit-code. We should definitely not bother with a pager in the
7018 * former case, as we will generate no output. Since we still properly
7019 * report our exit code even when a pager is run, we _could_ run a
7020 * pager with --exit-code. But since we have not done so historically,
7021 * and because it is easy to find people oneline advising "git diff
7022 * --exit-code" in hooks and other scripts, we do not do so.
7024 if (!opt
->flags
.exit_with_status
&&
7025 check_pager_config("diff") != 0)