2 * Copyright (C) 2005 Junio C Hamano
4 #include "git-compat-util.h"
9 #include "environment.h"
17 #include "xdiff-interface.h"
19 #include "run-command.h"
21 #include "object-store-ll.h"
23 #include "submodule.h"
27 #include "string-list.h"
30 #include "oid-array.h"
33 #include "parse-options.h"
35 #include "promisor-remote.h"
37 #include "object-file.h"
38 #include "object-name.h"
39 #include "read-cache-ll.h"
44 #ifdef NO_FAST_WORKING_DIRECTORY
45 #define FAST_WORKING_DIRECTORY 0
47 #define FAST_WORKING_DIRECTORY 1
50 static int diff_detect_rename_default
;
51 static int diff_indent_heuristic
= 1;
52 static int diff_rename_limit_default
= 1000;
53 static int diff_suppress_blank_empty
;
54 static int diff_use_color_default
= -1;
55 static int diff_color_moved_default
;
56 static int diff_color_moved_ws_default
;
57 static int diff_context_default
= 3;
58 static int diff_interhunk_context_default
;
59 static const char *diff_word_regex_cfg
;
60 static const char *external_diff_cmd_cfg
;
61 static const char *diff_order_file_cfg
;
62 int diff_auto_refresh_index
= 1;
63 static int diff_mnemonic_prefix
;
64 static int diff_no_prefix
;
65 static const char *diff_src_prefix
= "a/";
66 static const char *diff_dst_prefix
= "b/";
67 static int diff_relative
;
68 static int diff_stat_name_width
;
69 static int diff_stat_graph_width
;
70 static int diff_dirstat_permille_default
= 30;
71 static struct diff_options default_diff_options
;
72 static long diff_algorithm
;
73 static unsigned ws_error_highlight_default
= WSEH_NEW
;
75 static char diff_colors
[][COLOR_MAXLEN
] = {
77 GIT_COLOR_NORMAL
, /* CONTEXT */
78 GIT_COLOR_BOLD
, /* METAINFO */
79 GIT_COLOR_CYAN
, /* FRAGINFO */
80 GIT_COLOR_RED
, /* OLD */
81 GIT_COLOR_GREEN
, /* NEW */
82 GIT_COLOR_YELLOW
, /* COMMIT */
83 GIT_COLOR_BG_RED
, /* WHITESPACE */
84 GIT_COLOR_NORMAL
, /* FUNCINFO */
85 GIT_COLOR_BOLD_MAGENTA
, /* OLD_MOVED */
86 GIT_COLOR_BOLD_BLUE
, /* OLD_MOVED ALTERNATIVE */
87 GIT_COLOR_FAINT
, /* OLD_MOVED_DIM */
88 GIT_COLOR_FAINT_ITALIC
, /* OLD_MOVED_ALTERNATIVE_DIM */
89 GIT_COLOR_BOLD_CYAN
, /* NEW_MOVED */
90 GIT_COLOR_BOLD_YELLOW
, /* NEW_MOVED ALTERNATIVE */
91 GIT_COLOR_FAINT
, /* NEW_MOVED_DIM */
92 GIT_COLOR_FAINT_ITALIC
, /* NEW_MOVED_ALTERNATIVE_DIM */
93 GIT_COLOR_FAINT
, /* CONTEXT_DIM */
94 GIT_COLOR_FAINT_RED
, /* OLD_DIM */
95 GIT_COLOR_FAINT_GREEN
, /* NEW_DIM */
96 GIT_COLOR_BOLD
, /* CONTEXT_BOLD */
97 GIT_COLOR_BOLD_RED
, /* OLD_BOLD */
98 GIT_COLOR_BOLD_GREEN
, /* NEW_BOLD */
101 static const char *color_diff_slots
[] = {
102 [DIFF_CONTEXT
] = "context",
103 [DIFF_METAINFO
] = "meta",
104 [DIFF_FRAGINFO
] = "frag",
105 [DIFF_FILE_OLD
] = "old",
106 [DIFF_FILE_NEW
] = "new",
107 [DIFF_COMMIT
] = "commit",
108 [DIFF_WHITESPACE
] = "whitespace",
109 [DIFF_FUNCINFO
] = "func",
110 [DIFF_FILE_OLD_MOVED
] = "oldMoved",
111 [DIFF_FILE_OLD_MOVED_ALT
] = "oldMovedAlternative",
112 [DIFF_FILE_OLD_MOVED_DIM
] = "oldMovedDimmed",
113 [DIFF_FILE_OLD_MOVED_ALT_DIM
] = "oldMovedAlternativeDimmed",
114 [DIFF_FILE_NEW_MOVED
] = "newMoved",
115 [DIFF_FILE_NEW_MOVED_ALT
] = "newMovedAlternative",
116 [DIFF_FILE_NEW_MOVED_DIM
] = "newMovedDimmed",
117 [DIFF_FILE_NEW_MOVED_ALT_DIM
] = "newMovedAlternativeDimmed",
118 [DIFF_CONTEXT_DIM
] = "contextDimmed",
119 [DIFF_FILE_OLD_DIM
] = "oldDimmed",
120 [DIFF_FILE_NEW_DIM
] = "newDimmed",
121 [DIFF_CONTEXT_BOLD
] = "contextBold",
122 [DIFF_FILE_OLD_BOLD
] = "oldBold",
123 [DIFF_FILE_NEW_BOLD
] = "newBold",
126 define_list_config_array_extra(color_diff_slots
, {"plain"});
128 static int parse_diff_color_slot(const char *var
)
130 if (!strcasecmp(var
, "plain"))
132 return LOOKUP_CONFIG(color_diff_slots
, var
);
135 static int parse_dirstat_params(struct diff_options
*options
, const char *params_string
,
136 struct strbuf
*errmsg
)
138 char *params_copy
= xstrdup(params_string
);
139 struct string_list params
= STRING_LIST_INIT_NODUP
;
144 string_list_split_in_place(¶ms
, params_copy
, ",", -1);
145 for (i
= 0; i
< params
.nr
; i
++) {
146 const char *p
= params
.items
[i
].string
;
147 if (!strcmp(p
, "changes")) {
148 options
->flags
.dirstat_by_line
= 0;
149 options
->flags
.dirstat_by_file
= 0;
150 } else if (!strcmp(p
, "lines")) {
151 options
->flags
.dirstat_by_line
= 1;
152 options
->flags
.dirstat_by_file
= 0;
153 } else if (!strcmp(p
, "files")) {
154 options
->flags
.dirstat_by_line
= 0;
155 options
->flags
.dirstat_by_file
= 1;
156 } else if (!strcmp(p
, "noncumulative")) {
157 options
->flags
.dirstat_cumulative
= 0;
158 } else if (!strcmp(p
, "cumulative")) {
159 options
->flags
.dirstat_cumulative
= 1;
160 } else if (isdigit(*p
)) {
162 int permille
= strtoul(p
, &end
, 10) * 10;
163 if (*end
== '.' && isdigit(*++end
)) {
164 /* only use first digit */
165 permille
+= *end
- '0';
166 /* .. and ignore any further digits */
167 while (isdigit(*++end
))
171 options
->dirstat_permille
= permille
;
173 strbuf_addf(errmsg
, _(" Failed to parse dirstat cut-off percentage '%s'\n"),
178 strbuf_addf(errmsg
, _(" Unknown dirstat parameter '%s'\n"), p
);
183 string_list_clear(¶ms
, 0);
188 static int parse_submodule_params(struct diff_options
*options
, const char *value
)
190 if (!strcmp(value
, "log"))
191 options
->submodule_format
= DIFF_SUBMODULE_LOG
;
192 else if (!strcmp(value
, "short"))
193 options
->submodule_format
= DIFF_SUBMODULE_SHORT
;
194 else if (!strcmp(value
, "diff"))
195 options
->submodule_format
= DIFF_SUBMODULE_INLINE_DIFF
;
197 * Please update $__git_diff_submodule_formats in
198 * git-completion.bash when you add new formats.
205 int git_config_rename(const char *var
, const char *value
)
208 return DIFF_DETECT_RENAME
;
209 if (!strcasecmp(value
, "copies") || !strcasecmp(value
, "copy"))
210 return DIFF_DETECT_COPY
;
211 return git_config_bool(var
,value
) ? DIFF_DETECT_RENAME
: 0;
214 long parse_algorithm_value(const char *value
)
218 else if (!strcasecmp(value
, "myers") || !strcasecmp(value
, "default"))
220 else if (!strcasecmp(value
, "minimal"))
221 return XDF_NEED_MINIMAL
;
222 else if (!strcasecmp(value
, "patience"))
223 return XDF_PATIENCE_DIFF
;
224 else if (!strcasecmp(value
, "histogram"))
225 return XDF_HISTOGRAM_DIFF
;
227 * Please update $__git_diff_algorithms in git-completion.bash
228 * when you add new algorithms.
233 static int parse_one_token(const char **arg
, const char *token
)
236 if (skip_prefix(*arg
, token
, &rest
) && (!*rest
|| *rest
== ',')) {
243 static int parse_ws_error_highlight(const char *arg
)
245 const char *orig_arg
= arg
;
249 if (parse_one_token(&arg
, "none"))
251 else if (parse_one_token(&arg
, "default"))
253 else if (parse_one_token(&arg
, "all"))
254 val
= WSEH_NEW
| WSEH_OLD
| WSEH_CONTEXT
;
255 else if (parse_one_token(&arg
, "new"))
257 else if (parse_one_token(&arg
, "old"))
259 else if (parse_one_token(&arg
, "context"))
262 return -1 - (int)(arg
- orig_arg
);
271 * These are to give UI layer defaults.
272 * The core-level commands such as git-diff-files should
273 * never be affected by the setting of diff.renames
274 * the user happens to have in the configuration file.
276 void init_diff_ui_defaults(void)
278 diff_detect_rename_default
= DIFF_DETECT_RENAME
;
281 int git_diff_heuristic_config(const char *var
, const char *value
,
284 if (!strcmp(var
, "diff.indentheuristic"))
285 diff_indent_heuristic
= git_config_bool(var
, value
);
289 static int parse_color_moved(const char *arg
)
291 switch (git_parse_maybe_bool(arg
)) {
293 return COLOR_MOVED_NO
;
295 return COLOR_MOVED_DEFAULT
;
300 if (!strcmp(arg
, "no"))
301 return COLOR_MOVED_NO
;
302 else if (!strcmp(arg
, "plain"))
303 return COLOR_MOVED_PLAIN
;
304 else if (!strcmp(arg
, "blocks"))
305 return COLOR_MOVED_BLOCKS
;
306 else if (!strcmp(arg
, "zebra"))
307 return COLOR_MOVED_ZEBRA
;
308 else if (!strcmp(arg
, "default"))
309 return COLOR_MOVED_DEFAULT
;
310 else if (!strcmp(arg
, "dimmed-zebra"))
311 return COLOR_MOVED_ZEBRA_DIM
;
312 else if (!strcmp(arg
, "dimmed_zebra"))
313 return COLOR_MOVED_ZEBRA_DIM
;
315 return error(_("color moved setting must be one of 'no', 'default', 'blocks', 'zebra', 'dimmed-zebra', 'plain'"));
318 static unsigned parse_color_moved_ws(const char *arg
)
321 struct string_list l
= STRING_LIST_INIT_DUP
;
322 struct string_list_item
*i
;
324 string_list_split(&l
, arg
, ',', -1);
326 for_each_string_list_item(i
, &l
) {
327 struct strbuf sb
= STRBUF_INIT
;
328 strbuf_addstr(&sb
, i
->string
);
331 if (!strcmp(sb
.buf
, "no"))
333 else if (!strcmp(sb
.buf
, "ignore-space-change"))
334 ret
|= XDF_IGNORE_WHITESPACE_CHANGE
;
335 else if (!strcmp(sb
.buf
, "ignore-space-at-eol"))
336 ret
|= XDF_IGNORE_WHITESPACE_AT_EOL
;
337 else if (!strcmp(sb
.buf
, "ignore-all-space"))
338 ret
|= XDF_IGNORE_WHITESPACE
;
339 else if (!strcmp(sb
.buf
, "allow-indentation-change"))
340 ret
|= COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
;
342 ret
|= COLOR_MOVED_WS_ERROR
;
343 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
);
349 if ((ret
& COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
) &&
350 (ret
& XDF_WHITESPACE_FLAGS
)) {
351 error(_("color-moved-ws: allow-indentation-change cannot be combined with other whitespace modes"));
352 ret
|= COLOR_MOVED_WS_ERROR
;
355 string_list_clear(&l
, 0);
360 int git_diff_ui_config(const char *var
, const char *value
,
361 const struct config_context
*ctx
, void *cb
)
363 if (!strcmp(var
, "diff.color") || !strcmp(var
, "color.diff")) {
364 diff_use_color_default
= git_config_colorbool(var
, value
);
367 if (!strcmp(var
, "diff.colormoved")) {
368 int cm
= parse_color_moved(value
);
371 diff_color_moved_default
= cm
;
374 if (!strcmp(var
, "diff.colormovedws")) {
377 return config_error_nonbool(var
);
378 cm
= parse_color_moved_ws(value
);
379 if (cm
& COLOR_MOVED_WS_ERROR
)
381 diff_color_moved_ws_default
= cm
;
384 if (!strcmp(var
, "diff.context")) {
385 diff_context_default
= git_config_int(var
, value
, ctx
->kvi
);
386 if (diff_context_default
< 0)
390 if (!strcmp(var
, "diff.interhunkcontext")) {
391 diff_interhunk_context_default
= git_config_int(var
, value
,
393 if (diff_interhunk_context_default
< 0)
397 if (!strcmp(var
, "diff.renames")) {
398 diff_detect_rename_default
= git_config_rename(var
, value
);
401 if (!strcmp(var
, "diff.autorefreshindex")) {
402 diff_auto_refresh_index
= git_config_bool(var
, value
);
405 if (!strcmp(var
, "diff.mnemonicprefix")) {
406 diff_mnemonic_prefix
= git_config_bool(var
, value
);
409 if (!strcmp(var
, "diff.noprefix")) {
410 diff_no_prefix
= git_config_bool(var
, value
);
413 if (!strcmp(var
, "diff.srcprefix")) {
414 return git_config_string(&diff_src_prefix
, var
, value
);
416 if (!strcmp(var
, "diff.dstprefix")) {
417 return git_config_string(&diff_dst_prefix
, var
, value
);
419 if (!strcmp(var
, "diff.relative")) {
420 diff_relative
= git_config_bool(var
, value
);
423 if (!strcmp(var
, "diff.statnamewidth")) {
424 diff_stat_name_width
= git_config_int(var
, value
, ctx
->kvi
);
427 if (!strcmp(var
, "diff.statgraphwidth")) {
428 diff_stat_graph_width
= git_config_int(var
, value
, ctx
->kvi
);
431 if (!strcmp(var
, "diff.external"))
432 return git_config_string(&external_diff_cmd_cfg
, var
, value
);
433 if (!strcmp(var
, "diff.wordregex"))
434 return git_config_string(&diff_word_regex_cfg
, var
, value
);
435 if (!strcmp(var
, "diff.orderfile"))
436 return git_config_pathname(&diff_order_file_cfg
, var
, value
);
438 if (!strcmp(var
, "diff.ignoresubmodules")) {
440 return config_error_nonbool(var
);
441 handle_ignore_submodules_arg(&default_diff_options
, value
);
444 if (!strcmp(var
, "diff.submodule")) {
446 return config_error_nonbool(var
);
447 if (parse_submodule_params(&default_diff_options
, value
))
448 warning(_("Unknown value for 'diff.submodule' config variable: '%s'"),
453 if (!strcmp(var
, "diff.algorithm")) {
455 return config_error_nonbool(var
);
456 diff_algorithm
= parse_algorithm_value(value
);
457 if (diff_algorithm
< 0)
458 return error(_("unknown value for config '%s': %s"),
463 if (git_color_config(var
, value
, cb
) < 0)
466 return git_diff_basic_config(var
, value
, ctx
, cb
);
469 int git_diff_basic_config(const char *var
, const char *value
,
470 const struct config_context
*ctx
, void *cb
)
474 if (!strcmp(var
, "diff.renamelimit")) {
475 diff_rename_limit_default
= git_config_int(var
, value
, ctx
->kvi
);
479 if (userdiff_config(var
, value
) < 0)
482 if (skip_prefix(var
, "diff.color.", &name
) ||
483 skip_prefix(var
, "color.diff.", &name
)) {
484 int slot
= parse_diff_color_slot(name
);
488 return config_error_nonbool(var
);
489 return color_parse(value
, diff_colors
[slot
]);
492 if (!strcmp(var
, "diff.wserrorhighlight")) {
495 return config_error_nonbool(var
);
496 val
= parse_ws_error_highlight(value
);
498 return error(_("unknown value for config '%s': %s"),
500 ws_error_highlight_default
= val
;
504 /* like GNU diff's --suppress-blank-empty option */
505 if (!strcmp(var
, "diff.suppressblankempty") ||
506 /* for backwards compatibility */
507 !strcmp(var
, "diff.suppress-blank-empty")) {
508 diff_suppress_blank_empty
= git_config_bool(var
, value
);
512 if (!strcmp(var
, "diff.dirstat")) {
513 struct strbuf errmsg
= STRBUF_INIT
;
515 return config_error_nonbool(var
);
516 default_diff_options
.dirstat_permille
= diff_dirstat_permille_default
;
517 if (parse_dirstat_params(&default_diff_options
, value
, &errmsg
))
518 warning(_("Found errors in 'diff.dirstat' config variable:\n%s"),
520 strbuf_release(&errmsg
);
521 diff_dirstat_permille_default
= default_diff_options
.dirstat_permille
;
525 if (git_diff_heuristic_config(var
, value
, cb
) < 0)
528 return git_default_config(var
, value
, ctx
, cb
);
531 static char *quote_two(const char *one
, const char *two
)
533 int need_one
= quote_c_style(one
, NULL
, NULL
, CQUOTE_NODQ
);
534 int need_two
= quote_c_style(two
, NULL
, NULL
, CQUOTE_NODQ
);
535 struct strbuf res
= STRBUF_INIT
;
537 if (need_one
+ need_two
) {
538 strbuf_addch(&res
, '"');
539 quote_c_style(one
, &res
, NULL
, CQUOTE_NODQ
);
540 quote_c_style(two
, &res
, NULL
, CQUOTE_NODQ
);
541 strbuf_addch(&res
, '"');
543 strbuf_addstr(&res
, one
);
544 strbuf_addstr(&res
, two
);
546 return strbuf_detach(&res
, NULL
);
549 static const char *external_diff(void)
551 static const char *external_diff_cmd
= NULL
;
552 static int done_preparing
= 0;
555 return external_diff_cmd
;
556 external_diff_cmd
= xstrdup_or_null(getenv("GIT_EXTERNAL_DIFF"));
557 if (!external_diff_cmd
)
558 external_diff_cmd
= external_diff_cmd_cfg
;
560 return external_diff_cmd
;
564 * Keep track of files used for diffing. Sometimes such an entry
565 * refers to a temporary file, sometimes to an existing file, and
566 * sometimes to "/dev/null".
568 static struct diff_tempfile
{
570 * filename external diff should read from, or NULL if this
571 * entry is currently not in use:
575 char hex
[GIT_MAX_HEXSZ
+ 1];
579 * If this diff_tempfile instance refers to a temporary file,
580 * this tempfile object is used to manage its lifetime.
582 struct tempfile
*tempfile
;
585 struct emit_callback
{
588 int blank_at_eof_in_preimage
;
589 int blank_at_eof_in_postimage
;
591 int lno_in_postimage
;
592 const char **label_path
;
593 struct diff_words_data
*diff_words
;
594 struct diff_options
*opt
;
595 struct strbuf
*header
;
598 static int count_lines(const char *data
, int size
)
600 int count
, ch
, completely_empty
= 1, nl_just_seen
= 0;
607 completely_empty
= 0;
611 completely_empty
= 0;
614 if (completely_empty
)
617 count
++; /* no trailing newline */
621 static int fill_mmfile(struct repository
*r
, mmfile_t
*mf
,
622 struct diff_filespec
*one
)
624 if (!DIFF_FILE_VALID(one
)) {
625 mf
->ptr
= (char *)""; /* does not matter */
629 else if (diff_populate_filespec(r
, one
, NULL
))
633 mf
->size
= one
->size
;
637 /* like fill_mmfile, but only for size, so we can avoid retrieving blob */
638 static unsigned long diff_filespec_size(struct repository
*r
,
639 struct diff_filespec
*one
)
641 struct diff_populate_filespec_options dpf_options
= {
642 .check_size_only
= 1,
645 if (!DIFF_FILE_VALID(one
))
647 diff_populate_filespec(r
, one
, &dpf_options
);
651 static int count_trailing_blank(mmfile_t
*mf
)
654 long size
= mf
->size
;
659 ptr
+= size
- 1; /* pointing at the very end */
661 ; /* incomplete line */
663 ptr
--; /* skip the last LF */
664 while (mf
->ptr
< ptr
) {
666 for (prev_eol
= ptr
; mf
->ptr
<= prev_eol
; prev_eol
--)
667 if (*prev_eol
== '\n')
669 if (!ws_blank_line(prev_eol
+ 1, ptr
- prev_eol
))
677 static void check_blank_at_eof(mmfile_t
*mf1
, mmfile_t
*mf2
,
678 struct emit_callback
*ecbdata
)
681 l1
= count_trailing_blank(mf1
);
682 l2
= count_trailing_blank(mf2
);
684 ecbdata
->blank_at_eof_in_preimage
= 0;
685 ecbdata
->blank_at_eof_in_postimage
= 0;
688 at
= count_lines(mf1
->ptr
, mf1
->size
);
689 ecbdata
->blank_at_eof_in_preimage
= (at
- l1
) + 1;
691 at
= count_lines(mf2
->ptr
, mf2
->size
);
692 ecbdata
->blank_at_eof_in_postimage
= (at
- l2
) + 1;
695 static void emit_line_0(struct diff_options
*o
,
696 const char *set_sign
, const char *set
, unsigned reverse
, const char *reset
,
697 int first
, const char *line
, int len
)
699 int has_trailing_newline
, has_trailing_carriage_return
;
700 int needs_reset
= 0; /* at the end of the line */
701 FILE *file
= o
->file
;
703 fputs(diff_line_prefix(o
), file
);
705 has_trailing_newline
= (len
> 0 && line
[len
-1] == '\n');
706 if (has_trailing_newline
)
709 has_trailing_carriage_return
= (len
> 0 && line
[len
-1] == '\r');
710 if (has_trailing_carriage_return
)
716 if (reverse
&& want_color(o
->use_color
)) {
717 fputs(GIT_COLOR_REVERSE
, file
);
722 fputs(set_sign
, file
);
733 if (set_sign
&& set
!= set_sign
)
738 fwrite(line
, len
, 1, file
);
739 needs_reset
= 1; /* 'line' may contain color codes. */
744 if (has_trailing_carriage_return
)
746 if (has_trailing_newline
)
750 static void emit_line(struct diff_options
*o
, const char *set
, const char *reset
,
751 const char *line
, int len
)
753 emit_line_0(o
, set
, NULL
, 0, reset
, 0, line
, len
);
757 DIFF_SYMBOL_BINARY_DIFF_HEADER
,
758 DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
759 DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
760 DIFF_SYMBOL_BINARY_DIFF_BODY
,
761 DIFF_SYMBOL_BINARY_DIFF_FOOTER
,
762 DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
763 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
764 DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
765 DIFF_SYMBOL_STATS_LINE
,
766 DIFF_SYMBOL_WORD_DIFF
,
767 DIFF_SYMBOL_STAT_SEP
,
769 DIFF_SYMBOL_SUBMODULE_ADD
,
770 DIFF_SYMBOL_SUBMODULE_DEL
,
771 DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
772 DIFF_SYMBOL_SUBMODULE_MODIFIED
,
773 DIFF_SYMBOL_SUBMODULE_HEADER
,
774 DIFF_SYMBOL_SUBMODULE_ERROR
,
775 DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
,
776 DIFF_SYMBOL_REWRITE_DIFF
,
777 DIFF_SYMBOL_BINARY_FILES
,
779 DIFF_SYMBOL_FILEPAIR_PLUS
,
780 DIFF_SYMBOL_FILEPAIR_MINUS
,
781 DIFF_SYMBOL_WORDS_PORCELAIN
,
784 DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
787 DIFF_SYMBOL_NO_LF_EOF
,
788 DIFF_SYMBOL_CONTEXT_FRAGINFO
,
789 DIFF_SYMBOL_CONTEXT_MARKER
,
790 DIFF_SYMBOL_SEPARATOR
793 * Flags for content lines:
794 * 0..12 are whitespace rules
795 * 13-15 are WSEH_NEW | WSEH_OLD | WSEH_CONTEXT
796 * 16 is marking if the line is blank at EOF
798 #define DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF (1<<16)
799 #define DIFF_SYMBOL_MOVED_LINE (1<<17)
800 #define DIFF_SYMBOL_MOVED_LINE_ALT (1<<18)
801 #define DIFF_SYMBOL_MOVED_LINE_UNINTERESTING (1<<19)
802 #define DIFF_SYMBOL_CONTENT_WS_MASK (WSEH_NEW | WSEH_OLD | WSEH_CONTEXT | WS_RULE_MASK)
805 * This struct is used when we need to buffer the output of the diff output.
807 * NEEDSWORK: Instead of storing a copy of the line, add an offset pointer
808 * into the pre/post image file. This pointer could be a union with the
809 * line pointer. By storing an offset into the file instead of the literal line,
810 * we can decrease the memory footprint for the buffered output. At first we
811 * may want to only have indirection for the content lines, but we could also
812 * enhance the state for emitting prefabricated lines, e.g. the similarity
813 * score line or hunk/file headers would only need to store a number or path
814 * and then the output can be constructed later on depending on state.
816 struct emitted_diff_symbol
{
820 int indent_off
; /* Offset to first non-whitespace character */
821 int indent_width
; /* The visual width of the indentation */
825 #define EMITTED_DIFF_SYMBOL_INIT { 0 }
827 struct emitted_diff_symbols
{
828 struct emitted_diff_symbol
*buf
;
831 #define EMITTED_DIFF_SYMBOLS_INIT { 0 }
833 static void append_emitted_diff_symbol(struct diff_options
*o
,
834 struct emitted_diff_symbol
*e
)
836 struct emitted_diff_symbol
*f
;
838 ALLOC_GROW(o
->emitted_symbols
->buf
,
839 o
->emitted_symbols
->nr
+ 1,
840 o
->emitted_symbols
->alloc
);
841 f
= &o
->emitted_symbols
->buf
[o
->emitted_symbols
->nr
++];
843 memcpy(f
, e
, sizeof(struct emitted_diff_symbol
));
844 f
->line
= e
->line
? xmemdupz(e
->line
, e
->len
) : NULL
;
847 static void free_emitted_diff_symbols(struct emitted_diff_symbols
*e
)
856 const struct emitted_diff_symbol
*es
;
857 struct moved_entry
*next_line
;
858 struct moved_entry
*next_match
;
862 struct moved_entry
*match
;
863 int wsd
; /* The whitespace delta of this block */
866 #define INDENT_BLANKLINE INT_MIN
868 static void fill_es_indent_data(struct emitted_diff_symbol
*es
)
870 unsigned int off
= 0, i
;
871 int width
= 0, tab_width
= es
->flags
& WS_TAB_WIDTH_MASK
;
872 const char *s
= es
->line
;
873 const int len
= es
->len
;
875 /* skip any \v \f \r at start of indentation */
876 while (s
[off
] == '\f' || s
[off
] == '\v' ||
877 (s
[off
] == '\r' && off
< len
- 1))
880 /* calculate the visual width of indentation */
885 } else if (s
[off
] == '\t') {
886 width
+= tab_width
- (width
% tab_width
);
887 while (s
[++off
] == '\t')
894 /* check if this line is blank */
895 for (i
= off
; i
< len
; i
++)
900 es
->indent_width
= INDENT_BLANKLINE
;
901 es
->indent_off
= len
;
903 es
->indent_off
= off
;
904 es
->indent_width
= width
;
908 static int compute_ws_delta(const struct emitted_diff_symbol
*a
,
909 const struct emitted_diff_symbol
*b
)
911 int a_width
= a
->indent_width
,
912 b_width
= b
->indent_width
;
914 if (a_width
== INDENT_BLANKLINE
&& b_width
== INDENT_BLANKLINE
)
915 return INDENT_BLANKLINE
;
917 return a_width
- b_width
;
920 static int cmp_in_block_with_wsd(const struct moved_entry
*cur
,
921 const struct emitted_diff_symbol
*l
,
922 struct moved_block
*pmb
)
924 int a_width
= cur
->es
->indent_width
, b_width
= l
->indent_width
;
927 /* The text of each line must match */
928 if (cur
->es
->id
!= l
->id
)
932 * If 'l' and 'cur' are both blank then we don't need to check the
933 * indent. We only need to check cur as we know the strings match.
935 if (a_width
== INDENT_BLANKLINE
)
939 * The indent changes of the block are known and stored in pmb->wsd;
940 * however we need to check if the indent changes of the current line
941 * match those of the current block.
943 delta
= b_width
- a_width
;
946 * If the previous lines of this block were all blank then set its
949 if (pmb
->wsd
== INDENT_BLANKLINE
)
952 return delta
!= pmb
->wsd
;
955 struct interned_diff_symbol
{
956 struct hashmap_entry ent
;
957 struct emitted_diff_symbol
*es
;
960 static int interned_diff_symbol_cmp(const void *hashmap_cmp_fn_data
,
961 const struct hashmap_entry
*eptr
,
962 const struct hashmap_entry
*entry_or_key
,
963 const void *keydata UNUSED
)
965 const struct diff_options
*diffopt
= hashmap_cmp_fn_data
;
966 const struct emitted_diff_symbol
*a
, *b
;
967 unsigned flags
= diffopt
->color_moved_ws_handling
968 & XDF_WHITESPACE_FLAGS
;
970 a
= container_of(eptr
, const struct interned_diff_symbol
, ent
)->es
;
971 b
= container_of(entry_or_key
, const struct interned_diff_symbol
, ent
)->es
;
973 return !xdiff_compare_lines(a
->line
+ a
->indent_off
,
974 a
->len
- a
->indent_off
,
975 b
->line
+ b
->indent_off
,
976 b
->len
- b
->indent_off
, flags
);
979 static void prepare_entry(struct diff_options
*o
, struct emitted_diff_symbol
*l
,
980 struct interned_diff_symbol
*s
)
982 unsigned flags
= o
->color_moved_ws_handling
& XDF_WHITESPACE_FLAGS
;
983 unsigned int hash
= xdiff_hash_string(l
->line
+ l
->indent_off
,
984 l
->len
- l
->indent_off
, flags
);
986 hashmap_entry_init(&s
->ent
, hash
);
990 struct moved_entry_list
{
991 struct moved_entry
*add
, *del
;
994 static struct moved_entry_list
*add_lines_to_move_detection(struct diff_options
*o
,
995 struct mem_pool
*entry_mem_pool
)
997 struct moved_entry
*prev_line
= NULL
;
998 struct mem_pool interned_pool
;
999 struct hashmap interned_map
;
1000 struct moved_entry_list
*entry_list
= NULL
;
1001 size_t entry_list_alloc
= 0;
1005 hashmap_init(&interned_map
, interned_diff_symbol_cmp
, o
, 8096);
1006 mem_pool_init(&interned_pool
, 1024 * 1024);
1008 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1009 struct interned_diff_symbol key
;
1010 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1011 struct interned_diff_symbol
*s
;
1012 struct moved_entry
*entry
;
1014 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
) {
1019 if (o
->color_moved_ws_handling
&
1020 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1021 fill_es_indent_data(l
);
1023 prepare_entry(o
, l
, &key
);
1024 s
= hashmap_get_entry(&interned_map
, &key
, ent
, &key
.ent
);
1029 ALLOC_GROW_BY(entry_list
, id
, 1, entry_list_alloc
);
1030 hashmap_add(&interned_map
,
1031 memcpy(mem_pool_alloc(&interned_pool
,
1033 &key
, sizeof(key
)));
1035 entry
= mem_pool_alloc(entry_mem_pool
, sizeof(*entry
));
1037 entry
->next_line
= NULL
;
1038 if (prev_line
&& prev_line
->es
->s
== l
->s
)
1039 prev_line
->next_line
= entry
;
1041 if (l
->s
== DIFF_SYMBOL_PLUS
) {
1042 entry
->next_match
= entry_list
[l
->id
].add
;
1043 entry_list
[l
->id
].add
= entry
;
1045 entry
->next_match
= entry_list
[l
->id
].del
;
1046 entry_list
[l
->id
].del
= entry
;
1050 hashmap_clear(&interned_map
);
1051 mem_pool_discard(&interned_pool
, 0);
1056 static void pmb_advance_or_null(struct diff_options
*o
,
1057 struct emitted_diff_symbol
*l
,
1058 struct moved_block
*pmb
,
1063 for (i
= 0, j
= 0; i
< *pmb_nr
; i
++) {
1065 struct moved_entry
*prev
= pmb
[i
].match
;
1066 struct moved_entry
*cur
= (prev
&& prev
->next_line
) ?
1067 prev
->next_line
: NULL
;
1069 if (o
->color_moved_ws_handling
&
1070 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1072 !cmp_in_block_with_wsd(cur
, l
, &pmb
[i
]);
1074 match
= cur
&& cur
->es
->id
== l
->id
;
1078 pmb
[j
++].match
= cur
;
1084 static void fill_potential_moved_blocks(struct diff_options
*o
,
1085 struct moved_entry
*match
,
1086 struct emitted_diff_symbol
*l
,
1087 struct moved_block
**pmb_p
,
1088 int *pmb_alloc_p
, int *pmb_nr_p
)
1091 struct moved_block
*pmb
= *pmb_p
;
1092 int pmb_alloc
= *pmb_alloc_p
, pmb_nr
= *pmb_nr_p
;
1095 * The current line is the start of a new block.
1096 * Setup the set of potential blocks.
1098 for (; match
; match
= match
->next_match
) {
1099 ALLOC_GROW(pmb
, pmb_nr
+ 1, pmb_alloc
);
1100 if (o
->color_moved_ws_handling
&
1101 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE
)
1102 pmb
[pmb_nr
].wsd
= compute_ws_delta(l
, match
->es
);
1104 pmb
[pmb_nr
].wsd
= 0;
1105 pmb
[pmb_nr
++].match
= match
;
1109 *pmb_alloc_p
= pmb_alloc
;
1114 * If o->color_moved is COLOR_MOVED_PLAIN, this function does nothing.
1116 * Otherwise, if the last block has fewer alphanumeric characters than
1117 * COLOR_MOVED_MIN_ALNUM_COUNT, unset DIFF_SYMBOL_MOVED_LINE on all lines in
1120 * The last block consists of the (n - block_length)'th line up to but not
1121 * including the nth line.
1123 * Returns 0 if the last block is empty or is unset by this function, non zero
1126 * NEEDSWORK: This uses the same heuristic as blame_entry_score() in blame.c.
1127 * Think of a way to unify them.
1129 #define DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK \
1130 (DIFF_SYMBOL_MOVED_LINE | DIFF_SYMBOL_MOVED_LINE_ALT)
1131 static int adjust_last_block(struct diff_options
*o
, int n
, int block_length
)
1133 int i
, alnum_count
= 0;
1134 if (o
->color_moved
== COLOR_MOVED_PLAIN
)
1135 return block_length
;
1136 for (i
= 1; i
< block_length
+ 1; i
++) {
1137 const char *c
= o
->emitted_symbols
->buf
[n
- i
].line
;
1142 if (alnum_count
>= COLOR_MOVED_MIN_ALNUM_COUNT
)
1146 for (i
= 1; i
< block_length
+ 1; i
++)
1147 o
->emitted_symbols
->buf
[n
- i
].flags
&= ~DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
;
1151 /* Find blocks of moved code, delegate actual coloring decision to helper */
1152 static void mark_color_as_moved(struct diff_options
*o
,
1153 struct moved_entry_list
*entry_list
)
1155 struct moved_block
*pmb
= NULL
; /* potentially moved blocks */
1156 int pmb_nr
= 0, pmb_alloc
= 0;
1157 int n
, flipped_block
= 0, block_length
= 0;
1158 enum diff_symbol moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1161 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1162 struct moved_entry
*match
= NULL
;
1163 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1166 case DIFF_SYMBOL_PLUS
:
1167 match
= entry_list
[l
->id
].del
;
1169 case DIFF_SYMBOL_MINUS
:
1170 match
= entry_list
[l
->id
].add
;
1176 if (pmb_nr
&& (!match
|| l
->s
!= moved_symbol
)) {
1177 if (!adjust_last_block(o
, n
, block_length
) &&
1180 * Rewind in case there is another match
1181 * starting at the second line of the block
1191 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1195 if (o
->color_moved
== COLOR_MOVED_PLAIN
) {
1196 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1200 pmb_advance_or_null(o
, l
, pmb
, &pmb_nr
);
1203 int contiguous
= adjust_last_block(o
, n
, block_length
);
1205 if (!contiguous
&& block_length
> 1)
1207 * Rewind in case there is another match
1208 * starting at the second line of the block
1212 fill_potential_moved_blocks(o
, match
, l
,
1216 if (contiguous
&& pmb_nr
&& moved_symbol
== l
->s
)
1217 flipped_block
= (flipped_block
+ 1) % 2;
1222 moved_symbol
= l
->s
;
1224 moved_symbol
= DIFF_SYMBOL_BINARY_DIFF_HEADER
;
1231 l
->flags
|= DIFF_SYMBOL_MOVED_LINE
;
1232 if (flipped_block
&& o
->color_moved
!= COLOR_MOVED_BLOCKS
)
1233 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_ALT
;
1236 adjust_last_block(o
, n
, block_length
);
1241 static void dim_moved_lines(struct diff_options
*o
)
1244 for (n
= 0; n
< o
->emitted_symbols
->nr
; n
++) {
1245 struct emitted_diff_symbol
*prev
= (n
!= 0) ?
1246 &o
->emitted_symbols
->buf
[n
- 1] : NULL
;
1247 struct emitted_diff_symbol
*l
= &o
->emitted_symbols
->buf
[n
];
1248 struct emitted_diff_symbol
*next
=
1249 (n
< o
->emitted_symbols
->nr
- 1) ?
1250 &o
->emitted_symbols
->buf
[n
+ 1] : NULL
;
1252 /* Not a plus or minus line? */
1253 if (l
->s
!= DIFF_SYMBOL_PLUS
&& l
->s
!= DIFF_SYMBOL_MINUS
)
1256 /* Not a moved line? */
1257 if (!(l
->flags
& DIFF_SYMBOL_MOVED_LINE
))
1261 * If prev or next are not a plus or minus line,
1262 * pretend they don't exist
1264 if (prev
&& prev
->s
!= DIFF_SYMBOL_PLUS
&&
1265 prev
->s
!= DIFF_SYMBOL_MINUS
)
1267 if (next
&& next
->s
!= DIFF_SYMBOL_PLUS
&&
1268 next
->s
!= DIFF_SYMBOL_MINUS
)
1271 /* Inside a block? */
1273 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1274 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
)) &&
1276 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
) ==
1277 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ZEBRA_MASK
))) {
1278 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1282 /* Check if we are at an interesting bound: */
1283 if (prev
&& (prev
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1284 (prev
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1285 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1287 if (next
&& (next
->flags
& DIFF_SYMBOL_MOVED_LINE
) &&
1288 (next
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
) !=
1289 (l
->flags
& DIFF_SYMBOL_MOVED_LINE_ALT
))
1293 * The boundary to prev and next are not interesting,
1294 * so this line is not interesting as a whole
1296 l
->flags
|= DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
;
1300 static void emit_line_ws_markup(struct diff_options
*o
,
1301 const char *set_sign
, const char *set
,
1303 int sign_index
, const char *line
, int len
,
1304 unsigned ws_rule
, int blank_at_eof
)
1306 const char *ws
= NULL
;
1307 int sign
= o
->output_indicators
[sign_index
];
1309 if (o
->ws_error_highlight
& ws_rule
) {
1310 ws
= diff_get_color_opt(o
, DIFF_WHITESPACE
);
1315 if (!ws
&& !set_sign
)
1316 emit_line_0(o
, set
, NULL
, 0, reset
, sign
, line
, len
);
1318 emit_line_0(o
, set_sign
, set
, !!set_sign
, reset
, sign
, line
, len
);
1319 } else if (blank_at_eof
)
1320 /* Blank line at EOF - paint '+' as well */
1321 emit_line_0(o
, ws
, NULL
, 0, reset
, sign
, line
, len
);
1323 /* Emit just the prefix, then the rest. */
1324 emit_line_0(o
, set_sign
? set_sign
: set
, NULL
, !!set_sign
, reset
,
1326 ws_check_emit(line
, len
, ws_rule
,
1327 o
->file
, set
, reset
, ws
);
1331 static void emit_diff_symbol_from_struct(struct diff_options
*o
,
1332 struct emitted_diff_symbol
*eds
)
1334 static const char *nneof
= " No newline at end of file\n";
1335 const char *context
, *reset
, *set
, *set_sign
, *meta
, *fraginfo
;
1337 enum diff_symbol s
= eds
->s
;
1338 const char *line
= eds
->line
;
1340 unsigned flags
= eds
->flags
;
1343 case DIFF_SYMBOL_NO_LF_EOF
:
1344 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1345 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1346 putc('\n', o
->file
);
1347 emit_line_0(o
, context
, NULL
, 0, reset
, '\\',
1348 nneof
, strlen(nneof
));
1350 case DIFF_SYMBOL_SUBMODULE_HEADER
:
1351 case DIFF_SYMBOL_SUBMODULE_ERROR
:
1352 case DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
:
1353 case DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
:
1354 case DIFF_SYMBOL_SUMMARY
:
1355 case DIFF_SYMBOL_STATS_LINE
:
1356 case DIFF_SYMBOL_BINARY_DIFF_BODY
:
1357 case DIFF_SYMBOL_CONTEXT_FRAGINFO
:
1358 emit_line(o
, "", "", line
, len
);
1360 case DIFF_SYMBOL_CONTEXT_INCOMPLETE
:
1361 case DIFF_SYMBOL_CONTEXT_MARKER
:
1362 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1363 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1364 emit_line(o
, context
, reset
, line
, len
);
1366 case DIFF_SYMBOL_SEPARATOR
:
1367 fprintf(o
->file
, "%s%c",
1368 diff_line_prefix(o
),
1369 o
->line_termination
);
1371 case DIFF_SYMBOL_CONTEXT
:
1372 set
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1373 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1375 if (o
->flags
.dual_color_diffed_diffs
) {
1376 char c
= !len
? 0 : line
[0];
1379 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1381 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1383 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1385 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1386 OUTPUT_INDICATOR_CONTEXT
, line
, len
,
1387 flags
& (DIFF_SYMBOL_CONTENT_WS_MASK
), 0);
1389 case DIFF_SYMBOL_PLUS
:
1390 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1391 DIFF_SYMBOL_MOVED_LINE_ALT
|
1392 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1393 case DIFF_SYMBOL_MOVED_LINE
|
1394 DIFF_SYMBOL_MOVED_LINE_ALT
|
1395 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1396 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT_DIM
);
1398 case DIFF_SYMBOL_MOVED_LINE
|
1399 DIFF_SYMBOL_MOVED_LINE_ALT
:
1400 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_ALT
);
1402 case DIFF_SYMBOL_MOVED_LINE
|
1403 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1404 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED_DIM
);
1406 case DIFF_SYMBOL_MOVED_LINE
:
1407 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_MOVED
);
1410 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1412 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1413 if (!o
->flags
.dual_color_diffed_diffs
)
1416 char c
= !len
? 0 : line
[0];
1420 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_BOLD
);
1422 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1424 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_BOLD
);
1426 set
= diff_get_color_opt(o
, DIFF_CONTEXT_BOLD
);
1427 flags
&= ~DIFF_SYMBOL_CONTENT_WS_MASK
;
1429 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1430 OUTPUT_INDICATOR_NEW
, line
, len
,
1431 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
,
1432 flags
& DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
);
1434 case DIFF_SYMBOL_MINUS
:
1435 switch (flags
& (DIFF_SYMBOL_MOVED_LINE
|
1436 DIFF_SYMBOL_MOVED_LINE_ALT
|
1437 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
)) {
1438 case DIFF_SYMBOL_MOVED_LINE
|
1439 DIFF_SYMBOL_MOVED_LINE_ALT
|
1440 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1441 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT_DIM
);
1443 case DIFF_SYMBOL_MOVED_LINE
|
1444 DIFF_SYMBOL_MOVED_LINE_ALT
:
1445 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_ALT
);
1447 case DIFF_SYMBOL_MOVED_LINE
|
1448 DIFF_SYMBOL_MOVED_LINE_UNINTERESTING
:
1449 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED_DIM
);
1451 case DIFF_SYMBOL_MOVED_LINE
:
1452 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_MOVED
);
1455 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1457 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1458 if (!o
->flags
.dual_color_diffed_diffs
)
1461 char c
= !len
? 0 : line
[0];
1465 set
= diff_get_color_opt(o
, DIFF_FILE_NEW_DIM
);
1467 set
= diff_get_color_opt(o
, DIFF_FRAGINFO
);
1469 set
= diff_get_color_opt(o
, DIFF_FILE_OLD_DIM
);
1471 set
= diff_get_color_opt(o
, DIFF_CONTEXT_DIM
);
1473 emit_line_ws_markup(o
, set_sign
, set
, reset
,
1474 OUTPUT_INDICATOR_OLD
, line
, len
,
1475 flags
& DIFF_SYMBOL_CONTENT_WS_MASK
, 0);
1477 case DIFF_SYMBOL_WORDS_PORCELAIN
:
1478 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1479 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1480 emit_line(o
, context
, reset
, line
, len
);
1481 fputs("~\n", o
->file
);
1483 case DIFF_SYMBOL_WORDS
:
1484 context
= diff_get_color_opt(o
, DIFF_CONTEXT
);
1485 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1487 * Skip the prefix character, if any. With
1488 * diff_suppress_blank_empty, there may be
1491 if (line
[0] != '\n') {
1495 emit_line(o
, context
, reset
, line
, len
);
1497 case DIFF_SYMBOL_FILEPAIR_PLUS
:
1498 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1499 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1500 fprintf(o
->file
, "%s%s+++ %s%s%s\n", diff_line_prefix(o
), meta
,
1502 strchr(line
, ' ') ? "\t" : "");
1504 case DIFF_SYMBOL_FILEPAIR_MINUS
:
1505 meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
1506 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1507 fprintf(o
->file
, "%s%s--- %s%s%s\n", diff_line_prefix(o
), meta
,
1509 strchr(line
, ' ') ? "\t" : "");
1511 case DIFF_SYMBOL_BINARY_FILES
:
1512 case DIFF_SYMBOL_HEADER
:
1513 fprintf(o
->file
, "%s", line
);
1515 case DIFF_SYMBOL_BINARY_DIFF_HEADER
:
1516 fprintf(o
->file
, "%sGIT binary patch\n", diff_line_prefix(o
));
1518 case DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
:
1519 fprintf(o
->file
, "%sdelta %s\n", diff_line_prefix(o
), line
);
1521 case DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
:
1522 fprintf(o
->file
, "%sliteral %s\n", diff_line_prefix(o
), line
);
1524 case DIFF_SYMBOL_BINARY_DIFF_FOOTER
:
1525 fputs(diff_line_prefix(o
), o
->file
);
1526 fputc('\n', o
->file
);
1528 case DIFF_SYMBOL_REWRITE_DIFF
:
1529 fraginfo
= diff_get_color(o
->use_color
, DIFF_FRAGINFO
);
1530 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1531 emit_line(o
, fraginfo
, reset
, line
, len
);
1533 case DIFF_SYMBOL_SUBMODULE_ADD
:
1534 set
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
1535 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1536 emit_line(o
, set
, reset
, line
, len
);
1538 case DIFF_SYMBOL_SUBMODULE_DEL
:
1539 set
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
1540 reset
= diff_get_color_opt(o
, DIFF_RESET
);
1541 emit_line(o
, set
, reset
, line
, len
);
1543 case DIFF_SYMBOL_SUBMODULE_UNTRACKED
:
1544 fprintf(o
->file
, "%sSubmodule %s contains untracked content\n",
1545 diff_line_prefix(o
), line
);
1547 case DIFF_SYMBOL_SUBMODULE_MODIFIED
:
1548 fprintf(o
->file
, "%sSubmodule %s contains modified content\n",
1549 diff_line_prefix(o
), line
);
1551 case DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
:
1552 emit_line(o
, "", "", " 0 files changed\n",
1553 strlen(" 0 files changed\n"));
1555 case DIFF_SYMBOL_STATS_SUMMARY_ABBREV
:
1556 emit_line(o
, "", "", " ...\n", strlen(" ...\n"));
1558 case DIFF_SYMBOL_WORD_DIFF
:
1559 fprintf(o
->file
, "%.*s", len
, line
);
1561 case DIFF_SYMBOL_STAT_SEP
:
1562 fputs(o
->stat_sep
, o
->file
);
1565 BUG("unknown diff symbol");
1569 static void emit_diff_symbol(struct diff_options
*o
, enum diff_symbol s
,
1570 const char *line
, int len
, unsigned flags
)
1572 struct emitted_diff_symbol e
= {
1573 .line
= line
, .len
= len
, .flags
= flags
, .s
= s
1576 if (o
->emitted_symbols
)
1577 append_emitted_diff_symbol(o
, &e
);
1579 emit_diff_symbol_from_struct(o
, &e
);
1582 void diff_emit_submodule_del(struct diff_options
*o
, const char *line
)
1584 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_DEL
, line
, strlen(line
), 0);
1587 void diff_emit_submodule_add(struct diff_options
*o
, const char *line
)
1589 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ADD
, line
, strlen(line
), 0);
1592 void diff_emit_submodule_untracked(struct diff_options
*o
, const char *path
)
1594 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_UNTRACKED
,
1595 path
, strlen(path
), 0);
1598 void diff_emit_submodule_modified(struct diff_options
*o
, const char *path
)
1600 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_MODIFIED
,
1601 path
, strlen(path
), 0);
1604 void diff_emit_submodule_header(struct diff_options
*o
, const char *header
)
1606 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_HEADER
,
1607 header
, strlen(header
), 0);
1610 void diff_emit_submodule_error(struct diff_options
*o
, const char *err
)
1612 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_ERROR
, err
, strlen(err
), 0);
1615 void diff_emit_submodule_pipethrough(struct diff_options
*o
,
1616 const char *line
, int len
)
1618 emit_diff_symbol(o
, DIFF_SYMBOL_SUBMODULE_PIPETHROUGH
, line
, len
, 0);
1621 static int new_blank_line_at_eof(struct emit_callback
*ecbdata
, const char *line
, int len
)
1623 if (!((ecbdata
->ws_rule
& WS_BLANK_AT_EOF
) &&
1624 ecbdata
->blank_at_eof_in_preimage
&&
1625 ecbdata
->blank_at_eof_in_postimage
&&
1626 ecbdata
->blank_at_eof_in_preimage
<= ecbdata
->lno_in_preimage
&&
1627 ecbdata
->blank_at_eof_in_postimage
<= ecbdata
->lno_in_postimage
))
1629 return ws_blank_line(line
, len
);
1632 static void emit_add_line(struct emit_callback
*ecbdata
,
1633 const char *line
, int len
)
1635 unsigned flags
= WSEH_NEW
| ecbdata
->ws_rule
;
1636 if (new_blank_line_at_eof(ecbdata
, line
, len
))
1637 flags
|= DIFF_SYMBOL_CONTENT_BLANK_LINE_EOF
;
1639 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_PLUS
, line
, len
, flags
);
1642 static void emit_del_line(struct emit_callback
*ecbdata
,
1643 const char *line
, int len
)
1645 unsigned flags
= WSEH_OLD
| ecbdata
->ws_rule
;
1646 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_MINUS
, line
, len
, flags
);
1649 static void emit_context_line(struct emit_callback
*ecbdata
,
1650 const char *line
, int len
)
1652 unsigned flags
= WSEH_CONTEXT
| ecbdata
->ws_rule
;
1653 emit_diff_symbol(ecbdata
->opt
, DIFF_SYMBOL_CONTEXT
, line
, len
, flags
);
1656 static void emit_hunk_header(struct emit_callback
*ecbdata
,
1657 const char *line
, int len
)
1659 const char *context
= diff_get_color(ecbdata
->color_diff
, DIFF_CONTEXT
);
1660 const char *frag
= diff_get_color(ecbdata
->color_diff
, DIFF_FRAGINFO
);
1661 const char *func
= diff_get_color(ecbdata
->color_diff
, DIFF_FUNCINFO
);
1662 const char *reset
= diff_get_color(ecbdata
->color_diff
, DIFF_RESET
);
1663 const char *reverse
= ecbdata
->color_diff
? GIT_COLOR_REVERSE
: "";
1664 static const char atat
[2] = { '@', '@' };
1665 const char *cp
, *ep
;
1666 struct strbuf msgbuf
= STRBUF_INIT
;
1671 * As a hunk header must begin with "@@ -<old>, +<new> @@",
1672 * it always is at least 10 bytes long.
1675 memcmp(line
, atat
, 2) ||
1676 !(ep
= memmem(line
+ 2, len
- 2, atat
, 2))) {
1677 emit_diff_symbol(ecbdata
->opt
,
1678 DIFF_SYMBOL_CONTEXT_MARKER
, line
, len
, 0);
1681 ep
+= 2; /* skip over @@ */
1683 /* The hunk header in fraginfo color */
1684 if (ecbdata
->opt
->flags
.dual_color_diffed_diffs
)
1685 strbuf_addstr(&msgbuf
, reverse
);
1686 strbuf_addstr(&msgbuf
, frag
);
1687 if (ecbdata
->opt
->flags
.suppress_hunk_header_line_count
)
1688 strbuf_add(&msgbuf
, atat
, sizeof(atat
));
1690 strbuf_add(&msgbuf
, line
, ep
- line
);
1691 strbuf_addstr(&msgbuf
, reset
);
1697 if (line
[len
- i
] == '\r' || line
[len
- i
] == '\n')
1700 /* blank before the func header */
1701 for (cp
= ep
; ep
- line
< len
; ep
++)
1702 if (*ep
!= ' ' && *ep
!= '\t')
1705 strbuf_addstr(&msgbuf
, context
);
1706 strbuf_add(&msgbuf
, cp
, ep
- cp
);
1707 strbuf_addstr(&msgbuf
, reset
);
1710 if (ep
< line
+ len
) {
1711 strbuf_addstr(&msgbuf
, func
);
1712 strbuf_add(&msgbuf
, ep
, line
+ len
- ep
);
1713 strbuf_addstr(&msgbuf
, reset
);
1716 strbuf_add(&msgbuf
, line
+ len
, org_len
- len
);
1717 strbuf_complete_line(&msgbuf
);
1718 emit_diff_symbol(ecbdata
->opt
,
1719 DIFF_SYMBOL_CONTEXT_FRAGINFO
, msgbuf
.buf
, msgbuf
.len
, 0);
1720 strbuf_release(&msgbuf
);
1723 static struct diff_tempfile
*claim_diff_tempfile(void)
1726 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++)
1727 if (!diff_temp
[i
].name
)
1728 return diff_temp
+ i
;
1729 BUG("diff is failing to clean up its tempfiles");
1732 static void remove_tempfile(void)
1735 for (i
= 0; i
< ARRAY_SIZE(diff_temp
); i
++) {
1736 if (is_tempfile_active(diff_temp
[i
].tempfile
))
1737 delete_tempfile(&diff_temp
[i
].tempfile
);
1738 diff_temp
[i
].name
= NULL
;
1742 static void add_line_count(struct strbuf
*out
, int count
)
1746 strbuf_addstr(out
, "0,0");
1749 strbuf_addstr(out
, "1");
1752 strbuf_addf(out
, "1,%d", count
);
1757 static void emit_rewrite_lines(struct emit_callback
*ecb
,
1758 int prefix
, const char *data
, int size
)
1760 const char *endp
= NULL
;
1765 endp
= memchr(data
, '\n', size
);
1766 len
= endp
? (endp
- data
+ 1) : size
;
1767 if (prefix
!= '+') {
1768 ecb
->lno_in_preimage
++;
1769 emit_del_line(ecb
, data
, len
);
1771 ecb
->lno_in_postimage
++;
1772 emit_add_line(ecb
, data
, len
);
1778 emit_diff_symbol(ecb
->opt
, DIFF_SYMBOL_NO_LF_EOF
, NULL
, 0, 0);
1781 static void emit_rewrite_diff(const char *name_a
,
1783 struct diff_filespec
*one
,
1784 struct diff_filespec
*two
,
1785 struct userdiff_driver
*textconv_one
,
1786 struct userdiff_driver
*textconv_two
,
1787 struct diff_options
*o
)
1790 static struct strbuf a_name
= STRBUF_INIT
, b_name
= STRBUF_INIT
;
1791 const char *a_prefix
, *b_prefix
;
1792 char *data_one
, *data_two
;
1793 size_t size_one
, size_two
;
1794 struct emit_callback ecbdata
;
1795 struct strbuf out
= STRBUF_INIT
;
1797 if (diff_mnemonic_prefix
&& o
->flags
.reverse_diff
) {
1798 a_prefix
= o
->b_prefix
;
1799 b_prefix
= o
->a_prefix
;
1801 a_prefix
= o
->a_prefix
;
1802 b_prefix
= o
->b_prefix
;
1805 name_a
+= (*name_a
== '/');
1806 name_b
+= (*name_b
== '/');
1808 strbuf_reset(&a_name
);
1809 strbuf_reset(&b_name
);
1810 quote_two_c_style(&a_name
, a_prefix
, name_a
, 0);
1811 quote_two_c_style(&b_name
, b_prefix
, name_b
, 0);
1813 size_one
= fill_textconv(o
->repo
, textconv_one
, one
, &data_one
);
1814 size_two
= fill_textconv(o
->repo
, textconv_two
, two
, &data_two
);
1816 memset(&ecbdata
, 0, sizeof(ecbdata
));
1817 ecbdata
.color_diff
= want_color(o
->use_color
);
1818 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
1820 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
) {
1822 mf1
.ptr
= (char *)data_one
;
1823 mf2
.ptr
= (char *)data_two
;
1824 mf1
.size
= size_one
;
1825 mf2
.size
= size_two
;
1826 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
1828 ecbdata
.lno_in_preimage
= 1;
1829 ecbdata
.lno_in_postimage
= 1;
1831 lc_a
= count_lines(data_one
, size_one
);
1832 lc_b
= count_lines(data_two
, size_two
);
1834 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
1835 a_name
.buf
, a_name
.len
, 0);
1836 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
1837 b_name
.buf
, b_name
.len
, 0);
1839 strbuf_addstr(&out
, "@@ -");
1840 if (!o
->irreversible_delete
)
1841 add_line_count(&out
, lc_a
);
1843 strbuf_addstr(&out
, "?,?");
1844 strbuf_addstr(&out
, " +");
1845 add_line_count(&out
, lc_b
);
1846 strbuf_addstr(&out
, " @@\n");
1847 emit_diff_symbol(o
, DIFF_SYMBOL_REWRITE_DIFF
, out
.buf
, out
.len
, 0);
1848 strbuf_release(&out
);
1850 if (lc_a
&& !o
->irreversible_delete
)
1851 emit_rewrite_lines(&ecbdata
, '-', data_one
, size_one
);
1853 emit_rewrite_lines(&ecbdata
, '+', data_two
, size_two
);
1855 free((char *)data_one
);
1857 free((char *)data_two
);
1860 struct diff_words_buffer
{
1862 unsigned long alloc
;
1863 struct diff_words_orig
{
1864 const char *begin
, *end
;
1866 int orig_nr
, orig_alloc
;
1869 static void diff_words_append(char *line
, unsigned long len
,
1870 struct diff_words_buffer
*buffer
)
1872 ALLOC_GROW(buffer
->text
.ptr
, buffer
->text
.size
+ len
, buffer
->alloc
);
1875 memcpy(buffer
->text
.ptr
+ buffer
->text
.size
, line
, len
);
1876 buffer
->text
.size
+= len
;
1877 buffer
->text
.ptr
[buffer
->text
.size
] = '\0';
1880 struct diff_words_style_elem
{
1883 const char *color
; /* NULL; filled in by the setup code if
1884 * color is enabled */
1887 struct diff_words_style
{
1888 enum diff_words_type type
;
1889 struct diff_words_style_elem new_word
, old_word
, ctx
;
1890 const char *newline
;
1893 static struct diff_words_style diff_words_styles
[] = {
1894 { DIFF_WORDS_PORCELAIN
, {"+", "\n"}, {"-", "\n"}, {" ", "\n"}, "~\n" },
1895 { DIFF_WORDS_PLAIN
, {"{+", "+}"}, {"[-", "-]"}, {"", ""}, "\n" },
1896 { DIFF_WORDS_COLOR
, {"", ""}, {"", ""}, {"", ""}, "\n" }
1899 struct diff_words_data
{
1900 struct diff_words_buffer minus
, plus
;
1901 const char *current_plus
;
1903 struct diff_options
*opt
;
1904 regex_t
*word_regex
;
1905 enum diff_words_type type
;
1906 struct diff_words_style
*style
;
1909 static int fn_out_diff_words_write_helper(struct diff_options
*o
,
1910 struct diff_words_style_elem
*st_el
,
1911 const char *newline
,
1912 size_t count
, const char *buf
)
1915 struct strbuf sb
= STRBUF_INIT
;
1918 char *p
= memchr(buf
, '\n', count
);
1920 strbuf_addstr(&sb
, diff_line_prefix(o
));
1923 const char *reset
= st_el
->color
&& *st_el
->color
?
1924 GIT_COLOR_RESET
: NULL
;
1925 if (st_el
->color
&& *st_el
->color
)
1926 strbuf_addstr(&sb
, st_el
->color
);
1927 strbuf_addstr(&sb
, st_el
->prefix
);
1928 strbuf_add(&sb
, buf
, p
? p
- buf
: count
);
1929 strbuf_addstr(&sb
, st_el
->suffix
);
1931 strbuf_addstr(&sb
, reset
);
1936 strbuf_addstr(&sb
, newline
);
1937 count
-= p
+ 1 - buf
;
1941 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1949 emit_diff_symbol(o
, DIFF_SYMBOL_WORD_DIFF
,
1951 strbuf_release(&sb
);
1956 * '--color-words' algorithm can be described as:
1958 * 1. collect the minus/plus lines of a diff hunk, divided into
1959 * minus-lines and plus-lines;
1961 * 2. break both minus-lines and plus-lines into words and
1962 * place them into two mmfile_t with one word for each line;
1964 * 3. use xdiff to run diff on the two mmfile_t to get the words level diff;
1966 * And for the common parts of the both file, we output the plus side text.
1967 * diff_words->current_plus is used to trace the current position of the plus file
1968 * which printed. diff_words->last_minus is used to trace the last minus word
1971 * For '--graph' to work with '--color-words', we need to output the graph prefix
1972 * on each line of color words output. Generally, there are two conditions on
1973 * which we should output the prefix.
1975 * 1. diff_words->last_minus == 0 &&
1976 * diff_words->current_plus == diff_words->plus.text.ptr
1978 * that is: the plus text must start as a new line, and if there is no minus
1979 * word printed, a graph prefix must be printed.
1981 * 2. diff_words->current_plus > diff_words->plus.text.ptr &&
1982 * *(diff_words->current_plus - 1) == '\n'
1984 * that is: a graph prefix must be printed following a '\n'
1986 static int color_words_output_graph_prefix(struct diff_words_data
*diff_words
)
1988 if ((diff_words
->last_minus
== 0 &&
1989 diff_words
->current_plus
== diff_words
->plus
.text
.ptr
) ||
1990 (diff_words
->current_plus
> diff_words
->plus
.text
.ptr
&&
1991 *(diff_words
->current_plus
- 1) == '\n')) {
1998 static void fn_out_diff_words_aux(void *priv
,
1999 long minus_first
, long minus_len
,
2000 long plus_first
, long plus_len
,
2001 const char *func UNUSED
, long funclen UNUSED
)
2003 struct diff_words_data
*diff_words
= priv
;
2004 struct diff_words_style
*style
= diff_words
->style
;
2005 const char *minus_begin
, *minus_end
, *plus_begin
, *plus_end
;
2006 struct diff_options
*opt
= diff_words
->opt
;
2007 const char *line_prefix
;
2010 line_prefix
= diff_line_prefix(opt
);
2012 /* POSIX requires that first be decremented by one if len == 0... */
2014 minus_begin
= diff_words
->minus
.orig
[minus_first
].begin
;
2016 diff_words
->minus
.orig
[minus_first
+ minus_len
- 1].end
;
2018 minus_begin
= minus_end
=
2019 diff_words
->minus
.orig
[minus_first
].end
;
2022 plus_begin
= diff_words
->plus
.orig
[plus_first
].begin
;
2023 plus_end
= diff_words
->plus
.orig
[plus_first
+ plus_len
- 1].end
;
2025 plus_begin
= plus_end
= diff_words
->plus
.orig
[plus_first
].end
;
2027 if (color_words_output_graph_prefix(diff_words
)) {
2028 fputs(line_prefix
, diff_words
->opt
->file
);
2030 if (diff_words
->current_plus
!= plus_begin
) {
2031 fn_out_diff_words_write_helper(diff_words
->opt
,
2032 &style
->ctx
, style
->newline
,
2033 plus_begin
- diff_words
->current_plus
,
2034 diff_words
->current_plus
);
2036 if (minus_begin
!= minus_end
) {
2037 fn_out_diff_words_write_helper(diff_words
->opt
,
2038 &style
->old_word
, style
->newline
,
2039 minus_end
- minus_begin
, minus_begin
);
2041 if (plus_begin
!= plus_end
) {
2042 fn_out_diff_words_write_helper(diff_words
->opt
,
2043 &style
->new_word
, style
->newline
,
2044 plus_end
- plus_begin
, plus_begin
);
2047 diff_words
->current_plus
= plus_end
;
2048 diff_words
->last_minus
= minus_first
;
2051 /* This function starts looking at *begin, and returns 0 iff a word was found. */
2052 static int find_word_boundaries(mmfile_t
*buffer
, regex_t
*word_regex
,
2053 int *begin
, int *end
)
2055 while (word_regex
&& *begin
< buffer
->size
) {
2056 regmatch_t match
[1];
2057 if (!regexec_buf(word_regex
, buffer
->ptr
+ *begin
,
2058 buffer
->size
- *begin
, 1, match
, 0)) {
2059 char *p
= memchr(buffer
->ptr
+ *begin
+ match
[0].rm_so
,
2060 '\n', match
[0].rm_eo
- match
[0].rm_so
);
2061 *end
= p
? p
- buffer
->ptr
: match
[0].rm_eo
+ *begin
;
2062 *begin
+= match
[0].rm_so
;
2066 return *begin
> *end
;
2072 /* find the next word */
2073 while (*begin
< buffer
->size
&& isspace(buffer
->ptr
[*begin
]))
2075 if (*begin
>= buffer
->size
)
2078 /* find the end of the word */
2080 while (*end
< buffer
->size
&& !isspace(buffer
->ptr
[*end
]))
2087 * This function splits the words in buffer->text, stores the list with
2088 * newline separator into out, and saves the offsets of the original words
2091 static void diff_words_fill(struct diff_words_buffer
*buffer
, mmfile_t
*out
,
2092 regex_t
*word_regex
)
2100 /* fake an empty "0th" word */
2101 ALLOC_GROW(buffer
->orig
, 1, buffer
->orig_alloc
);
2102 buffer
->orig
[0].begin
= buffer
->orig
[0].end
= buffer
->text
.ptr
;
2103 buffer
->orig_nr
= 1;
2105 for (i
= 0; i
< buffer
->text
.size
; i
++) {
2106 if (find_word_boundaries(&buffer
->text
, word_regex
, &i
, &j
))
2109 /* store original boundaries */
2110 ALLOC_GROW(buffer
->orig
, buffer
->orig_nr
+ 1,
2111 buffer
->orig_alloc
);
2112 buffer
->orig
[buffer
->orig_nr
].begin
= buffer
->text
.ptr
+ i
;
2113 buffer
->orig
[buffer
->orig_nr
].end
= buffer
->text
.ptr
+ j
;
2116 /* store one word */
2117 ALLOC_GROW(out
->ptr
, out
->size
+ j
- i
+ 1, alloc
);
2118 memcpy(out
->ptr
+ out
->size
, buffer
->text
.ptr
+ i
, j
- i
);
2119 out
->ptr
[out
->size
+ j
- i
] = '\n';
2120 out
->size
+= j
- i
+ 1;
2126 /* this executes the word diff on the accumulated buffers */
2127 static void diff_words_show(struct diff_words_data
*diff_words
)
2131 mmfile_t minus
, plus
;
2132 struct diff_words_style
*style
= diff_words
->style
;
2134 struct diff_options
*opt
= diff_words
->opt
;
2135 const char *line_prefix
;
2138 line_prefix
= diff_line_prefix(opt
);
2140 /* special case: only removal */
2141 if (!diff_words
->plus
.text
.size
) {
2142 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2143 line_prefix
, strlen(line_prefix
), 0);
2144 fn_out_diff_words_write_helper(diff_words
->opt
,
2145 &style
->old_word
, style
->newline
,
2146 diff_words
->minus
.text
.size
,
2147 diff_words
->minus
.text
.ptr
);
2148 diff_words
->minus
.text
.size
= 0;
2152 diff_words
->current_plus
= diff_words
->plus
.text
.ptr
;
2153 diff_words
->last_minus
= 0;
2155 memset(&xpp
, 0, sizeof(xpp
));
2156 memset(&xecfg
, 0, sizeof(xecfg
));
2157 diff_words_fill(&diff_words
->minus
, &minus
, diff_words
->word_regex
);
2158 diff_words_fill(&diff_words
->plus
, &plus
, diff_words
->word_regex
);
2160 /* as only the hunk header will be parsed, we need a 0-context */
2162 if (xdi_diff_outf(&minus
, &plus
, fn_out_diff_words_aux
, NULL
,
2163 diff_words
, &xpp
, &xecfg
))
2164 die("unable to generate word diff");
2167 if (diff_words
->current_plus
!= diff_words
->plus
.text
.ptr
+
2168 diff_words
->plus
.text
.size
) {
2169 if (color_words_output_graph_prefix(diff_words
))
2170 emit_diff_symbol(diff_words
->opt
, DIFF_SYMBOL_WORD_DIFF
,
2171 line_prefix
, strlen(line_prefix
), 0);
2172 fn_out_diff_words_write_helper(diff_words
->opt
,
2173 &style
->ctx
, style
->newline
,
2174 diff_words
->plus
.text
.ptr
+ diff_words
->plus
.text
.size
2175 - diff_words
->current_plus
, diff_words
->current_plus
);
2177 diff_words
->minus
.text
.size
= diff_words
->plus
.text
.size
= 0;
2180 /* In "color-words" mode, show word-diff of words accumulated in the buffer */
2181 static void diff_words_flush(struct emit_callback
*ecbdata
)
2183 struct diff_options
*wo
= ecbdata
->diff_words
->opt
;
2185 if (ecbdata
->diff_words
->minus
.text
.size
||
2186 ecbdata
->diff_words
->plus
.text
.size
)
2187 diff_words_show(ecbdata
->diff_words
);
2189 if (wo
->emitted_symbols
) {
2190 struct diff_options
*o
= ecbdata
->opt
;
2191 struct emitted_diff_symbols
*wol
= wo
->emitted_symbols
;
2196 * Instead of appending each, concat all words to a line?
2198 for (i
= 0; i
< wol
->nr
; i
++)
2199 append_emitted_diff_symbol(o
, &wol
->buf
[i
]);
2201 for (i
= 0; i
< wol
->nr
; i
++)
2202 free((void *)wol
->buf
[i
].line
);
2208 static void diff_filespec_load_driver(struct diff_filespec
*one
,
2209 struct index_state
*istate
)
2211 /* Use already-loaded driver */
2215 if (S_ISREG(one
->mode
))
2216 one
->driver
= userdiff_find_by_path(istate
, one
->path
);
2218 /* Fallback to default settings */
2220 one
->driver
= userdiff_find_by_name("default");
2223 static const char *userdiff_word_regex(struct diff_filespec
*one
,
2224 struct index_state
*istate
)
2226 diff_filespec_load_driver(one
, istate
);
2227 return one
->driver
->word_regex
;
2230 static void init_diff_words_data(struct emit_callback
*ecbdata
,
2231 struct diff_options
*orig_opts
,
2232 struct diff_filespec
*one
,
2233 struct diff_filespec
*two
)
2236 struct diff_options
*o
= xmalloc(sizeof(struct diff_options
));
2237 memcpy(o
, orig_opts
, sizeof(struct diff_options
));
2239 CALLOC_ARRAY(ecbdata
->diff_words
, 1);
2240 ecbdata
->diff_words
->type
= o
->word_diff
;
2241 ecbdata
->diff_words
->opt
= o
;
2243 if (orig_opts
->emitted_symbols
)
2244 CALLOC_ARRAY(o
->emitted_symbols
, 1);
2247 o
->word_regex
= userdiff_word_regex(one
, o
->repo
->index
);
2249 o
->word_regex
= userdiff_word_regex(two
, o
->repo
->index
);
2251 o
->word_regex
= diff_word_regex_cfg
;
2252 if (o
->word_regex
) {
2253 ecbdata
->diff_words
->word_regex
= (regex_t
*)
2254 xmalloc(sizeof(regex_t
));
2255 if (regcomp(ecbdata
->diff_words
->word_regex
,
2257 REG_EXTENDED
| REG_NEWLINE
))
2258 die("invalid regular expression: %s",
2261 for (i
= 0; i
< ARRAY_SIZE(diff_words_styles
); i
++) {
2262 if (o
->word_diff
== diff_words_styles
[i
].type
) {
2263 ecbdata
->diff_words
->style
=
2264 &diff_words_styles
[i
];
2268 if (want_color(o
->use_color
)) {
2269 struct diff_words_style
*st
= ecbdata
->diff_words
->style
;
2270 st
->old_word
.color
= diff_get_color_opt(o
, DIFF_FILE_OLD
);
2271 st
->new_word
.color
= diff_get_color_opt(o
, DIFF_FILE_NEW
);
2272 st
->ctx
.color
= diff_get_color_opt(o
, DIFF_CONTEXT
);
2276 static void free_diff_words_data(struct emit_callback
*ecbdata
)
2278 if (ecbdata
->diff_words
) {
2279 diff_words_flush(ecbdata
);
2280 free_emitted_diff_symbols(ecbdata
->diff_words
->opt
->emitted_symbols
);
2281 free (ecbdata
->diff_words
->opt
);
2282 free (ecbdata
->diff_words
->minus
.text
.ptr
);
2283 free (ecbdata
->diff_words
->minus
.orig
);
2284 free (ecbdata
->diff_words
->plus
.text
.ptr
);
2285 free (ecbdata
->diff_words
->plus
.orig
);
2286 if (ecbdata
->diff_words
->word_regex
) {
2287 regfree(ecbdata
->diff_words
->word_regex
);
2288 free(ecbdata
->diff_words
->word_regex
);
2290 FREE_AND_NULL(ecbdata
->diff_words
);
2294 const char *diff_get_color(int diff_use_color
, enum color_diff ix
)
2296 if (want_color(diff_use_color
))
2297 return diff_colors
[ix
];
2301 const char *diff_line_prefix(struct diff_options
*opt
)
2303 struct strbuf
*msgbuf
;
2304 if (!opt
->output_prefix
)
2307 msgbuf
= opt
->output_prefix(opt
, opt
->output_prefix_data
);
2311 static unsigned long sane_truncate_line(char *line
, unsigned long len
)
2314 unsigned long allot
;
2320 (void) utf8_width(&cp
, &l
);
2322 break; /* truncated in the middle? */
2327 static void find_lno(const char *line
, struct emit_callback
*ecbdata
)
2330 ecbdata
->lno_in_preimage
= 0;
2331 ecbdata
->lno_in_postimage
= 0;
2332 p
= strchr(line
, '-');
2334 return; /* cannot happen */
2335 ecbdata
->lno_in_preimage
= strtol(p
+ 1, NULL
, 10);
2338 return; /* cannot happen */
2339 ecbdata
->lno_in_postimage
= strtol(p
+ 1, NULL
, 10);
2342 static int fn_out_consume(void *priv
, char *line
, unsigned long len
)
2344 struct emit_callback
*ecbdata
= priv
;
2345 struct diff_options
*o
= ecbdata
->opt
;
2347 o
->found_changes
= 1;
2349 if (ecbdata
->header
) {
2350 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
2351 ecbdata
->header
->buf
, ecbdata
->header
->len
, 0);
2352 strbuf_reset(ecbdata
->header
);
2353 ecbdata
->header
= NULL
;
2356 if (ecbdata
->label_path
[0]) {
2357 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_MINUS
,
2358 ecbdata
->label_path
[0],
2359 strlen(ecbdata
->label_path
[0]), 0);
2360 emit_diff_symbol(o
, DIFF_SYMBOL_FILEPAIR_PLUS
,
2361 ecbdata
->label_path
[1],
2362 strlen(ecbdata
->label_path
[1]), 0);
2363 ecbdata
->label_path
[0] = ecbdata
->label_path
[1] = NULL
;
2366 if (diff_suppress_blank_empty
2367 && len
== 2 && line
[0] == ' ' && line
[1] == '\n') {
2372 if (line
[0] == '@') {
2373 if (ecbdata
->diff_words
)
2374 diff_words_flush(ecbdata
);
2375 len
= sane_truncate_line(line
, len
);
2376 find_lno(line
, ecbdata
);
2377 emit_hunk_header(ecbdata
, line
, len
);
2381 if (ecbdata
->diff_words
) {
2382 enum diff_symbol s
=
2383 ecbdata
->diff_words
->type
== DIFF_WORDS_PORCELAIN
?
2384 DIFF_SYMBOL_WORDS_PORCELAIN
: DIFF_SYMBOL_WORDS
;
2385 if (line
[0] == '-') {
2386 diff_words_append(line
, len
,
2387 &ecbdata
->diff_words
->minus
);
2389 } else if (line
[0] == '+') {
2390 diff_words_append(line
, len
,
2391 &ecbdata
->diff_words
->plus
);
2393 } else if (starts_with(line
, "\\ ")) {
2395 * Eat the "no newline at eof" marker as if we
2396 * saw a "+" or "-" line with nothing on it,
2397 * and return without diff_words_flush() to
2398 * defer processing. If this is the end of
2399 * preimage, more "+" lines may come after it.
2403 diff_words_flush(ecbdata
);
2404 emit_diff_symbol(o
, s
, line
, len
, 0);
2410 ecbdata
->lno_in_postimage
++;
2411 emit_add_line(ecbdata
, line
+ 1, len
- 1);
2414 ecbdata
->lno_in_preimage
++;
2415 emit_del_line(ecbdata
, line
+ 1, len
- 1);
2418 ecbdata
->lno_in_postimage
++;
2419 ecbdata
->lno_in_preimage
++;
2420 emit_context_line(ecbdata
, line
+ 1, len
- 1);
2423 /* incomplete line at the end */
2424 ecbdata
->lno_in_preimage
++;
2425 emit_diff_symbol(o
, DIFF_SYMBOL_CONTEXT_INCOMPLETE
,
2432 static void pprint_rename(struct strbuf
*name
, const char *a
, const char *b
)
2434 const char *old_name
= a
;
2435 const char *new_name
= b
;
2436 int pfx_length
, sfx_length
;
2437 int pfx_adjust_for_slash
;
2438 int len_a
= strlen(a
);
2439 int len_b
= strlen(b
);
2440 int a_midlen
, b_midlen
;
2441 int qlen_a
= quote_c_style(a
, NULL
, NULL
, 0);
2442 int qlen_b
= quote_c_style(b
, NULL
, NULL
, 0);
2444 if (qlen_a
|| qlen_b
) {
2445 quote_c_style(a
, name
, NULL
, 0);
2446 strbuf_addstr(name
, " => ");
2447 quote_c_style(b
, name
, NULL
, 0);
2451 /* Find common prefix */
2453 while (*old_name
&& *new_name
&& *old_name
== *new_name
) {
2454 if (*old_name
== '/')
2455 pfx_length
= old_name
- a
+ 1;
2460 /* Find common suffix */
2461 old_name
= a
+ len_a
;
2462 new_name
= b
+ len_b
;
2465 * If there is a common prefix, it must end in a slash. In
2466 * that case we let this loop run 1 into the prefix to see the
2469 * If there is no common prefix, we cannot do this as it would
2470 * underrun the input strings.
2472 pfx_adjust_for_slash
= (pfx_length
? 1 : 0);
2473 while (a
+ pfx_length
- pfx_adjust_for_slash
<= old_name
&&
2474 b
+ pfx_length
- pfx_adjust_for_slash
<= new_name
&&
2475 *old_name
== *new_name
) {
2476 if (*old_name
== '/')
2477 sfx_length
= len_a
- (old_name
- a
);
2483 * pfx{mid-a => mid-b}sfx
2484 * {pfx-a => pfx-b}sfx
2485 * pfx{sfx-a => sfx-b}
2488 a_midlen
= len_a
- pfx_length
- sfx_length
;
2489 b_midlen
= len_b
- pfx_length
- sfx_length
;
2495 strbuf_grow(name
, pfx_length
+ a_midlen
+ b_midlen
+ sfx_length
+ 7);
2496 if (pfx_length
+ sfx_length
) {
2497 strbuf_add(name
, a
, pfx_length
);
2498 strbuf_addch(name
, '{');
2500 strbuf_add(name
, a
+ pfx_length
, a_midlen
);
2501 strbuf_addstr(name
, " => ");
2502 strbuf_add(name
, b
+ pfx_length
, b_midlen
);
2503 if (pfx_length
+ sfx_length
) {
2504 strbuf_addch(name
, '}');
2505 strbuf_add(name
, a
+ len_a
- sfx_length
, sfx_length
);
2509 static struct diffstat_file
*diffstat_add(struct diffstat_t
*diffstat
,
2513 struct diffstat_file
*x
;
2515 ALLOC_GROW(diffstat
->files
, diffstat
->nr
+ 1, diffstat
->alloc
);
2516 diffstat
->files
[diffstat
->nr
++] = x
;
2518 x
->from_name
= xstrdup(name_a
);
2519 x
->name
= xstrdup(name_b
);
2523 x
->from_name
= NULL
;
2524 x
->name
= xstrdup(name_a
);
2529 static int diffstat_consume(void *priv
, char *line
, unsigned long len
)
2531 struct diffstat_t
*diffstat
= priv
;
2532 struct diffstat_file
*x
= diffstat
->files
[diffstat
->nr
- 1];
2535 BUG("xdiff fed us an empty line");
2539 else if (line
[0] == '-')
2544 const char mime_boundary_leader
[] = "------------";
2546 static int scale_linear(int it
, int width
, int max_change
)
2551 * make sure that at least one '-' or '+' is printed if
2552 * there is any change to this path. The easiest way is to
2553 * scale linearly as if the allotted width is one column shorter
2554 * than it is, and then add 1 to the result.
2556 return 1 + (it
* (width
- 1) / max_change
);
2559 static void show_graph(struct strbuf
*out
, char ch
, int cnt
,
2560 const char *set
, const char *reset
)
2564 strbuf_addstr(out
, set
);
2565 strbuf_addchars(out
, ch
, cnt
);
2566 strbuf_addstr(out
, reset
);
2569 static void fill_print_name(struct diffstat_file
*file
)
2571 struct strbuf pname
= STRBUF_INIT
;
2573 if (file
->print_name
)
2576 if (file
->is_renamed
)
2577 pprint_rename(&pname
, file
->from_name
, file
->name
);
2579 quote_c_style(file
->name
, &pname
, NULL
, 0);
2582 strbuf_addf(&pname
, " (%s)", file
->comments
);
2584 file
->print_name
= strbuf_detach(&pname
, NULL
);
2587 static void print_stat_summary_inserts_deletes(struct diff_options
*options
,
2588 int files
, int insertions
, int deletions
)
2590 struct strbuf sb
= STRBUF_INIT
;
2593 assert(insertions
== 0 && deletions
== 0);
2594 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_NO_FILES
,
2600 (files
== 1) ? " %d file changed" : " %d files changed",
2604 * For binary diff, the caller may want to print "x files
2605 * changed" with insertions == 0 && deletions == 0.
2607 * Not omitting "0 insertions(+), 0 deletions(-)" in this case
2608 * is probably less confusing (i.e skip over "2 files changed
2609 * but nothing about added/removed lines? Is this a bug in Git?").
2611 if (insertions
|| deletions
== 0) {
2613 (insertions
== 1) ? ", %d insertion(+)" : ", %d insertions(+)",
2617 if (deletions
|| insertions
== 0) {
2619 (deletions
== 1) ? ", %d deletion(-)" : ", %d deletions(-)",
2622 strbuf_addch(&sb
, '\n');
2623 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_SUMMARY_INSERTS_DELETES
,
2625 strbuf_release(&sb
);
2628 void print_stat_summary(FILE *fp
, int files
,
2629 int insertions
, int deletions
)
2631 struct diff_options o
;
2632 memset(&o
, 0, sizeof(o
));
2635 print_stat_summary_inserts_deletes(&o
, files
, insertions
, deletions
);
2638 static void show_stats(struct diffstat_t
*data
, struct diff_options
*options
)
2640 int i
, len
, add
, del
, adds
= 0, dels
= 0;
2641 uintmax_t max_change
= 0, max_len
= 0;
2642 int total_files
= data
->nr
, count
;
2643 int width
, name_width
, graph_width
, number_width
= 0, bin_width
= 0;
2644 const char *reset
, *add_c
, *del_c
;
2645 int extra_shown
= 0;
2646 const char *line_prefix
= diff_line_prefix(options
);
2647 struct strbuf out
= STRBUF_INIT
;
2652 count
= options
->stat_count
? options
->stat_count
: data
->nr
;
2654 reset
= diff_get_color_opt(options
, DIFF_RESET
);
2655 add_c
= diff_get_color_opt(options
, DIFF_FILE_NEW
);
2656 del_c
= diff_get_color_opt(options
, DIFF_FILE_OLD
);
2659 * Find the longest filename and max number of changes
2661 for (i
= 0; (i
< count
) && (i
< data
->nr
); i
++) {
2662 struct diffstat_file
*file
= data
->files
[i
];
2663 uintmax_t change
= file
->added
+ file
->deleted
;
2665 if (!file
->is_interesting
&& (change
== 0)) {
2666 count
++; /* not shown == room for one more */
2669 fill_print_name(file
);
2670 len
= utf8_strwidth(file
->print_name
);
2674 if (file
->is_unmerged
) {
2675 /* "Unmerged" is 8 characters */
2676 bin_width
= bin_width
< 8 ? 8 : bin_width
;
2679 if (file
->is_binary
) {
2680 /* "Bin XXX -> YYY bytes" */
2681 int w
= 14 + decimal_width(file
->added
)
2682 + decimal_width(file
->deleted
);
2683 bin_width
= bin_width
< w
? w
: bin_width
;
2684 /* Display change counts aligned with "Bin" */
2689 if (max_change
< change
)
2690 max_change
= change
;
2692 count
= i
; /* where we can stop scanning in data->files[] */
2695 * We have width = stat_width or term_columns() columns total.
2696 * We want a maximum of min(max_len, stat_name_width) for the name part.
2697 * We want a maximum of min(max_change, stat_graph_width) for the +- part.
2698 * We also need 1 for " " and 4 + decimal_width(max_change)
2699 * for " | NNNN " and one the empty column at the end, altogether
2700 * 6 + decimal_width(max_change).
2702 * If there's not enough space, we will use the smaller of
2703 * stat_name_width (if set) and 5/8*width for the filename,
2704 * and the rest for constant elements + graph part, but no more
2705 * than stat_graph_width for the graph part.
2706 * (5/8 gives 50 for filename and 30 for the constant parts + graph
2707 * for the standard terminal size).
2709 * In other words: stat_width limits the maximum width, and
2710 * stat_name_width fixes the maximum width of the filename,
2711 * and is also used to divide available columns if there
2714 * Binary files are displayed with "Bin XXX -> YYY bytes"
2715 * instead of the change count and graph. This part is treated
2716 * similarly to the graph part, except that it is not
2717 * "scaled". If total width is too small to accommodate the
2718 * guaranteed minimum width of the filename part and the
2719 * separators and this message, this message will "overflow"
2720 * making the line longer than the maximum width.
2724 * NEEDSWORK: line_prefix is often used for "log --graph" output
2725 * and contains ANSI-colored string. utf8_strnwidth() should be
2726 * used to correctly count the display width instead of strlen().
2728 if (options
->stat_width
== -1)
2729 width
= term_columns() - strlen(line_prefix
);
2731 width
= options
->stat_width
? options
->stat_width
: 80;
2732 number_width
= decimal_width(max_change
) > number_width
?
2733 decimal_width(max_change
) : number_width
;
2735 if (options
->stat_name_width
== -1)
2736 options
->stat_name_width
= diff_stat_name_width
;
2737 if (options
->stat_graph_width
== -1)
2738 options
->stat_graph_width
= diff_stat_graph_width
;
2741 * Guarantee 3/8*16 == 6 for the graph part
2742 * and 5/8*16 == 10 for the filename part
2744 if (width
< 16 + 6 + number_width
)
2745 width
= 16 + 6 + number_width
;
2748 * First assign sizes that are wanted, ignoring available width.
2749 * strlen("Bin XXX -> YYY bytes") == bin_width, and the part
2750 * starting from "XXX" should fit in graph_width.
2752 graph_width
= max_change
+ 4 > bin_width
? max_change
: bin_width
- 4;
2753 if (options
->stat_graph_width
&&
2754 options
->stat_graph_width
< graph_width
)
2755 graph_width
= options
->stat_graph_width
;
2757 name_width
= (options
->stat_name_width
> 0 &&
2758 options
->stat_name_width
< max_len
) ?
2759 options
->stat_name_width
: max_len
;
2762 * Adjust adjustable widths not to exceed maximum width
2764 if (name_width
+ number_width
+ 6 + graph_width
> width
) {
2765 if (graph_width
> width
* 3/8 - number_width
- 6) {
2766 graph_width
= width
* 3/8 - number_width
- 6;
2767 if (graph_width
< 6)
2771 if (options
->stat_graph_width
&&
2772 graph_width
> options
->stat_graph_width
)
2773 graph_width
= options
->stat_graph_width
;
2774 if (name_width
> width
- number_width
- 6 - graph_width
)
2775 name_width
= width
- number_width
- 6 - graph_width
;
2777 graph_width
= width
- number_width
- 6 - name_width
;
2781 * From here name_width is the width of the name area,
2782 * and graph_width is the width of the graph area.
2783 * max_change is used to scale graph properly.
2785 for (i
= 0; i
< count
; i
++) {
2786 const char *prefix
= "";
2787 struct diffstat_file
*file
= data
->files
[i
];
2788 char *name
= file
->print_name
;
2789 uintmax_t added
= file
->added
;
2790 uintmax_t deleted
= file
->deleted
;
2791 int name_len
, padding
;
2793 if (!file
->is_interesting
&& (added
+ deleted
== 0))
2797 * "scale" the filename
2800 name_len
= utf8_strwidth(name
);
2801 if (name_width
< name_len
) {
2806 * NEEDSWORK: (name_len - len) counts the display
2807 * width, which would be shorter than the byte
2808 * length of the corresponding substring.
2809 * Advancing "name" by that number of bytes does
2810 * *NOT* skip over that many columns, so it is
2811 * very likely that chomping the pathname at the
2812 * slash we will find starting from "name" will
2813 * leave the resulting string still too long.
2815 name
+= name_len
- len
;
2816 slash
= strchr(name
, '/');
2820 padding
= len
- utf8_strwidth(name
);
2824 if (file
->is_binary
) {
2825 strbuf_addf(&out
, " %s%s%*s | %*s",
2826 prefix
, name
, padding
, "",
2827 number_width
, "Bin");
2828 if (!added
&& !deleted
) {
2829 strbuf_addch(&out
, '\n');
2830 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2831 out
.buf
, out
.len
, 0);
2835 strbuf_addf(&out
, " %s%"PRIuMAX
"%s",
2836 del_c
, deleted
, reset
);
2837 strbuf_addstr(&out
, " -> ");
2838 strbuf_addf(&out
, "%s%"PRIuMAX
"%s",
2839 add_c
, added
, reset
);
2840 strbuf_addstr(&out
, " bytes\n");
2841 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2842 out
.buf
, out
.len
, 0);
2846 else if (file
->is_unmerged
) {
2847 strbuf_addf(&out
, " %s%s%*s | %*s",
2848 prefix
, name
, padding
, "",
2849 number_width
, "Unmerged\n");
2850 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2851 out
.buf
, out
.len
, 0);
2857 * scale the add/delete
2862 if (graph_width
<= max_change
) {
2863 int total
= scale_linear(add
+ del
, graph_width
, max_change
);
2864 if (total
< 2 && add
&& del
)
2865 /* width >= 2 due to the sanity check */
2868 add
= scale_linear(add
, graph_width
, max_change
);
2871 del
= scale_linear(del
, graph_width
, max_change
);
2875 strbuf_addf(&out
, " %s%s%*s | %*"PRIuMAX
"%s",
2876 prefix
, name
, padding
, "",
2877 number_width
, added
+ deleted
,
2878 added
+ deleted
? " " : "");
2879 show_graph(&out
, '+', add
, add_c
, reset
);
2880 show_graph(&out
, '-', del
, del_c
, reset
);
2881 strbuf_addch(&out
, '\n');
2882 emit_diff_symbol(options
, DIFF_SYMBOL_STATS_LINE
,
2883 out
.buf
, out
.len
, 0);
2887 for (i
= 0; i
< data
->nr
; i
++) {
2888 struct diffstat_file
*file
= data
->files
[i
];
2889 uintmax_t added
= file
->added
;
2890 uintmax_t deleted
= file
->deleted
;
2892 if (file
->is_unmerged
||
2893 (!file
->is_interesting
&& (added
+ deleted
== 0))) {
2898 if (!file
->is_binary
) {
2905 emit_diff_symbol(options
,
2906 DIFF_SYMBOL_STATS_SUMMARY_ABBREV
,
2911 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2912 strbuf_release(&out
);
2915 static void show_shortstats(struct diffstat_t
*data
, struct diff_options
*options
)
2917 int i
, adds
= 0, dels
= 0, total_files
= data
->nr
;
2922 for (i
= 0; i
< data
->nr
; i
++) {
2923 int added
= data
->files
[i
]->added
;
2924 int deleted
= data
->files
[i
]->deleted
;
2926 if (data
->files
[i
]->is_unmerged
||
2927 (!data
->files
[i
]->is_interesting
&& (added
+ deleted
== 0))) {
2929 } else if (!data
->files
[i
]->is_binary
) { /* don't count bytes */
2934 print_stat_summary_inserts_deletes(options
, total_files
, adds
, dels
);
2937 static void show_numstat(struct diffstat_t
*data
, struct diff_options
*options
)
2944 for (i
= 0; i
< data
->nr
; i
++) {
2945 struct diffstat_file
*file
= data
->files
[i
];
2947 fprintf(options
->file
, "%s", diff_line_prefix(options
));
2949 if (file
->is_binary
)
2950 fprintf(options
->file
, "-\t-\t");
2952 fprintf(options
->file
,
2953 "%"PRIuMAX
"\t%"PRIuMAX
"\t",
2954 file
->added
, file
->deleted
);
2955 if (options
->line_termination
) {
2956 fill_print_name(file
);
2957 if (!file
->is_renamed
)
2958 write_name_quoted(file
->name
, options
->file
,
2959 options
->line_termination
);
2961 fputs(file
->print_name
, options
->file
);
2962 putc(options
->line_termination
, options
->file
);
2965 if (file
->is_renamed
) {
2966 putc('\0', options
->file
);
2967 write_name_quoted(file
->from_name
, options
->file
, '\0');
2969 write_name_quoted(file
->name
, options
->file
, '\0');
2974 struct dirstat_file
{
2976 unsigned long changed
;
2979 struct dirstat_dir
{
2980 struct dirstat_file
*files
;
2981 int alloc
, nr
, permille
, cumulative
;
2984 static long gather_dirstat(struct diff_options
*opt
, struct dirstat_dir
*dir
,
2985 unsigned long changed
, const char *base
, int baselen
)
2987 unsigned long sum_changes
= 0;
2988 unsigned int sources
= 0;
2989 const char *line_prefix
= diff_line_prefix(opt
);
2992 struct dirstat_file
*f
= dir
->files
;
2993 int namelen
= strlen(f
->name
);
2994 unsigned long changes
;
2997 if (namelen
< baselen
)
2999 if (memcmp(f
->name
, base
, baselen
))
3001 slash
= strchr(f
->name
+ baselen
, '/');
3003 int newbaselen
= slash
+ 1 - f
->name
;
3004 changes
= gather_dirstat(opt
, dir
, changed
, f
->name
, newbaselen
);
3007 changes
= f
->changed
;
3012 sum_changes
+= changes
;
3016 * We don't report dirstat's for
3018 * - or cases where everything came from a single directory
3019 * under this directory (sources == 1).
3021 if (baselen
&& sources
!= 1) {
3023 int permille
= sum_changes
* 1000 / changed
;
3024 if (permille
>= dir
->permille
) {
3025 fprintf(opt
->file
, "%s%4d.%01d%% %.*s\n", line_prefix
,
3026 permille
/ 10, permille
% 10, baselen
, base
);
3027 if (!dir
->cumulative
)
3035 static int dirstat_compare(const void *_a
, const void *_b
)
3037 const struct dirstat_file
*a
= _a
;
3038 const struct dirstat_file
*b
= _b
;
3039 return strcmp(a
->name
, b
->name
);
3042 static void conclude_dirstat(struct diff_options
*options
,
3043 struct dirstat_dir
*dir
,
3044 unsigned long changed
)
3046 struct dirstat_file
*to_free
= dir
->files
;
3049 /* This can happen even with many files, if everything was renames */
3052 /* Show all directories with more than x% of the changes */
3053 QSORT(dir
->files
, dir
->nr
, dirstat_compare
);
3054 gather_dirstat(options
, dir
, changed
, "", 0);
3060 static void show_dirstat(struct diff_options
*options
)
3063 unsigned long changed
;
3064 struct dirstat_dir dir
;
3065 struct diff_queue_struct
*q
= &diff_queued_diff
;
3070 dir
.permille
= options
->dirstat_permille
;
3071 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3074 for (i
= 0; i
< q
->nr
; i
++) {
3075 struct diff_filepair
*p
= q
->queue
[i
];
3077 unsigned long copied
, added
, damage
;
3078 struct diff_populate_filespec_options dpf_options
= {
3079 .check_size_only
= 1,
3082 name
= p
->two
->path
? p
->two
->path
: p
->one
->path
;
3084 if (p
->one
->oid_valid
&& p
->two
->oid_valid
&&
3085 oideq(&p
->one
->oid
, &p
->two
->oid
)) {
3087 * The SHA1 has not changed, so pre-/post-content is
3088 * identical. We can therefore skip looking at the
3089 * file contents altogether.
3095 if (options
->flags
.dirstat_by_file
) {
3097 * In --dirstat-by-file mode, we don't really need to
3098 * look at the actual file contents at all.
3099 * The fact that the SHA1 changed is enough for us to
3100 * add this file to the list of results
3101 * (with each file contributing equal damage).
3107 if (DIFF_FILE_VALID(p
->one
) && DIFF_FILE_VALID(p
->two
)) {
3108 diff_populate_filespec(options
->repo
, p
->one
, NULL
);
3109 diff_populate_filespec(options
->repo
, p
->two
, NULL
);
3110 diffcore_count_changes(options
->repo
,
3111 p
->one
, p
->two
, NULL
, NULL
,
3113 diff_free_filespec_data(p
->one
);
3114 diff_free_filespec_data(p
->two
);
3115 } else if (DIFF_FILE_VALID(p
->one
)) {
3116 diff_populate_filespec(options
->repo
, p
->one
, &dpf_options
);
3118 diff_free_filespec_data(p
->one
);
3119 } else if (DIFF_FILE_VALID(p
->two
)) {
3120 diff_populate_filespec(options
->repo
, p
->two
, &dpf_options
);
3122 added
= p
->two
->size
;
3123 diff_free_filespec_data(p
->two
);
3128 * Original minus copied is the removed material,
3129 * added is the new material. They are both damages
3130 * made to the preimage.
3131 * If the resulting damage is zero, we know that
3132 * diffcore_count_changes() considers the two entries to
3133 * be identical, but since the oid changed, we
3134 * know that there must have been _some_ kind of change,
3135 * so we force all entries to have damage > 0.
3137 damage
= (p
->one
->size
- copied
) + added
;
3142 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3143 dir
.files
[dir
.nr
].name
= name
;
3144 dir
.files
[dir
.nr
].changed
= damage
;
3149 conclude_dirstat(options
, &dir
, changed
);
3152 static void show_dirstat_by_line(struct diffstat_t
*data
, struct diff_options
*options
)
3155 unsigned long changed
;
3156 struct dirstat_dir dir
;
3164 dir
.permille
= options
->dirstat_permille
;
3165 dir
.cumulative
= options
->flags
.dirstat_cumulative
;
3168 for (i
= 0; i
< data
->nr
; i
++) {
3169 struct diffstat_file
*file
= data
->files
[i
];
3170 unsigned long damage
= file
->added
+ file
->deleted
;
3171 if (file
->is_binary
)
3173 * binary files counts bytes, not lines. Must find some
3174 * way to normalize binary bytes vs. textual lines.
3175 * The following heuristic assumes that there are 64
3177 * This is stupid and ugly, but very cheap...
3179 damage
= DIV_ROUND_UP(damage
, 64);
3180 ALLOC_GROW(dir
.files
, dir
.nr
+ 1, dir
.alloc
);
3181 dir
.files
[dir
.nr
].name
= file
->name
;
3182 dir
.files
[dir
.nr
].changed
= damage
;
3187 conclude_dirstat(options
, &dir
, changed
);
3190 static void free_diffstat_file(struct diffstat_file
*f
)
3192 free(f
->print_name
);
3198 void free_diffstat_info(struct diffstat_t
*diffstat
)
3201 for (i
= 0; i
< diffstat
->nr
; i
++)
3202 free_diffstat_file(diffstat
->files
[i
]);
3203 free(diffstat
->files
);
3206 struct checkdiff_t
{
3207 const char *filename
;
3209 int conflict_marker_size
;
3210 struct diff_options
*o
;
3215 static int is_conflict_marker(const char *line
, int marker_size
, unsigned long len
)
3220 if (len
< marker_size
+ 1)
3222 firstchar
= line
[0];
3223 switch (firstchar
) {
3224 case '=': case '>': case '<': case '|':
3229 for (cnt
= 1; cnt
< marker_size
; cnt
++)
3230 if (line
[cnt
] != firstchar
)
3232 /* line[1] through line[marker_size-1] are same as firstchar */
3233 if (len
< marker_size
+ 1 || !isspace(line
[marker_size
]))
3238 static void checkdiff_consume_hunk(void *priv
,
3239 long ob UNUSED
, long on UNUSED
,
3240 long nb
, long nn UNUSED
,
3241 const char *func UNUSED
, long funclen UNUSED
)
3244 struct checkdiff_t
*data
= priv
;
3245 data
->lineno
= nb
- 1;
3248 static int checkdiff_consume(void *priv
, char *line
, unsigned long len
)
3250 struct checkdiff_t
*data
= priv
;
3251 int marker_size
= data
->conflict_marker_size
;
3252 const char *ws
= diff_get_color(data
->o
->use_color
, DIFF_WHITESPACE
);
3253 const char *reset
= diff_get_color(data
->o
->use_color
, DIFF_RESET
);
3254 const char *set
= diff_get_color(data
->o
->use_color
, DIFF_FILE_NEW
);
3256 const char *line_prefix
;
3259 line_prefix
= diff_line_prefix(data
->o
);
3261 if (line
[0] == '+') {
3264 if (is_conflict_marker(line
+ 1, marker_size
, len
- 1)) {
3266 fprintf(data
->o
->file
,
3267 "%s%s:%d: leftover conflict marker\n",
3268 line_prefix
, data
->filename
, data
->lineno
);
3270 bad
= ws_check(line
+ 1, len
- 1, data
->ws_rule
);
3273 data
->status
|= bad
;
3274 err
= whitespace_error_string(bad
);
3275 fprintf(data
->o
->file
, "%s%s:%d: %s.\n",
3276 line_prefix
, data
->filename
, data
->lineno
, err
);
3278 emit_line(data
->o
, set
, reset
, line
, 1);
3279 ws_check_emit(line
+ 1, len
- 1, data
->ws_rule
,
3280 data
->o
->file
, set
, reset
, ws
);
3281 } else if (line
[0] == ' ') {
3287 static unsigned char *deflate_it(char *data
,
3289 unsigned long *result_size
)
3292 unsigned char *deflated
;
3295 git_deflate_init(&stream
, zlib_compression_level
);
3296 bound
= git_deflate_bound(&stream
, size
);
3297 deflated
= xmalloc(bound
);
3298 stream
.next_out
= deflated
;
3299 stream
.avail_out
= bound
;
3301 stream
.next_in
= (unsigned char *)data
;
3302 stream
.avail_in
= size
;
3303 while (git_deflate(&stream
, Z_FINISH
) == Z_OK
)
3305 git_deflate_end(&stream
);
3306 *result_size
= stream
.total_out
;
3310 static void emit_binary_diff_body(struct diff_options
*o
,
3311 mmfile_t
*one
, mmfile_t
*two
)
3317 unsigned long orig_size
;
3318 unsigned long delta_size
;
3319 unsigned long deflate_size
;
3320 unsigned long data_size
;
3322 /* We could do deflated delta, or we could do just deflated two,
3323 * whichever is smaller.
3326 deflated
= deflate_it(two
->ptr
, two
->size
, &deflate_size
);
3327 if (one
->size
&& two
->size
) {
3328 delta
= diff_delta(one
->ptr
, one
->size
,
3329 two
->ptr
, two
->size
,
3330 &delta_size
, deflate_size
);
3332 void *to_free
= delta
;
3333 orig_size
= delta_size
;
3334 delta
= deflate_it(delta
, delta_size
, &delta_size
);
3339 if (delta
&& delta_size
< deflate_size
) {
3340 char *s
= xstrfmt("%"PRIuMAX
, (uintmax_t)orig_size
);
3341 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_DELTA
,
3346 data_size
= delta_size
;
3348 char *s
= xstrfmt("%lu", two
->size
);
3349 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER_LITERAL
,
3354 data_size
= deflate_size
;
3357 /* emit data encoded in base85 */
3361 int bytes
= (52 < data_size
) ? 52 : data_size
;
3365 line
[0] = bytes
+ 'A' - 1;
3367 line
[0] = bytes
- 26 + 'a' - 1;
3368 encode_85(line
+ 1, cp
, bytes
);
3369 cp
= (char *) cp
+ bytes
;
3375 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_BODY
,
3378 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_FOOTER
, NULL
, 0, 0);
3382 static void emit_binary_diff(struct diff_options
*o
,
3383 mmfile_t
*one
, mmfile_t
*two
)
3385 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_DIFF_HEADER
, NULL
, 0, 0);
3386 emit_binary_diff_body(o
, one
, two
);
3387 emit_binary_diff_body(o
, two
, one
);
3390 int diff_filespec_is_binary(struct repository
*r
,
3391 struct diff_filespec
*one
)
3393 struct diff_populate_filespec_options dpf_options
= {
3397 if (one
->is_binary
== -1) {
3398 diff_filespec_load_driver(one
, r
->index
);
3399 if (one
->driver
->binary
!= -1)
3400 one
->is_binary
= one
->driver
->binary
;
3402 if (!one
->data
&& DIFF_FILE_VALID(one
))
3403 diff_populate_filespec(r
, one
, &dpf_options
);
3404 if (one
->is_binary
== -1 && one
->data
)
3405 one
->is_binary
= buffer_is_binary(one
->data
,
3407 if (one
->is_binary
== -1)
3411 return one
->is_binary
;
3414 static const struct userdiff_funcname
*
3415 diff_funcname_pattern(struct diff_options
*o
, struct diff_filespec
*one
)
3417 diff_filespec_load_driver(one
, o
->repo
->index
);
3418 return one
->driver
->funcname
.pattern
? &one
->driver
->funcname
: NULL
;
3421 void diff_set_mnemonic_prefix(struct diff_options
*options
, const char *a
, const char *b
)
3423 if (!options
->a_prefix
)
3424 options
->a_prefix
= a
;
3425 if (!options
->b_prefix
)
3426 options
->b_prefix
= b
;
3429 void diff_set_noprefix(struct diff_options
*options
)
3431 options
->a_prefix
= options
->b_prefix
= "";
3434 void diff_set_default_prefix(struct diff_options
*options
)
3436 options
->a_prefix
= diff_src_prefix
;
3437 options
->b_prefix
= diff_dst_prefix
;
3440 struct userdiff_driver
*get_textconv(struct repository
*r
,
3441 struct diff_filespec
*one
)
3443 if (!DIFF_FILE_VALID(one
))
3446 diff_filespec_load_driver(one
, r
->index
);
3447 return userdiff_get_textconv(r
, one
->driver
);
3450 static struct string_list
*additional_headers(struct diff_options
*o
,
3453 if (!o
->additional_path_headers
)
3455 return strmap_get(o
->additional_path_headers
, path
);
3458 static void add_formatted_header(struct strbuf
*msg
,
3460 const char *line_prefix
,
3464 const char *next
, *newline
;
3466 for (next
= header
; *next
; next
= newline
) {
3467 newline
= strchrnul(next
, '\n');
3468 strbuf_addf(msg
, "%s%s%.*s%s\n", line_prefix
, meta
,
3469 (int)(newline
- next
), next
, reset
);
3475 static void add_formatted_headers(struct strbuf
*msg
,
3476 struct string_list
*more_headers
,
3477 const char *line_prefix
,
3483 for (i
= 0; i
< more_headers
->nr
; i
++)
3484 add_formatted_header(msg
, more_headers
->items
[i
].string
,
3485 line_prefix
, meta
, reset
);
3488 static int diff_filepair_is_phoney(struct diff_filespec
*one
,
3489 struct diff_filespec
*two
)
3492 * This function specifically looks for pairs injected by
3493 * create_filepairs_for_header_only_notifications(). Such
3494 * pairs are "phoney" in that they do not represent any
3495 * content or even mode difference, but were inserted because
3496 * diff_queued_diff previously had no pair associated with
3497 * that path but we needed some pair to avoid losing the
3498 * "remerge CONFLICT" header associated with the path.
3500 return !DIFF_FILE_VALID(one
) && !DIFF_FILE_VALID(two
);
3503 static int set_diff_algorithm(struct diff_options
*opts
,
3506 long value
= parse_algorithm_value(alg
);
3511 /* clear out previous settings */
3512 DIFF_XDL_CLR(opts
, NEED_MINIMAL
);
3513 opts
->xdl_opts
&= ~XDF_DIFF_ALGORITHM_MASK
;
3514 opts
->xdl_opts
|= value
;
3519 static void builtin_diff(const char *name_a
,
3521 struct diff_filespec
*one
,
3522 struct diff_filespec
*two
,
3523 const char *xfrm_msg
,
3524 int must_show_header
,
3525 struct diff_options
*o
,
3526 int complete_rewrite
)
3530 char *a_one
, *b_two
;
3531 const char *meta
= diff_get_color_opt(o
, DIFF_METAINFO
);
3532 const char *reset
= diff_get_color_opt(o
, DIFF_RESET
);
3533 const char *a_prefix
, *b_prefix
;
3534 struct userdiff_driver
*textconv_one
= NULL
;
3535 struct userdiff_driver
*textconv_two
= NULL
;
3536 struct strbuf header
= STRBUF_INIT
;
3537 const char *line_prefix
= diff_line_prefix(o
);
3539 diff_set_mnemonic_prefix(o
, "a/", "b/");
3540 if (o
->flags
.reverse_diff
) {
3541 a_prefix
= o
->b_prefix
;
3542 b_prefix
= o
->a_prefix
;
3544 a_prefix
= o
->a_prefix
;
3545 b_prefix
= o
->b_prefix
;
3548 if (o
->submodule_format
== DIFF_SUBMODULE_LOG
&&
3549 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3550 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3551 (!diff_filepair_is_phoney(one
, two
))) {
3552 show_submodule_diff_summary(o
, one
->path
? one
->path
: two
->path
,
3553 &one
->oid
, &two
->oid
,
3554 two
->dirty_submodule
);
3556 } else if (o
->submodule_format
== DIFF_SUBMODULE_INLINE_DIFF
&&
3557 (!one
->mode
|| S_ISGITLINK(one
->mode
)) &&
3558 (!two
->mode
|| S_ISGITLINK(two
->mode
)) &&
3559 (!diff_filepair_is_phoney(one
, two
))) {
3560 show_submodule_inline_diff(o
, one
->path
? one
->path
: two
->path
,
3561 &one
->oid
, &two
->oid
,
3562 two
->dirty_submodule
);
3566 if (o
->flags
.allow_textconv
) {
3567 textconv_one
= get_textconv(o
->repo
, one
);
3568 textconv_two
= get_textconv(o
->repo
, two
);
3571 /* Never use a non-valid filename anywhere if at all possible */
3572 name_a
= DIFF_FILE_VALID(one
) ? name_a
: name_b
;
3573 name_b
= DIFF_FILE_VALID(two
) ? name_b
: name_a
;
3575 a_one
= quote_two(a_prefix
, name_a
+ (*name_a
== '/'));
3576 b_two
= quote_two(b_prefix
, name_b
+ (*name_b
== '/'));
3577 lbl
[0] = DIFF_FILE_VALID(one
) ? a_one
: "/dev/null";
3578 lbl
[1] = DIFF_FILE_VALID(two
) ? b_two
: "/dev/null";
3579 if (diff_filepair_is_phoney(one
, two
)) {
3581 * We should only reach this point for pairs generated from
3582 * create_filepairs_for_header_only_notifications(). For
3583 * these, we want to avoid the "/dev/null" special casing
3584 * above, because we do not want such pairs shown as either
3585 * "new file" or "deleted file" below.
3590 strbuf_addf(&header
, "%s%sdiff --git %s %s%s\n", line_prefix
, meta
, a_one
, b_two
, reset
);
3591 if (lbl
[0][0] == '/') {
3593 strbuf_addf(&header
, "%s%snew file mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3595 strbuf_addstr(&header
, xfrm_msg
);
3596 o
->found_changes
= 1;
3597 must_show_header
= 1;
3599 else if (lbl
[1][0] == '/') {
3600 strbuf_addf(&header
, "%s%sdeleted file mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3602 strbuf_addstr(&header
, xfrm_msg
);
3603 o
->found_changes
= 1;
3604 must_show_header
= 1;
3607 if (one
->mode
!= two
->mode
) {
3608 strbuf_addf(&header
, "%s%sold mode %06o%s\n", line_prefix
, meta
, one
->mode
, reset
);
3609 strbuf_addf(&header
, "%s%snew mode %06o%s\n", line_prefix
, meta
, two
->mode
, reset
);
3610 o
->found_changes
= 1;
3611 must_show_header
= 1;
3614 strbuf_addstr(&header
, xfrm_msg
);
3617 * we do not run diff between different kind
3620 if ((one
->mode
^ two
->mode
) & S_IFMT
)
3621 goto free_ab_and_return
;
3622 if (complete_rewrite
&&
3623 (textconv_one
|| !diff_filespec_is_binary(o
->repo
, one
)) &&
3624 (textconv_two
|| !diff_filespec_is_binary(o
->repo
, two
))) {
3625 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3626 header
.buf
, header
.len
, 0);
3627 strbuf_reset(&header
);
3628 emit_rewrite_diff(name_a
, name_b
, one
, two
,
3629 textconv_one
, textconv_two
, o
);
3630 o
->found_changes
= 1;
3631 goto free_ab_and_return
;
3635 if (o
->irreversible_delete
&& lbl
[1][0] == '/') {
3636 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
,
3638 strbuf_reset(&header
);
3639 goto free_ab_and_return
;
3640 } else if (!o
->flags
.text
&&
3641 ( (!textconv_one
&& diff_filespec_is_binary(o
->repo
, one
)) ||
3642 (!textconv_two
&& diff_filespec_is_binary(o
->repo
, two
)) )) {
3643 struct strbuf sb
= STRBUF_INIT
;
3644 if (!one
->data
&& !two
->data
&&
3645 S_ISREG(one
->mode
) && S_ISREG(two
->mode
) &&
3647 if (oideq(&one
->oid
, &two
->oid
)) {
3648 if (must_show_header
)
3649 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3650 header
.buf
, header
.len
,
3652 goto free_ab_and_return
;
3654 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3655 header
.buf
, header
.len
, 0);
3656 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3657 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3658 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3660 strbuf_release(&sb
);
3661 goto free_ab_and_return
;
3663 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3664 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3665 die("unable to read files to diff");
3666 /* Quite common confusing case */
3667 if (mf1
.size
== mf2
.size
&&
3668 !memcmp(mf1
.ptr
, mf2
.ptr
, mf1
.size
)) {
3669 if (must_show_header
)
3670 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3671 header
.buf
, header
.len
, 0);
3672 goto free_ab_and_return
;
3674 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
, header
.buf
, header
.len
, 0);
3675 strbuf_reset(&header
);
3676 if (o
->flags
.binary
)
3677 emit_binary_diff(o
, &mf1
, &mf2
);
3679 strbuf_addf(&sb
, "%sBinary files %s and %s differ\n",
3680 diff_line_prefix(o
), lbl
[0], lbl
[1]);
3681 emit_diff_symbol(o
, DIFF_SYMBOL_BINARY_FILES
,
3683 strbuf_release(&sb
);
3685 o
->found_changes
= 1;
3687 /* Crazy xdl interfaces.. */
3688 const char *diffopts
;
3692 struct emit_callback ecbdata
;
3693 const struct userdiff_funcname
*pe
;
3695 if (must_show_header
) {
3696 emit_diff_symbol(o
, DIFF_SYMBOL_HEADER
,
3697 header
.buf
, header
.len
, 0);
3698 strbuf_reset(&header
);
3701 mf1
.size
= fill_textconv(o
->repo
, textconv_one
, one
, &mf1
.ptr
);
3702 mf2
.size
= fill_textconv(o
->repo
, textconv_two
, two
, &mf2
.ptr
);
3704 pe
= diff_funcname_pattern(o
, one
);
3706 pe
= diff_funcname_pattern(o
, two
);
3708 memset(&xpp
, 0, sizeof(xpp
));
3709 memset(&xecfg
, 0, sizeof(xecfg
));
3710 memset(&ecbdata
, 0, sizeof(ecbdata
));
3711 if (o
->flags
.suppress_diff_headers
)
3713 ecbdata
.label_path
= lbl
;
3714 ecbdata
.color_diff
= want_color(o
->use_color
);
3715 ecbdata
.ws_rule
= whitespace_rule(o
->repo
->index
, name_b
);
3716 if (ecbdata
.ws_rule
& WS_BLANK_AT_EOF
)
3717 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3719 if (header
.len
&& !o
->flags
.suppress_diff_headers
)
3720 ecbdata
.header
= &header
;
3721 xpp
.flags
= o
->xdl_opts
;
3722 xpp
.ignore_regex
= o
->ignore_regex
;
3723 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3724 xpp
.anchors
= o
->anchors
;
3725 xpp
.anchors_nr
= o
->anchors_nr
;
3726 xecfg
.ctxlen
= o
->context
;
3727 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3728 xecfg
.flags
= XDL_EMIT_FUNCNAMES
;
3729 if (o
->flags
.funccontext
)
3730 xecfg
.flags
|= XDL_EMIT_FUNCCONTEXT
;
3732 xdiff_set_find_func(&xecfg
, pe
->pattern
, pe
->cflags
);
3734 diffopts
= getenv("GIT_DIFF_OPTS");
3737 else if (skip_prefix(diffopts
, "--unified=", &v
))
3738 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3739 else if (skip_prefix(diffopts
, "-u", &v
))
3740 xecfg
.ctxlen
= strtoul(v
, NULL
, 10);
3743 init_diff_words_data(&ecbdata
, o
, one
, two
);
3744 if (xdi_diff_outf(&mf1
, &mf2
, NULL
, fn_out_consume
,
3745 &ecbdata
, &xpp
, &xecfg
))
3746 die("unable to generate diff for %s", one
->path
);
3748 free_diff_words_data(&ecbdata
);
3753 xdiff_clear_find_func(&xecfg
);
3757 strbuf_release(&header
);
3758 diff_free_filespec_data(one
);
3759 diff_free_filespec_data(two
);
3765 static char *get_compact_summary(const struct diff_filepair
*p
, int is_renamed
)
3768 if (p
->status
== DIFF_STATUS_ADDED
) {
3769 if (S_ISLNK(p
->two
->mode
))
3771 else if ((p
->two
->mode
& 0777) == 0755)
3775 } else if (p
->status
== DIFF_STATUS_DELETED
)
3778 if (S_ISLNK(p
->one
->mode
) && !S_ISLNK(p
->two
->mode
))
3780 else if (!S_ISLNK(p
->one
->mode
) && S_ISLNK(p
->two
->mode
))
3782 else if ((p
->one
->mode
& 0777) == 0644 &&
3783 (p
->two
->mode
& 0777) == 0755)
3785 else if ((p
->one
->mode
& 0777) == 0755 &&
3786 (p
->two
->mode
& 0777) == 0644)
3791 static void builtin_diffstat(const char *name_a
, const char *name_b
,
3792 struct diff_filespec
*one
,
3793 struct diff_filespec
*two
,
3794 struct diffstat_t
*diffstat
,
3795 struct diff_options
*o
,
3796 struct diff_filepair
*p
)
3799 struct diffstat_file
*data
;
3801 int complete_rewrite
= 0;
3803 if (!DIFF_PAIR_UNMERGED(p
)) {
3804 if (p
->status
== DIFF_STATUS_MODIFIED
&& p
->score
)
3805 complete_rewrite
= 1;
3808 data
= diffstat_add(diffstat
, name_a
, name_b
);
3809 data
->is_interesting
= p
->status
!= DIFF_STATUS_UNKNOWN
;
3810 if (o
->flags
.stat_with_summary
)
3811 data
->comments
= get_compact_summary(p
, data
->is_renamed
);
3814 data
->is_unmerged
= 1;
3818 /* saves some reads if true, not a guarantee of diff outcome */
3819 may_differ
= !(one
->oid_valid
&& two
->oid_valid
&&
3820 oideq(&one
->oid
, &two
->oid
));
3822 if (diff_filespec_is_binary(o
->repo
, one
) ||
3823 diff_filespec_is_binary(o
->repo
, two
)) {
3824 data
->is_binary
= 1;
3829 data
->added
= diff_filespec_size(o
->repo
, two
);
3830 data
->deleted
= diff_filespec_size(o
->repo
, one
);
3834 else if (complete_rewrite
) {
3835 diff_populate_filespec(o
->repo
, one
, NULL
);
3836 diff_populate_filespec(o
->repo
, two
, NULL
);
3837 data
->deleted
= count_lines(one
->data
, one
->size
);
3838 data
->added
= count_lines(two
->data
, two
->size
);
3841 else if (may_differ
) {
3842 /* Crazy xdl interfaces.. */
3846 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3847 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3848 die("unable to read files to diff");
3850 memset(&xpp
, 0, sizeof(xpp
));
3851 memset(&xecfg
, 0, sizeof(xecfg
));
3852 xpp
.flags
= o
->xdl_opts
;
3853 xpp
.ignore_regex
= o
->ignore_regex
;
3854 xpp
.ignore_regex_nr
= o
->ignore_regex_nr
;
3855 xpp
.anchors
= o
->anchors
;
3856 xpp
.anchors_nr
= o
->anchors_nr
;
3857 xecfg
.ctxlen
= o
->context
;
3858 xecfg
.interhunkctxlen
= o
->interhunkcontext
;
3859 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
3860 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
3861 diffstat_consume
, diffstat
, &xpp
, &xecfg
))
3862 die("unable to generate diffstat for %s", one
->path
);
3864 if (DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
)) {
3865 struct diffstat_file
*file
=
3866 diffstat
->files
[diffstat
->nr
- 1];
3868 * Omit diffstats of modified files where nothing changed.
3869 * Even if may_differ, this might be the case due to
3870 * ignoring whitespace changes, etc.
3872 * But note that we special-case additions, deletions,
3873 * renames, and mode changes as adding an empty file,
3874 * for example is still of interest.
3876 if ((p
->status
== DIFF_STATUS_MODIFIED
)
3879 && one
->mode
== two
->mode
) {
3880 free_diffstat_file(file
);
3886 diff_free_filespec_data(one
);
3887 diff_free_filespec_data(two
);
3890 static void builtin_checkdiff(const char *name_a
, const char *name_b
,
3891 const char *attr_path
,
3892 struct diff_filespec
*one
,
3893 struct diff_filespec
*two
,
3894 struct diff_options
*o
)
3897 struct checkdiff_t data
;
3902 memset(&data
, 0, sizeof(data
));
3903 data
.filename
= name_b
? name_b
: name_a
;
3906 data
.ws_rule
= whitespace_rule(o
->repo
->index
, attr_path
);
3907 data
.conflict_marker_size
= ll_merge_marker_size(o
->repo
->index
, attr_path
);
3909 if (fill_mmfile(o
->repo
, &mf1
, one
) < 0 ||
3910 fill_mmfile(o
->repo
, &mf2
, two
) < 0)
3911 die("unable to read files to diff");
3914 * All the other codepaths check both sides, but not checking
3915 * the "old" side here is deliberate. We are checking the newly
3916 * introduced changes, and as long as the "new" side is text, we
3917 * can and should check what it introduces.
3919 if (diff_filespec_is_binary(o
->repo
, two
))
3920 goto free_and_return
;
3922 /* Crazy xdl interfaces.. */
3926 memset(&xpp
, 0, sizeof(xpp
));
3927 memset(&xecfg
, 0, sizeof(xecfg
));
3928 xecfg
.ctxlen
= 1; /* at least one context line */
3930 if (xdi_diff_outf(&mf1
, &mf2
, checkdiff_consume_hunk
,
3931 checkdiff_consume
, &data
,
3933 die("unable to generate checkdiff for %s", one
->path
);
3935 if (data
.ws_rule
& WS_BLANK_AT_EOF
) {
3936 struct emit_callback ecbdata
;
3939 ecbdata
.ws_rule
= data
.ws_rule
;
3940 check_blank_at_eof(&mf1
, &mf2
, &ecbdata
);
3941 blank_at_eof
= ecbdata
.blank_at_eof_in_postimage
;
3946 err
= whitespace_error_string(WS_BLANK_AT_EOF
);
3947 fprintf(o
->file
, "%s:%d: %s.\n",
3948 data
.filename
, blank_at_eof
, err
);
3949 data
.status
= 1; /* report errors */
3954 diff_free_filespec_data(one
);
3955 diff_free_filespec_data(two
);
3957 o
->flags
.check_failed
= 1;
3960 struct diff_filespec
*alloc_filespec(const char *path
)
3962 struct diff_filespec
*spec
;
3964 FLEXPTR_ALLOC_STR(spec
, path
, path
);
3966 spec
->is_binary
= -1;
3970 void free_filespec(struct diff_filespec
*spec
)
3972 if (!--spec
->count
) {
3973 diff_free_filespec_data(spec
);
3978 void fill_filespec(struct diff_filespec
*spec
, const struct object_id
*oid
,
3979 int oid_valid
, unsigned short mode
)
3982 spec
->mode
= canon_mode(mode
);
3983 oidcpy(&spec
->oid
, oid
);
3984 spec
->oid_valid
= oid_valid
;
3989 * Given a name and sha1 pair, if the index tells us the file in
3990 * the work tree has that object contents, return true, so that
3991 * prepare_temp_file() does not have to inflate and extract.
3993 static int reuse_worktree_file(struct index_state
*istate
,
3995 const struct object_id
*oid
,
3998 const struct cache_entry
*ce
;
4003 * We do not read the cache ourselves here, because the
4004 * benchmark with my previous version that always reads cache
4005 * shows that it makes things worse for diff-tree comparing
4006 * two linux-2.6 kernel trees in an already checked out work
4007 * tree. This is because most diff-tree comparisons deal with
4008 * only a small number of files, while reading the cache is
4009 * expensive for a large project, and its cost outweighs the
4010 * savings we get by not inflating the object to a temporary
4011 * file. Practically, this code only helps when we are used
4012 * by diff-cache --cached, which does read the cache before
4018 /* We want to avoid the working directory if our caller
4019 * doesn't need the data in a normal file, this system
4020 * is rather slow with its stat/open/mmap/close syscalls,
4021 * and the object is contained in a pack file. The pack
4022 * is probably already open and will be faster to obtain
4023 * the data through than the working directory. Loose
4024 * objects however would tend to be slower as they need
4025 * to be individually opened and inflated.
4027 if (!FAST_WORKING_DIRECTORY
&& !want_file
&& has_object_pack(oid
))
4031 * Similarly, if we'd have to convert the file contents anyway, that
4032 * makes the optimization not worthwhile.
4034 if (!want_file
&& would_convert_to_git(istate
, name
))
4038 * If this path does not match our sparse-checkout definition,
4039 * then the file will not be in the working directory.
4041 if (!path_in_sparse_checkout(name
, istate
))
4045 pos
= index_name_pos(istate
, name
, len
);
4048 ce
= istate
->cache
[pos
];
4051 * This is not the sha1 we are looking for, or
4052 * unreusable because it is not a regular file.
4054 if (!oideq(oid
, &ce
->oid
) || !S_ISREG(ce
->ce_mode
))
4058 * If ce is marked as "assume unchanged", there is no
4059 * guarantee that work tree matches what we are looking for.
4061 if ((ce
->ce_flags
& CE_VALID
) || ce_skip_worktree(ce
))
4065 * If ce matches the file in the work tree, we can reuse it.
4067 if (ce_uptodate(ce
) ||
4068 (!lstat(name
, &st
) && !ie_match_stat(istate
, ce
, &st
, 0)))
4074 static int diff_populate_gitlink(struct diff_filespec
*s
, int size_only
)
4076 struct strbuf buf
= STRBUF_INIT
;
4079 /* Are we looking at the work tree? */
4080 if (s
->dirty_submodule
)
4083 strbuf_addf(&buf
, "Subproject commit %s%s\n",
4084 oid_to_hex(&s
->oid
), dirty
);
4088 strbuf_release(&buf
);
4090 s
->data
= strbuf_detach(&buf
, NULL
);
4097 * While doing rename detection and pickaxe operation, we may need to
4098 * grab the data for the blob (or file) for our own in-core comparison.
4099 * diff_filespec has data and size fields for this purpose.
4101 int diff_populate_filespec(struct repository
*r
,
4102 struct diff_filespec
*s
,
4103 const struct diff_populate_filespec_options
*options
)
4105 int size_only
= options
? options
->check_size_only
: 0;
4106 int check_binary
= options
? options
->check_binary
: 0;
4108 int conv_flags
= global_conv_flags_eol
;
4110 * demote FAIL to WARN to allow inspecting the situation
4111 * instead of refusing.
4113 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
4114 conv_flags
= CONV_EOL_RNDTRP_WARN
;
4116 if (!DIFF_FILE_VALID(s
))
4117 die("internal error: asking to populate invalid file.");
4118 if (S_ISDIR(s
->mode
))
4124 if (size_only
&& 0 < s
->size
)
4127 if (S_ISGITLINK(s
->mode
))
4128 return diff_populate_gitlink(s
, size_only
);
4130 if (!s
->oid_valid
||
4131 reuse_worktree_file(r
->index
, s
->path
, &s
->oid
, 0)) {
4132 struct strbuf buf
= STRBUF_INIT
;
4136 if (lstat(s
->path
, &st
) < 0) {
4140 s
->data
= (char *)"";
4144 s
->size
= xsize_t(st
.st_size
);
4147 if (S_ISLNK(st
.st_mode
)) {
4148 struct strbuf sb
= STRBUF_INIT
;
4150 if (strbuf_readlink(&sb
, s
->path
, s
->size
))
4153 s
->data
= strbuf_detach(&sb
, NULL
);
4159 * Even if the caller would be happy with getting
4160 * only the size, we cannot return early at this
4161 * point if the path requires us to run the content
4164 if (size_only
&& !would_convert_to_git(r
->index
, s
->path
))
4168 * Note: this check uses xsize_t(st.st_size) that may
4169 * not be the true size of the blob after it goes
4170 * through convert_to_git(). This may not strictly be
4171 * correct, but the whole point of big_file_threshold
4172 * and is_binary check being that we want to avoid
4173 * opening the file and inspecting the contents, this
4177 s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4181 fd
= open(s
->path
, O_RDONLY
);
4184 s
->data
= xmmap(NULL
, s
->size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
4186 s
->should_munmap
= 1;
4189 * Convert from working tree format to canonical git format
4191 if (convert_to_git(r
->index
, s
->path
, s
->data
, s
->size
, &buf
, conv_flags
)) {
4193 munmap(s
->data
, s
->size
);
4194 s
->should_munmap
= 0;
4195 s
->data
= strbuf_detach(&buf
, &size
);
4201 struct object_info info
= {
4205 if (!(size_only
|| check_binary
))
4207 * Set contentp, since there is no chance that merely
4208 * the size is sufficient.
4210 info
.contentp
= &s
->data
;
4212 if (options
&& options
->missing_object_cb
) {
4213 if (!oid_object_info_extended(r
, &s
->oid
, &info
,
4214 OBJECT_INFO_LOOKUP_REPLACE
|
4215 OBJECT_INFO_SKIP_FETCH_OBJECT
))
4217 options
->missing_object_cb(options
->missing_object_data
);
4219 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4220 OBJECT_INFO_LOOKUP_REPLACE
))
4221 die("unable to read %s", oid_to_hex(&s
->oid
));
4224 if (size_only
|| check_binary
) {
4227 if (s
->size
> big_file_threshold
&& s
->is_binary
== -1) {
4232 if (!info
.contentp
) {
4233 info
.contentp
= &s
->data
;
4234 if (oid_object_info_extended(r
, &s
->oid
, &info
,
4235 OBJECT_INFO_LOOKUP_REPLACE
))
4236 die("unable to read %s", oid_to_hex(&s
->oid
));
4243 void diff_free_filespec_blob(struct diff_filespec
*s
)
4247 else if (s
->should_munmap
)
4248 munmap(s
->data
, s
->size
);
4250 if (s
->should_free
|| s
->should_munmap
) {
4251 s
->should_free
= s
->should_munmap
= 0;
4256 void diff_free_filespec_data(struct diff_filespec
*s
)
4261 diff_free_filespec_blob(s
);
4262 FREE_AND_NULL(s
->cnt_data
);
4265 static void prep_temp_blob(struct index_state
*istate
,
4266 const char *path
, struct diff_tempfile
*temp
,
4269 const struct object_id
*oid
,
4272 struct strbuf buf
= STRBUF_INIT
;
4273 char *path_dup
= xstrdup(path
);
4274 const char *base
= basename(path_dup
);
4275 struct checkout_metadata meta
;
4277 init_checkout_metadata(&meta
, NULL
, NULL
, oid
);
4279 temp
->tempfile
= mks_tempfile_dt("git-blob-XXXXXX", base
);
4280 if (!temp
->tempfile
)
4281 die_errno("unable to create temp-file");
4282 if (convert_to_working_tree(istate
, path
,
4283 (const char *)blob
, (size_t)size
, &buf
, &meta
)) {
4287 if (write_in_full(temp
->tempfile
->fd
, blob
, size
) < 0 ||
4288 close_tempfile_gently(temp
->tempfile
))
4289 die_errno("unable to write temp-file");
4290 temp
->name
= get_tempfile_path(temp
->tempfile
);
4291 oid_to_hex_r(temp
->hex
, oid
);
4292 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", mode
);
4293 strbuf_release(&buf
);
4297 static struct diff_tempfile
*prepare_temp_file(struct repository
*r
,
4298 struct diff_filespec
*one
)
4300 struct diff_tempfile
*temp
= claim_diff_tempfile();
4302 if (!DIFF_FILE_VALID(one
)) {
4304 /* A '-' entry produces this for file-2, and
4305 * a '+' entry produces this for file-1.
4307 temp
->name
= "/dev/null";
4308 xsnprintf(temp
->hex
, sizeof(temp
->hex
), ".");
4309 xsnprintf(temp
->mode
, sizeof(temp
->mode
), ".");
4313 if (!S_ISGITLINK(one
->mode
) &&
4315 reuse_worktree_file(r
->index
, one
->path
, &one
->oid
, 1))) {
4317 if (lstat(one
->path
, &st
) < 0) {
4318 if (errno
== ENOENT
)
4319 goto not_a_valid_file
;
4320 die_errno("stat(%s)", one
->path
);
4322 if (S_ISLNK(st
.st_mode
)) {
4323 struct strbuf sb
= STRBUF_INIT
;
4324 if (strbuf_readlink(&sb
, one
->path
, st
.st_size
) < 0)
4325 die_errno("readlink(%s)", one
->path
);
4326 prep_temp_blob(r
->index
, one
->path
, temp
, sb
.buf
, sb
.len
,
4328 &one
->oid
: null_oid()),
4330 one
->mode
: S_IFLNK
));
4331 strbuf_release(&sb
);
4334 /* we can borrow from the file in the work tree */
4335 temp
->name
= one
->path
;
4336 if (!one
->oid_valid
)
4337 oid_to_hex_r(temp
->hex
, null_oid());
4339 oid_to_hex_r(temp
->hex
, &one
->oid
);
4340 /* Even though we may sometimes borrow the
4341 * contents from the work tree, we always want
4342 * one->mode. mode is trustworthy even when
4343 * !(one->oid_valid), as long as
4344 * DIFF_FILE_VALID(one).
4346 xsnprintf(temp
->mode
, sizeof(temp
->mode
), "%06o", one
->mode
);
4351 if (diff_populate_filespec(r
, one
, NULL
))
4352 die("cannot read data blob for %s", one
->path
);
4353 prep_temp_blob(r
->index
, one
->path
, temp
,
4354 one
->data
, one
->size
,
4355 &one
->oid
, one
->mode
);
4360 static void add_external_diff_name(struct repository
*r
,
4361 struct strvec
*argv
,
4362 struct diff_filespec
*df
)
4364 struct diff_tempfile
*temp
= prepare_temp_file(r
, df
);
4365 strvec_push(argv
, temp
->name
);
4366 strvec_push(argv
, temp
->hex
);
4367 strvec_push(argv
, temp
->mode
);
4370 /* An external diff command takes:
4372 * diff-cmd name infile1 infile1-sha1 infile1-mode \
4373 * infile2 infile2-sha1 infile2-mode [ rename-to ]
4376 static void run_external_diff(const char *pgm
,
4379 struct diff_filespec
*one
,
4380 struct diff_filespec
*two
,
4381 const char *xfrm_msg
,
4382 struct diff_options
*o
)
4384 struct child_process cmd
= CHILD_PROCESS_INIT
;
4385 struct diff_queue_struct
*q
= &diff_queued_diff
;
4387 strvec_push(&cmd
.args
, pgm
);
4388 strvec_push(&cmd
.args
, name
);
4391 add_external_diff_name(o
->repo
, &cmd
.args
, one
);
4392 add_external_diff_name(o
->repo
, &cmd
.args
, two
);
4394 strvec_push(&cmd
.args
, other
);
4396 strvec_push(&cmd
.args
, xfrm_msg
);
4400 strvec_pushf(&cmd
.env
, "GIT_DIFF_PATH_COUNTER=%d",
4401 ++o
->diff_path_counter
);
4402 strvec_pushf(&cmd
.env
, "GIT_DIFF_PATH_TOTAL=%d", q
->nr
);
4404 diff_free_filespec_data(one
);
4405 diff_free_filespec_data(two
);
4407 if (run_command(&cmd
))
4408 die(_("external diff died, stopping at %s"), name
);
4413 static int similarity_index(struct diff_filepair
*p
)
4415 return p
->score
* 100 / MAX_SCORE
;
4418 static const char *diff_abbrev_oid(const struct object_id
*oid
, int abbrev
)
4420 if (startup_info
->have_repository
)
4421 return repo_find_unique_abbrev(the_repository
, oid
, abbrev
);
4423 char *hex
= oid_to_hex(oid
);
4425 abbrev
= FALLBACK_DEFAULT_ABBREV
;
4426 if (abbrev
> the_hash_algo
->hexsz
)
4427 BUG("oid abbreviation out of range: %d", abbrev
);
4434 static void fill_metainfo(struct strbuf
*msg
,
4437 struct diff_filespec
*one
,
4438 struct diff_filespec
*two
,
4439 struct diff_options
*o
,
4440 struct diff_filepair
*p
,
4441 int *must_show_header
,
4444 const char *set
= diff_get_color(use_color
, DIFF_METAINFO
);
4445 const char *reset
= diff_get_color(use_color
, DIFF_RESET
);
4446 const char *line_prefix
= diff_line_prefix(o
);
4447 struct string_list
*more_headers
= NULL
;
4449 *must_show_header
= 1;
4450 strbuf_init(msg
, PATH_MAX
* 2 + 300);
4451 switch (p
->status
) {
4452 case DIFF_STATUS_COPIED
:
4453 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4454 line_prefix
, set
, similarity_index(p
));
4455 strbuf_addf(msg
, "%s\n%s%scopy from ",
4456 reset
, line_prefix
, set
);
4457 quote_c_style(name
, msg
, NULL
, 0);
4458 strbuf_addf(msg
, "%s\n%s%scopy to ", reset
, line_prefix
, set
);
4459 quote_c_style(other
, msg
, NULL
, 0);
4460 strbuf_addf(msg
, "%s\n", reset
);
4462 case DIFF_STATUS_RENAMED
:
4463 strbuf_addf(msg
, "%s%ssimilarity index %d%%",
4464 line_prefix
, set
, similarity_index(p
));
4465 strbuf_addf(msg
, "%s\n%s%srename from ",
4466 reset
, line_prefix
, set
);
4467 quote_c_style(name
, msg
, NULL
, 0);
4468 strbuf_addf(msg
, "%s\n%s%srename to ",
4469 reset
, line_prefix
, set
);
4470 quote_c_style(other
, msg
, NULL
, 0);
4471 strbuf_addf(msg
, "%s\n", reset
);
4473 case DIFF_STATUS_MODIFIED
:
4475 strbuf_addf(msg
, "%s%sdissimilarity index %d%%%s\n",
4477 set
, similarity_index(p
), reset
);
4482 *must_show_header
= 0;
4484 if ((more_headers
= additional_headers(o
, name
))) {
4485 add_formatted_headers(msg
, more_headers
,
4486 line_prefix
, set
, reset
);
4487 *must_show_header
= 1;
4489 if (one
&& two
&& !oideq(&one
->oid
, &two
->oid
)) {
4490 const unsigned hexsz
= the_hash_algo
->hexsz
;
4491 int abbrev
= o
->abbrev
? o
->abbrev
: DEFAULT_ABBREV
;
4493 if (o
->flags
.full_index
)
4496 if (o
->flags
.binary
) {
4498 if ((!fill_mmfile(o
->repo
, &mf
, one
) &&
4499 diff_filespec_is_binary(o
->repo
, one
)) ||
4500 (!fill_mmfile(o
->repo
, &mf
, two
) &&
4501 diff_filespec_is_binary(o
->repo
, two
)))
4504 strbuf_addf(msg
, "%s%sindex %s..%s", line_prefix
, set
,
4505 diff_abbrev_oid(&one
->oid
, abbrev
),
4506 diff_abbrev_oid(&two
->oid
, abbrev
));
4507 if (one
->mode
== two
->mode
)
4508 strbuf_addf(msg
, " %06o", one
->mode
);
4509 strbuf_addf(msg
, "%s\n", reset
);
4513 static void run_diff_cmd(const char *pgm
,
4516 const char *attr_path
,
4517 struct diff_filespec
*one
,
4518 struct diff_filespec
*two
,
4520 struct diff_options
*o
,
4521 struct diff_filepair
*p
)
4523 const char *xfrm_msg
= NULL
;
4524 int complete_rewrite
= (p
->status
== DIFF_STATUS_MODIFIED
) && p
->score
;
4525 int must_show_header
= 0;
4526 struct userdiff_driver
*drv
= NULL
;
4528 if (o
->flags
.allow_external
|| !o
->ignore_driver_algorithm
)
4529 drv
= userdiff_find_by_path(o
->repo
->index
, attr_path
);
4531 if (o
->flags
.allow_external
&& drv
&& drv
->external
)
4532 pgm
= drv
->external
;
4536 * don't use colors when the header is intended for an
4537 * external diff driver
4539 fill_metainfo(msg
, name
, other
, one
, two
, o
, p
,
4541 want_color(o
->use_color
) && !pgm
);
4542 xfrm_msg
= msg
->len
? msg
->buf
: NULL
;
4546 run_external_diff(pgm
, name
, other
, one
, two
, xfrm_msg
, o
);
4550 if (!o
->ignore_driver_algorithm
&& drv
&& drv
->algorithm
)
4551 set_diff_algorithm(o
, drv
->algorithm
);
4553 builtin_diff(name
, other
? other
: name
,
4554 one
, two
, xfrm_msg
, must_show_header
,
4555 o
, complete_rewrite
);
4557 fprintf(o
->file
, "* Unmerged path %s\n", name
);
4558 o
->found_changes
= 1;
4562 static void diff_fill_oid_info(struct diff_filespec
*one
, struct index_state
*istate
)
4564 if (DIFF_FILE_VALID(one
)) {
4565 if (!one
->oid_valid
) {
4567 if (one
->is_stdin
) {
4571 if (lstat(one
->path
, &st
) < 0)
4572 die_errno("stat '%s'", one
->path
);
4573 if (index_path(istate
, &one
->oid
, one
->path
, &st
, 0))
4574 die("cannot hash %s", one
->path
);
4581 static void strip_prefix(int prefix_length
, const char **namep
, const char **otherp
)
4583 /* Strip the prefix but do not molest /dev/null and absolute paths */
4584 if (*namep
&& !is_absolute_path(*namep
)) {
4585 *namep
+= prefix_length
;
4589 if (*otherp
&& !is_absolute_path(*otherp
)) {
4590 *otherp
+= prefix_length
;
4591 if (**otherp
== '/')
4596 static void run_diff(struct diff_filepair
*p
, struct diff_options
*o
)
4598 const char *pgm
= external_diff();
4600 struct diff_filespec
*one
= p
->one
;
4601 struct diff_filespec
*two
= p
->two
;
4604 const char *attr_path
;
4607 other
= (strcmp(name
, two
->path
) ? two
->path
: NULL
);
4609 if (o
->prefix_length
)
4610 strip_prefix(o
->prefix_length
, &name
, &other
);
4612 if (!o
->flags
.allow_external
)
4615 if (DIFF_PAIR_UNMERGED(p
)) {
4616 run_diff_cmd(pgm
, name
, NULL
, attr_path
,
4617 NULL
, NULL
, NULL
, o
, p
);
4621 diff_fill_oid_info(one
, o
->repo
->index
);
4622 diff_fill_oid_info(two
, o
->repo
->index
);
4625 DIFF_FILE_VALID(one
) && DIFF_FILE_VALID(two
) &&
4626 (S_IFMT
& one
->mode
) != (S_IFMT
& two
->mode
)) {
4628 * a filepair that changes between file and symlink
4629 * needs to be split into deletion and creation.
4631 struct diff_filespec
*null
= alloc_filespec(two
->path
);
4632 run_diff_cmd(NULL
, name
, other
, attr_path
,
4636 strbuf_release(&msg
);
4638 null
= alloc_filespec(one
->path
);
4639 run_diff_cmd(NULL
, name
, other
, attr_path
,
4640 null
, two
, &msg
, o
, p
);
4644 run_diff_cmd(pgm
, name
, other
, attr_path
,
4645 one
, two
, &msg
, o
, p
);
4647 strbuf_release(&msg
);
4650 static void run_diffstat(struct diff_filepair
*p
, struct diff_options
*o
,
4651 struct diffstat_t
*diffstat
)
4656 if (!o
->ignore_driver_algorithm
) {
4657 struct userdiff_driver
*drv
= userdiff_find_by_path(o
->repo
->index
,
4660 if (drv
&& drv
->algorithm
)
4661 set_diff_algorithm(o
, drv
->algorithm
);
4664 if (DIFF_PAIR_UNMERGED(p
)) {
4666 builtin_diffstat(p
->one
->path
, NULL
, NULL
, NULL
,
4671 name
= p
->one
->path
;
4672 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4674 if (o
->prefix_length
)
4675 strip_prefix(o
->prefix_length
, &name
, &other
);
4677 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4678 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4680 builtin_diffstat(name
, other
, p
->one
, p
->two
,
4684 static void run_checkdiff(struct diff_filepair
*p
, struct diff_options
*o
)
4688 const char *attr_path
;
4690 if (DIFF_PAIR_UNMERGED(p
)) {
4695 name
= p
->one
->path
;
4696 other
= (strcmp(name
, p
->two
->path
) ? p
->two
->path
: NULL
);
4697 attr_path
= other
? other
: name
;
4699 if (o
->prefix_length
)
4700 strip_prefix(o
->prefix_length
, &name
, &other
);
4702 diff_fill_oid_info(p
->one
, o
->repo
->index
);
4703 diff_fill_oid_info(p
->two
, o
->repo
->index
);
4705 builtin_checkdiff(name
, other
, attr_path
, p
->one
, p
->two
, o
);
4708 void repo_diff_setup(struct repository
*r
, struct diff_options
*options
)
4710 memcpy(options
, &default_diff_options
, sizeof(*options
));
4712 options
->file
= stdout
;
4715 options
->output_indicators
[OUTPUT_INDICATOR_NEW
] = '+';
4716 options
->output_indicators
[OUTPUT_INDICATOR_OLD
] = '-';
4717 options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
] = ' ';
4718 options
->abbrev
= DEFAULT_ABBREV
;
4719 options
->line_termination
= '\n';
4720 options
->break_opt
= -1;
4721 options
->rename_limit
= -1;
4722 options
->dirstat_permille
= diff_dirstat_permille_default
;
4723 options
->context
= diff_context_default
;
4724 options
->interhunkcontext
= diff_interhunk_context_default
;
4725 options
->ws_error_highlight
= ws_error_highlight_default
;
4726 options
->flags
.rename_empty
= 1;
4727 options
->flags
.relative_name
= diff_relative
;
4728 options
->objfind
= NULL
;
4730 /* pathchange left =NULL by default */
4731 options
->change
= diff_change
;
4732 options
->add_remove
= diff_addremove
;
4733 options
->use_color
= diff_use_color_default
;
4734 options
->detect_rename
= diff_detect_rename_default
;
4735 options
->xdl_opts
|= diff_algorithm
;
4736 if (diff_indent_heuristic
)
4737 DIFF_XDL_SET(options
, INDENT_HEURISTIC
);
4739 options
->orderfile
= diff_order_file_cfg
;
4741 if (!options
->flags
.ignore_submodule_set
)
4742 options
->flags
.ignore_untracked_in_submodules
= 1;
4744 if (diff_no_prefix
) {
4745 diff_set_noprefix(options
);
4746 } else if (!diff_mnemonic_prefix
) {
4747 diff_set_default_prefix(options
);
4750 options
->color_moved
= diff_color_moved_default
;
4751 options
->color_moved_ws_handling
= diff_color_moved_ws_default
;
4754 static const char diff_status_letters
[] = {
4757 DIFF_STATUS_DELETED
,
4758 DIFF_STATUS_MODIFIED
,
4759 DIFF_STATUS_RENAMED
,
4760 DIFF_STATUS_TYPE_CHANGED
,
4761 DIFF_STATUS_UNKNOWN
,
4762 DIFF_STATUS_UNMERGED
,
4763 DIFF_STATUS_FILTER_AON
,
4764 DIFF_STATUS_FILTER_BROKEN
,
4768 static unsigned int filter_bit
['Z' + 1];
4770 static void prepare_filter_bits(void)
4774 if (!filter_bit
[DIFF_STATUS_ADDED
]) {
4775 for (i
= 0; diff_status_letters
[i
]; i
++)
4776 filter_bit
[(int) diff_status_letters
[i
]] = (1 << i
);
4780 static unsigned filter_bit_tst(char status
, const struct diff_options
*opt
)
4782 return opt
->filter
& filter_bit
[(int) status
];
4785 unsigned diff_filter_bit(char status
)
4787 prepare_filter_bits();
4788 return filter_bit
[(int) status
];
4791 int diff_check_follow_pathspec(struct pathspec
*ps
, int die_on_error
)
4793 unsigned forbidden_magic
;
4797 die(_("--follow requires exactly one pathspec"));
4801 forbidden_magic
= ps
->items
[0].magic
& ~(PATHSPEC_FROMTOP
|
4803 if (forbidden_magic
) {
4805 struct strbuf sb
= STRBUF_INIT
;
4806 pathspec_magic_names(forbidden_magic
, &sb
);
4807 die(_("pathspec magic not supported by --follow: %s"),
4816 void diff_setup_done(struct diff_options
*options
)
4818 unsigned check_mask
= DIFF_FORMAT_NAME
|
4819 DIFF_FORMAT_NAME_STATUS
|
4820 DIFF_FORMAT_CHECKDIFF
|
4821 DIFF_FORMAT_NO_OUTPUT
;
4823 * This must be signed because we're comparing against a potentially
4826 const int hexsz
= the_hash_algo
->hexsz
;
4828 if (options
->set_default
)
4829 options
->set_default(options
);
4831 if (HAS_MULTI_BITS(options
->output_format
& check_mask
))
4832 die(_("options '%s', '%s', '%s', and '%s' cannot be used together"),
4833 "--name-only", "--name-status", "--check", "-s");
4835 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
4836 die(_("options '%s', '%s', and '%s' cannot be used together"),
4837 "-G", "-S", "--find-object");
4839 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_G_REGEX_MASK
))
4840 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s'"),
4841 "-G", "--pickaxe-regex", "--pickaxe-regex", "-S");
4843 if (HAS_MULTI_BITS(options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_ALL_OBJFIND_MASK
))
4844 die(_("options '%s' and '%s' cannot be used together, use '%s' with '%s' and '%s'"),
4845 "--pickaxe-all", "--find-object", "--pickaxe-all", "-G", "-S");
4848 * Most of the time we can say "there are changes"
4849 * only by checking if there are changed paths, but
4850 * --ignore-whitespace* options force us to look
4854 if ((options
->xdl_opts
& XDF_WHITESPACE_FLAGS
) ||
4855 options
->ignore_regex_nr
)
4856 options
->flags
.diff_from_contents
= 1;
4858 options
->flags
.diff_from_contents
= 0;
4860 if (options
->flags
.find_copies_harder
)
4861 options
->detect_rename
= DIFF_DETECT_COPY
;
4863 if (!options
->flags
.relative_name
)
4864 options
->prefix
= NULL
;
4865 if (options
->prefix
)
4866 options
->prefix_length
= strlen(options
->prefix
);
4868 options
->prefix_length
= 0;
4871 * --name-only, --name-status, --checkdiff, and -s
4872 * turn other output format off.
4874 if (options
->output_format
& (DIFF_FORMAT_NAME
|
4875 DIFF_FORMAT_NAME_STATUS
|
4876 DIFF_FORMAT_CHECKDIFF
|
4877 DIFF_FORMAT_NO_OUTPUT
))
4878 options
->output_format
&= ~(DIFF_FORMAT_RAW
|
4879 DIFF_FORMAT_NUMSTAT
|
4880 DIFF_FORMAT_DIFFSTAT
|
4881 DIFF_FORMAT_SHORTSTAT
|
4882 DIFF_FORMAT_DIRSTAT
|
4883 DIFF_FORMAT_SUMMARY
|
4887 * These cases always need recursive; we do not drop caller-supplied
4888 * recursive bits for other formats here.
4890 if (options
->output_format
& (DIFF_FORMAT_PATCH
|
4891 DIFF_FORMAT_NUMSTAT
|
4892 DIFF_FORMAT_DIFFSTAT
|
4893 DIFF_FORMAT_SHORTSTAT
|
4894 DIFF_FORMAT_DIRSTAT
|
4895 DIFF_FORMAT_SUMMARY
|
4896 DIFF_FORMAT_CHECKDIFF
))
4897 options
->flags
.recursive
= 1;
4899 * Also pickaxe would not work very well if you do not say recursive
4901 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
4902 options
->flags
.recursive
= 1;
4904 * When patches are generated, submodules diffed against the work tree
4905 * must be checked for dirtiness too so it can be shown in the output
4907 if (options
->output_format
& DIFF_FORMAT_PATCH
)
4908 options
->flags
.dirty_submodules
= 1;
4910 if (options
->detect_rename
&& options
->rename_limit
< 0)
4911 options
->rename_limit
= diff_rename_limit_default
;
4912 if (hexsz
< options
->abbrev
)
4913 options
->abbrev
= hexsz
; /* full */
4916 * It does not make sense to show the first hit we happened
4917 * to have found. It does not make sense not to return with
4918 * exit code in such a case either.
4920 if (options
->flags
.quick
) {
4921 options
->output_format
= DIFF_FORMAT_NO_OUTPUT
;
4922 options
->flags
.exit_with_status
= 1;
4925 options
->diff_path_counter
= 0;
4927 if (options
->flags
.follow_renames
)
4928 diff_check_follow_pathspec(&options
->pathspec
, 1);
4930 if (!options
->use_color
|| external_diff())
4931 options
->color_moved
= 0;
4933 if (options
->filter_not
) {
4934 if (!options
->filter
)
4935 options
->filter
= ~filter_bit
[DIFF_STATUS_FILTER_AON
];
4936 options
->filter
&= ~options
->filter_not
;
4940 int parse_long_opt(const char *opt
, const char **argv
,
4941 const char **optarg
)
4943 const char *arg
= argv
[0];
4944 if (!skip_prefix(arg
, "--", &arg
))
4946 if (!skip_prefix(arg
, opt
, &arg
))
4948 if (*arg
== '=') { /* stuck form: --option=value */
4954 /* separate form: --option value */
4956 die("Option '--%s' requires a value", opt
);
4961 static int diff_opt_stat(const struct option
*opt
, const char *value
, int unset
)
4963 struct diff_options
*options
= opt
->value
;
4964 int width
= options
->stat_width
;
4965 int name_width
= options
->stat_name_width
;
4966 int graph_width
= options
->stat_graph_width
;
4967 int count
= options
->stat_count
;
4970 BUG_ON_OPT_NEG(unset
);
4972 if (!strcmp(opt
->long_name
, "stat")) {
4974 width
= strtoul(value
, &end
, 10);
4976 name_width
= strtoul(end
+1, &end
, 10);
4978 count
= strtoul(end
+1, &end
, 10);
4980 return error(_("invalid --stat value: %s"), value
);
4982 } else if (!strcmp(opt
->long_name
, "stat-width")) {
4983 width
= strtoul(value
, &end
, 10);
4985 return error(_("%s expects a numerical value"),
4987 } else if (!strcmp(opt
->long_name
, "stat-name-width")) {
4988 name_width
= strtoul(value
, &end
, 10);
4990 return error(_("%s expects a numerical value"),
4992 } else if (!strcmp(opt
->long_name
, "stat-graph-width")) {
4993 graph_width
= strtoul(value
, &end
, 10);
4995 return error(_("%s expects a numerical value"),
4997 } else if (!strcmp(opt
->long_name
, "stat-count")) {
4998 count
= strtoul(value
, &end
, 10);
5000 return error(_("%s expects a numerical value"),
5003 BUG("%s should not get here", opt
->long_name
);
5005 options
->output_format
&= ~DIFF_FORMAT_NO_OUTPUT
;
5006 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
5007 options
->stat_name_width
= name_width
;
5008 options
->stat_graph_width
= graph_width
;
5009 options
->stat_width
= width
;
5010 options
->stat_count
= count
;
5014 static int parse_dirstat_opt(struct diff_options
*options
, const char *params
)
5016 struct strbuf errmsg
= STRBUF_INIT
;
5017 if (parse_dirstat_params(options
, params
, &errmsg
))
5018 die(_("Failed to parse --dirstat/-X option parameter:\n%s"),
5020 strbuf_release(&errmsg
);
5022 * The caller knows a dirstat-related option is given from the command
5023 * line; allow it to say "return this_function();"
5025 options
->output_format
&= ~DIFF_FORMAT_NO_OUTPUT
;
5026 options
->output_format
|= DIFF_FORMAT_DIRSTAT
;
5030 static int diff_opt_diff_filter(const struct option
*option
,
5031 const char *optarg
, int unset
)
5033 struct diff_options
*opt
= option
->value
;
5036 BUG_ON_OPT_NEG(unset
);
5037 prepare_filter_bits();
5039 for (i
= 0; (optch
= optarg
[i
]) != '\0'; i
++) {
5043 if ('a' <= optch
&& optch
<= 'z') {
5045 optch
= toupper(optch
);
5050 bit
= (0 <= optch
&& optch
<= 'Z') ? filter_bit
[optch
] : 0;
5052 return error(_("unknown change class '%c' in --diff-filter=%s"),
5055 opt
->filter_not
|= bit
;
5062 static void enable_patch_output(int *fmt
)
5064 *fmt
&= ~DIFF_FORMAT_NO_OUTPUT
;
5065 *fmt
|= DIFF_FORMAT_PATCH
;
5068 static int diff_opt_ws_error_highlight(const struct option
*option
,
5069 const char *arg
, int unset
)
5071 struct diff_options
*opt
= option
->value
;
5072 int val
= parse_ws_error_highlight(arg
);
5074 BUG_ON_OPT_NEG(unset
);
5076 return error(_("unknown value after ws-error-highlight=%.*s"),
5078 opt
->ws_error_highlight
= val
;
5082 static int diff_opt_find_object(const struct option
*option
,
5083 const char *arg
, int unset
)
5085 struct diff_options
*opt
= option
->value
;
5086 struct object_id oid
;
5088 BUG_ON_OPT_NEG(unset
);
5089 if (repo_get_oid(the_repository
, arg
, &oid
))
5090 return error(_("unable to resolve '%s'"), arg
);
5093 CALLOC_ARRAY(opt
->objfind
, 1);
5095 opt
->pickaxe_opts
|= DIFF_PICKAXE_KIND_OBJFIND
;
5096 opt
->flags
.recursive
= 1;
5097 opt
->flags
.tree_in_recursive
= 1;
5098 oidset_insert(opt
->objfind
, &oid
);
5102 static int diff_opt_anchored(const struct option
*opt
,
5103 const char *arg
, int unset
)
5105 struct diff_options
*options
= opt
->value
;
5107 BUG_ON_OPT_NEG(unset
);
5108 options
->xdl_opts
= DIFF_WITH_ALG(options
, PATIENCE_DIFF
);
5109 ALLOC_GROW(options
->anchors
, options
->anchors_nr
+ 1,
5110 options
->anchors_alloc
);
5111 options
->anchors
[options
->anchors_nr
++] = xstrdup(arg
);
5115 static int diff_opt_binary(const struct option
*opt
,
5116 const char *arg
, int unset
)
5118 struct diff_options
*options
= opt
->value
;
5120 BUG_ON_OPT_NEG(unset
);
5121 BUG_ON_OPT_ARG(arg
);
5122 enable_patch_output(&options
->output_format
);
5123 options
->flags
.binary
= 1;
5127 static int diff_opt_break_rewrites(const struct option
*opt
,
5128 const char *arg
, int unset
)
5130 int *break_opt
= opt
->value
;
5133 BUG_ON_OPT_NEG(unset
);
5136 opt1
= parse_rename_score(&arg
);
5139 else if (*arg
!= '/')
5140 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5143 opt2
= parse_rename_score(&arg
);
5146 return error(_("%s expects <n>/<m> form"), opt
->long_name
);
5147 *break_opt
= opt1
| (opt2
<< 16);
5151 static int diff_opt_char(const struct option
*opt
,
5152 const char *arg
, int unset
)
5154 char *value
= opt
->value
;
5156 BUG_ON_OPT_NEG(unset
);
5158 return error(_("%s expects a character, got '%s'"),
5159 opt
->long_name
, arg
);
5164 static int diff_opt_color_moved(const struct option
*opt
,
5165 const char *arg
, int unset
)
5167 struct diff_options
*options
= opt
->value
;
5170 options
->color_moved
= COLOR_MOVED_NO
;
5172 if (diff_color_moved_default
)
5173 options
->color_moved
= diff_color_moved_default
;
5174 if (options
->color_moved
== COLOR_MOVED_NO
)
5175 options
->color_moved
= COLOR_MOVED_DEFAULT
;
5177 int cm
= parse_color_moved(arg
);
5179 return error(_("bad --color-moved argument: %s"), arg
);
5180 options
->color_moved
= cm
;
5185 static int diff_opt_color_moved_ws(const struct option
*opt
,
5186 const char *arg
, int unset
)
5188 struct diff_options
*options
= opt
->value
;
5192 options
->color_moved_ws_handling
= 0;
5196 cm
= parse_color_moved_ws(arg
);
5197 if (cm
& COLOR_MOVED_WS_ERROR
)
5198 return error(_("invalid mode '%s' in --color-moved-ws"), arg
);
5199 options
->color_moved_ws_handling
= cm
;
5203 static int diff_opt_color_words(const struct option
*opt
,
5204 const char *arg
, int unset
)
5206 struct diff_options
*options
= opt
->value
;
5208 BUG_ON_OPT_NEG(unset
);
5209 options
->use_color
= 1;
5210 options
->word_diff
= DIFF_WORDS_COLOR
;
5211 options
->word_regex
= arg
;
5215 static int diff_opt_compact_summary(const struct option
*opt
,
5216 const char *arg
, int unset
)
5218 struct diff_options
*options
= opt
->value
;
5220 BUG_ON_OPT_ARG(arg
);
5222 options
->flags
.stat_with_summary
= 0;
5224 options
->flags
.stat_with_summary
= 1;
5225 options
->output_format
&= ~DIFF_FORMAT_NO_OUTPUT
;
5226 options
->output_format
|= DIFF_FORMAT_DIFFSTAT
;
5231 static int diff_opt_diff_algorithm(const struct option
*opt
,
5232 const char *arg
, int unset
)
5234 struct diff_options
*options
= opt
->value
;
5236 BUG_ON_OPT_NEG(unset
);
5238 if (set_diff_algorithm(options
, arg
))
5239 return error(_("option diff-algorithm accepts \"myers\", "
5240 "\"minimal\", \"patience\" and \"histogram\""));
5242 options
->ignore_driver_algorithm
= 1;
5247 static int diff_opt_diff_algorithm_no_arg(const struct option
*opt
,
5248 const char *arg
, int unset
)
5250 struct diff_options
*options
= opt
->value
;
5252 BUG_ON_OPT_NEG(unset
);
5253 BUG_ON_OPT_ARG(arg
);
5255 if (set_diff_algorithm(options
, opt
->long_name
))
5256 BUG("available diff algorithms include \"myers\", "
5257 "\"minimal\", \"patience\" and \"histogram\"");
5259 options
->ignore_driver_algorithm
= 1;
5264 static int diff_opt_dirstat(const struct option
*opt
,
5265 const char *arg
, int unset
)
5267 struct diff_options
*options
= opt
->value
;
5269 BUG_ON_OPT_NEG(unset
);
5270 if (!strcmp(opt
->long_name
, "cumulative")) {
5272 BUG("how come --cumulative take a value?");
5274 } else if (!strcmp(opt
->long_name
, "dirstat-by-file"))
5275 parse_dirstat_opt(options
, "files");
5276 parse_dirstat_opt(options
, arg
? arg
: "");
5280 static int diff_opt_find_copies(const struct option
*opt
,
5281 const char *arg
, int unset
)
5283 struct diff_options
*options
= opt
->value
;
5285 BUG_ON_OPT_NEG(unset
);
5288 options
->rename_score
= parse_rename_score(&arg
);
5290 return error(_("invalid argument to %s"), opt
->long_name
);
5292 if (options
->detect_rename
== DIFF_DETECT_COPY
)
5293 options
->flags
.find_copies_harder
= 1;
5295 options
->detect_rename
= DIFF_DETECT_COPY
;
5300 static int diff_opt_find_renames(const struct option
*opt
,
5301 const char *arg
, int unset
)
5303 struct diff_options
*options
= opt
->value
;
5305 BUG_ON_OPT_NEG(unset
);
5308 options
->rename_score
= parse_rename_score(&arg
);
5310 return error(_("invalid argument to %s"), opt
->long_name
);
5312 options
->detect_rename
= DIFF_DETECT_RENAME
;
5316 static int diff_opt_follow(const struct option
*opt
,
5317 const char *arg
, int unset
)
5319 struct diff_options
*options
= opt
->value
;
5321 BUG_ON_OPT_ARG(arg
);
5323 options
->flags
.follow_renames
= 0;
5324 options
->flags
.default_follow_renames
= 0;
5326 options
->flags
.follow_renames
= 1;
5331 static int diff_opt_ignore_submodules(const struct option
*opt
,
5332 const char *arg
, int unset
)
5334 struct diff_options
*options
= opt
->value
;
5336 BUG_ON_OPT_NEG(unset
);
5339 options
->flags
.override_submodule_config
= 1;
5340 handle_ignore_submodules_arg(options
, arg
);
5344 static int diff_opt_line_prefix(const struct option
*opt
,
5345 const char *optarg
, int unset
)
5347 struct diff_options
*options
= opt
->value
;
5349 BUG_ON_OPT_NEG(unset
);
5350 options
->line_prefix
= optarg
;
5351 options
->line_prefix_length
= strlen(options
->line_prefix
);
5352 graph_setup_line_prefix(options
);
5356 static int diff_opt_no_prefix(const struct option
*opt
,
5357 const char *optarg
, int unset
)
5359 struct diff_options
*options
= opt
->value
;
5361 BUG_ON_OPT_NEG(unset
);
5362 BUG_ON_OPT_ARG(optarg
);
5363 diff_set_noprefix(options
);
5367 static int diff_opt_default_prefix(const struct option
*opt
,
5368 const char *optarg
, int unset
)
5370 struct diff_options
*options
= opt
->value
;
5372 BUG_ON_OPT_NEG(unset
);
5373 BUG_ON_OPT_ARG(optarg
);
5374 diff_src_prefix
= "a/";
5375 diff_dst_prefix
= "b/";
5376 diff_set_default_prefix(options
);
5380 static enum parse_opt_result
diff_opt_output(struct parse_opt_ctx_t
*ctx
,
5381 const struct option
*opt
,
5382 const char *arg
, int unset
)
5384 struct diff_options
*options
= opt
->value
;
5387 BUG_ON_OPT_NEG(unset
);
5388 path
= prefix_filename(ctx
->prefix
, arg
);
5389 options
->file
= xfopen(path
, "w");
5390 options
->close_file
= 1;
5391 if (options
->use_color
!= GIT_COLOR_ALWAYS
)
5392 options
->use_color
= GIT_COLOR_NEVER
;
5397 static int diff_opt_patience(const struct option
*opt
,
5398 const char *arg
, int unset
)
5400 struct diff_options
*options
= opt
->value
;
5403 BUG_ON_OPT_NEG(unset
);
5404 BUG_ON_OPT_ARG(arg
);
5406 * Both --patience and --anchored use PATIENCE_DIFF
5407 * internally, so remove any anchors previously
5410 for (i
= 0; i
< options
->anchors_nr
; i
++)
5411 free(options
->anchors
[i
]);
5412 options
->anchors_nr
= 0;
5413 options
->ignore_driver_algorithm
= 1;
5415 return set_diff_algorithm(options
, "patience");
5418 static int diff_opt_ignore_regex(const struct option
*opt
,
5419 const char *arg
, int unset
)
5421 struct diff_options
*options
= opt
->value
;
5424 BUG_ON_OPT_NEG(unset
);
5425 regex
= xmalloc(sizeof(*regex
));
5426 if (regcomp(regex
, arg
, REG_EXTENDED
| REG_NEWLINE
))
5427 return error(_("invalid regex given to -I: '%s'"), arg
);
5428 ALLOC_GROW(options
->ignore_regex
, options
->ignore_regex_nr
+ 1,
5429 options
->ignore_regex_alloc
);
5430 options
->ignore_regex
[options
->ignore_regex_nr
++] = regex
;
5434 static int diff_opt_pickaxe_regex(const struct option
*opt
,
5435 const char *arg
, int unset
)
5437 struct diff_options
*options
= opt
->value
;
5439 BUG_ON_OPT_NEG(unset
);
5440 options
->pickaxe
= arg
;
5441 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_G
;
5445 static int diff_opt_pickaxe_string(const struct option
*opt
,
5446 const char *arg
, int unset
)
5448 struct diff_options
*options
= opt
->value
;
5450 BUG_ON_OPT_NEG(unset
);
5451 options
->pickaxe
= arg
;
5452 options
->pickaxe_opts
|= DIFF_PICKAXE_KIND_S
;
5456 static int diff_opt_relative(const struct option
*opt
,
5457 const char *arg
, int unset
)
5459 struct diff_options
*options
= opt
->value
;
5461 options
->flags
.relative_name
= !unset
;
5463 options
->prefix
= arg
;
5467 static int diff_opt_submodule(const struct option
*opt
,
5468 const char *arg
, int unset
)
5470 struct diff_options
*options
= opt
->value
;
5472 BUG_ON_OPT_NEG(unset
);
5475 if (parse_submodule_params(options
, arg
))
5476 return error(_("failed to parse --submodule option parameter: '%s'"),
5481 static int diff_opt_textconv(const struct option
*opt
,
5482 const char *arg
, int unset
)
5484 struct diff_options
*options
= opt
->value
;
5486 BUG_ON_OPT_ARG(arg
);
5488 options
->flags
.allow_textconv
= 0;
5490 options
->flags
.allow_textconv
= 1;
5491 options
->flags
.textconv_set_via_cmdline
= 1;
5496 static int diff_opt_unified(const struct option
*opt
,
5497 const char *arg
, int unset
)
5499 struct diff_options
*options
= opt
->value
;
5502 BUG_ON_OPT_NEG(unset
);
5505 options
->context
= strtol(arg
, &s
, 10);
5507 return error(_("%s expects a numerical value"), "--unified");
5509 enable_patch_output(&options
->output_format
);
5514 static int diff_opt_word_diff(const struct option
*opt
,
5515 const char *arg
, int unset
)
5517 struct diff_options
*options
= opt
->value
;
5519 BUG_ON_OPT_NEG(unset
);
5521 if (!strcmp(arg
, "plain"))
5522 options
->word_diff
= DIFF_WORDS_PLAIN
;
5523 else if (!strcmp(arg
, "color")) {
5524 options
->use_color
= 1;
5525 options
->word_diff
= DIFF_WORDS_COLOR
;
5527 else if (!strcmp(arg
, "porcelain"))
5528 options
->word_diff
= DIFF_WORDS_PORCELAIN
;
5529 else if (!strcmp(arg
, "none"))
5530 options
->word_diff
= DIFF_WORDS_NONE
;
5532 return error(_("bad --word-diff argument: %s"), arg
);
5534 if (options
->word_diff
== DIFF_WORDS_NONE
)
5535 options
->word_diff
= DIFF_WORDS_PLAIN
;
5540 static int diff_opt_word_diff_regex(const struct option
*opt
,
5541 const char *arg
, int unset
)
5543 struct diff_options
*options
= opt
->value
;
5545 BUG_ON_OPT_NEG(unset
);
5546 if (options
->word_diff
== DIFF_WORDS_NONE
)
5547 options
->word_diff
= DIFF_WORDS_PLAIN
;
5548 options
->word_regex
= arg
;
5552 static int diff_opt_rotate_to(const struct option
*opt
, const char *arg
, int unset
)
5554 struct diff_options
*options
= opt
->value
;
5556 BUG_ON_OPT_NEG(unset
);
5557 if (!strcmp(opt
->long_name
, "skip-to"))
5558 options
->skip_instead_of_rotate
= 1;
5560 options
->skip_instead_of_rotate
= 0;
5561 options
->rotate_to
= arg
;
5566 * Consider adding new flags to __git_diff_common_options
5567 * in contrib/completion/git-completion.bash
5569 struct option
*add_diff_options(const struct option
*opts
,
5570 struct diff_options
*options
)
5572 struct option parseopts
[] = {
5573 OPT_GROUP(N_("Diff output format options")),
5574 OPT_BITOP('p', "patch", &options
->output_format
,
5575 N_("generate patch"),
5576 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5577 OPT_SET_INT('s', "no-patch", &options
->output_format
,
5578 N_("suppress diff output"), DIFF_FORMAT_NO_OUTPUT
),
5579 OPT_BITOP('u', NULL
, &options
->output_format
,
5580 N_("generate patch"),
5581 DIFF_FORMAT_PATCH
, DIFF_FORMAT_NO_OUTPUT
),
5582 OPT_CALLBACK_F('U', "unified", options
, N_("<n>"),
5583 N_("generate diffs with <n> lines context"),
5584 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_unified
),
5585 OPT_BOOL('W', "function-context", &options
->flags
.funccontext
,
5586 N_("generate diffs with <n> lines context")),
5587 OPT_BITOP(0, "raw", &options
->output_format
,
5588 N_("generate the diff in raw format"),
5589 DIFF_FORMAT_RAW
, DIFF_FORMAT_NO_OUTPUT
),
5590 OPT_BITOP(0, "patch-with-raw", &options
->output_format
,
5591 N_("synonym for '-p --raw'"),
5592 DIFF_FORMAT_PATCH
| DIFF_FORMAT_RAW
,
5593 DIFF_FORMAT_NO_OUTPUT
),
5594 OPT_BITOP(0, "patch-with-stat", &options
->output_format
,
5595 N_("synonym for '-p --stat'"),
5596 DIFF_FORMAT_PATCH
| DIFF_FORMAT_DIFFSTAT
,
5597 DIFF_FORMAT_NO_OUTPUT
),
5598 OPT_BITOP(0, "numstat", &options
->output_format
,
5599 N_("machine friendly --stat"),
5600 DIFF_FORMAT_NUMSTAT
, DIFF_FORMAT_NO_OUTPUT
),
5601 OPT_BITOP(0, "shortstat", &options
->output_format
,
5602 N_("output only the last line of --stat"),
5603 DIFF_FORMAT_SHORTSTAT
, DIFF_FORMAT_NO_OUTPUT
),
5604 OPT_CALLBACK_F('X', "dirstat", options
, N_("<param1>,<param2>..."),
5605 N_("output the distribution of relative amount of changes for each sub-directory"),
5606 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5608 OPT_CALLBACK_F(0, "cumulative", options
, NULL
,
5609 N_("synonym for --dirstat=cumulative"),
5610 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5612 OPT_CALLBACK_F(0, "dirstat-by-file", options
, N_("<param1>,<param2>..."),
5613 N_("synonym for --dirstat=files,<param1>,<param2>..."),
5614 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5616 OPT_BIT_F(0, "check", &options
->output_format
,
5617 N_("warn if changes introduce conflict markers or whitespace errors"),
5618 DIFF_FORMAT_CHECKDIFF
, PARSE_OPT_NONEG
),
5619 OPT_BITOP(0, "summary", &options
->output_format
,
5620 N_("condensed summary such as creations, renames and mode changes"),
5621 DIFF_FORMAT_SUMMARY
, DIFF_FORMAT_NO_OUTPUT
),
5622 OPT_BIT_F(0, "name-only", &options
->output_format
,
5623 N_("show only names of changed files"),
5624 DIFF_FORMAT_NAME
, PARSE_OPT_NONEG
),
5625 OPT_BIT_F(0, "name-status", &options
->output_format
,
5626 N_("show only names and status of changed files"),
5627 DIFF_FORMAT_NAME_STATUS
, PARSE_OPT_NONEG
),
5628 OPT_CALLBACK_F(0, "stat", options
, N_("<width>[,<name-width>[,<count>]]"),
5629 N_("generate diffstat"),
5630 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_stat
),
5631 OPT_CALLBACK_F(0, "stat-width", options
, N_("<width>"),
5632 N_("generate diffstat with a given width"),
5633 PARSE_OPT_NONEG
, diff_opt_stat
),
5634 OPT_CALLBACK_F(0, "stat-name-width", options
, N_("<width>"),
5635 N_("generate diffstat with a given name width"),
5636 PARSE_OPT_NONEG
, diff_opt_stat
),
5637 OPT_CALLBACK_F(0, "stat-graph-width", options
, N_("<width>"),
5638 N_("generate diffstat with a given graph width"),
5639 PARSE_OPT_NONEG
, diff_opt_stat
),
5640 OPT_CALLBACK_F(0, "stat-count", options
, N_("<count>"),
5641 N_("generate diffstat with limited lines"),
5642 PARSE_OPT_NONEG
, diff_opt_stat
),
5643 OPT_CALLBACK_F(0, "compact-summary", options
, NULL
,
5644 N_("generate compact summary in diffstat"),
5645 PARSE_OPT_NOARG
, diff_opt_compact_summary
),
5646 OPT_CALLBACK_F(0, "binary", options
, NULL
,
5647 N_("output a binary diff that can be applied"),
5648 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_binary
),
5649 OPT_BOOL(0, "full-index", &options
->flags
.full_index
,
5650 N_("show full pre- and post-image object names on the \"index\" lines")),
5651 OPT_COLOR_FLAG(0, "color", &options
->use_color
,
5652 N_("show colored diff")),
5653 OPT_CALLBACK_F(0, "ws-error-highlight", options
, N_("<kind>"),
5654 N_("highlight whitespace errors in the 'context', 'old' or 'new' lines in the diff"),
5655 PARSE_OPT_NONEG
, diff_opt_ws_error_highlight
),
5656 OPT_SET_INT('z', NULL
, &options
->line_termination
,
5657 N_("do not munge pathnames and use NULs as output field terminators in --raw or --numstat"),
5659 OPT__ABBREV(&options
->abbrev
),
5660 OPT_STRING_F(0, "src-prefix", &options
->a_prefix
, N_("<prefix>"),
5661 N_("show the given source prefix instead of \"a/\""),
5663 OPT_STRING_F(0, "dst-prefix", &options
->b_prefix
, N_("<prefix>"),
5664 N_("show the given destination prefix instead of \"b/\""),
5666 OPT_CALLBACK_F(0, "line-prefix", options
, N_("<prefix>"),
5667 N_("prepend an additional prefix to every line of output"),
5668 PARSE_OPT_NONEG
, diff_opt_line_prefix
),
5669 OPT_CALLBACK_F(0, "no-prefix", options
, NULL
,
5670 N_("do not show any source or destination prefix"),
5671 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_no_prefix
),
5672 OPT_CALLBACK_F(0, "default-prefix", options
, NULL
,
5673 N_("use default prefixes a/ and b/"),
5674 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, diff_opt_default_prefix
),
5675 OPT_INTEGER_F(0, "inter-hunk-context", &options
->interhunkcontext
,
5676 N_("show context between diff hunks up to the specified number of lines"),
5678 OPT_CALLBACK_F(0, "output-indicator-new",
5679 &options
->output_indicators
[OUTPUT_INDICATOR_NEW
],
5681 N_("specify the character to indicate a new line instead of '+'"),
5682 PARSE_OPT_NONEG
, diff_opt_char
),
5683 OPT_CALLBACK_F(0, "output-indicator-old",
5684 &options
->output_indicators
[OUTPUT_INDICATOR_OLD
],
5686 N_("specify the character to indicate an old line instead of '-'"),
5687 PARSE_OPT_NONEG
, diff_opt_char
),
5688 OPT_CALLBACK_F(0, "output-indicator-context",
5689 &options
->output_indicators
[OUTPUT_INDICATOR_CONTEXT
],
5691 N_("specify the character to indicate a context instead of ' '"),
5692 PARSE_OPT_NONEG
, diff_opt_char
),
5694 OPT_GROUP(N_("Diff rename options")),
5695 OPT_CALLBACK_F('B', "break-rewrites", &options
->break_opt
, N_("<n>[/<m>]"),
5696 N_("break complete rewrite changes into pairs of delete and create"),
5697 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5698 diff_opt_break_rewrites
),
5699 OPT_CALLBACK_F('M', "find-renames", options
, N_("<n>"),
5700 N_("detect renames"),
5701 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5702 diff_opt_find_renames
),
5703 OPT_SET_INT_F('D', "irreversible-delete", &options
->irreversible_delete
,
5704 N_("omit the preimage for deletes"),
5705 1, PARSE_OPT_NONEG
),
5706 OPT_CALLBACK_F('C', "find-copies", options
, N_("<n>"),
5707 N_("detect copies"),
5708 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5709 diff_opt_find_copies
),
5710 OPT_BOOL(0, "find-copies-harder", &options
->flags
.find_copies_harder
,
5711 N_("use unmodified files as source to find copies")),
5712 OPT_SET_INT_F(0, "no-renames", &options
->detect_rename
,
5713 N_("disable rename detection"),
5714 0, PARSE_OPT_NONEG
),
5715 OPT_BOOL(0, "rename-empty", &options
->flags
.rename_empty
,
5716 N_("use empty blobs as rename source")),
5717 OPT_CALLBACK_F(0, "follow", options
, NULL
,
5718 N_("continue listing the history of a file beyond renames"),
5719 PARSE_OPT_NOARG
, diff_opt_follow
),
5720 OPT_INTEGER('l', NULL
, &options
->rename_limit
,
5721 N_("prevent rename/copy detection if the number of rename/copy targets exceeds given limit")),
5723 OPT_GROUP(N_("Diff algorithm options")),
5724 OPT_CALLBACK_F(0, "minimal", options
, NULL
,
5725 N_("produce the smallest possible diff"),
5726 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5727 diff_opt_diff_algorithm_no_arg
),
5728 OPT_BIT_F('w', "ignore-all-space", &options
->xdl_opts
,
5729 N_("ignore whitespace when comparing lines"),
5730 XDF_IGNORE_WHITESPACE
, PARSE_OPT_NONEG
),
5731 OPT_BIT_F('b', "ignore-space-change", &options
->xdl_opts
,
5732 N_("ignore changes in amount of whitespace"),
5733 XDF_IGNORE_WHITESPACE_CHANGE
, PARSE_OPT_NONEG
),
5734 OPT_BIT_F(0, "ignore-space-at-eol", &options
->xdl_opts
,
5735 N_("ignore changes in whitespace at EOL"),
5736 XDF_IGNORE_WHITESPACE_AT_EOL
, PARSE_OPT_NONEG
),
5737 OPT_BIT_F(0, "ignore-cr-at-eol", &options
->xdl_opts
,
5738 N_("ignore carrier-return at the end of line"),
5739 XDF_IGNORE_CR_AT_EOL
, PARSE_OPT_NONEG
),
5740 OPT_BIT_F(0, "ignore-blank-lines", &options
->xdl_opts
,
5741 N_("ignore changes whose lines are all blank"),
5742 XDF_IGNORE_BLANK_LINES
, PARSE_OPT_NONEG
),
5743 OPT_CALLBACK_F('I', "ignore-matching-lines", options
, N_("<regex>"),
5744 N_("ignore changes whose all lines match <regex>"),
5745 0, diff_opt_ignore_regex
),
5746 OPT_BIT(0, "indent-heuristic", &options
->xdl_opts
,
5747 N_("heuristic to shift diff hunk boundaries for easy reading"),
5748 XDF_INDENT_HEURISTIC
),
5749 OPT_CALLBACK_F(0, "patience", options
, NULL
,
5750 N_("generate diff using the \"patience diff\" algorithm"),
5751 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5753 OPT_CALLBACK_F(0, "histogram", options
, NULL
,
5754 N_("generate diff using the \"histogram diff\" algorithm"),
5755 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
,
5756 diff_opt_diff_algorithm_no_arg
),
5757 OPT_CALLBACK_F(0, "diff-algorithm", options
, N_("<algorithm>"),
5758 N_("choose a diff algorithm"),
5759 PARSE_OPT_NONEG
, diff_opt_diff_algorithm
),
5760 OPT_CALLBACK_F(0, "anchored", options
, N_("<text>"),
5761 N_("generate diff using the \"anchored diff\" algorithm"),
5762 PARSE_OPT_NONEG
, diff_opt_anchored
),
5763 OPT_CALLBACK_F(0, "word-diff", options
, N_("<mode>"),
5764 N_("show word diff, using <mode> to delimit changed words"),
5765 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_word_diff
),
5766 OPT_CALLBACK_F(0, "word-diff-regex", options
, N_("<regex>"),
5767 N_("use <regex> to decide what a word is"),
5768 PARSE_OPT_NONEG
, diff_opt_word_diff_regex
),
5769 OPT_CALLBACK_F(0, "color-words", options
, N_("<regex>"),
5770 N_("equivalent to --word-diff=color --word-diff-regex=<regex>"),
5771 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
, diff_opt_color_words
),
5772 OPT_CALLBACK_F(0, "color-moved", options
, N_("<mode>"),
5773 N_("moved lines of code are colored differently"),
5774 PARSE_OPT_OPTARG
, diff_opt_color_moved
),
5775 OPT_CALLBACK_F(0, "color-moved-ws", options
, N_("<mode>"),
5776 N_("how white spaces are ignored in --color-moved"),
5777 0, diff_opt_color_moved_ws
),
5779 OPT_GROUP(N_("Other diff options")),
5780 OPT_CALLBACK_F(0, "relative", options
, N_("<prefix>"),
5781 N_("when run from subdir, exclude changes outside and show relative paths"),
5784 OPT_BOOL('a', "text", &options
->flags
.text
,
5785 N_("treat all files as text")),
5786 OPT_BOOL('R', NULL
, &options
->flags
.reverse_diff
,
5787 N_("swap two inputs, reverse the diff")),
5788 OPT_BOOL(0, "exit-code", &options
->flags
.exit_with_status
,
5789 N_("exit with 1 if there were differences, 0 otherwise")),
5790 OPT_BOOL(0, "quiet", &options
->flags
.quick
,
5791 N_("disable all output of the program")),
5792 OPT_BOOL(0, "ext-diff", &options
->flags
.allow_external
,
5793 N_("allow an external diff helper to be executed")),
5794 OPT_CALLBACK_F(0, "textconv", options
, NULL
,
5795 N_("run external text conversion filters when comparing binary files"),
5796 PARSE_OPT_NOARG
, diff_opt_textconv
),
5797 OPT_CALLBACK_F(0, "ignore-submodules", options
, N_("<when>"),
5798 N_("ignore changes to submodules in the diff generation"),
5799 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5800 diff_opt_ignore_submodules
),
5801 OPT_CALLBACK_F(0, "submodule", options
, N_("<format>"),
5802 N_("specify how differences in submodules are shown"),
5803 PARSE_OPT_NONEG
| PARSE_OPT_OPTARG
,
5804 diff_opt_submodule
),
5805 OPT_SET_INT_F(0, "ita-invisible-in-index", &options
->ita_invisible_in_index
,
5806 N_("hide 'git add -N' entries from the index"),
5807 1, PARSE_OPT_NONEG
),
5808 OPT_SET_INT_F(0, "ita-visible-in-index", &options
->ita_invisible_in_index
,
5809 N_("treat 'git add -N' entries as real in the index"),
5810 0, PARSE_OPT_NONEG
),
5811 OPT_CALLBACK_F('S', NULL
, options
, N_("<string>"),
5812 N_("look for differences that change the number of occurrences of the specified string"),
5813 0, diff_opt_pickaxe_string
),
5814 OPT_CALLBACK_F('G', NULL
, options
, N_("<regex>"),
5815 N_("look for differences that change the number of occurrences of the specified regex"),
5816 0, diff_opt_pickaxe_regex
),
5817 OPT_BIT_F(0, "pickaxe-all", &options
->pickaxe_opts
,
5818 N_("show all changes in the changeset with -S or -G"),
5819 DIFF_PICKAXE_ALL
, PARSE_OPT_NONEG
),
5820 OPT_BIT_F(0, "pickaxe-regex", &options
->pickaxe_opts
,
5821 N_("treat <string> in -S as extended POSIX regular expression"),
5822 DIFF_PICKAXE_REGEX
, PARSE_OPT_NONEG
),
5823 OPT_FILENAME('O', NULL
, &options
->orderfile
,
5824 N_("control the order in which files appear in the output")),
5825 OPT_CALLBACK_F(0, "rotate-to", options
, N_("<path>"),
5826 N_("show the change in the specified path first"),
5827 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5828 OPT_CALLBACK_F(0, "skip-to", options
, N_("<path>"),
5829 N_("skip the output to the specified path"),
5830 PARSE_OPT_NONEG
, diff_opt_rotate_to
),
5831 OPT_CALLBACK_F(0, "find-object", options
, N_("<object-id>"),
5832 N_("look for differences that change the number of occurrences of the specified object"),
5833 PARSE_OPT_NONEG
, diff_opt_find_object
),
5834 OPT_CALLBACK_F(0, "diff-filter", options
, N_("[(A|C|D|M|R|T|U|X|B)...[*]]"),
5835 N_("select files by diff type"),
5836 PARSE_OPT_NONEG
, diff_opt_diff_filter
),
5837 { OPTION_CALLBACK
, 0, "output", options
, N_("<file>"),
5838 N_("output to a specific file"),
5839 PARSE_OPT_NONEG
, NULL
, 0, diff_opt_output
},
5844 return parse_options_concat(opts
, parseopts
);
5847 int diff_opt_parse(struct diff_options
*options
,
5848 const char **av
, int ac
, const char *prefix
)
5850 struct option no_options
[] = { OPT_END() };
5851 struct option
*parseopts
= add_diff_options(no_options
, options
);
5856 ac
= parse_options(ac
, av
, prefix
, parseopts
, NULL
,
5857 PARSE_OPT_KEEP_DASHDASH
|
5858 PARSE_OPT_KEEP_UNKNOWN_OPT
|
5859 PARSE_OPT_NO_INTERNAL_HELP
|
5860 PARSE_OPT_ONE_SHOT
|
5861 PARSE_OPT_STOP_AT_NON_OPTION
);
5867 int parse_rename_score(const char **cp_p
)
5869 unsigned long num
, scale
;
5871 const char *cp
= *cp_p
;
5878 if ( !dot
&& ch
== '.' ) {
5881 } else if ( ch
== '%' ) {
5882 scale
= dot
? scale
*100 : 100;
5883 cp
++; /* % is always at the end */
5885 } else if ( ch
>= '0' && ch
<= '9' ) {
5886 if ( scale
< 100000 ) {
5888 num
= (num
*10) + (ch
-'0');
5897 /* user says num divided by scale and we say internally that
5898 * is MAX_SCORE * num / scale.
5900 return (int)((num
>= scale
) ? MAX_SCORE
: (MAX_SCORE
* num
/ scale
));
5903 struct diff_queue_struct diff_queued_diff
;
5905 void diff_q(struct diff_queue_struct
*queue
, struct diff_filepair
*dp
)
5907 ALLOC_GROW(queue
->queue
, queue
->nr
+ 1, queue
->alloc
);
5908 queue
->queue
[queue
->nr
++] = dp
;
5911 struct diff_filepair
*diff_queue(struct diff_queue_struct
*queue
,
5912 struct diff_filespec
*one
,
5913 struct diff_filespec
*two
)
5915 struct diff_filepair
*dp
= xcalloc(1, sizeof(*dp
));
5923 void diff_free_filepair(struct diff_filepair
*p
)
5925 free_filespec(p
->one
);
5926 free_filespec(p
->two
);
5930 void diff_free_queue(struct diff_queue_struct
*q
)
5932 for (int i
= 0; i
< q
->nr
; i
++)
5933 diff_free_filepair(q
->queue
[i
]);
5937 const char *diff_aligned_abbrev(const struct object_id
*oid
, int len
)
5942 /* Do we want all 40 hex characters? */
5943 if (len
== the_hash_algo
->hexsz
)
5944 return oid_to_hex(oid
);
5946 /* An abbreviated value is fine, possibly followed by an ellipsis. */
5947 abbrev
= diff_abbrev_oid(oid
, len
);
5949 if (!print_sha1_ellipsis())
5952 abblen
= strlen(abbrev
);
5955 * In well-behaved cases, where the abbreviated result is the
5956 * same as the requested length, append three dots after the
5957 * abbreviation (hence the whole logic is limited to the case
5958 * where abblen < 37); when the actual abbreviated result is a
5959 * bit longer than the requested length, we reduce the number
5960 * of dots so that they match the well-behaved ones. However,
5961 * if the actual abbreviation is longer than the requested
5962 * length by more than three, we give up on aligning, and add
5963 * three dots anyway, to indicate that the output is not the
5964 * full object name. Yes, this may be suboptimal, but this
5965 * appears only in "diff --raw --abbrev" output and it is not
5966 * worth the effort to change it now. Note that this would
5967 * likely to work fine when the automatic sizing of default
5968 * abbreviation length is used--we would be fed -1 in "len" in
5969 * that case, and will end up always appending three-dots, but
5970 * the automatic sizing is supposed to give abblen that ensures
5971 * uniqueness across all objects (statistically speaking).
5973 if (abblen
< the_hash_algo
->hexsz
- 3) {
5974 static char hex
[GIT_MAX_HEXSZ
+ 1];
5975 if (len
< abblen
&& abblen
<= len
+ 2)
5976 xsnprintf(hex
, sizeof(hex
), "%s%.*s", abbrev
, len
+3-abblen
, "..");
5978 xsnprintf(hex
, sizeof(hex
), "%s...", abbrev
);
5982 return oid_to_hex(oid
);
5985 static void diff_flush_raw(struct diff_filepair
*p
, struct diff_options
*opt
)
5987 int line_termination
= opt
->line_termination
;
5988 int inter_name_termination
= line_termination
? '\t' : '\0';
5990 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
5991 if (!(opt
->output_format
& DIFF_FORMAT_NAME_STATUS
)) {
5992 fprintf(opt
->file
, ":%06o %06o %s ", p
->one
->mode
, p
->two
->mode
,
5993 diff_aligned_abbrev(&p
->one
->oid
, opt
->abbrev
));
5994 fprintf(opt
->file
, "%s ",
5995 diff_aligned_abbrev(&p
->two
->oid
, opt
->abbrev
));
5998 fprintf(opt
->file
, "%c%03d%c", p
->status
, similarity_index(p
),
5999 inter_name_termination
);
6001 fprintf(opt
->file
, "%c%c", p
->status
, inter_name_termination
);
6004 if (p
->status
== DIFF_STATUS_COPIED
||
6005 p
->status
== DIFF_STATUS_RENAMED
) {
6006 const char *name_a
, *name_b
;
6007 name_a
= p
->one
->path
;
6008 name_b
= p
->two
->path
;
6009 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6010 write_name_quoted(name_a
, opt
->file
, inter_name_termination
);
6011 write_name_quoted(name_b
, opt
->file
, line_termination
);
6013 const char *name_a
, *name_b
;
6014 name_a
= p
->one
->mode
? p
->one
->path
: p
->two
->path
;
6016 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6017 write_name_quoted(name_a
, opt
->file
, line_termination
);
6021 int diff_unmodified_pair(struct diff_filepair
*p
)
6023 /* This function is written stricter than necessary to support
6024 * the currently implemented transformers, but the idea is to
6025 * let transformers to produce diff_filepairs any way they want,
6026 * and filter and clean them up here before producing the output.
6028 struct diff_filespec
*one
= p
->one
, *two
= p
->two
;
6030 if (DIFF_PAIR_UNMERGED(p
))
6031 return 0; /* unmerged is interesting */
6033 /* deletion, addition, mode or type change
6034 * and rename are all interesting.
6036 if (DIFF_FILE_VALID(one
) != DIFF_FILE_VALID(two
) ||
6037 DIFF_PAIR_MODE_CHANGED(p
) ||
6038 strcmp(one
->path
, two
->path
))
6041 /* both are valid and point at the same path. that is, we are
6042 * dealing with a change.
6044 if (one
->oid_valid
&& two
->oid_valid
&&
6045 oideq(&one
->oid
, &two
->oid
) &&
6046 !one
->dirty_submodule
&& !two
->dirty_submodule
)
6047 return 1; /* no change */
6048 if (!one
->oid_valid
&& !two
->oid_valid
)
6049 return 1; /* both look at the same file on the filesystem. */
6053 static void diff_flush_patch(struct diff_filepair
*p
, struct diff_options
*o
)
6055 int include_conflict_headers
=
6056 (additional_headers(o
, p
->one
->path
) &&
6058 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
6061 * Check if we can return early without showing a diff. Note that
6062 * diff_filepair only stores {oid, path, mode, is_valid}
6063 * information for each path, and thus diff_unmodified_pair() only
6064 * considers those bits of info. However, we do not want pairs
6065 * created by create_filepairs_for_header_only_notifications()
6066 * (which always look like unmodified pairs) to be ignored, so
6067 * return early if both p is unmodified AND we don't want to
6068 * include_conflict_headers.
6070 if (diff_unmodified_pair(p
) && !include_conflict_headers
)
6073 /* Actually, we can also return early to avoid showing tree diffs */
6074 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6075 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6081 static void diff_flush_stat(struct diff_filepair
*p
, struct diff_options
*o
,
6082 struct diffstat_t
*diffstat
)
6084 if (diff_unmodified_pair(p
))
6087 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6088 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6089 return; /* no useful stat for tree diffs */
6091 run_diffstat(p
, o
, diffstat
);
6094 static void diff_flush_checkdiff(struct diff_filepair
*p
,
6095 struct diff_options
*o
)
6097 if (diff_unmodified_pair(p
))
6100 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6101 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6102 return; /* nothing to check in tree diffs */
6104 run_checkdiff(p
, o
);
6107 int diff_queue_is_empty(struct diff_options
*o
)
6109 struct diff_queue_struct
*q
= &diff_queued_diff
;
6111 int include_conflict_headers
=
6112 (o
->additional_path_headers
&&
6113 strmap_get_size(o
->additional_path_headers
) &&
6115 (!o
->filter
|| filter_bit_tst(DIFF_STATUS_UNMERGED
, o
)));
6117 if (include_conflict_headers
)
6120 for (i
= 0; i
< q
->nr
; i
++)
6121 if (!diff_unmodified_pair(q
->queue
[i
]))
6127 void diff_debug_filespec(struct diff_filespec
*s
, int x
, const char *one
)
6129 fprintf(stderr
, "queue[%d] %s (%s) %s %06o %s\n",
6132 DIFF_FILE_VALID(s
) ? "valid" : "invalid",
6134 s
->oid_valid
? oid_to_hex(&s
->oid
) : "");
6135 fprintf(stderr
, "queue[%d] %s size %lu\n",
6140 void diff_debug_filepair(const struct diff_filepair
*p
, int i
)
6142 diff_debug_filespec(p
->one
, i
, "one");
6143 diff_debug_filespec(p
->two
, i
, "two");
6144 fprintf(stderr
, "score %d, status %c rename_used %d broken %d\n",
6145 p
->score
, p
->status
? p
->status
: '?',
6146 p
->one
->rename_used
, p
->broken_pair
);
6149 void diff_debug_queue(const char *msg
, struct diff_queue_struct
*q
)
6153 fprintf(stderr
, "%s\n", msg
);
6154 fprintf(stderr
, "q->nr = %d\n", q
->nr
);
6155 for (i
= 0; i
< q
->nr
; i
++) {
6156 struct diff_filepair
*p
= q
->queue
[i
];
6157 diff_debug_filepair(p
, i
);
6162 static void diff_resolve_rename_copy(void)
6165 struct diff_filepair
*p
;
6166 struct diff_queue_struct
*q
= &diff_queued_diff
;
6168 diff_debug_queue("resolve-rename-copy", q
);
6170 for (i
= 0; i
< q
->nr
; i
++) {
6172 p
->status
= 0; /* undecided */
6173 if (DIFF_PAIR_UNMERGED(p
))
6174 p
->status
= DIFF_STATUS_UNMERGED
;
6175 else if (!DIFF_FILE_VALID(p
->one
))
6176 p
->status
= DIFF_STATUS_ADDED
;
6177 else if (!DIFF_FILE_VALID(p
->two
))
6178 p
->status
= DIFF_STATUS_DELETED
;
6179 else if (DIFF_PAIR_TYPE_CHANGED(p
))
6180 p
->status
= DIFF_STATUS_TYPE_CHANGED
;
6182 /* from this point on, we are dealing with a pair
6183 * whose both sides are valid and of the same type, i.e.
6184 * either in-place edit or rename/copy edit.
6186 else if (DIFF_PAIR_RENAME(p
)) {
6188 * A rename might have re-connected a broken
6189 * pair up, causing the pathnames to be the
6190 * same again. If so, that's not a rename at
6191 * all, just a modification..
6193 * Otherwise, see if this source was used for
6194 * multiple renames, in which case we decrement
6195 * the count, and call it a copy.
6197 if (!strcmp(p
->one
->path
, p
->two
->path
))
6198 p
->status
= DIFF_STATUS_MODIFIED
;
6199 else if (--p
->one
->rename_used
> 0)
6200 p
->status
= DIFF_STATUS_COPIED
;
6202 p
->status
= DIFF_STATUS_RENAMED
;
6204 else if (!oideq(&p
->one
->oid
, &p
->two
->oid
) ||
6205 p
->one
->mode
!= p
->two
->mode
||
6206 p
->one
->dirty_submodule
||
6207 p
->two
->dirty_submodule
||
6208 is_null_oid(&p
->one
->oid
))
6209 p
->status
= DIFF_STATUS_MODIFIED
;
6211 /* This is a "no-change" entry and should not
6212 * happen anymore, but prepare for broken callers.
6214 error("feeding unmodified %s to diffcore",
6216 p
->status
= DIFF_STATUS_UNKNOWN
;
6219 diff_debug_queue("resolve-rename-copy done", q
);
6222 static int check_pair_status(struct diff_filepair
*p
)
6224 switch (p
->status
) {
6225 case DIFF_STATUS_UNKNOWN
:
6228 die("internal error in diff-resolve-rename-copy");
6234 static void flush_one_pair(struct diff_filepair
*p
, struct diff_options
*opt
)
6236 int fmt
= opt
->output_format
;
6238 if (fmt
& DIFF_FORMAT_CHECKDIFF
)
6239 diff_flush_checkdiff(p
, opt
);
6240 else if (fmt
& (DIFF_FORMAT_RAW
| DIFF_FORMAT_NAME_STATUS
))
6241 diff_flush_raw(p
, opt
);
6242 else if (fmt
& DIFF_FORMAT_NAME
) {
6243 const char *name_a
, *name_b
;
6244 name_a
= p
->two
->path
;
6246 strip_prefix(opt
->prefix_length
, &name_a
, &name_b
);
6247 fprintf(opt
->file
, "%s", diff_line_prefix(opt
));
6248 write_name_quoted(name_a
, opt
->file
, opt
->line_termination
);
6251 opt
->found_changes
= 1;
6254 static void show_file_mode_name(struct diff_options
*opt
, const char *newdelete
, struct diff_filespec
*fs
)
6256 struct strbuf sb
= STRBUF_INIT
;
6258 strbuf_addf(&sb
, " %s mode %06o ", newdelete
, fs
->mode
);
6260 strbuf_addf(&sb
, " %s ", newdelete
);
6262 quote_c_style(fs
->path
, &sb
, NULL
, 0);
6263 strbuf_addch(&sb
, '\n');
6264 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6266 strbuf_release(&sb
);
6269 static void show_mode_change(struct diff_options
*opt
, struct diff_filepair
*p
,
6272 if (p
->one
->mode
&& p
->two
->mode
&& p
->one
->mode
!= p
->two
->mode
) {
6273 struct strbuf sb
= STRBUF_INIT
;
6274 strbuf_addf(&sb
, " mode change %06o => %06o",
6275 p
->one
->mode
, p
->two
->mode
);
6277 strbuf_addch(&sb
, ' ');
6278 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6280 strbuf_addch(&sb
, '\n');
6281 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6283 strbuf_release(&sb
);
6287 static void show_rename_copy(struct diff_options
*opt
, const char *renamecopy
,
6288 struct diff_filepair
*p
)
6290 struct strbuf sb
= STRBUF_INIT
;
6291 struct strbuf names
= STRBUF_INIT
;
6293 pprint_rename(&names
, p
->one
->path
, p
->two
->path
);
6294 strbuf_addf(&sb
, " %s %s (%d%%)\n",
6295 renamecopy
, names
.buf
, similarity_index(p
));
6296 strbuf_release(&names
);
6297 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6299 show_mode_change(opt
, p
, 0);
6300 strbuf_release(&sb
);
6303 static void diff_summary(struct diff_options
*opt
, struct diff_filepair
*p
)
6306 case DIFF_STATUS_DELETED
:
6307 show_file_mode_name(opt
, "delete", p
->one
);
6309 case DIFF_STATUS_ADDED
:
6310 show_file_mode_name(opt
, "create", p
->two
);
6312 case DIFF_STATUS_COPIED
:
6313 show_rename_copy(opt
, "copy", p
);
6315 case DIFF_STATUS_RENAMED
:
6316 show_rename_copy(opt
, "rename", p
);
6320 struct strbuf sb
= STRBUF_INIT
;
6321 strbuf_addstr(&sb
, " rewrite ");
6322 quote_c_style(p
->two
->path
, &sb
, NULL
, 0);
6323 strbuf_addf(&sb
, " (%d%%)\n", similarity_index(p
));
6324 emit_diff_symbol(opt
, DIFF_SYMBOL_SUMMARY
,
6326 strbuf_release(&sb
);
6328 show_mode_change(opt
, p
, !p
->score
);
6338 static int remove_space(char *line
, int len
)
6344 for (i
= 0; i
< len
; i
++)
6345 if (!isspace((c
= line
[i
])))
6351 void flush_one_hunk(struct object_id
*result
, git_hash_ctx
*ctx
)
6353 unsigned char hash
[GIT_MAX_RAWSZ
];
6354 unsigned short carry
= 0;
6357 the_hash_algo
->final_fn(hash
, ctx
);
6358 the_hash_algo
->init_fn(ctx
);
6359 /* 20-byte sum, with carry */
6360 for (i
= 0; i
< the_hash_algo
->rawsz
; ++i
) {
6361 carry
+= result
->hash
[i
] + hash
[i
];
6362 result
->hash
[i
] = carry
;
6367 static int patch_id_consume(void *priv
, char *line
, unsigned long len
)
6369 struct patch_id_t
*data
= priv
;
6372 if (len
> 12 && starts_with(line
, "\\ "))
6374 new_len
= remove_space(line
, len
);
6376 the_hash_algo
->update_fn(data
->ctx
, line
, new_len
);
6377 data
->patchlen
+= new_len
;
6381 static void patch_id_add_string(git_hash_ctx
*ctx
, const char *str
)
6383 the_hash_algo
->update_fn(ctx
, str
, strlen(str
));
6386 static void patch_id_add_mode(git_hash_ctx
*ctx
, unsigned mode
)
6388 /* large enough for 2^32 in octal */
6390 int len
= xsnprintf(buf
, sizeof(buf
), "%06o", mode
);
6391 the_hash_algo
->update_fn(ctx
, buf
, len
);
6394 /* returns 0 upon success, and writes result into oid */
6395 static int diff_get_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
6397 struct diff_queue_struct
*q
= &diff_queued_diff
;
6400 struct patch_id_t data
;
6402 the_hash_algo
->init_fn(&ctx
);
6403 memset(&data
, 0, sizeof(struct patch_id_t
));
6407 for (i
= 0; i
< q
->nr
; i
++) {
6411 struct diff_filepair
*p
= q
->queue
[i
];
6414 memset(&xpp
, 0, sizeof(xpp
));
6415 memset(&xecfg
, 0, sizeof(xecfg
));
6417 return error("internal diff status error");
6418 if (p
->status
== DIFF_STATUS_UNKNOWN
)
6420 if (diff_unmodified_pair(p
))
6422 if ((DIFF_FILE_VALID(p
->one
) && S_ISDIR(p
->one
->mode
)) ||
6423 (DIFF_FILE_VALID(p
->two
) && S_ISDIR(p
->two
->mode
)))
6425 if (DIFF_PAIR_UNMERGED(p
))
6428 diff_fill_oid_info(p
->one
, options
->repo
->index
);
6429 diff_fill_oid_info(p
->two
, options
->repo
->index
);
6431 len1
= remove_space(p
->one
->path
, strlen(p
->one
->path
));
6432 len2
= remove_space(p
->two
->path
, strlen(p
->two
->path
));
6433 patch_id_add_string(&ctx
, "diff--git");
6434 patch_id_add_string(&ctx
, "a/");
6435 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6436 patch_id_add_string(&ctx
, "b/");
6437 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6439 if (p
->one
->mode
== 0) {
6440 patch_id_add_string(&ctx
, "newfilemode");
6441 patch_id_add_mode(&ctx
, p
->two
->mode
);
6442 } else if (p
->two
->mode
== 0) {
6443 patch_id_add_string(&ctx
, "deletedfilemode");
6444 patch_id_add_mode(&ctx
, p
->one
->mode
);
6445 } else if (p
->one
->mode
!= p
->two
->mode
) {
6446 patch_id_add_string(&ctx
, "oldmode");
6447 patch_id_add_mode(&ctx
, p
->one
->mode
);
6448 patch_id_add_string(&ctx
, "newmode");
6449 patch_id_add_mode(&ctx
, p
->two
->mode
);
6452 if (diff_header_only
) {
6453 /* don't do anything since we're only populating header info */
6454 } else if (diff_filespec_is_binary(options
->repo
, p
->one
) ||
6455 diff_filespec_is_binary(options
->repo
, p
->two
)) {
6456 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->one
->oid
),
6457 the_hash_algo
->hexsz
);
6458 the_hash_algo
->update_fn(&ctx
, oid_to_hex(&p
->two
->oid
),
6459 the_hash_algo
->hexsz
);
6461 if (p
->one
->mode
== 0) {
6462 patch_id_add_string(&ctx
, "---/dev/null");
6463 patch_id_add_string(&ctx
, "+++b/");
6464 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6465 } else if (p
->two
->mode
== 0) {
6466 patch_id_add_string(&ctx
, "---a/");
6467 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6468 patch_id_add_string(&ctx
, "+++/dev/null");
6470 patch_id_add_string(&ctx
, "---a/");
6471 the_hash_algo
->update_fn(&ctx
, p
->one
->path
, len1
);
6472 patch_id_add_string(&ctx
, "+++b/");
6473 the_hash_algo
->update_fn(&ctx
, p
->two
->path
, len2
);
6476 if (fill_mmfile(options
->repo
, &mf1
, p
->one
) < 0 ||
6477 fill_mmfile(options
->repo
, &mf2
, p
->two
) < 0)
6478 return error("unable to read files to diff");
6481 xecfg
.flags
= XDL_EMIT_NO_HUNK_HDR
;
6482 if (xdi_diff_outf(&mf1
, &mf2
, NULL
,
6483 patch_id_consume
, &data
, &xpp
, &xecfg
))
6484 return error("unable to generate patch-id diff for %s",
6487 flush_one_hunk(oid
, &ctx
);
6493 int diff_flush_patch_id(struct diff_options
*options
, struct object_id
*oid
, int diff_header_only
)
6495 struct diff_queue_struct
*q
= &diff_queued_diff
;
6496 int result
= diff_get_patch_id(options
, oid
, diff_header_only
);
6499 DIFF_QUEUE_CLEAR(q
);
6504 static int is_summary_empty(const struct diff_queue_struct
*q
)
6508 for (i
= 0; i
< q
->nr
; i
++) {
6509 const struct diff_filepair
*p
= q
->queue
[i
];
6511 switch (p
->status
) {
6512 case DIFF_STATUS_DELETED
:
6513 case DIFF_STATUS_ADDED
:
6514 case DIFF_STATUS_COPIED
:
6515 case DIFF_STATUS_RENAMED
:
6520 if (p
->one
->mode
&& p
->two
->mode
&&
6521 p
->one
->mode
!= p
->two
->mode
)
6529 static const char rename_limit_warning
[] =
6530 N_("exhaustive rename detection was skipped due to too many files.");
6532 static const char degrade_cc_to_c_warning
[] =
6533 N_("only found copies from modified paths due to too many files.");
6535 static const char rename_limit_advice
[] =
6536 N_("you may want to set your %s variable to at least "
6537 "%d and retry the command.");
6539 void diff_warn_rename_limit(const char *varname
, int needed
, int degraded_cc
)
6543 warning(_(degrade_cc_to_c_warning
));
6545 warning(_(rename_limit_warning
));
6549 warning(_(rename_limit_advice
), varname
, needed
);
6552 static void create_filepairs_for_header_only_notifications(struct diff_options
*o
)
6554 struct strset present
;
6555 struct diff_queue_struct
*q
= &diff_queued_diff
;
6556 struct hashmap_iter iter
;
6557 struct strmap_entry
*e
;
6560 strset_init_with_options(&present
, /*pool*/ NULL
, /*strdup*/ 0);
6563 * Find out which paths exist in diff_queued_diff, preferring
6564 * one->path for any pair that has multiple paths.
6566 for (i
= 0; i
< q
->nr
; i
++) {
6567 struct diff_filepair
*p
= q
->queue
[i
];
6568 char *path
= p
->one
->path
? p
->one
->path
: p
->two
->path
;
6570 if (strmap_contains(o
->additional_path_headers
, path
))
6571 strset_add(&present
, path
);
6575 * Loop over paths in additional_path_headers; for each NOT already
6576 * in diff_queued_diff, create a synthetic filepair and insert that
6577 * into diff_queued_diff.
6579 strmap_for_each_entry(o
->additional_path_headers
, &iter
, e
) {
6580 if (!strset_contains(&present
, e
->key
)) {
6581 struct diff_filespec
*one
, *two
;
6582 struct diff_filepair
*p
;
6584 one
= alloc_filespec(e
->key
);
6585 two
= alloc_filespec(e
->key
);
6586 fill_filespec(one
, null_oid(), 0, 0);
6587 fill_filespec(two
, null_oid(), 0, 0);
6588 p
= diff_queue(q
, one
, two
);
6589 p
->status
= DIFF_STATUS_MODIFIED
;
6593 /* Re-sort the filepairs */
6594 diffcore_fix_diff_index();
6597 strset_clear(&present
);
6600 static void diff_flush_patch_all_file_pairs(struct diff_options
*o
)
6603 static struct emitted_diff_symbols esm
= EMITTED_DIFF_SYMBOLS_INIT
;
6604 struct diff_queue_struct
*q
= &diff_queued_diff
;
6606 if (WSEH_NEW
& WS_RULE_MASK
)
6607 BUG("WS rules bit mask overlaps with diff symbol flags");
6610 o
->emitted_symbols
= &esm
;
6612 if (o
->additional_path_headers
)
6613 create_filepairs_for_header_only_notifications(o
);
6615 for (i
= 0; i
< q
->nr
; i
++) {
6616 struct diff_filepair
*p
= q
->queue
[i
];
6617 if (check_pair_status(p
))
6618 diff_flush_patch(p
, o
);
6621 if (o
->emitted_symbols
) {
6622 if (o
->color_moved
) {
6623 struct mem_pool entry_pool
;
6624 struct moved_entry_list
*entry_list
;
6626 mem_pool_init(&entry_pool
, 1024 * 1024);
6627 entry_list
= add_lines_to_move_detection(o
,
6629 mark_color_as_moved(o
, entry_list
);
6630 if (o
->color_moved
== COLOR_MOVED_ZEBRA_DIM
)
6633 mem_pool_discard(&entry_pool
, 0);
6637 for (i
= 0; i
< esm
.nr
; i
++)
6638 emit_diff_symbol_from_struct(o
, &esm
.buf
[i
]);
6640 for (i
= 0; i
< esm
.nr
; i
++)
6641 free((void *)esm
.buf
[i
].line
);
6644 o
->emitted_symbols
= NULL
;
6648 static void diff_free_file(struct diff_options
*options
)
6650 if (options
->close_file
)
6651 fclose(options
->file
);
6654 static void diff_free_ignore_regex(struct diff_options
*options
)
6658 for (i
= 0; i
< options
->ignore_regex_nr
; i
++) {
6659 regfree(options
->ignore_regex
[i
]);
6660 free(options
->ignore_regex
[i
]);
6662 free(options
->ignore_regex
);
6665 void diff_free(struct diff_options
*options
)
6667 if (options
->no_free
)
6670 diff_free_file(options
);
6671 diff_free_ignore_regex(options
);
6672 clear_pathspec(&options
->pathspec
);
6675 void diff_flush(struct diff_options
*options
)
6677 struct diff_queue_struct
*q
= &diff_queued_diff
;
6678 int i
, output_format
= options
->output_format
;
6680 int dirstat_by_line
= 0;
6683 * Order: raw, stat, summary, patch
6684 * or: name/name-status/checkdiff (other bits clear)
6686 if (!q
->nr
&& !options
->additional_path_headers
)
6689 if (output_format
& (DIFF_FORMAT_RAW
|
6691 DIFF_FORMAT_NAME_STATUS
|
6692 DIFF_FORMAT_CHECKDIFF
)) {
6693 for (i
= 0; i
< q
->nr
; i
++) {
6694 struct diff_filepair
*p
= q
->queue
[i
];
6695 if (check_pair_status(p
))
6696 flush_one_pair(p
, options
);
6701 if (output_format
& DIFF_FORMAT_DIRSTAT
&& options
->flags
.dirstat_by_line
)
6702 dirstat_by_line
= 1;
6704 if (output_format
& (DIFF_FORMAT_DIFFSTAT
|DIFF_FORMAT_SHORTSTAT
|DIFF_FORMAT_NUMSTAT
) ||
6706 struct diffstat_t diffstat
;
6708 compute_diffstat(options
, &diffstat
, q
);
6709 if (output_format
& DIFF_FORMAT_NUMSTAT
)
6710 show_numstat(&diffstat
, options
);
6711 if (output_format
& DIFF_FORMAT_DIFFSTAT
)
6712 show_stats(&diffstat
, options
);
6713 if (output_format
& DIFF_FORMAT_SHORTSTAT
)
6714 show_shortstats(&diffstat
, options
);
6715 if (output_format
& DIFF_FORMAT_DIRSTAT
&& dirstat_by_line
)
6716 show_dirstat_by_line(&diffstat
, options
);
6717 free_diffstat_info(&diffstat
);
6720 if ((output_format
& DIFF_FORMAT_DIRSTAT
) && !dirstat_by_line
)
6721 show_dirstat(options
);
6723 if (output_format
& DIFF_FORMAT_SUMMARY
&& !is_summary_empty(q
)) {
6724 for (i
= 0; i
< q
->nr
; i
++) {
6725 diff_summary(options
, q
->queue
[i
]);
6730 if (output_format
& DIFF_FORMAT_PATCH
) {
6732 emit_diff_symbol(options
, DIFF_SYMBOL_SEPARATOR
, NULL
, 0, 0);
6733 if (options
->stat_sep
)
6734 /* attach patch instead of inline */
6735 emit_diff_symbol(options
, DIFF_SYMBOL_STAT_SEP
,
6739 diff_flush_patch_all_file_pairs(options
);
6742 if (output_format
& DIFF_FORMAT_CALLBACK
)
6743 options
->format_callback(q
, options
, options
->format_callback_data
);
6745 if (output_format
& DIFF_FORMAT_NO_OUTPUT
&&
6746 options
->flags
.exit_with_status
&&
6747 options
->flags
.diff_from_contents
) {
6749 * run diff_flush_patch for the exit status. setting
6750 * options->file to /dev/null should be safe, because we
6751 * aren't supposed to produce any output anyway.
6753 diff_free_file(options
);
6754 options
->file
= xfopen("/dev/null", "w");
6755 options
->close_file
= 1;
6756 options
->color_moved
= 0;
6757 for (i
= 0; i
< q
->nr
; i
++) {
6758 struct diff_filepair
*p
= q
->queue
[i
];
6759 if (check_pair_status(p
))
6760 diff_flush_patch(p
, options
);
6761 if (options
->found_changes
)
6768 DIFF_QUEUE_CLEAR(q
);
6772 * Report the content-level differences with HAS_CHANGES;
6773 * diff_addremove/diff_change does not set the bit when
6774 * DIFF_FROM_CONTENTS is in effect (e.g. with -w).
6776 if (options
->flags
.diff_from_contents
) {
6777 if (options
->found_changes
)
6778 options
->flags
.has_changes
= 1;
6780 options
->flags
.has_changes
= 0;
6784 static int match_filter(const struct diff_options
*options
, const struct diff_filepair
*p
)
6786 return (((p
->status
== DIFF_STATUS_MODIFIED
) &&
6788 filter_bit_tst(DIFF_STATUS_FILTER_BROKEN
, options
)) ||
6790 filter_bit_tst(DIFF_STATUS_MODIFIED
, options
)))) ||
6791 ((p
->status
!= DIFF_STATUS_MODIFIED
) &&
6792 filter_bit_tst(p
->status
, options
)));
6795 static void diffcore_apply_filter(struct diff_options
*options
)
6798 struct diff_queue_struct
*q
= &diff_queued_diff
;
6799 struct diff_queue_struct outq
;
6801 DIFF_QUEUE_CLEAR(&outq
);
6803 if (!options
->filter
)
6806 if (filter_bit_tst(DIFF_STATUS_FILTER_AON
, options
)) {
6808 for (i
= found
= 0; !found
&& i
< q
->nr
; i
++) {
6809 if (match_filter(options
, q
->queue
[i
]))
6815 /* otherwise we will clear the whole queue
6816 * by copying the empty outq at the end of this
6817 * function, but first clear the current entries
6820 for (i
= 0; i
< q
->nr
; i
++)
6821 diff_free_filepair(q
->queue
[i
]);
6824 /* Only the matching ones */
6825 for (i
= 0; i
< q
->nr
; i
++) {
6826 struct diff_filepair
*p
= q
->queue
[i
];
6827 if (match_filter(options
, p
))
6830 diff_free_filepair(p
);
6837 /* Check whether two filespecs with the same mode and size are identical */
6838 static int diff_filespec_is_identical(struct repository
*r
,
6839 struct diff_filespec
*one
,
6840 struct diff_filespec
*two
)
6842 if (S_ISGITLINK(one
->mode
))
6844 if (diff_populate_filespec(r
, one
, NULL
))
6846 if (diff_populate_filespec(r
, two
, NULL
))
6848 return !memcmp(one
->data
, two
->data
, one
->size
);
6851 static int diff_filespec_check_stat_unmatch(struct repository
*r
,
6852 struct diff_filepair
*p
)
6854 struct diff_populate_filespec_options dpf_options
= {
6855 .check_size_only
= 1,
6856 .missing_object_cb
= diff_queued_diff_prefetch
,
6857 .missing_object_data
= r
,
6860 if (p
->done_skip_stat_unmatch
)
6861 return p
->skip_stat_unmatch_result
;
6863 p
->done_skip_stat_unmatch
= 1;
6864 p
->skip_stat_unmatch_result
= 0;
6866 * 1. Entries that come from stat info dirtiness
6867 * always have both sides (iow, not create/delete),
6868 * one side of the object name is unknown, with
6869 * the same mode and size. Keep the ones that
6870 * do not match these criteria. They have real
6873 * 2. At this point, the file is known to be modified,
6874 * with the same mode and size, and the object
6875 * name of one side is unknown. Need to inspect
6876 * the identical contents.
6878 if (!DIFF_FILE_VALID(p
->one
) || /* (1) */
6879 !DIFF_FILE_VALID(p
->two
) ||
6880 (p
->one
->oid_valid
&& p
->two
->oid_valid
) ||
6881 (p
->one
->mode
!= p
->two
->mode
) ||
6882 diff_populate_filespec(r
, p
->one
, &dpf_options
) ||
6883 diff_populate_filespec(r
, p
->two
, &dpf_options
) ||
6884 (p
->one
->size
!= p
->two
->size
) ||
6885 !diff_filespec_is_identical(r
, p
->one
, p
->two
)) /* (2) */
6886 p
->skip_stat_unmatch_result
= 1;
6887 return p
->skip_stat_unmatch_result
;
6890 static void diffcore_skip_stat_unmatch(struct diff_options
*diffopt
)
6893 struct diff_queue_struct
*q
= &diff_queued_diff
;
6894 struct diff_queue_struct outq
;
6895 DIFF_QUEUE_CLEAR(&outq
);
6897 for (i
= 0; i
< q
->nr
; i
++) {
6898 struct diff_filepair
*p
= q
->queue
[i
];
6900 if (diff_filespec_check_stat_unmatch(diffopt
->repo
, p
))
6904 * The caller can subtract 1 from skip_stat_unmatch
6905 * to determine how many paths were dirty only
6906 * due to stat info mismatch.
6908 if (!diffopt
->flags
.no_index
)
6909 diffopt
->skip_stat_unmatch
++;
6910 diff_free_filepair(p
);
6917 static int diffnamecmp(const void *a_
, const void *b_
)
6919 const struct diff_filepair
*a
= *((const struct diff_filepair
**)a_
);
6920 const struct diff_filepair
*b
= *((const struct diff_filepair
**)b_
);
6921 const char *name_a
, *name_b
;
6923 name_a
= a
->one
? a
->one
->path
: a
->two
->path
;
6924 name_b
= b
->one
? b
->one
->path
: b
->two
->path
;
6925 return strcmp(name_a
, name_b
);
6928 void diffcore_fix_diff_index(void)
6930 struct diff_queue_struct
*q
= &diff_queued_diff
;
6931 QSORT(q
->queue
, q
->nr
, diffnamecmp
);
6934 void diff_add_if_missing(struct repository
*r
,
6935 struct oid_array
*to_fetch
,
6936 const struct diff_filespec
*filespec
)
6938 if (filespec
&& filespec
->oid_valid
&&
6939 !S_ISGITLINK(filespec
->mode
) &&
6940 oid_object_info_extended(r
, &filespec
->oid
, NULL
,
6941 OBJECT_INFO_FOR_PREFETCH
))
6942 oid_array_append(to_fetch
, &filespec
->oid
);
6945 void diff_queued_diff_prefetch(void *repository
)
6947 struct repository
*repo
= repository
;
6949 struct diff_queue_struct
*q
= &diff_queued_diff
;
6950 struct oid_array to_fetch
= OID_ARRAY_INIT
;
6952 for (i
= 0; i
< q
->nr
; i
++) {
6953 struct diff_filepair
*p
= q
->queue
[i
];
6954 diff_add_if_missing(repo
, &to_fetch
, p
->one
);
6955 diff_add_if_missing(repo
, &to_fetch
, p
->two
);
6959 * NEEDSWORK: Consider deduplicating the OIDs sent.
6961 promisor_remote_get_direct(repo
, to_fetch
.oid
, to_fetch
.nr
);
6963 oid_array_clear(&to_fetch
);
6966 void init_diffstat_widths(struct diff_options
*options
)
6968 options
->stat_width
= -1; /* use full terminal width */
6969 options
->stat_name_width
= -1; /* respect diff.statNameWidth config */
6970 options
->stat_graph_width
= -1; /* respect diff.statGraphWidth config */
6973 void diffcore_std(struct diff_options
*options
)
6975 int output_formats_to_prefetch
= DIFF_FORMAT_DIFFSTAT
|
6976 DIFF_FORMAT_NUMSTAT
|
6978 DIFF_FORMAT_SHORTSTAT
|
6979 DIFF_FORMAT_DIRSTAT
;
6982 * Check if the user requested a blob-data-requiring diff output and/or
6983 * break-rewrite detection (which requires blob data). If yes, prefetch
6986 * If no prefetching occurs, diffcore_rename() will prefetch if it
6987 * decides that it needs inexact rename detection.
6989 if (options
->repo
== the_repository
&& repo_has_promisor_remote(the_repository
) &&
6990 (options
->output_format
& output_formats_to_prefetch
||
6991 options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
))
6992 diff_queued_diff_prefetch(options
->repo
);
6994 /* NOTE please keep the following in sync with diff_tree_combined() */
6995 if (options
->skip_stat_unmatch
)
6996 diffcore_skip_stat_unmatch(options
);
6997 if (!options
->found_follow
) {
6998 /* See try_to_follow_renames() in tree-diff.c */
6999 if (options
->break_opt
!= -1)
7000 diffcore_break(options
->repo
,
7001 options
->break_opt
);
7002 if (options
->detect_rename
)
7003 diffcore_rename(options
);
7004 if (options
->break_opt
!= -1)
7005 diffcore_merge_broken();
7007 if (options
->pickaxe_opts
& DIFF_PICKAXE_KINDS_MASK
)
7008 diffcore_pickaxe(options
);
7009 if (options
->orderfile
)
7010 diffcore_order(options
->orderfile
);
7011 if (options
->rotate_to
)
7012 diffcore_rotate(options
);
7013 if (!options
->found_follow
)
7014 /* See try_to_follow_renames() in tree-diff.c */
7015 diff_resolve_rename_copy();
7016 diffcore_apply_filter(options
);
7018 if (diff_queued_diff
.nr
&& !options
->flags
.diff_from_contents
)
7019 options
->flags
.has_changes
= 1;
7021 options
->flags
.has_changes
= 0;
7023 options
->found_follow
= 0;
7026 int diff_result_code(struct diff_options
*opt
)
7030 diff_warn_rename_limit("diff.renameLimit",
7031 opt
->needed_rename_limit
,
7032 opt
->degraded_cc_to_c
);
7034 if (opt
->flags
.exit_with_status
&&
7035 opt
->flags
.has_changes
)
7037 if ((opt
->output_format
& DIFF_FORMAT_CHECKDIFF
) &&
7038 opt
->flags
.check_failed
)
7043 int diff_can_quit_early(struct diff_options
*opt
)
7045 return (opt
->flags
.quick
&&
7047 opt
->flags
.has_changes
);
7051 * Shall changes to this submodule be ignored?
7053 * Submodule changes can be configured to be ignored separately for each path,
7054 * but that configuration can be overridden from the command line.
7056 static int is_submodule_ignored(const char *path
, struct diff_options
*options
)
7059 struct diff_flags orig_flags
= options
->flags
;
7060 if (!options
->flags
.override_submodule_config
)
7061 set_diffopt_flags_from_submodule_config(options
, path
);
7062 if (options
->flags
.ignore_submodules
)
7064 options
->flags
= orig_flags
;
7068 void compute_diffstat(struct diff_options
*options
,
7069 struct diffstat_t
*diffstat
,
7070 struct diff_queue_struct
*q
)
7074 memset(diffstat
, 0, sizeof(struct diffstat_t
));
7075 for (i
= 0; i
< q
->nr
; i
++) {
7076 struct diff_filepair
*p
= q
->queue
[i
];
7077 if (check_pair_status(p
))
7078 diff_flush_stat(p
, options
, diffstat
);
7080 options
->found_changes
= !!diffstat
->nr
;
7083 void diff_addremove(struct diff_options
*options
,
7084 int addremove
, unsigned mode
,
7085 const struct object_id
*oid
,
7087 const char *concatpath
, unsigned dirty_submodule
)
7089 struct diff_filespec
*one
, *two
;
7091 if (S_ISGITLINK(mode
) && is_submodule_ignored(concatpath
, options
))
7094 /* This may look odd, but it is a preparation for
7095 * feeding "there are unchanged files which should
7096 * not produce diffs, but when you are doing copy
7097 * detection you would need them, so here they are"
7098 * entries to the diff-core. They will be prefixed
7099 * with something like '=' or '*' (I haven't decided
7100 * which but should not make any difference).
7101 * Feeding the same new and old to diff_change()
7102 * also has the same effect.
7103 * Before the final output happens, they are pruned after
7104 * merged into rename/copy pairs as appropriate.
7106 if (options
->flags
.reverse_diff
)
7107 addremove
= (addremove
== '+' ? '-' :
7108 addremove
== '-' ? '+' : addremove
);
7110 if (options
->prefix
&&
7111 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
7114 one
= alloc_filespec(concatpath
);
7115 two
= alloc_filespec(concatpath
);
7117 if (addremove
!= '+')
7118 fill_filespec(one
, oid
, oid_valid
, mode
);
7119 if (addremove
!= '-') {
7120 fill_filespec(two
, oid
, oid_valid
, mode
);
7121 two
->dirty_submodule
= dirty_submodule
;
7124 diff_queue(&diff_queued_diff
, one
, two
);
7125 if (!options
->flags
.diff_from_contents
)
7126 options
->flags
.has_changes
= 1;
7129 void diff_change(struct diff_options
*options
,
7130 unsigned old_mode
, unsigned new_mode
,
7131 const struct object_id
*old_oid
,
7132 const struct object_id
*new_oid
,
7133 int old_oid_valid
, int new_oid_valid
,
7134 const char *concatpath
,
7135 unsigned old_dirty_submodule
, unsigned new_dirty_submodule
)
7137 struct diff_filespec
*one
, *two
;
7138 struct diff_filepair
*p
;
7140 if (S_ISGITLINK(old_mode
) && S_ISGITLINK(new_mode
) &&
7141 is_submodule_ignored(concatpath
, options
))
7144 if (options
->flags
.reverse_diff
) {
7145 SWAP(old_mode
, new_mode
);
7146 SWAP(old_oid
, new_oid
);
7147 SWAP(old_oid_valid
, new_oid_valid
);
7148 SWAP(old_dirty_submodule
, new_dirty_submodule
);
7151 if (options
->prefix
&&
7152 strncmp(concatpath
, options
->prefix
, options
->prefix_length
))
7155 one
= alloc_filespec(concatpath
);
7156 two
= alloc_filespec(concatpath
);
7157 fill_filespec(one
, old_oid
, old_oid_valid
, old_mode
);
7158 fill_filespec(two
, new_oid
, new_oid_valid
, new_mode
);
7159 one
->dirty_submodule
= old_dirty_submodule
;
7160 two
->dirty_submodule
= new_dirty_submodule
;
7161 p
= diff_queue(&diff_queued_diff
, one
, two
);
7163 if (options
->flags
.diff_from_contents
)
7166 if (options
->flags
.quick
&& options
->skip_stat_unmatch
&&
7167 !diff_filespec_check_stat_unmatch(options
->repo
, p
)) {
7168 diff_free_filespec_data(p
->one
);
7169 diff_free_filespec_data(p
->two
);
7173 options
->flags
.has_changes
= 1;
7176 struct diff_filepair
*diff_unmerge(struct diff_options
*options
, const char *path
)
7178 struct diff_filepair
*pair
;
7179 struct diff_filespec
*one
, *two
;
7181 if (options
->prefix
&&
7182 strncmp(path
, options
->prefix
, options
->prefix_length
))
7185 one
= alloc_filespec(path
);
7186 two
= alloc_filespec(path
);
7187 pair
= diff_queue(&diff_queued_diff
, one
, two
);
7188 pair
->is_unmerged
= 1;
7192 static char *run_textconv(struct repository
*r
,
7194 struct diff_filespec
*spec
,
7197 struct diff_tempfile
*temp
;
7198 struct child_process child
= CHILD_PROCESS_INIT
;
7199 struct strbuf buf
= STRBUF_INIT
;
7202 temp
= prepare_temp_file(r
, spec
);
7203 strvec_push(&child
.args
, pgm
);
7204 strvec_push(&child
.args
, temp
->name
);
7206 child
.use_shell
= 1;
7208 if (start_command(&child
)) {
7213 if (strbuf_read(&buf
, child
.out
, 0) < 0)
7214 err
= error("error reading from textconv command '%s'", pgm
);
7217 if (finish_command(&child
) || err
) {
7218 strbuf_release(&buf
);
7224 return strbuf_detach(&buf
, outsize
);
7227 size_t fill_textconv(struct repository
*r
,
7228 struct userdiff_driver
*driver
,
7229 struct diff_filespec
*df
,
7235 if (!DIFF_FILE_VALID(df
)) {
7239 if (diff_populate_filespec(r
, df
, NULL
))
7240 die("unable to read files to diff");
7245 if (!driver
->textconv
)
7246 BUG("fill_textconv called with non-textconv driver");
7248 if (driver
->textconv_cache
&& df
->oid_valid
) {
7249 *outbuf
= notes_cache_get(driver
->textconv_cache
,
7256 *outbuf
= run_textconv(r
, driver
->textconv
, df
, &size
);
7258 die("unable to read files to diff");
7260 if (driver
->textconv_cache
&& df
->oid_valid
) {
7261 /* ignore errors, as we might be in a readonly repository */
7262 notes_cache_put(driver
->textconv_cache
, &df
->oid
, *outbuf
,
7265 * we could save up changes and flush them all at the end,
7266 * but we would need an extra call after all diffing is done.
7267 * Since generating a cache entry is the slow path anyway,
7268 * this extra overhead probably isn't a big deal.
7270 notes_cache_write(driver
->textconv_cache
);
7276 int textconv_object(struct repository
*r
,
7279 const struct object_id
*oid
,
7282 unsigned long *buf_size
)
7284 struct diff_filespec
*df
;
7285 struct userdiff_driver
*textconv
;
7287 df
= alloc_filespec(path
);
7288 fill_filespec(df
, oid
, oid_valid
, mode
);
7289 textconv
= get_textconv(r
, df
);
7295 *buf_size
= fill_textconv(r
, textconv
, df
, buf
);
7300 void setup_diff_pager(struct diff_options
*opt
)
7303 * If the user asked for our exit code, then either they want --quiet
7304 * or --exit-code. We should definitely not bother with a pager in the
7305 * former case, as we will generate no output. Since we still properly
7306 * report our exit code even when a pager is run, we _could_ run a
7307 * pager with --exit-code. But since we have not done so historically,
7308 * and because it is easy to find people oneline advising "git diff
7309 * --exit-code" in hooks and other scripts, we do not do so.
7311 if (!opt
->flags
.exit_with_status
&&
7312 check_pager_config("diff") != 0)