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"
23 #include "string-list.h"
27 #include "parse-options.h"
29 #include "promisor-remote.h"
33 #ifdef NO_FAST_WORKING_DIRECTORY
34 #define FAST_WORKING_DIRECTORY 0
36 #define FAST_WORKING_DIRECTORY 1
39 static int diff_detect_rename_default
;
40 static int diff_indent_heuristic
= 1;
41 static int diff_rename_limit_default
= 1000;
42 static int diff_suppress_blank_empty
;
43 static int diff_use_color_default
= -1;
44 static int diff_color_moved_default
;
45 static int diff_color_moved_ws_default
;
46 static int diff_context_default
= 3;
47 static int diff_interhunk_context_default
;
48 static const char *diff_word_regex_cfg
;
49 static const char *external_diff_cmd_cfg
;
50 static const char *diff_order_file_cfg
;
51 int diff_auto_refresh_index
= 1;
52 static int diff_mnemonic_prefix
;
53 static int diff_no_prefix
;
54 static int diff_relative
;
55 static int diff_stat_graph_width
;
56 static int diff_dirstat_permille_default
= 30;
57 static struct diff_options default_diff_options
;
58 static long diff_algorithm
;
59 static unsigned ws_error_highlight_default
= WSEH_NEW
;
61 static char diff_colors
[][COLOR_MAXLEN
] = {
63 GIT_COLOR_NORMAL
, /* CONTEXT */
64 GIT_COLOR_BOLD
, /* METAINFO */
65 GIT_COLOR_CYAN
, /* FRAGINFO */
66 GIT_COLOR_RED
, /* OLD */
67 GIT_COLOR_GREEN
, /* NEW */
68 GIT_COLOR_YELLOW
, /* COMMIT */
69 GIT_COLOR_BG_RED
, /* WHITESPACE */
70 GIT_COLOR_NORMAL
, /* FUNCINFO */
71 GIT_COLOR_BOLD_MAGENTA
, /* OLD_MOVED */
72 GIT_COLOR_BOLD_BLUE
, /* OLD_MOVED ALTERNATIVE */
73 GIT_COLOR_FAINT
, /* OLD_MOVED_DIM */
74 GIT_COLOR_FAINT_ITALIC
, /* OLD_MOVED_ALTERNATIVE_DIM */
75 GIT_COLOR_BOLD_CYAN
, /* NEW_MOVED */
76 GIT_COLOR_BOLD_YELLOW
, /* NEW_MOVED ALTERNATIVE */
77 GIT_COLOR_FAINT
, /* NEW_MOVED_DIM */
78 GIT_COLOR_FAINT_ITALIC
, /* NEW_MOVED_ALTERNATIVE_DIM */
79 GIT_COLOR_FAINT
, /* CONTEXT_DIM */
80 GIT_COLOR_FAINT_RED
, /* OLD_DIM */
81 GIT_COLOR_FAINT_GREEN
, /* NEW_DIM */
82 GIT_COLOR_BOLD
, /* CONTEXT_BOLD */
83 GIT_COLOR_BOLD_RED
, /* OLD_BOLD */
84 GIT_COLOR_BOLD_GREEN
, /* NEW_BOLD */
87 static const char *color_diff_slots
[] = {
88 [DIFF_CONTEXT
] = "context",
89 [DIFF_METAINFO
] = "meta",
90 [DIFF_FRAGINFO
] = "frag",
91 [DIFF_FILE_OLD
] = "old",
92 [DIFF_FILE_NEW
] = "new",
93 [DIFF_COMMIT
] = "commit",
94 [DIFF_WHITESPACE
] = "whitespace",
95 [DIFF_FUNCINFO
] = "func",
96 [DIFF_FILE_OLD_MOVED
] = "oldMoved",
97 [DIFF_FILE_OLD_MOVED_ALT
] = "oldMovedAlternative",
98 [DIFF_FILE_OLD_MOVED_DIM
] = "oldMovedDimmed",
99 [DIFF_FILE_OLD_MOVED_ALT_DIM
] = "oldMovedAlternativeDimmed",
100 [DIFF_FILE_NEW_MOVED
] = "newMoved",
101 [DIFF_FILE_NEW_MOVED_ALT
] = "newMovedAlternative",
102 [DIFF_FILE_NEW_MOVED_DIM
] = "newMovedDimmed",
103 [DIFF_FILE_NEW_MOVED_ALT_DIM
] = "newMovedAlternativeDimmed",
104 [DIFF_CONTEXT_DIM
] = "contextDimmed",
105 [DIFF_FILE_OLD_DIM
] = "oldDimmed",
106 [DIFF_FILE_NEW_DIM
] = "newDimmed",
107 [DIFF_CONTEXT_BOLD
] = "contextBold",
108 [DIFF_FILE_OLD_BOLD
] = "oldBold",
109 [DIFF_FILE_NEW_BOLD
] = "newBold",
112 define_list_config_array_extra(color_diff_slots
, {"plain"});
114 static int parse_diff_color_slot(const char *var
)
116 if (!strcasecmp(var
, "plain"))
118 return LOOKUP_CONFIG(color_diff_slots
, var
);
121 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
122 struct strbuf
*errmsg
)
124 char *params_copy
= xstrdup(params_string
);
125 struct string_list params
= STRING_LIST_INIT_NODUP
;
130 string_list_split_in_place(¶ms
, params_copy
, ',', -1);
131 for (i
= 0; i
< params
.nr
; i
++) {
132 const char *p
= params
.items
[i
].string
;
133 if (!strcmp(p
, "changes")) {
134 options
->flags
.dirstat_by_line
= 0;
135 options
->flags
.dirstat_by_file
= 0;
136 } else if (!strcmp(p
, "lines")) {
137 options
->flags
.dirstat_by_line
= 1;
138 options
->flags
.dirstat_by_file
= 0;
139 } else if (!strcmp(p
, "files")) {
140 options
->flags
.dirstat_by_line
= 0;
141 options
->flags
.dirstat_by_file
= 1;
142 } else if (!strcmp(p
, "noncumulative")) {
143 options
->flags
.dirstat_cumulative
= 0;
144 } else if (!strcmp(p
, "cumulative")) {
145 options
->flags
.dirstat_cumulative
= 1;
146 } else if (isdigit(*p
)) {
148 int permille
= strtoul(p
, &end
, 10) * 10;
149 if (*end
== '.' && isdigit(*++end
)) {
150 /* only use first digit */
151 permille
+= *end
- '0';
152 /* .. and ignore any further digits */
153 while (isdigit(*++end
))
157 options
->dirstat_permille
= permille
;
159 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
164 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
169 string_list_clear(¶ms
, 0);
174 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
176 if (!strcmp(value
, "log"))
177 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
178 else if (!strcmp(value
, "short"))
179 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
180 else if (!strcmp(value
, "diff"))
181 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
183 * Please update $__git_diff_submodule_formats in
184 * git-completion.bash when you add new formats.
191 int git_config_rename(const char *var
, const char *value
)
194 return DIFF_DETECT_RENAME
;
195 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
196 return DIFF_DETECT_COPY
;
197 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
200 long parse_algorithm_value(const char *value
)
204 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
206 else if (!strcasecmp(value
, "minimal"))
207 return XDF_NEED_MINIMAL
;
208 else if (!strcasecmp(value
, "patience"))
209 return XDF_PATIENCE_DIFF
;
210 else if (!strcasecmp(value
, "histogram"))
211 return XDF_HISTOGRAM_DIFF
;
213 * Please update $__git_diff_algorithms in git-completion.bash
214 * when you add new algorithms.
219 static int parse_one_token(const char **arg
, const char *token
)
222 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
229 static int parse_ws_error_highlight(const char *arg
)
231 const char *orig_arg
= arg
;
235 if (parse_one_token(&arg
, "none"))
237 else if (parse_one_token(&arg
, "default"))
239 else if (parse_one_token(&arg
, "all"))
240 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
241 else if (parse_one_token(&arg
, "new"))
243 else if (parse_one_token(&arg
, "old"))
245 else if (parse_one_token(&arg
, "context"))
248 return -1 - (int)(arg
- orig_arg
);
257 * These are to give UI layer defaults.
258 * The core-level commands such as git-diff-files should
259 * never be affected by the setting of diff.renames
260 * the user happens to have in the configuration file.
262 void init_diff_ui_defaults(void)
264 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
267 int git_diff_heuristic_config(const char *var
, const char *value
, void *cb
)
269 if (!strcmp(var
, "diff.indentheuristic"))
270 diff_indent_heuristic
= git_config_bool(var
, value
);
274 static int parse_color_moved(const char *arg
)
276 switch (git_parse_maybe_bool(arg
)) {
278 return COLOR_MOVED_NO
;
280 return COLOR_MOVED_DEFAULT
;
285 if (!strcmp(arg
, "no"))
286 return COLOR_MOVED_NO
;
287 else if (!strcmp(arg
, "plain"))
288 return COLOR_MOVED_PLAIN
;
289 else if (!strcmp(arg
, "blocks"))
290 return COLOR_MOVED_BLOCKS
;
291 else if (!strcmp(arg
, "zebra"))
292 return COLOR_MOVED_ZEBRA
;
293 else if (!strcmp(arg
, "default"))
294 return COLOR_MOVED_DEFAULT
;
295 else if (!strcmp(arg
, "dimmed-zebra"))
296 return COLOR_MOVED_ZEBRA_DIM
;
297 else if (!strcmp(arg
, "dimmed_zebra"))
298 return COLOR_MOVED_ZEBRA_DIM
;
300 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
303 static unsigned parse_color_moved_ws(const char *arg
)
306 struct string_list l
= STRING_LIST_INIT_DUP
;
307 struct string_list_item
*i
;
309 string_list_split(&l
, arg
, ',', -1);
311 for_each_string_list_item(i
, &l
) {
312 struct strbuf sb
= STRBUF_INIT
;
313 strbuf_addstr(&sb
, i
->string
);
316 if (!strcmp(sb
.buf
, "no"))
318 else if (!strcmp(sb
.buf
, "ignore-space-change"))
319 ret
|= XDF_IGNORE_WHITESPACE_CHANGE
;
320 else if (!strcmp(sb
.buf
, "ignore-space-at-eol"))
321 ret
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
322 else if (!strcmp(sb
.buf
, "ignore-all-space"))
323 ret
|= XDF_IGNORE_WHITESPACE
;
324 else if (!strcmp(sb
.buf
, "allow-indentation-change"))
325 ret
|= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
;
327 ret
|= COLOR_MOVED_WS_ERROR
;
328 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
);
334 if ((ret
& COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
) &&
335 (ret
& XDF_WHITESPACE_FLAGS
)) {
336 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
337 ret
|= COLOR_MOVED_WS_ERROR
;
340 string_list_clear(&l
, 0);
345 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
347 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
348 diff_use_color_default
= git_config_colorbool(var
, value
);
351 if (!strcmp(var
, "diff.colormoved")) {
352 int cm
= parse_color_moved(value
);
355 diff_color_moved_default
= cm
;
358 if (!strcmp(var
, "diff.colormovedws")) {
359 unsigned cm
= parse_color_moved_ws(value
);
360 if (cm
& COLOR_MOVED_WS_ERROR
)
362 diff_color_moved_ws_default
= cm
;
365 if (!strcmp(var
, "diff.context")) {
366 diff_context_default
= git_config_int(var
, value
);
367 if (diff_context_default
< 0)
371 if (!strcmp(var
, "diff.interhunkcontext")) {
372 diff_interhunk_context_default
= git_config_int(var
, value
);
373 if (diff_interhunk_context_default
< 0)
377 if (!strcmp(var
, "diff.renames")) {
378 diff_detect_rename_default
= git_config_rename(var
, value
);
381 if (!strcmp(var
, "diff.autorefreshindex")) {
382 diff_auto_refresh_index
= git_config_bool(var
, value
);
385 if (!strcmp(var
, "diff.mnemonicprefix")) {
386 diff_mnemonic_prefix
= git_config_bool(var
, value
);
389 if (!strcmp(var
, "diff.noprefix")) {
390 diff_no_prefix
= git_config_bool(var
, value
);
393 if (!strcmp(var
, "diff.relative")) {
394 diff_relative
= git_config_bool(var
, value
);
397 if (!strcmp(var
, "diff.statgraphwidth")) {
398 diff_stat_graph_width
= git_config_int(var
, value
);
401 if (!strcmp(var
, "diff.external"))
402 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
403 if (!strcmp(var
, "diff.wordregex"))
404 return git_config_string(&diff_word_regex_cfg
, var
, value
);
405 if (!strcmp(var
, "diff.orderfile"))
406 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
408 if (!strcmp(var
, "diff.ignoresubmodules"))
409 handle_ignore_submodules_arg(&default_diff_options
, value
);
411 if (!strcmp(var
, "diff.submodule")) {
412 if (parse_submodule_params(&default_diff_options
, value
))
413 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
418 if (!strcmp(var
, "diff.algorithm")) {
419 diff_algorithm
= parse_algorithm_value(value
);
420 if (diff_algorithm
< 0)
425 if (git_color_config(var
, value
, cb
) < 0)
428 return git_diff_basic_config(var
, value
, cb
);
431 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
435 if (!strcmp(var
, "diff.renamelimit")) {
436 diff_rename_limit_default
= git_config_int(var
, value
);
440 if (userdiff_config(var
, value
) < 0)
443 if (skip_prefix(var
, "diff.color.", &name
) ||
444 skip_prefix(var
, "color.diff.", &name
)) {
445 int slot
= parse_diff_color_slot(name
);
449 return config_error_nonbool(var
);
450 return color_parse(value
, diff_colors
[slot
]);
453 if (!strcmp(var
, "diff.wserrorhighlight")) {
454 int val
= parse_ws_error_highlight(value
);
457 ws_error_highlight_default
= val
;
461 /* like GNU diff's --suppress-blank-empty option */
462 if (!strcmp(var
, "diff.suppressblankempty") ||
463 /* for backwards compatibility */
464 !strcmp(var
, "diff.suppress-blank-empty")) {
465 diff_suppress_blank_empty
= git_config_bool(var
, value
);
469 if (!strcmp(var
, "diff.dirstat")) {
470 struct strbuf errmsg
= STRBUF_INIT
;
471 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
472 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
473 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
475 strbuf_release(&errmsg
);
476 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
480 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
483 return git_default_config(var
, value
, cb
);
486 static char *quote_two(const char *one
, const char *two
)
488 int need_one
= quote_c_style(one
, NULL
, NULL
, CQUOTE_NODQ
);
489 int need_two
= quote_c_style(two
, NULL
, NULL
, CQUOTE_NODQ
);
490 struct strbuf res
= STRBUF_INIT
;
492 if (need_one
+ need_two
) {
493 strbuf_addch(&res
, '"');
494 quote_c_style(one
, &res
, NULL
, CQUOTE_NODQ
);
495 quote_c_style(two
, &res
, NULL
, CQUOTE_NODQ
);
496 strbuf_addch(&res
, '"');
498 strbuf_addstr(&res
, one
);
499 strbuf_addstr(&res
, two
);
501 return strbuf_detach(&res
, NULL
);
504 static const char *external_diff(void)
506 static const char *external_diff_cmd
= NULL
;
507 static int done_preparing
= 0;
510 return external_diff_cmd
;
511 external_diff_cmd
= xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
512 if (!external_diff_cmd
)
513 external_diff_cmd
= external_diff_cmd_cfg
;
515 return external_diff_cmd
;
519 * Keep track of files used for diffing. Sometimes such an entry
520 * refers to a temporary file, sometimes to an existing file, and
521 * sometimes to "/dev/null".
523 static struct diff_tempfile
{
525 * filename external diff should read from, or NULL if this
526 * entry is currently not in use:
530 char hex
[GIT_MAX_HEXSZ
+ 1];
534 * If this diff_tempfile instance refers to a temporary file,
535 * this tempfile object is used to manage its lifetime.
537 struct tempfile
*tempfile
;
540 struct emit_callback
{
543 int blank_at_eof_in_preimage
;
544 int blank_at_eof_in_postimage
;
546 int lno_in_postimage
;
547 const char **label_path
;
548 struct diff_words_data
*diff_words
;
549 struct diff_options
*opt
;
550 struct strbuf
*header
;
553 static int count_lines(const char *data
, int size
)
555 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
562 completely_empty
= 0;
566 completely_empty
= 0;
569 if (completely_empty
)
572 count
++; /* no trailing newline */
576 static int fill_mmfile(struct repository
*r
, mmfile_t
*mf
,
577 struct diff_filespec
*one
)
579 if (!DIFF_FILE_VALID(one
)) {
580 mf
->ptr
= (char *)""; /* does not matter */
584 else if (diff_populate_filespec(r
, one
, NULL
))
588 mf
->size
= one
->size
;
592 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
593 static unsigned long diff_filespec_size(struct repository
*r
,
594 struct diff_filespec
*one
)
596 struct diff_populate_filespec_options dpf_options
= {
597 .check_size_only
= 1,
600 if (!DIFF_FILE_VALID(one
))
602 diff_populate_filespec(r
, one
, &dpf_options
);
606 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
609 long size
= mf
->size
;
614 ptr
+= size
- 1; /* pointing at the very end */
616 ; /* incomplete line */
618 ptr
--; /* skip the last LF */
619 while (mf
->ptr
< ptr
) {
621 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
622 if (*prev_eol
== '\n')
624 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
632 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
633 struct emit_callback
*ecbdata
)
636 unsigned ws_rule
= ecbdata
->ws_rule
;
637 l1
= count_trailing_blank(mf1
, ws_rule
);
638 l2
= count_trailing_blank(mf2
, ws_rule
);
640 ecbdata
->blank_at_eof_in_preimage
= 0;
641 ecbdata
->blank_at_eof_in_postimage
= 0;
644 at
= count_lines(mf1
->ptr
, mf1
->size
);
645 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
647 at
= count_lines(mf2
->ptr
, mf2
->size
);
648 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
651 static void emit_line_0(struct diff_options
*o
,
652 const char *set_sign
, const char *set
, unsigned reverse
, const char *reset
,
653 int first
, const char *line
, int len
)
655 int has_trailing_newline
, has_trailing_carriage_return
;
656 int needs_reset
= 0; /* at the end of the line */
657 FILE *file
= o
->file
;
659 fputs(diff_line_prefix(o
), file
);
661 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
662 if (has_trailing_newline
)
665 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
666 if (has_trailing_carriage_return
)
672 if (reverse
&& want_color(o
->use_color
)) {
673 fputs(GIT_COLOR_REVERSE
, file
);
678 fputs(set_sign
, file
);
689 if (set_sign
&& set
!= set_sign
)
694 fwrite(line
, len
, 1, file
);
695 needs_reset
= 1; /* 'line' may contain color codes. */
700 if (has_trailing_carriage_return
)
702 if (has_trailing_newline
)
706 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
707 const char *line
, int len
)
709 emit_line_0(o
, set
, NULL
, 0, reset
, 0, line
, len
);
713 DIFF_SYMBOL_BINARY_DIFF_HEADER
,
714 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
715 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
716 DIFF_SYMBOL_BINARY_DIFF_BODY
,
717 DIFF_SYMBOL_BINARY_DIFF_FOOTER
,
718 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
719 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
720 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
721 DIFF_SYMBOL_STATS_LINE
,
722 DIFF_SYMBOL_WORD_DIFF
,
723 DIFF_SYMBOL_STAT_SEP
,
725 DIFF_SYMBOL_SUBMODULE_ADD
,
726 DIFF_SYMBOL_SUBMODULE_DEL
,
727 DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
728 DIFF_SYMBOL_SUBMODULE_MODIFIED
,
729 DIFF_SYMBOL_SUBMODULE_HEADER
,
730 DIFF_SYMBOL_SUBMODULE_ERROR
,
731 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
,
732 DIFF_SYMBOL_REWRITE_DIFF
,
733 DIFF_SYMBOL_BINARY_FILES
,
735 DIFF_SYMBOL_FILEPAIR_PLUS
,
736 DIFF_SYMBOL_FILEPAIR_MINUS
,
737 DIFF_SYMBOL_WORDS_PORCELAIN
,
740 DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
743 DIFF_SYMBOL_NO_LF_EOF
,
744 DIFF_SYMBOL_CONTEXT_FRAGINFO
,
745 DIFF_SYMBOL_CONTEXT_MARKER
,
746 DIFF_SYMBOL_SEPARATOR
749 * Flags for content lines:
750 * 0..12 are whitespace rules
751 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
752 * 16 is marking if the line is blank at EOF
754 #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
755 #define DIFF_SYMBOL_MOVED_LINE (1<<17)
756 #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
757 #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
758 #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
761 * This struct is used when we need to buffer the output of the diff output.
763 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
764 * into the pre/post image file. This pointer could be a union with the
765 * line pointer. By storing an offset into the file instead of the literal line,
766 * we can decrease the memory footprint for the buffered output. At first we
767 * may want to only have indirection for the content lines, but we could also
768 * enhance the state for emitting prefabricated lines, e.g. the similarity
769 * score line or hunk/file headers would only need to store a number or path
770 * and then the output can be constructed later on depending on state.
772 struct emitted_diff_symbol
{
776 int indent_off
; /* Offset to first non-whitespace character */
777 int indent_width
; /* The visual width of the indentation */
781 #define EMITTED_DIFF_SYMBOL_INIT { 0 }
783 struct emitted_diff_symbols
{
784 struct emitted_diff_symbol
*buf
;
787 #define EMITTED_DIFF_SYMBOLS_INIT { 0 }
789 static void append_emitted_diff_symbol(struct diff_options
*o
,
790 struct emitted_diff_symbol
*e
)
792 struct emitted_diff_symbol
*f
;
794 ALLOC_GROW(o
->emitted_symbols
->buf
,
795 o
->emitted_symbols
->nr
+ 1,
796 o
->emitted_symbols
->alloc
);
797 f
= &o
->emitted_symbols
->buf
[o
->emitted_symbols
->nr
++];
799 memcpy(f
, e
, sizeof(struct emitted_diff_symbol
));
800 f
->line
= e
->line
? xmemdupz(e
->line
, e
->len
) : NULL
;
803 static void free_emitted_diff_symbols(struct emitted_diff_symbols
*e
)
812 const struct emitted_diff_symbol
*es
;
813 struct moved_entry
*next_line
;
814 struct moved_entry
*next_match
;
818 struct moved_entry
*match
;
819 int wsd
; /* The whitespace delta of this block */
822 #define INDENT_BLANKLINE INT_MIN
824 static void fill_es_indent_data(struct emitted_diff_symbol
*es
)
826 unsigned int off
= 0, i
;
827 int width
= 0, tab_width
= es
->flags
& WS_TAB_WIDTH_MASK
;
828 const char *s
= es
->line
;
829 const int len
= es
->len
;
831 /* skip any \v \f \r at start of indentation */
832 while (s
[off
] == '\f' || s
[off
] == '\v' ||
833 (s
[off
] == '\r' && off
< len
- 1))
836 /* calculate the visual width of indentation */
841 } else if (s
[off
] == '\t') {
842 width
+= tab_width
- (width
% tab_width
);
843 while (s
[++off
] == '\t')
850 /* check if this line is blank */
851 for (i
= off
; i
< len
; i
++)
856 es
->indent_width
= INDENT_BLANKLINE
;
857 es
->indent_off
= len
;
859 es
->indent_off
= off
;
860 es
->indent_width
= width
;
864 static int compute_ws_delta(const struct emitted_diff_symbol
*a
,
865 const struct emitted_diff_symbol
*b
)
867 int a_width
= a
->indent_width
,
868 b_width
= b
->indent_width
;
870 if (a_width
== INDENT_BLANKLINE
&& b_width
== INDENT_BLANKLINE
)
871 return INDENT_BLANKLINE
;
873 return a_width
- b_width
;
876 static int cmp_in_block_with_wsd(const struct moved_entry
*cur
,
877 const struct emitted_diff_symbol
*l
,
878 struct moved_block
*pmb
)
880 int a_width
= cur
->es
->indent_width
, b_width
= l
->indent_width
;
883 /* The text of each line must match */
884 if (cur
->es
->id
!= l
->id
)
888 * If 'l' and 'cur' are both blank then we don't need to check the
889 * indent. We only need to check cur as we know the strings match.
891 if (a_width
== INDENT_BLANKLINE
)
895 * The indent changes of the block are known and stored in pmb->wsd;
896 * however we need to check if the indent changes of the current line
897 * match those of the current block.
899 delta
= b_width
- a_width
;
902 * If the previous lines of this block were all blank then set its
905 if (pmb
->wsd
== INDENT_BLANKLINE
)
908 return delta
!= pmb
->wsd
;
911 struct interned_diff_symbol
{
912 struct hashmap_entry ent
;
913 struct emitted_diff_symbol
*es
;
916 static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data
,
917 const struct hashmap_entry
*eptr
,
918 const struct hashmap_entry
*entry_or_key
,
921 const struct diff_options
*diffopt
= hashmap_cmp_fn_data
;
922 const struct emitted_diff_symbol
*a
, *b
;
923 unsigned flags
= diffopt
->color_moved_ws_handling
924 & XDF_WHITESPACE_FLAGS
;
926 a
= container_of(eptr
, const struct interned_diff_symbol
, ent
)->es
;
927 b
= container_of(entry_or_key
, const struct interned_diff_symbol
, ent
)->es
;
929 return !xdiff_compare_lines(a
->line
+ a
->indent_off
,
930 a
->len
- a
->indent_off
,
931 b
->line
+ b
->indent_off
,
932 b
->len
- b
->indent_off
, flags
);
935 static void prepare_entry(struct diff_options
*o
, struct emitted_diff_symbol
*l
,
936 struct interned_diff_symbol
*s
)
938 unsigned flags
= o
->color_moved_ws_handling
& XDF_WHITESPACE_FLAGS
;
939 unsigned int hash
= xdiff_hash_string(l
->line
+ l
->indent_off
,
940 l
->len
- l
->indent_off
, flags
);
942 hashmap_entry_init(&s
->ent
, hash
);
946 struct moved_entry_list
{
947 struct moved_entry
*add
, *del
;
950 static struct moved_entry_list
*add_lines_to_move_detection(struct diff_options
*o
,
951 struct mem_pool
*entry_mem_pool
)
953 struct moved_entry
*prev_line
= NULL
;
954 struct mem_pool interned_pool
;
955 struct hashmap interned_map
;
956 struct moved_entry_list
*entry_list
= NULL
;
957 size_t entry_list_alloc
= 0;
961 hashmap_init(&interned_map
, interned_diff_symbol_cmp
, o
, 8096);
962 mem_pool_init(&interned_pool
, 1024 * 1024);
964 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
965 struct interned_diff_symbol key
;
966 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
967 struct interned_diff_symbol
*s
;
968 struct moved_entry
*entry
;
970 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
) {
975 if (o
->color_moved_ws_handling
&
976 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
977 fill_es_indent_data(l
);
979 prepare_entry(o
, l
, &key
);
980 s
= hashmap_get_entry(&interned_map
, &key
, ent
, &key
.ent
);
985 ALLOC_GROW_BY(entry_list
, id
, 1, entry_list_alloc
);
986 hashmap_add(&interned_map
,
987 memcpy(mem_pool_alloc(&interned_pool
,
991 entry
= mem_pool_alloc(entry_mem_pool
, sizeof(*entry
));
993 entry
->next_line
= NULL
;
994 if (prev_line
&& prev_line
->es
->s
== l
->s
)
995 prev_line
->next_line
= entry
;
997 if (l
->s
== DIFF_SYMBOL_PLUS
) {
998 entry
->next_match
= entry_list
[l
->id
].add
;
999 entry_list
[l
->id
].add
= entry
;
1001 entry
->next_match
= entry_list
[l
->id
].del
;
1002 entry_list
[l
->id
].del
= entry
;
1006 hashmap_clear(&interned_map
);
1007 mem_pool_discard(&interned_pool
, 0);
1012 static void pmb_advance_or_null(struct diff_options
*o
,
1013 struct emitted_diff_symbol
*l
,
1014 struct moved_block
*pmb
,
1019 for (i
= 0, j
= 0; i
< *pmb_nr
; i
++) {
1021 struct moved_entry
*prev
= pmb
[i
].match
;
1022 struct moved_entry
*cur
= (prev
&& prev
->next_line
) ?
1023 prev
->next_line
: NULL
;
1025 if (o
->color_moved_ws_handling
&
1026 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1028 !cmp_in_block_with_wsd(cur
, l
, &pmb
[i
]);
1030 match
= cur
&& cur
->es
->id
== l
->id
;
1034 pmb
[j
++].match
= cur
;
1040 static void fill_potential_moved_blocks(struct diff_options
*o
,
1041 struct moved_entry
*match
,
1042 struct emitted_diff_symbol
*l
,
1043 struct moved_block
**pmb_p
,
1044 int *pmb_alloc_p
, int *pmb_nr_p
)
1047 struct moved_block
*pmb
= *pmb_p
;
1048 int pmb_alloc
= *pmb_alloc_p
, pmb_nr
= *pmb_nr_p
;
1051 * The current line is the start of a new block.
1052 * Setup the set of potential blocks.
1054 for (; match
; match
= match
->next_match
) {
1055 ALLOC_GROW(pmb
, pmb_nr
+ 1, pmb_alloc
);
1056 if (o
->color_moved_ws_handling
&
1057 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1058 pmb
[pmb_nr
].wsd
= compute_ws_delta(l
, match
->es
);
1060 pmb
[pmb_nr
].wsd
= 0;
1061 pmb
[pmb_nr
++].match
= match
;
1065 *pmb_alloc_p
= pmb_alloc
;
1070 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1072 * Otherwise, if the last block has fewer alphanumeric characters than
1073 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
1076 * The last block consists of the (n - block_length)'th line up to but not
1077 * including the nth line.
1079 * Returns 0 if the last block is empty or is unset by this function, non zero
1082 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1083 * Think of a way to unify them.
1085 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1086 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
1087 static int adjust_last_block(struct diff_options
*o
, int n
, int block_length
)
1089 int i
, alnum_count
= 0;
1090 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
1091 return block_length
;
1092 for (i
= 1; i
< block_length
+ 1; i
++) {
1093 const char *c
= o
->emitted_symbols
->buf
[n
- i
].line
;
1098 if (alnum_count
>= COLOR_MOVED_MIN_ALNUM_COUNT
)
1102 for (i
= 1; i
< block_length
+ 1; i
++)
1103 o
->emitted_symbols
->buf
[n
- i
].flags
&= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
;
1107 /* Find blocks of moved code, delegate actual coloring decision to helper */
1108 static void mark_color_as_moved(struct diff_options
*o
,
1109 struct moved_entry_list
*entry_list
)
1111 struct moved_block
*pmb
= NULL
; /* potentially moved blocks */
1112 int pmb_nr
= 0, pmb_alloc
= 0;
1113 int n
, flipped_block
= 0, block_length
= 0;
1114 enum diff_symbol moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1117 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1118 struct moved_entry
*match
= NULL
;
1119 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1122 case DIFF_SYMBOL_PLUS
:
1123 match
= entry_list
[l
->id
].del
;
1125 case DIFF_SYMBOL_MINUS
:
1126 match
= entry_list
[l
->id
].add
;
1132 if (pmb_nr
&& (!match
|| l
->s
!= moved_symbol
)) {
1133 if (!adjust_last_block(o
, n
, block_length
) &&
1136 * Rewind in case there is another match
1137 * starting at the second line of the block
1147 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1151 if (o
->color_moved
== COLOR_MOVED_PLAIN
) {
1152 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1156 pmb_advance_or_null(o
, l
, pmb
, &pmb_nr
);
1159 int contiguous
= adjust_last_block(o
, n
, block_length
);
1161 if (!contiguous
&& block_length
> 1)
1163 * Rewind in case there is another match
1164 * starting at the second line of the block
1168 fill_potential_moved_blocks(o
, match
, l
,
1172 if (contiguous
&& pmb_nr
&& moved_symbol
== l
->s
)
1173 flipped_block
= (flipped_block
+ 1) % 2;
1178 moved_symbol
= l
->s
;
1180 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1187 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1188 if (flipped_block
&& o
->color_moved
!= COLOR_MOVED_BLOCKS
)
1189 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_ALT
;
1192 adjust_last_block(o
, n
, block_length
);
1197 static void dim_moved_lines(struct diff_options
*o
)
1200 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1201 struct emitted_diff_symbol
*prev
= (n
!= 0) ?
1202 &o
->emitted_symbols
->buf
[n
- 1] : NULL
;
1203 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1204 struct emitted_diff_symbol
*next
=
1205 (n
< o
->emitted_symbols
->nr
- 1) ?
1206 &o
->emitted_symbols
->buf
[n
+ 1] : NULL
;
1208 /* Not a plus or minus line? */
1209 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
)
1212 /* Not a moved line? */
1213 if (!(l
->flags
& DIFF_SYMBOL_MOVED_LINE
))
1217 * If prev or next are not a plus or minus line,
1218 * pretend they don't exist
1220 if (prev
&& prev
->s
!= DIFF_SYMBOL_PLUS
&&
1221 prev
->s
!= DIFF_SYMBOL_MINUS
)
1223 if (next
&& next
->s
!= DIFF_SYMBOL_PLUS
&&
1224 next
->s
!= DIFF_SYMBOL_MINUS
)
1227 /* Inside a block? */
1229 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1230 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
)) &&
1232 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1233 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
))) {
1234 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1238 /* Check if we are at an interesting bound: */
1239 if (prev
&& (prev
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1240 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1241 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1243 if (next
&& (next
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1244 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1245 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1249 * The boundary to prev and next are not interesting,
1250 * so this line is not interesting as a whole
1252 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1256 static void emit_line_ws_markup(struct diff_options
*o
,
1257 const char *set_sign
, const char *set
,
1259 int sign_index
, const char *line
, int len
,
1260 unsigned ws_rule
, int blank_at_eof
)
1262 const char *ws
= NULL
;
1263 int sign
= o
->output_indicators
[sign_index
];
1265 if (o
->ws_error_highlight
& ws_rule
) {
1266 ws
= diff_get_color_opt(o
, DIFF_WHITESPACE
);
1271 if (!ws
&& !set_sign
)
1272 emit_line_0(o
, set
, NULL
, 0, reset
, sign
, line
, len
);
1274 emit_line_0(o
, set_sign
, set
, !!set_sign
, reset
, sign
, line
, len
);
1275 } else if (blank_at_eof
)
1276 /* Blank line at EOF - paint '+' as well */
1277 emit_line_0(o
, ws
, NULL
, 0, reset
, sign
, line
, len
);
1279 /* Emit just the prefix, then the rest. */
1280 emit_line_0(o
, set_sign
? set_sign
: set
, NULL
, !!set_sign
, reset
,
1282 ws_check_emit(line
, len
, ws_rule
,
1283 o
->file
, set
, reset
, ws
);
1287 static void emit_diff_symbol_from_struct(struct diff_options
*o
,
1288 struct emitted_diff_symbol
*eds
)
1290 static const char *nneof
= " No newline at end of file\n";
1291 const char *context
, *reset
, *set
, *set_sign
, *meta
, *fraginfo
;
1293 enum diff_symbol s
= eds
->s
;
1294 const char *line
= eds
->line
;
1296 unsigned flags
= eds
->flags
;
1299 case DIFF_SYMBOL_NO_LF_EOF
:
1300 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1301 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1302 putc('\n', o
->file
);
1303 emit_line_0(o
, context
, NULL
, 0, reset
, '\\',
1304 nneof
, strlen(nneof
));
1306 case DIFF_SYMBOL_SUBMODULE_HEADER
:
1307 case DIFF_SYMBOL_SUBMODULE_ERROR
:
1308 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
:
1309 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
:
1310 case DIFF_SYMBOL_SUMMARY
:
1311 case DIFF_SYMBOL_STATS_LINE
:
1312 case DIFF_SYMBOL_BINARY_DIFF_BODY
:
1313 case DIFF_SYMBOL_CONTEXT_FRAGINFO
:
1314 emit_line(o
, "", "", line
, len
);
1316 case DIFF_SYMBOL_CONTEXT_INCOMPLETE
:
1317 case DIFF_SYMBOL_CONTEXT_MARKER
:
1318 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1319 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1320 emit_line(o
, context
, reset
, line
, len
);
1322 case DIFF_SYMBOL_SEPARATOR
:
1323 fprintf(o
->file
, "%s%c",
1324 diff_line_prefix(o
),
1325 o
->line_termination
);
1327 case DIFF_SYMBOL_CONTEXT
:
1328 set
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1329 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1331 if (o
->flags
.dual_color_diffed_diffs
) {
1332 char c
= !len
? 0 : line
[0];
1335 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1337 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1339 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1341 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1342 OUTPUT_INDICATOR_CONTEXT
, line
, len
,
1343 flags
& (DIFF_SYMBOL_CONTENT_WS_MASK
), 0);
1345 case DIFF_SYMBOL_PLUS
:
1346 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1347 DIFF_SYMBOL_MOVED_LINE_ALT
|
1348 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1349 case DIFF_SYMBOL_MOVED_LINE
|
1350 DIFF_SYMBOL_MOVED_LINE_ALT
|
1351 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1352 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT_DIM
);
1354 case DIFF_SYMBOL_MOVED_LINE
|
1355 DIFF_SYMBOL_MOVED_LINE_ALT
:
1356 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT
);
1358 case DIFF_SYMBOL_MOVED_LINE
|
1359 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1360 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_DIM
);
1362 case DIFF_SYMBOL_MOVED_LINE
:
1363 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED
);
1366 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1368 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1369 if (!o
->flags
.dual_color_diffed_diffs
)
1372 char c
= !len
? 0 : line
[0];
1376 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_BOLD
);
1378 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1380 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_BOLD
);
1382 set
= diff_get_color_opt(o
, DIFF_CONTEXT_BOLD
);
1383 flags
&= ~DIFF_SYMBOL_CONTENT_WS_MASK
;
1385 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1386 OUTPUT_INDICATOR_NEW
, line
, len
,
1387 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
,
1388 flags
& DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
);
1390 case DIFF_SYMBOL_MINUS
:
1391 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1392 DIFF_SYMBOL_MOVED_LINE_ALT
|
1393 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1394 case DIFF_SYMBOL_MOVED_LINE
|
1395 DIFF_SYMBOL_MOVED_LINE_ALT
|
1396 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1397 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT_DIM
);
1399 case DIFF_SYMBOL_MOVED_LINE
|
1400 DIFF_SYMBOL_MOVED_LINE_ALT
:
1401 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT
);
1403 case DIFF_SYMBOL_MOVED_LINE
|
1404 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1405 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_DIM
);
1407 case DIFF_SYMBOL_MOVED_LINE
:
1408 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED
);
1411 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1413 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1414 if (!o
->flags
.dual_color_diffed_diffs
)
1417 char c
= !len
? 0 : line
[0];
1421 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_DIM
);
1423 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1425 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_DIM
);
1427 set
= diff_get_color_opt(o
, DIFF_CONTEXT_DIM
);
1429 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1430 OUTPUT_INDICATOR_OLD
, line
, len
,
1431 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
, 0);
1433 case DIFF_SYMBOL_WORDS_PORCELAIN
:
1434 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1435 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1436 emit_line(o
, context
, reset
, line
, len
);
1437 fputs("~\n", o
->file
);
1439 case DIFF_SYMBOL_WORDS
:
1440 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1441 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1443 * Skip the prefix character, if any. With
1444 * diff_suppress_blank_empty, there may be
1447 if (line
[0] != '\n') {
1451 emit_line(o
, context
, reset
, line
, len
);
1453 case DIFF_SYMBOL_FILEPAIR_PLUS
:
1454 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1455 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1456 fprintf(o
->file
, "%s%s+++ %s%s%s\n", diff_line_prefix(o
), meta
,
1458 strchr(line
, ' ') ? "\t" : "");
1460 case DIFF_SYMBOL_FILEPAIR_MINUS
:
1461 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1462 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1463 fprintf(o
->file
, "%s%s--- %s%s%s\n", diff_line_prefix(o
), meta
,
1465 strchr(line
, ' ') ? "\t" : "");
1467 case DIFF_SYMBOL_BINARY_FILES
:
1468 case DIFF_SYMBOL_HEADER
:
1469 fprintf(o
->file
, "%s", line
);
1471 case DIFF_SYMBOL_BINARY_DIFF_HEADER
:
1472 fprintf(o
->file
, "%sGIT binary patch\n", diff_line_prefix(o
));
1474 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
:
1475 fprintf(o
->file
, "%sdelta %s\n", diff_line_prefix(o
), line
);
1477 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
:
1478 fprintf(o
->file
, "%sliteral %s\n", diff_line_prefix(o
), line
);
1480 case DIFF_SYMBOL_BINARY_DIFF_FOOTER
:
1481 fputs(diff_line_prefix(o
), o
->file
);
1482 fputc('\n', o
->file
);
1484 case DIFF_SYMBOL_REWRITE_DIFF
:
1485 fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
1486 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1487 emit_line(o
, fraginfo
, reset
, line
, len
);
1489 case DIFF_SYMBOL_SUBMODULE_ADD
:
1490 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1491 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1492 emit_line(o
, set
, reset
, line
, len
);
1494 case DIFF_SYMBOL_SUBMODULE_DEL
:
1495 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1496 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1497 emit_line(o
, set
, reset
, line
, len
);
1499 case DIFF_SYMBOL_SUBMODULE_UNTRACKED
:
1500 fprintf(o
->file
, "%sSubmodule %s contains untracked content\n",
1501 diff_line_prefix(o
), line
);
1503 case DIFF_SYMBOL_SUBMODULE_MODIFIED
:
1504 fprintf(o
->file
, "%sSubmodule %s contains modified content\n",
1505 diff_line_prefix(o
), line
);
1507 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
:
1508 emit_line(o
, "", "", " 0 files changed\n",
1509 strlen(" 0 files changed\n"));
1511 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV
:
1512 emit_line(o
, "", "", " ...\n", strlen(" ...\n"));
1514 case DIFF_SYMBOL_WORD_DIFF
:
1515 fprintf(o
->file
, "%.*s", len
, line
);
1517 case DIFF_SYMBOL_STAT_SEP
:
1518 fputs(o
->stat_sep
, o
->file
);
1521 BUG("unknown diff symbol");
1525 static void emit_diff_symbol(struct diff_options
*o
, enum diff_symbol s
,
1526 const char *line
, int len
, unsigned flags
)
1528 struct emitted_diff_symbol e
= {
1529 .line
= line
, .len
= len
, .flags
= flags
, .s
= s
1532 if (o
->emitted_symbols
)
1533 append_emitted_diff_symbol(o
, &e
);
1535 emit_diff_symbol_from_struct(o
, &e
);
1538 void diff_emit_submodule_del(struct diff_options
*o
, const char *line
)
1540 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_DEL
, line
, strlen(line
), 0);
1543 void diff_emit_submodule_add(struct diff_options
*o
, const char *line
)
1545 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ADD
, line
, strlen(line
), 0);
1548 void diff_emit_submodule_untracked(struct diff_options
*o
, const char *path
)
1550 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
1551 path
, strlen(path
), 0);
1554 void diff_emit_submodule_modified(struct diff_options
*o
, const char *path
)
1556 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_MODIFIED
,
1557 path
, strlen(path
), 0);
1560 void diff_emit_submodule_header(struct diff_options
*o
, const char *header
)
1562 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_HEADER
,
1563 header
, strlen(header
), 0);
1566 void diff_emit_submodule_error(struct diff_options
*o
, const char *err
)
1568 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ERROR
, err
, strlen(err
), 0);
1571 void diff_emit_submodule_pipethrough(struct diff_options
*o
,
1572 const char *line
, int len
)
1574 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
, line
, len
, 0);
1577 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
1579 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
1580 ecbdata
->blank_at_eof_in_preimage
&&
1581 ecbdata
->blank_at_eof_in_postimage
&&
1582 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
1583 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
1585 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
1588 static void emit_add_line(struct emit_callback
*ecbdata
,
1589 const char *line
, int len
)
1591 unsigned flags
= WSEH_NEW
| ecbdata
->ws_rule
;
1592 if (new_blank_line_at_eof(ecbdata
, line
, len
))
1593 flags
|= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
;
1595 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_PLUS
, line
, len
, flags
);
1598 static void emit_del_line(struct emit_callback
*ecbdata
,
1599 const char *line
, int len
)
1601 unsigned flags
= WSEH_OLD
| ecbdata
->ws_rule
;
1602 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_MINUS
, line
, len
, flags
);
1605 static void emit_context_line(struct emit_callback
*ecbdata
,
1606 const char *line
, int len
)
1608 unsigned flags
= WSEH_CONTEXT
| ecbdata
->ws_rule
;
1609 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_CONTEXT
, line
, len
, flags
);
1612 static void emit_hunk_header(struct emit_callback
*ecbdata
,
1613 const char *line
, int len
)
1615 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1616 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
1617 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
1618 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1619 const char *reverse
= ecbdata
->color_diff
? GIT_COLOR_REVERSE
: "";
1620 static const char atat
[2] = { '@', '@' };
1621 const char *cp
, *ep
;
1622 struct strbuf msgbuf
= STRBUF_INIT
;
1627 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1628 * it always is at least 10 bytes long.
1631 memcmp(line
, atat
, 2) ||
1632 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
1633 emit_diff_symbol(ecbdata
->opt
,
1634 DIFF_SYMBOL_CONTEXT_MARKER
, line
, len
, 0);
1637 ep
+= 2; /* skip over @@ */
1639 /* The hunk header in fraginfo color */
1640 if (ecbdata
->opt
->flags
.dual_color_diffed_diffs
)
1641 strbuf_addstr(&msgbuf
, reverse
);
1642 strbuf_addstr(&msgbuf
, frag
);
1643 if (ecbdata
->opt
->flags
.suppress_hunk_header_line_count
)
1644 strbuf_add(&msgbuf
, atat
, sizeof(atat
));
1646 strbuf_add(&msgbuf
, line
, ep
- line
);
1647 strbuf_addstr(&msgbuf
, reset
);
1653 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
1656 /* blank before the func header */
1657 for (cp
= ep
; ep
- line
< len
; ep
++)
1658 if (*ep
!= ' ' && *ep
!= '\t')
1661 strbuf_addstr(&msgbuf
, context
);
1662 strbuf_add(&msgbuf
, cp
, ep
- cp
);
1663 strbuf_addstr(&msgbuf
, reset
);
1666 if (ep
< line
+ len
) {
1667 strbuf_addstr(&msgbuf
, func
);
1668 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
1669 strbuf_addstr(&msgbuf
, reset
);
1672 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
1673 strbuf_complete_line(&msgbuf
);
1674 emit_diff_symbol(ecbdata
->opt
,
1675 DIFF_SYMBOL_CONTEXT_FRAGINFO
, msgbuf
.buf
, msgbuf
.len
, 0);
1676 strbuf_release(&msgbuf
);
1679 static struct diff_tempfile
*claim_diff_tempfile(void)
1682 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
1683 if (!diff_temp
[i
].name
)
1684 return diff_temp
+ i
;
1685 BUG("diff is failing to clean up its tempfiles");
1688 static void remove_tempfile(void)
1691 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
1692 if (is_tempfile_active(diff_temp
[i
].tempfile
))
1693 delete_tempfile(&diff_temp
[i
].tempfile
);
1694 diff_temp
[i
].name
= NULL
;
1698 static void add_line_count(struct strbuf
*out
, int count
)
1702 strbuf_addstr(out
, "0,0");
1705 strbuf_addstr(out
, "1");
1708 strbuf_addf(out
, "1,%d", count
);
1713 static void emit_rewrite_lines(struct emit_callback
*ecb
,
1714 int prefix
, const char *data
, int size
)
1716 const char *endp
= NULL
;
1721 endp
= memchr(data
, '\n', size
);
1722 len
= endp
? (endp
- data
+ 1) : size
;
1723 if (prefix
!= '+') {
1724 ecb
->lno_in_preimage
++;
1725 emit_del_line(ecb
, data
, len
);
1727 ecb
->lno_in_postimage
++;
1728 emit_add_line(ecb
, data
, len
);
1734 emit_diff_symbol(ecb
->opt
, DIFF_SYMBOL_NO_LF_EOF
, NULL
, 0, 0);
1737 static void emit_rewrite_diff(const char *name_a
,
1739 struct diff_filespec
*one
,
1740 struct diff_filespec
*two
,
1741 struct userdiff_driver
*textconv_one
,
1742 struct userdiff_driver
*textconv_two
,
1743 struct diff_options
*o
)
1746 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
1747 const char *a_prefix
, *b_prefix
;
1748 char *data_one
, *data_two
;
1749 size_t size_one
, size_two
;
1750 struct emit_callback ecbdata
;
1751 struct strbuf out
= STRBUF_INIT
;
1753 if (diff_mnemonic_prefix
&& o
->flags
.reverse_diff
) {
1754 a_prefix
= o
->b_prefix
;
1755 b_prefix
= o
->a_prefix
;
1757 a_prefix
= o
->a_prefix
;
1758 b_prefix
= o
->b_prefix
;
1761 name_a
+= (*name_a
== '/');
1762 name_b
+= (*name_b
== '/');
1764 strbuf_reset(&a_name
);
1765 strbuf_reset(&b_name
);
1766 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
1767 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
1769 size_one
= fill_textconv(o
->repo
, textconv_one
, one
, &data_one
);
1770 size_two
= fill_textconv(o
->repo
, textconv_two
, two
, &data_two
);
1772 memset(&ecbdata
, 0, sizeof(ecbdata
));
1773 ecbdata
.color_diff
= want_color(o
->use_color
);
1774 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
1776 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
1778 mf1
.ptr
= (char *)data_one
;
1779 mf2
.ptr
= (char *)data_two
;
1780 mf1
.size
= size_one
;
1781 mf2
.size
= size_two
;
1782 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1784 ecbdata
.lno_in_preimage
= 1;
1785 ecbdata
.lno_in_postimage
= 1;
1787 lc_a
= count_lines(data_one
, size_one
);
1788 lc_b
= count_lines(data_two
, size_two
);
1790 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1791 a_name
.buf
, a_name
.len
, 0);
1792 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1793 b_name
.buf
, b_name
.len
, 0);
1795 strbuf_addstr(&out
, "@@ -");
1796 if (!o
->irreversible_delete
)
1797 add_line_count(&out
, lc_a
);
1799 strbuf_addstr(&out
, "?,?");
1800 strbuf_addstr(&out
, " +");
1801 add_line_count(&out
, lc_b
);
1802 strbuf_addstr(&out
, " @@\n");
1803 emit_diff_symbol(o
, DIFF_SYMBOL_REWRITE_DIFF
, out
.buf
, out
.len
, 0);
1804 strbuf_release(&out
);
1806 if (lc_a
&& !o
->irreversible_delete
)
1807 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
1809 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
1811 free((char *)data_one
);
1813 free((char *)data_two
);
1816 struct diff_words_buffer
{
1818 unsigned long alloc
;
1819 struct diff_words_orig
{
1820 const char *begin
, *end
;
1822 int orig_nr
, orig_alloc
;
1825 static void diff_words_append(char *line
, unsigned long len
,
1826 struct diff_words_buffer
*buffer
)
1828 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
1831 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
1832 buffer
->text
.size
+= len
;
1833 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
1836 struct diff_words_style_elem
{
1839 const char *color
; /* NULL; filled in by the setup code if
1840 * color is enabled */
1843 struct diff_words_style
{
1844 enum diff_words_type type
;
1845 struct diff_words_style_elem new_word
, old_word
, ctx
;
1846 const char *newline
;
1849 static struct diff_words_style diff_words_styles
[] = {
1850 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1851 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1852 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
1855 struct diff_words_data
{
1856 struct diff_words_buffer minus
, plus
;
1857 const char *current_plus
;
1859 struct diff_options
*opt
;
1860 regex_t
*word_regex
;
1861 enum diff_words_type type
;
1862 struct diff_words_style
*style
;
1865 static int fn_out_diff_words_write_helper(struct diff_options
*o
,
1866 struct diff_words_style_elem
*st_el
,
1867 const char *newline
,
1868 size_t count
, const char *buf
)
1871 struct strbuf sb
= STRBUF_INIT
;
1874 char *p
= memchr(buf
, '\n', count
);
1876 strbuf_addstr(&sb
, diff_line_prefix(o
));
1879 const char *reset
= st_el
->color
&& *st_el
->color
?
1880 GIT_COLOR_RESET
: NULL
;
1881 if (st_el
->color
&& *st_el
->color
)
1882 strbuf_addstr(&sb
, st_el
->color
);
1883 strbuf_addstr(&sb
, st_el
->prefix
);
1884 strbuf_add(&sb
, buf
, p
? p
- buf
: count
);
1885 strbuf_addstr(&sb
, st_el
->suffix
);
1887 strbuf_addstr(&sb
, reset
);
1892 strbuf_addstr(&sb
, newline
);
1893 count
-= p
+ 1 - buf
;
1897 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1905 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1907 strbuf_release(&sb
);
1912 * '--color-words' algorithm can be described as:
1914 * 1. collect the minus/plus lines of a diff hunk, divided into
1915 * minus-lines and plus-lines;
1917 * 2. break both minus-lines and plus-lines into words and
1918 * place them into two mmfile_t with one word for each line;
1920 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1922 * And for the common parts of the both file, we output the plus side text.
1923 * diff_words->current_plus is used to trace the current position of the plus file
1924 * which printed. diff_words->last_minus is used to trace the last minus word
1927 * For '--graph' to work with '--color-words', we need to output the graph prefix
1928 * on each line of color words output. Generally, there are two conditions on
1929 * which we should output the prefix.
1931 * 1. diff_words->last_minus == 0 &&
1932 * diff_words->current_plus == diff_words->plus.text.ptr
1934 * that is: the plus text must start as a new line, and if there is no minus
1935 * word printed, a graph prefix must be printed.
1937 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1938 * *(diff_words->current_plus - 1) == '\n'
1940 * that is: a graph prefix must be printed following a '\n'
1942 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
1944 if ((diff_words
->last_minus
== 0 &&
1945 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
1946 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
1947 *(diff_words
->current_plus
- 1) == '\n')) {
1954 static void fn_out_diff_words_aux(void *priv
,
1955 long minus_first
, long minus_len
,
1956 long plus_first
, long plus_len
,
1957 const char *func
, long funclen
)
1959 struct diff_words_data
*diff_words
= priv
;
1960 struct diff_words_style
*style
= diff_words
->style
;
1961 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
1962 struct diff_options
*opt
= diff_words
->opt
;
1963 const char *line_prefix
;
1966 line_prefix
= diff_line_prefix(opt
);
1968 /* POSIX requires that first be decremented by one if len == 0... */
1970 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
1972 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
1974 minus_begin
= minus_end
=
1975 diff_words
->minus
.orig
[minus_first
].end
;
1978 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
1979 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
1981 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
1983 if (color_words_output_graph_prefix(diff_words
)) {
1984 fputs(line_prefix
, diff_words
->opt
->file
);
1986 if (diff_words
->current_plus
!= plus_begin
) {
1987 fn_out_diff_words_write_helper(diff_words
->opt
,
1988 &style
->ctx
, style
->newline
,
1989 plus_begin
- diff_words
->current_plus
,
1990 diff_words
->current_plus
);
1992 if (minus_begin
!= minus_end
) {
1993 fn_out_diff_words_write_helper(diff_words
->opt
,
1994 &style
->old_word
, style
->newline
,
1995 minus_end
- minus_begin
, minus_begin
);
1997 if (plus_begin
!= plus_end
) {
1998 fn_out_diff_words_write_helper(diff_words
->opt
,
1999 &style
->new_word
, style
->newline
,
2000 plus_end
- plus_begin
, plus_begin
);
2003 diff_words
->current_plus
= plus_end
;
2004 diff_words
->last_minus
= minus_first
;
2007 /* This function starts looking at *begin, and returns 0 iff a word was found. */
2008 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
2009 int *begin
, int *end
)
2011 while (word_regex
&& *begin
< buffer
->size
) {
2012 regmatch_t match
[1];
2013 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
2014 buffer
->size
- *begin
, 1, match
, 0)) {
2015 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
2016 '\n', match
[0].rm_eo
- match
[0].rm_so
);
2017 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
2018 *begin
+= match
[0].rm_so
;
2022 return *begin
> *end
;
2028 /* find the next word */
2029 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
2031 if (*begin
>= buffer
->size
)
2034 /* find the end of the word */
2036 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
2043 * This function splits the words in buffer->text, stores the list with
2044 * newline separator into out, and saves the offsets of the original words
2047 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
2048 regex_t
*word_regex
)
2056 /* fake an empty "0th" word */
2057 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
2058 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
2059 buffer
->orig_nr
= 1;
2061 for (i
= 0; i
< buffer
->text
.size
; i
++) {
2062 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
2065 /* store original boundaries */
2066 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
2067 buffer
->orig_alloc
);
2068 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
2069 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
2072 /* store one word */
2073 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
2074 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
2075 out
->ptr
[out
->size
+ j
- i
] = '\n';
2076 out
->size
+= j
- i
+ 1;
2082 /* this executes the word diff on the accumulated buffers */
2083 static void diff_words_show(struct diff_words_data
*diff_words
)
2087 mmfile_t minus
, plus
;
2088 struct diff_words_style
*style
= diff_words
->style
;
2090 struct diff_options
*opt
= diff_words
->opt
;
2091 const char *line_prefix
;
2094 line_prefix
= diff_line_prefix(opt
);
2096 /* special case: only removal */
2097 if (!diff_words
->plus
.text
.size
) {
2098 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2099 line_prefix
, strlen(line_prefix
), 0);
2100 fn_out_diff_words_write_helper(diff_words
->opt
,
2101 &style
->old_word
, style
->newline
,
2102 diff_words
->minus
.text
.size
,
2103 diff_words
->minus
.text
.ptr
);
2104 diff_words
->minus
.text
.size
= 0;
2108 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
2109 diff_words
->last_minus
= 0;
2111 memset(&xpp
, 0, sizeof(xpp
));
2112 memset(&xecfg
, 0, sizeof(xecfg
));
2113 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
2114 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
2116 /* as only the hunk header will be parsed, we need a 0-context */
2118 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, NULL
,
2119 diff_words
, &xpp
, &xecfg
))
2120 die("unable to generate word diff");
2123 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
2124 diff_words
->plus
.text
.size
) {
2125 if (color_words_output_graph_prefix(diff_words
))
2126 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2127 line_prefix
, strlen(line_prefix
), 0);
2128 fn_out_diff_words_write_helper(diff_words
->opt
,
2129 &style
->ctx
, style
->newline
,
2130 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
2131 - diff_words
->current_plus
, diff_words
->current_plus
);
2133 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
2136 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
2137 static void diff_words_flush(struct emit_callback
*ecbdata
)
2139 struct diff_options
*wo
= ecbdata
->diff_words
->opt
;
2141 if (ecbdata
->diff_words
->minus
.text
.size
||
2142 ecbdata
->diff_words
->plus
.text
.size
)
2143 diff_words_show(ecbdata
->diff_words
);
2145 if (wo
->emitted_symbols
) {
2146 struct diff_options
*o
= ecbdata
->opt
;
2147 struct emitted_diff_symbols
*wol
= wo
->emitted_symbols
;
2152 * Instead of appending each, concat all words to a line?
2154 for (i
= 0; i
< wol
->nr
; i
++)
2155 append_emitted_diff_symbol(o
, &wol
->buf
[i
]);
2157 for (i
= 0; i
< wol
->nr
; i
++)
2158 free((void *)wol
->buf
[i
].line
);
2164 static void diff_filespec_load_driver(struct diff_filespec
*one
,
2165 struct index_state
*istate
)
2167 /* Use already-loaded driver */
2171 if (S_ISREG(one
->mode
))
2172 one
->driver
= userdiff_find_by_path(istate
, one
->path
);
2174 /* Fallback to default settings */
2176 one
->driver
= userdiff_find_by_name("default");
2179 static const char *userdiff_word_regex(struct diff_filespec
*one
,
2180 struct index_state
*istate
)
2182 diff_filespec_load_driver(one
, istate
);
2183 return one
->driver
->word_regex
;
2186 static void init_diff_words_data(struct emit_callback
*ecbdata
,
2187 struct diff_options
*orig_opts
,
2188 struct diff_filespec
*one
,
2189 struct diff_filespec
*two
)
2192 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
2193 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
2195 CALLOC_ARRAY(ecbdata
->diff_words
, 1);
2196 ecbdata
->diff_words
->type
= o
->word_diff
;
2197 ecbdata
->diff_words
->opt
= o
;
2199 if (orig_opts
->emitted_symbols
)
2200 CALLOC_ARRAY(o
->emitted_symbols
, 1);
2203 o
->word_regex
= userdiff_word_regex(one
, o
->repo
->index
);
2205 o
->word_regex
= userdiff_word_regex(two
, o
->repo
->index
);
2207 o
->word_regex
= diff_word_regex_cfg
;
2208 if (o
->word_regex
) {
2209 ecbdata
->diff_words
->word_regex
= (regex_t
*)
2210 xmalloc(sizeof(regex_t
));
2211 if (regcomp(ecbdata
->diff_words
->word_regex
,
2213 REG_EXTENDED
| REG_NEWLINE
))
2214 die("invalid regular expression: %s",
2217 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
2218 if (o
->word_diff
== diff_words_styles
[i
].type
) {
2219 ecbdata
->diff_words
->style
=
2220 &diff_words_styles
[i
];
2224 if (want_color(o
->use_color
)) {
2225 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
2226 st
->old_word
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2227 st
->new_word
.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2228 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
2232 static void free_diff_words_data(struct emit_callback
*ecbdata
)
2234 if (ecbdata
->diff_words
) {
2235 diff_words_flush(ecbdata
);
2236 free_emitted_diff_symbols(ecbdata
->diff_words
->opt
->emitted_symbols
);
2237 free (ecbdata
->diff_words
->opt
);
2238 free (ecbdata
->diff_words
->minus
.text
.ptr
);
2239 free (ecbdata
->diff_words
->minus
.orig
);
2240 free (ecbdata
->diff_words
->plus
.text
.ptr
);
2241 free (ecbdata
->diff_words
->plus
.orig
);
2242 if (ecbdata
->diff_words
->word_regex
) {
2243 regfree(ecbdata
->diff_words
->word_regex
);
2244 free(ecbdata
->diff_words
->word_regex
);
2246 FREE_AND_NULL(ecbdata
->diff_words
);
2250 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
2252 if (want_color(diff_use_color
))
2253 return diff_colors
[ix
];
2257 const char *diff_line_prefix(struct diff_options
*opt
)
2259 struct strbuf
*msgbuf
;
2260 if (!opt
->output_prefix
)
2263 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
2267 static unsigned long sane_truncate_line(char *line
, unsigned long len
)
2270 unsigned long allot
;
2276 (void) utf8_width(&cp
, &l
);
2278 break; /* truncated in the middle? */
2283 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
2286 ecbdata
->lno_in_preimage
= 0;
2287 ecbdata
->lno_in_postimage
= 0;
2288 p
= strchr(line
, '-');
2290 return; /* cannot happen */
2291 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
2294 return; /* cannot happen */
2295 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
2298 static int fn_out_consume(void *priv
, char *line
, unsigned long len
)
2300 struct emit_callback
*ecbdata
= priv
;
2301 struct diff_options
*o
= ecbdata
->opt
;
2303 o
->found_changes
= 1;
2305 if (ecbdata
->header
) {
2306 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
2307 ecbdata
->header
->buf
, ecbdata
->header
->len
, 0);
2308 strbuf_reset(ecbdata
->header
);
2309 ecbdata
->header
= NULL
;
2312 if (ecbdata
->label_path
[0]) {
2313 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
2314 ecbdata
->label_path
[0],
2315 strlen(ecbdata
->label_path
[0]), 0);
2316 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
2317 ecbdata
->label_path
[1],
2318 strlen(ecbdata
->label_path
[1]), 0);
2319 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
2322 if (diff_suppress_blank_empty
2323 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
2328 if (line
[0] == '@') {
2329 if (ecbdata
->diff_words
)
2330 diff_words_flush(ecbdata
);
2331 len
= sane_truncate_line(line
, len
);
2332 find_lno(line
, ecbdata
);
2333 emit_hunk_header(ecbdata
, line
, len
);
2337 if (ecbdata
->diff_words
) {
2338 enum diff_symbol s
=
2339 ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
?
2340 DIFF_SYMBOL_WORDS_PORCELAIN
: DIFF_SYMBOL_WORDS
;
2341 if (line
[0] == '-') {
2342 diff_words_append(line
, len
,
2343 &ecbdata
->diff_words
->minus
);
2345 } else if (line
[0] == '+') {
2346 diff_words_append(line
, len
,
2347 &ecbdata
->diff_words
->plus
);
2349 } else if (starts_with(line
, "\\ ")) {
2351 * Eat the "no newline at eof" marker as if we
2352 * saw a "+" or "-" line with nothing on it,
2353 * and return without diff_words_flush() to
2354 * defer processing. If this is the end of
2355 * preimage, more "+" lines may come after it.
2359 diff_words_flush(ecbdata
);
2360 emit_diff_symbol(o
, s
, line
, len
, 0);
2366 ecbdata
->lno_in_postimage
++;
2367 emit_add_line(ecbdata
, line
+ 1, len
- 1);
2370 ecbdata
->lno_in_preimage
++;
2371 emit_del_line(ecbdata
, line
+ 1, len
- 1);
2374 ecbdata
->lno_in_postimage
++;
2375 ecbdata
->lno_in_preimage
++;
2376 emit_context_line(ecbdata
, line
+ 1, len
- 1);
2379 /* incomplete line at the end */
2380 ecbdata
->lno_in_preimage
++;
2381 emit_diff_symbol(o
, DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
2388 static void pprint_rename(struct strbuf
*name
, const char *a
, const char *b
)
2390 const char *old_name
= a
;
2391 const char *new_name
= b
;
2392 int pfx_length
, sfx_length
;
2393 int pfx_adjust_for_slash
;
2394 int len_a
= strlen(a
);
2395 int len_b
= strlen(b
);
2396 int a_midlen
, b_midlen
;
2397 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
2398 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
2400 if (qlen_a
|| qlen_b
) {
2401 quote_c_style(a
, name
, NULL
, 0);
2402 strbuf_addstr(name
, " => ");
2403 quote_c_style(b
, name
, NULL
, 0);
2407 /* Find common prefix */
2409 while (*old_name
&& *new_name
&& *old_name
== *new_name
) {
2410 if (*old_name
== '/')
2411 pfx_length
= old_name
- a
+ 1;
2416 /* Find common suffix */
2417 old_name
= a
+ len_a
;
2418 new_name
= b
+ len_b
;
2421 * If there is a common prefix, it must end in a slash. In
2422 * that case we let this loop run 1 into the prefix to see the
2425 * If there is no common prefix, we cannot do this as it would
2426 * underrun the input strings.
2428 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
2429 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old_name
&&
2430 b
+ pfx_length
- pfx_adjust_for_slash
<= new_name
&&
2431 *old_name
== *new_name
) {
2432 if (*old_name
== '/')
2433 sfx_length
= len_a
- (old_name
- a
);
2439 * pfx{mid-a => mid-b}sfx
2440 * {pfx-a => pfx-b}sfx
2441 * pfx{sfx-a => sfx-b}
2444 a_midlen
= len_a
- pfx_length
- sfx_length
;
2445 b_midlen
= len_b
- pfx_length
- sfx_length
;
2451 strbuf_grow(name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
2452 if (pfx_length
+ sfx_length
) {
2453 strbuf_add(name
, a
, pfx_length
);
2454 strbuf_addch(name
, '{');
2456 strbuf_add(name
, a
+ pfx_length
, a_midlen
);
2457 strbuf_addstr(name
, " => ");
2458 strbuf_add(name
, b
+ pfx_length
, b_midlen
);
2459 if (pfx_length
+ sfx_length
) {
2460 strbuf_addch(name
, '}');
2461 strbuf_add(name
, a
+ len_a
- sfx_length
, sfx_length
);
2465 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
2469 struct diffstat_file
*x
;
2471 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
2472 diffstat
->files
[diffstat
->nr
++] = x
;
2474 x
->from_name
= xstrdup(name_a
);
2475 x
->name
= xstrdup(name_b
);
2479 x
->from_name
= NULL
;
2480 x
->name
= xstrdup(name_a
);
2485 static int diffstat_consume(void *priv
, char *line
, unsigned long len
)
2487 struct diffstat_t
*diffstat
= priv
;
2488 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
2492 else if (line
[0] == '-')
2497 const char mime_boundary_leader
[] = "------------";
2499 static int scale_linear(int it
, int width
, int max_change
)
2504 * make sure that at least one '-' or '+' is printed if
2505 * there is any change to this path. The easiest way is to
2506 * scale linearly as if the allotted width is one column shorter
2507 * than it is, and then add 1 to the result.
2509 return 1 + (it
* (width
- 1) / max_change
);
2512 static void show_graph(struct strbuf
*out
, char ch
, int cnt
,
2513 const char *set
, const char *reset
)
2517 strbuf_addstr(out
, set
);
2518 strbuf_addchars(out
, ch
, cnt
);
2519 strbuf_addstr(out
, reset
);
2522 static void fill_print_name(struct diffstat_file
*file
)
2524 struct strbuf pname
= STRBUF_INIT
;
2526 if (file
->print_name
)
2529 if (file
->is_renamed
)
2530 pprint_rename(&pname
, file
->from_name
, file
->name
);
2532 quote_c_style(file
->name
, &pname
, NULL
, 0);
2535 strbuf_addf(&pname
, " (%s)", file
->comments
);
2537 file
->print_name
= strbuf_detach(&pname
, NULL
);
2540 static void print_stat_summary_inserts_deletes(struct diff_options
*options
,
2541 int files
, int insertions
, int deletions
)
2543 struct strbuf sb
= STRBUF_INIT
;
2546 assert(insertions
== 0 && deletions
== 0);
2547 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
2553 (files
== 1) ? " %d file changed" : " %d files changed",
2557 * For binary diff, the caller may want to print "x files
2558 * changed" with insertions == 0 && deletions == 0.
2560 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2561 * is probably less confusing (i.e skip over "2 files changed
2562 * but nothing about added/removed lines? Is this a bug in Git?").
2564 if (insertions
|| deletions
== 0) {
2566 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2570 if (deletions
|| insertions
== 0) {
2572 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2575 strbuf_addch(&sb
, '\n');
2576 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
2578 strbuf_release(&sb
);
2581 void print_stat_summary(FILE *fp
, int files
,
2582 int insertions
, int deletions
)
2584 struct diff_options o
;
2585 memset(&o
, 0, sizeof(o
));
2588 print_stat_summary_inserts_deletes(&o
, files
, insertions
, deletions
);
2591 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
2593 int i
, len
, add
, del
, adds
= 0, dels
= 0;
2594 uintmax_t max_change
= 0, max_len
= 0;
2595 int total_files
= data
->nr
, count
;
2596 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
2597 const char *reset
, *add_c
, *del_c
;
2598 int extra_shown
= 0;
2599 const char *line_prefix
= diff_line_prefix(options
);
2600 struct strbuf out
= STRBUF_INIT
;
2605 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
2607 reset
= diff_get_color_opt(options
, DIFF_RESET
);
2608 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
2609 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
2612 * Find the longest filename and max number of changes
2614 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
2615 struct diffstat_file
*file
= data
->files
[i
];
2616 uintmax_t change
= file
->added
+ file
->deleted
;
2618 if (!file
->is_interesting
&& (change
== 0)) {
2619 count
++; /* not shown == room for one more */
2622 fill_print_name(file
);
2623 len
= strlen(file
->print_name
);
2627 if (file
->is_unmerged
) {
2628 /* "Unmerged" is 8 characters */
2629 bin_width
= bin_width
< 8 ? 8 : bin_width
;
2632 if (file
->is_binary
) {
2633 /* "Bin XXX -> YYY bytes" */
2634 int w
= 14 + decimal_width(file
->added
)
2635 + decimal_width(file
->deleted
);
2636 bin_width
= bin_width
< w
? w
: bin_width
;
2637 /* Display change counts aligned with "Bin" */
2642 if (max_change
< change
)
2643 max_change
= change
;
2645 count
= i
; /* where we can stop scanning in data->files[] */
2648 * We have width = stat_width or term_columns() columns total.
2649 * We want a maximum of min(max_len, stat_name_width) for the name part.
2650 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2651 * We also need 1 for " " and 4 + decimal_width(max_change)
2652 * for " | NNNN " and one the empty column at the end, altogether
2653 * 6 + decimal_width(max_change).
2655 * If there's not enough space, we will use the smaller of
2656 * stat_name_width (if set) and 5/8*width for the filename,
2657 * and the rest for constant elements + graph part, but no more
2658 * than stat_graph_width for the graph part.
2659 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2660 * for the standard terminal size).
2662 * In other words: stat_width limits the maximum width, and
2663 * stat_name_width fixes the maximum width of the filename,
2664 * and is also used to divide available columns if there
2667 * Binary files are displayed with "Bin XXX -> YYY bytes"
2668 * instead of the change count and graph. This part is treated
2669 * similarly to the graph part, except that it is not
2670 * "scaled". If total width is too small to accommodate the
2671 * guaranteed minimum width of the filename part and the
2672 * separators and this message, this message will "overflow"
2673 * making the line longer than the maximum width.
2676 if (options
->stat_width
== -1)
2677 width
= term_columns() - strlen(line_prefix
);
2679 width
= options
->stat_width
? options
->stat_width
: 80;
2680 number_width
= decimal_width(max_change
) > number_width
?
2681 decimal_width(max_change
) : number_width
;
2683 if (options
->stat_graph_width
== -1)
2684 options
->stat_graph_width
= diff_stat_graph_width
;
2687 * Guarantee 3/8*16==6 for the graph part
2688 * and 5/8*16==10 for the filename part
2690 if (width
< 16 + 6 + number_width
)
2691 width
= 16 + 6 + number_width
;
2694 * First assign sizes that are wanted, ignoring available width.
2695 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2696 * starting from "XXX" should fit in graph_width.
2698 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
2699 if (options
->stat_graph_width
&&
2700 options
->stat_graph_width
< graph_width
)
2701 graph_width
= options
->stat_graph_width
;
2703 name_width
= (options
->stat_name_width
> 0 &&
2704 options
->stat_name_width
< max_len
) ?
2705 options
->stat_name_width
: max_len
;
2708 * Adjust adjustable widths not to exceed maximum width
2710 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
2711 if (graph_width
> width
* 3/8 - number_width
- 6) {
2712 graph_width
= width
* 3/8 - number_width
- 6;
2713 if (graph_width
< 6)
2717 if (options
->stat_graph_width
&&
2718 graph_width
> options
->stat_graph_width
)
2719 graph_width
= options
->stat_graph_width
;
2720 if (name_width
> width
- number_width
- 6 - graph_width
)
2721 name_width
= width
- number_width
- 6 - graph_width
;
2723 graph_width
= width
- number_width
- 6 - name_width
;
2727 * From here name_width is the width of the name area,
2728 * and graph_width is the width of the graph area.
2729 * max_change is used to scale graph properly.
2731 for (i
= 0; i
< count
; i
++) {
2732 const char *prefix
= "";
2733 struct diffstat_file
*file
= data
->files
[i
];
2734 char *name
= file
->print_name
;
2735 uintmax_t added
= file
->added
;
2736 uintmax_t deleted
= file
->deleted
;
2739 if (!file
->is_interesting
&& (added
+ deleted
== 0))
2743 * "scale" the filename
2746 name_len
= strlen(name
);
2747 if (name_width
< name_len
) {
2751 name
+= name_len
- len
;
2752 slash
= strchr(name
, '/');
2757 if (file
->is_binary
) {
2758 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2759 strbuf_addf(&out
, " %*s", number_width
, "Bin");
2760 if (!added
&& !deleted
) {
2761 strbuf_addch(&out
, '\n');
2762 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2763 out
.buf
, out
.len
, 0);
2767 strbuf_addf(&out
, " %s%"PRIuMAX
"%s",
2768 del_c
, deleted
, reset
);
2769 strbuf_addstr(&out
, " -> ");
2770 strbuf_addf(&out
, "%s%"PRIuMAX
"%s",
2771 add_c
, added
, reset
);
2772 strbuf_addstr(&out
, " bytes\n");
2773 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2774 out
.buf
, out
.len
, 0);
2778 else if (file
->is_unmerged
) {
2779 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2780 strbuf_addstr(&out
, " Unmerged\n");
2781 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2782 out
.buf
, out
.len
, 0);
2788 * scale the add/delete
2793 if (graph_width
<= max_change
) {
2794 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
2795 if (total
< 2 && add
&& del
)
2796 /* width >= 2 due to the sanity check */
2799 add
= scale_linear(add
, graph_width
, max_change
);
2802 del
= scale_linear(del
, graph_width
, max_change
);
2806 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2807 strbuf_addf(&out
, " %*"PRIuMAX
"%s",
2808 number_width
, added
+ deleted
,
2809 added
+ deleted
? " " : "");
2810 show_graph(&out
, '+', add
, add_c
, reset
);
2811 show_graph(&out
, '-', del
, del_c
, reset
);
2812 strbuf_addch(&out
, '\n');
2813 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2814 out
.buf
, out
.len
, 0);
2818 for (i
= 0; i
< data
->nr
; i
++) {
2819 struct diffstat_file
*file
= data
->files
[i
];
2820 uintmax_t added
= file
->added
;
2821 uintmax_t deleted
= file
->deleted
;
2823 if (file
->is_unmerged
||
2824 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
2829 if (!file
->is_binary
) {
2836 emit_diff_symbol(options
,
2837 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
2842 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2843 strbuf_release(&out
);
2846 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
2848 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
2853 for (i
= 0; i
< data
->nr
; i
++) {
2854 int added
= data
->files
[i
]->added
;
2855 int deleted
= data
->files
[i
]->deleted
;
2857 if (data
->files
[i
]->is_unmerged
||
2858 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
2860 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
2865 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2868 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
2875 for (i
= 0; i
< data
->nr
; i
++) {
2876 struct diffstat_file
*file
= data
->files
[i
];
2878 fprintf(options
->file
, "%s", diff_line_prefix(options
));
2880 if (file
->is_binary
)
2881 fprintf(options
->file
, "-\t-\t");
2883 fprintf(options
->file
,
2884 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
2885 file
->added
, file
->deleted
);
2886 if (options
->line_termination
) {
2887 fill_print_name(file
);
2888 if (!file
->is_renamed
)
2889 write_name_quoted(file
->name
, options
->file
,
2890 options
->line_termination
);
2892 fputs(file
->print_name
, options
->file
);
2893 putc(options
->line_termination
, options
->file
);
2896 if (file
->is_renamed
) {
2897 putc('\0', options
->file
);
2898 write_name_quoted(file
->from_name
, options
->file
, '\0');
2900 write_name_quoted(file
->name
, options
->file
, '\0');
2905 struct dirstat_file
{
2907 unsigned long changed
;
2910 struct dirstat_dir
{
2911 struct dirstat_file
*files
;
2912 int alloc
, nr
, permille
, cumulative
;
2915 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
2916 unsigned long changed
, const char *base
, int baselen
)
2918 unsigned long sum_changes
= 0;
2919 unsigned int sources
= 0;
2920 const char *line_prefix
= diff_line_prefix(opt
);
2923 struct dirstat_file
*f
= dir
->files
;
2924 int namelen
= strlen(f
->name
);
2925 unsigned long changes
;
2928 if (namelen
< baselen
)
2930 if (memcmp(f
->name
, base
, baselen
))
2932 slash
= strchr(f
->name
+ baselen
, '/');
2934 int newbaselen
= slash
+ 1 - f
->name
;
2935 changes
= gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
2938 changes
= f
->changed
;
2943 sum_changes
+= changes
;
2947 * We don't report dirstat's for
2949 * - or cases where everything came from a single directory
2950 * under this directory (sources == 1).
2952 if (baselen
&& sources
!= 1) {
2954 int permille
= sum_changes
* 1000 / changed
;
2955 if (permille
>= dir
->permille
) {
2956 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
2957 permille
/ 10, permille
% 10, baselen
, base
);
2958 if (!dir
->cumulative
)
2966 static int dirstat_compare(const void *_a
, const void *_b
)
2968 const struct dirstat_file
*a
= _a
;
2969 const struct dirstat_file
*b
= _b
;
2970 return strcmp(a
->name
, b
->name
);
2973 static void show_dirstat(struct diff_options
*options
)
2976 unsigned long changed
;
2977 struct dirstat_dir dir
;
2978 struct diff_queue_struct
*q
= &diff_queued_diff
;
2983 dir
.permille
= options
->dirstat_permille
;
2984 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
2987 for (i
= 0; i
< q
->nr
; i
++) {
2988 struct diff_filepair
*p
= q
->queue
[i
];
2990 unsigned long copied
, added
, damage
;
2991 struct diff_populate_filespec_options dpf_options
= {
2992 .check_size_only
= 1,
2995 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
2997 if (p
->one
->oid_valid
&& p
->two
->oid_valid
&&
2998 oideq(&p
->one
->oid
, &p
->two
->oid
)) {
3000 * The SHA1 has not changed, so pre-/post-content is
3001 * identical. We can therefore skip looking at the
3002 * file contents altogether.
3008 if (options
->flags
.dirstat_by_file
) {
3010 * In --dirstat-by-file mode, we don't really need to
3011 * look at the actual file contents at all.
3012 * The fact that the SHA1 changed is enough for us to
3013 * add this file to the list of results
3014 * (with each file contributing equal damage).
3020 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
3021 diff_populate_filespec(options
->repo
, p
->one
, NULL
);
3022 diff_populate_filespec(options
->repo
, p
->two
, NULL
);
3023 diffcore_count_changes(options
->repo
,
3024 p
->one
, p
->two
, NULL
, NULL
,
3026 diff_free_filespec_data(p
->one
);
3027 diff_free_filespec_data(p
->two
);
3028 } else if (DIFF_FILE_VALID(p
->one
)) {
3029 diff_populate_filespec(options
->repo
, p
->one
, &dpf_options
);
3031 diff_free_filespec_data(p
->one
);
3032 } else if (DIFF_FILE_VALID(p
->two
)) {
3033 diff_populate_filespec(options
->repo
, p
->two
, &dpf_options
);
3035 added
= p
->two
->size
;
3036 diff_free_filespec_data(p
->two
);
3041 * Original minus copied is the removed material,
3042 * added is the new material. They are both damages
3043 * made to the preimage.
3044 * If the resulting damage is zero, we know that
3045 * diffcore_count_changes() considers the two entries to
3046 * be identical, but since the oid changed, we
3047 * know that there must have been _some_ kind of change,
3048 * so we force all entries to have damage > 0.
3050 damage
= (p
->one
->size
- copied
) + added
;
3055 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3056 dir
.files
[dir
.nr
].name
= name
;
3057 dir
.files
[dir
.nr
].changed
= damage
;
3062 /* This can happen even with many files, if everything was renames */
3066 /* Show all directories with more than x% of the changes */
3067 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
3068 gather_dirstat(options
, &dir
, changed
, "", 0);
3071 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
3074 unsigned long changed
;
3075 struct dirstat_dir dir
;
3083 dir
.permille
= options
->dirstat_permille
;
3084 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3087 for (i
= 0; i
< data
->nr
; i
++) {
3088 struct diffstat_file
*file
= data
->files
[i
];
3089 unsigned long damage
= file
->added
+ file
->deleted
;
3090 if (file
->is_binary
)
3092 * binary files counts bytes, not lines. Must find some
3093 * way to normalize binary bytes vs. textual lines.
3094 * The following heuristic assumes that there are 64
3096 * This is stupid and ugly, but very cheap...
3098 damage
= DIV_ROUND_UP(damage
, 64);
3099 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3100 dir
.files
[dir
.nr
].name
= file
->name
;
3101 dir
.files
[dir
.nr
].changed
= damage
;
3106 /* This can happen even with many files, if everything was renames */
3110 /* Show all directories with more than x% of the changes */
3111 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
3112 gather_dirstat(options
, &dir
, changed
, "", 0);
3115 static void free_diffstat_file(struct diffstat_file
*f
)
3117 free(f
->print_name
);
3123 void free_diffstat_info(struct diffstat_t
*diffstat
)
3126 for (i
= 0; i
< diffstat
->nr
; i
++)
3127 free_diffstat_file(diffstat
->files
[i
]);
3128 free(diffstat
->files
);
3131 struct checkdiff_t
{
3132 const char *filename
;
3134 int conflict_marker_size
;
3135 struct diff_options
*o
;
3140 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
3145 if (len
< marker_size
+ 1)
3147 firstchar
= line
[0];
3148 switch (firstchar
) {
3149 case '=': case '>': case '<': case '|':
3154 for (cnt
= 1; cnt
< marker_size
; cnt
++)
3155 if (line
[cnt
] != firstchar
)
3157 /* line[1] through line[marker_size-1] are same as firstchar */
3158 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
3163 static void checkdiff_consume_hunk(void *priv
,
3164 long ob
, long on
, long nb
, long nn
,
3165 const char *func
, long funclen
)
3168 struct checkdiff_t
*data
= priv
;
3169 data
->lineno
= nb
- 1;
3172 static int checkdiff_consume(void *priv
, char *line
, unsigned long len
)
3174 struct checkdiff_t
*data
= priv
;
3175 int marker_size
= data
->conflict_marker_size
;
3176 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
3177 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
3178 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
3180 const char *line_prefix
;
3183 line_prefix
= diff_line_prefix(data
->o
);
3185 if (line
[0] == '+') {
3188 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
3190 fprintf(data
->o
->file
,
3191 "%s%s:%d: leftover conflict marker\n",
3192 line_prefix
, data
->filename
, data
->lineno
);
3194 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
3197 data
->status
|= bad
;
3198 err
= whitespace_error_string(bad
);
3199 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
3200 line_prefix
, data
->filename
, data
->lineno
, err
);
3202 emit_line(data
->o
, set
, reset
, line
, 1);
3203 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
3204 data
->o
->file
, set
, reset
, ws
);
3205 } else if (line
[0] == ' ') {
3211 static unsigned char *deflate_it(char *data
,
3213 unsigned long *result_size
)
3216 unsigned char *deflated
;
3219 git_deflate_init(&stream
, zlib_compression_level
);
3220 bound
= git_deflate_bound(&stream
, size
);
3221 deflated
= xmalloc(bound
);
3222 stream
.next_out
= deflated
;
3223 stream
.avail_out
= bound
;
3225 stream
.next_in
= (unsigned char *)data
;
3226 stream
.avail_in
= size
;
3227 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
3229 git_deflate_end(&stream
);
3230 *result_size
= stream
.total_out
;
3234 static void emit_binary_diff_body(struct diff_options
*o
,
3235 mmfile_t
*one
, mmfile_t
*two
)
3241 unsigned long orig_size
;
3242 unsigned long delta_size
;
3243 unsigned long deflate_size
;
3244 unsigned long data_size
;
3246 /* We could do deflated delta, or we could do just deflated two,
3247 * whichever is smaller.
3250 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
3251 if (one
->size
&& two
->size
) {
3252 delta
= diff_delta(one
->ptr
, one
->size
,
3253 two
->ptr
, two
->size
,
3254 &delta_size
, deflate_size
);
3256 void *to_free
= delta
;
3257 orig_size
= delta_size
;
3258 delta
= deflate_it(delta
, delta_size
, &delta_size
);
3263 if (delta
&& delta_size
< deflate_size
) {
3264 char *s
= xstrfmt("%"PRIuMAX
, (uintmax_t)orig_size
);
3265 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
3270 data_size
= delta_size
;
3272 char *s
= xstrfmt("%lu", two
->size
);
3273 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
3278 data_size
= deflate_size
;
3281 /* emit data encoded in base85 */
3285 int bytes
= (52 < data_size
) ? 52 : data_size
;
3289 line
[0] = bytes
+ 'A' - 1;
3291 line
[0] = bytes
- 26 + 'a' - 1;
3292 encode_85(line
+ 1, cp
, bytes
);
3293 cp
= (char *) cp
+ bytes
;
3299 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_BODY
,
3302 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_FOOTER
, NULL
, 0, 0);
3306 static void emit_binary_diff(struct diff_options
*o
,
3307 mmfile_t
*one
, mmfile_t
*two
)
3309 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER
, NULL
, 0, 0);
3310 emit_binary_diff_body(o
, one
, two
);
3311 emit_binary_diff_body(o
, two
, one
);
3314 int diff_filespec_is_binary(struct repository
*r
,
3315 struct diff_filespec
*one
)
3317 struct diff_populate_filespec_options dpf_options
= {
3321 if (one
->is_binary
== -1) {
3322 diff_filespec_load_driver(one
, r
->index
);
3323 if (one
->driver
->binary
!= -1)
3324 one
->is_binary
= one
->driver
->binary
;
3326 if (!one
->data
&& DIFF_FILE_VALID(one
))
3327 diff_populate_filespec(r
, one
, &dpf_options
);
3328 if (one
->is_binary
== -1 && one
->data
)
3329 one
->is_binary
= buffer_is_binary(one
->data
,
3331 if (one
->is_binary
== -1)
3335 return one
->is_binary
;
3338 static const struct userdiff_funcname
*
3339 diff_funcname_pattern(struct diff_options
*o
, struct diff_filespec
*one
)
3341 diff_filespec_load_driver(one
, o
->repo
->index
);
3342 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
3345 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
3347 if (!options
->a_prefix
)
3348 options
->a_prefix
= a
;
3349 if (!options
->b_prefix
)
3350 options
->b_prefix
= b
;
3353 struct userdiff_driver
*get_textconv(struct repository
*r
,
3354 struct diff_filespec
*one
)
3356 if (!DIFF_FILE_VALID(one
))
3359 diff_filespec_load_driver(one
, r
->index
);
3360 return userdiff_get_textconv(r
, one
->driver
);
3363 static struct string_list
*additional_headers(struct diff_options
*o
,
3366 if (!o
->additional_path_headers
)
3368 return strmap_get(o
->additional_path_headers
, path
);
3371 static void add_formatted_header(struct strbuf
*msg
,
3373 const char *line_prefix
,
3377 const char *next
, *newline
;
3379 for (next
= header
; *next
; next
= newline
) {
3380 newline
= strchrnul(next
, '\n');
3381 strbuf_addf(msg
, "%s%s%.*s%s\n", line_prefix
, meta
,
3382 (int)(newline
- next
), next
, reset
);
3388 static void add_formatted_headers(struct strbuf
*msg
,
3389 struct string_list
*more_headers
,
3390 const char *line_prefix
,
3396 for (i
= 0; i
< more_headers
->nr
; i
++)
3397 add_formatted_header(msg
, more_headers
->items
[i
].string
,
3398 line_prefix
, meta
, reset
);
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 if (!DIFF_FILE_VALID(one
) && !DIFF_FILE_VALID(two
)) {
3461 * We should only reach this point for pairs from
3462 * create_filepairs_for_header_only_notifications(). For
3463 * these, we should avoid the "/dev/null" special casing
3464 * above, meaning we avoid showing such pairs as either
3465 * "new file" or "deleted file" below.
3470 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3471 if (lbl
[0][0] == '/') {
3473 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3475 strbuf_addstr(&header
, xfrm_msg
);
3476 must_show_header
= 1;
3478 else if (lbl
[1][0] == '/') {
3479 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3481 strbuf_addstr(&header
, xfrm_msg
);
3482 must_show_header
= 1;
3485 if (one
->mode
!= two
->mode
) {
3486 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3487 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3488 must_show_header
= 1;
3491 strbuf_addstr(&header
, xfrm_msg
);
3494 * we do not run diff between different kind
3497 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3498 goto free_ab_and_return
;
3499 if (complete_rewrite
&&
3500 (textconv_one
|| !diff_filespec_is_binary(o
->repo
, one
)) &&
3501 (textconv_two
|| !diff_filespec_is_binary(o
->repo
, two
))) {
3502 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3503 header
.buf
, header
.len
, 0);
3504 strbuf_reset(&header
);
3505 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3506 textconv_one
, textconv_two
, o
);
3507 o
->found_changes
= 1;
3508 goto free_ab_and_return
;
3512 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3513 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3515 strbuf_reset(&header
);
3516 goto free_ab_and_return
;
3517 } else if (!o
->flags
.text
&&
3518 ( (!textconv_one
&& diff_filespec_is_binary(o
->repo
, one
)) ||
3519 (!textconv_two
&& diff_filespec_is_binary(o
->repo
, two
)) )) {
3520 struct strbuf sb
= STRBUF_INIT
;
3521 if (!one
->data
&& !two
->data
&&
3522 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3524 if (oideq(&one
->oid
, &two
->oid
)) {
3525 if (must_show_header
)
3526 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3527 header
.buf
, header
.len
,
3529 goto free_ab_and_return
;
3531 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3532 header
.buf
, header
.len
, 0);
3533 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3534 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3535 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3537 strbuf_release(&sb
);
3538 goto free_ab_and_return
;
3540 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3541 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3542 die("unable to read files to diff");
3543 /* Quite common confusing case */
3544 if (mf1
.size
== mf2
.size
&&
3545 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3546 if (must_show_header
)
3547 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3548 header
.buf
, header
.len
, 0);
3549 goto free_ab_and_return
;
3551 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3552 strbuf_reset(&header
);
3553 if (o
->flags
.binary
)
3554 emit_binary_diff(o
, &mf1
, &mf2
);
3556 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3557 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3558 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3560 strbuf_release(&sb
);
3562 o
->found_changes
= 1;
3564 /* Crazy xdl interfaces.. */
3565 const char *diffopts
;
3569 struct emit_callback ecbdata
;
3570 const struct userdiff_funcname
*pe
;
3572 if (must_show_header
) {
3573 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3574 header
.buf
, header
.len
, 0);
3575 strbuf_reset(&header
);
3578 mf1
.size
= fill_textconv(o
->repo
, textconv_one
, one
, &mf1
.ptr
);
3579 mf2
.size
= fill_textconv(o
->repo
, textconv_two
, two
, &mf2
.ptr
);
3581 pe
= diff_funcname_pattern(o
, one
);
3583 pe
= diff_funcname_pattern(o
, two
);
3585 memset(&xpp
, 0, sizeof(xpp
));
3586 memset(&xecfg
, 0, sizeof(xecfg
));
3587 memset(&ecbdata
, 0, sizeof(ecbdata
));
3588 if (o
->flags
.suppress_diff_headers
)
3590 ecbdata
.label_path
= lbl
;
3591 ecbdata
.color_diff
= want_color(o
->use_color
);
3592 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
3593 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3594 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3596 if (header
.len
&& !o
->flags
.suppress_diff_headers
)
3597 ecbdata
.header
= &header
;
3598 xpp
.flags
= o
->xdl_opts
;
3599 xpp
.ignore_regex
= o
->ignore_regex
;
3600 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3601 xpp
.anchors
= o
->anchors
;
3602 xpp
.anchors_nr
= o
->anchors_nr
;
3603 xecfg
.ctxlen
= o
->context
;
3604 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3605 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3606 if (o
->flags
.funccontext
)
3607 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3609 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3611 diffopts
= getenv("GIT_DIFF_OPTS");
3614 else if (skip_prefix(diffopts
, "--unified=", &v
))
3615 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3616 else if (skip_prefix(diffopts
, "-u", &v
))
3617 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3620 init_diff_words_data(&ecbdata
, o
, one
, two
);
3621 if (xdi_diff_outf(&mf1
, &mf2
, NULL
, fn_out_consume
,
3622 &ecbdata
, &xpp
, &xecfg
))
3623 die("unable to generate diff for %s", one
->path
);
3625 free_diff_words_data(&ecbdata
);
3630 xdiff_clear_find_func(&xecfg
);
3634 strbuf_release(&header
);
3635 diff_free_filespec_data(one
);
3636 diff_free_filespec_data(two
);
3642 static char *get_compact_summary(const struct diff_filepair
*p
, int is_renamed
)
3645 if (p
->status
== DIFF_STATUS_ADDED
) {
3646 if (S_ISLNK(p
->two
->mode
))
3648 else if ((p
->two
->mode
& 0777) == 0755)
3652 } else if (p
->status
== DIFF_STATUS_DELETED
)
3655 if (S_ISLNK(p
->one
->mode
) && !S_ISLNK(p
->two
->mode
))
3657 else if (!S_ISLNK(p
->one
->mode
) && S_ISLNK(p
->two
->mode
))
3659 else if ((p
->one
->mode
& 0777) == 0644 &&
3660 (p
->two
->mode
& 0777) == 0755)
3662 else if ((p
->one
->mode
& 0777) == 0755 &&
3663 (p
->two
->mode
& 0777) == 0644)
3668 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3669 struct diff_filespec
*one
,
3670 struct diff_filespec
*two
,
3671 struct diffstat_t
*diffstat
,
3672 struct diff_options
*o
,
3673 struct diff_filepair
*p
)
3676 struct diffstat_file
*data
;
3678 int complete_rewrite
= 0;
3680 if (!DIFF_PAIR_UNMERGED(p
)) {
3681 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3682 complete_rewrite
= 1;
3685 data
= diffstat_add(diffstat
, name_a
, name_b
);
3686 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3687 if (o
->flags
.stat_with_summary
)
3688 data
->comments
= get_compact_summary(p
, data
->is_renamed
);
3691 data
->is_unmerged
= 1;
3695 /* saves some reads if true, not a guarantee of diff outcome */
3696 may_differ
= !(one
->oid_valid
&& two
->oid_valid
&&
3697 oideq(&one
->oid
, &two
->oid
));
3699 if (diff_filespec_is_binary(o
->repo
, one
) ||
3700 diff_filespec_is_binary(o
->repo
, two
)) {
3701 data
->is_binary
= 1;
3706 data
->added
= diff_filespec_size(o
->repo
, two
);
3707 data
->deleted
= diff_filespec_size(o
->repo
, one
);
3711 else if (complete_rewrite
) {
3712 diff_populate_filespec(o
->repo
, one
, NULL
);
3713 diff_populate_filespec(o
->repo
, two
, NULL
);
3714 data
->deleted
= count_lines(one
->data
, one
->size
);
3715 data
->added
= count_lines(two
->data
, two
->size
);
3718 else if (may_differ
) {
3719 /* Crazy xdl interfaces.. */
3723 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3724 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3725 die("unable to read files to diff");
3727 memset(&xpp
, 0, sizeof(xpp
));
3728 memset(&xecfg
, 0, sizeof(xecfg
));
3729 xpp
.flags
= o
->xdl_opts
;
3730 xpp
.ignore_regex
= o
->ignore_regex
;
3731 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3732 xpp
.anchors
= o
->anchors
;
3733 xpp
.anchors_nr
= o
->anchors_nr
;
3734 xecfg
.ctxlen
= o
->context
;
3735 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3736 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
3737 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
3738 diffstat_consume
, diffstat
, &xpp
, &xecfg
))
3739 die("unable to generate diffstat for %s", one
->path
);
3741 if (DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
)) {
3742 struct diffstat_file
*file
=
3743 diffstat
->files
[diffstat
->nr
- 1];
3745 * Omit diffstats of modified files where nothing changed.
3746 * Even if may_differ, this might be the case due to
3747 * ignoring whitespace changes, etc.
3749 * But note that we special-case additions, deletions,
3750 * renames, and mode changes as adding an empty file,
3751 * for example is still of interest.
3753 if ((p
->status
== DIFF_STATUS_MODIFIED
)
3756 && one
->mode
== two
->mode
) {
3757 free_diffstat_file(file
);
3763 diff_free_filespec_data(one
);
3764 diff_free_filespec_data(two
);
3767 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3768 const char *attr_path
,
3769 struct diff_filespec
*one
,
3770 struct diff_filespec
*two
,
3771 struct diff_options
*o
)
3774 struct checkdiff_t data
;
3779 memset(&data
, 0, sizeof(data
));
3780 data
.filename
= name_b
? name_b
: name_a
;
3783 data
.ws_rule
= whitespace_rule(o
->repo
->index
, attr_path
);
3784 data
.conflict_marker_size
= ll_merge_marker_size(o
->repo
->index
, attr_path
);
3786 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3787 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3788 die("unable to read files to diff");
3791 * All the other codepaths check both sides, but not checking
3792 * the "old" side here is deliberate. We are checking the newly
3793 * introduced changes, and as long as the "new" side is text, we
3794 * can and should check what it introduces.
3796 if (diff_filespec_is_binary(o
->repo
, two
))
3797 goto free_and_return
;
3799 /* Crazy xdl interfaces.. */
3803 memset(&xpp
, 0, sizeof(xpp
));
3804 memset(&xecfg
, 0, sizeof(xecfg
));
3805 xecfg
.ctxlen
= 1; /* at least one context line */
3807 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume_hunk
,
3808 checkdiff_consume
, &data
,
3810 die("unable to generate checkdiff for %s", one
->path
);
3812 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3813 struct emit_callback ecbdata
;
3816 ecbdata
.ws_rule
= data
.ws_rule
;
3817 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3818 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3823 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3824 fprintf(o
->file
, "%s:%d: %s.\n",
3825 data
.filename
, blank_at_eof
, err
);
3826 data
.status
= 1; /* report errors */
3831 diff_free_filespec_data(one
);
3832 diff_free_filespec_data(two
);
3834 o
->flags
.check_failed
= 1;
3837 struct diff_filespec
*alloc_filespec(const char *path
)
3839 struct diff_filespec
*spec
;
3841 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3843 spec
->is_binary
= -1;
3847 void free_filespec(struct diff_filespec
*spec
)
3849 if (!--spec
->count
) {
3850 diff_free_filespec_data(spec
);
3855 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3856 int oid_valid
, unsigned short mode
)
3859 spec
->mode
= canon_mode(mode
);
3860 oidcpy(&spec
->oid
, oid
);
3861 spec
->oid_valid
= oid_valid
;
3866 * Given a name and sha1 pair, if the index tells us the file in
3867 * the work tree has that object contents, return true, so that
3868 * prepare_temp_file() does not have to inflate and extract.
3870 static int reuse_worktree_file(struct index_state
*istate
,
3872 const struct object_id
*oid
,
3875 const struct cache_entry
*ce
;
3880 * We do not read the cache ourselves here, because the
3881 * benchmark with my previous version that always reads cache
3882 * shows that it makes things worse for diff-tree comparing
3883 * two linux-2.6 kernel trees in an already checked out work
3884 * tree. This is because most diff-tree comparisons deal with
3885 * only a small number of files, while reading the cache is
3886 * expensive for a large project, and its cost outweighs the
3887 * savings we get by not inflating the object to a temporary
3888 * file. Practically, this code only helps when we are used
3889 * by diff-cache --cached, which does read the cache before
3895 /* We want to avoid the working directory if our caller
3896 * doesn't need the data in a normal file, this system
3897 * is rather slow with its stat/open/mmap/close syscalls,
3898 * and the object is contained in a pack file. The pack
3899 * is probably already open and will be faster to obtain
3900 * the data through than the working directory. Loose
3901 * objects however would tend to be slower as they need
3902 * to be individually opened and inflated.
3904 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_object_pack(oid
))
3908 * Similarly, if we'd have to convert the file contents anyway, that
3909 * makes the optimization not worthwhile.
3911 if (!want_file
&& would_convert_to_git(istate
, name
))
3915 * If this path does not match our sparse-checkout definition,
3916 * then the file will not be in the working directory.
3918 if (!path_in_sparse_checkout(name
, istate
))
3922 pos
= index_name_pos(istate
, name
, len
);
3925 ce
= istate
->cache
[pos
];
3928 * This is not the sha1 we are looking for, or
3929 * unreusable because it is not a regular file.
3931 if (!oideq(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
3935 * If ce is marked as "assume unchanged", there is no
3936 * guarantee that work tree matches what we are looking for.
3938 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
3942 * If ce matches the file in the work tree, we can reuse it.
3944 if (ce_uptodate(ce
) ||
3945 (!lstat(name
, &st
) && !ie_match_stat(istate
, ce
, &st
, 0)))
3951 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
3953 struct strbuf buf
= STRBUF_INIT
;
3956 /* Are we looking at the work tree? */
3957 if (s
->dirty_submodule
)
3960 strbuf_addf(&buf
, "Subproject commit %s%s\n",
3961 oid_to_hex(&s
->oid
), dirty
);
3965 strbuf_release(&buf
);
3967 s
->data
= strbuf_detach(&buf
, NULL
);
3974 * While doing rename detection and pickaxe operation, we may need to
3975 * grab the data for the blob (or file) for our own in-core comparison.
3976 * diff_filespec has data and size fields for this purpose.
3978 int diff_populate_filespec(struct repository
*r
,
3979 struct diff_filespec
*s
,
3980 const struct diff_populate_filespec_options
*options
)
3982 int size_only
= options
? options
->check_size_only
: 0;
3983 int check_binary
= options
? options
->check_binary
: 0;
3985 int conv_flags
= global_conv_flags_eol
;
3987 * demote FAIL to WARN to allow inspecting the situation
3988 * instead of refusing.
3990 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
3991 conv_flags
= CONV_EOL_RNDTRP_WARN
;
3993 if (!DIFF_FILE_VALID(s
))
3994 die("internal error: asking to populate invalid file.");
3995 if (S_ISDIR(s
->mode
))
4001 if (size_only
&& 0 < s
->size
)
4004 if (S_ISGITLINK(s
->mode
))
4005 return diff_populate_gitlink(s
, size_only
);
4007 if (!s
->oid_valid
||
4008 reuse_worktree_file(r
->index
, s
->path
, &s
->oid
, 0)) {
4009 struct strbuf buf
= STRBUF_INIT
;
4013 if (lstat(s
->path
, &st
) < 0) {
4017 s
->data
= (char *)"";
4021 s
->size
= xsize_t(st
.st_size
);
4024 if (S_ISLNK(st
.st_mode
)) {
4025 struct strbuf sb
= STRBUF_INIT
;
4027 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
4030 s
->data
= strbuf_detach(&sb
, NULL
);
4036 * Even if the caller would be happy with getting
4037 * only the size, we cannot return early at this
4038 * point if the path requires us to run the content
4041 if (size_only
&& !would_convert_to_git(r
->index
, s
->path
))
4045 * Note: this check uses xsize_t(st.st_size) that may
4046 * not be the true size of the blob after it goes
4047 * through convert_to_git(). This may not strictly be
4048 * correct, but the whole point of big_file_threshold
4049 * and is_binary check being that we want to avoid
4050 * opening the file and inspecting the contents, this
4054 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4058 fd
= open(s
->path
, O_RDONLY
);
4061 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
4063 s
->should_munmap
= 1;
4066 * Convert from working tree format to canonical git format
4068 if (convert_to_git(r
->index
, s
->path
, s
->data
, s
->size
, &buf
, conv_flags
)) {
4070 munmap(s
->data
, s
->size
);
4071 s
->should_munmap
= 0;
4072 s
->data
= strbuf_detach(&buf
, &size
);
4078 struct object_info info
= {
4082 if (!(size_only
|| check_binary
))
4084 * Set contentp, since there is no chance that merely
4085 * the size is sufficient.
4087 info
.contentp
= &s
->data
;
4089 if (options
&& options
->missing_object_cb
) {
4090 if (!oid_object_info_extended(r
, &s
->oid
, &info
,
4091 OBJECT_INFO_LOOKUP_REPLACE
|
4092 OBJECT_INFO_SKIP_FETCH_OBJECT
))
4094 options
->missing_object_cb(options
->missing_object_data
);
4096 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4097 OBJECT_INFO_LOOKUP_REPLACE
))
4098 die("unable to read %s", oid_to_hex(&s
->oid
));
4101 if (size_only
|| check_binary
) {
4104 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4109 if (!info
.contentp
) {
4110 info
.contentp
= &s
->data
;
4111 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4112 OBJECT_INFO_LOOKUP_REPLACE
))
4113 die("unable to read %s", oid_to_hex(&s
->oid
));
4120 void diff_free_filespec_blob(struct diff_filespec
*s
)
4124 else if (s
->should_munmap
)
4125 munmap(s
->data
, s
->size
);
4127 if (s
->should_free
|| s
->should_munmap
) {
4128 s
->should_free
= s
->should_munmap
= 0;
4133 void diff_free_filespec_data(struct diff_filespec
*s
)
4138 diff_free_filespec_blob(s
);
4139 FREE_AND_NULL(s
->cnt_data
);
4142 static void prep_temp_blob(struct index_state
*istate
,
4143 const char *path
, struct diff_tempfile
*temp
,
4146 const struct object_id
*oid
,
4149 struct strbuf buf
= STRBUF_INIT
;
4150 char *path_dup
= xstrdup(path
);
4151 const char *base
= basename(path_dup
);
4152 struct checkout_metadata meta
;
4154 init_checkout_metadata(&meta
, NULL
, NULL
, oid
);
4156 temp
->tempfile
= mks_tempfile_dt("git-blob-XXXXXX", base
);
4157 if (!temp
->tempfile
)
4158 die_errno("unable to create temp-file");
4159 if (convert_to_working_tree(istate
, path
,
4160 (const char *)blob
, (size_t)size
, &buf
, &meta
)) {
4164 if (write_in_full(temp
->tempfile
->fd
, blob
, size
) < 0 ||
4165 close_tempfile_gently(temp
->tempfile
))
4166 die_errno("unable to write temp-file");
4167 temp
->name
= get_tempfile_path(temp
->tempfile
);
4168 oid_to_hex_r(temp
->hex
, oid
);
4169 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
4170 strbuf_release(&buf
);
4174 static struct diff_tempfile
*prepare_temp_file(struct repository
*r
,
4176 struct diff_filespec
*one
)
4178 struct diff_tempfile
*temp
= claim_diff_tempfile();
4180 if (!DIFF_FILE_VALID(one
)) {
4182 /* A '-' entry produces this for file-2, and
4183 * a '+' entry produces this for file-1.
4185 temp
->name
= "/dev/null";
4186 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
4187 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
4191 if (!S_ISGITLINK(one
->mode
) &&
4193 reuse_worktree_file(r
->index
, name
, &one
->oid
, 1))) {
4195 if (lstat(name
, &st
) < 0) {
4196 if (errno
== ENOENT
)
4197 goto not_a_valid_file
;
4198 die_errno("stat(%s)", name
);
4200 if (S_ISLNK(st
.st_mode
)) {
4201 struct strbuf sb
= STRBUF_INIT
;
4202 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
4203 die_errno("readlink(%s)", name
);
4204 prep_temp_blob(r
->index
, name
, temp
, sb
.buf
, sb
.len
,
4206 &one
->oid
: null_oid()),
4208 one
->mode
: S_IFLNK
));
4209 strbuf_release(&sb
);
4212 /* we can borrow from the file in the work tree */
4214 if (!one
->oid_valid
)
4215 oid_to_hex_r(temp
->hex
, null_oid());
4217 oid_to_hex_r(temp
->hex
, &one
->oid
);
4218 /* Even though we may sometimes borrow the
4219 * contents from the work tree, we always want
4220 * one->mode. mode is trustworthy even when
4221 * !(one->oid_valid), as long as
4222 * DIFF_FILE_VALID(one).
4224 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
4229 if (diff_populate_filespec(r
, one
, NULL
))
4230 die("cannot read data blob for %s", one
->path
);
4231 prep_temp_blob(r
->index
, name
, temp
,
4232 one
->data
, one
->size
,
4233 &one
->oid
, one
->mode
);
4238 static void add_external_diff_name(struct repository
*r
,
4239 struct strvec
*argv
,
4241 struct diff_filespec
*df
)
4243 struct diff_tempfile
*temp
= prepare_temp_file(r
, name
, df
);
4244 strvec_push(argv
, temp
->name
);
4245 strvec_push(argv
, temp
->hex
);
4246 strvec_push(argv
, temp
->mode
);
4249 /* An external diff command takes:
4251 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4252 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4255 static void run_external_diff(const char *pgm
,
4258 struct diff_filespec
*one
,
4259 struct diff_filespec
*two
,
4260 const char *xfrm_msg
,
4261 struct diff_options
*o
)
4263 struct strvec argv
= STRVEC_INIT
;
4264 struct strvec env
= STRVEC_INIT
;
4265 struct diff_queue_struct
*q
= &diff_queued_diff
;
4267 strvec_push(&argv
, pgm
);
4268 strvec_push(&argv
, name
);
4271 add_external_diff_name(o
->repo
, &argv
, name
, one
);
4273 add_external_diff_name(o
->repo
, &argv
, name
, two
);
4275 add_external_diff_name(o
->repo
, &argv
, other
, two
);
4276 strvec_push(&argv
, other
);
4277 strvec_push(&argv
, xfrm_msg
);
4281 strvec_pushf(&env
, "GIT_DIFF_PATH_COUNTER=%d", ++o
->diff_path_counter
);
4282 strvec_pushf(&env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
4284 diff_free_filespec_data(one
);
4285 diff_free_filespec_data(two
);
4286 if (run_command_v_opt_cd_env(argv
.v
, RUN_USING_SHELL
, NULL
, env
.v
))
4287 die(_("external diff died, stopping at %s"), name
);
4290 strvec_clear(&argv
);
4294 static int similarity_index(struct diff_filepair
*p
)
4296 return p
->score
* 100 / MAX_SCORE
;
4299 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
4301 if (startup_info
->have_repository
)
4302 return find_unique_abbrev(oid
, abbrev
);
4304 char *hex
= oid_to_hex(oid
);
4306 abbrev
= FALLBACK_DEFAULT_ABBREV
;
4307 if (abbrev
> the_hash_algo
->hexsz
)
4308 BUG("oid abbreviation out of range: %d", abbrev
);
4315 static void fill_metainfo(struct strbuf
*msg
,
4318 struct diff_filespec
*one
,
4319 struct diff_filespec
*two
,
4320 struct diff_options
*o
,
4321 struct diff_filepair
*p
,
4322 int *must_show_header
,
4325 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
4326 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
4327 const char *line_prefix
= diff_line_prefix(o
);
4328 struct string_list
*more_headers
= NULL
;
4330 *must_show_header
= 1;
4331 strbuf_init(msg
, PATH_MAX
* 2 + 300);
4332 switch (p
->status
) {
4333 case DIFF_STATUS_COPIED
:
4334 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4335 line_prefix
, set
, similarity_index(p
));
4336 strbuf_addf(msg
, "%s\n%s%scopy from ",
4337 reset
, line_prefix
, set
);
4338 quote_c_style(name
, msg
, NULL
, 0);
4339 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
4340 quote_c_style(other
, msg
, NULL
, 0);
4341 strbuf_addf(msg
, "%s\n", reset
);
4343 case DIFF_STATUS_RENAMED
:
4344 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4345 line_prefix
, set
, similarity_index(p
));
4346 strbuf_addf(msg
, "%s\n%s%srename from ",
4347 reset
, line_prefix
, set
);
4348 quote_c_style(name
, msg
, NULL
, 0);
4349 strbuf_addf(msg
, "%s\n%s%srename to ",
4350 reset
, line_prefix
, set
);
4351 quote_c_style(other
, msg
, NULL
, 0);
4352 strbuf_addf(msg
, "%s\n", reset
);
4354 case DIFF_STATUS_MODIFIED
:
4356 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
4358 set
, similarity_index(p
), reset
);
4363 *must_show_header
= 0;
4365 if ((more_headers
= additional_headers(o
, name
))) {
4366 add_formatted_headers(msg
, more_headers
,
4367 line_prefix
, set
, reset
);
4368 *must_show_header
= 1;
4370 if (one
&& two
&& !oideq(&one
->oid
, &two
->oid
)) {
4371 const unsigned hexsz
= the_hash_algo
->hexsz
;
4372 int abbrev
= o
->abbrev
? o
->abbrev
: DEFAULT_ABBREV
;
4374 if (o
->flags
.full_index
)
4377 if (o
->flags
.binary
) {
4379 if ((!fill_mmfile(o
->repo
, &mf
, one
) &&
4380 diff_filespec_is_binary(o
->repo
, one
)) ||
4381 (!fill_mmfile(o
->repo
, &mf
, two
) &&
4382 diff_filespec_is_binary(o
->repo
, two
)))
4385 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
4386 diff_abbrev_oid(&one
->oid
, abbrev
),
4387 diff_abbrev_oid(&two
->oid
, abbrev
));
4388 if (one
->mode
== two
->mode
)
4389 strbuf_addf(msg
, " %06o", one
->mode
);
4390 strbuf_addf(msg
, "%s\n", reset
);
4394 static void run_diff_cmd(const char *pgm
,
4397 const char *attr_path
,
4398 struct diff_filespec
*one
,
4399 struct diff_filespec
*two
,
4401 struct diff_options
*o
,
4402 struct diff_filepair
*p
)
4404 const char *xfrm_msg
= NULL
;
4405 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
4406 int must_show_header
= 0;
4409 if (o
->flags
.allow_external
) {
4410 struct userdiff_driver
*drv
;
4412 drv
= userdiff_find_by_path(o
->repo
->index
, attr_path
);
4413 if (drv
&& drv
->external
)
4414 pgm
= drv
->external
;
4419 * don't use colors when the header is intended for an
4420 * external diff driver
4422 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
4424 want_color(o
->use_color
) && !pgm
);
4425 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
4429 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
, o
);
4433 builtin_diff(name
, other
? other
: name
,
4434 one
, two
, xfrm_msg
, must_show_header
,
4435 o
, complete_rewrite
);
4437 fprintf(o
->file
, "* Unmerged path %s\n", name
);
4440 static void diff_fill_oid_info(struct diff_filespec
*one
, struct index_state
*istate
)
4442 if (DIFF_FILE_VALID(one
)) {
4443 if (!one
->oid_valid
) {
4445 if (one
->is_stdin
) {
4449 if (lstat(one
->path
, &st
) < 0)
4450 die_errno("stat '%s'", one
->path
);
4451 if (index_path(istate
, &one
->oid
, one
->path
, &st
, 0))
4452 die("cannot hash %s", one
->path
);
4459 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
4461 /* Strip the prefix but do not molest /dev/null and absolute paths */
4462 if (*namep
&& !is_absolute_path(*namep
)) {
4463 *namep
+= prefix_length
;
4467 if (*otherp
&& !is_absolute_path(*otherp
)) {
4468 *otherp
+= prefix_length
;
4469 if (**otherp
== '/')
4474 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
4476 const char *pgm
= external_diff();
4478 struct diff_filespec
*one
= p
->one
;
4479 struct diff_filespec
*two
= p
->two
;
4482 const char *attr_path
;
4485 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
4487 if (o
->prefix_length
)
4488 strip_prefix(o
->prefix_length
, &name
, &other
);
4490 if (!o
->flags
.allow_external
)
4493 if (DIFF_PAIR_UNMERGED(p
)) {
4494 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
4495 NULL
, NULL
, NULL
, o
, p
);
4499 diff_fill_oid_info(one
, o
->repo
->index
);
4500 diff_fill_oid_info(two
, o
->repo
->index
);
4503 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4504 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4506 * a filepair that changes between file and symlink
4507 * needs to be split into deletion and creation.
4509 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4510 run_diff_cmd(NULL
, name
, other
, attr_path
,
4514 strbuf_release(&msg
);
4516 null
= alloc_filespec(one
->path
);
4517 run_diff_cmd(NULL
, name
, other
, attr_path
,
4518 null
, two
, &msg
, o
, p
);
4522 run_diff_cmd(pgm
, name
, other
, attr_path
,
4523 one
, two
, &msg
, o
, p
);
4525 strbuf_release(&msg
);
4528 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4529 struct diffstat_t
*diffstat
)
4534 if (DIFF_PAIR_UNMERGED(p
)) {
4536 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
,
4541 name
= p
->one
->path
;
4542 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4544 if (o
->prefix_length
)
4545 strip_prefix(o
->prefix_length
, &name
, &other
);
4547 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4548 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4550 builtin_diffstat(name
, other
, p
->one
, p
->two
,
4554 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4558 const char *attr_path
;
4560 if (DIFF_PAIR_UNMERGED(p
)) {
4565 name
= p
->one
->path
;
4566 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4567 attr_path
= other
? other
: name
;
4569 if (o
->prefix_length
)
4570 strip_prefix(o
->prefix_length
, &name
, &other
);
4572 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4573 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4575 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4578 static void prep_parse_options(struct diff_options
*options
);
4580 void repo_diff_setup(struct repository
*r
, struct diff_options
*options
)
4582 memcpy(options
, &default_diff_options
, sizeof(*options
));
4584 options
->file
= stdout
;
4587 options
->output_indicators
[OUTPUT_INDICATOR_NEW
] = '+';
4588 options
->output_indicators
[OUTPUT_INDICATOR_OLD
] = '-';
4589 options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
] = ' ';
4590 options
->abbrev
= DEFAULT_ABBREV
;
4591 options
->line_termination
= '\n';
4592 options
->break_opt
= -1;
4593 options
->rename_limit
= -1;
4594 options
->dirstat_permille
= diff_dirstat_permille_default
;
4595 options
->context
= diff_context_default
;
4596 options
->interhunkcontext
= diff_interhunk_context_default
;
4597 options
->ws_error_highlight
= ws_error_highlight_default
;
4598 options
->flags
.rename_empty
= 1;
4599 options
->flags
.relative_name
= diff_relative
;
4600 options
->objfind
= NULL
;
4602 /* pathchange left =NULL by default */
4603 options
->change
= diff_change
;
4604 options
->add_remove
= diff_addremove
;
4605 options
->use_color
= diff_use_color_default
;
4606 options
->detect_rename
= diff_detect_rename_default
;
4607 options
->xdl_opts
|= diff_algorithm
;
4608 if (diff_indent_heuristic
)
4609 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4611 options
->orderfile
= diff_order_file_cfg
;
4613 if (!options
->flags
.ignore_submodule_set
)
4614 options
->flags
.ignore_untracked_in_submodules
= 1;
4616 if (diff_no_prefix
) {
4617 options
->a_prefix
= options
->b_prefix
= "";
4618 } else if (!diff_mnemonic_prefix
) {
4619 options
->a_prefix
= "a/";
4620 options
->b_prefix
= "b/";
4623 options
->color_moved
= diff_color_moved_default
;
4624 options
->color_moved_ws_handling
= diff_color_moved_ws_default
;
4626 prep_parse_options(options
);
4629 static const char diff_status_letters
[] = {
4632 DIFF_STATUS_DELETED
,
4633 DIFF_STATUS_MODIFIED
,
4634 DIFF_STATUS_RENAMED
,
4635 DIFF_STATUS_TYPE_CHANGED
,
4636 DIFF_STATUS_UNKNOWN
,
4637 DIFF_STATUS_UNMERGED
,
4638 DIFF_STATUS_FILTER_AON
,
4639 DIFF_STATUS_FILTER_BROKEN
,
4643 static unsigned int filter_bit
['Z' + 1];
4645 static void prepare_filter_bits(void)
4649 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4650 for (i
= 0; diff_status_letters
[i
]; i
++)
4651 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4655 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4657 return opt
->filter
& filter_bit
[(int) status
];
4660 unsigned diff_filter_bit(char status
)
4662 prepare_filter_bits();
4663 return filter_bit
[(int) status
];
4666 void diff_setup_done(struct diff_options
*options
)
4668 unsigned check_mask
= DIFF_FORMAT_NAME
|
4669 DIFF_FORMAT_NAME_STATUS
|
4670 DIFF_FORMAT_CHECKDIFF
|
4671 DIFF_FORMAT_NO_OUTPUT
;
4673 * This must be signed because we're comparing against a potentially
4676 const int hexsz
= the_hash_algo
->hexsz
;
4678 if (options
->set_default
)
4679 options
->set_default(options
);
4681 if (HAS_MULTI_BITS(options
->output_format
& check_mask
))
4682 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
4683 "--name-only", "--name-status", "--check", "-s");
4685 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
4686 die(_("options '%s', '%s', and '%s' cannot be used together"),
4687 "-G", "-S", "--find-object");
4689 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_G_REGEX_MASK
))
4690 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"),
4691 "-G", "--pickaxe-regex", "--pickaxe-regex", "-S");
4693 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK
))
4694 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"),
4695 "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S");
4698 * Most of the time we can say "there are changes"
4699 * only by checking if there are changed paths, but
4700 * --ignore-whitespace* options force us to look
4704 if ((options
->xdl_opts
& XDF_WHITESPACE_FLAGS
) ||
4705 options
->ignore_regex_nr
)
4706 options
->flags
.diff_from_contents
= 1;
4708 options
->flags
.diff_from_contents
= 0;
4710 if (options
->flags
.find_copies_harder
)
4711 options
->detect_rename
= DIFF_DETECT_COPY
;
4713 if (!options
->flags
.relative_name
)
4714 options
->prefix
= NULL
;
4715 if (options
->prefix
)
4716 options
->prefix_length
= strlen(options
->prefix
);
4718 options
->prefix_length
= 0;
4720 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4721 DIFF_FORMAT_NAME_STATUS
|
4722 DIFF_FORMAT_CHECKDIFF
|
4723 DIFF_FORMAT_NO_OUTPUT
))
4724 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4725 DIFF_FORMAT_NUMSTAT
|
4726 DIFF_FORMAT_DIFFSTAT
|
4727 DIFF_FORMAT_SHORTSTAT
|
4728 DIFF_FORMAT_DIRSTAT
|
4729 DIFF_FORMAT_SUMMARY
|
4733 * These cases always need recursive; we do not drop caller-supplied
4734 * recursive bits for other formats here.
4736 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4737 DIFF_FORMAT_NUMSTAT
|
4738 DIFF_FORMAT_DIFFSTAT
|
4739 DIFF_FORMAT_SHORTSTAT
|
4740 DIFF_FORMAT_DIRSTAT
|
4741 DIFF_FORMAT_SUMMARY
|
4742 DIFF_FORMAT_CHECKDIFF
))
4743 options
->flags
.recursive
= 1;
4745 * Also pickaxe would not work very well if you do not say recursive
4747 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
4748 options
->flags
.recursive
= 1;
4750 * When patches are generated, submodules diffed against the work tree
4751 * must be checked for dirtiness too so it can be shown in the output
4753 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4754 options
->flags
.dirty_submodules
= 1;
4756 if (options
->detect_rename
&& options
->rename_limit
< 0)
4757 options
->rename_limit
= diff_rename_limit_default
;
4758 if (hexsz
< options
->abbrev
)
4759 options
->abbrev
= hexsz
; /* full */
4762 * It does not make sense to show the first hit we happened
4763 * to have found. It does not make sense not to return with
4764 * exit code in such a case either.
4766 if (options
->flags
.quick
) {
4767 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4768 options
->flags
.exit_with_status
= 1;
4771 options
->diff_path_counter
= 0;
4773 if (options
->flags
.follow_renames
&& options
->pathspec
.nr
!= 1)
4774 die(_("--follow requires exactly one pathspec"));
4776 if (!options
->use_color
|| external_diff())
4777 options
->color_moved
= 0;
4779 if (options
->filter_not
) {
4780 if (!options
->filter
)
4781 options
->filter
= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4782 options
->filter
&= ~options
->filter_not
;
4785 FREE_AND_NULL(options
->parseopts
);
4788 int parse_long_opt(const char *opt
, const char **argv
,
4789 const char **optarg
)
4791 const char *arg
= argv
[0];
4792 if (!skip_prefix(arg
, "--", &arg
))
4794 if (!skip_prefix(arg
, opt
, &arg
))
4796 if (*arg
== '=') { /* stuck form: --option=value */
4802 /* separate form: --option value */
4804 die("Option '--%s' requires a value", opt
);
4809 static int diff_opt_stat(const struct option
*opt
, const char *value
, int unset
)
4811 struct diff_options
*options
= opt
->value
;
4812 int width
= options
->stat_width
;
4813 int name_width
= options
->stat_name_width
;
4814 int graph_width
= options
->stat_graph_width
;
4815 int count
= options
->stat_count
;
4818 BUG_ON_OPT_NEG(unset
);
4820 if (!strcmp(opt
->long_name
, "stat")) {
4822 width
= strtoul(value
, &end
, 10);
4824 name_width
= strtoul(end
+1, &end
, 10);
4826 count
= strtoul(end
+1, &end
, 10);
4828 return error(_("invalid --stat value: %s"), value
);
4830 } else if (!strcmp(opt
->long_name
, "stat-width")) {
4831 width
= strtoul(value
, &end
, 10);
4833 return error(_("%s expects a numerical value"),
4835 } else if (!strcmp(opt
->long_name
, "stat-name-width")) {
4836 name_width
= strtoul(value
, &end
, 10);
4838 return error(_("%s expects a numerical value"),
4840 } else if (!strcmp(opt
->long_name
, "stat-graph-width")) {
4841 graph_width
= strtoul(value
, &end
, 10);
4843 return error(_("%s expects a numerical value"),
4845 } else if (!strcmp(opt
->long_name
, "stat-count")) {
4846 count
= strtoul(value
, &end
, 10);
4848 return error(_("%s expects a numerical value"),
4851 BUG("%s should not get here", opt
->long_name
);
4853 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4854 options
->stat_name_width
= name_width
;
4855 options
->stat_graph_width
= graph_width
;
4856 options
->stat_width
= width
;
4857 options
->stat_count
= count
;
4861 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
4863 struct strbuf errmsg
= STRBUF_INIT
;
4864 if (parse_dirstat_params(options
, params
, &errmsg
))
4865 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
4867 strbuf_release(&errmsg
);
4869 * The caller knows a dirstat-related option is given from the command
4870 * line; allow it to say "return this_function();"
4872 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
4876 static int diff_opt_diff_filter(const struct option
*option
,
4877 const char *optarg
, int unset
)
4879 struct diff_options
*opt
= option
->value
;
4882 BUG_ON_OPT_NEG(unset
);
4883 prepare_filter_bits();
4885 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4889 if ('a' <= optch
&& optch
<= 'z') {
4891 optch
= toupper(optch
);
4896 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
4898 return error(_("unknown change class '%c' in --diff-filter=%s"),
4901 opt
->filter_not
|= bit
;
4908 static void enable_patch_output(int *fmt
)
4910 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
4911 *fmt
|= DIFF_FORMAT_PATCH
;
4914 static int diff_opt_ws_error_highlight(const struct option
*option
,
4915 const char *arg
, int unset
)
4917 struct diff_options
*opt
= option
->value
;
4918 int val
= parse_ws_error_highlight(arg
);
4920 BUG_ON_OPT_NEG(unset
);
4922 return error(_("unknown value after ws-error-highlight=%.*s"),
4924 opt
->ws_error_highlight
= val
;
4928 static int diff_opt_find_object(const struct option
*option
,
4929 const char *arg
, int unset
)
4931 struct diff_options
*opt
= option
->value
;
4932 struct object_id oid
;
4934 BUG_ON_OPT_NEG(unset
);
4935 if (get_oid(arg
, &oid
))
4936 return error(_("unable to resolve '%s'"), arg
);
4939 CALLOC_ARRAY(opt
->objfind
, 1);
4941 opt
->pickaxe_opts
|= DIFF_PICKAXE_KIND_OBJFIND
;
4942 opt
->flags
.recursive
= 1;
4943 opt
->flags
.tree_in_recursive
= 1;
4944 oidset_insert(opt
->objfind
, &oid
);
4948 static int diff_opt_anchored(const struct option
*opt
,
4949 const char *arg
, int unset
)
4951 struct diff_options
*options
= opt
->value
;
4953 BUG_ON_OPT_NEG(unset
);
4954 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
4955 ALLOC_GROW(options
->anchors
, options
->anchors_nr
+ 1,
4956 options
->anchors_alloc
);
4957 options
->anchors
[options
->anchors_nr
++] = xstrdup(arg
);
4961 static int diff_opt_binary(const struct option
*opt
,
4962 const char *arg
, int unset
)
4964 struct diff_options
*options
= opt
->value
;
4966 BUG_ON_OPT_NEG(unset
);
4967 BUG_ON_OPT_ARG(arg
);
4968 enable_patch_output(&options
->output_format
);
4969 options
->flags
.binary
= 1;
4973 static int diff_opt_break_rewrites(const struct option
*opt
,
4974 const char *arg
, int unset
)
4976 int *break_opt
= opt
->value
;
4979 BUG_ON_OPT_NEG(unset
);
4982 opt1
= parse_rename_score(&arg
);
4985 else if (*arg
!= '/')
4986 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
4989 opt2
= parse_rename_score(&arg
);
4992 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
4993 *break_opt
= opt1
| (opt2
<< 16);
4997 static int diff_opt_char(const struct option
*opt
,
4998 const char *arg
, int unset
)
5000 char *value
= opt
->value
;
5002 BUG_ON_OPT_NEG(unset
);
5004 return error(_("%s expects a character, got '%s'"),
5005 opt
->long_name
, arg
);
5010 static int diff_opt_color_moved(const struct option
*opt
,
5011 const char *arg
, int unset
)
5013 struct diff_options
*options
= opt
->value
;
5016 options
->color_moved
= COLOR_MOVED_NO
;
5018 if (diff_color_moved_default
)
5019 options
->color_moved
= diff_color_moved_default
;
5020 if (options
->color_moved
== COLOR_MOVED_NO
)
5021 options
->color_moved
= COLOR_MOVED_DEFAULT
;
5023 int cm
= parse_color_moved(arg
);
5025 return error(_("bad --color-moved argument: %s"), arg
);
5026 options
->color_moved
= cm
;
5031 static int diff_opt_color_moved_ws(const struct option
*opt
,
5032 const char *arg
, int unset
)
5034 struct diff_options
*options
= opt
->value
;
5038 options
->color_moved_ws_handling
= 0;
5042 cm
= parse_color_moved_ws(arg
);
5043 if (cm
& COLOR_MOVED_WS_ERROR
)
5044 return error(_("invalid mode '%s' in --color-moved-ws"), arg
);
5045 options
->color_moved_ws_handling
= cm
;
5049 static int diff_opt_color_words(const struct option
*opt
,
5050 const char *arg
, int unset
)
5052 struct diff_options
*options
= opt
->value
;
5054 BUG_ON_OPT_NEG(unset
);
5055 options
->use_color
= 1;
5056 options
->word_diff
= DIFF_WORDS_COLOR
;
5057 options
->word_regex
= arg
;
5061 static int diff_opt_compact_summary(const struct option
*opt
,
5062 const char *arg
, int unset
)
5064 struct diff_options
*options
= opt
->value
;
5066 BUG_ON_OPT_ARG(arg
);
5068 options
->flags
.stat_with_summary
= 0;
5070 options
->flags
.stat_with_summary
= 1;
5071 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
5076 static int diff_opt_diff_algorithm(const struct option
*opt
,
5077 const char *arg
, int unset
)
5079 struct diff_options
*options
= opt
->value
;
5080 long value
= parse_algorithm_value(arg
);
5082 BUG_ON_OPT_NEG(unset
);
5084 return error(_("option diff-algorithm accepts \"myers\", "
5085 "\"minimal\", \"patience\" and \"histogram\""));
5087 /* clear out previous settings */
5088 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
5089 options
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
5090 options
->xdl_opts
|= value
;
5094 static int diff_opt_dirstat(const struct option
*opt
,
5095 const char *arg
, int unset
)
5097 struct diff_options
*options
= opt
->value
;
5099 BUG_ON_OPT_NEG(unset
);
5100 if (!strcmp(opt
->long_name
, "cumulative")) {
5102 BUG("how come --cumulative take a value?");
5104 } else if (!strcmp(opt
->long_name
, "dirstat-by-file"))
5105 parse_dirstat_opt(options
, "files");
5106 parse_dirstat_opt(options
, arg
? arg
: "");
5110 static int diff_opt_find_copies(const struct option
*opt
,
5111 const char *arg
, int unset
)
5113 struct diff_options
*options
= opt
->value
;
5115 BUG_ON_OPT_NEG(unset
);
5118 options
->rename_score
= parse_rename_score(&arg
);
5120 return error(_("invalid argument to %s"), opt
->long_name
);
5122 if (options
->detect_rename
== DIFF_DETECT_COPY
)
5123 options
->flags
.find_copies_harder
= 1;
5125 options
->detect_rename
= DIFF_DETECT_COPY
;
5130 static int diff_opt_find_renames(const struct option
*opt
,
5131 const char *arg
, int unset
)
5133 struct diff_options
*options
= opt
->value
;
5135 BUG_ON_OPT_NEG(unset
);
5138 options
->rename_score
= parse_rename_score(&arg
);
5140 return error(_("invalid argument to %s"), opt
->long_name
);
5142 options
->detect_rename
= DIFF_DETECT_RENAME
;
5146 static int diff_opt_follow(const struct option
*opt
,
5147 const char *arg
, int unset
)
5149 struct diff_options
*options
= opt
->value
;
5151 BUG_ON_OPT_ARG(arg
);
5153 options
->flags
.follow_renames
= 0;
5154 options
->flags
.default_follow_renames
= 0;
5156 options
->flags
.follow_renames
= 1;
5161 static int diff_opt_ignore_submodules(const struct option
*opt
,
5162 const char *arg
, int unset
)
5164 struct diff_options
*options
= opt
->value
;
5166 BUG_ON_OPT_NEG(unset
);
5169 options
->flags
.override_submodule_config
= 1;
5170 handle_ignore_submodules_arg(options
, arg
);
5174 static int diff_opt_line_prefix(const struct option
*opt
,
5175 const char *optarg
, int unset
)
5177 struct diff_options
*options
= opt
->value
;
5179 BUG_ON_OPT_NEG(unset
);
5180 options
->line_prefix
= optarg
;
5181 options
->line_prefix_length
= strlen(options
->line_prefix
);
5182 graph_setup_line_prefix(options
);
5186 static int diff_opt_no_prefix(const struct option
*opt
,
5187 const char *optarg
, int unset
)
5189 struct diff_options
*options
= opt
->value
;
5191 BUG_ON_OPT_NEG(unset
);
5192 BUG_ON_OPT_ARG(optarg
);
5193 options
->a_prefix
= "";
5194 options
->b_prefix
= "";
5198 static enum parse_opt_result
diff_opt_output(struct parse_opt_ctx_t
*ctx
,
5199 const struct option
*opt
,
5200 const char *arg
, int unset
)
5202 struct diff_options
*options
= opt
->value
;
5205 BUG_ON_OPT_NEG(unset
);
5206 path
= prefix_filename(ctx
->prefix
, arg
);
5207 options
->file
= xfopen(path
, "w");
5208 options
->close_file
= 1;
5209 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
5210 options
->use_color
= GIT_COLOR_NEVER
;
5215 static int diff_opt_patience(const struct option
*opt
,
5216 const char *arg
, int unset
)
5218 struct diff_options
*options
= opt
->value
;
5221 BUG_ON_OPT_NEG(unset
);
5222 BUG_ON_OPT_ARG(arg
);
5223 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
5225 * Both --patience and --anchored use PATIENCE_DIFF
5226 * internally, so remove any anchors previously
5229 for (i
= 0; i
< options
->anchors_nr
; i
++)
5230 free(options
->anchors
[i
]);
5231 options
->anchors_nr
= 0;
5235 static int diff_opt_ignore_regex(const struct option
*opt
,
5236 const char *arg
, int unset
)
5238 struct diff_options
*options
= opt
->value
;
5241 BUG_ON_OPT_NEG(unset
);
5242 regex
= xmalloc(sizeof(*regex
));
5243 if (regcomp(regex
, arg
, REG_EXTENDED
| REG_NEWLINE
))
5244 return error(_("invalid regex given to -I: '%s'"), arg
);
5245 ALLOC_GROW(options
->ignore_regex
, options
->ignore_regex_nr
+ 1,
5246 options
->ignore_regex_alloc
);
5247 options
->ignore_regex
[options
->ignore_regex_nr
++] = regex
;
5251 static int diff_opt_pickaxe_regex(const struct option
*opt
,
5252 const char *arg
, int unset
)
5254 struct diff_options
*options
= opt
->value
;
5256 BUG_ON_OPT_NEG(unset
);
5257 options
->pickaxe
= arg
;
5258 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
5262 static int diff_opt_pickaxe_string(const struct option
*opt
,
5263 const char *arg
, int unset
)
5265 struct diff_options
*options
= opt
->value
;
5267 BUG_ON_OPT_NEG(unset
);
5268 options
->pickaxe
= arg
;
5269 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
5273 static int diff_opt_relative(const struct option
*opt
,
5274 const char *arg
, int unset
)
5276 struct diff_options
*options
= opt
->value
;
5278 options
->flags
.relative_name
= !unset
;
5280 options
->prefix
= arg
;
5284 static int diff_opt_submodule(const struct option
*opt
,
5285 const char *arg
, int unset
)
5287 struct diff_options
*options
= opt
->value
;
5289 BUG_ON_OPT_NEG(unset
);
5292 if (parse_submodule_params(options
, arg
))
5293 return error(_("failed to parse --submodule option parameter: '%s'"),
5298 static int diff_opt_textconv(const struct option
*opt
,
5299 const char *arg
, int unset
)
5301 struct diff_options
*options
= opt
->value
;
5303 BUG_ON_OPT_ARG(arg
);
5305 options
->flags
.allow_textconv
= 0;
5307 options
->flags
.allow_textconv
= 1;
5308 options
->flags
.textconv_set_via_cmdline
= 1;
5313 static int diff_opt_unified(const struct option
*opt
,
5314 const char *arg
, int unset
)
5316 struct diff_options
*options
= opt
->value
;
5319 BUG_ON_OPT_NEG(unset
);
5322 options
->context
= strtol(arg
, &s
, 10);
5324 return error(_("%s expects a numerical value"), "--unified");
5326 enable_patch_output(&options
->output_format
);
5331 static int diff_opt_word_diff(const struct option
*opt
,
5332 const char *arg
, int unset
)
5334 struct diff_options
*options
= opt
->value
;
5336 BUG_ON_OPT_NEG(unset
);
5338 if (!strcmp(arg
, "plain"))
5339 options
->word_diff
= DIFF_WORDS_PLAIN
;
5340 else if (!strcmp(arg
, "color")) {
5341 options
->use_color
= 1;
5342 options
->word_diff
= DIFF_WORDS_COLOR
;
5344 else if (!strcmp(arg
, "porcelain"))
5345 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
5346 else if (!strcmp(arg
, "none"))
5347 options
->word_diff
= DIFF_WORDS_NONE
;
5349 return error(_("bad --word-diff argument: %s"), arg
);
5351 if (options
->word_diff
== DIFF_WORDS_NONE
)
5352 options
->word_diff
= DIFF_WORDS_PLAIN
;
5357 static int diff_opt_word_diff_regex(const struct option
*opt
,
5358 const char *arg
, int unset
)
5360 struct diff_options
*options
= opt
->value
;
5362 BUG_ON_OPT_NEG(unset
);
5363 if (options
->word_diff
== DIFF_WORDS_NONE
)
5364 options
->word_diff
= DIFF_WORDS_PLAIN
;
5365 options
->word_regex
= arg
;
5369 static int diff_opt_rotate_to(const struct option
*opt
, const char *arg
, int unset
)
5371 struct diff_options
*options
= opt
->value
;
5373 BUG_ON_OPT_NEG(unset
);
5374 if (!strcmp(opt
->long_name
, "skip-to"))
5375 options
->skip_instead_of_rotate
= 1;
5377 options
->skip_instead_of_rotate
= 0;
5378 options
->rotate_to
= arg
;
5382 static void prep_parse_options(struct diff_options
*options
)
5384 struct option parseopts
[] = {
5385 OPT_GROUP(N_("Diff output format options")),
5386 OPT_BITOP('p', "patch", &options
->output_format
,
5387 N_("generate patch"),
5388 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5389 OPT_BIT_F('s', "no-patch", &options
->output_format
,
5390 N_("suppress diff output"),
5391 DIFF_FORMAT_NO_OUTPUT
, PARSE_OPT_NONEG
),
5392 OPT_BITOP('u', NULL
, &options
->output_format
,
5393 N_("generate patch"),
5394 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5395 OPT_CALLBACK_F('U', "unified", options
, N_("<n>"),
5396 N_("generate diffs with <n> lines context"),
5397 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_unified
),
5398 OPT_BOOL('W', "function-context", &options
->flags
.funccontext
,
5399 N_("generate diffs with <n> lines context")),
5400 OPT_BIT_F(0, "raw", &options
->output_format
,
5401 N_("generate the diff in raw format"),
5402 DIFF_FORMAT_RAW
, PARSE_OPT_NONEG
),
5403 OPT_BITOP(0, "patch-with-raw", &options
->output_format
,
5404 N_("synonym for '-p --raw'"),
5405 DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
,
5406 DIFF_FORMAT_NO_OUTPUT
),
5407 OPT_BITOP(0, "patch-with-stat", &options
->output_format
,
5408 N_("synonym for '-p --stat'"),
5409 DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
,
5410 DIFF_FORMAT_NO_OUTPUT
),
5411 OPT_BIT_F(0, "numstat", &options
->output_format
,
5412 N_("machine friendly --stat"),
5413 DIFF_FORMAT_NUMSTAT
, PARSE_OPT_NONEG
),
5414 OPT_BIT_F(0, "shortstat", &options
->output_format
,
5415 N_("output only the last line of --stat"),
5416 DIFF_FORMAT_SHORTSTAT
, PARSE_OPT_NONEG
),
5417 OPT_CALLBACK_F('X', "dirstat", options
, N_("<param1,param2>..."),
5418 N_("output the distribution of relative amount of changes for each sub-directory"),
5419 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5421 OPT_CALLBACK_F(0, "cumulative", options
, NULL
,
5422 N_("synonym for --dirstat=cumulative"),
5423 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5425 OPT_CALLBACK_F(0, "dirstat-by-file", options
, N_("<param1,param2>..."),
5426 N_("synonym for --dirstat=files,param1,param2..."),
5427 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5429 OPT_BIT_F(0, "check", &options
->output_format
,
5430 N_("warn if changes introduce conflict markers or whitespace errors"),
5431 DIFF_FORMAT_CHECKDIFF
, PARSE_OPT_NONEG
),
5432 OPT_BIT_F(0, "summary", &options
->output_format
,
5433 N_("condensed summary such as creations, renames and mode changes"),
5434 DIFF_FORMAT_SUMMARY
, PARSE_OPT_NONEG
),
5435 OPT_BIT_F(0, "name-only", &options
->output_format
,
5436 N_("show only names of changed files"),
5437 DIFF_FORMAT_NAME
, PARSE_OPT_NONEG
),
5438 OPT_BIT_F(0, "name-status", &options
->output_format
,
5439 N_("show only names and status of changed files"),
5440 DIFF_FORMAT_NAME_STATUS
, PARSE_OPT_NONEG
),
5441 OPT_CALLBACK_F(0, "stat", options
, N_("<width>[,<name-width>[,<count>]]"),
5442 N_("generate diffstat"),
5443 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_stat
),
5444 OPT_CALLBACK_F(0, "stat-width", options
, N_("<width>"),
5445 N_("generate diffstat with a given width"),
5446 PARSE_OPT_NONEG
, diff_opt_stat
),
5447 OPT_CALLBACK_F(0, "stat-name-width", options
, N_("<width>"),
5448 N_("generate diffstat with a given name width"),
5449 PARSE_OPT_NONEG
, diff_opt_stat
),
5450 OPT_CALLBACK_F(0, "stat-graph-width", options
, N_("<width>"),
5451 N_("generate diffstat with a given graph width"),
5452 PARSE_OPT_NONEG
, diff_opt_stat
),
5453 OPT_CALLBACK_F(0, "stat-count", options
, N_("<count>"),
5454 N_("generate diffstat with limited lines"),
5455 PARSE_OPT_NONEG
, diff_opt_stat
),
5456 OPT_CALLBACK_F(0, "compact-summary", options
, NULL
,
5457 N_("generate compact summary in diffstat"),
5458 PARSE_OPT_NOARG
, diff_opt_compact_summary
),
5459 OPT_CALLBACK_F(0, "binary", options
, NULL
,
5460 N_("output a binary diff that can be applied"),
5461 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_binary
),
5462 OPT_BOOL(0, "full-index", &options
->flags
.full_index
,
5463 N_("show full pre- and post-image object names on the \"index\" lines")),
5464 OPT_COLOR_FLAG(0, "color", &options
->use_color
,
5465 N_("show colored diff")),
5466 OPT_CALLBACK_F(0, "ws-error-highlight", options
, N_("<kind>"),
5467 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5468 PARSE_OPT_NONEG
, diff_opt_ws_error_highlight
),
5469 OPT_SET_INT('z', NULL
, &options
->line_termination
,
5470 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5472 OPT__ABBREV(&options
->abbrev
),
5473 OPT_STRING_F(0, "src-prefix", &options
->a_prefix
, N_("<prefix>"),
5474 N_("show the given source prefix instead of \"a/\""),
5476 OPT_STRING_F(0, "dst-prefix", &options
->b_prefix
, N_("<prefix>"),
5477 N_("show the given destination prefix instead of \"b/\""),
5479 OPT_CALLBACK_F(0, "line-prefix", options
, N_("<prefix>"),
5480 N_("prepend an additional prefix to every line of output"),
5481 PARSE_OPT_NONEG
, diff_opt_line_prefix
),
5482 OPT_CALLBACK_F(0, "no-prefix", options
, NULL
,
5483 N_("do not show any source or destination prefix"),
5484 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_no_prefix
),
5485 OPT_INTEGER_F(0, "inter-hunk-context", &options
->interhunkcontext
,
5486 N_("show context between diff hunks up to the specified number of lines"),
5488 OPT_CALLBACK_F(0, "output-indicator-new",
5489 &options
->output_indicators
[OUTPUT_INDICATOR_NEW
],
5491 N_("specify the character to indicate a new line instead of '+'"),
5492 PARSE_OPT_NONEG
, diff_opt_char
),
5493 OPT_CALLBACK_F(0, "output-indicator-old",
5494 &options
->output_indicators
[OUTPUT_INDICATOR_OLD
],
5496 N_("specify the character to indicate an old line instead of '-'"),
5497 PARSE_OPT_NONEG
, diff_opt_char
),
5498 OPT_CALLBACK_F(0, "output-indicator-context",
5499 &options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
],
5501 N_("specify the character to indicate a context instead of ' '"),
5502 PARSE_OPT_NONEG
, diff_opt_char
),
5504 OPT_GROUP(N_("Diff rename options")),
5505 OPT_CALLBACK_F('B', "break-rewrites", &options
->break_opt
, N_("<n>[/<m>]"),
5506 N_("break complete rewrite changes into pairs of delete and create"),
5507 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5508 diff_opt_break_rewrites
),
5509 OPT_CALLBACK_F('M', "find-renames", options
, N_("<n>"),
5510 N_("detect renames"),
5511 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5512 diff_opt_find_renames
),
5513 OPT_SET_INT_F('D', "irreversible-delete", &options
->irreversible_delete
,
5514 N_("omit the preimage for deletes"),
5515 1, PARSE_OPT_NONEG
),
5516 OPT_CALLBACK_F('C', "find-copies", options
, N_("<n>"),
5517 N_("detect copies"),
5518 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5519 diff_opt_find_copies
),
5520 OPT_BOOL(0, "find-copies-harder", &options
->flags
.find_copies_harder
,
5521 N_("use unmodified files as source to find copies")),
5522 OPT_SET_INT_F(0, "no-renames", &options
->detect_rename
,
5523 N_("disable rename detection"),
5524 0, PARSE_OPT_NONEG
),
5525 OPT_BOOL(0, "rename-empty", &options
->flags
.rename_empty
,
5526 N_("use empty blobs as rename source")),
5527 OPT_CALLBACK_F(0, "follow", options
, NULL
,
5528 N_("continue listing the history of a file beyond renames"),
5529 PARSE_OPT_NOARG
, diff_opt_follow
),
5530 OPT_INTEGER('l', NULL
, &options
->rename_limit
,
5531 N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
5533 OPT_GROUP(N_("Diff algorithm options")),
5534 OPT_BIT(0, "minimal", &options
->xdl_opts
,
5535 N_("produce the smallest possible diff"),
5537 OPT_BIT_F('w', "ignore-all-space", &options
->xdl_opts
,
5538 N_("ignore whitespace when comparing lines"),
5539 XDF_IGNORE_WHITESPACE
, PARSE_OPT_NONEG
),
5540 OPT_BIT_F('b', "ignore-space-change", &options
->xdl_opts
,
5541 N_("ignore changes in amount of whitespace"),
5542 XDF_IGNORE_WHITESPACE_CHANGE
, PARSE_OPT_NONEG
),
5543 OPT_BIT_F(0, "ignore-space-at-eol", &options
->xdl_opts
,
5544 N_("ignore changes in whitespace at EOL"),
5545 XDF_IGNORE_WHITESPACE_AT_EOL
, PARSE_OPT_NONEG
),
5546 OPT_BIT_F(0, "ignore-cr-at-eol", &options
->xdl_opts
,
5547 N_("ignore carrier-return at the end of line"),
5548 XDF_IGNORE_CR_AT_EOL
, PARSE_OPT_NONEG
),
5549 OPT_BIT_F(0, "ignore-blank-lines", &options
->xdl_opts
,
5550 N_("ignore changes whose lines are all blank"),
5551 XDF_IGNORE_BLANK_LINES
, PARSE_OPT_NONEG
),
5552 OPT_CALLBACK_F('I', "ignore-matching-lines", options
, N_("<regex>"),
5553 N_("ignore changes whose all lines match <regex>"),
5554 0, diff_opt_ignore_regex
),
5555 OPT_BIT(0, "indent-heuristic", &options
->xdl_opts
,
5556 N_("heuristic to shift diff hunk boundaries for easy reading"),
5557 XDF_INDENT_HEURISTIC
),
5558 OPT_CALLBACK_F(0, "patience", options
, NULL
,
5559 N_("generate diff using the \"patience diff\" algorithm"),
5560 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5562 OPT_BITOP(0, "histogram", &options
->xdl_opts
,
5563 N_("generate diff using the \"histogram diff\" algorithm"),
5564 XDF_HISTOGRAM_DIFF
, XDF_DIFF_ALGORITHM_MASK
),
5565 OPT_CALLBACK_F(0, "diff-algorithm", options
, N_("<algorithm>"),
5566 N_("choose a diff algorithm"),
5567 PARSE_OPT_NONEG
, diff_opt_diff_algorithm
),
5568 OPT_CALLBACK_F(0, "anchored", options
, N_("<text>"),
5569 N_("generate diff using the \"anchored diff\" algorithm"),
5570 PARSE_OPT_NONEG
, diff_opt_anchored
),
5571 OPT_CALLBACK_F(0, "word-diff", options
, N_("<mode>"),
5572 N_("show word diff, using <mode> to delimit changed words"),
5573 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_word_diff
),
5574 OPT_CALLBACK_F(0, "word-diff-regex", options
, N_("<regex>"),
5575 N_("use <regex> to decide what a word is"),
5576 PARSE_OPT_NONEG
, diff_opt_word_diff_regex
),
5577 OPT_CALLBACK_F(0, "color-words", options
, N_("<regex>"),
5578 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5579 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_color_words
),
5580 OPT_CALLBACK_F(0, "color-moved", options
, N_("<mode>"),
5581 N_("moved lines of code are colored differently"),
5582 PARSE_OPT_OPTARG
, diff_opt_color_moved
),
5583 OPT_CALLBACK_F(0, "color-moved-ws", options
, N_("<mode>"),
5584 N_("how white spaces are ignored in --color-moved"),
5585 0, diff_opt_color_moved_ws
),
5587 OPT_GROUP(N_("Other diff options")),
5588 OPT_CALLBACK_F(0, "relative", options
, N_("<prefix>"),
5589 N_("when run from subdir, exclude changes outside and show relative paths"),
5592 OPT_BOOL('a', "text", &options
->flags
.text
,
5593 N_("treat all files as text")),
5594 OPT_BOOL('R', NULL
, &options
->flags
.reverse_diff
,
5595 N_("swap two inputs, reverse the diff")),
5596 OPT_BOOL(0, "exit-code", &options
->flags
.exit_with_status
,
5597 N_("exit with 1 if there were differences, 0 otherwise")),
5598 OPT_BOOL(0, "quiet", &options
->flags
.quick
,
5599 N_("disable all output of the program")),
5600 OPT_BOOL(0, "ext-diff", &options
->flags
.allow_external
,
5601 N_("allow an external diff helper to be executed")),
5602 OPT_CALLBACK_F(0, "textconv", options
, NULL
,
5603 N_("run external text conversion filters when comparing binary files"),
5604 PARSE_OPT_NOARG
, diff_opt_textconv
),
5605 OPT_CALLBACK_F(0, "ignore-submodules", options
, N_("<when>"),
5606 N_("ignore changes to submodules in the diff generation"),
5607 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5608 diff_opt_ignore_submodules
),
5609 OPT_CALLBACK_F(0, "submodule", options
, N_("<format>"),
5610 N_("specify how differences in submodules are shown"),
5611 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5612 diff_opt_submodule
),
5613 OPT_SET_INT_F(0, "ita-invisible-in-index", &options
->ita_invisible_in_index
,
5614 N_("hide 'git add -N' entries from the index"),
5615 1, PARSE_OPT_NONEG
),
5616 OPT_SET_INT_F(0, "ita-visible-in-index", &options
->ita_invisible_in_index
,
5617 N_("treat 'git add -N' entries as real in the index"),
5618 0, PARSE_OPT_NONEG
),
5619 OPT_CALLBACK_F('S', NULL
, options
, N_("<string>"),
5620 N_("look for differences that change the number of occurrences of the specified string"),
5621 0, diff_opt_pickaxe_string
),
5622 OPT_CALLBACK_F('G', NULL
, options
, N_("<regex>"),
5623 N_("look for differences that change the number of occurrences of the specified regex"),
5624 0, diff_opt_pickaxe_regex
),
5625 OPT_BIT_F(0, "pickaxe-all", &options
->pickaxe_opts
,
5626 N_("show all changes in the changeset with -S or -G"),
5627 DIFF_PICKAXE_ALL
, PARSE_OPT_NONEG
),
5628 OPT_BIT_F(0, "pickaxe-regex", &options
->pickaxe_opts
,
5629 N_("treat <string> in -S as extended POSIX regular expression"),
5630 DIFF_PICKAXE_REGEX
, PARSE_OPT_NONEG
),
5631 OPT_FILENAME('O', NULL
, &options
->orderfile
,
5632 N_("control the order in which files appear in the output")),
5633 OPT_CALLBACK_F(0, "rotate-to", options
, N_("<path>"),
5634 N_("show the change in the specified path first"),
5635 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5636 OPT_CALLBACK_F(0, "skip-to", options
, N_("<path>"),
5637 N_("skip the output to the specified path"),
5638 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5639 OPT_CALLBACK_F(0, "find-object", options
, N_("<object-id>"),
5640 N_("look for differences that change the number of occurrences of the specified object"),
5641 PARSE_OPT_NONEG
, diff_opt_find_object
),
5642 OPT_CALLBACK_F(0, "diff-filter", options
, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5643 N_("select files by diff type"),
5644 PARSE_OPT_NONEG
, diff_opt_diff_filter
),
5645 { OPTION_CALLBACK
, 0, "output", options
, N_("<file>"),
5646 N_("output to a specific file"),
5647 PARSE_OPT_NONEG
, NULL
, 0, diff_opt_output
},
5652 ALLOC_ARRAY(options
->parseopts
, ARRAY_SIZE(parseopts
));
5653 memcpy(options
->parseopts
, parseopts
, sizeof(parseopts
));
5656 int diff_opt_parse(struct diff_options
*options
,
5657 const char **av
, int ac
, const char *prefix
)
5662 ac
= parse_options(ac
, av
, prefix
, options
->parseopts
, NULL
,
5663 PARSE_OPT_KEEP_DASHDASH
|
5664 PARSE_OPT_KEEP_UNKNOWN
|
5665 PARSE_OPT_NO_INTERNAL_HELP
|
5666 PARSE_OPT_ONE_SHOT
|
5667 PARSE_OPT_STOP_AT_NON_OPTION
);
5672 int parse_rename_score(const char **cp_p
)
5674 unsigned long num
, scale
;
5676 const char *cp
= *cp_p
;
5683 if ( !dot
&& ch
== '.' ) {
5686 } else if ( ch
== '%' ) {
5687 scale
= dot
? scale
*100 : 100;
5688 cp
++; /* % is always at the end */
5690 } else if ( ch
>= '0' && ch
<= '9' ) {
5691 if ( scale
< 100000 ) {
5693 num
= (num
*10) + (ch
-'0');
5702 /* user says num divided by scale and we say internally that
5703 * is MAX_SCORE * num / scale.
5705 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
5708 struct diff_queue_struct diff_queued_diff
;
5710 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
5712 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
5713 queue
->queue
[queue
->nr
++] = dp
;
5716 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
5717 struct diff_filespec
*one
,
5718 struct diff_filespec
*two
)
5720 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
5728 void diff_free_filepair(struct diff_filepair
*p
)
5730 free_filespec(p
->one
);
5731 free_filespec(p
->two
);
5735 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
5740 /* Do we want all 40 hex characters? */
5741 if (len
== the_hash_algo
->hexsz
)
5742 return oid_to_hex(oid
);
5744 /* An abbreviated value is fine, possibly followed by an ellipsis. */
5745 abbrev
= diff_abbrev_oid(oid
, len
);
5747 if (!print_sha1_ellipsis())
5750 abblen
= strlen(abbrev
);
5753 * In well-behaved cases, where the abbreviated result is the
5754 * same as the requested length, append three dots after the
5755 * abbreviation (hence the whole logic is limited to the case
5756 * where abblen < 37); when the actual abbreviated result is a
5757 * bit longer than the requested length, we reduce the number
5758 * of dots so that they match the well-behaved ones. However,
5759 * if the actual abbreviation is longer than the requested
5760 * length by more than three, we give up on aligning, and add
5761 * three dots anyway, to indicate that the output is not the
5762 * full object name. Yes, this may be suboptimal, but this
5763 * appears only in "diff --raw --abbrev" output and it is not
5764 * worth the effort to change it now. Note that this would
5765 * likely to work fine when the automatic sizing of default
5766 * abbreviation length is used--we would be fed -1 in "len" in
5767 * that case, and will end up always appending three-dots, but
5768 * the automatic sizing is supposed to give abblen that ensures
5769 * uniqueness across all objects (statistically speaking).
5771 if (abblen
< the_hash_algo
->hexsz
- 3) {
5772 static char hex
[GIT_MAX_HEXSZ
+ 1];
5773 if (len
< abblen
&& abblen
<= len
+ 2)
5774 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
5776 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
5780 return oid_to_hex(oid
);
5783 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
5785 int line_termination
= opt
->line_termination
;
5786 int inter_name_termination
= line_termination
? '\t' : '\0';
5788 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5789 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
5790 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
5791 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
5792 fprintf(opt
->file
, "%s ",
5793 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
5796 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
5797 inter_name_termination
);
5799 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
5802 if (p
->status
== DIFF_STATUS_COPIED
||
5803 p
->status
== DIFF_STATUS_RENAMED
) {
5804 const char *name_a
, *name_b
;
5805 name_a
= p
->one
->path
;
5806 name_b
= p
->two
->path
;
5807 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5808 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
5809 write_name_quoted(name_b
, opt
->file
, line_termination
);
5811 const char *name_a
, *name_b
;
5812 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
5814 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5815 write_name_quoted(name_a
, opt
->file
, line_termination
);
5819 int diff_unmodified_pair(struct diff_filepair
*p
)
5821 /* This function is written stricter than necessary to support
5822 * the currently implemented transformers, but the idea is to
5823 * let transformers to produce diff_filepairs any way they want,
5824 * and filter and clean them up here before producing the output.
5826 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
5828 if (DIFF_PAIR_UNMERGED(p
))
5829 return 0; /* unmerged is interesting */
5831 /* deletion, addition, mode or type change
5832 * and rename are all interesting.
5834 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
5835 DIFF_PAIR_MODE_CHANGED(p
) ||
5836 strcmp(one
->path
, two
->path
))
5839 /* both are valid and point at the same path. that is, we are
5840 * dealing with a change.
5842 if (one
->oid_valid
&& two
->oid_valid
&&
5843 oideq(&one
->oid
, &two
->oid
) &&
5844 !one
->dirty_submodule
&& !two
->dirty_submodule
)
5845 return 1; /* no change */
5846 if (!one
->oid_valid
&& !two
->oid_valid
)
5847 return 1; /* both look at the same file on the filesystem. */
5851 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
5853 int include_conflict_headers
=
5854 (additional_headers(o
, p
->one
->path
) &&
5855 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
5858 * Check if we can return early without showing a diff. Note that
5859 * diff_filepair only stores {oid, path, mode, is_valid}
5860 * information for each path, and thus diff_unmodified_pair() only
5861 * considers those bits of info. However, we do not want pairs
5862 * created by create_filepairs_for_header_only_notifications()
5863 * (which always look like unmodified pairs) to be ignored, so
5864 * return early if both p is unmodified AND we don't want to
5865 * include_conflict_headers.
5867 if (diff_unmodified_pair(p
) && !include_conflict_headers
)
5870 /* Actually, we can also return early to avoid showing tree diffs */
5871 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5872 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5878 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
5879 struct diffstat_t
*diffstat
)
5881 if (diff_unmodified_pair(p
))
5884 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5885 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5886 return; /* no useful stat for tree diffs */
5888 run_diffstat(p
, o
, diffstat
);
5891 static void diff_flush_checkdiff(struct diff_filepair
*p
,
5892 struct diff_options
*o
)
5894 if (diff_unmodified_pair(p
))
5897 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5898 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5899 return; /* nothing to check in tree diffs */
5901 run_checkdiff(p
, o
);
5904 int diff_queue_is_empty(struct diff_options
*o
)
5906 struct diff_queue_struct
*q
= &diff_queued_diff
;
5908 int include_conflict_headers
=
5909 (o
->additional_path_headers
&&
5910 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
5912 if (include_conflict_headers
)
5915 for (i
= 0; i
< q
->nr
; i
++)
5916 if (!diff_unmodified_pair(q
->queue
[i
]))
5922 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
5924 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
5927 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
5929 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
5930 fprintf(stderr
, "queue[%d] %s size %lu\n",
5935 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
5937 diff_debug_filespec(p
->one
, i
, "one");
5938 diff_debug_filespec(p
->two
, i
, "two");
5939 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
5940 p
->score
, p
->status
? p
->status
: '?',
5941 p
->one
->rename_used
, p
->broken_pair
);
5944 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
5948 fprintf(stderr
, "%s\n", msg
);
5949 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
5950 for (i
= 0; i
< q
->nr
; i
++) {
5951 struct diff_filepair
*p
= q
->queue
[i
];
5952 diff_debug_filepair(p
, i
);
5957 static void diff_resolve_rename_copy(void)
5960 struct diff_filepair
*p
;
5961 struct diff_queue_struct
*q
= &diff_queued_diff
;
5963 diff_debug_queue("resolve-rename-copy", q
);
5965 for (i
= 0; i
< q
->nr
; i
++) {
5967 p
->status
= 0; /* undecided */
5968 if (DIFF_PAIR_UNMERGED(p
))
5969 p
->status
= DIFF_STATUS_UNMERGED
;
5970 else if (!DIFF_FILE_VALID(p
->one
))
5971 p
->status
= DIFF_STATUS_ADDED
;
5972 else if (!DIFF_FILE_VALID(p
->two
))
5973 p
->status
= DIFF_STATUS_DELETED
;
5974 else if (DIFF_PAIR_TYPE_CHANGED(p
))
5975 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
5977 /* from this point on, we are dealing with a pair
5978 * whose both sides are valid and of the same type, i.e.
5979 * either in-place edit or rename/copy edit.
5981 else if (DIFF_PAIR_RENAME(p
)) {
5983 * A rename might have re-connected a broken
5984 * pair up, causing the pathnames to be the
5985 * same again. If so, that's not a rename at
5986 * all, just a modification..
5988 * Otherwise, see if this source was used for
5989 * multiple renames, in which case we decrement
5990 * the count, and call it a copy.
5992 if (!strcmp(p
->one
->path
, p
->two
->path
))
5993 p
->status
= DIFF_STATUS_MODIFIED
;
5994 else if (--p
->one
->rename_used
> 0)
5995 p
->status
= DIFF_STATUS_COPIED
;
5997 p
->status
= DIFF_STATUS_RENAMED
;
5999 else if (!oideq(&p
->one
->oid
, &p
->two
->oid
) ||
6000 p
->one
->mode
!= p
->two
->mode
||
6001 p
->one
->dirty_submodule
||
6002 p
->two
->dirty_submodule
||
6003 is_null_oid(&p
->one
->oid
))
6004 p
->status
= DIFF_STATUS_MODIFIED
;
6006 /* This is a "no-change" entry and should not
6007 * happen anymore, but prepare for broken callers.
6009 error("feeding unmodified %s to diffcore",
6011 p
->status
= DIFF_STATUS_UNKNOWN
;
6014 diff_debug_queue("resolve-rename-copy done", q
);
6017 static int check_pair_status(struct diff_filepair
*p
)
6019 switch (p
->status
) {
6020 case DIFF_STATUS_UNKNOWN
:
6023 die("internal error in diff-resolve-rename-copy");
6029 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
6031 int fmt
= opt
->output_format
;
6033 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
6034 diff_flush_checkdiff(p
, opt
);
6035 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
6036 diff_flush_raw(p
, opt
);
6037 else if (fmt
& DIFF_FORMAT_NAME
) {
6038 const char *name_a
, *name_b
;
6039 name_a
= p
->two
->path
;
6041 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6042 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
6043 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
6047 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
6049 struct strbuf sb
= STRBUF_INIT
;
6051 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
6053 strbuf_addf(&sb
, " %s ", newdelete
);
6055 quote_c_style(fs
->path
, &sb
, NULL
, 0);
6056 strbuf_addch(&sb
, '\n');
6057 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6059 strbuf_release(&sb
);
6062 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
6065 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
6066 struct strbuf sb
= STRBUF_INIT
;
6067 strbuf_addf(&sb
, " mode change %06o => %06o",
6068 p
->one
->mode
, p
->two
->mode
);
6070 strbuf_addch(&sb
, ' ');
6071 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6073 strbuf_addch(&sb
, '\n');
6074 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6076 strbuf_release(&sb
);
6080 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
6081 struct diff_filepair
*p
)
6083 struct strbuf sb
= STRBUF_INIT
;
6084 struct strbuf names
= STRBUF_INIT
;
6086 pprint_rename(&names
, p
->one
->path
, p
->two
->path
);
6087 strbuf_addf(&sb
, " %s %s (%d%%)\n",
6088 renamecopy
, names
.buf
, similarity_index(p
));
6089 strbuf_release(&names
);
6090 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6092 show_mode_change(opt
, p
, 0);
6093 strbuf_release(&sb
);
6096 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
6099 case DIFF_STATUS_DELETED
:
6100 show_file_mode_name(opt
, "delete", p
->one
);
6102 case DIFF_STATUS_ADDED
:
6103 show_file_mode_name(opt
, "create", p
->two
);
6105 case DIFF_STATUS_COPIED
:
6106 show_rename_copy(opt
, "copy", p
);
6108 case DIFF_STATUS_RENAMED
:
6109 show_rename_copy(opt
, "rename", p
);
6113 struct strbuf sb
= STRBUF_INIT
;
6114 strbuf_addstr(&sb
, " rewrite ");
6115 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6116 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
6117 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6119 strbuf_release(&sb
);
6121 show_mode_change(opt
, p
, !p
->score
);
6131 static int remove_space(char *line
, int len
)
6137 for (i
= 0; i
< len
; i
++)
6138 if (!isspace((c
= line
[i
])))
6144 void flush_one_hunk(struct object_id
*result
, git_hash_ctx
*ctx
)
6146 unsigned char hash
[GIT_MAX_RAWSZ
];
6147 unsigned short carry
= 0;
6150 the_hash_algo
->final_fn(hash
, ctx
);
6151 the_hash_algo
->init_fn(ctx
);
6152 /* 20-byte sum, with carry */
6153 for (i
= 0; i
< the_hash_algo
->rawsz
; ++i
) {
6154 carry
+= result
->hash
[i
] + hash
[i
];
6155 result
->hash
[i
] = carry
;
6160 static int patch_id_consume(void *priv
, char *line
, unsigned long len
)
6162 struct patch_id_t
*data
= priv
;
6165 if (len
> 12 && starts_with(line
, "\\ "))
6167 new_len
= remove_space(line
, len
);
6169 the_hash_algo
->update_fn(data
->ctx
, line
, new_len
);
6170 data
->patchlen
+= new_len
;
6174 static void patch_id_add_string(git_hash_ctx
*ctx
, const char *str
)
6176 the_hash_algo
->update_fn(ctx
, str
, strlen(str
));
6179 static void patch_id_add_mode(git_hash_ctx
*ctx
, unsigned mode
)
6181 /* large enough for 2^32 in octal */
6183 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
6184 the_hash_algo
->update_fn(ctx
, buf
, len
);
6187 /* returns 0 upon success, and writes result into oid */
6188 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
, int stable
)
6190 struct diff_queue_struct
*q
= &diff_queued_diff
;
6193 struct patch_id_t data
;
6195 the_hash_algo
->init_fn(&ctx
);
6196 memset(&data
, 0, sizeof(struct patch_id_t
));
6200 for (i
= 0; i
< q
->nr
; i
++) {
6204 struct diff_filepair
*p
= q
->queue
[i
];
6207 memset(&xpp
, 0, sizeof(xpp
));
6208 memset(&xecfg
, 0, sizeof(xecfg
));
6210 return error("internal diff status error");
6211 if (p
->status
== DIFF_STATUS_UNKNOWN
)
6213 if (diff_unmodified_pair(p
))
6215 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6216 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6218 if (DIFF_PAIR_UNMERGED(p
))
6221 diff_fill_oid_info(p
->one
, options
->repo
->index
);
6222 diff_fill_oid_info(p
->two
, options
->repo
->index
);
6224 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
6225 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
6226 patch_id_add_string(&ctx
, "diff--git");
6227 patch_id_add_string(&ctx
, "a/");
6228 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6229 patch_id_add_string(&ctx
, "b/");
6230 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6232 if (p
->one
->mode
== 0) {
6233 patch_id_add_string(&ctx
, "newfilemode");
6234 patch_id_add_mode(&ctx
, p
->two
->mode
);
6235 patch_id_add_string(&ctx
, "---/dev/null");
6236 patch_id_add_string(&ctx
, "+++b/");
6237 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6238 } else if (p
->two
->mode
== 0) {
6239 patch_id_add_string(&ctx
, "deletedfilemode");
6240 patch_id_add_mode(&ctx
, p
->one
->mode
);
6241 patch_id_add_string(&ctx
, "---a/");
6242 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6243 patch_id_add_string(&ctx
, "+++/dev/null");
6245 patch_id_add_string(&ctx
, "---a/");
6246 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6247 patch_id_add_string(&ctx
, "+++b/");
6248 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6251 if (diff_header_only
)
6254 if (fill_mmfile(options
->repo
, &mf1
, p
->one
) < 0 ||
6255 fill_mmfile(options
->repo
, &mf2
, p
->two
) < 0)
6256 return error("unable to read files to diff");
6258 if (diff_filespec_is_binary(options
->repo
, p
->one
) ||
6259 diff_filespec_is_binary(options
->repo
, p
->two
)) {
6260 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->one
->oid
),
6261 the_hash_algo
->hexsz
);
6262 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->two
->oid
),
6263 the_hash_algo
->hexsz
);
6269 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
6270 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
6271 patch_id_consume
, &data
, &xpp
, &xecfg
))
6272 return error("unable to generate patch-id diff for %s",
6276 flush_one_hunk(oid
, &ctx
);
6280 the_hash_algo
->final_oid_fn(oid
, &ctx
);
6285 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
, int stable
)
6287 struct diff_queue_struct
*q
= &diff_queued_diff
;
6289 int result
= diff_get_patch_id(options
, oid
, diff_header_only
, stable
);
6291 for (i
= 0; i
< q
->nr
; i
++)
6292 diff_free_filepair(q
->queue
[i
]);
6295 DIFF_QUEUE_CLEAR(q
);
6300 static int is_summary_empty(const struct diff_queue_struct
*q
)
6304 for (i
= 0; i
< q
->nr
; i
++) {
6305 const struct diff_filepair
*p
= q
->queue
[i
];
6307 switch (p
->status
) {
6308 case DIFF_STATUS_DELETED
:
6309 case DIFF_STATUS_ADDED
:
6310 case DIFF_STATUS_COPIED
:
6311 case DIFF_STATUS_RENAMED
:
6316 if (p
->one
->mode
&& p
->two
->mode
&&
6317 p
->one
->mode
!= p
->two
->mode
)
6325 static const char rename_limit_warning
[] =
6326 N_("exhaustive rename detection was skipped due to too many files.");
6328 static const char degrade_cc_to_c_warning
[] =
6329 N_("only found copies from modified paths due to too many files.");
6331 static const char rename_limit_advice
[] =
6332 N_("you may want to set your %s variable to at least "
6333 "%d and retry the command.");
6335 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
6339 warning(_(degrade_cc_to_c_warning
));
6341 warning(_(rename_limit_warning
));
6345 warning(_(rename_limit_advice
), varname
, needed
);
6348 static void create_filepairs_for_header_only_notifications(struct diff_options
*o
)
6350 struct strset present
;
6351 struct diff_queue_struct
*q
= &diff_queued_diff
;
6352 struct hashmap_iter iter
;
6353 struct strmap_entry
*e
;
6356 strset_init_with_options(&present
, /*pool*/ NULL
, /*strdup*/ 0);
6359 * Find out which paths exist in diff_queued_diff, preferring
6360 * one->path for any pair that has multiple paths.
6362 for (i
= 0; i
< q
->nr
; i
++) {
6363 struct diff_filepair
*p
= q
->queue
[i
];
6364 char *path
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
6366 if (strmap_contains(o
->additional_path_headers
, path
))
6367 strset_add(&present
, path
);
6371 * Loop over paths in additional_path_headers; for each NOT already
6372 * in diff_queued_diff, create a synthetic filepair and insert that
6373 * into diff_queued_diff.
6375 strmap_for_each_entry(o
->additional_path_headers
, &iter
, e
) {
6376 if (!strset_contains(&present
, e
->key
)) {
6377 struct diff_filespec
*one
, *two
;
6378 struct diff_filepair
*p
;
6380 one
= alloc_filespec(e
->key
);
6381 two
= alloc_filespec(e
->key
);
6382 fill_filespec(one
, null_oid(), 0, 0);
6383 fill_filespec(two
, null_oid(), 0, 0);
6384 p
= diff_queue(q
, one
, two
);
6385 p
->status
= DIFF_STATUS_MODIFIED
;
6389 /* Re-sort the filepairs */
6390 diffcore_fix_diff_index();
6393 strset_clear(&present
);
6396 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
6399 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
6400 struct diff_queue_struct
*q
= &diff_queued_diff
;
6402 if (WSEH_NEW
& WS_RULE_MASK
)
6403 BUG("WS rules bit mask overlaps with diff symbol flags");
6406 o
->emitted_symbols
= &esm
;
6408 if (o
->additional_path_headers
)
6409 create_filepairs_for_header_only_notifications(o
);
6411 for (i
= 0; i
< q
->nr
; i
++) {
6412 struct diff_filepair
*p
= q
->queue
[i
];
6413 if (check_pair_status(p
))
6414 diff_flush_patch(p
, o
);
6417 if (o
->emitted_symbols
) {
6418 if (o
->color_moved
) {
6419 struct mem_pool entry_pool
;
6420 struct moved_entry_list
*entry_list
;
6422 mem_pool_init(&entry_pool
, 1024 * 1024);
6423 entry_list
= add_lines_to_move_detection(o
,
6425 mark_color_as_moved(o
, entry_list
);
6426 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
6429 mem_pool_discard(&entry_pool
, 0);
6433 for (i
= 0; i
< esm
.nr
; i
++)
6434 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
6436 for (i
= 0; i
< esm
.nr
; i
++)
6437 free((void *)esm
.buf
[i
].line
);
6440 o
->emitted_symbols
= NULL
;
6444 static void diff_free_file(struct diff_options
*options
)
6446 if (options
->close_file
)
6447 fclose(options
->file
);
6450 static void diff_free_ignore_regex(struct diff_options
*options
)
6454 for (i
= 0; i
< options
->ignore_regex_nr
; i
++) {
6455 regfree(options
->ignore_regex
[i
]);
6456 free(options
->ignore_regex
[i
]);
6458 free(options
->ignore_regex
);
6461 void diff_free(struct diff_options
*options
)
6463 if (options
->no_free
)
6466 diff_free_file(options
);
6467 diff_free_ignore_regex(options
);
6468 clear_pathspec(&options
->pathspec
);
6469 FREE_AND_NULL(options
->parseopts
);
6472 void diff_flush(struct diff_options
*options
)
6474 struct diff_queue_struct
*q
= &diff_queued_diff
;
6475 int i
, output_format
= options
->output_format
;
6477 int dirstat_by_line
= 0;
6480 * Order: raw, stat, summary, patch
6481 * or: name/name-status/checkdiff (other bits clear)
6483 if (!q
->nr
&& !options
->additional_path_headers
)
6486 if (output_format
& (DIFF_FORMAT_RAW
|
6488 DIFF_FORMAT_NAME_STATUS
|
6489 DIFF_FORMAT_CHECKDIFF
)) {
6490 for (i
= 0; i
< q
->nr
; i
++) {
6491 struct diff_filepair
*p
= q
->queue
[i
];
6492 if (check_pair_status(p
))
6493 flush_one_pair(p
, options
);
6498 if (output_format
& DIFF_FORMAT_DIRSTAT
&& options
->flags
.dirstat_by_line
)
6499 dirstat_by_line
= 1;
6501 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
6503 struct diffstat_t diffstat
;
6505 compute_diffstat(options
, &diffstat
, q
);
6506 if (output_format
& DIFF_FORMAT_NUMSTAT
)
6507 show_numstat(&diffstat
, options
);
6508 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
6509 show_stats(&diffstat
, options
);
6510 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
6511 show_shortstats(&diffstat
, options
);
6512 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
6513 show_dirstat_by_line(&diffstat
, options
);
6514 free_diffstat_info(&diffstat
);
6517 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
6518 show_dirstat(options
);
6520 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
6521 for (i
= 0; i
< q
->nr
; i
++) {
6522 diff_summary(options
, q
->queue
[i
]);
6527 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
6528 options
->flags
.exit_with_status
&&
6529 options
->flags
.diff_from_contents
) {
6531 * run diff_flush_patch for the exit status. setting
6532 * options->file to /dev/null should be safe, because we
6533 * aren't supposed to produce any output anyway.
6535 diff_free_file(options
);
6536 options
->file
= xfopen("/dev/null", "w");
6537 options
->close_file
= 1;
6538 options
->color_moved
= 0;
6539 for (i
= 0; i
< q
->nr
; i
++) {
6540 struct diff_filepair
*p
= q
->queue
[i
];
6541 if (check_pair_status(p
))
6542 diff_flush_patch(p
, options
);
6543 if (options
->found_changes
)
6548 if (output_format
& DIFF_FORMAT_PATCH
) {
6550 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
6551 if (options
->stat_sep
)
6552 /* attach patch instead of inline */
6553 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
6557 diff_flush_patch_all_file_pairs(options
);
6560 if (output_format
& DIFF_FORMAT_CALLBACK
)
6561 options
->format_callback(q
, options
, options
->format_callback_data
);
6563 for (i
= 0; i
< q
->nr
; i
++)
6564 diff_free_filepair(q
->queue
[i
]);
6567 DIFF_QUEUE_CLEAR(q
);
6571 * Report the content-level differences with HAS_CHANGES;
6572 * diff_addremove/diff_change does not set the bit when
6573 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6575 if (options
->flags
.diff_from_contents
) {
6576 if (options
->found_changes
)
6577 options
->flags
.has_changes
= 1;
6579 options
->flags
.has_changes
= 0;
6583 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
6585 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
6587 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
6589 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
6590 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
6591 filter_bit_tst(p
->status
, options
)));
6594 static void diffcore_apply_filter(struct diff_options
*options
)
6597 struct diff_queue_struct
*q
= &diff_queued_diff
;
6598 struct diff_queue_struct outq
;
6600 DIFF_QUEUE_CLEAR(&outq
);
6602 if (!options
->filter
)
6605 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
6607 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
6608 if (match_filter(options
, q
->queue
[i
]))
6614 /* otherwise we will clear the whole queue
6615 * by copying the empty outq at the end of this
6616 * function, but first clear the current entries
6619 for (i
= 0; i
< q
->nr
; i
++)
6620 diff_free_filepair(q
->queue
[i
]);
6623 /* Only the matching ones */
6624 for (i
= 0; i
< q
->nr
; i
++) {
6625 struct diff_filepair
*p
= q
->queue
[i
];
6626 if (match_filter(options
, p
))
6629 diff_free_filepair(p
);
6636 /* Check whether two filespecs with the same mode and size are identical */
6637 static int diff_filespec_is_identical(struct repository
*r
,
6638 struct diff_filespec
*one
,
6639 struct diff_filespec
*two
)
6641 if (S_ISGITLINK(one
->mode
))
6643 if (diff_populate_filespec(r
, one
, NULL
))
6645 if (diff_populate_filespec(r
, two
, NULL
))
6647 return !memcmp(one
->data
, two
->data
, one
->size
);
6650 static int diff_filespec_check_stat_unmatch(struct repository
*r
,
6651 struct diff_filepair
*p
)
6653 struct diff_populate_filespec_options dpf_options
= {
6654 .check_size_only
= 1,
6655 .missing_object_cb
= diff_queued_diff_prefetch
,
6656 .missing_object_data
= r
,
6659 if (p
->done_skip_stat_unmatch
)
6660 return p
->skip_stat_unmatch_result
;
6662 p
->done_skip_stat_unmatch
= 1;
6663 p
->skip_stat_unmatch_result
= 0;
6665 * 1. Entries that come from stat info dirtiness
6666 * always have both sides (iow, not create/delete),
6667 * one side of the object name is unknown, with
6668 * the same mode and size. Keep the ones that
6669 * do not match these criteria. They have real
6672 * 2. At this point, the file is known to be modified,
6673 * with the same mode and size, and the object
6674 * name of one side is unknown. Need to inspect
6675 * the identical contents.
6677 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
6678 !DIFF_FILE_VALID(p
->two
) ||
6679 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
6680 (p
->one
->mode
!= p
->two
->mode
) ||
6681 diff_populate_filespec(r
, p
->one
, &dpf_options
) ||
6682 diff_populate_filespec(r
, p
->two
, &dpf_options
) ||
6683 (p
->one
->size
!= p
->two
->size
) ||
6684 !diff_filespec_is_identical(r
, p
->one
, p
->two
)) /* (2) */
6685 p
->skip_stat_unmatch_result
= 1;
6686 return p
->skip_stat_unmatch_result
;
6689 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
6692 struct diff_queue_struct
*q
= &diff_queued_diff
;
6693 struct diff_queue_struct outq
;
6694 DIFF_QUEUE_CLEAR(&outq
);
6696 for (i
= 0; i
< q
->nr
; i
++) {
6697 struct diff_filepair
*p
= q
->queue
[i
];
6699 if (diff_filespec_check_stat_unmatch(diffopt
->repo
, p
))
6703 * The caller can subtract 1 from skip_stat_unmatch
6704 * to determine how many paths were dirty only
6705 * due to stat info mismatch.
6707 if (!diffopt
->flags
.no_index
)
6708 diffopt
->skip_stat_unmatch
++;
6709 diff_free_filepair(p
);
6716 static int diffnamecmp(const void *a_
, const void *b_
)
6718 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
6719 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
6720 const char *name_a
, *name_b
;
6722 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
6723 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
6724 return strcmp(name_a
, name_b
);
6727 void diffcore_fix_diff_index(void)
6729 struct diff_queue_struct
*q
= &diff_queued_diff
;
6730 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
6733 void diff_add_if_missing(struct repository
*r
,
6734 struct oid_array
*to_fetch
,
6735 const struct diff_filespec
*filespec
)
6737 if (filespec
&& filespec
->oid_valid
&&
6738 !S_ISGITLINK(filespec
->mode
) &&
6739 oid_object_info_extended(r
, &filespec
->oid
, NULL
,
6740 OBJECT_INFO_FOR_PREFETCH
))
6741 oid_array_append(to_fetch
, &filespec
->oid
);
6744 void diff_queued_diff_prefetch(void *repository
)
6746 struct repository
*repo
= repository
;
6748 struct diff_queue_struct
*q
= &diff_queued_diff
;
6749 struct oid_array to_fetch
= OID_ARRAY_INIT
;
6751 for (i
= 0; i
< q
->nr
; i
++) {
6752 struct diff_filepair
*p
= q
->queue
[i
];
6753 diff_add_if_missing(repo
, &to_fetch
, p
->one
);
6754 diff_add_if_missing(repo
, &to_fetch
, p
->two
);
6758 * NEEDSWORK: Consider deduplicating the OIDs sent.
6760 promisor_remote_get_direct(repo
, to_fetch
.oid
, to_fetch
.nr
);
6762 oid_array_clear(&to_fetch
);
6765 void diffcore_std(struct diff_options
*options
)
6767 int output_formats_to_prefetch
= DIFF_FORMAT_DIFFSTAT
|
6768 DIFF_FORMAT_NUMSTAT
|
6770 DIFF_FORMAT_SHORTSTAT
|
6771 DIFF_FORMAT_DIRSTAT
;
6774 * Check if the user requested a blob-data-requiring diff output and/or
6775 * break-rewrite detection (which requires blob data). If yes, prefetch
6778 * If no prefetching occurs, diffcore_rename() will prefetch if it
6779 * decides that it needs inexact rename detection.
6781 if (options
->repo
== the_repository
&& has_promisor_remote() &&
6782 (options
->output_format
& output_formats_to_prefetch
||
6783 options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
6784 diff_queued_diff_prefetch(options
->repo
);
6786 /* NOTE please keep the following in sync with diff_tree_combined() */
6787 if (options
->skip_stat_unmatch
)
6788 diffcore_skip_stat_unmatch(options
);
6789 if (!options
->found_follow
) {
6790 /* See try_to_follow_renames() in tree-diff.c */
6791 if (options
->break_opt
!= -1)
6792 diffcore_break(options
->repo
,
6793 options
->break_opt
);
6794 if (options
->detect_rename
)
6795 diffcore_rename(options
);
6796 if (options
->break_opt
!= -1)
6797 diffcore_merge_broken();
6799 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
6800 diffcore_pickaxe(options
);
6801 if (options
->orderfile
)
6802 diffcore_order(options
->orderfile
);
6803 if (options
->rotate_to
)
6804 diffcore_rotate(options
);
6805 if (!options
->found_follow
)
6806 /* See try_to_follow_renames() in tree-diff.c */
6807 diff_resolve_rename_copy();
6808 diffcore_apply_filter(options
);
6810 if (diff_queued_diff
.nr
&& !options
->flags
.diff_from_contents
)
6811 options
->flags
.has_changes
= 1;
6813 options
->flags
.has_changes
= 0;
6815 options
->found_follow
= 0;
6818 int diff_result_code(struct diff_options
*opt
, int status
)
6822 diff_warn_rename_limit("diff.renameLimit",
6823 opt
->needed_rename_limit
,
6824 opt
->degraded_cc_to_c
);
6825 if (!opt
->flags
.exit_with_status
&&
6826 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
6828 if (opt
->flags
.exit_with_status
&&
6829 opt
->flags
.has_changes
)
6831 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
6832 opt
->flags
.check_failed
)
6837 int diff_can_quit_early(struct diff_options
*opt
)
6839 return (opt
->flags
.quick
&&
6841 opt
->flags
.has_changes
);
6845 * Shall changes to this submodule be ignored?
6847 * Submodule changes can be configured to be ignored separately for each path,
6848 * but that configuration can be overridden from the command line.
6850 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
6853 struct diff_flags orig_flags
= options
->flags
;
6854 if (!options
->flags
.override_submodule_config
)
6855 set_diffopt_flags_from_submodule_config(options
, path
);
6856 if (options
->flags
.ignore_submodules
)
6858 options
->flags
= orig_flags
;
6862 void compute_diffstat(struct diff_options
*options
,
6863 struct diffstat_t
*diffstat
,
6864 struct diff_queue_struct
*q
)
6868 memset(diffstat
, 0, sizeof(struct diffstat_t
));
6869 for (i
= 0; i
< q
->nr
; i
++) {
6870 struct diff_filepair
*p
= q
->queue
[i
];
6871 if (check_pair_status(p
))
6872 diff_flush_stat(p
, options
, diffstat
);
6876 void diff_addremove(struct diff_options
*options
,
6877 int addremove
, unsigned mode
,
6878 const struct object_id
*oid
,
6880 const char *concatpath
, unsigned dirty_submodule
)
6882 struct diff_filespec
*one
, *two
;
6884 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
6887 /* This may look odd, but it is a preparation for
6888 * feeding "there are unchanged files which should
6889 * not produce diffs, but when you are doing copy
6890 * detection you would need them, so here they are"
6891 * entries to the diff-core. They will be prefixed
6892 * with something like '=' or '*' (I haven't decided
6893 * which but should not make any difference).
6894 * Feeding the same new and old to diff_change()
6895 * also has the same effect.
6896 * Before the final output happens, they are pruned after
6897 * merged into rename/copy pairs as appropriate.
6899 if (options
->flags
.reverse_diff
)
6900 addremove
= (addremove
== '+' ? '-' :
6901 addremove
== '-' ? '+' : addremove
);
6903 if (options
->prefix
&&
6904 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
6907 one
= alloc_filespec(concatpath
);
6908 two
= alloc_filespec(concatpath
);
6910 if (addremove
!= '+')
6911 fill_filespec(one
, oid
, oid_valid
, mode
);
6912 if (addremove
!= '-') {
6913 fill_filespec(two
, oid
, oid_valid
, mode
);
6914 two
->dirty_submodule
= dirty_submodule
;
6917 diff_queue(&diff_queued_diff
, one
, two
);
6918 if (!options
->flags
.diff_from_contents
)
6919 options
->flags
.has_changes
= 1;
6922 void diff_change(struct diff_options
*options
,
6923 unsigned old_mode
, unsigned new_mode
,
6924 const struct object_id
*old_oid
,
6925 const struct object_id
*new_oid
,
6926 int old_oid_valid
, int new_oid_valid
,
6927 const char *concatpath
,
6928 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
6930 struct diff_filespec
*one
, *two
;
6931 struct diff_filepair
*p
;
6933 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
6934 is_submodule_ignored(concatpath
, options
))
6937 if (options
->flags
.reverse_diff
) {
6938 SWAP(old_mode
, new_mode
);
6939 SWAP(old_oid
, new_oid
);
6940 SWAP(old_oid_valid
, new_oid_valid
);
6941 SWAP(old_dirty_submodule
, new_dirty_submodule
);
6944 if (options
->prefix
&&
6945 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
6948 one
= alloc_filespec(concatpath
);
6949 two
= alloc_filespec(concatpath
);
6950 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
6951 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
6952 one
->dirty_submodule
= old_dirty_submodule
;
6953 two
->dirty_submodule
= new_dirty_submodule
;
6954 p
= diff_queue(&diff_queued_diff
, one
, two
);
6956 if (options
->flags
.diff_from_contents
)
6959 if (options
->flags
.quick
&& options
->skip_stat_unmatch
&&
6960 !diff_filespec_check_stat_unmatch(options
->repo
, p
)) {
6961 diff_free_filespec_data(p
->one
);
6962 diff_free_filespec_data(p
->two
);
6966 options
->flags
.has_changes
= 1;
6969 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
6971 struct diff_filepair
*pair
;
6972 struct diff_filespec
*one
, *two
;
6974 if (options
->prefix
&&
6975 strncmp(path
, options
->prefix
, options
->prefix_length
))
6978 one
= alloc_filespec(path
);
6979 two
= alloc_filespec(path
);
6980 pair
= diff_queue(&diff_queued_diff
, one
, two
);
6981 pair
->is_unmerged
= 1;
6985 static char *run_textconv(struct repository
*r
,
6987 struct diff_filespec
*spec
,
6990 struct diff_tempfile
*temp
;
6991 struct child_process child
= CHILD_PROCESS_INIT
;
6992 struct strbuf buf
= STRBUF_INIT
;
6995 temp
= prepare_temp_file(r
, spec
->path
, spec
);
6996 strvec_push(&child
.args
, pgm
);
6997 strvec_push(&child
.args
, temp
->name
);
6999 child
.use_shell
= 1;
7001 if (start_command(&child
)) {
7006 if (strbuf_read(&buf
, child
.out
, 0) < 0)
7007 err
= error("error reading from textconv command '%s'", pgm
);
7010 if (finish_command(&child
) || err
) {
7011 strbuf_release(&buf
);
7017 return strbuf_detach(&buf
, outsize
);
7020 size_t fill_textconv(struct repository
*r
,
7021 struct userdiff_driver
*driver
,
7022 struct diff_filespec
*df
,
7028 if (!DIFF_FILE_VALID(df
)) {
7032 if (diff_populate_filespec(r
, df
, NULL
))
7033 die("unable to read files to diff");
7038 if (!driver
->textconv
)
7039 BUG("fill_textconv called with non-textconv driver");
7041 if (driver
->textconv_cache
&& df
->oid_valid
) {
7042 *outbuf
= notes_cache_get(driver
->textconv_cache
,
7049 *outbuf
= run_textconv(r
, driver
->textconv
, df
, &size
);
7051 die("unable to read files to diff");
7053 if (driver
->textconv_cache
&& df
->oid_valid
) {
7054 /* ignore errors, as we might be in a readonly repository */
7055 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
7058 * we could save up changes and flush them all at the end,
7059 * but we would need an extra call after all diffing is done.
7060 * Since generating a cache entry is the slow path anyway,
7061 * this extra overhead probably isn't a big deal.
7063 notes_cache_write(driver
->textconv_cache
);
7069 int textconv_object(struct repository
*r
,
7072 const struct object_id
*oid
,
7075 unsigned long *buf_size
)
7077 struct diff_filespec
*df
;
7078 struct userdiff_driver
*textconv
;
7080 df
= alloc_filespec(path
);
7081 fill_filespec(df
, oid
, oid_valid
, mode
);
7082 textconv
= get_textconv(r
, df
);
7088 *buf_size
= fill_textconv(r
, textconv
, df
, buf
);
7093 void setup_diff_pager(struct diff_options
*opt
)
7096 * If the user asked for our exit code, then either they want --quiet
7097 * or --exit-code. We should definitely not bother with a pager in the
7098 * former case, as we will generate no output. Since we still properly
7099 * report our exit code even when a pager is run, we _could_ run a
7100 * pager with --exit-code. But since we have not done so historically,
7101 * and because it is easy to find people oneline advising "git diff
7102 * --exit-code" in hooks and other scripts, we do not do so.
7104 if (!opt
->flags
.exit_with_status
&&
7105 check_pager_config("diff") != 0)