2 * Copyright (C) 2005 Junio C Hamano
5 #define USE_THE_REPOSITORY_VARIABLE
7 #include "git-compat-util.h"
12 #include "environment.h"
21 #include "xdiff-interface.h"
23 #include "run-command.h"
25 #include "object-store-ll.h"
27 #include "submodule.h"
31 #include "string-list.h"
33 #include "tmp-objdir.h"
35 #include "oid-array.h"
38 #include "parse-options.h"
40 #include "promisor-remote.h"
42 #include "object-file.h"
43 #include "object-name.h"
44 #include "read-cache-ll.h"
49 #ifdef NO_FAST_WORKING_DIRECTORY
50 #define FAST_WORKING_DIRECTORY 0
52 #define FAST_WORKING_DIRECTORY 1
55 static int diff_detect_rename_default
;
56 static int diff_indent_heuristic
= 1;
57 static int diff_rename_limit_default
= 1000;
58 static int diff_suppress_blank_empty
;
59 static int diff_use_color_default
= -1;
60 static int diff_color_moved_default
;
61 static int diff_color_moved_ws_default
;
62 static int diff_context_default
= 3;
63 static int diff_interhunk_context_default
;
64 static char *diff_word_regex_cfg
;
65 static struct external_diff external_diff_cfg
;
66 static char *diff_order_file_cfg
;
67 int diff_auto_refresh_index
= 1;
68 static int diff_mnemonic_prefix
;
69 static int diff_no_prefix
;
70 static char *diff_src_prefix
;
71 static char *diff_dst_prefix
;
72 static int diff_relative
;
73 static int diff_stat_name_width
;
74 static int diff_stat_graph_width
;
75 static int diff_dirstat_permille_default
= 30;
76 static struct diff_options default_diff_options
;
77 static long diff_algorithm
;
78 static unsigned ws_error_highlight_default
= WSEH_NEW
;
80 static char diff_colors
[][COLOR_MAXLEN
] = {
82 GIT_COLOR_NORMAL
, /* CONTEXT */
83 GIT_COLOR_BOLD
, /* METAINFO */
84 GIT_COLOR_CYAN
, /* FRAGINFO */
85 GIT_COLOR_RED
, /* OLD */
86 GIT_COLOR_GREEN
, /* NEW */
87 GIT_COLOR_YELLOW
, /* COMMIT */
88 GIT_COLOR_BG_RED
, /* WHITESPACE */
89 GIT_COLOR_NORMAL
, /* FUNCINFO */
90 GIT_COLOR_BOLD_MAGENTA
, /* OLD_MOVED */
91 GIT_COLOR_BOLD_BLUE
, /* OLD_MOVED ALTERNATIVE */
92 GIT_COLOR_FAINT
, /* OLD_MOVED_DIM */
93 GIT_COLOR_FAINT_ITALIC
, /* OLD_MOVED_ALTERNATIVE_DIM */
94 GIT_COLOR_BOLD_CYAN
, /* NEW_MOVED */
95 GIT_COLOR_BOLD_YELLOW
, /* NEW_MOVED ALTERNATIVE */
96 GIT_COLOR_FAINT
, /* NEW_MOVED_DIM */
97 GIT_COLOR_FAINT_ITALIC
, /* NEW_MOVED_ALTERNATIVE_DIM */
98 GIT_COLOR_FAINT
, /* CONTEXT_DIM */
99 GIT_COLOR_FAINT_RED
, /* OLD_DIM */
100 GIT_COLOR_FAINT_GREEN
, /* NEW_DIM */
101 GIT_COLOR_BOLD
, /* CONTEXT_BOLD */
102 GIT_COLOR_BOLD_RED
, /* OLD_BOLD */
103 GIT_COLOR_BOLD_GREEN
, /* NEW_BOLD */
106 static const char *color_diff_slots
[] = {
107 [DIFF_CONTEXT
] = "context",
108 [DIFF_METAINFO
] = "meta",
109 [DIFF_FRAGINFO
] = "frag",
110 [DIFF_FILE_OLD
] = "old",
111 [DIFF_FILE_NEW
] = "new",
112 [DIFF_COMMIT
] = "commit",
113 [DIFF_WHITESPACE
] = "whitespace",
114 [DIFF_FUNCINFO
] = "func",
115 [DIFF_FILE_OLD_MOVED
] = "oldMoved",
116 [DIFF_FILE_OLD_MOVED_ALT
] = "oldMovedAlternative",
117 [DIFF_FILE_OLD_MOVED_DIM
] = "oldMovedDimmed",
118 [DIFF_FILE_OLD_MOVED_ALT_DIM
] = "oldMovedAlternativeDimmed",
119 [DIFF_FILE_NEW_MOVED
] = "newMoved",
120 [DIFF_FILE_NEW_MOVED_ALT
] = "newMovedAlternative",
121 [DIFF_FILE_NEW_MOVED_DIM
] = "newMovedDimmed",
122 [DIFF_FILE_NEW_MOVED_ALT_DIM
] = "newMovedAlternativeDimmed",
123 [DIFF_CONTEXT_DIM
] = "contextDimmed",
124 [DIFF_FILE_OLD_DIM
] = "oldDimmed",
125 [DIFF_FILE_NEW_DIM
] = "newDimmed",
126 [DIFF_CONTEXT_BOLD
] = "contextBold",
127 [DIFF_FILE_OLD_BOLD
] = "oldBold",
128 [DIFF_FILE_NEW_BOLD
] = "newBold",
131 define_list_config_array_extra(color_diff_slots
, {"plain"});
133 static int parse_diff_color_slot(const char *var
)
135 if (!strcasecmp(var
, "plain"))
137 return LOOKUP_CONFIG(color_diff_slots
, var
);
140 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
141 struct strbuf
*errmsg
)
143 char *params_copy
= xstrdup(params_string
);
144 struct string_list params
= STRING_LIST_INIT_NODUP
;
149 string_list_split_in_place(¶ms
, params_copy
, ",", -1);
150 for (i
= 0; i
< params
.nr
; i
++) {
151 const char *p
= params
.items
[i
].string
;
152 if (!strcmp(p
, "changes")) {
153 options
->flags
.dirstat_by_line
= 0;
154 options
->flags
.dirstat_by_file
= 0;
155 } else if (!strcmp(p
, "lines")) {
156 options
->flags
.dirstat_by_line
= 1;
157 options
->flags
.dirstat_by_file
= 0;
158 } else if (!strcmp(p
, "files")) {
159 options
->flags
.dirstat_by_line
= 0;
160 options
->flags
.dirstat_by_file
= 1;
161 } else if (!strcmp(p
, "noncumulative")) {
162 options
->flags
.dirstat_cumulative
= 0;
163 } else if (!strcmp(p
, "cumulative")) {
164 options
->flags
.dirstat_cumulative
= 1;
165 } else if (isdigit(*p
)) {
167 int permille
= strtoul(p
, &end
, 10) * 10;
168 if (*end
== '.' && isdigit(*++end
)) {
169 /* only use first digit */
170 permille
+= *end
- '0';
171 /* .. and ignore any further digits */
172 while (isdigit(*++end
))
176 options
->dirstat_permille
= permille
;
178 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
183 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
188 string_list_clear(¶ms
, 0);
193 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
195 if (!strcmp(value
, "log"))
196 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
197 else if (!strcmp(value
, "short"))
198 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
199 else if (!strcmp(value
, "diff"))
200 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
202 * Please update $__git_diff_submodule_formats in
203 * git-completion.bash when you add new formats.
210 int git_config_rename(const char *var
, const char *value
)
213 return DIFF_DETECT_RENAME
;
214 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
215 return DIFF_DETECT_COPY
;
216 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
219 long parse_algorithm_value(const char *value
)
223 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
225 else if (!strcasecmp(value
, "minimal"))
226 return XDF_NEED_MINIMAL
;
227 else if (!strcasecmp(value
, "patience"))
228 return XDF_PATIENCE_DIFF
;
229 else if (!strcasecmp(value
, "histogram"))
230 return XDF_HISTOGRAM_DIFF
;
232 * Please update $__git_diff_algorithms in git-completion.bash
233 * when you add new algorithms.
238 static int parse_one_token(const char **arg
, const char *token
)
241 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
248 static int parse_ws_error_highlight(const char *arg
)
250 const char *orig_arg
= arg
;
254 if (parse_one_token(&arg
, "none"))
256 else if (parse_one_token(&arg
, "default"))
258 else if (parse_one_token(&arg
, "all"))
259 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
260 else if (parse_one_token(&arg
, "new"))
262 else if (parse_one_token(&arg
, "old"))
264 else if (parse_one_token(&arg
, "context"))
267 return -1 - (int)(arg
- orig_arg
);
276 * These are to give UI layer defaults.
277 * The core-level commands such as git-diff-files should
278 * never be affected by the setting of diff.renames
279 * the user happens to have in the configuration file.
281 void init_diff_ui_defaults(void)
283 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
286 int git_diff_heuristic_config(const char *var
, const char *value
,
289 if (!strcmp(var
, "diff.indentheuristic"))
290 diff_indent_heuristic
= git_config_bool(var
, value
);
294 static int parse_color_moved(const char *arg
)
296 switch (git_parse_maybe_bool(arg
)) {
298 return COLOR_MOVED_NO
;
300 return COLOR_MOVED_DEFAULT
;
305 if (!strcmp(arg
, "no"))
306 return COLOR_MOVED_NO
;
307 else if (!strcmp(arg
, "plain"))
308 return COLOR_MOVED_PLAIN
;
309 else if (!strcmp(arg
, "blocks"))
310 return COLOR_MOVED_BLOCKS
;
311 else if (!strcmp(arg
, "zebra"))
312 return COLOR_MOVED_ZEBRA
;
313 else if (!strcmp(arg
, "default"))
314 return COLOR_MOVED_DEFAULT
;
315 else if (!strcmp(arg
, "dimmed-zebra"))
316 return COLOR_MOVED_ZEBRA_DIM
;
317 else if (!strcmp(arg
, "dimmed_zebra"))
318 return COLOR_MOVED_ZEBRA_DIM
;
320 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
323 static unsigned parse_color_moved_ws(const char *arg
)
326 struct string_list l
= STRING_LIST_INIT_DUP
;
327 struct string_list_item
*i
;
329 string_list_split(&l
, arg
, ',', -1);
331 for_each_string_list_item(i
, &l
) {
332 struct strbuf sb
= STRBUF_INIT
;
333 strbuf_addstr(&sb
, i
->string
);
336 if (!strcmp(sb
.buf
, "no"))
338 else if (!strcmp(sb
.buf
, "ignore-space-change"))
339 ret
|= XDF_IGNORE_WHITESPACE_CHANGE
;
340 else if (!strcmp(sb
.buf
, "ignore-space-at-eol"))
341 ret
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
342 else if (!strcmp(sb
.buf
, "ignore-all-space"))
343 ret
|= XDF_IGNORE_WHITESPACE
;
344 else if (!strcmp(sb
.buf
, "allow-indentation-change"))
345 ret
|= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
;
347 ret
|= COLOR_MOVED_WS_ERROR
;
348 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
);
354 if ((ret
& COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
) &&
355 (ret
& XDF_WHITESPACE_FLAGS
)) {
356 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
357 ret
|= COLOR_MOVED_WS_ERROR
;
360 string_list_clear(&l
, 0);
365 int git_diff_ui_config(const char *var
, const char *value
,
366 const struct config_context
*ctx
, void *cb
)
368 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
369 diff_use_color_default
= git_config_colorbool(var
, value
);
372 if (!strcmp(var
, "diff.colormoved")) {
373 int cm
= parse_color_moved(value
);
376 diff_color_moved_default
= cm
;
379 if (!strcmp(var
, "diff.colormovedws")) {
382 return config_error_nonbool(var
);
383 cm
= parse_color_moved_ws(value
);
384 if (cm
& COLOR_MOVED_WS_ERROR
)
386 diff_color_moved_ws_default
= cm
;
389 if (!strcmp(var
, "diff.context")) {
390 diff_context_default
= git_config_int(var
, value
, ctx
->kvi
);
391 if (diff_context_default
< 0)
395 if (!strcmp(var
, "diff.interhunkcontext")) {
396 diff_interhunk_context_default
= git_config_int(var
, value
,
398 if (diff_interhunk_context_default
< 0)
402 if (!strcmp(var
, "diff.renames")) {
403 diff_detect_rename_default
= git_config_rename(var
, value
);
406 if (!strcmp(var
, "diff.autorefreshindex")) {
407 diff_auto_refresh_index
= git_config_bool(var
, value
);
410 if (!strcmp(var
, "diff.mnemonicprefix")) {
411 diff_mnemonic_prefix
= git_config_bool(var
, value
);
414 if (!strcmp(var
, "diff.noprefix")) {
415 diff_no_prefix
= git_config_bool(var
, value
);
418 if (!strcmp(var
, "diff.srcprefix")) {
419 FREE_AND_NULL(diff_src_prefix
);
420 return git_config_string(&diff_src_prefix
, var
, value
);
422 if (!strcmp(var
, "diff.dstprefix")) {
423 FREE_AND_NULL(diff_dst_prefix
);
424 return git_config_string(&diff_dst_prefix
, var
, value
);
426 if (!strcmp(var
, "diff.relative")) {
427 diff_relative
= git_config_bool(var
, value
);
430 if (!strcmp(var
, "diff.statnamewidth")) {
431 diff_stat_name_width
= git_config_int(var
, value
, ctx
->kvi
);
434 if (!strcmp(var
, "diff.statgraphwidth")) {
435 diff_stat_graph_width
= git_config_int(var
, value
, ctx
->kvi
);
438 if (!strcmp(var
, "diff.external"))
439 return git_config_string(&external_diff_cfg
.cmd
, var
, value
);
440 if (!strcmp(var
, "diff.trustexitcode")) {
441 external_diff_cfg
.trust_exit_code
= git_config_bool(var
, value
);
444 if (!strcmp(var
, "diff.wordregex"))
445 return git_config_string(&diff_word_regex_cfg
, var
, value
);
446 if (!strcmp(var
, "diff.orderfile"))
447 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
449 if (!strcmp(var
, "diff.ignoresubmodules")) {
451 return config_error_nonbool(var
);
452 handle_ignore_submodules_arg(&default_diff_options
, value
);
455 if (!strcmp(var
, "diff.submodule")) {
457 return config_error_nonbool(var
);
458 if (parse_submodule_params(&default_diff_options
, value
))
459 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
464 if (!strcmp(var
, "diff.algorithm")) {
466 return config_error_nonbool(var
);
467 diff_algorithm
= parse_algorithm_value(value
);
468 if (diff_algorithm
< 0)
469 return error(_("unknown value for config '%s': %s"),
474 if (git_color_config(var
, value
, cb
) < 0)
477 return git_diff_basic_config(var
, value
, ctx
, cb
);
480 int git_diff_basic_config(const char *var
, const char *value
,
481 const struct config_context
*ctx
, void *cb
)
485 if (!strcmp(var
, "diff.renamelimit")) {
486 diff_rename_limit_default
= git_config_int(var
, value
, ctx
->kvi
);
490 if (userdiff_config(var
, value
) < 0)
493 if (skip_prefix(var
, "diff.color.", &name
) ||
494 skip_prefix(var
, "color.diff.", &name
)) {
495 int slot
= parse_diff_color_slot(name
);
499 return config_error_nonbool(var
);
500 return color_parse(value
, diff_colors
[slot
]);
503 if (!strcmp(var
, "diff.wserrorhighlight")) {
506 return config_error_nonbool(var
);
507 val
= parse_ws_error_highlight(value
);
509 return error(_("unknown value for config '%s': %s"),
511 ws_error_highlight_default
= val
;
515 /* like GNU diff's --suppress-blank-empty option */
516 if (!strcmp(var
, "diff.suppressblankempty") ||
517 /* for backwards compatibility */
518 !strcmp(var
, "diff.suppress-blank-empty")) {
519 diff_suppress_blank_empty
= git_config_bool(var
, value
);
523 if (!strcmp(var
, "diff.dirstat")) {
524 struct strbuf errmsg
= STRBUF_INIT
;
526 return config_error_nonbool(var
);
527 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
528 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
529 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
531 strbuf_release(&errmsg
);
532 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
536 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
539 return git_default_config(var
, value
, ctx
, cb
);
542 static char *quote_two(const char *one
, const char *two
)
544 int need_one
= quote_c_style(one
, NULL
, NULL
, CQUOTE_NODQ
);
545 int need_two
= quote_c_style(two
, NULL
, NULL
, CQUOTE_NODQ
);
546 struct strbuf res
= STRBUF_INIT
;
548 if (need_one
+ need_two
) {
549 strbuf_addch(&res
, '"');
550 quote_c_style(one
, &res
, NULL
, CQUOTE_NODQ
);
551 quote_c_style(two
, &res
, NULL
, CQUOTE_NODQ
);
552 strbuf_addch(&res
, '"');
554 strbuf_addstr(&res
, one
);
555 strbuf_addstr(&res
, two
);
557 return strbuf_detach(&res
, NULL
);
560 static const struct external_diff
*external_diff(void)
562 static struct external_diff external_diff_env
, *external_diff_ptr
;
563 static int done_preparing
= 0;
566 return external_diff_ptr
;
567 external_diff_env
.cmd
= xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
568 if (git_env_bool("GIT_EXTERNAL_DIFF_TRUST_EXIT_CODE", 0))
569 external_diff_env
.trust_exit_code
= 1;
570 if (external_diff_env
.cmd
)
571 external_diff_ptr
= &external_diff_env
;
572 else if (external_diff_cfg
.cmd
)
573 external_diff_ptr
= &external_diff_cfg
;
575 return external_diff_ptr
;
579 * Keep track of files used for diffing. Sometimes such an entry
580 * refers to a temporary file, sometimes to an existing file, and
581 * sometimes to "/dev/null".
583 static struct diff_tempfile
{
585 * filename external diff should read from, or NULL if this
586 * entry is currently not in use:
590 char hex
[GIT_MAX_HEXSZ
+ 1];
594 * If this diff_tempfile instance refers to a temporary file,
595 * this tempfile object is used to manage its lifetime.
597 struct tempfile
*tempfile
;
600 struct emit_callback
{
603 int blank_at_eof_in_preimage
;
604 int blank_at_eof_in_postimage
;
606 int lno_in_postimage
;
607 const char **label_path
;
608 struct diff_words_data
*diff_words
;
609 struct diff_options
*opt
;
610 struct strbuf
*header
;
613 static int count_lines(const char *data
, int size
)
615 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
622 completely_empty
= 0;
626 completely_empty
= 0;
629 if (completely_empty
)
632 count
++; /* no trailing newline */
636 static int fill_mmfile(struct repository
*r
, mmfile_t
*mf
,
637 struct diff_filespec
*one
)
639 if (!DIFF_FILE_VALID(one
)) {
640 mf
->ptr
= (char *)""; /* does not matter */
644 else if (diff_populate_filespec(r
, one
, NULL
))
648 mf
->size
= one
->size
;
652 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
653 static unsigned long diff_filespec_size(struct repository
*r
,
654 struct diff_filespec
*one
)
656 struct diff_populate_filespec_options dpf_options
= {
657 .check_size_only
= 1,
660 if (!DIFF_FILE_VALID(one
))
662 diff_populate_filespec(r
, one
, &dpf_options
);
666 static int count_trailing_blank(mmfile_t
*mf
)
669 long size
= mf
->size
;
674 ptr
+= size
- 1; /* pointing at the very end */
676 ; /* incomplete line */
678 ptr
--; /* skip the last LF */
679 while (mf
->ptr
< ptr
) {
681 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
682 if (*prev_eol
== '\n')
684 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
))
692 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
693 struct emit_callback
*ecbdata
)
696 l1
= count_trailing_blank(mf1
);
697 l2
= count_trailing_blank(mf2
);
699 ecbdata
->blank_at_eof_in_preimage
= 0;
700 ecbdata
->blank_at_eof_in_postimage
= 0;
703 at
= count_lines(mf1
->ptr
, mf1
->size
);
704 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
706 at
= count_lines(mf2
->ptr
, mf2
->size
);
707 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
710 static void emit_line_0(struct diff_options
*o
,
711 const char *set_sign
, const char *set
, unsigned reverse
, const char *reset
,
712 int first
, const char *line
, int len
)
714 int has_trailing_newline
, has_trailing_carriage_return
;
715 int needs_reset
= 0; /* at the end of the line */
716 FILE *file
= o
->file
;
718 fputs(diff_line_prefix(o
), file
);
720 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
721 if (has_trailing_newline
)
724 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
725 if (has_trailing_carriage_return
)
731 if (reverse
&& want_color(o
->use_color
)) {
732 fputs(GIT_COLOR_REVERSE
, file
);
737 fputs(set_sign
, file
);
748 if (set_sign
&& set
!= set_sign
)
753 fwrite(line
, len
, 1, file
);
754 needs_reset
= 1; /* 'line' may contain color codes. */
759 if (has_trailing_carriage_return
)
761 if (has_trailing_newline
)
765 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
766 const char *line
, int len
)
768 emit_line_0(o
, set
, NULL
, 0, reset
, 0, line
, len
);
772 DIFF_SYMBOL_BINARY_DIFF_HEADER
,
773 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
774 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
775 DIFF_SYMBOL_BINARY_DIFF_BODY
,
776 DIFF_SYMBOL_BINARY_DIFF_FOOTER
,
777 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
778 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
779 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
780 DIFF_SYMBOL_STATS_LINE
,
781 DIFF_SYMBOL_WORD_DIFF
,
782 DIFF_SYMBOL_STAT_SEP
,
784 DIFF_SYMBOL_SUBMODULE_ADD
,
785 DIFF_SYMBOL_SUBMODULE_DEL
,
786 DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
787 DIFF_SYMBOL_SUBMODULE_MODIFIED
,
788 DIFF_SYMBOL_SUBMODULE_HEADER
,
789 DIFF_SYMBOL_SUBMODULE_ERROR
,
790 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
,
791 DIFF_SYMBOL_REWRITE_DIFF
,
792 DIFF_SYMBOL_BINARY_FILES
,
794 DIFF_SYMBOL_FILEPAIR_PLUS
,
795 DIFF_SYMBOL_FILEPAIR_MINUS
,
796 DIFF_SYMBOL_WORDS_PORCELAIN
,
799 DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
802 DIFF_SYMBOL_NO_LF_EOF
,
803 DIFF_SYMBOL_CONTEXT_FRAGINFO
,
804 DIFF_SYMBOL_CONTEXT_MARKER
,
805 DIFF_SYMBOL_SEPARATOR
808 * Flags for content lines:
809 * 0..12 are whitespace rules
810 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
811 * 16 is marking if the line is blank at EOF
813 #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
814 #define DIFF_SYMBOL_MOVED_LINE (1<<17)
815 #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
816 #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
817 #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
820 * This struct is used when we need to buffer the output of the diff output.
822 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
823 * into the pre/post image file. This pointer could be a union with the
824 * line pointer. By storing an offset into the file instead of the literal line,
825 * we can decrease the memory footprint for the buffered output. At first we
826 * may want to only have indirection for the content lines, but we could also
827 * enhance the state for emitting prefabricated lines, e.g. the similarity
828 * score line or hunk/file headers would only need to store a number or path
829 * and then the output can be constructed later on depending on state.
831 struct emitted_diff_symbol
{
835 int indent_off
; /* Offset to first non-whitespace character */
836 int indent_width
; /* The visual width of the indentation */
840 #define EMITTED_DIFF_SYMBOL_INIT { 0 }
842 struct emitted_diff_symbols
{
843 struct emitted_diff_symbol
*buf
;
846 #define EMITTED_DIFF_SYMBOLS_INIT { 0 }
848 static void append_emitted_diff_symbol(struct diff_options
*o
,
849 struct emitted_diff_symbol
*e
)
851 struct emitted_diff_symbol
*f
;
853 ALLOC_GROW(o
->emitted_symbols
->buf
,
854 o
->emitted_symbols
->nr
+ 1,
855 o
->emitted_symbols
->alloc
);
856 f
= &o
->emitted_symbols
->buf
[o
->emitted_symbols
->nr
++];
858 memcpy(f
, e
, sizeof(struct emitted_diff_symbol
));
859 f
->line
= e
->line
? xmemdupz(e
->line
, e
->len
) : NULL
;
862 static void free_emitted_diff_symbols(struct emitted_diff_symbols
*e
)
871 const struct emitted_diff_symbol
*es
;
872 struct moved_entry
*next_line
;
873 struct moved_entry
*next_match
;
877 struct moved_entry
*match
;
878 int wsd
; /* The whitespace delta of this block */
881 #define INDENT_BLANKLINE INT_MIN
883 static void fill_es_indent_data(struct emitted_diff_symbol
*es
)
885 unsigned int off
= 0, i
;
886 int width
= 0, tab_width
= es
->flags
& WS_TAB_WIDTH_MASK
;
887 const char *s
= es
->line
;
888 const int len
= es
->len
;
890 /* skip any \v \f \r at start of indentation */
891 while (s
[off
] == '\f' || s
[off
] == '\v' ||
892 (s
[off
] == '\r' && off
< len
- 1))
895 /* calculate the visual width of indentation */
900 } else if (s
[off
] == '\t') {
901 width
+= tab_width
- (width
% tab_width
);
902 while (s
[++off
] == '\t')
909 /* check if this line is blank */
910 for (i
= off
; i
< len
; i
++)
915 es
->indent_width
= INDENT_BLANKLINE
;
916 es
->indent_off
= len
;
918 es
->indent_off
= off
;
919 es
->indent_width
= width
;
923 static int compute_ws_delta(const struct emitted_diff_symbol
*a
,
924 const struct emitted_diff_symbol
*b
)
926 int a_width
= a
->indent_width
,
927 b_width
= b
->indent_width
;
929 if (a_width
== INDENT_BLANKLINE
&& b_width
== INDENT_BLANKLINE
)
930 return INDENT_BLANKLINE
;
932 return a_width
- b_width
;
935 static int cmp_in_block_with_wsd(const struct moved_entry
*cur
,
936 const struct emitted_diff_symbol
*l
,
937 struct moved_block
*pmb
)
939 int a_width
= cur
->es
->indent_width
, b_width
= l
->indent_width
;
942 /* The text of each line must match */
943 if (cur
->es
->id
!= l
->id
)
947 * If 'l' and 'cur' are both blank then we don't need to check the
948 * indent. We only need to check cur as we know the strings match.
950 if (a_width
== INDENT_BLANKLINE
)
954 * The indent changes of the block are known and stored in pmb->wsd;
955 * however we need to check if the indent changes of the current line
956 * match those of the current block.
958 delta
= b_width
- a_width
;
961 * If the previous lines of this block were all blank then set its
964 if (pmb
->wsd
== INDENT_BLANKLINE
)
967 return delta
!= pmb
->wsd
;
970 struct interned_diff_symbol
{
971 struct hashmap_entry ent
;
972 struct emitted_diff_symbol
*es
;
975 static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data
,
976 const struct hashmap_entry
*eptr
,
977 const struct hashmap_entry
*entry_or_key
,
978 const void *keydata UNUSED
)
980 const struct diff_options
*diffopt
= hashmap_cmp_fn_data
;
981 const struct emitted_diff_symbol
*a
, *b
;
982 unsigned flags
= diffopt
->color_moved_ws_handling
983 & XDF_WHITESPACE_FLAGS
;
985 a
= container_of(eptr
, const struct interned_diff_symbol
, ent
)->es
;
986 b
= container_of(entry_or_key
, const struct interned_diff_symbol
, ent
)->es
;
988 return !xdiff_compare_lines(a
->line
+ a
->indent_off
,
989 a
->len
- a
->indent_off
,
990 b
->line
+ b
->indent_off
,
991 b
->len
- b
->indent_off
, flags
);
994 static void prepare_entry(struct diff_options
*o
, struct emitted_diff_symbol
*l
,
995 struct interned_diff_symbol
*s
)
997 unsigned flags
= o
->color_moved_ws_handling
& XDF_WHITESPACE_FLAGS
;
998 unsigned int hash
= xdiff_hash_string(l
->line
+ l
->indent_off
,
999 l
->len
- l
->indent_off
, flags
);
1001 hashmap_entry_init(&s
->ent
, hash
);
1005 struct moved_entry_list
{
1006 struct moved_entry
*add
, *del
;
1009 static struct moved_entry_list
*add_lines_to_move_detection(struct diff_options
*o
,
1010 struct mem_pool
*entry_mem_pool
)
1012 struct moved_entry
*prev_line
= NULL
;
1013 struct mem_pool interned_pool
;
1014 struct hashmap interned_map
;
1015 struct moved_entry_list
*entry_list
= NULL
;
1016 size_t entry_list_alloc
= 0;
1020 hashmap_init(&interned_map
, interned_diff_symbol_cmp
, o
, 8096);
1021 mem_pool_init(&interned_pool
, 1024 * 1024);
1023 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1024 struct interned_diff_symbol key
;
1025 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1026 struct interned_diff_symbol
*s
;
1027 struct moved_entry
*entry
;
1029 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
) {
1034 if (o
->color_moved_ws_handling
&
1035 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1036 fill_es_indent_data(l
);
1038 prepare_entry(o
, l
, &key
);
1039 s
= hashmap_get_entry(&interned_map
, &key
, ent
, &key
.ent
);
1044 ALLOC_GROW_BY(entry_list
, id
, 1, entry_list_alloc
);
1045 hashmap_add(&interned_map
,
1046 memcpy(mem_pool_alloc(&interned_pool
,
1048 &key
, sizeof(key
)));
1050 entry
= mem_pool_alloc(entry_mem_pool
, sizeof(*entry
));
1052 entry
->next_line
= NULL
;
1053 if (prev_line
&& prev_line
->es
->s
== l
->s
)
1054 prev_line
->next_line
= entry
;
1056 if (l
->s
== DIFF_SYMBOL_PLUS
) {
1057 entry
->next_match
= entry_list
[l
->id
].add
;
1058 entry_list
[l
->id
].add
= entry
;
1060 entry
->next_match
= entry_list
[l
->id
].del
;
1061 entry_list
[l
->id
].del
= entry
;
1065 hashmap_clear(&interned_map
);
1066 mem_pool_discard(&interned_pool
, 0);
1071 static void pmb_advance_or_null(struct diff_options
*o
,
1072 struct emitted_diff_symbol
*l
,
1073 struct moved_block
*pmb
,
1078 for (i
= 0, j
= 0; i
< *pmb_nr
; i
++) {
1080 struct moved_entry
*prev
= pmb
[i
].match
;
1081 struct moved_entry
*cur
= (prev
&& prev
->next_line
) ?
1082 prev
->next_line
: NULL
;
1084 if (o
->color_moved_ws_handling
&
1085 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1087 !cmp_in_block_with_wsd(cur
, l
, &pmb
[i
]);
1089 match
= cur
&& cur
->es
->id
== l
->id
;
1093 pmb
[j
++].match
= cur
;
1099 static void fill_potential_moved_blocks(struct diff_options
*o
,
1100 struct moved_entry
*match
,
1101 struct emitted_diff_symbol
*l
,
1102 struct moved_block
**pmb_p
,
1103 int *pmb_alloc_p
, int *pmb_nr_p
)
1106 struct moved_block
*pmb
= *pmb_p
;
1107 int pmb_alloc
= *pmb_alloc_p
, pmb_nr
= *pmb_nr_p
;
1110 * The current line is the start of a new block.
1111 * Setup the set of potential blocks.
1113 for (; match
; match
= match
->next_match
) {
1114 ALLOC_GROW(pmb
, pmb_nr
+ 1, pmb_alloc
);
1115 if (o
->color_moved_ws_handling
&
1116 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1117 pmb
[pmb_nr
].wsd
= compute_ws_delta(l
, match
->es
);
1119 pmb
[pmb_nr
].wsd
= 0;
1120 pmb
[pmb_nr
++].match
= match
;
1124 *pmb_alloc_p
= pmb_alloc
;
1129 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1131 * Otherwise, if the last block has fewer alphanumeric characters than
1132 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
1135 * The last block consists of the (n - block_length)'th line up to but not
1136 * including the nth line.
1138 * Returns 0 if the last block is empty or is unset by this function, non zero
1141 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1142 * Think of a way to unify them.
1144 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1145 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
1146 static int adjust_last_block(struct diff_options
*o
, int n
, int block_length
)
1148 int i
, alnum_count
= 0;
1149 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
1150 return block_length
;
1151 for (i
= 1; i
< block_length
+ 1; i
++) {
1152 const char *c
= o
->emitted_symbols
->buf
[n
- i
].line
;
1157 if (alnum_count
>= COLOR_MOVED_MIN_ALNUM_COUNT
)
1161 for (i
= 1; i
< block_length
+ 1; i
++)
1162 o
->emitted_symbols
->buf
[n
- i
].flags
&= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
;
1166 /* Find blocks of moved code, delegate actual coloring decision to helper */
1167 static void mark_color_as_moved(struct diff_options
*o
,
1168 struct moved_entry_list
*entry_list
)
1170 struct moved_block
*pmb
= NULL
; /* potentially moved blocks */
1171 int pmb_nr
= 0, pmb_alloc
= 0;
1172 int n
, flipped_block
= 0, block_length
= 0;
1173 enum diff_symbol moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1176 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1177 struct moved_entry
*match
= NULL
;
1178 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1181 case DIFF_SYMBOL_PLUS
:
1182 match
= entry_list
[l
->id
].del
;
1184 case DIFF_SYMBOL_MINUS
:
1185 match
= entry_list
[l
->id
].add
;
1191 if (pmb_nr
&& (!match
|| l
->s
!= moved_symbol
)) {
1192 if (!adjust_last_block(o
, n
, block_length
) &&
1195 * Rewind in case there is another match
1196 * starting at the second line of the block
1206 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1210 if (o
->color_moved
== COLOR_MOVED_PLAIN
) {
1211 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1215 pmb_advance_or_null(o
, l
, pmb
, &pmb_nr
);
1218 int contiguous
= adjust_last_block(o
, n
, block_length
);
1220 if (!contiguous
&& block_length
> 1)
1222 * Rewind in case there is another match
1223 * starting at the second line of the block
1227 fill_potential_moved_blocks(o
, match
, l
,
1231 if (contiguous
&& pmb_nr
&& moved_symbol
== l
->s
)
1232 flipped_block
= (flipped_block
+ 1) % 2;
1237 moved_symbol
= l
->s
;
1239 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1246 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1247 if (flipped_block
&& o
->color_moved
!= COLOR_MOVED_BLOCKS
)
1248 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_ALT
;
1251 adjust_last_block(o
, n
, block_length
);
1256 static void dim_moved_lines(struct diff_options
*o
)
1259 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1260 struct emitted_diff_symbol
*prev
= (n
!= 0) ?
1261 &o
->emitted_symbols
->buf
[n
- 1] : NULL
;
1262 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1263 struct emitted_diff_symbol
*next
=
1264 (n
< o
->emitted_symbols
->nr
- 1) ?
1265 &o
->emitted_symbols
->buf
[n
+ 1] : NULL
;
1267 /* Not a plus or minus line? */
1268 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
)
1271 /* Not a moved line? */
1272 if (!(l
->flags
& DIFF_SYMBOL_MOVED_LINE
))
1276 * If prev or next are not a plus or minus line,
1277 * pretend they don't exist
1279 if (prev
&& prev
->s
!= DIFF_SYMBOL_PLUS
&&
1280 prev
->s
!= DIFF_SYMBOL_MINUS
)
1282 if (next
&& next
->s
!= DIFF_SYMBOL_PLUS
&&
1283 next
->s
!= DIFF_SYMBOL_MINUS
)
1286 /* Inside a block? */
1288 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1289 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
)) &&
1291 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1292 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
))) {
1293 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1297 /* Check if we are at an interesting bound: */
1298 if (prev
&& (prev
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1299 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1300 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1302 if (next
&& (next
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1303 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1304 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1308 * The boundary to prev and next are not interesting,
1309 * so this line is not interesting as a whole
1311 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1315 static void emit_line_ws_markup(struct diff_options
*o
,
1316 const char *set_sign
, const char *set
,
1318 int sign_index
, const char *line
, int len
,
1319 unsigned ws_rule
, int blank_at_eof
)
1321 const char *ws
= NULL
;
1322 int sign
= o
->output_indicators
[sign_index
];
1324 if (o
->ws_error_highlight
& ws_rule
) {
1325 ws
= diff_get_color_opt(o
, DIFF_WHITESPACE
);
1330 if (!ws
&& !set_sign
)
1331 emit_line_0(o
, set
, NULL
, 0, reset
, sign
, line
, len
);
1333 emit_line_0(o
, set_sign
, set
, !!set_sign
, reset
, sign
, line
, len
);
1334 } else if (blank_at_eof
)
1335 /* Blank line at EOF - paint '+' as well */
1336 emit_line_0(o
, ws
, NULL
, 0, reset
, sign
, line
, len
);
1338 /* Emit just the prefix, then the rest. */
1339 emit_line_0(o
, set_sign
? set_sign
: set
, NULL
, !!set_sign
, reset
,
1341 ws_check_emit(line
, len
, ws_rule
,
1342 o
->file
, set
, reset
, ws
);
1346 static void emit_diff_symbol_from_struct(struct diff_options
*o
,
1347 struct emitted_diff_symbol
*eds
)
1349 static const char *nneof
= " No newline at end of file\n";
1350 const char *context
, *reset
, *set
, *set_sign
, *meta
, *fraginfo
;
1352 enum diff_symbol s
= eds
->s
;
1353 const char *line
= eds
->line
;
1355 unsigned flags
= eds
->flags
;
1358 case DIFF_SYMBOL_NO_LF_EOF
:
1359 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1360 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1361 putc('\n', o
->file
);
1362 emit_line_0(o
, context
, NULL
, 0, reset
, '\\',
1363 nneof
, strlen(nneof
));
1365 case DIFF_SYMBOL_SUBMODULE_HEADER
:
1366 case DIFF_SYMBOL_SUBMODULE_ERROR
:
1367 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
:
1368 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
:
1369 case DIFF_SYMBOL_SUMMARY
:
1370 case DIFF_SYMBOL_STATS_LINE
:
1371 case DIFF_SYMBOL_BINARY_DIFF_BODY
:
1372 case DIFF_SYMBOL_CONTEXT_FRAGINFO
:
1373 emit_line(o
, "", "", line
, len
);
1375 case DIFF_SYMBOL_CONTEXT_INCOMPLETE
:
1376 case DIFF_SYMBOL_CONTEXT_MARKER
:
1377 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1378 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1379 emit_line(o
, context
, reset
, line
, len
);
1381 case DIFF_SYMBOL_SEPARATOR
:
1382 fprintf(o
->file
, "%s%c",
1383 diff_line_prefix(o
),
1384 o
->line_termination
);
1386 case DIFF_SYMBOL_CONTEXT
:
1387 set
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1388 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1390 if (o
->flags
.dual_color_diffed_diffs
) {
1391 char c
= !len
? 0 : line
[0];
1394 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1396 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1398 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1400 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1401 OUTPUT_INDICATOR_CONTEXT
, line
, len
,
1402 flags
& (DIFF_SYMBOL_CONTENT_WS_MASK
), 0);
1404 case DIFF_SYMBOL_PLUS
:
1405 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1406 DIFF_SYMBOL_MOVED_LINE_ALT
|
1407 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1408 case DIFF_SYMBOL_MOVED_LINE
|
1409 DIFF_SYMBOL_MOVED_LINE_ALT
|
1410 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1411 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT_DIM
);
1413 case DIFF_SYMBOL_MOVED_LINE
|
1414 DIFF_SYMBOL_MOVED_LINE_ALT
:
1415 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT
);
1417 case DIFF_SYMBOL_MOVED_LINE
|
1418 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1419 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_DIM
);
1421 case DIFF_SYMBOL_MOVED_LINE
:
1422 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED
);
1425 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1427 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1428 if (!o
->flags
.dual_color_diffed_diffs
)
1431 char c
= !len
? 0 : line
[0];
1435 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_BOLD
);
1437 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1439 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_BOLD
);
1441 set
= diff_get_color_opt(o
, DIFF_CONTEXT_BOLD
);
1442 flags
&= ~DIFF_SYMBOL_CONTENT_WS_MASK
;
1444 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1445 OUTPUT_INDICATOR_NEW
, line
, len
,
1446 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
,
1447 flags
& DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
);
1449 case DIFF_SYMBOL_MINUS
:
1450 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1451 DIFF_SYMBOL_MOVED_LINE_ALT
|
1452 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1453 case DIFF_SYMBOL_MOVED_LINE
|
1454 DIFF_SYMBOL_MOVED_LINE_ALT
|
1455 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1456 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT_DIM
);
1458 case DIFF_SYMBOL_MOVED_LINE
|
1459 DIFF_SYMBOL_MOVED_LINE_ALT
:
1460 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT
);
1462 case DIFF_SYMBOL_MOVED_LINE
|
1463 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1464 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_DIM
);
1466 case DIFF_SYMBOL_MOVED_LINE
:
1467 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED
);
1470 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1472 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1473 if (!o
->flags
.dual_color_diffed_diffs
)
1476 char c
= !len
? 0 : line
[0];
1480 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_DIM
);
1482 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1484 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_DIM
);
1486 set
= diff_get_color_opt(o
, DIFF_CONTEXT_DIM
);
1488 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1489 OUTPUT_INDICATOR_OLD
, line
, len
,
1490 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
, 0);
1492 case DIFF_SYMBOL_WORDS_PORCELAIN
:
1493 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1494 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1495 emit_line(o
, context
, reset
, line
, len
);
1496 fputs("~\n", o
->file
);
1498 case DIFF_SYMBOL_WORDS
:
1499 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1500 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1502 * Skip the prefix character, if any. With
1503 * diff_suppress_blank_empty, there may be
1506 if (line
[0] != '\n') {
1510 emit_line(o
, context
, reset
, line
, len
);
1512 case DIFF_SYMBOL_FILEPAIR_PLUS
:
1513 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1514 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1515 fprintf(o
->file
, "%s%s+++ %s%s%s\n", diff_line_prefix(o
), meta
,
1517 strchr(line
, ' ') ? "\t" : "");
1519 case DIFF_SYMBOL_FILEPAIR_MINUS
:
1520 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1521 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1522 fprintf(o
->file
, "%s%s--- %s%s%s\n", diff_line_prefix(o
), meta
,
1524 strchr(line
, ' ') ? "\t" : "");
1526 case DIFF_SYMBOL_BINARY_FILES
:
1527 case DIFF_SYMBOL_HEADER
:
1528 fprintf(o
->file
, "%s", line
);
1530 case DIFF_SYMBOL_BINARY_DIFF_HEADER
:
1531 fprintf(o
->file
, "%sGIT binary patch\n", diff_line_prefix(o
));
1533 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
:
1534 fprintf(o
->file
, "%sdelta %s\n", diff_line_prefix(o
), line
);
1536 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
:
1537 fprintf(o
->file
, "%sliteral %s\n", diff_line_prefix(o
), line
);
1539 case DIFF_SYMBOL_BINARY_DIFF_FOOTER
:
1540 fputs(diff_line_prefix(o
), o
->file
);
1541 fputc('\n', o
->file
);
1543 case DIFF_SYMBOL_REWRITE_DIFF
:
1544 fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
1545 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1546 emit_line(o
, fraginfo
, reset
, line
, len
);
1548 case DIFF_SYMBOL_SUBMODULE_ADD
:
1549 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1550 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1551 emit_line(o
, set
, reset
, line
, len
);
1553 case DIFF_SYMBOL_SUBMODULE_DEL
:
1554 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1555 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1556 emit_line(o
, set
, reset
, line
, len
);
1558 case DIFF_SYMBOL_SUBMODULE_UNTRACKED
:
1559 fprintf(o
->file
, "%sSubmodule %s contains untracked content\n",
1560 diff_line_prefix(o
), line
);
1562 case DIFF_SYMBOL_SUBMODULE_MODIFIED
:
1563 fprintf(o
->file
, "%sSubmodule %s contains modified content\n",
1564 diff_line_prefix(o
), line
);
1566 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
:
1567 emit_line(o
, "", "", " 0 files changed\n",
1568 strlen(" 0 files changed\n"));
1570 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV
:
1571 emit_line(o
, "", "", " ...\n", strlen(" ...\n"));
1573 case DIFF_SYMBOL_WORD_DIFF
:
1574 fprintf(o
->file
, "%.*s", len
, line
);
1576 case DIFF_SYMBOL_STAT_SEP
:
1577 fputs(o
->stat_sep
, o
->file
);
1580 BUG("unknown diff symbol");
1584 static void emit_diff_symbol(struct diff_options
*o
, enum diff_symbol s
,
1585 const char *line
, int len
, unsigned flags
)
1587 struct emitted_diff_symbol e
= {
1588 .line
= line
, .len
= len
, .flags
= flags
, .s
= s
1591 if (o
->emitted_symbols
)
1592 append_emitted_diff_symbol(o
, &e
);
1594 emit_diff_symbol_from_struct(o
, &e
);
1597 void diff_emit_submodule_del(struct diff_options
*o
, const char *line
)
1599 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_DEL
, line
, strlen(line
), 0);
1602 void diff_emit_submodule_add(struct diff_options
*o
, const char *line
)
1604 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ADD
, line
, strlen(line
), 0);
1607 void diff_emit_submodule_untracked(struct diff_options
*o
, const char *path
)
1609 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
1610 path
, strlen(path
), 0);
1613 void diff_emit_submodule_modified(struct diff_options
*o
, const char *path
)
1615 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_MODIFIED
,
1616 path
, strlen(path
), 0);
1619 void diff_emit_submodule_header(struct diff_options
*o
, const char *header
)
1621 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_HEADER
,
1622 header
, strlen(header
), 0);
1625 void diff_emit_submodule_error(struct diff_options
*o
, const char *err
)
1627 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ERROR
, err
, strlen(err
), 0);
1630 void diff_emit_submodule_pipethrough(struct diff_options
*o
,
1631 const char *line
, int len
)
1633 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
, line
, len
, 0);
1636 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
1638 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
1639 ecbdata
->blank_at_eof_in_preimage
&&
1640 ecbdata
->blank_at_eof_in_postimage
&&
1641 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
1642 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
1644 return ws_blank_line(line
, len
);
1647 static void emit_add_line(struct emit_callback
*ecbdata
,
1648 const char *line
, int len
)
1650 unsigned flags
= WSEH_NEW
| ecbdata
->ws_rule
;
1651 if (new_blank_line_at_eof(ecbdata
, line
, len
))
1652 flags
|= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
;
1654 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_PLUS
, line
, len
, flags
);
1657 static void emit_del_line(struct emit_callback
*ecbdata
,
1658 const char *line
, int len
)
1660 unsigned flags
= WSEH_OLD
| ecbdata
->ws_rule
;
1661 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_MINUS
, line
, len
, flags
);
1664 static void emit_context_line(struct emit_callback
*ecbdata
,
1665 const char *line
, int len
)
1667 unsigned flags
= WSEH_CONTEXT
| ecbdata
->ws_rule
;
1668 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_CONTEXT
, line
, len
, flags
);
1671 static void emit_hunk_header(struct emit_callback
*ecbdata
,
1672 const char *line
, int len
)
1674 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1675 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
1676 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
1677 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1678 const char *reverse
= ecbdata
->color_diff
? GIT_COLOR_REVERSE
: "";
1679 static const char atat
[2] = { '@', '@' };
1680 const char *cp
, *ep
;
1681 struct strbuf msgbuf
= STRBUF_INIT
;
1686 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1687 * it always is at least 10 bytes long.
1690 memcmp(line
, atat
, 2) ||
1691 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
1692 emit_diff_symbol(ecbdata
->opt
,
1693 DIFF_SYMBOL_CONTEXT_MARKER
, line
, len
, 0);
1696 ep
+= 2; /* skip over @@ */
1698 /* The hunk header in fraginfo color */
1699 if (ecbdata
->opt
->flags
.dual_color_diffed_diffs
)
1700 strbuf_addstr(&msgbuf
, reverse
);
1701 strbuf_addstr(&msgbuf
, frag
);
1702 if (ecbdata
->opt
->flags
.suppress_hunk_header_line_count
)
1703 strbuf_add(&msgbuf
, atat
, sizeof(atat
));
1705 strbuf_add(&msgbuf
, line
, ep
- line
);
1706 strbuf_addstr(&msgbuf
, reset
);
1712 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
1715 /* blank before the func header */
1716 for (cp
= ep
; ep
- line
< len
; ep
++)
1717 if (*ep
!= ' ' && *ep
!= '\t')
1720 strbuf_addstr(&msgbuf
, context
);
1721 strbuf_add(&msgbuf
, cp
, ep
- cp
);
1722 strbuf_addstr(&msgbuf
, reset
);
1725 if (ep
< line
+ len
) {
1726 strbuf_addstr(&msgbuf
, func
);
1727 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
1728 strbuf_addstr(&msgbuf
, reset
);
1731 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
1732 strbuf_complete_line(&msgbuf
);
1733 emit_diff_symbol(ecbdata
->opt
,
1734 DIFF_SYMBOL_CONTEXT_FRAGINFO
, msgbuf
.buf
, msgbuf
.len
, 0);
1735 strbuf_release(&msgbuf
);
1738 static struct diff_tempfile
*claim_diff_tempfile(void)
1741 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
1742 if (!diff_temp
[i
].name
)
1743 return diff_temp
+ i
;
1744 BUG("diff is failing to clean up its tempfiles");
1747 static void remove_tempfile(void)
1750 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
1751 if (is_tempfile_active(diff_temp
[i
].tempfile
))
1752 delete_tempfile(&diff_temp
[i
].tempfile
);
1753 diff_temp
[i
].name
= NULL
;
1757 static void add_line_count(struct strbuf
*out
, int count
)
1761 strbuf_addstr(out
, "0,0");
1764 strbuf_addstr(out
, "1");
1767 strbuf_addf(out
, "1,%d", count
);
1772 static void emit_rewrite_lines(struct emit_callback
*ecb
,
1773 int prefix
, const char *data
, int size
)
1775 const char *endp
= NULL
;
1780 endp
= memchr(data
, '\n', size
);
1781 len
= endp
? (endp
- data
+ 1) : size
;
1782 if (prefix
!= '+') {
1783 ecb
->lno_in_preimage
++;
1784 emit_del_line(ecb
, data
, len
);
1786 ecb
->lno_in_postimage
++;
1787 emit_add_line(ecb
, data
, len
);
1793 emit_diff_symbol(ecb
->opt
, DIFF_SYMBOL_NO_LF_EOF
, NULL
, 0, 0);
1796 static void emit_rewrite_diff(const char *name_a
,
1798 struct diff_filespec
*one
,
1799 struct diff_filespec
*two
,
1800 struct userdiff_driver
*textconv_one
,
1801 struct userdiff_driver
*textconv_two
,
1802 struct diff_options
*o
)
1805 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
1806 const char *a_prefix
, *b_prefix
;
1807 char *data_one
, *data_two
;
1808 size_t size_one
, size_two
;
1809 struct emit_callback ecbdata
;
1810 struct strbuf out
= STRBUF_INIT
;
1812 if (diff_mnemonic_prefix
&& o
->flags
.reverse_diff
) {
1813 a_prefix
= o
->b_prefix
;
1814 b_prefix
= o
->a_prefix
;
1816 a_prefix
= o
->a_prefix
;
1817 b_prefix
= o
->b_prefix
;
1820 name_a
+= (*name_a
== '/');
1821 name_b
+= (*name_b
== '/');
1823 strbuf_reset(&a_name
);
1824 strbuf_reset(&b_name
);
1825 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
1826 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
1828 size_one
= fill_textconv(o
->repo
, textconv_one
, one
, &data_one
);
1829 size_two
= fill_textconv(o
->repo
, textconv_two
, two
, &data_two
);
1831 memset(&ecbdata
, 0, sizeof(ecbdata
));
1832 ecbdata
.color_diff
= want_color(o
->use_color
);
1833 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
1835 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
1837 mf1
.ptr
= (char *)data_one
;
1838 mf2
.ptr
= (char *)data_two
;
1839 mf1
.size
= size_one
;
1840 mf2
.size
= size_two
;
1841 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1843 ecbdata
.lno_in_preimage
= 1;
1844 ecbdata
.lno_in_postimage
= 1;
1846 lc_a
= count_lines(data_one
, size_one
);
1847 lc_b
= count_lines(data_two
, size_two
);
1849 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1850 a_name
.buf
, a_name
.len
, 0);
1851 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1852 b_name
.buf
, b_name
.len
, 0);
1854 strbuf_addstr(&out
, "@@ -");
1855 if (!o
->irreversible_delete
)
1856 add_line_count(&out
, lc_a
);
1858 strbuf_addstr(&out
, "?,?");
1859 strbuf_addstr(&out
, " +");
1860 add_line_count(&out
, lc_b
);
1861 strbuf_addstr(&out
, " @@\n");
1862 emit_diff_symbol(o
, DIFF_SYMBOL_REWRITE_DIFF
, out
.buf
, out
.len
, 0);
1863 strbuf_release(&out
);
1865 if (lc_a
&& !o
->irreversible_delete
)
1866 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
1868 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
1870 free((char *)data_one
);
1872 free((char *)data_two
);
1875 struct diff_words_buffer
{
1877 unsigned long alloc
;
1878 struct diff_words_orig
{
1879 const char *begin
, *end
;
1881 int orig_nr
, orig_alloc
;
1884 static void diff_words_append(char *line
, unsigned long len
,
1885 struct diff_words_buffer
*buffer
)
1887 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
1890 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
1891 buffer
->text
.size
+= len
;
1892 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
1895 struct diff_words_style_elem
{
1898 const char *color
; /* NULL; filled in by the setup code if
1899 * color is enabled */
1902 struct diff_words_style
{
1903 enum diff_words_type type
;
1904 struct diff_words_style_elem new_word
, old_word
, ctx
;
1905 const char *newline
;
1908 static struct diff_words_style diff_words_styles
[] = {
1909 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1910 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1911 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
1914 struct diff_words_data
{
1915 struct diff_words_buffer minus
, plus
;
1916 const char *current_plus
;
1918 struct diff_options
*opt
;
1919 regex_t
*word_regex
;
1920 enum diff_words_type type
;
1921 struct diff_words_style
*style
;
1924 static int fn_out_diff_words_write_helper(struct diff_options
*o
,
1925 struct diff_words_style_elem
*st_el
,
1926 const char *newline
,
1927 size_t count
, const char *buf
)
1930 struct strbuf sb
= STRBUF_INIT
;
1933 char *p
= memchr(buf
, '\n', count
);
1935 strbuf_addstr(&sb
, diff_line_prefix(o
));
1938 const char *reset
= st_el
->color
&& *st_el
->color
?
1939 GIT_COLOR_RESET
: NULL
;
1940 if (st_el
->color
&& *st_el
->color
)
1941 strbuf_addstr(&sb
, st_el
->color
);
1942 strbuf_addstr(&sb
, st_el
->prefix
);
1943 strbuf_add(&sb
, buf
, p
? p
- buf
: count
);
1944 strbuf_addstr(&sb
, st_el
->suffix
);
1946 strbuf_addstr(&sb
, reset
);
1951 strbuf_addstr(&sb
, newline
);
1952 count
-= p
+ 1 - buf
;
1956 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1964 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1966 strbuf_release(&sb
);
1971 * '--color-words' algorithm can be described as:
1973 * 1. collect the minus/plus lines of a diff hunk, divided into
1974 * minus-lines and plus-lines;
1976 * 2. break both minus-lines and plus-lines into words and
1977 * place them into two mmfile_t with one word for each line;
1979 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1981 * And for the common parts of the both file, we output the plus side text.
1982 * diff_words->current_plus is used to trace the current position of the plus file
1983 * which printed. diff_words->last_minus is used to trace the last minus word
1986 * For '--graph' to work with '--color-words', we need to output the graph prefix
1987 * on each line of color words output. Generally, there are two conditions on
1988 * which we should output the prefix.
1990 * 1. diff_words->last_minus == 0 &&
1991 * diff_words->current_plus == diff_words->plus.text.ptr
1993 * that is: the plus text must start as a new line, and if there is no minus
1994 * word printed, a graph prefix must be printed.
1996 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1997 * *(diff_words->current_plus - 1) == '\n'
1999 * that is: a graph prefix must be printed following a '\n'
2001 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
2003 if ((diff_words
->last_minus
== 0 &&
2004 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
2005 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
2006 *(diff_words
->current_plus
- 1) == '\n')) {
2013 static void fn_out_diff_words_aux(void *priv
,
2014 long minus_first
, long minus_len
,
2015 long plus_first
, long plus_len
,
2016 const char *func UNUSED
, long funclen UNUSED
)
2018 struct diff_words_data
*diff_words
= priv
;
2019 struct diff_words_style
*style
= diff_words
->style
;
2020 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
2021 struct diff_options
*opt
= diff_words
->opt
;
2022 const char *line_prefix
;
2025 line_prefix
= diff_line_prefix(opt
);
2027 /* POSIX requires that first be decremented by one if len == 0... */
2029 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
2031 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
2033 minus_begin
= minus_end
=
2034 diff_words
->minus
.orig
[minus_first
].end
;
2037 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
2038 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
2040 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
2042 if (color_words_output_graph_prefix(diff_words
)) {
2043 fputs(line_prefix
, diff_words
->opt
->file
);
2045 if (diff_words
->current_plus
!= plus_begin
) {
2046 fn_out_diff_words_write_helper(diff_words
->opt
,
2047 &style
->ctx
, style
->newline
,
2048 plus_begin
- diff_words
->current_plus
,
2049 diff_words
->current_plus
);
2051 if (minus_begin
!= minus_end
) {
2052 fn_out_diff_words_write_helper(diff_words
->opt
,
2053 &style
->old_word
, style
->newline
,
2054 minus_end
- minus_begin
, minus_begin
);
2056 if (plus_begin
!= plus_end
) {
2057 fn_out_diff_words_write_helper(diff_words
->opt
,
2058 &style
->new_word
, style
->newline
,
2059 plus_end
- plus_begin
, plus_begin
);
2062 diff_words
->current_plus
= plus_end
;
2063 diff_words
->last_minus
= minus_first
;
2066 /* This function starts looking at *begin, and returns 0 iff a word was found. */
2067 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
2068 int *begin
, int *end
)
2070 while (word_regex
&& *begin
< buffer
->size
) {
2071 regmatch_t match
[1];
2072 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
2073 buffer
->size
- *begin
, 1, match
, 0)) {
2074 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
2075 '\n', match
[0].rm_eo
- match
[0].rm_so
);
2076 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
2077 *begin
+= match
[0].rm_so
;
2081 return *begin
> *end
;
2087 /* find the next word */
2088 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
2090 if (*begin
>= buffer
->size
)
2093 /* find the end of the word */
2095 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
2102 * This function splits the words in buffer->text, stores the list with
2103 * newline separator into out, and saves the offsets of the original words
2106 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
2107 regex_t
*word_regex
)
2115 /* fake an empty "0th" word */
2116 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
2117 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
2118 buffer
->orig_nr
= 1;
2120 for (i
= 0; i
< buffer
->text
.size
; i
++) {
2121 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
2124 /* store original boundaries */
2125 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
2126 buffer
->orig_alloc
);
2127 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
2128 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
2131 /* store one word */
2132 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
2133 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
2134 out
->ptr
[out
->size
+ j
- i
] = '\n';
2135 out
->size
+= j
- i
+ 1;
2141 /* this executes the word diff on the accumulated buffers */
2142 static void diff_words_show(struct diff_words_data
*diff_words
)
2146 mmfile_t minus
, plus
;
2147 struct diff_words_style
*style
= diff_words
->style
;
2149 struct diff_options
*opt
= diff_words
->opt
;
2150 const char *line_prefix
;
2153 line_prefix
= diff_line_prefix(opt
);
2155 /* special case: only removal */
2156 if (!diff_words
->plus
.text
.size
) {
2157 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2158 line_prefix
, strlen(line_prefix
), 0);
2159 fn_out_diff_words_write_helper(diff_words
->opt
,
2160 &style
->old_word
, style
->newline
,
2161 diff_words
->minus
.text
.size
,
2162 diff_words
->minus
.text
.ptr
);
2163 diff_words
->minus
.text
.size
= 0;
2167 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
2168 diff_words
->last_minus
= 0;
2170 memset(&xpp
, 0, sizeof(xpp
));
2171 memset(&xecfg
, 0, sizeof(xecfg
));
2172 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
2173 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
2175 /* as only the hunk header will be parsed, we need a 0-context */
2177 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, NULL
,
2178 diff_words
, &xpp
, &xecfg
))
2179 die("unable to generate word diff");
2182 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
2183 diff_words
->plus
.text
.size
) {
2184 if (color_words_output_graph_prefix(diff_words
))
2185 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2186 line_prefix
, strlen(line_prefix
), 0);
2187 fn_out_diff_words_write_helper(diff_words
->opt
,
2188 &style
->ctx
, style
->newline
,
2189 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
2190 - diff_words
->current_plus
, diff_words
->current_plus
);
2192 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
2195 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
2196 static void diff_words_flush(struct emit_callback
*ecbdata
)
2198 struct diff_options
*wo
= ecbdata
->diff_words
->opt
;
2200 if (ecbdata
->diff_words
->minus
.text
.size
||
2201 ecbdata
->diff_words
->plus
.text
.size
)
2202 diff_words_show(ecbdata
->diff_words
);
2204 if (wo
->emitted_symbols
) {
2205 struct diff_options
*o
= ecbdata
->opt
;
2206 struct emitted_diff_symbols
*wol
= wo
->emitted_symbols
;
2211 * Instead of appending each, concat all words to a line?
2213 for (i
= 0; i
< wol
->nr
; i
++)
2214 append_emitted_diff_symbol(o
, &wol
->buf
[i
]);
2216 for (i
= 0; i
< wol
->nr
; i
++)
2217 free((void *)wol
->buf
[i
].line
);
2223 static void diff_filespec_load_driver(struct diff_filespec
*one
,
2224 struct index_state
*istate
)
2226 /* Use already-loaded driver */
2230 if (S_ISREG(one
->mode
))
2231 one
->driver
= userdiff_find_by_path(istate
, one
->path
);
2233 /* Fallback to default settings */
2235 one
->driver
= userdiff_find_by_name("default");
2238 static const char *userdiff_word_regex(struct diff_filespec
*one
,
2239 struct index_state
*istate
)
2241 diff_filespec_load_driver(one
, istate
);
2242 return one
->driver
->word_regex
;
2245 static void init_diff_words_data(struct emit_callback
*ecbdata
,
2246 struct diff_options
*orig_opts
,
2247 struct diff_filespec
*one
,
2248 struct diff_filespec
*two
)
2251 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
2252 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
2254 CALLOC_ARRAY(ecbdata
->diff_words
, 1);
2255 ecbdata
->diff_words
->type
= o
->word_diff
;
2256 ecbdata
->diff_words
->opt
= o
;
2258 if (orig_opts
->emitted_symbols
)
2259 CALLOC_ARRAY(o
->emitted_symbols
, 1);
2262 o
->word_regex
= userdiff_word_regex(one
, o
->repo
->index
);
2264 o
->word_regex
= userdiff_word_regex(two
, o
->repo
->index
);
2266 o
->word_regex
= diff_word_regex_cfg
;
2267 if (o
->word_regex
) {
2268 ecbdata
->diff_words
->word_regex
= (regex_t
*)
2269 xmalloc(sizeof(regex_t
));
2270 if (regcomp(ecbdata
->diff_words
->word_regex
,
2272 REG_EXTENDED
| REG_NEWLINE
))
2273 die("invalid regular expression: %s",
2276 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
2277 if (o
->word_diff
== diff_words_styles
[i
].type
) {
2278 ecbdata
->diff_words
->style
=
2279 &diff_words_styles
[i
];
2283 if (want_color(o
->use_color
)) {
2284 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
2285 st
->old_word
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2286 st
->new_word
.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2287 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
2291 static void free_diff_words_data(struct emit_callback
*ecbdata
)
2293 if (ecbdata
->diff_words
) {
2294 diff_words_flush(ecbdata
);
2295 free_emitted_diff_symbols(ecbdata
->diff_words
->opt
->emitted_symbols
);
2296 free (ecbdata
->diff_words
->opt
);
2297 free (ecbdata
->diff_words
->minus
.text
.ptr
);
2298 free (ecbdata
->diff_words
->minus
.orig
);
2299 free (ecbdata
->diff_words
->plus
.text
.ptr
);
2300 free (ecbdata
->diff_words
->plus
.orig
);
2301 if (ecbdata
->diff_words
->word_regex
) {
2302 regfree(ecbdata
->diff_words
->word_regex
);
2303 free(ecbdata
->diff_words
->word_regex
);
2305 FREE_AND_NULL(ecbdata
->diff_words
);
2309 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
2311 if (want_color(diff_use_color
))
2312 return diff_colors
[ix
];
2316 const char *diff_line_prefix(struct diff_options
*opt
)
2318 struct strbuf
*msgbuf
;
2319 if (!opt
->output_prefix
)
2322 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
2326 static unsigned long sane_truncate_line(char *line
, unsigned long len
)
2329 unsigned long allot
;
2335 (void) utf8_width(&cp
, &l
);
2337 break; /* truncated in the middle? */
2342 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
2345 ecbdata
->lno_in_preimage
= 0;
2346 ecbdata
->lno_in_postimage
= 0;
2347 p
= strchr(line
, '-');
2349 return; /* cannot happen */
2350 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
2353 return; /* cannot happen */
2354 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
2357 static int fn_out_consume(void *priv
, char *line
, unsigned long len
)
2359 struct emit_callback
*ecbdata
= priv
;
2360 struct diff_options
*o
= ecbdata
->opt
;
2362 o
->found_changes
= 1;
2364 if (ecbdata
->header
) {
2365 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
2366 ecbdata
->header
->buf
, ecbdata
->header
->len
, 0);
2367 strbuf_reset(ecbdata
->header
);
2368 ecbdata
->header
= NULL
;
2371 if (ecbdata
->label_path
[0]) {
2372 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
2373 ecbdata
->label_path
[0],
2374 strlen(ecbdata
->label_path
[0]), 0);
2375 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
2376 ecbdata
->label_path
[1],
2377 strlen(ecbdata
->label_path
[1]), 0);
2378 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
2381 if (diff_suppress_blank_empty
2382 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
2387 if (line
[0] == '@') {
2388 if (ecbdata
->diff_words
)
2389 diff_words_flush(ecbdata
);
2390 len
= sane_truncate_line(line
, len
);
2391 find_lno(line
, ecbdata
);
2392 emit_hunk_header(ecbdata
, line
, len
);
2396 if (ecbdata
->diff_words
) {
2397 enum diff_symbol s
=
2398 ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
?
2399 DIFF_SYMBOL_WORDS_PORCELAIN
: DIFF_SYMBOL_WORDS
;
2400 if (line
[0] == '-') {
2401 diff_words_append(line
, len
,
2402 &ecbdata
->diff_words
->minus
);
2404 } else if (line
[0] == '+') {
2405 diff_words_append(line
, len
,
2406 &ecbdata
->diff_words
->plus
);
2408 } else if (starts_with(line
, "\\ ")) {
2410 * Eat the "no newline at eof" marker as if we
2411 * saw a "+" or "-" line with nothing on it,
2412 * and return without diff_words_flush() to
2413 * defer processing. If this is the end of
2414 * preimage, more "+" lines may come after it.
2418 diff_words_flush(ecbdata
);
2419 emit_diff_symbol(o
, s
, line
, len
, 0);
2425 ecbdata
->lno_in_postimage
++;
2426 emit_add_line(ecbdata
, line
+ 1, len
- 1);
2429 ecbdata
->lno_in_preimage
++;
2430 emit_del_line(ecbdata
, line
+ 1, len
- 1);
2433 ecbdata
->lno_in_postimage
++;
2434 ecbdata
->lno_in_preimage
++;
2435 emit_context_line(ecbdata
, line
+ 1, len
- 1);
2438 /* incomplete line at the end */
2439 ecbdata
->lno_in_preimage
++;
2440 emit_diff_symbol(o
, DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
2447 static void pprint_rename(struct strbuf
*name
, const char *a
, const char *b
)
2449 const char *old_name
= a
;
2450 const char *new_name
= b
;
2451 int pfx_length
, sfx_length
;
2452 int pfx_adjust_for_slash
;
2453 int len_a
= strlen(a
);
2454 int len_b
= strlen(b
);
2455 int a_midlen
, b_midlen
;
2456 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
2457 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
2459 if (qlen_a
|| qlen_b
) {
2460 quote_c_style(a
, name
, NULL
, 0);
2461 strbuf_addstr(name
, " => ");
2462 quote_c_style(b
, name
, NULL
, 0);
2466 /* Find common prefix */
2468 while (*old_name
&& *new_name
&& *old_name
== *new_name
) {
2469 if (*old_name
== '/')
2470 pfx_length
= old_name
- a
+ 1;
2475 /* Find common suffix */
2476 old_name
= a
+ len_a
;
2477 new_name
= b
+ len_b
;
2480 * If there is a common prefix, it must end in a slash. In
2481 * that case we let this loop run 1 into the prefix to see the
2484 * If there is no common prefix, we cannot do this as it would
2485 * underrun the input strings.
2487 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
2488 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old_name
&&
2489 b
+ pfx_length
- pfx_adjust_for_slash
<= new_name
&&
2490 *old_name
== *new_name
) {
2491 if (*old_name
== '/')
2492 sfx_length
= len_a
- (old_name
- a
);
2498 * pfx{mid-a => mid-b}sfx
2499 * {pfx-a => pfx-b}sfx
2500 * pfx{sfx-a => sfx-b}
2503 a_midlen
= len_a
- pfx_length
- sfx_length
;
2504 b_midlen
= len_b
- pfx_length
- sfx_length
;
2510 strbuf_grow(name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
2511 if (pfx_length
+ sfx_length
) {
2512 strbuf_add(name
, a
, pfx_length
);
2513 strbuf_addch(name
, '{');
2515 strbuf_add(name
, a
+ pfx_length
, a_midlen
);
2516 strbuf_addstr(name
, " => ");
2517 strbuf_add(name
, b
+ pfx_length
, b_midlen
);
2518 if (pfx_length
+ sfx_length
) {
2519 strbuf_addch(name
, '}');
2520 strbuf_add(name
, a
+ len_a
- sfx_length
, sfx_length
);
2524 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
2528 struct diffstat_file
*x
;
2530 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
2531 diffstat
->files
[diffstat
->nr
++] = x
;
2533 x
->from_name
= xstrdup(name_a
);
2534 x
->name
= xstrdup(name_b
);
2538 x
->from_name
= NULL
;
2539 x
->name
= xstrdup(name_a
);
2544 static int diffstat_consume(void *priv
, char *line
, unsigned long len
)
2546 struct diffstat_t
*diffstat
= priv
;
2547 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
2550 BUG("xdiff fed us an empty line");
2554 else if (line
[0] == '-')
2559 const char mime_boundary_leader
[] = "------------";
2561 static int scale_linear(int it
, int width
, int max_change
)
2566 * make sure that at least one '-' or '+' is printed if
2567 * there is any change to this path. The easiest way is to
2568 * scale linearly as if the allotted width is one column shorter
2569 * than it is, and then add 1 to the result.
2571 return 1 + (it
* (width
- 1) / max_change
);
2574 static void show_graph(struct strbuf
*out
, char ch
, int cnt
,
2575 const char *set
, const char *reset
)
2579 strbuf_addstr(out
, set
);
2580 strbuf_addchars(out
, ch
, cnt
);
2581 strbuf_addstr(out
, reset
);
2584 static void fill_print_name(struct diffstat_file
*file
)
2586 struct strbuf pname
= STRBUF_INIT
;
2588 if (file
->print_name
)
2591 if (file
->is_renamed
)
2592 pprint_rename(&pname
, file
->from_name
, file
->name
);
2594 quote_c_style(file
->name
, &pname
, NULL
, 0);
2597 strbuf_addf(&pname
, " (%s)", file
->comments
);
2599 file
->print_name
= strbuf_detach(&pname
, NULL
);
2602 static void print_stat_summary_inserts_deletes(struct diff_options
*options
,
2603 int files
, int insertions
, int deletions
)
2605 struct strbuf sb
= STRBUF_INIT
;
2608 assert(insertions
== 0 && deletions
== 0);
2609 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
2615 (files
== 1) ? " %d file changed" : " %d files changed",
2619 * For binary diff, the caller may want to print "x files
2620 * changed" with insertions == 0 && deletions == 0.
2622 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2623 * is probably less confusing (i.e skip over "2 files changed
2624 * but nothing about added/removed lines? Is this a bug in Git?").
2626 if (insertions
|| deletions
== 0) {
2628 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2632 if (deletions
|| insertions
== 0) {
2634 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2637 strbuf_addch(&sb
, '\n');
2638 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
2640 strbuf_release(&sb
);
2643 void print_stat_summary(FILE *fp
, int files
,
2644 int insertions
, int deletions
)
2646 struct diff_options o
;
2647 memset(&o
, 0, sizeof(o
));
2650 print_stat_summary_inserts_deletes(&o
, files
, insertions
, deletions
);
2653 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
2655 int i
, len
, add
, del
, adds
= 0, dels
= 0;
2656 uintmax_t max_change
= 0, max_len
= 0;
2657 int total_files
= data
->nr
, count
;
2658 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
2659 const char *reset
, *add_c
, *del_c
;
2660 int extra_shown
= 0;
2661 const char *line_prefix
= diff_line_prefix(options
);
2662 struct strbuf out
= STRBUF_INIT
;
2667 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
2669 reset
= diff_get_color_opt(options
, DIFF_RESET
);
2670 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
2671 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
2674 * Find the longest filename and max number of changes
2676 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
2677 struct diffstat_file
*file
= data
->files
[i
];
2678 uintmax_t change
= file
->added
+ file
->deleted
;
2680 if (!file
->is_interesting
&& (change
== 0)) {
2681 count
++; /* not shown == room for one more */
2684 fill_print_name(file
);
2685 len
= utf8_strwidth(file
->print_name
);
2689 if (file
->is_unmerged
) {
2690 /* "Unmerged" is 8 characters */
2691 bin_width
= bin_width
< 8 ? 8 : bin_width
;
2694 if (file
->is_binary
) {
2695 /* "Bin XXX -> YYY bytes" */
2696 int w
= 14 + decimal_width(file
->added
)
2697 + decimal_width(file
->deleted
);
2698 bin_width
= bin_width
< w
? w
: bin_width
;
2699 /* Display change counts aligned with "Bin" */
2704 if (max_change
< change
)
2705 max_change
= change
;
2707 count
= i
; /* where we can stop scanning in data->files[] */
2710 * We have width = stat_width or term_columns() columns total.
2711 * We want a maximum of min(max_len, stat_name_width) for the name part.
2712 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2713 * We also need 1 for " " and 4 + decimal_width(max_change)
2714 * for " | NNNN " and one the empty column at the end, altogether
2715 * 6 + decimal_width(max_change).
2717 * If there's not enough space, we will use the smaller of
2718 * stat_name_width (if set) and 5/8*width for the filename,
2719 * and the rest for constant elements + graph part, but no more
2720 * than stat_graph_width for the graph part.
2721 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2722 * for the standard terminal size).
2724 * In other words: stat_width limits the maximum width, and
2725 * stat_name_width fixes the maximum width of the filename,
2726 * and is also used to divide available columns if there
2729 * Binary files are displayed with "Bin XXX -> YYY bytes"
2730 * instead of the change count and graph. This part is treated
2731 * similarly to the graph part, except that it is not
2732 * "scaled". If total width is too small to accommodate the
2733 * guaranteed minimum width of the filename part and the
2734 * separators and this message, this message will "overflow"
2735 * making the line longer than the maximum width.
2739 * NEEDSWORK: line_prefix is often used for "log --graph" output
2740 * and contains ANSI-colored string. utf8_strnwidth() should be
2741 * used to correctly count the display width instead of strlen().
2743 if (options
->stat_width
== -1)
2744 width
= term_columns() - strlen(line_prefix
);
2746 width
= options
->stat_width
? options
->stat_width
: 80;
2747 number_width
= decimal_width(max_change
) > number_width
?
2748 decimal_width(max_change
) : number_width
;
2750 if (options
->stat_name_width
== -1)
2751 options
->stat_name_width
= diff_stat_name_width
;
2752 if (options
->stat_graph_width
== -1)
2753 options
->stat_graph_width
= diff_stat_graph_width
;
2756 * Guarantee 3/8*16 == 6 for the graph part
2757 * and 5/8*16 == 10 for the filename part
2759 if (width
< 16 + 6 + number_width
)
2760 width
= 16 + 6 + number_width
;
2763 * First assign sizes that are wanted, ignoring available width.
2764 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2765 * starting from "XXX" should fit in graph_width.
2767 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
2768 if (options
->stat_graph_width
&&
2769 options
->stat_graph_width
< graph_width
)
2770 graph_width
= options
->stat_graph_width
;
2772 name_width
= (options
->stat_name_width
> 0 &&
2773 options
->stat_name_width
< max_len
) ?
2774 options
->stat_name_width
: max_len
;
2777 * Adjust adjustable widths not to exceed maximum width
2779 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
2780 if (graph_width
> width
* 3/8 - number_width
- 6) {
2781 graph_width
= width
* 3/8 - number_width
- 6;
2782 if (graph_width
< 6)
2786 if (options
->stat_graph_width
&&
2787 graph_width
> options
->stat_graph_width
)
2788 graph_width
= options
->stat_graph_width
;
2789 if (name_width
> width
- number_width
- 6 - graph_width
)
2790 name_width
= width
- number_width
- 6 - graph_width
;
2792 graph_width
= width
- number_width
- 6 - name_width
;
2796 * From here name_width is the width of the name area,
2797 * and graph_width is the width of the graph area.
2798 * max_change is used to scale graph properly.
2800 for (i
= 0; i
< count
; i
++) {
2801 const char *prefix
= "";
2802 struct diffstat_file
*file
= data
->files
[i
];
2803 char *name
= file
->print_name
;
2804 uintmax_t added
= file
->added
;
2805 uintmax_t deleted
= file
->deleted
;
2806 int name_len
, padding
;
2808 if (!file
->is_interesting
&& (added
+ deleted
== 0))
2812 * "scale" the filename
2815 name_len
= utf8_strwidth(name
);
2816 if (name_width
< name_len
) {
2821 * NEEDSWORK: (name_len - len) counts the display
2822 * width, which would be shorter than the byte
2823 * length of the corresponding substring.
2824 * Advancing "name" by that number of bytes does
2825 * *NOT* skip over that many columns, so it is
2826 * very likely that chomping the pathname at the
2827 * slash we will find starting from "name" will
2828 * leave the resulting string still too long.
2830 name
+= name_len
- len
;
2831 slash
= strchr(name
, '/');
2835 padding
= len
- utf8_strwidth(name
);
2839 if (file
->is_binary
) {
2840 strbuf_addf(&out
, " %s%s%*s | %*s",
2841 prefix
, name
, padding
, "",
2842 number_width
, "Bin");
2843 if (!added
&& !deleted
) {
2844 strbuf_addch(&out
, '\n');
2845 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2846 out
.buf
, out
.len
, 0);
2850 strbuf_addf(&out
, " %s%"PRIuMAX
"%s",
2851 del_c
, deleted
, reset
);
2852 strbuf_addstr(&out
, " -> ");
2853 strbuf_addf(&out
, "%s%"PRIuMAX
"%s",
2854 add_c
, added
, reset
);
2855 strbuf_addstr(&out
, " bytes\n");
2856 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2857 out
.buf
, out
.len
, 0);
2861 else if (file
->is_unmerged
) {
2862 strbuf_addf(&out
, " %s%s%*s | %*s",
2863 prefix
, name
, padding
, "",
2864 number_width
, "Unmerged\n");
2865 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2866 out
.buf
, out
.len
, 0);
2872 * scale the add/delete
2877 if (graph_width
<= max_change
) {
2878 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
2879 if (total
< 2 && add
&& del
)
2880 /* width >= 2 due to the sanity check */
2883 add
= scale_linear(add
, graph_width
, max_change
);
2886 del
= scale_linear(del
, graph_width
, max_change
);
2890 strbuf_addf(&out
, " %s%s%*s | %*"PRIuMAX
"%s",
2891 prefix
, name
, padding
, "",
2892 number_width
, added
+ deleted
,
2893 added
+ deleted
? " " : "");
2894 show_graph(&out
, '+', add
, add_c
, reset
);
2895 show_graph(&out
, '-', del
, del_c
, reset
);
2896 strbuf_addch(&out
, '\n');
2897 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2898 out
.buf
, out
.len
, 0);
2902 for (i
= 0; i
< data
->nr
; i
++) {
2903 struct diffstat_file
*file
= data
->files
[i
];
2904 uintmax_t added
= file
->added
;
2905 uintmax_t deleted
= file
->deleted
;
2907 if (file
->is_unmerged
||
2908 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
2913 if (!file
->is_binary
) {
2920 emit_diff_symbol(options
,
2921 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
2926 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2927 strbuf_release(&out
);
2930 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
2932 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
2937 for (i
= 0; i
< data
->nr
; i
++) {
2938 int added
= data
->files
[i
]->added
;
2939 int deleted
= data
->files
[i
]->deleted
;
2941 if (data
->files
[i
]->is_unmerged
||
2942 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
2944 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
2949 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2952 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
2959 for (i
= 0; i
< data
->nr
; i
++) {
2960 struct diffstat_file
*file
= data
->files
[i
];
2962 fprintf(options
->file
, "%s", diff_line_prefix(options
));
2964 if (file
->is_binary
)
2965 fprintf(options
->file
, "-\t-\t");
2967 fprintf(options
->file
,
2968 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
2969 file
->added
, file
->deleted
);
2970 if (options
->line_termination
) {
2971 fill_print_name(file
);
2972 if (!file
->is_renamed
)
2973 write_name_quoted(file
->name
, options
->file
,
2974 options
->line_termination
);
2976 fputs(file
->print_name
, options
->file
);
2977 putc(options
->line_termination
, options
->file
);
2980 if (file
->is_renamed
) {
2981 putc('\0', options
->file
);
2982 write_name_quoted(file
->from_name
, options
->file
, '\0');
2984 write_name_quoted(file
->name
, options
->file
, '\0');
2989 struct dirstat_file
{
2991 unsigned long changed
;
2994 struct dirstat_dir
{
2995 struct dirstat_file
*files
;
2996 int alloc
, nr
, permille
, cumulative
;
2999 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
3000 unsigned long changed
, const char *base
, int baselen
)
3002 unsigned long sum_changes
= 0;
3003 unsigned int sources
= 0;
3004 const char *line_prefix
= diff_line_prefix(opt
);
3007 struct dirstat_file
*f
= dir
->files
;
3008 int namelen
= strlen(f
->name
);
3009 unsigned long changes
;
3012 if (namelen
< baselen
)
3014 if (memcmp(f
->name
, base
, baselen
))
3016 slash
= strchr(f
->name
+ baselen
, '/');
3018 int newbaselen
= slash
+ 1 - f
->name
;
3019 changes
= gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
3022 changes
= f
->changed
;
3027 sum_changes
+= changes
;
3031 * We don't report dirstat's for
3033 * - or cases where everything came from a single directory
3034 * under this directory (sources == 1).
3036 if (baselen
&& sources
!= 1) {
3038 int permille
= sum_changes
* 1000 / changed
;
3039 if (permille
>= dir
->permille
) {
3040 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
3041 permille
/ 10, permille
% 10, baselen
, base
);
3042 if (!dir
->cumulative
)
3050 static int dirstat_compare(const void *_a
, const void *_b
)
3052 const struct dirstat_file
*a
= _a
;
3053 const struct dirstat_file
*b
= _b
;
3054 return strcmp(a
->name
, b
->name
);
3057 static void conclude_dirstat(struct diff_options
*options
,
3058 struct dirstat_dir
*dir
,
3059 unsigned long changed
)
3061 struct dirstat_file
*to_free
= dir
->files
;
3064 /* This can happen even with many files, if everything was renames */
3067 /* Show all directories with more than x% of the changes */
3068 QSORT(dir
->files
, dir
->nr
, dirstat_compare
);
3069 gather_dirstat(options
, dir
, changed
, "", 0);
3075 static void show_dirstat(struct diff_options
*options
)
3078 unsigned long changed
;
3079 struct dirstat_dir dir
;
3080 struct diff_queue_struct
*q
= &diff_queued_diff
;
3085 dir
.permille
= options
->dirstat_permille
;
3086 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3089 for (i
= 0; i
< q
->nr
; i
++) {
3090 struct diff_filepair
*p
= q
->queue
[i
];
3092 unsigned long copied
, added
, damage
;
3093 struct diff_populate_filespec_options dpf_options
= {
3094 .check_size_only
= 1,
3097 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
3099 if (p
->one
->oid_valid
&& p
->two
->oid_valid
&&
3100 oideq(&p
->one
->oid
, &p
->two
->oid
)) {
3102 * The SHA1 has not changed, so pre-/post-content is
3103 * identical. We can therefore skip looking at the
3104 * file contents altogether.
3110 if (options
->flags
.dirstat_by_file
) {
3112 * In --dirstat-by-file mode, we don't really need to
3113 * look at the actual file contents at all.
3114 * The fact that the SHA1 changed is enough for us to
3115 * add this file to the list of results
3116 * (with each file contributing equal damage).
3122 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
3123 diff_populate_filespec(options
->repo
, p
->one
, NULL
);
3124 diff_populate_filespec(options
->repo
, p
->two
, NULL
);
3125 diffcore_count_changes(options
->repo
,
3126 p
->one
, p
->two
, NULL
, NULL
,
3128 diff_free_filespec_data(p
->one
);
3129 diff_free_filespec_data(p
->two
);
3130 } else if (DIFF_FILE_VALID(p
->one
)) {
3131 diff_populate_filespec(options
->repo
, p
->one
, &dpf_options
);
3133 diff_free_filespec_data(p
->one
);
3134 } else if (DIFF_FILE_VALID(p
->two
)) {
3135 diff_populate_filespec(options
->repo
, p
->two
, &dpf_options
);
3137 added
= p
->two
->size
;
3138 diff_free_filespec_data(p
->two
);
3143 * Original minus copied is the removed material,
3144 * added is the new material. They are both damages
3145 * made to the preimage.
3146 * If the resulting damage is zero, we know that
3147 * diffcore_count_changes() considers the two entries to
3148 * be identical, but since the oid changed, we
3149 * know that there must have been _some_ kind of change,
3150 * so we force all entries to have damage > 0.
3152 damage
= (p
->one
->size
- copied
) + added
;
3157 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3158 dir
.files
[dir
.nr
].name
= name
;
3159 dir
.files
[dir
.nr
].changed
= damage
;
3164 conclude_dirstat(options
, &dir
, changed
);
3167 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
3170 unsigned long changed
;
3171 struct dirstat_dir dir
;
3179 dir
.permille
= options
->dirstat_permille
;
3180 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3183 for (i
= 0; i
< data
->nr
; i
++) {
3184 struct diffstat_file
*file
= data
->files
[i
];
3185 unsigned long damage
= file
->added
+ file
->deleted
;
3186 if (file
->is_binary
)
3188 * binary files counts bytes, not lines. Must find some
3189 * way to normalize binary bytes vs. textual lines.
3190 * The following heuristic assumes that there are 64
3192 * This is stupid and ugly, but very cheap...
3194 damage
= DIV_ROUND_UP(damage
, 64);
3195 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3196 dir
.files
[dir
.nr
].name
= file
->name
;
3197 dir
.files
[dir
.nr
].changed
= damage
;
3202 conclude_dirstat(options
, &dir
, changed
);
3205 static void free_diffstat_file(struct diffstat_file
*f
)
3207 free(f
->print_name
);
3213 void free_diffstat_info(struct diffstat_t
*diffstat
)
3216 for (i
= 0; i
< diffstat
->nr
; i
++)
3217 free_diffstat_file(diffstat
->files
[i
]);
3218 free(diffstat
->files
);
3221 struct checkdiff_t
{
3222 const char *filename
;
3224 int conflict_marker_size
;
3225 struct diff_options
*o
;
3230 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
3235 if (len
< marker_size
+ 1)
3237 firstchar
= line
[0];
3238 switch (firstchar
) {
3239 case '=': case '>': case '<': case '|':
3244 for (cnt
= 1; cnt
< marker_size
; cnt
++)
3245 if (line
[cnt
] != firstchar
)
3247 /* line[1] through line[marker_size-1] are same as firstchar */
3248 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
3253 static void checkdiff_consume_hunk(void *priv
,
3254 long ob UNUSED
, long on UNUSED
,
3255 long nb
, long nn UNUSED
,
3256 const char *func UNUSED
, long funclen UNUSED
)
3259 struct checkdiff_t
*data
= priv
;
3260 data
->lineno
= nb
- 1;
3263 static int checkdiff_consume(void *priv
, char *line
, unsigned long len
)
3265 struct checkdiff_t
*data
= priv
;
3266 int marker_size
= data
->conflict_marker_size
;
3267 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
3268 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
3269 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
3271 const char *line_prefix
;
3274 line_prefix
= diff_line_prefix(data
->o
);
3276 if (line
[0] == '+') {
3279 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
3281 fprintf(data
->o
->file
,
3282 "%s%s:%d: leftover conflict marker\n",
3283 line_prefix
, data
->filename
, data
->lineno
);
3285 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
3288 data
->status
|= bad
;
3289 err
= whitespace_error_string(bad
);
3290 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
3291 line_prefix
, data
->filename
, data
->lineno
, err
);
3293 emit_line(data
->o
, set
, reset
, line
, 1);
3294 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
3295 data
->o
->file
, set
, reset
, ws
);
3296 } else if (line
[0] == ' ') {
3302 static unsigned char *deflate_it(char *data
,
3304 unsigned long *result_size
)
3307 unsigned char *deflated
;
3310 git_deflate_init(&stream
, zlib_compression_level
);
3311 bound
= git_deflate_bound(&stream
, size
);
3312 deflated
= xmalloc(bound
);
3313 stream
.next_out
= deflated
;
3314 stream
.avail_out
= bound
;
3316 stream
.next_in
= (unsigned char *)data
;
3317 stream
.avail_in
= size
;
3318 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
3320 git_deflate_end(&stream
);
3321 *result_size
= stream
.total_out
;
3325 static void emit_binary_diff_body(struct diff_options
*o
,
3326 mmfile_t
*one
, mmfile_t
*two
)
3332 unsigned long orig_size
;
3333 unsigned long delta_size
;
3334 unsigned long deflate_size
;
3335 unsigned long data_size
;
3337 /* We could do deflated delta, or we could do just deflated two,
3338 * whichever is smaller.
3341 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
3342 if (one
->size
&& two
->size
) {
3343 delta
= diff_delta(one
->ptr
, one
->size
,
3344 two
->ptr
, two
->size
,
3345 &delta_size
, deflate_size
);
3347 void *to_free
= delta
;
3348 orig_size
= delta_size
;
3349 delta
= deflate_it(delta
, delta_size
, &delta_size
);
3354 if (delta
&& delta_size
< deflate_size
) {
3355 char *s
= xstrfmt("%"PRIuMAX
, (uintmax_t)orig_size
);
3356 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
3361 data_size
= delta_size
;
3363 char *s
= xstrfmt("%lu", two
->size
);
3364 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
3369 data_size
= deflate_size
;
3372 /* emit data encoded in base85 */
3376 int bytes
= (52 < data_size
) ? 52 : data_size
;
3380 line
[0] = bytes
+ 'A' - 1;
3382 line
[0] = bytes
- 26 + 'a' - 1;
3383 encode_85(line
+ 1, cp
, bytes
);
3384 cp
= (char *) cp
+ bytes
;
3390 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_BODY
,
3393 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_FOOTER
, NULL
, 0, 0);
3397 static void emit_binary_diff(struct diff_options
*o
,
3398 mmfile_t
*one
, mmfile_t
*two
)
3400 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER
, NULL
, 0, 0);
3401 emit_binary_diff_body(o
, one
, two
);
3402 emit_binary_diff_body(o
, two
, one
);
3405 int diff_filespec_is_binary(struct repository
*r
,
3406 struct diff_filespec
*one
)
3408 struct diff_populate_filespec_options dpf_options
= {
3412 if (one
->is_binary
== -1) {
3413 diff_filespec_load_driver(one
, r
->index
);
3414 if (one
->driver
->binary
!= -1)
3415 one
->is_binary
= one
->driver
->binary
;
3417 if (!one
->data
&& DIFF_FILE_VALID(one
))
3418 diff_populate_filespec(r
, one
, &dpf_options
);
3419 if (one
->is_binary
== -1 && one
->data
)
3420 one
->is_binary
= buffer_is_binary(one
->data
,
3422 if (one
->is_binary
== -1)
3426 return one
->is_binary
;
3429 static const struct userdiff_funcname
*
3430 diff_funcname_pattern(struct diff_options
*o
, struct diff_filespec
*one
)
3432 diff_filespec_load_driver(one
, o
->repo
->index
);
3433 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
3436 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
3438 if (!options
->a_prefix
)
3439 options
->a_prefix
= a
;
3440 if (!options
->b_prefix
)
3441 options
->b_prefix
= b
;
3444 void diff_set_noprefix(struct diff_options
*options
)
3446 options
->a_prefix
= options
->b_prefix
= "";
3449 void diff_set_default_prefix(struct diff_options
*options
)
3451 options
->a_prefix
= diff_src_prefix
? diff_src_prefix
: "a/";
3452 options
->b_prefix
= diff_dst_prefix
? diff_dst_prefix
: "b/";
3455 struct userdiff_driver
*get_textconv(struct repository
*r
,
3456 struct diff_filespec
*one
)
3458 if (!DIFF_FILE_VALID(one
))
3461 diff_filespec_load_driver(one
, r
->index
);
3462 return userdiff_get_textconv(r
, one
->driver
);
3465 static struct string_list
*additional_headers(struct diff_options
*o
,
3468 if (!o
->additional_path_headers
)
3470 return strmap_get(o
->additional_path_headers
, path
);
3473 static void add_formatted_header(struct strbuf
*msg
,
3475 const char *line_prefix
,
3479 const char *next
, *newline
;
3481 for (next
= header
; *next
; next
= newline
) {
3482 newline
= strchrnul(next
, '\n');
3483 strbuf_addf(msg
, "%s%s%.*s%s\n", line_prefix
, meta
,
3484 (int)(newline
- next
), next
, reset
);
3490 static void add_formatted_headers(struct strbuf
*msg
,
3491 struct string_list
*more_headers
,
3492 const char *line_prefix
,
3498 for (i
= 0; i
< more_headers
->nr
; i
++)
3499 add_formatted_header(msg
, more_headers
->items
[i
].string
,
3500 line_prefix
, meta
, reset
);
3503 static int diff_filepair_is_phoney(struct diff_filespec
*one
,
3504 struct diff_filespec
*two
)
3507 * This function specifically looks for pairs injected by
3508 * create_filepairs_for_header_only_notifications(). Such
3509 * pairs are "phoney" in that they do not represent any
3510 * content or even mode difference, but were inserted because
3511 * diff_queued_diff previously had no pair associated with
3512 * that path but we needed some pair to avoid losing the
3513 * "remerge CONFLICT" header associated with the path.
3515 return !DIFF_FILE_VALID(one
) && !DIFF_FILE_VALID(two
);
3518 static int set_diff_algorithm(struct diff_options
*opts
,
3521 long value
= parse_algorithm_value(alg
);
3526 /* clear out previous settings */
3527 DIFF_XDL_CLR(opts
, NEED_MINIMAL
);
3528 opts
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3529 opts
->xdl_opts
|= value
;
3534 static void builtin_diff(const char *name_a
,
3536 struct diff_filespec
*one
,
3537 struct diff_filespec
*two
,
3538 const char *xfrm_msg
,
3539 int must_show_header
,
3540 struct diff_options
*o
,
3541 int complete_rewrite
)
3545 char *a_one
, *b_two
;
3546 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
3547 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
3548 const char *a_prefix
, *b_prefix
;
3549 struct userdiff_driver
*textconv_one
= NULL
;
3550 struct userdiff_driver
*textconv_two
= NULL
;
3551 struct strbuf header
= STRBUF_INIT
;
3552 const char *line_prefix
= diff_line_prefix(o
);
3554 diff_set_mnemonic_prefix(o
, "a/", "b/");
3555 if (o
->flags
.reverse_diff
) {
3556 a_prefix
= o
->b_prefix
;
3557 b_prefix
= o
->a_prefix
;
3559 a_prefix
= o
->a_prefix
;
3560 b_prefix
= o
->b_prefix
;
3563 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
3564 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3565 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3566 (!diff_filepair_is_phoney(one
, two
))) {
3567 show_submodule_diff_summary(o
, one
->path
? one
->path
: two
->path
,
3568 &one
->oid
, &two
->oid
,
3569 two
->dirty_submodule
);
3570 o
->found_changes
= 1;
3572 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
3573 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3574 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3575 (!diff_filepair_is_phoney(one
, two
))) {
3576 show_submodule_inline_diff(o
, one
->path
? one
->path
: two
->path
,
3577 &one
->oid
, &two
->oid
,
3578 two
->dirty_submodule
);
3579 o
->found_changes
= 1;
3583 if (o
->flags
.allow_textconv
) {
3584 textconv_one
= get_textconv(o
->repo
, one
);
3585 textconv_two
= get_textconv(o
->repo
, two
);
3588 /* Never use a non-valid filename anywhere if at all possible */
3589 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
3590 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
3592 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
3593 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
3594 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
3595 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
3596 if (diff_filepair_is_phoney(one
, two
)) {
3598 * We should only reach this point for pairs generated from
3599 * create_filepairs_for_header_only_notifications(). For
3600 * these, we want to avoid the "/dev/null" special casing
3601 * above, because we do not want such pairs shown as either
3602 * "new file" or "deleted file" below.
3607 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3608 if (lbl
[0][0] == '/') {
3610 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3612 strbuf_addstr(&header
, xfrm_msg
);
3613 o
->found_changes
= 1;
3614 must_show_header
= 1;
3616 else if (lbl
[1][0] == '/') {
3617 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3619 strbuf_addstr(&header
, xfrm_msg
);
3620 o
->found_changes
= 1;
3621 must_show_header
= 1;
3624 if (one
->mode
!= two
->mode
) {
3625 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3626 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3627 o
->found_changes
= 1;
3628 must_show_header
= 1;
3631 strbuf_addstr(&header
, xfrm_msg
);
3634 * we do not run diff between different kind
3637 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3638 goto free_ab_and_return
;
3639 if (complete_rewrite
&&
3640 (textconv_one
|| !diff_filespec_is_binary(o
->repo
, one
)) &&
3641 (textconv_two
|| !diff_filespec_is_binary(o
->repo
, two
))) {
3642 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3643 header
.buf
, header
.len
, 0);
3644 strbuf_reset(&header
);
3645 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3646 textconv_one
, textconv_two
, o
);
3647 o
->found_changes
= 1;
3648 goto free_ab_and_return
;
3652 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3653 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3655 strbuf_reset(&header
);
3656 goto free_ab_and_return
;
3657 } else if (!o
->flags
.text
&&
3658 ( (!textconv_one
&& diff_filespec_is_binary(o
->repo
, one
)) ||
3659 (!textconv_two
&& diff_filespec_is_binary(o
->repo
, two
)) )) {
3660 struct strbuf sb
= STRBUF_INIT
;
3661 if (!one
->data
&& !two
->data
&&
3662 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3664 if (oideq(&one
->oid
, &two
->oid
)) {
3665 if (must_show_header
)
3666 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3667 header
.buf
, header
.len
,
3669 goto free_ab_and_return
;
3671 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3672 header
.buf
, header
.len
, 0);
3673 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3674 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3675 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3677 strbuf_release(&sb
);
3678 goto free_ab_and_return
;
3680 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3681 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3682 die("unable to read files to diff");
3683 /* Quite common confusing case */
3684 if (mf1
.size
== mf2
.size
&&
3685 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3686 if (must_show_header
)
3687 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3688 header
.buf
, header
.len
, 0);
3689 goto free_ab_and_return
;
3691 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3692 strbuf_reset(&header
);
3693 if (o
->flags
.binary
)
3694 emit_binary_diff(o
, &mf1
, &mf2
);
3696 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3697 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3698 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3700 strbuf_release(&sb
);
3702 o
->found_changes
= 1;
3704 /* Crazy xdl interfaces.. */
3705 const char *diffopts
;
3709 struct emit_callback ecbdata
;
3710 const struct userdiff_funcname
*pe
;
3712 if (must_show_header
) {
3713 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3714 header
.buf
, header
.len
, 0);
3715 strbuf_reset(&header
);
3718 mf1
.size
= fill_textconv(o
->repo
, textconv_one
, one
, &mf1
.ptr
);
3719 mf2
.size
= fill_textconv(o
->repo
, textconv_two
, two
, &mf2
.ptr
);
3721 pe
= diff_funcname_pattern(o
, one
);
3723 pe
= diff_funcname_pattern(o
, two
);
3725 memset(&xpp
, 0, sizeof(xpp
));
3726 memset(&xecfg
, 0, sizeof(xecfg
));
3727 memset(&ecbdata
, 0, sizeof(ecbdata
));
3728 if (o
->flags
.suppress_diff_headers
)
3730 ecbdata
.label_path
= lbl
;
3731 ecbdata
.color_diff
= want_color(o
->use_color
);
3732 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
3733 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3734 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3736 if (header
.len
&& !o
->flags
.suppress_diff_headers
)
3737 ecbdata
.header
= &header
;
3738 xpp
.flags
= o
->xdl_opts
;
3739 xpp
.ignore_regex
= o
->ignore_regex
;
3740 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3741 xpp
.anchors
= o
->anchors
;
3742 xpp
.anchors_nr
= o
->anchors_nr
;
3743 xecfg
.ctxlen
= o
->context
;
3744 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3745 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3746 if (o
->flags
.funccontext
)
3747 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3749 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3751 diffopts
= getenv("GIT_DIFF_OPTS");
3754 else if (skip_prefix(diffopts
, "--unified=", &v
))
3755 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3756 else if (skip_prefix(diffopts
, "-u", &v
))
3757 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3760 init_diff_words_data(&ecbdata
, o
, one
, two
);
3761 if (xdi_diff_outf(&mf1
, &mf2
, NULL
, fn_out_consume
,
3762 &ecbdata
, &xpp
, &xecfg
))
3763 die("unable to generate diff for %s", one
->path
);
3765 free_diff_words_data(&ecbdata
);
3770 xdiff_clear_find_func(&xecfg
);
3774 strbuf_release(&header
);
3775 diff_free_filespec_data(one
);
3776 diff_free_filespec_data(two
);
3782 static const char *get_compact_summary(const struct diff_filepair
*p
, int is_renamed
)
3785 if (p
->status
== DIFF_STATUS_ADDED
) {
3786 if (S_ISLNK(p
->two
->mode
))
3788 else if ((p
->two
->mode
& 0777) == 0755)
3792 } else if (p
->status
== DIFF_STATUS_DELETED
)
3795 if (S_ISLNK(p
->one
->mode
) && !S_ISLNK(p
->two
->mode
))
3797 else if (!S_ISLNK(p
->one
->mode
) && S_ISLNK(p
->two
->mode
))
3799 else if ((p
->one
->mode
& 0777) == 0644 &&
3800 (p
->two
->mode
& 0777) == 0755)
3802 else if ((p
->one
->mode
& 0777) == 0755 &&
3803 (p
->two
->mode
& 0777) == 0644)
3808 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3809 struct diff_filespec
*one
,
3810 struct diff_filespec
*two
,
3811 struct diffstat_t
*diffstat
,
3812 struct diff_options
*o
,
3813 struct diff_filepair
*p
)
3816 struct diffstat_file
*data
;
3818 int complete_rewrite
= 0;
3820 if (!DIFF_PAIR_UNMERGED(p
)) {
3821 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3822 complete_rewrite
= 1;
3825 data
= diffstat_add(diffstat
, name_a
, name_b
);
3826 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3827 if (o
->flags
.stat_with_summary
)
3828 data
->comments
= get_compact_summary(p
, data
->is_renamed
);
3831 data
->is_unmerged
= 1;
3835 /* saves some reads if true, not a guarantee of diff outcome */
3836 may_differ
= !(one
->oid_valid
&& two
->oid_valid
&&
3837 oideq(&one
->oid
, &two
->oid
));
3839 if (diff_filespec_is_binary(o
->repo
, one
) ||
3840 diff_filespec_is_binary(o
->repo
, two
)) {
3841 data
->is_binary
= 1;
3846 data
->added
= diff_filespec_size(o
->repo
, two
);
3847 data
->deleted
= diff_filespec_size(o
->repo
, one
);
3851 else if (complete_rewrite
) {
3852 diff_populate_filespec(o
->repo
, one
, NULL
);
3853 diff_populate_filespec(o
->repo
, two
, NULL
);
3854 data
->deleted
= count_lines(one
->data
, one
->size
);
3855 data
->added
= count_lines(two
->data
, two
->size
);
3858 else if (may_differ
) {
3859 /* Crazy xdl interfaces.. */
3863 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3864 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3865 die("unable to read files to diff");
3867 memset(&xpp
, 0, sizeof(xpp
));
3868 memset(&xecfg
, 0, sizeof(xecfg
));
3869 xpp
.flags
= o
->xdl_opts
;
3870 xpp
.ignore_regex
= o
->ignore_regex
;
3871 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3872 xpp
.anchors
= o
->anchors
;
3873 xpp
.anchors_nr
= o
->anchors_nr
;
3874 xecfg
.ctxlen
= o
->context
;
3875 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3876 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
3877 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
3878 diffstat_consume
, diffstat
, &xpp
, &xecfg
))
3879 die("unable to generate diffstat for %s", one
->path
);
3881 if (DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
)) {
3882 struct diffstat_file
*file
=
3883 diffstat
->files
[diffstat
->nr
- 1];
3885 * Omit diffstats of modified files where nothing changed.
3886 * Even if may_differ, this might be the case due to
3887 * ignoring whitespace changes, etc.
3889 * But note that we special-case additions, deletions,
3890 * renames, and mode changes as adding an empty file,
3891 * for example is still of interest.
3893 if ((p
->status
== DIFF_STATUS_MODIFIED
)
3896 && one
->mode
== two
->mode
) {
3897 free_diffstat_file(file
);
3903 diff_free_filespec_data(one
);
3904 diff_free_filespec_data(two
);
3907 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3908 const char *attr_path
,
3909 struct diff_filespec
*one
,
3910 struct diff_filespec
*two
,
3911 struct diff_options
*o
)
3914 struct checkdiff_t data
;
3919 memset(&data
, 0, sizeof(data
));
3920 data
.filename
= name_b
? name_b
: name_a
;
3923 data
.ws_rule
= whitespace_rule(o
->repo
->index
, attr_path
);
3924 data
.conflict_marker_size
= ll_merge_marker_size(o
->repo
->index
, attr_path
);
3926 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3927 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3928 die("unable to read files to diff");
3931 * All the other codepaths check both sides, but not checking
3932 * the "old" side here is deliberate. We are checking the newly
3933 * introduced changes, and as long as the "new" side is text, we
3934 * can and should check what it introduces.
3936 if (diff_filespec_is_binary(o
->repo
, two
))
3937 goto free_and_return
;
3939 /* Crazy xdl interfaces.. */
3943 memset(&xpp
, 0, sizeof(xpp
));
3944 memset(&xecfg
, 0, sizeof(xecfg
));
3945 xecfg
.ctxlen
= 1; /* at least one context line */
3947 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume_hunk
,
3948 checkdiff_consume
, &data
,
3950 die("unable to generate checkdiff for %s", one
->path
);
3952 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3953 struct emit_callback ecbdata
;
3956 ecbdata
.ws_rule
= data
.ws_rule
;
3957 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3958 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3963 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3964 fprintf(o
->file
, "%s:%d: %s.\n",
3965 data
.filename
, blank_at_eof
, err
);
3966 data
.status
= 1; /* report errors */
3971 diff_free_filespec_data(one
);
3972 diff_free_filespec_data(two
);
3974 o
->flags
.check_failed
= 1;
3977 struct diff_filespec
*alloc_filespec(const char *path
)
3979 struct diff_filespec
*spec
;
3981 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3983 spec
->is_binary
= -1;
3987 void free_filespec(struct diff_filespec
*spec
)
3989 if (!--spec
->count
) {
3990 diff_free_filespec_data(spec
);
3995 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3996 int oid_valid
, unsigned short mode
)
3999 spec
->mode
= canon_mode(mode
);
4000 oidcpy(&spec
->oid
, oid
);
4001 spec
->oid_valid
= oid_valid
;
4006 * Given a name and sha1 pair, if the index tells us the file in
4007 * the work tree has that object contents, return true, so that
4008 * prepare_temp_file() does not have to inflate and extract.
4010 static int reuse_worktree_file(struct index_state
*istate
,
4012 const struct object_id
*oid
,
4015 const struct cache_entry
*ce
;
4020 * We do not read the cache ourselves here, because the
4021 * benchmark with my previous version that always reads cache
4022 * shows that it makes things worse for diff-tree comparing
4023 * two linux-2.6 kernel trees in an already checked out work
4024 * tree. This is because most diff-tree comparisons deal with
4025 * only a small number of files, while reading the cache is
4026 * expensive for a large project, and its cost outweighs the
4027 * savings we get by not inflating the object to a temporary
4028 * file. Practically, this code only helps when we are used
4029 * by diff-cache --cached, which does read the cache before
4035 /* We want to avoid the working directory if our caller
4036 * doesn't need the data in a normal file, this system
4037 * is rather slow with its stat/open/mmap/close syscalls,
4038 * and the object is contained in a pack file. The pack
4039 * is probably already open and will be faster to obtain
4040 * the data through than the working directory. Loose
4041 * objects however would tend to be slower as they need
4042 * to be individually opened and inflated.
4044 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_object_pack(oid
))
4048 * Similarly, if we'd have to convert the file contents anyway, that
4049 * makes the optimization not worthwhile.
4051 if (!want_file
&& would_convert_to_git(istate
, name
))
4055 * If this path does not match our sparse-checkout definition,
4056 * then the file will not be in the working directory.
4058 if (!path_in_sparse_checkout(name
, istate
))
4062 pos
= index_name_pos(istate
, name
, len
);
4065 ce
= istate
->cache
[pos
];
4068 * This is not the sha1 we are looking for, or
4069 * unreusable because it is not a regular file.
4071 if (!oideq(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
4075 * If ce is marked as "assume unchanged", there is no
4076 * guarantee that work tree matches what we are looking for.
4078 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
4082 * If ce matches the file in the work tree, we can reuse it.
4084 if (ce_uptodate(ce
) ||
4085 (!lstat(name
, &st
) && !ie_match_stat(istate
, ce
, &st
, 0)))
4091 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
4093 struct strbuf buf
= STRBUF_INIT
;
4094 const char *dirty
= "";
4096 /* Are we looking at the work tree? */
4097 if (s
->dirty_submodule
)
4100 strbuf_addf(&buf
, "Subproject commit %s%s\n",
4101 oid_to_hex(&s
->oid
), dirty
);
4105 strbuf_release(&buf
);
4107 s
->data
= strbuf_detach(&buf
, NULL
);
4114 * While doing rename detection and pickaxe operation, we may need to
4115 * grab the data for the blob (or file) for our own in-core comparison.
4116 * diff_filespec has data and size fields for this purpose.
4118 int diff_populate_filespec(struct repository
*r
,
4119 struct diff_filespec
*s
,
4120 const struct diff_populate_filespec_options
*options
)
4122 int size_only
= options
? options
->check_size_only
: 0;
4123 int check_binary
= options
? options
->check_binary
: 0;
4125 int conv_flags
= global_conv_flags_eol
;
4127 * demote FAIL to WARN to allow inspecting the situation
4128 * instead of refusing.
4130 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
4131 conv_flags
= CONV_EOL_RNDTRP_WARN
;
4133 if (!DIFF_FILE_VALID(s
))
4134 die("internal error: asking to populate invalid file.");
4135 if (S_ISDIR(s
->mode
))
4141 if (size_only
&& 0 < s
->size
)
4144 if (S_ISGITLINK(s
->mode
))
4145 return diff_populate_gitlink(s
, size_only
);
4147 if (!s
->oid_valid
||
4148 reuse_worktree_file(r
->index
, s
->path
, &s
->oid
, 0)) {
4149 struct strbuf buf
= STRBUF_INIT
;
4153 if (lstat(s
->path
, &st
) < 0) {
4157 s
->data
= (char *)"";
4161 s
->size
= xsize_t(st
.st_size
);
4164 if (S_ISLNK(st
.st_mode
)) {
4165 struct strbuf sb
= STRBUF_INIT
;
4167 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
4170 s
->data
= strbuf_detach(&sb
, NULL
);
4176 * Even if the caller would be happy with getting
4177 * only the size, we cannot return early at this
4178 * point if the path requires us to run the content
4181 if (size_only
&& !would_convert_to_git(r
->index
, s
->path
))
4185 * Note: this check uses xsize_t(st.st_size) that may
4186 * not be the true size of the blob after it goes
4187 * through convert_to_git(). This may not strictly be
4188 * correct, but the whole point of big_file_threshold
4189 * and is_binary check being that we want to avoid
4190 * opening the file and inspecting the contents, this
4194 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4198 fd
= open(s
->path
, O_RDONLY
);
4201 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
4203 s
->should_munmap
= 1;
4206 * Convert from working tree format to canonical git format
4208 if (convert_to_git(r
->index
, s
->path
, s
->data
, s
->size
, &buf
, conv_flags
)) {
4210 munmap(s
->data
, s
->size
);
4211 s
->should_munmap
= 0;
4212 s
->data
= strbuf_detach(&buf
, &size
);
4218 struct object_info info
= {
4222 if (!(size_only
|| check_binary
))
4224 * Set contentp, since there is no chance that merely
4225 * the size is sufficient.
4227 info
.contentp
= &s
->data
;
4229 if (options
&& options
->missing_object_cb
) {
4230 if (!oid_object_info_extended(r
, &s
->oid
, &info
,
4231 OBJECT_INFO_LOOKUP_REPLACE
|
4232 OBJECT_INFO_SKIP_FETCH_OBJECT
))
4234 options
->missing_object_cb(options
->missing_object_data
);
4236 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4237 OBJECT_INFO_LOOKUP_REPLACE
))
4238 die("unable to read %s", oid_to_hex(&s
->oid
));
4241 if (size_only
|| check_binary
) {
4244 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4249 if (!info
.contentp
) {
4250 info
.contentp
= &s
->data
;
4251 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4252 OBJECT_INFO_LOOKUP_REPLACE
))
4253 die("unable to read %s", oid_to_hex(&s
->oid
));
4260 void diff_free_filespec_blob(struct diff_filespec
*s
)
4264 else if (s
->should_munmap
)
4265 munmap(s
->data
, s
->size
);
4267 if (s
->should_free
|| s
->should_munmap
) {
4268 s
->should_free
= s
->should_munmap
= 0;
4273 void diff_free_filespec_data(struct diff_filespec
*s
)
4278 diff_free_filespec_blob(s
);
4279 FREE_AND_NULL(s
->cnt_data
);
4282 static void prep_temp_blob(struct index_state
*istate
,
4283 const char *path
, struct diff_tempfile
*temp
,
4286 const struct object_id
*oid
,
4289 struct strbuf buf
= STRBUF_INIT
;
4290 char *path_dup
= xstrdup(path
);
4291 const char *base
= basename(path_dup
);
4292 struct checkout_metadata meta
;
4294 init_checkout_metadata(&meta
, NULL
, NULL
, oid
);
4296 temp
->tempfile
= mks_tempfile_dt("git-blob-XXXXXX", base
);
4297 if (!temp
->tempfile
)
4298 die_errno("unable to create temp-file");
4299 if (convert_to_working_tree(istate
, path
,
4300 (const char *)blob
, (size_t)size
, &buf
, &meta
)) {
4304 if (write_in_full(temp
->tempfile
->fd
, blob
, size
) < 0 ||
4305 close_tempfile_gently(temp
->tempfile
))
4306 die_errno("unable to write temp-file");
4307 temp
->name
= get_tempfile_path(temp
->tempfile
);
4308 oid_to_hex_r(temp
->hex
, oid
);
4309 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
4310 strbuf_release(&buf
);
4314 static struct diff_tempfile
*prepare_temp_file(struct repository
*r
,
4315 struct diff_filespec
*one
)
4317 struct diff_tempfile
*temp
= claim_diff_tempfile();
4319 if (!DIFF_FILE_VALID(one
)) {
4321 /* A '-' entry produces this for file-2, and
4322 * a '+' entry produces this for file-1.
4324 temp
->name
= "/dev/null";
4325 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
4326 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
4330 if (!S_ISGITLINK(one
->mode
) &&
4332 reuse_worktree_file(r
->index
, one
->path
, &one
->oid
, 1))) {
4334 if (lstat(one
->path
, &st
) < 0) {
4335 if (errno
== ENOENT
)
4336 goto not_a_valid_file
;
4337 die_errno("stat(%s)", one
->path
);
4339 if (S_ISLNK(st
.st_mode
)) {
4340 struct strbuf sb
= STRBUF_INIT
;
4341 if (strbuf_readlink(&sb
, one
->path
, st
.st_size
) < 0)
4342 die_errno("readlink(%s)", one
->path
);
4343 prep_temp_blob(r
->index
, one
->path
, temp
, sb
.buf
, sb
.len
,
4345 &one
->oid
: null_oid()),
4347 one
->mode
: S_IFLNK
));
4348 strbuf_release(&sb
);
4351 /* we can borrow from the file in the work tree */
4352 temp
->name
= one
->path
;
4353 if (!one
->oid_valid
)
4354 oid_to_hex_r(temp
->hex
, null_oid());
4356 oid_to_hex_r(temp
->hex
, &one
->oid
);
4357 /* Even though we may sometimes borrow the
4358 * contents from the work tree, we always want
4359 * one->mode. mode is trustworthy even when
4360 * !(one->oid_valid), as long as
4361 * DIFF_FILE_VALID(one).
4363 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
4368 if (diff_populate_filespec(r
, one
, NULL
))
4369 die("cannot read data blob for %s", one
->path
);
4370 prep_temp_blob(r
->index
, one
->path
, temp
,
4371 one
->data
, one
->size
,
4372 &one
->oid
, one
->mode
);
4377 static void add_external_diff_name(struct repository
*r
,
4378 struct strvec
*argv
,
4379 struct diff_filespec
*df
)
4381 struct diff_tempfile
*temp
= prepare_temp_file(r
, df
);
4382 strvec_push(argv
, temp
->name
);
4383 strvec_push(argv
, temp
->hex
);
4384 strvec_push(argv
, temp
->mode
);
4387 /* An external diff command takes:
4389 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4390 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4393 static void run_external_diff(const struct external_diff
*pgm
,
4396 struct diff_filespec
*one
,
4397 struct diff_filespec
*two
,
4398 const char *xfrm_msg
,
4399 struct diff_options
*o
)
4401 struct child_process cmd
= CHILD_PROCESS_INIT
;
4402 struct diff_queue_struct
*q
= &diff_queued_diff
;
4403 int quiet
= !(o
->output_format
& DIFF_FORMAT_PATCH
);
4407 * Trivial equality is handled by diff_unmodified_pair() before
4408 * we get here. If we don't need to show the diff and the
4409 * external diff program lacks the ability to tell us whether
4410 * it's empty then we consider it non-empty without even asking.
4412 if (!pgm
->trust_exit_code
&& quiet
) {
4413 o
->found_changes
= 1;
4417 strvec_push(&cmd
.args
, pgm
->cmd
);
4418 strvec_push(&cmd
.args
, name
);
4421 add_external_diff_name(o
->repo
, &cmd
.args
, one
);
4422 add_external_diff_name(o
->repo
, &cmd
.args
, two
);
4424 strvec_push(&cmd
.args
, other
);
4426 strvec_push(&cmd
.args
, xfrm_msg
);
4430 strvec_pushf(&cmd
.env
, "GIT_DIFF_PATH_COUNTER=%d",
4431 ++o
->diff_path_counter
);
4432 strvec_pushf(&cmd
.env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
4434 diff_free_filespec_data(one
);
4435 diff_free_filespec_data(two
);
4437 cmd
.no_stdout
= quiet
;
4438 rc
= run_command(&cmd
);
4439 if (!pgm
->trust_exit_code
&& rc
== 0)
4440 o
->found_changes
= 1;
4441 else if (pgm
->trust_exit_code
&& rc
== 0)
4443 else if (pgm
->trust_exit_code
&& rc
== 1)
4444 o
->found_changes
= 1;
4446 die(_("external diff died, stopping at %s"), name
);
4451 static int similarity_index(struct diff_filepair
*p
)
4453 return p
->score
* 100 / MAX_SCORE
;
4456 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
4458 if (startup_info
->have_repository
)
4459 return repo_find_unique_abbrev(the_repository
, oid
, abbrev
);
4461 char *hex
= oid_to_hex(oid
);
4463 abbrev
= FALLBACK_DEFAULT_ABBREV
;
4464 if (abbrev
> the_hash_algo
->hexsz
)
4465 BUG("oid abbreviation out of range: %d", abbrev
);
4472 static void fill_metainfo(struct strbuf
*msg
,
4475 struct diff_filespec
*one
,
4476 struct diff_filespec
*two
,
4477 struct diff_options
*o
,
4478 struct diff_filepair
*p
,
4479 int *must_show_header
,
4482 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
4483 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
4484 const char *line_prefix
= diff_line_prefix(o
);
4485 struct string_list
*more_headers
= NULL
;
4487 *must_show_header
= 1;
4488 strbuf_init(msg
, PATH_MAX
* 2 + 300);
4489 switch (p
->status
) {
4490 case DIFF_STATUS_COPIED
:
4491 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4492 line_prefix
, set
, similarity_index(p
));
4493 strbuf_addf(msg
, "%s\n%s%scopy from ",
4494 reset
, line_prefix
, set
);
4495 quote_c_style(name
, msg
, NULL
, 0);
4496 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
4497 quote_c_style(other
, msg
, NULL
, 0);
4498 strbuf_addf(msg
, "%s\n", reset
);
4500 case DIFF_STATUS_RENAMED
:
4501 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4502 line_prefix
, set
, similarity_index(p
));
4503 strbuf_addf(msg
, "%s\n%s%srename from ",
4504 reset
, line_prefix
, set
);
4505 quote_c_style(name
, msg
, NULL
, 0);
4506 strbuf_addf(msg
, "%s\n%s%srename to ",
4507 reset
, line_prefix
, set
);
4508 quote_c_style(other
, msg
, NULL
, 0);
4509 strbuf_addf(msg
, "%s\n", reset
);
4511 case DIFF_STATUS_MODIFIED
:
4513 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
4515 set
, similarity_index(p
), reset
);
4520 *must_show_header
= 0;
4522 if ((more_headers
= additional_headers(o
, name
))) {
4523 add_formatted_headers(msg
, more_headers
,
4524 line_prefix
, set
, reset
);
4525 *must_show_header
= 1;
4527 if (one
&& two
&& !oideq(&one
->oid
, &two
->oid
)) {
4528 const unsigned hexsz
= the_hash_algo
->hexsz
;
4529 int abbrev
= o
->abbrev
? o
->abbrev
: DEFAULT_ABBREV
;
4531 if (o
->flags
.full_index
)
4534 if (o
->flags
.binary
) {
4536 if ((!fill_mmfile(o
->repo
, &mf
, one
) &&
4537 diff_filespec_is_binary(o
->repo
, one
)) ||
4538 (!fill_mmfile(o
->repo
, &mf
, two
) &&
4539 diff_filespec_is_binary(o
->repo
, two
)))
4542 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
4543 diff_abbrev_oid(&one
->oid
, abbrev
),
4544 diff_abbrev_oid(&two
->oid
, abbrev
));
4545 if (one
->mode
== two
->mode
)
4546 strbuf_addf(msg
, " %06o", one
->mode
);
4547 strbuf_addf(msg
, "%s\n", reset
);
4551 static void run_diff_cmd(const struct external_diff
*pgm
,
4554 const char *attr_path
,
4555 struct diff_filespec
*one
,
4556 struct diff_filespec
*two
,
4558 struct diff_options
*o
,
4559 struct diff_filepair
*p
)
4561 const char *xfrm_msg
= NULL
;
4562 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
4563 int must_show_header
= 0;
4564 struct userdiff_driver
*drv
= NULL
;
4566 if (o
->flags
.allow_external
|| !o
->ignore_driver_algorithm
)
4567 drv
= userdiff_find_by_path(o
->repo
->index
, attr_path
);
4569 if (o
->flags
.allow_external
&& drv
&& drv
->external
.cmd
)
4570 pgm
= &drv
->external
;
4574 * don't use colors when the header is intended for an
4575 * external diff driver
4577 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
4579 want_color(o
->use_color
) && !pgm
);
4580 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
4584 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
, o
);
4588 if (!o
->ignore_driver_algorithm
&& drv
&& drv
->algorithm
)
4589 set_diff_algorithm(o
, drv
->algorithm
);
4591 builtin_diff(name
, other
? other
: name
,
4592 one
, two
, xfrm_msg
, must_show_header
,
4593 o
, complete_rewrite
);
4594 if (p
->status
== DIFF_STATUS_COPIED
||
4595 p
->status
== DIFF_STATUS_RENAMED
)
4596 o
->found_changes
= 1;
4598 fprintf(o
->file
, "* Unmerged path %s\n", name
);
4599 o
->found_changes
= 1;
4603 static void diff_fill_oid_info(struct diff_filespec
*one
, struct index_state
*istate
)
4605 if (DIFF_FILE_VALID(one
)) {
4606 if (!one
->oid_valid
) {
4608 if (one
->is_stdin
) {
4609 oidclr(&one
->oid
, the_repository
->hash_algo
);
4612 if (lstat(one
->path
, &st
) < 0)
4613 die_errno("stat '%s'", one
->path
);
4614 if (index_path(istate
, &one
->oid
, one
->path
, &st
, 0))
4615 die("cannot hash %s", one
->path
);
4619 oidclr(&one
->oid
, the_repository
->hash_algo
);
4622 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
4624 /* Strip the prefix but do not molest /dev/null and absolute paths */
4625 if (*namep
&& !is_absolute_path(*namep
)) {
4626 *namep
+= prefix_length
;
4630 if (*otherp
&& !is_absolute_path(*otherp
)) {
4631 *otherp
+= prefix_length
;
4632 if (**otherp
== '/')
4637 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
4639 const struct external_diff
*pgm
= external_diff();
4641 struct diff_filespec
*one
= p
->one
;
4642 struct diff_filespec
*two
= p
->two
;
4645 const char *attr_path
;
4648 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
4650 if (o
->prefix_length
)
4651 strip_prefix(o
->prefix_length
, &name
, &other
);
4653 if (!o
->flags
.allow_external
)
4656 if (DIFF_PAIR_UNMERGED(p
)) {
4657 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
4658 NULL
, NULL
, NULL
, o
, p
);
4662 diff_fill_oid_info(one
, o
->repo
->index
);
4663 diff_fill_oid_info(two
, o
->repo
->index
);
4666 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4667 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4669 * a filepair that changes between file and symlink
4670 * needs to be split into deletion and creation.
4672 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4673 run_diff_cmd(NULL
, name
, other
, attr_path
,
4677 strbuf_release(&msg
);
4679 null
= alloc_filespec(one
->path
);
4680 run_diff_cmd(NULL
, name
, other
, attr_path
,
4681 null
, two
, &msg
, o
, p
);
4685 run_diff_cmd(pgm
, name
, other
, attr_path
,
4686 one
, two
, &msg
, o
, p
);
4688 strbuf_release(&msg
);
4691 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4692 struct diffstat_t
*diffstat
)
4697 if (!o
->ignore_driver_algorithm
) {
4698 struct userdiff_driver
*drv
= userdiff_find_by_path(o
->repo
->index
,
4701 if (drv
&& drv
->algorithm
)
4702 set_diff_algorithm(o
, drv
->algorithm
);
4705 if (DIFF_PAIR_UNMERGED(p
)) {
4707 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
,
4712 name
= p
->one
->path
;
4713 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4715 if (o
->prefix_length
)
4716 strip_prefix(o
->prefix_length
, &name
, &other
);
4718 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4719 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4721 builtin_diffstat(name
, other
, p
->one
, p
->two
,
4725 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4729 const char *attr_path
;
4731 if (DIFF_PAIR_UNMERGED(p
)) {
4736 name
= p
->one
->path
;
4737 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4738 attr_path
= other
? other
: name
;
4740 if (o
->prefix_length
)
4741 strip_prefix(o
->prefix_length
, &name
, &other
);
4743 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4744 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4746 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4749 void repo_diff_setup(struct repository
*r
, struct diff_options
*options
)
4751 memcpy(options
, &default_diff_options
, sizeof(*options
));
4753 options
->file
= stdout
;
4756 options
->output_indicators
[OUTPUT_INDICATOR_NEW
] = '+';
4757 options
->output_indicators
[OUTPUT_INDICATOR_OLD
] = '-';
4758 options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
] = ' ';
4759 options
->abbrev
= DEFAULT_ABBREV
;
4760 options
->line_termination
= '\n';
4761 options
->break_opt
= -1;
4762 options
->rename_limit
= -1;
4763 options
->dirstat_permille
= diff_dirstat_permille_default
;
4764 options
->context
= diff_context_default
;
4765 options
->interhunkcontext
= diff_interhunk_context_default
;
4766 options
->ws_error_highlight
= ws_error_highlight_default
;
4767 options
->flags
.rename_empty
= 1;
4768 options
->flags
.relative_name
= diff_relative
;
4769 options
->objfind
= NULL
;
4771 /* pathchange left =NULL by default */
4772 options
->change
= diff_change
;
4773 options
->add_remove
= diff_addremove
;
4774 options
->use_color
= diff_use_color_default
;
4775 options
->detect_rename
= diff_detect_rename_default
;
4776 options
->xdl_opts
|= diff_algorithm
;
4777 if (diff_indent_heuristic
)
4778 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4780 options
->orderfile
= diff_order_file_cfg
;
4782 if (!options
->flags
.ignore_submodule_set
)
4783 options
->flags
.ignore_untracked_in_submodules
= 1;
4785 if (diff_no_prefix
) {
4786 diff_set_noprefix(options
);
4787 } else if (!diff_mnemonic_prefix
) {
4788 diff_set_default_prefix(options
);
4791 options
->color_moved
= diff_color_moved_default
;
4792 options
->color_moved_ws_handling
= diff_color_moved_ws_default
;
4795 static const char diff_status_letters
[] = {
4798 DIFF_STATUS_DELETED
,
4799 DIFF_STATUS_MODIFIED
,
4800 DIFF_STATUS_RENAMED
,
4801 DIFF_STATUS_TYPE_CHANGED
,
4802 DIFF_STATUS_UNKNOWN
,
4803 DIFF_STATUS_UNMERGED
,
4804 DIFF_STATUS_FILTER_AON
,
4805 DIFF_STATUS_FILTER_BROKEN
,
4809 static unsigned int filter_bit
['Z' + 1];
4811 static void prepare_filter_bits(void)
4815 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4816 for (i
= 0; diff_status_letters
[i
]; i
++)
4817 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4821 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4823 return opt
->filter
& filter_bit
[(int) status
];
4826 unsigned diff_filter_bit(char status
)
4828 prepare_filter_bits();
4829 return filter_bit
[(int) status
];
4832 int diff_check_follow_pathspec(struct pathspec
*ps
, int die_on_error
)
4834 unsigned forbidden_magic
;
4838 die(_("--follow requires exactly one pathspec"));
4842 forbidden_magic
= ps
->items
[0].magic
& ~(PATHSPEC_FROMTOP
|
4844 if (forbidden_magic
) {
4846 struct strbuf sb
= STRBUF_INIT
;
4847 pathspec_magic_names(forbidden_magic
, &sb
);
4848 die(_("pathspec magic not supported by --follow: %s"),
4857 void diff_setup_done(struct diff_options
*options
)
4859 unsigned check_mask
= DIFF_FORMAT_NAME
|
4860 DIFF_FORMAT_NAME_STATUS
|
4861 DIFF_FORMAT_CHECKDIFF
|
4862 DIFF_FORMAT_NO_OUTPUT
;
4864 * This must be signed because we're comparing against a potentially
4867 const int hexsz
= the_hash_algo
->hexsz
;
4869 if (options
->set_default
)
4870 options
->set_default(options
);
4872 if (HAS_MULTI_BITS(options
->output_format
& check_mask
))
4873 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
4874 "--name-only", "--name-status", "--check", "-s");
4876 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
4877 die(_("options '%s', '%s', and '%s' cannot be used together"),
4878 "-G", "-S", "--find-object");
4880 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_G_REGEX_MASK
))
4881 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"),
4882 "-G", "--pickaxe-regex", "--pickaxe-regex", "-S");
4884 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK
))
4885 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"),
4886 "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S");
4889 * Most of the time we can say "there are changes"
4890 * only by checking if there are changed paths, but
4891 * --ignore-whitespace* options force us to look
4895 if ((options
->xdl_opts
& XDF_WHITESPACE_FLAGS
) ||
4896 options
->ignore_regex_nr
)
4897 options
->flags
.diff_from_contents
= 1;
4899 options
->flags
.diff_from_contents
= 0;
4901 if (options
->flags
.find_copies_harder
)
4902 options
->detect_rename
= DIFF_DETECT_COPY
;
4904 if (!options
->flags
.relative_name
)
4905 options
->prefix
= NULL
;
4906 if (options
->prefix
)
4907 options
->prefix_length
= strlen(options
->prefix
);
4909 options
->prefix_length
= 0;
4912 * --name-only, --name-status, --checkdiff, and -s
4913 * turn other output format off.
4915 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4916 DIFF_FORMAT_NAME_STATUS
|
4917 DIFF_FORMAT_CHECKDIFF
|
4918 DIFF_FORMAT_NO_OUTPUT
))
4919 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4920 DIFF_FORMAT_NUMSTAT
|
4921 DIFF_FORMAT_DIFFSTAT
|
4922 DIFF_FORMAT_SHORTSTAT
|
4923 DIFF_FORMAT_DIRSTAT
|
4924 DIFF_FORMAT_SUMMARY
|
4928 * These cases always need recursive; we do not drop caller-supplied
4929 * recursive bits for other formats here.
4931 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4932 DIFF_FORMAT_NUMSTAT
|
4933 DIFF_FORMAT_DIFFSTAT
|
4934 DIFF_FORMAT_SHORTSTAT
|
4935 DIFF_FORMAT_DIRSTAT
|
4936 DIFF_FORMAT_SUMMARY
|
4937 DIFF_FORMAT_CHECKDIFF
))
4938 options
->flags
.recursive
= 1;
4940 * Also pickaxe would not work very well if you do not say recursive
4942 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
4943 options
->flags
.recursive
= 1;
4945 * When patches are generated, submodules diffed against the work tree
4946 * must be checked for dirtiness too so it can be shown in the output
4948 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4949 options
->flags
.dirty_submodules
= 1;
4951 if (options
->detect_rename
&& options
->rename_limit
< 0)
4952 options
->rename_limit
= diff_rename_limit_default
;
4953 if (hexsz
< options
->abbrev
)
4954 options
->abbrev
= hexsz
; /* full */
4957 * It does not make sense to show the first hit we happened
4958 * to have found. It does not make sense not to return with
4959 * exit code in such a case either.
4961 if (options
->flags
.quick
) {
4962 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4963 options
->flags
.exit_with_status
= 1;
4967 * External diffs could declare non-identical contents equal
4968 * (think diff --ignore-space-change).
4970 if (options
->flags
.allow_external
&& options
->flags
.exit_with_status
)
4971 options
->flags
.diff_from_contents
= 1;
4973 options
->diff_path_counter
= 0;
4975 if (options
->flags
.follow_renames
)
4976 diff_check_follow_pathspec(&options
->pathspec
, 1);
4978 if (!options
->use_color
||
4979 (options
->flags
.allow_external
&& external_diff()))
4980 options
->color_moved
= 0;
4982 if (options
->filter_not
) {
4983 if (!options
->filter
)
4984 options
->filter
= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4985 options
->filter
&= ~options
->filter_not
;
4989 int parse_long_opt(const char *opt
, const char **argv
,
4990 const char **optarg
)
4992 const char *arg
= argv
[0];
4993 if (!skip_prefix(arg
, "--", &arg
))
4995 if (!skip_prefix(arg
, opt
, &arg
))
4997 if (*arg
== '=') { /* stuck form: --option=value */
5003 /* separate form: --option value */
5005 die("Option '--%s' requires a value", opt
);
5010 static int diff_opt_stat(const struct option
*opt
, const char *value
, int unset
)
5012 struct diff_options
*options
= opt
->value
;
5013 int width
= options
->stat_width
;
5014 int name_width
= options
->stat_name_width
;
5015 int graph_width
= options
->stat_graph_width
;
5016 int count
= options
->stat_count
;
5019 BUG_ON_OPT_NEG(unset
);
5021 if (!strcmp(opt
->long_name
, "stat")) {
5023 width
= strtoul(value
, &end
, 10);
5025 name_width
= strtoul(end
+1, &end
, 10);
5027 count
= strtoul(end
+1, &end
, 10);
5029 return error(_("invalid --stat value: %s"), value
);
5031 } else if (!strcmp(opt
->long_name
, "stat-width")) {
5032 width
= strtoul(value
, &end
, 10);
5034 return error(_("%s expects a numerical value"),
5036 } else if (!strcmp(opt
->long_name
, "stat-name-width")) {
5037 name_width
= strtoul(value
, &end
, 10);
5039 return error(_("%s expects a numerical value"),
5041 } else if (!strcmp(opt
->long_name
, "stat-graph-width")) {
5042 graph_width
= strtoul(value
, &end
, 10);
5044 return error(_("%s expects a numerical value"),
5046 } else if (!strcmp(opt
->long_name
, "stat-count")) {
5047 count
= strtoul(value
, &end
, 10);
5049 return error(_("%s expects a numerical value"),
5052 BUG("%s should not get here", opt
->long_name
);
5054 options
->output_format
&= ~DIFF_FORMAT_NO_OUTPUT
;
5055 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
5056 options
->stat_name_width
= name_width
;
5057 options
->stat_graph_width
= graph_width
;
5058 options
->stat_width
= width
;
5059 options
->stat_count
= count
;
5063 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
5065 struct strbuf errmsg
= STRBUF_INIT
;
5066 if (parse_dirstat_params(options
, params
, &errmsg
))
5067 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
5069 strbuf_release(&errmsg
);
5071 * The caller knows a dirstat-related option is given from the command
5072 * line; allow it to say "return this_function();"
5074 options
->output_format
&= ~DIFF_FORMAT_NO_OUTPUT
;
5075 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
5079 static int diff_opt_diff_filter(const struct option
*option
,
5080 const char *optarg
, int unset
)
5082 struct diff_options
*opt
= option
->value
;
5085 BUG_ON_OPT_NEG(unset
);
5086 prepare_filter_bits();
5088 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
5092 if ('a' <= optch
&& optch
<= 'z') {
5094 optch
= toupper(optch
);
5099 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
5101 return error(_("unknown change class '%c' in --diff-filter=%s"),
5104 opt
->filter_not
|= bit
;
5111 static void enable_patch_output(int *fmt
)
5113 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
5114 *fmt
|= DIFF_FORMAT_PATCH
;
5117 static int diff_opt_ws_error_highlight(const struct option
*option
,
5118 const char *arg
, int unset
)
5120 struct diff_options
*opt
= option
->value
;
5121 int val
= parse_ws_error_highlight(arg
);
5123 BUG_ON_OPT_NEG(unset
);
5125 return error(_("unknown value after ws-error-highlight=%.*s"),
5127 opt
->ws_error_highlight
= val
;
5131 static int diff_opt_find_object(const struct option
*option
,
5132 const char *arg
, int unset
)
5134 struct diff_options
*opt
= option
->value
;
5135 struct object_id oid
;
5137 BUG_ON_OPT_NEG(unset
);
5138 if (repo_get_oid(the_repository
, arg
, &oid
))
5139 return error(_("unable to resolve '%s'"), arg
);
5142 CALLOC_ARRAY(opt
->objfind
, 1);
5144 opt
->pickaxe_opts
|= DIFF_PICKAXE_KIND_OBJFIND
;
5145 opt
->flags
.recursive
= 1;
5146 opt
->flags
.tree_in_recursive
= 1;
5147 oidset_insert(opt
->objfind
, &oid
);
5151 static int diff_opt_anchored(const struct option
*opt
,
5152 const char *arg
, int unset
)
5154 struct diff_options
*options
= opt
->value
;
5156 BUG_ON_OPT_NEG(unset
);
5157 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
5158 ALLOC_GROW(options
->anchors
, options
->anchors_nr
+ 1,
5159 options
->anchors_alloc
);
5160 options
->anchors
[options
->anchors_nr
++] = xstrdup(arg
);
5164 static int diff_opt_binary(const struct option
*opt
,
5165 const char *arg
, int unset
)
5167 struct diff_options
*options
= opt
->value
;
5169 BUG_ON_OPT_NEG(unset
);
5170 BUG_ON_OPT_ARG(arg
);
5171 enable_patch_output(&options
->output_format
);
5172 options
->flags
.binary
= 1;
5176 static int diff_opt_break_rewrites(const struct option
*opt
,
5177 const char *arg
, int unset
)
5179 int *break_opt
= opt
->value
;
5182 BUG_ON_OPT_NEG(unset
);
5185 opt1
= parse_rename_score(&arg
);
5188 else if (*arg
!= '/')
5189 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5192 opt2
= parse_rename_score(&arg
);
5195 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5196 *break_opt
= opt1
| (opt2
<< 16);
5200 static int diff_opt_char(const struct option
*opt
,
5201 const char *arg
, int unset
)
5203 char *value
= opt
->value
;
5205 BUG_ON_OPT_NEG(unset
);
5207 return error(_("%s expects a character, got '%s'"),
5208 opt
->long_name
, arg
);
5213 static int diff_opt_color_moved(const struct option
*opt
,
5214 const char *arg
, int unset
)
5216 struct diff_options
*options
= opt
->value
;
5219 options
->color_moved
= COLOR_MOVED_NO
;
5221 if (diff_color_moved_default
)
5222 options
->color_moved
= diff_color_moved_default
;
5223 if (options
->color_moved
== COLOR_MOVED_NO
)
5224 options
->color_moved
= COLOR_MOVED_DEFAULT
;
5226 int cm
= parse_color_moved(arg
);
5228 return error(_("bad --color-moved argument: %s"), arg
);
5229 options
->color_moved
= cm
;
5234 static int diff_opt_color_moved_ws(const struct option
*opt
,
5235 const char *arg
, int unset
)
5237 struct diff_options
*options
= opt
->value
;
5241 options
->color_moved_ws_handling
= 0;
5245 cm
= parse_color_moved_ws(arg
);
5246 if (cm
& COLOR_MOVED_WS_ERROR
)
5247 return error(_("invalid mode '%s' in --color-moved-ws"), arg
);
5248 options
->color_moved_ws_handling
= cm
;
5252 static int diff_opt_color_words(const struct option
*opt
,
5253 const char *arg
, int unset
)
5255 struct diff_options
*options
= opt
->value
;
5257 BUG_ON_OPT_NEG(unset
);
5258 options
->use_color
= 1;
5259 options
->word_diff
= DIFF_WORDS_COLOR
;
5260 options
->word_regex
= arg
;
5264 static int diff_opt_compact_summary(const struct option
*opt
,
5265 const char *arg
, int unset
)
5267 struct diff_options
*options
= opt
->value
;
5269 BUG_ON_OPT_ARG(arg
);
5271 options
->flags
.stat_with_summary
= 0;
5273 options
->flags
.stat_with_summary
= 1;
5274 options
->output_format
&= ~DIFF_FORMAT_NO_OUTPUT
;
5275 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
5280 static int diff_opt_diff_algorithm(const struct option
*opt
,
5281 const char *arg
, int unset
)
5283 struct diff_options
*options
= opt
->value
;
5285 BUG_ON_OPT_NEG(unset
);
5287 if (set_diff_algorithm(options
, arg
))
5288 return error(_("option diff-algorithm accepts \"myers\", "
5289 "\"minimal\", \"patience\" and \"histogram\""));
5291 options
->ignore_driver_algorithm
= 1;
5296 static int diff_opt_diff_algorithm_no_arg(const struct option
*opt
,
5297 const char *arg
, int unset
)
5299 struct diff_options
*options
= opt
->value
;
5301 BUG_ON_OPT_NEG(unset
);
5302 BUG_ON_OPT_ARG(arg
);
5304 if (set_diff_algorithm(options
, opt
->long_name
))
5305 BUG("available diff algorithms include \"myers\", "
5306 "\"minimal\", \"patience\" and \"histogram\"");
5308 options
->ignore_driver_algorithm
= 1;
5313 static int diff_opt_dirstat(const struct option
*opt
,
5314 const char *arg
, int unset
)
5316 struct diff_options
*options
= opt
->value
;
5318 BUG_ON_OPT_NEG(unset
);
5319 if (!strcmp(opt
->long_name
, "cumulative")) {
5321 BUG("how come --cumulative take a value?");
5323 } else if (!strcmp(opt
->long_name
, "dirstat-by-file"))
5324 parse_dirstat_opt(options
, "files");
5325 parse_dirstat_opt(options
, arg
? arg
: "");
5329 static int diff_opt_find_copies(const struct option
*opt
,
5330 const char *arg
, int unset
)
5332 struct diff_options
*options
= opt
->value
;
5334 BUG_ON_OPT_NEG(unset
);
5337 options
->rename_score
= parse_rename_score(&arg
);
5339 return error(_("invalid argument to %s"), opt
->long_name
);
5341 if (options
->detect_rename
== DIFF_DETECT_COPY
)
5342 options
->flags
.find_copies_harder
= 1;
5344 options
->detect_rename
= DIFF_DETECT_COPY
;
5349 static int diff_opt_find_renames(const struct option
*opt
,
5350 const char *arg
, int unset
)
5352 struct diff_options
*options
= opt
->value
;
5354 BUG_ON_OPT_NEG(unset
);
5357 options
->rename_score
= parse_rename_score(&arg
);
5359 return error(_("invalid argument to %s"), opt
->long_name
);
5361 options
->detect_rename
= DIFF_DETECT_RENAME
;
5365 static int diff_opt_follow(const struct option
*opt
,
5366 const char *arg
, int unset
)
5368 struct diff_options
*options
= opt
->value
;
5370 BUG_ON_OPT_ARG(arg
);
5372 options
->flags
.follow_renames
= 0;
5373 options
->flags
.default_follow_renames
= 0;
5375 options
->flags
.follow_renames
= 1;
5380 static int diff_opt_ignore_submodules(const struct option
*opt
,
5381 const char *arg
, int unset
)
5383 struct diff_options
*options
= opt
->value
;
5385 BUG_ON_OPT_NEG(unset
);
5388 options
->flags
.override_submodule_config
= 1;
5389 handle_ignore_submodules_arg(options
, arg
);
5393 static int diff_opt_line_prefix(const struct option
*opt
,
5394 const char *optarg
, int unset
)
5396 struct diff_options
*options
= opt
->value
;
5398 BUG_ON_OPT_NEG(unset
);
5399 options
->line_prefix
= optarg
;
5400 options
->line_prefix_length
= strlen(options
->line_prefix
);
5401 graph_setup_line_prefix(options
);
5405 static int diff_opt_no_prefix(const struct option
*opt
,
5406 const char *optarg
, int unset
)
5408 struct diff_options
*options
= opt
->value
;
5410 BUG_ON_OPT_NEG(unset
);
5411 BUG_ON_OPT_ARG(optarg
);
5412 diff_set_noprefix(options
);
5416 static int diff_opt_default_prefix(const struct option
*opt
,
5417 const char *optarg
, int unset
)
5419 struct diff_options
*options
= opt
->value
;
5421 BUG_ON_OPT_NEG(unset
);
5422 BUG_ON_OPT_ARG(optarg
);
5423 FREE_AND_NULL(diff_src_prefix
);
5424 FREE_AND_NULL(diff_dst_prefix
);
5425 diff_set_default_prefix(options
);
5429 static enum parse_opt_result
diff_opt_output(struct parse_opt_ctx_t
*ctx
,
5430 const struct option
*opt
,
5431 const char *arg
, int unset
)
5433 struct diff_options
*options
= opt
->value
;
5436 BUG_ON_OPT_NEG(unset
);
5437 path
= prefix_filename(ctx
->prefix
, arg
);
5438 options
->file
= xfopen(path
, "w");
5439 options
->close_file
= 1;
5440 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
5441 options
->use_color
= GIT_COLOR_NEVER
;
5446 static int diff_opt_patience(const struct option
*opt
,
5447 const char *arg
, int unset
)
5449 struct diff_options
*options
= opt
->value
;
5452 BUG_ON_OPT_NEG(unset
);
5453 BUG_ON_OPT_ARG(arg
);
5455 * Both --patience and --anchored use PATIENCE_DIFF
5456 * internally, so remove any anchors previously
5459 for (i
= 0; i
< options
->anchors_nr
; i
++)
5460 free(options
->anchors
[i
]);
5461 options
->anchors_nr
= 0;
5462 options
->ignore_driver_algorithm
= 1;
5464 return set_diff_algorithm(options
, "patience");
5467 static int diff_opt_ignore_regex(const struct option
*opt
,
5468 const char *arg
, int unset
)
5470 struct diff_options
*options
= opt
->value
;
5473 BUG_ON_OPT_NEG(unset
);
5475 regex
= xmalloc(sizeof(*regex
));
5476 if (regcomp(regex
, arg
, REG_EXTENDED
| REG_NEWLINE
)) {
5478 return error(_("invalid regex given to -I: '%s'"), arg
);
5481 ALLOC_GROW(options
->ignore_regex
, options
->ignore_regex_nr
+ 1,
5482 options
->ignore_regex_alloc
);
5483 options
->ignore_regex
[options
->ignore_regex_nr
++] = regex
;
5487 static int diff_opt_pickaxe_regex(const struct option
*opt
,
5488 const char *arg
, int unset
)
5490 struct diff_options
*options
= opt
->value
;
5492 BUG_ON_OPT_NEG(unset
);
5493 options
->pickaxe
= arg
;
5494 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
5498 static int diff_opt_pickaxe_string(const struct option
*opt
,
5499 const char *arg
, int unset
)
5501 struct diff_options
*options
= opt
->value
;
5503 BUG_ON_OPT_NEG(unset
);
5504 options
->pickaxe
= arg
;
5505 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
5509 static int diff_opt_relative(const struct option
*opt
,
5510 const char *arg
, int unset
)
5512 struct diff_options
*options
= opt
->value
;
5514 options
->flags
.relative_name
= !unset
;
5516 options
->prefix
= arg
;
5520 static int diff_opt_submodule(const struct option
*opt
,
5521 const char *arg
, int unset
)
5523 struct diff_options
*options
= opt
->value
;
5525 BUG_ON_OPT_NEG(unset
);
5528 if (parse_submodule_params(options
, arg
))
5529 return error(_("failed to parse --submodule option parameter: '%s'"),
5534 static int diff_opt_textconv(const struct option
*opt
,
5535 const char *arg
, int unset
)
5537 struct diff_options
*options
= opt
->value
;
5539 BUG_ON_OPT_ARG(arg
);
5541 options
->flags
.allow_textconv
= 0;
5543 options
->flags
.allow_textconv
= 1;
5544 options
->flags
.textconv_set_via_cmdline
= 1;
5549 static int diff_opt_unified(const struct option
*opt
,
5550 const char *arg
, int unset
)
5552 struct diff_options
*options
= opt
->value
;
5555 BUG_ON_OPT_NEG(unset
);
5558 options
->context
= strtol(arg
, &s
, 10);
5560 return error(_("%s expects a numerical value"), "--unified");
5562 enable_patch_output(&options
->output_format
);
5567 static int diff_opt_word_diff(const struct option
*opt
,
5568 const char *arg
, int unset
)
5570 struct diff_options
*options
= opt
->value
;
5572 BUG_ON_OPT_NEG(unset
);
5574 if (!strcmp(arg
, "plain"))
5575 options
->word_diff
= DIFF_WORDS_PLAIN
;
5576 else if (!strcmp(arg
, "color")) {
5577 options
->use_color
= 1;
5578 options
->word_diff
= DIFF_WORDS_COLOR
;
5580 else if (!strcmp(arg
, "porcelain"))
5581 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
5582 else if (!strcmp(arg
, "none"))
5583 options
->word_diff
= DIFF_WORDS_NONE
;
5585 return error(_("bad --word-diff argument: %s"), arg
);
5587 if (options
->word_diff
== DIFF_WORDS_NONE
)
5588 options
->word_diff
= DIFF_WORDS_PLAIN
;
5593 static int diff_opt_word_diff_regex(const struct option
*opt
,
5594 const char *arg
, int unset
)
5596 struct diff_options
*options
= opt
->value
;
5598 BUG_ON_OPT_NEG(unset
);
5599 if (options
->word_diff
== DIFF_WORDS_NONE
)
5600 options
->word_diff
= DIFF_WORDS_PLAIN
;
5601 options
->word_regex
= arg
;
5605 static int diff_opt_rotate_to(const struct option
*opt
, const char *arg
, int unset
)
5607 struct diff_options
*options
= opt
->value
;
5609 BUG_ON_OPT_NEG(unset
);
5610 if (!strcmp(opt
->long_name
, "skip-to"))
5611 options
->skip_instead_of_rotate
= 1;
5613 options
->skip_instead_of_rotate
= 0;
5614 options
->rotate_to
= arg
;
5619 * Consider adding new flags to __git_diff_common_options
5620 * in contrib/completion/git-completion.bash
5622 struct option
*add_diff_options(const struct option
*opts
,
5623 struct diff_options
*options
)
5625 struct option parseopts
[] = {
5626 OPT_GROUP(N_("Diff output format options")),
5627 OPT_BITOP('p', "patch", &options
->output_format
,
5628 N_("generate patch"),
5629 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5630 OPT_SET_INT('s', "no-patch", &options
->output_format
,
5631 N_("suppress diff output"), DIFF_FORMAT_NO_OUTPUT
),
5632 OPT_BITOP('u', NULL
, &options
->output_format
,
5633 N_("generate patch"),
5634 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5635 OPT_CALLBACK_F('U', "unified", options
, N_("<n>"),
5636 N_("generate diffs with <n> lines context"),
5637 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_unified
),
5638 OPT_BOOL('W', "function-context", &options
->flags
.funccontext
,
5639 N_("generate diffs with <n> lines context")),
5640 OPT_BITOP(0, "raw", &options
->output_format
,
5641 N_("generate the diff in raw format"),
5642 DIFF_FORMAT_RAW
, DIFF_FORMAT_NO_OUTPUT
),
5643 OPT_BITOP(0, "patch-with-raw", &options
->output_format
,
5644 N_("synonym for '-p --raw'"),
5645 DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
,
5646 DIFF_FORMAT_NO_OUTPUT
),
5647 OPT_BITOP(0, "patch-with-stat", &options
->output_format
,
5648 N_("synonym for '-p --stat'"),
5649 DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
,
5650 DIFF_FORMAT_NO_OUTPUT
),
5651 OPT_BITOP(0, "numstat", &options
->output_format
,
5652 N_("machine friendly --stat"),
5653 DIFF_FORMAT_NUMSTAT
, DIFF_FORMAT_NO_OUTPUT
),
5654 OPT_BITOP(0, "shortstat", &options
->output_format
,
5655 N_("output only the last line of --stat"),
5656 DIFF_FORMAT_SHORTSTAT
, DIFF_FORMAT_NO_OUTPUT
),
5657 OPT_CALLBACK_F('X', "dirstat", options
, N_("<param1>,<param2>..."),
5658 N_("output the distribution of relative amount of changes for each sub-directory"),
5659 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5661 OPT_CALLBACK_F(0, "cumulative", options
, NULL
,
5662 N_("synonym for --dirstat=cumulative"),
5663 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5665 OPT_CALLBACK_F(0, "dirstat-by-file", options
, N_("<param1>,<param2>..."),
5666 N_("synonym for --dirstat=files,<param1>,<param2>..."),
5667 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5669 OPT_BIT_F(0, "check", &options
->output_format
,
5670 N_("warn if changes introduce conflict markers or whitespace errors"),
5671 DIFF_FORMAT_CHECKDIFF
, PARSE_OPT_NONEG
),
5672 OPT_BITOP(0, "summary", &options
->output_format
,
5673 N_("condensed summary such as creations, renames and mode changes"),
5674 DIFF_FORMAT_SUMMARY
, DIFF_FORMAT_NO_OUTPUT
),
5675 OPT_BIT_F(0, "name-only", &options
->output_format
,
5676 N_("show only names of changed files"),
5677 DIFF_FORMAT_NAME
, PARSE_OPT_NONEG
),
5678 OPT_BIT_F(0, "name-status", &options
->output_format
,
5679 N_("show only names and status of changed files"),
5680 DIFF_FORMAT_NAME_STATUS
, PARSE_OPT_NONEG
),
5681 OPT_CALLBACK_F(0, "stat", options
, N_("<width>[,<name-width>[,<count>]]"),
5682 N_("generate diffstat"),
5683 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_stat
),
5684 OPT_CALLBACK_F(0, "stat-width", options
, N_("<width>"),
5685 N_("generate diffstat with a given width"),
5686 PARSE_OPT_NONEG
, diff_opt_stat
),
5687 OPT_CALLBACK_F(0, "stat-name-width", options
, N_("<width>"),
5688 N_("generate diffstat with a given name width"),
5689 PARSE_OPT_NONEG
, diff_opt_stat
),
5690 OPT_CALLBACK_F(0, "stat-graph-width", options
, N_("<width>"),
5691 N_("generate diffstat with a given graph width"),
5692 PARSE_OPT_NONEG
, diff_opt_stat
),
5693 OPT_CALLBACK_F(0, "stat-count", options
, N_("<count>"),
5694 N_("generate diffstat with limited lines"),
5695 PARSE_OPT_NONEG
, diff_opt_stat
),
5696 OPT_CALLBACK_F(0, "compact-summary", options
, NULL
,
5697 N_("generate compact summary in diffstat"),
5698 PARSE_OPT_NOARG
, diff_opt_compact_summary
),
5699 OPT_CALLBACK_F(0, "binary", options
, NULL
,
5700 N_("output a binary diff that can be applied"),
5701 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_binary
),
5702 OPT_BOOL(0, "full-index", &options
->flags
.full_index
,
5703 N_("show full pre- and post-image object names on the \"index\" lines")),
5704 OPT_COLOR_FLAG(0, "color", &options
->use_color
,
5705 N_("show colored diff")),
5706 OPT_CALLBACK_F(0, "ws-error-highlight", options
, N_("<kind>"),
5707 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5708 PARSE_OPT_NONEG
, diff_opt_ws_error_highlight
),
5709 OPT_SET_INT('z', NULL
, &options
->line_termination
,
5710 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5712 OPT__ABBREV(&options
->abbrev
),
5713 OPT_STRING_F(0, "src-prefix", &options
->a_prefix
, N_("<prefix>"),
5714 N_("show the given source prefix instead of \"a/\""),
5716 OPT_STRING_F(0, "dst-prefix", &options
->b_prefix
, N_("<prefix>"),
5717 N_("show the given destination prefix instead of \"b/\""),
5719 OPT_CALLBACK_F(0, "line-prefix", options
, N_("<prefix>"),
5720 N_("prepend an additional prefix to every line of output"),
5721 PARSE_OPT_NONEG
, diff_opt_line_prefix
),
5722 OPT_CALLBACK_F(0, "no-prefix", options
, NULL
,
5723 N_("do not show any source or destination prefix"),
5724 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_no_prefix
),
5725 OPT_CALLBACK_F(0, "default-prefix", options
, NULL
,
5726 N_("use default prefixes a/ and b/"),
5727 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_default_prefix
),
5728 OPT_INTEGER_F(0, "inter-hunk-context", &options
->interhunkcontext
,
5729 N_("show context between diff hunks up to the specified number of lines"),
5731 OPT_CALLBACK_F(0, "output-indicator-new",
5732 &options
->output_indicators
[OUTPUT_INDICATOR_NEW
],
5734 N_("specify the character to indicate a new line instead of '+'"),
5735 PARSE_OPT_NONEG
, diff_opt_char
),
5736 OPT_CALLBACK_F(0, "output-indicator-old",
5737 &options
->output_indicators
[OUTPUT_INDICATOR_OLD
],
5739 N_("specify the character to indicate an old line instead of '-'"),
5740 PARSE_OPT_NONEG
, diff_opt_char
),
5741 OPT_CALLBACK_F(0, "output-indicator-context",
5742 &options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
],
5744 N_("specify the character to indicate a context instead of ' '"),
5745 PARSE_OPT_NONEG
, diff_opt_char
),
5747 OPT_GROUP(N_("Diff rename options")),
5748 OPT_CALLBACK_F('B', "break-rewrites", &options
->break_opt
, N_("<n>[/<m>]"),
5749 N_("break complete rewrite changes into pairs of delete and create"),
5750 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5751 diff_opt_break_rewrites
),
5752 OPT_CALLBACK_F('M', "find-renames", options
, N_("<n>"),
5753 N_("detect renames"),
5754 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5755 diff_opt_find_renames
),
5756 OPT_SET_INT_F('D', "irreversible-delete", &options
->irreversible_delete
,
5757 N_("omit the preimage for deletes"),
5758 1, PARSE_OPT_NONEG
),
5759 OPT_CALLBACK_F('C', "find-copies", options
, N_("<n>"),
5760 N_("detect copies"),
5761 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5762 diff_opt_find_copies
),
5763 OPT_BOOL(0, "find-copies-harder", &options
->flags
.find_copies_harder
,
5764 N_("use unmodified files as source to find copies")),
5765 OPT_SET_INT_F(0, "no-renames", &options
->detect_rename
,
5766 N_("disable rename detection"),
5767 0, PARSE_OPT_NONEG
),
5768 OPT_BOOL(0, "rename-empty", &options
->flags
.rename_empty
,
5769 N_("use empty blobs as rename source")),
5770 OPT_CALLBACK_F(0, "follow", options
, NULL
,
5771 N_("continue listing the history of a file beyond renames"),
5772 PARSE_OPT_NOARG
, diff_opt_follow
),
5773 OPT_INTEGER('l', NULL
, &options
->rename_limit
,
5774 N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
5776 OPT_GROUP(N_("Diff algorithm options")),
5777 OPT_CALLBACK_F(0, "minimal", options
, NULL
,
5778 N_("produce the smallest possible diff"),
5779 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5780 diff_opt_diff_algorithm_no_arg
),
5781 OPT_BIT_F('w', "ignore-all-space", &options
->xdl_opts
,
5782 N_("ignore whitespace when comparing lines"),
5783 XDF_IGNORE_WHITESPACE
, PARSE_OPT_NONEG
),
5784 OPT_BIT_F('b', "ignore-space-change", &options
->xdl_opts
,
5785 N_("ignore changes in amount of whitespace"),
5786 XDF_IGNORE_WHITESPACE_CHANGE
, PARSE_OPT_NONEG
),
5787 OPT_BIT_F(0, "ignore-space-at-eol", &options
->xdl_opts
,
5788 N_("ignore changes in whitespace at EOL"),
5789 XDF_IGNORE_WHITESPACE_AT_EOL
, PARSE_OPT_NONEG
),
5790 OPT_BIT_F(0, "ignore-cr-at-eol", &options
->xdl_opts
,
5791 N_("ignore carrier-return at the end of line"),
5792 XDF_IGNORE_CR_AT_EOL
, PARSE_OPT_NONEG
),
5793 OPT_BIT_F(0, "ignore-blank-lines", &options
->xdl_opts
,
5794 N_("ignore changes whose lines are all blank"),
5795 XDF_IGNORE_BLANK_LINES
, PARSE_OPT_NONEG
),
5796 OPT_CALLBACK_F('I', "ignore-matching-lines", options
, N_("<regex>"),
5797 N_("ignore changes whose all lines match <regex>"),
5798 0, diff_opt_ignore_regex
),
5799 OPT_BIT(0, "indent-heuristic", &options
->xdl_opts
,
5800 N_("heuristic to shift diff hunk boundaries for easy reading"),
5801 XDF_INDENT_HEURISTIC
),
5802 OPT_CALLBACK_F(0, "patience", options
, NULL
,
5803 N_("generate diff using the \"patience diff\" algorithm"),
5804 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5806 OPT_CALLBACK_F(0, "histogram", options
, NULL
,
5807 N_("generate diff using the \"histogram diff\" algorithm"),
5808 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5809 diff_opt_diff_algorithm_no_arg
),
5810 OPT_CALLBACK_F(0, "diff-algorithm", options
, N_("<algorithm>"),
5811 N_("choose a diff algorithm"),
5812 PARSE_OPT_NONEG
, diff_opt_diff_algorithm
),
5813 OPT_CALLBACK_F(0, "anchored", options
, N_("<text>"),
5814 N_("generate diff using the \"anchored diff\" algorithm"),
5815 PARSE_OPT_NONEG
, diff_opt_anchored
),
5816 OPT_CALLBACK_F(0, "word-diff", options
, N_("<mode>"),
5817 N_("show word diff, using <mode> to delimit changed words"),
5818 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_word_diff
),
5819 OPT_CALLBACK_F(0, "word-diff-regex", options
, N_("<regex>"),
5820 N_("use <regex> to decide what a word is"),
5821 PARSE_OPT_NONEG
, diff_opt_word_diff_regex
),
5822 OPT_CALLBACK_F(0, "color-words", options
, N_("<regex>"),
5823 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5824 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_color_words
),
5825 OPT_CALLBACK_F(0, "color-moved", options
, N_("<mode>"),
5826 N_("moved lines of code are colored differently"),
5827 PARSE_OPT_OPTARG
, diff_opt_color_moved
),
5828 OPT_CALLBACK_F(0, "color-moved-ws", options
, N_("<mode>"),
5829 N_("how white spaces are ignored in --color-moved"),
5830 0, diff_opt_color_moved_ws
),
5832 OPT_GROUP(N_("Other diff options")),
5833 OPT_CALLBACK_F(0, "relative", options
, N_("<prefix>"),
5834 N_("when run from subdir, exclude changes outside and show relative paths"),
5837 OPT_BOOL('a', "text", &options
->flags
.text
,
5838 N_("treat all files as text")),
5839 OPT_BOOL('R', NULL
, &options
->flags
.reverse_diff
,
5840 N_("swap two inputs, reverse the diff")),
5841 OPT_BOOL(0, "exit-code", &options
->flags
.exit_with_status
,
5842 N_("exit with 1 if there were differences, 0 otherwise")),
5843 OPT_BOOL(0, "quiet", &options
->flags
.quick
,
5844 N_("disable all output of the program")),
5845 OPT_BOOL(0, "ext-diff", &options
->flags
.allow_external
,
5846 N_("allow an external diff helper to be executed")),
5847 OPT_CALLBACK_F(0, "textconv", options
, NULL
,
5848 N_("run external text conversion filters when comparing binary files"),
5849 PARSE_OPT_NOARG
, diff_opt_textconv
),
5850 OPT_CALLBACK_F(0, "ignore-submodules", options
, N_("<when>"),
5851 N_("ignore changes to submodules in the diff generation"),
5852 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5853 diff_opt_ignore_submodules
),
5854 OPT_CALLBACK_F(0, "submodule", options
, N_("<format>"),
5855 N_("specify how differences in submodules are shown"),
5856 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5857 diff_opt_submodule
),
5858 OPT_SET_INT_F(0, "ita-invisible-in-index", &options
->ita_invisible_in_index
,
5859 N_("hide 'git add -N' entries from the index"),
5860 1, PARSE_OPT_NONEG
),
5861 OPT_SET_INT_F(0, "ita-visible-in-index", &options
->ita_invisible_in_index
,
5862 N_("treat 'git add -N' entries as real in the index"),
5863 0, PARSE_OPT_NONEG
),
5864 OPT_CALLBACK_F('S', NULL
, options
, N_("<string>"),
5865 N_("look for differences that change the number of occurrences of the specified string"),
5866 0, diff_opt_pickaxe_string
),
5867 OPT_CALLBACK_F('G', NULL
, options
, N_("<regex>"),
5868 N_("look for differences that change the number of occurrences of the specified regex"),
5869 0, diff_opt_pickaxe_regex
),
5870 OPT_BIT_F(0, "pickaxe-all", &options
->pickaxe_opts
,
5871 N_("show all changes in the changeset with -S or -G"),
5872 DIFF_PICKAXE_ALL
, PARSE_OPT_NONEG
),
5873 OPT_BIT_F(0, "pickaxe-regex", &options
->pickaxe_opts
,
5874 N_("treat <string> in -S as extended POSIX regular expression"),
5875 DIFF_PICKAXE_REGEX
, PARSE_OPT_NONEG
),
5876 OPT_FILENAME('O', NULL
, &options
->orderfile
,
5877 N_("control the order in which files appear in the output")),
5878 OPT_CALLBACK_F(0, "rotate-to", options
, N_("<path>"),
5879 N_("show the change in the specified path first"),
5880 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5881 OPT_CALLBACK_F(0, "skip-to", options
, N_("<path>"),
5882 N_("skip the output to the specified path"),
5883 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5884 OPT_CALLBACK_F(0, "find-object", options
, N_("<object-id>"),
5885 N_("look for differences that change the number of occurrences of the specified object"),
5886 PARSE_OPT_NONEG
, diff_opt_find_object
),
5887 OPT_CALLBACK_F(0, "diff-filter", options
, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5888 N_("select files by diff type"),
5889 PARSE_OPT_NONEG
, diff_opt_diff_filter
),
5890 { OPTION_CALLBACK
, 0, "output", options
, N_("<file>"),
5891 N_("output to a specific file"),
5892 PARSE_OPT_NONEG
, NULL
, 0, diff_opt_output
},
5897 return parse_options_concat(opts
, parseopts
);
5900 int diff_opt_parse(struct diff_options
*options
,
5901 const char **av
, int ac
, const char *prefix
)
5903 struct option no_options
[] = { OPT_END() };
5904 struct option
*parseopts
= add_diff_options(no_options
, options
);
5909 ac
= parse_options(ac
, av
, prefix
, parseopts
, NULL
,
5910 PARSE_OPT_KEEP_DASHDASH
|
5911 PARSE_OPT_KEEP_UNKNOWN_OPT
|
5912 PARSE_OPT_NO_INTERNAL_HELP
|
5913 PARSE_OPT_ONE_SHOT
|
5914 PARSE_OPT_STOP_AT_NON_OPTION
);
5920 int parse_rename_score(const char **cp_p
)
5922 unsigned long num
, scale
;
5924 const char *cp
= *cp_p
;
5931 if ( !dot
&& ch
== '.' ) {
5934 } else if ( ch
== '%' ) {
5935 scale
= dot
? scale
*100 : 100;
5936 cp
++; /* % is always at the end */
5938 } else if ( ch
>= '0' && ch
<= '9' ) {
5939 if ( scale
< 100000 ) {
5941 num
= (num
*10) + (ch
-'0');
5950 /* user says num divided by scale and we say internally that
5951 * is MAX_SCORE * num / scale.
5953 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
5956 struct diff_queue_struct diff_queued_diff
;
5958 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
5960 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
5961 queue
->queue
[queue
->nr
++] = dp
;
5964 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
5965 struct diff_filespec
*one
,
5966 struct diff_filespec
*two
)
5968 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
5976 void diff_free_filepair(struct diff_filepair
*p
)
5978 free_filespec(p
->one
);
5979 free_filespec(p
->two
);
5983 void diff_free_queue(struct diff_queue_struct
*q
)
5985 for (int i
= 0; i
< q
->nr
; i
++)
5986 diff_free_filepair(q
->queue
[i
]);
5990 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
5995 /* Do we want all 40 hex characters? */
5996 if (len
== the_hash_algo
->hexsz
)
5997 return oid_to_hex(oid
);
5999 /* An abbreviated value is fine, possibly followed by an ellipsis. */
6000 abbrev
= diff_abbrev_oid(oid
, len
);
6002 if (!print_sha1_ellipsis())
6005 abblen
= strlen(abbrev
);
6008 * In well-behaved cases, where the abbreviated result is the
6009 * same as the requested length, append three dots after the
6010 * abbreviation (hence the whole logic is limited to the case
6011 * where abblen < 37); when the actual abbreviated result is a
6012 * bit longer than the requested length, we reduce the number
6013 * of dots so that they match the well-behaved ones. However,
6014 * if the actual abbreviation is longer than the requested
6015 * length by more than three, we give up on aligning, and add
6016 * three dots anyway, to indicate that the output is not the
6017 * full object name. Yes, this may be suboptimal, but this
6018 * appears only in "diff --raw --abbrev" output and it is not
6019 * worth the effort to change it now. Note that this would
6020 * likely to work fine when the automatic sizing of default
6021 * abbreviation length is used--we would be fed -1 in "len" in
6022 * that case, and will end up always appending three-dots, but
6023 * the automatic sizing is supposed to give abblen that ensures
6024 * uniqueness across all objects (statistically speaking).
6026 if (abblen
< the_hash_algo
->hexsz
- 3) {
6027 static char hex
[GIT_MAX_HEXSZ
+ 1];
6028 if (len
< abblen
&& abblen
<= len
+ 2)
6029 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
6031 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
6035 return oid_to_hex(oid
);
6038 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
6040 int line_termination
= opt
->line_termination
;
6041 int inter_name_termination
= line_termination
? '\t' : '\0';
6043 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
6044 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
6045 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
6046 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
6047 fprintf(opt
->file
, "%s ",
6048 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
6051 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
6052 inter_name_termination
);
6054 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
6057 if (p
->status
== DIFF_STATUS_COPIED
||
6058 p
->status
== DIFF_STATUS_RENAMED
) {
6059 const char *name_a
, *name_b
;
6060 name_a
= p
->one
->path
;
6061 name_b
= p
->two
->path
;
6062 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6063 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
6064 write_name_quoted(name_b
, opt
->file
, line_termination
);
6066 const char *name_a
, *name_b
;
6067 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
6069 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6070 write_name_quoted(name_a
, opt
->file
, line_termination
);
6074 int diff_unmodified_pair(struct diff_filepair
*p
)
6076 /* This function is written stricter than necessary to support
6077 * the currently implemented transformers, but the idea is to
6078 * let transformers to produce diff_filepairs any way they want,
6079 * and filter and clean them up here before producing the output.
6081 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
6083 if (DIFF_PAIR_UNMERGED(p
))
6084 return 0; /* unmerged is interesting */
6086 /* deletion, addition, mode or type change
6087 * and rename are all interesting.
6089 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
6090 DIFF_PAIR_MODE_CHANGED(p
) ||
6091 strcmp(one
->path
, two
->path
))
6094 /* both are valid and point at the same path. that is, we are
6095 * dealing with a change.
6097 if (one
->oid_valid
&& two
->oid_valid
&&
6098 oideq(&one
->oid
, &two
->oid
) &&
6099 !one
->dirty_submodule
&& !two
->dirty_submodule
)
6100 return 1; /* no change */
6101 if (!one
->oid_valid
&& !two
->oid_valid
)
6102 return 1; /* both look at the same file on the filesystem. */
6106 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
6108 int include_conflict_headers
=
6109 (additional_headers(o
, p
->one
->path
) &&
6111 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
6114 * Check if we can return early without showing a diff. Note that
6115 * diff_filepair only stores {oid, path, mode, is_valid}
6116 * information for each path, and thus diff_unmodified_pair() only
6117 * considers those bits of info. However, we do not want pairs
6118 * created by create_filepairs_for_header_only_notifications()
6119 * (which always look like unmodified pairs) to be ignored, so
6120 * return early if both p is unmodified AND we don't want to
6121 * include_conflict_headers.
6123 if (diff_unmodified_pair(p
) && !include_conflict_headers
)
6126 /* Actually, we can also return early to avoid showing tree diffs */
6127 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6128 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6134 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
6135 struct diffstat_t
*diffstat
)
6137 if (diff_unmodified_pair(p
))
6140 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6141 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6142 return; /* no useful stat for tree diffs */
6144 run_diffstat(p
, o
, diffstat
);
6147 static void diff_flush_checkdiff(struct diff_filepair
*p
,
6148 struct diff_options
*o
)
6150 if (diff_unmodified_pair(p
))
6153 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6154 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6155 return; /* nothing to check in tree diffs */
6157 run_checkdiff(p
, o
);
6160 int diff_queue_is_empty(struct diff_options
*o
)
6162 struct diff_queue_struct
*q
= &diff_queued_diff
;
6164 int include_conflict_headers
=
6165 (o
->additional_path_headers
&&
6166 strmap_get_size(o
->additional_path_headers
) &&
6168 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
6170 if (include_conflict_headers
)
6173 for (i
= 0; i
< q
->nr
; i
++)
6174 if (!diff_unmodified_pair(q
->queue
[i
]))
6180 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
6182 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
6185 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
6187 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
6188 fprintf(stderr
, "queue[%d] %s size %lu\n",
6193 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
6195 diff_debug_filespec(p
->one
, i
, "one");
6196 diff_debug_filespec(p
->two
, i
, "two");
6197 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
6198 p
->score
, p
->status
? p
->status
: '?',
6199 p
->one
->rename_used
, p
->broken_pair
);
6202 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
6206 fprintf(stderr
, "%s\n", msg
);
6207 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
6208 for (i
= 0; i
< q
->nr
; i
++) {
6209 struct diff_filepair
*p
= q
->queue
[i
];
6210 diff_debug_filepair(p
, i
);
6215 static void diff_resolve_rename_copy(void)
6218 struct diff_filepair
*p
;
6219 struct diff_queue_struct
*q
= &diff_queued_diff
;
6221 diff_debug_queue("resolve-rename-copy", q
);
6223 for (i
= 0; i
< q
->nr
; i
++) {
6225 p
->status
= 0; /* undecided */
6226 if (DIFF_PAIR_UNMERGED(p
))
6227 p
->status
= DIFF_STATUS_UNMERGED
;
6228 else if (!DIFF_FILE_VALID(p
->one
))
6229 p
->status
= DIFF_STATUS_ADDED
;
6230 else if (!DIFF_FILE_VALID(p
->two
))
6231 p
->status
= DIFF_STATUS_DELETED
;
6232 else if (DIFF_PAIR_TYPE_CHANGED(p
))
6233 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
6235 /* from this point on, we are dealing with a pair
6236 * whose both sides are valid and of the same type, i.e.
6237 * either in-place edit or rename/copy edit.
6239 else if (DIFF_PAIR_RENAME(p
)) {
6241 * A rename might have re-connected a broken
6242 * pair up, causing the pathnames to be the
6243 * same again. If so, that's not a rename at
6244 * all, just a modification..
6246 * Otherwise, see if this source was used for
6247 * multiple renames, in which case we decrement
6248 * the count, and call it a copy.
6250 if (!strcmp(p
->one
->path
, p
->two
->path
))
6251 p
->status
= DIFF_STATUS_MODIFIED
;
6252 else if (--p
->one
->rename_used
> 0)
6253 p
->status
= DIFF_STATUS_COPIED
;
6255 p
->status
= DIFF_STATUS_RENAMED
;
6257 else if (!oideq(&p
->one
->oid
, &p
->two
->oid
) ||
6258 p
->one
->mode
!= p
->two
->mode
||
6259 p
->one
->dirty_submodule
||
6260 p
->two
->dirty_submodule
||
6261 is_null_oid(&p
->one
->oid
))
6262 p
->status
= DIFF_STATUS_MODIFIED
;
6264 /* This is a "no-change" entry and should not
6265 * happen anymore, but prepare for broken callers.
6267 error("feeding unmodified %s to diffcore",
6269 p
->status
= DIFF_STATUS_UNKNOWN
;
6272 diff_debug_queue("resolve-rename-copy done", q
);
6275 static int check_pair_status(struct diff_filepair
*p
)
6277 switch (p
->status
) {
6278 case DIFF_STATUS_UNKNOWN
:
6281 die("internal error in diff-resolve-rename-copy");
6287 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
6289 int fmt
= opt
->output_format
;
6291 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
6292 diff_flush_checkdiff(p
, opt
);
6293 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
6294 diff_flush_raw(p
, opt
);
6295 else if (fmt
& DIFF_FORMAT_NAME
) {
6296 const char *name_a
, *name_b
;
6297 name_a
= p
->two
->path
;
6299 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6300 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
6301 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
6304 opt
->found_changes
= 1;
6307 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
6309 struct strbuf sb
= STRBUF_INIT
;
6311 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
6313 strbuf_addf(&sb
, " %s ", newdelete
);
6315 quote_c_style(fs
->path
, &sb
, NULL
, 0);
6316 strbuf_addch(&sb
, '\n');
6317 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6319 strbuf_release(&sb
);
6322 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
6325 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
6326 struct strbuf sb
= STRBUF_INIT
;
6327 strbuf_addf(&sb
, " mode change %06o => %06o",
6328 p
->one
->mode
, p
->two
->mode
);
6330 strbuf_addch(&sb
, ' ');
6331 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6333 strbuf_addch(&sb
, '\n');
6334 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6336 strbuf_release(&sb
);
6340 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
6341 struct diff_filepair
*p
)
6343 struct strbuf sb
= STRBUF_INIT
;
6344 struct strbuf names
= STRBUF_INIT
;
6346 pprint_rename(&names
, p
->one
->path
, p
->two
->path
);
6347 strbuf_addf(&sb
, " %s %s (%d%%)\n",
6348 renamecopy
, names
.buf
, similarity_index(p
));
6349 strbuf_release(&names
);
6350 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6352 show_mode_change(opt
, p
, 0);
6353 strbuf_release(&sb
);
6356 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
6359 case DIFF_STATUS_DELETED
:
6360 show_file_mode_name(opt
, "delete", p
->one
);
6362 case DIFF_STATUS_ADDED
:
6363 show_file_mode_name(opt
, "create", p
->two
);
6365 case DIFF_STATUS_COPIED
:
6366 show_rename_copy(opt
, "copy", p
);
6368 case DIFF_STATUS_RENAMED
:
6369 show_rename_copy(opt
, "rename", p
);
6373 struct strbuf sb
= STRBUF_INIT
;
6374 strbuf_addstr(&sb
, " rewrite ");
6375 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6376 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
6377 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6379 strbuf_release(&sb
);
6381 show_mode_change(opt
, p
, !p
->score
);
6391 static int remove_space(char *line
, int len
)
6397 for (i
= 0; i
< len
; i
++)
6398 if (!isspace((c
= line
[i
])))
6404 void flush_one_hunk(struct object_id
*result
, git_hash_ctx
*ctx
)
6406 unsigned char hash
[GIT_MAX_RAWSZ
];
6407 unsigned short carry
= 0;
6410 the_hash_algo
->final_fn(hash
, ctx
);
6411 the_hash_algo
->init_fn(ctx
);
6412 /* 20-byte sum, with carry */
6413 for (i
= 0; i
< the_hash_algo
->rawsz
; ++i
) {
6414 carry
+= result
->hash
[i
] + hash
[i
];
6415 result
->hash
[i
] = carry
;
6420 static int patch_id_consume(void *priv
, char *line
, unsigned long len
)
6422 struct patch_id_t
*data
= priv
;
6425 if (len
> 12 && starts_with(line
, "\\ "))
6427 new_len
= remove_space(line
, len
);
6429 the_hash_algo
->update_fn(data
->ctx
, line
, new_len
);
6430 data
->patchlen
+= new_len
;
6434 static void patch_id_add_string(git_hash_ctx
*ctx
, const char *str
)
6436 the_hash_algo
->update_fn(ctx
, str
, strlen(str
));
6439 static void patch_id_add_mode(git_hash_ctx
*ctx
, unsigned mode
)
6441 /* large enough for 2^32 in octal */
6443 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
6444 the_hash_algo
->update_fn(ctx
, buf
, len
);
6447 /* returns 0 upon success, and writes result into oid */
6448 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
6450 struct diff_queue_struct
*q
= &diff_queued_diff
;
6453 struct patch_id_t data
;
6455 the_hash_algo
->init_fn(&ctx
);
6456 memset(&data
, 0, sizeof(struct patch_id_t
));
6458 oidclr(oid
, the_repository
->hash_algo
);
6460 for (i
= 0; i
< q
->nr
; i
++) {
6464 struct diff_filepair
*p
= q
->queue
[i
];
6467 memset(&xpp
, 0, sizeof(xpp
));
6468 memset(&xecfg
, 0, sizeof(xecfg
));
6470 return error("internal diff status error");
6471 if (p
->status
== DIFF_STATUS_UNKNOWN
)
6473 if (diff_unmodified_pair(p
))
6475 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6476 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6478 if (DIFF_PAIR_UNMERGED(p
))
6481 diff_fill_oid_info(p
->one
, options
->repo
->index
);
6482 diff_fill_oid_info(p
->two
, options
->repo
->index
);
6484 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
6485 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
6486 patch_id_add_string(&ctx
, "diff--git");
6487 patch_id_add_string(&ctx
, "a/");
6488 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6489 patch_id_add_string(&ctx
, "b/");
6490 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6492 if (p
->one
->mode
== 0) {
6493 patch_id_add_string(&ctx
, "newfilemode");
6494 patch_id_add_mode(&ctx
, p
->two
->mode
);
6495 } else if (p
->two
->mode
== 0) {
6496 patch_id_add_string(&ctx
, "deletedfilemode");
6497 patch_id_add_mode(&ctx
, p
->one
->mode
);
6498 } else if (p
->one
->mode
!= p
->two
->mode
) {
6499 patch_id_add_string(&ctx
, "oldmode");
6500 patch_id_add_mode(&ctx
, p
->one
->mode
);
6501 patch_id_add_string(&ctx
, "newmode");
6502 patch_id_add_mode(&ctx
, p
->two
->mode
);
6505 if (diff_header_only
) {
6506 /* don't do anything since we're only populating header info */
6507 } else if (diff_filespec_is_binary(options
->repo
, p
->one
) ||
6508 diff_filespec_is_binary(options
->repo
, p
->two
)) {
6509 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->one
->oid
),
6510 the_hash_algo
->hexsz
);
6511 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->two
->oid
),
6512 the_hash_algo
->hexsz
);
6514 if (p
->one
->mode
== 0) {
6515 patch_id_add_string(&ctx
, "---/dev/null");
6516 patch_id_add_string(&ctx
, "+++b/");
6517 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6518 } else if (p
->two
->mode
== 0) {
6519 patch_id_add_string(&ctx
, "---a/");
6520 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6521 patch_id_add_string(&ctx
, "+++/dev/null");
6523 patch_id_add_string(&ctx
, "---a/");
6524 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6525 patch_id_add_string(&ctx
, "+++b/");
6526 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6529 if (fill_mmfile(options
->repo
, &mf1
, p
->one
) < 0 ||
6530 fill_mmfile(options
->repo
, &mf2
, p
->two
) < 0)
6531 return error("unable to read files to diff");
6534 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
6535 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
6536 patch_id_consume
, &data
, &xpp
, &xecfg
))
6537 return error("unable to generate patch-id diff for %s",
6540 flush_one_hunk(oid
, &ctx
);
6546 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
6548 struct diff_queue_struct
*q
= &diff_queued_diff
;
6549 int result
= diff_get_patch_id(options
, oid
, diff_header_only
);
6552 DIFF_QUEUE_CLEAR(q
);
6557 static int is_summary_empty(const struct diff_queue_struct
*q
)
6561 for (i
= 0; i
< q
->nr
; i
++) {
6562 const struct diff_filepair
*p
= q
->queue
[i
];
6564 switch (p
->status
) {
6565 case DIFF_STATUS_DELETED
:
6566 case DIFF_STATUS_ADDED
:
6567 case DIFF_STATUS_COPIED
:
6568 case DIFF_STATUS_RENAMED
:
6573 if (p
->one
->mode
&& p
->two
->mode
&&
6574 p
->one
->mode
!= p
->two
->mode
)
6582 static const char rename_limit_warning
[] =
6583 N_("exhaustive rename detection was skipped due to too many files.");
6585 static const char degrade_cc_to_c_warning
[] =
6586 N_("only found copies from modified paths due to too many files.");
6588 static const char rename_limit_advice
[] =
6589 N_("you may want to set your %s variable to at least "
6590 "%d and retry the command.");
6592 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
6596 warning(_(degrade_cc_to_c_warning
));
6598 warning(_(rename_limit_warning
));
6602 warning(_(rename_limit_advice
), varname
, needed
);
6605 static void create_filepairs_for_header_only_notifications(struct diff_options
*o
)
6607 struct strset present
;
6608 struct diff_queue_struct
*q
= &diff_queued_diff
;
6609 struct hashmap_iter iter
;
6610 struct strmap_entry
*e
;
6613 strset_init_with_options(&present
, /*pool*/ NULL
, /*strdup*/ 0);
6616 * Find out which paths exist in diff_queued_diff, preferring
6617 * one->path for any pair that has multiple paths.
6619 for (i
= 0; i
< q
->nr
; i
++) {
6620 struct diff_filepair
*p
= q
->queue
[i
];
6621 char *path
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
6623 if (strmap_contains(o
->additional_path_headers
, path
))
6624 strset_add(&present
, path
);
6628 * Loop over paths in additional_path_headers; for each NOT already
6629 * in diff_queued_diff, create a synthetic filepair and insert that
6630 * into diff_queued_diff.
6632 strmap_for_each_entry(o
->additional_path_headers
, &iter
, e
) {
6633 if (!strset_contains(&present
, e
->key
)) {
6634 struct diff_filespec
*one
, *two
;
6635 struct diff_filepair
*p
;
6637 one
= alloc_filespec(e
->key
);
6638 two
= alloc_filespec(e
->key
);
6639 fill_filespec(one
, null_oid(), 0, 0);
6640 fill_filespec(two
, null_oid(), 0, 0);
6641 p
= diff_queue(q
, one
, two
);
6642 p
->status
= DIFF_STATUS_MODIFIED
;
6646 /* Re-sort the filepairs */
6647 diffcore_fix_diff_index();
6650 strset_clear(&present
);
6653 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
6656 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
6657 struct diff_queue_struct
*q
= &diff_queued_diff
;
6659 if (WSEH_NEW
& WS_RULE_MASK
)
6660 BUG("WS rules bit mask overlaps with diff symbol flags");
6663 o
->emitted_symbols
= &esm
;
6665 if (o
->additional_path_headers
)
6666 create_filepairs_for_header_only_notifications(o
);
6668 for (i
= 0; i
< q
->nr
; i
++) {
6669 struct diff_filepair
*p
= q
->queue
[i
];
6670 if (check_pair_status(p
))
6671 diff_flush_patch(p
, o
);
6674 if (o
->emitted_symbols
) {
6675 if (o
->color_moved
) {
6676 struct mem_pool entry_pool
;
6677 struct moved_entry_list
*entry_list
;
6679 mem_pool_init(&entry_pool
, 1024 * 1024);
6680 entry_list
= add_lines_to_move_detection(o
,
6682 mark_color_as_moved(o
, entry_list
);
6683 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
6686 mem_pool_discard(&entry_pool
, 0);
6690 for (i
= 0; i
< esm
.nr
; i
++)
6691 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
6693 for (i
= 0; i
< esm
.nr
; i
++)
6694 free((void *)esm
.buf
[i
].line
);
6697 o
->emitted_symbols
= NULL
;
6701 static void diff_free_file(struct diff_options
*options
)
6703 if (options
->close_file
&& options
->file
) {
6704 fclose(options
->file
);
6705 options
->file
= NULL
;
6709 static void diff_free_ignore_regex(struct diff_options
*options
)
6713 for (i
= 0; i
< options
->ignore_regex_nr
; i
++) {
6714 regfree(options
->ignore_regex
[i
]);
6715 free(options
->ignore_regex
[i
]);
6718 FREE_AND_NULL(options
->ignore_regex
);
6719 options
->ignore_regex_nr
= 0;
6722 void diff_free(struct diff_options
*options
)
6724 if (options
->no_free
)
6727 if (options
->objfind
) {
6728 oidset_clear(options
->objfind
);
6729 FREE_AND_NULL(options
->objfind
);
6732 for (size_t i
= 0; i
< options
->anchors_nr
; i
++)
6733 free(options
->anchors
[i
]);
6734 FREE_AND_NULL(options
->anchors
);
6735 options
->anchors_nr
= options
->anchors_alloc
= 0;
6737 diff_free_file(options
);
6738 diff_free_ignore_regex(options
);
6739 clear_pathspec(&options
->pathspec
);
6742 void diff_flush(struct diff_options
*options
)
6744 struct diff_queue_struct
*q
= &diff_queued_diff
;
6745 int i
, output_format
= options
->output_format
;
6747 int dirstat_by_line
= 0;
6750 * Order: raw, stat, summary, patch
6751 * or: name/name-status/checkdiff (other bits clear)
6753 if (!q
->nr
&& !options
->additional_path_headers
)
6756 if (output_format
& (DIFF_FORMAT_RAW
|
6758 DIFF_FORMAT_NAME_STATUS
|
6759 DIFF_FORMAT_CHECKDIFF
)) {
6760 for (i
= 0; i
< q
->nr
; i
++) {
6761 struct diff_filepair
*p
= q
->queue
[i
];
6762 if (check_pair_status(p
))
6763 flush_one_pair(p
, options
);
6768 if (output_format
& DIFF_FORMAT_DIRSTAT
&& options
->flags
.dirstat_by_line
)
6769 dirstat_by_line
= 1;
6771 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
6773 struct diffstat_t diffstat
;
6775 compute_diffstat(options
, &diffstat
, q
);
6776 if (output_format
& DIFF_FORMAT_NUMSTAT
)
6777 show_numstat(&diffstat
, options
);
6778 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
6779 show_stats(&diffstat
, options
);
6780 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
6781 show_shortstats(&diffstat
, options
);
6782 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
6783 show_dirstat_by_line(&diffstat
, options
);
6784 free_diffstat_info(&diffstat
);
6787 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
6788 show_dirstat(options
);
6790 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
6791 for (i
= 0; i
< q
->nr
; i
++) {
6792 diff_summary(options
, q
->queue
[i
]);
6797 if (output_format
& DIFF_FORMAT_PATCH
) {
6799 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
6800 if (options
->stat_sep
)
6801 /* attach patch instead of inline */
6802 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
6806 diff_flush_patch_all_file_pairs(options
);
6809 if (output_format
& DIFF_FORMAT_CALLBACK
)
6810 options
->format_callback(q
, options
, options
->format_callback_data
);
6812 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
6813 options
->flags
.exit_with_status
&&
6814 options
->flags
.diff_from_contents
) {
6816 * run diff_flush_patch for the exit status. setting
6817 * options->file to /dev/null should be safe, because we
6818 * aren't supposed to produce any output anyway.
6820 diff_free_file(options
);
6821 options
->file
= xfopen("/dev/null", "w");
6822 options
->close_file
= 1;
6823 options
->color_moved
= 0;
6824 for (i
= 0; i
< q
->nr
; i
++) {
6825 struct diff_filepair
*p
= q
->queue
[i
];
6826 if (check_pair_status(p
))
6827 diff_flush_patch(p
, options
);
6828 if (options
->found_changes
)
6835 DIFF_QUEUE_CLEAR(q
);
6839 * Report the content-level differences with HAS_CHANGES;
6840 * diff_addremove/diff_change does not set the bit when
6841 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6843 if (options
->flags
.diff_from_contents
) {
6844 if (options
->found_changes
)
6845 options
->flags
.has_changes
= 1;
6847 options
->flags
.has_changes
= 0;
6851 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
6853 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
6855 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
6857 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
6858 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
6859 filter_bit_tst(p
->status
, options
)));
6862 static void diffcore_apply_filter(struct diff_options
*options
)
6865 struct diff_queue_struct
*q
= &diff_queued_diff
;
6866 struct diff_queue_struct outq
;
6868 DIFF_QUEUE_CLEAR(&outq
);
6870 if (!options
->filter
)
6873 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
6875 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
6876 if (match_filter(options
, q
->queue
[i
]))
6882 /* otherwise we will clear the whole queue
6883 * by copying the empty outq at the end of this
6884 * function, but first clear the current entries
6887 for (i
= 0; i
< q
->nr
; i
++)
6888 diff_free_filepair(q
->queue
[i
]);
6891 /* Only the matching ones */
6892 for (i
= 0; i
< q
->nr
; i
++) {
6893 struct diff_filepair
*p
= q
->queue
[i
];
6894 if (match_filter(options
, p
))
6897 diff_free_filepair(p
);
6904 /* Check whether two filespecs with the same mode and size are identical */
6905 static int diff_filespec_is_identical(struct repository
*r
,
6906 struct diff_filespec
*one
,
6907 struct diff_filespec
*two
)
6909 if (S_ISGITLINK(one
->mode
))
6911 if (diff_populate_filespec(r
, one
, NULL
))
6913 if (diff_populate_filespec(r
, two
, NULL
))
6915 return !memcmp(one
->data
, two
->data
, one
->size
);
6918 static int diff_filespec_check_stat_unmatch(struct repository
*r
,
6919 struct diff_filepair
*p
)
6921 struct diff_populate_filespec_options dpf_options
= {
6922 .check_size_only
= 1,
6923 .missing_object_cb
= diff_queued_diff_prefetch
,
6924 .missing_object_data
= r
,
6927 if (p
->done_skip_stat_unmatch
)
6928 return p
->skip_stat_unmatch_result
;
6930 p
->done_skip_stat_unmatch
= 1;
6931 p
->skip_stat_unmatch_result
= 0;
6933 * 1. Entries that come from stat info dirtiness
6934 * always have both sides (iow, not create/delete),
6935 * one side of the object name is unknown, with
6936 * the same mode and size. Keep the ones that
6937 * do not match these criteria. They have real
6940 * 2. At this point, the file is known to be modified,
6941 * with the same mode and size, and the object
6942 * name of one side is unknown. Need to inspect
6943 * the identical contents.
6945 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
6946 !DIFF_FILE_VALID(p
->two
) ||
6947 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
6948 (p
->one
->mode
!= p
->two
->mode
) ||
6949 diff_populate_filespec(r
, p
->one
, &dpf_options
) ||
6950 diff_populate_filespec(r
, p
->two
, &dpf_options
) ||
6951 (p
->one
->size
!= p
->two
->size
) ||
6952 !diff_filespec_is_identical(r
, p
->one
, p
->two
)) /* (2) */
6953 p
->skip_stat_unmatch_result
= 1;
6954 return p
->skip_stat_unmatch_result
;
6957 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
6960 struct diff_queue_struct
*q
= &diff_queued_diff
;
6961 struct diff_queue_struct outq
;
6962 DIFF_QUEUE_CLEAR(&outq
);
6964 for (i
= 0; i
< q
->nr
; i
++) {
6965 struct diff_filepair
*p
= q
->queue
[i
];
6967 if (diff_filespec_check_stat_unmatch(diffopt
->repo
, p
))
6971 * The caller can subtract 1 from skip_stat_unmatch
6972 * to determine how many paths were dirty only
6973 * due to stat info mismatch.
6975 if (!diffopt
->flags
.no_index
)
6976 diffopt
->skip_stat_unmatch
++;
6977 diff_free_filepair(p
);
6984 static int diffnamecmp(const void *a_
, const void *b_
)
6986 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
6987 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
6988 const char *name_a
, *name_b
;
6990 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
6991 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
6992 return strcmp(name_a
, name_b
);
6995 void diffcore_fix_diff_index(void)
6997 struct diff_queue_struct
*q
= &diff_queued_diff
;
6998 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
7001 void diff_add_if_missing(struct repository
*r
,
7002 struct oid_array
*to_fetch
,
7003 const struct diff_filespec
*filespec
)
7005 if (filespec
&& filespec
->oid_valid
&&
7006 !S_ISGITLINK(filespec
->mode
) &&
7007 oid_object_info_extended(r
, &filespec
->oid
, NULL
,
7008 OBJECT_INFO_FOR_PREFETCH
))
7009 oid_array_append(to_fetch
, &filespec
->oid
);
7012 void diff_queued_diff_prefetch(void *repository
)
7014 struct repository
*repo
= repository
;
7016 struct diff_queue_struct
*q
= &diff_queued_diff
;
7017 struct oid_array to_fetch
= OID_ARRAY_INIT
;
7019 for (i
= 0; i
< q
->nr
; i
++) {
7020 struct diff_filepair
*p
= q
->queue
[i
];
7021 diff_add_if_missing(repo
, &to_fetch
, p
->one
);
7022 diff_add_if_missing(repo
, &to_fetch
, p
->two
);
7026 * NEEDSWORK: Consider deduplicating the OIDs sent.
7028 promisor_remote_get_direct(repo
, to_fetch
.oid
, to_fetch
.nr
);
7030 oid_array_clear(&to_fetch
);
7033 void init_diffstat_widths(struct diff_options
*options
)
7035 options
->stat_width
= -1; /* use full terminal width */
7036 options
->stat_name_width
= -1; /* respect diff.statNameWidth config */
7037 options
->stat_graph_width
= -1; /* respect diff.statGraphWidth config */
7040 void diffcore_std(struct diff_options
*options
)
7042 int output_formats_to_prefetch
= DIFF_FORMAT_DIFFSTAT
|
7043 DIFF_FORMAT_NUMSTAT
|
7045 DIFF_FORMAT_SHORTSTAT
|
7046 DIFF_FORMAT_DIRSTAT
;
7049 * Check if the user requested a blob-data-requiring diff output and/or
7050 * break-rewrite detection (which requires blob data). If yes, prefetch
7053 * If no prefetching occurs, diffcore_rename() will prefetch if it
7054 * decides that it needs inexact rename detection.
7056 if (options
->repo
== the_repository
&& repo_has_promisor_remote(the_repository
) &&
7057 (options
->output_format
& output_formats_to_prefetch
||
7058 options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
7059 diff_queued_diff_prefetch(options
->repo
);
7061 /* NOTE please keep the following in sync with diff_tree_combined() */
7062 if (options
->skip_stat_unmatch
)
7063 diffcore_skip_stat_unmatch(options
);
7064 if (!options
->found_follow
) {
7065 /* See try_to_follow_renames() in tree-diff.c */
7066 if (options
->break_opt
!= -1)
7067 diffcore_break(options
->repo
,
7068 options
->break_opt
);
7069 if (options
->detect_rename
)
7070 diffcore_rename(options
);
7071 if (options
->break_opt
!= -1)
7072 diffcore_merge_broken();
7074 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
7075 diffcore_pickaxe(options
);
7076 if (options
->orderfile
)
7077 diffcore_order(options
->orderfile
);
7078 if (options
->rotate_to
)
7079 diffcore_rotate(options
);
7080 if (!options
->found_follow
)
7081 /* See try_to_follow_renames() in tree-diff.c */
7082 diff_resolve_rename_copy();
7083 diffcore_apply_filter(options
);
7085 if (diff_queued_diff
.nr
&& !options
->flags
.diff_from_contents
)
7086 options
->flags
.has_changes
= 1;
7088 options
->flags
.has_changes
= 0;
7090 options
->found_follow
= 0;
7093 int diff_result_code(struct rev_info
*revs
)
7095 struct diff_options
*opt
= &revs
->diffopt
;
7098 if (revs
->remerge_diff
) {
7099 tmp_objdir_destroy(revs
->remerge_objdir
);
7100 revs
->remerge_objdir
= NULL
;
7103 diff_warn_rename_limit("diff.renameLimit",
7104 opt
->needed_rename_limit
,
7105 opt
->degraded_cc_to_c
);
7107 if (opt
->flags
.exit_with_status
&&
7108 opt
->flags
.has_changes
)
7110 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
7111 opt
->flags
.check_failed
)
7116 int diff_can_quit_early(struct diff_options
*opt
)
7118 return (opt
->flags
.quick
&&
7120 opt
->flags
.has_changes
);
7124 * Shall changes to this submodule be ignored?
7126 * Submodule changes can be configured to be ignored separately for each path,
7127 * but that configuration can be overridden from the command line.
7129 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
7132 struct diff_flags orig_flags
= options
->flags
;
7133 if (!options
->flags
.override_submodule_config
)
7134 set_diffopt_flags_from_submodule_config(options
, path
);
7135 if (options
->flags
.ignore_submodules
)
7137 options
->flags
= orig_flags
;
7141 void compute_diffstat(struct diff_options
*options
,
7142 struct diffstat_t
*diffstat
,
7143 struct diff_queue_struct
*q
)
7147 memset(diffstat
, 0, sizeof(struct diffstat_t
));
7148 for (i
= 0; i
< q
->nr
; i
++) {
7149 struct diff_filepair
*p
= q
->queue
[i
];
7150 if (check_pair_status(p
))
7151 diff_flush_stat(p
, options
, diffstat
);
7153 options
->found_changes
= !!diffstat
->nr
;
7156 void diff_addremove(struct diff_options
*options
,
7157 int addremove
, unsigned mode
,
7158 const struct object_id
*oid
,
7160 const char *concatpath
, unsigned dirty_submodule
)
7162 struct diff_filespec
*one
, *two
;
7164 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
7167 /* This may look odd, but it is a preparation for
7168 * feeding "there are unchanged files which should
7169 * not produce diffs, but when you are doing copy
7170 * detection you would need them, so here they are"
7171 * entries to the diff-core. They will be prefixed
7172 * with something like '=' or '*' (I haven't decided
7173 * which but should not make any difference).
7174 * Feeding the same new and old to diff_change()
7175 * also has the same effect.
7176 * Before the final output happens, they are pruned after
7177 * merged into rename/copy pairs as appropriate.
7179 if (options
->flags
.reverse_diff
)
7180 addremove
= (addremove
== '+' ? '-' :
7181 addremove
== '-' ? '+' : addremove
);
7183 if (options
->prefix
&&
7184 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
7187 one
= alloc_filespec(concatpath
);
7188 two
= alloc_filespec(concatpath
);
7190 if (addremove
!= '+')
7191 fill_filespec(one
, oid
, oid_valid
, mode
);
7192 if (addremove
!= '-') {
7193 fill_filespec(two
, oid
, oid_valid
, mode
);
7194 two
->dirty_submodule
= dirty_submodule
;
7197 diff_queue(&diff_queued_diff
, one
, two
);
7198 if (!options
->flags
.diff_from_contents
)
7199 options
->flags
.has_changes
= 1;
7202 void diff_change(struct diff_options
*options
,
7203 unsigned old_mode
, unsigned new_mode
,
7204 const struct object_id
*old_oid
,
7205 const struct object_id
*new_oid
,
7206 int old_oid_valid
, int new_oid_valid
,
7207 const char *concatpath
,
7208 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
7210 struct diff_filespec
*one
, *two
;
7211 struct diff_filepair
*p
;
7213 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
7214 is_submodule_ignored(concatpath
, options
))
7217 if (options
->flags
.reverse_diff
) {
7218 SWAP(old_mode
, new_mode
);
7219 SWAP(old_oid
, new_oid
);
7220 SWAP(old_oid_valid
, new_oid_valid
);
7221 SWAP(old_dirty_submodule
, new_dirty_submodule
);
7224 if (options
->prefix
&&
7225 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
7228 one
= alloc_filespec(concatpath
);
7229 two
= alloc_filespec(concatpath
);
7230 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
7231 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
7232 one
->dirty_submodule
= old_dirty_submodule
;
7233 two
->dirty_submodule
= new_dirty_submodule
;
7234 p
= diff_queue(&diff_queued_diff
, one
, two
);
7236 if (options
->flags
.diff_from_contents
)
7239 if (options
->flags
.quick
&& options
->skip_stat_unmatch
&&
7240 !diff_filespec_check_stat_unmatch(options
->repo
, p
)) {
7241 diff_free_filespec_data(p
->one
);
7242 diff_free_filespec_data(p
->two
);
7246 options
->flags
.has_changes
= 1;
7249 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
7251 struct diff_filepair
*pair
;
7252 struct diff_filespec
*one
, *two
;
7254 if (options
->prefix
&&
7255 strncmp(path
, options
->prefix
, options
->prefix_length
))
7258 one
= alloc_filespec(path
);
7259 two
= alloc_filespec(path
);
7260 pair
= diff_queue(&diff_queued_diff
, one
, two
);
7261 pair
->is_unmerged
= 1;
7265 static char *run_textconv(struct repository
*r
,
7267 struct diff_filespec
*spec
,
7270 struct diff_tempfile
*temp
;
7271 struct child_process child
= CHILD_PROCESS_INIT
;
7272 struct strbuf buf
= STRBUF_INIT
;
7275 temp
= prepare_temp_file(r
, spec
);
7276 strvec_push(&child
.args
, pgm
);
7277 strvec_push(&child
.args
, temp
->name
);
7279 child
.use_shell
= 1;
7281 if (start_command(&child
)) {
7286 if (strbuf_read(&buf
, child
.out
, 0) < 0)
7287 err
= error("error reading from textconv command '%s'", pgm
);
7290 if (finish_command(&child
) || err
) {
7291 strbuf_release(&buf
);
7297 return strbuf_detach(&buf
, outsize
);
7300 size_t fill_textconv(struct repository
*r
,
7301 struct userdiff_driver
*driver
,
7302 struct diff_filespec
*df
,
7308 if (!DIFF_FILE_VALID(df
)) {
7309 *outbuf
= (char *) "";
7312 if (diff_populate_filespec(r
, df
, NULL
))
7313 die("unable to read files to diff");
7318 if (!driver
->textconv
)
7319 BUG("fill_textconv called with non-textconv driver");
7321 if (driver
->textconv_cache
&& df
->oid_valid
) {
7322 *outbuf
= notes_cache_get(driver
->textconv_cache
,
7329 *outbuf
= run_textconv(r
, driver
->textconv
, df
, &size
);
7331 die("unable to read files to diff");
7333 if (driver
->textconv_cache
&& df
->oid_valid
) {
7334 /* ignore errors, as we might be in a readonly repository */
7335 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
7338 * we could save up changes and flush them all at the end,
7339 * but we would need an extra call after all diffing is done.
7340 * Since generating a cache entry is the slow path anyway,
7341 * this extra overhead probably isn't a big deal.
7343 notes_cache_write(driver
->textconv_cache
);
7349 int textconv_object(struct repository
*r
,
7352 const struct object_id
*oid
,
7355 unsigned long *buf_size
)
7357 struct diff_filespec
*df
;
7358 struct userdiff_driver
*textconv
;
7360 df
= alloc_filespec(path
);
7361 fill_filespec(df
, oid
, oid_valid
, mode
);
7362 textconv
= get_textconv(r
, df
);
7368 *buf_size
= fill_textconv(r
, textconv
, df
, buf
);
7373 void setup_diff_pager(struct diff_options
*opt
)
7376 * If the user asked for our exit code, then either they want --quiet
7377 * or --exit-code. We should definitely not bother with a pager in the
7378 * former case, as we will generate no output. Since we still properly
7379 * report our exit code even when a pager is run, we _could_ run a
7380 * pager with --exit-code. But since we have not done so historically,
7381 * and because it is easy to find people oneline advising "git diff
7382 * --exit-code" in hooks and other scripts, we do not do so.
7384 if (!opt
->flags
.exit_with_status
&&
7385 check_pager_config("diff") != 0)