2 * Copyright (C) 2005 Junio C Hamano
11 #include "xdiff-interface.h"
14 #include "run-command.h"
16 #include "object-store.h"
18 #include "submodule-config.h"
19 #include "submodule.h"
23 #include "string-list.h"
27 #include "parse-options.h"
29 #include "promisor-remote.h"
33 #ifdef NO_FAST_WORKING_DIRECTORY
34 #define FAST_WORKING_DIRECTORY 0
36 #define FAST_WORKING_DIRECTORY 1
39 static int diff_detect_rename_default
;
40 static int diff_indent_heuristic
= 1;
41 static int diff_rename_limit_default
= 1000;
42 static int diff_suppress_blank_empty
;
43 static int diff_use_color_default
= -1;
44 static int diff_color_moved_default
;
45 static int diff_color_moved_ws_default
;
46 static int diff_context_default
= 3;
47 static int diff_interhunk_context_default
;
48 static const char *diff_word_regex_cfg
;
49 static const char *external_diff_cmd_cfg
;
50 static const char *diff_order_file_cfg
;
51 int diff_auto_refresh_index
= 1;
52 static int diff_mnemonic_prefix
;
53 static int diff_no_prefix
;
54 static int diff_relative
;
55 static int diff_stat_graph_width
;
56 static int diff_dirstat_permille_default
= 30;
57 static struct diff_options default_diff_options
;
58 static long diff_algorithm
;
59 static unsigned ws_error_highlight_default
= WSEH_NEW
;
61 static char diff_colors
[][COLOR_MAXLEN
] = {
63 GIT_COLOR_NORMAL
, /* CONTEXT */
64 GIT_COLOR_BOLD
, /* METAINFO */
65 GIT_COLOR_CYAN
, /* FRAGINFO */
66 GIT_COLOR_RED
, /* OLD */
67 GIT_COLOR_GREEN
, /* NEW */
68 GIT_COLOR_YELLOW
, /* COMMIT */
69 GIT_COLOR_BG_RED
, /* WHITESPACE */
70 GIT_COLOR_NORMAL
, /* FUNCINFO */
71 GIT_COLOR_BOLD_MAGENTA
, /* OLD_MOVED */
72 GIT_COLOR_BOLD_BLUE
, /* OLD_MOVED ALTERNATIVE */
73 GIT_COLOR_FAINT
, /* OLD_MOVED_DIM */
74 GIT_COLOR_FAINT_ITALIC
, /* OLD_MOVED_ALTERNATIVE_DIM */
75 GIT_COLOR_BOLD_CYAN
, /* NEW_MOVED */
76 GIT_COLOR_BOLD_YELLOW
, /* NEW_MOVED ALTERNATIVE */
77 GIT_COLOR_FAINT
, /* NEW_MOVED_DIM */
78 GIT_COLOR_FAINT_ITALIC
, /* NEW_MOVED_ALTERNATIVE_DIM */
79 GIT_COLOR_FAINT
, /* CONTEXT_DIM */
80 GIT_COLOR_FAINT_RED
, /* OLD_DIM */
81 GIT_COLOR_FAINT_GREEN
, /* NEW_DIM */
82 GIT_COLOR_BOLD
, /* CONTEXT_BOLD */
83 GIT_COLOR_BOLD_RED
, /* OLD_BOLD */
84 GIT_COLOR_BOLD_GREEN
, /* NEW_BOLD */
87 static const char *color_diff_slots
[] = {
88 [DIFF_CONTEXT
] = "context",
89 [DIFF_METAINFO
] = "meta",
90 [DIFF_FRAGINFO
] = "frag",
91 [DIFF_FILE_OLD
] = "old",
92 [DIFF_FILE_NEW
] = "new",
93 [DIFF_COMMIT
] = "commit",
94 [DIFF_WHITESPACE
] = "whitespace",
95 [DIFF_FUNCINFO
] = "func",
96 [DIFF_FILE_OLD_MOVED
] = "oldMoved",
97 [DIFF_FILE_OLD_MOVED_ALT
] = "oldMovedAlternative",
98 [DIFF_FILE_OLD_MOVED_DIM
] = "oldMovedDimmed",
99 [DIFF_FILE_OLD_MOVED_ALT_DIM
] = "oldMovedAlternativeDimmed",
100 [DIFF_FILE_NEW_MOVED
] = "newMoved",
101 [DIFF_FILE_NEW_MOVED_ALT
] = "newMovedAlternative",
102 [DIFF_FILE_NEW_MOVED_DIM
] = "newMovedDimmed",
103 [DIFF_FILE_NEW_MOVED_ALT_DIM
] = "newMovedAlternativeDimmed",
104 [DIFF_CONTEXT_DIM
] = "contextDimmed",
105 [DIFF_FILE_OLD_DIM
] = "oldDimmed",
106 [DIFF_FILE_NEW_DIM
] = "newDimmed",
107 [DIFF_CONTEXT_BOLD
] = "contextBold",
108 [DIFF_FILE_OLD_BOLD
] = "oldBold",
109 [DIFF_FILE_NEW_BOLD
] = "newBold",
112 define_list_config_array_extra(color_diff_slots
, {"plain"});
114 static int parse_diff_color_slot(const char *var
)
116 if (!strcasecmp(var
, "plain"))
118 return LOOKUP_CONFIG(color_diff_slots
, var
);
121 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
122 struct strbuf
*errmsg
)
124 char *params_copy
= xstrdup(params_string
);
125 struct string_list params
= STRING_LIST_INIT_NODUP
;
130 string_list_split_in_place(¶ms
, params_copy
, ',', -1);
131 for (i
= 0; i
< params
.nr
; i
++) {
132 const char *p
= params
.items
[i
].string
;
133 if (!strcmp(p
, "changes")) {
134 options
->flags
.dirstat_by_line
= 0;
135 options
->flags
.dirstat_by_file
= 0;
136 } else if (!strcmp(p
, "lines")) {
137 options
->flags
.dirstat_by_line
= 1;
138 options
->flags
.dirstat_by_file
= 0;
139 } else if (!strcmp(p
, "files")) {
140 options
->flags
.dirstat_by_line
= 0;
141 options
->flags
.dirstat_by_file
= 1;
142 } else if (!strcmp(p
, "noncumulative")) {
143 options
->flags
.dirstat_cumulative
= 0;
144 } else if (!strcmp(p
, "cumulative")) {
145 options
->flags
.dirstat_cumulative
= 1;
146 } else if (isdigit(*p
)) {
148 int permille
= strtoul(p
, &end
, 10) * 10;
149 if (*end
== '.' && isdigit(*++end
)) {
150 /* only use first digit */
151 permille
+= *end
- '0';
152 /* .. and ignore any further digits */
153 while (isdigit(*++end
))
157 options
->dirstat_permille
= permille
;
159 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
164 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
169 string_list_clear(¶ms
, 0);
174 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
176 if (!strcmp(value
, "log"))
177 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
178 else if (!strcmp(value
, "short"))
179 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
180 else if (!strcmp(value
, "diff"))
181 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
183 * Please update $__git_diff_submodule_formats in
184 * git-completion.bash when you add new formats.
191 int git_config_rename(const char *var
, const char *value
)
194 return DIFF_DETECT_RENAME
;
195 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
196 return DIFF_DETECT_COPY
;
197 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
200 long parse_algorithm_value(const char *value
)
204 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
206 else if (!strcasecmp(value
, "minimal"))
207 return XDF_NEED_MINIMAL
;
208 else if (!strcasecmp(value
, "patience"))
209 return XDF_PATIENCE_DIFF
;
210 else if (!strcasecmp(value
, "histogram"))
211 return XDF_HISTOGRAM_DIFF
;
213 * Please update $__git_diff_algorithms in git-completion.bash
214 * when you add new algorithms.
219 static int parse_one_token(const char **arg
, const char *token
)
222 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
229 static int parse_ws_error_highlight(const char *arg
)
231 const char *orig_arg
= arg
;
235 if (parse_one_token(&arg
, "none"))
237 else if (parse_one_token(&arg
, "default"))
239 else if (parse_one_token(&arg
, "all"))
240 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
241 else if (parse_one_token(&arg
, "new"))
243 else if (parse_one_token(&arg
, "old"))
245 else if (parse_one_token(&arg
, "context"))
248 return -1 - (int)(arg
- orig_arg
);
257 * These are to give UI layer defaults.
258 * The core-level commands such as git-diff-files should
259 * never be affected by the setting of diff.renames
260 * the user happens to have in the configuration file.
262 void init_diff_ui_defaults(void)
264 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
267 int git_diff_heuristic_config(const char *var
, const char *value
,
270 if (!strcmp(var
, "diff.indentheuristic"))
271 diff_indent_heuristic
= git_config_bool(var
, value
);
275 static int parse_color_moved(const char *arg
)
277 switch (git_parse_maybe_bool(arg
)) {
279 return COLOR_MOVED_NO
;
281 return COLOR_MOVED_DEFAULT
;
286 if (!strcmp(arg
, "no"))
287 return COLOR_MOVED_NO
;
288 else if (!strcmp(arg
, "plain"))
289 return COLOR_MOVED_PLAIN
;
290 else if (!strcmp(arg
, "blocks"))
291 return COLOR_MOVED_BLOCKS
;
292 else if (!strcmp(arg
, "zebra"))
293 return COLOR_MOVED_ZEBRA
;
294 else if (!strcmp(arg
, "default"))
295 return COLOR_MOVED_DEFAULT
;
296 else if (!strcmp(arg
, "dimmed-zebra"))
297 return COLOR_MOVED_ZEBRA_DIM
;
298 else if (!strcmp(arg
, "dimmed_zebra"))
299 return COLOR_MOVED_ZEBRA_DIM
;
301 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
304 static unsigned parse_color_moved_ws(const char *arg
)
307 struct string_list l
= STRING_LIST_INIT_DUP
;
308 struct string_list_item
*i
;
310 string_list_split(&l
, arg
, ',', -1);
312 for_each_string_list_item(i
, &l
) {
313 struct strbuf sb
= STRBUF_INIT
;
314 strbuf_addstr(&sb
, i
->string
);
317 if (!strcmp(sb
.buf
, "no"))
319 else if (!strcmp(sb
.buf
, "ignore-space-change"))
320 ret
|= XDF_IGNORE_WHITESPACE_CHANGE
;
321 else if (!strcmp(sb
.buf
, "ignore-space-at-eol"))
322 ret
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
323 else if (!strcmp(sb
.buf
, "ignore-all-space"))
324 ret
|= XDF_IGNORE_WHITESPACE
;
325 else if (!strcmp(sb
.buf
, "allow-indentation-change"))
326 ret
|= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
;
328 ret
|= COLOR_MOVED_WS_ERROR
;
329 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
);
335 if ((ret
& COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
) &&
336 (ret
& XDF_WHITESPACE_FLAGS
)) {
337 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
338 ret
|= COLOR_MOVED_WS_ERROR
;
341 string_list_clear(&l
, 0);
346 int git_diff_ui_config(const char *var
, const char *value
, void *cb
)
348 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
349 diff_use_color_default
= git_config_colorbool(var
, value
);
352 if (!strcmp(var
, "diff.colormoved")) {
353 int cm
= parse_color_moved(value
);
356 diff_color_moved_default
= cm
;
359 if (!strcmp(var
, "diff.colormovedws")) {
360 unsigned cm
= parse_color_moved_ws(value
);
361 if (cm
& COLOR_MOVED_WS_ERROR
)
363 diff_color_moved_ws_default
= cm
;
366 if (!strcmp(var
, "diff.context")) {
367 diff_context_default
= git_config_int(var
, value
);
368 if (diff_context_default
< 0)
372 if (!strcmp(var
, "diff.interhunkcontext")) {
373 diff_interhunk_context_default
= git_config_int(var
, value
);
374 if (diff_interhunk_context_default
< 0)
378 if (!strcmp(var
, "diff.renames")) {
379 diff_detect_rename_default
= git_config_rename(var
, value
);
382 if (!strcmp(var
, "diff.autorefreshindex")) {
383 diff_auto_refresh_index
= git_config_bool(var
, value
);
386 if (!strcmp(var
, "diff.mnemonicprefix")) {
387 diff_mnemonic_prefix
= git_config_bool(var
, value
);
390 if (!strcmp(var
, "diff.noprefix")) {
391 diff_no_prefix
= git_config_bool(var
, value
);
394 if (!strcmp(var
, "diff.relative")) {
395 diff_relative
= git_config_bool(var
, value
);
398 if (!strcmp(var
, "diff.statgraphwidth")) {
399 diff_stat_graph_width
= git_config_int(var
, value
);
402 if (!strcmp(var
, "diff.external"))
403 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
404 if (!strcmp(var
, "diff.wordregex"))
405 return git_config_string(&diff_word_regex_cfg
, var
, value
);
406 if (!strcmp(var
, "diff.orderfile"))
407 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
409 if (!strcmp(var
, "diff.ignoresubmodules"))
410 handle_ignore_submodules_arg(&default_diff_options
, value
);
412 if (!strcmp(var
, "diff.submodule")) {
413 if (parse_submodule_params(&default_diff_options
, value
))
414 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
419 if (!strcmp(var
, "diff.algorithm")) {
420 diff_algorithm
= parse_algorithm_value(value
);
421 if (diff_algorithm
< 0)
426 if (git_color_config(var
, value
, cb
) < 0)
429 return git_diff_basic_config(var
, value
, cb
);
432 int git_diff_basic_config(const char *var
, const char *value
, void *cb
)
436 if (!strcmp(var
, "diff.renamelimit")) {
437 diff_rename_limit_default
= git_config_int(var
, value
);
441 if (userdiff_config(var
, value
) < 0)
444 if (skip_prefix(var
, "diff.color.", &name
) ||
445 skip_prefix(var
, "color.diff.", &name
)) {
446 int slot
= parse_diff_color_slot(name
);
450 return config_error_nonbool(var
);
451 return color_parse(value
, diff_colors
[slot
]);
454 if (!strcmp(var
, "diff.wserrorhighlight")) {
455 int val
= parse_ws_error_highlight(value
);
458 ws_error_highlight_default
= val
;
462 /* like GNU diff's --suppress-blank-empty option */
463 if (!strcmp(var
, "diff.suppressblankempty") ||
464 /* for backwards compatibility */
465 !strcmp(var
, "diff.suppress-blank-empty")) {
466 diff_suppress_blank_empty
= git_config_bool(var
, value
);
470 if (!strcmp(var
, "diff.dirstat")) {
471 struct strbuf errmsg
= STRBUF_INIT
;
472 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
473 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
474 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
476 strbuf_release(&errmsg
);
477 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
481 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
484 return git_default_config(var
, value
, cb
);
487 static char *quote_two(const char *one
, const char *two
)
489 int need_one
= quote_c_style(one
, NULL
, NULL
, CQUOTE_NODQ
);
490 int need_two
= quote_c_style(two
, NULL
, NULL
, CQUOTE_NODQ
);
491 struct strbuf res
= STRBUF_INIT
;
493 if (need_one
+ need_two
) {
494 strbuf_addch(&res
, '"');
495 quote_c_style(one
, &res
, NULL
, CQUOTE_NODQ
);
496 quote_c_style(two
, &res
, NULL
, CQUOTE_NODQ
);
497 strbuf_addch(&res
, '"');
499 strbuf_addstr(&res
, one
);
500 strbuf_addstr(&res
, two
);
502 return strbuf_detach(&res
, NULL
);
505 static const char *external_diff(void)
507 static const char *external_diff_cmd
= NULL
;
508 static int done_preparing
= 0;
511 return external_diff_cmd
;
512 external_diff_cmd
= xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
513 if (!external_diff_cmd
)
514 external_diff_cmd
= external_diff_cmd_cfg
;
516 return external_diff_cmd
;
520 * Keep track of files used for diffing. Sometimes such an entry
521 * refers to a temporary file, sometimes to an existing file, and
522 * sometimes to "/dev/null".
524 static struct diff_tempfile
{
526 * filename external diff should read from, or NULL if this
527 * entry is currently not in use:
531 char hex
[GIT_MAX_HEXSZ
+ 1];
535 * If this diff_tempfile instance refers to a temporary file,
536 * this tempfile object is used to manage its lifetime.
538 struct tempfile
*tempfile
;
541 struct emit_callback
{
544 int blank_at_eof_in_preimage
;
545 int blank_at_eof_in_postimage
;
547 int lno_in_postimage
;
548 const char **label_path
;
549 struct diff_words_data
*diff_words
;
550 struct diff_options
*opt
;
551 struct strbuf
*header
;
554 static int count_lines(const char *data
, int size
)
556 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
563 completely_empty
= 0;
567 completely_empty
= 0;
570 if (completely_empty
)
573 count
++; /* no trailing newline */
577 static int fill_mmfile(struct repository
*r
, mmfile_t
*mf
,
578 struct diff_filespec
*one
)
580 if (!DIFF_FILE_VALID(one
)) {
581 mf
->ptr
= (char *)""; /* does not matter */
585 else if (diff_populate_filespec(r
, one
, NULL
))
589 mf
->size
= one
->size
;
593 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
594 static unsigned long diff_filespec_size(struct repository
*r
,
595 struct diff_filespec
*one
)
597 struct diff_populate_filespec_options dpf_options
= {
598 .check_size_only
= 1,
601 if (!DIFF_FILE_VALID(one
))
603 diff_populate_filespec(r
, one
, &dpf_options
);
607 static int count_trailing_blank(mmfile_t
*mf
, unsigned ws_rule
)
610 long size
= mf
->size
;
615 ptr
+= size
- 1; /* pointing at the very end */
617 ; /* incomplete line */
619 ptr
--; /* skip the last LF */
620 while (mf
->ptr
< ptr
) {
622 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
623 if (*prev_eol
== '\n')
625 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
, ws_rule
))
633 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
634 struct emit_callback
*ecbdata
)
637 unsigned ws_rule
= ecbdata
->ws_rule
;
638 l1
= count_trailing_blank(mf1
, ws_rule
);
639 l2
= count_trailing_blank(mf2
, ws_rule
);
641 ecbdata
->blank_at_eof_in_preimage
= 0;
642 ecbdata
->blank_at_eof_in_postimage
= 0;
645 at
= count_lines(mf1
->ptr
, mf1
->size
);
646 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
648 at
= count_lines(mf2
->ptr
, mf2
->size
);
649 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
652 static void emit_line_0(struct diff_options
*o
,
653 const char *set_sign
, const char *set
, unsigned reverse
, const char *reset
,
654 int first
, const char *line
, int len
)
656 int has_trailing_newline
, has_trailing_carriage_return
;
657 int needs_reset
= 0; /* at the end of the line */
658 FILE *file
= o
->file
;
660 fputs(diff_line_prefix(o
), file
);
662 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
663 if (has_trailing_newline
)
666 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
667 if (has_trailing_carriage_return
)
673 if (reverse
&& want_color(o
->use_color
)) {
674 fputs(GIT_COLOR_REVERSE
, file
);
679 fputs(set_sign
, file
);
690 if (set_sign
&& set
!= set_sign
)
695 fwrite(line
, len
, 1, file
);
696 needs_reset
= 1; /* 'line' may contain color codes. */
701 if (has_trailing_carriage_return
)
703 if (has_trailing_newline
)
707 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
708 const char *line
, int len
)
710 emit_line_0(o
, set
, NULL
, 0, reset
, 0, line
, len
);
714 DIFF_SYMBOL_BINARY_DIFF_HEADER
,
715 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
716 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
717 DIFF_SYMBOL_BINARY_DIFF_BODY
,
718 DIFF_SYMBOL_BINARY_DIFF_FOOTER
,
719 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
720 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
721 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
722 DIFF_SYMBOL_STATS_LINE
,
723 DIFF_SYMBOL_WORD_DIFF
,
724 DIFF_SYMBOL_STAT_SEP
,
726 DIFF_SYMBOL_SUBMODULE_ADD
,
727 DIFF_SYMBOL_SUBMODULE_DEL
,
728 DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
729 DIFF_SYMBOL_SUBMODULE_MODIFIED
,
730 DIFF_SYMBOL_SUBMODULE_HEADER
,
731 DIFF_SYMBOL_SUBMODULE_ERROR
,
732 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
,
733 DIFF_SYMBOL_REWRITE_DIFF
,
734 DIFF_SYMBOL_BINARY_FILES
,
736 DIFF_SYMBOL_FILEPAIR_PLUS
,
737 DIFF_SYMBOL_FILEPAIR_MINUS
,
738 DIFF_SYMBOL_WORDS_PORCELAIN
,
741 DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
744 DIFF_SYMBOL_NO_LF_EOF
,
745 DIFF_SYMBOL_CONTEXT_FRAGINFO
,
746 DIFF_SYMBOL_CONTEXT_MARKER
,
747 DIFF_SYMBOL_SEPARATOR
750 * Flags for content lines:
751 * 0..12 are whitespace rules
752 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
753 * 16 is marking if the line is blank at EOF
755 #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
756 #define DIFF_SYMBOL_MOVED_LINE (1<<17)
757 #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
758 #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
759 #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
762 * This struct is used when we need to buffer the output of the diff output.
764 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
765 * into the pre/post image file. This pointer could be a union with the
766 * line pointer. By storing an offset into the file instead of the literal line,
767 * we can decrease the memory footprint for the buffered output. At first we
768 * may want to only have indirection for the content lines, but we could also
769 * enhance the state for emitting prefabricated lines, e.g. the similarity
770 * score line or hunk/file headers would only need to store a number or path
771 * and then the output can be constructed later on depending on state.
773 struct emitted_diff_symbol
{
777 int indent_off
; /* Offset to first non-whitespace character */
778 int indent_width
; /* The visual width of the indentation */
782 #define EMITTED_DIFF_SYMBOL_INIT { 0 }
784 struct emitted_diff_symbols
{
785 struct emitted_diff_symbol
*buf
;
788 #define EMITTED_DIFF_SYMBOLS_INIT { 0 }
790 static void append_emitted_diff_symbol(struct diff_options
*o
,
791 struct emitted_diff_symbol
*e
)
793 struct emitted_diff_symbol
*f
;
795 ALLOC_GROW(o
->emitted_symbols
->buf
,
796 o
->emitted_symbols
->nr
+ 1,
797 o
->emitted_symbols
->alloc
);
798 f
= &o
->emitted_symbols
->buf
[o
->emitted_symbols
->nr
++];
800 memcpy(f
, e
, sizeof(struct emitted_diff_symbol
));
801 f
->line
= e
->line
? xmemdupz(e
->line
, e
->len
) : NULL
;
804 static void free_emitted_diff_symbols(struct emitted_diff_symbols
*e
)
813 const struct emitted_diff_symbol
*es
;
814 struct moved_entry
*next_line
;
815 struct moved_entry
*next_match
;
819 struct moved_entry
*match
;
820 int wsd
; /* The whitespace delta of this block */
823 #define INDENT_BLANKLINE INT_MIN
825 static void fill_es_indent_data(struct emitted_diff_symbol
*es
)
827 unsigned int off
= 0, i
;
828 int width
= 0, tab_width
= es
->flags
& WS_TAB_WIDTH_MASK
;
829 const char *s
= es
->line
;
830 const int len
= es
->len
;
832 /* skip any \v \f \r at start of indentation */
833 while (s
[off
] == '\f' || s
[off
] == '\v' ||
834 (s
[off
] == '\r' && off
< len
- 1))
837 /* calculate the visual width of indentation */
842 } else if (s
[off
] == '\t') {
843 width
+= tab_width
- (width
% tab_width
);
844 while (s
[++off
] == '\t')
851 /* check if this line is blank */
852 for (i
= off
; i
< len
; i
++)
857 es
->indent_width
= INDENT_BLANKLINE
;
858 es
->indent_off
= len
;
860 es
->indent_off
= off
;
861 es
->indent_width
= width
;
865 static int compute_ws_delta(const struct emitted_diff_symbol
*a
,
866 const struct emitted_diff_symbol
*b
)
868 int a_width
= a
->indent_width
,
869 b_width
= b
->indent_width
;
871 if (a_width
== INDENT_BLANKLINE
&& b_width
== INDENT_BLANKLINE
)
872 return INDENT_BLANKLINE
;
874 return a_width
- b_width
;
877 static int cmp_in_block_with_wsd(const struct moved_entry
*cur
,
878 const struct emitted_diff_symbol
*l
,
879 struct moved_block
*pmb
)
881 int a_width
= cur
->es
->indent_width
, b_width
= l
->indent_width
;
884 /* The text of each line must match */
885 if (cur
->es
->id
!= l
->id
)
889 * If 'l' and 'cur' are both blank then we don't need to check the
890 * indent. We only need to check cur as we know the strings match.
892 if (a_width
== INDENT_BLANKLINE
)
896 * The indent changes of the block are known and stored in pmb->wsd;
897 * however we need to check if the indent changes of the current line
898 * match those of the current block.
900 delta
= b_width
- a_width
;
903 * If the previous lines of this block were all blank then set its
906 if (pmb
->wsd
== INDENT_BLANKLINE
)
909 return delta
!= pmb
->wsd
;
912 struct interned_diff_symbol
{
913 struct hashmap_entry ent
;
914 struct emitted_diff_symbol
*es
;
917 static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data
,
918 const struct hashmap_entry
*eptr
,
919 const struct hashmap_entry
*entry_or_key
,
920 const void *keydata UNUSED
)
922 const struct diff_options
*diffopt
= hashmap_cmp_fn_data
;
923 const struct emitted_diff_symbol
*a
, *b
;
924 unsigned flags
= diffopt
->color_moved_ws_handling
925 & XDF_WHITESPACE_FLAGS
;
927 a
= container_of(eptr
, const struct interned_diff_symbol
, ent
)->es
;
928 b
= container_of(entry_or_key
, const struct interned_diff_symbol
, ent
)->es
;
930 return !xdiff_compare_lines(a
->line
+ a
->indent_off
,
931 a
->len
- a
->indent_off
,
932 b
->line
+ b
->indent_off
,
933 b
->len
- b
->indent_off
, flags
);
936 static void prepare_entry(struct diff_options
*o
, struct emitted_diff_symbol
*l
,
937 struct interned_diff_symbol
*s
)
939 unsigned flags
= o
->color_moved_ws_handling
& XDF_WHITESPACE_FLAGS
;
940 unsigned int hash
= xdiff_hash_string(l
->line
+ l
->indent_off
,
941 l
->len
- l
->indent_off
, flags
);
943 hashmap_entry_init(&s
->ent
, hash
);
947 struct moved_entry_list
{
948 struct moved_entry
*add
, *del
;
951 static struct moved_entry_list
*add_lines_to_move_detection(struct diff_options
*o
,
952 struct mem_pool
*entry_mem_pool
)
954 struct moved_entry
*prev_line
= NULL
;
955 struct mem_pool interned_pool
;
956 struct hashmap interned_map
;
957 struct moved_entry_list
*entry_list
= NULL
;
958 size_t entry_list_alloc
= 0;
962 hashmap_init(&interned_map
, interned_diff_symbol_cmp
, o
, 8096);
963 mem_pool_init(&interned_pool
, 1024 * 1024);
965 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
966 struct interned_diff_symbol key
;
967 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
968 struct interned_diff_symbol
*s
;
969 struct moved_entry
*entry
;
971 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
) {
976 if (o
->color_moved_ws_handling
&
977 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
978 fill_es_indent_data(l
);
980 prepare_entry(o
, l
, &key
);
981 s
= hashmap_get_entry(&interned_map
, &key
, ent
, &key
.ent
);
986 ALLOC_GROW_BY(entry_list
, id
, 1, entry_list_alloc
);
987 hashmap_add(&interned_map
,
988 memcpy(mem_pool_alloc(&interned_pool
,
992 entry
= mem_pool_alloc(entry_mem_pool
, sizeof(*entry
));
994 entry
->next_line
= NULL
;
995 if (prev_line
&& prev_line
->es
->s
== l
->s
)
996 prev_line
->next_line
= entry
;
998 if (l
->s
== DIFF_SYMBOL_PLUS
) {
999 entry
->next_match
= entry_list
[l
->id
].add
;
1000 entry_list
[l
->id
].add
= entry
;
1002 entry
->next_match
= entry_list
[l
->id
].del
;
1003 entry_list
[l
->id
].del
= entry
;
1007 hashmap_clear(&interned_map
);
1008 mem_pool_discard(&interned_pool
, 0);
1013 static void pmb_advance_or_null(struct diff_options
*o
,
1014 struct emitted_diff_symbol
*l
,
1015 struct moved_block
*pmb
,
1020 for (i
= 0, j
= 0; i
< *pmb_nr
; i
++) {
1022 struct moved_entry
*prev
= pmb
[i
].match
;
1023 struct moved_entry
*cur
= (prev
&& prev
->next_line
) ?
1024 prev
->next_line
: NULL
;
1026 if (o
->color_moved_ws_handling
&
1027 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1029 !cmp_in_block_with_wsd(cur
, l
, &pmb
[i
]);
1031 match
= cur
&& cur
->es
->id
== l
->id
;
1035 pmb
[j
++].match
= cur
;
1041 static void fill_potential_moved_blocks(struct diff_options
*o
,
1042 struct moved_entry
*match
,
1043 struct emitted_diff_symbol
*l
,
1044 struct moved_block
**pmb_p
,
1045 int *pmb_alloc_p
, int *pmb_nr_p
)
1048 struct moved_block
*pmb
= *pmb_p
;
1049 int pmb_alloc
= *pmb_alloc_p
, pmb_nr
= *pmb_nr_p
;
1052 * The current line is the start of a new block.
1053 * Setup the set of potential blocks.
1055 for (; match
; match
= match
->next_match
) {
1056 ALLOC_GROW(pmb
, pmb_nr
+ 1, pmb_alloc
);
1057 if (o
->color_moved_ws_handling
&
1058 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1059 pmb
[pmb_nr
].wsd
= compute_ws_delta(l
, match
->es
);
1061 pmb
[pmb_nr
].wsd
= 0;
1062 pmb
[pmb_nr
++].match
= match
;
1066 *pmb_alloc_p
= pmb_alloc
;
1071 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1073 * Otherwise, if the last block has fewer alphanumeric characters than
1074 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
1077 * The last block consists of the (n - block_length)'th line up to but not
1078 * including the nth line.
1080 * Returns 0 if the last block is empty or is unset by this function, non zero
1083 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1084 * Think of a way to unify them.
1086 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1087 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
1088 static int adjust_last_block(struct diff_options
*o
, int n
, int block_length
)
1090 int i
, alnum_count
= 0;
1091 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
1092 return block_length
;
1093 for (i
= 1; i
< block_length
+ 1; i
++) {
1094 const char *c
= o
->emitted_symbols
->buf
[n
- i
].line
;
1099 if (alnum_count
>= COLOR_MOVED_MIN_ALNUM_COUNT
)
1103 for (i
= 1; i
< block_length
+ 1; i
++)
1104 o
->emitted_symbols
->buf
[n
- i
].flags
&= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
;
1108 /* Find blocks of moved code, delegate actual coloring decision to helper */
1109 static void mark_color_as_moved(struct diff_options
*o
,
1110 struct moved_entry_list
*entry_list
)
1112 struct moved_block
*pmb
= NULL
; /* potentially moved blocks */
1113 int pmb_nr
= 0, pmb_alloc
= 0;
1114 int n
, flipped_block
= 0, block_length
= 0;
1115 enum diff_symbol moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1118 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1119 struct moved_entry
*match
= NULL
;
1120 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1123 case DIFF_SYMBOL_PLUS
:
1124 match
= entry_list
[l
->id
].del
;
1126 case DIFF_SYMBOL_MINUS
:
1127 match
= entry_list
[l
->id
].add
;
1133 if (pmb_nr
&& (!match
|| l
->s
!= moved_symbol
)) {
1134 if (!adjust_last_block(o
, n
, block_length
) &&
1137 * Rewind in case there is another match
1138 * starting at the second line of the block
1148 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1152 if (o
->color_moved
== COLOR_MOVED_PLAIN
) {
1153 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1157 pmb_advance_or_null(o
, l
, pmb
, &pmb_nr
);
1160 int contiguous
= adjust_last_block(o
, n
, block_length
);
1162 if (!contiguous
&& block_length
> 1)
1164 * Rewind in case there is another match
1165 * starting at the second line of the block
1169 fill_potential_moved_blocks(o
, match
, l
,
1173 if (contiguous
&& pmb_nr
&& moved_symbol
== l
->s
)
1174 flipped_block
= (flipped_block
+ 1) % 2;
1179 moved_symbol
= l
->s
;
1181 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1188 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1189 if (flipped_block
&& o
->color_moved
!= COLOR_MOVED_BLOCKS
)
1190 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_ALT
;
1193 adjust_last_block(o
, n
, block_length
);
1198 static void dim_moved_lines(struct diff_options
*o
)
1201 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1202 struct emitted_diff_symbol
*prev
= (n
!= 0) ?
1203 &o
->emitted_symbols
->buf
[n
- 1] : NULL
;
1204 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1205 struct emitted_diff_symbol
*next
=
1206 (n
< o
->emitted_symbols
->nr
- 1) ?
1207 &o
->emitted_symbols
->buf
[n
+ 1] : NULL
;
1209 /* Not a plus or minus line? */
1210 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
)
1213 /* Not a moved line? */
1214 if (!(l
->flags
& DIFF_SYMBOL_MOVED_LINE
))
1218 * If prev or next are not a plus or minus line,
1219 * pretend they don't exist
1221 if (prev
&& prev
->s
!= DIFF_SYMBOL_PLUS
&&
1222 prev
->s
!= DIFF_SYMBOL_MINUS
)
1224 if (next
&& next
->s
!= DIFF_SYMBOL_PLUS
&&
1225 next
->s
!= DIFF_SYMBOL_MINUS
)
1228 /* Inside a block? */
1230 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1231 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
)) &&
1233 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1234 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
))) {
1235 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1239 /* Check if we are at an interesting bound: */
1240 if (prev
&& (prev
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1241 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1242 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1244 if (next
&& (next
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1245 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1246 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1250 * The boundary to prev and next are not interesting,
1251 * so this line is not interesting as a whole
1253 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1257 static void emit_line_ws_markup(struct diff_options
*o
,
1258 const char *set_sign
, const char *set
,
1260 int sign_index
, const char *line
, int len
,
1261 unsigned ws_rule
, int blank_at_eof
)
1263 const char *ws
= NULL
;
1264 int sign
= o
->output_indicators
[sign_index
];
1266 if (o
->ws_error_highlight
& ws_rule
) {
1267 ws
= diff_get_color_opt(o
, DIFF_WHITESPACE
);
1272 if (!ws
&& !set_sign
)
1273 emit_line_0(o
, set
, NULL
, 0, reset
, sign
, line
, len
);
1275 emit_line_0(o
, set_sign
, set
, !!set_sign
, reset
, sign
, line
, len
);
1276 } else if (blank_at_eof
)
1277 /* Blank line at EOF - paint '+' as well */
1278 emit_line_0(o
, ws
, NULL
, 0, reset
, sign
, line
, len
);
1280 /* Emit just the prefix, then the rest. */
1281 emit_line_0(o
, set_sign
? set_sign
: set
, NULL
, !!set_sign
, reset
,
1283 ws_check_emit(line
, len
, ws_rule
,
1284 o
->file
, set
, reset
, ws
);
1288 static void emit_diff_symbol_from_struct(struct diff_options
*o
,
1289 struct emitted_diff_symbol
*eds
)
1291 static const char *nneof
= " No newline at end of file\n";
1292 const char *context
, *reset
, *set
, *set_sign
, *meta
, *fraginfo
;
1294 enum diff_symbol s
= eds
->s
;
1295 const char *line
= eds
->line
;
1297 unsigned flags
= eds
->flags
;
1300 case DIFF_SYMBOL_NO_LF_EOF
:
1301 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1302 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1303 putc('\n', o
->file
);
1304 emit_line_0(o
, context
, NULL
, 0, reset
, '\\',
1305 nneof
, strlen(nneof
));
1307 case DIFF_SYMBOL_SUBMODULE_HEADER
:
1308 case DIFF_SYMBOL_SUBMODULE_ERROR
:
1309 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
:
1310 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
:
1311 case DIFF_SYMBOL_SUMMARY
:
1312 case DIFF_SYMBOL_STATS_LINE
:
1313 case DIFF_SYMBOL_BINARY_DIFF_BODY
:
1314 case DIFF_SYMBOL_CONTEXT_FRAGINFO
:
1315 emit_line(o
, "", "", line
, len
);
1317 case DIFF_SYMBOL_CONTEXT_INCOMPLETE
:
1318 case DIFF_SYMBOL_CONTEXT_MARKER
:
1319 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1320 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1321 emit_line(o
, context
, reset
, line
, len
);
1323 case DIFF_SYMBOL_SEPARATOR
:
1324 fprintf(o
->file
, "%s%c",
1325 diff_line_prefix(o
),
1326 o
->line_termination
);
1328 case DIFF_SYMBOL_CONTEXT
:
1329 set
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1330 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1332 if (o
->flags
.dual_color_diffed_diffs
) {
1333 char c
= !len
? 0 : line
[0];
1336 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1338 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1340 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1342 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1343 OUTPUT_INDICATOR_CONTEXT
, line
, len
,
1344 flags
& (DIFF_SYMBOL_CONTENT_WS_MASK
), 0);
1346 case DIFF_SYMBOL_PLUS
:
1347 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1348 DIFF_SYMBOL_MOVED_LINE_ALT
|
1349 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1350 case DIFF_SYMBOL_MOVED_LINE
|
1351 DIFF_SYMBOL_MOVED_LINE_ALT
|
1352 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1353 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT_DIM
);
1355 case DIFF_SYMBOL_MOVED_LINE
|
1356 DIFF_SYMBOL_MOVED_LINE_ALT
:
1357 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT
);
1359 case DIFF_SYMBOL_MOVED_LINE
|
1360 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1361 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_DIM
);
1363 case DIFF_SYMBOL_MOVED_LINE
:
1364 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED
);
1367 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1369 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1370 if (!o
->flags
.dual_color_diffed_diffs
)
1373 char c
= !len
? 0 : line
[0];
1377 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_BOLD
);
1379 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1381 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_BOLD
);
1383 set
= diff_get_color_opt(o
, DIFF_CONTEXT_BOLD
);
1384 flags
&= ~DIFF_SYMBOL_CONTENT_WS_MASK
;
1386 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1387 OUTPUT_INDICATOR_NEW
, line
, len
,
1388 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
,
1389 flags
& DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
);
1391 case DIFF_SYMBOL_MINUS
:
1392 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1393 DIFF_SYMBOL_MOVED_LINE_ALT
|
1394 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1395 case DIFF_SYMBOL_MOVED_LINE
|
1396 DIFF_SYMBOL_MOVED_LINE_ALT
|
1397 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1398 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT_DIM
);
1400 case DIFF_SYMBOL_MOVED_LINE
|
1401 DIFF_SYMBOL_MOVED_LINE_ALT
:
1402 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT
);
1404 case DIFF_SYMBOL_MOVED_LINE
|
1405 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1406 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_DIM
);
1408 case DIFF_SYMBOL_MOVED_LINE
:
1409 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED
);
1412 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1414 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1415 if (!o
->flags
.dual_color_diffed_diffs
)
1418 char c
= !len
? 0 : line
[0];
1422 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_DIM
);
1424 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1426 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_DIM
);
1428 set
= diff_get_color_opt(o
, DIFF_CONTEXT_DIM
);
1430 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1431 OUTPUT_INDICATOR_OLD
, line
, len
,
1432 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
, 0);
1434 case DIFF_SYMBOL_WORDS_PORCELAIN
:
1435 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1436 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1437 emit_line(o
, context
, reset
, line
, len
);
1438 fputs("~\n", o
->file
);
1440 case DIFF_SYMBOL_WORDS
:
1441 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1442 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1444 * Skip the prefix character, if any. With
1445 * diff_suppress_blank_empty, there may be
1448 if (line
[0] != '\n') {
1452 emit_line(o
, context
, reset
, line
, len
);
1454 case DIFF_SYMBOL_FILEPAIR_PLUS
:
1455 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1456 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1457 fprintf(o
->file
, "%s%s+++ %s%s%s\n", diff_line_prefix(o
), meta
,
1459 strchr(line
, ' ') ? "\t" : "");
1461 case DIFF_SYMBOL_FILEPAIR_MINUS
:
1462 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1463 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1464 fprintf(o
->file
, "%s%s--- %s%s%s\n", diff_line_prefix(o
), meta
,
1466 strchr(line
, ' ') ? "\t" : "");
1468 case DIFF_SYMBOL_BINARY_FILES
:
1469 case DIFF_SYMBOL_HEADER
:
1470 fprintf(o
->file
, "%s", line
);
1472 case DIFF_SYMBOL_BINARY_DIFF_HEADER
:
1473 fprintf(o
->file
, "%sGIT binary patch\n", diff_line_prefix(o
));
1475 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
:
1476 fprintf(o
->file
, "%sdelta %s\n", diff_line_prefix(o
), line
);
1478 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
:
1479 fprintf(o
->file
, "%sliteral %s\n", diff_line_prefix(o
), line
);
1481 case DIFF_SYMBOL_BINARY_DIFF_FOOTER
:
1482 fputs(diff_line_prefix(o
), o
->file
);
1483 fputc('\n', o
->file
);
1485 case DIFF_SYMBOL_REWRITE_DIFF
:
1486 fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
1487 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1488 emit_line(o
, fraginfo
, reset
, line
, len
);
1490 case DIFF_SYMBOL_SUBMODULE_ADD
:
1491 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1492 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1493 emit_line(o
, set
, reset
, line
, len
);
1495 case DIFF_SYMBOL_SUBMODULE_DEL
:
1496 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1497 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1498 emit_line(o
, set
, reset
, line
, len
);
1500 case DIFF_SYMBOL_SUBMODULE_UNTRACKED
:
1501 fprintf(o
->file
, "%sSubmodule %s contains untracked content\n",
1502 diff_line_prefix(o
), line
);
1504 case DIFF_SYMBOL_SUBMODULE_MODIFIED
:
1505 fprintf(o
->file
, "%sSubmodule %s contains modified content\n",
1506 diff_line_prefix(o
), line
);
1508 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
:
1509 emit_line(o
, "", "", " 0 files changed\n",
1510 strlen(" 0 files changed\n"));
1512 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV
:
1513 emit_line(o
, "", "", " ...\n", strlen(" ...\n"));
1515 case DIFF_SYMBOL_WORD_DIFF
:
1516 fprintf(o
->file
, "%.*s", len
, line
);
1518 case DIFF_SYMBOL_STAT_SEP
:
1519 fputs(o
->stat_sep
, o
->file
);
1522 BUG("unknown diff symbol");
1526 static void emit_diff_symbol(struct diff_options
*o
, enum diff_symbol s
,
1527 const char *line
, int len
, unsigned flags
)
1529 struct emitted_diff_symbol e
= {
1530 .line
= line
, .len
= len
, .flags
= flags
, .s
= s
1533 if (o
->emitted_symbols
)
1534 append_emitted_diff_symbol(o
, &e
);
1536 emit_diff_symbol_from_struct(o
, &e
);
1539 void diff_emit_submodule_del(struct diff_options
*o
, const char *line
)
1541 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_DEL
, line
, strlen(line
), 0);
1544 void diff_emit_submodule_add(struct diff_options
*o
, const char *line
)
1546 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ADD
, line
, strlen(line
), 0);
1549 void diff_emit_submodule_untracked(struct diff_options
*o
, const char *path
)
1551 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
1552 path
, strlen(path
), 0);
1555 void diff_emit_submodule_modified(struct diff_options
*o
, const char *path
)
1557 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_MODIFIED
,
1558 path
, strlen(path
), 0);
1561 void diff_emit_submodule_header(struct diff_options
*o
, const char *header
)
1563 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_HEADER
,
1564 header
, strlen(header
), 0);
1567 void diff_emit_submodule_error(struct diff_options
*o
, const char *err
)
1569 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ERROR
, err
, strlen(err
), 0);
1572 void diff_emit_submodule_pipethrough(struct diff_options
*o
,
1573 const char *line
, int len
)
1575 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
, line
, len
, 0);
1578 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
1580 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
1581 ecbdata
->blank_at_eof_in_preimage
&&
1582 ecbdata
->blank_at_eof_in_postimage
&&
1583 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
1584 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
1586 return ws_blank_line(line
, len
, ecbdata
->ws_rule
);
1589 static void emit_add_line(struct emit_callback
*ecbdata
,
1590 const char *line
, int len
)
1592 unsigned flags
= WSEH_NEW
| ecbdata
->ws_rule
;
1593 if (new_blank_line_at_eof(ecbdata
, line
, len
))
1594 flags
|= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
;
1596 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_PLUS
, line
, len
, flags
);
1599 static void emit_del_line(struct emit_callback
*ecbdata
,
1600 const char *line
, int len
)
1602 unsigned flags
= WSEH_OLD
| ecbdata
->ws_rule
;
1603 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_MINUS
, line
, len
, flags
);
1606 static void emit_context_line(struct emit_callback
*ecbdata
,
1607 const char *line
, int len
)
1609 unsigned flags
= WSEH_CONTEXT
| ecbdata
->ws_rule
;
1610 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_CONTEXT
, line
, len
, flags
);
1613 static void emit_hunk_header(struct emit_callback
*ecbdata
,
1614 const char *line
, int len
)
1616 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1617 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
1618 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
1619 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1620 const char *reverse
= ecbdata
->color_diff
? GIT_COLOR_REVERSE
: "";
1621 static const char atat
[2] = { '@', '@' };
1622 const char *cp
, *ep
;
1623 struct strbuf msgbuf
= STRBUF_INIT
;
1628 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1629 * it always is at least 10 bytes long.
1632 memcmp(line
, atat
, 2) ||
1633 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
1634 emit_diff_symbol(ecbdata
->opt
,
1635 DIFF_SYMBOL_CONTEXT_MARKER
, line
, len
, 0);
1638 ep
+= 2; /* skip over @@ */
1640 /* The hunk header in fraginfo color */
1641 if (ecbdata
->opt
->flags
.dual_color_diffed_diffs
)
1642 strbuf_addstr(&msgbuf
, reverse
);
1643 strbuf_addstr(&msgbuf
, frag
);
1644 if (ecbdata
->opt
->flags
.suppress_hunk_header_line_count
)
1645 strbuf_add(&msgbuf
, atat
, sizeof(atat
));
1647 strbuf_add(&msgbuf
, line
, ep
- line
);
1648 strbuf_addstr(&msgbuf
, reset
);
1654 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
1657 /* blank before the func header */
1658 for (cp
= ep
; ep
- line
< len
; ep
++)
1659 if (*ep
!= ' ' && *ep
!= '\t')
1662 strbuf_addstr(&msgbuf
, context
);
1663 strbuf_add(&msgbuf
, cp
, ep
- cp
);
1664 strbuf_addstr(&msgbuf
, reset
);
1667 if (ep
< line
+ len
) {
1668 strbuf_addstr(&msgbuf
, func
);
1669 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
1670 strbuf_addstr(&msgbuf
, reset
);
1673 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
1674 strbuf_complete_line(&msgbuf
);
1675 emit_diff_symbol(ecbdata
->opt
,
1676 DIFF_SYMBOL_CONTEXT_FRAGINFO
, msgbuf
.buf
, msgbuf
.len
, 0);
1677 strbuf_release(&msgbuf
);
1680 static struct diff_tempfile
*claim_diff_tempfile(void)
1683 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
1684 if (!diff_temp
[i
].name
)
1685 return diff_temp
+ i
;
1686 BUG("diff is failing to clean up its tempfiles");
1689 static void remove_tempfile(void)
1692 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
1693 if (is_tempfile_active(diff_temp
[i
].tempfile
))
1694 delete_tempfile(&diff_temp
[i
].tempfile
);
1695 diff_temp
[i
].name
= NULL
;
1699 static void add_line_count(struct strbuf
*out
, int count
)
1703 strbuf_addstr(out
, "0,0");
1706 strbuf_addstr(out
, "1");
1709 strbuf_addf(out
, "1,%d", count
);
1714 static void emit_rewrite_lines(struct emit_callback
*ecb
,
1715 int prefix
, const char *data
, int size
)
1717 const char *endp
= NULL
;
1722 endp
= memchr(data
, '\n', size
);
1723 len
= endp
? (endp
- data
+ 1) : size
;
1724 if (prefix
!= '+') {
1725 ecb
->lno_in_preimage
++;
1726 emit_del_line(ecb
, data
, len
);
1728 ecb
->lno_in_postimage
++;
1729 emit_add_line(ecb
, data
, len
);
1735 emit_diff_symbol(ecb
->opt
, DIFF_SYMBOL_NO_LF_EOF
, NULL
, 0, 0);
1738 static void emit_rewrite_diff(const char *name_a
,
1740 struct diff_filespec
*one
,
1741 struct diff_filespec
*two
,
1742 struct userdiff_driver
*textconv_one
,
1743 struct userdiff_driver
*textconv_two
,
1744 struct diff_options
*o
)
1747 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
1748 const char *a_prefix
, *b_prefix
;
1749 char *data_one
, *data_two
;
1750 size_t size_one
, size_two
;
1751 struct emit_callback ecbdata
;
1752 struct strbuf out
= STRBUF_INIT
;
1754 if (diff_mnemonic_prefix
&& o
->flags
.reverse_diff
) {
1755 a_prefix
= o
->b_prefix
;
1756 b_prefix
= o
->a_prefix
;
1758 a_prefix
= o
->a_prefix
;
1759 b_prefix
= o
->b_prefix
;
1762 name_a
+= (*name_a
== '/');
1763 name_b
+= (*name_b
== '/');
1765 strbuf_reset(&a_name
);
1766 strbuf_reset(&b_name
);
1767 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
1768 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
1770 size_one
= fill_textconv(o
->repo
, textconv_one
, one
, &data_one
);
1771 size_two
= fill_textconv(o
->repo
, textconv_two
, two
, &data_two
);
1773 memset(&ecbdata
, 0, sizeof(ecbdata
));
1774 ecbdata
.color_diff
= want_color(o
->use_color
);
1775 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
1777 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
1779 mf1
.ptr
= (char *)data_one
;
1780 mf2
.ptr
= (char *)data_two
;
1781 mf1
.size
= size_one
;
1782 mf2
.size
= size_two
;
1783 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1785 ecbdata
.lno_in_preimage
= 1;
1786 ecbdata
.lno_in_postimage
= 1;
1788 lc_a
= count_lines(data_one
, size_one
);
1789 lc_b
= count_lines(data_two
, size_two
);
1791 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1792 a_name
.buf
, a_name
.len
, 0);
1793 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1794 b_name
.buf
, b_name
.len
, 0);
1796 strbuf_addstr(&out
, "@@ -");
1797 if (!o
->irreversible_delete
)
1798 add_line_count(&out
, lc_a
);
1800 strbuf_addstr(&out
, "?,?");
1801 strbuf_addstr(&out
, " +");
1802 add_line_count(&out
, lc_b
);
1803 strbuf_addstr(&out
, " @@\n");
1804 emit_diff_symbol(o
, DIFF_SYMBOL_REWRITE_DIFF
, out
.buf
, out
.len
, 0);
1805 strbuf_release(&out
);
1807 if (lc_a
&& !o
->irreversible_delete
)
1808 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
1810 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
1812 free((char *)data_one
);
1814 free((char *)data_two
);
1817 struct diff_words_buffer
{
1819 unsigned long alloc
;
1820 struct diff_words_orig
{
1821 const char *begin
, *end
;
1823 int orig_nr
, orig_alloc
;
1826 static void diff_words_append(char *line
, unsigned long len
,
1827 struct diff_words_buffer
*buffer
)
1829 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
1832 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
1833 buffer
->text
.size
+= len
;
1834 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
1837 struct diff_words_style_elem
{
1840 const char *color
; /* NULL; filled in by the setup code if
1841 * color is enabled */
1844 struct diff_words_style
{
1845 enum diff_words_type type
;
1846 struct diff_words_style_elem new_word
, old_word
, ctx
;
1847 const char *newline
;
1850 static struct diff_words_style diff_words_styles
[] = {
1851 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1852 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1853 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
1856 struct diff_words_data
{
1857 struct diff_words_buffer minus
, plus
;
1858 const char *current_plus
;
1860 struct diff_options
*opt
;
1861 regex_t
*word_regex
;
1862 enum diff_words_type type
;
1863 struct diff_words_style
*style
;
1866 static int fn_out_diff_words_write_helper(struct diff_options
*o
,
1867 struct diff_words_style_elem
*st_el
,
1868 const char *newline
,
1869 size_t count
, const char *buf
)
1872 struct strbuf sb
= STRBUF_INIT
;
1875 char *p
= memchr(buf
, '\n', count
);
1877 strbuf_addstr(&sb
, diff_line_prefix(o
));
1880 const char *reset
= st_el
->color
&& *st_el
->color
?
1881 GIT_COLOR_RESET
: NULL
;
1882 if (st_el
->color
&& *st_el
->color
)
1883 strbuf_addstr(&sb
, st_el
->color
);
1884 strbuf_addstr(&sb
, st_el
->prefix
);
1885 strbuf_add(&sb
, buf
, p
? p
- buf
: count
);
1886 strbuf_addstr(&sb
, st_el
->suffix
);
1888 strbuf_addstr(&sb
, reset
);
1893 strbuf_addstr(&sb
, newline
);
1894 count
-= p
+ 1 - buf
;
1898 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1906 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1908 strbuf_release(&sb
);
1913 * '--color-words' algorithm can be described as:
1915 * 1. collect the minus/plus lines of a diff hunk, divided into
1916 * minus-lines and plus-lines;
1918 * 2. break both minus-lines and plus-lines into words and
1919 * place them into two mmfile_t with one word for each line;
1921 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1923 * And for the common parts of the both file, we output the plus side text.
1924 * diff_words->current_plus is used to trace the current position of the plus file
1925 * which printed. diff_words->last_minus is used to trace the last minus word
1928 * For '--graph' to work with '--color-words', we need to output the graph prefix
1929 * on each line of color words output. Generally, there are two conditions on
1930 * which we should output the prefix.
1932 * 1. diff_words->last_minus == 0 &&
1933 * diff_words->current_plus == diff_words->plus.text.ptr
1935 * that is: the plus text must start as a new line, and if there is no minus
1936 * word printed, a graph prefix must be printed.
1938 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1939 * *(diff_words->current_plus - 1) == '\n'
1941 * that is: a graph prefix must be printed following a '\n'
1943 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
1945 if ((diff_words
->last_minus
== 0 &&
1946 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
1947 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
1948 *(diff_words
->current_plus
- 1) == '\n')) {
1955 static void fn_out_diff_words_aux(void *priv
,
1956 long minus_first
, long minus_len
,
1957 long plus_first
, long plus_len
,
1958 const char *func
, long funclen
)
1960 struct diff_words_data
*diff_words
= priv
;
1961 struct diff_words_style
*style
= diff_words
->style
;
1962 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
1963 struct diff_options
*opt
= diff_words
->opt
;
1964 const char *line_prefix
;
1967 line_prefix
= diff_line_prefix(opt
);
1969 /* POSIX requires that first be decremented by one if len == 0... */
1971 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
1973 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
1975 minus_begin
= minus_end
=
1976 diff_words
->minus
.orig
[minus_first
].end
;
1979 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
1980 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
1982 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
1984 if (color_words_output_graph_prefix(diff_words
)) {
1985 fputs(line_prefix
, diff_words
->opt
->file
);
1987 if (diff_words
->current_plus
!= plus_begin
) {
1988 fn_out_diff_words_write_helper(diff_words
->opt
,
1989 &style
->ctx
, style
->newline
,
1990 plus_begin
- diff_words
->current_plus
,
1991 diff_words
->current_plus
);
1993 if (minus_begin
!= minus_end
) {
1994 fn_out_diff_words_write_helper(diff_words
->opt
,
1995 &style
->old_word
, style
->newline
,
1996 minus_end
- minus_begin
, minus_begin
);
1998 if (plus_begin
!= plus_end
) {
1999 fn_out_diff_words_write_helper(diff_words
->opt
,
2000 &style
->new_word
, style
->newline
,
2001 plus_end
- plus_begin
, plus_begin
);
2004 diff_words
->current_plus
= plus_end
;
2005 diff_words
->last_minus
= minus_first
;
2008 /* This function starts looking at *begin, and returns 0 iff a word was found. */
2009 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
2010 int *begin
, int *end
)
2012 while (word_regex
&& *begin
< buffer
->size
) {
2013 regmatch_t match
[1];
2014 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
2015 buffer
->size
- *begin
, 1, match
, 0)) {
2016 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
2017 '\n', match
[0].rm_eo
- match
[0].rm_so
);
2018 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
2019 *begin
+= match
[0].rm_so
;
2023 return *begin
> *end
;
2029 /* find the next word */
2030 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
2032 if (*begin
>= buffer
->size
)
2035 /* find the end of the word */
2037 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
2044 * This function splits the words in buffer->text, stores the list with
2045 * newline separator into out, and saves the offsets of the original words
2048 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
2049 regex_t
*word_regex
)
2057 /* fake an empty "0th" word */
2058 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
2059 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
2060 buffer
->orig_nr
= 1;
2062 for (i
= 0; i
< buffer
->text
.size
; i
++) {
2063 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
2066 /* store original boundaries */
2067 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
2068 buffer
->orig_alloc
);
2069 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
2070 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
2073 /* store one word */
2074 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
2075 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
2076 out
->ptr
[out
->size
+ j
- i
] = '\n';
2077 out
->size
+= j
- i
+ 1;
2083 /* this executes the word diff on the accumulated buffers */
2084 static void diff_words_show(struct diff_words_data
*diff_words
)
2088 mmfile_t minus
, plus
;
2089 struct diff_words_style
*style
= diff_words
->style
;
2091 struct diff_options
*opt
= diff_words
->opt
;
2092 const char *line_prefix
;
2095 line_prefix
= diff_line_prefix(opt
);
2097 /* special case: only removal */
2098 if (!diff_words
->plus
.text
.size
) {
2099 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2100 line_prefix
, strlen(line_prefix
), 0);
2101 fn_out_diff_words_write_helper(diff_words
->opt
,
2102 &style
->old_word
, style
->newline
,
2103 diff_words
->minus
.text
.size
,
2104 diff_words
->minus
.text
.ptr
);
2105 diff_words
->minus
.text
.size
= 0;
2109 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
2110 diff_words
->last_minus
= 0;
2112 memset(&xpp
, 0, sizeof(xpp
));
2113 memset(&xecfg
, 0, sizeof(xecfg
));
2114 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
2115 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
2117 /* as only the hunk header will be parsed, we need a 0-context */
2119 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, NULL
,
2120 diff_words
, &xpp
, &xecfg
))
2121 die("unable to generate word diff");
2124 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
2125 diff_words
->plus
.text
.size
) {
2126 if (color_words_output_graph_prefix(diff_words
))
2127 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2128 line_prefix
, strlen(line_prefix
), 0);
2129 fn_out_diff_words_write_helper(diff_words
->opt
,
2130 &style
->ctx
, style
->newline
,
2131 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
2132 - diff_words
->current_plus
, diff_words
->current_plus
);
2134 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
2137 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
2138 static void diff_words_flush(struct emit_callback
*ecbdata
)
2140 struct diff_options
*wo
= ecbdata
->diff_words
->opt
;
2142 if (ecbdata
->diff_words
->minus
.text
.size
||
2143 ecbdata
->diff_words
->plus
.text
.size
)
2144 diff_words_show(ecbdata
->diff_words
);
2146 if (wo
->emitted_symbols
) {
2147 struct diff_options
*o
= ecbdata
->opt
;
2148 struct emitted_diff_symbols
*wol
= wo
->emitted_symbols
;
2153 * Instead of appending each, concat all words to a line?
2155 for (i
= 0; i
< wol
->nr
; i
++)
2156 append_emitted_diff_symbol(o
, &wol
->buf
[i
]);
2158 for (i
= 0; i
< wol
->nr
; i
++)
2159 free((void *)wol
->buf
[i
].line
);
2165 static void diff_filespec_load_driver(struct diff_filespec
*one
,
2166 struct index_state
*istate
)
2168 /* Use already-loaded driver */
2172 if (S_ISREG(one
->mode
))
2173 one
->driver
= userdiff_find_by_path(istate
, one
->path
);
2175 /* Fallback to default settings */
2177 one
->driver
= userdiff_find_by_name("default");
2180 static const char *userdiff_word_regex(struct diff_filespec
*one
,
2181 struct index_state
*istate
)
2183 diff_filespec_load_driver(one
, istate
);
2184 return one
->driver
->word_regex
;
2187 static void init_diff_words_data(struct emit_callback
*ecbdata
,
2188 struct diff_options
*orig_opts
,
2189 struct diff_filespec
*one
,
2190 struct diff_filespec
*two
)
2193 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
2194 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
2196 CALLOC_ARRAY(ecbdata
->diff_words
, 1);
2197 ecbdata
->diff_words
->type
= o
->word_diff
;
2198 ecbdata
->diff_words
->opt
= o
;
2200 if (orig_opts
->emitted_symbols
)
2201 CALLOC_ARRAY(o
->emitted_symbols
, 1);
2204 o
->word_regex
= userdiff_word_regex(one
, o
->repo
->index
);
2206 o
->word_regex
= userdiff_word_regex(two
, o
->repo
->index
);
2208 o
->word_regex
= diff_word_regex_cfg
;
2209 if (o
->word_regex
) {
2210 ecbdata
->diff_words
->word_regex
= (regex_t
*)
2211 xmalloc(sizeof(regex_t
));
2212 if (regcomp(ecbdata
->diff_words
->word_regex
,
2214 REG_EXTENDED
| REG_NEWLINE
))
2215 die("invalid regular expression: %s",
2218 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
2219 if (o
->word_diff
== diff_words_styles
[i
].type
) {
2220 ecbdata
->diff_words
->style
=
2221 &diff_words_styles
[i
];
2225 if (want_color(o
->use_color
)) {
2226 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
2227 st
->old_word
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2228 st
->new_word
.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2229 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
2233 static void free_diff_words_data(struct emit_callback
*ecbdata
)
2235 if (ecbdata
->diff_words
) {
2236 diff_words_flush(ecbdata
);
2237 free_emitted_diff_symbols(ecbdata
->diff_words
->opt
->emitted_symbols
);
2238 free (ecbdata
->diff_words
->opt
);
2239 free (ecbdata
->diff_words
->minus
.text
.ptr
);
2240 free (ecbdata
->diff_words
->minus
.orig
);
2241 free (ecbdata
->diff_words
->plus
.text
.ptr
);
2242 free (ecbdata
->diff_words
->plus
.orig
);
2243 if (ecbdata
->diff_words
->word_regex
) {
2244 regfree(ecbdata
->diff_words
->word_regex
);
2245 free(ecbdata
->diff_words
->word_regex
);
2247 FREE_AND_NULL(ecbdata
->diff_words
);
2251 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
2253 if (want_color(diff_use_color
))
2254 return diff_colors
[ix
];
2258 const char *diff_line_prefix(struct diff_options
*opt
)
2260 struct strbuf
*msgbuf
;
2261 if (!opt
->output_prefix
)
2264 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
2268 static unsigned long sane_truncate_line(char *line
, unsigned long len
)
2271 unsigned long allot
;
2277 (void) utf8_width(&cp
, &l
);
2279 break; /* truncated in the middle? */
2284 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
2287 ecbdata
->lno_in_preimage
= 0;
2288 ecbdata
->lno_in_postimage
= 0;
2289 p
= strchr(line
, '-');
2291 return; /* cannot happen */
2292 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
2295 return; /* cannot happen */
2296 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
2299 static int fn_out_consume(void *priv
, char *line
, unsigned long len
)
2301 struct emit_callback
*ecbdata
= priv
;
2302 struct diff_options
*o
= ecbdata
->opt
;
2304 o
->found_changes
= 1;
2306 if (ecbdata
->header
) {
2307 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
2308 ecbdata
->header
->buf
, ecbdata
->header
->len
, 0);
2309 strbuf_reset(ecbdata
->header
);
2310 ecbdata
->header
= NULL
;
2313 if (ecbdata
->label_path
[0]) {
2314 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
2315 ecbdata
->label_path
[0],
2316 strlen(ecbdata
->label_path
[0]), 0);
2317 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
2318 ecbdata
->label_path
[1],
2319 strlen(ecbdata
->label_path
[1]), 0);
2320 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
2323 if (diff_suppress_blank_empty
2324 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
2329 if (line
[0] == '@') {
2330 if (ecbdata
->diff_words
)
2331 diff_words_flush(ecbdata
);
2332 len
= sane_truncate_line(line
, len
);
2333 find_lno(line
, ecbdata
);
2334 emit_hunk_header(ecbdata
, line
, len
);
2338 if (ecbdata
->diff_words
) {
2339 enum diff_symbol s
=
2340 ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
?
2341 DIFF_SYMBOL_WORDS_PORCELAIN
: DIFF_SYMBOL_WORDS
;
2342 if (line
[0] == '-') {
2343 diff_words_append(line
, len
,
2344 &ecbdata
->diff_words
->minus
);
2346 } else if (line
[0] == '+') {
2347 diff_words_append(line
, len
,
2348 &ecbdata
->diff_words
->plus
);
2350 } else if (starts_with(line
, "\\ ")) {
2352 * Eat the "no newline at eof" marker as if we
2353 * saw a "+" or "-" line with nothing on it,
2354 * and return without diff_words_flush() to
2355 * defer processing. If this is the end of
2356 * preimage, more "+" lines may come after it.
2360 diff_words_flush(ecbdata
);
2361 emit_diff_symbol(o
, s
, line
, len
, 0);
2367 ecbdata
->lno_in_postimage
++;
2368 emit_add_line(ecbdata
, line
+ 1, len
- 1);
2371 ecbdata
->lno_in_preimage
++;
2372 emit_del_line(ecbdata
, line
+ 1, len
- 1);
2375 ecbdata
->lno_in_postimage
++;
2376 ecbdata
->lno_in_preimage
++;
2377 emit_context_line(ecbdata
, line
+ 1, len
- 1);
2380 /* incomplete line at the end */
2381 ecbdata
->lno_in_preimage
++;
2382 emit_diff_symbol(o
, DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
2389 static void pprint_rename(struct strbuf
*name
, const char *a
, const char *b
)
2391 const char *old_name
= a
;
2392 const char *new_name
= b
;
2393 int pfx_length
, sfx_length
;
2394 int pfx_adjust_for_slash
;
2395 int len_a
= strlen(a
);
2396 int len_b
= strlen(b
);
2397 int a_midlen
, b_midlen
;
2398 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
2399 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
2401 if (qlen_a
|| qlen_b
) {
2402 quote_c_style(a
, name
, NULL
, 0);
2403 strbuf_addstr(name
, " => ");
2404 quote_c_style(b
, name
, NULL
, 0);
2408 /* Find common prefix */
2410 while (*old_name
&& *new_name
&& *old_name
== *new_name
) {
2411 if (*old_name
== '/')
2412 pfx_length
= old_name
- a
+ 1;
2417 /* Find common suffix */
2418 old_name
= a
+ len_a
;
2419 new_name
= b
+ len_b
;
2422 * If there is a common prefix, it must end in a slash. In
2423 * that case we let this loop run 1 into the prefix to see the
2426 * If there is no common prefix, we cannot do this as it would
2427 * underrun the input strings.
2429 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
2430 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old_name
&&
2431 b
+ pfx_length
- pfx_adjust_for_slash
<= new_name
&&
2432 *old_name
== *new_name
) {
2433 if (*old_name
== '/')
2434 sfx_length
= len_a
- (old_name
- a
);
2440 * pfx{mid-a => mid-b}sfx
2441 * {pfx-a => pfx-b}sfx
2442 * pfx{sfx-a => sfx-b}
2445 a_midlen
= len_a
- pfx_length
- sfx_length
;
2446 b_midlen
= len_b
- pfx_length
- sfx_length
;
2452 strbuf_grow(name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
2453 if (pfx_length
+ sfx_length
) {
2454 strbuf_add(name
, a
, pfx_length
);
2455 strbuf_addch(name
, '{');
2457 strbuf_add(name
, a
+ pfx_length
, a_midlen
);
2458 strbuf_addstr(name
, " => ");
2459 strbuf_add(name
, b
+ pfx_length
, b_midlen
);
2460 if (pfx_length
+ sfx_length
) {
2461 strbuf_addch(name
, '}');
2462 strbuf_add(name
, a
+ len_a
- sfx_length
, sfx_length
);
2466 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
2470 struct diffstat_file
*x
;
2472 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
2473 diffstat
->files
[diffstat
->nr
++] = x
;
2475 x
->from_name
= xstrdup(name_a
);
2476 x
->name
= xstrdup(name_b
);
2480 x
->from_name
= NULL
;
2481 x
->name
= xstrdup(name_a
);
2486 static int diffstat_consume(void *priv
, char *line
, unsigned long len
)
2488 struct diffstat_t
*diffstat
= priv
;
2489 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
2492 BUG("xdiff fed us an empty line");
2496 else if (line
[0] == '-')
2501 const char mime_boundary_leader
[] = "------------";
2503 static int scale_linear(int it
, int width
, int max_change
)
2508 * make sure that at least one '-' or '+' is printed if
2509 * there is any change to this path. The easiest way is to
2510 * scale linearly as if the allotted width is one column shorter
2511 * than it is, and then add 1 to the result.
2513 return 1 + (it
* (width
- 1) / max_change
);
2516 static void show_graph(struct strbuf
*out
, char ch
, int cnt
,
2517 const char *set
, const char *reset
)
2521 strbuf_addstr(out
, set
);
2522 strbuf_addchars(out
, ch
, cnt
);
2523 strbuf_addstr(out
, reset
);
2526 static void fill_print_name(struct diffstat_file
*file
)
2528 struct strbuf pname
= STRBUF_INIT
;
2530 if (file
->print_name
)
2533 if (file
->is_renamed
)
2534 pprint_rename(&pname
, file
->from_name
, file
->name
);
2536 quote_c_style(file
->name
, &pname
, NULL
, 0);
2539 strbuf_addf(&pname
, " (%s)", file
->comments
);
2541 file
->print_name
= strbuf_detach(&pname
, NULL
);
2544 static void print_stat_summary_inserts_deletes(struct diff_options
*options
,
2545 int files
, int insertions
, int deletions
)
2547 struct strbuf sb
= STRBUF_INIT
;
2550 assert(insertions
== 0 && deletions
== 0);
2551 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
2557 (files
== 1) ? " %d file changed" : " %d files changed",
2561 * For binary diff, the caller may want to print "x files
2562 * changed" with insertions == 0 && deletions == 0.
2564 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2565 * is probably less confusing (i.e skip over "2 files changed
2566 * but nothing about added/removed lines? Is this a bug in Git?").
2568 if (insertions
|| deletions
== 0) {
2570 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2574 if (deletions
|| insertions
== 0) {
2576 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2579 strbuf_addch(&sb
, '\n');
2580 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
2582 strbuf_release(&sb
);
2585 void print_stat_summary(FILE *fp
, int files
,
2586 int insertions
, int deletions
)
2588 struct diff_options o
;
2589 memset(&o
, 0, sizeof(o
));
2592 print_stat_summary_inserts_deletes(&o
, files
, insertions
, deletions
);
2595 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
2597 int i
, len
, add
, del
, adds
= 0, dels
= 0;
2598 uintmax_t max_change
= 0, max_len
= 0;
2599 int total_files
= data
->nr
, count
;
2600 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
2601 const char *reset
, *add_c
, *del_c
;
2602 int extra_shown
= 0;
2603 const char *line_prefix
= diff_line_prefix(options
);
2604 struct strbuf out
= STRBUF_INIT
;
2609 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
2611 reset
= diff_get_color_opt(options
, DIFF_RESET
);
2612 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
2613 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
2616 * Find the longest filename and max number of changes
2618 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
2619 struct diffstat_file
*file
= data
->files
[i
];
2620 uintmax_t change
= file
->added
+ file
->deleted
;
2622 if (!file
->is_interesting
&& (change
== 0)) {
2623 count
++; /* not shown == room for one more */
2626 fill_print_name(file
);
2627 len
= strlen(file
->print_name
);
2631 if (file
->is_unmerged
) {
2632 /* "Unmerged" is 8 characters */
2633 bin_width
= bin_width
< 8 ? 8 : bin_width
;
2636 if (file
->is_binary
) {
2637 /* "Bin XXX -> YYY bytes" */
2638 int w
= 14 + decimal_width(file
->added
)
2639 + decimal_width(file
->deleted
);
2640 bin_width
= bin_width
< w
? w
: bin_width
;
2641 /* Display change counts aligned with "Bin" */
2646 if (max_change
< change
)
2647 max_change
= change
;
2649 count
= i
; /* where we can stop scanning in data->files[] */
2652 * We have width = stat_width or term_columns() columns total.
2653 * We want a maximum of min(max_len, stat_name_width) for the name part.
2654 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2655 * We also need 1 for " " and 4 + decimal_width(max_change)
2656 * for " | NNNN " and one the empty column at the end, altogether
2657 * 6 + decimal_width(max_change).
2659 * If there's not enough space, we will use the smaller of
2660 * stat_name_width (if set) and 5/8*width for the filename,
2661 * and the rest for constant elements + graph part, but no more
2662 * than stat_graph_width for the graph part.
2663 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2664 * for the standard terminal size).
2666 * In other words: stat_width limits the maximum width, and
2667 * stat_name_width fixes the maximum width of the filename,
2668 * and is also used to divide available columns if there
2671 * Binary files are displayed with "Bin XXX -> YYY bytes"
2672 * instead of the change count and graph. This part is treated
2673 * similarly to the graph part, except that it is not
2674 * "scaled". If total width is too small to accommodate the
2675 * guaranteed minimum width of the filename part and the
2676 * separators and this message, this message will "overflow"
2677 * making the line longer than the maximum width.
2680 if (options
->stat_width
== -1)
2681 width
= term_columns() - strlen(line_prefix
);
2683 width
= options
->stat_width
? options
->stat_width
: 80;
2684 number_width
= decimal_width(max_change
) > number_width
?
2685 decimal_width(max_change
) : number_width
;
2687 if (options
->stat_graph_width
== -1)
2688 options
->stat_graph_width
= diff_stat_graph_width
;
2691 * Guarantee 3/8*16==6 for the graph part
2692 * and 5/8*16==10 for the filename part
2694 if (width
< 16 + 6 + number_width
)
2695 width
= 16 + 6 + number_width
;
2698 * First assign sizes that are wanted, ignoring available width.
2699 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2700 * starting from "XXX" should fit in graph_width.
2702 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
2703 if (options
->stat_graph_width
&&
2704 options
->stat_graph_width
< graph_width
)
2705 graph_width
= options
->stat_graph_width
;
2707 name_width
= (options
->stat_name_width
> 0 &&
2708 options
->stat_name_width
< max_len
) ?
2709 options
->stat_name_width
: max_len
;
2712 * Adjust adjustable widths not to exceed maximum width
2714 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
2715 if (graph_width
> width
* 3/8 - number_width
- 6) {
2716 graph_width
= width
* 3/8 - number_width
- 6;
2717 if (graph_width
< 6)
2721 if (options
->stat_graph_width
&&
2722 graph_width
> options
->stat_graph_width
)
2723 graph_width
= options
->stat_graph_width
;
2724 if (name_width
> width
- number_width
- 6 - graph_width
)
2725 name_width
= width
- number_width
- 6 - graph_width
;
2727 graph_width
= width
- number_width
- 6 - name_width
;
2731 * From here name_width is the width of the name area,
2732 * and graph_width is the width of the graph area.
2733 * max_change is used to scale graph properly.
2735 for (i
= 0; i
< count
; i
++) {
2736 const char *prefix
= "";
2737 struct diffstat_file
*file
= data
->files
[i
];
2738 char *name
= file
->print_name
;
2739 uintmax_t added
= file
->added
;
2740 uintmax_t deleted
= file
->deleted
;
2743 if (!file
->is_interesting
&& (added
+ deleted
== 0))
2747 * "scale" the filename
2750 name_len
= strlen(name
);
2751 if (name_width
< name_len
) {
2755 name
+= name_len
- len
;
2756 slash
= strchr(name
, '/');
2761 if (file
->is_binary
) {
2762 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2763 strbuf_addf(&out
, " %*s", number_width
, "Bin");
2764 if (!added
&& !deleted
) {
2765 strbuf_addch(&out
, '\n');
2766 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2767 out
.buf
, out
.len
, 0);
2771 strbuf_addf(&out
, " %s%"PRIuMAX
"%s",
2772 del_c
, deleted
, reset
);
2773 strbuf_addstr(&out
, " -> ");
2774 strbuf_addf(&out
, "%s%"PRIuMAX
"%s",
2775 add_c
, added
, reset
);
2776 strbuf_addstr(&out
, " bytes\n");
2777 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2778 out
.buf
, out
.len
, 0);
2782 else if (file
->is_unmerged
) {
2783 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2784 strbuf_addstr(&out
, " Unmerged\n");
2785 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2786 out
.buf
, out
.len
, 0);
2792 * scale the add/delete
2797 if (graph_width
<= max_change
) {
2798 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
2799 if (total
< 2 && add
&& del
)
2800 /* width >= 2 due to the sanity check */
2803 add
= scale_linear(add
, graph_width
, max_change
);
2806 del
= scale_linear(del
, graph_width
, max_change
);
2810 strbuf_addf(&out
, " %s%-*s |", prefix
, len
, name
);
2811 strbuf_addf(&out
, " %*"PRIuMAX
"%s",
2812 number_width
, added
+ deleted
,
2813 added
+ deleted
? " " : "");
2814 show_graph(&out
, '+', add
, add_c
, reset
);
2815 show_graph(&out
, '-', del
, del_c
, reset
);
2816 strbuf_addch(&out
, '\n');
2817 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2818 out
.buf
, out
.len
, 0);
2822 for (i
= 0; i
< data
->nr
; i
++) {
2823 struct diffstat_file
*file
= data
->files
[i
];
2824 uintmax_t added
= file
->added
;
2825 uintmax_t deleted
= file
->deleted
;
2827 if (file
->is_unmerged
||
2828 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
2833 if (!file
->is_binary
) {
2840 emit_diff_symbol(options
,
2841 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
2846 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2847 strbuf_release(&out
);
2850 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
2852 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
2857 for (i
= 0; i
< data
->nr
; i
++) {
2858 int added
= data
->files
[i
]->added
;
2859 int deleted
= data
->files
[i
]->deleted
;
2861 if (data
->files
[i
]->is_unmerged
||
2862 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
2864 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
2869 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2872 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
2879 for (i
= 0; i
< data
->nr
; i
++) {
2880 struct diffstat_file
*file
= data
->files
[i
];
2882 fprintf(options
->file
, "%s", diff_line_prefix(options
));
2884 if (file
->is_binary
)
2885 fprintf(options
->file
, "-\t-\t");
2887 fprintf(options
->file
,
2888 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
2889 file
->added
, file
->deleted
);
2890 if (options
->line_termination
) {
2891 fill_print_name(file
);
2892 if (!file
->is_renamed
)
2893 write_name_quoted(file
->name
, options
->file
,
2894 options
->line_termination
);
2896 fputs(file
->print_name
, options
->file
);
2897 putc(options
->line_termination
, options
->file
);
2900 if (file
->is_renamed
) {
2901 putc('\0', options
->file
);
2902 write_name_quoted(file
->from_name
, options
->file
, '\0');
2904 write_name_quoted(file
->name
, options
->file
, '\0');
2909 struct dirstat_file
{
2911 unsigned long changed
;
2914 struct dirstat_dir
{
2915 struct dirstat_file
*files
;
2916 int alloc
, nr
, permille
, cumulative
;
2919 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
2920 unsigned long changed
, const char *base
, int baselen
)
2922 unsigned long sum_changes
= 0;
2923 unsigned int sources
= 0;
2924 const char *line_prefix
= diff_line_prefix(opt
);
2927 struct dirstat_file
*f
= dir
->files
;
2928 int namelen
= strlen(f
->name
);
2929 unsigned long changes
;
2932 if (namelen
< baselen
)
2934 if (memcmp(f
->name
, base
, baselen
))
2936 slash
= strchr(f
->name
+ baselen
, '/');
2938 int newbaselen
= slash
+ 1 - f
->name
;
2939 changes
= gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
2942 changes
= f
->changed
;
2947 sum_changes
+= changes
;
2951 * We don't report dirstat's for
2953 * - or cases where everything came from a single directory
2954 * under this directory (sources == 1).
2956 if (baselen
&& sources
!= 1) {
2958 int permille
= sum_changes
* 1000 / changed
;
2959 if (permille
>= dir
->permille
) {
2960 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
2961 permille
/ 10, permille
% 10, baselen
, base
);
2962 if (!dir
->cumulative
)
2970 static int dirstat_compare(const void *_a
, const void *_b
)
2972 const struct dirstat_file
*a
= _a
;
2973 const struct dirstat_file
*b
= _b
;
2974 return strcmp(a
->name
, b
->name
);
2977 static void show_dirstat(struct diff_options
*options
)
2980 unsigned long changed
;
2981 struct dirstat_dir dir
;
2982 struct diff_queue_struct
*q
= &diff_queued_diff
;
2987 dir
.permille
= options
->dirstat_permille
;
2988 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
2991 for (i
= 0; i
< q
->nr
; i
++) {
2992 struct diff_filepair
*p
= q
->queue
[i
];
2994 unsigned long copied
, added
, damage
;
2995 struct diff_populate_filespec_options dpf_options
= {
2996 .check_size_only
= 1,
2999 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
3001 if (p
->one
->oid_valid
&& p
->two
->oid_valid
&&
3002 oideq(&p
->one
->oid
, &p
->two
->oid
)) {
3004 * The SHA1 has not changed, so pre-/post-content is
3005 * identical. We can therefore skip looking at the
3006 * file contents altogether.
3012 if (options
->flags
.dirstat_by_file
) {
3014 * In --dirstat-by-file mode, we don't really need to
3015 * look at the actual file contents at all.
3016 * The fact that the SHA1 changed is enough for us to
3017 * add this file to the list of results
3018 * (with each file contributing equal damage).
3024 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
3025 diff_populate_filespec(options
->repo
, p
->one
, NULL
);
3026 diff_populate_filespec(options
->repo
, p
->two
, NULL
);
3027 diffcore_count_changes(options
->repo
,
3028 p
->one
, p
->two
, NULL
, NULL
,
3030 diff_free_filespec_data(p
->one
);
3031 diff_free_filespec_data(p
->two
);
3032 } else if (DIFF_FILE_VALID(p
->one
)) {
3033 diff_populate_filespec(options
->repo
, p
->one
, &dpf_options
);
3035 diff_free_filespec_data(p
->one
);
3036 } else if (DIFF_FILE_VALID(p
->two
)) {
3037 diff_populate_filespec(options
->repo
, p
->two
, &dpf_options
);
3039 added
= p
->two
->size
;
3040 diff_free_filespec_data(p
->two
);
3045 * Original minus copied is the removed material,
3046 * added is the new material. They are both damages
3047 * made to the preimage.
3048 * If the resulting damage is zero, we know that
3049 * diffcore_count_changes() considers the two entries to
3050 * be identical, but since the oid changed, we
3051 * know that there must have been _some_ kind of change,
3052 * so we force all entries to have damage > 0.
3054 damage
= (p
->one
->size
- copied
) + added
;
3059 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3060 dir
.files
[dir
.nr
].name
= name
;
3061 dir
.files
[dir
.nr
].changed
= damage
;
3066 /* This can happen even with many files, if everything was renames */
3070 /* Show all directories with more than x% of the changes */
3071 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
3072 gather_dirstat(options
, &dir
, changed
, "", 0);
3075 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
3078 unsigned long changed
;
3079 struct dirstat_dir dir
;
3087 dir
.permille
= options
->dirstat_permille
;
3088 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3091 for (i
= 0; i
< data
->nr
; i
++) {
3092 struct diffstat_file
*file
= data
->files
[i
];
3093 unsigned long damage
= file
->added
+ file
->deleted
;
3094 if (file
->is_binary
)
3096 * binary files counts bytes, not lines. Must find some
3097 * way to normalize binary bytes vs. textual lines.
3098 * The following heuristic assumes that there are 64
3100 * This is stupid and ugly, but very cheap...
3102 damage
= DIV_ROUND_UP(damage
, 64);
3103 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3104 dir
.files
[dir
.nr
].name
= file
->name
;
3105 dir
.files
[dir
.nr
].changed
= damage
;
3110 /* This can happen even with many files, if everything was renames */
3114 /* Show all directories with more than x% of the changes */
3115 QSORT(dir
.files
, dir
.nr
, dirstat_compare
);
3116 gather_dirstat(options
, &dir
, changed
, "", 0);
3119 static void free_diffstat_file(struct diffstat_file
*f
)
3121 free(f
->print_name
);
3127 void free_diffstat_info(struct diffstat_t
*diffstat
)
3130 for (i
= 0; i
< diffstat
->nr
; i
++)
3131 free_diffstat_file(diffstat
->files
[i
]);
3132 free(diffstat
->files
);
3135 struct checkdiff_t
{
3136 const char *filename
;
3138 int conflict_marker_size
;
3139 struct diff_options
*o
;
3144 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
3149 if (len
< marker_size
+ 1)
3151 firstchar
= line
[0];
3152 switch (firstchar
) {
3153 case '=': case '>': case '<': case '|':
3158 for (cnt
= 1; cnt
< marker_size
; cnt
++)
3159 if (line
[cnt
] != firstchar
)
3161 /* line[1] through line[marker_size-1] are same as firstchar */
3162 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
3167 static void checkdiff_consume_hunk(void *priv
,
3168 long ob
, long on
, long nb
, long nn
,
3169 const char *func
, long funclen
)
3172 struct checkdiff_t
*data
= priv
;
3173 data
->lineno
= nb
- 1;
3176 static int checkdiff_consume(void *priv
, char *line
, unsigned long len
)
3178 struct checkdiff_t
*data
= priv
;
3179 int marker_size
= data
->conflict_marker_size
;
3180 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
3181 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
3182 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
3184 const char *line_prefix
;
3187 line_prefix
= diff_line_prefix(data
->o
);
3189 if (line
[0] == '+') {
3192 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
3194 fprintf(data
->o
->file
,
3195 "%s%s:%d: leftover conflict marker\n",
3196 line_prefix
, data
->filename
, data
->lineno
);
3198 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
3201 data
->status
|= bad
;
3202 err
= whitespace_error_string(bad
);
3203 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
3204 line_prefix
, data
->filename
, data
->lineno
, err
);
3206 emit_line(data
->o
, set
, reset
, line
, 1);
3207 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
3208 data
->o
->file
, set
, reset
, ws
);
3209 } else if (line
[0] == ' ') {
3215 static unsigned char *deflate_it(char *data
,
3217 unsigned long *result_size
)
3220 unsigned char *deflated
;
3223 git_deflate_init(&stream
, zlib_compression_level
);
3224 bound
= git_deflate_bound(&stream
, size
);
3225 deflated
= xmalloc(bound
);
3226 stream
.next_out
= deflated
;
3227 stream
.avail_out
= bound
;
3229 stream
.next_in
= (unsigned char *)data
;
3230 stream
.avail_in
= size
;
3231 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
3233 git_deflate_end(&stream
);
3234 *result_size
= stream
.total_out
;
3238 static void emit_binary_diff_body(struct diff_options
*o
,
3239 mmfile_t
*one
, mmfile_t
*two
)
3245 unsigned long orig_size
;
3246 unsigned long delta_size
;
3247 unsigned long deflate_size
;
3248 unsigned long data_size
;
3250 /* We could do deflated delta, or we could do just deflated two,
3251 * whichever is smaller.
3254 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
3255 if (one
->size
&& two
->size
) {
3256 delta
= diff_delta(one
->ptr
, one
->size
,
3257 two
->ptr
, two
->size
,
3258 &delta_size
, deflate_size
);
3260 void *to_free
= delta
;
3261 orig_size
= delta_size
;
3262 delta
= deflate_it(delta
, delta_size
, &delta_size
);
3267 if (delta
&& delta_size
< deflate_size
) {
3268 char *s
= xstrfmt("%"PRIuMAX
, (uintmax_t)orig_size
);
3269 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
3274 data_size
= delta_size
;
3276 char *s
= xstrfmt("%lu", two
->size
);
3277 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
3282 data_size
= deflate_size
;
3285 /* emit data encoded in base85 */
3289 int bytes
= (52 < data_size
) ? 52 : data_size
;
3293 line
[0] = bytes
+ 'A' - 1;
3295 line
[0] = bytes
- 26 + 'a' - 1;
3296 encode_85(line
+ 1, cp
, bytes
);
3297 cp
= (char *) cp
+ bytes
;
3303 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_BODY
,
3306 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_FOOTER
, NULL
, 0, 0);
3310 static void emit_binary_diff(struct diff_options
*o
,
3311 mmfile_t
*one
, mmfile_t
*two
)
3313 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER
, NULL
, 0, 0);
3314 emit_binary_diff_body(o
, one
, two
);
3315 emit_binary_diff_body(o
, two
, one
);
3318 int diff_filespec_is_binary(struct repository
*r
,
3319 struct diff_filespec
*one
)
3321 struct diff_populate_filespec_options dpf_options
= {
3325 if (one
->is_binary
== -1) {
3326 diff_filespec_load_driver(one
, r
->index
);
3327 if (one
->driver
->binary
!= -1)
3328 one
->is_binary
= one
->driver
->binary
;
3330 if (!one
->data
&& DIFF_FILE_VALID(one
))
3331 diff_populate_filespec(r
, one
, &dpf_options
);
3332 if (one
->is_binary
== -1 && one
->data
)
3333 one
->is_binary
= buffer_is_binary(one
->data
,
3335 if (one
->is_binary
== -1)
3339 return one
->is_binary
;
3342 static const struct userdiff_funcname
*
3343 diff_funcname_pattern(struct diff_options
*o
, struct diff_filespec
*one
)
3345 diff_filespec_load_driver(one
, o
->repo
->index
);
3346 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
3349 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
3351 if (!options
->a_prefix
)
3352 options
->a_prefix
= a
;
3353 if (!options
->b_prefix
)
3354 options
->b_prefix
= b
;
3357 struct userdiff_driver
*get_textconv(struct repository
*r
,
3358 struct diff_filespec
*one
)
3360 if (!DIFF_FILE_VALID(one
))
3363 diff_filespec_load_driver(one
, r
->index
);
3364 return userdiff_get_textconv(r
, one
->driver
);
3367 static struct string_list
*additional_headers(struct diff_options
*o
,
3370 if (!o
->additional_path_headers
)
3372 return strmap_get(o
->additional_path_headers
, path
);
3375 static void add_formatted_header(struct strbuf
*msg
,
3377 const char *line_prefix
,
3381 const char *next
, *newline
;
3383 for (next
= header
; *next
; next
= newline
) {
3384 newline
= strchrnul(next
, '\n');
3385 strbuf_addf(msg
, "%s%s%.*s%s\n", line_prefix
, meta
,
3386 (int)(newline
- next
), next
, reset
);
3392 static void add_formatted_headers(struct strbuf
*msg
,
3393 struct string_list
*more_headers
,
3394 const char *line_prefix
,
3400 for (i
= 0; i
< more_headers
->nr
; i
++)
3401 add_formatted_header(msg
, more_headers
->items
[i
].string
,
3402 line_prefix
, meta
, reset
);
3405 static int diff_filepair_is_phoney(struct diff_filespec
*one
,
3406 struct diff_filespec
*two
)
3409 * This function specifically looks for pairs injected by
3410 * create_filepairs_for_header_only_notifications(). Such
3411 * pairs are "phoney" in that they do not represent any
3412 * content or even mode difference, but were inserted because
3413 * diff_queued_diff previously had no pair associated with
3414 * that path but we needed some pair to avoid losing the
3415 * "remerge CONFLICT" header associated with the path.
3417 return !DIFF_FILE_VALID(one
) && !DIFF_FILE_VALID(two
);
3420 static void builtin_diff(const char *name_a
,
3422 struct diff_filespec
*one
,
3423 struct diff_filespec
*two
,
3424 const char *xfrm_msg
,
3425 int must_show_header
,
3426 struct diff_options
*o
,
3427 int complete_rewrite
)
3431 char *a_one
, *b_two
;
3432 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
3433 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
3434 const char *a_prefix
, *b_prefix
;
3435 struct userdiff_driver
*textconv_one
= NULL
;
3436 struct userdiff_driver
*textconv_two
= NULL
;
3437 struct strbuf header
= STRBUF_INIT
;
3438 const char *line_prefix
= diff_line_prefix(o
);
3440 diff_set_mnemonic_prefix(o
, "a/", "b/");
3441 if (o
->flags
.reverse_diff
) {
3442 a_prefix
= o
->b_prefix
;
3443 b_prefix
= o
->a_prefix
;
3445 a_prefix
= o
->a_prefix
;
3446 b_prefix
= o
->b_prefix
;
3449 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
3450 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3451 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3452 (!diff_filepair_is_phoney(one
, two
))) {
3453 show_submodule_diff_summary(o
, one
->path
? one
->path
: two
->path
,
3454 &one
->oid
, &two
->oid
,
3455 two
->dirty_submodule
);
3457 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
3458 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3459 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3460 (!diff_filepair_is_phoney(one
, two
))) {
3461 show_submodule_inline_diff(o
, one
->path
? one
->path
: two
->path
,
3462 &one
->oid
, &two
->oid
,
3463 two
->dirty_submodule
);
3467 if (o
->flags
.allow_textconv
) {
3468 textconv_one
= get_textconv(o
->repo
, one
);
3469 textconv_two
= get_textconv(o
->repo
, two
);
3472 /* Never use a non-valid filename anywhere if at all possible */
3473 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
3474 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
3476 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
3477 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
3478 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
3479 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
3480 if (diff_filepair_is_phoney(one
, two
)) {
3482 * We should only reach this point for pairs generated from
3483 * create_filepairs_for_header_only_notifications(). For
3484 * these, we want to avoid the "/dev/null" special casing
3485 * above, because we do not want such pairs shown as either
3486 * "new file" or "deleted file" below.
3491 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3492 if (lbl
[0][0] == '/') {
3494 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3496 strbuf_addstr(&header
, xfrm_msg
);
3497 must_show_header
= 1;
3499 else if (lbl
[1][0] == '/') {
3500 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3502 strbuf_addstr(&header
, xfrm_msg
);
3503 must_show_header
= 1;
3506 if (one
->mode
!= two
->mode
) {
3507 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3508 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3509 must_show_header
= 1;
3512 strbuf_addstr(&header
, xfrm_msg
);
3515 * we do not run diff between different kind
3518 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3519 goto free_ab_and_return
;
3520 if (complete_rewrite
&&
3521 (textconv_one
|| !diff_filespec_is_binary(o
->repo
, one
)) &&
3522 (textconv_two
|| !diff_filespec_is_binary(o
->repo
, two
))) {
3523 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3524 header
.buf
, header
.len
, 0);
3525 strbuf_reset(&header
);
3526 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3527 textconv_one
, textconv_two
, o
);
3528 o
->found_changes
= 1;
3529 goto free_ab_and_return
;
3533 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3534 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3536 strbuf_reset(&header
);
3537 goto free_ab_and_return
;
3538 } else if (!o
->flags
.text
&&
3539 ( (!textconv_one
&& diff_filespec_is_binary(o
->repo
, one
)) ||
3540 (!textconv_two
&& diff_filespec_is_binary(o
->repo
, two
)) )) {
3541 struct strbuf sb
= STRBUF_INIT
;
3542 if (!one
->data
&& !two
->data
&&
3543 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3545 if (oideq(&one
->oid
, &two
->oid
)) {
3546 if (must_show_header
)
3547 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3548 header
.buf
, header
.len
,
3550 goto free_ab_and_return
;
3552 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3553 header
.buf
, header
.len
, 0);
3554 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3555 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3556 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3558 strbuf_release(&sb
);
3559 goto free_ab_and_return
;
3561 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3562 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3563 die("unable to read files to diff");
3564 /* Quite common confusing case */
3565 if (mf1
.size
== mf2
.size
&&
3566 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3567 if (must_show_header
)
3568 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3569 header
.buf
, header
.len
, 0);
3570 goto free_ab_and_return
;
3572 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3573 strbuf_reset(&header
);
3574 if (o
->flags
.binary
)
3575 emit_binary_diff(o
, &mf1
, &mf2
);
3577 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3578 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3579 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3581 strbuf_release(&sb
);
3583 o
->found_changes
= 1;
3585 /* Crazy xdl interfaces.. */
3586 const char *diffopts
;
3590 struct emit_callback ecbdata
;
3591 const struct userdiff_funcname
*pe
;
3593 if (must_show_header
) {
3594 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3595 header
.buf
, header
.len
, 0);
3596 strbuf_reset(&header
);
3599 mf1
.size
= fill_textconv(o
->repo
, textconv_one
, one
, &mf1
.ptr
);
3600 mf2
.size
= fill_textconv(o
->repo
, textconv_two
, two
, &mf2
.ptr
);
3602 pe
= diff_funcname_pattern(o
, one
);
3604 pe
= diff_funcname_pattern(o
, two
);
3606 memset(&xpp
, 0, sizeof(xpp
));
3607 memset(&xecfg
, 0, sizeof(xecfg
));
3608 memset(&ecbdata
, 0, sizeof(ecbdata
));
3609 if (o
->flags
.suppress_diff_headers
)
3611 ecbdata
.label_path
= lbl
;
3612 ecbdata
.color_diff
= want_color(o
->use_color
);
3613 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
3614 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3615 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3617 if (header
.len
&& !o
->flags
.suppress_diff_headers
)
3618 ecbdata
.header
= &header
;
3619 xpp
.flags
= o
->xdl_opts
;
3620 xpp
.ignore_regex
= o
->ignore_regex
;
3621 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3622 xpp
.anchors
= o
->anchors
;
3623 xpp
.anchors_nr
= o
->anchors_nr
;
3624 xecfg
.ctxlen
= o
->context
;
3625 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3626 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3627 if (o
->flags
.funccontext
)
3628 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3630 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3632 diffopts
= getenv("GIT_DIFF_OPTS");
3635 else if (skip_prefix(diffopts
, "--unified=", &v
))
3636 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3637 else if (skip_prefix(diffopts
, "-u", &v
))
3638 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3641 init_diff_words_data(&ecbdata
, o
, one
, two
);
3642 if (xdi_diff_outf(&mf1
, &mf2
, NULL
, fn_out_consume
,
3643 &ecbdata
, &xpp
, &xecfg
))
3644 die("unable to generate diff for %s", one
->path
);
3646 free_diff_words_data(&ecbdata
);
3651 xdiff_clear_find_func(&xecfg
);
3655 strbuf_release(&header
);
3656 diff_free_filespec_data(one
);
3657 diff_free_filespec_data(two
);
3663 static char *get_compact_summary(const struct diff_filepair
*p
, int is_renamed
)
3666 if (p
->status
== DIFF_STATUS_ADDED
) {
3667 if (S_ISLNK(p
->two
->mode
))
3669 else if ((p
->two
->mode
& 0777) == 0755)
3673 } else if (p
->status
== DIFF_STATUS_DELETED
)
3676 if (S_ISLNK(p
->one
->mode
) && !S_ISLNK(p
->two
->mode
))
3678 else if (!S_ISLNK(p
->one
->mode
) && S_ISLNK(p
->two
->mode
))
3680 else if ((p
->one
->mode
& 0777) == 0644 &&
3681 (p
->two
->mode
& 0777) == 0755)
3683 else if ((p
->one
->mode
& 0777) == 0755 &&
3684 (p
->two
->mode
& 0777) == 0644)
3689 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3690 struct diff_filespec
*one
,
3691 struct diff_filespec
*two
,
3692 struct diffstat_t
*diffstat
,
3693 struct diff_options
*o
,
3694 struct diff_filepair
*p
)
3697 struct diffstat_file
*data
;
3699 int complete_rewrite
= 0;
3701 if (!DIFF_PAIR_UNMERGED(p
)) {
3702 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3703 complete_rewrite
= 1;
3706 data
= diffstat_add(diffstat
, name_a
, name_b
);
3707 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3708 if (o
->flags
.stat_with_summary
)
3709 data
->comments
= get_compact_summary(p
, data
->is_renamed
);
3712 data
->is_unmerged
= 1;
3716 /* saves some reads if true, not a guarantee of diff outcome */
3717 may_differ
= !(one
->oid_valid
&& two
->oid_valid
&&
3718 oideq(&one
->oid
, &two
->oid
));
3720 if (diff_filespec_is_binary(o
->repo
, one
) ||
3721 diff_filespec_is_binary(o
->repo
, two
)) {
3722 data
->is_binary
= 1;
3727 data
->added
= diff_filespec_size(o
->repo
, two
);
3728 data
->deleted
= diff_filespec_size(o
->repo
, one
);
3732 else if (complete_rewrite
) {
3733 diff_populate_filespec(o
->repo
, one
, NULL
);
3734 diff_populate_filespec(o
->repo
, two
, NULL
);
3735 data
->deleted
= count_lines(one
->data
, one
->size
);
3736 data
->added
= count_lines(two
->data
, two
->size
);
3739 else if (may_differ
) {
3740 /* Crazy xdl interfaces.. */
3744 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3745 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3746 die("unable to read files to diff");
3748 memset(&xpp
, 0, sizeof(xpp
));
3749 memset(&xecfg
, 0, sizeof(xecfg
));
3750 xpp
.flags
= o
->xdl_opts
;
3751 xpp
.ignore_regex
= o
->ignore_regex
;
3752 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3753 xpp
.anchors
= o
->anchors
;
3754 xpp
.anchors_nr
= o
->anchors_nr
;
3755 xecfg
.ctxlen
= o
->context
;
3756 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3757 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
3758 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
3759 diffstat_consume
, diffstat
, &xpp
, &xecfg
))
3760 die("unable to generate diffstat for %s", one
->path
);
3762 if (DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
)) {
3763 struct diffstat_file
*file
=
3764 diffstat
->files
[diffstat
->nr
- 1];
3766 * Omit diffstats of modified files where nothing changed.
3767 * Even if may_differ, this might be the case due to
3768 * ignoring whitespace changes, etc.
3770 * But note that we special-case additions, deletions,
3771 * renames, and mode changes as adding an empty file,
3772 * for example is still of interest.
3774 if ((p
->status
== DIFF_STATUS_MODIFIED
)
3777 && one
->mode
== two
->mode
) {
3778 free_diffstat_file(file
);
3784 diff_free_filespec_data(one
);
3785 diff_free_filespec_data(two
);
3788 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3789 const char *attr_path
,
3790 struct diff_filespec
*one
,
3791 struct diff_filespec
*two
,
3792 struct diff_options
*o
)
3795 struct checkdiff_t data
;
3800 memset(&data
, 0, sizeof(data
));
3801 data
.filename
= name_b
? name_b
: name_a
;
3804 data
.ws_rule
= whitespace_rule(o
->repo
->index
, attr_path
);
3805 data
.conflict_marker_size
= ll_merge_marker_size(o
->repo
->index
, attr_path
);
3807 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3808 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3809 die("unable to read files to diff");
3812 * All the other codepaths check both sides, but not checking
3813 * the "old" side here is deliberate. We are checking the newly
3814 * introduced changes, and as long as the "new" side is text, we
3815 * can and should check what it introduces.
3817 if (diff_filespec_is_binary(o
->repo
, two
))
3818 goto free_and_return
;
3820 /* Crazy xdl interfaces.. */
3824 memset(&xpp
, 0, sizeof(xpp
));
3825 memset(&xecfg
, 0, sizeof(xecfg
));
3826 xecfg
.ctxlen
= 1; /* at least one context line */
3828 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume_hunk
,
3829 checkdiff_consume
, &data
,
3831 die("unable to generate checkdiff for %s", one
->path
);
3833 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3834 struct emit_callback ecbdata
;
3837 ecbdata
.ws_rule
= data
.ws_rule
;
3838 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3839 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3844 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3845 fprintf(o
->file
, "%s:%d: %s.\n",
3846 data
.filename
, blank_at_eof
, err
);
3847 data
.status
= 1; /* report errors */
3852 diff_free_filespec_data(one
);
3853 diff_free_filespec_data(two
);
3855 o
->flags
.check_failed
= 1;
3858 struct diff_filespec
*alloc_filespec(const char *path
)
3860 struct diff_filespec
*spec
;
3862 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3864 spec
->is_binary
= -1;
3868 void free_filespec(struct diff_filespec
*spec
)
3870 if (!--spec
->count
) {
3871 diff_free_filespec_data(spec
);
3876 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3877 int oid_valid
, unsigned short mode
)
3880 spec
->mode
= canon_mode(mode
);
3881 oidcpy(&spec
->oid
, oid
);
3882 spec
->oid_valid
= oid_valid
;
3887 * Given a name and sha1 pair, if the index tells us the file in
3888 * the work tree has that object contents, return true, so that
3889 * prepare_temp_file() does not have to inflate and extract.
3891 static int reuse_worktree_file(struct index_state
*istate
,
3893 const struct object_id
*oid
,
3896 const struct cache_entry
*ce
;
3901 * We do not read the cache ourselves here, because the
3902 * benchmark with my previous version that always reads cache
3903 * shows that it makes things worse for diff-tree comparing
3904 * two linux-2.6 kernel trees in an already checked out work
3905 * tree. This is because most diff-tree comparisons deal with
3906 * only a small number of files, while reading the cache is
3907 * expensive for a large project, and its cost outweighs the
3908 * savings we get by not inflating the object to a temporary
3909 * file. Practically, this code only helps when we are used
3910 * by diff-cache --cached, which does read the cache before
3916 /* We want to avoid the working directory if our caller
3917 * doesn't need the data in a normal file, this system
3918 * is rather slow with its stat/open/mmap/close syscalls,
3919 * and the object is contained in a pack file. The pack
3920 * is probably already open and will be faster to obtain
3921 * the data through than the working directory. Loose
3922 * objects however would tend to be slower as they need
3923 * to be individually opened and inflated.
3925 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_object_pack(oid
))
3929 * Similarly, if we'd have to convert the file contents anyway, that
3930 * makes the optimization not worthwhile.
3932 if (!want_file
&& would_convert_to_git(istate
, name
))
3936 * If this path does not match our sparse-checkout definition,
3937 * then the file will not be in the working directory.
3939 if (!path_in_sparse_checkout(name
, istate
))
3943 pos
= index_name_pos(istate
, name
, len
);
3946 ce
= istate
->cache
[pos
];
3949 * This is not the sha1 we are looking for, or
3950 * unreusable because it is not a regular file.
3952 if (!oideq(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
3956 * If ce is marked as "assume unchanged", there is no
3957 * guarantee that work tree matches what we are looking for.
3959 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
3963 * If ce matches the file in the work tree, we can reuse it.
3965 if (ce_uptodate(ce
) ||
3966 (!lstat(name
, &st
) && !ie_match_stat(istate
, ce
, &st
, 0)))
3972 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
3974 struct strbuf buf
= STRBUF_INIT
;
3977 /* Are we looking at the work tree? */
3978 if (s
->dirty_submodule
)
3981 strbuf_addf(&buf
, "Subproject commit %s%s\n",
3982 oid_to_hex(&s
->oid
), dirty
);
3986 strbuf_release(&buf
);
3988 s
->data
= strbuf_detach(&buf
, NULL
);
3995 * While doing rename detection and pickaxe operation, we may need to
3996 * grab the data for the blob (or file) for our own in-core comparison.
3997 * diff_filespec has data and size fields for this purpose.
3999 int diff_populate_filespec(struct repository
*r
,
4000 struct diff_filespec
*s
,
4001 const struct diff_populate_filespec_options
*options
)
4003 int size_only
= options
? options
->check_size_only
: 0;
4004 int check_binary
= options
? options
->check_binary
: 0;
4006 int conv_flags
= global_conv_flags_eol
;
4008 * demote FAIL to WARN to allow inspecting the situation
4009 * instead of refusing.
4011 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
4012 conv_flags
= CONV_EOL_RNDTRP_WARN
;
4014 if (!DIFF_FILE_VALID(s
))
4015 die("internal error: asking to populate invalid file.");
4016 if (S_ISDIR(s
->mode
))
4022 if (size_only
&& 0 < s
->size
)
4025 if (S_ISGITLINK(s
->mode
))
4026 return diff_populate_gitlink(s
, size_only
);
4028 if (!s
->oid_valid
||
4029 reuse_worktree_file(r
->index
, s
->path
, &s
->oid
, 0)) {
4030 struct strbuf buf
= STRBUF_INIT
;
4034 if (lstat(s
->path
, &st
) < 0) {
4038 s
->data
= (char *)"";
4042 s
->size
= xsize_t(st
.st_size
);
4045 if (S_ISLNK(st
.st_mode
)) {
4046 struct strbuf sb
= STRBUF_INIT
;
4048 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
4051 s
->data
= strbuf_detach(&sb
, NULL
);
4057 * Even if the caller would be happy with getting
4058 * only the size, we cannot return early at this
4059 * point if the path requires us to run the content
4062 if (size_only
&& !would_convert_to_git(r
->index
, s
->path
))
4066 * Note: this check uses xsize_t(st.st_size) that may
4067 * not be the true size of the blob after it goes
4068 * through convert_to_git(). This may not strictly be
4069 * correct, but the whole point of big_file_threshold
4070 * and is_binary check being that we want to avoid
4071 * opening the file and inspecting the contents, this
4075 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4079 fd
= open(s
->path
, O_RDONLY
);
4082 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
4084 s
->should_munmap
= 1;
4087 * Convert from working tree format to canonical git format
4089 if (convert_to_git(r
->index
, s
->path
, s
->data
, s
->size
, &buf
, conv_flags
)) {
4091 munmap(s
->data
, s
->size
);
4092 s
->should_munmap
= 0;
4093 s
->data
= strbuf_detach(&buf
, &size
);
4099 struct object_info info
= {
4103 if (!(size_only
|| check_binary
))
4105 * Set contentp, since there is no chance that merely
4106 * the size is sufficient.
4108 info
.contentp
= &s
->data
;
4110 if (options
&& options
->missing_object_cb
) {
4111 if (!oid_object_info_extended(r
, &s
->oid
, &info
,
4112 OBJECT_INFO_LOOKUP_REPLACE
|
4113 OBJECT_INFO_SKIP_FETCH_OBJECT
))
4115 options
->missing_object_cb(options
->missing_object_data
);
4117 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4118 OBJECT_INFO_LOOKUP_REPLACE
))
4119 die("unable to read %s", oid_to_hex(&s
->oid
));
4122 if (size_only
|| check_binary
) {
4125 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4130 if (!info
.contentp
) {
4131 info
.contentp
= &s
->data
;
4132 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4133 OBJECT_INFO_LOOKUP_REPLACE
))
4134 die("unable to read %s", oid_to_hex(&s
->oid
));
4141 void diff_free_filespec_blob(struct diff_filespec
*s
)
4145 else if (s
->should_munmap
)
4146 munmap(s
->data
, s
->size
);
4148 if (s
->should_free
|| s
->should_munmap
) {
4149 s
->should_free
= s
->should_munmap
= 0;
4154 void diff_free_filespec_data(struct diff_filespec
*s
)
4159 diff_free_filespec_blob(s
);
4160 FREE_AND_NULL(s
->cnt_data
);
4163 static void prep_temp_blob(struct index_state
*istate
,
4164 const char *path
, struct diff_tempfile
*temp
,
4167 const struct object_id
*oid
,
4170 struct strbuf buf
= STRBUF_INIT
;
4171 char *path_dup
= xstrdup(path
);
4172 const char *base
= basename(path_dup
);
4173 struct checkout_metadata meta
;
4175 init_checkout_metadata(&meta
, NULL
, NULL
, oid
);
4177 temp
->tempfile
= mks_tempfile_dt("git-blob-XXXXXX", base
);
4178 if (!temp
->tempfile
)
4179 die_errno("unable to create temp-file");
4180 if (convert_to_working_tree(istate
, path
,
4181 (const char *)blob
, (size_t)size
, &buf
, &meta
)) {
4185 if (write_in_full(temp
->tempfile
->fd
, blob
, size
) < 0 ||
4186 close_tempfile_gently(temp
->tempfile
))
4187 die_errno("unable to write temp-file");
4188 temp
->name
= get_tempfile_path(temp
->tempfile
);
4189 oid_to_hex_r(temp
->hex
, oid
);
4190 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
4191 strbuf_release(&buf
);
4195 static struct diff_tempfile
*prepare_temp_file(struct repository
*r
,
4197 struct diff_filespec
*one
)
4199 struct diff_tempfile
*temp
= claim_diff_tempfile();
4201 if (!DIFF_FILE_VALID(one
)) {
4203 /* A '-' entry produces this for file-2, and
4204 * a '+' entry produces this for file-1.
4206 temp
->name
= "/dev/null";
4207 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
4208 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
4212 if (!S_ISGITLINK(one
->mode
) &&
4214 reuse_worktree_file(r
->index
, name
, &one
->oid
, 1))) {
4216 if (lstat(name
, &st
) < 0) {
4217 if (errno
== ENOENT
)
4218 goto not_a_valid_file
;
4219 die_errno("stat(%s)", name
);
4221 if (S_ISLNK(st
.st_mode
)) {
4222 struct strbuf sb
= STRBUF_INIT
;
4223 if (strbuf_readlink(&sb
, name
, st
.st_size
) < 0)
4224 die_errno("readlink(%s)", name
);
4225 prep_temp_blob(r
->index
, name
, temp
, sb
.buf
, sb
.len
,
4227 &one
->oid
: null_oid()),
4229 one
->mode
: S_IFLNK
));
4230 strbuf_release(&sb
);
4233 /* we can borrow from the file in the work tree */
4235 if (!one
->oid_valid
)
4236 oid_to_hex_r(temp
->hex
, null_oid());
4238 oid_to_hex_r(temp
->hex
, &one
->oid
);
4239 /* Even though we may sometimes borrow the
4240 * contents from the work tree, we always want
4241 * one->mode. mode is trustworthy even when
4242 * !(one->oid_valid), as long as
4243 * DIFF_FILE_VALID(one).
4245 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
4250 if (diff_populate_filespec(r
, one
, NULL
))
4251 die("cannot read data blob for %s", one
->path
);
4252 prep_temp_blob(r
->index
, name
, temp
,
4253 one
->data
, one
->size
,
4254 &one
->oid
, one
->mode
);
4259 static void add_external_diff_name(struct repository
*r
,
4260 struct strvec
*argv
,
4262 struct diff_filespec
*df
)
4264 struct diff_tempfile
*temp
= prepare_temp_file(r
, name
, df
);
4265 strvec_push(argv
, temp
->name
);
4266 strvec_push(argv
, temp
->hex
);
4267 strvec_push(argv
, temp
->mode
);
4270 /* An external diff command takes:
4272 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4273 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4276 static void run_external_diff(const char *pgm
,
4279 struct diff_filespec
*one
,
4280 struct diff_filespec
*two
,
4281 const char *xfrm_msg
,
4282 struct diff_options
*o
)
4284 struct strvec argv
= STRVEC_INIT
;
4285 struct strvec env
= STRVEC_INIT
;
4286 struct diff_queue_struct
*q
= &diff_queued_diff
;
4288 strvec_push(&argv
, pgm
);
4289 strvec_push(&argv
, name
);
4292 add_external_diff_name(o
->repo
, &argv
, name
, one
);
4294 add_external_diff_name(o
->repo
, &argv
, name
, two
);
4296 add_external_diff_name(o
->repo
, &argv
, other
, two
);
4297 strvec_push(&argv
, other
);
4298 strvec_push(&argv
, xfrm_msg
);
4302 strvec_pushf(&env
, "GIT_DIFF_PATH_COUNTER=%d", ++o
->diff_path_counter
);
4303 strvec_pushf(&env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
4305 diff_free_filespec_data(one
);
4306 diff_free_filespec_data(two
);
4307 if (run_command_v_opt_cd_env(argv
.v
, RUN_USING_SHELL
, NULL
, env
.v
))
4308 die(_("external diff died, stopping at %s"), name
);
4311 strvec_clear(&argv
);
4315 static int similarity_index(struct diff_filepair
*p
)
4317 return p
->score
* 100 / MAX_SCORE
;
4320 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
4322 if (startup_info
->have_repository
)
4323 return find_unique_abbrev(oid
, abbrev
);
4325 char *hex
= oid_to_hex(oid
);
4327 abbrev
= FALLBACK_DEFAULT_ABBREV
;
4328 if (abbrev
> the_hash_algo
->hexsz
)
4329 BUG("oid abbreviation out of range: %d", abbrev
);
4336 static void fill_metainfo(struct strbuf
*msg
,
4339 struct diff_filespec
*one
,
4340 struct diff_filespec
*two
,
4341 struct diff_options
*o
,
4342 struct diff_filepair
*p
,
4343 int *must_show_header
,
4346 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
4347 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
4348 const char *line_prefix
= diff_line_prefix(o
);
4349 struct string_list
*more_headers
= NULL
;
4351 *must_show_header
= 1;
4352 strbuf_init(msg
, PATH_MAX
* 2 + 300);
4353 switch (p
->status
) {
4354 case DIFF_STATUS_COPIED
:
4355 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4356 line_prefix
, set
, similarity_index(p
));
4357 strbuf_addf(msg
, "%s\n%s%scopy from ",
4358 reset
, line_prefix
, set
);
4359 quote_c_style(name
, msg
, NULL
, 0);
4360 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
4361 quote_c_style(other
, msg
, NULL
, 0);
4362 strbuf_addf(msg
, "%s\n", reset
);
4364 case DIFF_STATUS_RENAMED
:
4365 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4366 line_prefix
, set
, similarity_index(p
));
4367 strbuf_addf(msg
, "%s\n%s%srename from ",
4368 reset
, line_prefix
, set
);
4369 quote_c_style(name
, msg
, NULL
, 0);
4370 strbuf_addf(msg
, "%s\n%s%srename to ",
4371 reset
, line_prefix
, set
);
4372 quote_c_style(other
, msg
, NULL
, 0);
4373 strbuf_addf(msg
, "%s\n", reset
);
4375 case DIFF_STATUS_MODIFIED
:
4377 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
4379 set
, similarity_index(p
), reset
);
4384 *must_show_header
= 0;
4386 if ((more_headers
= additional_headers(o
, name
))) {
4387 add_formatted_headers(msg
, more_headers
,
4388 line_prefix
, set
, reset
);
4389 *must_show_header
= 1;
4391 if (one
&& two
&& !oideq(&one
->oid
, &two
->oid
)) {
4392 const unsigned hexsz
= the_hash_algo
->hexsz
;
4393 int abbrev
= o
->abbrev
? o
->abbrev
: DEFAULT_ABBREV
;
4395 if (o
->flags
.full_index
)
4398 if (o
->flags
.binary
) {
4400 if ((!fill_mmfile(o
->repo
, &mf
, one
) &&
4401 diff_filespec_is_binary(o
->repo
, one
)) ||
4402 (!fill_mmfile(o
->repo
, &mf
, two
) &&
4403 diff_filespec_is_binary(o
->repo
, two
)))
4406 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
4407 diff_abbrev_oid(&one
->oid
, abbrev
),
4408 diff_abbrev_oid(&two
->oid
, abbrev
));
4409 if (one
->mode
== two
->mode
)
4410 strbuf_addf(msg
, " %06o", one
->mode
);
4411 strbuf_addf(msg
, "%s\n", reset
);
4415 static void run_diff_cmd(const char *pgm
,
4418 const char *attr_path
,
4419 struct diff_filespec
*one
,
4420 struct diff_filespec
*two
,
4422 struct diff_options
*o
,
4423 struct diff_filepair
*p
)
4425 const char *xfrm_msg
= NULL
;
4426 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
4427 int must_show_header
= 0;
4430 if (o
->flags
.allow_external
) {
4431 struct userdiff_driver
*drv
;
4433 drv
= userdiff_find_by_path(o
->repo
->index
, attr_path
);
4434 if (drv
&& drv
->external
)
4435 pgm
= drv
->external
;
4440 * don't use colors when the header is intended for an
4441 * external diff driver
4443 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
4445 want_color(o
->use_color
) && !pgm
);
4446 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
4450 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
, o
);
4454 builtin_diff(name
, other
? other
: name
,
4455 one
, two
, xfrm_msg
, must_show_header
,
4456 o
, complete_rewrite
);
4458 fprintf(o
->file
, "* Unmerged path %s\n", name
);
4461 static void diff_fill_oid_info(struct diff_filespec
*one
, struct index_state
*istate
)
4463 if (DIFF_FILE_VALID(one
)) {
4464 if (!one
->oid_valid
) {
4466 if (one
->is_stdin
) {
4470 if (lstat(one
->path
, &st
) < 0)
4471 die_errno("stat '%s'", one
->path
);
4472 if (index_path(istate
, &one
->oid
, one
->path
, &st
, 0))
4473 die("cannot hash %s", one
->path
);
4480 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
4482 /* Strip the prefix but do not molest /dev/null and absolute paths */
4483 if (*namep
&& !is_absolute_path(*namep
)) {
4484 *namep
+= prefix_length
;
4488 if (*otherp
&& !is_absolute_path(*otherp
)) {
4489 *otherp
+= prefix_length
;
4490 if (**otherp
== '/')
4495 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
4497 const char *pgm
= external_diff();
4499 struct diff_filespec
*one
= p
->one
;
4500 struct diff_filespec
*two
= p
->two
;
4503 const char *attr_path
;
4506 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
4508 if (o
->prefix_length
)
4509 strip_prefix(o
->prefix_length
, &name
, &other
);
4511 if (!o
->flags
.allow_external
)
4514 if (DIFF_PAIR_UNMERGED(p
)) {
4515 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
4516 NULL
, NULL
, NULL
, o
, p
);
4520 diff_fill_oid_info(one
, o
->repo
->index
);
4521 diff_fill_oid_info(two
, o
->repo
->index
);
4524 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4525 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4527 * a filepair that changes between file and symlink
4528 * needs to be split into deletion and creation.
4530 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4531 run_diff_cmd(NULL
, name
, other
, attr_path
,
4535 strbuf_release(&msg
);
4537 null
= alloc_filespec(one
->path
);
4538 run_diff_cmd(NULL
, name
, other
, attr_path
,
4539 null
, two
, &msg
, o
, p
);
4543 run_diff_cmd(pgm
, name
, other
, attr_path
,
4544 one
, two
, &msg
, o
, p
);
4546 strbuf_release(&msg
);
4549 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4550 struct diffstat_t
*diffstat
)
4555 if (DIFF_PAIR_UNMERGED(p
)) {
4557 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
,
4562 name
= p
->one
->path
;
4563 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4565 if (o
->prefix_length
)
4566 strip_prefix(o
->prefix_length
, &name
, &other
);
4568 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4569 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4571 builtin_diffstat(name
, other
, p
->one
, p
->two
,
4575 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4579 const char *attr_path
;
4581 if (DIFF_PAIR_UNMERGED(p
)) {
4586 name
= p
->one
->path
;
4587 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4588 attr_path
= other
? other
: name
;
4590 if (o
->prefix_length
)
4591 strip_prefix(o
->prefix_length
, &name
, &other
);
4593 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4594 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4596 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4599 static void prep_parse_options(struct diff_options
*options
);
4601 void repo_diff_setup(struct repository
*r
, struct diff_options
*options
)
4603 memcpy(options
, &default_diff_options
, sizeof(*options
));
4605 options
->file
= stdout
;
4608 options
->output_indicators
[OUTPUT_INDICATOR_NEW
] = '+';
4609 options
->output_indicators
[OUTPUT_INDICATOR_OLD
] = '-';
4610 options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
] = ' ';
4611 options
->abbrev
= DEFAULT_ABBREV
;
4612 options
->line_termination
= '\n';
4613 options
->break_opt
= -1;
4614 options
->rename_limit
= -1;
4615 options
->dirstat_permille
= diff_dirstat_permille_default
;
4616 options
->context
= diff_context_default
;
4617 options
->interhunkcontext
= diff_interhunk_context_default
;
4618 options
->ws_error_highlight
= ws_error_highlight_default
;
4619 options
->flags
.rename_empty
= 1;
4620 options
->flags
.relative_name
= diff_relative
;
4621 options
->objfind
= NULL
;
4623 /* pathchange left =NULL by default */
4624 options
->change
= diff_change
;
4625 options
->add_remove
= diff_addremove
;
4626 options
->use_color
= diff_use_color_default
;
4627 options
->detect_rename
= diff_detect_rename_default
;
4628 options
->xdl_opts
|= diff_algorithm
;
4629 if (diff_indent_heuristic
)
4630 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4632 options
->orderfile
= diff_order_file_cfg
;
4634 if (!options
->flags
.ignore_submodule_set
)
4635 options
->flags
.ignore_untracked_in_submodules
= 1;
4637 if (diff_no_prefix
) {
4638 options
->a_prefix
= options
->b_prefix
= "";
4639 } else if (!diff_mnemonic_prefix
) {
4640 options
->a_prefix
= "a/";
4641 options
->b_prefix
= "b/";
4644 options
->color_moved
= diff_color_moved_default
;
4645 options
->color_moved_ws_handling
= diff_color_moved_ws_default
;
4647 prep_parse_options(options
);
4650 static const char diff_status_letters
[] = {
4653 DIFF_STATUS_DELETED
,
4654 DIFF_STATUS_MODIFIED
,
4655 DIFF_STATUS_RENAMED
,
4656 DIFF_STATUS_TYPE_CHANGED
,
4657 DIFF_STATUS_UNKNOWN
,
4658 DIFF_STATUS_UNMERGED
,
4659 DIFF_STATUS_FILTER_AON
,
4660 DIFF_STATUS_FILTER_BROKEN
,
4664 static unsigned int filter_bit
['Z' + 1];
4666 static void prepare_filter_bits(void)
4670 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4671 for (i
= 0; diff_status_letters
[i
]; i
++)
4672 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4676 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4678 return opt
->filter
& filter_bit
[(int) status
];
4681 unsigned diff_filter_bit(char status
)
4683 prepare_filter_bits();
4684 return filter_bit
[(int) status
];
4687 void diff_setup_done(struct diff_options
*options
)
4689 unsigned check_mask
= DIFF_FORMAT_NAME
|
4690 DIFF_FORMAT_NAME_STATUS
|
4691 DIFF_FORMAT_CHECKDIFF
|
4692 DIFF_FORMAT_NO_OUTPUT
;
4694 * This must be signed because we're comparing against a potentially
4697 const int hexsz
= the_hash_algo
->hexsz
;
4699 if (options
->set_default
)
4700 options
->set_default(options
);
4702 if (HAS_MULTI_BITS(options
->output_format
& check_mask
))
4703 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
4704 "--name-only", "--name-status", "--check", "-s");
4706 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
4707 die(_("options '%s', '%s', and '%s' cannot be used together"),
4708 "-G", "-S", "--find-object");
4710 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_G_REGEX_MASK
))
4711 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"),
4712 "-G", "--pickaxe-regex", "--pickaxe-regex", "-S");
4714 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK
))
4715 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"),
4716 "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S");
4719 * Most of the time we can say "there are changes"
4720 * only by checking if there are changed paths, but
4721 * --ignore-whitespace* options force us to look
4725 if ((options
->xdl_opts
& XDF_WHITESPACE_FLAGS
) ||
4726 options
->ignore_regex_nr
)
4727 options
->flags
.diff_from_contents
= 1;
4729 options
->flags
.diff_from_contents
= 0;
4731 if (options
->flags
.find_copies_harder
)
4732 options
->detect_rename
= DIFF_DETECT_COPY
;
4734 if (!options
->flags
.relative_name
)
4735 options
->prefix
= NULL
;
4736 if (options
->prefix
)
4737 options
->prefix_length
= strlen(options
->prefix
);
4739 options
->prefix_length
= 0;
4741 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4742 DIFF_FORMAT_NAME_STATUS
|
4743 DIFF_FORMAT_CHECKDIFF
|
4744 DIFF_FORMAT_NO_OUTPUT
))
4745 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4746 DIFF_FORMAT_NUMSTAT
|
4747 DIFF_FORMAT_DIFFSTAT
|
4748 DIFF_FORMAT_SHORTSTAT
|
4749 DIFF_FORMAT_DIRSTAT
|
4750 DIFF_FORMAT_SUMMARY
|
4754 * These cases always need recursive; we do not drop caller-supplied
4755 * recursive bits for other formats here.
4757 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4758 DIFF_FORMAT_NUMSTAT
|
4759 DIFF_FORMAT_DIFFSTAT
|
4760 DIFF_FORMAT_SHORTSTAT
|
4761 DIFF_FORMAT_DIRSTAT
|
4762 DIFF_FORMAT_SUMMARY
|
4763 DIFF_FORMAT_CHECKDIFF
))
4764 options
->flags
.recursive
= 1;
4766 * Also pickaxe would not work very well if you do not say recursive
4768 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
4769 options
->flags
.recursive
= 1;
4771 * When patches are generated, submodules diffed against the work tree
4772 * must be checked for dirtiness too so it can be shown in the output
4774 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4775 options
->flags
.dirty_submodules
= 1;
4777 if (options
->detect_rename
&& options
->rename_limit
< 0)
4778 options
->rename_limit
= diff_rename_limit_default
;
4779 if (hexsz
< options
->abbrev
)
4780 options
->abbrev
= hexsz
; /* full */
4783 * It does not make sense to show the first hit we happened
4784 * to have found. It does not make sense not to return with
4785 * exit code in such a case either.
4787 if (options
->flags
.quick
) {
4788 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4789 options
->flags
.exit_with_status
= 1;
4792 options
->diff_path_counter
= 0;
4794 if (options
->flags
.follow_renames
&& options
->pathspec
.nr
!= 1)
4795 die(_("--follow requires exactly one pathspec"));
4797 if (!options
->use_color
|| external_diff())
4798 options
->color_moved
= 0;
4800 if (options
->filter_not
) {
4801 if (!options
->filter
)
4802 options
->filter
= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4803 options
->filter
&= ~options
->filter_not
;
4806 FREE_AND_NULL(options
->parseopts
);
4809 int parse_long_opt(const char *opt
, const char **argv
,
4810 const char **optarg
)
4812 const char *arg
= argv
[0];
4813 if (!skip_prefix(arg
, "--", &arg
))
4815 if (!skip_prefix(arg
, opt
, &arg
))
4817 if (*arg
== '=') { /* stuck form: --option=value */
4823 /* separate form: --option value */
4825 die("Option '--%s' requires a value", opt
);
4830 static int diff_opt_stat(const struct option
*opt
, const char *value
, int unset
)
4832 struct diff_options
*options
= opt
->value
;
4833 int width
= options
->stat_width
;
4834 int name_width
= options
->stat_name_width
;
4835 int graph_width
= options
->stat_graph_width
;
4836 int count
= options
->stat_count
;
4839 BUG_ON_OPT_NEG(unset
);
4841 if (!strcmp(opt
->long_name
, "stat")) {
4843 width
= strtoul(value
, &end
, 10);
4845 name_width
= strtoul(end
+1, &end
, 10);
4847 count
= strtoul(end
+1, &end
, 10);
4849 return error(_("invalid --stat value: %s"), value
);
4851 } else if (!strcmp(opt
->long_name
, "stat-width")) {
4852 width
= strtoul(value
, &end
, 10);
4854 return error(_("%s expects a numerical value"),
4856 } else if (!strcmp(opt
->long_name
, "stat-name-width")) {
4857 name_width
= strtoul(value
, &end
, 10);
4859 return error(_("%s expects a numerical value"),
4861 } else if (!strcmp(opt
->long_name
, "stat-graph-width")) {
4862 graph_width
= strtoul(value
, &end
, 10);
4864 return error(_("%s expects a numerical value"),
4866 } else if (!strcmp(opt
->long_name
, "stat-count")) {
4867 count
= strtoul(value
, &end
, 10);
4869 return error(_("%s expects a numerical value"),
4872 BUG("%s should not get here", opt
->long_name
);
4874 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
4875 options
->stat_name_width
= name_width
;
4876 options
->stat_graph_width
= graph_width
;
4877 options
->stat_width
= width
;
4878 options
->stat_count
= count
;
4882 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
4884 struct strbuf errmsg
= STRBUF_INIT
;
4885 if (parse_dirstat_params(options
, params
, &errmsg
))
4886 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
4888 strbuf_release(&errmsg
);
4890 * The caller knows a dirstat-related option is given from the command
4891 * line; allow it to say "return this_function();"
4893 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
4897 static int diff_opt_diff_filter(const struct option
*option
,
4898 const char *optarg
, int unset
)
4900 struct diff_options
*opt
= option
->value
;
4903 BUG_ON_OPT_NEG(unset
);
4904 prepare_filter_bits();
4906 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
4910 if ('a' <= optch
&& optch
<= 'z') {
4912 optch
= toupper(optch
);
4917 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
4919 return error(_("unknown change class '%c' in --diff-filter=%s"),
4922 opt
->filter_not
|= bit
;
4929 static void enable_patch_output(int *fmt
)
4931 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
4932 *fmt
|= DIFF_FORMAT_PATCH
;
4935 static int diff_opt_ws_error_highlight(const struct option
*option
,
4936 const char *arg
, int unset
)
4938 struct diff_options
*opt
= option
->value
;
4939 int val
= parse_ws_error_highlight(arg
);
4941 BUG_ON_OPT_NEG(unset
);
4943 return error(_("unknown value after ws-error-highlight=%.*s"),
4945 opt
->ws_error_highlight
= val
;
4949 static int diff_opt_find_object(const struct option
*option
,
4950 const char *arg
, int unset
)
4952 struct diff_options
*opt
= option
->value
;
4953 struct object_id oid
;
4955 BUG_ON_OPT_NEG(unset
);
4956 if (get_oid(arg
, &oid
))
4957 return error(_("unable to resolve '%s'"), arg
);
4960 CALLOC_ARRAY(opt
->objfind
, 1);
4962 opt
->pickaxe_opts
|= DIFF_PICKAXE_KIND_OBJFIND
;
4963 opt
->flags
.recursive
= 1;
4964 opt
->flags
.tree_in_recursive
= 1;
4965 oidset_insert(opt
->objfind
, &oid
);
4969 static int diff_opt_anchored(const struct option
*opt
,
4970 const char *arg
, int unset
)
4972 struct diff_options
*options
= opt
->value
;
4974 BUG_ON_OPT_NEG(unset
);
4975 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
4976 ALLOC_GROW(options
->anchors
, options
->anchors_nr
+ 1,
4977 options
->anchors_alloc
);
4978 options
->anchors
[options
->anchors_nr
++] = xstrdup(arg
);
4982 static int diff_opt_binary(const struct option
*opt
,
4983 const char *arg
, int unset
)
4985 struct diff_options
*options
= opt
->value
;
4987 BUG_ON_OPT_NEG(unset
);
4988 BUG_ON_OPT_ARG(arg
);
4989 enable_patch_output(&options
->output_format
);
4990 options
->flags
.binary
= 1;
4994 static int diff_opt_break_rewrites(const struct option
*opt
,
4995 const char *arg
, int unset
)
4997 int *break_opt
= opt
->value
;
5000 BUG_ON_OPT_NEG(unset
);
5003 opt1
= parse_rename_score(&arg
);
5006 else if (*arg
!= '/')
5007 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5010 opt2
= parse_rename_score(&arg
);
5013 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5014 *break_opt
= opt1
| (opt2
<< 16);
5018 static int diff_opt_char(const struct option
*opt
,
5019 const char *arg
, int unset
)
5021 char *value
= opt
->value
;
5023 BUG_ON_OPT_NEG(unset
);
5025 return error(_("%s expects a character, got '%s'"),
5026 opt
->long_name
, arg
);
5031 static int diff_opt_color_moved(const struct option
*opt
,
5032 const char *arg
, int unset
)
5034 struct diff_options
*options
= opt
->value
;
5037 options
->color_moved
= COLOR_MOVED_NO
;
5039 if (diff_color_moved_default
)
5040 options
->color_moved
= diff_color_moved_default
;
5041 if (options
->color_moved
== COLOR_MOVED_NO
)
5042 options
->color_moved
= COLOR_MOVED_DEFAULT
;
5044 int cm
= parse_color_moved(arg
);
5046 return error(_("bad --color-moved argument: %s"), arg
);
5047 options
->color_moved
= cm
;
5052 static int diff_opt_color_moved_ws(const struct option
*opt
,
5053 const char *arg
, int unset
)
5055 struct diff_options
*options
= opt
->value
;
5059 options
->color_moved_ws_handling
= 0;
5063 cm
= parse_color_moved_ws(arg
);
5064 if (cm
& COLOR_MOVED_WS_ERROR
)
5065 return error(_("invalid mode '%s' in --color-moved-ws"), arg
);
5066 options
->color_moved_ws_handling
= cm
;
5070 static int diff_opt_color_words(const struct option
*opt
,
5071 const char *arg
, int unset
)
5073 struct diff_options
*options
= opt
->value
;
5075 BUG_ON_OPT_NEG(unset
);
5076 options
->use_color
= 1;
5077 options
->word_diff
= DIFF_WORDS_COLOR
;
5078 options
->word_regex
= arg
;
5082 static int diff_opt_compact_summary(const struct option
*opt
,
5083 const char *arg
, int unset
)
5085 struct diff_options
*options
= opt
->value
;
5087 BUG_ON_OPT_ARG(arg
);
5089 options
->flags
.stat_with_summary
= 0;
5091 options
->flags
.stat_with_summary
= 1;
5092 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
5097 static int diff_opt_diff_algorithm(const struct option
*opt
,
5098 const char *arg
, int unset
)
5100 struct diff_options
*options
= opt
->value
;
5101 long value
= parse_algorithm_value(arg
);
5103 BUG_ON_OPT_NEG(unset
);
5105 return error(_("option diff-algorithm accepts \"myers\", "
5106 "\"minimal\", \"patience\" and \"histogram\""));
5108 /* clear out previous settings */
5109 DIFF_XDL_CLR(options
, NEED_MINIMAL
);
5110 options
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
5111 options
->xdl_opts
|= value
;
5115 static int diff_opt_dirstat(const struct option
*opt
,
5116 const char *arg
, int unset
)
5118 struct diff_options
*options
= opt
->value
;
5120 BUG_ON_OPT_NEG(unset
);
5121 if (!strcmp(opt
->long_name
, "cumulative")) {
5123 BUG("how come --cumulative take a value?");
5125 } else if (!strcmp(opt
->long_name
, "dirstat-by-file"))
5126 parse_dirstat_opt(options
, "files");
5127 parse_dirstat_opt(options
, arg
? arg
: "");
5131 static int diff_opt_find_copies(const struct option
*opt
,
5132 const char *arg
, int unset
)
5134 struct diff_options
*options
= opt
->value
;
5136 BUG_ON_OPT_NEG(unset
);
5139 options
->rename_score
= parse_rename_score(&arg
);
5141 return error(_("invalid argument to %s"), opt
->long_name
);
5143 if (options
->detect_rename
== DIFF_DETECT_COPY
)
5144 options
->flags
.find_copies_harder
= 1;
5146 options
->detect_rename
= DIFF_DETECT_COPY
;
5151 static int diff_opt_find_renames(const struct option
*opt
,
5152 const char *arg
, int unset
)
5154 struct diff_options
*options
= opt
->value
;
5156 BUG_ON_OPT_NEG(unset
);
5159 options
->rename_score
= parse_rename_score(&arg
);
5161 return error(_("invalid argument to %s"), opt
->long_name
);
5163 options
->detect_rename
= DIFF_DETECT_RENAME
;
5167 static int diff_opt_follow(const struct option
*opt
,
5168 const char *arg
, int unset
)
5170 struct diff_options
*options
= opt
->value
;
5172 BUG_ON_OPT_ARG(arg
);
5174 options
->flags
.follow_renames
= 0;
5175 options
->flags
.default_follow_renames
= 0;
5177 options
->flags
.follow_renames
= 1;
5182 static int diff_opt_ignore_submodules(const struct option
*opt
,
5183 const char *arg
, int unset
)
5185 struct diff_options
*options
= opt
->value
;
5187 BUG_ON_OPT_NEG(unset
);
5190 options
->flags
.override_submodule_config
= 1;
5191 handle_ignore_submodules_arg(options
, arg
);
5195 static int diff_opt_line_prefix(const struct option
*opt
,
5196 const char *optarg
, int unset
)
5198 struct diff_options
*options
= opt
->value
;
5200 BUG_ON_OPT_NEG(unset
);
5201 options
->line_prefix
= optarg
;
5202 options
->line_prefix_length
= strlen(options
->line_prefix
);
5203 graph_setup_line_prefix(options
);
5207 static int diff_opt_no_prefix(const struct option
*opt
,
5208 const char *optarg
, int unset
)
5210 struct diff_options
*options
= opt
->value
;
5212 BUG_ON_OPT_NEG(unset
);
5213 BUG_ON_OPT_ARG(optarg
);
5214 options
->a_prefix
= "";
5215 options
->b_prefix
= "";
5219 static enum parse_opt_result
diff_opt_output(struct parse_opt_ctx_t
*ctx
,
5220 const struct option
*opt
,
5221 const char *arg
, int unset
)
5223 struct diff_options
*options
= opt
->value
;
5226 BUG_ON_OPT_NEG(unset
);
5227 path
= prefix_filename(ctx
->prefix
, arg
);
5228 options
->file
= xfopen(path
, "w");
5229 options
->close_file
= 1;
5230 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
5231 options
->use_color
= GIT_COLOR_NEVER
;
5236 static int diff_opt_patience(const struct option
*opt
,
5237 const char *arg
, int unset
)
5239 struct diff_options
*options
= opt
->value
;
5242 BUG_ON_OPT_NEG(unset
);
5243 BUG_ON_OPT_ARG(arg
);
5244 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
5246 * Both --patience and --anchored use PATIENCE_DIFF
5247 * internally, so remove any anchors previously
5250 for (i
= 0; i
< options
->anchors_nr
; i
++)
5251 free(options
->anchors
[i
]);
5252 options
->anchors_nr
= 0;
5256 static int diff_opt_ignore_regex(const struct option
*opt
,
5257 const char *arg
, int unset
)
5259 struct diff_options
*options
= opt
->value
;
5262 BUG_ON_OPT_NEG(unset
);
5263 regex
= xmalloc(sizeof(*regex
));
5264 if (regcomp(regex
, arg
, REG_EXTENDED
| REG_NEWLINE
))
5265 return error(_("invalid regex given to -I: '%s'"), arg
);
5266 ALLOC_GROW(options
->ignore_regex
, options
->ignore_regex_nr
+ 1,
5267 options
->ignore_regex_alloc
);
5268 options
->ignore_regex
[options
->ignore_regex_nr
++] = regex
;
5272 static int diff_opt_pickaxe_regex(const struct option
*opt
,
5273 const char *arg
, int unset
)
5275 struct diff_options
*options
= opt
->value
;
5277 BUG_ON_OPT_NEG(unset
);
5278 options
->pickaxe
= arg
;
5279 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
5283 static int diff_opt_pickaxe_string(const struct option
*opt
,
5284 const char *arg
, int unset
)
5286 struct diff_options
*options
= opt
->value
;
5288 BUG_ON_OPT_NEG(unset
);
5289 options
->pickaxe
= arg
;
5290 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
5294 static int diff_opt_relative(const struct option
*opt
,
5295 const char *arg
, int unset
)
5297 struct diff_options
*options
= opt
->value
;
5299 options
->flags
.relative_name
= !unset
;
5301 options
->prefix
= arg
;
5305 static int diff_opt_submodule(const struct option
*opt
,
5306 const char *arg
, int unset
)
5308 struct diff_options
*options
= opt
->value
;
5310 BUG_ON_OPT_NEG(unset
);
5313 if (parse_submodule_params(options
, arg
))
5314 return error(_("failed to parse --submodule option parameter: '%s'"),
5319 static int diff_opt_textconv(const struct option
*opt
,
5320 const char *arg
, int unset
)
5322 struct diff_options
*options
= opt
->value
;
5324 BUG_ON_OPT_ARG(arg
);
5326 options
->flags
.allow_textconv
= 0;
5328 options
->flags
.allow_textconv
= 1;
5329 options
->flags
.textconv_set_via_cmdline
= 1;
5334 static int diff_opt_unified(const struct option
*opt
,
5335 const char *arg
, int unset
)
5337 struct diff_options
*options
= opt
->value
;
5340 BUG_ON_OPT_NEG(unset
);
5343 options
->context
= strtol(arg
, &s
, 10);
5345 return error(_("%s expects a numerical value"), "--unified");
5347 enable_patch_output(&options
->output_format
);
5352 static int diff_opt_word_diff(const struct option
*opt
,
5353 const char *arg
, int unset
)
5355 struct diff_options
*options
= opt
->value
;
5357 BUG_ON_OPT_NEG(unset
);
5359 if (!strcmp(arg
, "plain"))
5360 options
->word_diff
= DIFF_WORDS_PLAIN
;
5361 else if (!strcmp(arg
, "color")) {
5362 options
->use_color
= 1;
5363 options
->word_diff
= DIFF_WORDS_COLOR
;
5365 else if (!strcmp(arg
, "porcelain"))
5366 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
5367 else if (!strcmp(arg
, "none"))
5368 options
->word_diff
= DIFF_WORDS_NONE
;
5370 return error(_("bad --word-diff argument: %s"), arg
);
5372 if (options
->word_diff
== DIFF_WORDS_NONE
)
5373 options
->word_diff
= DIFF_WORDS_PLAIN
;
5378 static int diff_opt_word_diff_regex(const struct option
*opt
,
5379 const char *arg
, int unset
)
5381 struct diff_options
*options
= opt
->value
;
5383 BUG_ON_OPT_NEG(unset
);
5384 if (options
->word_diff
== DIFF_WORDS_NONE
)
5385 options
->word_diff
= DIFF_WORDS_PLAIN
;
5386 options
->word_regex
= arg
;
5390 static int diff_opt_rotate_to(const struct option
*opt
, const char *arg
, int unset
)
5392 struct diff_options
*options
= opt
->value
;
5394 BUG_ON_OPT_NEG(unset
);
5395 if (!strcmp(opt
->long_name
, "skip-to"))
5396 options
->skip_instead_of_rotate
= 1;
5398 options
->skip_instead_of_rotate
= 0;
5399 options
->rotate_to
= arg
;
5403 static void prep_parse_options(struct diff_options
*options
)
5405 struct option parseopts
[] = {
5406 OPT_GROUP(N_("Diff output format options")),
5407 OPT_BITOP('p', "patch", &options
->output_format
,
5408 N_("generate patch"),
5409 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5410 OPT_BIT_F('s', "no-patch", &options
->output_format
,
5411 N_("suppress diff output"),
5412 DIFF_FORMAT_NO_OUTPUT
, PARSE_OPT_NONEG
),
5413 OPT_BITOP('u', NULL
, &options
->output_format
,
5414 N_("generate patch"),
5415 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5416 OPT_CALLBACK_F('U', "unified", options
, N_("<n>"),
5417 N_("generate diffs with <n> lines context"),
5418 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_unified
),
5419 OPT_BOOL('W', "function-context", &options
->flags
.funccontext
,
5420 N_("generate diffs with <n> lines context")),
5421 OPT_BIT_F(0, "raw", &options
->output_format
,
5422 N_("generate the diff in raw format"),
5423 DIFF_FORMAT_RAW
, PARSE_OPT_NONEG
),
5424 OPT_BITOP(0, "patch-with-raw", &options
->output_format
,
5425 N_("synonym for '-p --raw'"),
5426 DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
,
5427 DIFF_FORMAT_NO_OUTPUT
),
5428 OPT_BITOP(0, "patch-with-stat", &options
->output_format
,
5429 N_("synonym for '-p --stat'"),
5430 DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
,
5431 DIFF_FORMAT_NO_OUTPUT
),
5432 OPT_BIT_F(0, "numstat", &options
->output_format
,
5433 N_("machine friendly --stat"),
5434 DIFF_FORMAT_NUMSTAT
, PARSE_OPT_NONEG
),
5435 OPT_BIT_F(0, "shortstat", &options
->output_format
,
5436 N_("output only the last line of --stat"),
5437 DIFF_FORMAT_SHORTSTAT
, PARSE_OPT_NONEG
),
5438 OPT_CALLBACK_F('X', "dirstat", options
, N_("<param1,param2>..."),
5439 N_("output the distribution of relative amount of changes for each sub-directory"),
5440 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5442 OPT_CALLBACK_F(0, "cumulative", options
, NULL
,
5443 N_("synonym for --dirstat=cumulative"),
5444 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5446 OPT_CALLBACK_F(0, "dirstat-by-file", options
, N_("<param1,param2>..."),
5447 N_("synonym for --dirstat=files,param1,param2..."),
5448 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5450 OPT_BIT_F(0, "check", &options
->output_format
,
5451 N_("warn if changes introduce conflict markers or whitespace errors"),
5452 DIFF_FORMAT_CHECKDIFF
, PARSE_OPT_NONEG
),
5453 OPT_BIT_F(0, "summary", &options
->output_format
,
5454 N_("condensed summary such as creations, renames and mode changes"),
5455 DIFF_FORMAT_SUMMARY
, PARSE_OPT_NONEG
),
5456 OPT_BIT_F(0, "name-only", &options
->output_format
,
5457 N_("show only names of changed files"),
5458 DIFF_FORMAT_NAME
, PARSE_OPT_NONEG
),
5459 OPT_BIT_F(0, "name-status", &options
->output_format
,
5460 N_("show only names and status of changed files"),
5461 DIFF_FORMAT_NAME_STATUS
, PARSE_OPT_NONEG
),
5462 OPT_CALLBACK_F(0, "stat", options
, N_("<width>[,<name-width>[,<count>]]"),
5463 N_("generate diffstat"),
5464 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_stat
),
5465 OPT_CALLBACK_F(0, "stat-width", options
, N_("<width>"),
5466 N_("generate diffstat with a given width"),
5467 PARSE_OPT_NONEG
, diff_opt_stat
),
5468 OPT_CALLBACK_F(0, "stat-name-width", options
, N_("<width>"),
5469 N_("generate diffstat with a given name width"),
5470 PARSE_OPT_NONEG
, diff_opt_stat
),
5471 OPT_CALLBACK_F(0, "stat-graph-width", options
, N_("<width>"),
5472 N_("generate diffstat with a given graph width"),
5473 PARSE_OPT_NONEG
, diff_opt_stat
),
5474 OPT_CALLBACK_F(0, "stat-count", options
, N_("<count>"),
5475 N_("generate diffstat with limited lines"),
5476 PARSE_OPT_NONEG
, diff_opt_stat
),
5477 OPT_CALLBACK_F(0, "compact-summary", options
, NULL
,
5478 N_("generate compact summary in diffstat"),
5479 PARSE_OPT_NOARG
, diff_opt_compact_summary
),
5480 OPT_CALLBACK_F(0, "binary", options
, NULL
,
5481 N_("output a binary diff that can be applied"),
5482 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_binary
),
5483 OPT_BOOL(0, "full-index", &options
->flags
.full_index
,
5484 N_("show full pre- and post-image object names on the \"index\" lines")),
5485 OPT_COLOR_FLAG(0, "color", &options
->use_color
,
5486 N_("show colored diff")),
5487 OPT_CALLBACK_F(0, "ws-error-highlight", options
, N_("<kind>"),
5488 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5489 PARSE_OPT_NONEG
, diff_opt_ws_error_highlight
),
5490 OPT_SET_INT('z', NULL
, &options
->line_termination
,
5491 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5493 OPT__ABBREV(&options
->abbrev
),
5494 OPT_STRING_F(0, "src-prefix", &options
->a_prefix
, N_("<prefix>"),
5495 N_("show the given source prefix instead of \"a/\""),
5497 OPT_STRING_F(0, "dst-prefix", &options
->b_prefix
, N_("<prefix>"),
5498 N_("show the given destination prefix instead of \"b/\""),
5500 OPT_CALLBACK_F(0, "line-prefix", options
, N_("<prefix>"),
5501 N_("prepend an additional prefix to every line of output"),
5502 PARSE_OPT_NONEG
, diff_opt_line_prefix
),
5503 OPT_CALLBACK_F(0, "no-prefix", options
, NULL
,
5504 N_("do not show any source or destination prefix"),
5505 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_no_prefix
),
5506 OPT_INTEGER_F(0, "inter-hunk-context", &options
->interhunkcontext
,
5507 N_("show context between diff hunks up to the specified number of lines"),
5509 OPT_CALLBACK_F(0, "output-indicator-new",
5510 &options
->output_indicators
[OUTPUT_INDICATOR_NEW
],
5512 N_("specify the character to indicate a new line instead of '+'"),
5513 PARSE_OPT_NONEG
, diff_opt_char
),
5514 OPT_CALLBACK_F(0, "output-indicator-old",
5515 &options
->output_indicators
[OUTPUT_INDICATOR_OLD
],
5517 N_("specify the character to indicate an old line instead of '-'"),
5518 PARSE_OPT_NONEG
, diff_opt_char
),
5519 OPT_CALLBACK_F(0, "output-indicator-context",
5520 &options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
],
5522 N_("specify the character to indicate a context instead of ' '"),
5523 PARSE_OPT_NONEG
, diff_opt_char
),
5525 OPT_GROUP(N_("Diff rename options")),
5526 OPT_CALLBACK_F('B', "break-rewrites", &options
->break_opt
, N_("<n>[/<m>]"),
5527 N_("break complete rewrite changes into pairs of delete and create"),
5528 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5529 diff_opt_break_rewrites
),
5530 OPT_CALLBACK_F('M', "find-renames", options
, N_("<n>"),
5531 N_("detect renames"),
5532 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5533 diff_opt_find_renames
),
5534 OPT_SET_INT_F('D', "irreversible-delete", &options
->irreversible_delete
,
5535 N_("omit the preimage for deletes"),
5536 1, PARSE_OPT_NONEG
),
5537 OPT_CALLBACK_F('C', "find-copies", options
, N_("<n>"),
5538 N_("detect copies"),
5539 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5540 diff_opt_find_copies
),
5541 OPT_BOOL(0, "find-copies-harder", &options
->flags
.find_copies_harder
,
5542 N_("use unmodified files as source to find copies")),
5543 OPT_SET_INT_F(0, "no-renames", &options
->detect_rename
,
5544 N_("disable rename detection"),
5545 0, PARSE_OPT_NONEG
),
5546 OPT_BOOL(0, "rename-empty", &options
->flags
.rename_empty
,
5547 N_("use empty blobs as rename source")),
5548 OPT_CALLBACK_F(0, "follow", options
, NULL
,
5549 N_("continue listing the history of a file beyond renames"),
5550 PARSE_OPT_NOARG
, diff_opt_follow
),
5551 OPT_INTEGER('l', NULL
, &options
->rename_limit
,
5552 N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
5554 OPT_GROUP(N_("Diff algorithm options")),
5555 OPT_BIT(0, "minimal", &options
->xdl_opts
,
5556 N_("produce the smallest possible diff"),
5558 OPT_BIT_F('w', "ignore-all-space", &options
->xdl_opts
,
5559 N_("ignore whitespace when comparing lines"),
5560 XDF_IGNORE_WHITESPACE
, PARSE_OPT_NONEG
),
5561 OPT_BIT_F('b', "ignore-space-change", &options
->xdl_opts
,
5562 N_("ignore changes in amount of whitespace"),
5563 XDF_IGNORE_WHITESPACE_CHANGE
, PARSE_OPT_NONEG
),
5564 OPT_BIT_F(0, "ignore-space-at-eol", &options
->xdl_opts
,
5565 N_("ignore changes in whitespace at EOL"),
5566 XDF_IGNORE_WHITESPACE_AT_EOL
, PARSE_OPT_NONEG
),
5567 OPT_BIT_F(0, "ignore-cr-at-eol", &options
->xdl_opts
,
5568 N_("ignore carrier-return at the end of line"),
5569 XDF_IGNORE_CR_AT_EOL
, PARSE_OPT_NONEG
),
5570 OPT_BIT_F(0, "ignore-blank-lines", &options
->xdl_opts
,
5571 N_("ignore changes whose lines are all blank"),
5572 XDF_IGNORE_BLANK_LINES
, PARSE_OPT_NONEG
),
5573 OPT_CALLBACK_F('I', "ignore-matching-lines", options
, N_("<regex>"),
5574 N_("ignore changes whose all lines match <regex>"),
5575 0, diff_opt_ignore_regex
),
5576 OPT_BIT(0, "indent-heuristic", &options
->xdl_opts
,
5577 N_("heuristic to shift diff hunk boundaries for easy reading"),
5578 XDF_INDENT_HEURISTIC
),
5579 OPT_CALLBACK_F(0, "patience", options
, NULL
,
5580 N_("generate diff using the \"patience diff\" algorithm"),
5581 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5583 OPT_BITOP(0, "histogram", &options
->xdl_opts
,
5584 N_("generate diff using the \"histogram diff\" algorithm"),
5585 XDF_HISTOGRAM_DIFF
, XDF_DIFF_ALGORITHM_MASK
),
5586 OPT_CALLBACK_F(0, "diff-algorithm", options
, N_("<algorithm>"),
5587 N_("choose a diff algorithm"),
5588 PARSE_OPT_NONEG
, diff_opt_diff_algorithm
),
5589 OPT_CALLBACK_F(0, "anchored", options
, N_("<text>"),
5590 N_("generate diff using the \"anchored diff\" algorithm"),
5591 PARSE_OPT_NONEG
, diff_opt_anchored
),
5592 OPT_CALLBACK_F(0, "word-diff", options
, N_("<mode>"),
5593 N_("show word diff, using <mode> to delimit changed words"),
5594 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_word_diff
),
5595 OPT_CALLBACK_F(0, "word-diff-regex", options
, N_("<regex>"),
5596 N_("use <regex> to decide what a word is"),
5597 PARSE_OPT_NONEG
, diff_opt_word_diff_regex
),
5598 OPT_CALLBACK_F(0, "color-words", options
, N_("<regex>"),
5599 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5600 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_color_words
),
5601 OPT_CALLBACK_F(0, "color-moved", options
, N_("<mode>"),
5602 N_("moved lines of code are colored differently"),
5603 PARSE_OPT_OPTARG
, diff_opt_color_moved
),
5604 OPT_CALLBACK_F(0, "color-moved-ws", options
, N_("<mode>"),
5605 N_("how white spaces are ignored in --color-moved"),
5606 0, diff_opt_color_moved_ws
),
5608 OPT_GROUP(N_("Other diff options")),
5609 OPT_CALLBACK_F(0, "relative", options
, N_("<prefix>"),
5610 N_("when run from subdir, exclude changes outside and show relative paths"),
5613 OPT_BOOL('a', "text", &options
->flags
.text
,
5614 N_("treat all files as text")),
5615 OPT_BOOL('R', NULL
, &options
->flags
.reverse_diff
,
5616 N_("swap two inputs, reverse the diff")),
5617 OPT_BOOL(0, "exit-code", &options
->flags
.exit_with_status
,
5618 N_("exit with 1 if there were differences, 0 otherwise")),
5619 OPT_BOOL(0, "quiet", &options
->flags
.quick
,
5620 N_("disable all output of the program")),
5621 OPT_BOOL(0, "ext-diff", &options
->flags
.allow_external
,
5622 N_("allow an external diff helper to be executed")),
5623 OPT_CALLBACK_F(0, "textconv", options
, NULL
,
5624 N_("run external text conversion filters when comparing binary files"),
5625 PARSE_OPT_NOARG
, diff_opt_textconv
),
5626 OPT_CALLBACK_F(0, "ignore-submodules", options
, N_("<when>"),
5627 N_("ignore changes to submodules in the diff generation"),
5628 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5629 diff_opt_ignore_submodules
),
5630 OPT_CALLBACK_F(0, "submodule", options
, N_("<format>"),
5631 N_("specify how differences in submodules are shown"),
5632 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5633 diff_opt_submodule
),
5634 OPT_SET_INT_F(0, "ita-invisible-in-index", &options
->ita_invisible_in_index
,
5635 N_("hide 'git add -N' entries from the index"),
5636 1, PARSE_OPT_NONEG
),
5637 OPT_SET_INT_F(0, "ita-visible-in-index", &options
->ita_invisible_in_index
,
5638 N_("treat 'git add -N' entries as real in the index"),
5639 0, PARSE_OPT_NONEG
),
5640 OPT_CALLBACK_F('S', NULL
, options
, N_("<string>"),
5641 N_("look for differences that change the number of occurrences of the specified string"),
5642 0, diff_opt_pickaxe_string
),
5643 OPT_CALLBACK_F('G', NULL
, options
, N_("<regex>"),
5644 N_("look for differences that change the number of occurrences of the specified regex"),
5645 0, diff_opt_pickaxe_regex
),
5646 OPT_BIT_F(0, "pickaxe-all", &options
->pickaxe_opts
,
5647 N_("show all changes in the changeset with -S or -G"),
5648 DIFF_PICKAXE_ALL
, PARSE_OPT_NONEG
),
5649 OPT_BIT_F(0, "pickaxe-regex", &options
->pickaxe_opts
,
5650 N_("treat <string> in -S as extended POSIX regular expression"),
5651 DIFF_PICKAXE_REGEX
, PARSE_OPT_NONEG
),
5652 OPT_FILENAME('O', NULL
, &options
->orderfile
,
5653 N_("control the order in which files appear in the output")),
5654 OPT_CALLBACK_F(0, "rotate-to", options
, N_("<path>"),
5655 N_("show the change in the specified path first"),
5656 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5657 OPT_CALLBACK_F(0, "skip-to", options
, N_("<path>"),
5658 N_("skip the output to the specified path"),
5659 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5660 OPT_CALLBACK_F(0, "find-object", options
, N_("<object-id>"),
5661 N_("look for differences that change the number of occurrences of the specified object"),
5662 PARSE_OPT_NONEG
, diff_opt_find_object
),
5663 OPT_CALLBACK_F(0, "diff-filter", options
, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5664 N_("select files by diff type"),
5665 PARSE_OPT_NONEG
, diff_opt_diff_filter
),
5666 { OPTION_CALLBACK
, 0, "output", options
, N_("<file>"),
5667 N_("output to a specific file"),
5668 PARSE_OPT_NONEG
, NULL
, 0, diff_opt_output
},
5673 ALLOC_ARRAY(options
->parseopts
, ARRAY_SIZE(parseopts
));
5674 memcpy(options
->parseopts
, parseopts
, sizeof(parseopts
));
5677 int diff_opt_parse(struct diff_options
*options
,
5678 const char **av
, int ac
, const char *prefix
)
5683 ac
= parse_options(ac
, av
, prefix
, options
->parseopts
, NULL
,
5684 PARSE_OPT_KEEP_DASHDASH
|
5685 PARSE_OPT_KEEP_UNKNOWN_OPT
|
5686 PARSE_OPT_NO_INTERNAL_HELP
|
5687 PARSE_OPT_ONE_SHOT
|
5688 PARSE_OPT_STOP_AT_NON_OPTION
);
5693 int parse_rename_score(const char **cp_p
)
5695 unsigned long num
, scale
;
5697 const char *cp
= *cp_p
;
5704 if ( !dot
&& ch
== '.' ) {
5707 } else if ( ch
== '%' ) {
5708 scale
= dot
? scale
*100 : 100;
5709 cp
++; /* % is always at the end */
5711 } else if ( ch
>= '0' && ch
<= '9' ) {
5712 if ( scale
< 100000 ) {
5714 num
= (num
*10) + (ch
-'0');
5723 /* user says num divided by scale and we say internally that
5724 * is MAX_SCORE * num / scale.
5726 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
5729 struct diff_queue_struct diff_queued_diff
;
5731 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
5733 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
5734 queue
->queue
[queue
->nr
++] = dp
;
5737 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
5738 struct diff_filespec
*one
,
5739 struct diff_filespec
*two
)
5741 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
5749 void diff_free_filepair(struct diff_filepair
*p
)
5751 free_filespec(p
->one
);
5752 free_filespec(p
->two
);
5756 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
5761 /* Do we want all 40 hex characters? */
5762 if (len
== the_hash_algo
->hexsz
)
5763 return oid_to_hex(oid
);
5765 /* An abbreviated value is fine, possibly followed by an ellipsis. */
5766 abbrev
= diff_abbrev_oid(oid
, len
);
5768 if (!print_sha1_ellipsis())
5771 abblen
= strlen(abbrev
);
5774 * In well-behaved cases, where the abbreviated result is the
5775 * same as the requested length, append three dots after the
5776 * abbreviation (hence the whole logic is limited to the case
5777 * where abblen < 37); when the actual abbreviated result is a
5778 * bit longer than the requested length, we reduce the number
5779 * of dots so that they match the well-behaved ones. However,
5780 * if the actual abbreviation is longer than the requested
5781 * length by more than three, we give up on aligning, and add
5782 * three dots anyway, to indicate that the output is not the
5783 * full object name. Yes, this may be suboptimal, but this
5784 * appears only in "diff --raw --abbrev" output and it is not
5785 * worth the effort to change it now. Note that this would
5786 * likely to work fine when the automatic sizing of default
5787 * abbreviation length is used--we would be fed -1 in "len" in
5788 * that case, and will end up always appending three-dots, but
5789 * the automatic sizing is supposed to give abblen that ensures
5790 * uniqueness across all objects (statistically speaking).
5792 if (abblen
< the_hash_algo
->hexsz
- 3) {
5793 static char hex
[GIT_MAX_HEXSZ
+ 1];
5794 if (len
< abblen
&& abblen
<= len
+ 2)
5795 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
5797 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
5801 return oid_to_hex(oid
);
5804 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
5806 int line_termination
= opt
->line_termination
;
5807 int inter_name_termination
= line_termination
? '\t' : '\0';
5809 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5810 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
5811 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
5812 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
5813 fprintf(opt
->file
, "%s ",
5814 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
5817 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
5818 inter_name_termination
);
5820 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
5823 if (p
->status
== DIFF_STATUS_COPIED
||
5824 p
->status
== DIFF_STATUS_RENAMED
) {
5825 const char *name_a
, *name_b
;
5826 name_a
= p
->one
->path
;
5827 name_b
= p
->two
->path
;
5828 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5829 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
5830 write_name_quoted(name_b
, opt
->file
, line_termination
);
5832 const char *name_a
, *name_b
;
5833 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
5835 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
5836 write_name_quoted(name_a
, opt
->file
, line_termination
);
5840 int diff_unmodified_pair(struct diff_filepair
*p
)
5842 /* This function is written stricter than necessary to support
5843 * the currently implemented transformers, but the idea is to
5844 * let transformers to produce diff_filepairs any way they want,
5845 * and filter and clean them up here before producing the output.
5847 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
5849 if (DIFF_PAIR_UNMERGED(p
))
5850 return 0; /* unmerged is interesting */
5852 /* deletion, addition, mode or type change
5853 * and rename are all interesting.
5855 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
5856 DIFF_PAIR_MODE_CHANGED(p
) ||
5857 strcmp(one
->path
, two
->path
))
5860 /* both are valid and point at the same path. that is, we are
5861 * dealing with a change.
5863 if (one
->oid_valid
&& two
->oid_valid
&&
5864 oideq(&one
->oid
, &two
->oid
) &&
5865 !one
->dirty_submodule
&& !two
->dirty_submodule
)
5866 return 1; /* no change */
5867 if (!one
->oid_valid
&& !two
->oid_valid
)
5868 return 1; /* both look at the same file on the filesystem. */
5872 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
5874 int include_conflict_headers
=
5875 (additional_headers(o
, p
->one
->path
) &&
5877 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
5880 * Check if we can return early without showing a diff. Note that
5881 * diff_filepair only stores {oid, path, mode, is_valid}
5882 * information for each path, and thus diff_unmodified_pair() only
5883 * considers those bits of info. However, we do not want pairs
5884 * created by create_filepairs_for_header_only_notifications()
5885 * (which always look like unmodified pairs) to be ignored, so
5886 * return early if both p is unmodified AND we don't want to
5887 * include_conflict_headers.
5889 if (diff_unmodified_pair(p
) && !include_conflict_headers
)
5892 /* Actually, we can also return early to avoid showing tree diffs */
5893 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5894 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5900 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
5901 struct diffstat_t
*diffstat
)
5903 if (diff_unmodified_pair(p
))
5906 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5907 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5908 return; /* no useful stat for tree diffs */
5910 run_diffstat(p
, o
, diffstat
);
5913 static void diff_flush_checkdiff(struct diff_filepair
*p
,
5914 struct diff_options
*o
)
5916 if (diff_unmodified_pair(p
))
5919 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
5920 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
5921 return; /* nothing to check in tree diffs */
5923 run_checkdiff(p
, o
);
5926 int diff_queue_is_empty(struct diff_options
*o
)
5928 struct diff_queue_struct
*q
= &diff_queued_diff
;
5930 int include_conflict_headers
=
5931 (o
->additional_path_headers
&&
5932 strmap_get_size(o
->additional_path_headers
) &&
5934 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
5936 if (include_conflict_headers
)
5939 for (i
= 0; i
< q
->nr
; i
++)
5940 if (!diff_unmodified_pair(q
->queue
[i
]))
5946 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
5948 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
5951 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
5953 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
5954 fprintf(stderr
, "queue[%d] %s size %lu\n",
5959 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
5961 diff_debug_filespec(p
->one
, i
, "one");
5962 diff_debug_filespec(p
->two
, i
, "two");
5963 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
5964 p
->score
, p
->status
? p
->status
: '?',
5965 p
->one
->rename_used
, p
->broken_pair
);
5968 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
5972 fprintf(stderr
, "%s\n", msg
);
5973 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
5974 for (i
= 0; i
< q
->nr
; i
++) {
5975 struct diff_filepair
*p
= q
->queue
[i
];
5976 diff_debug_filepair(p
, i
);
5981 static void diff_resolve_rename_copy(void)
5984 struct diff_filepair
*p
;
5985 struct diff_queue_struct
*q
= &diff_queued_diff
;
5987 diff_debug_queue("resolve-rename-copy", q
);
5989 for (i
= 0; i
< q
->nr
; i
++) {
5991 p
->status
= 0; /* undecided */
5992 if (DIFF_PAIR_UNMERGED(p
))
5993 p
->status
= DIFF_STATUS_UNMERGED
;
5994 else if (!DIFF_FILE_VALID(p
->one
))
5995 p
->status
= DIFF_STATUS_ADDED
;
5996 else if (!DIFF_FILE_VALID(p
->two
))
5997 p
->status
= DIFF_STATUS_DELETED
;
5998 else if (DIFF_PAIR_TYPE_CHANGED(p
))
5999 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
6001 /* from this point on, we are dealing with a pair
6002 * whose both sides are valid and of the same type, i.e.
6003 * either in-place edit or rename/copy edit.
6005 else if (DIFF_PAIR_RENAME(p
)) {
6007 * A rename might have re-connected a broken
6008 * pair up, causing the pathnames to be the
6009 * same again. If so, that's not a rename at
6010 * all, just a modification..
6012 * Otherwise, see if this source was used for
6013 * multiple renames, in which case we decrement
6014 * the count, and call it a copy.
6016 if (!strcmp(p
->one
->path
, p
->two
->path
))
6017 p
->status
= DIFF_STATUS_MODIFIED
;
6018 else if (--p
->one
->rename_used
> 0)
6019 p
->status
= DIFF_STATUS_COPIED
;
6021 p
->status
= DIFF_STATUS_RENAMED
;
6023 else if (!oideq(&p
->one
->oid
, &p
->two
->oid
) ||
6024 p
->one
->mode
!= p
->two
->mode
||
6025 p
->one
->dirty_submodule
||
6026 p
->two
->dirty_submodule
||
6027 is_null_oid(&p
->one
->oid
))
6028 p
->status
= DIFF_STATUS_MODIFIED
;
6030 /* This is a "no-change" entry and should not
6031 * happen anymore, but prepare for broken callers.
6033 error("feeding unmodified %s to diffcore",
6035 p
->status
= DIFF_STATUS_UNKNOWN
;
6038 diff_debug_queue("resolve-rename-copy done", q
);
6041 static int check_pair_status(struct diff_filepair
*p
)
6043 switch (p
->status
) {
6044 case DIFF_STATUS_UNKNOWN
:
6047 die("internal error in diff-resolve-rename-copy");
6053 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
6055 int fmt
= opt
->output_format
;
6057 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
6058 diff_flush_checkdiff(p
, opt
);
6059 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
6060 diff_flush_raw(p
, opt
);
6061 else if (fmt
& DIFF_FORMAT_NAME
) {
6062 const char *name_a
, *name_b
;
6063 name_a
= p
->two
->path
;
6065 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6066 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
6067 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
6071 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
6073 struct strbuf sb
= STRBUF_INIT
;
6075 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
6077 strbuf_addf(&sb
, " %s ", newdelete
);
6079 quote_c_style(fs
->path
, &sb
, NULL
, 0);
6080 strbuf_addch(&sb
, '\n');
6081 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6083 strbuf_release(&sb
);
6086 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
6089 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
6090 struct strbuf sb
= STRBUF_INIT
;
6091 strbuf_addf(&sb
, " mode change %06o => %06o",
6092 p
->one
->mode
, p
->two
->mode
);
6094 strbuf_addch(&sb
, ' ');
6095 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6097 strbuf_addch(&sb
, '\n');
6098 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6100 strbuf_release(&sb
);
6104 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
6105 struct diff_filepair
*p
)
6107 struct strbuf sb
= STRBUF_INIT
;
6108 struct strbuf names
= STRBUF_INIT
;
6110 pprint_rename(&names
, p
->one
->path
, p
->two
->path
);
6111 strbuf_addf(&sb
, " %s %s (%d%%)\n",
6112 renamecopy
, names
.buf
, similarity_index(p
));
6113 strbuf_release(&names
);
6114 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6116 show_mode_change(opt
, p
, 0);
6117 strbuf_release(&sb
);
6120 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
6123 case DIFF_STATUS_DELETED
:
6124 show_file_mode_name(opt
, "delete", p
->one
);
6126 case DIFF_STATUS_ADDED
:
6127 show_file_mode_name(opt
, "create", p
->two
);
6129 case DIFF_STATUS_COPIED
:
6130 show_rename_copy(opt
, "copy", p
);
6132 case DIFF_STATUS_RENAMED
:
6133 show_rename_copy(opt
, "rename", p
);
6137 struct strbuf sb
= STRBUF_INIT
;
6138 strbuf_addstr(&sb
, " rewrite ");
6139 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6140 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
6141 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6143 strbuf_release(&sb
);
6145 show_mode_change(opt
, p
, !p
->score
);
6155 static int remove_space(char *line
, int len
)
6161 for (i
= 0; i
< len
; i
++)
6162 if (!isspace((c
= line
[i
])))
6168 void flush_one_hunk(struct object_id
*result
, git_hash_ctx
*ctx
)
6170 unsigned char hash
[GIT_MAX_RAWSZ
];
6171 unsigned short carry
= 0;
6174 the_hash_algo
->final_fn(hash
, ctx
);
6175 the_hash_algo
->init_fn(ctx
);
6176 /* 20-byte sum, with carry */
6177 for (i
= 0; i
< the_hash_algo
->rawsz
; ++i
) {
6178 carry
+= result
->hash
[i
] + hash
[i
];
6179 result
->hash
[i
] = carry
;
6184 static int patch_id_consume(void *priv
, char *line
, unsigned long len
)
6186 struct patch_id_t
*data
= priv
;
6189 if (len
> 12 && starts_with(line
, "\\ "))
6191 new_len
= remove_space(line
, len
);
6193 the_hash_algo
->update_fn(data
->ctx
, line
, new_len
);
6194 data
->patchlen
+= new_len
;
6198 static void patch_id_add_string(git_hash_ctx
*ctx
, const char *str
)
6200 the_hash_algo
->update_fn(ctx
, str
, strlen(str
));
6203 static void patch_id_add_mode(git_hash_ctx
*ctx
, unsigned mode
)
6205 /* large enough for 2^32 in octal */
6207 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
6208 the_hash_algo
->update_fn(ctx
, buf
, len
);
6211 /* returns 0 upon success, and writes result into oid */
6212 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
, int stable
)
6214 struct diff_queue_struct
*q
= &diff_queued_diff
;
6217 struct patch_id_t data
;
6219 the_hash_algo
->init_fn(&ctx
);
6220 memset(&data
, 0, sizeof(struct patch_id_t
));
6224 for (i
= 0; i
< q
->nr
; i
++) {
6228 struct diff_filepair
*p
= q
->queue
[i
];
6231 memset(&xpp
, 0, sizeof(xpp
));
6232 memset(&xecfg
, 0, sizeof(xecfg
));
6234 return error("internal diff status error");
6235 if (p
->status
== DIFF_STATUS_UNKNOWN
)
6237 if (diff_unmodified_pair(p
))
6239 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6240 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6242 if (DIFF_PAIR_UNMERGED(p
))
6245 diff_fill_oid_info(p
->one
, options
->repo
->index
);
6246 diff_fill_oid_info(p
->two
, options
->repo
->index
);
6248 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
6249 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
6250 patch_id_add_string(&ctx
, "diff--git");
6251 patch_id_add_string(&ctx
, "a/");
6252 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6253 patch_id_add_string(&ctx
, "b/");
6254 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6256 if (p
->one
->mode
== 0) {
6257 patch_id_add_string(&ctx
, "newfilemode");
6258 patch_id_add_mode(&ctx
, p
->two
->mode
);
6259 patch_id_add_string(&ctx
, "---/dev/null");
6260 patch_id_add_string(&ctx
, "+++b/");
6261 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6262 } else if (p
->two
->mode
== 0) {
6263 patch_id_add_string(&ctx
, "deletedfilemode");
6264 patch_id_add_mode(&ctx
, p
->one
->mode
);
6265 patch_id_add_string(&ctx
, "---a/");
6266 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6267 patch_id_add_string(&ctx
, "+++/dev/null");
6269 patch_id_add_string(&ctx
, "---a/");
6270 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6271 patch_id_add_string(&ctx
, "+++b/");
6272 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6275 if (diff_header_only
)
6278 if (fill_mmfile(options
->repo
, &mf1
, p
->one
) < 0 ||
6279 fill_mmfile(options
->repo
, &mf2
, p
->two
) < 0)
6280 return error("unable to read files to diff");
6282 if (diff_filespec_is_binary(options
->repo
, p
->one
) ||
6283 diff_filespec_is_binary(options
->repo
, p
->two
)) {
6284 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->one
->oid
),
6285 the_hash_algo
->hexsz
);
6286 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->two
->oid
),
6287 the_hash_algo
->hexsz
);
6293 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
6294 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
6295 patch_id_consume
, &data
, &xpp
, &xecfg
))
6296 return error("unable to generate patch-id diff for %s",
6300 flush_one_hunk(oid
, &ctx
);
6304 the_hash_algo
->final_oid_fn(oid
, &ctx
);
6309 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
, int stable
)
6311 struct diff_queue_struct
*q
= &diff_queued_diff
;
6313 int result
= diff_get_patch_id(options
, oid
, diff_header_only
, stable
);
6315 for (i
= 0; i
< q
->nr
; i
++)
6316 diff_free_filepair(q
->queue
[i
]);
6319 DIFF_QUEUE_CLEAR(q
);
6324 static int is_summary_empty(const struct diff_queue_struct
*q
)
6328 for (i
= 0; i
< q
->nr
; i
++) {
6329 const struct diff_filepair
*p
= q
->queue
[i
];
6331 switch (p
->status
) {
6332 case DIFF_STATUS_DELETED
:
6333 case DIFF_STATUS_ADDED
:
6334 case DIFF_STATUS_COPIED
:
6335 case DIFF_STATUS_RENAMED
:
6340 if (p
->one
->mode
&& p
->two
->mode
&&
6341 p
->one
->mode
!= p
->two
->mode
)
6349 static const char rename_limit_warning
[] =
6350 N_("exhaustive rename detection was skipped due to too many files.");
6352 static const char degrade_cc_to_c_warning
[] =
6353 N_("only found copies from modified paths due to too many files.");
6355 static const char rename_limit_advice
[] =
6356 N_("you may want to set your %s variable to at least "
6357 "%d and retry the command.");
6359 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
6363 warning(_(degrade_cc_to_c_warning
));
6365 warning(_(rename_limit_warning
));
6369 warning(_(rename_limit_advice
), varname
, needed
);
6372 static void create_filepairs_for_header_only_notifications(struct diff_options
*o
)
6374 struct strset present
;
6375 struct diff_queue_struct
*q
= &diff_queued_diff
;
6376 struct hashmap_iter iter
;
6377 struct strmap_entry
*e
;
6380 strset_init_with_options(&present
, /*pool*/ NULL
, /*strdup*/ 0);
6383 * Find out which paths exist in diff_queued_diff, preferring
6384 * one->path for any pair that has multiple paths.
6386 for (i
= 0; i
< q
->nr
; i
++) {
6387 struct diff_filepair
*p
= q
->queue
[i
];
6388 char *path
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
6390 if (strmap_contains(o
->additional_path_headers
, path
))
6391 strset_add(&present
, path
);
6395 * Loop over paths in additional_path_headers; for each NOT already
6396 * in diff_queued_diff, create a synthetic filepair and insert that
6397 * into diff_queued_diff.
6399 strmap_for_each_entry(o
->additional_path_headers
, &iter
, e
) {
6400 if (!strset_contains(&present
, e
->key
)) {
6401 struct diff_filespec
*one
, *two
;
6402 struct diff_filepair
*p
;
6404 one
= alloc_filespec(e
->key
);
6405 two
= alloc_filespec(e
->key
);
6406 fill_filespec(one
, null_oid(), 0, 0);
6407 fill_filespec(two
, null_oid(), 0, 0);
6408 p
= diff_queue(q
, one
, two
);
6409 p
->status
= DIFF_STATUS_MODIFIED
;
6413 /* Re-sort the filepairs */
6414 diffcore_fix_diff_index();
6417 strset_clear(&present
);
6420 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
6423 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
6424 struct diff_queue_struct
*q
= &diff_queued_diff
;
6426 if (WSEH_NEW
& WS_RULE_MASK
)
6427 BUG("WS rules bit mask overlaps with diff symbol flags");
6430 o
->emitted_symbols
= &esm
;
6432 if (o
->additional_path_headers
)
6433 create_filepairs_for_header_only_notifications(o
);
6435 for (i
= 0; i
< q
->nr
; i
++) {
6436 struct diff_filepair
*p
= q
->queue
[i
];
6437 if (check_pair_status(p
))
6438 diff_flush_patch(p
, o
);
6441 if (o
->emitted_symbols
) {
6442 if (o
->color_moved
) {
6443 struct mem_pool entry_pool
;
6444 struct moved_entry_list
*entry_list
;
6446 mem_pool_init(&entry_pool
, 1024 * 1024);
6447 entry_list
= add_lines_to_move_detection(o
,
6449 mark_color_as_moved(o
, entry_list
);
6450 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
6453 mem_pool_discard(&entry_pool
, 0);
6457 for (i
= 0; i
< esm
.nr
; i
++)
6458 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
6460 for (i
= 0; i
< esm
.nr
; i
++)
6461 free((void *)esm
.buf
[i
].line
);
6464 o
->emitted_symbols
= NULL
;
6468 static void diff_free_file(struct diff_options
*options
)
6470 if (options
->close_file
)
6471 fclose(options
->file
);
6474 static void diff_free_ignore_regex(struct diff_options
*options
)
6478 for (i
= 0; i
< options
->ignore_regex_nr
; i
++) {
6479 regfree(options
->ignore_regex
[i
]);
6480 free(options
->ignore_regex
[i
]);
6482 free(options
->ignore_regex
);
6485 void diff_free(struct diff_options
*options
)
6487 if (options
->no_free
)
6490 diff_free_file(options
);
6491 diff_free_ignore_regex(options
);
6492 clear_pathspec(&options
->pathspec
);
6493 FREE_AND_NULL(options
->parseopts
);
6496 void diff_flush(struct diff_options
*options
)
6498 struct diff_queue_struct
*q
= &diff_queued_diff
;
6499 int i
, output_format
= options
->output_format
;
6501 int dirstat_by_line
= 0;
6504 * Order: raw, stat, summary, patch
6505 * or: name/name-status/checkdiff (other bits clear)
6507 if (!q
->nr
&& !options
->additional_path_headers
)
6510 if (output_format
& (DIFF_FORMAT_RAW
|
6512 DIFF_FORMAT_NAME_STATUS
|
6513 DIFF_FORMAT_CHECKDIFF
)) {
6514 for (i
= 0; i
< q
->nr
; i
++) {
6515 struct diff_filepair
*p
= q
->queue
[i
];
6516 if (check_pair_status(p
))
6517 flush_one_pair(p
, options
);
6522 if (output_format
& DIFF_FORMAT_DIRSTAT
&& options
->flags
.dirstat_by_line
)
6523 dirstat_by_line
= 1;
6525 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
6527 struct diffstat_t diffstat
;
6529 compute_diffstat(options
, &diffstat
, q
);
6530 if (output_format
& DIFF_FORMAT_NUMSTAT
)
6531 show_numstat(&diffstat
, options
);
6532 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
6533 show_stats(&diffstat
, options
);
6534 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
6535 show_shortstats(&diffstat
, options
);
6536 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
6537 show_dirstat_by_line(&diffstat
, options
);
6538 free_diffstat_info(&diffstat
);
6541 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
6542 show_dirstat(options
);
6544 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
6545 for (i
= 0; i
< q
->nr
; i
++) {
6546 diff_summary(options
, q
->queue
[i
]);
6551 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
6552 options
->flags
.exit_with_status
&&
6553 options
->flags
.diff_from_contents
) {
6555 * run diff_flush_patch for the exit status. setting
6556 * options->file to /dev/null should be safe, because we
6557 * aren't supposed to produce any output anyway.
6559 diff_free_file(options
);
6560 options
->file
= xfopen("/dev/null", "w");
6561 options
->close_file
= 1;
6562 options
->color_moved
= 0;
6563 for (i
= 0; i
< q
->nr
; i
++) {
6564 struct diff_filepair
*p
= q
->queue
[i
];
6565 if (check_pair_status(p
))
6566 diff_flush_patch(p
, options
);
6567 if (options
->found_changes
)
6572 if (output_format
& DIFF_FORMAT_PATCH
) {
6574 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
6575 if (options
->stat_sep
)
6576 /* attach patch instead of inline */
6577 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
6581 diff_flush_patch_all_file_pairs(options
);
6584 if (output_format
& DIFF_FORMAT_CALLBACK
)
6585 options
->format_callback(q
, options
, options
->format_callback_data
);
6587 for (i
= 0; i
< q
->nr
; i
++)
6588 diff_free_filepair(q
->queue
[i
]);
6591 DIFF_QUEUE_CLEAR(q
);
6595 * Report the content-level differences with HAS_CHANGES;
6596 * diff_addremove/diff_change does not set the bit when
6597 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6599 if (options
->flags
.diff_from_contents
) {
6600 if (options
->found_changes
)
6601 options
->flags
.has_changes
= 1;
6603 options
->flags
.has_changes
= 0;
6607 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
6609 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
6611 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
6613 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
6614 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
6615 filter_bit_tst(p
->status
, options
)));
6618 static void diffcore_apply_filter(struct diff_options
*options
)
6621 struct diff_queue_struct
*q
= &diff_queued_diff
;
6622 struct diff_queue_struct outq
;
6624 DIFF_QUEUE_CLEAR(&outq
);
6626 if (!options
->filter
)
6629 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
6631 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
6632 if (match_filter(options
, q
->queue
[i
]))
6638 /* otherwise we will clear the whole queue
6639 * by copying the empty outq at the end of this
6640 * function, but first clear the current entries
6643 for (i
= 0; i
< q
->nr
; i
++)
6644 diff_free_filepair(q
->queue
[i
]);
6647 /* Only the matching ones */
6648 for (i
= 0; i
< q
->nr
; i
++) {
6649 struct diff_filepair
*p
= q
->queue
[i
];
6650 if (match_filter(options
, p
))
6653 diff_free_filepair(p
);
6660 /* Check whether two filespecs with the same mode and size are identical */
6661 static int diff_filespec_is_identical(struct repository
*r
,
6662 struct diff_filespec
*one
,
6663 struct diff_filespec
*two
)
6665 if (S_ISGITLINK(one
->mode
))
6667 if (diff_populate_filespec(r
, one
, NULL
))
6669 if (diff_populate_filespec(r
, two
, NULL
))
6671 return !memcmp(one
->data
, two
->data
, one
->size
);
6674 static int diff_filespec_check_stat_unmatch(struct repository
*r
,
6675 struct diff_filepair
*p
)
6677 struct diff_populate_filespec_options dpf_options
= {
6678 .check_size_only
= 1,
6679 .missing_object_cb
= diff_queued_diff_prefetch
,
6680 .missing_object_data
= r
,
6683 if (p
->done_skip_stat_unmatch
)
6684 return p
->skip_stat_unmatch_result
;
6686 p
->done_skip_stat_unmatch
= 1;
6687 p
->skip_stat_unmatch_result
= 0;
6689 * 1. Entries that come from stat info dirtiness
6690 * always have both sides (iow, not create/delete),
6691 * one side of the object name is unknown, with
6692 * the same mode and size. Keep the ones that
6693 * do not match these criteria. They have real
6696 * 2. At this point, the file is known to be modified,
6697 * with the same mode and size, and the object
6698 * name of one side is unknown. Need to inspect
6699 * the identical contents.
6701 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
6702 !DIFF_FILE_VALID(p
->two
) ||
6703 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
6704 (p
->one
->mode
!= p
->two
->mode
) ||
6705 diff_populate_filespec(r
, p
->one
, &dpf_options
) ||
6706 diff_populate_filespec(r
, p
->two
, &dpf_options
) ||
6707 (p
->one
->size
!= p
->two
->size
) ||
6708 !diff_filespec_is_identical(r
, p
->one
, p
->two
)) /* (2) */
6709 p
->skip_stat_unmatch_result
= 1;
6710 return p
->skip_stat_unmatch_result
;
6713 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
6716 struct diff_queue_struct
*q
= &diff_queued_diff
;
6717 struct diff_queue_struct outq
;
6718 DIFF_QUEUE_CLEAR(&outq
);
6720 for (i
= 0; i
< q
->nr
; i
++) {
6721 struct diff_filepair
*p
= q
->queue
[i
];
6723 if (diff_filespec_check_stat_unmatch(diffopt
->repo
, p
))
6727 * The caller can subtract 1 from skip_stat_unmatch
6728 * to determine how many paths were dirty only
6729 * due to stat info mismatch.
6731 if (!diffopt
->flags
.no_index
)
6732 diffopt
->skip_stat_unmatch
++;
6733 diff_free_filepair(p
);
6740 static int diffnamecmp(const void *a_
, const void *b_
)
6742 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
6743 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
6744 const char *name_a
, *name_b
;
6746 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
6747 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
6748 return strcmp(name_a
, name_b
);
6751 void diffcore_fix_diff_index(void)
6753 struct diff_queue_struct
*q
= &diff_queued_diff
;
6754 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
6757 void diff_add_if_missing(struct repository
*r
,
6758 struct oid_array
*to_fetch
,
6759 const struct diff_filespec
*filespec
)
6761 if (filespec
&& filespec
->oid_valid
&&
6762 !S_ISGITLINK(filespec
->mode
) &&
6763 oid_object_info_extended(r
, &filespec
->oid
, NULL
,
6764 OBJECT_INFO_FOR_PREFETCH
))
6765 oid_array_append(to_fetch
, &filespec
->oid
);
6768 void diff_queued_diff_prefetch(void *repository
)
6770 struct repository
*repo
= repository
;
6772 struct diff_queue_struct
*q
= &diff_queued_diff
;
6773 struct oid_array to_fetch
= OID_ARRAY_INIT
;
6775 for (i
= 0; i
< q
->nr
; i
++) {
6776 struct diff_filepair
*p
= q
->queue
[i
];
6777 diff_add_if_missing(repo
, &to_fetch
, p
->one
);
6778 diff_add_if_missing(repo
, &to_fetch
, p
->two
);
6782 * NEEDSWORK: Consider deduplicating the OIDs sent.
6784 promisor_remote_get_direct(repo
, to_fetch
.oid
, to_fetch
.nr
);
6786 oid_array_clear(&to_fetch
);
6789 void diffcore_std(struct diff_options
*options
)
6791 int output_formats_to_prefetch
= DIFF_FORMAT_DIFFSTAT
|
6792 DIFF_FORMAT_NUMSTAT
|
6794 DIFF_FORMAT_SHORTSTAT
|
6795 DIFF_FORMAT_DIRSTAT
;
6798 * Check if the user requested a blob-data-requiring diff output and/or
6799 * break-rewrite detection (which requires blob data). If yes, prefetch
6802 * If no prefetching occurs, diffcore_rename() will prefetch if it
6803 * decides that it needs inexact rename detection.
6805 if (options
->repo
== the_repository
&& has_promisor_remote() &&
6806 (options
->output_format
& output_formats_to_prefetch
||
6807 options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
6808 diff_queued_diff_prefetch(options
->repo
);
6810 /* NOTE please keep the following in sync with diff_tree_combined() */
6811 if (options
->skip_stat_unmatch
)
6812 diffcore_skip_stat_unmatch(options
);
6813 if (!options
->found_follow
) {
6814 /* See try_to_follow_renames() in tree-diff.c */
6815 if (options
->break_opt
!= -1)
6816 diffcore_break(options
->repo
,
6817 options
->break_opt
);
6818 if (options
->detect_rename
)
6819 diffcore_rename(options
);
6820 if (options
->break_opt
!= -1)
6821 diffcore_merge_broken();
6823 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
6824 diffcore_pickaxe(options
);
6825 if (options
->orderfile
)
6826 diffcore_order(options
->orderfile
);
6827 if (options
->rotate_to
)
6828 diffcore_rotate(options
);
6829 if (!options
->found_follow
)
6830 /* See try_to_follow_renames() in tree-diff.c */
6831 diff_resolve_rename_copy();
6832 diffcore_apply_filter(options
);
6834 if (diff_queued_diff
.nr
&& !options
->flags
.diff_from_contents
)
6835 options
->flags
.has_changes
= 1;
6837 options
->flags
.has_changes
= 0;
6839 options
->found_follow
= 0;
6842 int diff_result_code(struct diff_options
*opt
, int status
)
6846 diff_warn_rename_limit("diff.renameLimit",
6847 opt
->needed_rename_limit
,
6848 opt
->degraded_cc_to_c
);
6849 if (!opt
->flags
.exit_with_status
&&
6850 !(opt
->output_format
& DIFF_FORMAT_CHECKDIFF
))
6852 if (opt
->flags
.exit_with_status
&&
6853 opt
->flags
.has_changes
)
6855 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
6856 opt
->flags
.check_failed
)
6861 int diff_can_quit_early(struct diff_options
*opt
)
6863 return (opt
->flags
.quick
&&
6865 opt
->flags
.has_changes
);
6869 * Shall changes to this submodule be ignored?
6871 * Submodule changes can be configured to be ignored separately for each path,
6872 * but that configuration can be overridden from the command line.
6874 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
6877 struct diff_flags orig_flags
= options
->flags
;
6878 if (!options
->flags
.override_submodule_config
)
6879 set_diffopt_flags_from_submodule_config(options
, path
);
6880 if (options
->flags
.ignore_submodules
)
6882 options
->flags
= orig_flags
;
6886 void compute_diffstat(struct diff_options
*options
,
6887 struct diffstat_t
*diffstat
,
6888 struct diff_queue_struct
*q
)
6892 memset(diffstat
, 0, sizeof(struct diffstat_t
));
6893 for (i
= 0; i
< q
->nr
; i
++) {
6894 struct diff_filepair
*p
= q
->queue
[i
];
6895 if (check_pair_status(p
))
6896 diff_flush_stat(p
, options
, diffstat
);
6900 void diff_addremove(struct diff_options
*options
,
6901 int addremove
, unsigned mode
,
6902 const struct object_id
*oid
,
6904 const char *concatpath
, unsigned dirty_submodule
)
6906 struct diff_filespec
*one
, *two
;
6908 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
6911 /* This may look odd, but it is a preparation for
6912 * feeding "there are unchanged files which should
6913 * not produce diffs, but when you are doing copy
6914 * detection you would need them, so here they are"
6915 * entries to the diff-core. They will be prefixed
6916 * with something like '=' or '*' (I haven't decided
6917 * which but should not make any difference).
6918 * Feeding the same new and old to diff_change()
6919 * also has the same effect.
6920 * Before the final output happens, they are pruned after
6921 * merged into rename/copy pairs as appropriate.
6923 if (options
->flags
.reverse_diff
)
6924 addremove
= (addremove
== '+' ? '-' :
6925 addremove
== '-' ? '+' : addremove
);
6927 if (options
->prefix
&&
6928 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
6931 one
= alloc_filespec(concatpath
);
6932 two
= alloc_filespec(concatpath
);
6934 if (addremove
!= '+')
6935 fill_filespec(one
, oid
, oid_valid
, mode
);
6936 if (addremove
!= '-') {
6937 fill_filespec(two
, oid
, oid_valid
, mode
);
6938 two
->dirty_submodule
= dirty_submodule
;
6941 diff_queue(&diff_queued_diff
, one
, two
);
6942 if (!options
->flags
.diff_from_contents
)
6943 options
->flags
.has_changes
= 1;
6946 void diff_change(struct diff_options
*options
,
6947 unsigned old_mode
, unsigned new_mode
,
6948 const struct object_id
*old_oid
,
6949 const struct object_id
*new_oid
,
6950 int old_oid_valid
, int new_oid_valid
,
6951 const char *concatpath
,
6952 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
6954 struct diff_filespec
*one
, *two
;
6955 struct diff_filepair
*p
;
6957 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
6958 is_submodule_ignored(concatpath
, options
))
6961 if (options
->flags
.reverse_diff
) {
6962 SWAP(old_mode
, new_mode
);
6963 SWAP(old_oid
, new_oid
);
6964 SWAP(old_oid_valid
, new_oid_valid
);
6965 SWAP(old_dirty_submodule
, new_dirty_submodule
);
6968 if (options
->prefix
&&
6969 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
6972 one
= alloc_filespec(concatpath
);
6973 two
= alloc_filespec(concatpath
);
6974 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
6975 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
6976 one
->dirty_submodule
= old_dirty_submodule
;
6977 two
->dirty_submodule
= new_dirty_submodule
;
6978 p
= diff_queue(&diff_queued_diff
, one
, two
);
6980 if (options
->flags
.diff_from_contents
)
6983 if (options
->flags
.quick
&& options
->skip_stat_unmatch
&&
6984 !diff_filespec_check_stat_unmatch(options
->repo
, p
)) {
6985 diff_free_filespec_data(p
->one
);
6986 diff_free_filespec_data(p
->two
);
6990 options
->flags
.has_changes
= 1;
6993 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
6995 struct diff_filepair
*pair
;
6996 struct diff_filespec
*one
, *two
;
6998 if (options
->prefix
&&
6999 strncmp(path
, options
->prefix
, options
->prefix_length
))
7002 one
= alloc_filespec(path
);
7003 two
= alloc_filespec(path
);
7004 pair
= diff_queue(&diff_queued_diff
, one
, two
);
7005 pair
->is_unmerged
= 1;
7009 static char *run_textconv(struct repository
*r
,
7011 struct diff_filespec
*spec
,
7014 struct diff_tempfile
*temp
;
7015 struct child_process child
= CHILD_PROCESS_INIT
;
7016 struct strbuf buf
= STRBUF_INIT
;
7019 temp
= prepare_temp_file(r
, spec
->path
, spec
);
7020 strvec_push(&child
.args
, pgm
);
7021 strvec_push(&child
.args
, temp
->name
);
7023 child
.use_shell
= 1;
7025 if (start_command(&child
)) {
7030 if (strbuf_read(&buf
, child
.out
, 0) < 0)
7031 err
= error("error reading from textconv command '%s'", pgm
);
7034 if (finish_command(&child
) || err
) {
7035 strbuf_release(&buf
);
7041 return strbuf_detach(&buf
, outsize
);
7044 size_t fill_textconv(struct repository
*r
,
7045 struct userdiff_driver
*driver
,
7046 struct diff_filespec
*df
,
7052 if (!DIFF_FILE_VALID(df
)) {
7056 if (diff_populate_filespec(r
, df
, NULL
))
7057 die("unable to read files to diff");
7062 if (!driver
->textconv
)
7063 BUG("fill_textconv called with non-textconv driver");
7065 if (driver
->textconv_cache
&& df
->oid_valid
) {
7066 *outbuf
= notes_cache_get(driver
->textconv_cache
,
7073 *outbuf
= run_textconv(r
, driver
->textconv
, df
, &size
);
7075 die("unable to read files to diff");
7077 if (driver
->textconv_cache
&& df
->oid_valid
) {
7078 /* ignore errors, as we might be in a readonly repository */
7079 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
7082 * we could save up changes and flush them all at the end,
7083 * but we would need an extra call after all diffing is done.
7084 * Since generating a cache entry is the slow path anyway,
7085 * this extra overhead probably isn't a big deal.
7087 notes_cache_write(driver
->textconv_cache
);
7093 int textconv_object(struct repository
*r
,
7096 const struct object_id
*oid
,
7099 unsigned long *buf_size
)
7101 struct diff_filespec
*df
;
7102 struct userdiff_driver
*textconv
;
7104 df
= alloc_filespec(path
);
7105 fill_filespec(df
, oid
, oid_valid
, mode
);
7106 textconv
= get_textconv(r
, df
);
7112 *buf_size
= fill_textconv(r
, textconv
, df
, buf
);
7117 void setup_diff_pager(struct diff_options
*opt
)
7120 * If the user asked for our exit code, then either they want --quiet
7121 * or --exit-code. We should definitely not bother with a pager in the
7122 * former case, as we will generate no output. Since we still properly
7123 * report our exit code even when a pager is run, we _could_ run a
7124 * pager with --exit-code. But since we have not done so historically,
7125 * and because it is easy to find people oneline advising "git diff
7126 * --exit-code" in hooks and other scripts, we do not do so.
7128 if (!opt
->flags
.exit_with_status
&&
7129 check_pager_config("diff") != 0)