2 * Copyright (C) 2005 Junio C Hamano
15 #include "xdiff-interface.h"
18 #include "run-command.h"
20 #include "object-store.h"
22 #include "submodule-config.h"
23 #include "submodule.h"
27 #include "string-list.h"
31 #include "parse-options.h"
33 #include "promisor-remote.h"
37 #ifdef NO_FAST_WORKING_DIRECTORY
38 #define FAST_WORKING_DIRECTORY 0
40 #define FAST_WORKING_DIRECTORY 1
43 static int diff_detect_rename_default
;
44 static int diff_indent_heuristic
= 1;
45 static int diff_rename_limit_default
= 1000;
46 static int diff_suppress_blank_empty
;
47 static int diff_use_color_default
= -1;
48 static int diff_color_moved_default
;
49 static int diff_color_moved_ws_default
;
50 static int diff_context_default
= 3;
51 static int diff_interhunk_context_default
;
52 static const char *diff_word_regex_cfg
;
53 static const char *external_diff_cmd_cfg
;
54 static const char *diff_order_file_cfg
;
55 int diff_auto_refresh_index
= 1;
56 static int diff_mnemonic_prefix
;
57 static int diff_no_prefix
;
58 static int diff_relative
;
59 static int diff_stat_graph_width
;
60 static int diff_dirstat_permille_default
= 30;
61 static struct diff_options default_diff_options
;
62 static long diff_algorithm
;
63 static unsigned ws_error_highlight_default
= WSEH_NEW
;
65 static char diff_colors
[][COLOR_MAXLEN
] = {
67 GIT_COLOR_NORMAL
, /* CONTEXT */
68 GIT_COLOR_BOLD
, /* METAINFO */
69 GIT_COLOR_CYAN
, /* FRAGINFO */
70 GIT_COLOR_RED
, /* OLD */
71 GIT_COLOR_GREEN
, /* NEW */
72 GIT_COLOR_YELLOW
, /* COMMIT */
73 GIT_COLOR_BG_RED
, /* WHITESPACE */
74 GIT_COLOR_NORMAL
, /* FUNCINFO */
75 GIT_COLOR_BOLD_MAGENTA
, /* OLD_MOVED */
76 GIT_COLOR_BOLD_BLUE
, /* OLD_MOVED ALTERNATIVE */
77 GIT_COLOR_FAINT
, /* OLD_MOVED_DIM */
78 GIT_COLOR_FAINT_ITALIC
, /* OLD_MOVED_ALTERNATIVE_DIM */
79 GIT_COLOR_BOLD_CYAN
, /* NEW_MOVED */
80 GIT_COLOR_BOLD_YELLOW
, /* NEW_MOVED ALTERNATIVE */
81 GIT_COLOR_FAINT
, /* NEW_MOVED_DIM */
82 GIT_COLOR_FAINT_ITALIC
, /* NEW_MOVED_ALTERNATIVE_DIM */
83 GIT_COLOR_FAINT
, /* CONTEXT_DIM */
84 GIT_COLOR_FAINT_RED
, /* OLD_DIM */
85 GIT_COLOR_FAINT_GREEN
, /* NEW_DIM */
86 GIT_COLOR_BOLD
, /* CONTEXT_BOLD */
87 GIT_COLOR_BOLD_RED
, /* OLD_BOLD */
88 GIT_COLOR_BOLD_GREEN
, /* NEW_BOLD */
91 static const char *color_diff_slots
[] = {
92 [DIFF_CONTEXT
] = "context",
93 [DIFF_METAINFO
] = "meta",
94 [DIFF_FRAGINFO
] = "frag",
95 [DIFF_FILE_OLD
] = "old",
96 [DIFF_FILE_NEW
] = "new",
97 [DIFF_COMMIT
] = "commit",
98 [DIFF_WHITESPACE
] = "whitespace",
99 [DIFF_FUNCINFO
] = "func",
100 [DIFF_FILE_OLD_MOVED
] = "oldMoved",
101 [DIFF_FILE_OLD_MOVED_ALT
] = "oldMovedAlternative",
102 [DIFF_FILE_OLD_MOVED_DIM
] = "oldMovedDimmed",
103 [DIFF_FILE_OLD_MOVED_ALT_DIM
] = "oldMovedAlternativeDimmed",
104 [DIFF_FILE_NEW_MOVED
] = "newMoved",
105 [DIFF_FILE_NEW_MOVED_ALT
] = "newMovedAlternative",
106 [DIFF_FILE_NEW_MOVED_DIM
] = "newMovedDimmed",
107 [DIFF_FILE_NEW_MOVED_ALT_DIM
] = "newMovedAlternativeDimmed",
108 [DIFF_CONTEXT_DIM
] = "contextDimmed",
109 [DIFF_FILE_OLD_DIM
] = "oldDimmed",
110 [DIFF_FILE_NEW_DIM
] = "newDimmed",
111 [DIFF_CONTEXT_BOLD
] = "contextBold",
112 [DIFF_FILE_OLD_BOLD
] = "oldBold",
113 [DIFF_FILE_NEW_BOLD
] = "newBold",
116 define_list_config_array_extra(color_diff_slots
, {"plain"});
118 static int parse_diff_color_slot(const char *var
)
120 if (!strcasecmp(var
, "plain"))
122 return LOOKUP_CONFIG(color_diff_slots
, var
);
125 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
126 struct strbuf
*errmsg
)
128 char *params_copy
= xstrdup(params_string
);
129 struct string_list params
= STRING_LIST_INIT_NODUP
;
134 string_list_split_in_place(¶ms
, params_copy
, ',', -1);
135 for (i
= 0; i
< params
.nr
; i
++) {
136 const char *p
= params
.items
[i
].string
;
137 if (!strcmp(p
, "changes")) {
138 options
->flags
.dirstat_by_line
= 0;
139 options
->flags
.dirstat_by_file
= 0;
140 } else if (!strcmp(p
, "lines")) {
141 options
->flags
.dirstat_by_line
= 1;
142 options
->flags
.dirstat_by_file
= 0;
143 } else if (!strcmp(p
, "files")) {
144 options
->flags
.dirstat_by_line
= 0;
145 options
->flags
.dirstat_by_file
= 1;
146 } else if (!strcmp(p
, "noncumulative")) {
147 options
->flags
.dirstat_cumulative
= 0;
148 } else if (!strcmp(p
, "cumulative")) {
149 options
->flags
.dirstat_cumulative
= 1;
150 } else if (isdigit(*p
)) {
152 int permille
= strtoul(p
, &end
, 10) * 10;
153 if (*end
== '.' && isdigit(*++end
)) {
154 /* only use first digit */
155 permille
+= *end
- '0';
156 /* .. and ignore any further digits */
157 while (isdigit(*++end
))
161 options
->dirstat_permille
= permille
;
163 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
168 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
173 string_list_clear(¶ms
, 0);
178 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
180 if (!strcmp(value
, "log"))
181 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
182 else if (!strcmp(value
, "short"))
183 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
184 else if (!strcmp(value
, "diff"))
185 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
187 * Please update $__git_diff_submodule_formats in
188 * git-completion.bash when you add new formats.
195 int git_config_rename(const char *var
, const char *value
)
198 return DIFF_DETECT_RENAME
;
199 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
200 return DIFF_DETECT_COPY
;
201 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
204 long parse_algorithm_value(const char *value
)
208 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
210 else if (!strcasecmp(value
, "minimal"))
211 return XDF_NEED_MINIMAL
;
212 else if (!strcasecmp(value
, "patience"))
213 return XDF_PATIENCE_DIFF
;
214 else if (!strcasecmp(value
, "histogram"))
215 return XDF_HISTOGRAM_DIFF
;
217 * Please update $__git_diff_algorithms in git-completion.bash
218 * when you add new algorithms.
223 static int parse_one_token(const char **arg
, const char *token
)
226 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
233 static int parse_ws_error_highlight(const char *arg
)
235 const char *orig_arg
= arg
;
239 if (parse_one_token(&arg
, "none"))
241 else if (parse_one_token(&arg
, "default"))
243 else if (parse_one_token(&arg
, "all"))
244 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
245 else if (parse_one_token(&arg
, "new"))
247 else if (parse_one_token(&arg
, "old"))
249 else if (parse_one_token(&arg
, "context"))
252 return -1 - (int)(arg
- orig_arg
);
261 * These are to give UI layer defaults.
262 * The core-level commands such as git-diff-files should
263 * never be affected by the setting of diff.renames
264 * the user happens to have in the configuration file.
266 void init_diff_ui_defaults(void)
268 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
271 int git_diff_heuristic_config(const char *var
, const char *value
,
274 if (!strcmp(var
, "diff.indentheuristic"))
275 diff_indent_heuristic
= git_config_bool(var
, value
);
279 static int parse_color_moved(const char *arg
)
281 switch (git_parse_maybe_bool(arg
)) {
283 return COLOR_MOVED_NO
;
285 return COLOR_MOVED_DEFAULT
;
290 if (!strcmp(arg
, "no"))
291 return COLOR_MOVED_NO
;
292 else if (!strcmp(arg
, "plain"))
293 return COLOR_MOVED_PLAIN
;
294 else if (!strcmp(arg
, "blocks"))
295 return COLOR_MOVED_BLOCKS
;
296 else if (!strcmp(arg
, "zebra"))
297 return COLOR_MOVED_ZEBRA
;
298 else if (!strcmp(arg
, "default"))
299 return COLOR_MOVED_DEFAULT
;
300 else if (!strcmp(arg
, "dimmed-zebra"))
301 return COLOR_MOVED_ZEBRA_DIM
;
302 else if (!strcmp(arg
, "dimmed_zebra"))
303 return COLOR_MOVED_ZEBRA_DIM
;
305 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
308 static unsigned parse_color_moved_ws(const char *arg
)
311 struct string_list l
= STRING_LIST_INIT_DUP
;
312 struct string_list_item
*i
;
314 string_list_split(&l
, arg
, ',', -1);
316 for_each_string_list_item(i
, &l
) {
317 struct strbuf sb
= STRBUF_INIT
;
318 strbuf_addstr(&sb
, i
->string
);
321 if (!strcmp(sb
.buf
, "no"))
323 else if (!strcmp(sb
.buf
, "ignore-space-change"))
324 ret
|= XDF_IGNORE_WHITESPACE_CHANGE
;
325 else if (!strcmp(sb
.buf
, "ignore-space-at-eol"))
326 ret
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
327 else if (!strcmp(sb
.buf
, "ignore-all-space"))
328 ret
|= XDF_IGNORE_WHITESPACE
;
329 else if (!strcmp(sb
.buf
, "allow-indentation-change"))
330 ret
|= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
;
332 ret
|= COLOR_MOVED_WS_ERROR
;
333 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
);
339 if ((ret
& COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
) &&
340 (ret
& XDF_WHITESPACE_FLAGS
)) {
341 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
342 ret
|= COLOR_MOVED_WS_ERROR
;
345 string_list_clear(&l
, 0);
350 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
352 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
353 diff_use_color_default
= git_config_colorbool(var
, value
);
356 if (!strcmp(var
, "diff.colormoved")) {
357 int cm
= parse_color_moved(value
);
360 diff_color_moved_default
= cm
;
363 if (!strcmp(var
, "diff.colormovedws")) {
364 unsigned cm
= parse_color_moved_ws(value
);
365 if (cm
& COLOR_MOVED_WS_ERROR
)
367 diff_color_moved_ws_default
= cm
;
370 if (!strcmp(var
, "diff.context")) {
371 diff_context_default
= git_config_int(var
, value
);
372 if (diff_context_default
< 0)
376 if (!strcmp(var
, "diff.interhunkcontext")) {
377 diff_interhunk_context_default
= git_config_int(var
, value
);
378 if (diff_interhunk_context_default
< 0)
382 if (!strcmp(var
, "diff.renames")) {
383 diff_detect_rename_default
= git_config_rename(var
, value
);
386 if (!strcmp(var
, "diff.autorefreshindex")) {
387 diff_auto_refresh_index
= git_config_bool(var
, value
);
390 if (!strcmp(var
, "diff.mnemonicprefix")) {
391 diff_mnemonic_prefix
= git_config_bool(var
, value
);
394 if (!strcmp(var
, "diff.noprefix")) {
395 diff_no_prefix
= git_config_bool(var
, value
);
398 if (!strcmp(var
, "diff.relative")) {
399 diff_relative
= git_config_bool(var
, value
);
402 if (!strcmp(var
, "diff.statgraphwidth")) {
403 diff_stat_graph_width
= git_config_int(var
, value
);
406 if (!strcmp(var
, "diff.external"))
407 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
408 if (!strcmp(var
, "diff.wordregex"))
409 return git_config_string(&diff_word_regex_cfg
, var
, value
);
410 if (!strcmp(var
, "diff.orderfile"))
411 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
413 if (!strcmp(var
, "diff.ignoresubmodules"))
414 handle_ignore_submodules_arg(&default_diff_options
, value
);
416 if (!strcmp(var
, "diff.submodule")) {
417 if (parse_submodule_params(&default_diff_options
, value
))
418 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
423 if (!strcmp(var
, "diff.algorithm")) {
424 diff_algorithm
= parse_algorithm_value(value
);
425 if (diff_algorithm
< 0)
430 if (git_color_config(var
, value
, cb
) < 0)
433 return git_diff_basic_config(var
, value
, cb
);
436 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
440 if (!strcmp(var
, "diff.renamelimit")) {
441 diff_rename_limit_default
= git_config_int(var
, value
);
445 if (userdiff_config(var
, value
) < 0)
448 if (skip_prefix(var
, "diff.color.", &name
) ||
449 skip_prefix(var
, "color.diff.", &name
)) {
450 int slot
= parse_diff_color_slot(name
);
454 return config_error_nonbool(var
);
455 return color_parse(value
, diff_colors
[slot
]);
458 if (!strcmp(var
, "diff.wserrorhighlight")) {
459 int val
= parse_ws_error_highlight(value
);
462 ws_error_highlight_default
= val
;
466 /* like GNU diff's --suppress-blank-empty option */
467 if (!strcmp(var
, "diff.suppressblankempty") ||
468 /* for backwards compatibility */
469 !strcmp(var
, "diff.suppress-blank-empty")) {
470 diff_suppress_blank_empty
= git_config_bool(var
, value
);
474 if (!strcmp(var
, "diff.dirstat")) {
475 struct strbuf errmsg
= STRBUF_INIT
;
476 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
477 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
478 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
480 strbuf_release(&errmsg
);
481 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
485 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
488 return git_default_config(var
, value
, cb
);
491 static char *quote_two(const char *one
, const char *two
)
493 int need_one
= quote_c_style(one
, NULL
, NULL
, CQUOTE_NODQ
);
494 int need_two
= quote_c_style(two
, NULL
, NULL
, CQUOTE_NODQ
);
495 struct strbuf res
= STRBUF_INIT
;
497 if (need_one
+ need_two
) {
498 strbuf_addch(&res
, '"');
499 quote_c_style(one
, &res
, NULL
, CQUOTE_NODQ
);
500 quote_c_style(two
, &res
, NULL
, CQUOTE_NODQ
);
501 strbuf_addch(&res
, '"');
503 strbuf_addstr(&res
, one
);
504 strbuf_addstr(&res
, two
);
506 return strbuf_detach(&res
, NULL
);
509 static const char *external_diff(void)
511 static const char *external_diff_cmd
= NULL
;
512 static int done_preparing
= 0;
515 return external_diff_cmd
;
516 external_diff_cmd
= xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
517 if (!external_diff_cmd
)
518 external_diff_cmd
= external_diff_cmd_cfg
;
520 return external_diff_cmd
;
524 * Keep track of files used for diffing. Sometimes such an entry
525 * refers to a temporary file, sometimes to an existing file, and
526 * sometimes to "/dev/null".
528 static struct diff_tempfile
{
530 * filename external diff should read from, or NULL if this
531 * entry is currently not in use:
535 char hex
[GIT_MAX_HEXSZ
+ 1];
539 * If this diff_tempfile instance refers to a temporary file,
540 * this tempfile object is used to manage its lifetime.
542 struct tempfile
*tempfile
;
545 struct emit_callback
{
548 int blank_at_eof_in_preimage
;
549 int blank_at_eof_in_postimage
;
551 int lno_in_postimage
;
552 const char **label_path
;
553 struct diff_words_data
*diff_words
;
554 struct diff_options
*opt
;
555 struct strbuf
*header
;
558 static int count_lines(const char *data
, int size
)
560 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
567 completely_empty
= 0;
571 completely_empty
= 0;
574 if (completely_empty
)
577 count
++; /* no trailing newline */
581 static int fill_mmfile(struct repository
*r
, mmfile_t
*mf
,
582 struct diff_filespec
*one
)
584 if (!DIFF_FILE_VALID(one
)) {
585 mf
->ptr
= (char *)""; /* does not matter */
589 else if (diff_populate_filespec(r
, one
, NULL
))
593 mf
->size
= one
->size
;
597 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
598 static unsigned long diff_filespec_size(struct repository
*r
,
599 struct diff_filespec
*one
)
601 struct diff_populate_filespec_options dpf_options
= {
602 .check_size_only
= 1,
605 if (!DIFF_FILE_VALID(one
))
607 diff_populate_filespec(r
, one
, &dpf_options
);
611 static int count_trailing_blank(mmfile_t
*mf
)
614 long size
= mf
->size
;
619 ptr
+= size
- 1; /* pointing at the very end */
621 ; /* incomplete line */
623 ptr
--; /* skip the last LF */
624 while (mf
->ptr
< ptr
) {
626 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
627 if (*prev_eol
== '\n')
629 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
))
637 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
638 struct emit_callback
*ecbdata
)
641 l1
= count_trailing_blank(mf1
);
642 l2
= count_trailing_blank(mf2
);
644 ecbdata
->blank_at_eof_in_preimage
= 0;
645 ecbdata
->blank_at_eof_in_postimage
= 0;
648 at
= count_lines(mf1
->ptr
, mf1
->size
);
649 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
651 at
= count_lines(mf2
->ptr
, mf2
->size
);
652 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
655 static void emit_line_0(struct diff_options
*o
,
656 const char *set_sign
, const char *set
, unsigned reverse
, const char *reset
,
657 int first
, const char *line
, int len
)
659 int has_trailing_newline
, has_trailing_carriage_return
;
660 int needs_reset
= 0; /* at the end of the line */
661 FILE *file
= o
->file
;
663 fputs(diff_line_prefix(o
), file
);
665 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
666 if (has_trailing_newline
)
669 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
670 if (has_trailing_carriage_return
)
676 if (reverse
&& want_color(o
->use_color
)) {
677 fputs(GIT_COLOR_REVERSE
, file
);
682 fputs(set_sign
, file
);
693 if (set_sign
&& set
!= set_sign
)
698 fwrite(line
, len
, 1, file
);
699 needs_reset
= 1; /* 'line' may contain color codes. */
704 if (has_trailing_carriage_return
)
706 if (has_trailing_newline
)
710 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
711 const char *line
, int len
)
713 emit_line_0(o
, set
, NULL
, 0, reset
, 0, line
, len
);
717 DIFF_SYMBOL_BINARY_DIFF_HEADER
,
718 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
719 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
720 DIFF_SYMBOL_BINARY_DIFF_BODY
,
721 DIFF_SYMBOL_BINARY_DIFF_FOOTER
,
722 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
723 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
724 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
725 DIFF_SYMBOL_STATS_LINE
,
726 DIFF_SYMBOL_WORD_DIFF
,
727 DIFF_SYMBOL_STAT_SEP
,
729 DIFF_SYMBOL_SUBMODULE_ADD
,
730 DIFF_SYMBOL_SUBMODULE_DEL
,
731 DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
732 DIFF_SYMBOL_SUBMODULE_MODIFIED
,
733 DIFF_SYMBOL_SUBMODULE_HEADER
,
734 DIFF_SYMBOL_SUBMODULE_ERROR
,
735 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
,
736 DIFF_SYMBOL_REWRITE_DIFF
,
737 DIFF_SYMBOL_BINARY_FILES
,
739 DIFF_SYMBOL_FILEPAIR_PLUS
,
740 DIFF_SYMBOL_FILEPAIR_MINUS
,
741 DIFF_SYMBOL_WORDS_PORCELAIN
,
744 DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
747 DIFF_SYMBOL_NO_LF_EOF
,
748 DIFF_SYMBOL_CONTEXT_FRAGINFO
,
749 DIFF_SYMBOL_CONTEXT_MARKER
,
750 DIFF_SYMBOL_SEPARATOR
753 * Flags for content lines:
754 * 0..12 are whitespace rules
755 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
756 * 16 is marking if the line is blank at EOF
758 #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
759 #define DIFF_SYMBOL_MOVED_LINE (1<<17)
760 #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
761 #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
762 #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
765 * This struct is used when we need to buffer the output of the diff output.
767 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
768 * into the pre/post image file. This pointer could be a union with the
769 * line pointer. By storing an offset into the file instead of the literal line,
770 * we can decrease the memory footprint for the buffered output. At first we
771 * may want to only have indirection for the content lines, but we could also
772 * enhance the state for emitting prefabricated lines, e.g. the similarity
773 * score line or hunk/file headers would only need to store a number or path
774 * and then the output can be constructed later on depending on state.
776 struct emitted_diff_symbol
{
780 int indent_off
; /* Offset to first non-whitespace character */
781 int indent_width
; /* The visual width of the indentation */
785 #define EMITTED_DIFF_SYMBOL_INIT { 0 }
787 struct emitted_diff_symbols
{
788 struct emitted_diff_symbol
*buf
;
791 #define EMITTED_DIFF_SYMBOLS_INIT { 0 }
793 static void append_emitted_diff_symbol(struct diff_options
*o
,
794 struct emitted_diff_symbol
*e
)
796 struct emitted_diff_symbol
*f
;
798 ALLOC_GROW(o
->emitted_symbols
->buf
,
799 o
->emitted_symbols
->nr
+ 1,
800 o
->emitted_symbols
->alloc
);
801 f
= &o
->emitted_symbols
->buf
[o
->emitted_symbols
->nr
++];
803 memcpy(f
, e
, sizeof(struct emitted_diff_symbol
));
804 f
->line
= e
->line
? xmemdupz(e
->line
, e
->len
) : NULL
;
807 static void free_emitted_diff_symbols(struct emitted_diff_symbols
*e
)
816 const struct emitted_diff_symbol
*es
;
817 struct moved_entry
*next_line
;
818 struct moved_entry
*next_match
;
822 struct moved_entry
*match
;
823 int wsd
; /* The whitespace delta of this block */
826 #define INDENT_BLANKLINE INT_MIN
828 static void fill_es_indent_data(struct emitted_diff_symbol
*es
)
830 unsigned int off
= 0, i
;
831 int width
= 0, tab_width
= es
->flags
& WS_TAB_WIDTH_MASK
;
832 const char *s
= es
->line
;
833 const int len
= es
->len
;
835 /* skip any \v \f \r at start of indentation */
836 while (s
[off
] == '\f' || s
[off
] == '\v' ||
837 (s
[off
] == '\r' && off
< len
- 1))
840 /* calculate the visual width of indentation */
845 } else if (s
[off
] == '\t') {
846 width
+= tab_width
- (width
% tab_width
);
847 while (s
[++off
] == '\t')
854 /* check if this line is blank */
855 for (i
= off
; i
< len
; i
++)
860 es
->indent_width
= INDENT_BLANKLINE
;
861 es
->indent_off
= len
;
863 es
->indent_off
= off
;
864 es
->indent_width
= width
;
868 static int compute_ws_delta(const struct emitted_diff_symbol
*a
,
869 const struct emitted_diff_symbol
*b
)
871 int a_width
= a
->indent_width
,
872 b_width
= b
->indent_width
;
874 if (a_width
== INDENT_BLANKLINE
&& b_width
== INDENT_BLANKLINE
)
875 return INDENT_BLANKLINE
;
877 return a_width
- b_width
;
880 static int cmp_in_block_with_wsd(const struct moved_entry
*cur
,
881 const struct emitted_diff_symbol
*l
,
882 struct moved_block
*pmb
)
884 int a_width
= cur
->es
->indent_width
, b_width
= l
->indent_width
;
887 /* The text of each line must match */
888 if (cur
->es
->id
!= l
->id
)
892 * If 'l' and 'cur' are both blank then we don't need to check the
893 * indent. We only need to check cur as we know the strings match.
895 if (a_width
== INDENT_BLANKLINE
)
899 * The indent changes of the block are known and stored in pmb->wsd;
900 * however we need to check if the indent changes of the current line
901 * match those of the current block.
903 delta
= b_width
- a_width
;
906 * If the previous lines of this block were all blank then set its
909 if (pmb
->wsd
== INDENT_BLANKLINE
)
912 return delta
!= pmb
->wsd
;
915 struct interned_diff_symbol
{
916 struct hashmap_entry ent
;
917 struct emitted_diff_symbol
*es
;
920 static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data
,
921 const struct hashmap_entry
*eptr
,
922 const struct hashmap_entry
*entry_or_key
,
923 const void *keydata UNUSED
)
925 const struct diff_options
*diffopt
= hashmap_cmp_fn_data
;
926 const struct emitted_diff_symbol
*a
, *b
;
927 unsigned flags
= diffopt
->color_moved_ws_handling
928 & XDF_WHITESPACE_FLAGS
;
930 a
= container_of(eptr
, const struct interned_diff_symbol
, ent
)->es
;
931 b
= container_of(entry_or_key
, const struct interned_diff_symbol
, ent
)->es
;
933 return !xdiff_compare_lines(a
->line
+ a
->indent_off
,
934 a
->len
- a
->indent_off
,
935 b
->line
+ b
->indent_off
,
936 b
->len
- b
->indent_off
, flags
);
939 static void prepare_entry(struct diff_options
*o
, struct emitted_diff_symbol
*l
,
940 struct interned_diff_symbol
*s
)
942 unsigned flags
= o
->color_moved_ws_handling
& XDF_WHITESPACE_FLAGS
;
943 unsigned int hash
= xdiff_hash_string(l
->line
+ l
->indent_off
,
944 l
->len
- l
->indent_off
, flags
);
946 hashmap_entry_init(&s
->ent
, hash
);
950 struct moved_entry_list
{
951 struct moved_entry
*add
, *del
;
954 static struct moved_entry_list
*add_lines_to_move_detection(struct diff_options
*o
,
955 struct mem_pool
*entry_mem_pool
)
957 struct moved_entry
*prev_line
= NULL
;
958 struct mem_pool interned_pool
;
959 struct hashmap interned_map
;
960 struct moved_entry_list
*entry_list
= NULL
;
961 size_t entry_list_alloc
= 0;
965 hashmap_init(&interned_map
, interned_diff_symbol_cmp
, o
, 8096);
966 mem_pool_init(&interned_pool
, 1024 * 1024);
968 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
969 struct interned_diff_symbol key
;
970 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
971 struct interned_diff_symbol
*s
;
972 struct moved_entry
*entry
;
974 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
) {
979 if (o
->color_moved_ws_handling
&
980 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
981 fill_es_indent_data(l
);
983 prepare_entry(o
, l
, &key
);
984 s
= hashmap_get_entry(&interned_map
, &key
, ent
, &key
.ent
);
989 ALLOC_GROW_BY(entry_list
, id
, 1, entry_list_alloc
);
990 hashmap_add(&interned_map
,
991 memcpy(mem_pool_alloc(&interned_pool
,
995 entry
= mem_pool_alloc(entry_mem_pool
, sizeof(*entry
));
997 entry
->next_line
= NULL
;
998 if (prev_line
&& prev_line
->es
->s
== l
->s
)
999 prev_line
->next_line
= entry
;
1001 if (l
->s
== DIFF_SYMBOL_PLUS
) {
1002 entry
->next_match
= entry_list
[l
->id
].add
;
1003 entry_list
[l
->id
].add
= entry
;
1005 entry
->next_match
= entry_list
[l
->id
].del
;
1006 entry_list
[l
->id
].del
= entry
;
1010 hashmap_clear(&interned_map
);
1011 mem_pool_discard(&interned_pool
, 0);
1016 static void pmb_advance_or_null(struct diff_options
*o
,
1017 struct emitted_diff_symbol
*l
,
1018 struct moved_block
*pmb
,
1023 for (i
= 0, j
= 0; i
< *pmb_nr
; i
++) {
1025 struct moved_entry
*prev
= pmb
[i
].match
;
1026 struct moved_entry
*cur
= (prev
&& prev
->next_line
) ?
1027 prev
->next_line
: NULL
;
1029 if (o
->color_moved_ws_handling
&
1030 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1032 !cmp_in_block_with_wsd(cur
, l
, &pmb
[i
]);
1034 match
= cur
&& cur
->es
->id
== l
->id
;
1038 pmb
[j
++].match
= cur
;
1044 static void fill_potential_moved_blocks(struct diff_options
*o
,
1045 struct moved_entry
*match
,
1046 struct emitted_diff_symbol
*l
,
1047 struct moved_block
**pmb_p
,
1048 int *pmb_alloc_p
, int *pmb_nr_p
)
1051 struct moved_block
*pmb
= *pmb_p
;
1052 int pmb_alloc
= *pmb_alloc_p
, pmb_nr
= *pmb_nr_p
;
1055 * The current line is the start of a new block.
1056 * Setup the set of potential blocks.
1058 for (; match
; match
= match
->next_match
) {
1059 ALLOC_GROW(pmb
, pmb_nr
+ 1, pmb_alloc
);
1060 if (o
->color_moved_ws_handling
&
1061 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1062 pmb
[pmb_nr
].wsd
= compute_ws_delta(l
, match
->es
);
1064 pmb
[pmb_nr
].wsd
= 0;
1065 pmb
[pmb_nr
++].match
= match
;
1069 *pmb_alloc_p
= pmb_alloc
;
1074 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1076 * Otherwise, if the last block has fewer alphanumeric characters than
1077 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
1080 * The last block consists of the (n - block_length)'th line up to but not
1081 * including the nth line.
1083 * Returns 0 if the last block is empty or is unset by this function, non zero
1086 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1087 * Think of a way to unify them.
1089 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1090 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
1091 static int adjust_last_block(struct diff_options
*o
, int n
, int block_length
)
1093 int i
, alnum_count
= 0;
1094 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
1095 return block_length
;
1096 for (i
= 1; i
< block_length
+ 1; i
++) {
1097 const char *c
= o
->emitted_symbols
->buf
[n
- i
].line
;
1102 if (alnum_count
>= COLOR_MOVED_MIN_ALNUM_COUNT
)
1106 for (i
= 1; i
< block_length
+ 1; i
++)
1107 o
->emitted_symbols
->buf
[n
- i
].flags
&= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
;
1111 /* Find blocks of moved code, delegate actual coloring decision to helper */
1112 static void mark_color_as_moved(struct diff_options
*o
,
1113 struct moved_entry_list
*entry_list
)
1115 struct moved_block
*pmb
= NULL
; /* potentially moved blocks */
1116 int pmb_nr
= 0, pmb_alloc
= 0;
1117 int n
, flipped_block
= 0, block_length
= 0;
1118 enum diff_symbol moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1121 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1122 struct moved_entry
*match
= NULL
;
1123 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1126 case DIFF_SYMBOL_PLUS
:
1127 match
= entry_list
[l
->id
].del
;
1129 case DIFF_SYMBOL_MINUS
:
1130 match
= entry_list
[l
->id
].add
;
1136 if (pmb_nr
&& (!match
|| l
->s
!= moved_symbol
)) {
1137 if (!adjust_last_block(o
, n
, block_length
) &&
1140 * Rewind in case there is another match
1141 * starting at the second line of the block
1151 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1155 if (o
->color_moved
== COLOR_MOVED_PLAIN
) {
1156 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1160 pmb_advance_or_null(o
, l
, pmb
, &pmb_nr
);
1163 int contiguous
= adjust_last_block(o
, n
, block_length
);
1165 if (!contiguous
&& block_length
> 1)
1167 * Rewind in case there is another match
1168 * starting at the second line of the block
1172 fill_potential_moved_blocks(o
, match
, l
,
1176 if (contiguous
&& pmb_nr
&& moved_symbol
== l
->s
)
1177 flipped_block
= (flipped_block
+ 1) % 2;
1182 moved_symbol
= l
->s
;
1184 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1191 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1192 if (flipped_block
&& o
->color_moved
!= COLOR_MOVED_BLOCKS
)
1193 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_ALT
;
1196 adjust_last_block(o
, n
, block_length
);
1201 static void dim_moved_lines(struct diff_options
*o
)
1204 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1205 struct emitted_diff_symbol
*prev
= (n
!= 0) ?
1206 &o
->emitted_symbols
->buf
[n
- 1] : NULL
;
1207 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1208 struct emitted_diff_symbol
*next
=
1209 (n
< o
->emitted_symbols
->nr
- 1) ?
1210 &o
->emitted_symbols
->buf
[n
+ 1] : NULL
;
1212 /* Not a plus or minus line? */
1213 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
)
1216 /* Not a moved line? */
1217 if (!(l
->flags
& DIFF_SYMBOL_MOVED_LINE
))
1221 * If prev or next are not a plus or minus line,
1222 * pretend they don't exist
1224 if (prev
&& prev
->s
!= DIFF_SYMBOL_PLUS
&&
1225 prev
->s
!= DIFF_SYMBOL_MINUS
)
1227 if (next
&& next
->s
!= DIFF_SYMBOL_PLUS
&&
1228 next
->s
!= DIFF_SYMBOL_MINUS
)
1231 /* Inside a block? */
1233 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1234 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
)) &&
1236 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1237 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
))) {
1238 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1242 /* Check if we are at an interesting bound: */
1243 if (prev
&& (prev
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1244 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1245 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1247 if (next
&& (next
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1248 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1249 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1253 * The boundary to prev and next are not interesting,
1254 * so this line is not interesting as a whole
1256 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1260 static void emit_line_ws_markup(struct diff_options
*o
,
1261 const char *set_sign
, const char *set
,
1263 int sign_index
, const char *line
, int len
,
1264 unsigned ws_rule
, int blank_at_eof
)
1266 const char *ws
= NULL
;
1267 int sign
= o
->output_indicators
[sign_index
];
1269 if (o
->ws_error_highlight
& ws_rule
) {
1270 ws
= diff_get_color_opt(o
, DIFF_WHITESPACE
);
1275 if (!ws
&& !set_sign
)
1276 emit_line_0(o
, set
, NULL
, 0, reset
, sign
, line
, len
);
1278 emit_line_0(o
, set_sign
, set
, !!set_sign
, reset
, sign
, line
, len
);
1279 } else if (blank_at_eof
)
1280 /* Blank line at EOF - paint '+' as well */
1281 emit_line_0(o
, ws
, NULL
, 0, reset
, sign
, line
, len
);
1283 /* Emit just the prefix, then the rest. */
1284 emit_line_0(o
, set_sign
? set_sign
: set
, NULL
, !!set_sign
, reset
,
1286 ws_check_emit(line
, len
, ws_rule
,
1287 o
->file
, set
, reset
, ws
);
1291 static void emit_diff_symbol_from_struct(struct diff_options
*o
,
1292 struct emitted_diff_symbol
*eds
)
1294 static const char *nneof
= " No newline at end of file\n";
1295 const char *context
, *reset
, *set
, *set_sign
, *meta
, *fraginfo
;
1297 enum diff_symbol s
= eds
->s
;
1298 const char *line
= eds
->line
;
1300 unsigned flags
= eds
->flags
;
1303 case DIFF_SYMBOL_NO_LF_EOF
:
1304 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1305 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1306 putc('\n', o
->file
);
1307 emit_line_0(o
, context
, NULL
, 0, reset
, '\\',
1308 nneof
, strlen(nneof
));
1310 case DIFF_SYMBOL_SUBMODULE_HEADER
:
1311 case DIFF_SYMBOL_SUBMODULE_ERROR
:
1312 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
:
1313 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
:
1314 case DIFF_SYMBOL_SUMMARY
:
1315 case DIFF_SYMBOL_STATS_LINE
:
1316 case DIFF_SYMBOL_BINARY_DIFF_BODY
:
1317 case DIFF_SYMBOL_CONTEXT_FRAGINFO
:
1318 emit_line(o
, "", "", line
, len
);
1320 case DIFF_SYMBOL_CONTEXT_INCOMPLETE
:
1321 case DIFF_SYMBOL_CONTEXT_MARKER
:
1322 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1323 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1324 emit_line(o
, context
, reset
, line
, len
);
1326 case DIFF_SYMBOL_SEPARATOR
:
1327 fprintf(o
->file
, "%s%c",
1328 diff_line_prefix(o
),
1329 o
->line_termination
);
1331 case DIFF_SYMBOL_CONTEXT
:
1332 set
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1333 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1335 if (o
->flags
.dual_color_diffed_diffs
) {
1336 char c
= !len
? 0 : line
[0];
1339 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1341 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1343 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1345 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1346 OUTPUT_INDICATOR_CONTEXT
, line
, len
,
1347 flags
& (DIFF_SYMBOL_CONTENT_WS_MASK
), 0);
1349 case DIFF_SYMBOL_PLUS
:
1350 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1351 DIFF_SYMBOL_MOVED_LINE_ALT
|
1352 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1353 case DIFF_SYMBOL_MOVED_LINE
|
1354 DIFF_SYMBOL_MOVED_LINE_ALT
|
1355 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1356 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT_DIM
);
1358 case DIFF_SYMBOL_MOVED_LINE
|
1359 DIFF_SYMBOL_MOVED_LINE_ALT
:
1360 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT
);
1362 case DIFF_SYMBOL_MOVED_LINE
|
1363 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1364 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_DIM
);
1366 case DIFF_SYMBOL_MOVED_LINE
:
1367 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED
);
1370 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1372 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1373 if (!o
->flags
.dual_color_diffed_diffs
)
1376 char c
= !len
? 0 : line
[0];
1380 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_BOLD
);
1382 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1384 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_BOLD
);
1386 set
= diff_get_color_opt(o
, DIFF_CONTEXT_BOLD
);
1387 flags
&= ~DIFF_SYMBOL_CONTENT_WS_MASK
;
1389 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1390 OUTPUT_INDICATOR_NEW
, line
, len
,
1391 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
,
1392 flags
& DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
);
1394 case DIFF_SYMBOL_MINUS
:
1395 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1396 DIFF_SYMBOL_MOVED_LINE_ALT
|
1397 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1398 case DIFF_SYMBOL_MOVED_LINE
|
1399 DIFF_SYMBOL_MOVED_LINE_ALT
|
1400 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1401 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT_DIM
);
1403 case DIFF_SYMBOL_MOVED_LINE
|
1404 DIFF_SYMBOL_MOVED_LINE_ALT
:
1405 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT
);
1407 case DIFF_SYMBOL_MOVED_LINE
|
1408 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1409 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_DIM
);
1411 case DIFF_SYMBOL_MOVED_LINE
:
1412 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED
);
1415 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1417 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1418 if (!o
->flags
.dual_color_diffed_diffs
)
1421 char c
= !len
? 0 : line
[0];
1425 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_DIM
);
1427 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1429 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_DIM
);
1431 set
= diff_get_color_opt(o
, DIFF_CONTEXT_DIM
);
1433 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1434 OUTPUT_INDICATOR_OLD
, line
, len
,
1435 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
, 0);
1437 case DIFF_SYMBOL_WORDS_PORCELAIN
:
1438 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1439 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1440 emit_line(o
, context
, reset
, line
, len
);
1441 fputs("~\n", o
->file
);
1443 case DIFF_SYMBOL_WORDS
:
1444 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1445 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1447 * Skip the prefix character, if any. With
1448 * diff_suppress_blank_empty, there may be
1451 if (line
[0] != '\n') {
1455 emit_line(o
, context
, reset
, line
, len
);
1457 case DIFF_SYMBOL_FILEPAIR_PLUS
:
1458 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1459 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1460 fprintf(o
->file
, "%s%s+++ %s%s%s\n", diff_line_prefix(o
), meta
,
1462 strchr(line
, ' ') ? "\t" : "");
1464 case DIFF_SYMBOL_FILEPAIR_MINUS
:
1465 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1466 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1467 fprintf(o
->file
, "%s%s--- %s%s%s\n", diff_line_prefix(o
), meta
,
1469 strchr(line
, ' ') ? "\t" : "");
1471 case DIFF_SYMBOL_BINARY_FILES
:
1472 case DIFF_SYMBOL_HEADER
:
1473 fprintf(o
->file
, "%s", line
);
1475 case DIFF_SYMBOL_BINARY_DIFF_HEADER
:
1476 fprintf(o
->file
, "%sGIT binary patch\n", diff_line_prefix(o
));
1478 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
:
1479 fprintf(o
->file
, "%sdelta %s\n", diff_line_prefix(o
), line
);
1481 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
:
1482 fprintf(o
->file
, "%sliteral %s\n", diff_line_prefix(o
), line
);
1484 case DIFF_SYMBOL_BINARY_DIFF_FOOTER
:
1485 fputs(diff_line_prefix(o
), o
->file
);
1486 fputc('\n', o
->file
);
1488 case DIFF_SYMBOL_REWRITE_DIFF
:
1489 fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
1490 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1491 emit_line(o
, fraginfo
, reset
, line
, len
);
1493 case DIFF_SYMBOL_SUBMODULE_ADD
:
1494 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1495 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1496 emit_line(o
, set
, reset
, line
, len
);
1498 case DIFF_SYMBOL_SUBMODULE_DEL
:
1499 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1500 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1501 emit_line(o
, set
, reset
, line
, len
);
1503 case DIFF_SYMBOL_SUBMODULE_UNTRACKED
:
1504 fprintf(o
->file
, "%sSubmodule %s contains untracked content\n",
1505 diff_line_prefix(o
), line
);
1507 case DIFF_SYMBOL_SUBMODULE_MODIFIED
:
1508 fprintf(o
->file
, "%sSubmodule %s contains modified content\n",
1509 diff_line_prefix(o
), line
);
1511 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
:
1512 emit_line(o
, "", "", " 0 files changed\n",
1513 strlen(" 0 files changed\n"));
1515 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV
:
1516 emit_line(o
, "", "", " ...\n", strlen(" ...\n"));
1518 case DIFF_SYMBOL_WORD_DIFF
:
1519 fprintf(o
->file
, "%.*s", len
, line
);
1521 case DIFF_SYMBOL_STAT_SEP
:
1522 fputs(o
->stat_sep
, o
->file
);
1525 BUG("unknown diff symbol");
1529 static void emit_diff_symbol(struct diff_options
*o
, enum diff_symbol s
,
1530 const char *line
, int len
, unsigned flags
)
1532 struct emitted_diff_symbol e
= {
1533 .line
= line
, .len
= len
, .flags
= flags
, .s
= s
1536 if (o
->emitted_symbols
)
1537 append_emitted_diff_symbol(o
, &e
);
1539 emit_diff_symbol_from_struct(o
, &e
);
1542 void diff_emit_submodule_del(struct diff_options
*o
, const char *line
)
1544 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_DEL
, line
, strlen(line
), 0);
1547 void diff_emit_submodule_add(struct diff_options
*o
, const char *line
)
1549 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ADD
, line
, strlen(line
), 0);
1552 void diff_emit_submodule_untracked(struct diff_options
*o
, const char *path
)
1554 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
1555 path
, strlen(path
), 0);
1558 void diff_emit_submodule_modified(struct diff_options
*o
, const char *path
)
1560 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_MODIFIED
,
1561 path
, strlen(path
), 0);
1564 void diff_emit_submodule_header(struct diff_options
*o
, const char *header
)
1566 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_HEADER
,
1567 header
, strlen(header
), 0);
1570 void diff_emit_submodule_error(struct diff_options
*o
, const char *err
)
1572 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ERROR
, err
, strlen(err
), 0);
1575 void diff_emit_submodule_pipethrough(struct diff_options
*o
,
1576 const char *line
, int len
)
1578 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
, line
, len
, 0);
1581 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
1583 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
1584 ecbdata
->blank_at_eof_in_preimage
&&
1585 ecbdata
->blank_at_eof_in_postimage
&&
1586 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
1587 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
1589 return ws_blank_line(line
, len
);
1592 static void emit_add_line(struct emit_callback
*ecbdata
,
1593 const char *line
, int len
)
1595 unsigned flags
= WSEH_NEW
| ecbdata
->ws_rule
;
1596 if (new_blank_line_at_eof(ecbdata
, line
, len
))
1597 flags
|= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
;
1599 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_PLUS
, line
, len
, flags
);
1602 static void emit_del_line(struct emit_callback
*ecbdata
,
1603 const char *line
, int len
)
1605 unsigned flags
= WSEH_OLD
| ecbdata
->ws_rule
;
1606 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_MINUS
, line
, len
, flags
);
1609 static void emit_context_line(struct emit_callback
*ecbdata
,
1610 const char *line
, int len
)
1612 unsigned flags
= WSEH_CONTEXT
| ecbdata
->ws_rule
;
1613 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_CONTEXT
, line
, len
, flags
);
1616 static void emit_hunk_header(struct emit_callback
*ecbdata
,
1617 const char *line
, int len
)
1619 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1620 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
1621 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
1622 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1623 const char *reverse
= ecbdata
->color_diff
? GIT_COLOR_REVERSE
: "";
1624 static const char atat
[2] = { '@', '@' };
1625 const char *cp
, *ep
;
1626 struct strbuf msgbuf
= STRBUF_INIT
;
1631 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1632 * it always is at least 10 bytes long.
1635 memcmp(line
, atat
, 2) ||
1636 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
1637 emit_diff_symbol(ecbdata
->opt
,
1638 DIFF_SYMBOL_CONTEXT_MARKER
, line
, len
, 0);
1641 ep
+= 2; /* skip over @@ */
1643 /* The hunk header in fraginfo color */
1644 if (ecbdata
->opt
->flags
.dual_color_diffed_diffs
)
1645 strbuf_addstr(&msgbuf
, reverse
);
1646 strbuf_addstr(&msgbuf
, frag
);
1647 if (ecbdata
->opt
->flags
.suppress_hunk_header_line_count
)
1648 strbuf_add(&msgbuf
, atat
, sizeof(atat
));
1650 strbuf_add(&msgbuf
, line
, ep
- line
);
1651 strbuf_addstr(&msgbuf
, reset
);
1657 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
1660 /* blank before the func header */
1661 for (cp
= ep
; ep
- line
< len
; ep
++)
1662 if (*ep
!= ' ' && *ep
!= '\t')
1665 strbuf_addstr(&msgbuf
, context
);
1666 strbuf_add(&msgbuf
, cp
, ep
- cp
);
1667 strbuf_addstr(&msgbuf
, reset
);
1670 if (ep
< line
+ len
) {
1671 strbuf_addstr(&msgbuf
, func
);
1672 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
1673 strbuf_addstr(&msgbuf
, reset
);
1676 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
1677 strbuf_complete_line(&msgbuf
);
1678 emit_diff_symbol(ecbdata
->opt
,
1679 DIFF_SYMBOL_CONTEXT_FRAGINFO
, msgbuf
.buf
, msgbuf
.len
, 0);
1680 strbuf_release(&msgbuf
);
1683 static struct diff_tempfile
*claim_diff_tempfile(void)
1686 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
1687 if (!diff_temp
[i
].name
)
1688 return diff_temp
+ i
;
1689 BUG("diff is failing to clean up its tempfiles");
1692 static void remove_tempfile(void)
1695 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
1696 if (is_tempfile_active(diff_temp
[i
].tempfile
))
1697 delete_tempfile(&diff_temp
[i
].tempfile
);
1698 diff_temp
[i
].name
= NULL
;
1702 static void add_line_count(struct strbuf
*out
, int count
)
1706 strbuf_addstr(out
, "0,0");
1709 strbuf_addstr(out
, "1");
1712 strbuf_addf(out
, "1,%d", count
);
1717 static void emit_rewrite_lines(struct emit_callback
*ecb
,
1718 int prefix
, const char *data
, int size
)
1720 const char *endp
= NULL
;
1725 endp
= memchr(data
, '\n', size
);
1726 len
= endp
? (endp
- data
+ 1) : size
;
1727 if (prefix
!= '+') {
1728 ecb
->lno_in_preimage
++;
1729 emit_del_line(ecb
, data
, len
);
1731 ecb
->lno_in_postimage
++;
1732 emit_add_line(ecb
, data
, len
);
1738 emit_diff_symbol(ecb
->opt
, DIFF_SYMBOL_NO_LF_EOF
, NULL
, 0, 0);
1741 static void emit_rewrite_diff(const char *name_a
,
1743 struct diff_filespec
*one
,
1744 struct diff_filespec
*two
,
1745 struct userdiff_driver
*textconv_one
,
1746 struct userdiff_driver
*textconv_two
,
1747 struct diff_options
*o
)
1750 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
1751 const char *a_prefix
, *b_prefix
;
1752 char *data_one
, *data_two
;
1753 size_t size_one
, size_two
;
1754 struct emit_callback ecbdata
;
1755 struct strbuf out
= STRBUF_INIT
;
1757 if (diff_mnemonic_prefix
&& o
->flags
.reverse_diff
) {
1758 a_prefix
= o
->b_prefix
;
1759 b_prefix
= o
->a_prefix
;
1761 a_prefix
= o
->a_prefix
;
1762 b_prefix
= o
->b_prefix
;
1765 name_a
+= (*name_a
== '/');
1766 name_b
+= (*name_b
== '/');
1768 strbuf_reset(&a_name
);
1769 strbuf_reset(&b_name
);
1770 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
1771 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
1773 size_one
= fill_textconv(o
->repo
, textconv_one
, one
, &data_one
);
1774 size_two
= fill_textconv(o
->repo
, textconv_two
, two
, &data_two
);
1776 memset(&ecbdata
, 0, sizeof(ecbdata
));
1777 ecbdata
.color_diff
= want_color(o
->use_color
);
1778 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
1780 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
1782 mf1
.ptr
= (char *)data_one
;
1783 mf2
.ptr
= (char *)data_two
;
1784 mf1
.size
= size_one
;
1785 mf2
.size
= size_two
;
1786 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1788 ecbdata
.lno_in_preimage
= 1;
1789 ecbdata
.lno_in_postimage
= 1;
1791 lc_a
= count_lines(data_one
, size_one
);
1792 lc_b
= count_lines(data_two
, size_two
);
1794 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1795 a_name
.buf
, a_name
.len
, 0);
1796 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1797 b_name
.buf
, b_name
.len
, 0);
1799 strbuf_addstr(&out
, "@@ -");
1800 if (!o
->irreversible_delete
)
1801 add_line_count(&out
, lc_a
);
1803 strbuf_addstr(&out
, "?,?");
1804 strbuf_addstr(&out
, " +");
1805 add_line_count(&out
, lc_b
);
1806 strbuf_addstr(&out
, " @@\n");
1807 emit_diff_symbol(o
, DIFF_SYMBOL_REWRITE_DIFF
, out
.buf
, out
.len
, 0);
1808 strbuf_release(&out
);
1810 if (lc_a
&& !o
->irreversible_delete
)
1811 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
1813 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
1815 free((char *)data_one
);
1817 free((char *)data_two
);
1820 struct diff_words_buffer
{
1822 unsigned long alloc
;
1823 struct diff_words_orig
{
1824 const char *begin
, *end
;
1826 int orig_nr
, orig_alloc
;
1829 static void diff_words_append(char *line
, unsigned long len
,
1830 struct diff_words_buffer
*buffer
)
1832 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
1835 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
1836 buffer
->text
.size
+= len
;
1837 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
1840 struct diff_words_style_elem
{
1843 const char *color
; /* NULL; filled in by the setup code if
1844 * color is enabled */
1847 struct diff_words_style
{
1848 enum diff_words_type type
;
1849 struct diff_words_style_elem new_word
, old_word
, ctx
;
1850 const char *newline
;
1853 static struct diff_words_style diff_words_styles
[] = {
1854 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1855 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1856 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
1859 struct diff_words_data
{
1860 struct diff_words_buffer minus
, plus
;
1861 const char *current_plus
;
1863 struct diff_options
*opt
;
1864 regex_t
*word_regex
;
1865 enum diff_words_type type
;
1866 struct diff_words_style
*style
;
1869 static int fn_out_diff_words_write_helper(struct diff_options
*o
,
1870 struct diff_words_style_elem
*st_el
,
1871 const char *newline
,
1872 size_t count
, const char *buf
)
1875 struct strbuf sb
= STRBUF_INIT
;
1878 char *p
= memchr(buf
, '\n', count
);
1880 strbuf_addstr(&sb
, diff_line_prefix(o
));
1883 const char *reset
= st_el
->color
&& *st_el
->color
?
1884 GIT_COLOR_RESET
: NULL
;
1885 if (st_el
->color
&& *st_el
->color
)
1886 strbuf_addstr(&sb
, st_el
->color
);
1887 strbuf_addstr(&sb
, st_el
->prefix
);
1888 strbuf_add(&sb
, buf
, p
? p
- buf
: count
);
1889 strbuf_addstr(&sb
, st_el
->suffix
);
1891 strbuf_addstr(&sb
, reset
);
1896 strbuf_addstr(&sb
, newline
);
1897 count
-= p
+ 1 - buf
;
1901 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1909 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1911 strbuf_release(&sb
);
1916 * '--color-words' algorithm can be described as:
1918 * 1. collect the minus/plus lines of a diff hunk, divided into
1919 * minus-lines and plus-lines;
1921 * 2. break both minus-lines and plus-lines into words and
1922 * place them into two mmfile_t with one word for each line;
1924 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1926 * And for the common parts of the both file, we output the plus side text.
1927 * diff_words->current_plus is used to trace the current position of the plus file
1928 * which printed. diff_words->last_minus is used to trace the last minus word
1931 * For '--graph' to work with '--color-words', we need to output the graph prefix
1932 * on each line of color words output. Generally, there are two conditions on
1933 * which we should output the prefix.
1935 * 1. diff_words->last_minus == 0 &&
1936 * diff_words->current_plus == diff_words->plus.text.ptr
1938 * that is: the plus text must start as a new line, and if there is no minus
1939 * word printed, a graph prefix must be printed.
1941 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1942 * *(diff_words->current_plus - 1) == '\n'
1944 * that is: a graph prefix must be printed following a '\n'
1946 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
1948 if ((diff_words
->last_minus
== 0 &&
1949 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
1950 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
1951 *(diff_words
->current_plus
- 1) == '\n')) {
1958 static void fn_out_diff_words_aux(void *priv
,
1959 long minus_first
, long minus_len
,
1960 long plus_first
, long plus_len
,
1961 const char *func UNUSED
, long funclen UNUSED
)
1963 struct diff_words_data
*diff_words
= priv
;
1964 struct diff_words_style
*style
= diff_words
->style
;
1965 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
1966 struct diff_options
*opt
= diff_words
->opt
;
1967 const char *line_prefix
;
1970 line_prefix
= diff_line_prefix(opt
);
1972 /* POSIX requires that first be decremented by one if len == 0... */
1974 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
1976 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
1978 minus_begin
= minus_end
=
1979 diff_words
->minus
.orig
[minus_first
].end
;
1982 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
1983 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
1985 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
1987 if (color_words_output_graph_prefix(diff_words
)) {
1988 fputs(line_prefix
, diff_words
->opt
->file
);
1990 if (diff_words
->current_plus
!= plus_begin
) {
1991 fn_out_diff_words_write_helper(diff_words
->opt
,
1992 &style
->ctx
, style
->newline
,
1993 plus_begin
- diff_words
->current_plus
,
1994 diff_words
->current_plus
);
1996 if (minus_begin
!= minus_end
) {
1997 fn_out_diff_words_write_helper(diff_words
->opt
,
1998 &style
->old_word
, style
->newline
,
1999 minus_end
- minus_begin
, minus_begin
);
2001 if (plus_begin
!= plus_end
) {
2002 fn_out_diff_words_write_helper(diff_words
->opt
,
2003 &style
->new_word
, style
->newline
,
2004 plus_end
- plus_begin
, plus_begin
);
2007 diff_words
->current_plus
= plus_end
;
2008 diff_words
->last_minus
= minus_first
;
2011 /* This function starts looking at *begin, and returns 0 iff a word was found. */
2012 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
2013 int *begin
, int *end
)
2015 while (word_regex
&& *begin
< buffer
->size
) {
2016 regmatch_t match
[1];
2017 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
2018 buffer
->size
- *begin
, 1, match
, 0)) {
2019 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
2020 '\n', match
[0].rm_eo
- match
[0].rm_so
);
2021 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
2022 *begin
+= match
[0].rm_so
;
2026 return *begin
> *end
;
2032 /* find the next word */
2033 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
2035 if (*begin
>= buffer
->size
)
2038 /* find the end of the word */
2040 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
2047 * This function splits the words in buffer->text, stores the list with
2048 * newline separator into out, and saves the offsets of the original words
2051 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
2052 regex_t
*word_regex
)
2060 /* fake an empty "0th" word */
2061 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
2062 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
2063 buffer
->orig_nr
= 1;
2065 for (i
= 0; i
< buffer
->text
.size
; i
++) {
2066 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
2069 /* store original boundaries */
2070 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
2071 buffer
->orig_alloc
);
2072 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
2073 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
2076 /* store one word */
2077 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
2078 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
2079 out
->ptr
[out
->size
+ j
- i
] = '\n';
2080 out
->size
+= j
- i
+ 1;
2086 /* this executes the word diff on the accumulated buffers */
2087 static void diff_words_show(struct diff_words_data
*diff_words
)
2091 mmfile_t minus
, plus
;
2092 struct diff_words_style
*style
= diff_words
->style
;
2094 struct diff_options
*opt
= diff_words
->opt
;
2095 const char *line_prefix
;
2098 line_prefix
= diff_line_prefix(opt
);
2100 /* special case: only removal */
2101 if (!diff_words
->plus
.text
.size
) {
2102 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2103 line_prefix
, strlen(line_prefix
), 0);
2104 fn_out_diff_words_write_helper(diff_words
->opt
,
2105 &style
->old_word
, style
->newline
,
2106 diff_words
->minus
.text
.size
,
2107 diff_words
->minus
.text
.ptr
);
2108 diff_words
->minus
.text
.size
= 0;
2112 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
2113 diff_words
->last_minus
= 0;
2115 memset(&xpp
, 0, sizeof(xpp
));
2116 memset(&xecfg
, 0, sizeof(xecfg
));
2117 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
2118 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
2120 /* as only the hunk header will be parsed, we need a 0-context */
2122 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, NULL
,
2123 diff_words
, &xpp
, &xecfg
))
2124 die("unable to generate word diff");
2127 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
2128 diff_words
->plus
.text
.size
) {
2129 if (color_words_output_graph_prefix(diff_words
))
2130 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2131 line_prefix
, strlen(line_prefix
), 0);
2132 fn_out_diff_words_write_helper(diff_words
->opt
,
2133 &style
->ctx
, style
->newline
,
2134 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
2135 - diff_words
->current_plus
, diff_words
->current_plus
);
2137 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
2140 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
2141 static void diff_words_flush(struct emit_callback
*ecbdata
)
2143 struct diff_options
*wo
= ecbdata
->diff_words
->opt
;
2145 if (ecbdata
->diff_words
->minus
.text
.size
||
2146 ecbdata
->diff_words
->plus
.text
.size
)
2147 diff_words_show(ecbdata
->diff_words
);
2149 if (wo
->emitted_symbols
) {
2150 struct diff_options
*o
= ecbdata
->opt
;
2151 struct emitted_diff_symbols
*wol
= wo
->emitted_symbols
;
2156 * Instead of appending each, concat all words to a line?
2158 for (i
= 0; i
< wol
->nr
; i
++)
2159 append_emitted_diff_symbol(o
, &wol
->buf
[i
]);
2161 for (i
= 0; i
< wol
->nr
; i
++)
2162 free((void *)wol
->buf
[i
].line
);
2168 static void diff_filespec_load_driver(struct diff_filespec
*one
,
2169 struct index_state
*istate
)
2171 /* Use already-loaded driver */
2175 if (S_ISREG(one
->mode
))
2176 one
->driver
= userdiff_find_by_path(istate
, one
->path
);
2178 /* Fallback to default settings */
2180 one
->driver
= userdiff_find_by_name("default");
2183 static const char *userdiff_word_regex(struct diff_filespec
*one
,
2184 struct index_state
*istate
)
2186 diff_filespec_load_driver(one
, istate
);
2187 return one
->driver
->word_regex
;
2190 static void init_diff_words_data(struct emit_callback
*ecbdata
,
2191 struct diff_options
*orig_opts
,
2192 struct diff_filespec
*one
,
2193 struct diff_filespec
*two
)
2196 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
2197 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
2199 CALLOC_ARRAY(ecbdata
->diff_words
, 1);
2200 ecbdata
->diff_words
->type
= o
->word_diff
;
2201 ecbdata
->diff_words
->opt
= o
;
2203 if (orig_opts
->emitted_symbols
)
2204 CALLOC_ARRAY(o
->emitted_symbols
, 1);
2207 o
->word_regex
= userdiff_word_regex(one
, o
->repo
->index
);
2209 o
->word_regex
= userdiff_word_regex(two
, o
->repo
->index
);
2211 o
->word_regex
= diff_word_regex_cfg
;
2212 if (o
->word_regex
) {
2213 ecbdata
->diff_words
->word_regex
= (regex_t
*)
2214 xmalloc(sizeof(regex_t
));
2215 if (regcomp(ecbdata
->diff_words
->word_regex
,
2217 REG_EXTENDED
| REG_NEWLINE
))
2218 die("invalid regular expression: %s",
2221 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
2222 if (o
->word_diff
== diff_words_styles
[i
].type
) {
2223 ecbdata
->diff_words
->style
=
2224 &diff_words_styles
[i
];
2228 if (want_color(o
->use_color
)) {
2229 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
2230 st
->old_word
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2231 st
->new_word
.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2232 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
2236 static void free_diff_words_data(struct emit_callback
*ecbdata
)
2238 if (ecbdata
->diff_words
) {
2239 diff_words_flush(ecbdata
);
2240 free_emitted_diff_symbols(ecbdata
->diff_words
->opt
->emitted_symbols
);
2241 free (ecbdata
->diff_words
->opt
);
2242 free (ecbdata
->diff_words
->minus
.text
.ptr
);
2243 free (ecbdata
->diff_words
->minus
.orig
);
2244 free (ecbdata
->diff_words
->plus
.text
.ptr
);
2245 free (ecbdata
->diff_words
->plus
.orig
);
2246 if (ecbdata
->diff_words
->word_regex
) {
2247 regfree(ecbdata
->diff_words
->word_regex
);
2248 free(ecbdata
->diff_words
->word_regex
);
2250 FREE_AND_NULL(ecbdata
->diff_words
);
2254 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
2256 if (want_color(diff_use_color
))
2257 return diff_colors
[ix
];
2261 const char *diff_line_prefix(struct diff_options
*opt
)
2263 struct strbuf
*msgbuf
;
2264 if (!opt
->output_prefix
)
2267 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
2271 static unsigned long sane_truncate_line(char *line
, unsigned long len
)
2274 unsigned long allot
;
2280 (void) utf8_width(&cp
, &l
);
2282 break; /* truncated in the middle? */
2287 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
2290 ecbdata
->lno_in_preimage
= 0;
2291 ecbdata
->lno_in_postimage
= 0;
2292 p
= strchr(line
, '-');
2294 return; /* cannot happen */
2295 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
2298 return; /* cannot happen */
2299 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
2302 static int fn_out_consume(void *priv
, char *line
, unsigned long len
)
2304 struct emit_callback
*ecbdata
= priv
;
2305 struct diff_options
*o
= ecbdata
->opt
;
2307 o
->found_changes
= 1;
2309 if (ecbdata
->header
) {
2310 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
2311 ecbdata
->header
->buf
, ecbdata
->header
->len
, 0);
2312 strbuf_reset(ecbdata
->header
);
2313 ecbdata
->header
= NULL
;
2316 if (ecbdata
->label_path
[0]) {
2317 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
2318 ecbdata
->label_path
[0],
2319 strlen(ecbdata
->label_path
[0]), 0);
2320 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
2321 ecbdata
->label_path
[1],
2322 strlen(ecbdata
->label_path
[1]), 0);
2323 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
2326 if (diff_suppress_blank_empty
2327 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
2332 if (line
[0] == '@') {
2333 if (ecbdata
->diff_words
)
2334 diff_words_flush(ecbdata
);
2335 len
= sane_truncate_line(line
, len
);
2336 find_lno(line
, ecbdata
);
2337 emit_hunk_header(ecbdata
, line
, len
);
2341 if (ecbdata
->diff_words
) {
2342 enum diff_symbol s
=
2343 ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
?
2344 DIFF_SYMBOL_WORDS_PORCELAIN
: DIFF_SYMBOL_WORDS
;
2345 if (line
[0] == '-') {
2346 diff_words_append(line
, len
,
2347 &ecbdata
->diff_words
->minus
);
2349 } else if (line
[0] == '+') {
2350 diff_words_append(line
, len
,
2351 &ecbdata
->diff_words
->plus
);
2353 } else if (starts_with(line
, "\\ ")) {
2355 * Eat the "no newline at eof" marker as if we
2356 * saw a "+" or "-" line with nothing on it,
2357 * and return without diff_words_flush() to
2358 * defer processing. If this is the end of
2359 * preimage, more "+" lines may come after it.
2363 diff_words_flush(ecbdata
);
2364 emit_diff_symbol(o
, s
, line
, len
, 0);
2370 ecbdata
->lno_in_postimage
++;
2371 emit_add_line(ecbdata
, line
+ 1, len
- 1);
2374 ecbdata
->lno_in_preimage
++;
2375 emit_del_line(ecbdata
, line
+ 1, len
- 1);
2378 ecbdata
->lno_in_postimage
++;
2379 ecbdata
->lno_in_preimage
++;
2380 emit_context_line(ecbdata
, line
+ 1, len
- 1);
2383 /* incomplete line at the end */
2384 ecbdata
->lno_in_preimage
++;
2385 emit_diff_symbol(o
, DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
2392 static void pprint_rename(struct strbuf
*name
, const char *a
, const char *b
)
2394 const char *old_name
= a
;
2395 const char *new_name
= b
;
2396 int pfx_length
, sfx_length
;
2397 int pfx_adjust_for_slash
;
2398 int len_a
= strlen(a
);
2399 int len_b
= strlen(b
);
2400 int a_midlen
, b_midlen
;
2401 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
2402 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
2404 if (qlen_a
|| qlen_b
) {
2405 quote_c_style(a
, name
, NULL
, 0);
2406 strbuf_addstr(name
, " => ");
2407 quote_c_style(b
, name
, NULL
, 0);
2411 /* Find common prefix */
2413 while (*old_name
&& *new_name
&& *old_name
== *new_name
) {
2414 if (*old_name
== '/')
2415 pfx_length
= old_name
- a
+ 1;
2420 /* Find common suffix */
2421 old_name
= a
+ len_a
;
2422 new_name
= b
+ len_b
;
2425 * If there is a common prefix, it must end in a slash. In
2426 * that case we let this loop run 1 into the prefix to see the
2429 * If there is no common prefix, we cannot do this as it would
2430 * underrun the input strings.
2432 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
2433 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old_name
&&
2434 b
+ pfx_length
- pfx_adjust_for_slash
<= new_name
&&
2435 *old_name
== *new_name
) {
2436 if (*old_name
== '/')
2437 sfx_length
= len_a
- (old_name
- a
);
2443 * pfx{mid-a => mid-b}sfx
2444 * {pfx-a => pfx-b}sfx
2445 * pfx{sfx-a => sfx-b}
2448 a_midlen
= len_a
- pfx_length
- sfx_length
;
2449 b_midlen
= len_b
- pfx_length
- sfx_length
;
2455 strbuf_grow(name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
2456 if (pfx_length
+ sfx_length
) {
2457 strbuf_add(name
, a
, pfx_length
);
2458 strbuf_addch(name
, '{');
2460 strbuf_add(name
, a
+ pfx_length
, a_midlen
);
2461 strbuf_addstr(name
, " => ");
2462 strbuf_add(name
, b
+ pfx_length
, b_midlen
);
2463 if (pfx_length
+ sfx_length
) {
2464 strbuf_addch(name
, '}');
2465 strbuf_add(name
, a
+ len_a
- sfx_length
, sfx_length
);
2469 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
2473 struct diffstat_file
*x
;
2475 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
2476 diffstat
->files
[diffstat
->nr
++] = x
;
2478 x
->from_name
= xstrdup(name_a
);
2479 x
->name
= xstrdup(name_b
);
2483 x
->from_name
= NULL
;
2484 x
->name
= xstrdup(name_a
);
2489 static int diffstat_consume(void *priv
, char *line
, unsigned long len
)
2491 struct diffstat_t
*diffstat
= priv
;
2492 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
2495 BUG("xdiff fed us an empty line");
2499 else if (line
[0] == '-')
2504 const char mime_boundary_leader
[] = "------------";
2506 static int scale_linear(int it
, int width
, int max_change
)
2511 * make sure that at least one '-' or '+' is printed if
2512 * there is any change to this path. The easiest way is to
2513 * scale linearly as if the allotted width is one column shorter
2514 * than it is, and then add 1 to the result.
2516 return 1 + (it
* (width
- 1) / max_change
);
2519 static void show_graph(struct strbuf
*out
, char ch
, int cnt
,
2520 const char *set
, const char *reset
)
2524 strbuf_addstr(out
, set
);
2525 strbuf_addchars(out
, ch
, cnt
);
2526 strbuf_addstr(out
, reset
);
2529 static void fill_print_name(struct diffstat_file
*file
)
2531 struct strbuf pname
= STRBUF_INIT
;
2533 if (file
->print_name
)
2536 if (file
->is_renamed
)
2537 pprint_rename(&pname
, file
->from_name
, file
->name
);
2539 quote_c_style(file
->name
, &pname
, NULL
, 0);
2542 strbuf_addf(&pname
, " (%s)", file
->comments
);
2544 file
->print_name
= strbuf_detach(&pname
, NULL
);
2547 static void print_stat_summary_inserts_deletes(struct diff_options
*options
,
2548 int files
, int insertions
, int deletions
)
2550 struct strbuf sb
= STRBUF_INIT
;
2553 assert(insertions
== 0 && deletions
== 0);
2554 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
2560 (files
== 1) ? " %d file changed" : " %d files changed",
2564 * For binary diff, the caller may want to print "x files
2565 * changed" with insertions == 0 && deletions == 0.
2567 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2568 * is probably less confusing (i.e skip over "2 files changed
2569 * but nothing about added/removed lines? Is this a bug in Git?").
2571 if (insertions
|| deletions
== 0) {
2573 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2577 if (deletions
|| insertions
== 0) {
2579 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2582 strbuf_addch(&sb
, '\n');
2583 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
2585 strbuf_release(&sb
);
2588 void print_stat_summary(FILE *fp
, int files
,
2589 int insertions
, int deletions
)
2591 struct diff_options o
;
2592 memset(&o
, 0, sizeof(o
));
2595 print_stat_summary_inserts_deletes(&o
, files
, insertions
, deletions
);
2598 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
2600 int i
, len
, add
, del
, adds
= 0, dels
= 0;
2601 uintmax_t max_change
= 0, max_len
= 0;
2602 int total_files
= data
->nr
, count
;
2603 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
2604 const char *reset
, *add_c
, *del_c
;
2605 int extra_shown
= 0;
2606 const char *line_prefix
= diff_line_prefix(options
);
2607 struct strbuf out
= STRBUF_INIT
;
2612 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
2614 reset
= diff_get_color_opt(options
, DIFF_RESET
);
2615 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
2616 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
2619 * Find the longest filename and max number of changes
2621 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
2622 struct diffstat_file
*file
= data
->files
[i
];
2623 uintmax_t change
= file
->added
+ file
->deleted
;
2625 if (!file
->is_interesting
&& (change
== 0)) {
2626 count
++; /* not shown == room for one more */
2629 fill_print_name(file
);
2630 len
= utf8_strwidth(file
->print_name
);
2634 if (file
->is_unmerged
) {
2635 /* "Unmerged" is 8 characters */
2636 bin_width
= bin_width
< 8 ? 8 : bin_width
;
2639 if (file
->is_binary
) {
2640 /* "Bin XXX -> YYY bytes" */
2641 int w
= 14 + decimal_width(file
->added
)
2642 + decimal_width(file
->deleted
);
2643 bin_width
= bin_width
< w
? w
: bin_width
;
2644 /* Display change counts aligned with "Bin" */
2649 if (max_change
< change
)
2650 max_change
= change
;
2652 count
= i
; /* where we can stop scanning in data->files[] */
2655 * We have width = stat_width or term_columns() columns total.
2656 * We want a maximum of min(max_len, stat_name_width) for the name part.
2657 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2658 * We also need 1 for " " and 4 + decimal_width(max_change)
2659 * for " | NNNN " and one the empty column at the end, altogether
2660 * 6 + decimal_width(max_change).
2662 * If there's not enough space, we will use the smaller of
2663 * stat_name_width (if set) and 5/8*width for the filename,
2664 * and the rest for constant elements + graph part, but no more
2665 * than stat_graph_width for the graph part.
2666 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2667 * for the standard terminal size).
2669 * In other words: stat_width limits the maximum width, and
2670 * stat_name_width fixes the maximum width of the filename,
2671 * and is also used to divide available columns if there
2674 * Binary files are displayed with "Bin XXX -> YYY bytes"
2675 * instead of the change count and graph. This part is treated
2676 * similarly to the graph part, except that it is not
2677 * "scaled". If total width is too small to accommodate the
2678 * guaranteed minimum width of the filename part and the
2679 * separators and this message, this message will "overflow"
2680 * making the line longer than the maximum width.
2684 * NEEDSWORK: line_prefix is often used for "log --graph" output
2685 * and contains ANSI-colored string. utf8_strnwidth() should be
2686 * used to correctly count the display width instead of strlen().
2688 if (options
->stat_width
== -1)
2689 width
= term_columns() - strlen(line_prefix
);
2691 width
= options
->stat_width
? options
->stat_width
: 80;
2692 number_width
= decimal_width(max_change
) > number_width
?
2693 decimal_width(max_change
) : number_width
;
2695 if (options
->stat_graph_width
== -1)
2696 options
->stat_graph_width
= diff_stat_graph_width
;
2699 * Guarantee 3/8*16==6 for the graph part
2700 * and 5/8*16==10 for the filename part
2702 if (width
< 16 + 6 + number_width
)
2703 width
= 16 + 6 + number_width
;
2706 * First assign sizes that are wanted, ignoring available width.
2707 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2708 * starting from "XXX" should fit in graph_width.
2710 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
2711 if (options
->stat_graph_width
&&
2712 options
->stat_graph_width
< graph_width
)
2713 graph_width
= options
->stat_graph_width
;
2715 name_width
= (options
->stat_name_width
> 0 &&
2716 options
->stat_name_width
< max_len
) ?
2717 options
->stat_name_width
: max_len
;
2720 * Adjust adjustable widths not to exceed maximum width
2722 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
2723 if (graph_width
> width
* 3/8 - number_width
- 6) {
2724 graph_width
= width
* 3/8 - number_width
- 6;
2725 if (graph_width
< 6)
2729 if (options
->stat_graph_width
&&
2730 graph_width
> options
->stat_graph_width
)
2731 graph_width
= options
->stat_graph_width
;
2732 if (name_width
> width
- number_width
- 6 - graph_width
)
2733 name_width
= width
- number_width
- 6 - graph_width
;
2735 graph_width
= width
- number_width
- 6 - name_width
;
2739 * From here name_width is the width of the name area,
2740 * and graph_width is the width of the graph area.
2741 * max_change is used to scale graph properly.
2743 for (i
= 0; i
< count
; i
++) {
2744 const char *prefix
= "";
2745 struct diffstat_file
*file
= data
->files
[i
];
2746 char *name
= file
->print_name
;
2747 uintmax_t added
= file
->added
;
2748 uintmax_t deleted
= file
->deleted
;
2749 int name_len
, padding
;
2751 if (!file
->is_interesting
&& (added
+ deleted
== 0))
2755 * "scale" the filename
2758 name_len
= utf8_strwidth(name
);
2759 if (name_width
< name_len
) {
2764 * NEEDSWORK: (name_len - len) counts the display
2765 * width, which would be shorter than the byte
2766 * length of the corresponding substring.
2767 * Advancing "name" by that number of bytes does
2768 * *NOT* skip over that many columns, so it is
2769 * very likely that chomping the pathname at the
2770 * slash we will find starting from "name" will
2771 * leave the resulting string still too long.
2773 name
+= name_len
- len
;
2774 slash
= strchr(name
, '/');
2778 padding
= len
- utf8_strwidth(name
);
2782 if (file
->is_binary
) {
2783 strbuf_addf(&out
, " %s%s%*s | %*s",
2784 prefix
, name
, padding
, "",
2785 number_width
, "Bin");
2786 if (!added
&& !deleted
) {
2787 strbuf_addch(&out
, '\n');
2788 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2789 out
.buf
, out
.len
, 0);
2793 strbuf_addf(&out
, " %s%"PRIuMAX
"%s",
2794 del_c
, deleted
, reset
);
2795 strbuf_addstr(&out
, " -> ");
2796 strbuf_addf(&out
, "%s%"PRIuMAX
"%s",
2797 add_c
, added
, reset
);
2798 strbuf_addstr(&out
, " bytes\n");
2799 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2800 out
.buf
, out
.len
, 0);
2804 else if (file
->is_unmerged
) {
2805 strbuf_addf(&out
, " %s%s%*s | %*s",
2806 prefix
, name
, padding
, "",
2807 number_width
, "Unmerged\n");
2808 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2809 out
.buf
, out
.len
, 0);
2815 * scale the add/delete
2820 if (graph_width
<= max_change
) {
2821 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
2822 if (total
< 2 && add
&& del
)
2823 /* width >= 2 due to the sanity check */
2826 add
= scale_linear(add
, graph_width
, max_change
);
2829 del
= scale_linear(del
, graph_width
, max_change
);
2833 strbuf_addf(&out
, " %s%s%*s | %*"PRIuMAX
"%s",
2834 prefix
, name
, padding
, "",
2835 number_width
, added
+ deleted
,
2836 added
+ deleted
? " " : "");
2837 show_graph(&out
, '+', add
, add_c
, reset
);
2838 show_graph(&out
, '-', del
, del_c
, reset
);
2839 strbuf_addch(&out
, '\n');
2840 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2841 out
.buf
, out
.len
, 0);
2845 for (i
= 0; i
< data
->nr
; i
++) {
2846 struct diffstat_file
*file
= data
->files
[i
];
2847 uintmax_t added
= file
->added
;
2848 uintmax_t deleted
= file
->deleted
;
2850 if (file
->is_unmerged
||
2851 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
2856 if (!file
->is_binary
) {
2863 emit_diff_symbol(options
,
2864 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
2869 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2870 strbuf_release(&out
);
2873 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
2875 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
2880 for (i
= 0; i
< data
->nr
; i
++) {
2881 int added
= data
->files
[i
]->added
;
2882 int deleted
= data
->files
[i
]->deleted
;
2884 if (data
->files
[i
]->is_unmerged
||
2885 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
2887 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
2892 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2895 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
2902 for (i
= 0; i
< data
->nr
; i
++) {
2903 struct diffstat_file
*file
= data
->files
[i
];
2905 fprintf(options
->file
, "%s", diff_line_prefix(options
));
2907 if (file
->is_binary
)
2908 fprintf(options
->file
, "-\t-\t");
2910 fprintf(options
->file
,
2911 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
2912 file
->added
, file
->deleted
);
2913 if (options
->line_termination
) {
2914 fill_print_name(file
);
2915 if (!file
->is_renamed
)
2916 write_name_quoted(file
->name
, options
->file
,
2917 options
->line_termination
);
2919 fputs(file
->print_name
, options
->file
);
2920 putc(options
->line_termination
, options
->file
);
2923 if (file
->is_renamed
) {
2924 putc('\0', options
->file
);
2925 write_name_quoted(file
->from_name
, options
->file
, '\0');
2927 write_name_quoted(file
->name
, options
->file
, '\0');
2932 struct dirstat_file
{
2934 unsigned long changed
;
2937 struct dirstat_dir
{
2938 struct dirstat_file
*files
;
2939 int alloc
, nr
, permille
, cumulative
;
2942 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
2943 unsigned long changed
, const char *base
, int baselen
)
2945 unsigned long sum_changes
= 0;
2946 unsigned int sources
= 0;
2947 const char *line_prefix
= diff_line_prefix(opt
);
2950 struct dirstat_file
*f
= dir
->files
;
2951 int namelen
= strlen(f
->name
);
2952 unsigned long changes
;
2955 if (namelen
< baselen
)
2957 if (memcmp(f
->name
, base
, baselen
))
2959 slash
= strchr(f
->name
+ baselen
, '/');
2961 int newbaselen
= slash
+ 1 - f
->name
;
2962 changes
= gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
2965 changes
= f
->changed
;
2970 sum_changes
+= changes
;
2974 * We don't report dirstat's for
2976 * - or cases where everything came from a single directory
2977 * under this directory (sources == 1).
2979 if (baselen
&& sources
!= 1) {
2981 int permille
= sum_changes
* 1000 / changed
;
2982 if (permille
>= dir
->permille
) {
2983 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
2984 permille
/ 10, permille
% 10, baselen
, base
);
2985 if (!dir
->cumulative
)
2993 static int dirstat_compare(const void *_a
, const void *_b
)
2995 const struct dirstat_file
*a
= _a
;
2996 const struct dirstat_file
*b
= _b
;
2997 return strcmp(a
->name
, b
->name
);
3000 static void show_dirstat(struct diff_options
*options
)
3003 unsigned long changed
;
3004 struct dirstat_dir dir
;
3005 struct diff_queue_struct
*q
= &diff_queued_diff
;
3010 dir
.permille
= options
->dirstat_permille
;
3011 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3014 for (i
= 0; i
< q
->nr
; i
++) {
3015 struct diff_filepair
*p
= q
->queue
[i
];
3017 unsigned long copied
, added
, damage
;
3018 struct diff_populate_filespec_options dpf_options
= {
3019 .check_size_only
= 1,
3022 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
3024 if (p
->one
->oid_valid
&& p
->two
->oid_valid
&&
3025 oideq(&p
->one
->oid
, &p
->two
->oid
)) {
3027 * The SHA1 has not changed, so pre-/post-content is
3028 * identical. We can therefore skip looking at the
3029 * file contents altogether.
3035 if (options
->flags
.dirstat_by_file
) {
3037 * In --dirstat-by-file mode, we don't really need to
3038 * look at the actual file contents at all.
3039 * The fact that the SHA1 changed is enough for us to
3040 * add this file to the list of results
3041 * (with each file contributing equal damage).
3047 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
3048 diff_populate_filespec(options
->repo
, p
->one
, NULL
);
3049 diff_populate_filespec(options
->repo
, p
->two
, NULL
);
3050 diffcore_count_changes(options
->repo
,
3051 p
->one
, p
->two
, NULL
, NULL
,
3053 diff_free_filespec_data(p
->one
);
3054 diff_free_filespec_data(p
->two
);
3055 } else if (DIFF_FILE_VALID(p
->one
)) {
3056 diff_populate_filespec(options
->repo
, p
->one
, &dpf_options
);
3058 diff_free_filespec_data(p
->one
);
3059 } else if (DIFF_FILE_VALID(p
->two
)) {
3060 diff_populate_filespec(options
->repo
, p
->two
, &dpf_options
);
3062 added
= p
->two
->size
;
3063 diff_free_filespec_data(p
->two
);
3068 * Original minus copied is the removed material,
3069 * added is the new material. They are both damages
3070 * made to the preimage.
3071 * If the resulting damage is zero, we know that
3072 * diffcore_count_changes() considers the two entries to
3073 * be identical, but since the oid changed, we
3074 * know that there must have been _some_ kind of change,
3075 * so we force all entries to have damage > 0.
3077 damage
= (p
->one
->size
- copied
) + added
;
3082 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3083 dir
.files
[dir
.nr
].name
= name
;
3084 dir
.files
[dir
.nr
].changed
= damage
;
3089 /* This can happen even with many files, if everything was renames */
3093 /* Show all directories with more than x% of the changes */
3094 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
3095 gather_dirstat(options
, &dir
, changed
, "", 0);
3098 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
3101 unsigned long changed
;
3102 struct dirstat_dir dir
;
3110 dir
.permille
= options
->dirstat_permille
;
3111 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3114 for (i
= 0; i
< data
->nr
; i
++) {
3115 struct diffstat_file
*file
= data
->files
[i
];
3116 unsigned long damage
= file
->added
+ file
->deleted
;
3117 if (file
->is_binary
)
3119 * binary files counts bytes, not lines. Must find some
3120 * way to normalize binary bytes vs. textual lines.
3121 * The following heuristic assumes that there are 64
3123 * This is stupid and ugly, but very cheap...
3125 damage
= DIV_ROUND_UP(damage
, 64);
3126 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3127 dir
.files
[dir
.nr
].name
= file
->name
;
3128 dir
.files
[dir
.nr
].changed
= damage
;
3133 /* This can happen even with many files, if everything was renames */
3137 /* Show all directories with more than x% of the changes */
3138 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
3139 gather_dirstat(options
, &dir
, changed
, "", 0);
3142 static void free_diffstat_file(struct diffstat_file
*f
)
3144 free(f
->print_name
);
3150 void free_diffstat_info(struct diffstat_t
*diffstat
)
3153 for (i
= 0; i
< diffstat
->nr
; i
++)
3154 free_diffstat_file(diffstat
->files
[i
]);
3155 free(diffstat
->files
);
3158 struct checkdiff_t
{
3159 const char *filename
;
3161 int conflict_marker_size
;
3162 struct diff_options
*o
;
3167 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
3172 if (len
< marker_size
+ 1)
3174 firstchar
= line
[0];
3175 switch (firstchar
) {
3176 case '=': case '>': case '<': case '|':
3181 for (cnt
= 1; cnt
< marker_size
; cnt
++)
3182 if (line
[cnt
] != firstchar
)
3184 /* line[1] through line[marker_size-1] are same as firstchar */
3185 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
3190 static void checkdiff_consume_hunk(void *priv
,
3191 long ob UNUSED
, long on UNUSED
,
3192 long nb
, long nn UNUSED
,
3193 const char *func UNUSED
, long funclen UNUSED
)
3196 struct checkdiff_t
*data
= priv
;
3197 data
->lineno
= nb
- 1;
3200 static int checkdiff_consume(void *priv
, char *line
, unsigned long len
)
3202 struct checkdiff_t
*data
= priv
;
3203 int marker_size
= data
->conflict_marker_size
;
3204 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
3205 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
3206 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
3208 const char *line_prefix
;
3211 line_prefix
= diff_line_prefix(data
->o
);
3213 if (line
[0] == '+') {
3216 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
3218 fprintf(data
->o
->file
,
3219 "%s%s:%d: leftover conflict marker\n",
3220 line_prefix
, data
->filename
, data
->lineno
);
3222 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
3225 data
->status
|= bad
;
3226 err
= whitespace_error_string(bad
);
3227 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
3228 line_prefix
, data
->filename
, data
->lineno
, err
);
3230 emit_line(data
->o
, set
, reset
, line
, 1);
3231 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
3232 data
->o
->file
, set
, reset
, ws
);
3233 } else if (line
[0] == ' ') {
3239 static unsigned char *deflate_it(char *data
,
3241 unsigned long *result_size
)
3244 unsigned char *deflated
;
3247 git_deflate_init(&stream
, zlib_compression_level
);
3248 bound
= git_deflate_bound(&stream
, size
);
3249 deflated
= xmalloc(bound
);
3250 stream
.next_out
= deflated
;
3251 stream
.avail_out
= bound
;
3253 stream
.next_in
= (unsigned char *)data
;
3254 stream
.avail_in
= size
;
3255 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
3257 git_deflate_end(&stream
);
3258 *result_size
= stream
.total_out
;
3262 static void emit_binary_diff_body(struct diff_options
*o
,
3263 mmfile_t
*one
, mmfile_t
*two
)
3269 unsigned long orig_size
;
3270 unsigned long delta_size
;
3271 unsigned long deflate_size
;
3272 unsigned long data_size
;
3274 /* We could do deflated delta, or we could do just deflated two,
3275 * whichever is smaller.
3278 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
3279 if (one
->size
&& two
->size
) {
3280 delta
= diff_delta(one
->ptr
, one
->size
,
3281 two
->ptr
, two
->size
,
3282 &delta_size
, deflate_size
);
3284 void *to_free
= delta
;
3285 orig_size
= delta_size
;
3286 delta
= deflate_it(delta
, delta_size
, &delta_size
);
3291 if (delta
&& delta_size
< deflate_size
) {
3292 char *s
= xstrfmt("%"PRIuMAX
, (uintmax_t)orig_size
);
3293 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
3298 data_size
= delta_size
;
3300 char *s
= xstrfmt("%lu", two
->size
);
3301 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
3306 data_size
= deflate_size
;
3309 /* emit data encoded in base85 */
3313 int bytes
= (52 < data_size
) ? 52 : data_size
;
3317 line
[0] = bytes
+ 'A' - 1;
3319 line
[0] = bytes
- 26 + 'a' - 1;
3320 encode_85(line
+ 1, cp
, bytes
);
3321 cp
= (char *) cp
+ bytes
;
3327 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_BODY
,
3330 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_FOOTER
, NULL
, 0, 0);
3334 static void emit_binary_diff(struct diff_options
*o
,
3335 mmfile_t
*one
, mmfile_t
*two
)
3337 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER
, NULL
, 0, 0);
3338 emit_binary_diff_body(o
, one
, two
);
3339 emit_binary_diff_body(o
, two
, one
);
3342 int diff_filespec_is_binary(struct repository
*r
,
3343 struct diff_filespec
*one
)
3345 struct diff_populate_filespec_options dpf_options
= {
3349 if (one
->is_binary
== -1) {
3350 diff_filespec_load_driver(one
, r
->index
);
3351 if (one
->driver
->binary
!= -1)
3352 one
->is_binary
= one
->driver
->binary
;
3354 if (!one
->data
&& DIFF_FILE_VALID(one
))
3355 diff_populate_filespec(r
, one
, &dpf_options
);
3356 if (one
->is_binary
== -1 && one
->data
)
3357 one
->is_binary
= buffer_is_binary(one
->data
,
3359 if (one
->is_binary
== -1)
3363 return one
->is_binary
;
3366 static const struct userdiff_funcname
*
3367 diff_funcname_pattern(struct diff_options
*o
, struct diff_filespec
*one
)
3369 diff_filespec_load_driver(one
, o
->repo
->index
);
3370 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
3373 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
3375 if (!options
->a_prefix
)
3376 options
->a_prefix
= a
;
3377 if (!options
->b_prefix
)
3378 options
->b_prefix
= b
;
3381 struct userdiff_driver
*get_textconv(struct repository
*r
,
3382 struct diff_filespec
*one
)
3384 if (!DIFF_FILE_VALID(one
))
3387 diff_filespec_load_driver(one
, r
->index
);
3388 return userdiff_get_textconv(r
, one
->driver
);
3391 static struct string_list
*additional_headers(struct diff_options
*o
,
3394 if (!o
->additional_path_headers
)
3396 return strmap_get(o
->additional_path_headers
, path
);
3399 static void add_formatted_header(struct strbuf
*msg
,
3401 const char *line_prefix
,
3405 const char *next
, *newline
;
3407 for (next
= header
; *next
; next
= newline
) {
3408 newline
= strchrnul(next
, '\n');
3409 strbuf_addf(msg
, "%s%s%.*s%s\n", line_prefix
, meta
,
3410 (int)(newline
- next
), next
, reset
);
3416 static void add_formatted_headers(struct strbuf
*msg
,
3417 struct string_list
*more_headers
,
3418 const char *line_prefix
,
3424 for (i
= 0; i
< more_headers
->nr
; i
++)
3425 add_formatted_header(msg
, more_headers
->items
[i
].string
,
3426 line_prefix
, meta
, reset
);
3429 static int diff_filepair_is_phoney(struct diff_filespec
*one
,
3430 struct diff_filespec
*two
)
3433 * This function specifically looks for pairs injected by
3434 * create_filepairs_for_header_only_notifications(). Such
3435 * pairs are "phoney" in that they do not represent any
3436 * content or even mode difference, but were inserted because
3437 * diff_queued_diff previously had no pair associated with
3438 * that path but we needed some pair to avoid losing the
3439 * "remerge CONFLICT" header associated with the path.
3441 return !DIFF_FILE_VALID(one
) && !DIFF_FILE_VALID(two
);
3444 static int set_diff_algorithm(struct diff_options
*opts
,
3447 long value
= parse_algorithm_value(alg
);
3452 /* clear out previous settings */
3453 DIFF_XDL_CLR(opts
, NEED_MINIMAL
);
3454 opts
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3455 opts
->xdl_opts
|= value
;
3460 static void builtin_diff(const char *name_a
,
3462 struct diff_filespec
*one
,
3463 struct diff_filespec
*two
,
3464 const char *xfrm_msg
,
3465 int must_show_header
,
3466 struct diff_options
*o
,
3467 int complete_rewrite
)
3471 char *a_one
, *b_two
;
3472 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
3473 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
3474 const char *a_prefix
, *b_prefix
;
3475 struct userdiff_driver
*textconv_one
= NULL
;
3476 struct userdiff_driver
*textconv_two
= NULL
;
3477 struct strbuf header
= STRBUF_INIT
;
3478 const char *line_prefix
= diff_line_prefix(o
);
3480 diff_set_mnemonic_prefix(o
, "a/", "b/");
3481 if (o
->flags
.reverse_diff
) {
3482 a_prefix
= o
->b_prefix
;
3483 b_prefix
= o
->a_prefix
;
3485 a_prefix
= o
->a_prefix
;
3486 b_prefix
= o
->b_prefix
;
3489 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
3490 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3491 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3492 (!diff_filepair_is_phoney(one
, two
))) {
3493 show_submodule_diff_summary(o
, one
->path
? one
->path
: two
->path
,
3494 &one
->oid
, &two
->oid
,
3495 two
->dirty_submodule
);
3497 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
3498 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3499 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3500 (!diff_filepair_is_phoney(one
, two
))) {
3501 show_submodule_inline_diff(o
, one
->path
? one
->path
: two
->path
,
3502 &one
->oid
, &two
->oid
,
3503 two
->dirty_submodule
);
3507 if (o
->flags
.allow_textconv
) {
3508 textconv_one
= get_textconv(o
->repo
, one
);
3509 textconv_two
= get_textconv(o
->repo
, two
);
3512 /* Never use a non-valid filename anywhere if at all possible */
3513 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
3514 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
3516 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
3517 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
3518 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
3519 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
3520 if (diff_filepair_is_phoney(one
, two
)) {
3522 * We should only reach this point for pairs generated from
3523 * create_filepairs_for_header_only_notifications(). For
3524 * these, we want to avoid the "/dev/null" special casing
3525 * above, because we do not want such pairs shown as either
3526 * "new file" or "deleted file" below.
3531 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3532 if (lbl
[0][0] == '/') {
3534 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3536 strbuf_addstr(&header
, xfrm_msg
);
3537 must_show_header
= 1;
3539 else if (lbl
[1][0] == '/') {
3540 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3542 strbuf_addstr(&header
, xfrm_msg
);
3543 must_show_header
= 1;
3546 if (one
->mode
!= two
->mode
) {
3547 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3548 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3549 must_show_header
= 1;
3552 strbuf_addstr(&header
, xfrm_msg
);
3555 * we do not run diff between different kind
3558 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3559 goto free_ab_and_return
;
3560 if (complete_rewrite
&&
3561 (textconv_one
|| !diff_filespec_is_binary(o
->repo
, one
)) &&
3562 (textconv_two
|| !diff_filespec_is_binary(o
->repo
, two
))) {
3563 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3564 header
.buf
, header
.len
, 0);
3565 strbuf_reset(&header
);
3566 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3567 textconv_one
, textconv_two
, o
);
3568 o
->found_changes
= 1;
3569 goto free_ab_and_return
;
3573 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3574 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3576 strbuf_reset(&header
);
3577 goto free_ab_and_return
;
3578 } else if (!o
->flags
.text
&&
3579 ( (!textconv_one
&& diff_filespec_is_binary(o
->repo
, one
)) ||
3580 (!textconv_two
&& diff_filespec_is_binary(o
->repo
, two
)) )) {
3581 struct strbuf sb
= STRBUF_INIT
;
3582 if (!one
->data
&& !two
->data
&&
3583 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3585 if (oideq(&one
->oid
, &two
->oid
)) {
3586 if (must_show_header
)
3587 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3588 header
.buf
, header
.len
,
3590 goto free_ab_and_return
;
3592 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3593 header
.buf
, header
.len
, 0);
3594 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3595 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3596 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3598 strbuf_release(&sb
);
3599 goto free_ab_and_return
;
3601 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3602 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3603 die("unable to read files to diff");
3604 /* Quite common confusing case */
3605 if (mf1
.size
== mf2
.size
&&
3606 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3607 if (must_show_header
)
3608 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3609 header
.buf
, header
.len
, 0);
3610 goto free_ab_and_return
;
3612 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3613 strbuf_reset(&header
);
3614 if (o
->flags
.binary
)
3615 emit_binary_diff(o
, &mf1
, &mf2
);
3617 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3618 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3619 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3621 strbuf_release(&sb
);
3623 o
->found_changes
= 1;
3625 /* Crazy xdl interfaces.. */
3626 const char *diffopts
;
3630 struct emit_callback ecbdata
;
3631 const struct userdiff_funcname
*pe
;
3633 if (must_show_header
) {
3634 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3635 header
.buf
, header
.len
, 0);
3636 strbuf_reset(&header
);
3639 mf1
.size
= fill_textconv(o
->repo
, textconv_one
, one
, &mf1
.ptr
);
3640 mf2
.size
= fill_textconv(o
->repo
, textconv_two
, two
, &mf2
.ptr
);
3642 pe
= diff_funcname_pattern(o
, one
);
3644 pe
= diff_funcname_pattern(o
, two
);
3646 memset(&xpp
, 0, sizeof(xpp
));
3647 memset(&xecfg
, 0, sizeof(xecfg
));
3648 memset(&ecbdata
, 0, sizeof(ecbdata
));
3649 if (o
->flags
.suppress_diff_headers
)
3651 ecbdata
.label_path
= lbl
;
3652 ecbdata
.color_diff
= want_color(o
->use_color
);
3653 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
3654 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3655 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3657 if (header
.len
&& !o
->flags
.suppress_diff_headers
)
3658 ecbdata
.header
= &header
;
3659 xpp
.flags
= o
->xdl_opts
;
3660 xpp
.ignore_regex
= o
->ignore_regex
;
3661 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3662 xpp
.anchors
= o
->anchors
;
3663 xpp
.anchors_nr
= o
->anchors_nr
;
3664 xecfg
.ctxlen
= o
->context
;
3665 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3666 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3667 if (o
->flags
.funccontext
)
3668 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3670 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3672 diffopts
= getenv("GIT_DIFF_OPTS");
3675 else if (skip_prefix(diffopts
, "--unified=", &v
))
3676 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3677 else if (skip_prefix(diffopts
, "-u", &v
))
3678 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3681 init_diff_words_data(&ecbdata
, o
, one
, two
);
3682 if (xdi_diff_outf(&mf1
, &mf2
, NULL
, fn_out_consume
,
3683 &ecbdata
, &xpp
, &xecfg
))
3684 die("unable to generate diff for %s", one
->path
);
3686 free_diff_words_data(&ecbdata
);
3691 xdiff_clear_find_func(&xecfg
);
3695 strbuf_release(&header
);
3696 diff_free_filespec_data(one
);
3697 diff_free_filespec_data(two
);
3703 static char *get_compact_summary(const struct diff_filepair
*p
, int is_renamed
)
3706 if (p
->status
== DIFF_STATUS_ADDED
) {
3707 if (S_ISLNK(p
->two
->mode
))
3709 else if ((p
->two
->mode
& 0777) == 0755)
3713 } else if (p
->status
== DIFF_STATUS_DELETED
)
3716 if (S_ISLNK(p
->one
->mode
) && !S_ISLNK(p
->two
->mode
))
3718 else if (!S_ISLNK(p
->one
->mode
) && S_ISLNK(p
->two
->mode
))
3720 else if ((p
->one
->mode
& 0777) == 0644 &&
3721 (p
->two
->mode
& 0777) == 0755)
3723 else if ((p
->one
->mode
& 0777) == 0755 &&
3724 (p
->two
->mode
& 0777) == 0644)
3729 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3730 struct diff_filespec
*one
,
3731 struct diff_filespec
*two
,
3732 struct diffstat_t
*diffstat
,
3733 struct diff_options
*o
,
3734 struct diff_filepair
*p
)
3737 struct diffstat_file
*data
;
3739 int complete_rewrite
= 0;
3741 if (!DIFF_PAIR_UNMERGED(p
)) {
3742 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3743 complete_rewrite
= 1;
3746 data
= diffstat_add(diffstat
, name_a
, name_b
);
3747 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3748 if (o
->flags
.stat_with_summary
)
3749 data
->comments
= get_compact_summary(p
, data
->is_renamed
);
3752 data
->is_unmerged
= 1;
3756 /* saves some reads if true, not a guarantee of diff outcome */
3757 may_differ
= !(one
->oid_valid
&& two
->oid_valid
&&
3758 oideq(&one
->oid
, &two
->oid
));
3760 if (diff_filespec_is_binary(o
->repo
, one
) ||
3761 diff_filespec_is_binary(o
->repo
, two
)) {
3762 data
->is_binary
= 1;
3767 data
->added
= diff_filespec_size(o
->repo
, two
);
3768 data
->deleted
= diff_filespec_size(o
->repo
, one
);
3772 else if (complete_rewrite
) {
3773 diff_populate_filespec(o
->repo
, one
, NULL
);
3774 diff_populate_filespec(o
->repo
, two
, NULL
);
3775 data
->deleted
= count_lines(one
->data
, one
->size
);
3776 data
->added
= count_lines(two
->data
, two
->size
);
3779 else if (may_differ
) {
3780 /* Crazy xdl interfaces.. */
3784 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3785 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3786 die("unable to read files to diff");
3788 memset(&xpp
, 0, sizeof(xpp
));
3789 memset(&xecfg
, 0, sizeof(xecfg
));
3790 xpp
.flags
= o
->xdl_opts
;
3791 xpp
.ignore_regex
= o
->ignore_regex
;
3792 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3793 xpp
.anchors
= o
->anchors
;
3794 xpp
.anchors_nr
= o
->anchors_nr
;
3795 xecfg
.ctxlen
= o
->context
;
3796 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3797 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
3798 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
3799 diffstat_consume
, diffstat
, &xpp
, &xecfg
))
3800 die("unable to generate diffstat for %s", one
->path
);
3802 if (DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
)) {
3803 struct diffstat_file
*file
=
3804 diffstat
->files
[diffstat
->nr
- 1];
3806 * Omit diffstats of modified files where nothing changed.
3807 * Even if may_differ, this might be the case due to
3808 * ignoring whitespace changes, etc.
3810 * But note that we special-case additions, deletions,
3811 * renames, and mode changes as adding an empty file,
3812 * for example is still of interest.
3814 if ((p
->status
== DIFF_STATUS_MODIFIED
)
3817 && one
->mode
== two
->mode
) {
3818 free_diffstat_file(file
);
3824 diff_free_filespec_data(one
);
3825 diff_free_filespec_data(two
);
3828 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3829 const char *attr_path
,
3830 struct diff_filespec
*one
,
3831 struct diff_filespec
*two
,
3832 struct diff_options
*o
)
3835 struct checkdiff_t data
;
3840 memset(&data
, 0, sizeof(data
));
3841 data
.filename
= name_b
? name_b
: name_a
;
3844 data
.ws_rule
= whitespace_rule(o
->repo
->index
, attr_path
);
3845 data
.conflict_marker_size
= ll_merge_marker_size(o
->repo
->index
, attr_path
);
3847 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3848 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3849 die("unable to read files to diff");
3852 * All the other codepaths check both sides, but not checking
3853 * the "old" side here is deliberate. We are checking the newly
3854 * introduced changes, and as long as the "new" side is text, we
3855 * can and should check what it introduces.
3857 if (diff_filespec_is_binary(o
->repo
, two
))
3858 goto free_and_return
;
3860 /* Crazy xdl interfaces.. */
3864 memset(&xpp
, 0, sizeof(xpp
));
3865 memset(&xecfg
, 0, sizeof(xecfg
));
3866 xecfg
.ctxlen
= 1; /* at least one context line */
3868 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume_hunk
,
3869 checkdiff_consume
, &data
,
3871 die("unable to generate checkdiff for %s", one
->path
);
3873 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3874 struct emit_callback ecbdata
;
3877 ecbdata
.ws_rule
= data
.ws_rule
;
3878 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3879 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3884 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3885 fprintf(o
->file
, "%s:%d: %s.\n",
3886 data
.filename
, blank_at_eof
, err
);
3887 data
.status
= 1; /* report errors */
3892 diff_free_filespec_data(one
);
3893 diff_free_filespec_data(two
);
3895 o
->flags
.check_failed
= 1;
3898 struct diff_filespec
*alloc_filespec(const char *path
)
3900 struct diff_filespec
*spec
;
3902 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3904 spec
->is_binary
= -1;
3908 void free_filespec(struct diff_filespec
*spec
)
3910 if (!--spec
->count
) {
3911 diff_free_filespec_data(spec
);
3916 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3917 int oid_valid
, unsigned short mode
)
3920 spec
->mode
= canon_mode(mode
);
3921 oidcpy(&spec
->oid
, oid
);
3922 spec
->oid_valid
= oid_valid
;
3927 * Given a name and sha1 pair, if the index tells us the file in
3928 * the work tree has that object contents, return true, so that
3929 * prepare_temp_file() does not have to inflate and extract.
3931 static int reuse_worktree_file(struct index_state
*istate
,
3933 const struct object_id
*oid
,
3936 const struct cache_entry
*ce
;
3941 * We do not read the cache ourselves here, because the
3942 * benchmark with my previous version that always reads cache
3943 * shows that it makes things worse for diff-tree comparing
3944 * two linux-2.6 kernel trees in an already checked out work
3945 * tree. This is because most diff-tree comparisons deal with
3946 * only a small number of files, while reading the cache is
3947 * expensive for a large project, and its cost outweighs the
3948 * savings we get by not inflating the object to a temporary
3949 * file. Practically, this code only helps when we are used
3950 * by diff-cache --cached, which does read the cache before
3956 /* We want to avoid the working directory if our caller
3957 * doesn't need the data in a normal file, this system
3958 * is rather slow with its stat/open/mmap/close syscalls,
3959 * and the object is contained in a pack file. The pack
3960 * is probably already open and will be faster to obtain
3961 * the data through than the working directory. Loose
3962 * objects however would tend to be slower as they need
3963 * to be individually opened and inflated.
3965 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_object_pack(oid
))
3969 * Similarly, if we'd have to convert the file contents anyway, that
3970 * makes the optimization not worthwhile.
3972 if (!want_file
&& would_convert_to_git(istate
, name
))
3976 * If this path does not match our sparse-checkout definition,
3977 * then the file will not be in the working directory.
3979 if (!path_in_sparse_checkout(name
, istate
))
3983 pos
= index_name_pos(istate
, name
, len
);
3986 ce
= istate
->cache
[pos
];
3989 * This is not the sha1 we are looking for, or
3990 * unreusable because it is not a regular file.
3992 if (!oideq(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
3996 * If ce is marked as "assume unchanged", there is no
3997 * guarantee that work tree matches what we are looking for.
3999 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
4003 * If ce matches the file in the work tree, we can reuse it.
4005 if (ce_uptodate(ce
) ||
4006 (!lstat(name
, &st
) && !ie_match_stat(istate
, ce
, &st
, 0)))
4012 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
4014 struct strbuf buf
= STRBUF_INIT
;
4017 /* Are we looking at the work tree? */
4018 if (s
->dirty_submodule
)
4021 strbuf_addf(&buf
, "Subproject commit %s%s\n",
4022 oid_to_hex(&s
->oid
), dirty
);
4026 strbuf_release(&buf
);
4028 s
->data
= strbuf_detach(&buf
, NULL
);
4035 * While doing rename detection and pickaxe operation, we may need to
4036 * grab the data for the blob (or file) for our own in-core comparison.
4037 * diff_filespec has data and size fields for this purpose.
4039 int diff_populate_filespec(struct repository
*r
,
4040 struct diff_filespec
*s
,
4041 const struct diff_populate_filespec_options
*options
)
4043 int size_only
= options
? options
->check_size_only
: 0;
4044 int check_binary
= options
? options
->check_binary
: 0;
4046 int conv_flags
= global_conv_flags_eol
;
4048 * demote FAIL to WARN to allow inspecting the situation
4049 * instead of refusing.
4051 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
4052 conv_flags
= CONV_EOL_RNDTRP_WARN
;
4054 if (!DIFF_FILE_VALID(s
))
4055 die("internal error: asking to populate invalid file.");
4056 if (S_ISDIR(s
->mode
))
4062 if (size_only
&& 0 < s
->size
)
4065 if (S_ISGITLINK(s
->mode
))
4066 return diff_populate_gitlink(s
, size_only
);
4068 if (!s
->oid_valid
||
4069 reuse_worktree_file(r
->index
, s
->path
, &s
->oid
, 0)) {
4070 struct strbuf buf
= STRBUF_INIT
;
4074 if (lstat(s
->path
, &st
) < 0) {
4078 s
->data
= (char *)"";
4082 s
->size
= xsize_t(st
.st_size
);
4085 if (S_ISLNK(st
.st_mode
)) {
4086 struct strbuf sb
= STRBUF_INIT
;
4088 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
4091 s
->data
= strbuf_detach(&sb
, NULL
);
4097 * Even if the caller would be happy with getting
4098 * only the size, we cannot return early at this
4099 * point if the path requires us to run the content
4102 if (size_only
&& !would_convert_to_git(r
->index
, s
->path
))
4106 * Note: this check uses xsize_t(st.st_size) that may
4107 * not be the true size of the blob after it goes
4108 * through convert_to_git(). This may not strictly be
4109 * correct, but the whole point of big_file_threshold
4110 * and is_binary check being that we want to avoid
4111 * opening the file and inspecting the contents, this
4115 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4119 fd
= open(s
->path
, O_RDONLY
);
4122 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
4124 s
->should_munmap
= 1;
4127 * Convert from working tree format to canonical git format
4129 if (convert_to_git(r
->index
, s
->path
, s
->data
, s
->size
, &buf
, conv_flags
)) {
4131 munmap(s
->data
, s
->size
);
4132 s
->should_munmap
= 0;
4133 s
->data
= strbuf_detach(&buf
, &size
);
4139 struct object_info info
= {
4143 if (!(size_only
|| check_binary
))
4145 * Set contentp, since there is no chance that merely
4146 * the size is sufficient.
4148 info
.contentp
= &s
->data
;
4150 if (options
&& options
->missing_object_cb
) {
4151 if (!oid_object_info_extended(r
, &s
->oid
, &info
,
4152 OBJECT_INFO_LOOKUP_REPLACE
|
4153 OBJECT_INFO_SKIP_FETCH_OBJECT
))
4155 options
->missing_object_cb(options
->missing_object_data
);
4157 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4158 OBJECT_INFO_LOOKUP_REPLACE
))
4159 die("unable to read %s", oid_to_hex(&s
->oid
));
4162 if (size_only
|| check_binary
) {
4165 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4170 if (!info
.contentp
) {
4171 info
.contentp
= &s
->data
;
4172 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4173 OBJECT_INFO_LOOKUP_REPLACE
))
4174 die("unable to read %s", oid_to_hex(&s
->oid
));
4181 void diff_free_filespec_blob(struct diff_filespec
*s
)
4185 else if (s
->should_munmap
)
4186 munmap(s
->data
, s
->size
);
4188 if (s
->should_free
|| s
->should_munmap
) {
4189 s
->should_free
= s
->should_munmap
= 0;
4194 void diff_free_filespec_data(struct diff_filespec
*s
)
4199 diff_free_filespec_blob(s
);
4200 FREE_AND_NULL(s
->cnt_data
);
4203 static void prep_temp_blob(struct index_state
*istate
,
4204 const char *path
, struct diff_tempfile
*temp
,
4207 const struct object_id
*oid
,
4210 struct strbuf buf
= STRBUF_INIT
;
4211 char *path_dup
= xstrdup(path
);
4212 const char *base
= basename(path_dup
);
4213 struct checkout_metadata meta
;
4215 init_checkout_metadata(&meta
, NULL
, NULL
, oid
);
4217 temp
->tempfile
= mks_tempfile_dt("git-blob-XXXXXX", base
);
4218 if (!temp
->tempfile
)
4219 die_errno("unable to create temp-file");
4220 if (convert_to_working_tree(istate
, path
,
4221 (const char *)blob
, (size_t)size
, &buf
, &meta
)) {
4225 if (write_in_full(temp
->tempfile
->fd
, blob
, size
) < 0 ||
4226 close_tempfile_gently(temp
->tempfile
))
4227 die_errno("unable to write temp-file");
4228 temp
->name
= get_tempfile_path(temp
->tempfile
);
4229 oid_to_hex_r(temp
->hex
, oid
);
4230 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
4231 strbuf_release(&buf
);
4235 static struct diff_tempfile
*prepare_temp_file(struct repository
*r
,
4236 struct diff_filespec
*one
)
4238 struct diff_tempfile
*temp
= claim_diff_tempfile();
4240 if (!DIFF_FILE_VALID(one
)) {
4242 /* A '-' entry produces this for file-2, and
4243 * a '+' entry produces this for file-1.
4245 temp
->name
= "/dev/null";
4246 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
4247 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
4251 if (!S_ISGITLINK(one
->mode
) &&
4253 reuse_worktree_file(r
->index
, one
->path
, &one
->oid
, 1))) {
4255 if (lstat(one
->path
, &st
) < 0) {
4256 if (errno
== ENOENT
)
4257 goto not_a_valid_file
;
4258 die_errno("stat(%s)", one
->path
);
4260 if (S_ISLNK(st
.st_mode
)) {
4261 struct strbuf sb
= STRBUF_INIT
;
4262 if (strbuf_readlink(&sb
, one
->path
, st
.st_size
) < 0)
4263 die_errno("readlink(%s)", one
->path
);
4264 prep_temp_blob(r
->index
, one
->path
, temp
, sb
.buf
, sb
.len
,
4266 &one
->oid
: null_oid()),
4268 one
->mode
: S_IFLNK
));
4269 strbuf_release(&sb
);
4272 /* we can borrow from the file in the work tree */
4273 temp
->name
= one
->path
;
4274 if (!one
->oid_valid
)
4275 oid_to_hex_r(temp
->hex
, null_oid());
4277 oid_to_hex_r(temp
->hex
, &one
->oid
);
4278 /* Even though we may sometimes borrow the
4279 * contents from the work tree, we always want
4280 * one->mode. mode is trustworthy even when
4281 * !(one->oid_valid), as long as
4282 * DIFF_FILE_VALID(one).
4284 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
4289 if (diff_populate_filespec(r
, one
, NULL
))
4290 die("cannot read data blob for %s", one
->path
);
4291 prep_temp_blob(r
->index
, one
->path
, temp
,
4292 one
->data
, one
->size
,
4293 &one
->oid
, one
->mode
);
4298 static void add_external_diff_name(struct repository
*r
,
4299 struct strvec
*argv
,
4300 struct diff_filespec
*df
)
4302 struct diff_tempfile
*temp
= prepare_temp_file(r
, df
);
4303 strvec_push(argv
, temp
->name
);
4304 strvec_push(argv
, temp
->hex
);
4305 strvec_push(argv
, temp
->mode
);
4308 /* An external diff command takes:
4310 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4311 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4314 static void run_external_diff(const char *pgm
,
4317 struct diff_filespec
*one
,
4318 struct diff_filespec
*two
,
4319 const char *xfrm_msg
,
4320 struct diff_options
*o
)
4322 struct child_process cmd
= CHILD_PROCESS_INIT
;
4323 struct diff_queue_struct
*q
= &diff_queued_diff
;
4325 strvec_push(&cmd
.args
, pgm
);
4326 strvec_push(&cmd
.args
, name
);
4329 add_external_diff_name(o
->repo
, &cmd
.args
, one
);
4330 add_external_diff_name(o
->repo
, &cmd
.args
, two
);
4332 strvec_push(&cmd
.args
, other
);
4333 strvec_push(&cmd
.args
, xfrm_msg
);
4337 strvec_pushf(&cmd
.env
, "GIT_DIFF_PATH_COUNTER=%d",
4338 ++o
->diff_path_counter
);
4339 strvec_pushf(&cmd
.env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
4341 diff_free_filespec_data(one
);
4342 diff_free_filespec_data(two
);
4344 if (run_command(&cmd
))
4345 die(_("external diff died, stopping at %s"), name
);
4350 static int similarity_index(struct diff_filepair
*p
)
4352 return p
->score
* 100 / MAX_SCORE
;
4355 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
4357 if (startup_info
->have_repository
)
4358 return find_unique_abbrev(oid
, abbrev
);
4360 char *hex
= oid_to_hex(oid
);
4362 abbrev
= FALLBACK_DEFAULT_ABBREV
;
4363 if (abbrev
> the_hash_algo
->hexsz
)
4364 BUG("oid abbreviation out of range: %d", abbrev
);
4371 static void fill_metainfo(struct strbuf
*msg
,
4374 struct diff_filespec
*one
,
4375 struct diff_filespec
*two
,
4376 struct diff_options
*o
,
4377 struct diff_filepair
*p
,
4378 int *must_show_header
,
4381 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
4382 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
4383 const char *line_prefix
= diff_line_prefix(o
);
4384 struct string_list
*more_headers
= NULL
;
4386 *must_show_header
= 1;
4387 strbuf_init(msg
, PATH_MAX
* 2 + 300);
4388 switch (p
->status
) {
4389 case DIFF_STATUS_COPIED
:
4390 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4391 line_prefix
, set
, similarity_index(p
));
4392 strbuf_addf(msg
, "%s\n%s%scopy from ",
4393 reset
, line_prefix
, set
);
4394 quote_c_style(name
, msg
, NULL
, 0);
4395 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
4396 quote_c_style(other
, msg
, NULL
, 0);
4397 strbuf_addf(msg
, "%s\n", reset
);
4399 case DIFF_STATUS_RENAMED
:
4400 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4401 line_prefix
, set
, similarity_index(p
));
4402 strbuf_addf(msg
, "%s\n%s%srename from ",
4403 reset
, line_prefix
, set
);
4404 quote_c_style(name
, msg
, NULL
, 0);
4405 strbuf_addf(msg
, "%s\n%s%srename to ",
4406 reset
, line_prefix
, set
);
4407 quote_c_style(other
, msg
, NULL
, 0);
4408 strbuf_addf(msg
, "%s\n", reset
);
4410 case DIFF_STATUS_MODIFIED
:
4412 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
4414 set
, similarity_index(p
), reset
);
4419 *must_show_header
= 0;
4421 if ((more_headers
= additional_headers(o
, name
))) {
4422 add_formatted_headers(msg
, more_headers
,
4423 line_prefix
, set
, reset
);
4424 *must_show_header
= 1;
4426 if (one
&& two
&& !oideq(&one
->oid
, &two
->oid
)) {
4427 const unsigned hexsz
= the_hash_algo
->hexsz
;
4428 int abbrev
= o
->abbrev
? o
->abbrev
: DEFAULT_ABBREV
;
4430 if (o
->flags
.full_index
)
4433 if (o
->flags
.binary
) {
4435 if ((!fill_mmfile(o
->repo
, &mf
, one
) &&
4436 diff_filespec_is_binary(o
->repo
, one
)) ||
4437 (!fill_mmfile(o
->repo
, &mf
, two
) &&
4438 diff_filespec_is_binary(o
->repo
, two
)))
4441 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
4442 diff_abbrev_oid(&one
->oid
, abbrev
),
4443 diff_abbrev_oid(&two
->oid
, abbrev
));
4444 if (one
->mode
== two
->mode
)
4445 strbuf_addf(msg
, " %06o", one
->mode
);
4446 strbuf_addf(msg
, "%s\n", reset
);
4450 static void run_diff_cmd(const char *pgm
,
4453 const char *attr_path
,
4454 struct diff_filespec
*one
,
4455 struct diff_filespec
*two
,
4457 struct diff_options
*o
,
4458 struct diff_filepair
*p
)
4460 const char *xfrm_msg
= NULL
;
4461 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
4462 int must_show_header
= 0;
4463 struct userdiff_driver
*drv
= NULL
;
4465 if (o
->flags
.allow_external
|| !o
->ignore_driver_algorithm
)
4466 drv
= userdiff_find_by_path(o
->repo
->index
, attr_path
);
4468 if (o
->flags
.allow_external
&& drv
&& drv
->external
)
4469 pgm
= drv
->external
;
4473 * don't use colors when the header is intended for an
4474 * external diff driver
4476 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
4478 want_color(o
->use_color
) && !pgm
);
4479 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
4483 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
, o
);
4487 if (!o
->ignore_driver_algorithm
&& drv
&& drv
->algorithm
)
4488 set_diff_algorithm(o
, drv
->algorithm
);
4490 builtin_diff(name
, other
? other
: name
,
4491 one
, two
, xfrm_msg
, must_show_header
,
4492 o
, complete_rewrite
);
4494 fprintf(o
->file
, "* Unmerged path %s\n", name
);
4498 static void diff_fill_oid_info(struct diff_filespec
*one
, struct index_state
*istate
)
4500 if (DIFF_FILE_VALID(one
)) {
4501 if (!one
->oid_valid
) {
4503 if (one
->is_stdin
) {
4507 if (lstat(one
->path
, &st
) < 0)
4508 die_errno("stat '%s'", one
->path
);
4509 if (index_path(istate
, &one
->oid
, one
->path
, &st
, 0))
4510 die("cannot hash %s", one
->path
);
4517 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
4519 /* Strip the prefix but do not molest /dev/null and absolute paths */
4520 if (*namep
&& !is_absolute_path(*namep
)) {
4521 *namep
+= prefix_length
;
4525 if (*otherp
&& !is_absolute_path(*otherp
)) {
4526 *otherp
+= prefix_length
;
4527 if (**otherp
== '/')
4532 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
4534 const char *pgm
= external_diff();
4536 struct diff_filespec
*one
= p
->one
;
4537 struct diff_filespec
*two
= p
->two
;
4540 const char *attr_path
;
4543 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
4545 if (o
->prefix_length
)
4546 strip_prefix(o
->prefix_length
, &name
, &other
);
4548 if (!o
->flags
.allow_external
)
4551 if (DIFF_PAIR_UNMERGED(p
)) {
4552 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
4553 NULL
, NULL
, NULL
, o
, p
);
4557 diff_fill_oid_info(one
, o
->repo
->index
);
4558 diff_fill_oid_info(two
, o
->repo
->index
);
4561 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4562 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4564 * a filepair that changes between file and symlink
4565 * needs to be split into deletion and creation.
4567 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4568 run_diff_cmd(NULL
, name
, other
, attr_path
,
4572 strbuf_release(&msg
);
4574 null
= alloc_filespec(one
->path
);
4575 run_diff_cmd(NULL
, name
, other
, attr_path
,
4576 null
, two
, &msg
, o
, p
);
4580 run_diff_cmd(pgm
, name
, other
, attr_path
,
4581 one
, two
, &msg
, o
, p
);
4583 strbuf_release(&msg
);
4586 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4587 struct diffstat_t
*diffstat
)
4592 if (!o
->ignore_driver_algorithm
) {
4593 struct userdiff_driver
*drv
= userdiff_find_by_path(o
->repo
->index
,
4596 if (drv
&& drv
->algorithm
)
4597 set_diff_algorithm(o
, drv
->algorithm
);
4600 if (DIFF_PAIR_UNMERGED(p
)) {
4602 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
,
4607 name
= p
->one
->path
;
4608 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4610 if (o
->prefix_length
)
4611 strip_prefix(o
->prefix_length
, &name
, &other
);
4613 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4614 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4616 builtin_diffstat(name
, other
, p
->one
, p
->two
,
4620 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4624 const char *attr_path
;
4626 if (DIFF_PAIR_UNMERGED(p
)) {
4631 name
= p
->one
->path
;
4632 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4633 attr_path
= other
? other
: name
;
4635 if (o
->prefix_length
)
4636 strip_prefix(o
->prefix_length
, &name
, &other
);
4638 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4639 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4641 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4644 void repo_diff_setup(struct repository
*r
, struct diff_options
*options
)
4646 memcpy(options
, &default_diff_options
, sizeof(*options
));
4648 options
->file
= stdout
;
4651 options
->output_indicators
[OUTPUT_INDICATOR_NEW
] = '+';
4652 options
->output_indicators
[OUTPUT_INDICATOR_OLD
] = '-';
4653 options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
] = ' ';
4654 options
->abbrev
= DEFAULT_ABBREV
;
4655 options
->line_termination
= '\n';
4656 options
->break_opt
= -1;
4657 options
->rename_limit
= -1;
4658 options
->dirstat_permille
= diff_dirstat_permille_default
;
4659 options
->context
= diff_context_default
;
4660 options
->interhunkcontext
= diff_interhunk_context_default
;
4661 options
->ws_error_highlight
= ws_error_highlight_default
;
4662 options
->flags
.rename_empty
= 1;
4663 options
->flags
.relative_name
= diff_relative
;
4664 options
->objfind
= NULL
;
4666 /* pathchange left =NULL by default */
4667 options
->change
= diff_change
;
4668 options
->add_remove
= diff_addremove
;
4669 options
->use_color
= diff_use_color_default
;
4670 options
->detect_rename
= diff_detect_rename_default
;
4671 options
->xdl_opts
|= diff_algorithm
;
4672 if (diff_indent_heuristic
)
4673 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4675 options
->orderfile
= diff_order_file_cfg
;
4677 if (!options
->flags
.ignore_submodule_set
)
4678 options
->flags
.ignore_untracked_in_submodules
= 1;
4680 if (diff_no_prefix
) {
4681 options
->a_prefix
= options
->b_prefix
= "";
4682 } else if (!diff_mnemonic_prefix
) {
4683 options
->a_prefix
= "a/";
4684 options
->b_prefix
= "b/";
4687 options
->color_moved
= diff_color_moved_default
;
4688 options
->color_moved_ws_handling
= diff_color_moved_ws_default
;
4691 static const char diff_status_letters
[] = {
4694 DIFF_STATUS_DELETED
,
4695 DIFF_STATUS_MODIFIED
,
4696 DIFF_STATUS_RENAMED
,
4697 DIFF_STATUS_TYPE_CHANGED
,
4698 DIFF_STATUS_UNKNOWN
,
4699 DIFF_STATUS_UNMERGED
,
4700 DIFF_STATUS_FILTER_AON
,
4701 DIFF_STATUS_FILTER_BROKEN
,
4705 static unsigned int filter_bit
['Z' + 1];
4707 static void prepare_filter_bits(void)
4711 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4712 for (i
= 0; diff_status_letters
[i
]; i
++)
4713 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4717 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4719 return opt
->filter
& filter_bit
[(int) status
];
4722 unsigned diff_filter_bit(char status
)
4724 prepare_filter_bits();
4725 return filter_bit
[(int) status
];
4728 void diff_setup_done(struct diff_options
*options
)
4730 unsigned check_mask
= DIFF_FORMAT_NAME
|
4731 DIFF_FORMAT_NAME_STATUS
|
4732 DIFF_FORMAT_CHECKDIFF
|
4733 DIFF_FORMAT_NO_OUTPUT
;
4735 * This must be signed because we're comparing against a potentially
4738 const int hexsz
= the_hash_algo
->hexsz
;
4740 if (options
->set_default
)
4741 options
->set_default(options
);
4743 if (HAS_MULTI_BITS(options
->output_format
& check_mask
))
4744 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
4745 "--name-only", "--name-status", "--check", "-s");
4747 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
4748 die(_("options '%s', '%s', and '%s' cannot be used together"),
4749 "-G", "-S", "--find-object");
4751 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_G_REGEX_MASK
))
4752 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"),
4753 "-G", "--pickaxe-regex", "--pickaxe-regex", "-S");
4755 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK
))
4756 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"),
4757 "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S");
4760 * Most of the time we can say "there are changes"
4761 * only by checking if there are changed paths, but
4762 * --ignore-whitespace* options force us to look
4766 if ((options
->xdl_opts
& XDF_WHITESPACE_FLAGS
) ||
4767 options
->ignore_regex_nr
)
4768 options
->flags
.diff_from_contents
= 1;
4770 options
->flags
.diff_from_contents
= 0;
4772 if (options
->flags
.find_copies_harder
)
4773 options
->detect_rename
= DIFF_DETECT_COPY
;
4775 if (!options
->flags
.relative_name
)
4776 options
->prefix
= NULL
;
4777 if (options
->prefix
)
4778 options
->prefix_length
= strlen(options
->prefix
);
4780 options
->prefix_length
= 0;
4782 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4783 DIFF_FORMAT_NAME_STATUS
|
4784 DIFF_FORMAT_CHECKDIFF
|
4785 DIFF_FORMAT_NO_OUTPUT
))
4786 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4787 DIFF_FORMAT_NUMSTAT
|
4788 DIFF_FORMAT_DIFFSTAT
|
4789 DIFF_FORMAT_SHORTSTAT
|
4790 DIFF_FORMAT_DIRSTAT
|
4791 DIFF_FORMAT_SUMMARY
|
4795 * These cases always need recursive; we do not drop caller-supplied
4796 * recursive bits for other formats here.
4798 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4799 DIFF_FORMAT_NUMSTAT
|
4800 DIFF_FORMAT_DIFFSTAT
|
4801 DIFF_FORMAT_SHORTSTAT
|
4802 DIFF_FORMAT_DIRSTAT
|
4803 DIFF_FORMAT_SUMMARY
|
4804 DIFF_FORMAT_CHECKDIFF
))
4805 options
->flags
.recursive
= 1;
4807 * Also pickaxe would not work very well if you do not say recursive
4809 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
4810 options
->flags
.recursive
= 1;
4812 * When patches are generated, submodules diffed against the work tree
4813 * must be checked for dirtiness too so it can be shown in the output
4815 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4816 options
->flags
.dirty_submodules
= 1;
4818 if (options
->detect_rename
&& options
->rename_limit
< 0)
4819 options
->rename_limit
= diff_rename_limit_default
;
4820 if (hexsz
< options
->abbrev
)
4821 options
->abbrev
= hexsz
; /* full */
4824 * It does not make sense to show the first hit we happened
4825 * to have found. It does not make sense not to return with
4826 * exit code in such a case either.
4828 if (options
->flags
.quick
) {
4829 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4830 options
->flags
.exit_with_status
= 1;
4833 options
->diff_path_counter
= 0;
4835 if (options
->flags
.follow_renames
&& options
->pathspec
.nr
!= 1)
4836 die(_("--follow requires exactly one pathspec"));
4838 if (!options
->use_color
|| external_diff())
4839 options
->color_moved
= 0;
4841 if (options
->filter_not
) {
4842 if (!options
->filter
)
4843 options
->filter
= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4844 options
->filter
&= ~options
->filter_not
;
4848 int parse_long_opt(const char *opt
, const char **argv
,
4849 const char **optarg
)
4851 const char *arg
= argv
[0];
4852 if (!skip_prefix(arg
, "--", &arg
))
4854 if (!skip_prefix(arg
, opt
, &arg
))
4856 if (*arg
== '=') { /* stuck form: --option=value */
4862 /* separate form: --option value */
4864 die("Option '--%s' requires a value", opt
);
4869 static int diff_opt_stat(const struct option
*opt
, const char *value
, int unset
)
4871 struct diff_options
*options
= opt
->value
;
4872 int width
= options
->stat_width
;
4873 int name_width
= options
->stat_name_width
;
4874 int graph_width
= options
->stat_graph_width
;
4875 int count
= options
->stat_count
;
4878 BUG_ON_OPT_NEG(unset
);
4880 if (!strcmp(opt
->long_name
, "stat")) {
4882 width
= strtoul(value
, &end
, 10);
4884 name_width
= strtoul(end
+1, &end
, 10);
4886 count
= strtoul(end
+1, &end
, 10);
4888 return error(_("invalid --stat value: %s"), value
);
4890 } else if (!strcmp(opt
->long_name
, "stat-width")) {
4891 width
= strtoul(value
, &end
, 10);
4893 return error(_("%s expects a numerical value"),
4895 } else if (!strcmp(opt
->long_name
, "stat-name-width")) {
4896 name_width
= strtoul(value
, &end
, 10);
4898 return error(_("%s expects a numerical value"),
4900 } else if (!strcmp(opt
->long_name
, "stat-graph-width")) {
4901 graph_width
= strtoul(value
, &end
, 10);
4903 return error(_("%s expects a numerical value"),
4905 } else if (!strcmp(opt
->long_name
, "stat-count")) {
4906 count
= strtoul(value
, &end
, 10);
4908 return error(_("%s expects a numerical value"),
4911 BUG("%s should not get here", opt
->long_name
);
4913 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4914 options
->stat_name_width
= name_width
;
4915 options
->stat_graph_width
= graph_width
;
4916 options
->stat_width
= width
;
4917 options
->stat_count
= count
;
4921 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
4923 struct strbuf errmsg
= STRBUF_INIT
;
4924 if (parse_dirstat_params(options
, params
, &errmsg
))
4925 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
4927 strbuf_release(&errmsg
);
4929 * The caller knows a dirstat-related option is given from the command
4930 * line; allow it to say "return this_function();"
4932 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
4936 static int diff_opt_diff_filter(const struct option
*option
,
4937 const char *optarg
, int unset
)
4939 struct diff_options
*opt
= option
->value
;
4942 BUG_ON_OPT_NEG(unset
);
4943 prepare_filter_bits();
4945 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4949 if ('a' <= optch
&& optch
<= 'z') {
4951 optch
= toupper(optch
);
4956 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
4958 return error(_("unknown change class '%c' in --diff-filter=%s"),
4961 opt
->filter_not
|= bit
;
4968 static void enable_patch_output(int *fmt
)
4970 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
4971 *fmt
|= DIFF_FORMAT_PATCH
;
4974 static int diff_opt_ws_error_highlight(const struct option
*option
,
4975 const char *arg
, int unset
)
4977 struct diff_options
*opt
= option
->value
;
4978 int val
= parse_ws_error_highlight(arg
);
4980 BUG_ON_OPT_NEG(unset
);
4982 return error(_("unknown value after ws-error-highlight=%.*s"),
4984 opt
->ws_error_highlight
= val
;
4988 static int diff_opt_find_object(const struct option
*option
,
4989 const char *arg
, int unset
)
4991 struct diff_options
*opt
= option
->value
;
4992 struct object_id oid
;
4994 BUG_ON_OPT_NEG(unset
);
4995 if (get_oid(arg
, &oid
))
4996 return error(_("unable to resolve '%s'"), arg
);
4999 CALLOC_ARRAY(opt
->objfind
, 1);
5001 opt
->pickaxe_opts
|= DIFF_PICKAXE_KIND_OBJFIND
;
5002 opt
->flags
.recursive
= 1;
5003 opt
->flags
.tree_in_recursive
= 1;
5004 oidset_insert(opt
->objfind
, &oid
);
5008 static int diff_opt_anchored(const struct option
*opt
,
5009 const char *arg
, int unset
)
5011 struct diff_options
*options
= opt
->value
;
5013 BUG_ON_OPT_NEG(unset
);
5014 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
5015 ALLOC_GROW(options
->anchors
, options
->anchors_nr
+ 1,
5016 options
->anchors_alloc
);
5017 options
->anchors
[options
->anchors_nr
++] = xstrdup(arg
);
5021 static int diff_opt_binary(const struct option
*opt
,
5022 const char *arg
, int unset
)
5024 struct diff_options
*options
= opt
->value
;
5026 BUG_ON_OPT_NEG(unset
);
5027 BUG_ON_OPT_ARG(arg
);
5028 enable_patch_output(&options
->output_format
);
5029 options
->flags
.binary
= 1;
5033 static int diff_opt_break_rewrites(const struct option
*opt
,
5034 const char *arg
, int unset
)
5036 int *break_opt
= opt
->value
;
5039 BUG_ON_OPT_NEG(unset
);
5042 opt1
= parse_rename_score(&arg
);
5045 else if (*arg
!= '/')
5046 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5049 opt2
= parse_rename_score(&arg
);
5052 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5053 *break_opt
= opt1
| (opt2
<< 16);
5057 static int diff_opt_char(const struct option
*opt
,
5058 const char *arg
, int unset
)
5060 char *value
= opt
->value
;
5062 BUG_ON_OPT_NEG(unset
);
5064 return error(_("%s expects a character, got '%s'"),
5065 opt
->long_name
, arg
);
5070 static int diff_opt_color_moved(const struct option
*opt
,
5071 const char *arg
, int unset
)
5073 struct diff_options
*options
= opt
->value
;
5076 options
->color_moved
= COLOR_MOVED_NO
;
5078 if (diff_color_moved_default
)
5079 options
->color_moved
= diff_color_moved_default
;
5080 if (options
->color_moved
== COLOR_MOVED_NO
)
5081 options
->color_moved
= COLOR_MOVED_DEFAULT
;
5083 int cm
= parse_color_moved(arg
);
5085 return error(_("bad --color-moved argument: %s"), arg
);
5086 options
->color_moved
= cm
;
5091 static int diff_opt_color_moved_ws(const struct option
*opt
,
5092 const char *arg
, int unset
)
5094 struct diff_options
*options
= opt
->value
;
5098 options
->color_moved_ws_handling
= 0;
5102 cm
= parse_color_moved_ws(arg
);
5103 if (cm
& COLOR_MOVED_WS_ERROR
)
5104 return error(_("invalid mode '%s' in --color-moved-ws"), arg
);
5105 options
->color_moved_ws_handling
= cm
;
5109 static int diff_opt_color_words(const struct option
*opt
,
5110 const char *arg
, int unset
)
5112 struct diff_options
*options
= opt
->value
;
5114 BUG_ON_OPT_NEG(unset
);
5115 options
->use_color
= 1;
5116 options
->word_diff
= DIFF_WORDS_COLOR
;
5117 options
->word_regex
= arg
;
5121 static int diff_opt_compact_summary(const struct option
*opt
,
5122 const char *arg
, int unset
)
5124 struct diff_options
*options
= opt
->value
;
5126 BUG_ON_OPT_ARG(arg
);
5128 options
->flags
.stat_with_summary
= 0;
5130 options
->flags
.stat_with_summary
= 1;
5131 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
5136 static int diff_opt_diff_algorithm(const struct option
*opt
,
5137 const char *arg
, int unset
)
5139 struct diff_options
*options
= opt
->value
;
5141 BUG_ON_OPT_NEG(unset
);
5143 if (set_diff_algorithm(options
, arg
))
5144 return error(_("option diff-algorithm accepts \"myers\", "
5145 "\"minimal\", \"patience\" and \"histogram\""));
5147 options
->ignore_driver_algorithm
= 1;
5152 static int diff_opt_diff_algorithm_no_arg(const struct option
*opt
,
5153 const char *arg
, int unset
)
5155 struct diff_options
*options
= opt
->value
;
5157 BUG_ON_OPT_NEG(unset
);
5158 BUG_ON_OPT_ARG(arg
);
5160 if (set_diff_algorithm(options
, opt
->long_name
))
5161 BUG("available diff algorithms include \"myers\", "
5162 "\"minimal\", \"patience\" and \"histogram\"");
5164 options
->ignore_driver_algorithm
= 1;
5169 static int diff_opt_dirstat(const struct option
*opt
,
5170 const char *arg
, int unset
)
5172 struct diff_options
*options
= opt
->value
;
5174 BUG_ON_OPT_NEG(unset
);
5175 if (!strcmp(opt
->long_name
, "cumulative")) {
5177 BUG("how come --cumulative take a value?");
5179 } else if (!strcmp(opt
->long_name
, "dirstat-by-file"))
5180 parse_dirstat_opt(options
, "files");
5181 parse_dirstat_opt(options
, arg
? arg
: "");
5185 static int diff_opt_find_copies(const struct option
*opt
,
5186 const char *arg
, int unset
)
5188 struct diff_options
*options
= opt
->value
;
5190 BUG_ON_OPT_NEG(unset
);
5193 options
->rename_score
= parse_rename_score(&arg
);
5195 return error(_("invalid argument to %s"), opt
->long_name
);
5197 if (options
->detect_rename
== DIFF_DETECT_COPY
)
5198 options
->flags
.find_copies_harder
= 1;
5200 options
->detect_rename
= DIFF_DETECT_COPY
;
5205 static int diff_opt_find_renames(const struct option
*opt
,
5206 const char *arg
, int unset
)
5208 struct diff_options
*options
= opt
->value
;
5210 BUG_ON_OPT_NEG(unset
);
5213 options
->rename_score
= parse_rename_score(&arg
);
5215 return error(_("invalid argument to %s"), opt
->long_name
);
5217 options
->detect_rename
= DIFF_DETECT_RENAME
;
5221 static int diff_opt_follow(const struct option
*opt
,
5222 const char *arg
, int unset
)
5224 struct diff_options
*options
= opt
->value
;
5226 BUG_ON_OPT_ARG(arg
);
5228 options
->flags
.follow_renames
= 0;
5229 options
->flags
.default_follow_renames
= 0;
5231 options
->flags
.follow_renames
= 1;
5236 static int diff_opt_ignore_submodules(const struct option
*opt
,
5237 const char *arg
, int unset
)
5239 struct diff_options
*options
= opt
->value
;
5241 BUG_ON_OPT_NEG(unset
);
5244 options
->flags
.override_submodule_config
= 1;
5245 handle_ignore_submodules_arg(options
, arg
);
5249 static int diff_opt_line_prefix(const struct option
*opt
,
5250 const char *optarg
, int unset
)
5252 struct diff_options
*options
= opt
->value
;
5254 BUG_ON_OPT_NEG(unset
);
5255 options
->line_prefix
= optarg
;
5256 options
->line_prefix_length
= strlen(options
->line_prefix
);
5257 graph_setup_line_prefix(options
);
5261 static int diff_opt_no_prefix(const struct option
*opt
,
5262 const char *optarg
, int unset
)
5264 struct diff_options
*options
= opt
->value
;
5266 BUG_ON_OPT_NEG(unset
);
5267 BUG_ON_OPT_ARG(optarg
);
5268 options
->a_prefix
= "";
5269 options
->b_prefix
= "";
5273 static enum parse_opt_result
diff_opt_output(struct parse_opt_ctx_t
*ctx
,
5274 const struct option
*opt
,
5275 const char *arg
, int unset
)
5277 struct diff_options
*options
= opt
->value
;
5280 BUG_ON_OPT_NEG(unset
);
5281 path
= prefix_filename(ctx
->prefix
, arg
);
5282 options
->file
= xfopen(path
, "w");
5283 options
->close_file
= 1;
5284 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
5285 options
->use_color
= GIT_COLOR_NEVER
;
5290 static int diff_opt_patience(const struct option
*opt
,
5291 const char *arg
, int unset
)
5293 struct diff_options
*options
= opt
->value
;
5296 BUG_ON_OPT_NEG(unset
);
5297 BUG_ON_OPT_ARG(arg
);
5299 * Both --patience and --anchored use PATIENCE_DIFF
5300 * internally, so remove any anchors previously
5303 for (i
= 0; i
< options
->anchors_nr
; i
++)
5304 free(options
->anchors
[i
]);
5305 options
->anchors_nr
= 0;
5306 options
->ignore_driver_algorithm
= 1;
5308 return set_diff_algorithm(options
, "patience");
5311 static int diff_opt_ignore_regex(const struct option
*opt
,
5312 const char *arg
, int unset
)
5314 struct diff_options
*options
= opt
->value
;
5317 BUG_ON_OPT_NEG(unset
);
5318 regex
= xmalloc(sizeof(*regex
));
5319 if (regcomp(regex
, arg
, REG_EXTENDED
| REG_NEWLINE
))
5320 return error(_("invalid regex given to -I: '%s'"), arg
);
5321 ALLOC_GROW(options
->ignore_regex
, options
->ignore_regex_nr
+ 1,
5322 options
->ignore_regex_alloc
);
5323 options
->ignore_regex
[options
->ignore_regex_nr
++] = regex
;
5327 static int diff_opt_pickaxe_regex(const struct option
*opt
,
5328 const char *arg
, int unset
)
5330 struct diff_options
*options
= opt
->value
;
5332 BUG_ON_OPT_NEG(unset
);
5333 options
->pickaxe
= arg
;
5334 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
5338 static int diff_opt_pickaxe_string(const struct option
*opt
,
5339 const char *arg
, int unset
)
5341 struct diff_options
*options
= opt
->value
;
5343 BUG_ON_OPT_NEG(unset
);
5344 options
->pickaxe
= arg
;
5345 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
5349 static int diff_opt_relative(const struct option
*opt
,
5350 const char *arg
, int unset
)
5352 struct diff_options
*options
= opt
->value
;
5354 options
->flags
.relative_name
= !unset
;
5356 options
->prefix
= arg
;
5360 static int diff_opt_submodule(const struct option
*opt
,
5361 const char *arg
, int unset
)
5363 struct diff_options
*options
= opt
->value
;
5365 BUG_ON_OPT_NEG(unset
);
5368 if (parse_submodule_params(options
, arg
))
5369 return error(_("failed to parse --submodule option parameter: '%s'"),
5374 static int diff_opt_textconv(const struct option
*opt
,
5375 const char *arg
, int unset
)
5377 struct diff_options
*options
= opt
->value
;
5379 BUG_ON_OPT_ARG(arg
);
5381 options
->flags
.allow_textconv
= 0;
5383 options
->flags
.allow_textconv
= 1;
5384 options
->flags
.textconv_set_via_cmdline
= 1;
5389 static int diff_opt_unified(const struct option
*opt
,
5390 const char *arg
, int unset
)
5392 struct diff_options
*options
= opt
->value
;
5395 BUG_ON_OPT_NEG(unset
);
5398 options
->context
= strtol(arg
, &s
, 10);
5400 return error(_("%s expects a numerical value"), "--unified");
5402 enable_patch_output(&options
->output_format
);
5407 static int diff_opt_word_diff(const struct option
*opt
,
5408 const char *arg
, int unset
)
5410 struct diff_options
*options
= opt
->value
;
5412 BUG_ON_OPT_NEG(unset
);
5414 if (!strcmp(arg
, "plain"))
5415 options
->word_diff
= DIFF_WORDS_PLAIN
;
5416 else if (!strcmp(arg
, "color")) {
5417 options
->use_color
= 1;
5418 options
->word_diff
= DIFF_WORDS_COLOR
;
5420 else if (!strcmp(arg
, "porcelain"))
5421 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
5422 else if (!strcmp(arg
, "none"))
5423 options
->word_diff
= DIFF_WORDS_NONE
;
5425 return error(_("bad --word-diff argument: %s"), arg
);
5427 if (options
->word_diff
== DIFF_WORDS_NONE
)
5428 options
->word_diff
= DIFF_WORDS_PLAIN
;
5433 static int diff_opt_word_diff_regex(const struct option
*opt
,
5434 const char *arg
, int unset
)
5436 struct diff_options
*options
= opt
->value
;
5438 BUG_ON_OPT_NEG(unset
);
5439 if (options
->word_diff
== DIFF_WORDS_NONE
)
5440 options
->word_diff
= DIFF_WORDS_PLAIN
;
5441 options
->word_regex
= arg
;
5445 static int diff_opt_rotate_to(const struct option
*opt
, const char *arg
, int unset
)
5447 struct diff_options
*options
= opt
->value
;
5449 BUG_ON_OPT_NEG(unset
);
5450 if (!strcmp(opt
->long_name
, "skip-to"))
5451 options
->skip_instead_of_rotate
= 1;
5453 options
->skip_instead_of_rotate
= 0;
5454 options
->rotate_to
= arg
;
5458 struct option
*add_diff_options(const struct option
*opts
,
5459 struct diff_options
*options
)
5461 struct option parseopts
[] = {
5462 OPT_GROUP(N_("Diff output format options")),
5463 OPT_BITOP('p', "patch", &options
->output_format
,
5464 N_("generate patch"),
5465 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5466 OPT_BIT_F('s', "no-patch", &options
->output_format
,
5467 N_("suppress diff output"),
5468 DIFF_FORMAT_NO_OUTPUT
, PARSE_OPT_NONEG
),
5469 OPT_BITOP('u', NULL
, &options
->output_format
,
5470 N_("generate patch"),
5471 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5472 OPT_CALLBACK_F('U', "unified", options
, N_("<n>"),
5473 N_("generate diffs with <n> lines context"),
5474 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_unified
),
5475 OPT_BOOL('W', "function-context", &options
->flags
.funccontext
,
5476 N_("generate diffs with <n> lines context")),
5477 OPT_BIT_F(0, "raw", &options
->output_format
,
5478 N_("generate the diff in raw format"),
5479 DIFF_FORMAT_RAW
, PARSE_OPT_NONEG
),
5480 OPT_BITOP(0, "patch-with-raw", &options
->output_format
,
5481 N_("synonym for '-p --raw'"),
5482 DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
,
5483 DIFF_FORMAT_NO_OUTPUT
),
5484 OPT_BITOP(0, "patch-with-stat", &options
->output_format
,
5485 N_("synonym for '-p --stat'"),
5486 DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
,
5487 DIFF_FORMAT_NO_OUTPUT
),
5488 OPT_BIT_F(0, "numstat", &options
->output_format
,
5489 N_("machine friendly --stat"),
5490 DIFF_FORMAT_NUMSTAT
, PARSE_OPT_NONEG
),
5491 OPT_BIT_F(0, "shortstat", &options
->output_format
,
5492 N_("output only the last line of --stat"),
5493 DIFF_FORMAT_SHORTSTAT
, PARSE_OPT_NONEG
),
5494 OPT_CALLBACK_F('X', "dirstat", options
, N_("<param1,param2>..."),
5495 N_("output the distribution of relative amount of changes for each sub-directory"),
5496 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5498 OPT_CALLBACK_F(0, "cumulative", options
, NULL
,
5499 N_("synonym for --dirstat=cumulative"),
5500 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5502 OPT_CALLBACK_F(0, "dirstat-by-file", options
, N_("<param1,param2>..."),
5503 N_("synonym for --dirstat=files,param1,param2..."),
5504 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5506 OPT_BIT_F(0, "check", &options
->output_format
,
5507 N_("warn if changes introduce conflict markers or whitespace errors"),
5508 DIFF_FORMAT_CHECKDIFF
, PARSE_OPT_NONEG
),
5509 OPT_BIT_F(0, "summary", &options
->output_format
,
5510 N_("condensed summary such as creations, renames and mode changes"),
5511 DIFF_FORMAT_SUMMARY
, PARSE_OPT_NONEG
),
5512 OPT_BIT_F(0, "name-only", &options
->output_format
,
5513 N_("show only names of changed files"),
5514 DIFF_FORMAT_NAME
, PARSE_OPT_NONEG
),
5515 OPT_BIT_F(0, "name-status", &options
->output_format
,
5516 N_("show only names and status of changed files"),
5517 DIFF_FORMAT_NAME_STATUS
, PARSE_OPT_NONEG
),
5518 OPT_CALLBACK_F(0, "stat", options
, N_("<width>[,<name-width>[,<count>]]"),
5519 N_("generate diffstat"),
5520 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_stat
),
5521 OPT_CALLBACK_F(0, "stat-width", options
, N_("<width>"),
5522 N_("generate diffstat with a given width"),
5523 PARSE_OPT_NONEG
, diff_opt_stat
),
5524 OPT_CALLBACK_F(0, "stat-name-width", options
, N_("<width>"),
5525 N_("generate diffstat with a given name width"),
5526 PARSE_OPT_NONEG
, diff_opt_stat
),
5527 OPT_CALLBACK_F(0, "stat-graph-width", options
, N_("<width>"),
5528 N_("generate diffstat with a given graph width"),
5529 PARSE_OPT_NONEG
, diff_opt_stat
),
5530 OPT_CALLBACK_F(0, "stat-count", options
, N_("<count>"),
5531 N_("generate diffstat with limited lines"),
5532 PARSE_OPT_NONEG
, diff_opt_stat
),
5533 OPT_CALLBACK_F(0, "compact-summary", options
, NULL
,
5534 N_("generate compact summary in diffstat"),
5535 PARSE_OPT_NOARG
, diff_opt_compact_summary
),
5536 OPT_CALLBACK_F(0, "binary", options
, NULL
,
5537 N_("output a binary diff that can be applied"),
5538 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_binary
),
5539 OPT_BOOL(0, "full-index", &options
->flags
.full_index
,
5540 N_("show full pre- and post-image object names on the \"index\" lines")),
5541 OPT_COLOR_FLAG(0, "color", &options
->use_color
,
5542 N_("show colored diff")),
5543 OPT_CALLBACK_F(0, "ws-error-highlight", options
, N_("<kind>"),
5544 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5545 PARSE_OPT_NONEG
, diff_opt_ws_error_highlight
),
5546 OPT_SET_INT('z', NULL
, &options
->line_termination
,
5547 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5549 OPT__ABBREV(&options
->abbrev
),
5550 OPT_STRING_F(0, "src-prefix", &options
->a_prefix
, N_("<prefix>"),
5551 N_("show the given source prefix instead of \"a/\""),
5553 OPT_STRING_F(0, "dst-prefix", &options
->b_prefix
, N_("<prefix>"),
5554 N_("show the given destination prefix instead of \"b/\""),
5556 OPT_CALLBACK_F(0, "line-prefix", options
, N_("<prefix>"),
5557 N_("prepend an additional prefix to every line of output"),
5558 PARSE_OPT_NONEG
, diff_opt_line_prefix
),
5559 OPT_CALLBACK_F(0, "no-prefix", options
, NULL
,
5560 N_("do not show any source or destination prefix"),
5561 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_no_prefix
),
5562 OPT_INTEGER_F(0, "inter-hunk-context", &options
->interhunkcontext
,
5563 N_("show context between diff hunks up to the specified number of lines"),
5565 OPT_CALLBACK_F(0, "output-indicator-new",
5566 &options
->output_indicators
[OUTPUT_INDICATOR_NEW
],
5568 N_("specify the character to indicate a new line instead of '+'"),
5569 PARSE_OPT_NONEG
, diff_opt_char
),
5570 OPT_CALLBACK_F(0, "output-indicator-old",
5571 &options
->output_indicators
[OUTPUT_INDICATOR_OLD
],
5573 N_("specify the character to indicate an old line instead of '-'"),
5574 PARSE_OPT_NONEG
, diff_opt_char
),
5575 OPT_CALLBACK_F(0, "output-indicator-context",
5576 &options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
],
5578 N_("specify the character to indicate a context instead of ' '"),
5579 PARSE_OPT_NONEG
, diff_opt_char
),
5581 OPT_GROUP(N_("Diff rename options")),
5582 OPT_CALLBACK_F('B', "break-rewrites", &options
->break_opt
, N_("<n>[/<m>]"),
5583 N_("break complete rewrite changes into pairs of delete and create"),
5584 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5585 diff_opt_break_rewrites
),
5586 OPT_CALLBACK_F('M', "find-renames", options
, N_("<n>"),
5587 N_("detect renames"),
5588 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5589 diff_opt_find_renames
),
5590 OPT_SET_INT_F('D', "irreversible-delete", &options
->irreversible_delete
,
5591 N_("omit the preimage for deletes"),
5592 1, PARSE_OPT_NONEG
),
5593 OPT_CALLBACK_F('C', "find-copies", options
, N_("<n>"),
5594 N_("detect copies"),
5595 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5596 diff_opt_find_copies
),
5597 OPT_BOOL(0, "find-copies-harder", &options
->flags
.find_copies_harder
,
5598 N_("use unmodified files as source to find copies")),
5599 OPT_SET_INT_F(0, "no-renames", &options
->detect_rename
,
5600 N_("disable rename detection"),
5601 0, PARSE_OPT_NONEG
),
5602 OPT_BOOL(0, "rename-empty", &options
->flags
.rename_empty
,
5603 N_("use empty blobs as rename source")),
5604 OPT_CALLBACK_F(0, "follow", options
, NULL
,
5605 N_("continue listing the history of a file beyond renames"),
5606 PARSE_OPT_NOARG
, diff_opt_follow
),
5607 OPT_INTEGER('l', NULL
, &options
->rename_limit
,
5608 N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
5610 OPT_GROUP(N_("Diff algorithm options")),
5611 OPT_CALLBACK_F(0, "minimal", options
, NULL
,
5612 N_("produce the smallest possible diff"),
5613 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5614 diff_opt_diff_algorithm_no_arg
),
5615 OPT_BIT_F('w', "ignore-all-space", &options
->xdl_opts
,
5616 N_("ignore whitespace when comparing lines"),
5617 XDF_IGNORE_WHITESPACE
, PARSE_OPT_NONEG
),
5618 OPT_BIT_F('b', "ignore-space-change", &options
->xdl_opts
,
5619 N_("ignore changes in amount of whitespace"),
5620 XDF_IGNORE_WHITESPACE_CHANGE
, PARSE_OPT_NONEG
),
5621 OPT_BIT_F(0, "ignore-space-at-eol", &options
->xdl_opts
,
5622 N_("ignore changes in whitespace at EOL"),
5623 XDF_IGNORE_WHITESPACE_AT_EOL
, PARSE_OPT_NONEG
),
5624 OPT_BIT_F(0, "ignore-cr-at-eol", &options
->xdl_opts
,
5625 N_("ignore carrier-return at the end of line"),
5626 XDF_IGNORE_CR_AT_EOL
, PARSE_OPT_NONEG
),
5627 OPT_BIT_F(0, "ignore-blank-lines", &options
->xdl_opts
,
5628 N_("ignore changes whose lines are all blank"),
5629 XDF_IGNORE_BLANK_LINES
, PARSE_OPT_NONEG
),
5630 OPT_CALLBACK_F('I', "ignore-matching-lines", options
, N_("<regex>"),
5631 N_("ignore changes whose all lines match <regex>"),
5632 0, diff_opt_ignore_regex
),
5633 OPT_BIT(0, "indent-heuristic", &options
->xdl_opts
,
5634 N_("heuristic to shift diff hunk boundaries for easy reading"),
5635 XDF_INDENT_HEURISTIC
),
5636 OPT_CALLBACK_F(0, "patience", options
, NULL
,
5637 N_("generate diff using the \"patience diff\" algorithm"),
5638 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5640 OPT_CALLBACK_F(0, "histogram", options
, NULL
,
5641 N_("generate diff using the \"histogram diff\" algorithm"),
5642 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5643 diff_opt_diff_algorithm_no_arg
),
5644 OPT_CALLBACK_F(0, "diff-algorithm", options
, N_("<algorithm>"),
5645 N_("choose a diff algorithm"),
5646 PARSE_OPT_NONEG
, diff_opt_diff_algorithm
),
5647 OPT_CALLBACK_F(0, "anchored", options
, N_("<text>"),
5648 N_("generate diff using the \"anchored diff\" algorithm"),
5649 PARSE_OPT_NONEG
, diff_opt_anchored
),
5650 OPT_CALLBACK_F(0, "word-diff", options
, N_("<mode>"),
5651 N_("show word diff, using <mode> to delimit changed words"),
5652 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_word_diff
),
5653 OPT_CALLBACK_F(0, "word-diff-regex", options
, N_("<regex>"),
5654 N_("use <regex> to decide what a word is"),
5655 PARSE_OPT_NONEG
, diff_opt_word_diff_regex
),
5656 OPT_CALLBACK_F(0, "color-words", options
, N_("<regex>"),
5657 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5658 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_color_words
),
5659 OPT_CALLBACK_F(0, "color-moved", options
, N_("<mode>"),
5660 N_("moved lines of code are colored differently"),
5661 PARSE_OPT_OPTARG
, diff_opt_color_moved
),
5662 OPT_CALLBACK_F(0, "color-moved-ws", options
, N_("<mode>"),
5663 N_("how white spaces are ignored in --color-moved"),
5664 0, diff_opt_color_moved_ws
),
5666 OPT_GROUP(N_("Other diff options")),
5667 OPT_CALLBACK_F(0, "relative", options
, N_("<prefix>"),
5668 N_("when run from subdir, exclude changes outside and show relative paths"),
5671 OPT_BOOL('a', "text", &options
->flags
.text
,
5672 N_("treat all files as text")),
5673 OPT_BOOL('R', NULL
, &options
->flags
.reverse_diff
,
5674 N_("swap two inputs, reverse the diff")),
5675 OPT_BOOL(0, "exit-code", &options
->flags
.exit_with_status
,
5676 N_("exit with 1 if there were differences, 0 otherwise")),
5677 OPT_BOOL(0, "quiet", &options
->flags
.quick
,
5678 N_("disable all output of the program")),
5679 OPT_BOOL(0, "ext-diff", &options
->flags
.allow_external
,
5680 N_("allow an external diff helper to be executed")),
5681 OPT_CALLBACK_F(0, "textconv", options
, NULL
,
5682 N_("run external text conversion filters when comparing binary files"),
5683 PARSE_OPT_NOARG
, diff_opt_textconv
),
5684 OPT_CALLBACK_F(0, "ignore-submodules", options
, N_("<when>"),
5685 N_("ignore changes to submodules in the diff generation"),
5686 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5687 diff_opt_ignore_submodules
),
5688 OPT_CALLBACK_F(0, "submodule", options
, N_("<format>"),
5689 N_("specify how differences in submodules are shown"),
5690 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5691 diff_opt_submodule
),
5692 OPT_SET_INT_F(0, "ita-invisible-in-index", &options
->ita_invisible_in_index
,
5693 N_("hide 'git add -N' entries from the index"),
5694 1, PARSE_OPT_NONEG
),
5695 OPT_SET_INT_F(0, "ita-visible-in-index", &options
->ita_invisible_in_index
,
5696 N_("treat 'git add -N' entries as real in the index"),
5697 0, PARSE_OPT_NONEG
),
5698 OPT_CALLBACK_F('S', NULL
, options
, N_("<string>"),
5699 N_("look for differences that change the number of occurrences of the specified string"),
5700 0, diff_opt_pickaxe_string
),
5701 OPT_CALLBACK_F('G', NULL
, options
, N_("<regex>"),
5702 N_("look for differences that change the number of occurrences of the specified regex"),
5703 0, diff_opt_pickaxe_regex
),
5704 OPT_BIT_F(0, "pickaxe-all", &options
->pickaxe_opts
,
5705 N_("show all changes in the changeset with -S or -G"),
5706 DIFF_PICKAXE_ALL
, PARSE_OPT_NONEG
),
5707 OPT_BIT_F(0, "pickaxe-regex", &options
->pickaxe_opts
,
5708 N_("treat <string> in -S as extended POSIX regular expression"),
5709 DIFF_PICKAXE_REGEX
, PARSE_OPT_NONEG
),
5710 OPT_FILENAME('O', NULL
, &options
->orderfile
,
5711 N_("control the order in which files appear in the output")),
5712 OPT_CALLBACK_F(0, "rotate-to", options
, N_("<path>"),
5713 N_("show the change in the specified path first"),
5714 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5715 OPT_CALLBACK_F(0, "skip-to", options
, N_("<path>"),
5716 N_("skip the output to the specified path"),
5717 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5718 OPT_CALLBACK_F(0, "find-object", options
, N_("<object-id>"),
5719 N_("look for differences that change the number of occurrences of the specified object"),
5720 PARSE_OPT_NONEG
, diff_opt_find_object
),
5721 OPT_CALLBACK_F(0, "diff-filter", options
, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5722 N_("select files by diff type"),
5723 PARSE_OPT_NONEG
, diff_opt_diff_filter
),
5724 { OPTION_CALLBACK
, 0, "output", options
, N_("<file>"),
5725 N_("output to a specific file"),
5726 PARSE_OPT_NONEG
, NULL
, 0, diff_opt_output
},
5731 return parse_options_concat(opts
, parseopts
);
5734 int diff_opt_parse(struct diff_options
*options
,
5735 const char **av
, int ac
, const char *prefix
)
5737 struct option no_options
[] = { OPT_END() };
5738 struct option
*parseopts
= add_diff_options(no_options
, options
);
5743 ac
= parse_options(ac
, av
, prefix
, parseopts
, NULL
,
5744 PARSE_OPT_KEEP_DASHDASH
|
5745 PARSE_OPT_KEEP_UNKNOWN_OPT
|
5746 PARSE_OPT_NO_INTERNAL_HELP
|
5747 PARSE_OPT_ONE_SHOT
|
5748 PARSE_OPT_STOP_AT_NON_OPTION
);
5754 int parse_rename_score(const char **cp_p
)
5756 unsigned long num
, scale
;
5758 const char *cp
= *cp_p
;
5765 if ( !dot
&& ch
== '.' ) {
5768 } else if ( ch
== '%' ) {
5769 scale
= dot
? scale
*100 : 100;
5770 cp
++; /* % is always at the end */
5772 } else if ( ch
>= '0' && ch
<= '9' ) {
5773 if ( scale
< 100000 ) {
5775 num
= (num
*10) + (ch
-'0');
5784 /* user says num divided by scale and we say internally that
5785 * is MAX_SCORE * num / scale.
5787 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
5790 struct diff_queue_struct diff_queued_diff
;
5792 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
5794 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
5795 queue
->queue
[queue
->nr
++] = dp
;
5798 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
5799 struct diff_filespec
*one
,
5800 struct diff_filespec
*two
)
5802 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
5810 void diff_free_filepair(struct diff_filepair
*p
)
5812 free_filespec(p
->one
);
5813 free_filespec(p
->two
);
5817 void diff_free_queue(struct diff_queue_struct
*q
)
5819 for (int i
= 0; i
< q
->nr
; i
++)
5820 diff_free_filepair(q
->queue
[i
]);
5824 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
5829 /* Do we want all 40 hex characters? */
5830 if (len
== the_hash_algo
->hexsz
)
5831 return oid_to_hex(oid
);
5833 /* An abbreviated value is fine, possibly followed by an ellipsis. */
5834 abbrev
= diff_abbrev_oid(oid
, len
);
5836 if (!print_sha1_ellipsis())
5839 abblen
= strlen(abbrev
);
5842 * In well-behaved cases, where the abbreviated result is the
5843 * same as the requested length, append three dots after the
5844 * abbreviation (hence the whole logic is limited to the case
5845 * where abblen < 37); when the actual abbreviated result is a
5846 * bit longer than the requested length, we reduce the number
5847 * of dots so that they match the well-behaved ones. However,
5848 * if the actual abbreviation is longer than the requested
5849 * length by more than three, we give up on aligning, and add
5850 * three dots anyway, to indicate that the output is not the
5851 * full object name. Yes, this may be suboptimal, but this
5852 * appears only in "diff --raw --abbrev" output and it is not
5853 * worth the effort to change it now. Note that this would
5854 * likely to work fine when the automatic sizing of default
5855 * abbreviation length is used--we would be fed -1 in "len" in
5856 * that case, and will end up always appending three-dots, but
5857 * the automatic sizing is supposed to give abblen that ensures
5858 * uniqueness across all objects (statistically speaking).
5860 if (abblen
< the_hash_algo
->hexsz
- 3) {
5861 static char hex
[GIT_MAX_HEXSZ
+ 1];
5862 if (len
< abblen
&& abblen
<= len
+ 2)
5863 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
5865 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
5869 return oid_to_hex(oid
);
5872 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
5874 int line_termination
= opt
->line_termination
;
5875 int inter_name_termination
= line_termination
? '\t' : '\0';
5877 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5878 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
5879 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
5880 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
5881 fprintf(opt
->file
, "%s ",
5882 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
5885 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
5886 inter_name_termination
);
5888 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
5891 if (p
->status
== DIFF_STATUS_COPIED
||
5892 p
->status
== DIFF_STATUS_RENAMED
) {
5893 const char *name_a
, *name_b
;
5894 name_a
= p
->one
->path
;
5895 name_b
= p
->two
->path
;
5896 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5897 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
5898 write_name_quoted(name_b
, opt
->file
, line_termination
);
5900 const char *name_a
, *name_b
;
5901 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
5903 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5904 write_name_quoted(name_a
, opt
->file
, line_termination
);
5908 int diff_unmodified_pair(struct diff_filepair
*p
)
5910 /* This function is written stricter than necessary to support
5911 * the currently implemented transformers, but the idea is to
5912 * let transformers to produce diff_filepairs any way they want,
5913 * and filter and clean them up here before producing the output.
5915 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
5917 if (DIFF_PAIR_UNMERGED(p
))
5918 return 0; /* unmerged is interesting */
5920 /* deletion, addition, mode or type change
5921 * and rename are all interesting.
5923 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
5924 DIFF_PAIR_MODE_CHANGED(p
) ||
5925 strcmp(one
->path
, two
->path
))
5928 /* both are valid and point at the same path. that is, we are
5929 * dealing with a change.
5931 if (one
->oid_valid
&& two
->oid_valid
&&
5932 oideq(&one
->oid
, &two
->oid
) &&
5933 !one
->dirty_submodule
&& !two
->dirty_submodule
)
5934 return 1; /* no change */
5935 if (!one
->oid_valid
&& !two
->oid_valid
)
5936 return 1; /* both look at the same file on the filesystem. */
5940 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
5942 int include_conflict_headers
=
5943 (additional_headers(o
, p
->one
->path
) &&
5945 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
5948 * Check if we can return early without showing a diff. Note that
5949 * diff_filepair only stores {oid, path, mode, is_valid}
5950 * information for each path, and thus diff_unmodified_pair() only
5951 * considers those bits of info. However, we do not want pairs
5952 * created by create_filepairs_for_header_only_notifications()
5953 * (which always look like unmodified pairs) to be ignored, so
5954 * return early if both p is unmodified AND we don't want to
5955 * include_conflict_headers.
5957 if (diff_unmodified_pair(p
) && !include_conflict_headers
)
5960 /* Actually, we can also return early to avoid showing tree diffs */
5961 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5962 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5968 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
5969 struct diffstat_t
*diffstat
)
5971 if (diff_unmodified_pair(p
))
5974 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5975 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5976 return; /* no useful stat for tree diffs */
5978 run_diffstat(p
, o
, diffstat
);
5981 static void diff_flush_checkdiff(struct diff_filepair
*p
,
5982 struct diff_options
*o
)
5984 if (diff_unmodified_pair(p
))
5987 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5988 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5989 return; /* nothing to check in tree diffs */
5991 run_checkdiff(p
, o
);
5994 int diff_queue_is_empty(struct diff_options
*o
)
5996 struct diff_queue_struct
*q
= &diff_queued_diff
;
5998 int include_conflict_headers
=
5999 (o
->additional_path_headers
&&
6000 strmap_get_size(o
->additional_path_headers
) &&
6002 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
6004 if (include_conflict_headers
)
6007 for (i
= 0; i
< q
->nr
; i
++)
6008 if (!diff_unmodified_pair(q
->queue
[i
]))
6014 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
6016 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
6019 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
6021 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
6022 fprintf(stderr
, "queue[%d] %s size %lu\n",
6027 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
6029 diff_debug_filespec(p
->one
, i
, "one");
6030 diff_debug_filespec(p
->two
, i
, "two");
6031 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
6032 p
->score
, p
->status
? p
->status
: '?',
6033 p
->one
->rename_used
, p
->broken_pair
);
6036 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
6040 fprintf(stderr
, "%s\n", msg
);
6041 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
6042 for (i
= 0; i
< q
->nr
; i
++) {
6043 struct diff_filepair
*p
= q
->queue
[i
];
6044 diff_debug_filepair(p
, i
);
6049 static void diff_resolve_rename_copy(void)
6052 struct diff_filepair
*p
;
6053 struct diff_queue_struct
*q
= &diff_queued_diff
;
6055 diff_debug_queue("resolve-rename-copy", q
);
6057 for (i
= 0; i
< q
->nr
; i
++) {
6059 p
->status
= 0; /* undecided */
6060 if (DIFF_PAIR_UNMERGED(p
))
6061 p
->status
= DIFF_STATUS_UNMERGED
;
6062 else if (!DIFF_FILE_VALID(p
->one
))
6063 p
->status
= DIFF_STATUS_ADDED
;
6064 else if (!DIFF_FILE_VALID(p
->two
))
6065 p
->status
= DIFF_STATUS_DELETED
;
6066 else if (DIFF_PAIR_TYPE_CHANGED(p
))
6067 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
6069 /* from this point on, we are dealing with a pair
6070 * whose both sides are valid and of the same type, i.e.
6071 * either in-place edit or rename/copy edit.
6073 else if (DIFF_PAIR_RENAME(p
)) {
6075 * A rename might have re-connected a broken
6076 * pair up, causing the pathnames to be the
6077 * same again. If so, that's not a rename at
6078 * all, just a modification..
6080 * Otherwise, see if this source was used for
6081 * multiple renames, in which case we decrement
6082 * the count, and call it a copy.
6084 if (!strcmp(p
->one
->path
, p
->two
->path
))
6085 p
->status
= DIFF_STATUS_MODIFIED
;
6086 else if (--p
->one
->rename_used
> 0)
6087 p
->status
= DIFF_STATUS_COPIED
;
6089 p
->status
= DIFF_STATUS_RENAMED
;
6091 else if (!oideq(&p
->one
->oid
, &p
->two
->oid
) ||
6092 p
->one
->mode
!= p
->two
->mode
||
6093 p
->one
->dirty_submodule
||
6094 p
->two
->dirty_submodule
||
6095 is_null_oid(&p
->one
->oid
))
6096 p
->status
= DIFF_STATUS_MODIFIED
;
6098 /* This is a "no-change" entry and should not
6099 * happen anymore, but prepare for broken callers.
6101 error("feeding unmodified %s to diffcore",
6103 p
->status
= DIFF_STATUS_UNKNOWN
;
6106 diff_debug_queue("resolve-rename-copy done", q
);
6109 static int check_pair_status(struct diff_filepair
*p
)
6111 switch (p
->status
) {
6112 case DIFF_STATUS_UNKNOWN
:
6115 die("internal error in diff-resolve-rename-copy");
6121 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
6123 int fmt
= opt
->output_format
;
6125 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
6126 diff_flush_checkdiff(p
, opt
);
6127 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
6128 diff_flush_raw(p
, opt
);
6129 else if (fmt
& DIFF_FORMAT_NAME
) {
6130 const char *name_a
, *name_b
;
6131 name_a
= p
->two
->path
;
6133 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6134 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
6135 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
6139 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
6141 struct strbuf sb
= STRBUF_INIT
;
6143 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
6145 strbuf_addf(&sb
, " %s ", newdelete
);
6147 quote_c_style(fs
->path
, &sb
, NULL
, 0);
6148 strbuf_addch(&sb
, '\n');
6149 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6151 strbuf_release(&sb
);
6154 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
6157 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
6158 struct strbuf sb
= STRBUF_INIT
;
6159 strbuf_addf(&sb
, " mode change %06o => %06o",
6160 p
->one
->mode
, p
->two
->mode
);
6162 strbuf_addch(&sb
, ' ');
6163 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6165 strbuf_addch(&sb
, '\n');
6166 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6168 strbuf_release(&sb
);
6172 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
6173 struct diff_filepair
*p
)
6175 struct strbuf sb
= STRBUF_INIT
;
6176 struct strbuf names
= STRBUF_INIT
;
6178 pprint_rename(&names
, p
->one
->path
, p
->two
->path
);
6179 strbuf_addf(&sb
, " %s %s (%d%%)\n",
6180 renamecopy
, names
.buf
, similarity_index(p
));
6181 strbuf_release(&names
);
6182 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6184 show_mode_change(opt
, p
, 0);
6185 strbuf_release(&sb
);
6188 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
6191 case DIFF_STATUS_DELETED
:
6192 show_file_mode_name(opt
, "delete", p
->one
);
6194 case DIFF_STATUS_ADDED
:
6195 show_file_mode_name(opt
, "create", p
->two
);
6197 case DIFF_STATUS_COPIED
:
6198 show_rename_copy(opt
, "copy", p
);
6200 case DIFF_STATUS_RENAMED
:
6201 show_rename_copy(opt
, "rename", p
);
6205 struct strbuf sb
= STRBUF_INIT
;
6206 strbuf_addstr(&sb
, " rewrite ");
6207 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6208 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
6209 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6211 strbuf_release(&sb
);
6213 show_mode_change(opt
, p
, !p
->score
);
6223 static int remove_space(char *line
, int len
)
6229 for (i
= 0; i
< len
; i
++)
6230 if (!isspace((c
= line
[i
])))
6236 void flush_one_hunk(struct object_id
*result
, git_hash_ctx
*ctx
)
6238 unsigned char hash
[GIT_MAX_RAWSZ
];
6239 unsigned short carry
= 0;
6242 the_hash_algo
->final_fn(hash
, ctx
);
6243 the_hash_algo
->init_fn(ctx
);
6244 /* 20-byte sum, with carry */
6245 for (i
= 0; i
< the_hash_algo
->rawsz
; ++i
) {
6246 carry
+= result
->hash
[i
] + hash
[i
];
6247 result
->hash
[i
] = carry
;
6252 static int patch_id_consume(void *priv
, char *line
, unsigned long len
)
6254 struct patch_id_t
*data
= priv
;
6257 if (len
> 12 && starts_with(line
, "\\ "))
6259 new_len
= remove_space(line
, len
);
6261 the_hash_algo
->update_fn(data
->ctx
, line
, new_len
);
6262 data
->patchlen
+= new_len
;
6266 static void patch_id_add_string(git_hash_ctx
*ctx
, const char *str
)
6268 the_hash_algo
->update_fn(ctx
, str
, strlen(str
));
6271 static void patch_id_add_mode(git_hash_ctx
*ctx
, unsigned mode
)
6273 /* large enough for 2^32 in octal */
6275 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
6276 the_hash_algo
->update_fn(ctx
, buf
, len
);
6279 /* returns 0 upon success, and writes result into oid */
6280 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
6282 struct diff_queue_struct
*q
= &diff_queued_diff
;
6285 struct patch_id_t data
;
6287 the_hash_algo
->init_fn(&ctx
);
6288 memset(&data
, 0, sizeof(struct patch_id_t
));
6292 for (i
= 0; i
< q
->nr
; i
++) {
6296 struct diff_filepair
*p
= q
->queue
[i
];
6299 memset(&xpp
, 0, sizeof(xpp
));
6300 memset(&xecfg
, 0, sizeof(xecfg
));
6302 return error("internal diff status error");
6303 if (p
->status
== DIFF_STATUS_UNKNOWN
)
6305 if (diff_unmodified_pair(p
))
6307 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6308 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6310 if (DIFF_PAIR_UNMERGED(p
))
6313 diff_fill_oid_info(p
->one
, options
->repo
->index
);
6314 diff_fill_oid_info(p
->two
, options
->repo
->index
);
6316 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
6317 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
6318 patch_id_add_string(&ctx
, "diff--git");
6319 patch_id_add_string(&ctx
, "a/");
6320 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6321 patch_id_add_string(&ctx
, "b/");
6322 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6324 if (p
->one
->mode
== 0) {
6325 patch_id_add_string(&ctx
, "newfilemode");
6326 patch_id_add_mode(&ctx
, p
->two
->mode
);
6327 } else if (p
->two
->mode
== 0) {
6328 patch_id_add_string(&ctx
, "deletedfilemode");
6329 patch_id_add_mode(&ctx
, p
->one
->mode
);
6330 } else if (p
->one
->mode
!= p
->two
->mode
) {
6331 patch_id_add_string(&ctx
, "oldmode");
6332 patch_id_add_mode(&ctx
, p
->one
->mode
);
6333 patch_id_add_string(&ctx
, "newmode");
6334 patch_id_add_mode(&ctx
, p
->two
->mode
);
6337 if (diff_header_only
) {
6338 /* don't do anything since we're only populating header info */
6339 } else if (diff_filespec_is_binary(options
->repo
, p
->one
) ||
6340 diff_filespec_is_binary(options
->repo
, p
->two
)) {
6341 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->one
->oid
),
6342 the_hash_algo
->hexsz
);
6343 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->two
->oid
),
6344 the_hash_algo
->hexsz
);
6346 if (p
->one
->mode
== 0) {
6347 patch_id_add_string(&ctx
, "---/dev/null");
6348 patch_id_add_string(&ctx
, "+++b/");
6349 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6350 } else if (p
->two
->mode
== 0) {
6351 patch_id_add_string(&ctx
, "---a/");
6352 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6353 patch_id_add_string(&ctx
, "+++/dev/null");
6355 patch_id_add_string(&ctx
, "---a/");
6356 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6357 patch_id_add_string(&ctx
, "+++b/");
6358 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6361 if (fill_mmfile(options
->repo
, &mf1
, p
->one
) < 0 ||
6362 fill_mmfile(options
->repo
, &mf2
, p
->two
) < 0)
6363 return error("unable to read files to diff");
6366 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
6367 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
6368 patch_id_consume
, &data
, &xpp
, &xecfg
))
6369 return error("unable to generate patch-id diff for %s",
6372 flush_one_hunk(oid
, &ctx
);
6378 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
6380 struct diff_queue_struct
*q
= &diff_queued_diff
;
6381 int result
= diff_get_patch_id(options
, oid
, diff_header_only
);
6384 DIFF_QUEUE_CLEAR(q
);
6389 static int is_summary_empty(const struct diff_queue_struct
*q
)
6393 for (i
= 0; i
< q
->nr
; i
++) {
6394 const struct diff_filepair
*p
= q
->queue
[i
];
6396 switch (p
->status
) {
6397 case DIFF_STATUS_DELETED
:
6398 case DIFF_STATUS_ADDED
:
6399 case DIFF_STATUS_COPIED
:
6400 case DIFF_STATUS_RENAMED
:
6405 if (p
->one
->mode
&& p
->two
->mode
&&
6406 p
->one
->mode
!= p
->two
->mode
)
6414 static const char rename_limit_warning
[] =
6415 N_("exhaustive rename detection was skipped due to too many files.");
6417 static const char degrade_cc_to_c_warning
[] =
6418 N_("only found copies from modified paths due to too many files.");
6420 static const char rename_limit_advice
[] =
6421 N_("you may want to set your %s variable to at least "
6422 "%d and retry the command.");
6424 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
6428 warning(_(degrade_cc_to_c_warning
));
6430 warning(_(rename_limit_warning
));
6434 warning(_(rename_limit_advice
), varname
, needed
);
6437 static void create_filepairs_for_header_only_notifications(struct diff_options
*o
)
6439 struct strset present
;
6440 struct diff_queue_struct
*q
= &diff_queued_diff
;
6441 struct hashmap_iter iter
;
6442 struct strmap_entry
*e
;
6445 strset_init_with_options(&present
, /*pool*/ NULL
, /*strdup*/ 0);
6448 * Find out which paths exist in diff_queued_diff, preferring
6449 * one->path for any pair that has multiple paths.
6451 for (i
= 0; i
< q
->nr
; i
++) {
6452 struct diff_filepair
*p
= q
->queue
[i
];
6453 char *path
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
6455 if (strmap_contains(o
->additional_path_headers
, path
))
6456 strset_add(&present
, path
);
6460 * Loop over paths in additional_path_headers; for each NOT already
6461 * in diff_queued_diff, create a synthetic filepair and insert that
6462 * into diff_queued_diff.
6464 strmap_for_each_entry(o
->additional_path_headers
, &iter
, e
) {
6465 if (!strset_contains(&present
, e
->key
)) {
6466 struct diff_filespec
*one
, *two
;
6467 struct diff_filepair
*p
;
6469 one
= alloc_filespec(e
->key
);
6470 two
= alloc_filespec(e
->key
);
6471 fill_filespec(one
, null_oid(), 0, 0);
6472 fill_filespec(two
, null_oid(), 0, 0);
6473 p
= diff_queue(q
, one
, two
);
6474 p
->status
= DIFF_STATUS_MODIFIED
;
6478 /* Re-sort the filepairs */
6479 diffcore_fix_diff_index();
6482 strset_clear(&present
);
6485 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
6488 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
6489 struct diff_queue_struct
*q
= &diff_queued_diff
;
6491 if (WSEH_NEW
& WS_RULE_MASK
)
6492 BUG("WS rules bit mask overlaps with diff symbol flags");
6495 o
->emitted_symbols
= &esm
;
6497 if (o
->additional_path_headers
)
6498 create_filepairs_for_header_only_notifications(o
);
6500 for (i
= 0; i
< q
->nr
; i
++) {
6501 struct diff_filepair
*p
= q
->queue
[i
];
6502 if (check_pair_status(p
))
6503 diff_flush_patch(p
, o
);
6506 if (o
->emitted_symbols
) {
6507 if (o
->color_moved
) {
6508 struct mem_pool entry_pool
;
6509 struct moved_entry_list
*entry_list
;
6511 mem_pool_init(&entry_pool
, 1024 * 1024);
6512 entry_list
= add_lines_to_move_detection(o
,
6514 mark_color_as_moved(o
, entry_list
);
6515 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
6518 mem_pool_discard(&entry_pool
, 0);
6522 for (i
= 0; i
< esm
.nr
; i
++)
6523 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
6525 for (i
= 0; i
< esm
.nr
; i
++)
6526 free((void *)esm
.buf
[i
].line
);
6529 o
->emitted_symbols
= NULL
;
6533 static void diff_free_file(struct diff_options
*options
)
6535 if (options
->close_file
)
6536 fclose(options
->file
);
6539 static void diff_free_ignore_regex(struct diff_options
*options
)
6543 for (i
= 0; i
< options
->ignore_regex_nr
; i
++) {
6544 regfree(options
->ignore_regex
[i
]);
6545 free(options
->ignore_regex
[i
]);
6547 free(options
->ignore_regex
);
6550 void diff_free(struct diff_options
*options
)
6552 if (options
->no_free
)
6555 diff_free_file(options
);
6556 diff_free_ignore_regex(options
);
6557 clear_pathspec(&options
->pathspec
);
6560 void diff_flush(struct diff_options
*options
)
6562 struct diff_queue_struct
*q
= &diff_queued_diff
;
6563 int i
, output_format
= options
->output_format
;
6565 int dirstat_by_line
= 0;
6568 * Order: raw, stat, summary, patch
6569 * or: name/name-status/checkdiff (other bits clear)
6571 if (!q
->nr
&& !options
->additional_path_headers
)
6574 if (output_format
& (DIFF_FORMAT_RAW
|
6576 DIFF_FORMAT_NAME_STATUS
|
6577 DIFF_FORMAT_CHECKDIFF
)) {
6578 for (i
= 0; i
< q
->nr
; i
++) {
6579 struct diff_filepair
*p
= q
->queue
[i
];
6580 if (check_pair_status(p
))
6581 flush_one_pair(p
, options
);
6586 if (output_format
& DIFF_FORMAT_DIRSTAT
&& options
->flags
.dirstat_by_line
)
6587 dirstat_by_line
= 1;
6589 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
6591 struct diffstat_t diffstat
;
6593 compute_diffstat(options
, &diffstat
, q
);
6594 if (output_format
& DIFF_FORMAT_NUMSTAT
)
6595 show_numstat(&diffstat
, options
);
6596 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
6597 show_stats(&diffstat
, options
);
6598 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
6599 show_shortstats(&diffstat
, options
);
6600 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
6601 show_dirstat_by_line(&diffstat
, options
);
6602 free_diffstat_info(&diffstat
);
6605 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
6606 show_dirstat(options
);
6608 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
6609 for (i
= 0; i
< q
->nr
; i
++) {
6610 diff_summary(options
, q
->queue
[i
]);
6615 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
6616 options
->flags
.exit_with_status
&&
6617 options
->flags
.diff_from_contents
) {
6619 * run diff_flush_patch for the exit status. setting
6620 * options->file to /dev/null should be safe, because we
6621 * aren't supposed to produce any output anyway.
6623 diff_free_file(options
);
6624 options
->file
= xfopen("/dev/null", "w");
6625 options
->close_file
= 1;
6626 options
->color_moved
= 0;
6627 for (i
= 0; i
< q
->nr
; i
++) {
6628 struct diff_filepair
*p
= q
->queue
[i
];
6629 if (check_pair_status(p
))
6630 diff_flush_patch(p
, options
);
6631 if (options
->found_changes
)
6636 if (output_format
& DIFF_FORMAT_PATCH
) {
6638 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
6639 if (options
->stat_sep
)
6640 /* attach patch instead of inline */
6641 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
6645 diff_flush_patch_all_file_pairs(options
);
6648 if (output_format
& DIFF_FORMAT_CALLBACK
)
6649 options
->format_callback(q
, options
, options
->format_callback_data
);
6653 DIFF_QUEUE_CLEAR(q
);
6657 * Report the content-level differences with HAS_CHANGES;
6658 * diff_addremove/diff_change does not set the bit when
6659 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6661 if (options
->flags
.diff_from_contents
) {
6662 if (options
->found_changes
)
6663 options
->flags
.has_changes
= 1;
6665 options
->flags
.has_changes
= 0;
6669 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
6671 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
6673 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
6675 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
6676 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
6677 filter_bit_tst(p
->status
, options
)));
6680 static void diffcore_apply_filter(struct diff_options
*options
)
6683 struct diff_queue_struct
*q
= &diff_queued_diff
;
6684 struct diff_queue_struct outq
;
6686 DIFF_QUEUE_CLEAR(&outq
);
6688 if (!options
->filter
)
6691 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
6693 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
6694 if (match_filter(options
, q
->queue
[i
]))
6700 /* otherwise we will clear the whole queue
6701 * by copying the empty outq at the end of this
6702 * function, but first clear the current entries
6705 for (i
= 0; i
< q
->nr
; i
++)
6706 diff_free_filepair(q
->queue
[i
]);
6709 /* Only the matching ones */
6710 for (i
= 0; i
< q
->nr
; i
++) {
6711 struct diff_filepair
*p
= q
->queue
[i
];
6712 if (match_filter(options
, p
))
6715 diff_free_filepair(p
);
6722 /* Check whether two filespecs with the same mode and size are identical */
6723 static int diff_filespec_is_identical(struct repository
*r
,
6724 struct diff_filespec
*one
,
6725 struct diff_filespec
*two
)
6727 if (S_ISGITLINK(one
->mode
))
6729 if (diff_populate_filespec(r
, one
, NULL
))
6731 if (diff_populate_filespec(r
, two
, NULL
))
6733 return !memcmp(one
->data
, two
->data
, one
->size
);
6736 static int diff_filespec_check_stat_unmatch(struct repository
*r
,
6737 struct diff_filepair
*p
)
6739 struct diff_populate_filespec_options dpf_options
= {
6740 .check_size_only
= 1,
6741 .missing_object_cb
= diff_queued_diff_prefetch
,
6742 .missing_object_data
= r
,
6745 if (p
->done_skip_stat_unmatch
)
6746 return p
->skip_stat_unmatch_result
;
6748 p
->done_skip_stat_unmatch
= 1;
6749 p
->skip_stat_unmatch_result
= 0;
6751 * 1. Entries that come from stat info dirtiness
6752 * always have both sides (iow, not create/delete),
6753 * one side of the object name is unknown, with
6754 * the same mode and size. Keep the ones that
6755 * do not match these criteria. They have real
6758 * 2. At this point, the file is known to be modified,
6759 * with the same mode and size, and the object
6760 * name of one side is unknown. Need to inspect
6761 * the identical contents.
6763 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
6764 !DIFF_FILE_VALID(p
->two
) ||
6765 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
6766 (p
->one
->mode
!= p
->two
->mode
) ||
6767 diff_populate_filespec(r
, p
->one
, &dpf_options
) ||
6768 diff_populate_filespec(r
, p
->two
, &dpf_options
) ||
6769 (p
->one
->size
!= p
->two
->size
) ||
6770 !diff_filespec_is_identical(r
, p
->one
, p
->two
)) /* (2) */
6771 p
->skip_stat_unmatch_result
= 1;
6772 return p
->skip_stat_unmatch_result
;
6775 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
6778 struct diff_queue_struct
*q
= &diff_queued_diff
;
6779 struct diff_queue_struct outq
;
6780 DIFF_QUEUE_CLEAR(&outq
);
6782 for (i
= 0; i
< q
->nr
; i
++) {
6783 struct diff_filepair
*p
= q
->queue
[i
];
6785 if (diff_filespec_check_stat_unmatch(diffopt
->repo
, p
))
6789 * The caller can subtract 1 from skip_stat_unmatch
6790 * to determine how many paths were dirty only
6791 * due to stat info mismatch.
6793 if (!diffopt
->flags
.no_index
)
6794 diffopt
->skip_stat_unmatch
++;
6795 diff_free_filepair(p
);
6802 static int diffnamecmp(const void *a_
, const void *b_
)
6804 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
6805 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
6806 const char *name_a
, *name_b
;
6808 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
6809 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
6810 return strcmp(name_a
, name_b
);
6813 void diffcore_fix_diff_index(void)
6815 struct diff_queue_struct
*q
= &diff_queued_diff
;
6816 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
6819 void diff_add_if_missing(struct repository
*r
,
6820 struct oid_array
*to_fetch
,
6821 const struct diff_filespec
*filespec
)
6823 if (filespec
&& filespec
->oid_valid
&&
6824 !S_ISGITLINK(filespec
->mode
) &&
6825 oid_object_info_extended(r
, &filespec
->oid
, NULL
,
6826 OBJECT_INFO_FOR_PREFETCH
))
6827 oid_array_append(to_fetch
, &filespec
->oid
);
6830 void diff_queued_diff_prefetch(void *repository
)
6832 struct repository
*repo
= repository
;
6834 struct diff_queue_struct
*q
= &diff_queued_diff
;
6835 struct oid_array to_fetch
= OID_ARRAY_INIT
;
6837 for (i
= 0; i
< q
->nr
; i
++) {
6838 struct diff_filepair
*p
= q
->queue
[i
];
6839 diff_add_if_missing(repo
, &to_fetch
, p
->one
);
6840 diff_add_if_missing(repo
, &to_fetch
, p
->two
);
6844 * NEEDSWORK: Consider deduplicating the OIDs sent.
6846 promisor_remote_get_direct(repo
, to_fetch
.oid
, to_fetch
.nr
);
6848 oid_array_clear(&to_fetch
);
6851 void diffcore_std(struct diff_options
*options
)
6853 int output_formats_to_prefetch
= DIFF_FORMAT_DIFFSTAT
|
6854 DIFF_FORMAT_NUMSTAT
|
6856 DIFF_FORMAT_SHORTSTAT
|
6857 DIFF_FORMAT_DIRSTAT
;
6860 * Check if the user requested a blob-data-requiring diff output and/or
6861 * break-rewrite detection (which requires blob data). If yes, prefetch
6864 * If no prefetching occurs, diffcore_rename() will prefetch if it
6865 * decides that it needs inexact rename detection.
6867 if (options
->repo
== the_repository
&& has_promisor_remote() &&
6868 (options
->output_format
& output_formats_to_prefetch
||
6869 options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
6870 diff_queued_diff_prefetch(options
->repo
);
6872 /* NOTE please keep the following in sync with diff_tree_combined() */
6873 if (options
->skip_stat_unmatch
)
6874 diffcore_skip_stat_unmatch(options
);
6875 if (!options
->found_follow
) {
6876 /* See try_to_follow_renames() in tree-diff.c */
6877 if (options
->break_opt
!= -1)
6878 diffcore_break(options
->repo
,
6879 options
->break_opt
);
6880 if (options
->detect_rename
)
6881 diffcore_rename(options
);
6882 if (options
->break_opt
!= -1)
6883 diffcore_merge_broken();
6885 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
6886 diffcore_pickaxe(options
);
6887 if (options
->orderfile
)
6888 diffcore_order(options
->orderfile
);
6889 if (options
->rotate_to
)
6890 diffcore_rotate(options
);
6891 if (!options
->found_follow
)
6892 /* See try_to_follow_renames() in tree-diff.c */
6893 diff_resolve_rename_copy();
6894 diffcore_apply_filter(options
);
6896 if (diff_queued_diff
.nr
&& !options
->flags
.diff_from_contents
)
6897 options
->flags
.has_changes
= 1;
6899 options
->flags
.has_changes
= 0;
6901 options
->found_follow
= 0;
6904 int diff_result_code(struct diff_options
*opt
, int status
)
6908 diff_warn_rename_limit("diff.renameLimit",
6909 opt
->needed_rename_limit
,
6910 opt
->degraded_cc_to_c
);
6911 if (!opt
->flags
.exit_with_status
&&
6912 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
6914 if (opt
->flags
.exit_with_status
&&
6915 opt
->flags
.has_changes
)
6917 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
6918 opt
->flags
.check_failed
)
6923 int diff_can_quit_early(struct diff_options
*opt
)
6925 return (opt
->flags
.quick
&&
6927 opt
->flags
.has_changes
);
6931 * Shall changes to this submodule be ignored?
6933 * Submodule changes can be configured to be ignored separately for each path,
6934 * but that configuration can be overridden from the command line.
6936 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
6939 struct diff_flags orig_flags
= options
->flags
;
6940 if (!options
->flags
.override_submodule_config
)
6941 set_diffopt_flags_from_submodule_config(options
, path
);
6942 if (options
->flags
.ignore_submodules
)
6944 options
->flags
= orig_flags
;
6948 void compute_diffstat(struct diff_options
*options
,
6949 struct diffstat_t
*diffstat
,
6950 struct diff_queue_struct
*q
)
6954 memset(diffstat
, 0, sizeof(struct diffstat_t
));
6955 for (i
= 0; i
< q
->nr
; i
++) {
6956 struct diff_filepair
*p
= q
->queue
[i
];
6957 if (check_pair_status(p
))
6958 diff_flush_stat(p
, options
, diffstat
);
6962 void diff_addremove(struct diff_options
*options
,
6963 int addremove
, unsigned mode
,
6964 const struct object_id
*oid
,
6966 const char *concatpath
, unsigned dirty_submodule
)
6968 struct diff_filespec
*one
, *two
;
6970 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
6973 /* This may look odd, but it is a preparation for
6974 * feeding "there are unchanged files which should
6975 * not produce diffs, but when you are doing copy
6976 * detection you would need them, so here they are"
6977 * entries to the diff-core. They will be prefixed
6978 * with something like '=' or '*' (I haven't decided
6979 * which but should not make any difference).
6980 * Feeding the same new and old to diff_change()
6981 * also has the same effect.
6982 * Before the final output happens, they are pruned after
6983 * merged into rename/copy pairs as appropriate.
6985 if (options
->flags
.reverse_diff
)
6986 addremove
= (addremove
== '+' ? '-' :
6987 addremove
== '-' ? '+' : addremove
);
6989 if (options
->prefix
&&
6990 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
6993 one
= alloc_filespec(concatpath
);
6994 two
= alloc_filespec(concatpath
);
6996 if (addremove
!= '+')
6997 fill_filespec(one
, oid
, oid_valid
, mode
);
6998 if (addremove
!= '-') {
6999 fill_filespec(two
, oid
, oid_valid
, mode
);
7000 two
->dirty_submodule
= dirty_submodule
;
7003 diff_queue(&diff_queued_diff
, one
, two
);
7004 if (!options
->flags
.diff_from_contents
)
7005 options
->flags
.has_changes
= 1;
7008 void diff_change(struct diff_options
*options
,
7009 unsigned old_mode
, unsigned new_mode
,
7010 const struct object_id
*old_oid
,
7011 const struct object_id
*new_oid
,
7012 int old_oid_valid
, int new_oid_valid
,
7013 const char *concatpath
,
7014 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
7016 struct diff_filespec
*one
, *two
;
7017 struct diff_filepair
*p
;
7019 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
7020 is_submodule_ignored(concatpath
, options
))
7023 if (options
->flags
.reverse_diff
) {
7024 SWAP(old_mode
, new_mode
);
7025 SWAP(old_oid
, new_oid
);
7026 SWAP(old_oid_valid
, new_oid_valid
);
7027 SWAP(old_dirty_submodule
, new_dirty_submodule
);
7030 if (options
->prefix
&&
7031 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
7034 one
= alloc_filespec(concatpath
);
7035 two
= alloc_filespec(concatpath
);
7036 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
7037 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
7038 one
->dirty_submodule
= old_dirty_submodule
;
7039 two
->dirty_submodule
= new_dirty_submodule
;
7040 p
= diff_queue(&diff_queued_diff
, one
, two
);
7042 if (options
->flags
.diff_from_contents
)
7045 if (options
->flags
.quick
&& options
->skip_stat_unmatch
&&
7046 !diff_filespec_check_stat_unmatch(options
->repo
, p
)) {
7047 diff_free_filespec_data(p
->one
);
7048 diff_free_filespec_data(p
->two
);
7052 options
->flags
.has_changes
= 1;
7055 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
7057 struct diff_filepair
*pair
;
7058 struct diff_filespec
*one
, *two
;
7060 if (options
->prefix
&&
7061 strncmp(path
, options
->prefix
, options
->prefix_length
))
7064 one
= alloc_filespec(path
);
7065 two
= alloc_filespec(path
);
7066 pair
= diff_queue(&diff_queued_diff
, one
, two
);
7067 pair
->is_unmerged
= 1;
7071 static char *run_textconv(struct repository
*r
,
7073 struct diff_filespec
*spec
,
7076 struct diff_tempfile
*temp
;
7077 struct child_process child
= CHILD_PROCESS_INIT
;
7078 struct strbuf buf
= STRBUF_INIT
;
7081 temp
= prepare_temp_file(r
, spec
);
7082 strvec_push(&child
.args
, pgm
);
7083 strvec_push(&child
.args
, temp
->name
);
7085 child
.use_shell
= 1;
7087 if (start_command(&child
)) {
7092 if (strbuf_read(&buf
, child
.out
, 0) < 0)
7093 err
= error("error reading from textconv command '%s'", pgm
);
7096 if (finish_command(&child
) || err
) {
7097 strbuf_release(&buf
);
7103 return strbuf_detach(&buf
, outsize
);
7106 size_t fill_textconv(struct repository
*r
,
7107 struct userdiff_driver
*driver
,
7108 struct diff_filespec
*df
,
7114 if (!DIFF_FILE_VALID(df
)) {
7118 if (diff_populate_filespec(r
, df
, NULL
))
7119 die("unable to read files to diff");
7124 if (!driver
->textconv
)
7125 BUG("fill_textconv called with non-textconv driver");
7127 if (driver
->textconv_cache
&& df
->oid_valid
) {
7128 *outbuf
= notes_cache_get(driver
->textconv_cache
,
7135 *outbuf
= run_textconv(r
, driver
->textconv
, df
, &size
);
7137 die("unable to read files to diff");
7139 if (driver
->textconv_cache
&& df
->oid_valid
) {
7140 /* ignore errors, as we might be in a readonly repository */
7141 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
7144 * we could save up changes and flush them all at the end,
7145 * but we would need an extra call after all diffing is done.
7146 * Since generating a cache entry is the slow path anyway,
7147 * this extra overhead probably isn't a big deal.
7149 notes_cache_write(driver
->textconv_cache
);
7155 int textconv_object(struct repository
*r
,
7158 const struct object_id
*oid
,
7161 unsigned long *buf_size
)
7163 struct diff_filespec
*df
;
7164 struct userdiff_driver
*textconv
;
7166 df
= alloc_filespec(path
);
7167 fill_filespec(df
, oid
, oid_valid
, mode
);
7168 textconv
= get_textconv(r
, df
);
7174 *buf_size
= fill_textconv(r
, textconv
, df
, buf
);
7179 void setup_diff_pager(struct diff_options
*opt
)
7182 * If the user asked for our exit code, then either they want --quiet
7183 * or --exit-code. We should definitely not bother with a pager in the
7184 * former case, as we will generate no output. Since we still properly
7185 * report our exit code even when a pager is run, we _could_ run a
7186 * pager with --exit-code. But since we have not done so historically,
7187 * and because it is easy to find people oneline advising "git diff
7188 * --exit-code" in hooks and other scripts, we do not do so.
7190 if (!opt
->flags
.exit_with_status
&&
7191 check_pager_config("diff") != 0)